-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
executable file
·104 lines (89 loc) · 2.85 KB
/
main.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
/*
* Meething Mediasoup Server
* Meething Mediasoup SFU
* https://github.com/meething
*
*/
const http = require("https");
const { WebSocketServer } = require("protoo-server");
const mediasoup = require("mediasoup");
const ConfRoom = require("./lib/Room");
const fs = require('fs');
const stun = require('stun');
// LRU with last used sockets
const QuickLRU = require("quick-lru");
const lru = new QuickLRU({ maxSize: 1000, onEviction: false });
const path = require('path')
var options = {
cert: process.env.SSLCERT ? fs.readFileSync(process.env.SSLCERT) : false,
key: process.env.SSLKEY ? fs.readFileSync(process.env.SSLKEY) : false,
};
(async () => {
var serverOptions = {
rtcMinPort: process.env.MINPORT || 20000,
rtcMaxPort: process.env.MAXPORT || 29999
};
const res = await stun.request('stun.l.google.com:19302');
var pubIp = res.getXorAddress().address;
if(pubIp) {
console.log('Detected Server IP', pubIp);
serverOptions.rtcAnnouncedIPv4 = pubIp;
}
const worker = await mediasoup.createWorker(serverOptions);
worker.on("died", () => {
console.log("mediasoup Worker died, exit..");
process.exit(1);
});
const router = await worker.createRouter({
mediaCodecs: [
{
kind: "audio",
name: "opus",
mimeType: "audio/opus",
clockRate: 48000,
channels: 2
},
{
kind: "video",
name: "VP8",
mimeType: "video/VP8",
clockRate: 90000
}
]
});
const httpServer = http.createServer(options);
await new Promise(resolve => {
httpServer.listen(process.env.PORT || 2345, "0.0.0.0", resolve);
});
const wsServer = new WebSocketServer(httpServer);
wsServer.on("connectionrequest", (info, accept) => {
console.log(
"protoo connection request [peerId:%s, address:%s, room:%s]",
info.socket.remoteAddress,
info.request.url
);
// Parse WSS Query Paraemters
var queryString = info.request.url || '';
if(queryString.substr(0,1) === '/') { queryString = queryString.substr(1, queryString.length); }
const urlParams = new URLSearchParams(queryString);
const roomId = urlParams.get('roomId') || 'lobby';
const peerId = urlParams.get('peerId') || `p${String(Math.random()).slice(2)}`;
if (lru.has(roomId)) {
var room = lru.get(roomId);
room.handlePeerConnect({
peerId: peerId,
protooWebSocketTransport: accept()
});
console.log("existing room stat", roomId, peerId, room.getStatus() );
} else {
var room = new ConfRoom(router);
lru.set(roomId,room);
room.handlePeerConnect({
peerId: peerId,
protooWebSocketTransport: accept()
});
console.log("new room stat", roomId, peerId, room.getStatus() );
}
});
console.log("MediaSoup server started on wss://0.0.0.0:",process.env.PORT||2345);
})();