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
|
|
|
// Scrutinizer CI directives. |
8
|
|
|
/** global: M */ |
9
|
|
|
/** global: Y */ |
10
|
|
|
/** global: tinyMCEPopup */ |
11
|
|
|
/** global: recordrtc */ |
12
|
|
|
/** global: alertWarning */ |
13
|
|
|
/** global: alertDanger */ |
14
|
|
|
/** global: maxUploadSize */ |
15
|
|
|
/** global: mediaRecorder */ |
16
|
|
|
/** global: player */ |
17
|
|
|
/** global: playerDOM */ |
18
|
|
|
/** global: recType */ |
19
|
|
|
/** global: startStopBtn */ |
20
|
|
|
/** global: uploadBtn */ |
21
|
|
|
/** global: socket */ |
22
|
|
|
|
23
|
|
|
// This function is initialized from PHP. |
24
|
|
|
M.tinymce_recordrtc.view_init = function() { |
25
|
|
|
// Assignment of global variables. |
26
|
|
|
alertWarning = Y.one('div#alert-warning'); |
27
|
|
|
alertDanger = Y.one('div#alert-danger'); |
28
|
|
|
player = Y.one('audio#player'); |
29
|
|
|
playerDOM = document.querySelector('audio#player'); |
30
|
|
|
startStopBtn = Y.one('button#start-stop'); |
31
|
|
|
uploadBtn = Y.one('button#upload'); |
32
|
|
|
recType = 'audio'; |
33
|
|
|
socket = window.io(window.params.serverurl); |
34
|
|
|
|
35
|
|
|
// Show alert and redirect user if connection is not secure. |
36
|
|
|
M.tinymce_recordrtc.check_secure(); |
37
|
|
|
// Show alert if using non-ideal browser. |
38
|
|
|
M.tinymce_recordrtc.check_browser(); |
39
|
|
|
|
40
|
|
|
// Connect to premium recording server. |
41
|
|
|
M.tinymce_recordrtc.init_connection(); |
42
|
|
|
|
43
|
|
|
// Run when user clicks on "record" button. |
44
|
|
|
startStopBtn.on('click', function() { |
45
|
|
|
startStopBtn.set('disabled', true); |
46
|
|
|
|
47
|
|
|
// If button is displaying "Start Recording" or "Record Again". |
48
|
|
|
if ((startStopBtn.get('textContent') === M.util.get_string('startrecording', 'tinymce_recordrtc')) || |
49
|
|
|
(startStopBtn.get('textContent') === M.util.get_string('recordagain', 'tinymce_recordrtc')) || |
50
|
|
|
(startStopBtn.get('textContent') === M.util.get_string('recordingfailed', 'tinymce_recordrtc'))) { |
51
|
|
|
// Make sure the audio player and upload button are not shown. |
52
|
|
|
player.ancestor().ancestor().addClass('hide'); |
53
|
|
|
uploadBtn.ancestor().ancestor().addClass('hide'); |
54
|
|
|
|
55
|
|
|
// Change look of recording button. |
56
|
|
|
if (!recordrtc.oldermoodle) { |
57
|
|
|
startStopBtn.replaceClass('btn-outline-danger', 'btn-danger'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Initialize common configurations. |
61
|
|
|
var commonConfig = { |
62
|
|
|
// When the stream is captured from the microphone/webcam. |
63
|
|
|
onMediaCaptured: function(stream) { |
64
|
|
|
// Make audio stream available at a higher level by making it a property of startStopBtn. |
65
|
|
|
startStopBtn.stream = stream; |
66
|
|
|
|
67
|
|
|
M.tinymce_recordrtc.start_recording(recType, startStopBtn.stream); |
68
|
|
|
}, |
69
|
|
|
|
70
|
|
|
// Revert button to "Record Again" when recording is stopped. |
71
|
|
|
onMediaStopped: function(btnLabel) { |
72
|
|
|
startStopBtn.set('textContent', btnLabel); |
73
|
|
|
startStopBtn.set('disabled', false); |
74
|
|
|
if (!recordrtc.oldermoodle) { |
75
|
|
|
startStopBtn.replaceClass('btn-danger', 'btn-outline-danger'); |
76
|
|
|
} |
77
|
|
|
}, |
78
|
|
|
|
79
|
|
|
// Handle recording errors. |
80
|
|
|
onMediaCapturingFailed: function(error) { |
81
|
|
|
var btnLabel = M.util.get_string('recordingfailed', 'tinymce_recordrtc'); |
82
|
|
|
var treatAsStopped = function() { |
83
|
|
|
commonConfig.onMediaStopped(btnLabel); |
84
|
|
|
}; |
85
|
|
|
|
86
|
|
|
// Handle getUserMedia-thrown errors. |
87
|
|
|
// After alert, proceed to treat as stopped recording, or close dialogue. |
88
|
|
|
switch (error.name) { |
89
|
|
|
case 'AbortError': |
90
|
|
|
M.tinymce_recordrtc.show_alert('gumabort', treatAsStopped); |
91
|
|
|
|
92
|
|
|
break; |
93
|
|
|
case 'NotAllowedError': |
94
|
|
|
M.tinymce_recordrtc.show_alert('gumnotallowed', treatAsStopped); |
95
|
|
|
|
96
|
|
|
break; |
97
|
|
|
case 'NotFoundError': |
98
|
|
|
M.tinymce_recordrtc.show_alert('gumnotfound', treatAsStopped); |
99
|
|
|
|
100
|
|
|
break; |
101
|
|
|
case 'NotReadableError': |
102
|
|
|
M.tinymce_recordrtc.show_alert('gumnotreadable', treatAsStopped); |
103
|
|
|
|
104
|
|
|
break; |
105
|
|
|
case 'OverConstrainedError': |
106
|
|
|
M.tinymce_recordrtc.show_alert('gumoverconstrained', treatAsStopped); |
107
|
|
|
|
108
|
|
|
break; |
109
|
|
|
case 'SecurityError': |
110
|
|
|
M.tinymce_recordrtc.show_alert('gumsecurity', function() { |
111
|
|
|
tinyMCEPopup.close(); |
112
|
|
|
}); |
113
|
|
|
|
114
|
|
|
break; |
115
|
|
|
case 'TypeError': |
116
|
|
|
M.tinymce_recordrtc.show_alert('gumtype', treatAsStopped); |
117
|
|
|
|
118
|
|
|
break; |
119
|
|
|
default: |
120
|
|
|
break; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
}; |
124
|
|
|
|
125
|
|
|
// Capture audio stream from microphone. |
126
|
|
|
M.tinymce_recordrtc.capture_audio(commonConfig); |
127
|
|
|
} else { // If button is displaying "Stop Recording". |
128
|
|
|
// Disable "Record Again" button for 1s to allow background processing (closing streams). |
129
|
|
|
window.setTimeout(function() { |
130
|
|
|
startStopBtn.set('disabled', false); |
131
|
|
|
}, 1000); |
132
|
|
|
|
133
|
|
|
// Stop recording. |
134
|
|
|
M.tinymce_recordrtc.stop_recording_audio(startStopBtn.stream); |
135
|
|
|
|
136
|
|
|
// Change button to offer to record again. |
137
|
|
|
startStopBtn.set('textContent', M.util.get_string('recordagain', 'tinymce_recordrtc')); |
138
|
|
|
if (!recordrtc.oldermoodle) { |
139
|
|
|
startStopBtn.replaceClass('btn-danger', 'btn-outline-danger'); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
}); |
143
|
|
|
}; |
144
|
|
|
|
145
|
|
|
// Setup to get audio stream from microphone. |
146
|
|
|
M.tinymce_recordrtc.capture_audio = function(config) { |
147
|
|
|
M.tinymce_recordrtc.capture_user_media( |
148
|
|
|
// Media constraints. |
149
|
|
|
{ |
150
|
|
|
audio: true |
151
|
|
|
}, |
152
|
|
|
|
153
|
|
|
// Success callback. |
154
|
|
|
function(audioStream) { |
155
|
|
|
// Set audio player source to microphone stream. |
156
|
|
|
playerDOM.srcObject = audioStream; |
157
|
|
|
|
158
|
|
|
config.onMediaCaptured(audioStream); |
159
|
|
|
}, |
160
|
|
|
|
161
|
|
|
// Error callback. |
162
|
|
|
function(error) { |
163
|
|
|
config.onMediaCapturingFailed(error); |
164
|
|
|
} |
165
|
|
|
); |
166
|
|
|
}; |
167
|
|
|
|
168
|
|
|
M.tinymce_recordrtc.stop_recording_audio = function(stream) { |
169
|
|
|
// Stop recording microphone stream. |
170
|
|
|
mediaRecorder.stop(); |
171
|
|
|
|
172
|
|
|
// Stop each individual MediaTrack. |
173
|
|
|
stream.getTracks().forEach(function(track) { |
174
|
|
|
track.stop(); |
175
|
|
|
}); |
176
|
|
|
|
177
|
|
|
// Show upload button. |
178
|
|
|
uploadBtn.ancestor().ancestor().removeClass('hide'); |
179
|
|
|
uploadBtn.set('textContent', M.util.get_string('attachrecording', 'tinymce_recordrtc')); |
180
|
|
|
uploadBtn.set('disabled', false); |
181
|
|
|
|
182
|
|
|
// Handle when upload button is clicked. |
183
|
|
|
uploadBtn.on('click', function() { |
184
|
|
|
// Trigger error if no recording has been made. |
185
|
|
|
if (!player.get('src')) { |
186
|
|
|
M.tinymce_recordrtc.show_alert('norecordingfound'); |
187
|
|
|
} else { |
188
|
|
|
uploadBtn.set('disabled', true); |
189
|
|
|
|
190
|
|
|
M.tinymce_recordrtc.insert_annotation(recType, player.get('src')); |
191
|
|
|
} |
192
|
|
|
}); |
193
|
|
|
}; |
194
|
|
|
|