From 6c797754cdd84274b7c06a45c4f137044f597e2c Mon Sep 17 00:00:00 2001 From: Oliver Lazoroski Date: Thu, 31 Oct 2024 16:09:17 +0100 Subject: [PATCH] feat: prevent flickering when a new stream arrives for an existing participant --- packages/client/src/rtc/Subscriber.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/client/src/rtc/Subscriber.ts b/packages/client/src/rtc/Subscriber.ts index ea68a0ea8..0f43e40e3 100644 --- a/packages/client/src/rtc/Subscriber.ts +++ b/packages/client/src/rtc/Subscriber.ts @@ -218,7 +218,15 @@ export class Subscriber { this.logger('error', `Unknown track type: ${rawTrackType}`); return; } + const previousStream = participantToUpdate[streamKindProp]; + + // replace the previous stream with the new one, prevents flickering + this.state.updateParticipant(participantToUpdate.sessionId, { + [streamKindProp]: primaryStream, + }); + + // now, dispose the previous stream if (previousStream) { this.logger( 'info', @@ -229,9 +237,6 @@ export class Subscriber { previousStream.removeTrack(t); }); } - this.state.updateParticipant(participantToUpdate.sessionId, { - [streamKindProp]: primaryStream, - }); }; private onIceCandidate = (e: RTCPeerConnectionIceEvent) => {