diff options
Diffstat (limited to 'webrtc/src')
-rw-r--r-- | webrtc/src/Server.ts | 31 | ||||
-rw-r--r-- | webrtc/src/opcodes/Connect.ts | 4 | ||||
-rw-r--r-- | webrtc/src/opcodes/Identify.ts | 68 | ||||
-rw-r--r-- | webrtc/src/opcodes/SelectProtocol.ts | 70 | ||||
-rw-r--r-- | webrtc/src/start.ts | 6 |
5 files changed, 160 insertions, 19 deletions
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 |