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