-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathBridgeChannel.js
437 lines (370 loc) · 14.9 KB
/
BridgeChannel.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import { safeJsonParse } from '@jitsi/js-utils/json';
import { getLogger } from '@jitsi/logger';
import RTCEvents from '../../service/RTC/RTCEvents';
import { createBridgeChannelClosedEvent } from '../../service/statistics/AnalyticsEvents';
import Statistics from '../statistics/statistics';
const logger = getLogger(__filename);
/**
* Handles a WebRTC RTCPeerConnection or a WebSocket instance to communicate
* with the videobridge.
*/
export default class BridgeChannel {
/**
* Binds "ondatachannel" event listener on the given RTCPeerConnection
* instance, or creates a WebSocket connection with the videobridge.
* At least one of both, peerconnection or wsUrl parameters, must be
* given.
* @param {RTCPeerConnection|null} peerconnection WebRTC peer connection
* instance.
* @param {string|null} wsUrl WebSocket URL.
* @param {EventEmitter} emitter the EventEmitter instance to use for
* event emission.
* @param {JitsiConference} conference the conference instance.
*/
constructor(peerconnection, wsUrl, emitter, conference) {
if (!peerconnection && !wsUrl) {
throw new TypeError('At least peerconnection or wsUrl must be given');
} else if (peerconnection && wsUrl) {
throw new TypeError('Just one of peerconnection or wsUrl must be given');
}
if (peerconnection) {
logger.debug('constructor() with peerconnection');
} else {
logger.debug(`constructor() with wsUrl:"${wsUrl}"`);
}
// The underlying WebRTC RTCDataChannel or WebSocket instance.
// @type {RTCDataChannel|WebSocket}
this._channel = null;
// The conference that uses this bridge channel.
this._conference = conference;
// Whether the channel is connected or not. It will start as undefined
// for the first connection attempt. Then transition to either true or false.
this._connected = undefined;
// @type {EventEmitter}
this._eventEmitter = emitter;
// Whether a RTCDataChannel or WebSocket is internally used.
// @type {string} "datachannel" / "websocket"
this._mode = null;
// Indicates whether the connection retries are enabled or not.
this._areRetriesEnabled = false;
// Indicates whether the connection was closed from the client or not.
this._closedFromClient = false;
// If a RTCPeerConnection is given, listen for new RTCDataChannel
// event.
if (peerconnection) {
const datachannel
= peerconnection.createDataChannel(
'JVB data channel', {
protocol: 'http://jitsi.org/protocols/colibri'
});
// Handle the RTCDataChannel.
this._handleChannel(datachannel);
this._mode = 'datachannel';
// Otherwise create a WebSocket connection.
} else if (wsUrl) {
this._areRetriesEnabled = true;
this._wsUrl = wsUrl;
this._initWebSocket();
}
}
/**
* Initializes the web socket channel.
*
* @returns {void}
*/
_initWebSocket() {
// Create a WebSocket instance.
const ws = new WebSocket(this._wsUrl);
// Handle the WebSocket.
this._handleChannel(ws);
this._mode = 'websocket';
}
/**
* Starts the websocket connection retries.
*
* @returns {void}
*/
_startConnectionRetries() {
let timeoutS = 1;
const reload = () => {
const isConnecting = this._channel && (this._channel.readyState === 'connecting'
|| this._channel.readyState === WebSocket.CONNECTING);
// Should not spawn new websockets while one is already trying to connect.
if (isConnecting) {
// Timeout is still required as there is flag `_areRetriesEnabled` that
// blocks new retrying cycles until any channel opens in current cycle.
this._retryTimeout = setTimeout(reload, timeoutS * 1000);
return;
}
if (this.isOpen()) {
return;
}
this._initWebSocket(this._wsUrl);
timeoutS = Math.min(timeoutS * 2, 60);
this._retryTimeout = setTimeout(reload, timeoutS * 1000);
};
this._retryTimeout = setTimeout(reload, timeoutS * 1000);
}
/**
* Stops the websocket connection retries.
*
* @returns {void}
*/
_stopConnectionRetries() {
if (this._retryTimeout) {
clearTimeout(this._retryTimeout);
this._retryTimeout = undefined;
}
}
/**
* Retries to establish the websocket connection after the connection was closed by the server.
*
* @param {CloseEvent} closeEvent - The close event that triggered the retries.
* @returns {void}
*/
_retryWebSocketConnection(closeEvent) {
if (!this._areRetriesEnabled) {
return;
}
const { code, reason } = closeEvent;
Statistics.sendAnalytics(createBridgeChannelClosedEvent(code, reason));
this._areRetriesEnabled = false;
this._eventEmitter.once(RTCEvents.DATA_CHANNEL_OPEN, () => {
this._stopConnectionRetries();
this._areRetriesEnabled = true;
});
this._startConnectionRetries();
}
/**
* The channel mode.
* @return {string} "datachannel" or "websocket" (or null if not yet set).
*/
get mode() {
return this._mode;
}
/**
* Closes the currently opened channel.
*/
close() {
this._closedFromClient = true;
this._stopConnectionRetries();
this._areRetriesEnabled = false;
if (this._channel) {
try {
this._channel.close();
} catch (error) {} // eslint-disable-line no-empty
this._channel = null;
}
}
/**
* Whether there is an underlying RTCDataChannel or WebSocket and it's
* open.
* @return {boolean}
*/
isOpen() {
return this._channel && (this._channel.readyState === 'open'
|| this._channel.readyState === WebSocket.OPEN);
}
/**
* Sends local stats via the bridge channel.
* @param {Object} payload The payload of the message.
* @throws NetworkError/InvalidStateError/Error if the operation fails or if there is no data channel created.
*/
sendEndpointStatsMessage(payload) {
this._send({
colibriClass: 'EndpointStats',
...payload
});
}
/**
* Sends message via the channel.
* @param {string} to The id of the endpoint that should receive the
* message. If "" the message will be sent to all participants.
* @param {object} payload The payload of the message.
* @throws NetworkError or InvalidStateError from RTCDataChannel#send (@see
* {@link https://developer.mozilla.org/docs/Web/API/RTCDataChannel/send})
* or from WebSocket#send or Error with "No opened channel" message.
*/
sendMessage(to, payload) {
this._send({
colibriClass: 'EndpointMessage',
msgPayload: payload,
to
});
}
/**
* Sends a "lastN value changed" message via the channel.
* @param {number} value The new value for lastN. -1 means unlimited.
*/
sendSetLastNMessage(value) {
logger.log(`Sending lastN=${value}.`);
this._send({
colibriClass: 'LastNChangedEvent',
lastN: value
});
}
/**
* Sends a 'ReceiverVideoConstraints' message via the bridge channel.
*
* @param {ReceiverVideoConstraints} constraints video
* constraints.
*/
sendReceiverVideoConstraintsMessage(constraints) {
logger.log(`Sending ReceiverVideoConstraints with ${JSON.stringify(constraints)}`);
this._send({
colibriClass: 'ReceiverVideoConstraints',
...constraints
});
}
/**
* Sends a 'SourceVideoTypeMessage' message via the bridge channel.
*
* @param {BridgeVideoType} videoType - the video type.
* @param {SourceName} sourceName - the source name of the video track.
* @returns {void}
*/
sendSourceVideoTypeMessage(sourceName, videoType) {
logger.info(`Sending SourceVideoTypeMessage with video type ${sourceName}: ${videoType}`);
this._send({
colibriClass: 'SourceVideoTypeMessage',
sourceName,
videoType
});
}
/**
* Set events on the given RTCDataChannel or WebSocket instance.
*/
_handleChannel(channel) {
const emitter = this._eventEmitter;
channel.onopen = () => {
logger.info(`${this._mode} channel opened`);
this._connected = true;
emitter.emit(RTCEvents.DATA_CHANNEL_OPEN);
};
channel.onerror = event => {
// WS error events contain no information about the failure (this is available in the onclose event) and
// the event references the WS object itself, which causes hangs on mobile.
if (this._mode !== 'websocket') {
logger.error(`Channel error: ${event.message}`);
}
};
channel.onmessage = ({ data }) => {
// JSON object.
let obj;
try {
obj = safeJsonParse(data);
} catch (error) {
logger.error('Failed to parse channel message as JSON: ', data, error);
return;
}
const colibriClass = obj.colibriClass;
switch (colibriClass) {
case 'DominantSpeakerEndpointChangeEvent': {
const { dominantSpeakerEndpoint, previousSpeakers = [], silence } = obj;
logger.debug(`Dominant speaker: ${dominantSpeakerEndpoint}, previous speakers: ${previousSpeakers}`);
emitter.emit(RTCEvents.DOMINANT_SPEAKER_CHANGED, dominantSpeakerEndpoint, previousSpeakers, silence);
break;
}
case 'EndpointConnectivityStatusChangeEvent': {
const endpoint = obj.endpoint;
const isActive = obj.active === 'true';
logger.info(`Endpoint connection status changed: ${endpoint} active=${isActive}`);
emitter.emit(RTCEvents.ENDPOINT_CONN_STATUS_CHANGED, endpoint, isActive);
break;
}
case 'EndpointMessage': {
emitter.emit(RTCEvents.ENDPOINT_MESSAGE_RECEIVED, obj.from, obj.msgPayload);
break;
}
case 'EndpointStats': {
emitter.emit(RTCEvents.ENDPOINT_STATS_RECEIVED, obj.from, obj);
break;
}
case 'ForwardedSources': {
const forwardedSources = obj.forwardedSources;
logger.info(`New forwarded sources: ${forwardedSources}`);
emitter.emit(RTCEvents.FORWARDED_SOURCES_CHANGED, forwardedSources);
break;
}
case 'SenderSourceConstraints': {
if (typeof obj.sourceName === 'string' && typeof obj.maxHeight === 'number') {
logger.info(`SenderSourceConstraints: ${obj.sourceName} - ${obj.maxHeight}`);
emitter.emit(RTCEvents.SENDER_VIDEO_CONSTRAINTS_CHANGED, obj);
} else {
logger.error(`Invalid SenderSourceConstraints: ${obj.sourceName} - ${obj.maxHeight}`);
}
break;
}
case 'ServerHello': {
logger.info(`Received ServerHello, version=${obj.version}.`);
break;
}
case 'VideoSourcesMap': {
logger.info(`Received VideoSourcesMap: ${JSON.stringify(obj.mappedSources)}`);
emitter.emit(RTCEvents.VIDEO_SSRCS_REMAPPED, obj);
break;
}
case 'AudioSourcesMap': {
logger.info(`Received AudioSourcesMap: ${JSON.stringify(obj.mappedSources)}`);
emitter.emit(RTCEvents.AUDIO_SSRCS_REMAPPED, obj);
break;
}
default: {
logger.debug('Channel JSON-formatted message: ', obj);
// The received message appears to be appropriately formatted
// (i.e. is a JSON object which assigns a value to the
// mandatory property colibriClass) so don't just swallow it,
// expose it to public consumption.
emitter.emit(`rtc.datachannel.${colibriClass}`, obj);
}
}
};
channel.onclose = event => {
logger.debug(`Channel closed by ${this._closedFromClient ? 'client' : 'server'}`);
if (channel !== this._channel) {
logger.debug('Skip close handler, channel instance is not equal to stored one');
return;
}
// When the JVB closes the connection gracefully due to the participant being alone in the meeting it uses
// code 1001. However, the same code is also used by Cloudflare when it terminates the ws. Therefore, check
// for the number of remote participants in the call and abort retries only when the endpoint is the only
// endpoint in the call.
const isGracefulClose = this._closedFromClient
|| (event.code === 1001 && this._conference.getParticipantCount() === 1);
if (!isGracefulClose) {
const { code, reason } = event;
logger.error(`Channel closed: ${code} ${reason}`);
if (this._mode === 'websocket') {
this._retryWebSocketConnection(event);
// We only want to send this event the first time the failure happens.
if (this._connected !== false) {
emitter.emit(RTCEvents.DATA_CHANNEL_CLOSED, {
code,
reason
});
}
}
}
this._connected = false;
// Remove the channel.
this._channel = null;
};
// Store the channel.
this._channel = channel;
}
/**
* Sends passed object via the channel.
* @param {object} jsonObject The object that will be sent.
* @throws NetworkError or InvalidStateError from RTCDataChannel#send (@see
* {@link https://developer.mozilla.org/docs/Web/API/RTCDataChannel/send})
* or from WebSocket#send or Error with "No opened channel" message.
*/
_send(jsonObject) {
const channel = this._channel;
if (!this.isOpen()) {
logger.error('Bridge Channel send: no opened channel.');
throw new Error('No opened channel');
}
channel.send(JSON.stringify(jsonObject));
}
}