summary refs log tree commit diff
path: root/webrtc/src
diff options
context:
space:
mode:
Diffstat (limited to 'webrtc/src')
-rw-r--r--webrtc/src/Server.ts100
-rw-r--r--webrtc/src/opcodes/Connect.ts10
-rw-r--r--webrtc/src/opcodes/Heartbeat.ts8
-rw-r--r--webrtc/src/opcodes/Identify.ts57
-rw-r--r--webrtc/src/opcodes/Resume.ts6
-rw-r--r--webrtc/src/opcodes/SelectProtocol.ts128
-rw-r--r--webrtc/src/opcodes/Speaking.ts7
-rw-r--r--webrtc/src/opcodes/index.ts37
-rw-r--r--webrtc/src/start.ts7
-rw-r--r--webrtc/src/util/Heartbeat.ts18
-rw-r--r--webrtc/src/util/index.ts1
11 files changed, 357 insertions, 22 deletions
diff --git a/webrtc/src/Server.ts b/webrtc/src/Server.ts

index 6591691c..dcbf216a 100644 --- a/webrtc/src/Server.ts +++ b/webrtc/src/Server.ts
@@ -1,46 +1,102 @@ import { Server as WebSocketServer } from "ws"; -import { Config, db } from "@fosscord/util"; -import mediasoup from "mediasoup"; +import { WebSocket, Payload, CLOSECODES } from "@fosscord/gateway"; +import { Config, initDatabase } from "@fosscord/util"; +import OPCodeHandlers from "./opcodes"; +import { setHeartbeat } from "./util"; +import * as mediasoup from "mediasoup"; +import { types as MediasoupTypes } from "mediasoup"; var port = Number(process.env.PORT); if (isNaN(port)) port = 3004; export class Server { public ws: WebSocketServer; - public turn: any; + public mediasoupWorkers: MediasoupTypes.Worker[] = []; + public mediasoupRouters: MediasoupTypes.Router[] = []; + public mediasoupTransports: MediasoupTypes.Transport[] = []; constructor() { this.ws = new WebSocketServer({ port, maxPayload: 4096, }); - this.ws.on("connection", (socket) => { - socket.on("message", (message) => { - socket.emit( - JSON.stringify({ - op: 2, - d: { - ssrc: 1, - ip: "127.0.0.1", - port: 3004, - modes: [ - "xsalsa20_poly1305", - "xsalsa20_poly1305_suffix", - "xsalsa20_poly1305_lite", - ], - heartbeat_interval: 1, - }, - }) - ); + this.ws.on("connection", async (socket: WebSocket) => { + await setHeartbeat(socket); + + socket.on("message", async (message: string) => { + const payload: Payload = JSON.parse(message); + + if (OPCodeHandlers[payload.op]) + await OPCodeHandlers[payload.op].call(this, socket, payload); + else { + console.error(`Unimplemented`, payload); + socket.close(CLOSECODES.Unknown_opcode); + } }); }); } async listen(): Promise<void> { // @ts-ignore - await (db as Promise<Connection>); + await initDatabase(); await Config.init(); + await this.createWorkers(); console.log("[DB] connected"); console.log(`[WebRTC] online on 0.0.0.0:${port}`); } + + async createWorkers(): Promise<void> { + const numWorkers = 1; + for (let i = 0; i < numWorkers; i++) { + const worker = await mediasoup.createWorker({ logLevel: "debug" }); + if (!worker) return; + + worker.on("died", () => { + console.error("mediasoup worker died"); + }); + + worker.observer.on("newrouter", async (router: MediasoupTypes.Router) => { + console.log("new router created [id:%s]", router.id); + + this.mediasoupRouters.push(router); + + router.observer.on("newtransport", async (transport: MediasoupTypes.Transport) => { + console.log("new transport created [id:%s]", transport.id); + + await transport.enableTraceEvent(); + + transport.observer.on("newproducer", (producer: MediasoupTypes.Producer) => { + console.log("new producer created [id:%s]", producer.id); + }); + + transport.observer.on("newconsumer", (consumer: MediasoupTypes.Consumer) => { + console.log("new consumer created [id:%s]", consumer.id); + }); + + transport.observer.on("newdataproducer", (dataProducer) => { + console.log("new data producer created [id:%s]", dataProducer.id); + }); + + transport.on("trace", (trace) => { + console.log(trace); + }); + + this.mediasoupTransports.push(transport); + }); + }); + + await worker.createRouter({ + mediaCodecs: [ + { + kind: "audio", + mimeType: "audio/opus", + clockRate: 48000, + channels: 2 + }, + ] + }); + + this.mediasoupWorkers.push(worker); + } + } } diff --git a/webrtc/src/opcodes/Connect.ts b/webrtc/src/opcodes/Connect.ts new file mode 100644
index 00000000..b312d6f2 --- /dev/null +++ b/webrtc/src/opcodes/Connect.ts
@@ -0,0 +1,10 @@ +import { WebSocket } from "@fosscord/gateway"; +import { Payload } from "./index"; +import { Server } from "../Server" + +export async function onConnect(this: Server, socket: WebSocket, data: Payload) { + socket.send(JSON.stringify({ + op: 15, + d: { any: 100 } + })) +} \ No newline at end of file diff --git a/webrtc/src/opcodes/Heartbeat.ts b/webrtc/src/opcodes/Heartbeat.ts new file mode 100644
index 00000000..06d6bcb1 --- /dev/null +++ b/webrtc/src/opcodes/Heartbeat.ts
@@ -0,0 +1,8 @@ +import { WebSocket } from "@fosscord/gateway"; +import { Payload } from "./index"; +import { setHeartbeat } from "./../util"; +import { Server } from "../Server" + +export async function onHeartbeat(this: Server, socket: WebSocket, data: Payload) { + await setHeartbeat(socket); +} \ No newline at end of file diff --git a/webrtc/src/opcodes/Identify.ts b/webrtc/src/opcodes/Identify.ts new file mode 100644
index 00000000..82f327be --- /dev/null +++ b/webrtc/src/opcodes/Identify.ts
@@ -0,0 +1,57 @@ +import { WebSocket } from "@fosscord/gateway"; +import { Payload } from "./index"; +import { VoiceOPCodes } from "@fosscord/util"; +import { Server } from "../Server"; + +export async function onIdentify(this: Server, socket: WebSocket, data: Payload) { + var transport = await this.mediasoupRouters[0].createWebRtcTransport({ + listenIps: [{ ip: "0.0.0.0", announcedIp: "127.0.0.1" }], + enableUdp: true, + enableTcp: true, + preferUdp: true, + }); + + /* + //discord proper sends: + { + "streams": [ + { "type": "video", "ssrc": 1311885, "rtx_ssrc": 1311886, "rid": "50", "quality": 50, "active": false }, + { "type": "video", "ssrc": 1311887, "rtx_ssrc": 1311888, "rid": "100", "quality": 100, "active": false } + ], + "ssrc": 1311884, + "port": 50008, + "modes": [ + "aead_aes256_gcm_rtpsize", + "aead_aes256_gcm", + "xsalsa20_poly1305_lite_rtpsize", + "xsalsa20_poly1305_lite", + "xsalsa20_poly1305_suffix", + "xsalsa20_poly1305" + ], + "ip": "109.200.214.158", + "experiments": [ + "bwe_conservative_link_estimate", + "bwe_remote_locus_client", + "fixed_keyframe_interval" + ] + } + */ + + socket.send(JSON.stringify({ + op: VoiceOPCodes.READY, + d: { + streams: [], + ssrc: 1, + ip: transport.iceCandidates[0].ip, + port: transport.iceCandidates[0].port, + modes: [ + "aead_aes256_gcm_rtpsize", + // "xsalsa20_poly1305", + // "xsalsa20_poly1305_suffix", + // "xsalsa20_poly1305_lite", + ], + heartbeat_interval: 1, + experiments: [], + }, + })); +} \ No newline at end of file diff --git a/webrtc/src/opcodes/Resume.ts b/webrtc/src/opcodes/Resume.ts new file mode 100644
index 00000000..dcd4f4cd --- /dev/null +++ b/webrtc/src/opcodes/Resume.ts
@@ -0,0 +1,6 @@ +import { WebSocket } from "@fosscord/gateway"; +import { Payload } from "./index"; +import { Server } from "../Server" + +export async function onResume(this: Server, socket: WebSocket, data: Payload) { +} \ No newline at end of file diff --git a/webrtc/src/opcodes/SelectProtocol.ts b/webrtc/src/opcodes/SelectProtocol.ts new file mode 100644
index 00000000..36527a8b --- /dev/null +++ b/webrtc/src/opcodes/SelectProtocol.ts
@@ -0,0 +1,128 @@ +import { WebSocket } from "@fosscord/gateway"; +import { Payload } from "./index"; +import { VoiceOPCodes } from "@fosscord/util"; +import { Server } from "../Server"; +import * as mediasoup from "mediasoup"; +import { RtpCodecCapability } from "mediasoup/node/lib/RtpParameters"; +import * as sdpTransform from 'sdp-transform'; + +/* + { + op: 1, + d: { + protocol: "webrtc", + data: " + a=extmap-allow-mixed + a=ice-ufrag:ilWh + a=ice-pwd:Mx7TDnPKXDnTgYWC+qMaqspQ + a=ice-options:trickle + a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level + a=extmap:2 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time + a=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01 + a=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid + a=rtpmap:111 opus/48000/2 + a=extmap:14 urn:ietf:params:rtp-hdrext:toffset + a=extmap:13 urn:3gpp:video-orientation + a=extmap:5 http://www.webrtc.org/experiments/rtp-hdrext/playout-delay + a=extmap:6 http://www.webrtc.org/experiments/rtp-hdrext/video-content-type + a=extmap:7 http://www.webrtc.org/experiments/rtp-hdrext/video-timing + a=extmap:8 http://www.webrtc.org/experiments/rtp-hdrext/color-space + a=extmap:10 urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id + a=extmap:11 urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id + a=rtpmap:96 VP8/90000 + a=rtpmap:97 rtx/90000 + ", + sdp: "same data as in d.data? also not documented by discord", + codecs: [ + { + name: "opus", + type: "audio", + priority: 1000, + payload_type: 111, + rtx_payload_type: null, + }, + { + name: "H264", + type: "video", + priority: 1000, + payload_type: 102, + rtx_payload_type: 121, + }, + { + name: "VP8", + type: "video", + priority: 2000, + payload_type: 96, + rtx_payload_type: 97, + }, + { + name: "VP9", + type: "video", + priority: 3000, + payload_type: 98, + rtx_payload_type: 99, + }, + ], + rtc_connection_id: "b3c8628a-edb5-49ae-b860-ab0d2842b104", + }, + } +*/ + +var test_hasMadeProducer = false; + +export async function onSelectProtocol(this: Server, socket: WebSocket, data: Payload) { + const rtpCapabilities = this.mediasoupRouters[0].rtpCapabilities; + const codecs = rtpCapabilities.codecs as RtpCodecCapability[]; + + const transport = this.mediasoupTransports[0]; //whatever + + const res = sdpTransform.parse(data.d.sdp); + + /* + res.media.map(x => x.rtp).flat(1).map(x => ({ + codec: x.codec, + payloadType: x.payload, + clockRate: x.rate as number, + mimeType: `audio/${x.codec}`, + })), + */ + + if (!test_hasMadeProducer) { + const producer = await transport.produce({ + kind: "audio", + rtpParameters: { + mid: "audio", + codecs: [{ + clockRate: 48000, + payloadType: 111, + mimeType: "audio/opus", + channels: 2, + }], + headerExtensions: res.ext?.map(x => ({ + id: x.value, + uri: x.uri, + })) + }, + paused: false, + }); + + const consumer = await transport.consume({ + producerId: producer.id, + paused: false, + rtpCapabilities, + }) + + test_hasMadeProducer = true; + } + + socket.send(JSON.stringify({ + op: VoiceOPCodes.SESSION_DESCRIPTION, + d: { + video_codec: data.d.codecs.find((x: any) => x.type === "video").name, + secret_key: new Array(32).fill(null).map(x => Math.random() * 256), + mode: "xsalsa20_poly1305", + media_session_id: this.mediasoupTransports[0].id, + audio_codec: data.d.codecs.find((x: any) => x.type === "audio").name, + } + })); +} \ No newline at end of file diff --git a/webrtc/src/opcodes/Speaking.ts b/webrtc/src/opcodes/Speaking.ts new file mode 100644
index 00000000..861a7c3d --- /dev/null +++ b/webrtc/src/opcodes/Speaking.ts
@@ -0,0 +1,7 @@ +import { WebSocket } from "@fosscord/gateway"; +import { Payload } from "./index" +import { VoiceOPCodes } from "@fosscord/util"; +import { Server } from "../Server" + +export async function onSpeaking(this: Server, socket: WebSocket, data: Payload) { +} \ No newline at end of file diff --git a/webrtc/src/opcodes/index.ts b/webrtc/src/opcodes/index.ts new file mode 100644
index 00000000..36d30e7d --- /dev/null +++ b/webrtc/src/opcodes/index.ts
@@ -0,0 +1,37 @@ +import { WebSocket } from "@fosscord/gateway"; +import { VoiceOPCodes } from "@fosscord/util"; + +export interface Payload { + op: number; + d?: any; + s?: number; + t?: string; +} + +import { onIdentify } from "./Identify"; +import { onSelectProtocol } from "./SelectProtocol"; +import { onHeartbeat } from "./Heartbeat"; +import { onSpeaking } from "./Speaking"; +import { onResume } from "./Resume"; +import { onConnect } from "./Connect"; + +export type OPCodeHandler = (this: WebSocket, data: Payload) => any; + +export default { + [VoiceOPCodes.IDENTIFY]: onIdentify, //op 0 + [VoiceOPCodes.SELECT_PROTOCOL]: onSelectProtocol, //op 1 + //op 2 voice_ready + [VoiceOPCodes.HEARTBEAT]: onHeartbeat, //op 3 + //op 4 session_description + [VoiceOPCodes.SPEAKING]: onSpeaking, //op 5 + //op 6 heartbeat_ack + [VoiceOPCodes.RESUME]: onResume, //op 7 + //op 8 hello + //op 9 resumed + //op 10? + //op 11? + [VoiceOPCodes.CLIENT_CONNECT]: onConnect, //op 12 + //op 13? + //op 15? + //op 16? empty data on client send but server sends {"voice":"0.8.24+bugfix.voice.streams.opt.branch-ffcefaff7","rtc_worker":"0.3.14-crypto-collision-copy"} +}; \ No newline at end of file diff --git a/webrtc/src/start.ts b/webrtc/src/start.ts
index 68867a2c..299bfce8 100644 --- a/webrtc/src/start.ts +++ b/webrtc/src/start.ts
@@ -1,3 +1,10 @@ +import { config } from "dotenv"; +config(); + import { Server } from "./Server"; +//testing +process.env.DATABASE = "../bundle/database.db"; + const server = new Server(); +server.listen(); \ No newline at end of file diff --git a/webrtc/src/util/Heartbeat.ts b/webrtc/src/util/Heartbeat.ts new file mode 100644
index 00000000..7b5ed9cd --- /dev/null +++ b/webrtc/src/util/Heartbeat.ts
@@ -0,0 +1,18 @@ +import { WebSocket, CLOSECODES } from "@fosscord/gateway"; +import { VoiceOPCodes } from "@fosscord/util"; + +export async function setHeartbeat(socket: WebSocket) { + if (socket.heartbeatTimeout) clearTimeout(socket.heartbeatTimeout); + + socket.heartbeatTimeout = setTimeout(() => { + return socket.close(CLOSECODES.Session_timed_out); + }, 1000 * 45); + + socket.send(JSON.stringify({ + op: VoiceOPCodes.HEARTBEAT_ACK, + d: { + v: 6, + heartbeat_interval: 13750, + } + })); +} \ No newline at end of file diff --git a/webrtc/src/util/index.ts b/webrtc/src/util/index.ts new file mode 100644
index 00000000..e8557452 --- /dev/null +++ b/webrtc/src/util/index.ts
@@ -0,0 +1 @@ +export * from "./Heartbeat" \ No newline at end of file