Completed
Push — master ( 93164a...2ac899 )
by Jacob
01:57
created

tinymce/js/premiumvideomodule.js   A

Complexity

Total Complexity 29
Complexity/F 2.07

Size

Lines of Code 178
Function Count 14

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 3
dl 0
loc 178
rs 10
wmc 29
mnd 2
bc 21
fnc 14
bpm 1.5
cpm 2.0714
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
B M.tinymce_recordrtc.stop_recording_video 0 26 1
B M.tinymce_recordrtc.capture_audio_video 0 26 1
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('video#player');
29
    playerDOM = document.querySelector('video#player');
30
    startStopBtn = Y.one('button#start-stop');
31
    uploadBtn = Y.one('button#upload');
32
    recType = 'video';
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 upload button is not shown.
52
            uploadBtn.ancestor().ancestor().addClass('hide');
53
54
            // Change look of recording button.
55
            if (!recordrtc.oldermoodle) {
56
                startStopBtn.replaceClass('btn-outline-danger', 'btn-danger');
57
            }
58
59
            // Initialize common configurations.
60
            var commonConfig = {
61
                // When the stream is captured from the microphone/webcam.
62
                onMediaCaptured: function(stream) {
63
                    // Make video stream available at a higher level by making it a property of startStopBtn.
64
                    startStopBtn.stream = stream;
65
66
                    M.tinymce_recordrtc.start_recording(recType, startStopBtn.stream);
67
                },
68
69
                // Revert button to "Record Again" when recording is stopped.
70
                onMediaStopped: function(btnLabel) {
71
                    startStopBtn.set('textContent', btnLabel);
72
                    startStopBtn.set('disabled', false);
73
                    if (!recordrtc.oldermoodle) {
74
                        startStopBtn.replaceClass('btn-danger', 'btn-outline-danger');
75
                    }
76
                },
77
78
                // Handle recording errors.
79
                onMediaCapturingFailed: function(error) {
80
                    var btnLabel = M.util.get_string('recordingfailed', 'tinymce_recordrtc');
81
                    var treatAsStopped = function() {
82
                        commonConfig.onMediaStopped(btnLabel);
83
                    };
84
85
                    // Handle getUserMedia-thrown errors.
86
                    // After alert, proceed to treat as stopped recording, or close dialogue.
87
                    switch (error.name) {
88
                        case 'AbortError':
89
                            M.tinymce_recordrtc.show_alert('gumabort', treatAsStopped);
90
91
                            break;
92
                        case 'NotAllowedError':
93
                            M.tinymce_recordrtc.show_alert('gumnotallowed', treatAsStopped);
94
95
                            break;
96
                        case 'NotFoundError':
97
                            M.tinymce_recordrtc.show_alert('gumnotfound', treatAsStopped);
98
99
                            break;
100
                        case 'NotReadableError':
101
                            M.tinymce_recordrtc.show_alert('gumnotreadable', treatAsStopped);
102
103
                            break;
104
                        case 'OverConstrainedError':
105
                            M.tinymce_recordrtc.show_alert('gumoverconstrained', treatAsStopped);
106
107
                            break;
108
                        case 'SecurityError':
109
                            M.tinymce_recordrtc.show_alert('gumsecurity', function() {
110
                                tinyMCEPopup.close();
111
                            });
112
113
                            break;
114
                        case 'TypeError':
115
                            M.tinymce_recordrtc.show_alert('gumtype', treatAsStopped);
116
117
                            break;
118
                        default:
119
                            break;
120
                    }
121
                }
122
            };
123
124
            // Show video tag without controls to view webcam stream.
125
            player.ancestor().ancestor().removeClass('hide');
126
            player.set('controls', false);
127
128
            // Capture audio+video stream from webcam/microphone.
129
            M.tinymce_recordrtc.capture_audio_video(commonConfig);
130
        } else { // If button is displaying "Stop Recording".
131
            // Disable "Record Again" button for 1s to allow background processing (closing streams).
132
            window.setTimeout(function() {
133
                startStopBtn.set('disabled', false);
134
            }, 1000);
135
136
            // Stop recording.
137
            M.tinymce_recordrtc.stop_recording_video(startStopBtn.stream);
138
139
            // Change button to offer to record again.
140
            startStopBtn.set('textContent', M.util.get_string('recordagain', 'tinymce_recordrtc'));
141
            if (!recordrtc.oldermoodle) {
142
                startStopBtn.replaceClass('btn-danger', 'btn-outline-danger');
143
            }
144
        }
145
    });
146
};
147
148
// Setup to get audio+video stream from microphone/webcam.
149
M.tinymce_recordrtc.capture_audio_video = function(config) {
150
    M.tinymce_recordrtc.capture_user_media(
151
        // Media constraints.
152
        {
153
            audio: true,
154
            video: {
155
                width: {ideal: 640},
156
                height: {ideal: 480}
157
            }
158
        },
159
160
        // Success callback.
161
        function(audioVideoStream) {
162
            // Set video player source to microphone+webcam stream, and play it back as it's recording.
163
            playerDOM.srcObject = audioVideoStream;
164
            playerDOM.play();
165
166
            config.onMediaCaptured(audioVideoStream);
167
        },
168
169
        // Error callback.
170
        function(error) {
171
            config.onMediaCapturingFailed(error);
172
        }
173
    );
174
};
175
176
M.tinymce_recordrtc.stop_recording_video = function(stream) {
177
    // Stop recording microphone stream.
178
    mediaRecorder.stop();
179
180
    // Stop each individual MediaTrack.
181
    stream.getTracks().forEach(function(track) {
182
        track.stop();
183
    });
184
185
    // Show upload button.
186
    uploadBtn.ancestor().ancestor().removeClass('hide');
187
    uploadBtn.set('textContent', M.util.get_string('attachrecording', 'tinymce_recordrtc'));
188
    uploadBtn.set('disabled', false);
189
190
    // Handle when upload button is clicked.
191
    uploadBtn.on('click', function() {
192
        // Trigger error if no recording has been made.
193
        if (!player.get('src')) {
194
            M.tinymce_recordrtc.show_alert('norecordingfound');
195
        } else {
196
            uploadBtn.set('disabled', true);
197
198
            M.tinymce_recordrtc.insert_annotation(recType, player.get('src'));
199
        }
200
    });
201
};
202