1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
//
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Atto recordrtc library functions
|
|
|
19 |
*
|
|
|
20 |
* @package atto_recordrtc
|
|
|
21 |
* @author Jesus Federico (jesus [at] blindsidenetworks [dt] com)
|
|
|
22 |
* @author Jacob Prud'homme (jacob [dt] prudhomme [at] blindsidenetworks [dt] com)
|
|
|
23 |
* @copyright 2017 Blindside Networks Inc.
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
// ESLint directives.
|
|
|
28 |
/* eslint-disable camelcase, spaced-comment */
|
|
|
29 |
|
|
|
30 |
// Scrutinizer CI directives.
|
|
|
31 |
/** global: M */
|
|
|
32 |
/** global: Y */
|
|
|
33 |
|
|
|
34 |
M.atto_recordrtc = M.atto_recordrtc || {};
|
|
|
35 |
|
|
|
36 |
// Shorten access to module namespaces.
|
|
|
37 |
var cm = M.atto_recordrtc.commonmodule,
|
|
|
38 |
am = M.atto_recordrtc.abstractmodule,
|
|
|
39 |
ccm = M.atto_recordrtc.compatcheckmodule;
|
|
|
40 |
|
|
|
41 |
M.atto_recordrtc.audiomodule = {
|
|
|
42 |
init: function(scope) {
|
|
|
43 |
// Assignment of global variables.
|
|
|
44 |
cm.editorScope = scope; // Allows access to the editor's "this" context.
|
|
|
45 |
cm.alertWarning = Y.one('div#alert-warning');
|
|
|
46 |
cm.alertDanger = Y.one('div#alert-danger');
|
|
|
47 |
cm.player = Y.one('audio#player');
|
|
|
48 |
cm.playerDOM = document.querySelector('audio#player');
|
|
|
49 |
cm.startStopBtn = Y.one('button#start-stop');
|
|
|
50 |
cm.uploadBtn = Y.one('button#upload');
|
|
|
51 |
cm.recType = 'audio';
|
|
|
52 |
cm.maxUploadSize = scope.get('maxrecsize');
|
|
|
53 |
|
|
|
54 |
// Show alert and close plugin if WebRTC is not supported.
|
|
|
55 |
ccm.check_has_gum();
|
|
|
56 |
// Show alert and redirect user if connection is not secure.
|
|
|
57 |
ccm.check_secure();
|
|
|
58 |
|
|
|
59 |
// Run when user clicks on "record" button.
|
|
|
60 |
cm.startStopBtn.on('click', function() {
|
|
|
61 |
cm.startStopBtn.set('disabled', true);
|
|
|
62 |
|
|
|
63 |
// If button is displaying "Start Recording" or "Record Again".
|
|
|
64 |
if ((cm.startStopBtn.get('textContent') === M.util.get_string('startrecording', 'atto_recordrtc')) ||
|
|
|
65 |
(cm.startStopBtn.get('textContent') === M.util.get_string('recordagain', 'atto_recordrtc')) ||
|
|
|
66 |
(cm.startStopBtn.get('textContent') === M.util.get_string('recordingfailed', 'atto_recordrtc'))) {
|
|
|
67 |
// Make sure the audio player and upload button are not shown.
|
|
|
68 |
cm.player.ancestor().ancestor().addClass('hide');
|
|
|
69 |
cm.uploadBtn.ancestor().ancestor().addClass('hide');
|
|
|
70 |
|
|
|
71 |
// Change look of recording button.
|
|
|
72 |
cm.startStopBtn.replaceClass('btn-outline-danger', 'btn-danger');
|
|
|
73 |
|
|
|
74 |
// Empty the array containing the previously recorded chunks.
|
|
|
75 |
cm.chunks = [];
|
|
|
76 |
cm.blobSize = 0;
|
|
|
77 |
cm.uploadBtn.detach('click');
|
|
|
78 |
|
|
|
79 |
// Initialize common configurations.
|
|
|
80 |
var commonConfig = {
|
|
|
81 |
// When the stream is captured from the microphone/webcam.
|
|
|
82 |
onMediaCaptured: function(stream) {
|
|
|
83 |
// Make audio stream available at a higher level by making it a property of the common module.
|
|
|
84 |
cm.stream = stream;
|
|
|
85 |
|
|
|
86 |
cm.start_recording(cm.recType, cm.stream);
|
|
|
87 |
},
|
|
|
88 |
|
|
|
89 |
// Revert button to "Record Again" when recording is stopped.
|
|
|
90 |
onMediaStopped: function(btnLabel) {
|
|
|
91 |
cm.startStopBtn.set('textContent', btnLabel);
|
|
|
92 |
cm.startStopBtn.set('disabled', false);
|
|
|
93 |
cm.startStopBtn.replaceClass('btn-danger', 'btn-outline-danger');
|
|
|
94 |
},
|
|
|
95 |
|
|
|
96 |
// Handle recording errors.
|
|
|
97 |
onMediaCapturingFailed: function(error) {
|
|
|
98 |
am.handle_gum_errors(error, commonConfig);
|
|
|
99 |
}
|
|
|
100 |
};
|
|
|
101 |
|
|
|
102 |
// Capture audio stream from microphone.
|
|
|
103 |
M.atto_recordrtc.audiomodule.capture_audio(commonConfig);
|
|
|
104 |
} else { // If button is displaying "Stop Recording".
|
|
|
105 |
// First of all clears the countdownTicker.
|
|
|
106 |
window.clearInterval(cm.countdownTicker);
|
|
|
107 |
|
|
|
108 |
// Disable "Record Again" button for 1s to allow background processing (closing streams).
|
|
|
109 |
window.setTimeout(function() {
|
|
|
110 |
cm.startStopBtn.set('disabled', false);
|
|
|
111 |
}, 1000);
|
|
|
112 |
|
|
|
113 |
// Stop recording.
|
|
|
114 |
cm.stop_recording(cm.stream);
|
|
|
115 |
|
|
|
116 |
// Change button to offer to record again.
|
|
|
117 |
cm.startStopBtn.set('textContent', M.util.get_string('recordagain', 'atto_recordrtc'));
|
|
|
118 |
cm.startStopBtn.replaceClass('btn-danger', 'btn-outline-danger');
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
// Get dialogue centered.
|
|
|
122 |
cm.editorScope.getDialogue().centered();
|
|
|
123 |
});
|
|
|
124 |
},
|
|
|
125 |
|
|
|
126 |
// Setup to get audio stream from microphone.
|
|
|
127 |
capture_audio: function(config) {
|
|
|
128 |
cm.capture_user_media(
|
|
|
129 |
// Media constraints.
|
|
|
130 |
{
|
|
|
131 |
audio: true
|
|
|
132 |
},
|
|
|
133 |
|
|
|
134 |
// Success callback.
|
|
|
135 |
function(audioStream) {
|
|
|
136 |
// Set audio player source to microphone stream.
|
|
|
137 |
cm.playerDOM.srcObject = audioStream;
|
|
|
138 |
|
|
|
139 |
config.onMediaCaptured(audioStream);
|
|
|
140 |
},
|
|
|
141 |
|
|
|
142 |
// Error callback.
|
|
|
143 |
function(error) {
|
|
|
144 |
config.onMediaCapturingFailed(error);
|
|
|
145 |
}
|
|
|
146 |
);
|
|
|
147 |
}
|
|
|
148 |
};
|