diff --git a/src/webrtc/opcodes/Video.ts b/src/webrtc/opcodes/Video.ts index ca597a33..7bf96a4f 100644 --- a/src/webrtc/opcodes/Video.ts +++ b/src/webrtc/opcodes/Video.ts @@ -26,7 +26,7 @@ import { import type { WebRtcClient } from "spacebar-webrtc-types"; export async function onVideo(this: WebRtcWebSocket, payload: VoicePayload) { - if (!this.webRtcClient || !this.webRtcClient.webrtcConnected) return; + if (!this.webRtcClient) return; const { voiceRoomId } = this.webRtcClient; @@ -47,12 +47,22 @@ export async function onVideo(this: WebRtcWebSocket, payload: VoicePayload) { const stream = d.streams?.find((element) => element.active); - await Send(this, { op: VoiceOPCodes.MEDIA_SINK_WANTS, d: { any: 100 } }); - const clientsThatNeedUpdate = new Set>(); const wantsToProduceAudio = d.audio_ssrc !== 0; const wantsToProduceVideo = d.video_ssrc !== 0 && stream?.active; + // this is to handle a really weird case where the client sends audio info before the + // dtls ice connection is completely connected. TODO: find a better way to handle this + if (!this.webRtcClient.webrtcConnected) { + if (wantsToProduceAudio) { + await new Promise((resolve, reject) => { + this.webRtcClient?.emitter.once("connected", () => resolve()); + }); + } else return; + } + + await Send(this, { op: VoiceOPCodes.MEDIA_SINK_WANTS, d: { any: 100 } }); + // first check if we need stop any tracks if (!wantsToProduceAudio && this.webRtcClient.isProducingAudio()) { this.webRtcClient.stopPublishingTrack("audio");