Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
//
16
 
17
/**
18
 * Atto recordrtc library functions
19
 *
20
 * @package    atto_recordrtc
21
 * @author     Jesus Federico (jesus [at] blindsidenetworks [dt] com)
22
 * @author     Jacob Prud'homme (jacob [dt] prudhomme [at] blindsidenetworks [dt] com)
23
 * @copyright  2017 Blindside Networks Inc.
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
 
27
// ESLint directives.
28
/* eslint-disable camelcase, spaced-comment */
29
 
30
// Scrutinizer CI directives.
31
/** global: M */
32
/** global: Y */
33
 
34
M.atto_recordrtc = M.atto_recordrtc || {};
35
 
36
// Shorten access to module namespaces.
37
var cm = M.atto_recordrtc.commonmodule,
38
    am = M.atto_recordrtc.abstractmodule,
39
    ccm = M.atto_recordrtc.compatcheckmodule;
40
 
41
M.atto_recordrtc.videomodule = {
42
    init: function(scope) {
43
        // Assignment of global variables.
44
        cm.editorScope = scope; // Allows access to the editor's "this" context.
45
        cm.alertWarning = Y.one('div#alert-warning');
46
        cm.alertDanger = Y.one('div#alert-danger');
47
        cm.player = Y.one('video#player');
48
        cm.playerDOM = document.querySelector('video#player');
49
        cm.startStopBtn = Y.one('button#start-stop');
50
        cm.uploadBtn = Y.one('button#upload');
51
        cm.recType = 'video';
52
        cm.maxUploadSize = scope.get('maxrecsize');
53
 
54
        // Show alert and close plugin if WebRTC is not supported.
55
        ccm.check_has_gum();
56
        // Show alert and redirect user if connection is not secure.
57
        ccm.check_secure();
58
 
59
        // Run when user clicks on "record" button.
60
        cm.startStopBtn.on('click', function() {
61
            cm.startStopBtn.set('disabled', true);
62
 
63
            // If button is displaying "Start Recording" or "Record Again".
64
            if ((cm.startStopBtn.get('textContent') === M.util.get_string('startrecording', 'atto_recordrtc')) ||
65
                (cm.startStopBtn.get('textContent') === M.util.get_string('recordagain', 'atto_recordrtc')) ||
66
                (cm.startStopBtn.get('textContent') === M.util.get_string('recordingfailed', 'atto_recordrtc'))) {
67
                // Make sure the upload button is not shown.
68
                cm.uploadBtn.ancestor().ancestor().addClass('hide');
69
 
70
                // Change look of recording button.
71
                cm.startStopBtn.replaceClass('btn-outline-danger', 'btn-danger');
72
 
73
                // Empty the array containing the previously recorded chunks.
74
                cm.chunks = [];
75
                cm.blobSize = 0;
76
                cm.uploadBtn.detach('click');
77
 
78
                // Initialize common configurations.
79
                var commonConfig = {
80
                    // When the stream is captured from the microphone/webcam.
81
                    onMediaCaptured: function(stream) {
82
                        // Make video stream available at a higher level by making it a property of the common module.
83
                        cm.stream = stream;
84
 
85
                        cm.start_recording(cm.recType, cm.stream);
86
                    },
87
 
88
                    // Revert button to "Record Again" when recording is stopped.
89
                    onMediaStopped: function(btnLabel) {
90
                        cm.startStopBtn.set('textContent', btnLabel);
91
                        cm.startStopBtn.set('disabled', false);
92
                        cm.startStopBtn.replaceClass('btn-danger', 'btn-outline-danger');
93
                    },
94
 
95
                    // Handle recording errors.
96
                    onMediaCapturingFailed: function(error) {
97
                        am.handle_gum_errors(error, commonConfig);
98
                    }
99
                };
100
 
101
                // Show video tag without controls to view webcam stream.
102
                cm.player.ancestor().ancestor().removeClass('hide');
103
                cm.player.set('controls', false);
104
 
105
                // Capture audio+video stream from webcam/microphone.
106
                M.atto_recordrtc.videomodule.capture_audio_video(commonConfig);
107
            } else { // If button is displaying "Stop Recording".
108
                // First of all clears the countdownTicker.
109
                window.clearInterval(cm.countdownTicker);
110
 
111
                // Disable "Record Again" button for 1s to allow background processing (closing streams).
112
                window.setTimeout(function() {
113
                    cm.startStopBtn.set('disabled', false);
114
                }, 1000);
115
 
116
                // Stop recording.
117
                cm.stop_recording(cm.stream);
118
 
119
                // Change button to offer to record again.
120
                cm.startStopBtn.set('textContent', M.util.get_string('recordagain', 'atto_recordrtc'));
121
                cm.startStopBtn.replaceClass('btn-danger', 'btn-outline-danger');
122
            }
123
 
124
            // Get dialogue centered.
125
            cm.editorScope.getDialogue().centered();
126
        });
127
    },
128
 
129
    // Setup to get audio+video stream from microphone/webcam.
130
    capture_audio_video: function(config) {
131
        cm.capture_user_media(
132
            // Media constraints.
133
            {
134
                audio: true,
135
                video: {
136
                    width: {ideal: 640},
137
                    height: {ideal: 480}
138
                }
139
            },
140
 
141
            // Success callback.
142
            function(audioVideoStream) {
143
                // Set video player source to microphone+webcam stream, and play it back as it's recording.
144
                cm.playerDOM.srcObject = audioVideoStream;
145
                cm.playerDOM.play();
146
 
147
                config.onMediaCaptured(audioVideoStream);
148
            },
149
 
150
            // Error callback.
151
            function(error) {
152
                config.onMediaCapturingFailed(error);
153
            }
154
        );
155
    }
156
};