tinymce/js/videomodule.js   A
last analyzed

Complexity

Total Complexity 15
Complexity/F 1.67

Size

Lines of Code 118
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 40
Bugs 4 Features 1
Metric Value
cc 0
c 40
b 4
f 1
nc 3
dl 0
loc 118
rs 10
wmc 15
mnd 2
bc 14
fnc 9
bpm 1.5555
cpm 1.6666
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
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
// @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('video#player');
42
    playerDOM = document.querySelector('video#player');
43
    startStopBtn = Y.one('button#start-stop');
44
    uploadBtn = Y.one('button#upload');
45
    recType = 'video';
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 upload button is not shown.
65
            uploadBtn.ancestor().ancestor().addClass('hide');
66
67
            // Change look of recording button.
68
            if (!recordrtc.oldermoodle) {
69
                startStopBtn.replaceClass('btn-outline-danger', 'btn-danger');
70
            }
71
72
            // Empty the array containing the previously recorded chunks.
73
            chunks = [];
74
            blobSize = 0;
75
76
            // Initialize common configurations.
77
            var commonConfig = {
78
                // When the stream is captured from the microphone/webcam.
79
                onMediaCaptured: function(stream) {
80
                    // Make video stream available at a higher level by making it a property of startStopBtn.
81
                    startStopBtn.stream = stream;
82
83
                    M.tinymce_recordrtc.start_recording(recType, startStopBtn.stream);
84
                },
85
86
                // Revert button to "Record Again" when recording is stopped.
87
                onMediaStopped: function(btnLabel) {
88
                    startStopBtn.set('textContent', btnLabel);
89
                    startStopBtn.set('disabled', false);
90
                    if (!recordrtc.oldermoodle) {
91
                        startStopBtn.replaceClass('btn-danger', 'btn-outline-danger');
92
                    }
93
                },
94
95
                // Handle recording errors.
96
                onMediaCapturingFailed: function(error) {
97
                    M.tinymce_recordrtc.handle_gum_errors(error, commonConfig);
98
                }
99
            };
100
101
            // Show video tag without controls to view webcam stream.
102
            player.ancestor().ancestor().removeClass('hide');
103
            player.set('controls', false);
104
105
            // Capture audio+video stream from webcam/microphone.
106
            M.tinymce_recordrtc.capture_audio_video(commonConfig);
107
        } else { // If button is displaying "Stop Recording".
108
            // First of all clears the countdownTicker.
109
            window.clearInterval(countdownTicker);
110
111
            // Disable "Record Again" button for 1s to allow background processing (closing streams).
112
            window.setTimeout(function() {
113
                startStopBtn.set('disabled', false);
114
            }, 1000);
115
116
            // Stop recording.
117
            M.tinymce_recordrtc.stop_recording(startStopBtn.stream);
118
119
            // Change button to offer to record again.
120
            startStopBtn.set('textContent', M.util.get_string('recordagain', 'tinymce_recordrtc'));
121
            if (!recordrtc.oldermoodle) {
122
                startStopBtn.replaceClass('btn-danger', 'btn-outline-danger');
123
            }
124
        }
125
    });
126
};
127
128
// Setup to get audio+video stream from microphone/webcam.
129
M.tinymce_recordrtc.capture_audio_video = function(config) {
130
    M.tinymce_recordrtc.capture_user_media(
131
        // Media constraints.
132
        {
133
            audio: true,
134
            video: {
135
                width: {ideal: 640},
136
                height: {ideal: 480}
137
            }
138
        },
139
140
        // Success callback.
141
        function(audioVideoStream) {
142
            // Set video player source to microphone+webcam stream, and play it back as it's recording.
143
            playerDOM.srcObject = audioVideoStream;
144
            playerDOM.play();
145
146
            config.onMediaCaptured(audioVideoStream);
147
        },
148
149
        // Error callback.
150
        function(error) {
151
            config.onMediaCapturingFailed(error);
152
        }
153
    );
154
};
155