-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathindex.js
377 lines (309 loc) · 8.79 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
#!/usr/bin/env node
// Based upon mediasoup-demo server
// https://github.com/versatica/mediasoup-demo/tree/v3/server
process.title = 'dialog';
process.env.DEBUG = process.env.DEBUG || '*INFO* *WARN* *ERROR*';
const config = require('./config');
/* eslint-disable no-console */
console.log('process.env.DEBUG:', process.env.DEBUG);
console.log('config.js:\n%s', JSON.stringify(config, null, ' '));
/* eslint-enable no-console */
const fs = require('fs');
const https = require('https');
const http = require('http');
const url = require('url');
const protoo = require('protoo-server');
const mediasoup = require('mediasoup');
const express = require('express');
const bodyParser = require('body-parser');
const { AwaitQueue } = require('awaitqueue');
const Logger = require('./lib/Logger');
const Room = require('./lib/Room');
const interactiveServer = require('./lib/interactiveServer');
const interactiveClient = require('./lib/interactiveClient');
const util = require('util');
const readFile = util.promisify(fs.readFile);
const utils = require('./lib/utils');
const os = require('os');
const logger = new Logger();
const queue = new AwaitQueue();
const rooms = utils.rooms;
let httpsServer;
let expressApp;
let expressAdminApp;
let protooWebSocketServer;
const mediasoupWorkers = [];
let authKey;
run();
async function run()
{
await interactiveServer();
if (process.env.INTERACTIVE === 'true' || process.env.INTERACTIVE === '1')
await interactiveClient();
await runMediasoupWorkers();
await createExpressApp();
await createAdminExpressApp();
await runHttpsServer();
try {
authKey = await readFile(config.authKey, 'utf8');
} catch (error) {
logger.error("authKey not set; jwt verification will not work.", error);
}
await runProtooWebSocketServer();
// Log rooms status every X seconds.
setInterval(() =>
{
for (const room of rooms.values())
{
room.logStatus();
}
}, 900000);
}
async function runMediasoupWorkers()
{
const { numWorkers } = config.mediasoup;
logger.info('running %d mediasoup Workers...', numWorkers);
for (let i = 0; i < numWorkers; ++i)
{
const worker = await mediasoup.createWorker(
{
logLevel : config.mediasoup.workerSettings.logLevel,
logTags : config.mediasoup.workerSettings.logTags,
rtcMinPort : Number(config.mediasoup.workerSettings.rtcMinPort),
rtcMaxPort : Number(config.mediasoup.workerSettings.rtcMaxPort)
});
worker.on('died', () =>
{
logger.error(
'mediasoup Worker died, exiting in 2 seconds... [pid:%d]', worker.pid);
setTimeout(() => process.exit(1), 2000);
});
mediasoupWorkers.push(worker);
utils.workerLoadMan.set(worker._pid, { peerCnt: 0, roomReqCnt: 0, rooms: new Map() });
}
utils.workerLoadMan.runSurvey();
setInterval(async () => {
const startTimestampNs = process.hrtime.bigint();
utils.workerLoadMan.runSurvey();
const elapsedMs = Number(process.hrtime.bigint() - startTimestampNs) / 1000000;
if (elapsedMs > 0.1) { logger.warn('runSurvey() took: %s ms', elapsedMs); }
}, 5000);
}
async function createExpressApp()
{
logger.info('creating Express app...');
expressApp = express();
expressApp.use(bodyParser.json());
expressApp.param(
'roomId', (req, res, next, roomId) =>
{
if (!rooms.has(roomId))
{
const error = new Error(`room with id "${roomId}" not found`);
error.status = 404;
throw error;
}
req.room = rooms.get(roomId);
next();
});
/**
* Error handler.
*/
expressApp.use(
(error, req, res, next) =>
{
if (error)
{
logger.warn('Express app %s', String(error));
error.status = error.status || (error.name === 'TypeError' ? 400 : 500);
res.statusMessage = error.message;
res.status(error.status).send(String(error));
}
else
{
next();
}
});
}
async function createAdminExpressApp()
{
logger.info('creating Admin Express app...');
expressAdminApp = express();
expressAdminApp.use(bodyParser.json());
/**
* Temporary deprecated API to emulate Janus endpoint to get CCU by reticulum.
*/
expressAdminApp.post(
'/admin', (req, res) =>
{
const sessions = [];
for (const room in rooms.values()) {
for (let i = 0; i < room.getCCU(); i++) {
sessions.push({});
}
}
res.status(200).json({ sessions });
});
/**
* meta API to report current capacity
*/
expressAdminApp.get(
'/meta', (req, res) =>
{
res.status(200).json({
cap: utils.workerLoadMan.sum(),
// ip: process.env.MEDIASOUP_ANNOUNCED_IP
});
});
/**
* full report
*/
expressAdminApp.get(
'/report', (req, res) =>
{
const report = new Map(utils.workerLoadMan.get());
report.set('_hostname', os.hostname());
report.set('_capacity', utils.workerLoadMan.sum());
res.set({ 'Content-Type': 'application/json' })
.status(200)
.send(JSON.stringify(report, utils.stableSortReplacer, 2));
});
/**
* dump room
*/
expressAdminApp.get(
'/report/rooms/:roomId', (req, res) =>
{
const room = rooms.get(req.params.roomId);
console.log(room)
res.set({ 'Content-Type': 'application/json' })
.status(200)
.send(room);
});
/**
* dump peer
*/
expressAdminApp.get(
'/report/peers/:peerId', (req, res) =>
{
const peerId = req.params.peerId
let room = {}
for (const [k,v] of rooms.entries()){
if (v._protooRoom.hasPeer(peerId)){
room =v
}
}
const peer = room._protooRoom.getPeer(peerId);
console.log(peer)
res.set({ 'Content-Type': 'application/json' })
.status(200)
.send(peer._data);
});
/**
* Error handler.
*/
expressAdminApp.use(
(error, req, res, next) =>
{
if (error)
{
logger.warn('Express app %s', String(error));
error.status = error.status || (error.name === 'TypeError' ? 400 : 500);
res.statusMessage = error.message;
res.status(error.status).send(String(error));
}
else
{
next();
}
});
}
/**
* Create a Node.js HTTPS server. It listens in the IP and port given in the
* configuration file and reuses the Express application as request listener.
*/
async function runHttpsServer()
{
logger.info('running an HTTPS server...');
// HTTPS server for the protoo WebSocket server.
const tls =
{
cert : fs.readFileSync(config.https.tls.cert),
key : fs.readFileSync(config.https.tls.key)
};
httpsServer = https.createServer(tls, expressApp);
await new Promise((resolve) =>
{
httpsServer.listen(
Number(config.https.listenPort), config.https.listenIp, resolve);
});
// TODO remove, alt server needed to spoof janus API.
logger.info('running an Admin HTTP server...');
adminHttpServer = http.createServer(expressAdminApp);
await new Promise((resolve) =>
{
adminHttpServer.listen(
Number(config.adminHttp.listenPort), config.adminHttp.listenIp, resolve);
});
}
/**
* Create a protoo WebSocketServer to allow WebSocket connections from browsers.
*/
async function runProtooWebSocketServer()
{
logger.info('running protoo WebSocketServer...');
// Create the protoo WebSocket server.
protooWebSocketServer = new protoo.WebSocketServer(httpsServer,
{
maxReceivedFrameSize : 960000, // 960 KBytes.
maxReceivedMessageSize : 960000,
fragmentOutgoingMessages : true,
fragmentationThreshold : 960000
});
// Handle connections from clients.
protooWebSocketServer.on('connectionrequest', (info, accept, reject) =>
{
// The client indicates the roomId and peerId in the URL query.
const u = url.parse(info.request.url, true);
const roomId = u.query['roomId'];
const peerId = u.query['peerId'];
if (!roomId || !peerId)
{
reject(400, 'Connection request without roomId and/or peerId');
return;
}
logger.info(
'protoo connection request [roomId:%s, peerId:%s, address:%s, origin:%s]',
roomId, peerId, info.socket.remoteAddress, info.origin);
const roomSize = info.request.headers['x-ret-max-room-size'];
logger.info('roomId: %s, x-ret-max-room-size: %s', roomId, roomSize);
// Serialize this code into the queue to avoid that two peers connecting at
// the same time with the same roomId create two separate rooms with same
// roomId.
queue.push(async () =>
{
const room = await getOrCreateRoom({ roomId, roomSize });
// Accept the protoo WebSocket connection.
const protooWebSocketTransport = accept();
room.handleProtooConnection({ peerId, protooWebSocketTransport });
})
.catch((error) =>
{
logger.error('room creation or room joining failed:%o', error);
reject(error);
});
});
}
async function getOrCreateRoom({ roomId, roomSize=0 })
{
let room = rooms.get(roomId);
// If the Room does not exist create a new one.
if (!room)
{
logger.info('creating a new Room [roomId:%s]', roomId);
room = await Room.create({ mediasoupWorkers, roomId, authKey, roomSize });
rooms.set(roomId, room);
room.on('close', () => rooms.delete(roomId));
}
return room;
}