From c8b738f28d792f459a0cab32d052be0344de1096 Mon Sep 17 00:00:00 2001 From: murdle Date: Sun, 7 Dec 2025 23:50:59 +0200 Subject: [PATCH] active type fix --- src/webrtc/opcodes/Video.ts | 91 +++++++++---------------------------- 1 file changed, 22 insertions(+), 69 deletions(-) diff --git a/src/webrtc/opcodes/Video.ts b/src/webrtc/opcodes/Video.ts index 1309fc4f..8a55d90e 100644 --- a/src/webrtc/opcodes/Video.ts +++ b/src/webrtc/opcodes/Video.ts @@ -16,13 +16,7 @@ along with this program. If not, see . */ import { Stream } from "@spacebar/util"; -import { - mediaServer, - Send, - VoiceOPCodes, - VoicePayload, - WebRtcWebSocket, -} from "@spacebar/webrtc"; +import { mediaServer, Send, VoiceOPCodes, VoicePayload, WebRtcWebSocket } from "@spacebar/webrtc"; import type { WebRtcClient } from "@spacebarchat/spacebar-webrtc-types"; import { validateSchema, VoiceVideoSchema } from "@spacebar/schemas"; @@ -46,7 +40,7 @@ export async function onVideo(this: WebRtcWebSocket, payload: VoicePayload) { } } - const stream = d.streams?.find((element) => element.active); + const stream = d.streams?.find((element) => element.active ?? true); const clientsThatNeedUpdate = new Set>(); const wantsToProduceAudio = d.audio_ssrc !== 0; @@ -60,9 +54,7 @@ export async function onVideo(this: WebRtcWebSocket, payload: VoicePayload) { try { await Promise.race([ new Promise((resolve, reject) => { - this.webRtcClient?.emitter.once("connected", () => - resolve(), - ); + this.webRtcClient?.emitter.once("connected", () => resolve()); }), new Promise((resolve, reject) => { // Reject after 3 seconds if still not connected @@ -93,28 +85,19 @@ export async function onVideo(this: WebRtcWebSocket, payload: VoicePayload) { if (wantsToProduceAudio) { // check if we are already producing audio, if not, publish a new audio track for it if (!this.webRtcClient!.isProducingAudio()) { - console.log( - `[${this.user_id}] publishing new audio track ssrc:${d.audio_ssrc}`, - ); + console.log(`[${this.user_id}] publishing new audio track ssrc:${d.audio_ssrc}`); await this.webRtcClient.publishTrack("audio", { audio_ssrc: d.audio_ssrc, }); } // now check that all clients have subscribed to our audio - for (const client of mediaServer.getClientsForRtcServer( - voiceRoomId, - )) { + for (const client of mediaServer.getClientsForRtcServer(voiceRoomId)) { if (client.user_id === this.user_id) continue; if (!client.isSubscribedToTrack(this.user_id, "audio")) { - console.log( - `[${client.user_id}] subscribing to audio track ssrcs: ${d.audio_ssrc}`, - ); - await client.subscribeToTrack( - this.webRtcClient.user_id, - "audio", - ); + console.log(`[${client.user_id}] subscribing to audio track ssrcs: ${d.audio_ssrc}`); + await client.subscribeToTrack(this.webRtcClient.user_id, "audio"); clientsThatNeedUpdate.add(client); } @@ -122,12 +105,10 @@ export async function onVideo(this: WebRtcWebSocket, payload: VoicePayload) { } // check if client has signaled that it will send video if (wantsToProduceVideo) { - this.webRtcClient!.videoStream = { ...stream, type: "video" }; // client sends "screen" on go live but expects "video" on response + this.webRtcClient!.videoStream = { ...stream, type: "video", active: stream.active ?? true }; // client sends "screen" on go live but expects "video" on response // check if we are already publishing video, if not, publish a new video track for it if (!this.webRtcClient!.isProducingVideo()) { - console.log( - `[${this.user_id}] publishing new video track ssrc:${d.video_ssrc}`, - ); + console.log(`[${this.user_id}] publishing new video track ssrc:${d.video_ssrc}`); await this.webRtcClient.publishTrack("video", { video_ssrc: d.video_ssrc, rtx_ssrc: d.rtx_ssrc, @@ -135,19 +116,12 @@ export async function onVideo(this: WebRtcWebSocket, payload: VoicePayload) { } // now check that all clients have subscribed to our video track - for (const client of mediaServer.getClientsForRtcServer( - voiceRoomId, - )) { + for (const client of mediaServer.getClientsForRtcServer(voiceRoomId)) { if (client.user_id === this.user_id) continue; if (!client.isSubscribedToTrack(this.user_id, "video")) { - console.log( - `[${client.user_id}] subscribing to video track ssrc: ${d.video_ssrc}`, - ); - await client.subscribeToTrack( - this.webRtcClient.user_id, - "video", - ); + console.log(`[${client.user_id}] subscribing to video track ssrc: ${d.video_ssrc}`); + await client.subscribeToTrack(this.webRtcClient.user_id, "video"); clientsThatNeedUpdate.add(client); } @@ -163,9 +137,7 @@ export async function onVideo(this: WebRtcWebSocket, payload: VoicePayload) { d: { user_id: this.user_id, // can never send audio ssrc as 0, it will mess up client state for some reason. send server generated ssrc as backup - audio_ssrc: - ssrcs.audio_ssrc ?? - this.webRtcClient!.getIncomingStreamSSRCs().audio_ssrc, + audio_ssrc: ssrcs.audio_ssrc ?? this.webRtcClient!.getIncomingStreamSSRCs().audio_ssrc, video_ssrc: ssrcs.video_ssrc ?? 0, rtx_ssrc: ssrcs.rtx_ssrc ?? 0, streams: d.streams?.map((x) => ({ @@ -173,6 +145,7 @@ export async function onVideo(this: WebRtcWebSocket, payload: VoicePayload) { ssrc: ssrcs.video_ssrc ?? 0, rtx_ssrc: ssrcs.rtx_ssrc ?? 0, type: "video", + active: x.active ?? true, })), } as VoiceVideoSchema, }); @@ -181,14 +154,10 @@ export async function onVideo(this: WebRtcWebSocket, payload: VoicePayload) { } // check if we are not subscribed to producers in this server, if not, subscribe -export async function subscribeToProducers( - this: WebRtcWebSocket, -): Promise { +export async function subscribeToProducers(this: WebRtcWebSocket): Promise { if (!this.webRtcClient || !this.webRtcClient.webrtcConnected) return; - const clients = mediaServer.getClientsForRtcServer( - this.webRtcClient.voiceRoomId, - ); + const clients = mediaServer.getClientsForRtcServer(this.webRtcClient.voiceRoomId); await Promise.all( Array.from(clients).map(async (client) => { @@ -196,42 +165,26 @@ export async function subscribeToProducers( if (client.user_id === this.user_id) return; // cannot subscribe to self - if ( - client.isProducingAudio() && - !this.webRtcClient!.isSubscribedToTrack(client.user_id, "audio") - ) { - await this.webRtcClient!.subscribeToTrack( - client.user_id, - "audio", - ); + if (client.isProducingAudio() && !this.webRtcClient!.isSubscribedToTrack(client.user_id, "audio")) { + await this.webRtcClient!.subscribeToTrack(client.user_id, "audio"); needsUpdate = true; } - if ( - client.isProducingVideo() && - !this.webRtcClient!.isSubscribedToTrack(client.user_id, "video") - ) { - await this.webRtcClient!.subscribeToTrack( - client.user_id, - "video", - ); + if (client.isProducingVideo() && !this.webRtcClient!.isSubscribedToTrack(client.user_id, "video")) { + await this.webRtcClient!.subscribeToTrack(client.user_id, "video"); needsUpdate = true; } if (!needsUpdate) return; - const ssrcs = this.webRtcClient!.getOutgoingStreamSSRCsForUser( - client.user_id, - ); + const ssrcs = this.webRtcClient!.getOutgoingStreamSSRCsForUser(client.user_id); await Send(this, { op: VoiceOPCodes.VIDEO, d: { user_id: client.user_id, // can never send audio ssrc as 0, it will mess up client state for some reason. send server generated ssrc as backup - audio_ssrc: - ssrcs.audio_ssrc ?? - client.getIncomingStreamSSRCs().audio_ssrc, + audio_ssrc: ssrcs.audio_ssrc ?? client.getIncomingStreamSSRCs().audio_ssrc, video_ssrc: ssrcs.video_ssrc ?? 0, rtx_ssrc: ssrcs.rtx_ssrc ?? 0, streams: [