Completed
Push — master ( 46959c...a001d3 )
by Jacob
01:35
created

tinymce/js/premiumhelpermodule.js   A

Complexity

Total Complexity 19
Complexity/F 1.73

Size

Lines of Code 127
Function Count 11

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 4
dl 0
loc 127
rs 10
wmc 19
mnd 4
bc 20
fnc 11
bpm 1.8181
cpm 1.7272
noi 8

4 Functions

Rating   Name   Duplication   Size   Complexity  
A M.tinymce_recordrtc.handle_stop 0 12 1
A M.tinymce_recordrtc.handle_data_available 0 3 1
B M.tinymce_recordrtc.init_connection 0 30 1
C M.tinymce_recordrtc.start_recording 0 70 9
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: tinyMCEPopup */
10
11
M.tinymce_recordrtc = M.tinymce_recordrtc || {};
12
13
// Initialize some variables.
14
var socket = null;
15
16
// Attempt to connect to the premium server via Socket.io.
17
M.tinymce_recordrtc.init_connection = function() {
18
    // Dialogue-closing behaviour.
19
    var closeDialogue = function() {
20
        tinyMCEPopup.close();
21
    };
22
23
    socket.connect();
24
25
    socket.on('connect', function() {
26
        // Send key and secret from Moodle settings
27
        socket.emit('authentication', {
28
            key: window.params.apikey,
29
            secret: window.params.apisecret
30
        });
31
32
        socket.on('authenticated', function() {
33
            // Continue as normal.
34
        });
35
36
        socket.on('unauthorized', function(err) {
0 ignored issues
show
Unused Code introduced by
The parameter err is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
37
            M.tinymce_recordrtc.show_alert('notpremium', closeDialogue);
38
        });
39
    });
40
41
    socket.on('connect_error', function() {
42
        socket.disconnect();
43
44
        M.tinymce_recordrtc.show_alert('servernotfound', closeDialogue);
45
    });
46
};
47
48
// Push chunks of audio/video to server when made available.
49
M.tinymce_recordrtc.handle_data_available = function(event) {
50
    socket.emit('data available', event.data);
51
};
52
53
// Stop recording and handle end.
54
M.tinymce_recordrtc.handle_stop = function(event) {
0 ignored issues
show
Unused Code introduced by
The parameter event is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
55
    startStopBtn.set('textContent', 'Start Recording');
0 ignored issues
show
Bug introduced by
The variable startStopBtn seems to be never declared. If this is a global, consider adding a /** global: startStopBtn */ 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...
56
57
    socket.emit('recording stopped');
58
59
    socket.on('save finished', function(path) {
60
        player.set('src', path);
0 ignored issues
show
Bug introduced by
The variable player seems to be never declared. If this is a global, consider adding a /** global: player */ 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...
61
        player.set('controls', true);
62
        player.set('muted', false);
63
        player.ancestor().ancestor().removeClass('hide'); // AUDIO ONLY
64
    });
65
};
66
67
// Get everything set up to start recording.
68
M.tinymce_recordrtc.start_recording = function(type, stream) {
69
    // Generate filename with random ID and file extension.
70
    var fileName = (Math.random() * 1000).toString().replace('.', '');
71
    if (type === 'audio') {
72
        fileName += '-audio.ogg';
73
    } else {
74
        fileName += '-video.webm';
75
    }
76
77
    var data = {
78
        contextid: window.params.contextid,
79
        type: recType,
0 ignored issues
show
Bug introduced by
The variable recType seems to be never declared. If this is a global, consider adding a /** global: recType */ 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...
80
        itemid: window.params.sesskey, // Use session key as item ID.
81
        filename: fileName
82
    };
83
    socket.emit('recording started', data);
84
85
    // The options for the recording codecs and bitrates.
86
    var options = null;
87
    if (type === 'audio') {
88
        if (window.MediaRecorder.isTypeSupported('audio/webm;codecs=opus')) {
89
            options = {
90
                audioBitsPerSecond: window.params.audiobitrate,
91
                mimeType: 'audio/webm;codecs=opus'
92
            };
93
        } else if (window.MediaRecorder.isTypeSupported('audio/ogg;codecs=opus')) {
94
            options = {
95
                audioBitsPerSecond: window.params.audiobitrate,
96
                mimeType: 'audio/ogg;codecs=opus'
97
            };
98
        }
99
    } else {
100
        if (window.MediaRecorder.isTypeSupported('video/webm;codecs=vp9,opus')) {
101
            options = {
102
                audioBitsPerSecond: window.params.audiobitrate,
103
                videoBitsPerSecond: window.params.videobitrate,
104
                mimeType: 'video/webm;codecs=vp9,opus'
105
            };
106
        } else if (window.MediaRecorder.isTypeSupported('video/webm;codecs=h264,opus')) {
107
            options = {
108
                audioBitsPerSecond: window.params.audiobitrate,
109
                videoBitsPerSecond: window.params.videobitrate,
110
                mimeType: 'video/webm;codecs=h264,opus'
111
            };
112
        } else if (window.MediaRecorder.isTypeSupported('video/webm;codecs=vp8,opus')) {
113
            options = {
114
                audioBitsPerSecond: window.params.audiobitrate,
115
                videoBitsPerSecond: window.params.videobitrate,
116
                mimeType: 'video/webm;codecs=vp8,opus'
117
            };
118
        }
119
    }
120
121
    // If none of the options above are supported, fall back on browser defaults.
122
    mediaRecorder = options ? new window.MediaRecorder(stream, options)
0 ignored issues
show
Bug introduced by
The variable mediaRecorder seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.mediaRecorder.
Loading history...
123
                            : new window.MediaRecorder(stream);
124
125
    socket.on('recording started', function() {
126
        // Make button clickable again, to allow stopping recording.
127
        startStopBtn.set('textContent', M.util.get_string('stoprecording', 'tinymce_recordrtc'));
0 ignored issues
show
Bug introduced by
The variable startStopBtn seems to be never declared. If this is a global, consider adding a /** global: startStopBtn */ 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...
128
        startStopBtn.set('disabled', false);
129
130
        // Mute audio, distracting while recording.
131
        player.set('muted', true);
0 ignored issues
show
Bug introduced by
The variable player seems to be never declared. If this is a global, consider adding a /** global: player */ 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...
132
133
        mediaRecorder.ondataavailable = M.tinymce_recordrtc.handle_data_available;
134
        mediaRecorder.onstop = M.tinymce_recordrtc.handle_stop;
135
        mediaRecorder.start(1500); // Capture in 1.5s chunks.
136
    });
137
};
138