-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
474 lines (378 loc) · 14.5 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
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
466
467
468
469
470
471
472
473
474
const express = require('express');
const fs = require('fs');
const http = require('http');
const https = require('https');
const WSServer = require("./scripts_serverside/WSServer.js");
var AppState = WSServer.AppState;
const WebSocket = require('ws');
// const expressWs = require("express-ws");
var createError = require('http-errors');
var path = require('path');
var cookieParser = require('cookie-parser');
// var logger = require('morgan');
const app = express();
const { networkInterfaces } = require('os');
const fileUpload = require('express-fileupload');
const PORT = 3000;
// enable files upload
app.use(fileUpload({
createParentPath: true
}));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
// app.use(express.static(path.join(__dirname, 'public')));
// Page Table
app.get('/', (req, res) => {
res.sendFile(__dirname+"/pages/main.html");
console.log("sent main.html");
})
// WebRTC testing
app.get('/webrtc', (req, res) => {
res.sendFile(__dirname+"/pages/webrtc.html");
console.log("sent webrtc.html");
})
// WebRTC nonVR testing
app.get('/webrtc_nonvr', (req, res) => {
res.sendFile(__dirname+"/pages/webrtc_nonvr.html");
console.log("sent webrtc_nonvr.html");
})
// converting testing
app.get('/convert', (req, res) => {
res.sendFile(__dirname+"/pages/convert.html");
console.log("sent convert.html");
})
// converting testing
app.post('/convert', (req, res) => {
console.log(req.files.audio_file)
// Work on the file
fs.writeFileSync(__dirname+'/tmp/' + req.files.audio_file.name, Buffer.from(req.files.audio_file.data))
convertFile(__dirname+'/tmp/' + req.files.audio_file.name);
// Send the html page
res.download(__dirname+'/tmp/testOutput.mp3', req.files.audio_file.name + ".mp3");
console.log("sent convert.html");
})
app.use("/js", express.static(__dirname+'/scripts/'));
app.use("/jquery.js", express.static(__dirname+"/node_modules/jquery/dist/jquery.js"));
app.use("/babylon.js", express.static(__dirname+"/node_modules/babylonjs/babylon.js"));
app.use("/assets", express.static(__dirname+"/assets/"));
app.use("/math.js", express.static(__dirname+"/node_modules/mathjs/lib/browser/math.js"))
app.use("/favicon.ico", express.static(__dirname+"/assets/favicon.ico"));
app.use("/babylon-vrm.js", express.static(__dirname+"/node_modules/babylon-vrm-loader/dist/index.js"));
/*
=====================================
RUN SERVER
=====================================
*/
http.createServer(app).listen(80);
var httpsServer = https.createServer({ // need https for webcam
key: fs.readFileSync(__dirname+'/cert/tlsharkey.com.key'),
cert: fs.readFileSync(__dirname+'/cert/tlsharkey.com.crt')
}, app);
httpsServer.listen(443, (err) => {
if (err) console.log("Error starting express server");
// Get IP
let nets = networkInterfaces();
let results = Object.create(null); // or just '{}', an empty object
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
// skip over non-ipv4 and internal (i.e. 127.0.0.1) addresses
if (net.family === 'IPv4' && !net.internal) {
if (!results[name]) {
results[name] = [];
}
results[name].push(net.address);
}
}
}
console.log(results);
let ip = results[Object.keys(results)[0]][0]
console.log("Hosting Server on https://"+ip);
});
// expressWs(app, httpsServer);
// app.ws("/cnxtn", (ws, req) => {
// console.log("Websocket connected");
// WSServer.onClientConnect(ws);
// })
const wss = new WebSocket.Server({
// server: httpsServer,
port: 8000
})
wss.on('connection', function(ws) {
console.log("Websocket Connected");
WSServer.onClientConnect(ws);
})
// for converting
function convertFile(filename) {
console.log(filename)
if (filename.endsWith(".ogg")) {
execSync('ffmpeg -y -i \'' + filename + '\' -acodec libmp3lame \'' + __dirname+ "/tmp/testOutput.mp3\'")
console.log("Done with converting ogg --> mp3")
} else if (filename.endsWith(".weba") || filename.endsWith(".pcm")) {
execSync('ffmpeg -y -ac 2 -i \'' + filename +"\' -c:a libmp3lame -b:a 256k \'" + __dirname + "/tmp/testOutput.mp3\'")
console.log("Done with converting pcm --> mp3")
}
}
/*
================================================================================
WebRTC
================================================================================
*/
/**
* server mediasoup code for webrtc
* https://github.com/Dirvann/mediasoup-sfu-webrtc-video-rooms
*
*/
const io = require('socket.io')(httpsServer)
const mediasoup = require('mediasoup')
const config = require('./scripts_serverside/config')
// Begin worker methods
// Mediasoup workers
let workers = []
let nextMediasoupIndex = 0
// Initialize the workers asynchronously
;
(async () => {
await createWorkers()
})()
// function to initialize the workers
async function createWorkers() {
// Gets the system's cpu count
let {
numWorkers
} = config.mediasoup
// Initialize a single worker for each cpu core and put it into the list
for (let i = 0; i < numWorkers; i++) {
// Creates a new worker
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,
})
// if a worker unexpectedly dies, we want to exit that process.
worker.on('died', () => {
console.error('mediasoup worker died, exiting in 2 seconds... [pid:%d]', worker.pid);
setTimeout(() => process.exit(1), 2000);
})
workers.push(worker)
}
}
/**
* Get next mediasoup Worker. This gets the next worker so that we can
* evenly distribute the work among all workers.
*/
function getMediasoupWorker() {
const worker = workers[nextMediasoupIndex];
nextMediasoupIndex += 1;
nextMediasoupIndex %= workers.length;
return worker;
}
// End worker methods
// Begin room list and initializations
// Each Room is a meeting
const Room = require('./scripts_serverside/Room')
// Each peer is a user
const Peer = require('./scripts_serverside/Peer');
const { time } = require('console');
const { execSync } = require('child_process');
/**
* roomList
* {
* room_id: Room {
* id:
* router:
* peers: {
* id:,
* name:,
* master: [boolean],
* transports: [Map],
* producers: [Map],
* consumers: [Map],
* rtpCapabilities:
* }
* }
* }
*/
// Initialize a room list for all possible rooms by the server
let roomList = new Map()
// on a connection request from the client (i.e. client goes to IP)
io.on('connection', socket => {
socket.on("srslyMessage", (msg) => {
WSServer.onMessage(socket, msg);
})
// If client wants to create a room.
socket.on('createRoom', async ({
room_id
}, callback) => {
// Given a room_id, create a room.
if (roomList.has(room_id)) {
callback('already exists')
} else {
console.log('---created room--- ', room_id)
// gets a worker for this room
let worker = await getMediasoupWorker()
// This list will have a Room object
roomList.set(room_id, new Room(room_id, worker, io))
// calls the callback function finally
callback(room_id)
}
})
// On a join event to join a room:
socket.on('join', ({
room_id,
name
}, callback) => {
// log the user trying to join the room and callback an error if it doesn't exist.
console.log('---user joined--- \"' + room_id + '\": ' + name)
if (!roomList.has(room_id)) {
return callback({
error: 'room does not exist'
})
}
// Create a peer object to keep track of the user
roomList.get(room_id).addPeer(new Peer(socket.id, name))
socket.room_id = room_id
// Get the Room and callback on that data.
callback(roomList.get(room_id).toJson())
})
// On an event to get the producers of a room, usually called by people joining the call
socket.on('getProducers', () => {
console.log(`---get producers--- name:${roomList.get(socket.room_id).getPeers().get(socket.id).name}`)
// send all the current producer to newly joined member
if (!roomList.has(socket.room_id)) return
// getProducerListForPeer simply just gets all the producers for that room
let producerList = roomList.get(socket.room_id).getProducerListForPeer(socket.id)
// tells all the other members in the call that there is a new producer
socket.emit('newProducers', producerList)
})
// Client asks for router rtp capabilities
socket.on('getRouterRtpCapabilities', (_, callback) => {
console.log(`---get RouterRtpCapabilities--- name: ${roomList.get(socket.room_id).getPeers().get(socket.id).name}`)
try {
callback(roomList.get(socket.room_id).getRtpCapabilities());
} catch (e) {
callback({
error: e.message
})
}
});
// Client asks to create a webrtc transport for producers and consumer pipes
socket.on('createWebRtcTransport', async (_, callback) => {
console.log(`---create webrtc transport--- name: ${roomList.get(socket.room_id).getPeers().get(socket.id).name}`)
try {
const {
params
} = await roomList.get(socket.room_id).createWebRtcTransport(socket.id);
callback(params);
} catch (err) {
console.error(err);
callback({
error: err.message
});
}
});
// Connects the transport from the client to the room
socket.on('connectTransport', async ({
transport_id,
dtlsParameters
}, callback) => {
console.log(`---connect transport--- name: ${roomList.get(socket.room_id).getPeers().get(socket.id).name}`)
if (!roomList.has(socket.room_id)) return
// Gets the room and connects the transport from the client by setting the endpoints
await roomList.get(socket.room_id).connectPeerTransport(socket.id, transport_id, dtlsParameters)
callback('success')
})
// When a client wants to send data to the server (produce media); these parameters will be used by the server to create a Producer instance.
socket.on('produce', async ({
kind,
rtpParameters,
producerTransportId
}, callback) => {
if(!roomList.has(socket.room_id)) {
return callback({error: 'not is a room'})
}
// with the transport id, we want to tell the router to receive audio or video RTP
let producer_id = await roomList.get(socket.room_id).produce(socket.id, producerTransportId, rtpParameters, kind)
console.log(`---produce--- type: ${kind} name: ${roomList.get(socket.room_id).getPeers().get(socket.id).name} id: ${producer_id}`)
callback({
producer_id
})
})
// When a client wants to consume data from the server, to extract media from media soup
socket.on('consume', async ({
consumerTransportId,
producerId,
rtpCapabilities
}, callback) => {
if(!roomList.has(socket.room_id)) {
return callback({error: 'not is a room'})
}
//TODO null handling
// gets the parameters for Consumer
let params = await roomList.get(socket.room_id).consume(socket.id, consumerTransportId, producerId, rtpCapabilities)
console.log(`---consuming--- name: ${roomList.get(socket.room_id) && roomList.get(socket.room_id).getPeers().get(socket.id).name} prod_id:${producerId} consumer_id:${params.id}`)
callback(params)
})
// ERIC: this is not used at all, maybe used when we want to resume certain streams
// to continue resumption of a transport
// socket.on('resume', async (data, callback) => {
// await consumer.resume();
// callback();
// });
// Obtains a room's info of the client
socket.on('getMyRoomInfo', (_, cb) => {
cb(roomList.get(socket.room_id).toJson())
})
// When a user disconnets from the room
socket.on('disconnect', () => {
console.log(`---disconnect--- name: ${roomList.get(socket.room_id) && roomList.get(socket.room_id).getPeers().get(socket.id).name}`)
if (!socket.room_id) return
// This removePeer will handle all the required actions to remove the user from the Room
roomList.get(socket.room_id).removePeer(socket.id)
WSServer.onClose(socket);
})
// when a producer closes (i.e. someone stops sending audio/video)
socket.on('producerClosed', ({
producer_id
}) => {
console.log(`---producer close--- name: ${roomList.get(socket.room_id) && roomList.get(socket.room_id).getPeers().get(socket.id).name}`)
roomList.get(socket.room_id).closeProducer(socket.id, producer_id)
})
// When someone exits the room
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; removePeer will handle all the required actions to remove the user from the Room
await roomList.get(socket.room_id).removePeer(socket.id)
// If no one else in the room, delete the room
if (roomList.get(socket.room_id).getPeers().size === 0) {
roomList.delete(socket.room_id)
}
socket.room_id = null
callback('successfully exited room')
})
})
// not used
// function room() {
// return Object.values(roomList).map(r => {
// return {
// router: r.router.id,
// peers: Object.values(r.peers).map(p => {
// return {
// name: p.name,
// }
// }),
// id: r.id
// }
// })
// }