/* Copyright 2023-2026 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import { deepCompare } from "../utils.ts"; import { type RTCCallCapability, type RTCCallIntent, type Transport, type SlotDescription } from "./types.ts"; import { type MatrixEvent } from "../models/event.ts"; import { type Logger, logger } from "../logger.ts"; import { computeSlotId, slotIdToDescription } from "./utils.ts"; import { checkRtcMembershipData, computeRtcIdentityRaw, type RtcMembershipData, checkSessionsMembershipData, type SessionMembershipData, MatrixRTCMembershipParseError, } from "./membershipData/index.ts"; import { EventType } from "../@types/event.ts"; import { isUnstableLivekitTransport, selectLivekitTransport } from "./LivekitTransport.ts"; import { toStableCallIntent, toUnstableCallIntent } from "./CallIntent.ts"; /** * The default duration in milliseconds that a membership is considered valid for. * Ordinarily the client responsible for the session will update the membership before it expires. * We use this duration as the fallback case where stale sessions are present for some reason. */ export const DEFAULT_EXPIRE_DURATION = 1000 * 60 * 60 * 4; /** The fields of a legacy session membership that belong to the session, not the application */ const sessionFields: (keyof SessionMembershipData)[] = [ "application", "call_id", "device_id", "focus_active", "foci_preferred", "created_ts", "scope", "expires", "m.call.intent", "membershipID", ]; /** * Describes the source event type that provided the membership data. */ enum MembershipKind { /** * The modern MSC4143 format event. */ RTC = "rtc", /** * The legacy call event type. */ Session = "session", } type MembershipData = | { kind: MembershipKind.RTC; data: RtcMembershipData } | { kind: MembershipKind.Session; data: SessionMembershipData }; function getDeviceId({ kind, data }: MembershipData): string { switch (kind) { case MembershipKind.RTC: return data.member.device_id; case MembershipKind.Session: return data.device_id; } } function getMemberId({ kind, data }: MembershipData, sender: string): string { // the createdTs behaves equivalent to the membershipID. // we only need the field for the legacy member events where we needed to update them // synapse ignores sending state events if they have the same content. switch (kind) { case MembershipKind.RTC: return data.member.id; case MembershipKind.Session: return ( // best case we have a client already publishing the right custom membershipId data.membershipID ?? // alternatively we use the hardcoded jwt id default value (used until version 0.16.0) `${sender}:${data.device_id}` ); } } function getTransports({ kind, data }: MembershipData): Transport[] { switch (kind) { case MembershipKind.RTC: return data.transports.published; case MembershipKind.Session: return data.foci_preferred ?? []; // Validation allows memberships without `foci_preferred` } } type LimitedEvent = Pick; // TODO: Rename to RtcMembership once we removed the legacy SessionMembership is removed, to avoid confusion. export class CallMembership { /** * Parse the membershipdata from a call membership event. * @param matrixEvent The Matrix event to read. * @returns MembershipData in either MembershipKind.RTC or MembershipKind.Session format. * @throws If the content is neither format. */ public static membershipDataFromMatrixEvent(matrixEvent: LimitedEvent): MembershipData { const sender = matrixEvent.getSender(); const evType = matrixEvent.getType(); const data = matrixEvent.getContent(); if (sender === undefined) throw new Error("matrixEvent is missing sender field"); try { // Event types are strictly checked here. if (evType === EventType.RTCMembership && checkRtcMembershipData(data, sender)) { return { kind: MembershipKind.RTC, data }; } else if (evType === EventType.GroupCallMemberPrefix && checkSessionsMembershipData(data)) { return { kind: MembershipKind.Session, data }; } else { throw Error(`'${evType} is not a known call membership type`); } } catch (ex) { if (ex instanceof MatrixRTCMembershipParseError) { logger.debug("CallMembership.MatrixRTCMembershipParseError provided invalid data", data); } throw ex; } } /** * Parse the contents of a MatrixEvent and create a CallMembership instance. * @param matrixEvent The Matrix event to read. */ public static async parseFromEvent(matrixEvent: LimitedEvent): Promise { const sender = matrixEvent.getSender(); if (sender === undefined) throw new Error("matrixEvent is missing sender field"); const membershipData: MembershipData = this.membershipDataFromMatrixEvent(matrixEvent); const livekitTransports = getTransports(membershipData).filter(isUnstableLivekitTransport); const legacyIdentity = `${sender}:${getDeviceId(membershipData)}`; const backendIdentities: string[] = []; if (livekitTransports.some((t) => "url" in t)) { backendIdentities.push(await computeRtcIdentityRaw(sender, getMemberId(membershipData, sender))); } if (livekitTransports.some((t) => "livekit_service_url" in t)) { backendIdentities.push(legacyIdentity); } // rtcBackendIdentity is deprecated, so it's fine to always set it to the legacy identity. return new CallMembership(matrixEvent, membershipData, backendIdentities, legacyIdentity); } public static equal(a?: CallMembership, b?: CallMembership): boolean { return deepCompare(a?.membershipData, b?.membershipData); } private logger: Logger; /** The parsed data from the Matrix event. * To access checked eventId and sender from the matrixEvent. * Class construction will fail if these values cannot get obtained. */ private readonly matrixEventData: { eventId: string; sender: string }; /** * Use `parseFromEvent`. * Constructor should only be used by tests. * @private */ public constructor( /** The Matrix event that this membership is based on */ private readonly matrixEvent: LimitedEvent, private readonly membershipData: MembershipData, /** * Possible identities which this member may have on the backends of * their published transports. */ public readonly backendIdentities: string[], /** * @deprecated Check the backend for the existence of any of * {@link backendIdentities} instead. */ public readonly rtcBackendIdentity: string, ) { const eventId = matrixEvent.getId(); const sender = matrixEvent.getSender(); if (eventId === undefined) throw new Error("matrixEvent is missing eventId field"); if (sender === undefined) throw new Error("matrixEvent is missing sender field"); this.logger = logger.getChild(`[CallMembership ${sender}:${this.deviceId}]`); this.matrixEventData = { eventId, sender }; } /** @deprecated use userId instead */ public get sender(): string { return this.userId; } public get userId(): string { return this.matrixEventData.sender; } public get eventId(): string { return this.matrixEventData.eventId; } /** * The ID of the MatrixRTC slot that this membership belongs to (format `{application}#{id}`). * This is computed in case SessionMembershipData is used. */ public get slotId(): string { const { kind, data } = this.membershipData; if (data.application === "m.call") { switch (kind) { case MembershipKind.RTC: return data.slot_id; case MembershipKind.Session: default: { const [application, id] = [data.application, data.call_id]; // INFO_SLOT_ID_LEGACY_CASE (search for all occurances of this INFO to get the full picture) // The spec got changed to use `"ROOM"` instead of `""` empyt string for the implicit default call. // State events still are sent with `""` however. To find other events that should end up in the same call, // we use the slotId. // Since the CallMembership is the public representation of a rtc.member event, we just pretend it is a // "ROOM" slotId/call_id. // This makes all the remote members work with just this simple trick. // // We of course now need to be careful when sending legacy events (state events) // They get a slotDescription containing "ROOM" since this is what we use starting at the time this comment // is commited. // // See the Other INFO_SLOT_ID_LEGACY_CASE comments to see where we revert back to "" just before sending the event. let compatibilityAdaptedId: string; if (id === "") { compatibilityAdaptedId = "ROOM"; this.logger?.info("use slotId compat hack emptyString -> ROOM"); } else { compatibilityAdaptedId = id; } return computeSlotId({ application, id: compatibilityAdaptedId, }); } } } this.logger?.info("NOT using slotId compat hack emptyString -> ROOM"); // This is what the function should look like for any other application that did not // go through a `""`=> `"ROOM"` rename switch (kind) { case MembershipKind.RTC: return data.slot_id; case MembershipKind.Session: default: return computeSlotId({ application: data.application, id: data.call_id }); } } public get deviceId(): string { return getDeviceId(this.membershipData); } /** * The stable call intent this member advertises, or `undefined` if none. * * @experimental Part of [MSC4196](https://github.com/matrix-org/matrix-spec-proposals/pull/4196). */ public get intent(): RTCCallIntent | undefined { const { kind, data } = this.membershipData; let intent: unknown; switch (kind) { case MembershipKind.RTC: if (data.application["type"] === "m.call") { // Prefer the stable key `intent`. Otherwise, fall back to the unstable `m.call.intent` // sent by older clients which don't know `intent`. intent = data.application["intent"] ?? data.application["m.call.intent"]; } break; case MembershipKind.Session: if (data.application === "m.call") { intent = data["m.call.intent"]; } break; } // If we have an intent, try to map it to the stable identifiers. return typeof intent === "string" ? toStableCallIntent(intent) : undefined; } /** * The unstable call intent this member advertises, or `undefined` if none. * * @experimental Part of [MSC4196](https://github.com/matrix-org/matrix-spec-proposals/pull/4196). * @deprecated Use {@link CallMembership.intent} instead. */ public get callIntent(): RTCCallIntent | undefined { return toUnstableCallIntent(this.intent); } public get callCapabilities(): RTCCallCapability[] | undefined { const capabilities = this.applicationData["capabilities"]; if (capabilities === undefined) return undefined; if (Array.isArray(capabilities) && capabilities.every((c) => typeof c === "string")) return capabilities; this.logger.warn("RTC membership has invalid capabilities"); return undefined; } /** * Parsed `slot_id` (format `{application}#{id}`) into its components (application and id). */ public get slotDescription(): SlotDescription { const { kind, data } = this.membershipData; if (kind === MembershipKind.RTC) { const id = data.slot_id.slice(`${data.application.type}#`.length); return { application: data.application.type, id }; } return slotIdToDescription(this.slotId); } /** * Information about the application being used for the RTC session. * May contain extra keys specific to the application. */ public get applicationData(): { type: string; [key: string]: unknown } { const { kind, data } = this.membershipData; switch (kind) { case MembershipKind.RTC: return data.application; case MembershipKind.Session: default: { // SessionData has no application object: the application's own // data sits at the top level beside the session fields, so // everything but those is the application's const { application, ...rest } = data; for (const key of sessionFields) delete rest[key as keyof typeof rest]; return { ...rest, "type": application, "m.call.intent": data["m.call.intent"] }; } } } /** @deprecated scope is not used and will be removed in future versions. replaced by application specific types.*/ public get scope(): SessionMembershipData["scope"] | undefined { const { kind, data } = this.membershipData; switch (kind) { case MembershipKind.RTC: return undefined; case MembershipKind.Session: default: return data.scope; } } /** * This computes the membership ID for the membership. * For the sticky event based rtcSessionData this is trivial it is `member.id`. * This is not supposed to be used to identity on an rtc backend. This is just a nuance for * a generated (sha256) anonymised identity. Only send {@link backendIdentities} to any rtc backend service. * * For the legacy sessionMemberEvents it is a bit more complex. Here we sometimes do not have this data * in the event content and we expected the SFU and the client to use `${this.matrixEventData.sender}:${data.device_id}`. * * So if there is no membershipID we use the hard coded jwt id default (`${this.matrixEventData.sender}:${data.device_id}`) * value (used until version 0.16.0) * * It is also possible for a session event to set a custom membershipID. in that case this will be used. */ public get memberId(): string { return getMemberId(this.membershipData, this.matrixEventData.sender); } public createdTs(): number { const { kind, data } = this.membershipData; switch (kind) { case MembershipKind.RTC: // TODO Do we need this to represent the TS of the original join? return this.matrixEvent.getTs(); case MembershipKind.Session: default: return data.created_ts ?? this.matrixEvent.getTs(); } } /** * Gets the absolute expiry timestamp of the membership. * @returns The absolute expiry time of the membership as a unix timestamp in milliseconds or undefined if not applicable */ public getAbsoluteExpiry(): number | undefined { const { kind, data } = this.membershipData; switch (kind) { case MembershipKind.RTC: return undefined; case MembershipKind.Session: default: // TODO: calculate this from the MatrixRTCSession join configuration directly return this.createdTs() + (data.expires ?? DEFAULT_EXPIRE_DURATION); } } /** * @returns The number of milliseconds until the membership expires or undefined if applicable * @deprecated Not used by RTC events. */ public getMsUntilExpiry(): number | undefined { const { kind } = this.membershipData; if (kind === MembershipKind.Session) { const absExpiry = this.getAbsoluteExpiry(); if (absExpiry) { // Assume that local clock is sufficiently in sync with other clocks in the distributed system. // We used to try and adjust for the local clock being skewed, but there are cases where this is not accurate. // The current implementation allows for the local clock to be -infinity to +MatrixRTCSession.MEMBERSHIP_EXPIRY_TIME/2 return absExpiry - Date.now(); } } return undefined; } /** * @returns true if the membership has expired, otherwise false */ public isExpired(): boolean { const { kind } = this.membershipData; switch (kind) { case MembershipKind.RTC: return false; case MembershipKind.Session: default: return this.getMsUntilExpiry()! <= 0; } } /** * Gets the primary transport used by this call membership to publish media. * * For RTC membership (m.rtc.member), the transport is computed from the `transports.published` array. * For legacy session membership (m.call.member), in turn, the transport is computed from the `foci_preferred` * array. * * Regardless of the membership kind, the transport is chosen in the following sequence: * * 1. Return the first `m.livekit` transport from the array if there is one. * 2. Otherwise, return the first `livekit` transport from the array if there is one. * 3. Otherwise, return the first element of the array. * * @param _oldestMembership Deprecated and unused. * * @returns The transport this membership uses to publish media. * * @experimental Part of [MSC4195](https://github.com/matrix-org/matrix-spec-proposals/pull/4195). */ public getTransport(_oldestMembership?: CallMembership): Transport | undefined { const transports = getTransports(this.membershipData); return selectLivekitTransport(transports) ?? transports[0]; } /** * The value of the `transports.published` field for RTC memberships (m.rtc.member). * Or the value of the `foci_preferred` field for legacy session memberships (m.call.member). */ public get transports(): Transport[] { return getTransports(this.membershipData); } }