-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
337 lines (275 loc) · 9.28 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
const mediasoup = require('mediasoup');
const fs = require('fs');
const https = require('https');
const express = require('express');
const socketIO = require('socket.io');
const config = require('./config');
const Room = require('./lib/Room');
const Peer = require('./lib/Peer');
// Global variables
let webServer;
let socketServer;
let expressApp;
(async () => {
try {
await runExpressApp();
await runWebServer();
await runSocketServer();
await runMediasoupWorkers();
} catch (err) {
console.error(err);
}
})();
async function runExpressApp() {
expressApp = express();
expressApp.use((error, _req, res, next) => {
if (error) {
console.warn('Express app error,', error.message);
error.status = error.status || (error.name === 'TypeError' ? 400 : 500);
res.statusMessage = error.message;
res.status(error.status).send(String(error));
} else {
next();
}
});
expressApp.get('/', (_req, res) => {
res.send('Running');
});
}
async function runWebServer() {
const { sslKey, sslCrt } = config;
if (!fs.existsSync(sslKey) || !fs.existsSync(sslCrt)) {
console.error('SSL files are not found. check your config.js file');
process.exit(0);
}
const tls = {
key: fs.readFileSync(config.sslKey, 'utf-8'),
cert: fs.readFileSync(config.sslCrt, 'utf-8')
};
webServer = https.createServer(tls, expressApp);
webServer.on('error', (err) => {
console.error('starting web server failed:', err.message);
});
await new Promise((resolve) => {
const { listenIp, listenPort } = config;
webServer.listen(listenPort, listenIp, () => {
const listenIps = config.mediasoup.webRtcTransport.listenIps[0];
const ip = listenIps.announcedIp || listenIps.ip;
console.log(`server is running - https://${ip}:${listenPort}`);
resolve();
});
});
}
async function runSocketServer() {
socketServer = socketIO(webServer, {
serveClient: false,
path: '/server',
log: false
});
socketServer.on('connection', (socket) => {
console.log('client connected');
socket.on('connect_error', (err) => {
console.error(`connect_error due to ${err.message}`);
});
socket.on('createRoom', async ({ room_id }, callback) => {
if (roomList.has(room_id)) {
callback('already exists')
} else {
let worker = await getMediasoupWorker();
roomList.set(room_id, new Room(room_id, worker, socketServer));
callback(room_id);
}
});
socket.on('removeRoom', async ({ room_id }, callback) => {
if (!roomList.has(room_id)) {
callback('Room does not exist')
} else {
console.log('Remove room', { room_id: room_id });
// TODO: need remove the room from roomList and make the worker to free for usage
// roomList.delete(room_id);
callback('The room removed')
}
});
socket.on('join', ({ room_id, name }, callback) => {
if (!roomList.has(room_id)) {
return callback({
error: 'Room does not exist'
});
}
const room = roomList.get(room_id);
room.addPeer(new Peer(socket.id, name, room_id));
socket.room_id = room_id;
callback(room.toJson());
});
socket.on('getProducers', () => {
if (!roomList.has(socket.room_id)) return;
const room = roomList.get(socket.room_id);
console.log('Get producers', { name: `${room.getPeers().get(socket.id).name}` });
let producerList = room.getProducerListForPeer(socket.id)
socket.emit('newProducers', producerList)
});
socket.on('getRouterRtpCapabilities', (_, callback) => {
const room = roomList.get(socket.room_id);
try {
console.log('Get RouterRtpCapabilities', { name: `${room.getPeers().get(socket.id).name}` });
callback(room.getRtpCapabilities());
} catch (e) {
callback({
error: e.message
})
}
});
socket.on('createWebRtcTransport', async (_, callback) => {
const room = roomList.get(socket.room_id);
try {
console.log('Create webrtc transport', { name: `${room.getPeers().get(socket.id).name}` });
const { params } = await room.createWebRtcTransport(socket.id);
callback(params)
} catch (err) {
console.error(err)
callback({
error: err.message
})
}
});
socket.on('connectTransport', async ({ transport_id, dtlsParameters }, callback) => {
if (!roomList.has(socket.room_id)) return;
const room = roomList.get(socket.room_id);
console.log('Connect transport', { name: `${room.getPeers().get(socket.id).name}` });
await room.connectPeerTransport(socket.id, transport_id, dtlsParameters);
callback('success');
});
socket.on('produce', async ({ kind, rtpParameters, producerTransportId }, callback) => {
if (!roomList.has(socket.room_id)) {
return callback({ error: 'not is a room' })
}
const room = roomList.get(socket.room_id);
let producer_id = await room.produce(socket.id, producerTransportId, rtpParameters, kind);
console.log('Produce', {
type: `${kind}`,
name: `${room.getPeers().get(socket.id).name}`,
id: `${producer_id}`
});
callback({ producer_id });
});
socket.on('consume', async ({ consumerTransportId, producerId, rtpCapabilities }, callback) => {
if (!roomList.has(socket.room_id)) {
return callback({ error: 'not is a room' })
}
try {
const room = roomList.get(socket.room_id);
let params = await room.consume(socket.id, consumerTransportId, producerId, rtpCapabilities);
console.log('Consuming', {
name: `${room && room.getPeers().get(socket.id).name}`,
producer_id: `${producerId}`,
consumer_id: `${params.id}`
})
callback(params);
} catch (err) {
console.error(err)
callback({
error: err.message
})
}
});
socket.on('resume', async (_data, callback) => {
await consumer.resume();
callback();
});
socket.on('getMyRoomInfo', (_, cb) => {
cb(roomList.get(socket.room_id).toJson());
});
socket.on('disconnect', () => {
if (!socket.room_id) return;
const room = roomList.get(socket.room_id);
console.log('Disconnect', { name: `${room && room.getPeers().get(socket.id).name}` });
roomList.get(socket.room_id).removePeer(socket.id);
});
socket.on('producerClosed', ({ producer_id }) => {
const room = roomList.get(socket.room_id);
console.log('Producer close', { name: `${room && room.getPeers().get(socket.id).name}` });
room.closeProducer(socket.id, producer_id);
})
socket.on('exitRoom', async (_, callback) => {
console.log('Exit room', {
name: `${roomList.get(socket.room_id) && roomList.get(socket.room_id).getPeers().get(socket.id).name}`
})
if (!roomList.has(socket.room_id)) {
callback({
error: 'not currently in a room'
})
return
}
// close transports
await roomList.get(socket.room_id).removePeer(socket.id)
if (roomList.get(socket.room_id).getPeers().size === 0) {
console.log('Delete room', socket.room_id);
roomList.delete(socket.room_id);
}
socket.room_id = null;
callback('successfully exited room')
})
// Recording handlers
socket.on('startRecording', async (_, callback) => {
await roomList.get(socket.room_id).startRecording(socket.id);
callback('success');
});
socket.on('stopRecording', async (_, callback) => {
await roomList.get(socket.room_id).stopRecording(socket.id);
callback('success');
});
});
}
// all mediasoup workers
let workers = [];
let nextMediasoupWorkerIdx = 0;
/**
* roomList
* {
* room_id: Room {
* id:
* router:
* peers: {
* id:,
* name:,
* master: [boolean],
* transports: [Map],
* producers: [Map],
* consumers: [Map],
* rtpCapabilities:,
* audioRtpTransport:,
* videoRtpTransport:,
* audioRtpConsumer:,
* videoRtpConsumer:,
* recProcess:
* }
* }
* }
*/
let roomList = new Map();
async function runMediasoupWorkers() {
let { numWorkers } = config.mediasoup
for (let i = 0; i < numWorkers; i++) {
let worker = await mediasoup.createWorker({
logLevel: config.mediasoup.worker.logLevel,
logTags: config.mediasoup.worker.logTags,
rtcMinPort: config.mediasoup.worker.rtcMinPort,
rtcMaxPort: config.mediasoup.worker.rtcMaxPort
})
worker.on('died', () => {
console.error('mediasoup worker died, exiting in 2 seconds... [pid:%d]', worker.pid)
setTimeout(() => process.exit(1), 2000)
})
workers.push(worker)
// log worker resource usage
/*setInterval(async () => {
const usage = await worker.getResourceUsage();
console.info('mediasoup Worker resource usage [pid:%d]: %o', worker.pid, usage);
}, 120000);*/
}
};
function getMediasoupWorker() {
const worker = workers[nextMediasoupWorkerIdx]
if (++nextMediasoupWorkerIdx === workers.length) nextMediasoupWorkerIdx = 0
return worker
};