2025-08-28 13:52:12 +02:00
|
|
|
/*
|
|
|
|
|
Copyright 2025 New Vector Ltd.
|
|
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
|
Please see LICENSE in the repository root for full details.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-09-30 11:33:45 +02:00
|
|
|
import { connectedParticipantsObserver, connectionStateObserver } from "@livekit/components-core";
|
|
|
|
|
import { type ConnectionState, type E2EEOptions, Room as LivekitRoom } from "livekit-client";
|
|
|
|
|
import { type CallMembership, type LivekitFocus } from "matrix-js-sdk/lib/matrixrtc";
|
|
|
|
|
import { combineLatest } from "rxjs";
|
2025-08-28 13:52:12 +02:00
|
|
|
|
2025-09-30 17:02:48 +02:00
|
|
|
import { getSFUConfigWithOpenID, type OpenIDClientParts, type SFUConfig } from "../livekit/openIDSFU";
|
2025-08-29 18:46:24 +02:00
|
|
|
import { type Behavior } from "./Behavior";
|
2025-08-28 13:52:12 +02:00
|
|
|
import { type ObservableScope } from "./ObservableScope";
|
2025-08-28 17:45:14 +02:00
|
|
|
import { defaultLiveKitOptions } from "../livekit/options";
|
2025-08-28 13:52:12 +02:00
|
|
|
|
2025-09-30 17:02:48 +02:00
|
|
|
export interface ConnectionOpts {
|
|
|
|
|
/** The focus server to connect to. */
|
|
|
|
|
focus: LivekitFocus;
|
|
|
|
|
/** The Matrix client to use for OpenID and SFU config requests. */
|
|
|
|
|
client: OpenIDClientParts;
|
|
|
|
|
/** The observable scope to use for this connection. */
|
|
|
|
|
scope: ObservableScope;
|
|
|
|
|
/** An observable of the current RTC call memberships and their associated focus. */
|
|
|
|
|
membershipsFocusMap$: Behavior<{ membership: CallMembership; focus: LivekitFocus }[]>;
|
|
|
|
|
}
|
2025-09-30 11:33:45 +02:00
|
|
|
/**
|
|
|
|
|
* A connection to a Matrix RTC LiveKit backend.
|
|
|
|
|
*
|
|
|
|
|
* Expose observables for participants and connection state.
|
|
|
|
|
*/
|
2025-08-28 13:52:12 +02:00
|
|
|
export class Connection {
|
2025-09-30 11:33:45 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the connection has been stopped.
|
|
|
|
|
* @see Connection.stop
|
|
|
|
|
* */
|
2025-08-28 17:45:14 +02:00
|
|
|
protected stopped = false;
|
2025-08-28 13:52:12 +02:00
|
|
|
|
2025-09-30 11:33:45 +02:00
|
|
|
/**
|
|
|
|
|
* Starts the connection.
|
|
|
|
|
*
|
|
|
|
|
* This will:
|
|
|
|
|
* 1. Request an OpenId token `request_token` (allows matrix users to verify their identity with a third-party service.)
|
|
|
|
|
* 2. Use this token to request the SFU config to the MatrixRtc authentication service.
|
|
|
|
|
* 3. Connect to the configured LiveKit room.
|
|
|
|
|
*/
|
2025-08-28 13:52:12 +02:00
|
|
|
public async start(): Promise<void> {
|
|
|
|
|
this.stopped = false;
|
2025-09-30 17:02:48 +02:00
|
|
|
// TODO could this be loaded earlier to save time?
|
|
|
|
|
const { url, jwt } = await this.getSFUConfigWithOpenID();
|
|
|
|
|
|
2025-08-28 13:52:12 +02:00
|
|
|
if (!this.stopped) await this.livekitRoom.connect(url, jwt);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 17:02:48 +02:00
|
|
|
|
|
|
|
|
protected async getSFUConfigWithOpenID(): Promise<SFUConfig> {
|
|
|
|
|
return await getSFUConfigWithOpenID(
|
|
|
|
|
this.client,
|
|
|
|
|
this.targetFocus.livekit_service_url,
|
|
|
|
|
this.targetFocus.livekit_alias
|
|
|
|
|
)
|
|
|
|
|
}
|
2025-09-30 11:33:45 +02:00
|
|
|
/**
|
|
|
|
|
* Stops the connection.
|
|
|
|
|
*
|
|
|
|
|
* This will disconnect from the LiveKit room.
|
|
|
|
|
* If the connection is already stopped, this is a no-op.
|
|
|
|
|
*/
|
2025-08-28 13:52:12 +02:00
|
|
|
public stop(): void {
|
2025-09-26 13:20:55 -04:00
|
|
|
if (this.stopped) return;
|
2025-08-28 13:52:12 +02:00
|
|
|
void this.livekitRoom.disconnect();
|
|
|
|
|
this.stopped = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 11:33:45 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* An observable of the participants that are publishing on this connection.
|
|
|
|
|
* This is derived from `participantsIncludingSubscribers$` and `membershipsFocusMap$`.
|
|
|
|
|
* It filters the participants to only those that are associated with a membership that claims to publish on this connection.
|
|
|
|
|
*/
|
2025-08-28 17:45:14 +02:00
|
|
|
public readonly publishingParticipants$;
|
2025-09-30 11:33:45 +02:00
|
|
|
|
|
|
|
|
/**
|
2025-09-30 17:02:48 +02:00
|
|
|
* The focus server to connect to.
|
2025-09-30 11:33:45 +02:00
|
|
|
*/
|
2025-09-30 17:02:48 +02:00
|
|
|
protected readonly targetFocus: LivekitFocus;
|
2025-08-28 17:45:14 +02:00
|
|
|
|
2025-09-30 11:33:45 +02:00
|
|
|
/**
|
|
|
|
|
* An observable of the livekit connection state.
|
|
|
|
|
* Converts the livekit room events StateChange to an observable.
|
|
|
|
|
*/
|
2025-08-28 17:45:14 +02:00
|
|
|
public connectionState$: Behavior<ConnectionState>;
|
2025-09-30 11:33:45 +02:00
|
|
|
|
2025-09-30 17:02:48 +02:00
|
|
|
|
|
|
|
|
private readonly client: OpenIDClientParts;
|
2025-09-30 11:33:45 +02:00
|
|
|
/**
|
|
|
|
|
* Creates a new connection to a matrix RTC LiveKit backend.
|
|
|
|
|
*
|
2025-09-30 17:02:48 +02:00
|
|
|
* @param livekitRoom - LiveKit room instance to use.
|
|
|
|
|
* @param opts - Connection options {@link ConnectionOpts}.
|
|
|
|
|
*
|
2025-09-30 11:33:45 +02:00
|
|
|
*/
|
2025-09-30 17:02:48 +02:00
|
|
|
protected constructor(
|
|
|
|
|
public readonly livekitRoom: LivekitRoom,
|
|
|
|
|
opts: ConnectionOpts,
|
2025-08-28 17:45:14 +02:00
|
|
|
) {
|
2025-09-30 17:02:48 +02:00
|
|
|
const { focus, client, scope, membershipsFocusMap$ } =
|
|
|
|
|
opts;
|
|
|
|
|
|
|
|
|
|
this.livekitRoom = livekitRoom
|
|
|
|
|
this.targetFocus = focus;
|
|
|
|
|
this.client = client;
|
|
|
|
|
|
|
|
|
|
const participantsIncludingSubscribers$ = scope.behavior(
|
2025-08-28 17:45:14 +02:00
|
|
|
connectedParticipantsObserver(this.livekitRoom),
|
2025-09-30 11:33:45 +02:00
|
|
|
[]
|
2025-08-28 17:45:14 +02:00
|
|
|
);
|
|
|
|
|
|
2025-09-30 17:02:48 +02:00
|
|
|
this.publishingParticipants$ = scope.behavior(
|
2025-09-25 21:29:02 -04:00
|
|
|
combineLatest(
|
2025-09-30 17:02:48 +02:00
|
|
|
[participantsIncludingSubscribers$, membershipsFocusMap$],
|
2025-09-25 21:29:02 -04:00
|
|
|
(participants, membershipsFocusMap) =>
|
2025-08-28 15:32:46 +02:00
|
|
|
membershipsFocusMap
|
|
|
|
|
// Find all members that claim to publish on this connection
|
|
|
|
|
.flatMap(({ membership, focus }) =>
|
2025-09-30 17:02:48 +02:00
|
|
|
focus.livekit_service_url === this.targetFocus.livekit_service_url
|
2025-08-28 15:32:46 +02:00
|
|
|
? [membership]
|
2025-09-30 11:33:45 +02:00
|
|
|
: []
|
2025-08-28 15:32:46 +02:00
|
|
|
)
|
|
|
|
|
// Find all associated publishing livekit participant objects
|
2025-08-28 17:45:14 +02:00
|
|
|
.flatMap((membership) => {
|
2025-08-28 15:32:46 +02:00
|
|
|
const participant = participants.find(
|
2025-08-28 17:45:14 +02:00
|
|
|
(p) =>
|
2025-09-30 11:33:45 +02:00
|
|
|
p.identity === `${membership.sender}:${membership.deviceId}`
|
2025-08-28 15:32:46 +02:00
|
|
|
);
|
2025-08-28 17:45:14 +02:00
|
|
|
return participant ? [{ participant, membership }] : [];
|
2025-09-30 11:33:45 +02:00
|
|
|
})
|
2025-08-28 13:52:12 +02:00
|
|
|
),
|
2025-09-30 11:33:45 +02:00
|
|
|
[]
|
2025-08-28 13:52:12 +02:00
|
|
|
);
|
2025-09-30 17:02:48 +02:00
|
|
|
this.connectionState$ = scope.behavior<ConnectionState>(
|
2025-09-30 11:33:45 +02:00
|
|
|
connectionStateObserver(this.livekitRoom)
|
2025-08-28 17:45:14 +02:00
|
|
|
);
|
2025-09-26 13:20:55 -04:00
|
|
|
|
2025-09-30 17:02:48 +02:00
|
|
|
scope.onEnd(() => this.stop());
|
2025-08-28 17:45:14 +02:00
|
|
|
}
|
2025-08-28 13:52:12 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-30 17:02:48 +02:00
|
|
|
/**
|
|
|
|
|
* A remote connection to the Matrix RTC LiveKit backend.
|
|
|
|
|
*
|
|
|
|
|
* This connection is used for subscribing to remote participants.
|
|
|
|
|
* It does not publish any local tracks.
|
|
|
|
|
*/
|
|
|
|
|
export class RemoteConnection extends Connection {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new remote connection to a matrix RTC LiveKit backend.
|
|
|
|
|
* @param opts
|
|
|
|
|
* @param sharedE2eeOption - The shared E2EE options to use for the connection.
|
|
|
|
|
*/
|
|
|
|
|
public constructor(opts: ConnectionOpts, sharedE2eeOption: E2EEOptions | undefined) {
|
|
|
|
|
const livekitRoom = new LivekitRoom({
|
|
|
|
|
...defaultLiveKitOptions,
|
|
|
|
|
e2ee: sharedE2eeOption
|
|
|
|
|
});
|
|
|
|
|
super(livekitRoom, opts);
|
|
|
|
|
}
|
|
|
|
|
}
|