Adding more logs

This commit is contained in:
Valere
2025-11-13 16:41:32 +01:00
parent 0115242a2b
commit 03bec6f904
9 changed files with 131 additions and 73 deletions
@@ -22,6 +22,7 @@ import { ClientEvent, SyncState, type Room as MatrixRoom } from "matrix-js-sdk";
import {
BehaviorSubject,
combineLatest,
distinctUntilChanged,
fromEvent,
map,
type Observable,
@@ -29,8 +30,9 @@ import {
scan,
startWith,
switchMap,
tap,
} from "rxjs";
import { logger } from "matrix-js-sdk/lib/logger";
import { type Logger, logger } from "matrix-js-sdk/lib/logger";
import { type Behavior } from "../../Behavior";
import { type IConnectionManager } from "../remoteMembers/ConnectionManager";
@@ -61,6 +63,7 @@ export enum LivekitState {
Disconnected = "disconnected",
Disconnecting = "disconnecting",
}
type LocalMemberLivekitState =
| { state: LivekitState.Error; error: string }
| { state: LivekitState.Connected }
@@ -74,6 +77,7 @@ export enum MatrixState {
Disconnected = "disconnected",
Connecting = "connecting",
}
type LocalMemberMatrixState =
| { state: MatrixState.Connected }
| { state: MatrixState.Connecting }
@@ -106,6 +110,7 @@ interface Props {
localTransport$: Behavior<LivekitTransport | null>;
trackProcessorState$: Behavior<ProcessorState>;
widget: WidgetHelpers | null;
logger: Logger;
}
/**
@@ -132,6 +137,7 @@ export const createLocalMembership$ = ({
matrixRoom,
trackProcessorState$,
widget,
logger,
}: Props): {
// publisher: Publisher
requestConnect: () => LocalMemberConnectionState;
@@ -157,6 +163,8 @@ export const createLocalMembership$ = ({
/** @deprecated use state instead*/
configError$: Behavior<ElementCallError | null>;
} => {
const prefixLogger = logger.getChild("[LocalMembership]");
prefixLogger.debug(`Creating local membership..`);
const state = {
livekit$: new BehaviorSubject<LocalMemberLivekitState>({
state: LivekitState.Uninitialized,
@@ -178,49 +186,73 @@ export const createLocalMembership$ = ({
const tracks$ = new BehaviorSubject<LocalTrack[]>([]);
// Drop Epoch data here since we will not combine this anymore
const connection$ = scope.behavior(
combineLatest(
[connectionManager.connections$, localTransport$],
(connections, transport) => {
if (transport === null) return null;
return (
connections.value.find((connection) =>
areLivekitTransportsEqual(connection.transport, transport),
) ?? null
);
},
),
const localConnection$ = scope.behavior(
combineLatest([connectionManager.connections$, localTransport$])
.pipe(
map(([connections, localTransport]) => {
if (localTransport === null) {
return null;
}
return (
connections.value.find((connection) =>
areLivekitTransportsEqual(connection.transport, localTransport),
) ?? null
);
}),
)
.pipe(
distinctUntilChanged((a, b) => {
const eq = a === b;
logger.debug(
`distinctUntilChanged: Local connection equality check: ${eq}`,
);
return eq;
}),
)
.pipe(
tap((connection) => {
prefixLogger.info(
`Local connection updated: ${connection?.transport?.livekit_service_url}`,
);
}),
),
);
/**
* Whether we are connected to the MatrixRTC session.
*/
const homeserverConnected$ = scope.behavior(
// To consider ourselves connected to MatrixRTC, we check the following:
and$(
// The client is connected to the sync loop
(
fromEvent(matrixRoom.client, ClientEvent.Sync) as Observable<
[SyncState]
>
).pipe(
startWith([matrixRoom.client.getSyncState()]),
map(([state]) => state === SyncState.Syncing),
const homeserverConnected$ = scope
.behavior(
// To consider ourselves connected to MatrixRTC, we check the following:
and$(
// The client is connected to the sync loop
(
fromEvent(matrixRoom.client, ClientEvent.Sync) as Observable<
[SyncState]
>
).pipe(
startWith([matrixRoom.client.getSyncState()]),
map(([state]) => state === SyncState.Syncing),
),
// Room state observed by session says we're connected
fromEvent(matrixRTCSession, MembershipManagerEvent.StatusChanged).pipe(
startWith(null),
map(() => matrixRTCSession.membershipStatus === Status.Connected),
),
// Also watch out for warnings that we've likely hit a timeout and our
// delayed leave event is being sent (this condition is here because it
// provides an earlier warning than the sync loop timeout, and we wouldn't
// see the actual leave event until we reconnect to the sync loop)
fromEvent(matrixRTCSession, MembershipManagerEvent.ProbablyLeft).pipe(
startWith(null),
map(() => matrixRTCSession.probablyLeft !== true),
),
),
// Room state observed by session says we're connected
fromEvent(matrixRTCSession, MembershipManagerEvent.StatusChanged).pipe(
startWith(null),
map(() => matrixRTCSession.membershipStatus === Status.Connected),
),
// Also watch out for warnings that we've likely hit a timeout and our
// delayed leave event is being sent (this condition is here because it
// provides an earlier warning than the sync loop timeout, and we wouldn't
// see the actual leave event until we reconnect to the sync loop)
fromEvent(matrixRTCSession, MembershipManagerEvent.ProbablyLeft).pipe(
startWith(null),
map(() => matrixRTCSession.probablyLeft !== true),
),
),
);
)
.pipe(
tap((connected) => {
prefixLogger.info(`Homeserver connected update: ${connected}`);
}),
);
// /**
// * Whether we are "fully" connected to the call. Accounts for both the
@@ -230,7 +262,7 @@ export const createLocalMembership$ = ({
const connected$ = scope.behavior(
and$(
homeserverConnected$,
connection$.pipe(
localConnection$.pipe(
switchMap((c) =>
c
? c.state$.pipe(map((state) => state.state === "ConnectedToLkRoom"))
@@ -241,8 +273,9 @@ export const createLocalMembership$ = ({
);
const publisher$ = new BehaviorSubject<Publisher | null>(null);
connection$.subscribe((connection) => {
localConnection$.subscribe((connection) => {
if (connection !== null && publisher$.value === null) {
// TODO looks strange to not change publisher if connection changes.
publisher$.next(
new Publisher(
scope,
@@ -366,7 +399,7 @@ export const createLocalMembership$ = ({
// We use matrixConnected$ rather than reconnecting$ because we want to
// pause tracks during the initial joining sequence too until we're sure
// that our own media is displayed on screen.
combineLatest([connection$, homeserverConnected$])
combineLatest([localConnection$, homeserverConnected$])
.pipe(scope.bind())
.subscribe(([connection, connected]) => {
if (connection?.state$.value.state !== "ConnectedToLkRoom") return;
@@ -451,7 +484,7 @@ export const createLocalMembership$ = ({
* Whether the user is currently sharing their screen.
*/
const sharingScreen$ = scope.behavior(
connection$.pipe(
localConnection$.pipe(
switchMap((c) =>
c === null
? of(false)
@@ -472,7 +505,7 @@ export const createLocalMembership$ = ({
// We also allow screen sharing to be toggled even if the connection
// is still initializing or publishing tracks, because there's no
// technical reason to disallow this. LiveKit will publish if it can.
void connection$.value?.livekitRoom.localParticipant
void localConnection$.value?.livekitRoom.localParticipant
.setScreenShareEnabled(!sharingScreen$.value, {
audio: true,
selfBrowserSurface: "include",
@@ -483,7 +516,7 @@ export const createLocalMembership$ = ({
: null;
const participant$ = scope.behavior(
connection$.pipe(map((c) => c?.livekitRoom.localParticipant ?? null)),
localConnection$.pipe(map((c) => c?.livekitRoom.localParticipant ?? null)),
);
return {
startTracks,
@@ -497,7 +530,7 @@ export const createLocalMembership$ = ({
sharingScreen$,
toggleScreenSharing,
participant$,
connection$,
connection$: localConnection$,
};
};