Passed
Pull Request — master (#4)
by Jacob
01:34
created

yui/src/recording/js/videomodule.js   B

Complexity

Total Complexity 39
Complexity/F 1.86

Size

Lines of Code 254
Function Count 21

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 22
Bugs 1 Features 0
Metric Value
cc 0
c 22
b 1
f 0
nc 12
dl 0
loc 254
rs 8.2857
wmc 39
mnd 5
bc 32
fnc 21
bpm 1.5238
cpm 1.8571
noi 19

3 Functions

Rating   Name   Duplication   Size   Complexity  
B M.atto_recordrtc.videomodule.capture_audio_video 0 26 1
B M.atto_recordrtc.videomodule.init 0 162 1
A M.atto_recordrtc.videomodule.stop_recording 0 56 1
1
// Atto recordrtc library functions.
2
// @package    atto_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
/*jshint es5: true */
8
/*jshint onevar: false */
9
/*jshint shadow: true */
10
/*global M */
11
/*global MediaRecorder */
12
/*global URL */
13
/*global InstallTrigger */
14
15
M.atto_recordrtc = M.atto_recordrtc || {};
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
16
17
// Shorten access to M.atto_recordrtc.commonmodule namespace.
18
var cm = M.atto_recordrtc.commonmodule;
19
20
M.atto_recordrtc.videomodule = {
21
    init: function(scope) {
22
        // Assignment of global variables.
23
        cm.editorScope = scope; // Allows access to the editor's "this" context.
24
        cm.alertWarning = Y.one('div#alert-warning');
0 ignored issues
show
Bug introduced by
The variable Y seems to be never declared. If this is a global, consider adding a /** global: Y */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
25
        cm.alertDanger = Y.one('div#alert-danger');
26
        cm.player = Y.one('video#player');
27
        cm.playerDOM = document.querySelector('video#player');
28
        cm.startStopBtn = Y.one('button#start-stop');
29
        cm.uploadBtn = Y.one('button#upload');
30
        cm.recType = 'video';
31
        cm.olderMoodle = scope.get('oldermoodle');
32
        // Extract the numbers from the string, and convert to bytes.
33
        cm.maxUploadSize = parseInt(scope.get('maxrecsize').match(/\d+/)[0], 10) * Math.pow(1024, 2);
34
35
        // Show alert and redirect user if connection is not secure.
36
        cm.check_secure();
37
        // Show alert if using non-ideal browser.
38
        cm.check_browser();
39
40
        // Run when user clicks on "record" button.
41
        cm.startStopBtn.on('click', function() {
42
            cm.startStopBtn.set('disabled', true);
43
44
            // If button is displaying "Start Recording" or "Record Again".
45
            if ((cm.startStopBtn.get('textContent') === M.util.get_string('startrecording', 'atto_recordrtc')) ||
46
                (cm.startStopBtn.get('textContent') === M.util.get_string('recordagain', 'atto_recordrtc')) ||
47
                (cm.startStopBtn.get('textContent') === M.util.get_string('recordingfailed', 'atto_recordrtc'))) {
48
                // Make sure the upload button is not shown.
49
                cm.uploadBtn.ancestor().ancestor().addClass('hide');
50
51
                // Change look of recording button.
52
                if (!cm.olderMoodle) {
53
                    cm.startStopBtn.replaceClass('btn-outline-danger', 'btn-danger');
54
                }
55
56
                // Empty the array containing the previously recorded chunks.
57
                cm.chunks = [];
58
                cm.blobSize = 0;
59
60
                // Initialize common configurations.
61
                var commonConfig = {
62
                    // When the stream is captured from the microphone/webcam.
63
                    onMediaCaptured: function(stream) {
64
                        // Make video stream available at a higher level by making it a property of the common module.
65
                        cm.stream = stream;
66
67
                        cm.start_recording(cm.recType, cm.stream);
68
                    },
69
70
                    // Revert button to "Record Again" when recording is stopped.
71
                    onMediaStopped: function(btnLabel) {
72
                        cm.startStopBtn.set('textContent', btnLabel);
73
                    },
74
75
                    // Handle recording errors.
76
                    onMediaCapturingFailed: function(error) {
77
                        var btnLabel = null;
78
79
                        // Handle getUserMedia-thrown errors.
80
                        switch (error.name) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
81
                            case 'AbortError':
82
                                Y.use('moodle-core-notification-alert', function() {
0 ignored issues
show
Bug introduced by
The variable Y seems to be never declared. If this is a global, consider adding a /** global: Y */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
83
                                    new M.core.alert({
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new M.core.alert({Identi...ode(atto_recordrtc)))}) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
84
                                        title: M.util.get_string('gumabort_title', 'atto_recordrtc'),
85
                                        message: M.util.get_string('gumabort', 'atto_recordrtc')
86
                                    });
87
                                });
88
89
                                btnLabel = M.util.get_string('recordingfailed', 'atto_recordrtc');
90
                                break;
91
                            case 'NotAllowedError':
92
                                Y.use('moodle-core-notification-alert', function() {
0 ignored issues
show
Bug introduced by
The variable Y seems to be never declared. If this is a global, consider adding a /** global: Y */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
93
                                    new M.core.alert({
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new M.core.alert({Identi...ode(atto_recordrtc)))}) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
94
                                        title: M.util.get_string('gumnotallowed_title', 'atto_recordrtc'),
95
                                        message: M.util.get_string('gumnotallowed', 'atto_recordrtc')
96
                                    });
97
                                });
98
99
                                btnLabel = M.util.get_string('recordingfailed', 'atto_recordrtc');
100
                                break;
101
                            case 'NotFoundError':
102
                                Y.use('moodle-core-notification-alert', function() {
0 ignored issues
show
Bug introduced by
The variable Y seems to be never declared. If this is a global, consider adding a /** global: Y */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
103
                                    new M.core.alert({
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new M.core.alert({Identi...ode(atto_recordrtc)))}) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
104
                                        title: M.util.get_string('gumnotfound_title', 'atto_recordrtc'),
105
                                        message: M.util.get_string('gumnotfound', 'atto_recordrtc')
106
                                    });
107
                                });
108
109
                                btnLabel = M.util.get_string('recordingfailed', 'atto_recordrtc');
110
                                break;
111
                            case 'NotReadableError':
112
                                Y.use('moodle-core-notification-alert', function() {
0 ignored issues
show
Bug introduced by
The variable Y seems to be never declared. If this is a global, consider adding a /** global: Y */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
113
                                    new M.core.alert({
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new M.core.alert({Identi...ode(atto_recordrtc)))}) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
114
                                        title: M.util.get_string('gumnotreadable_title', 'atto_recordrtc'),
115
                                        message: M.util.get_string('gumnotreadable', 'atto_recordrtc')
116
                                    });
117
                                });
