-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathserver.js
465 lines (392 loc) · 16.8 KB
/
server.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
var SIP = require('sip.js');
var kurento = require('kurento-client');
var util = require('util');
var fs = require('fs');
var io = require('socket.io-client');
var sdpparser = require('rtc-sdp');
const ws_uri = 'ws://meet.akademia.no:8080/kurento';
const io_uri = 'https://meet.uninett.no';
var kurentoClient = null;
var idCounter = 0;
var rooms = {};
function nextUniqueId() {
idCounter++;
return idCounter.toString();
}
var ua = new SIP.UA({
traceSip: true,
register: true,
uri: '[email protected]',
password: 'DFOdH1abdsTDCqp',
rel100: SIP.C.supported.SUPPORTED,
wsServers: 'wss://meeting.akademia.no',
mediaHandlerFactory: function() {
var sessionId = nextUniqueId();
var roomName = null;
var offer = null;
var answer = null;
this.isReady = function isReady() {
return true;
}
this.close = function() {
console.log('Closing SIP dialog: ' + sessionId + ' to room: ' + roomName);
stop(sessionId);
}
this.render = new Function();
this.mute = new Function();
this.unmute = new Function();
this.getDescription = (onSuccess, onFailure, mediaHint) => {
console.log('Getdescription called for dialog: ' + sessionId + ' to room: ' + roomName);
joinRoom(roomName, sessionId, offer, (error, sdpAnswer) => {
if (error) {
onFailure();
} else {
onSuccess({
body: sdpAnswer,
contentType: 'application/sdp'
});
}
});
}
this.hasDescription = message => {
return true;
}
this.setDescription = (message, onSuccess, onFailure) => {
offer = message.body;
roomName = message.getHeader('X-Room');
console.log('Setdescription called for dialog: ' + sessionId + ' to room: ' + roomName);
onSuccess();
}
return this;
}
});
ua.on('invite', session => {
session.accept();
});
function getKurentoClient(callback) {
if (kurentoClient !== null) {
console.log('KurentoClient already created');
return callback(null, kurentoClient);
}
kurento(ws_uri, (error, _kurentoClient) => {
if (error) {
console.log('Coult not find media server at address ' + ws_uri);
return callback('Could not find media server at address' + ws_uri +
'. Exiting with error ' + error);
}
console.log('Created KurentoClient, connected to: ' + ws_uri);
kurentoClient = _kurentoClient;
callback(null, kurentoClient);
});
}
// Retrieve or create mediaPipeline
function prepareMediaPipeline(id, callback) {
getKurentoClient((error, _kurentoClient) => {
if (error) {
return callback(error);
}
_kurentoClient.create('MediaPipeline', (error, _pipeline) => {
if (error) {
return callback(error);
}
console.log('Created MediaPipeline for room: ' + rooms[id].room + ' with ID: ' + id);
rooms[id].MediaPipeline = _pipeline;
rooms[id].MediaPipeline.create('Composite', (error, _composite) => {
if (error) {
stop(id);
return callback(error);
}
console.log('Created Composite Hub for room: ' + rooms[id].room + ' with ID: ' + id);
rooms[id].Composite = _composite;
callback(null);
});
});
});
}
function createSIPEndpoint(id, callback) {
rooms[id].Composite.createHubPort((error, _hubPort) => {
if (error) {
return callback(error);
}
console.log('Created HubPort for WebRtcEndpoint in room: ' + rooms[id].room + ' with ID: ' + id);
rooms[id].SIPClient.HubPort = _hubPort;
rooms[id].MediaPipeline.create('WebRtcEndpoint', (error, _webRtcEndpoint) => {
if (error) {
stop(id);
return callback(error);
}
console.log('Created WebRtcEndpoint in room: ' + rooms[id].room + ' with ID: ' + id);
rooms[id].SIPClient.WebRtcEndpoint = _webRtcEndpoint;
rooms[id].SIPClient.HubPort.connect(rooms[id].SIPClient.WebRtcEndpoint, error => {
if (error) {
stop(id);
console.log('Error connecting ' + error);
return callback(error);
}
console.log('Connected HubPort to WebRtcEndpoint in room: ' + rooms[id].room + ' with ID: ' + id);
callback(null);
});
});
});
}
function createWebRtcEndpoint(id, pid, callback) {
rooms[id].MediaPipeline.create('WebRtcEndpoint', (error, _webrtcEndpoint) => {
if (error) {
removeParticipant(id, pid);
return callback(error);
}
console.info('Created WebRtcEndpoint in room: ' + rooms[id].room + ' with ID: ' + id);
rooms[id].WebRTCClients[pid].WebRTCEndpoint = _webrtcEndpoint;
if (rooms[id].WebRTCClients[pid].iceCandidateQueue) {
while (rooms[id].WebRTCClients[pid].iceCandidateQueue.length) {
let iceCandidate = rooms[id].WebRTCClients[pid].iceCandidateQueue.shift();
rooms[id].WebRTCClients[pid].WebRTCEndpoint.addIceCandidate(iceCandidate.candidate);
}
delete rooms[id].WebRTCClients[pid].WebRTCEndpoint.iceCandidateQueue;
}
rooms[id].WebRTCClients[pid].WebRTCEndpoint.on('IceCandidateFound', event => {
let candidate = kurento.register.complexTypes.IceCandidate(event.candidate);
rooms[id].RoomSocket.emit('iceCandidate', {
pid: pid,
candidate: candidate
});
});
rooms[id].Composite.createHubPort((error, _hubPort) => {
if (error) {
removeParticipant(id, pid);
return callback(error);
}
console.info('Created HubPort for WebRtcEndpoint in room: ' + rooms[id].room + ' with ID: ' + id);
rooms[id].WebRTCClients[pid].HubPort = _hubPort;
rooms[id].WebRTCClients[pid].WebRTCEndpoint.connect(rooms[id].WebRTCClients[pid].HubPort, error => {
if (error) {
removeParticipant(id, pid);
return callback(error);
}
console.log('Connected WebRtcEndpoint to HubPort in room: ' + rooms[id].room + ' with ID: ' + id);
rooms[id].SIPClient.WebRtcEndpoint.connect(rooms[id].WebRTCClients[pid].WebRTCEndpoint, error => {
if (error) {
removeParticipant(id, pid);
return callback(error);
}
console.log('Connected WebRtcEndpoint to WebRtcEndpoint: ' + pid + ' in room: ' + rooms[id].room + ' with ID: ' + id);
callback(null);
});
});
});
});
}
function createRoomSocket(id) {
rooms[id].RoomSocket = io.connect(io_uri);
rooms[id].RoomSocket.on('connection', socket => {
console.log('RoomSocket connected to ' + io_uri + '/' + rooms[id].room);
});
rooms[id].RoomSocket.on('sdp', msg => {
console.log('RoomSocket received sdpOffer from: ', msg.pid);
if (msg.sdp.type == 'offer') {
if (typeof(rooms[id].WebRTCClients[msg.pid]) == 'undefined') {
rooms[id].WebRTCClients[msg.pid] = {};
}
createWebRtcEndpoint(id, msg.pid, error => {
if (error) {
console.log('Error creating WebRtcEndpoint: ' + msg.pid + ' in room: ' + rooms[id].room + ' with ID: ' + id + ' error: ' + error);
removeParticipant(id, msg.pid);
return error;
}
rooms[id].WebRTCClients[msg.pid].WebRTCEndpoint.processOffer(msg.sdp.sdp, (error, sdpAnswer) => {
if (error) {
console.log('Error processing offer for WebRtcEndpoint: ' + msg.pid + ' in room: ' + rooms[id].room + ' with ID: ' + id + ' error: ' + error);
removeParticipant(id, msg.pid);
return error;
}
rooms[id].RoomSocket.emit('sdp', {
pid: msg.pid,
sdp: {
sdp: sdpAnswer,
type: 'answer'
}
});
rooms[id].WebRTCClients[msg.pid].WebRTCEndpoint.gatherCandidates(error => {
if (error) {
console.log('Error gathering ICE for WebRtcEndpoint: ' + msg.pid + ' in room: ' + rooms[id].room + ' with ID: ' + id + ' error: ' + error);
return error;
}
});
});
});
} else if (msg.sdp.type == 'answer') {
rooms[id].WebRTCClients[msg.pid].WebRTCEndpoint.processAnswer(msg.sdp.sdp, error => {
if (error) {
console.log('Error processing answer for WebRtcEndpoint: ' + msg.pid + ' in room: ' + rooms[id].room + ' with ID: ' + id + ' error: ' + error);
removeParticipant(id, msg.pid);
return error;
}
});
}
});
rooms[id].RoomSocket.on('iceCandidate', msg => {
if (typeof(rooms[id].WebRTCClients[msg.pid]) == 'undefined') {
rooms[id].WebRTCClients[msg.pid] = {};
}
console.log('RoomSocket got iceCandidate from %s: %s', msg.pid, msg.candidate);
let candidate = kurento.register.complexTypes.IceCandidate(msg.candidate);
if (rooms[id].WebRTCClients[msg.pid].WebRTCEndpoint) {
rooms[id].WebRTCClients[msg.pid].WebRTCEndpoint.addIceCandidate(candidate);
} else {
if (typeof(rooms[id].WebRTCClients[msg.pid].iceCandidateQueue) == 'undefined') {
rooms[id].WebRTCClients[msg.pid].iceCandidateQueue = [];
}
rooms[id].WebRTCClients[msg.pid].iceCandidateQueue.push({
candidate: candidate
});
}
});
rooms[id].RoomSocket.on('participantReady', msg => {
if (typeof(rooms[id].WebRTCClients[msg.pid]) == 'undefined') {
rooms[id].WebRTCClients[msg.pid] = {};
}
console.log('RoomSocket got participantReady: ', msg.pid);
createWebRtcEndpoint(id, msg.pid, error => {
if (error) {
console.log('Error creating WebRtcEndpoint: ' + msg.pid + ' in room: ' + rooms[id].room + ' with ID: ' + id + ' error: ' + error);
removeParticipant(id, msg.pid);
return error;
}
rooms[id].WebRTCClients[msg.pid].WebRTCEndpoint.generateOffer((error, sdpOffer) => {
if (error) {
console.log('Error generating offer for WebRtcEndpoint: ' + msg.pid + ' in room: ' + rooms[id].room + ' with ID: ' + id + ' error: ' + error);
removeParticipant(id, msg.pid);
return error;
}
rooms[id].RoomSocket.emit('sdp', {
pid: msg.pid,
sdp: {
sdp: sdpOffer,
type: 'offer'
}
});
rooms[id].WebRTCClients[msg.pid].WebRTCEndpoint.gatherCandidates(error => {
if (error) {
console.log('Error gathering ICE for WebRtcEndpoint: ' + msg.pid + ' in room: ' + rooms[id].room + ' with ID: ' + id + ' error: ' + error);
return error;
}
});
});
});
});
rooms[id].RoomSocket.on('bye', msg => {
console.log('Got bye from WebRtcEndpoint: ' + msg.pid + ' in room: ' + rooms[id].room + ' with ID: ' + id);
removeParticipant(id, msg.pid);
});
rooms[id].RoomSocket.on('participantDied', msg => {
console.log('WebRtcEndpoint: ' + msg.pid + ' died in room: ' + rooms[id].room + ' with ID: ' + id);
removeParticipant(id, msg.pid);
});
console.log('Emitting ready in room: ' + rooms[id].room + ' with ID: ' + id);
rooms[id].RoomSocket.emit('ready', rooms[id].room);
}
function joinRoom(room, id, sdpOffer, callback) {
rooms[id] = {
room: room,
SIPClient: {
WebRtcEndpoint: null,
HubPort: null,
sdp: sdpOffer
},
WebRTCClients: {},
MediaPipeline: null,
Composite: null,
RoomSocket: null
}
prepareMediaPipeline(id, error => {
if (error) {
console.log('Error preparing MediaPipeline in room: ' + rooms[id].room + ' with ID: ' + id + ' error: ' + error);
stop(id);
return callback(error);
}
createSIPEndpoint(id, error => {
if (error) {
console.log('Error creating SIPEndpoint in room: ' + rooms[id].room + ' with ID: ' + id + ' error: ' + error);
stop(id);
return callback(error);
}
rooms[id].SIPClient.WebRtcEndpoint.on('IceCandidateFound', event => {
event.candidate.candidate = 'a=' + event.candidate.candidate;
rooms[id].SIPClient.sdp.addIceCandidate(event.candidate);
});
rooms[id].SIPClient.WebRtcEndpoint.on('IceGatheringDone', event => {
console.info('gathering done');
callback(null, rooms[id].SIPClient.sdp.toString());
});
rooms[id].SIPClient.WebRtcEndpoint.processOffer(sdpOffer, (error, sdpAnswer) => {
if (error) {
stop(id);
console.log('Error processing offer from WebRtcEndpoint in room: ' + rooms[id].room + ' with ID: ' + id + ' error: ' + error);
return callback(error);
}
rooms[id].SIPClient.sdp = sdpparser(sdpAnswer);
createRoomSocket(id);
rooms[id].SIPClient.WebRtcEndpoint.gatherCandidates(error => {
if (error) {
return error;
}
console.info('gathering candidates');
});
});
});
});
}
function removeParticipant(id, pid) {
if (rooms[id].WebRTCClients) {
if (rooms[id].WebRTCClients[pid]) {
if (rooms[id].WebRTCClients[pid].WebRTCEndpoint) {
console.log('Releasing WebRtcEndpoint: ' + pid + ' in room: ' + rooms[id].room + ' with ID: ' + id);
rooms[id].WebRTCClients[pid].WebRTCEndpoint.release();
}
if (rooms[id].WebRTCClients[pid].HubPort) {
console.log('Releasing WebRtcEndpoint HubPort: ' + pid + ' in room: ' + rooms[id].room + ' with ID: ' + id);
rooms[id].WebRTCClients[pid].HubPort.release();
}
delete rooms[id].WebRTCClients[pid];
}
}
}
function stop(id) {
if (rooms[id]) {
if (rooms[id].RoomSocket) {
console.log('Emitting bye in room: ' + rooms[id].room + ' with ID: ' + id);
rooms[id].RoomSocket.emit('bye');
rooms[id].RoomSocket.disconnect();
}
if (rooms[id].SIPClient.WebRtcEndpoint) {
console.log('Releasing WebRtcEndpoint: ' + id + ' in room: ' + rooms[id].room);
rooms[id].SIPClient.WebRtcEndpoint.release();
}
if (rooms[id].SIPClient.HubPort) {
console.log('Releasing WebRtcEndpoint HubPort: ' + id + ' in room: ' + rooms[id].room);
rooms[id].SIPClient.HubPort.release();
}
if (rooms[id].WebRTCClients) {
while (rooms[id].WebRTCClients.length) {
let WebRTCClient = rooms[id].WebRTCClients.shift();
if (WebRTCClient.WebRTCEndpoint) {
console.log('Releasing WebRtcEndpoint: ' + id + ' in room: ' + rooms[id].room);
WebRTCClient.WebRTCEndpoint.release();
}
if (WebRTCClient.HubPort) {
console.log('Releasing WebRtcEndpoint HubPort: ' + id + ' in room: ' + rooms[id].room);
WebRTCClient.HubPort.release();
}
}
}
if (rooms[id].Composite) {
console.log('Releasing Composite: ' + id + ' in room: ' + rooms[id].room);
rooms[id].Composite.release();
}
if (rooms[id].MediaPipeline) {
console.log('Releasing MediaPipeline: ' + id + ' in room: ' + rooms[id].room);
rooms[id].MediaPipeline.release();
}
delete rooms[id];
}
}