Source: lib/videoMail.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
var Janus = require('../externals/janus.nojquery');
var Promise = require('bluebird/js/release/bluebird');


/**
 * VideoMail usecase
 * @class
 * @classdesc VideoMail usecase
 * @param  {object} onEvents       Event handlers: onRecording, onStopped
 * @param  {object} domElements    DOM elements: videomail
 * @param  {object} options        Available options: stream
 * @return {Promise}               VideoMail methods: startRecording, stopRecording, toggleAudio, toggleVideo
 * @example
 * var onEvents = {
 *     onRecording: function() {
 *          // Recording
 *     },
 *     onStopped: function() {
 *          // Stopped
 *     }
 * };
 *
 * var domElements = {
 *     videomail: document.getElementById('videomail')
 * };
 *
 * var options = {
 *     stream: {
 *         audioEnabled: true,
 *         videoEnabled: true,
 *         aDeviceId: null,
 *         vDeviceId: null
 *     }
 * };
 *
 * usecases.videoMail(onEvents, domElements, options)
 *     .then(function(action) {
 *          // Use Case has been atacched succesfully
 *          ...
 *     })
 *     .catch(function(cause) {
 *         // Error attaching the Use Case
 *         console.log("Error Attach " + cause );
 *     })
 */
var videoMail = function(onEvents, domElements, options) {

    var videomailHandle = null;
    var recordingId = null;
    var bandwidth = 1024 * 1024;

    // Stream options
    var streamOptions = (options && options.stream) ? options.stream : {};
    if(!streamOptions.audioEnabled) streamOptions.audioEnabled = false;
    if(!streamOptions.videoEnabled) streamOptions.videoEnabled = false;
    if(!streamOptions.aDeviceId) streamOptions.aDeviceId = undefined;
    if(!streamOptions.vDeviceId) streamOptions.vDeviceId = undefined;

    /**
     * Close the current UseCase. It's recommended combine with disconnect method
     * @return {nothing}
     * @example
     * action.closeUsecase();
     * myVideoApp.disconnect(); // Recommended
     */
    var closeUsecase = function() {
        if(domElements) {
            for (var element in domElements) {
                if (!domElements.hasOwnProperty(element)) continue;
                var obj = domElements[element];
                if(obj && obj.innerHTML) obj.innerHTML = '';
            }
        }
    };

    /**
     * The User sends a request to start recording a Video message
     * @param  {string} recordPrefix  Prefix record
     * @param  {string} name          File name
     * @return {nothing}
     * @example
     * action.startRecording('__default__', 'MyRecordMobile');
     */
    var startRecording = function(recordPrefix, name) {
        var fullname = recordPrefix + "VideoMail-" + name + "-" + Date.now();
        // bitrate and keyframe interval can be set at any time:
        // before, after, during recording
        videomailHandle.send({
            'message': {
                'request': 'configure',
                'video-bitrate-max': bandwidth, // a quarter megabit
                'video-keyframe-interval': 15000 // 15 seconds
            }
        });
        videomailHandle.createOffer(
            {
                media: { // Publishers are sendonly
                    audioRecv: false,
                    audioSend: streamOptions.audioEnabled || false,
                    videoRecv: false,
                    videoSend: streamOptions.videoEnabled || false,
                    data: false,
                    audio: {
                        deviceId: {
                            exact: streamOptions.aDeviceId
                        }
                    },
                    video: {
                        deviceId: {
                            exact: streamOptions.vDeviceId
                        }
                    }
                },
                // By default, it's sendrecv for audio and video...
                success: function(jsep) {
                    Janus.debug("Got SDP!");
                    Janus.debug(jsep);
                    var body = { "request": "record", "name": fullname };
                    videomailHandle.send({"message": body, "jsep": jsep});
                },
                error: function(error) {
                    Janus.error("WebRTC error...", error);
                    videomailHandle.hangup();
                }
        });
    };

    /**
     * The User sends a request to stop recording the current Video message
     * @return {nothing}
     * @example
     * action.stopRecording();
     */
    var stopRecording = function() {
        var request = { "request": "stop" };
        videomailHandle.send({"message": request});
        videomailHandle.hangup();
    };

    /**
     * Toggle Audio stream (Mute/Unmute)
     * @return {boolean} Is audio muted?
     * @example
     * action.toggleAudio(); // true or false
     */
    var toggleAudio = function() {
        if (videomailHandle.isAudioMuted()) videomailHandle.unmuteAudio();
        else videomailHandle.muteAudio();
        return videomailHandle.isAudioMuted();
    };

    /**
     * Toggle Video stream (Mute/Unmute)
     * @return {boolean} Is video muted?
     * @example
     * action.toggleVideo(); // true or false
     */
    var toggleVideo = function() {
        if (videomailHandle.isVideoMuted()) videomailHandle.unmuteVideo();
        else videomailHandle.muteVideo();
        return videomailHandle.isVideoMuted();
    };

    return new Promise(function (resolve, reject) {
        window.VideoRTC.connection.handle.attach({
            plugin: "janus.plugin.recordplay",
            success: function(pluginHandle) {
                videomailHandle = pluginHandle;
                resolve({
                    closeUsecase: closeUsecase,
                    startRecording: startRecording,
                    stopRecording: stopRecording,
                    toggleAudio: toggleAudio,
                    toggleVideo: toggleVideo
                });
            },
            error: function(cause) {
                Janus.error("  -- Error attaching plugin...", cause);
                reject(cause);
            },
            consentDialog: function(on) {
                // Nothing
            },
            onmessage: function(msg, jsep) {
                var result = msg["result"];
                if(result !== null && result !== undefined) {
                    if(result["status"] !== undefined && result["status"] !== null) {
                        var event = result["status"];
                        if(event === 'recording') {
                            // Got an ANSWER to our recording OFFER
                            if(jsep !== null && jsep !== undefined)
                                videomailHandle.handleRemoteJsep({jsep: jsep});
                            var id = result["id"];
                            if(id !== null && id !== undefined) {
                                Janus.log("The ID of the current recording is " + id);
                                recordingId = id;
                            }
                            if(onEvents && onEvents.onRecording) onEvents.onRecording();
                        } else if(event === 'slow_link') {
                            var uplink = result["uplink"];
                            if(uplink !== 0) {
                                // Janus detected issues when receiving our media, let's slow down
                                bandwidth = parseInt(bandwidth / 1.5);
                                videomailHandle.send({
                                    'message': {
                                        'request': 'configure',
                                        'video-bitrate-max': bandwidth, // Reduce the bitrate
                                        'video-keyframe-interval': 15000 // Keep the 15 seconds key frame interval
                                    }
                                });
                            }
                        } else if(event === 'stopped') {
                            Janus.log("Session has stopped!");
                            var id = result["id"];
                            if(recordingId !== null && recordingId !== undefined) {
                                if(recordingId !== id) {
                                    Janus.warn("Not a stop to our recording?");
                                    return;
                                }
                            }
                            var container = domElements.videomail;
                            container.innerHTML = '';
                            videomailHandle.hangup();
                            if(recordingId !== null && onEvents && onEvents.onStopped) onEvents.onStopped();
                            recordingId = null;
                        }
                    }
                }
            },
            onlocalstream: function(stream) {
                Janus.debug(" ::: Got a local stream :::");
                var container = domElements.videomail;
                container.innerHTML = '<video autoplay poster="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" id="vmail"></video>';
                var videoElem = document.getElementById('vmail');
                Janus.attachMediaStream(videoElem, stream);
                videoElem.muted = true;

                var videoTracks = stream.getVideoTracks();
                if(videoTracks === null || videoTracks === undefined || videoTracks.length === 0) {
                    // No remote video
                    var nodeNoCamera = document.createElement('div');
                    nodeNoCamera.innerHTML = 'No camera available';
                    nodeNoCamera.style.position = "relative";
                    nodeNoCamera.style.height = "100%";
                    nodeNoCamera.style.width = "100%";
                    nodeNoCamera.style.top = "100%";
                    nodeNoCamera.style.left = "50%";
                    nodeNoCamera.style.transform = "translate(-50%,-50%)";
                    nodeNoCamera.className = "no-camera-available";
                    container.insertBefore(nodeNoCamera, container.firstChild);
                }
            },
            onremotestream: function(stream) {
                // Nothing
            },
            oncleanup: function() {
                var container = domElements.videomail;
                container.innerHTML = '';
            },
            detached: function() {
                // Nothing
            }
        });
    });

};

exports.videoMail = videoMail;