|
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: tinyMCEPopup */ |
|
9
|
|
|
/** global: bowser */ |
|
10
|
|
|
/** global: params */ |
|
11
|
|
|
/** global: recordrtc */ |
|
12
|
|
|
|
|
13
|
|
|
M.tinymce_recordrtc = M.tinymce_recordrtc || {}; |
|
14
|
|
|
|
|
15
|
|
|
// Extract plugin settings to params hash. |
|
16
|
|
|
(function() { |
|
17
|
|
|
var params = {}; |
|
18
|
|
|
var r = /([^&=]+)=?([^&]*)/g; |
|
19
|
|
|
|
|
20
|
|
|
var d = function(s) { |
|
21
|
|
|
return decodeURIComponent(s.replace(/\+/g, ' ')); |
|
22
|
|
|
}; |
|
23
|
|
|
|
|
24
|
|
|
var search = window.location.search; |
|
25
|
|
|
var match = r.exec(search.substring(1)); |
|
26
|
|
|
while (match) { |
|
27
|
|
|
params[d(match[1])] = d(match[2]); |
|
28
|
|
|
|
|
29
|
|
|
if (d(match[2]) === 'true' || d(match[2]) === 'false') { |
|
30
|
|
|
params[d(match[1])] = d(match[2]) === 'true' ? true : false; |
|
31
|
|
|
} |
|
32
|
|
|
match = r.exec(search.substring(1)); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
window.params = params; |
|
36
|
|
|
})(); |
|
37
|
|
|
|
|
38
|
|
|
// Initialize some variables. |
|
39
|
|
|
var player = null; |
|
40
|
|
|
var startStopBtn = null; |
|
41
|
|
|
var uploadBtn = null; |
|
42
|
|
|
var countdownSeconds = null; |
|
43
|
|
|
var countdownTicker = null; |
|
44
|
|
|
var recType = null; |
|
45
|
|
|
var mediaRecorder = null; |
|
46
|
|
|
var chunks = null; |
|
47
|
|
|
var blobSize = null; |
|
48
|
|
|
var maxUploadSize = null; |
|
49
|
|
|
|
|
50
|
|
|
// Notify and redirect user if plugin is used from insecure location. |
|
51
|
|
|
M.tinymce_recordrtc.check_secure = function() { |
|
52
|
|
|
var isSecureOrigin = (window.location.protocol === 'https:') || |
|
53
|
|
|
(window.location.host.indexOf('localhost') !== -1); |
|
54
|
|
|
|
|
55
|
|
|
if (!isSecureOrigin) { |
|
56
|
|
|
window.alert(M.util.get_string('insecurealert', 'tinymce_recordrtc')); |
|
57
|
|
|
tinyMCEPopup.close(); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Display "consider switching browsers" message if not using: |
|
62
|
|
|
// - Firefox 29+; |
|
63
|
|
|
// - Chrome 49+; |
|
64
|
|
|
// - Opera 36+. |
|
65
|
|
|
M.tinymce_recordrtc.check_browser = function() { |
|
66
|
|
|
if (!((bowser.firefox && bowser.version >= 29) || |
|
67
|
|
|
(bowser.chrome && bowser.version >= 49) || |
|
68
|
|
|
(bowser.opera && bowser.version >= 36))) { |
|
69
|
|
|
var alert = document.querySelector('div[id=alert-warning]'); |
|
70
|
|
|
alert.parentElement.parentElement.classList.remove('hide'); |
|
71
|
|
|
} |
|
72
|
|
|
}; |
|
73
|
|
|
|
|
74
|
|
|
// Capture webcam/microphone stream. |
|
75
|
|
|
M.tinymce_recordrtc.captureUserMedia = function(mediaConstraints, successCallback, errorCallback) { |
|
76
|
|
|
navigator.mediaDevices.getUserMedia(mediaConstraints).then(successCallback).catch(errorCallback); |
|
|
|
|
|
|
77
|
|
|
}; |
|
78
|
|
|
|
|
79
|
|
|
// Add chunks of audio/video to array when made available. |
|
80
|
|
|
M.tinymce_recordrtc.handleDataAvailable = function(event) { |
|
81
|
|
|
// Size of all recorded data so far. |
|
82
|
|
|
blobSize += event.data.size; |
|
83
|
|
|
|
|
84
|
|
|
// Push recording slice to array. |
|
85
|
|
|
// If total size of recording so far exceeds max upload limit, stop recording. |
|
86
|
|
|
// An extra condition exists to avoid displaying alert twice. |
|
87
|
|
|
if ((blobSize >= maxUploadSize) && (!localStorage.getItem('alerted'))) { |
|
|
|
|
|
|
88
|
|
|
localStorage.setItem('alerted', 'true'); |
|
89
|
|
|
|
|
90
|
|
|
startStopBtn.click(); |
|
91
|
|
|
window.alert(M.util.get_string('nearingmaxsize', 'tinymce_recordrtc')); |
|
92
|
|
|
} else if ((blobSize >= maxUploadSize) && (localStorage.getItem('alerted') === 'true')) { |
|
93
|
|
|
localStorage.removeItem('alerted'); |
|
94
|
|
|
} else { |
|
95
|
|
|
chunks.push(event.data); |
|
96
|
|
|
} |
|
97
|
|
|
}; |
|
98
|
|
|
|
|
99
|
|
|
// Get everything set up to start recording. |
|
100
|
|
|
M.tinymce_recordrtc.startRecording = function(type, stream) { |
|
101
|
|
|
// The options for the recording codecs and bitrates. |
|
102
|
|
|
var options = null; |
|
103
|
|
|
if (type === 'audio') { |
|
104
|
|
|
if (MediaRecorder.isTypeSupported('audio/webm;codecs=opus')) { |
|
|
|
|
|
|
105
|
|
|
options = { |
|
106
|
|
|
audioBitsPerSecond: params['audiobitrate'], |
|
107
|
|
|
mimeType: 'audio/webm;codecs=opus' |
|
108
|
|
|
}; |
|
109
|
|
|
} else if (MediaRecorder.isTypeSupported('audio/ogg;codecs=opus')) { |
|
110
|
|
|
options = { |
|
111
|
|
|
audioBitsPerSecond: params['audiobitrate'], |
|
112
|
|
|
mimeType: 'audio/ogg;codecs=opus' |
|
113
|
|
|
}; |
|
114
|
|
|
} |
|
115
|
|
|
} else { |
|
116
|
|
|
if (MediaRecorder.isTypeSupported('video/webm;codecs=vp9,opus')) { |
|
117
|
|
|
options = { |
|
118
|
|
|
audioBitsPerSecond: params['audiobitrate'], |
|
119
|
|
|
videoBitsPerSecond: params['videobitrate'], |
|
120
|
|
|
mimeType: 'video/webm;codecs=vp9,opus' |
|
121
|
|
|
}; |
|
122
|
|
|
} else if (MediaRecorder.isTypeSupported('video/webm;codecs=h264,opus')) { |
|
123
|
|
|
options = { |
|
124
|
|
|
audioBitsPerSecond: params['audiobitrate'], |
|
125
|
|
|
videoBitsPerSecond: params['videobitrate'], |
|
126
|
|
|
mimeType: 'video/webm;codecs=h264,opus' |
|
127
|
|
|
}; |
|
128
|
|
|
} else if (MediaRecorder.isTypeSupported('video/webm;codecs=vp8,opus')) { |
|
129
|
|
|
options = { |
|
130
|
|
|
audioBitsPerSecond: params['audiobitrate'], |
|
131
|
|
|
videoBitsPerSecond: params['videobitrate'], |
|
132
|
|
|
mimeType: 'video/webm;codecs=vp8,opus' |
|
133
|
|
|
}; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
// If none of the options above are supported, fall back on browser defaults. |
|
138
|
|
|
mediaRecorder = options ? new MediaRecorder(stream) |
|
139
|
|
|
: new MediaRecorder(stream, options); |
|
140
|
|
|
|
|
141
|
|
|
// Initialize MediaRecorder events and start recording. |
|
142
|
|
|
mediaRecorder.ondataavailable = M.tinymce_recordrtc.handleDataAvailable; |
|
143
|
|
|
mediaRecorder.start(1000); // Capture in 10ms chunks. Must be set to work with Firefox. |
|
144
|
|
|
|
|
145
|
|
|
// Mute audio, distracting while recording. |
|
146
|
|
|
player.muted = true; |
|
147
|
|
|
|
|
148
|
|
|
// Set recording timer to the time specified in the settings. |
|
149
|
|
|
countdownSeconds = params['timelimit']; |
|
150
|
|
|
countdownSeconds++; |
|
151
|
|
|
startStopBtn.innerHTML = M.util.get_string('stoprecording', 'tinymce_recordrtc') + ' (<span id="minutes"></span>:<span id="seconds"></span>)'; |
|
152
|
|
|
M.tinymce_recordrtc.setTime(); |
|
153
|
|
|
countdownTicker = setInterval(M.tinymce_recordrtc.setTime, 1000); |
|
154
|
|
|
|
|
155
|
|
|
// Make button clickable again, to allow stopping recording. |
|
156
|
|
|
startStopBtn.disabled = false; |
|
157
|
|
|
}; |
|
158
|
|
|
|
|
159
|
|
|
// Upload recorded audio/video to server. |
|
160
|
|
|
M.tinymce_recordrtc.uploadToServer = function(type, callback) { |
|
161
|
|
|
var xhr = new XMLHttpRequest(); |
|
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
// Get src media of audio/video tag. |
|
164
|
|
|
xhr.open('GET', player.src, true); |
|
165
|
|
|
xhr.responseType = 'blob'; |
|
166
|
|
|
|
|
167
|
|
|
xhr.onload = function() { |
|
168
|
|
|
if (xhr.status === 200) { // If src media was successfully retrieved. |
|
169
|
|
|
// blob is now the media that the audio/video tag's src pointed to. |
|
170
|
|
|
var blob = this.response; |
|
171
|
|
|
|
|
172
|
|
|
// Generate filename with random ID and file extension. |
|
173
|
|
|
var fileName = (Math.random() * 1000).toString().replace('.', ''); |
|
174
|
|
|
if (type === 'audio') { |
|
175
|
|
|
fileName += '-audio.ogg'; |
|
176
|
|
|
} else { |
|
177
|
|
|
fileName += '-video.webm'; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
// Create FormData to send to PHP upload/save script. |
|
181
|
|
|
var formData = new FormData(); |
|
182
|
|
|
formData.append('contextid', recordrtc.contextid); |
|
183
|
|
|
formData.append('sesskey', parent.M.cfg.sesskey); |
|
|
|
|
|
|
184
|
|
|
formData.append(type + '-filename', fileName); |
|
185
|
|
|
formData.append(type + '-blob', blob); |
|
186
|
|
|
|
|
187
|
|
|
// Pass FormData to PHP script using XHR. |
|
188
|
|
|
M.tinymce_recordrtc.makeXMLHttpRequest('save.php', formData, function(progress, responseText) { |
|
189
|
|
|
if (progress === 'upload-ended') { |
|
190
|
|
|
var initialURL = location.href.replace(location.href.split('/').pop(), '') + 'uploads.php/'; |
|
191
|
|
|
callback('ended', initialURL + responseText); |
|
192
|
|
|
} else { |
|
193
|
|
|
callback(progress); |
|
194
|
|
|
} |
|
195
|
|
|
}); |
|
196
|
|
|
} |
|
197
|
|
|
}; |
|
198
|
|
|
|
|
199
|
|
|
xhr.send(); |
|
200
|
|
|
}; |
|
201
|
|
|
|
|
202
|
|
|
// Handle XHR sending/receiving/status. |
|
203
|
|
|
M.tinymce_recordrtc.makeXMLHttpRequest = function(url, data, callback) { |
|
204
|
|
|
var xhr = new XMLHttpRequest(); |
|
|
|
|
|
|
205
|
|
|
|
|
206
|
|
|
xhr.onreadystatechange = function() { |
|
207
|
|
|
if ((xhr.readyState === 4) && (xhr.status === 200)) { // When request is finished and successful. |
|
208
|
|
|
callback('upload-ended', xhr.responseText); |
|
209
|
|
|
} else if (xhr.status === 404) { // When request returns 404 Not Found. |
|
210
|
|
|
callback('upload-failed-404'); |
|
211
|
|
|
} |
|
212
|
|
|
}; |
|
213
|
|
|
|
|
214
|
|
|
xhr.upload.onprogress = function(event) { |
|
215
|
|
|
callback(Math.round(event.loaded / event.total * 100) + "% " + M.util.get_string('uploadprogress', 'tinymce_recordrtc')); |
|
216
|
|
|
}; |
|
217
|
|
|
|
|
218
|
|
|
xhr.upload.onerror = function(error) { |
|
219
|
|
|
callback('upload-failed', error); |
|
220
|
|
|
}; |
|
221
|
|
|
|
|
222
|
|
|
xhr.upload.onabort = function(error) { |
|
223
|
|
|
callback('upload-aborted', error); |
|
224
|
|
|
}; |
|
225
|
|
|
|
|
226
|
|
|
// POST FormData to PHP script that handles uploading/saving. |
|
227
|
|
|
xhr.open('POST', url); |
|
228
|
|
|
xhr.send(data); |
|
229
|
|
|
}; |
|
230
|
|
|
|
|
231
|
|
|
// Makes 1min and 2s display as 1:02 on timer instead of 1:2, for example. |
|
232
|
|
|
M.tinymce_recordrtc.pad = function(val) { |
|
233
|
|
|
var valString = val + ""; |
|
234
|
|
|
|
|
235
|
|
|
if (valString.length < 2) { |
|
236
|
|
|
return "0" + valString; |
|
237
|
|
|
} else { |
|
238
|
|
|
return valString; |
|
239
|
|
|
} |
|
240
|
|
|
}; |
|
241
|
|
|
|
|
242
|
|
|
// Functionality to make recording timer count down. |
|
243
|
|
|
// Also makes recording stop when time limit is hit. |
|
244
|
|
|
M.tinymce_recordrtc.setTime = function() { |
|
245
|
|
|
countdownSeconds--; |
|
246
|
|
|
|
|
247
|
|
|
startStopBtn.querySelector('span#seconds').textContent = M.tinymce_recordrtc.pad(countdownSeconds % 60); |
|
248
|
|
|
startStopBtn.querySelector('span#minutes').textContent = M.tinymce_recordrtc.pad(parseInt(countdownSeconds / 60)); |
|
249
|
|
|
|
|
250
|
|
|
if (countdownSeconds === 0) { |
|
251
|
|
|
startStopBtn.click(); |
|
252
|
|
|
} |
|
253
|
|
|
}; |
|
254
|
|
|
|
|
255
|
|
|
// Generates link to recorded annotation to be inserted. |
|
256
|
|
|
M.tinymce_recordrtc.create_annotation = function(type, recording_url) { |
|
257
|
|
|
var linkText = window.prompt(M.util.get_string('annotationprompt', 'tinymce_recordrtc'), |
|
258
|
|
|
M.util.get_string('annotation:' + type, 'tinymce_recordrtc')); |
|
259
|
|
|
|
|
260
|
|
|
// Return HTML for annotation link, if user did not press "Cancel". |
|
261
|
|
|
if (!linkText) { |
|
262
|
|
|
return undefined; |
|
263
|
|
|
} else { |
|
264
|
|
|
var annotation = '<div id="recordrtc_annotation" class="text-center"><a target="_blank" href="' + recording_url + '">' + linkText + '</a></div>'; |
|
265
|
|
|
return annotation; |
|
266
|
|
|
} |
|
267
|
|
|
}; |
|
268
|
|
|
|
|
269
|
|
|
// Inserts link to annotation in editor text area. |
|
270
|
|
|
M.tinymce_recordrtc.insert_annotation = function(type, recording_url) { |
|
271
|
|
|
var annotation = M.tinymce_recordrtc.create_annotation(type, recording_url); |
|
272
|
|
|
|
|
273
|
|
|
// Insert annotation link. |
|
274
|
|
|
// If user pressed "Cancel", just go back to main recording screen. |
|
275
|
|
|
if (!annotation) { |
|
276
|
|
|
uploadBtn.textContent = M.util.get_string('attachrecording', 'tinymce_recordrtc'); |
|
277
|
|
|
} else { |
|
278
|
|
|
tinyMCEPopup.editor.execCommand('mceInsertContent', false, annotation); |
|
279
|
|
|
tinyMCEPopup.close(); |
|
280
|
|
|
} |
|
281
|
|
|
}; |
|
282
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.