From 765387b3cc50ddfed29fef28afc46f2a6e3dbe0b Mon Sep 17 00:00:00 2001 From: Timo K Date: Thu, 28 Aug 2025 11:18:38 +0200 Subject: [PATCH 1/2] add logging Signed-off-by: Timo K --- src/rtcSessionHelpers.ts | 2 +- src/state/CallViewModel.ts | 35 ++++++++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/rtcSessionHelpers.ts b/src/rtcSessionHelpers.ts index 07cc49fc..a88502af 100644 --- a/src/rtcSessionHelpers.ts +++ b/src/rtcSessionHelpers.ts @@ -45,7 +45,7 @@ async function makeFocusInternal( // Prioritize the .well-known/matrix/client, if available, over the configured SFU const domain = rtcSession.room.client.getDomain(); if (localStorage.getItem("timo-focus-url")) { - const timoFocusUrl = JSON.parse(localStorage.getItem("timo-focus-url")!); + const timoFocusUrl = localStorage.getItem("timo-focus-url")!; const focusFromUrl: LivekitFocus = { type: "livekit", livekit_service_url: timoFocusUrl, diff --git a/src/state/CallViewModel.ts b/src/state/CallViewModel.ts index a39108ac..bc4c23db 100644 --- a/src/state/CallViewModel.ts +++ b/src/state/CallViewModel.ts @@ -436,8 +436,8 @@ function getRoomMemberFromRtcMember( return { id, member }; } +// TODO-MULTI-SFU Add all device syncing logic from useLivekit class Connection { - // TODO-MULTI-SFU Add all device syncing logic from useLivekit private readonly sfuConfig = getSFUConfigWithOpenID( this.client, this.serviceUrl, @@ -498,13 +498,34 @@ class Connection { ) .filter((f) => f.livekit_service_url === this.serviceUrl) .map((f) => f.membership); - return publishingMembers - .map((m) => - participants.find( - (p) => p.identity === `${m.sender}:${m.deviceId}`, - ), - ) + + const publishingP = publishingMembers + .map((m) => { + logger.log( + "Publishing participants: all participants at: ", + this.livekitAlias, + this.serviceUrl, + participants, + ); + return participants.find((p) => { + logger.log( + "Publishing participants: compare", + p.identity, + "===", + `${m.sender}:${m.deviceId}`, + ); + return p.identity === `${m.sender}:${m.deviceId}`; + }); + }) .filter((p): p is RemoteParticipant => !!p); + logger.log( + "Publishing participants: find participants for url ", + this.serviceUrl, + publishingMembers, + "Publishing participants: ", + publishingP, + ); + return publishingP; }), ), [], From b996f6346600685e22c834686bf09be012233161 Mon Sep 17 00:00:00 2001 From: Timo K Date: Thu, 28 Aug 2025 13:37:17 +0200 Subject: [PATCH 2/2] make it work Signed-off-by: Timo K --- src/state/CallViewModel.ts | 72 ++++++++++++++------------------------ 1 file changed, 27 insertions(+), 45 deletions(-) diff --git a/src/state/CallViewModel.ts b/src/state/CallViewModel.ts index bc4c23db..c75c84c9 100644 --- a/src/state/CallViewModel.ts +++ b/src/state/CallViewModel.ts @@ -448,11 +448,6 @@ class Connection { this.stopped = false; const { url, jwt } = await this.sfuConfig; if (!this.stopped) await this.livekitRoom.connect(url, jwt); - // TODO-MULTI-SFU in this livekit room we really do not want to publish any tracks. - // this is only for testing purposes - const tracks = await this.livekitRoom.localParticipant.createTracks({ - audio: { deviceId: "default" }, - }); await this.livekitRoom.localParticipant.publishTrack(tracks[0]); } @@ -460,9 +455,10 @@ class Connection { this.stopped = false; const { url, jwt } = await this.sfuConfig; if (!this.stopped) await this.livekitRoom.connect(url, jwt); + if (!this.stopped) { const tracks = await this.livekitRoom.localParticipant.createTracks({ - audio: { deviceId: "default" }, + audio: true, video: true, }); for (const track of tracks) { @@ -501,30 +497,11 @@ class Connection { const publishingP = publishingMembers .map((m) => { - logger.log( - "Publishing participants: all participants at: ", - this.livekitAlias, - this.serviceUrl, - participants, - ); return participants.find((p) => { - logger.log( - "Publishing participants: compare", - p.identity, - "===", - `${m.sender}:${m.deviceId}`, - ); return p.identity === `${m.sender}:${m.deviceId}`; }); }) .filter((p): p is RemoteParticipant => !!p); - logger.log( - "Publishing participants: find participants for url ", - this.serviceUrl, - publishingMembers, - "Publishing participants: ", - publishingP, - ); return publishingP; }), ), @@ -589,21 +566,22 @@ export class CallViewModel extends ViewModel { ), ); - private readonly remoteConnections$ = combineLatest([ - this.localFocus, - this.foci$, - ]).pipe( - accumulate(new Map(), (prev, [localFocus, foci]) => { - const stopped = new Map(prev); - const next = new Map(); + private readonly remoteConnections$ = this.scope.behavior( + combineLatest([this.localFocus, this.foci$]).pipe( + accumulate(new Map(), (prev, [localFocus, foci]) => { + const stopped = new Map(prev); + const next = new Map(); + for (const focus of foci) { + if (focus !== localFocus.livekit_service_url) { + stopped.delete(focus); - for (const focus of foci) { - if (focus !== localFocus.livekit_service_url) { - stopped.delete(focus); - next.set( - focus, - prev.get(focus) ?? - new Connection( + let nextConnection = prev.get(focus); + if (!nextConnection) { + logger.log( + "SFU remoteConnections$ construct new connection: ", + focus, + ); + nextConnection = new Connection( new LivekitRoom({ ...defaultLiveKitOptions, e2ee: this.e2eeOptions, @@ -613,14 +591,18 @@ export class CallViewModel extends ViewModel { this.matrixRTCSession.room.client, this.scope, this.matrixRTCSession, - ), - ); + ); + } else { + logger.log("SFU remoteConnections$ use prev connection: ", focus); + } + next.set(focus, nextConnection); + } } - } - for (const connection of stopped.values()) connection.stop(); - return next; - }), + for (const connection of stopped.values()) connection.stop(); + return next; + }), + ), ); private readonly joined$ = new Subject();