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