1
|
|
|
// Atto recordrtc library functions. |
2
|
|
|
// @package atto_recordrtc. |
3
|
|
|
// @author Jesus Federico (jesus [at] blindsidenetworks [dt] com). |
4
|
|
|
// @copyright 2016 to present, Blindside Networks Inc. |
5
|
|
|
// @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. |
6
|
|
|
|
7
|
|
|
/*jshint es5: true */ |
8
|
|
|
/*jshint onevar: false */ |
9
|
|
|
/*jshint shadow: true */ |
10
|
|
|
/*global M */ |
11
|
|
|
/*global MediaRecorder */ |
12
|
|
|
/*global URL */ |
13
|
|
|
/*global InstallTrigger */ |
14
|
|
|
|
15
|
|
|
M.atto_recordrtc = M.atto_recordrtc || {}; |
|
|
|
|
16
|
|
|
|
17
|
|
|
// Shorten access to M.atto_recordrtc.commonmodule namespace. |
18
|
|
|
var cm = M.atto_recordrtc.commonmodule; |
19
|
|
|
|
20
|
|
|
M.atto_recordrtc.audiomodule = { |
21
|
|
|
init: function(scope) { |
22
|
|
|
// Assignment of global variables. |
23
|
|
|
cm.editorScope = scope; // Allows access to the editor's "this" context. |
24
|
|
|
cm.alertWarning = Y.one('div#alert-warning'); |
|
|
|
|
25
|
|
|
cm.alertDanger = Y.one('div#alert-danger'); |
26
|
|
|
cm.player = Y.one('audio#player'); |
27
|
|
|
cm.playerDOM = document.querySelector('audio#player'); |
28
|
|
|
cm.startStopBtn = Y.one('button#start-stop'); |
29
|
|
|
cm.uploadBtn = Y.one('button#upload'); |
30
|
|
|
cm.recType = 'audio'; |
31
|
|
|
cm.olderMoodle = scope.get('oldermoodle'); |
32
|
|
|
// Extract the numbers from the string, and convert to bytes. |
33
|
|
|
cm.maxUploadSize = parseInt(scope.get('maxrecsize').match(/\d+/)[0], 10) * Math.pow(1024, 2); |
34
|
|
|
|
35
|
|
|
// Show alert and redirect user if connection is not secure. |
36
|
|
|
cm.check_secure(); |
37
|
|
|
// Show alert if using non-ideal browser. |
38
|
|
|
cm.check_browser(); |
39
|
|
|
|
40
|
|
|
// Run when user clicks on "record" button. |
41
|
|
|
cm.startStopBtn.on('click', function() { |
42
|
|
|
cm.startStopBtn.set('disabled', true); |
43
|
|
|
|
44
|
|
|
// If button is displaying "Start Recording" or "Record Again". |
45
|
|
|
if ((cm.startStopBtn.get('textContent') === M.util.get_string('startrecording', 'atto_recordrtc')) || |
46
|
|
|
(cm.startStopBtn.get('textContent') === M.util.get_string('recordagain', 'atto_recordrtc')) || |
47
|
|
|
(cm.startStopBtn.get('textContent') === M.util.get_string('recordingfailed', 'atto_recordrtc'))) { |
48
|
|
|
// Make sure the audio player and upload button are not shown. |
49
|
|
|
cm.player.ancestor().ancestor().addClass('hide'); |
50
|
|
|
cm.uploadBtn.ancestor().ancestor().addClass('hide'); |
51
|
|
|
|
52
|
|
|
// Change look of recording button. |
53
|
|
|
if (!cm.olderMoodle) { |
54
|
|
|
cm.startStopBtn.replaceClass('btn-outline-danger', 'btn-danger'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Empty the array containing the previously recorded chunks. |
58
|
|
|
cm.chunks = []; |
59
|
|
|
cm.blobSize = 0; |
60
|
|
|
|
61
|
|
|
// Initialize common configurations. |
62
|
|
|
var commonConfig = { |
63
|
|
|
// When the stream is captured from the microphone/webcam. |
64
|
|
|
onMediaCaptured: function(stream) { |
65
|
|
|
// Make audio stream available at a higher level by making it a property of the common module. |
66
|
|
|
cm.stream = stream; |
67
|
|
|
|
68
|
|
|
cm.start_recording(cm.recType, cm.stream); |
69
|
|
|
}, |
70
|
|
|
|
71
|
|
|
// Revert button to "Record Again" when recording is stopped. |
72
|
|
|
onMediaStopped: function(btnLabel) { |
73
|
|
|
cm.startStopBtn.set('textContent', btnLabel); |
74
|
|
|
}, |
75
|
|
|
|
76
|
|
|
// Handle recording errors. |
77
|
|
|
onMediaCapturingFailed: function(error) { |
78
|
|
|
var btnLabel = null; |
79
|
|
|
|
80
|
|
|
// Handle getUserMedia-thrown errors. |
81
|
|
|
switch (error.name) { |
|
|
|
|
82
|
|
|
case 'AbortError': |
83
|
|
|
Y.use('moodle-core-notification-alert', function() { |
|
|
|
|
84
|
|
|
new M.core.alert({ |
|
|
|
|
85
|
|
|
title: M.util.get_string('gumabort_title', 'atto_recordrtc'), |
86
|
|
|
message: M.util.get_string('gumabort', 'atto_recordrtc') |
87
|
|
|
}); |
88
|
|
|
}); |
89
|
|
|
|
90
|
|
|
btnLabel = M.util.get_string('recordingfailed', 'atto_recordrtc'); |
91
|
|
|
break; |
92
|
|
|
case 'NotAllowedError': |
93
|
|
|
Y.use('moodle-core-notification-alert', function() { |
|
|
|
|
94
|
|
|
new M.core.alert({ |
|
|
|
|
95
|
|
|
title: M.util.get_string('gumnotallowed_title', 'atto_recordrtc'), |
96
|
|
|
message: M.util.get_string('gumnotallowed', 'atto_recordrtc') |
97
|
|
|
}); |
98
|
|
|
}); |
99
|
|
|
|
100
|
|
|
btnLabel = M.util.get_string('recordingfailed', 'atto_recordrtc'); |
101
|
|
|
break; |
102
|
|
|
case 'NotFoundError': |
103
|
|
|
Y.use('moodle-core-notification-alert', function() { |
|
|
|
|
104
|
|
|
new M.core.alert({ |
|
|
|
|
105
|
|
|
title: M.util.get_string('gumnotfound_title', 'atto_recordrtc'), |
106
|
|
|
message: M.util.get_string('gumnotfound', 'atto_recordrtc') |
107
|
|
|
}); |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
btnLabel = M.util.get_string('recordingfailed', 'atto_recordrtc'); |
111
|
|
|
break; |
112
|
|
|
case 'NotReadableError': |
113
|
|
|
Y.use('moodle-core-notification-alert', function() { |
|
|
|
|
114
|
|
|
new M.core.alert({ |
|
|
|
|
115
|
|
|
title: M.util.get_string('gumnotreadable_title', 'atto_recordrtc'), |
116
|
|
|
message: M.util.get_string('gumnotreadable', 'atto_recordrtc') |
117
|
|
|
}); |
118
|
|
|
}); |
119
|
|
|
|
120
|
|
|
btnLabel = M.util.get_string('recordingfailed', 'atto_recordrtc'); |
121
|
|
|
break; |
122
|
|
|
case 'OverConstrainedError': |
123
|
|
|
Y.use('moodle-core-notification-alert', function() { |
|
|
|
|
124
|
|
|
new M.core.alert({ |
|
|
|
|
125
|
|
|
title: M.util.get_string('gumoverconstrained_title', 'atto_recordrtc'), |
126
|
|
|
message: M.util.get_string('gumoverconstrained', 'atto_recordrtc') |
127
|
|
|
}); |
128
|
|
|
}); |
129
|
|
|
|
130
|
|
|
btnLabel = M.util.get_string('recordingfailed', 'atto_recordrtc'); |
131
|
|
|
break; |
132
|
|
|
case 'SecurityError': |
133
|
|
|
Y.use('moodle-core-notification-alert', function() { |
|
|
|
|
134
|
|
|
new M.core.alert({ |
|
|
|
|
135
|
|
|
title: M.util.get_string('gumsecurity_title', 'atto_recordrtc'), |
136
|
|
|
message: M.util.get_string('gumsecurity', 'atto_recordrtc') |
137
|
|
|
}); |
138
|
|
|
}); |
139
|
|
|
|
140
|
|
|
cm.editorScope.closeDialogue(cm.editorScope); |
141
|
|
|
break; |
142
|
|
|
case 'TypeError': |
143
|
|
|
Y.use('moodle-core-notification-alert', function() { |
|
|
|
|
144
|
|
|
new M.core.alert({ |
|
|
|
|
145
|
|
|
title: M.util.get_string('gumtype_title', 'atto_recordrtc'), |
146
|
|
|
message: M.util.get_string('gumtype', 'atto_recordrtc') |
147
|
|
|
}); |
148
|
|
|
}); |
149
|
|
|
|
150
|
|
|
btnLabel = M.util.get_string('recordingfailed', 'atto_recordrtc'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// Proceed to treat as a stopped recording. |
154
|
|
|
commonConfig.onMediaStopped(btnLabel); |
155
|
|
|
} |
156
|
|
|
}; |
157
|
|
|
|
158
|
|
|
// Capture audio stream from microphone. |
159
|
|
|
M.atto_recordrtc.audiomodule.capture_audio(commonConfig); |
160
|
|
|
} else { // If button is displaying "Stop Recording". |
161
|
|
|
// First of all clears the countdownTicker. |
162
|
|
|
clearInterval(cm.countdownTicker); |
163
|
|
|
|
164
|
|
|
// Disable "Record Again" button for 1s to allow background processing (closing streams). |
165
|
|
|
setTimeout(function() { |
166
|
|
|
cm.startStopBtn.set('disabled', false); |
167
|
|
|
}, 1000); |
168
|
|
|
|
169
|
|
|
// Stop recording. |
170
|
|
|
M.atto_recordrtc.audiomodule.stop_recording(cm.stream); |
171
|
|
|
|
172
|
|
|
// Change button to offer to record again. |
173
|
|
|
cm.startStopBtn.set('textContent', M.util.get_string('recordagain', 'atto_recordrtc')); |
174
|
|
|
if (!cm.olderMoodle) { |
175
|
|
|
cm.startStopBtn.replaceClass('btn-danger', 'btn-outline-danger'); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
}); |
179
|
|
|
}, |
180
|
|
|
|
181
|
|
|
// Setup to get audio stream from microphone. |
182
|
|
|
capture_audio: function(config) { |
183
|
|
|
cm.capture_user_media( |
184
|
|
|
// Media constraints. |
185
|
|
|
{ |
186
|
|
|
audio: true |
187
|
|
|
}, |
188
|
|
|
|
189
|
|
|
// Success callback. |
190
|
|
|
function(audioStream) { |
191
|
|
|
// Set audio player source to microphone stream. |
192
|
|
|
cm.playerDOM.srcObject = audioStream; |
193
|
|
|
|
194
|
|
|
config.onMediaCaptured(audioStream); |
195
|
|
|
}, |
196
|
|
|
|
197
|
|
|
// Error callback. |
198
|
|
|
function(error) { |
199
|
|
|
config.onMediaCapturingFailed(error); |
200
|
|
|
} |
201
|
|
|
); |
202
|
|
|
}, |
203
|
|
|
|
204
|
|
|
stop_recording: function(stream) { |
205
|
|
|
// Stop recording microphone stream. |
206
|
|
|
cm.mediaRecorder.stop(); |
207
|
|
|
|
208
|
|
|
// Stop each individual MediaTrack. |
209
|
|
|
stream.getTracks().forEach(function(track) { |
210
|
|
|
track.stop(); |
211
|
|
|
}); |
212
|
|
|
|
213
|
|
|
// Set source of audio player. |
214
|
|
|
var blob = new Blob(cm.chunks, {type: cm.mediaRecorder.mimeType}); |
|
|
|
|
215
|
|
|
cm.player.set('src', URL.createObjectURL(blob)); |
216
|
|
|
|
217
|
|
|
// Show audio player with controls enabled, and unmute. |
218
|
|
|
cm.player.set('muted', false); |
219
|
|
|
cm.player.set('controls', true); |
220
|
|
|
cm.player.ancestor().ancestor().removeClass('hide'); |
221
|
|
|
|
222
|
|
|
// Show upload button. |
223
|
|
|
cm.uploadBtn.ancestor().ancestor().removeClass('hide'); |
224
|
|
|
cm.uploadBtn.set('textContent', M.util.get_string('attachrecording', 'atto_recordrtc')); |
225
|
|
|
cm.uploadBtn.set('disabled', false); |
226
|
|
|
|
227
|
|
|
// Handle when upload button is clicked. |
228
|
|
|
cm.uploadBtn.on('click', function() { |
229
|
|
|
// Trigger error if no recording has been made. |
230
|
|
|
if (!cm.player.get('src') || cm.chunks === []) { |
231
|
|
|
Y.use('moodle-core-notification-alert', function() { |
|
|
|
|
232
|
|
|
new M.core.alert({ |
|
|
|
|
233
|
|
|
title: M.util.get_string('norecordingfound_title', 'atto_recordrtc'), |
234
|
|
|
message: M.util.get_string('norecordingfound', 'atto_recordrtc') |
235
|
|
|
}); |
236
|
|
|
}); |
237
|
|
|
} else { |
238
|
|
|
cm.uploadBtn.set('disabled', true); |
239
|
|
|
|
240
|
|
|
// Upload recording to server. |
241
|
|
|
cm.upload_to_server(cm.recType, function(progress, fileURLOrError) { |
242
|
|
|
if (progress === 'ended') { // Insert annotation in text. |
243
|
|
|
cm.uploadBtn.set('disabled', false); |
244
|
|
|
cm.insert_annotation(cm.recType, fileURLOrError); |
245
|
|
|
} else if (progress === 'upload-failed') { // Show error message in upload button. |
246
|
|
|
cm.uploadBtn.set('disabled', false); |
247
|
|
|
cm.uploadBtn.set('textContent', M.util.get_string('uploadfailed', 'atto_recordrtc') + ' ' + fileURLOrError); |
248
|
|
|
} else if (progress === 'upload-failed-404') { // 404 error = File too large in Moodle. |
249
|
|
|
cm.uploadBtn.set('disabled', false); |
250
|
|
|
cm.uploadBtn.set('textContent', M.util.get_string('uploadfailed404', 'atto_recordrtc')); |
251
|
|
|
} else if (progress === 'upload-aborted') { |
252
|
|
|
cm.uploadBtn.set('disabled', false); |
253
|
|
|
cm.uploadBtn.set('textContent', M.util.get_string('uploadaborted', 'atto_recordrtc') + ' ' + fileURLOrError); |
254
|
|
|
} else { |
255
|
|
|
cm.uploadBtn.set('textContent', progress); |
256
|
|
|
} |
257
|
|
|
}); |
258
|
|
|
} |
259
|
|
|
}); |
260
|
|
|
} |
261
|
|
|
}; |
262
|
|
|
|