summary refs log tree commit diff
path: root/webrtc
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-01-17 02:59:26 +1100
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-01-17 02:59:26 +1100
commit7c87f8d60a15359a129ab076c212cbf4f1cd8884 (patch)
tree29386e8ad0dbb2bb94425ce4bde2730a3974b89b /webrtc
parentmessing around with things I don't understand (diff)
downloadserver-7c87f8d60a15359a129ab076c212cbf4f1cd8884.tar.xz
more fuckery
Diffstat (limited to 'webrtc')
-rw-r--r--webrtc/package-lock.json14
-rw-r--r--webrtc/package.json1
-rw-r--r--webrtc/src/Server.ts31
-rw-r--r--webrtc/src/opcodes/Connect.ts4
-rw-r--r--webrtc/src/opcodes/Identify.ts68
-rw-r--r--webrtc/src/opcodes/SelectProtocol.ts70
-rw-r--r--webrtc/src/start.ts6
7 files changed, 175 insertions, 19 deletions
diff --git a/webrtc/package-lock.json b/webrtc/package-lock.json
index 6c3726dc..43bb8cd8 100644
--- a/webrtc/package-lock.json
+++ b/webrtc/package-lock.json
@@ -9,6 +9,7 @@
 			"version": "1.0.0",
 			"license": "ISC",
 			"dependencies": {
+				"dotenv": "^12.0.4",
 				"mediasoup": "^3.9.5",
 				"node-turn": "^0.0.6",
 				"tsconfig-paths": "^3.12.0",
@@ -210,6 +211,14 @@
 				"node": ">=0.3.1"
 			}
 		},
+		"node_modules/dotenv": {
+			"version": "12.0.4",
+			"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-12.0.4.tgz",
+			"integrity": "sha512-oWdqbSywffzH1l4WXKPHWA0TWYpqp7IyLfqjipT4upoIFS0HPMqtNotykQpD4iIg0BqtNmdgPCh2WMvMt7yTiw==",
+			"engines": {
+				"node": ">=12"
+			}
+		},
 		"node_modules/esprima": {
 			"version": "4.0.1",
 			"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
@@ -672,6 +681,11 @@
 			"integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
 			"dev": true
 		},
+		"dotenv": {
+			"version": "12.0.4",
+			"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-12.0.4.tgz",
+			"integrity": "sha512-oWdqbSywffzH1l4WXKPHWA0TWYpqp7IyLfqjipT4upoIFS0HPMqtNotykQpD4iIg0BqtNmdgPCh2WMvMt7yTiw=="
+		},
 		"esprima": {
 			"version": "4.0.1",
 			"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
diff --git a/webrtc/package.json b/webrtc/package.json
index 8c66245d..d5a994a1 100644
--- a/webrtc/package.json
+++ b/webrtc/package.json
@@ -18,6 +18,7 @@
 		"typescript": "^4.3.2"
 	},
 	"dependencies": {
+		"dotenv": "^12.0.4",
 		"mediasoup": "^3.9.5",
 		"node-turn": "^0.0.6",
 		"tsconfig-paths": "^3.12.0",
diff --git a/webrtc/src/Server.ts b/webrtc/src/Server.ts
index cdda10ec..1d2e73e7 100644
--- a/webrtc/src/Server.ts
+++ b/webrtc/src/Server.ts
@@ -54,21 +54,32 @@ export class Server {
 			});
 
 			worker.observer.on("newrouter", async (router: MediasoupTypes.Router) => {
-				console.log("new router");
+				console.log("new router created [id:%s]", router.id);
 
 				this.mediasoupRouters.push(router);
 
-				router.observer.on("newtransport", (transport: MediasoupTypes.Transport) => {
-					console.log("new transport");
+				router.observer.on("newtransport", async (transport: MediasoupTypes.Transport) => {
+					console.log("new transport created [id:%s]", transport.id);
 
-					this.mediasoupTransports.push(transport);
-				})
+					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);
+					});
 
-				await router.createWebRtcTransport({
-					listenIps: [{ ip: "127.0.0.1" }],
-					enableUdp: true,
-					enableTcp: true,
-					preferUdp: true
+					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);
 				});
 			});
 
diff --git a/webrtc/src/opcodes/Connect.ts b/webrtc/src/opcodes/Connect.ts
index 5db11638..b312d6f2 100644
--- a/webrtc/src/opcodes/Connect.ts
+++ b/webrtc/src/opcodes/Connect.ts
@@ -3,4 +3,8 @@ 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/Identify.ts b/webrtc/src/opcodes/Identify.ts
index 6043a460..6bbed04c 100644
--- a/webrtc/src/opcodes/Identify.ts
+++ b/webrtc/src/opcodes/Identify.ts
@@ -1,9 +1,67 @@
 import { WebSocket } from "@fosscord/gateway";
-import { Payload } from "./index"
+import { Payload } from "./index";
 import { VoiceOPCodes } from "@fosscord/util";
