1
|
|
|
// TinyMCE recordrtc library functions. |
2
|
|
|
// @package tinymce_recordrtc. |
3
|
|
|
// @author Jesus Federico (jesus [at] blindsidenetworks [dt] com). |
4
|
|
|
// @author Jacob Prud'homme (jacob [dt] prudhomme [at] blindsidenetworks [dt] com) |
5
|
|
|
// @copyright 2016 onwards, Blindside Networks Inc. |
6
|
|
|
// @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. |
7
|
|
|
|
8
|
|
|
// ESLint directives. |
9
|
|
|
/* global recordrtc, alertWarning, alertDanger, player, playerDOM, startStopBtn, uploadBtn */ |
10
|
|
|
/* global recType, maxUploadSize, chunks, blobSize, countdownTicker, mediaRecorder */ |
11
|
|
|
/* exported alertWarning, alertDanger, maxUploadSize, chunks, blobSize */ |
12
|
|
|
/* eslint-disable camelcase, no-global-assign */ |
13
|
|
|
|
14
|
|
|
// JSHint directives. |
15
|
|
|
/* global alertWarning: true, alertDanger: true, player: true, playerDOM: true, startStopBtn: true */ |
16
|
|
|
/* global uploadBtn: true, recType: true, maxUploadSize: true, chunks: true, blobSize: true */ |
17
|
|
|
|
18
|
|
|
// Scrutinizer CI directives. |
19
|
|
|
/** global: M */ |
20
|
|
|
/** global: Y */ |
21
|
|
|
/** global: recordrtc */ |
22
|
|
|
/** global: alertWarning */ |
23
|
|
|
/** global: alertDanger */ |
24
|
|
|
/** global: blobSize */ |
25
|
|
|
/** global: chunks */ |
26
|
|
|
/** global: countdownSeconds */ |
27
|
|
|
/** global: countdownTicker */ |
28
|
|
|
/** global: maxUploadSize */ |
29
|
|
|
/** global: mediaRecorder */ |
30
|
|
|
/** global: player */ |
31
|
|
|
/** global: playerDOM */ |
32
|
|
|
/** global: recType */ |
33
|
|
|
/** global: startStopBtn */ |
34
|
|
|
/** global: uploadBtn */ |
35
|
|
|
|
36
|
|
|
// This function is initialized from PHP. |
37
|
|
|
M.tinymce_recordrtc.view_init = function() { |
38
|
|
|
// Assignment of global variables. |
39
|
|
|
alertWarning = Y.one('div#alert-warning'); |
40
|
|
|
alertDanger = Y.one('div#alert-danger'); |
41
|
|
|
player = Y.one('audio#player'); |
42
|
|
|
playerDOM = document.querySelector('audio#player'); |
43
|
|
|
startStopBtn = Y.one('button#start-stop'); |
44
|
|
|
uploadBtn = Y.one('button#upload'); |
45
|
|
|
recType = 'audio'; |
46
|
|
|
// Extract the numbers from the string, and convert to bytes. |
47
|
|
|
maxUploadSize = window.parseInt(recordrtc.maxfilesize.match(/\d+/)[0], 10) * Math.pow(1024, 2); |
48
|
|
|
|
49
|
|
|
// Show alert and close plugin if WebRTC is not supported. |
50
|
|
|
M.tinymce_recordrtc.check_has_gum(); |
51
|
|
|
// Show alert and redirect user if connection is not secure. |
52
|
|
|
M.tinymce_recordrtc.check_secure(); |
53
|
|
|
// Show alert if using non-ideal browser. |
54
|
|
|
M.tinymce_recordrtc.check_browser(); |
55
|
|
|
|
56
|
|
|
// Run when user clicks on "record" button. |
57
|
|
|
startStopBtn.on('click', function() { |
58
|
|
|
startStopBtn.set('disabled', true); |
59
|
|
|
|
60
|
|
|
// If button is displaying "Start Recording" or "Record Again". |
61
|
|
|
if ((startStopBtn.get('textContent') === M.util.get_string('startrecording', 'tinymce_recordrtc')) || |
62
|
|
|
(startStopBtn.get('textContent') === M.util.get_string('recordagain', 'tinymce_recordrtc')) || |
63
|
|
|
(startStopBtn.get('textContent') === M.util.get_string('recordingfailed', 'tinymce_recordrtc'))) { |
64
|
|
|
// Make sure the audio player and upload button are not shown. |
65
|
|
|
player.ancestor().ancestor().addClass('hide'); |
66
|
|
|
uploadBtn.ancestor().ancestor().addClass('hide'); |
67
|
|
|
|
68
|
|
|
// Change look of recording button. |
69
|
|
|
if (!recordrtc.oldermoodle) { |
70
|
|
|
startStopBtn.replaceClass('btn-outline-danger', 'btn-danger'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Empty the array containing the previously recorded chunks. |
74
|
|
|
chunks = []; |
75
|
|
|
blobSize = 0; |
76
|
|
|
|
77
|
|
|
// Initialize common configurations. |
78
|
|
|
var commonConfig = { |
79
|
|
|
// When the stream is captured from the microphone/webcam. |
80
|
|
|
onMediaCaptured: function(stream) { |
81
|
|
|
// Make audio stream available at a higher level by making it a property of startStopBtn. |
82
|
|
|
startStopBtn.stream = stream; |
83
|
|
|
|
84
|
|
|
M.tinymce_recordrtc.start_recording(recType, startStopBtn.stream); |
85
|
|
|
}, |
86
|
|
|
|
87
|
|
|
// Revert button to "Record Again" when recording is stopped. |
88
|
|
|
onMediaStopped: function(btnLabel) { |
89
|
|
|
startStopBtn.set('textContent', btnLabel); |
90
|
|
|
startStopBtn.set('disabled', false); |
91
|
|
|
if (!recordrtc.oldermoodle) { |
92
|
|
|
startStopBtn.replaceClass('btn-danger', 'btn-outline-danger'); |
93
|
|
|
} |
94
|
|
|
}, |
95
|
|
|
|
96
|
|
|
// Handle recording errors. |
97
|
|
|
onMediaCapturingFailed: function(error) { |
98
|
|
|
M.tinymce_recordrtc.handle_gum_errors(error, commonConfig); |
99
|
|
|
} |
100
|
|
|
}; |
101
|
|
|
|
102
|
|
|
// Capture audio stream from microphone. |
103
|
|
|
M.tinymce_recordrtc.capture_audio(commonConfig); |
104
|
|
|
} else { // If button is displaying "Stop Recording". |
105
|
|
|
// First of all clears the countdownTicker. |
106
|
|
|
window.clearInterval(countdownTicker); |
107
|
|
|
|
108
|
|
|
// Disable "Record Again" button for 1s to allow background processing (closing streams). |
109
|
|
|
window.setTimeout(function() { |
110
|
|
|
startStopBtn.set('disabled', false); |
111
|
|
|
}, 1000); |
112
|
|
|
|
113
|
|
|
// Stop recording. |
114
|
|
|
M.tinymce_recordrtc.stop_recording(startStopBtn.stream); |
115
|
|
|
|
116
|
|
|
// Change button to offer to record again. |
117
|
|
|
startStopBtn.set('textContent', M.util.get_string('recordagain', 'tinymce_recordrtc')); |
118
|
|
|
if (!recordrtc.oldermoodle) { |
119
|
|
|
startStopBtn.replaceClass('btn-danger', 'btn-outline-danger'); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
}); |
123
|
|
|
}; |
124
|
|
|
|
125
|
|
|
// Setup to get audio stream from microphone. |
126
|
|
|
M.tinymce_recordrtc.capture_audio = function(config) { |
127
|
|
|
M.tinymce_recordrtc.capture_user_media( |
128
|
|
|
// Media constraints. |
129
|
|
|
{ |
130
|
|
|
audio: true |
131
|
|
|
}, |
132
|
|
|
|
133
|
|
|
// Success callback. |
134
|
|
|
function(audioStream) { |
135
|
|
|
// Set audio player source to microphone stream. |
136
|
|
|
playerDOM.srcObject = audioStream; |
137
|
|
|
|
138
|
|
|
config.onMediaCaptured(audioStream); |
139
|
|
|
}, |
140
|
|
|
|
141
|
|
|
// Error callback. |
142
|
|
|
function(error) { |
143
|
|
|
config.onMediaCapturingFailed(error); |
144
|
|
|
} |
145
|
|
|
); |
146
|
|
|
}; |
147
|
|
|
|