Skip to content

Commit 5457ecc

Browse files
committed
WIP webrtc media server relay for multiple media servers
1 parent 4862d60 commit 5457ecc

6 files changed

Lines changed: 472 additions & 133 deletions

File tree

config.example.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
"serve_selector": true,
1414
"require_release_date_cookie": true,
1515
"cache_authenticated_get_requests" : true,
16+
"mr_server": false,
17+
"mr_server_port" : 4444,
1618
"default_client_build": "october_5_2017",
1719
"debug_logs": {
1820
"gateway" : true,
1921
"rtc" : true,
2022
"media" : true,
2123
"udp" : true,
2224
"dispatcher" : true,
25+
"mr" : true,
2326
"rest" : true,
2427
"errors": true
2528
},

handlers/mr.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const { EventEmitter } = require("ws");
2+
const globalUtils = require("../helpers/globalutils");
3+
const session = require("../helpers/session");
4+
5+
const OPCODES = {
6+
IDENTIFY: "IDENTIFY",
7+
ALRIGHT: "ALRIGHT",
8+
HEARTBEAT_INFO: "HEARTBEAT_INFO",
9+
ANSWER: "ANSWER",
10+
VIDEO_BATCH: "VIDEO_BATCH",
11+
SPEAKING_BATCH: "SPEAKING_BATCH",
12+
HEARTBEAT: "HEARTBEAT",
13+
HEARTBEAT_ACK: "HEARTBEAT_ACK",
14+
};
15+
16+
async function handleIdentify(socket, packet) {
17+
let public_ip = packet.d.public_ip;
18+
let public_port = packet.d.public_port;
19+
let timestamp = packet.d.timestamp;
20+
21+
global.mrServer.debug(`New media server has connected! Added to internal store.`);
22+
23+
//to-do find a proper & fast way to lookup these public ips to serve whats close to a user
24+
25+
socket.public_ip = public_ip;
26+
socket.public_port = public_port;
27+
socket.emitter = new EventEmitter();
28+
29+
global.mrServer.servers.set(public_ip, {
30+
socket: socket,
31+
port: public_port,
32+
seen_at: timestamp
33+
});
34+
35+
socket.send(JSON.stringify({
36+
op: OPCODES.ALRIGHT,
37+
d: {
38+
location: global.mrServer.servers.size + 1
39+
}
40+
}));
41+
}
42+
43+
async function handleHeartbeat(socket, packet) {
44+
if (!socket.hb) return;
45+
46+
socket.hb.acknowledge(packet.d);
47+
socket.hb.reset();
48+
}
49+
50+
async function handleAnswer(socket, packet) {
51+
socket.emitter.emit('answer-received', packet.d);
52+
}
53+
54+
async function handleVideoBatch(socket, packet) {
55+
socket.emitter.emit('video-batch', packet.d);
56+
}
57+
58+
59+
async function handleSpeakingBatch(socket, packet) {
60+
socket.emitter.emit('speaking-batch', packet.d);
61+
}
62+
63+
const mrHandlers = {
64+
[OPCODES.IDENTIFY]: handleIdentify,
65+
[OPCODES.HEARTBEAT]: handleHeartbeat,
66+
[OPCODES.ANSWER]: handleAnswer,
67+
[OPCODES.VIDEO_BATCH]: handleVideoBatch,
68+
[OPCODES.SPEAKING_BATCH]: handleSpeakingBatch
69+
};
70+
71+
module.exports = {
72+
mrHandlers,
73+
OPCODES
74+
};

0 commit comments

Comments
 (0)