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) { |
|
|
|
|
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) { |
|
|
|
|
55
|
|
|
startStopBtn.set('textContent', 'Start Recording'); |
|
|
|
|
56
|
|
|
|
57
|
|
|
socket.emit('recording stopped'); |
58
|
|
|
|
59
|
|
|
socket.on('save finished', function(path) { |
60
|
|
|
player.set('src', path); |
|
|
|
|
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, |
|
|
|
|
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) |
|
|
|
|
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')); |
|
|
|
|
128
|
|
|
startStopBtn.set('disabled', false); |
129
|
|
|
|
130
|
|
|
// Mute audio, distracting while recording. |
131
|
|
|
player.set('muted', true); |
|
|
|
|
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
|
|
|
|
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.