This repository was archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 381
/
Copy pathindex.js
419 lines (376 loc) · 17 KB
/
index.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
// Copyright (C) <2019> Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
'use strict';
var path = require('path');
var WrtcConnection = require('./wrtcConnection');
var Connections = require('./connections');
var InternalConnectionFactory = require('./InternalConnectionFactory');
var logger = require('../logger').logger;
// Logger
var log = logger.getLogger('WebrtcNode');
var addon = require('../rtcConn/build/Release/rtcConn.node');
var threadPool = new addon.ThreadPool(global.config.webrtc.num_workers || 24);
threadPool.start();
// ThreadPool for libnice connection's main loop
var ioThreadPool = new addon.IOThreadPool(global.config.webrtc.io_workers || 8);
ioThreadPool.start();
module.exports = function (rpcClient, selfRpcId, parentRpcId, clusterWorkerIP) {
var that = {
agentID: parentRpcId,
clusterIP: clusterWorkerIP
};
var connections = new Connections;
var internalConnFactory = new InternalConnectionFactory;
// Map { transportId => WrtcConnection }
var peerConnections = new Map();
// Map { publicTrackId => WrtcTrack }
var mediaTracks = new Map();
// Map { transportId => Map { trackId => publicTrackId } }
var mappingPublicId = new Map();
// Map { operationId => transportId }
var mappingTransports = new Map();
var notifyTransportStatus = function (controller, transportId, status) {
rpcClient.remoteCast(controller, 'onTransportProgress', [transportId, status]);
};
var notifyMediaUpdate = function (controller, publicTrackId, direction, mediaUpdate) {
rpcClient.remoteCast(controller, 'onMediaUpdate', [publicTrackId, direction, mediaUpdate]);
};
/* updateInfo = {
* event: ('track-added' | 'track-removed' | 'track-updated'),
* trackId, mediaType, mediaFormat, active }
*/
var notifyTrackUpdate = function (controller, transportId, updateInfo) {
rpcClient.remoteCast(controller, 'onTrackUpdate', [transportId, updateInfo]);
};
var handleTrackInfo = function (transportId, trackInfo, controller) {
var publicTrackId;
var updateInfo;
if (trackInfo.type === 'track-added') {
// Generate public track ID
const track = trackInfo.track;
publicTrackId = transportId + '-' + track.id;
if (mediaTracks.has(publicTrackId)) {
log.error('Conflict public track id:', publicTrackId, transportId, track.id);
return;
}
mediaTracks.set(publicTrackId, track);
mappingPublicId.get(transportId).set(track.id, publicTrackId);
connections.addConnection(publicTrackId, 'webrtc', controller, track, track.direction)
.catch(e => log.warn('Unexpected error during track add:', e));
// Bind media-update handler
track.on('media-update', (jsonUpdate) => {
notifyMediaUpdate(controller, publicTrackId, track.direction, JSON.parse(jsonUpdate));
});
// Notify controller
const mediaType = track.format('audio') ? 'audio' : 'video';
updateInfo = {
type: 'track-added',
trackId: publicTrackId,
mediaType: track.format('audio') ? 'audio' : 'video',
mediaFormat: track.format(mediaType),
direction: track.direction,
operationId: trackInfo.operationId,
mid: trackInfo.mid,
rid: trackInfo.rid,
active: true,
};
log.debug('notifyTrackUpdate', controller, publicTrackId, updateInfo);
notifyTrackUpdate(controller, transportId, updateInfo);
} else if (trackInfo.type === 'track-removed') {
publicTrackId = mappingPublicId.get(transportId).get(trackInfo.trackId);
if (!mediaTracks.has(publicTrackId)) {
log.error('Non-exist public track id:', publicTrackId, transportId, trackInfo.trackId);
return;
}
connections.removeConnection(publicTrackId)
.then(ok => {
mediaTracks.get(publicTrackId).close();
mediaTracks.delete(publicTrackId);
mappingPublicId.get(transportId).delete(trackInfo.trackId);
})
.catch(e => log.warn('Unexpected error during track remove:', e));
// Notify controller
updateInfo = {
type: 'track-removed',
trackId: publicTrackId,
};
notifyTrackUpdate(controller, transportId, updateInfo);
} else if (trackInfo.type === 'tracks-complete') {
updateInfo = {
type: 'tracks-complete',
operationId: trackInfo.operationId
};
notifyTrackUpdate(controller, transportId, updateInfo);
}
};
var createWebRTCConnection = function (transportId, controller, owner, isScreen) {
if (peerConnections.has(transportId)) {
log.debug('PeerConnection already created:', transportId);
return peerConnections.get(transportId);
}
var connection = new WrtcConnection({
connectionId: transportId,
threadPool: threadPool,
ioThreadPool: ioThreadPool,
network_interfaces: global.config.webrtc.network_interfaces,
owner,
isScreen,
}, function onTransportStatus(status) {
notifyTransportStatus(controller, transportId, status);
}, function onTrack(trackInfo) {
handleTrackInfo(transportId, trackInfo, controller);
});
peerConnections.set(transportId, connection);
mappingPublicId.set(transportId, new Map());
connection.controller = controller;
return connection;
};
var getWebRTCConnection = function (operationId) {
var transportId = mappingTransports.get(operationId);
if (peerConnections.has(transportId)) {
return peerConnections.get(transportId);
}
return null;
};
var onSuccess = function (callback) {
return function(result) {
callback('callback', result);
};
};
var onError = function (callback) {
return function(reason) {
if (typeof reason === 'string') {
callback('callback', 'error', reason);
} else {
callback('callback', reason);
}
};
};
that.createTransport = function (transportId, controller, callback) {
createWebRTCConnection(transportId, controller);
callback('callback', 'ok');
};
that.destroyTransport = function (transportId, callback) {
log.debug('destroyTransport, transportId:', transportId);
if (!peerConnections.has(transportId)) {
callback('callback', 'error', 'Transport does not exists: ' + transportId);
return;
}
mappingPublicId.get(transportId).forEach((publicTrackId, trackId) => {
connections.removeConnection(publicTrackId)
.catch(e => log.warn('Unexpected error during track destroy:', e));
mediaTracks.get(publicTrackId).close();
mediaTracks.delete(publicTrackId);
// Notify controller
const updateInfo = {
type: 'track-removed',
trackId: publicTrackId,
};
const controller = peerConnections.get(transportId).controller;
notifyTrackUpdate(controller, publicTrackId, updateInfo);
});
mappingPublicId.delete(transportId);
peerConnections.get(transportId).close();
peerConnections.delete(transportId);
callback('callback', 'ok');
};
that.createInternalConnection = function (connectionId, direction, internalOpt, callback) {
internalOpt.minport = global.config.internal.minport;
internalOpt.maxport = global.config.internal.maxport;
var portInfo = internalConnFactory.create(connectionId, direction, internalOpt);
callback('callback', {ip: global.config.internal.ip_address, port: portInfo});
};
that.destroyInternalConnection = function (connectionId, direction, callback) {
internalConnFactory.destroy(connectionId, direction);
callback('callback', 'ok');
};
/*
* For operations on type webrtc, publicTrackId is connectionId.
* For operations on type internal, operationId is connectionId.
*/
// functions: publish, unpublish, subscribe, unsubscribe, linkup, cutoff
// options = { transportId, tracks = [{mid, type, formatPreference}], controller, owner}
that.publish = function (operationId, connectionType, options, callback) {
log.debug('publish, operationId:', operationId, 'connectionType:', connectionType, 'options:', options);
if (connections.getConnection(operationId)) {
return callback('callback', {type: 'failed', reason: 'Connection already exists:'+operationId});
}
var conn = null;
if (connectionType === 'internal') {
conn = internalConnFactory.fetch(operationId, 'in');
if (conn) {
conn.connect(options);
connections.addConnection(operationId, connectionType, options.controller, conn, 'in')
.then(onSuccess(callback), onError(callback));
}
} else if (connectionType === 'webrtc') {
if (!options.transportId) {
// Generate a transportId
}
const isScreen = !!options.tracks.find((track) => (
track.type === 'video' && track.source === 'screen-cast'
));
conn = createWebRTCConnection(options.transportId, options.controller, options.owner, isScreen);
options.tracks.forEach(function trackOp(t) {
conn.addTrackOperation(operationId, t.mid, t.type, 'sendonly', t.formatPreference);
});
mappingTransports.set(operationId, options.transportId);
callback('callback', 'ok');
} else {
log.error('Connection type invalid:' + connectionType);
}
if (!conn) {
log.error('Create connection failed', operationId, connectionType);
callback('callback', {type: 'failed', reason: 'Create Connection failed'});
}
};
that.unpublish = function (operationId, callback) {
log.debug('unpublish, operationId:', operationId);
var conn = getWebRTCConnection(operationId);
if (conn) {
// For 'webrtc'
conn.removeTrackOperation(operationId);
callback('callback', 'ok');
} else {
conn = connections.getConnection(operationId);
connections.removeConnection(operationId).then(function(ok) {
if (conn && conn.type === 'internal') {
internalConnFactory.destroy(operationId, 'in');
}
callback('callback', 'ok');
}, onError(callback));
}
};
that.subscribe = function (operationId, connectionType, options, callback) {
log.debug('subscribe, operationId:', operationId, 'connectionType:', connectionType, 'options:', options);
if (connections.getConnection(operationId)) {
return callback('callback', {type: 'failed', reason: 'Connection already exists:'+operationId});
}
var conn = null;
if (connectionType === 'internal') {
conn = internalConnFactory.fetch(operationId, 'out');
if (conn) {
conn.connect(options);
connections.addConnection(operationId, connectionType, options.controller, conn, 'out')
.then(onSuccess(callback), onError(callback));
}
} else if (connectionType === 'webrtc') {
if (!options.transportId) {
// Generate a transportId
}
const isScreen = !!options.tracks.find((track) => (
track.type === 'video' && track.source === 'screen-cast'
));
conn = createWebRTCConnection(options.transportId, options.controller, options.owner, isScreen);
options.tracks.forEach(function trackOp(t) {
conn.addTrackOperation(operationId, t.mid, t.type, 'recvonly', t.formatPreference);
});
mappingTransports.set(operationId, options.transportId);
callback('callback', 'ok');
} else {
log.error('Connection type invalid:' + connectionType);
}
if (!conn) {
log.error('Create connection failed', operationId, connectionType);
callback('callback', {type: 'failed', reason: 'Create Connection failed'});
}
};
that.unsubscribe = function (operationId, callback) {
log.debug('unsubscribe, operationId:', operationId);
var conn = getWebRTCConnection(operationId);
if (conn) {
// For 'webrtc'
conn.removeTrackOperation(operationId);
callback('callback', 'ok');
} else {
conn = connections.getConnection(operationId);
connections.removeConnection(operationId).then(function(ok) {
if (conn && conn.type === 'internal') {
internalConnFactory.destroy(operationId, 'out');
}
callback('callback', 'ok');
}, onError(callback));
}
};
that.linkup = function (connectionId, audioFrom, videoFrom, dataFrom, callback) {
log.debug('linkup, connectionId:', connectionId, 'audioFrom:', audioFrom, 'videoFrom:', videoFrom, 'dataFrom:', dataFrom);
connections.linkupConnection(connectionId, audioFrom, videoFrom).then(onSuccess(callback), onError(callback));
};
that.cutoff = function (connectionId, callback) {
log.debug('cutoff, connectionId:', connectionId);
connections.cutoffConnection(connectionId).then(onSuccess(callback), onError(callback));
};
that.onTransportSignaling = function (connectionId, msg, callback) {
log.debug('onTransportSignaling, connection id:', connectionId, 'msg:', msg);
var conn = getWebRTCConnection(connectionId);
if (conn) {
conn.onSignalling(msg, connectionId);
callback('callback', 'ok');
} else {
callback('callback', 'error', 'Connection does NOT exist:' + connectionId);
}
};
that.mediaOnOff = function (connectionId, tracks, direction, action, callback) {
log.debug('mediaOnOff, connection id:', connectionId, 'tracks:', tracks, 'direction:', direction, 'action:', action);
var conn = getWebRTCConnection(connectionId);
var promises;
if (conn) {
promises = tracks.map(trackId => new Promise((resolve, reject) => {
if (mediaTracks.has(trackId)) {
log.debug('got on off track:', trackId);
mediaTracks.get(trackId).onTrackControl(
'av', direction, action, resolve, reject);
} else {
resolve();
}
}));
Promise.all(promises).then(() => {
callback('callback', 'ok');
}).catch(reason => {
callback('callback', 'error', reason);
});
} else {
log.info('WebRTC Connection does NOT exist:' + connectionId);
callback('callback', 'error', 'Connection does NOT exist:' + connectionId);
}
};
that.setVideoBitrate = function (connectionId, bitrate, callback) {
log.debug('setVideoBitrate, connection id:', connectionId, 'bitrate:', bitrate);
var conn = connections.getConnection(connectionId);
if (conn && conn.direction === 'in') {
if (conn.type === 'webrtc') {//NOTE: Only webrtc connection supports setting video bitrate.
conn.connection.setVideoBitrate(bitrate, function () {
callback('callback', 'ok');
}, function (err_reason) {
log.info('set video bitrate failed:', err_reason);
callback('callback', 'error', err_reason);
});
} else {
callback('callback', 'error', 'setVideoBitrate on non-webrtc connection');
}
} else {
callback('callback', 'error', 'Connection does NOT exist:' + connectionId);
}
};
that.close = function() {
log.debug('close called');
var connIds = connections.getIds();
for (let connectionId of connIds) {
var conn = connections.getConnection(connectionId);
connections.removeConnection(connectionId);
if (conn && conn.type === 'internal') {
internalConnFactory.destroy(connectionId, conn.direction);
} else if (conn) {
conn.connection.close();
}
}
peerConnections.forEach(pc => {
pc.close();
});
};
that.onFaultDetected = function (message) {
connections.onFaultDetected(message);
};
return that;
};