118
119
                                btnLabel = M.util.get_string('recordingfailed', 'atto_recordrtc');
120
                                break;
121
                            case 'OverConstrainedError':
122
                                Y.use('moodle-core-notification-alert', function() {
0 ignored issues
show
Bug introduced by
The variable Y seems to be never declared. If this is a global, consider adding a /** global: Y */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
123
                                    new M.core.alert({
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new M.core.alert({Identi...ode(atto_recordrtc)))}) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
124
                                        title: M.util.get_string('gumoverconstrained_title', 'atto_recordrtc'),
125
                                        message: M.util.get_string('gumoverconstrained', 'atto_recordrtc')
126
                                    });
127
                                });
128
129
                                btnLabel = M.util.get_string('recordingfailed', 'atto_recordrtc');
130
                                break;
131
                            case 'SecurityError':
132
                                Y.use('moodle-core-notification-alert', function() {
0 ignored issues
show
Bug introduced by
The variable Y seems to be never declared. If this is a global, consider adding a /** global: Y */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
133
                                    new M.core.alert({
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new M.core.alert({Identi...ode(atto_recordrtc)))}) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
134
                                        title: M.util.get_string('gumsecurity_title', 'atto_recordrtc'),
135
                                        message: M.util.get_string('gumsecurity', 'atto_recordrtc')
136
                                    });
137
                                });
138
139
                                cm.editorScope.closeDialogue(cm.editorScope);
140
                                break;
141
                            case 'TypeError':
142
                                Y.use('moodle-core-notification-alert', function() {
0 ignored issues
show
Bug introduced by
The variable Y seems to be never declared. If this is a global, consider adding a /** global: Y */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
143
                                    new M.core.alert({
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new M.core.alert({Identi...ode(atto_recordrtc)))}) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
144
                                        title: M.util.get_string('gumtype_title', 'atto_recordrtc'),
145
                                        message: M.util.get_string('gumtype', 'atto_recordrtc')
146
                                    });
147
                                });
148
149
                                btnLabel = M.util.get_string('recordingfailed', 'atto_recordrtc');
150
                        }
151
152
                        // Proceed to treat as a stopped recording.
153
                        commonConfig.onMediaStopped(btnLabel);
154
                    }
155
                };
156
157
                // Show video tag without controls to view webcam stream.
158
                cm.player.ancestor().ancestor().removeClass('hide');
159
                cm.player.set('controls', false);
160
161
                // Capture audio+video stream from webcam/microphone.
162
                M.atto_recordrtc.videomodule.capture_audio_video(commonConfig);
