2025-11-04 20:24:15 +01:00
|
|
|
/*
|
2025-11-18 10:13:10 +01:00
|
|
|
Copyright 2025 Element Creations Ltd.
|
2025-11-04 20:24:15 +01:00
|
|
|
|
|
|
|
|
SPDX-License-IdFentifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
|
Please see LICENSE in the repository root for full details.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
type CallMembership,
|
|
|
|
|
isLivekitTransport,
|
|
|
|
|
type LivekitTransport,
|
|
|
|
|
isLivekitTransportConfig,
|
2025-12-29 17:45:41 +00:00
|
|
|
type Transport,
|
2025-11-04 20:24:15 +01:00
|
|
|
} from "matrix-js-sdk/lib/matrixrtc";
|
2025-12-29 17:45:41 +00:00
|
|
|
import { MatrixError, type MatrixClient } from "matrix-js-sdk";
|
2025-11-21 14:02:15 +01:00
|
|
|
import {
|
|
|
|
|
combineLatest,
|
|
|
|
|
distinctUntilChanged,
|
|
|
|
|
first,
|
|
|
|
|
from,
|
|
|
|
|
map,
|
|
|
|
|
switchMap,
|
|
|
|
|
} from "rxjs";
|
|
|
|
|
import { logger as rootLogger } from "matrix-js-sdk/lib/logger";
|
2025-11-04 20:24:15 +01:00
|
|
|
import { AutoDiscovery } from "matrix-js-sdk/lib/autodiscovery";
|
2025-12-17 09:53:49 +01:00
|
|
|
import { type CallMembershipIdentityParts } from "matrix-js-sdk/lib/matrixrtc/EncryptionManager";
|
2025-11-04 20:24:15 +01:00
|
|
|
|
2025-11-07 08:44:44 +01:00
|
|
|
import { type Behavior } from "../../Behavior.ts";
|
2025-11-10 15:55:01 +01:00
|
|
|
import { type Epoch, type ObservableScope } from "../../ObservableScope.ts";
|
2025-11-07 08:44:44 +01:00
|
|
|
import { Config } from "../../../config/Config.ts";
|
2025-12-29 17:45:41 +00:00
|
|
|
import {
|
|
|
|
|
FailToGetOpenIdToken,
|
|
|
|
|
MatrixRTCTransportMissingError,
|
|
|
|
|
} from "../../../utils/errors.ts";
|
2025-11-20 14:42:12 +01:00
|
|
|
import {
|
|
|
|
|
getSFUConfigWithOpenID,
|
|
|
|
|
type OpenIDClientParts,
|
|
|
|
|
} from "../../../livekit/openIDSFU.ts";
|
2025-11-07 19:07:45 +01:00
|
|
|
import { areLivekitTransportsEqual } from "../remoteMembers/MatrixLivekitMembers.ts";
|
2025-11-21 14:02:15 +01:00
|
|
|
import { customLivekitUrl } from "../../../settings/settings.ts";
|
|
|
|
|
|
|
|
|
|
const logger = rootLogger.getChild("[LocalTransport]");
|
2025-11-04 20:24:15 +01:00
|
|
|
|
|
|
|
|
/*
|
2025-11-20 14:42:12 +01:00
|
|
|
* It figures out “which LiveKit focus URL/alias the local user should use,”
|
|
|
|
|
* optionally aligning with the oldest member, and ensures the SFU path is primed
|
|
|
|
|
* before advertising that choice.
|
2025-11-04 20:24:15 +01:00
|
|
|
*/
|
|
|
|
|
interface Props {
|
|
|
|
|
scope: ObservableScope;
|
2025-12-17 09:53:49 +01:00
|
|
|
ownMembershipIdentity: CallMembershipIdentityParts;
|
2025-11-06 21:54:34 +01:00
|
|
|
memberships$: Behavior<Epoch<CallMembership[]>>;
|
2026-01-05 21:08:21 +01:00
|
|
|
client: Pick<
|
|
|
|
|
MatrixClient,
|
|
|
|
|
"getDomain" | "baseUrl" | "_unstable_getRTCTransports"
|
|
|
|
|
> &
|
2025-12-29 17:45:41 +00:00
|
|
|
OpenIDClientParts;
|
2025-11-04 20:24:15 +01:00
|
|
|
roomId: string;
|
|
|
|
|
useOldestMember$: Behavior<boolean>;
|
2025-12-29 17:38:54 +01:00
|
|
|
useOldJwtEndpoint$: Behavior<boolean>;
|
2025-12-17 09:53:49 +01:00
|
|
|
delayId$: Behavior<string | null>;
|
2025-11-04 20:24:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This class is responsible for managing the local transport.
|
|
|
|
|
* "Which transport is the local member going to use"
|
|
|
|
|
*
|
|
|
|
|
* @prop useOldestMember Whether to use the same transport as the oldest member.
|
|
|
|
|
* This will only update once the first oldest member appears. Will not recompute if the oldest member leaves.
|
2025-11-20 14:42:12 +01:00
|
|
|
*
|
2026-01-07 15:47:44 +01:00
|
|
|
* @prop useOldJwtEndpoint$ Whether to set forceOldJwtEndpoint on the returned transport and to use the old JWT endpoint.
|
2025-12-29 17:38:54 +01:00
|
|
|
* This is used when the connection manager needs to know if it has to use the legacy endpoint which implies a string concatenated rtcBackendIdentity.
|
|
|
|
|
* (which is expected for non sticky event based rtc member events)
|
2026-01-07 15:47:44 +01:00
|
|
|
* @returns The local transport. It will be created using the correct sfu endpoint based on the useOldJwtEndpoint$ value.
|
2025-11-20 14:42:12 +01:00
|
|
|
* @throws MatrixRTCTransportMissingError | FailToGetOpenIdToken
|
2025-11-04 20:24:15 +01:00
|
|
|
*/
|
2025-11-05 18:57:24 +01:00
|
|
|
export const createLocalTransport$ = ({
|
2025-11-04 20:24:15 +01:00
|
|
|
scope,
|
|
|
|
|
memberships$,
|
2025-12-17 09:53:49 +01:00
|
|
|
ownMembershipIdentity,
|
2025-11-04 20:24:15 +01:00
|
|
|
client,
|
|
|
|
|
roomId,
|
|
|
|
|
useOldestMember$,
|
2025-12-29 17:38:54 +01:00
|
|
|
useOldJwtEndpoint$,
|
2025-12-17 09:53:49 +01:00
|
|
|
delayId$,
|
2026-01-07 15:47:44 +01:00
|
|
|
}: Props): Behavior<LivekitTransport | null> => {
|
2025-11-04 20:24:15 +01:00
|
|
|
/**
|
|
|
|
|
* The transport over which we should be actively publishing our media.
|
|
|
|
|
* undefined when not joined.
|
|
|
|
|
*/
|
|
|
|
|
const oldestMemberTransport$ = scope.behavior(
|
2025-12-29 17:38:54 +01:00
|
|
|
combineLatest([memberships$, useOldJwtEndpoint$]).pipe(
|
|
|
|
|
map(([memberships, forceOldJwtEndpoint]) => {
|
2025-12-17 09:53:49 +01:00
|
|
|
const oldestMember = memberships.value[0];
|
2025-12-29 17:38:54 +01:00
|
|
|
const transport = oldestMember?.getTransport(memberships.value[0]);
|
|
|
|
|
if (!transport) return null;
|
2026-01-07 15:47:44 +01:00
|
|
|
return transport;
|
2025-12-17 09:53:49 +01:00
|
|
|
}),
|
2025-11-07 12:32:29 +01:00
|
|
|
first((t) => t != null && isLivekitTransport(t)),
|
2025-11-04 20:24:15 +01:00
|
|
|
),
|
2025-11-07 12:32:29 +01:00
|
|
|
null,
|
2025-11-04 20:24:15 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The transport that we would personally prefer to publish on (if not for the
|
|
|
|
|
* transport preferences of others, perhaps).
|
2025-11-20 14:42:12 +01:00
|
|
|
*
|
2025-12-10 21:14:13 +01:00
|
|
|
* @throws MatrixRTCTransportMissingError | FailToGetOpenIdToken
|
2025-11-04 20:24:15 +01:00
|
|
|
*/
|
2026-01-07 13:26:37 +01:00
|
|
|
const preferredTransport$ = scope.behavior(
|
2025-12-29 17:38:54 +01:00
|
|
|
combineLatest([customLivekitUrl.value$, delayId$, useOldJwtEndpoint$]).pipe(
|
|
|
|
|
switchMap(([customUrl, delayId, forceOldJwtEndpoint]) =>
|
|
|
|
|
from(
|
|
|
|
|
makeTransport(
|
|
|
|
|
client,
|
|
|
|
|
ownMembershipIdentity,
|
|
|
|
|
roomId,
|
|
|
|
|
customUrl,
|
|
|
|
|
forceOldJwtEndpoint,
|
|
|
|
|
delayId ?? undefined,
|
2025-12-17 09:53:49 +01:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
2025-12-29 17:38:54 +01:00
|
|
|
),
|
|
|
|
|
null,
|
|
|
|
|
);
|
2025-11-04 20:24:15 +01:00
|
|
|
|
|
|
|
|
/**
|
2025-11-21 14:02:15 +01:00
|
|
|
* The chosen transport we should advertise in our MatrixRTC membership.
|
2025-11-04 20:24:15 +01:00
|
|
|
*/
|
2025-11-21 14:02:15 +01:00
|
|
|
return scope.behavior(
|
2025-11-07 19:07:45 +01:00
|
|
|
combineLatest([
|
|
|
|
|
useOldestMember$,
|
|
|
|
|
oldestMemberTransport$,
|
|
|
|
|
preferredTransport$,
|
|
|
|
|
]).pipe(
|
|
|
|
|
map(([useOldestMember, oldestMemberTransport, preferredTransport]) =>
|
2025-11-07 17:13:49 +01:00
|
|
|
useOldestMember
|
|
|
|
|
? (oldestMemberTransport ?? preferredTransport)
|
|
|
|
|
: preferredTransport,
|
2025-11-07 19:07:45 +01:00
|
|
|
),
|
2025-12-29 17:38:54 +01:00
|
|
|
distinctUntilChanged((t1, t2) => areLivekitTransportsEqual(t1, t2)),
|
2025-11-07 19:07:45 +01:00
|
|
|
),
|
2025-11-04 20:24:15 +01:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const FOCI_WK_KEY = "org.matrix.msc4143.rtc_foci";
|
|
|
|
|
|
2025-11-20 14:42:12 +01:00
|
|
|
/**
|
2025-12-29 17:45:41 +00:00
|
|
|
* Determine the correct Transport for the current session, including
|
|
|
|
|
* validating auth against the service to ensure it's correct.
|
|
|
|
|
* Prefers in order:
|
|
|
|
|
*
|
2026-01-05 21:08:21 +01:00
|
|
|
|
2025-12-29 17:45:41 +00:00
|
|
|
* 1. The `urlFromDevSettings` value. If this cannot be validated, the function will throw.
|
|
|
|
|
* 2. The transports returned via the homeserver.
|
|
|
|
|
* 3. The transports returned via .well-known.
|
|
|
|
|
* 4. The transport configured in Element Call's config.
|
2025-11-20 14:42:12 +01:00
|
|
|
*
|
2025-12-29 17:45:41 +00:00
|
|
|
* @param client The authenticated Matrix client for the current user
|
2026-01-05 22:20:19 +01:00
|
|
|
* @param membership The membership identity of the user.
|
2025-12-29 17:45:41 +00:00
|
|
|
* @param roomId The ID of the room to be connected to.
|
|
|
|
|
* @param urlFromDevSettings Override URL provided by the user's local config.
|
2026-01-05 22:20:19 +01:00
|
|
|
* @param forceOldJwtEndpoint Whether to force the old JWT endpoint (not hashing the backendIdentity).
|
|
|
|
|
* @param delayId the delay id passed to the jwt service.
|
|
|
|
|
*
|
2025-12-29 17:45:41 +00:00
|
|
|
* @returns A fully validated transport config.
|
2025-11-20 14:42:12 +01:00
|
|
|
* @throws MatrixRTCTransportMissingError | FailToGetOpenIdToken
|
|
|
|
|
*/
|
|
|
|
|
async function makeTransport(
|
2026-01-05 21:08:21 +01:00
|
|
|
client: Pick<
|
|
|
|
|
MatrixClient,
|
|
|
|
|
"getDomain" | "baseUrl" | "_unstable_getRTCTransports"
|
|
|
|
|
> &
|
2025-12-29 17:45:41 +00:00
|
|
|
OpenIDClientParts,
|
2025-12-17 09:53:49 +01:00
|
|
|
membership: CallMembershipIdentityParts,
|
2025-11-04 20:24:15 +01:00
|
|
|
roomId: string,
|
2025-11-21 14:02:15 +01:00
|
|
|
urlFromDevSettings: string | null,
|
2025-12-29 17:38:54 +01:00
|
|
|
forceOldJwtEndpoint: boolean,
|
2025-12-17 09:53:49 +01:00
|
|
|
delayId?: string,
|
2026-01-07 15:47:44 +01:00
|
|
|
): Promise<LivekitTransport> {
|
2025-11-21 14:02:15 +01:00
|
|
|
logger.trace("Searching for a preferred transport");
|
2025-12-29 17:45:41 +00:00
|
|
|
|
2026-01-07 15:47:44 +01:00
|
|
|
async function doOpenIdAndJWTFromUrl(url: string): Promise<LivekitTransport> {
|
2026-01-05 21:08:33 +01:00
|
|
|
const { livekitAlias } = await getSFUConfigWithOpenID(
|
2025-12-29 17:45:41 +00:00
|
|
|
client,
|
2026-01-05 21:08:33 +01:00
|
|
|
membership,
|
2026-01-05 21:17:37 +01:00
|
|
|
url,
|
2026-01-05 21:08:33 +01:00
|
|
|
forceOldJwtEndpoint,
|
2025-12-29 17:45:41 +00:00
|
|
|
roomId,
|
2026-01-05 21:08:33 +01:00
|
|
|
client.baseUrl,
|
|
|
|
|
delayId,
|
|
|
|
|
logger,
|
2025-12-29 17:45:41 +00:00
|
|
|
);
|
|
|
|
|
return {
|
2025-11-04 20:24:15 +01:00
|
|
|
type: "livekit",
|
2026-01-05 21:17:37 +01:00
|
|
|
livekit_service_url: url,
|
2026-01-05 21:08:33 +01:00
|
|
|
livekit_alias: livekitAlias,
|
2025-11-04 20:24:15 +01:00
|
|
|
};
|
|
|
|
|
}
|
2026-01-05 21:17:37 +01:00
|
|
|
// We will call `getSFUConfigWithOpenID` once per transport here as it's our
|
|
|
|
|
// only mechanism of valiation. This means we will also ask the
|
|
|
|
|
// homeserver for a OpenID token a few times. Since OpenID tokens are single
|
|
|
|
|
// use we don't want to risk any issues by re-using a token.
|
|
|
|
|
//
|
|
|
|
|
// If the OpenID request were to fail then it's acceptable for us to fail
|
|
|
|
|
// this function early, as we assume the homeserver has got some problems.
|
|
|
|
|
|
|
|
|
|
// DEVTOOL: Highest priority: Load from devtool setting
|
|
|
|
|
if (urlFromDevSettings !== null) {
|
|
|
|
|
// Validate that the SFU is up. Otherwise, we want to fail on this
|
|
|
|
|
// as we don't permit other SFUs.
|
|
|
|
|
// This will call the jwt/sfu/get endpoint to pre create the livekit room.
|
|
|
|
|
logger.info("Using LiveKit transport from dev tools: ", urlFromDevSettings);
|
|
|
|
|
return await doOpenIdAndJWTFromUrl(urlFromDevSettings);
|
|
|
|
|
}
|
2025-11-04 20:24:15 +01:00
|
|
|
|
2025-12-29 17:45:41 +00:00
|
|
|
async function getFirstUsableTransport(
|
|
|
|
|
transports: Transport[],
|
2026-01-07 15:47:44 +01:00
|
|
|
): Promise<LivekitTransport | null> {
|
2025-12-29 17:45:41 +00:00
|
|
|
for (const potentialTransport of transports) {
|
|
|
|
|
if (isLivekitTransportConfig(potentialTransport)) {
|
|
|
|
|
try {
|
2026-01-05 21:08:33 +01:00
|
|
|
// This will call the jwt/sfu/get endpoint to pre create the livekit room.
|
2026-01-05 21:17:37 +01:00
|
|
|
return await doOpenIdAndJWTFromUrl(
|
2025-12-29 17:45:41 +00:00
|
|
|
potentialTransport.livekit_service_url,
|
|
|
|
|
);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
if (ex instanceof FailToGetOpenIdToken) {
|
|
|
|
|
// Explictly throw these
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
logger.debug(
|
|
|
|
|
`Could not use SFU service "${potentialTransport.livekit_service_url}" as SFU`,
|
|
|
|
|
ex,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MSC4143: Attempt to fetch transports from backend.
|
|
|
|
|
if ("_unstable_getRTCTransports" in client) {
|
|
|
|
|
try {
|
|
|
|
|
const selectedTransport = await getFirstUsableTransport(
|
|
|
|
|
await client._unstable_getRTCTransports(),
|
|
|
|
|
);
|
|
|
|
|
if (selectedTransport) {
|
|
|
|
|
logger.info("Using backend-configured SFU", selectedTransport);
|
|
|
|
|
return selectedTransport;
|
|
|
|
|
}
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
if (ex instanceof MatrixError && ex.httpStatus === 404) {
|
|
|
|
|
// Expected, this is an unstable endpoint and it's not required.
|
|
|
|
|
logger.debug("Backend does not provide any RTC transports", ex);
|
|
|
|
|
} else if (ex instanceof FailToGetOpenIdToken) {
|
|
|
|
|
throw ex;
|
|
|
|
|
} else {
|
|
|
|
|
// We got an error that wasn't just missing support for the feature, so log it loudly.
|
|
|
|
|
logger.error(
|
|
|
|
|
"Unexpected error fetching RTC transports from backend",
|
|
|
|
|
ex,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Legacy MSC4143 (to be removed) WELL_KNOWN: Prioritize the .well-known/matrix/client, if available.
|
2025-11-04 20:24:15 +01:00
|
|
|
const domain = client.getDomain();
|
2025-12-29 17:45:41 +00:00
|
|
|
if (domain) {
|
2025-11-04 20:24:15 +01:00
|
|
|
// we use AutoDiscovery instead of relying on the MatrixClient having already
|
|
|
|
|
// been fully configured and started
|
|
|
|
|
const wellKnownFoci = (await AutoDiscovery.getRawClientConfig(domain))?.[
|
|
|
|
|
FOCI_WK_KEY
|
|
|
|
|
];
|
2025-12-29 17:45:41 +00:00
|
|
|
const selectedTransport = Array.isArray(wellKnownFoci)
|
|
|
|
|
? await getFirstUsableTransport(wellKnownFoci)
|
|
|
|
|
: null;
|
|
|
|
|
if (selectedTransport) {
|
|
|
|
|
logger.info("Using .well-known SFU", selectedTransport);
|
|
|
|
|
return selectedTransport;
|
2025-11-04 20:24:15 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-21 14:02:15 +01:00
|
|
|
// CONFIG: Least prioritized; Load from config file
|
2025-11-04 20:24:15 +01:00
|
|
|
const urlFromConf = Config.get().livekit?.livekit_service_url;
|
2025-12-29 17:45:41 +00:00
|
|
|
if (urlFromConf) {
|
|
|
|
|
try {
|
2026-01-05 21:08:33 +01:00
|
|
|
// This will call the jwt/sfu/get endpoint to pre create the livekit room.
|
2026-01-05 21:17:37 +01:00
|
|
|
logger.info("Using config SFU", urlFromConf);
|
|
|
|
|
return await doOpenIdAndJWTFromUrl(urlFromConf);
|
2025-12-29 17:45:41 +00:00
|
|
|
} catch (ex) {
|
|
|
|
|
if (ex instanceof FailToGetOpenIdToken) {
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
logger.error("Failed to validate config SFU", ex);
|
|
|
|
|
}
|
2025-11-04 20:24:15 +01:00
|
|
|
}
|
2025-11-21 14:02:15 +01:00
|
|
|
|
2026-01-05 21:08:33 +01:00
|
|
|
// If we do not have returned a transport by now we throw an error
|
|
|
|
|
throw new MatrixRTCTransportMissingError(domain ?? "");
|
2025-11-04 20:24:15 +01:00
|
|
|
}
|