-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
358 lines (346 loc) · 14.6 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
const fs = require('fs');
const dotenv = require('dotenv');
const dotenvParseVariables = require('dotenv-parse-variables');
let env = dotenv.config({})
if (env.error) throw env.error;
env = dotenvParseVariables(env.parsed);
// --- set ssl server ---
let sslOptions = {};
if (env.HTTPS) {
sslOptions.key = fs.readFileSync(env.HTTPS_KEY_FILE).toString();
sslOptions.cert = fs.readFileSync(env.HTTPS_CERT_FILE).toString();
}
// --- prepare server ---
const http = require("http");
const https = require("https");
const express = require('express');
const app = express();
app.use(express.static('public'));
let webServer = null;
if (env.HTTPS) {
// -- https ---
webServer = https.createServer(sslOptions, app).listen(env.PORT, function () {
console.log('Web server start. https://' + env.HOST_NAME + ':' + webServer.address().port + '/');
});
}
else {
// --- http ---
webServer = http.Server(app).listen(env.PORT, function () {
console.log('Web server start. http://' + env.HOST_NAME + ':' + webServer.address().port + '/');
});
}
// ========= room ===========
global.Room = require('./services/room').Room
// ========= mediasoup ===========
global.mediasoup = require("mediasoup");
const helpers_mediasoup = require("./helpers/helper_mediasoup");
(async function () {
const mediasoupOptions = {
// Worker settings
worker: {
rtcMinPort: 10000,
rtcMaxPort: 10100,
logLevel: 'warn',
logTags: [
'info',
'ice',
'dtls',
'rtp',
'srtp',
'rtcp',
// 'rtx',
// 'bwe',
// 'score',
// 'simulcast',
// 'svc'
],
},
// Router settings
router: {
mediaCodecs:
[
{
kind: 'audio',
mimeType: 'audio/opus',
clockRate: 48000,
channels: 2
},
{
kind: 'video',
mimeType: 'video/VP8',
clockRate: 90000,
parameters:
{
'x-google-start-bitrate': 1000
}
},
]
},
// WebRtcTransport settings
webRtcTransport: {
listenIps: [
{ ip: '127.0.0.1', announcedIp: null }
],
enableUdp: true,
enableTcp: true,
preferUdp: true,
maxIncomingBitrate: 1500000,
initialAvailableOutgoingBitrate: 1000000,
}
};
let { worker } = await helpers_mediasoup.startWorker(mediasoupOptions)
// --- socket.io server ---
global.io = require('socket.io')(webServer);
const helpers = require('./helpers/helpers');
console.log('socket.io server start. port=' + webServer.address().port);
io.on('connection', function (socket) {
const id = helpers.getId(socket);
console.log('client connected. socket id=' + id + ' , total clients=' + helpers.getClientCount());
socket.on('disconnect', function () {
const roomName = getRoomname();
// close user connection
console.log('client disconnected. socket id=' + id + ' , total clients=' + helpers.getClientCount());
cleanUpPeer(roomName, socket);
// --- socket.io room ---
socket.leave(roomName);
});
// --- setup room ---
socket.on('prepare_room', async (data) => {
const roomId = data.roomId;
const existRoom = Room.getRoom(roomId);
if (existRoom) {
console.log('--- use exist room. roomId=' + roomId);
} else {
console.log('--- create new room. roomId=' + roomId);
await setupRoom(roomId);
}
// --- socket.io room ---
socket.join(roomId);
setRoomname(roomId);
})
socket.on('error', function (err) {
console.error('socket ERROR:', err);
});
socket.on('connect_error', (err) => {
console.error('client connection error', err);
});
socket.on('getRouterRtpCapabilities', (data, callback) => {
getRouterRTP(data, callback, 0);
});
// --- producer streamer ----
socket.on('createProducerTransport', async (data, callback) => {
const room = Room.getRoom(data.roomName);
console.log('-- createProducerTransport ---');
producerSocketId = id;
const { transport, params } = await helpers_mediasoup.createTransport(room.router, mediasoupOptions);
helpers_mediasoup.addProducerTrasport(room, id, transport);
producerTransport = transport;
producerTransport.observer.on('close', () => {
const videoProducer = helpers_mediasoup.getProducer(room, id, 'video');
if (videoProducer) {
videoProducer.close();
helpers_mediasoup.removeProducer(room, id, 'video');
}
const audioProducer = helpers_mediasoup.getProducer(room, id, 'audio');
if (audioProducer) {
audioProducer.close();
helpers_mediasoup.removeProducer(room, id, 'audio');
}
helpers_mediasoup.removeProducerTransport(room, id);
});
//console.log('-- createProducerTransport params:', params);
helpers.sendResponse(params, callback);
});
socket.on('connectProducerTransport', async (data, callback) => {
const room = Room.getRoom(data.roomName);
let transport = room.getProducerTrasnport(id);
await transport.connect({ dtlsParameters: data.dtlsParameters });
helpers.sendResponse({}, callback);
});
socket.on('produce', async (data, callback) => {
const room = Room.getRoom(data.roomName);
const { kind, rtpParameters } = data;
console.log('-- produce --- kind=', kind);
const transport = room.getProducerTrasnport(id);
if (!transport) {
console.error('transport NOT EXIST for id=' + id);
return;
}
const producer = await transport.produce({ kind, rtpParameters });
helpers_mediasoup.addProducer(room, id, producer, kind);
producer.observer.on('close', () => {
console.log('producer closed --- kind=' + kind);
})
helpers.sendResponse({ id: producer.id }, callback);
if (room) {
console.log('--broadcast room=%s newProducer ---', room.name);
socket.broadcast.to(room.name).emit('newProducer', { socketId: id, producerId: producer.id, kind: producer.kind });
}
else {
console.log('--broadcast newProducer ---');
socket.broadcast.emit('newProducer', { socketId: id, producerId: producer.id, kind: producer.kind });
}
});
// --- consumer viewer ----
socket.on('createConsumerTransport', async (data, callback) => {
const roomId = data.roomName;
setRoomname(roomId);
socket.join(roomId);
console.log(socket.rooms);
const room = Room.getRoom(data.roomName);
console.log('-- createConsumerTransport ---');
const { transport, params } = await helpers_mediasoup.createTransport(room.router, mediasoupOptions);
helpers_mediasoup.addConsumerTrasport(room, id, transport);
transport.observer.on('close', () => {
console.log('--- consumerTransport closed. --')
helpers_mediasoup.removeConsumerSetDeep(room, localId);
helpers_mediasoup.removeConsumerTransport(room, id);
});
helpers.sendResponse(params, callback);
});
socket.on('connectConsumerTransport', async (data, callback) => {
const room = Room.getRoom(data.roomName);
console.log(room)
console.log('-- connectConsumerTransport ---');
let transport = helpers_mediasoup.getConsumerTrasnport(room, id);
if (!transport) {
console.error('transport NOT EXIST for id=' + id);
return;
}
await transport.connect({ dtlsParameters: data.dtlsParameters });
helpers.sendResponse({}, callback);
});
socket.on('consume', async (data, callback) => {
const kind = data.kind;
const room = Room.getRoom(data.roomName);
const producerSocketId = room.getOwnStream();
console.log('-- consume --kind=' + kind);
const producer = helpers_mediasoup.getProducer(room, producerSocketId, kind);
if (producer) {
let transport = helpers_mediasoup.getConsumerTrasnport(room, id);
if (!transport) {
console.error('transport NOT EXIST for id=' + id);
return;
}
const { consumer, params } = await helpers_mediasoup.createConsumer(transport, producer, data.rtpCapabilities, room.router); // producer must exist before consume
//subscribeConsumer = consumer;
helpers_mediasoup.addConsumerVA(room, id, consumer, kind);
consumer.observer.on('close', () => {
console.log('consumer closed ---');
})
consumer.on('producerclose', () => {
console.log('consumer -- on.producerclose');
consumer.close();
helpers_mediasoup.removeConsumerSetDeep(room.name, id);
if (room) {
console.log('--broadcast room=%s producerclose ---' + room.name);
socket.broadcast.to(room.name).emit('producerClosed', { localId: id, remoteId: "null", kind: kind });
socket.emit('producerClosed', { localId: id, remoteId: "null", kind: kind });
}
else {
console.log('--broadcast producerclose ---');
socket.broadcast.emit('producerClosed', { localId: id, remoteId: "null", kind: kind });
}
// -- notify to client ---
// socket.emit('producerClosed', { localId: id, remoteId: producerSocketId, kind: kind });
});
console.log('-- consumer ready ---');
helpers.sendResponse(params, callback);
}
else {
console.log('-- consume, but producer NOT READY');
const params = { producerId: null, id: null, kind: kind, rtpParameters: {} };
helpers.sendResponse(params, callback);
}
});
socket.on('resume', async (data, callback) => {
const room = Room.getRoom(data.roomName);
const kind = data.kind;
console.log('-- resume -- kind=' + kind);
if (kind === 'video') {
let consumer = helpers_mediasoup.getVideoConsumer(room, id, kind);
if (!consumer) {
console.error('consumer NOT EXIST for id=' + id);
helpers.sendResponse({}, callback);
return;
}
await consumer.resume();
helpers.sendResponse({}, callback);
}
else {
console.warn('NO resume for audio');
}
});
function setRoomname(room) {
socket.roomname = room;
}
function getRouterRTP(data, callback, numOfTries) {
const room = Room.getRoom(data.roomName);
numOfTries++;
if (room) {
const { router } = room;
if (router) {
console.log('getRouterRtpCapabilities: ', router.rtpCapabilities);
helpers.sendResponse(router.rtpCapabilities, callback);
}
else {
helpers.sendReject({ text: 'ERROR- router NOT READY' }, callback);
}
} else if (numOfTries < 10) {
setTimeout(() => {
console.log('try to get room');
getRouterRTP(data, callback, numOfTries);
numOfTries++;
console.log("Number of trying: " + numOfTries);
}, 500);
} else {
helpers.sendReject({ text: 'ERROR- router NOT READY' }, callback);
}
}
function getRoomname() {
const room = socket.roomname;
return room;
}
});
function cleanUpPeer(roomname, socket) {
const id = helpers.getId(socket);
const room = Room.getRoom(roomname);
if (room) {
const transport = helpers_mediasoup.getConsumerTrasnport(room, id);
if (transport) {
transport.close();
helpers_mediasoup.removeConsumerTransport(room, id);
}
const videoProducer = helpers_mediasoup.getProducer(room, id, 'video');
if (videoProducer) {
videoProducer.close();
helpers_mediasoup.removeProducer(room, id, 'video');
}
const audioProducer = helpers_mediasoup.getProducer(room, id, 'audio');
if (audioProducer) {
audioProducer.close();
helpers_mediasoup.removeProducer(room, id, 'audio');
}
const producerTransport = helpers_mediasoup.getProducerTrasnport(room, id);
if (producerTransport) {
producerTransport.close();
helpers_mediasoup.removeProducerTransport(room, id);
}
}
}
async function setupRoom(name) {
const room = new Room(name);
const mediaCodecs = mediasoupOptions.router.mediaCodecs;
const router = await worker.createRouter({ mediaCodecs });
router.roomname = name;
router.observer.on('close', () => {
console.log('-- router closed. room=%s', name);
});
router.observer.on('newtransport', transport => {
console.log('-- router newtransport. room=%s', name);
});
room.router = router;
Room.addRoom(room, name);
return room;
}
})();