163
            } else { // If button is displaying "Stop Recording".
164
                // First of all clears the countdownTicker.
165
                clearInterval(cm.countdownTicker);
166
167
                // Disable "Record Again" button for 1s to allow background processing (closing streams).
168
                setTimeout(function() {
169
                    cm.startStopBtn.set('disabled', false);
170
                }, 1000);
171
172
                // Stop recording.
173
                M.atto_recordrtc.videomodule.stop_recording(cm.stream);
174
175
                // Change button to offer to record again.
176
                cm.startStopBtn.set('textContent', M.util.get_string('recordagain', 'atto_recordrtc'));
177
                if (!cm.olderMoodle) {
178
                    cm.startStopBtn.replaceClass('btn-danger', 'btn-outline-danger');
179
                }
180
            }
181
        });
182
    },
183
184
    // Setup to get audio+video stream from microphone/webcam.
185
    capture_audio_video: function(config) {
186
        cm.capture_user_media(
187
            // Media constraints.
188
            {
189
                audio: true,
190
                video: {
191
                    width: {ideal: 640},
192
                    height: {ideal: 480}
193
                }
194
            },
195
196
            // Success callback.
197
            function(audioVideoStream) {
198
                // Set video player source to microphone+webcam stream, and play it back as it's recording.
199
                cm.playerDOM.srcObject = audioVideoStream;
200
                cm.playerDOM.play();
201
202
                config.onMediaCaptured(audioVideoStream);
203
            },
204
205
            // Error callback.
206
            function(error) {
207
                config.onMediaCapturingFailed(error);
208
            }
209
        );
210
    },
211
212
    stop_recording: function(stream) {
213
        // Stop recording microphone stream.
214
        cm.mediaRecorder.stop();
215
216
        // Stop each individual MediaTrack.
217
        stream.getTracks().forEach(function(track) {
218
            track.stop();
219
        });
220
221
        // Set source of video player.
222
        var blob = new Blob(cm.chunks, {type: cm.mediaRecorder.mimeType});
0 ignored issues
show
Bug introduced by
The variable Blob seems to be never declared. If this is a global, consider adding a /** global: Blob */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
223
        cm.player.set('src', URL.createObjectURL(blob));
224
225
        // Enable controls for video player, and unmute.
226
        cm.player.set('muted', false);
227
        cm.player.set('controls', true);
228
229
        // Show upload button.
230
        cm.uploadBtn.ancestor().ancestor().removeClass('hide');
231
        cm.uploadBtn.set('textContent', M.util.get_string('attachrecording', 'atto_recordrtc'));
232
        cm.uploadBtn.set('disabled', false);
233
234
        // Handle when upload button is clicked.
235
        cm.uploadBtn.on('click', function() {
236
            // Trigger error if no recording has been made.
237
            if (!cm.player.get('src') || cm.chunks === []) {
238
                Y.use('moodle-core-notification-alert', function() {
0 ignored issues
show
Bug introduced by
The variable Y seems to be never declared. If this is a global, consider adding a /** global: Y */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
239
                    new M.core.alert({
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new M.core.alert({Identi...ode(atto_recordrtc)))}) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
240
                        title: M.util.get_string('norecordingfound_title', 'atto_recordrtc'),
241
                        message: M.util.get_string('norecordingfound', 'atto_recordrtc')
242
                    });
243
                });
244
            } else {
245
                cm.uploadBtn.set('disabled', true);
246
247
                // Upload recording to server.
248
                cm.upload_to_server(cm.recType, function(progress, fileURLOrError) {
249
                    if (progress === 'ended') { // Insert annotation in text.
250
                        cm.uploadBtn.set('disabled', false);
251
                        cm.insert_annotation(cm.recType, fileURLOrError);
252
                    } else if (progress === 'upload-failed') { // Show error message in upload button.
253
                        cm.uploadBtn.set('disabled', false);
254
                        cm.uploadBtn.set('textContent', M.util.get_string('uploadfailed', 'atto_recordrtc') + ' ' + fileURLOrError);
255
                    } else if (progress === 'upload-failed-404') { // 404 error = File too large in Moodle.
256
                        cm.uploadBtn.set('disabled', false);
257
                        cm.uploadBtn.set('textContent', M.util.get_string('uploadfailed404', 'atto_recordrtc'));
258
                    } else if (progress === 'upload-aborted') {
259
                        cm.uploadBtn.set('disabled', false);
260
                        cm.uploadBtn.set('textContent', M.util.get_string('uploadaborted', 'atto_recordrtc') + ' ' + fileURLOrError);
261
                    } else {
262
                        cm.uploadBtn.set('textContent', progress);
263
                    }
264
                });
265
            }
266
        });
267
    }
268
};
269