-import { Server } from "../Server"
+import { Server } from "../Server";
+import * as mediasoup from "mediasoup";
+import { RtpCodecCapability } from "mediasoup/node/lib/RtpParameters";
+
+const test = "extmap-allow-mixed\na=ice-ufrag:ilWh\na=ice-pwd:Mx7TDnPKXDnTgYWC+qMaqspQ\na=ice-options:trickle\na=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\na=extmap:2 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\na=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01\na=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid\na=rtpmap:111 opus/48000/2\na=extmap:14 urn:ietf:params:rtp-hdrext:toffset\na=extmap:13 urn:3gpp:video-orientation\na=extmap:5 http://www.webrtc.org/experiments/rtp-hdrext/playout-delay\na=extmap:6 http://www.webrtc.org/experiments/rtp-hdrext/video-content-type\na=extmap:7 http://www.webrtc.org/experiments/rtp-hdrext/video-timing\na=extmap:8 http://www.webrtc.org/experiments/rtp-hdrext/color-space\na=extmap:10 urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id\na=extmap:11 urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id\na=rtpmap:96 VP8/90000\na=rtpmap:97 rtx/90000";
 
 export async function onIdentify(this: Server, socket: WebSocket, data: Payload) {
+	var transport = await this.mediasoupRouters[0].createWebRtcTransport({
+		listenIps: [{ ip: "127.0.0.1" }],
+		enableUdp: true,
+		enableTcp: true,
+		preferUdp: true,
+	});
+
+	const rtpCapabilities = this.mediasoupRouters[0].rtpCapabilities;
+	const codecs = rtpCapabilities.codecs as RtpCodecCapability[];
+
+	var producer = await transport.produce(
+		{
+			kind: "audio",
+			rtpParameters:
+			{
+				mid: "1",
+				codecs: codecs.filter(x => x.kind === "audio").map((x: RtpCodecCapability) => {
+					return {
+						mimeType: x.mimeType,
+						kind: x.kind,
+						clockRate: x.clockRate,
+						channels: x.channels,
+						payloadType: x.preferredPayloadType as number
+					};
+				}),
+				headerExtensions: test.split("\na=").map((x, i) => ({
+					id: i + 1,
+					uri: x,
+				}))
+			}
+		});
+
+	const consumer = await transport.consume(
+		{
+			producerId: producer.id,
+			rtpCapabilities:
+			{
+				codecs: codecs.filter(x => x.kind === "audio").map((x: RtpCodecCapability) => {
+					return {
+						mimeType: x.mimeType,
+						kind: x.kind,
+						clockRate: x.clockRate,
+						channels: x.channels,
+						payloadType: x.preferredPayloadType as number
+					};
+				}),
+				headerExtensions: test.split("\na=").map((x, i) => ({
+					kind: "audio",
+					preferredId: i + 1,
+					uri: x,
+				}))
+			}
+		});
+
 	socket.send(JSON.stringify({
 		op: VoiceOPCodes.READY,
 		d: {
@@ -11,11 +69,11 @@ export async function onIdentify(this: Server, socket: WebSocket, data: Payload)
 			ip: "127.0.0.1",
 
 			//@ts-ignore
-			port: this.mediasoupTransports[0].iceCandidates.port,
+			port: transport.iceCandidates[0].port,
 			modes: [
 				"xsalsa20_poly1305",
-				"xsalsa20_poly1305_suffix",
-				"xsalsa20_poly1305_lite",
+				// "xsalsa20_poly1305_suffix",
+				// "xsalsa20_poly1305_lite",
 			],
 			heartbeat_interval: 1,
 		},
diff --git a/webrtc/src/opcodes/SelectProtocol.ts b/webrtc/src/opcodes/SelectProtocol.ts
index fcc45855..24e8ef5f 100644
--- a/webrtc/src/opcodes/SelectProtocol.ts
+++ b/webrtc/src/opcodes/SelectProtocol.ts
@@ -1,17 +1,79 @@
 import { WebSocket } from "@fosscord/gateway";
 import { Payload } from "./index";
 import { VoiceOPCodes } from "@fosscord/util";
-import { Server } from "../Server"
+import { Server } from "../Server";
+
+/*
+	{
+	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",
+	},
+	}
+*/
 
 export async function onSelectProtocol(this: Server, socket: WebSocket, data: Payload) {
 	socket.send(JSON.stringify({
 		op: VoiceOPCodes.SESSION_DESCRIPTION,
 		d: {
-			video_codec: "H264",
+			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: "aead_aes256_gcm_rtpsize",
+			mode: "xsalsa20_poly1305",
 			media_session_id: this.mediasoupTransports[0].id,
-			audio_codec: "opus",
+			audio_codec: data.d.codecs.find((x: any) => x.type === "audio").name,
 		}
 	}));
 }
\ No newline at end of file
diff --git a/webrtc/src/start.ts b/webrtc/src/start.ts
index 5614982d..299bfce8 100644
--- a/webrtc/src/start.ts
+++ b/webrtc/src/start.ts
@@ -1,4 +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