From 24e151805d789bed5436fd6db72a1680f3d0af1c Mon Sep 17 00:00:00 2001 From: Elysia <71698422+aiko-chan-ai@users.noreply.github.com> Date: Thu, 25 Jul 2024 19:18:47 +0700 Subject: [PATCH] feat: Video receiver (1/n) --- src/client/voice/VoiceConnection.js | 29 ++++++++++++++++++- src/client/voice/dispatcher/BaseDispatcher.js | 1 + src/client/voice/networking/VoiceWebSocket.js | 8 +++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/client/voice/VoiceConnection.js b/src/client/voice/VoiceConnection.js index 33a5dbf..859c2d5 100644 --- a/src/client/voice/VoiceConnection.js +++ b/src/client/voice/VoiceConnection.js @@ -106,7 +106,7 @@ class VoiceConnection extends EventEmitter { /** * Map SSRC values to user IDs - * @type {Map} + * @type {Map} * @private */ this.ssrcMap = new Map(); @@ -513,6 +513,7 @@ class VoiceConnection extends EventEmitter { ws.on('ready', this.onReady.bind(this)); ws.on('sessionDescription', this.onSessionDescription.bind(this)); ws.on('startSpeaking', this.onStartSpeaking.bind(this)); + ws.on('startStreaming', this.onStartStreaming.bind(this)); this.sockets.ws.connect(); } @@ -569,6 +570,32 @@ class VoiceConnection extends EventEmitter { }); } + onStartStreaming({ video_ssrc, user_id, audio_ssrc }) { + this.ssrcMap.set(+audio_ssrc, { + ...(this.ssrcMap.get(+audio_ssrc) || {}), + userId: user_id, + hasVideo: Boolean(video_ssrc), // Maybe ? + }); + /** +{ + video_ssrc: 0, + user_id: 'uid', + streams: [ + { + ssrc: 27734, + rtx_ssrc: 27735, + rid: '100', + quality: 100, + max_resolution: [Object], + max_framerate: 60, + active: false + } + ], + audio_ssrc: 27733 +} + */ + } + /** * Invoked when a speaking event is received. * @param {Object} data The received data diff --git a/src/client/voice/dispatcher/BaseDispatcher.js b/src/client/voice/dispatcher/BaseDispatcher.js index d2f1700..d043176 100644 --- a/src/client/voice/dispatcher/BaseDispatcher.js +++ b/src/client/voice/dispatcher/BaseDispatcher.js @@ -255,6 +255,7 @@ class BaseDispatcher extends Writable { * Discord seems to send this extension on every video packet * @see https://webrtc.googlesource.com/src/+/refs/heads/main/docs/native-code/rtp-hdrext/playout-delay * @returns {Buffer} playout-delay extension + * Buffer * @private */ createHeaderExtension() { diff --git a/src/client/voice/networking/VoiceWebSocket.js b/src/client/voice/networking/VoiceWebSocket.js index af8ae10..6ad95e0 100644 --- a/src/client/voice/networking/VoiceWebSocket.js +++ b/src/client/voice/networking/VoiceWebSocket.js @@ -214,6 +214,14 @@ class VoiceWebSocket extends EventEmitter { */ this.emit('startSpeaking', packet.d); break; + case VoiceOpcodes.SOURCES: + /** + * Emitted whenever a streaming packet is received. + * @param {Object} data + * @event VoiceWebSocket#startStreaming + */ + this.emit('startStreaming', packet.d); + break; default: /** * Emitted when an unhandled packet is received.