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: URL */ |
9
|
|
|
/** global: params */ |
10
|
|
|
/** global: initialized variables */ |
11
|
|
|
|
12
|
|
|
M.tinymce_recordrtc = M.tinymce_recordrtc || {}; |
|
|
|
|
13
|
|
|
|
14
|
|
|
// Extract plugin settings to params hash. |
15
|
|
|
(function() { |
16
|
|
|
var params = {}; |
17
|
|
|
var r = /([^&=]+)=?([^&]*)/g; |
18
|
|
|
|
19
|
|
|
var d = function(s) { |
20
|
|
|
return decodeURIComponent(s.replace(/\+/g, ' ')); |
21
|
|
|
}; |
22
|
|
|
|
23
|
|
|
var search = window.location.search; |
24
|
|
|
var match = r.exec(search.substring(1)); |
25
|
|
|
while (match) { |
26
|
|
|
params[d(match[1])] = d(match[2]); |
27
|
|
|
|
28
|
|
|
if (d(match[2]) === 'true' || d(match[2]) === 'false') { |
29
|
|
|
params[d(match[1])] = d(match[2]) === 'true' ? true : false; |
30
|
|
|
} |
31
|
|
|
match = r.exec(search.substring(1)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
window.params = params; |
35
|
|
|
})(); |
36
|
|
|
|
37
|
|
|
// Initialize some variables. |
38
|
|
|
var player = null; |
39
|
|
|
var startStopBtn = null; |
40
|
|
|
var uploadBtn = null; |
41
|
|
|
var countdownSeconds = null; |
42
|
|
|
var countdownTicker = null; |
43
|
|
|
var mediaRecorder = null; |
44
|
|
|
var chunks = null; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* This function is initialized from PHP |
48
|
|
|
* |
49
|
|
|
* @param {Object} |
|
|
|
|
50
|
|
|
* Y YUI instance |
51
|
|
|
*/ |
52
|
|
|
M.tinymce_recordrtc.view_init = function() { |
53
|
|
|
// Assignment of global variables. |
54
|
|
|
player = document.querySelector('video#player'); |
55
|
|
|
startStopBtn = document.querySelector('button#start-stop'); |
56
|
|
|
uploadBtn = document.querySelector('button#upload'); |
57
|
|
|
|
58
|
|
|
// Show alert if using non-ideal browser. |
59
|
|
|
M.tinymce_recordrtc.check_browser(); |
60
|
|
|
|
61
|
|
|
// Run when user clicks on "record" button. |
62
|
|
|
startStopBtn.onclick = function() { |
63
|
|
|
var btn = this; |
64
|
|
|
btn.disabled = true; |
65
|
|
|
|
66
|
|
|
// If button is displaying "Start Recording" or "Record Again". |
67
|
|
|
if ((btn.textContent === M.util.get_string('startrecording', 'tinymce_recordrtc')) || |
68
|
|
|
(btn.textContent === M.util.get_string('recordagain', 'tinymce_recordrtc')) || |
69
|
|
|
(btn.textContent === M.util.get_string('recordingfailed', 'tinymce_recordrtc'))) { |
70
|
|
|
// Hide alert-danger if it is shown. |
71
|
|
|
var alert = document.querySelector('div[id=alert-danger]'); |
72
|
|
|
alert.parentElement.parentElement.classList.add('hide'); |
73
|
|
|
|
74
|
|
|
// Make sure the upload button is not shown. |
75
|
|
|
uploadBtn.parentElement.parentElement.classList.add('hide'); |
76
|
|
|
|
77
|
|
|
// Change look of recording button. |
78
|
|
|
startStopBtn.classList.remove('btn-outline-danger'); |
79
|
|
|
startStopBtn.classList.add('btn-danger'); |
80
|
|
|
|
81
|
|
|
// Empty the array containing the previously recorded chunks. |
82
|
|
|
chunks = []; |
83
|
|
|
|
84
|
|
|
// Initialize common configurations. |
85
|
|
|
var commonConfig = { |
86
|
|
|
// When the stream is captured from the microphone/webcam. |
87
|
|
|
onMediaCaptured: function(stream) { |
88
|
|
|
// Make video stream available at a higher level by making it a property of btn. |
89
|
|
|
btn.stream = stream; |
90
|
|
|
|
91
|
|
|
if (btn.mediaCapturedCallback) { |
92
|
|
|
btn.mediaCapturedCallback(); |
93
|
|
|
} |
94
|
|
|
}, |
95
|
|
|
|
96
|
|
|
// Revert button to "Record Again" when recording is stopped. |
97
|
|
|
onMediaStopped: function(btnLabel) { |
98
|
|
|
btn.textContent = btnLabel; |
99
|
|
|
}, |
100
|
|
|
|
101
|
|
|
// Handle recording errors. |
102
|
|
|
onMediaCapturingFailed: function(error) { |
103
|
|
|
var btnLabel = null; |
104
|
|
|
|
105
|
|
|
// If Firefox and Permission Denied error. |
106
|
|
|
if ((error.name === 'PermissionDeniedError') && bowser.firefox) { |
|
|
|
|
107
|
|
|
InstallTrigger.install({ |
|
|
|
|
108
|
|
|
'Foo': { |
109
|
|
|
// Link: https://addons.mozilla.org/firefox/downloads/latest/655146/addon-655146... |
110
|
|
|
// ...-latest.xpi?src=dp-btn-primary. |
111
|
|
|
URL: 'https://addons.mozilla.org/en-US/firefox/addon/enable-screen-capturing/', |
112
|
|
|
toString: function() { |
113
|
|
|
return this.URL; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
}); |
117
|
|
|
|
118
|
|
|
btnLabel = M.util.get_string('startrecording', 'tinymce_recordrtc'); |
119
|
|
|
} else if ((error.name === 'DevicesNotFoundError') || |
120
|
|
|
(error.name === 'NotFoundError')) { // If Device Not Found error. |
121
|
|
|
var alert = document.querySelector('div[id=alert-danger]'); |
122
|
|
|
alert.parentElement.parentElement.classList.remove('hide'); |
123
|
|
|
alert.textContent = M.util.get_string('inputdevicealert', 'tinymce_recordrtc') + ' ' + M.util.get_string('inputdevicealert', 'tinymce_recordrtc'); |
124
|
|
|
|
125
|
|
|
btnLabel = M.util.get_string('recordingfailed', 'tinymce_recordrtc'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// Proceed to treat as a stopped recording. |
129
|
|
|
commonConfig.onMediaStopped(btnLabel); |
130
|
|
|
} |
131
|
|
|
}; |
132
|
|
|
|
133
|
|
|
// Show video tag without controls to view webcam stream. |
134
|
|
|
player.parentElement.parentElement.classList.remove('hide'); |
135
|
|
|
player.controls = false; |
136
|
|
|
|
137
|
|
|
// Capture audio+video stream from webcam/microphone. |
138
|
|
|
M.tinymce_recordrtc.captureAudioVideo(commonConfig); |
139
|
|
|
|
140
|
|
|
// When audio+video stream is successfully captured, start recording. |
141
|
|
|
btn.mediaCapturedCallback = function() { |
142
|
|
|
M.tinymce_recordrtc.startRecording(btn.stream); |
143
|
|
|
}; |
144
|
|
|
|
145
|
|
|
return; |
|
|
|
|
146
|
|
|
} else { // If button is displaying "Stop Recording". |
147
|
|
|
// First of all clears the countdownTicker. |
148
|
|
|
clearInterval(countdownTicker); |
149
|
|
|
|
150
|
|
|
// Disable "Record Again" button for 1s to allow background processing (closing streams). |
151
|
|
|
setTimeout(function() { |
152
|
|
|
btn.disabled = false; |
153
|
|
|
}, 1000); |
154
|
|
|
|
155
|
|
|
// Stop recording. |
156
|
|
|
M.tinymce_recordrtc.stopRecording(btn.stream); |
157
|
|
|
|
158
|
|
|
// Change button to offer to record again. |
159
|
|
|
btn.textContent = M.util.get_string('recordagain', 'tinymce_recordrtc'); |
160
|
|
|
startStopBtn.classList.remove('btn-danger'); |
161
|
|
|
startStopBtn.classList.add('btn-outline-danger'); |
162
|
|
|
|
163
|
|
|
return; |
|
|
|
|
164
|
|
|
} |
165
|
|
|
}; |
166
|
|
|
}; |
167
|
|
|
|
168
|
|
|
///////////////////////// |
169
|
|
|
// Functions for capturing, recording, and uploading stream. |
170
|
|
|
///////////////////////// |
171
|
|
|
|
172
|
|
|
// Setup to get audio+video stream from microphone/webcam. |
173
|
|
|
M.tinymce_recordrtc.captureAudioVideo = function(config) { |
174
|
|
|
M.tinymce_recordrtc.captureUserMedia( |
175
|
|
|
// Media constraints. |
176
|
|
|
{ |
177
|
|
|
audio: true, |
178
|
|
|
video: { |
179
|
|
|
width: {ideal: 640}, |
180
|
|
|
height: {ideal: 480} |
181
|
|
|
} |
182
|
|
|
}, |
183
|
|
|
|
184
|
|
|
// Success callback. |
185
|
|
|
function(audioVideoStream) { |
186
|
|
|
console.log('getUserMedia() got stream:', audioVideoStream); |
|
|
|
|
187
|
|
|
|
188
|
|
|
// Set video player to play microphone+webcam stream. |
189
|
|
|
player.srcObject = audioVideoStream; |
190
|
|
|
player.play(); |
191
|
|
|
|
192
|
|
|
config.onMediaCaptured(audioVideoStream); |
193
|
|
|
}, |
194
|
|
|
|
195
|
|
|
// Error callback. |
196
|
|
|
function(error) { |
197
|
|
|
console.log('getUserMedia() error:', error); |
|
|
|
|
198
|
|
|
config.onMediaCapturingFailed(error); |
199
|
|
|
} |
200
|
|
|
); |
201
|
|
|
}; |
202
|
|
|
|
203
|
|
|
M.tinymce_recordrtc.stopRecording = function(stream) { |
204
|
|
|
mediaRecorder.stop(); |
205
|
|
|
|
206
|
|
|
stream.getTracks().forEach(function(track) { |
207
|
|
|
track.stop(); |
208
|
|
|
console.log('MediaTrack stopped:', track); |
|
|
|
|
209
|
|
|
}); |
210
|
|
|
|
211
|
|
|
// Set source of video player, then show it with controls enabled. |
212
|
|
|
var blob = new Blob(chunks, { |
|
|
|
|
213
|
|
|
type: 'video/webm;codecs=vp8' |
214
|
|
|
}); |
215
|
|
|
player.src = URL.createObjectURL(blob); |
216
|
|
|
|
217
|
|
|
player.muted = false; |
218
|
|
|
player.controls = true; |
219
|
|
|
player.play(); |
220
|
|
|
|
221
|
|
|
player.onended = function() { |
222
|
|
|
player.pause(); |
223
|
|
|
}; |
224
|
|
|
|
225
|
|
|
// Show upload button. |
226
|
|
|
uploadBtn.parentElement.parentElement.classList.remove('hide'); |
227
|
|
|
uploadBtn.textContent = M.util.get_string('attachrecording', 'tinymce_recordrtc'); |
228
|
|
|
uploadBtn.disabled = false; |
229
|
|
|
|
230
|
|
|
// Handle when upload button is clicked. |
231
|
|
|
uploadBtn.onclick = function() { |
232
|
|
|
// Trigger error if no recording has been made. |
233
|
|
|
if (!player.src || chunks === []) { |
234
|
|
|
return alert(M.util.get_string('norecordingfound', 'tinymce_recordrtc')); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
var btn = uploadBtn; |
238
|
|
|
btn.disabled = true; |
239
|
|
|
|
240
|
|
|
// Upload recording to server. |
241
|
|
|
M.tinymce_recordrtc.uploadToServer('video', function(progress, fileURL) { |
242
|
|
|
if (progress === 'ended') { |
243
|
|
|
btn.disabled = false; |
244
|
|
|
M.tinymce_recordrtc.insert_annotation(fileURL); |
245
|
|
|
return; |
|
|
|
|
246
|
|
|
} else if (progress === 'upload-failed') { |
247
|
|
|
btn.disabled = false; |
248
|
|
|
btn.textContent = M.util.get_string('uploadfailed', 'tinymce_recordrtc'); |
249
|
|
|
return; |
|
|
|
|
250
|
|
|
} else { |
251
|
|
|
btn.textContent = progress; |
252
|
|
|
return; |
|
|
|
|
253
|
|
|
} |
254
|
|
|
}); |
|
|
|
|
255
|
|
|
}; |
256
|
|
|
}; |
257
|
|
|
|