Compare commits

..
Author SHA1 Message Date
renovate[bot] b2f7fccc6e Update ghcr.io/element-hq/element-web:develop Docker digest to 2f9e5d9 (#4490)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-11-21 02:07:47 +00:00
TimoandClaude Fable 5.2 f9dafcd5ea Publish late device (#4485)
* Publish a camera or microphone listed after the call view opened

Without a lobby the call decides what to publish once, as its view
mounts. A device the browser listed a moment later switched to its
default "on" without the mute handler being asked, so the button showed
on and nothing was published: the other side kept waiting for media.

The default is now asked of the handler like any other request where one
is set, so the call publishes the device. A device that comes back while
a handler is set stays off, since the user may have turned it off; the
button used to show it as on there too. Without a handler, as in the
lobby, the default is assumed at once as before.

Follows the patch tiran133 posted on the issue. A stop-gap until one
reconciler owns the local media.

Fixes https://github.com/element-hq/element-call/issues/4403

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-20 17:07:51 +00:00
Timo ded4471525 Merge pull request #4486 from element-hq/toger5/4479-exact-device-constraint
Exact device constraint (fixes chrome device switches)
2026-11-20 17:57:43 +01:00
Timo K. e74e83ab08 comments on what fixes the chrome issue 2026-11-20 17:15:41 +01:00
Johannes Marbach 3917bbb8f2 Merge pull request #4484 from ciefa/ciefa/fullscreen-button-clear-of-footer
Keep the tile buttons clear of the footer when it overlays the video
2026-11-20 15:33:43 +01:00
Timo K.andClaude Fable 5.2 4c66fe6f7d Ask the browser for the exact camera and microphone picked
Chromium hands back its default device for a deviceId that is only
preferred, so picking another camera reopened the same one: the preview
flashed and nothing changed. The lobby's own capture and the in-call
switch both passed the id as a preference since they stopped going
through LiveKit's track creation, which made it exact.

- The lobby media asks for the exact device, and falls back to the
  browser's choice only where the remembered device is gone.
- A track handed over to the call keeps the exact device, so that LiveKit
  reopens the same one after a mute.
- Switching the facing mode lets go of the pinned device.
- setDevice rejects where the track did not end up on the device, in the
  lobby and in the call, and keeps the track it had.

Fixes https://github.com/element-hq/element-call/issues/4479

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-20 14:07:17 +01:00
Johannes Marbach ffd6360277 Merge pull request #4472 from element-hq/johannes/stable-rtc-device
Request capability to receive m.rtc.encryption_key events
2026-11-20 11:37:20 +01:00
Johannes Marbach eabc6c0823 Merge branch 'main' into johannes/stable-rtc-device 2026-11-20 10:53:09 +01:00
renovate[bot] 7d6fbd336d Update ghcr.io/element-hq/element-web:develop Docker digest to dd8dbe3 (#4482)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-11-20 09:52:33 +00:00
Johannes Marbach f9fb566f41 Merge branch 'main' into johannes/stable-rtc-device 2026-11-20 09:21:20 +01:00
Santino Wagner 95a646b3bc Keep the tile buttons clear of the footer when it overlays the video
In a call view at most 600px high, and in a one-on-one call in a view
at most 600px wide, the footer is drawn over the video. The spotlight
tile's bottom-right buttons (full screen, expand, screen share volume)
stayed pinned to the tile's bottom edge, underneath the footer, so a
click on them landed on the footer instead.

Offset the buttons by --call-view-safe-area-inset-bottom, which the call
view already sets to the footer's height while the footer overlays the
tiles. The media foreground of a maximised tile uses the same inset. The
variable is unset in every other layout, so nothing moves there.

The rule is not limited to maximised tiles: with the layout switch on
"grid", a short window shows a spotlight tile that is not maximised but
still reaches the bottom edge.
2026-11-19 16:35:00 +01:00
Johannes Marbach 6cdcd5e83e Merge branch 'main' into johannes/stable-rtc-device 2026-11-19 09:16:01 +01:00
Johannes Marbach 1cdcb62791 Request capability to receive m.rtc.encryption_key events 2026-11-18 11:16:41 +01:00
78 changed files with 1462 additions and 2314 deletions
-93
View File
@@ -1,93 +0,0 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { afterEach, beforeEach, expect, test, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import { type MatrixClient, type Room } from "matrix-js-sdk";
import { mockEmitter, mockMatrixRoom } from "../src/utils/test";
import { ElementCall } from "./index";
// The call itself is out of scope: what matters is what it is handed
const callView = vi.hoisted(() => vi.fn());
vi.mock("../src/room/CallView", () => ({
CallView: (props: object): string => {
callView(props);
return "call";
},
}));
// Media devices need a browser jsdom does not have
vi.mock("../src/state/AppViewModel", () => ({
AppViewModel: class {
public mediaDevices = {};
public backgroundEffects = {};
},
}));
const roomId = "!room:example.org";
let room: Room;
beforeEach(() => {
// What the call's backend takes from the room
room = mockMatrixRoom({
roomId,
getMembersWithMembership: vi.fn(),
getLiveTimeline: vi.fn(),
getVersion: vi.fn(),
hasMembershipState: vi.fn(),
_unstable_getStickyEvents: vi.fn(),
getMxcAvatarUrl: vi.fn(),
getJoinRule: vi.fn(),
});
});
afterEach(() => {
callView.mockClear();
});
test("calls in the room, leaving the session to the call", async () => {
render(<ElementCall client={mockClient()} roomId={roomId} />);
await screen.findByText("call");
const props = callView.mock.lastCall?.[0];
expect(props).toMatchObject({ backend: { roomId } });
expect(props).not.toHaveProperty("rtcSession");
});
test("shows no call in a room the host's client does not know", () => {
render(<ElementCall client={mockClient(null)} roomId={roomId} />);
expect(screen.queryByText("call")).toBeNull();
});
// No `matrixRTC`: the host's session manager is not ours to use
function mockClient(knownRoom: Room | null = room): MatrixClient {
return {
...mockEmitter(),
getRoom: (id: string) => (id === roomId ? knownRoom : null),
// What the call view's client is made from
baseUrl: "https://matrix.example.org",
getUserId: () => "@alice:example.org",
getDeviceId: () => "DEVICE",
getAccessToken: () => "token",
getSyncState: () => null,
getUser: () => null,
getCrypto: () => undefined,
sendEvent: vi.fn(),
sendStateEvent: vi.fn(),
_unstable_sendStickyEvent: vi.fn(),
_unstable_sendStickyDelayedEvent: vi.fn(),
cancelPendingEvent: vi.fn(),
encryptAndSendToDevice: vi.fn(),
redactEvent: vi.fn(),
decryptEventIfNeeded: vi.fn(),
setDisplayName: vi.fn(),
setAvatarUrl: vi.fn(),
uploadContent: vi.fn(),
doesServerSupportUnstableFeature: vi.fn(),
} as unknown as MatrixClient;
}
-118
View File
@@ -1,118 +0,0 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { afterEach, expect, test, vi } from "vitest";
import {
ClientEvent,
EventType,
type MatrixClient,
SyncState,
} from "matrix-js-sdk";
import { createComponentLocalJsClient } from "./componentLocalJsClient";
afterEach(() => {
vi.unstubAllGlobals();
});
test("reads the host's live state from the host's client", () => {
const host = mockHost();
const client = createComponentLocalJsClient(host);
const listener = (): void => {};
expect(client.getSyncState()).toBe(SyncState.Syncing);
client.on(ClientEvent.Sync, listener);
client.off(ClientEvent.Sync, listener);
client.getUser("@bob:example.org");
client.getCrypto();
expect(host.getSyncState).toHaveBeenCalledOnce();
expect(host.on).toHaveBeenCalledWith(ClientEvent.Sync, listener);
expect(host.off).toHaveBeenCalledWith(ClientEvent.Sync, listener);
expect(host.getUser).toHaveBeenCalledWith("@bob:example.org");
expect(host.getCrypto).toHaveBeenCalledOnce();
});
test("sends the RTC session's encrypted events with the host's crypto", async () => {
const host = mockHost();
const client = createComponentLocalJsClient(host);
await client._unstable_sendStickyEvent(
"!room:example.org",
1000,
null,
EventType.RTCMembership,
{ slot_id: "m.call#ROOM", msc4354_sticky_key: "key" },
);
await client.encryptAndSendToDevice("m.room.encrypted", [], {});
expect(host._unstable_sendStickyEvent).toHaveBeenCalledOnce();
expect(host.encryptAndSendToDevice).toHaveBeenCalledOnce();
});
test("takes its credentials from the host", () => {
const client = createComponentLocalJsClient(mockHost());
expect(client.getUserId()).toBe("@alice:example.org");
expect(client.getDeviceId()).toBe("DEVICE");
expect(client.getDomain()).toBe("example.org");
expect(client.baseUrl).toBe("https://matrix.example.org");
});
test("makes requests with whichever token the host holds now", async () => {
const fetch = vi.fn(
async () =>
await Promise.resolve(
new Response(JSON.stringify({ access_token: "openid" }), {
headers: { "Content-Type": "application/json" },
}),
),
);
vi.stubGlobal("fetch", fetch);
const host = mockHost();
const client = createComponentLocalJsClient(host);
// As if the host had refreshed its token after we were created
host.token = "refreshed";
await client.getOpenIdToken();
expect(fetch).toHaveBeenCalledOnce();
const [url, init] = fetch.mock.calls[0] as unknown as [URL, RequestInit];
expect(url.toString()).toBe(
"https://matrix.example.org/_matrix/client/v3/user/%40alice%3Aexample.org/openid/request_token",
);
expect(new Headers(init.headers).get("Authorization")).toBe(
"Bearer refreshed",
);
});
function mockHost(): MatrixClient & { token: string } {
const host = {
token: "first",
baseUrl: "https://matrix.example.org",
getUserId: (): string => "@alice:example.org",
getDeviceId: (): string => "DEVICE",
getAccessToken: (): string => host.token,
getSyncState: vi.fn(() => SyncState.Syncing),
on: vi.fn(),
off: vi.fn(),
getUser: vi.fn(() => null),
getCrypto: vi.fn(() => undefined),
sendEvent: vi.fn(),
sendStateEvent: vi.fn(),
_unstable_sendStickyEvent: vi.fn(),
_unstable_sendStickyDelayedEvent: vi.fn(),
encryptAndSendToDevice: vi.fn(),
redactEvent: vi.fn(),
decryptEventIfNeeded: vi.fn(),
setDisplayName: vi.fn(),
setAvatarUrl: vi.fn(),
uploadContent: vi.fn(),
doesServerSupportUnstableFeature: vi.fn(),
};
return host as unknown as MatrixClient & { token: string };
}
-108
View File
@@ -1,108 +0,0 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { createClient, type MatrixClient } from "matrix-js-sdk";
import type { createCallMatrixBackend } from "../src/CallMatrixBackend";
/**
* The client for Element Call in Component mode.
* We do get the full host client but we dont want to use it as it
* might use older versions of methods were we expect changes. (f.ex. getLivekitToken)
* We really want the expected implementation to not get runtime errors.
*
* This creates a new client that does use the hosts token and "stable"
* methods but uses the ElementCall Components js-sdk's client impl for anything else.
*/
export function createComponentLocalJsClient(
host: MatrixClient,
): Parameters<typeof createCallMatrixBackend>[0] {
const own = createClient({
baseUrl: host.baseUrl,
userId: host.getUserId() ?? undefined,
deviceId: host.getDeviceId() ?? undefined,
accessToken: host.getAccessToken() ?? undefined,
// The host refreshes its token without telling us, so each request takes
// whichever token the host holds now
fetchFn: async (input, init) => {
const headers = new Headers(init?.headers);
const token = host.getAccessToken();
if (token !== null) headers.set("Authorization", `Bearer ${token}`);
return globalThis.fetch(input, { ...init, headers });
},
});
return {
// Own has no sync loop: `syncApi?.getSyncState() ?? null` is always null
getSyncState: host.getSyncState.bind(host),
// Only the syncing host emits `Sync`, `ToDeviceEvent` and `Decrypted`
on: host.on.bind(host),
// The same emitter as `on`
off: host.off.bind(host),
// `this.store.getUser`: own's store is empty, only the host's sync fills it
getUser: host.getUser.bind(host),
// Own never inits crypto, and a second one on the device would corrupt it
getCrypto: host.getCrypto.bind(host),
// Own has no rooms: `encryptEventIfNeeded`'s `if (!room) return` skips E2EE
sendEvent: host.sendEvent.bind(host),
// Not encrypted, but the local echo goes to `getRoom()`: only the host's
redactEvent: host.redactEvent.bind(host),
// `attemptDecryption` only runs `if (this.getCrypto())`: a no-op on own
decryptEventIfNeeded: host.decryptEventIfNeeded.bind(host),
// Like `sendEvent`: on own, `if (!room) return` sends it in plaintext
_unstable_sendStickyEvent: host._unstable_sendStickyEvent.bind(host),
// Like `sendEvent`: delayed, but still `encryptEventIfNeeded(event, room)`
_unstable_sendStickyDelayedEvent:
host._unstable_sendStickyDelayedEvent.bind(host),
// `if (!this.cryptoBackend) throw`: own has no crypto
encryptAndSendToDevice: host.encryptAndSendToDevice.bind(host),
// Static: own was created with the host's user id
getUserId: own.getUserId.bind(own),
// Static: own was created with the host's device id
getDeviceId: own.getDeviceId.bind(own),
// Static: derived from the user id
getDomain: own.getDomain.bind(own),
// Static: own was created with the host's base URL
baseUrl: own.baseUrl,
// MSC4143 stable or unstable prefix, picked by /versions: ours knows both
getRTCTransports: own.getRTCTransports.bind(own),
// Plain HTTP with the copied credentials, like the other LiveKit auth
getOpenIdToken: own.getOpenIdToken.bind(own),
// MSC4195 stable or `io.element.msc4195` prefix: an older host may lack it
getLivekitToken: own.getLivekitToken.bind(own),
// MSC4195, like `getLivekitToken`: an older host may lack it
delegateDelayedLeave: own.delegateDelayedLeave.bind(own),
// Plain HTTP into own's cache, which `getCachedCapabilities` reads
fetchCapabilities: own.fetchCapabilities.bind(own),
// Has to be the client whose cache `fetchCapabilities` filled
getCachedCapabilities: own.getCachedCapabilities.bind(own),
// Straight to `scheduleDelayedEvent`: no room, no crypto, plain HTTP
_unstable_sendDelayedStateEvent:
own._unstable_sendDelayedStateEvent.bind(own),
// Plain POST `/delayed_events/{id}/cancel`; newer API an old host may lack
_unstable_cancelScheduledDelayedEvent:
own._unstable_cancelScheduledDelayedEvent.bind(own),
// Plain POST by the server's delay id: restarts the host's sticky leave too
_unstable_restartScheduledDelayedEvent:
own._unstable_restartScheduledDelayedEvent.bind(own),
// Plain POST `/delayed_events/{id}/send`; newer API an old host may lack
_unstable_sendScheduledDelayedEvent:
own._unstable_sendScheduledDelayedEvent.bind(own),
// Plain HTTP, either client works; a stable API, so the host's
sendStateEvent: host.sendStateEvent.bind(host),
// Updates the `User` in the host's store, which `useProfile` listens to
setDisplayName: host.setDisplayName.bind(host),
// Like `setDisplayName`: updates the `User` in the host's store
setAvatarUrl: host.setAvatarUrl.bind(host),
// Plain HTTP, either client works; a stable API, so the host's
uploadContent: host.uploadContent.bind(host),
// Reads the host's /versions, which it has already fetched
doesServerSupportUnstableFeature:
host.doesServerSupportUnstableFeature.bind(host),
};
}
+6 -13
View File
@@ -84,9 +84,7 @@ import styles from "./ElementCall.module.css";
import { useComponentHostBridge } from "./host";
import { CallPortalRoot } from "./CallPortalRoot";
import { type ElementCallProps } from "./api";
import { createComponentLocalJsClient } from "./componentLocalJsClient";
import { supportedLanguages, translationsBackend } from "./localization";
import { createCallMatrixBackend } from "../src/CallMatrixBackend";
// The languages Element Call can be shown in
export { supportedLanguages } from "./localization";
@@ -207,14 +205,12 @@ export const ElementCall: FC<ElementCallProps> = ({
}, [controlledAudioDevices, callIntent]);
const room = client.getRoom(roomId);
const backend = useMemo(
() =>
room &&
createCallMatrixBackend(createComponentLocalJsClient(client), room),
const rtcSession = useMemo(
() => (room === null ? null : client.matrixRTC.getRoomSession(room)),
[client, room],
);
if (room === null)
if (rtcSession === null)
logger.error(
`Element Call was asked to call in ${roomId}, which its host's client does not know about`,
);
@@ -224,7 +220,7 @@ export const ElementCall: FC<ElementCallProps> = ({
// is told that Element Call has loaded, as the widget tells its client once
// its own initialisation is over. Once per mount, however often the pieces
// are later swapped out.
const ready = container !== null && backend !== null && vm !== null;
const ready = container !== null && rtcSession !== null && vm !== null;
const announcedLoaded = useRef(false);
useEffect(() => {
if (!ready || announcedLoaded.current) return;
@@ -254,17 +250,14 @@ export const ElementCall: FC<ElementCallProps> = ({
>
<Decoration>
<TooltipProvider>
{/* The host's own client: what reads it through
the context (avatars, the encryption system,
rageshakes, the disconnected banner) uses only its
stable methods. */}
<ClientProvider client={client}>
<MediaDevicesContext value={vm.mediaDevices}>
<BackgroundEffectsProvider
effects={vm.backgroundEffects}
>
<CallView
backend={backend}
client={client}
rtcSession={rtcSession}
isPasswordlessUser={false}
confineToRoom={params.confineToRoom}
preload={params.preload}
+2 -2
View File
@@ -19,10 +19,10 @@ services:
- ./backend/playwright_homeserver-othersite.yaml:/data/cfg/homeserver.yaml:Z
element-web:
# Pin to a SHA so that upstream cannot break our tests. Renovate handles regular updates.
image: ghcr.io/element-hq/element-web:develop@sha256:f85350f2b31a7521a800d9050e9f5c0676d04cfb60675242ccc0c6e25cad7d04
image: ghcr.io/element-hq/element-web:develop@sha256:2f9e5d993e20adbd062453fd1c437821d554bac1f22ed3177290b7911f667848
element-web-1:
# Pin to a SHA so that upstream cannot break our tests. Renovate handles regular updates.
image: ghcr.io/element-hq/element-web:develop@sha256:f85350f2b31a7521a800d9050e9f5c0676d04cfb60675242ccc0c6e25cad7d04
image: ghcr.io/element-hq/element-web:develop@sha256:2f9e5d993e20adbd062453fd1c437821d554bac1f22ed3177290b7911f667848
volumes:
# The other site opens the embedded package under test in place of the
# copy its image bundles (the first site opens the dev server's app.
+2
View File
@@ -118,6 +118,8 @@
"livekit_connection_error": "Failed to connect to Livekit server",
"livekit_connection_error_description": "An error occurred while connecting to the Livekit server (<1>Reason:</1> <2>{{ reason }}</2>).",
"matrix_rtc_transport_missing": "The server is not configured to work with {{brand}} and no other call participant provides a usable transport. Please contact your server admin (Domain: {{domain}}, Error Code: {{ errorCode }}).",
"membership_manager": "Membership Manager Error",
"membership_manager_description": "The Membership Manager had to shut down. This is caused by many consecutive failed network requests.",
"no_matrix_2_authorization_service": "The authorization service for your media server (SFU) is out of date.",
"open_elsewhere": "Opened in another tab",
"open_elsewhere_description": "{{brand}} has been opened in another tab. If that doesn't sound right, try reloading the page.",
+78
View File
@@ -0,0 +1,78 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { expect, test, type Page } from "@playwright/test";
import { SpaHelpers } from "./spa-helpers.ts";
test.use({
launchOptions: {
args: [
"--use-fake-ui-for-media-stream",
"--use-fake-device-for-media-stream=device-count=2",
"--mute-audio",
],
},
});
test.describe("the camera menu", () => {
test.skip(
({ browserName }) => browserName !== "chromium",
"Only Chromium offers a second fake camera",
);
test("moves the preview to the camera picked, and the call after it", async ({
page,
}) => {
await page.goto("/");
await SpaHelpers.createCall(page, "Camera user", "Camera switch");
await expectCamera(page, "fake_device_0");
await selectCamera(page, "fake_device_1");
await expectCamera(page, "fake_device_1");
await page.getByTestId("lobby_joinCall").click();
await expect(page.getByTestId("name_tag")).toContainText("Camera user");
await expectCamera(page, "fake_device_1");
await selectCamera(page, "fake_device_0");
await expectCamera(page, "fake_device_0");
// Turning the camera on again opens it anew
const camera = page.getByTestId("incall_videomute");
await camera.click();
await expect(camera).toHaveAttribute("aria-checked", "false");
await camera.click();
await expect(camera).toHaveAttribute("aria-checked", "true");
await expectCamera(page, "fake_device_0");
});
});
async function selectCamera(page: Page, name: string): Promise<void> {
await page.getByRole("button", { name: "Camera", exact: true }).click();
await page.getByRole("menuitemradio", { name }).click();
// Selecting doesn't close the menu, so dismiss it.
await page.keyboard.press("Escape");
await expect(page.getByRole("menu")).not.toBeVisible();
}
/** Every video on the page shows this camera, and there is one that does. */
async function expectCamera(page: Page, label: string): Promise<void> {
await expect
.poll(async () =>
page.evaluate(() => [
...new Set(
[...document.querySelectorAll("video")].flatMap((video) =>
video.srcObject instanceof MediaStream
? video.srcObject.getVideoTracks().map((track) => track.label)
: [],
),
),
]),
)
.toEqual([label]);
}
+36
View File
@@ -0,0 +1,36 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { expect, test } from "@playwright/test";
import { SpaHelpers } from "./spa-helpers.ts";
test("The full screen button can be clicked while the footer overlays the video", async ({
page,
}) => {
await page.goto("/");
await SpaHelpers.createCall(page, "John Doe", "ShortWindow", true);
await SpaHelpers.expectVideoTilesCount(page, 1);
const fullScreenButton = page.getByRole("button", { name: "maximise" });
const footer = page.getByTestId("footer-container");
// Short enough for the footer to be drawn over the video
await page.setViewportSize({ width: 1000, height: 500 });
await expect(fullScreenButton).toBeVisible();
await page.getByTestId("videoTile").first().hover();
await expect(footer).toHaveCSS("opacity", "1");
const buttonBox = (await fullScreenButton.boundingBox())!;
const footerBox = (await footer.boundingBox())!;
expect(buttonBox.y + buttonBox.height).toBeLessThanOrEqual(footerBox.y);
await fullScreenButton.click();
await expect
.poll(async () => page.evaluate(() => document.fullscreenElement !== null))
.toBe(true);
});
+95
View File
@@ -0,0 +1,95 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { expect, test, type Page } from "@playwright/test";
import { widgetTest } from "../fixtures/widget-user.ts";
import { TestHelpers } from "./test-helpers.ts";
widgetTest.use({ callType: "dm" });
// The bug from issue #4403, in a real call between two users in Element Web.
// 1. Brooks' browser is made slow: it lists its camera and microphone only
// after the call is on screen.
// 2. Brooks starts a video call in a DM. A DM call has no lobby.
// 3. Whistler picks up.
// 4. Brooks' camera button shows "on".
// 5. Whistler must see two videos: their own and the one brooks sends.
// Before the fix whistler saw only their own video.
//
// What it guards: the unit tests only check the mute button's own logic. This
// test checks the whole chain: button, call, SFU, and what the other person
// really sees. The bug depends on timing (camera listed before or after the
// call opens), so the normal DM call tests pass by luck most of the time.
// This test forces the bad timing every run, so a regression shows up always
// and not only sometimes.
widgetTest(
"Publishes a camera the browser lists only once the call is on screen",
async ({ asWidget, browserName }) => {
test.skip(
browserName === "firefox",
"The is test is not working on firefox CI environment. No mic/audio device inputs so cam/mic are disabled",
);
// Two Element Webs and a real call.
test.slow();
const { brooks, whistler } = asWidget;
await listDevicesOnlyInTheCall(brooks.page);
await TestHelpers.startCallInCurrentRoom(brooks.page, false);
await expect(whistler.page.getByText("Incoming video call")).toBeVisible();
await whistler.page
.getByRole("alert")
.getByRole("button", { name: "Join" })
.click();
const brooksFrame = brooks.page
.locator('iframe[title="Element Call"]')
.contentFrame();
await expect(
brooksFrame.getByRole("switch", { name: "Stop video", checked: true }),
).toBeVisible();
// Whistler's own camera and the one brooks sends
const whistlerFrame = whistler.page
.locator('iframe[title="Element Call"]')
.contentFrame();
await TestHelpers.expectVisibleVideoCount(whistlerFrame, 2);
await expect(whistlerFrame.getByText("Waiting for media...")).toHaveCount(
0,
);
},
);
/**
* Holds the device list back in the widget until the call view shows a tile,
* as a browser that is slow to answer does. Call before the widget loads.
*
* How: it replaces the browser's `enumerateDevices` with one that waits. It
* checks every 50ms whether a video tile is on the page, waits half a second
* more, and only then gives the real list. Element Web itself (the outer
* page) is left alone; only the call inside the iframe is slowed down.
*/
async function listDevicesOnlyInTheCall(page: Page): Promise<void> {
await page.addInitScript(() => {
if (window.parent === window) return;
const devices = navigator.mediaDevices;
const enumerate = devices.enumerateDevices.bind(devices);
const inTheCall = new Promise<void>((resolve) => {
const poll = setInterval(() => {
if (document.querySelector("[data-testid='videoTile']") === null)
return;
clearInterval(poll);
setTimeout(resolve, 500);
}, 50);
});
devices.enumerateDevices = async (): Promise<MediaDeviceInfo[]> => {
await inTheCall;
return enumerate();
};
});
}
+5 -5
View File
@@ -20,7 +20,6 @@ import { EMPTY, filter, map, type Observable, of, switchMap, take } from "rxjs";
import {
type Behavior,
createRTCSlot,
createSdkMatrixBackend,
E2eeType,
type LocalRTCMember,
ObservableScope,
@@ -37,6 +36,7 @@ import {
import {
captureSettings,
rtcParticipationOptions,
rtcSlotOptions,
} from "../src/room/InCallView";
import { createWidgetHostBridge } from "../src/HostBridge";
import { MediaDevices } from "../src/state/MediaDevices";
@@ -94,9 +94,10 @@ export async function createMatrixRTCSdk(
);
const encryptionSystem = { kind: E2eeType.PER_PARTICIPANT } as const;
const capture = captureSettings();
const slot = createRTCSlot(scope, createSdkMatrixBackend(client, room), {
encryptionSystem,
slotDescription: { application, id },
const slot = createRTCSlot(scope, client, room, {
...rtcSlotOptions(encryptionSystem),
application,
id,
});
let vm: CallViewModel | undefined;
const failed =
@@ -130,7 +131,6 @@ export async function createMatrixRTCSdk(
{
rtcParticipation: participation,
matrixRoom: room,
client,
mediaDevices,
muteStates,
encryptionSystem,
+6 -7
View File
@@ -19,7 +19,6 @@ nothing in Element Call's `src/`.
```ts
import {
createRTCSlot,
createSdkMatrixBackend,
E2eeType,
MatrixRTCMode,
ObservableScope,
@@ -29,15 +28,15 @@ import {
const scope = new ObservableScope();
const slot = createRTCSlot(
scope,
createSdkMatrixBackend(
client, // a matrix-js-sdk MatrixClient, logged in and syncing
room, // the matrix-js-sdk Room, from client.getRoom() once the join has synced
),
{ encryptionSystem: { kind: E2eeType.PER_PARTICIPANT } }, // the room's call, m.call#ROOM
client, // a matrix-js-sdk MatrixClient, logged in and syncing
room, // the matrix-js-sdk Room, from client.getRoom() once the join has synced
{
encryptionSystem: { kind: E2eeType.PER_PARTICIPANT },
matrixRTCMode: MatrixRTCMode.Compatibility,
},
);
const participation = slot.join({
publish: [{ source: "microphone" }, { source: "camera" }],
matrixRTCMode: MatrixRTCMode.Compatibility,
});
// A <video> for each remote member's camera and an <audio> for its microphone.
+12 -12
View File
@@ -143,18 +143,18 @@ participation split in
[`client-into-slot-session-split-plan.md`](./client-into-slot-session-split-plan.md);
the sections below still describe `MatrixRTCClient` until that lands.
| Word | Meaning here | Elsewhere |
| --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------- |
| slot | an `RTCSlot`: the namespace in a room that memberships join into, `application` plus `id`. It exists while the room does, with nobody in it too. Holds `members$`, its open or closed `status$`, and `join()` | the js-sdk `MatrixRTCSession` is one per slot; the MSC4143 slot event |
| participation | an `RTCParticipation`: the span between our `join()` of a slot and our `leave()`. Holds the connection and its `state$`, `localMember$`, `remoteMembers$`, the connected transports and the data channel. Made by `slot.join()`, never reused | "session" is not used here, because in the js-sdk it means the slot |
| member | an `RTCMember`: one membership in a slot with what the SDK derives from it, `rtcBackendIdentity`, `userId`, `deviceId`, `memberId`, `matrixEventId$`, `displayName$`, `avatarUrl$`, `transport$` and `applicationData$`. No media. What the slot hands out in `members$` | the js-sdk `CallMembership` behind it, which never leaves the SDK; Element Call's old "matrixLivekitMember" |
| local member, remote member | `LocalRTCMember` and `RemoteRTCMember`: an `RTCMember` plus its `MemberMedia`, which only a participation can carry. The local one's tracks carry the controls. What the participation hands out in `localMember$` and `remoteMembers$` | LiveKit `LocalParticipant` and `RemoteParticipant`, which never leave the SDK |
| media | a `MemberMedia`: what a member sends once it has arrived on its transport, `tracks$`, null while waiting, and `encryptionError$` beside it. The media backend supplies exactly these two fields per member | Element Call's `MediaViewModel` is a view of it |
| track | a `MediaTrack`: one publication of a member, audio or video, with `source`, `kind`, `id` and the behaviors on it. `LocalAudioMediaTrack` and `LocalVideoMediaTrack` add the controls over our own | LiveKit `TrackPublication` and `Track`, which never leave the SDK |
| publish request | a `PublishRequest`: a source, where to capture it from and how to encode it. Given at the join in `publish`, or later to `publish` on the participation | |
| attach, detach | handing a `<video>` or `<audio>` element to a track and taking it back. The SDK owns the stream and the observers on the element | LiveKit `Track.attach` |
| transport | an `RTCTransport`: where media is exchanged, a LiveKit service url today. Carries the raw js-sdk `Transport` from the membership and, in `resolved$`, what the backend fetched to connect to it | MSC4143 "focus" |
| media backend | a `MediaBackend`: what carries the media, behind `sdk/src/media-backend/api.ts`. One per participation, chosen with `backend` in the join options; LiveKit under `sdk/src/media-backend/livekit/` is the one that exists | "SFU", a LiveKit room, a peer connection |
| Word | Meaning here | Elsewhere |
| --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------- |
| slot | an `RTCSlot`: the namespace in a room that memberships join into, `application` plus `id`. It exists while the room does, with nobody in it too. Holds `members$`, its open or closed `status$`, and `join()` | the js-sdk `MatrixRTCSession` is one per slot; the MSC4143 slot event |
| participation | an `RTCParticipation`: the span between our `join()` of a slot and our `leave()`. Holds the connection and its `state$`, `localMember$`, `remoteMembers$`, the connected transports and the data channel. Made by `slot.join()`, never reused | "session" is not used here, because in the js-sdk it means the slot |
| member | an `RTCMember`: one membership in a slot with what the SDK derives from it, `rtcBackendIdentity`, `userId`, `deviceId`, `memberId`, `displayName$`, `avatarUrl$`, `transport$` and `applicationData$`. No media. What the slot hands out in `members$` | the js-sdk `CallMembership` behind it, which never leaves the SDK; Element Call's old "matrixLivekitMember" |
| local member, remote member | `LocalRTCMember` and `RemoteRTCMember`: an `RTCMember` plus its `MemberMedia`, which only a participation can carry. The local one's tracks carry the controls. What the participation hands out in `localMember$` and `remoteMembers$` | LiveKit `LocalParticipant` and `RemoteParticipant`, which never leave the SDK |
| media | a `MemberMedia`: what a member sends once it has arrived on its transport, `tracks$`, null while waiting, and `encryptionError$` beside it. The media backend supplies exactly these two fields per member | Element Call's `MediaViewModel` is a view of it |
| track | a `MediaTrack`: one publication of a member, audio or video, with `source`, `kind`, `id` and the behaviors on it. `LocalAudioMediaTrack` and `LocalVideoMediaTrack` add the controls over our own | LiveKit `TrackPublication` and `Track`, which never leave the SDK |
| publish request | a `PublishRequest`: a source, where to capture it from and how to encode it. Given at the join in `publish`, or later to `publish` on the participation | |
| attach, detach | handing a `<video>` or `<audio>` element to a track and taking it back. The SDK owns the stream and the observers on the element | LiveKit `Track.attach` |
| transport | an `RTCTransport`: where media is exchanged, a LiveKit service url today. Carries the raw js-sdk `Transport` from the membership and, in `resolved$`, what the backend fetched to connect to it | MSC4143 "focus" |
| media backend | a `MediaBackend`: what carries the media, behind `sdk/src/media-backend/api.ts`. One per participation, chosen with `backend` in the join options; LiveKit under `sdk/src/media-backend/livekit/` is the one that exists | "SFU", a LiveKit room, a peer connection |
Names use `RTC` in capitals, as the js-sdk does: `RTCSlot`, `RTCParticipation`,
`RTCMember`, `matrixRTCMode`.
+2 -3
View File
@@ -16,7 +16,6 @@ import { combineLatest, type Observable, of, switchMap } from "rxjs";
import {
type AudioMediaTrack,
createRTCSlot,
createSdkMatrixBackend,
E2eeType,
type LocalRTCMember,
MatrixRTCMode,
@@ -71,12 +70,12 @@ async function start(
const room = await joinRoom(client, roomIdOrAlias);
const scope = new ObservableScope();
const slot = createRTCSlot(scope, createSdkMatrixBackend(client, room), {
const slot = createRTCSlot(scope, client, room, {
encryptionSystem: { kind: E2eeType.PER_PARTICIPANT },
matrixRTCMode: MatrixRTCMode.Compatibility,
});
const rtcParticipation = slot.join({
publish: [{ source: "microphone" }, { source: "camera" }],
matrixRTCMode: MatrixRTCMode.Compatibility,
});
rtcParticipation.state$.pipe(scope.bind()).subscribe((state) => {
status.textContent =
-5
View File
@@ -57,11 +57,6 @@ export * from "./src/media-api";
export * from "./src/media-backend/api";
export { trackBySource$, type TrackOfSource } from "./src/utils/tracks";
export { createRTCSlot } from "./src/RTCSlot";
export {
createSdkMatrixBackend,
type SdkMatrixBackend,
withEmitter,
} from "./src/SdkMatrixBackend";
export {
createLivekitBackend,
type LivekitBackendOptions,
+53 -3
View File
@@ -24,7 +24,7 @@ describe("createLocalMedia", () => {
]);
expect(getUserMedia).toHaveBeenCalledExactlyOnceWith({
audio: { deviceId: "mic" },
audio: { deviceId: { exact: "mic" } },
video: { width: 640, height: 480, frameRate: undefined },
});
expect(tracks.map((t) => t.source)).toEqual(["microphone", "camera"]);
@@ -124,7 +124,7 @@ describe("createLocalMedia", () => {
await camera.setDevice("other");
expect(getUserMedia).toHaveBeenLastCalledWith({
video: { deviceId: "other" },
video: { deviceId: { exact: "other" } },
});
expect(first.stop).toHaveBeenCalled();
expect(processor.restart).toHaveBeenCalledOnce();
@@ -133,7 +133,53 @@ describe("createLocalMedia", () => {
expect(input).toBe(vi.mocked(processor.init).mock.calls[0][1]);
expect(tracksOn(element)).toEqual([processor.output]);
expect(media.takeTracks().get("camera")?.constraints).toEqual({
deviceId: "other",
deviceId: { exact: "other" },
});
});
it("opens the browser's choice where the device asked for is gone", async () => {
const getUserMedia = vi.mocked(fakeGetUserMedia());
getUserMedia.mockRejectedValueOnce(overconstrained());
const media = createLocalMedia(getUserMedia);
const tracks = await media.capture([
{ source: "microphone", deviceId: "unplugged" },
{ source: "camera", deviceId: "cam" },
]);
expect(getUserMedia).toHaveBeenLastCalledWith({ audio: {}, video: {} });
expect(tracks.map((t) => t.source)).toEqual(["microphone", "camera"]);
});
it("keeps the camera it has where another cannot be opened", async () => {
const getUserMedia = vi.mocked(fakeGetUserMedia());
const media = createLocalMedia(getUserMedia);
const [camera] = await media.capture([
{ source: "camera", deviceId: "cam" },
]);
const first = trackBehind(camera);
getUserMedia.mockRejectedValueOnce(overconstrained());
await expect(camera.setDevice("unplugged")).rejects.toThrow();
expect(first.stop).not.toHaveBeenCalled();
expect(media.takeTracks().get("camera")).toMatchObject({
track: first,
constraints: { deviceId: { exact: "cam" } },
});
});
it("lets go of the device it was opened on to face the other way", async () => {
const getUserMedia = fakeGetUserMedia({ facingMode: "user" });
const media = createLocalMedia(getUserMedia);
const [camera] = (await media.capture([
{ source: "camera", deviceId: "front" },
])) as [LocalVideoMediaTrack];
await camera.switchFacingMode();
expect(getUserMedia).toHaveBeenLastCalledWith({
video: { facingMode: "environment" },
});
});
@@ -227,6 +273,10 @@ function fakeGetUserMedia(videoSettings?: MediaTrackSettings): GetUserMedia {
});
}
function overconstrained(): Error {
return Object.assign(new Error("deviceId"), { name: "OverconstrainedError" });
}
function fakeProcessor(): VideoProcessor & { output: MediaStreamTrack } {
const output = fakeTrack("video");
return {
+83 -14
View File
@@ -78,11 +78,14 @@ export function createLocalMedia(
);
bySource.set(request.source, request);
}
const constraints: MediaStreamConstraints = {};
for (const request of bySource.values())
constraints[request.source === "microphone" ? "audio" : "video"] =
trackConstraints(request);
const stream = await getUserMedia(constraints);
const { stream, constraints } = await open(getUserMedia, {
microphone: bySource.has("microphone")
? trackConstraints(bySource.get("microphone")!)
: undefined,
camera: bySource.has("camera")
? trackConstraints(bySource.get("camera")!)
: undefined,
});
const captured: CapturedTrack[] = [];
for (const track of stream.getTracks()) {
const source: Source = track.kind === "audio" ? "microphone" : "camera";
@@ -91,7 +94,7 @@ export function createLocalMedia(
const captured_ = new CapturedTrack(
source,
track,
trackConstraints(request),
constraints[source]!,
getUserMedia,
);
capturedTracks.set(source, captured_);
@@ -139,9 +142,50 @@ export function createLocalMedia(
};
}
type SourceConstraints = Partial<Record<Source, MediaTrackConstraints>>;
/**
* Opens the devices asked for. Where one of them is gone, as a remembered
* choice that was unplugged, the browser picks instead.
*
* This is needed because we use `exact` device ids: an exact request for a
* device that no longer exists fails, where a preferred one would have opened
* another device. Without the retry here a stale choice would open nothing.
*/
async function open(
getUserMedia: GetUserMedia,
constraints: SourceConstraints,
): Promise<{ stream: MediaStream; constraints: SourceConstraints }> {
const streamOf = async (wanted: SourceConstraints): Promise<MediaStream> =>
getUserMedia({
...(wanted.microphone && { audio: wanted.microphone }),
...(wanted.camera && { video: wanted.camera }),
});
try {
return { stream: await streamOf(constraints), constraints };
} catch (e) {
if (!(e instanceof Error) || e.name !== "OverconstrainedError") throw e;
const anyDevice: SourceConstraints = {
...(constraints.microphone && {
microphone: { ...constraints.microphone, deviceId: undefined },
}),
...(constraints.camera && {
camera: { ...constraints.camera, deviceId: undefined },
}),
};
return { stream: await streamOf(anyDevice), constraints: anyDevice };
}
}
/** The constraints a request means to the browser. */
function trackConstraints(request: Request): MediaTrackConstraints {
const device = request.deviceId && { deviceId: request.deviceId };
// THE FIX for "cannot switch cameras in Chrome" (#4479), first capture.
// `exact` makes the browser open this device or fail. A plain
// `deviceId: "abc"` is only a wish, and Chromium ignores the wish and
// opens its default device.
const device = request.deviceId && {
deviceId: { exact: request.deviceId },
};
if (request.source === "microphone") return { ...device, ...request.capture };
const resolution = request.capture?.resolution;
return {
@@ -184,7 +228,12 @@ class CapturedTrack {
// The device actually in use, so that a backend asked for it again
// recognises it rather than restarting the track
const { deviceId } = track.getSettings();
const constraints = { ...this.constraints, ...(deviceId && { deviceId }) };
// `exact` here too: the call reopens the camera with these constraints
// after a mute, and must get the same device again, not the default one
const constraints = {
...this.constraints,
...(deviceId && { deviceId: { exact: deviceId } }),
};
this.finish();
return { track, constraints, muted: this.muted$.value, processor };
}
@@ -245,12 +294,28 @@ class CapturedTrack {
for (const element of this.elements) play(element, this.shown());
}
private async reacquire(constraints: MediaTrackConstraints): Promise<void> {
/**
* Replace the current with the one from the selector. On fail, keep current.
*
* The old `deviceId` and `facingMode` are removed before the new selector
* is applied. This is needed because we use `exact` device ids: if the old
* exact id stayed, asking for a new facing mode could not leave the deviceId.
* "flip camera" would do nothing.
*/
private async reacquire(
selector: Pick<MediaTrackConstraints, "deviceId" | "facingMode">,
): Promise<void> {
this.live();
this.constraints = { ...this.constraints, ...constraints };
const stream = await this.getUserMedia({
[this.track.kind]: this.constraints,
});
const constraints = {
...this.constraints,
// One selects the device at a time: a facing mode cannot move a track
// off a device it is pinned to
deviceId: undefined,
facingMode: undefined,
...selector,
};
const stream = await this.getUserMedia({ [this.track.kind]: constraints });
this.constraints = constraints;
this.track.stop();
this.track = stream.getTracks()[0];
this.track.enabled = !this.muted$.value;
@@ -288,7 +353,11 @@ class CapturedTrack {
},
setDevice: async (deviceId: string): Promise<void> => {
if (this.track.getSettings().deviceId === deviceId) return;
await this.reacquire({ deviceId });
// THE FIX for "cannot switch cameras in Chrome" (#4479), switching in
// the lobby. With a plain `{ deviceId }` Chromium reopened the same
// camera, so the preview only flashed. `exact` opens the one picked,
// or rejects and leaves the current camera running.
await this.reacquire({ deviceId: { exact: deviceId } });
},
};
if (this.source === "microphone")
+13 -16
View File
@@ -22,6 +22,7 @@ import {
type ParticipationState,
type RTCMember,
type RTCParticipationOptions,
type RTCSlot,
type TransportMetadata,
} from "./api";
import { defaultSessionTimings, MatrixRTCMode } from "./config";
@@ -85,7 +86,6 @@ function member(
rtcBackendIdentity: overrides.memberId,
backendIdentities: [overrides.memberId],
deviceId: "DEV",
matrixEventId$: constant(`$${overrides.memberId}`),
transport$: constant(metadata(transport)),
applicationData$: constant({}),
displayName$: constant(overrides.userId),
@@ -111,6 +111,7 @@ interface FakeBackend {
function setup(
options: Partial<RTCParticipationOptions> = {},
slotOptions: { matrixRTCMode?: MatrixRTCMode } = {},
{
homeserverTransports = [transport],
memberships = [],
@@ -190,24 +191,19 @@ function setup(
scope,
{
client,
slot: {
roomId: room.roomId,
application: "m.call",
id: "ROOM",
status$: constant(undefined),
members$: scope.behavior(members$),
},
room,
slot: {} as RTCSlot,
jsSdkSession: mockedSession,
members$: scope.behavior(members$),
transports: { get: metadata, connected$: constant([]) },
slotOptions: { encryptionSystem: { kind: E2eeType.PER_PARTICIPANT } },
slotOptions: {
matrixRTCMode: MatrixRTCMode.Compatibility,
encryptionSystem: { kind: E2eeType.PER_PARTICIPANT },
...slotOptions,
},
logger,
},
{
publish: [],
matrixRTCMode: MatrixRTCMode.Compatibility,
backend: backendFactory,
...options,
},
{ publish: [], backend: backendFactory, ...options },
);
const states: ParticipationState[] = [];
participation.state$.subscribe((state) => states.push(state));
@@ -258,6 +254,7 @@ describe("createRTCParticipation", () => {
fociPreferred: [{ type: "livekit", url: "wss://sfu.example.org" }],
});
const { join, fake } = setup(
{},
{},
{ homeserverTransports: [], memberships: [bobOnSfu] },
);
@@ -352,7 +349,7 @@ describe("createRTCParticipation", () => {
});
compatibility.scope.end();
const matrix2 = setup({ matrixRTCMode: MatrixRTCMode.Matrix_2_0 });
const matrix2 = setup({}, { matrixRTCMode: MatrixRTCMode.Matrix_2_0 });
const { memberId } = matrix2.fake.context.ownMembershipIdentity;
expect(memberId).not.toBe(legacyMemberId);
expect(memberId).toMatch(/^[0-9a-f-]{36}$/);
+33 -53
View File
@@ -5,7 +5,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { type MatrixClient } from "matrix-js-sdk";
import { type MatrixClient, type Room } from "matrix-js-sdk";
import { type Logger } from "matrix-js-sdk/lib/logger";
import {
type MatrixRTCSession as JsSdkRTCSession,
@@ -30,17 +30,19 @@ import {
import { defaultSessionTimings, MatrixRTCMode } from "./config";
import { E2eeType } from "./encryption";
import { type Behavior } from "./reactive/Behavior";
import { type ObservableScope } from "./reactive/ObservableScope";
import { generateItems } from "./reactive/observable";
import { mapScoped } from "./utils/mapScoped";
import {
type ParticipationSlot,
type DataMessage,
type LocalRTCMember,
type ParticipationState,
type RemoteRTCMember,
type RTCMember,
type RTCParticipation,
type RTCParticipationOptions,
type RTCSlot,
type RTCSlotOptions,
} from "./api";
import {
@@ -65,34 +67,15 @@ import { createKeyRotationSuppressed$ } from "./matrixrtc/JsRtcSessionBehaviors"
import { type TransportRegistry } from "./matrixrtc/Transports";
import { participationState } from "./participationState";
/** What is needed for our own participation. */
/** What the slot lends a participation. */
export interface ParticipationDeps {
client: Pick<
MatrixClient,
// Our own membership identity
| "getUserId"
| "getDeviceId"
// Local transport discovery
| "getDomain"
| "getRTCTransports"
// LiveKit auth: `getOpenIdToken` for the JWT service flow, `getLivekitToken`
// for the client-server API flow (MSC4195), `delegateDelayedLeave` and
// `baseUrl` for the delayed leave delegation and its probe
| "getOpenIdToken"
| "getLivekitToken"
| "delegateDelayedLeave"
| "baseUrl"
// Homeserver connection: whether the client is syncing
| "getSyncState"
// `on`/`off`, to listen for:
// - `ClientEvent.Sync` (homeserver connection)
| "on"
| "off"
>;
slot: ParticipationSlot;
client: MatrixClient;
room: Room;
slot: RTCSlot;
jsSdkSession: JsSdkRTCSession;
members$: Behavior<RTCMember[]>;
transports: TransportRegistry;
slotOptions: RTCSlotOptions;
slotOptions: Pick<RTCSlotOptions, "matrixRTCMode" | "encryptionSystem">;
logger: Logger;
}
@@ -111,19 +94,16 @@ export function createRTCParticipation(
scope: ObservableScope,
{
client,
room,
slot,
jsSdkSession,
members$,
transports,
slotOptions: { encryptionSystem },
slotOptions: { matrixRTCMode, encryptionSystem },
logger: parentLogger,
}: ParticipationDeps,
options: RTCParticipationOptions,
): ParticipationInternals {
const { matrixRTCMode } = options;
const { roomId, members$ } = slot;
// The full `{application}#{id}`, which the auth service and the other
// members key the media room on
const slotId = jsSdkSession.slotId;
const logger = parentLogger.getChild("[RTCParticipation]");
const userId = client.getUserId()!;
const deviceId = client.getDeviceId()!;
@@ -142,8 +122,8 @@ export function createRTCParticipation(
};
const backend = createBackend(scope, client, options, {
roomId,
slotId,
roomId: room.roomId,
slotId: jsSdkSession.slotId,
ownMembershipIdentity,
publish: options.publish,
encryptionSystem,
@@ -164,8 +144,8 @@ export function createRTCParticipation(
// to come back later rather than wait in a half-working lobby
memberships: jsSdkSession.memberships,
ownMembershipIdentity,
roomId,
slotId,
roomId: room.roomId,
slotId: jsSdkSession.slotId,
logger,
})
: null;
@@ -195,15 +175,21 @@ export function createRTCParticipation(
timings.syncDisconnectGracePeriodMs,
),
joinMatrixRTC: (transport, delayedLeave) =>
joinJsSdkSession(roomId, jsSdkSession, ownMembershipIdentity, transport, {
encryptMedia: encryptionSystem.kind !== E2eeType.NONE,
matrixRTCMode,
sendNotificationType: options.sendNotificationType,
callIntent: options.callIntent,
applicationData: options.applicationData,
timings,
delayedLeave,
}),
joinJsSdkSession(
room.roomId,
jsSdkSession,
ownMembershipIdentity,
transport,
{
encryptMedia: encryptionSystem.kind !== E2eeType.NONE,
matrixRTCMode,
sendNotificationType: options.sendNotificationType,
callIntent: options.callIntent,
applicationData: options.applicationData,
timings,
delayedLeave,
},
),
membershipManagerError$: fromEvent(
jsSdkSession,
MatrixRTCSessionEvent.MembershipManagerError,
@@ -329,13 +315,7 @@ export function createRTCParticipation(
/** The host's backend, or LiveKit configured from the participation options. */
function createBackend(
scope: ObservableScope,
client: Pick<
MatrixClient,
// LiveKit auth: `getOpenIdToken` for the JWT service flow, `getLivekitToken`
// for the client-server API flow (MSC4195), `delegateDelayedLeave` and
// `baseUrl` for the delayed leave delegation and its probe
"getOpenIdToken" | "getLivekitToken" | "delegateDelayedLeave" | "baseUrl"
>,
client: MatrixClient,
{ backend, mediaQuality }: RTCParticipationOptions,
context: MediaBackendContext,
): MediaBackend {
+8 -132
View File
@@ -5,7 +5,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { describe, expect, it, vi } from "vitest";
import { describe, expect, it } from "vitest";
import { type MatrixClient, type Room } from "matrix-js-sdk";
import { EventType } from "matrix-js-sdk/lib/@types/event";
import { type MatrixEvent } from "matrix-js-sdk/lib/models/event";
@@ -13,26 +13,15 @@ import {
type RoomState,
RoomStateEvent,
} from "matrix-js-sdk/lib/models/room-state";
import { RoomStickyEventsEvent } from "matrix-js-sdk/lib/models/room-sticky-events";
import {
MatrixRTCSessionEvent,
type MatrixRTCSessionEventHandlerMap,
} from "matrix-js-sdk/lib/matrixrtc";
import { createRTCSlot } from "./RTCSlot";
import { type SdkMatrixBackend } from "./SdkMatrixBackend";
import { ObservableScope } from "./reactive/ObservableScope";
import { type RTCSlotOptions } from "./api";
import { MatrixRTCMode } from "./config";
import { E2eeType } from "./encryption";
import {
mockEmitter,
mockRtcMembership,
MockRTCSession,
testScope,
} from "./utils/test";
import { mockEmitter, MockRTCSession, testScope } from "./utils/test";
const options: RTCSlotOptions = {
matrixRTCMode: MatrixRTCMode.Compatibility,
encryptionSystem: { kind: E2eeType.NONE },
};
@@ -45,117 +34,28 @@ function setup(): {
...mockEmitter(),
roomId: "!room:example.org",
getMembersWithMembership: (): [] => [],
// An empty room, for a session of the slot's own
getLiveTimeline: () => ({ getState: (): undefined => undefined }),
hasMembershipState: (): boolean => false,
getVersion: (): string => "11",
_unstable_getStickyEvents: (): [] => [],
} as unknown as Room;
const session = new MockRTCSession(room);
const client = {
...mockEmitter(),
getUserId: (): string => "@alice:example.org",
getDeviceId: (): string => "AAAA",
matrixRTC: { getRoomSession: (): MockRTCSession => session },
} as unknown as MatrixClient;
return { client, room, session };
}
function backendOf(client: MatrixClient, room: Room): SdkMatrixBackend {
return {
...client,
...room,
clientEmitter: client,
roomEmitter: room,
} as unknown as SdkMatrixBackend;
}
describe("createRTCSlot", () => {
describe("session", () => {
it("joins the room's call slot by default", () => {
const { client, room } = setup();
const slot = createRTCSlot(testScope(), backendOf(client, room), options);
expect(slot).toMatchObject({ application: "m.call", id: "ROOM" });
});
it("joins the slot the options name", () => {
const { client, room } = setup();
const slot = createRTCSlot(testScope(), backendOf(client, room), {
...options,
slotDescription: { application: "io.element.example", id: "" },
});
expect(slot).toMatchObject({ application: "io.element.example", id: "" });
});
it("stops its own session when the scope ends", async () => {
const { client, room } = setup();
const off = vi.spyOn(room, "off");
const scope = new ObservableScope();
createRTCSlot(scope, backendOf(client, room), options);
scope.end();
await vi.waitFor(() =>
expect(off).toHaveBeenCalledWith(
RoomStickyEventsEvent.Update,
expect.any(Function),
),
);
});
it("leaves a session it is given to its caller", () => {
const { client, room, session } = setup();
const given = session.asMockedSession();
given.stop = vi.fn();
const scope = new ObservableScope();
createRTCSlot(scope, backendOf(client, room), options, given);
scope.end();
expect(given.stop).not.toHaveBeenCalled();
});
it("has the session's statistics and sent call notifications", () => {
const { client, room, session } = setup();
const slot = createRTCSlot(
testScope(),
backendOf(client, room),
options,
session.asMockedSession(),
);
expect(slot.statistics).toBe(session.statistics);
const sent: unknown[] = [];
slot.sentCallNotification$.subscribe((n) => sent.push(n));
const notification = { event_id: "$notification" };
session.emit(
MatrixRTCSessionEvent.DidSendCallNotification,
notification as Parameters<
MatrixRTCSessionEventHandlerMap[MatrixRTCSessionEvent.DidSendCallNotification]
>[0],
);
expect(sent).toEqual([notification]);
});
});
describe("status$", () => {
it("is undefined while the room has no slot event", () => {
const { client, room, session } = setup();
const slot = createRTCSlot(
testScope(),
backendOf(client, room),
options,
session.asMockedSession(),
);
const { client, room } = setup();
const slot = createRTCSlot(testScope(), client, room, options);
expect(slot.status$.value).toBeUndefined();
});
it("follows the slot event", () => {
const { client, room, session } = setup();
session.rtcSlot = { status: "open" };
const slot = createRTCSlot(
testScope(),
backendOf(client, room),
options,
session.asMockedSession(),
);
const slot = createRTCSlot(testScope(), client, room, options);
expect(slot.status$.value).toBe("open");
session.rtcSlot = { status: "closed" };
@@ -168,28 +68,4 @@ describe("createRTCSlot", () => {
expect(slot.status$.value).toBe("closed");
});
});
describe("members$", () => {
it("keeps the member when its membership is sent again, with the new event id", () => {
const { client, room, session } = setup();
const first = mockRtcMembership("@bob:example.org", "BOB");
session.memberships = [first];
const slot = createRTCSlot(
testScope(),
backendOf(client, room),
options,
session.asMockedSession(),
);
const [bob] = slot.members$.value;
expect(bob.matrixEventId$.value).toBe(first.eventId);
const resent = mockRtcMembership("@bob:example.org", "BOB");
session.memberships = [resent];
session.emit(MatrixRTCSessionEvent.MembershipsChanged, [first], [resent]);
expect(slot.members$.value).toHaveLength(1);
expect(slot.members$.value[0]).toBe(bob);
expect(bob.matrixEventId$.value).toBe(resent.eventId);
expect(resent.eventId).not.toBe(first.eventId);
});
});
});
+74 -124
View File
@@ -5,14 +5,12 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { type MatrixClient } from "matrix-js-sdk";
import { type MatrixClient, type Room } from "matrix-js-sdk";
import { EventType } from "matrix-js-sdk/lib/@types/event";
import { logger as rootLogger } from "matrix-js-sdk/lib/logger";
import { type Logger, logger as rootLogger } from "matrix-js-sdk/lib/logger";
import {
type Statistics,
MatrixRTCSession as JsMatrixRTCSession,
MatrixRTCSessionEvent,
type IRTCNotificationContent,
type MatrixRTCSession as JsSdkRTCSession,
MatrixRTCSessionManager,
type SlotDescription,
} from "matrix-js-sdk/lib/matrixrtc";
import { type MatrixEvent } from "matrix-js-sdk/lib/models/event";
@@ -22,7 +20,6 @@ import {
filter,
fromEvent,
map,
type Observable,
of,
startWith,
switchMap,
@@ -31,7 +28,6 @@ import {
import { ObservableScope } from "./reactive/ObservableScope";
import { generateItems } from "./reactive/observable";
import {
type ParticipationSlot,
type RTCMember,
type RTCParticipation,
type RTCParticipationOptions,
@@ -54,64 +50,33 @@ import {
membershipKeys,
} from "./matrixrtc/Members";
import { createTransportRegistry } from "./matrixrtc/Transports";
import {
type createSdkMatrixBackend,
type SdkMatrixBackend,
withEmitter,
} from "./SdkMatrixBackend";
const ROOM_CALL_SLOT: SlotDescription = { application: "m.call", id: "ROOM" };
/**
* The slot runs on a matrix-js-sdk session of its own, from the SDK's copy of
* matrix-js-sdk rather than the client's, and stops it when the scope ends.
* Takes the whole `MatrixClient` rather than a slice of it, and finds the
* MatrixRTC session itself. The js-sdk is the MatrixRTC implementation today;
* when the rust-rtc crate replaces it, the SDK has to bridge the crate to the
* js-sdk client, and only the SDK knows what that bridge needs. Holding the
* client keeps that change inside the SDK.
*/
export function createRTCSlot(
scope: ObservableScope,
backend: SdkMatrixBackend,
client: MatrixClient,
room: Room,
options: RTCSlotOptions,
/**
* @deprecated A session the caller owns and stops, in place of the slot's
* own; `options.slotDescription` is then the session's. Only for the
* standalone app, which still takes its session from its client's session
* manager.
*/
externalJsSdkSession?: JsMatrixRTCSession,
): RTCSlot & {
// STOP GAP: This should be its own external machine. It is call
// specific and should not live inside the sdk/rtc-slot.
// We only put it here today, to make sure the rtcSession is fully
// hidden inside the sdk.
// Next step. disable sending call notificaitons in the sdk, write a new
// notification sender in EC directly on client + room.
sentCallNotification$: Observable<
{
event_id: string;
} & IRTCNotificationContent
>;
// STOP GAP: the same as above. Has to live here. but should become a native
// SDK propery independent of the rtcSession
statistics: Statistics;
} {
// STOP GAP: the slot is still written against a client and a room. Both
// are refactored along with the backend.
const client = withEmitter(backend, backend.clientEmitter);
const room = withEmitter(backend, backend.roomEmitter);
): RTCSlot {
const logger = rootLogger.getChild("[RTCSlot]");
const userId = client.getUserId();
const deviceId = client.getDeviceId();
const roomId = room.roomId;
if (!(userId && deviceId))
throw new MatrixRTCError("The client has to be logged in");
const { application = "m.call", id = "ROOM" } = options;
const jsSdkSession =
externalJsSdkSession ??
createJsSdkSession(
scope,
client,
room,
options.slotDescription ?? ROOM_CALL_SLOT,
);
const jsSdkSession = sessionManager(
scope,
client,
{ application, id },
logger,
).getRoomSession(room);
const memberships$ = createMemberships$(scope, jsSdkSession);
const active$ = new BehaviorSubject<ParticipationInternals | null>(null);
@@ -161,85 +126,70 @@ export function createRTCSlot(
),
);
const participationSlot: ParticipationSlot = {
roomId,
const slot: RTCSlot = {
roomId: room.roomId,
application,
id,
status$,
members$,
...jsSdkSession.slotDescription,
};
const join = (
participationOptions: RTCParticipationOptions,
): RTCParticipation => {
if (active$.value !== null)
throw new MatrixRTCError(
"Already in this slot: leave the participation before joining again",
);
const participationScope = new ObservableScope();
participationScopes.add(participationScope);
const internals = createRTCParticipation(
participationScope,
{
client,
slot: participationSlot,
jsSdkSession,
transports,
slotOptions: options,
logger,
},
participationOptions,
);
participationScope.onEnd(() => {
participationScopes.delete(participationScope);
if (active$.value === internals) active$.next(null);
});
active$.next(internals);
return internals.participation;
};
const slot = {
join,
participation$: scope.behavior(
active$.pipe(map((active) => active?.participation ?? null)),
),
...participationSlot,
join: (participationOptions: RTCParticipationOptions): RTCParticipation => {
if (active$.value !== null)
throw new MatrixRTCError(
"Already in this slot: leave the participation before joining again",
);
const participationScope = new ObservableScope();
participationScopes.add(participationScope);
const internals = createRTCParticipation(
participationScope,
{
client,
room,
slot,
jsSdkSession,
members$,
transports,
slotOptions: options,
logger,
},
participationOptions,
);
participationScope.onEnd(() => {
participationScopes.delete(participationScope);
if (active$.value === internals) active$.next(null);
});
active$.next(internals);
return internals.participation;
},
};
return {
...slot,
sentCallNotification$: fromEvent(
jsSdkSession,
MatrixRTCSessionEvent.DidSendCallNotification,
(notification: { event_id: string } & IRTCNotificationContent) =>
notification,
),
statistics: jsSdkSession.statistics,
};
}
function createJsSdkSession(
scope: ObservableScope,
client: Pick<
MatrixClient,
keyof Parameters<typeof createSdkMatrixBackend>[0]
>,
room: ConstructorParameters<typeof JsMatrixRTCSession>[1],
slotDescription: SlotDescription,
): JsMatrixRTCSession {
const session = new JsMatrixRTCSession(
// The session's type also asks for `cancelPendingEvent`,
// `_unstable_updateDelayedEvent` and `decryptEventIfNeeded`, which it
// never calls
client as ConstructorParameters<typeof JsMatrixRTCSession>[0],
room,
slotDescription,
);
scope.onEnd(() => void session.stop());
return session;
return slot;
}
function slotStatus(
session: Pick<JsMatrixRTCSession, "getRtcSlot">,
session: Pick<JsSdkRTCSession, "getRtcSlot">,
): "open" | "closed" | undefined {
return session.getRtcSlot()?.status;
}
/**
* The js-sdk keeps one session manager per slot. The client comes with the
* manager for the default slot; any other slot gets a manager of its own,
* which lives as long as the scope.
*/
function sessionManager(
scope: ObservableScope,
client: MatrixClient,
{ application, id }: SlotDescription,
logger: Logger,
): MatrixRTCSessionManager {
if (application === "m.call" && id === "ROOM") return client.matrixRTC;
const manager = new MatrixRTCSessionManager(logger, client, {
application,
id,
});
manager.start();
scope.onEnd(() => manager.stop());
return manager;
}
-85
View File
@@ -1,85 +0,0 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { describe, expect, it, vi } from "vitest";
import {
ClientEvent,
createClient,
type MatrixClient,
Room,
RoomEvent,
SyncState,
} from "matrix-js-sdk";
import { createSdkMatrixBackend, withEmitter } from "./SdkMatrixBackend";
describe("createSdkMatrixBackend", () => {
it("calls each member on the client or the room it came from", () => {
const { client, room } = setup();
const backend = createSdkMatrixBackend(client, room);
expect(backend.getUserId()).toBe("@alice:example.org");
expect(backend.roomId).toBe("!room:example.org");
expect(backend.getVersion()).toBe(room.getVersion());
});
it("keeps the client's and the room's events apart", () => {
const { client, room } = setup();
const backend = createSdkMatrixBackend(client, room);
const onClient = vi.fn();
const onRoom = vi.fn();
backend.clientEmitter.on(ClientEvent.Sync, onClient);
backend.roomEmitter.on(RoomEvent.Name, onRoom);
client.emit(ClientEvent.Sync, SyncState.Syncing, null);
expect(onClient).toHaveBeenCalledOnce();
expect(onRoom).not.toHaveBeenCalled();
room.emit(RoomEvent.Name, room);
expect(onRoom).toHaveBeenCalledOnce();
});
});
describe("withEmitter", () => {
it("is the backend with one side's events", () => {
const { client, room } = setup();
const backend = createSdkMatrixBackend(client, room);
const asRoom = withEmitter(backend, backend.roomEmitter);
const listener = vi.fn();
asRoom.on(RoomEvent.Name, listener);
room.emit(RoomEvent.Name, room);
expect(listener).toHaveBeenCalledOnce();
expect(asRoom.getUserId()).toBe("@alice:example.org");
});
it("reads the backend's getters through", () => {
const { room } = setup();
const asRoom = withEmitter(
{
get name() {
return room.name;
},
},
room,
);
room.name = "Renamed";
expect(asRoom.name).toBe("Renamed");
});
});
function setup(): { client: MatrixClient; room: Room } {
const client = createClient({
baseUrl: "https://matrix.example.org",
userId: "@alice:example.org",
deviceId: "DEVICE",
});
const room = new Room("!room:example.org", client, "@alice:example.org");
return { client, room };
}
-129
View File
@@ -1,129 +0,0 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { type MatrixClient, type Room } from "matrix-js-sdk";
/** What the SDK needs of the client. */
type SdkClientPart = Pick<
MatrixClient,
// Our own membership identity
| "getUserId"
| "getDeviceId"
// The session: memberships, delayed leave events, sticky events, call
// notifications, and the media keys sent to device
| "fetchCapabilities"
| "getCachedCapabilities"
| "sendEvent"
| "sendStateEvent"
| "_unstable_sendDelayedStateEvent"
| "_unstable_cancelScheduledDelayedEvent"
| "_unstable_restartScheduledDelayedEvent"
| "_unstable_sendScheduledDelayedEvent"
| "_unstable_sendStickyEvent"
| "_unstable_sendStickyDelayedEvent"
| "encryptAndSendToDevice"
// Local transport discovery
| "getDomain"
| "getRTCTransports"
// LiveKit auth: `getOpenIdToken` for the JWT service flow, `getLivekitToken`
// for the client-server API flow (MSC4195), `delegateDelayedLeave` and
// `baseUrl` for the delayed leave delegation and its probe
| "getOpenIdToken"
| "getLivekitToken"
| "delegateDelayedLeave"
| "baseUrl"
// Homeserver connection: whether the client is syncing
| "getSyncState"
>;
/** What the SDK needs of the room. */
type SdkRoomPart = Pick<
Room,
| "roomId"
// The members' display names and avatars
| "getMembersWithMembership"
// The session: memberships from the room's state, timeline and sticky events
| "getLiveTimeline"
| "getVersion"
| "hasMembershipState"
| "_unstable_getStickyEvents"
>;
/**
* STOP GAP: what the SDK needs of a Matrix client and room, on one object.
* Only their emitters stay apart, since both have an `on` and an `off`. It is
* to converge, along with the SDK that takes it, on whichever API fits every
* layer best.
*/
export interface SdkMatrixBackend extends SdkClientPart, SdkRoomPart {
/**
* `ClientEvent.Sync` (homeserver connection) and `ClientEvent.ToDeviceEvent`
* (the session's media keys)
*/
clientEmitter: Pick<MatrixClient, "on" | "off">;
/** The room's state, member and sticky events */
roomEmitter: Pick<Room, "on" | "off">;
}
/** The SDK's backend from a client and a room. */
export function createSdkMatrixBackend(
client: SdkClientPart & Pick<MatrixClient, "on" | "off">,
room: SdkRoomPart & Pick<Room, "on" | "off">,
): SdkMatrixBackend {
return {
getUserId: client.getUserId.bind(client),
getDeviceId: client.getDeviceId.bind(client),
fetchCapabilities: client.fetchCapabilities.bind(client),
getCachedCapabilities: client.getCachedCapabilities.bind(client),
sendEvent: client.sendEvent.bind(client),
sendStateEvent: client.sendStateEvent.bind(client),
_unstable_sendDelayedStateEvent:
client._unstable_sendDelayedStateEvent.bind(client),
_unstable_cancelScheduledDelayedEvent:
client._unstable_cancelScheduledDelayedEvent.bind(client),
_unstable_restartScheduledDelayedEvent:
client._unstable_restartScheduledDelayedEvent.bind(client),
_unstable_sendScheduledDelayedEvent:
client._unstable_sendScheduledDelayedEvent.bind(client),
_unstable_sendStickyEvent: client._unstable_sendStickyEvent.bind(client),
_unstable_sendStickyDelayedEvent:
client._unstable_sendStickyDelayedEvent.bind(client),
encryptAndSendToDevice: client.encryptAndSendToDevice.bind(client),
getDomain: client.getDomain.bind(client),
getRTCTransports: client.getRTCTransports.bind(client),
getOpenIdToken: client.getOpenIdToken.bind(client),
getLivekitToken: client.getLivekitToken.bind(client),
delegateDelayedLeave: client.delegateDelayedLeave.bind(client),
baseUrl: client.baseUrl,
getSyncState: client.getSyncState.bind(client),
roomId: room.roomId,
getMembersWithMembership: room.getMembersWithMembership.bind(room),
getLiveTimeline: room.getLiveTimeline.bind(room),
getVersion: room.getVersion.bind(room),
hasMembershipState: room.hasMembershipState.bind(room),
_unstable_getStickyEvents: room._unstable_getStickyEvents.bind(room),
clientEmitter: client,
roomEmitter: room,
};
}
/**
* STOP GAP: the backend as the client (or the room) code written against one
* still expects, with that side's `on` and `off`.
*/
export function withEmitter<
Backend extends object,
Emitter extends {
on: (...args: never[]) => unknown;
off: (...args: never[]) => unknown;
},
>(backend: Backend, emitter: Emitter): Backend & Pick<Emitter, "on" | "off"> {
return Object.defineProperties<object>(
{ on: emitter.on.bind(emitter), off: emitter.off.bind(emitter) },
Object.getOwnPropertyDescriptors(backend),
) as Backend & Pick<Emitter, "on" | "off">;
}
+7 -23
View File
@@ -14,7 +14,6 @@ Please see LICENSE in the repository root for full details.
import {
type RTCCallIntent,
type RTCNotificationType,
type SlotDescription,
type Transport,
} from "matrix-js-sdk/lib/matrixrtc";
import { type Observable } from "rxjs";
@@ -44,9 +43,13 @@ export { MatrixRTCError } from "./errors";
// Slot
export interface RTCSlotOptions {
/** The application the slot belongs to, as named in the membership. Default `m.call`. */
application?: string;
/** The application's slot in the room. Default `ROOM`. */
id?: string;
/** How memberships and keys in this slot are written. Resolved by the host. */
matrixRTCMode: MatrixRTCMode;
encryptionSystem: EncryptionSystem;
/** Which slot of the room to join. Defaults to the room's call, `m.call#ROOM`. */
slotDescription?: SlotDescription;
}
/**
@@ -78,12 +81,6 @@ export interface RTCSlot {
join(options: RTCParticipationOptions): RTCParticipation; // Maybe MediaSession,
}
/**
* The slot as its participation sees it: members, status and identity, without
* `join` and `participation$`, which belong to whoever joins.
*/
export type ParticipationSlot = Omit<RTCSlot, "join" | "participation$">;
// ---------------------------------------------------------------------------
// Participation
@@ -98,12 +95,6 @@ export interface RTCParticipationOptions {
* before the first connection exists.
*/
publish: PublishRequest[] | LocalMedia;
/**
* How our membership and keys are written: the member id and whether the
* membership is a sticky event. Resolved by the host. Others' memberships
* are read whatever their mode.
*/
matrixRTCMode: MatrixRTCMode;
/**
* LEGACY
*
@@ -175,8 +166,7 @@ export type ParticipationState =
/** The span between our `join()` of a slot and our `leave()`. */
export interface RTCParticipation {
/** Required slot information (no `join` fn and `participation`) */
readonly slot: ParticipationSlot;
readonly slot: RTCSlot;
/** Sends the leave, tears the connections down and ends the participation. */
leave(): void;
state$: Behavior<ParticipationState>;
@@ -299,12 +289,6 @@ export interface RTCMember {
deviceId: string;
/** The membership's own id; what the Matrix 2.0 identity is derived from. */
memberId: string;
/**
* The Matrix event currently carrying the membership, which events about
* it (reactions, raised hands) relate to. Changes when the membership is
* sent again while the member stays the same.
*/
matrixEventId$: Behavior<string>;
/** Which transport this member is on; undefined when the membership has none. */
transport$: Behavior<TransportMetadata | undefined>;
/** What the member's application says about itself, e.g. `intent`. */
+21 -69
View File
@@ -8,7 +8,7 @@ Please see LICENSE in the repository root for full details.
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { EventEmitter } from "events";
import { ClientEvent, type MatrixClient, SyncState } from "matrix-js-sdk";
import { ClientEvent, SyncState } from "matrix-js-sdk";
import { MembershipManagerEvent, Status } from "matrix-js-sdk/lib/matrixrtc";
import { ObservableScope } from "../reactive/ObservableScope";
@@ -20,7 +20,7 @@ import { testScope, withTestScheduler } from "../utils/test";
```
createHomeserverConnected$(
scope: ObservableScope,
client: Pick<MatrixClient, "getSyncState" | "on" | "off">,
client: NodeStyleEventEmitter & Pick<MatrixClient, "getSyncState">,
matrixRTCSession: NodeStyleEventEmitter &
Pick<MatrixRTCSession, "membershipStatus" | "probablyLeft">,
)
@@ -34,15 +34,12 @@ class MockMatrixClient extends EventEmitter {
}
public setSyncState(state: SyncState): void {
this.syncState = state;
this.emit(ClientEvent.Sync, state);
// Matrix's Sync event in createHomeserverConnected$ expects [SyncState]
this.emit(ClientEvent.Sync, [state]);
}
public getSyncState(): SyncState {
return this.syncState;
}
/** `MatrixClient`'s `on`/`off` return the client itself, which no mock is. */
public get asClient(): Pick<MatrixClient, "getSyncState" | "on" | "off"> {
return this as unknown as Pick<MatrixClient, "getSyncState" | "on" | "off">;
}
}
/**
@@ -50,7 +47,7 @@ class MockMatrixClient extends EventEmitter {
```
createHomeserverConnected$(
scope: ObservableScope,
client: Pick<MatrixClient, "getSyncState" | "on" | "off">,
client: NodeStyleEventEmitter & Pick<MatrixClient, "getSyncState">,
matrixRTCSession: NodeStyleEventEmitter &
Pick<MatrixRTCSession, "membershipStatus" | "probablyLeft">,
)
@@ -102,33 +99,18 @@ describe("createHomeserverConnected$", () => {
// easy enough to read them so I think they can stay.
// Note: gracePeriodMs is set to 0 to avoid debouncing delays in tests
it("reports syncing reason when sync state is not Syncing", () => {
const hsConnected = createHomeserverConnected$(
scope,
client.asClient,
session,
0,
);
const hsConnected = createHomeserverConnected$(scope, client, session, 0);
expect(hsConnected.disconnectReason$.value).toEqual("sync");
});
it("reports membership reason when sync is Syncing but membership is not Connected", () => {
const hsConnected = createHomeserverConnected$(
scope,
client.asClient,
session,
0,
);
const hsConnected = createHomeserverConnected$(scope, client, session, 0);
client.setSyncState(SyncState.Syncing);
expect(hsConnected.disconnectReason$.value).toEqual("membership");
});
it("reports probablyLeft reason when membership transitions to Connected but ProbablyLeft is true", () => {
const hsConnected = createHomeserverConnected$(
scope,
client.asClient,
session,
0,
);
const hsConnected = createHomeserverConnected$(scope, client, session, 0);
// Make sync loop OK
client.setSyncState(SyncState.Syncing);
// Indicate probable leave before connection
@@ -138,12 +120,7 @@ describe("createHomeserverConnected$", () => {
});
it("becomes null (connected) only when all three conditions are satisfied", () => {
const hsConnected = createHomeserverConnected$(
scope,
client.asClient,
session,
0,
);
const hsConnected = createHomeserverConnected$(scope, client, session, 0);
// 1. Sync loop connected
client.setSyncState(SyncState.Syncing);
expect(hsConnected.disconnectReason$.value).toEqual("membership"); // not yet membership connected
@@ -153,12 +130,7 @@ describe("createHomeserverConnected$", () => {
});
it("returns syncing reason when sync loop leaves Syncing", () => {
const hsConnected = createHomeserverConnected$(
scope,
client.asClient,
session,
0,
);
const hsConnected = createHomeserverConnected$(scope, client, session, 0);
// Reach connected state
client.setSyncState(SyncState.Syncing);
session.setMembershipStatus(Status.Connected);
@@ -170,12 +142,7 @@ describe("createHomeserverConnected$", () => {
});
it("returns membershipConnected reason when membership status becomes disconnected", () => {
const hsConnected = createHomeserverConnected$(
scope,
client.asClient,
session,
0,
);
const hsConnected = createHomeserverConnected$(scope, client, session, 0);
client.setSyncState(SyncState.Syncing);
session.setMembershipStatus(Status.Connected);
expect(hsConnected.disconnectReason$.value).toEqual(null);
@@ -185,12 +152,7 @@ describe("createHomeserverConnected$", () => {
});
it("returns certainlyConnected reason when ProbablyLeft is emitted", () => {
const hsConnected = createHomeserverConnected$(
scope,
client.asClient,
session,
0,
);
const hsConnected = createHomeserverConnected$(scope, client, session, 0);
client.setSyncState(SyncState.Syncing);
session.setMembershipStatus(Status.Connected);
expect(hsConnected.disconnectReason$.value).toEqual(null);
@@ -200,12 +162,7 @@ describe("createHomeserverConnected$", () => {
});
it("recovers to null (connected) if ProbablyLeft becomes false again while other conditions remain true", () => {
const hsConnected = createHomeserverConnected$(
scope,
client.asClient,
session,
0,
);
const hsConnected = createHomeserverConnected$(scope, client, session, 0);
client.setSyncState(SyncState.Syncing);
session.setMembershipStatus(Status.Connected);
expect(hsConnected.disconnectReason$.value).toEqual(null);
@@ -219,12 +176,7 @@ describe("createHomeserverConnected$", () => {
});
it("composite sequence reflects each individual failure reason", () => {
const hsConnected = createHomeserverConnected$(
scope,
client.asClient,
session,
0,
);
const hsConnected = createHomeserverConnected$(scope, client, session, 0);
// Initially: sync error + membership disconnected → syncing wins (highest priority)
expect(hsConnected.disconnectReason$.value).toEqual("sync");
@@ -273,7 +225,7 @@ describe("createHomeserverConnected$ - disconnectReason$ reason values", () => {
it("is null when all three conditions are satisfied", () => {
const { disconnectReason$ } = createHomeserverConnected$(
scope,
client.asClient,
client,
session,
0,
);
@@ -285,7 +237,7 @@ describe("createHomeserverConnected$ - disconnectReason$ reason values", () => {
it("reports syncing when sync loop is not Syncing", () => {
const { disconnectReason$ } = createHomeserverConnected$(
scope,
client.asClient,
client,
session,
0,
);
@@ -296,7 +248,7 @@ describe("createHomeserverConnected$ - disconnectReason$ reason values", () => {
it("reports membershipConnected when sync is fine but membership is not Connected", () => {
const { disconnectReason$ } = createHomeserverConnected$(
scope,
client.asClient,
client,
session,
0,
);
@@ -308,7 +260,7 @@ describe("createHomeserverConnected$ - disconnectReason$ reason values", () => {
it("reports certainlyConnected when probablyLeft is true", () => {
const { disconnectReason$ } = createHomeserverConnected$(
scope,
client.asClient,
client,
session,
0,
);
@@ -321,7 +273,7 @@ describe("createHomeserverConnected$ - disconnectReason$ reason values", () => {
it("prioritises syncing over membershipConnected when both fail", () => {
const { disconnectReason$ } = createHomeserverConnected$(
scope,
client.asClient,
client,
session,
0,
);
@@ -332,7 +284,7 @@ describe("createHomeserverConnected$ - disconnectReason$ reason values", () => {
it("updates reason as conditions change", () => {
const { disconnectReason$ } = createHomeserverConnected$(
scope,
client.asClient,
client,
session,
0,
);
@@ -377,7 +329,7 @@ describe("createHomeserverConnected$ - Grace Period", () => {
});
const hsConnected = createHomeserverConnected$(
testScope(),
client.asClient,
client,
session,
GRACE_PERIOD,
);
+6 -15
View File
@@ -17,6 +17,7 @@ import {
startWith,
map,
tap,
type Observable,
distinctUntilChanged,
switchMap,
of,
@@ -60,15 +61,7 @@ export interface HomeserverConnected {
*/
export function createHomeserverConnected$(
scope: ObservableScope,
client: Pick<
MatrixClient,
// Whether the client is syncing
| "getSyncState"
// `on`/`off`, to listen for:
// - `ClientEvent.Sync`
| "on"
| "off"
>,
client: NodeStyleEventEmitter & Pick<MatrixClient, "getSyncState">,
matrixRTCSession: NodeStyleEventEmitter &
Pick<MatrixRTCSession, "membershipStatus" | "probablyLeft">,
gracePeriodMs?: number,
@@ -78,13 +71,11 @@ export function createHomeserverConnected$(
const graceMs =
gracePeriodMs ?? defaultSessionTimings.syncDisconnectGracePeriodMs;
const syncing$ = fromEvent(
client,
ClientEvent.Sync,
(state: SyncState) => state,
const syncing$ = (
fromEvent(client, ClientEvent.Sync) as Observable<[SyncState]>
).pipe(
startWith(client.getSyncState()),
map((state) => state === SyncState.Syncing),
startWith([client.getSyncState()]),
map(([state]) => state === SyncState.Syncing),
distinctUntilChanged(),
switchMap((isSyncing) => {
if (isSyncing || graceMs <= 0) {
+2 -5
View File
@@ -23,9 +23,7 @@ export type RoomMemberMap = Map<
string,
Pick<RoomMember, "userId" | "getMxcAvatarUrl" | "rawDisplayName">
>;
export function roomToMembersMap(
matrixRoom: Pick<MatrixRoom, "getMembersWithMembership">,
): RoomMemberMap {
export function roomToMembersMap(matrixRoom: MatrixRoom): RoomMemberMap {
const members = matrixRoom
.getMembersWithMembership(KnownMembership.Join)
.concat(matrixRoom.getMembersWithMembership(KnownMembership.Invite));
@@ -39,10 +37,9 @@ export function roomToMembersMap(
}, new Map());
}
/** Listens for `RoomStateEvent.Members` on the room. */
export function createRoomMembers$(
scope: ObservableScope,
matrixRoom: Pick<MatrixRoom, "getMembersWithMembership" | "on" | "off">,
matrixRoom: MatrixRoom,
): Behavior<RoomMemberMap> {
return scope.behavior(
fromEvent(matrixRoom, RoomStateEvent.Members).pipe(
-6
View File
@@ -57,12 +57,6 @@ export function createRTCMember(
userId,
deviceId,
memberId,
matrixEventId$: scope.behavior(
membership$.pipe(
map((membership) => membership.eventId),
distinctUntilChanged(),
),
),
displayName$: scope.behavior(
metadata
.createDisplayNameBehavior$(scope, userId)
+4 -1
View File
@@ -143,7 +143,10 @@ export interface LocalMediaTrack {
* from the request where the device could not be used.
*/
setEnabled(enabled: boolean): Promise<boolean>;
/** Captures from another device. Rejects for a screen share, which has none. */
/**
* Captures from another device. Rejects where that device cannot be opened,
* leaving the track on the one it had, and for a screen share, which has none.
*/
setDevice(deviceId: string): Promise<void>;
}
@@ -344,7 +344,6 @@ describe("createLivekitBackend", () => {
userId: "@bob:example.org",
deviceId: "DEV000",
memberId: legacyId,
matrixEventId$: constant("$bob"),
transport$: constant({
type: "livekit",
id: JSON.stringify(transport),
@@ -210,11 +210,7 @@ describe("createLivekitMediaTrack", () => {
await expect(track.setEnabled(false)).resolves.toBe(false);
expect(setEnabled).toHaveBeenCalledWith("microphone", false);
await track.setDevice("mic-2");
expect(switchActiveDevice).toHaveBeenCalledWith(
"audioinput",
"mic-2",
false,
);
expect(switchActiveDevice).toHaveBeenCalledWith("audioinput", "mic-2");
});
// LiveKit's media stream track is the processed one while a processor runs,
@@ -225,19 +221,23 @@ describe("createLivekitMediaTrack", () => {
])(
"leaves a processed camera alone unless its device changes: $deviceId",
async ({ deviceId, switches }) => {
let openDevice = "cam-1";
const camera = Object.assign(Object.create(LocalVideoTrack.prototype), {
source: Track.Source.Camera,
_mediaStreamTrack: {
readyState: "live",
label: "",
getSettings: () => ({ deviceId: "cam-1" }),
getSettings: () => ({ deviceId: openDevice }),
},
// What LiveKit shows as the track, with no device of its own
processor: {
processedTrack: { label: "", getSettings: () => ({}) },
},
}) as LocalVideoTrack;
const switchActiveDevice = vi.fn().mockResolvedValue(true);
const switchActiveDevice = vi.fn(async (_kind: string, id: string) => {
openDevice = id;
return Promise.resolve(true);
});
const { participant, publication, room } = fakes({
track: camera as unknown as FakeTrack,
});
@@ -253,11 +253,38 @@ describe("createLivekitMediaTrack", () => {
await track.setDevice(deviceId);
expect(switchActiveDevice.mock.calls).toEqual(
switches ? [["videoinput", deviceId, false]] : [],
switches ? [["videoinput", deviceId]] : [],
);
},
);
it("rejects where the camera did not move to the device asked for", async () => {
const camera = Object.assign(Object.create(LocalVideoTrack.prototype), {
source: Track.Source.Camera,
_mediaStreamTrack: {
readyState: "live",
label: "",
getSettings: () => ({ deviceId: "cam-1" }),
},
}) as LocalVideoTrack;
const { participant, publication, room } = fakes({
track: camera as unknown as FakeTrack,
});
const track = createLocalLivekitMediaTrack(
scope,
Object.assign(participant, {
isLocal: true,
}) as unknown as LocalParticipant,
publication as unknown as TrackPublication,
Object.assign(room, {
switchActiveDevice: vi.fn().mockResolvedValue(true),
}),
vi.fn(),
);
await expect(track.setDevice("cam-2")).rejects.toThrow("another device");
});
it("takes the facing mode of a processed camera from the camera", async () => {
const source = {
readyState: "live",
@@ -208,11 +208,27 @@ export function createLocalLivekitMediaTrack(
(await current.getDeviceId(false)) === deviceId
)
return;
// THE FIX for "cannot switch cameras in Chrome" (#4479), switching in
// the call. No third argument: LiveKit then asks for the device with
// `exact`. Passing `false` there made it a wish, which Chromium ignores
// by reopening its default device.
await room.switchActiveDevice(
base.kind === "audio" ? "audioinput" : "videoinput",
deviceId,
false,
);
// A check that the switch really happened, so that a failure is an
// error and not silence. Asked of the track again rather than taken
// from LiveKit's answer, which compares against the processed track
// too. A muted track is stopped and takes the device on turning on again
const now = publication.track;
const deviceNow =
now instanceof LocalTrack && !now.isMuted
? await now.getDeviceId(false)
: undefined;
if (deviceNow !== undefined && deviceNow !== deviceId)
throw new MatrixRTCError(
`The ${base.source} is on another device than the one asked for`,
);
},
};
if (base.kind === "audio") return { ...base, ...local };
-1
View File
@@ -276,7 +276,6 @@ export class MockRTCSession extends TypedEventEmitter<
return this.joined;
}
public slotDescription = { application: "m.call", id: "ROOM" };
public slotId = "m.call#ROOM";
/** The id of the delayed leave event; undefined until the manager has sent one. */
public delayId: string | undefined = undefined;
-42
View File
@@ -1,42 +0,0 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { expect, test } from "vitest";
import { createClient, Room } from "matrix-js-sdk";
import { withEmitter } from "@element-hq/matrixrtc-sdk";
import { createCallMatrixBackend } from "./CallMatrixBackend";
test("has the SDK's part and the call's own", () => {
const { backend, room } = setup();
expect(backend.getUserId()).toBe("@alice:example.org");
expect(backend.getJoinRule()).toBe(room.getJoinRule());
expect(backend.relations).toBe(room.relations);
});
test("reads the room's name as it is now, also as a room", () => {
const { backend, room } = setup();
const asRoom = withEmitter(backend, backend.roomEmitter);
room.name = "Renamed";
expect(backend.name).toBe("Renamed");
expect(asRoom.name).toBe("Renamed");
});
function setup(): {
backend: ReturnType<typeof createCallMatrixBackend>;
room: Room;
} {
const client = createClient({
baseUrl: "https://matrix.example.org",
userId: "@alice:example.org",
deviceId: "DEVICE",
});
const room = new Room("!room:example.org", client, "@alice:example.org");
return { backend: createCallMatrixBackend(client, room), room };
}
-77
View File
@@ -1,77 +0,0 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { type MatrixClient, type Room } from "matrix-js-sdk";
import {
createSdkMatrixBackend,
type SdkMatrixBackend,
} from "@element-hq/matrixrtc-sdk";
/** What a call needs of the client beyond the SDK's part. */
type CallClientPart = Pick<
MatrixClient,
// Reactions and raised hands: decrypting, and lowering a hand
| "decryptEventIfNeeded"
| "redactEvent"
// Our own display name and avatar
| "getUser"
// Changing them, where the host lets us (`supportsProfileChanges`)
| "setDisplayName"
| "setAvatarUrl"
| "uploadContent"
// Developer settings
| "doesServerSupportUnstableFeature"
| "getCrypto"
>;
/** What a call needs of the room beyond the SDK's part. */
type CallRoomPart = Pick<
Room,
// The header and the invite link
| "name"
| "getMxcAvatarUrl"
// Whether to offer the invite link
| "getJoinRule"
// Raised hands already in the room
| "relations"
>;
/**
* STOP GAP: everything a call needs from Matrix on one object. Its
* `clientEmitter` also carries `MatrixEventEvent.Decrypted` (reactions); its
* `roomEmitter` every room event.
*/
export interface CallMatrixBackend
extends SdkMatrixBackend, CallClientPart, CallRoomPart {}
/** A call's backend from a client and a room. */
export function createCallMatrixBackend(
client: Parameters<typeof createSdkMatrixBackend>[0] & CallClientPart,
room: Parameters<typeof createSdkMatrixBackend>[1] & CallRoomPart,
): CallMatrixBackend {
return {
...createSdkMatrixBackend(client, room),
decryptEventIfNeeded: client.decryptEventIfNeeded.bind(client),
redactEvent: client.redactEvent.bind(client),
getUser: client.getUser.bind(client),
setDisplayName: client.setDisplayName.bind(client),
setAvatarUrl: client.setAvatarUrl.bind(client),
uploadContent: client.uploadContent.bind(client),
doesServerSupportUnstableFeature:
client.doesServerSupportUnstableFeature.bind(client),
getCrypto: client.getCrypto.bind(client),
// Read through: the room is renamed while we show it
get name() {
return room.name;
},
getMxcAvatarUrl: room.getMxcAvatarUrl.bind(room),
getJoinRule: room.getJoinRule.bind(room),
get relations() {
return room.relations;
},
};
}
+2 -40
View File
@@ -142,7 +142,7 @@ interface Props {
* itself — from the widget API, or by restoring or creating a session of its
* own.
*/
client?: SuppliedClient;
client?: MatrixClient;
/**
* How Element Call finds a client when none is supplied: the standalone app
* restores or creates a session of its own. Stable, as a hook dependency.
@@ -161,37 +161,6 @@ interface Props {
loadClient?: ClientLoader;
}
/**
* What is read of a supplied client under a call, and by this provider and
* analytics. Stable methods only, so a host's client, from the host's own copy
* of matrix-js-sdk, will do.
*/
export type SuppliedClient = Pick<
MatrixClient,
// The disconnected banner, and stopping a client opened elsewhere
| "on"
| "removeListener"
| "stopClient"
// Analytics, through `window.matrixclient`
| "getCrypto"
| "isGuest"
| "getAccountDataFromServer"
| "setAccountData"
// The room's encryption system
| "getRoom"
// Avatars
| "mxcUrlToHttp"
| "getAccessToken"
// Rageshakes: who sends them, their crypto state, and asking others for theirs
| "getUserId"
| "getUser"
| "credentials"
| "deviceId"
| "secretStorage"
| "isKeyBackupKeyStored"
| "sendEvent"
>;
/**
* Restores or creates a session of Element Call's own, kept where the given
* functions keep it. Resolves to null when no one is signed in.
@@ -201,14 +170,7 @@ export type ClientLoader = (
clearSession: () => void,
) => Promise<InitResult | null>;
export const ClientProvider: FC<Props> = ({
children,
client: suppliedClient,
loadClient,
}) => {
// STOP GAP: held as a whole client, as the standalone and widget pages need
// one. A component host's client is only read through `SuppliedClient`.
const client = suppliedClient as MatrixClient | undefined;
export const ClientProvider: FC<Props> = ({ children, client, loadClient }) => {
const leaveToHome = useLeaveToHome();
const hostBridge = useHostBridge();
+22 -20
View File
@@ -15,7 +15,7 @@ import {
afterAll,
} from "vitest";
import { logger } from "matrix-js-sdk/lib/logger";
import { type Statistics } from "matrix-js-sdk/lib/matrixrtc";
import { type MatrixRTCSession } from "matrix-js-sdk/lib/matrixrtc";
import { PosthogAnalytics } from "./PosthogAnalytics";
import {
@@ -34,14 +34,16 @@ const defaultTotals = {
roomEventEncryptionKeysReceivedTotalAge: 500,
};
function createMockStatistics(overrides?: {
function createMockRtcSession(overrides?: {
counters?: Partial<typeof defaultCounters>;
totals?: Partial<typeof defaultTotals>;
}): Statistics {
}): MatrixRTCSession {
return {
counters: { ...defaultCounters, ...overrides?.counters },
totals: { ...defaultTotals, ...overrides?.totals },
} as Statistics;
statistics: {
counters: { ...defaultCounters, ...overrides?.counters },
totals: { ...defaultTotals, ...overrides?.totals },
},
} as unknown as MatrixRTCSession;
}
describe("CallEnded", () => {
@@ -63,9 +65,9 @@ describe("CallEnded", () => {
it("warns if startTime is missing when track is called", () => {
const warnSpy = vi.spyOn(logger, "warn");
const tracker = new CallEndedTracker();
const statistics = createMockStatistics();
const mockSession = createMockRtcSession();
tracker.track("test-call-id", 2, false, statistics);
tracker.track("test-call-id", 2, false, mockSession);
expect(warnSpy).toHaveBeenCalledWith(
"[PosthogEvents] Failed to send posthog callEnded event due to missing startTime",
@@ -75,11 +77,11 @@ describe("CallEnded", () => {
it("tracks event with correct properties when startTime is set", () => {
const tracker = new CallEndedTracker();
const statistics = createMockStatistics();
const mockSession = createMockRtcSession();
tracker.cacheStartCall(new Date(Date.now() - 60000));
tracker.cacheParticipantCountChanged(5);
tracker.track("test-call-id", 3, true, statistics);
tracker.track("test-call-id", 3, true, mockSession);
expect(PosthogAnalytics.instance.trackEvent).toHaveBeenCalledWith(
{
@@ -103,13 +105,13 @@ describe("CallEnded", () => {
it("tracks maxParticipantsCount correctly across multiple changes", () => {
const tracker = new CallEndedTracker();
const statistics = createMockStatistics();
const mockSession = createMockRtcSession();
tracker.cacheStartCall(new Date());
tracker.cacheParticipantCountChanged(3);
tracker.cacheParticipantCountChanged(7);
tracker.cacheParticipantCountChanged(2);
tracker.track("test-call-id", 1, false, statistics);
tracker.track("test-call-id", 1, false, mockSession);
expect(PosthogAnalytics.instance.trackEvent).toHaveBeenCalledWith(
expect.objectContaining({
@@ -121,12 +123,12 @@ describe("CallEnded", () => {
it("computes roomEventEncryptionKeysReceivedAverageAge as 0 when no keys received", () => {
const tracker = new CallEndedTracker();
const statistics = createMockStatistics({
const mockSession = createMockRtcSession({
counters: { roomEventEncryptionKeysReceived: 0 },
});
tracker.cacheStartCall(new Date());
tracker.track("test-call-id", 1, false, statistics);
tracker.track("test-call-id", 1, false, mockSession);
expect(PosthogAnalytics.instance.trackEvent).toHaveBeenCalledWith(
expect.objectContaining({
@@ -138,13 +140,13 @@ describe("CallEnded", () => {
it("computes roomEventEncryptionKeysReceivedAverageAge correctly when keys are received", () => {
const tracker = new CallEndedTracker();
const statistics = createMockStatistics({
const mockSession = createMockRtcSession({
counters: { roomEventEncryptionKeysReceived: 4 },
totals: { roomEventEncryptionKeysReceivedTotalAge: 200 },
});
tracker.cacheStartCall(new Date());
tracker.track("test-call-id", 1, false, statistics);
tracker.track("test-call-id", 1, false, mockSession);
expect(PosthogAnalytics.instance.trackEvent).toHaveBeenCalledWith(
expect.objectContaining({
@@ -156,10 +158,10 @@ describe("CallEnded", () => {
it("passes send_instantly option correctly", () => {
const tracker = new CallEndedTracker();
const statistics = createMockStatistics();
const mockSession = createMockRtcSession();
tracker.cacheStartCall(new Date());
tracker.track("test-call-id", 1, false, statistics);
tracker.track("test-call-id", 1, false, mockSession);
expect(PosthogAnalytics.instance.trackEvent).toHaveBeenCalledWith(
expect.anything(),
@@ -169,14 +171,14 @@ describe("CallEnded", () => {
it("includes per-reason reconnecting counts in CallEnded", () => {
const tracker = new CallEndedTracker();
const statistics = createMockStatistics();
const mockSession = createMockRtcSession();
tracker.cacheStartCall(new Date());
tracker.cacheReconnecting("sync");
tracker.cacheReconnecting("sync");
tracker.cacheReconnecting("livekit");
tracker.cacheReconnecting("membership");
tracker.track("test-call-id", 1, false, statistics);
tracker.track("test-call-id", 1, false, mockSession);
expect(PosthogAnalytics.instance.trackEvent).toHaveBeenCalledWith(
expect.objectContaining({
+8 -7
View File
@@ -7,7 +7,7 @@ Please see LICENSE in the repository root for full details.
import { type DisconnectReason } from "livekit-client";
import { logger } from "matrix-js-sdk/lib/logger";
import { type Statistics } from "matrix-js-sdk/lib/matrixrtc";
import { type MatrixRTCSession } from "matrix-js-sdk/lib/matrixrtc";
import {
type IPosthogEvent,
@@ -80,7 +80,7 @@ export class CallEndedTracker {
callId: string,
callParticipantsNow: number,
sendInstantly: boolean,
statistics: Statistics,
rtcSession: MatrixRTCSession,
): void {
if (this.cache.startTime) {
PosthogAnalytics.instance.trackEvent<CallEnded>(
@@ -91,13 +91,14 @@ export class CallEndedTracker {
callParticipantsOnLeave: callParticipantsNow,
callDuration: (Date.now() - this.cache.startTime.getTime()) / 1000,
roomEventEncryptionKeysSent:
statistics.counters.roomEventEncryptionKeysSent,
rtcSession.statistics.counters.roomEventEncryptionKeysSent,
roomEventEncryptionKeysReceived:
statistics.counters.roomEventEncryptionKeysReceived,
rtcSession.statistics.counters.roomEventEncryptionKeysReceived,
roomEventEncryptionKeysReceivedAverageAge:
statistics.counters.roomEventEncryptionKeysReceived > 0
? statistics.totals.roomEventEncryptionKeysReceivedTotalAge /
statistics.counters.roomEventEncryptionKeysReceived
rtcSession.statistics.counters.roomEventEncryptionKeysReceived > 0
? rtcSession.statistics.totals
.roomEventEncryptionKeysReceivedTotalAge /
rtcSession.statistics.counters.roomEventEncryptionKeysReceived
: 0,
callReconnectingCount: this.cache.reconnectingCount,
callReconnectingCountSync: this.cache.reconnectingCountByReason.sync,
+27 -35
View File
@@ -10,13 +10,13 @@ import { expect, test } from "vitest";
import { TooltipProvider } from "@vector-im/compound-web";
import { userEvent } from "@testing-library/user-event";
import { type ReactNode } from "react";
import { type MatrixClient, type Room } from "matrix-js-sdk";
import { ReactionToggleButton } from "./ReactionToggleButton";
import { ElementCallReactionEventType } from "../reactions";
import { type CallViewModel } from "../state/CallViewModel/CallViewModel";
import { getBasicCallViewModelEnvironment } from "../utils/test-viewmodel";
import { alice, local, localRtcMember } from "../utils/test-fixtures";
import { type MockRTCSession } from "../utils/test";
import { ReactionsSenderProvider } from "../reactions/useReactionsSender";
import { initializeWidget } from "../widget";
initializeWidget();
@@ -24,17 +24,18 @@ initializeWidget();
const localIdent = `${localRtcMember.userId}:${localRtcMember.deviceId}`;
function TestComponent({
matrixRoom,
client,
rtcSession,
vm,
}: {
matrixRoom: Room;
client: MatrixClient;
rtcSession: MockRTCSession;
vm: CallViewModel;
}): ReactNode {
return (
<TooltipProvider>
<ReactionsSenderProvider vm={vm} room={matrixRoom} client={client}>
<ReactionsSenderProvider
vm={vm}
rtcSession={rtcSession.asMockedSession()}
>
<ReactionToggleButton
reactionData={{
reactions$: vm.reactions$,
@@ -49,9 +50,9 @@ function TestComponent({
test("Can open menu", async () => {
const user = userEvent.setup();
const { vm, matrixRoom, client } = getBasicCallViewModelEnvironment([alice]);
const { vm, rtcSession } = getBasicCallViewModelEnvironment([alice]);
const { getByLabelText, container } = render(
<TestComponent vm={vm} matrixRoom={matrixRoom} client={client} />,
<TestComponent vm={vm} rtcSession={rtcSession} />,
);
await user.click(getByLabelText("Reactions"));
expect(container).toMatchSnapshot();
@@ -59,15 +60,15 @@ test("Can open menu", async () => {
test("Can raise hand", async () => {
const user = userEvent.setup();
const { vm, matrixRoom, client, handRaisedSubject$ } =
const { vm, rtcSession, handRaisedSubject$ } =
getBasicCallViewModelEnvironment([local, alice]);
const { getByLabelText, container } = render(
<TestComponent vm={vm} matrixRoom={matrixRoom} client={client} />,
<TestComponent vm={vm} rtcSession={rtcSession} />,
);
await user.click(getByLabelText("Reactions"));
await user.click(getByLabelText("Raise hand"));
expect(client.sendEvent).toHaveBeenCalledWith(
matrixRoom.roomId,
expect(rtcSession.room.client.sendEvent).toHaveBeenCalledWith(
rtcSession.room.roomId,
"m.reaction",
{
"m.relates_to": {
@@ -93,10 +94,10 @@ test("Can raise hand", async () => {
test("Can lower hand", async () => {
const reactionEventId = "$my-reaction-event:example.org";
const user = userEvent.setup();
const { vm, matrixRoom, client, handRaisedSubject$ } =
const { vm, rtcSession, handRaisedSubject$ } =
getBasicCallViewModelEnvironment([local, alice]);
const { getByLabelText, container } = render(
<TestComponent vm={vm} matrixRoom={matrixRoom} client={client} />,
<TestComponent vm={vm} rtcSession={rtcSession} />,
);
await user.click(getByLabelText("Reactions"));
await user.click(getByLabelText("Raise hand"));
@@ -111,8 +112,8 @@ test("Can lower hand", async () => {
});
await user.click(getByLabelText("Reactions"));
await user.click(getByLabelText("Lower hand"));
expect(client.redactEvent).toHaveBeenCalledWith(
matrixRoom.roomId,
expect(rtcSession.room.client.redactEvent).toHaveBeenCalledWith(
rtcSession.room.roomId,
reactionEventId,
);
act(() => {
@@ -124,17 +125,14 @@ test("Can lower hand", async () => {
test("Can react with emoji", async () => {
const user = userEvent.setup();
const { vm, matrixRoom, client } = getBasicCallViewModelEnvironment([
local,
alice,
]);
const { vm, rtcSession } = getBasicCallViewModelEnvironment([local, alice]);
const { getByLabelText, getByText } = render(
<TestComponent vm={vm} matrixRoom={matrixRoom} client={client} />,
<TestComponent vm={vm} rtcSession={rtcSession} />,
);
await user.click(getByLabelText("Reactions"));
await user.click(getByText("🐶"));
expect(client.sendEvent).toHaveBeenCalledWith(
matrixRoom.roomId,
expect(rtcSession.room.client.sendEvent).toHaveBeenCalledWith(
rtcSession.room.roomId,
ElementCallReactionEventType,
{
"m.relates_to": {
@@ -149,19 +147,16 @@ test("Can react with emoji", async () => {
test("Can fully expand emoji picker", async () => {
const user = userEvent.setup();
const { vm, matrixRoom, client } = getBasicCallViewModelEnvironment([
local,
alice,
]);
const { vm, rtcSession } = getBasicCallViewModelEnvironment([local, alice]);
const { getByLabelText, container, getByText } = render(
<TestComponent vm={vm} matrixRoom={matrixRoom} client={client} />,
<TestComponent vm={vm} rtcSession={rtcSession} />,
);
await user.click(getByLabelText("Reactions"));
await user.click(getByLabelText("Show more"));
expect(container).toMatchSnapshot();
await user.click(getByText("🦗"));
expect(client.sendEvent).toHaveBeenCalledWith(
matrixRoom.roomId,
expect(rtcSession.room.client.sendEvent).toHaveBeenCalledWith(
rtcSession.room.roomId,
ElementCallReactionEventType,
{
"m.relates_to": {
@@ -176,12 +171,9 @@ test("Can fully expand emoji picker", async () => {
test("Can close reaction dialog", async () => {
const user = userEvent.setup();
const { vm, matrixRoom, client } = getBasicCallViewModelEnvironment([
local,
alice,
]);
const { vm, rtcSession } = getBasicCallViewModelEnvironment([local, alice]);
const { getByLabelText, container } = render(
<TestComponent vm={vm} matrixRoom={matrixRoom} client={client} />,
<TestComponent vm={vm} rtcSession={rtcSession} />,
);
await user.click(getByLabelText("Reactions"));
await user.click(getByLabelText("Show more"));
+1 -6
View File
@@ -37,12 +37,7 @@ interface UseProfile extends ProfileLoadState {
saveProfile: ProfileSaveCallback;
}
export type ProfileClientParts = Pick<
MatrixClient,
"getUserId" | "getUser" | "setDisplayName" | "setAvatarUrl" | "uploadContent"
>;
export function useProfile(client: ProfileClientParts | undefined): UseProfile {
export function useProfile(client: MatrixClient | undefined): UseProfile {
const [{ success, loading, displayName, avatarUrl, error }, setState] =
useState<ProfileLoadState>(() => {
let user: User | undefined = undefined;
+56 -172
View File
@@ -23,130 +23,30 @@ import {
localRtcMember,
} from "../utils/test-fixtures";
import { getBasicRTCSession } from "../utils/test-viewmodel";
import { mockRtcMembership, testScope, withTestScheduler } from "../utils/test";
import { testScope, withTestScheduler } from "../utils/test";
import { ElementCallReactionEventType, ReactionSet } from ".";
afterEach(() => {
vitest.useRealTimers();
});
function raiseLocalHand(
matrixRoom: ReturnType<typeof getBasicRTCSession>["matrixRoom"],
reactionEventId: string,
localTimestamp: Date,
): void {
matrixRoom.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: matrixRoom.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: EventType.Reaction,
origin_server_ts: localTimestamp.getTime(),
content: {
"m.relates_to": { event_id: localRtcMember.eventId, key: "🖐️" },
},
}),
matrixRoom,
undefined,
false,
{} as IRoomTimelineData,
);
}
test("lowers the hand of a member who leaves", () => {
const { members$, matrixRoom, client, rtcMemberships$ } = getBasicRTCSession([
local,
alice,
]);
const reactionEventId = "$my_event_id:example.org";
const localTimestamp = new Date();
withTestScheduler(({ schedule, expectObservable }) => {
renderHook(() => {
const { raisedHands$ } = new ReactionsReader(
testScope(),
members$,
matrixRoom,
client,
);
schedule("abc", {
a: () => {},
b: () => raiseLocalHand(matrixRoom, reactionEventId, localTimestamp),
c: () => rtcMemberships$.next([aliceRtcMember]),
});
expectObservable(raisedHands$).toBe("abc", {
a: {},
b: {
[`${localRtcMember.userId}:${localRtcMember.deviceId}`]: {
reactionEventId,
membershipEventId: localRtcMember.eventId,
time: localTimestamp,
},
},
c: {},
});
});
});
});
test("lowers the hand of a member whose membership is sent again", () => {
const { members$, matrixRoom, client, rtcMemberships$ } = getBasicRTCSession([
local,
alice,
]);
const reactionEventId = "$my_event_id:example.org";
const localTimestamp = new Date();
const resent = mockRtcMembership(
localRtcMember.userId,
localRtcMember.deviceId,
);
withTestScheduler(({ schedule, expectObservable }) => {
renderHook(() => {
const { raisedHands$ } = new ReactionsReader(
testScope(),
members$,
matrixRoom,
client,
);
schedule("abc", {
a: () => {},
b: () => raiseLocalHand(matrixRoom, reactionEventId, localTimestamp),
c: () => rtcMemberships$.next([resent, aliceRtcMember]),
});
expectObservable(raisedHands$).toBe("abc", {
a: {},
b: {
[`${localRtcMember.userId}:${localRtcMember.deviceId}`]: {
reactionEventId,
membershipEventId: localRtcMember.eventId,
time: localTimestamp,
},
},
c: {},
});
});
});
});
test("handles a hand raised reaction", () => {
const { members$, matrixRoom, client } = getBasicRTCSession([local, alice]);
const { rtcSession } = getBasicRTCSession([local, alice]);
const reactionEventId = "$my_event_id:example.org";
const localTimestamp = new Date();
withTestScheduler(({ schedule, expectObservable }) => {
renderHook(() => {
const { raisedHands$ } = new ReactionsReader(
testScope(),
members$,
matrixRoom,
client,
rtcSession.asMockedSession(),
);
schedule("ab", {
a: () => {},
b: () => {
matrixRoom.emit(
rtcSession.room.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: matrixRoom.roomId,
room_id: rtcSession.room.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: EventType.Reaction,
@@ -158,7 +58,7 @@ test("handles a hand raised reaction", () => {
},
},
}),
matrixRoom,
rtcSession.room,
undefined,
false,
{} as IRoomTimelineData,
@@ -180,24 +80,22 @@ test("handles a hand raised reaction", () => {
});
test("handles a redaction", () => {
const { members$, matrixRoom, client } = getBasicRTCSession([local, alice]);
const { rtcSession } = getBasicRTCSession([local, alice]);
const reactionEventId = "$my_event_id:example.org";
const localTimestamp = new Date();
withTestScheduler(({ schedule, expectObservable }) => {
renderHook(() => {
const { raisedHands$ } = new ReactionsReader(
testScope(),
members$,
matrixRoom,
client,
rtcSession.asMockedSession(),
);
schedule("abc", {
a: () => {},
b: () => {
matrixRoom.emit(
rtcSession.room.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: matrixRoom.roomId,
room_id: rtcSession.room.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: EventType.Reaction,
@@ -209,23 +107,23 @@ test("handles a redaction", () => {
},
},
}),
matrixRoom,
rtcSession.room,
undefined,
false,
{} as IRoomTimelineData,
);
},
c: () => {
matrixRoom.emit(
rtcSession.room.emit(
MatrixRoomEvent.Redaction,
new MatrixEvent({
room_id: matrixRoom.roomId,
room_id: rtcSession.room.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: EventType.RoomRedaction,
redacts: reactionEventId,
}),
matrixRoom,
rtcSession.room,
undefined,
);
},
@@ -246,22 +144,20 @@ test("handles a redaction", () => {
});
test("handles waiting for event decryption", () => {
const { members$, matrixRoom, client } = getBasicRTCSession([local, alice]);
const { rtcSession } = getBasicRTCSession([local, alice]);
const reactionEventId = "$my_event_id:example.org";
const localTimestamp = new Date();
withTestScheduler(({ schedule, expectObservable }) => {
renderHook(() => {
const { raisedHands$ } = new ReactionsReader(
testScope(),
members$,
matrixRoom,
client,
rtcSession.asMockedSession(),
);
schedule("abc", {
a: () => {},
b: () => {
const encryptedEvent = new MatrixEvent({
room_id: matrixRoom.roomId,
room_id: rtcSession.room.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: EventType.Reaction,
@@ -275,20 +171,20 @@ test("handles waiting for event decryption", () => {
});
// Should ignore encrypted events that are still encrypting
encryptedEvent["decryptionPromise"] = Promise.resolve();
matrixRoom.emit(
rtcSession.room.emit(
MatrixRoomEvent.Timeline,
encryptedEvent,
matrixRoom,
rtcSession.room,
undefined,
false,
{} as IRoomTimelineData,
);
},
c: () => {
client.emit(
rtcSession.room.client.emit(
MatrixEventEvent.Decrypted,
new MatrixEvent({
room_id: matrixRoom.roomId,
room_id: rtcSession.room.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: EventType.Reaction,
@@ -318,24 +214,22 @@ test("handles waiting for event decryption", () => {
});
test("hands rejecting events without a proper membership", () => {
const { members$, matrixRoom, client } = getBasicRTCSession([local, alice]);
const { rtcSession } = getBasicRTCSession([local, alice]);
const reactionEventId = "$my_event_id:example.org";
const localTimestamp = new Date();
withTestScheduler(({ schedule, expectObservable }) => {
renderHook(() => {
const { raisedHands$ } = new ReactionsReader(
testScope(),
members$,
matrixRoom,
client,
rtcSession.asMockedSession(),
);
schedule("ab", {
a: () => {},
b: () => {
matrixRoom.emit(
rtcSession.room.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: matrixRoom.roomId,
room_id: rtcSession.room.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: EventType.Reaction,
@@ -347,7 +241,7 @@ test("hands rejecting events without a proper membership", () => {
},
},
}),
matrixRoom,
rtcSession.room,
undefined,
false,
{} as IRoomTimelineData,
@@ -362,7 +256,7 @@ test("hands rejecting events without a proper membership", () => {
});
test("handles a reaction", () => {
const { members$, matrixRoom, client } = getBasicRTCSession([local, alice]);
const { rtcSession } = getBasicRTCSession([local, alice]);
const reactionEventId = "$my_event_id:example.org";
const reaction = ReactionSet[1];
@@ -373,17 +267,15 @@ test("handles a reaction", () => {
renderHook(() => {
const { reactions$ } = new ReactionsReader(
testScope(),
members$,
matrixRoom,
client,
rtcSession.asMockedSession(),
);
schedule(`abc`, {
a: () => {},
b: () => {
matrixRoom.emit(
rtcSession.room.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: matrixRoom.roomId,
room_id: rtcSession.room.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: ElementCallReactionEventType,
@@ -395,7 +287,7 @@ test("handles a reaction", () => {
},
},
}),
matrixRoom,
rtcSession.room,
undefined,
false,
{} as IRoomTimelineData,
@@ -424,11 +316,7 @@ test("handles a reaction", () => {
});
test("ignores bad reaction events", () => {
const {
members$,
matrixRoom: room,
client,
} = getBasicRTCSession([local, alice]);
const { rtcSession } = getBasicRTCSession([local, alice]);
const reactionEventId = "$my_event_id:example.org";
const reaction = ReactionSet[1];
@@ -438,33 +326,31 @@ test("ignores bad reaction events", () => {
renderHook(() => {
const { reactions$ } = new ReactionsReader(
testScope(),
members$,
room,
client,
rtcSession.asMockedSession(),
);
schedule("ab", {
a: () => {},
b: () => {
// Missing content
room.emit(
rtcSession.room.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: room.roomId,
room_id: rtcSession.room.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: ElementCallReactionEventType,
content: {},
}),
room,
rtcSession.room,
undefined,
false,
{} as IRoomTimelineData,
);
// Wrong relates event
room.emit(
rtcSession.room.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: room.roomId,
room_id: rtcSession.room.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: ElementCallReactionEventType,
@@ -476,16 +362,16 @@ test("ignores bad reaction events", () => {
},
},
}),
room,
rtcSession.room,
undefined,
false,
{} as IRoomTimelineData,
);
// Wrong rtc member event
room.emit(
rtcSession.room.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: room.roomId,
room_id: rtcSession.room.roomId,
event_id: reactionEventId,
sender: aliceRtcMember.userId,
type: ElementCallReactionEventType,
@@ -497,16 +383,16 @@ test("ignores bad reaction events", () => {
},
},
}),
room,
rtcSession.room,
undefined,
false,
{} as IRoomTimelineData,
);
// No emoji
room.emit(
rtcSession.room.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: room.roomId,
room_id: rtcSession.room.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: ElementCallReactionEventType,
@@ -517,16 +403,16 @@ test("ignores bad reaction events", () => {
},
},
}),
room,
rtcSession.room,
undefined,
false,
{} as IRoomTimelineData,
);
// Invalid emoji
room.emit(
rtcSession.room.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: room.roomId,
room_id: rtcSession.room.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: ElementCallReactionEventType,
@@ -538,7 +424,7 @@ test("ignores bad reaction events", () => {
},
},
}),
room,
rtcSession.room,
undefined,
false,
{} as IRoomTimelineData,
@@ -553,7 +439,7 @@ test("ignores bad reaction events", () => {
});
test("that reactions cannot be spammed", () => {
const { members$, matrixRoom, client } = getBasicRTCSession([local, alice]);
const { rtcSession } = getBasicRTCSession([local, alice]);
const reactionEventId = "$my_event_id:example.org";
const reactionA = ReactionSet[1];
const reactionB = ReactionSet[2];
@@ -565,17 +451,15 @@ test("that reactions cannot be spammed", () => {
renderHook(() => {
const { reactions$ } = new ReactionsReader(
testScope(),
members$,
matrixRoom,
client,
rtcSession.asMockedSession(),
);
schedule("abcd", {
a: () => {},
b: () => {
matrixRoom.emit(
rtcSession.room.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: matrixRoom.roomId,
room_id: rtcSession.room.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: ElementCallReactionEventType,
@@ -587,17 +471,17 @@ test("that reactions cannot be spammed", () => {
},
},
}),
matrixRoom,
rtcSession.room,
undefined,
false,
{} as IRoomTimelineData,
);
},
c: () => {
matrixRoom.emit(
rtcSession.room.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: matrixRoom.roomId,
room_id: rtcSession.room.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: ElementCallReactionEventType,
@@ -609,7 +493,7 @@ test("that reactions cannot be spammed", () => {
},
},
}),
matrixRoom,
rtcSession.room,
undefined,
false,
{} as IRoomTimelineData,
+66 -87
View File
@@ -5,28 +5,20 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { logger } from "matrix-js-sdk/lib/logger";
import {
type MatrixClient,
type MatrixEvent,
MatrixEventEvent,
type Room,
} from "matrix-js-sdk";
type CallMembership,
MatrixRTCSessionEvent,
type MatrixRTCSession,
} from "matrix-js-sdk/lib/matrixrtc";
import { logger } from "matrix-js-sdk/lib/logger";
import { type MatrixEvent, MatrixEventEvent } from "matrix-js-sdk";
import { type ReactionEventContent } from "matrix-js-sdk/lib/types";
import {
RelationType,
EventType,
RoomEvent as MatrixRoomEvent,
} from "matrix-js-sdk";
import {
BehaviorSubject,
combineLatest,
delay,
map,
of,
switchMap,
} from "rxjs";
import { type RTCMember } from "@element-hq/matrixrtc-sdk";
import { BehaviorSubject, delay } from "rxjs";
import {
ElementCallReactionEventType,
@@ -37,20 +29,13 @@ import {
type ReactionInfo,
} from ".";
import { type ObservableScope } from "../state/ObservableScope";
import { type Behavior } from "../state/Behavior";
export const REACTION_ACTIVE_TIME_MS = 3000;
/** A member's membership event, which its reactions relate to. */
interface MembershipEvent {
userId: string;
deviceId: string;
eventId: string;
}
/**
* Listens for reactions to the slot members' memberships and populates
* subjects for consumption by the CallViewModel.
* Listens for reactions from a RTCSession and populates subjects
* for consumption by the CallViewModel.
* @param rtcSession
*/
export class ReactionsReader {
private readonly raisedHandsSubject$ = new BehaviorSubject<
@@ -70,33 +55,9 @@ export class ReactionsReader {
*/
public readonly reactions$ = this.reactionsSubject$.asObservable();
private memberships: MembershipEvent[] = [];
public constructor(
private readonly scope: ObservableScope,
members$: Behavior<RTCMember[]>,
private readonly room: Pick<
Room,
// To ignore events from other rooms
| "roomId"
// The raised hands already in the room, as reactions to memberships
| "relations"
// `on`/`off`, to listen for:
// - `RoomEvent.Timeline` (new reactions)
// - `RoomEvent.Redaction` (lowered hands)
// - `RoomEvent.LocalEchoUpdated` (the real id of our own reactions)
| "on"
| "off"
>,
private readonly client: Pick<
MatrixClient,
// Reactions that arrived encrypted
| "decryptEventIfNeeded"
// `on`/`off`, to listen for:
// - `MatrixEventEvent.Decrypted` (reactions once decrypted)
| "on"
| "off"
>,
private readonly rtcSession: MatrixRTCSession,
) {
// Hide reactions after a given time.
this.reactionsSubject$
@@ -115,44 +76,62 @@ export class ReactionsReader {
// TODO: Convert this class to the functional reactive style and get rid of
// all this manual setup and teardown for event listeners
this.room.on(MatrixRoomEvent.Timeline, this.handleReactionEvent);
this.rtcSession.room.on(MatrixRoomEvent.Timeline, this.handleReactionEvent);
this.scope.onEnd(() =>
this.room.off(MatrixRoomEvent.Timeline, this.handleReactionEvent),
this.rtcSession.room.off(
MatrixRoomEvent.Timeline,
this.handleReactionEvent,
),
);
this.room.on(MatrixRoomEvent.Redaction, this.handleReactionEvent);
this.rtcSession.room.on(
MatrixRoomEvent.Redaction,
this.handleReactionEvent,
);
this.scope.onEnd(() =>
this.room.off(MatrixRoomEvent.Redaction, this.handleReactionEvent),
this.rtcSession.room.off(
MatrixRoomEvent.Redaction,
this.handleReactionEvent,
),
);
this.client.on(MatrixEventEvent.Decrypted, this.handleReactionEvent);
this.rtcSession.room.client.on(
MatrixEventEvent.Decrypted,
this.handleReactionEvent,
);
this.scope.onEnd(() =>
this.client.off(MatrixEventEvent.Decrypted, this.handleReactionEvent),
this.rtcSession.room.client.off(
MatrixEventEvent.Decrypted,
this.handleReactionEvent,
),
);
// We listen for a local echo to get the real event ID, as timeline events
// may still be sending.
this.room.on(MatrixRoomEvent.LocalEchoUpdated, this.handleReactionEvent);
this.rtcSession.room.on(
MatrixRoomEvent.LocalEchoUpdated,
this.handleReactionEvent,
);
this.scope.onEnd(() =>
this.room.off(MatrixRoomEvent.LocalEchoUpdated, this.handleReactionEvent),
this.rtcSession.room.off(
MatrixRoomEvent.LocalEchoUpdated,
this.handleReactionEvent,
),
);
members$
.pipe(
switchMap((members) =>
members.length === 0
? of([])
: combineLatest(
members.map(({ userId, deviceId, matrixEventId$ }) =>
matrixEventId$.pipe(
map((eventId) => ({ userId, deviceId, eventId })),
),
),
),
),
this.scope.bind(),
)
.subscribe(this.onMembershipsChanged);
this.rtcSession.on(
MatrixRTCSessionEvent.MembershipsChanged,
this.onMembershipsChanged,
);
this.scope.onEnd(() =>
this.rtcSession.off(
MatrixRTCSessionEvent.MembershipsChanged,
this.onMembershipsChanged,
),
);
// Run this once to ensure we have fetched the state from the call.
this.onMembershipsChanged([]);
}
/**
@@ -166,7 +145,7 @@ export class ReactionsReader {
membershipEventId: string,
expectedSender: string,
): MatrixEvent | undefined {
const relations = this.room.relations.getChildEventsForEvent(
const relations = this.rtcSession.room.relations.getChildEventsForEvent(
membershipEventId,
RelationType.Annotation,
EventType.Reaction,
@@ -183,23 +162,22 @@ export class ReactionsReader {
/**
* Will remove any hand raises by old members, and look for any
* existing hand raises by new members.
* @param memberships The membership events of everyone in the call now.
* @param oldMemberships Any members who have left the call.
*/
private onMembershipsChanged = (memberships: MembershipEvent[]): void => {
this.memberships = memberships;
private onMembershipsChanged = (oldMemberships: CallMembership[]): void => {
// Remove any raised hands for users no longer joined to the call.
const present = new Set(
memberships.map((m) => `${m.userId}:${m.deviceId}`),
);
for (const identifier of Object.keys(this.raisedHandsSubject$.value).filter(
(rhId) => !present.has(rhId),
(rhId) => oldMemberships.find((u) => u.userId == rhId),
)) {
this.removeRaisedHand(identifier);
}
// For each member in the call, check to see if a reaction has
// been raised and adjust.
for (const m of memberships) {
for (const m of this.rtcSession.memberships) {
if (!m.userId || !m.eventId) {
continue;
}
const identifier = `${m.userId}:${m.deviceId}`;
if (
this.raisedHandsSubject$.value[identifier] &&
@@ -257,8 +235,9 @@ export class ReactionsReader {
* @param event The incoming matrix event, which may or may not be decrypted.
*/
private handleReactionEvent = (event: MatrixEvent): void => {
const room = this.rtcSession.room;
// Decrypted events might come from a different room
if (event.getRoomId() !== this.room.roomId) return;
if (event.getRoomId() !== room.roomId) return;
// Skip any events that are still sending.
if (event.isSending()) return;
@@ -267,7 +246,7 @@ export class ReactionsReader {
// Skip any event without a sender or event ID.
if (!sender || !reactionEventId) return;
this.client
room.client
.decryptEventIfNeeded(event)
.catch((e) => logger.warn(`Failed to decrypt ${event.getId()}`, e));
if (event.isBeingDecrypted() || event.isDecryptionFailure()) return;
@@ -276,7 +255,7 @@ export class ReactionsReader {
const content: ECallReactionEventContent = event.getContent();
const membershipEventId = content?.["m.relates_to"]?.event_id;
const membershipEvent = this.memberships.find(
const membershipEvent = this.rtcSession.memberships.find(
(e) => e.eventId === membershipEventId && e.userId === sender,
);
// Check to see if this reaction was made to a membership event (and the
@@ -336,7 +315,7 @@ export class ReactionsReader {
// Check to see if this reaction was made to a membership event (and the
// sender of the reaction matches the membership)
const membershipEvent = this.memberships.find(
const membershipEvent = this.rtcSession.memberships.find(
(e) => e.eventId === membershipEventId && e.userId === sender,
);
if (!membershipEvent) {
+38 -40
View File
@@ -5,12 +5,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import {
EventType,
type MatrixClient,
RelationType,
type Room,
} from "matrix-js-sdk";
import { EventType, RelationType } from "matrix-js-sdk";
import {
createContext,
use,
@@ -19,15 +14,14 @@ import {
useMemo,
type JSX,
} from "react";
import { type MatrixRTCSession } from "matrix-js-sdk/lib/matrixrtc";
import { logger } from "matrix-js-sdk/lib/logger";
import { useMatrixRTCSessionMemberships } from "../useMatrixRTCSessionMemberships";
import { useClientState } from "../ClientContext";
import { ElementCallReactionEventType, type ReactionOption } from ".";
import { type CallViewModel } from "../state/CallViewModel/CallViewModel";
import { useBehavior } from "../useBehavior";
import { constant } from "../state/Behavior";
const noMembershipEvent$ = constant(undefined);
interface ReactionsSenderContextType {
supportsReactions: boolean;
@@ -52,34 +46,28 @@ export const useReactionsSender = (): ReactionsSenderContextType => {
*/
export const ReactionsSenderProvider = ({
children,
room,
client,
rtcSession,
vm,
}: {
children: ReactNode;
/** The room the reactions are sent in. */
room: Pick<Room, "roomId">;
client: Pick<
MatrixClient,
// Our own identity, which reactions and raised hands are keyed by
| "getUserId"
| "getDeviceId"
// Sending reactions and raising a hand
| "sendEvent"
// Lowering a hand
| "redactEvent"
>;
/** Our own membership, which the reactions relate to, and the current reactions. */
rtcSession: MatrixRTCSession;
vm: CallViewModel;
}): JSX.Element => {
const memberships = useMatrixRTCSessionMemberships(rtcSession);
const clientState = useClientState();
const supportsReactions =
clientState?.state === "valid" && clientState.supportedFeatures.reactions;
const myMembershipIdentifier = `${client.getUserId()}:${client.getDeviceId()}`;
const room = rtcSession.room;
const myUserId = room.client.getUserId();
const myDeviceId = room.client.getDeviceId();
const myMembershipIdentifier = `${myUserId}:${myDeviceId}`;
const localMember = useBehavior(vm.localMember$);
const myMembershipEvent = useBehavior(
localMember?.matrixEventId$ ?? noMembershipEvent$,
const myMembershipEvent = useMemo(
() =>
memberships.find(
(m) => m.userId === myUserId && m.deviceId === myDeviceId,
)?.eventId,
[memberships, myUserId, myDeviceId],
);
const reactions = useBehavior(vm.reactions$);
@@ -111,8 +99,8 @@ export const ReactionsSenderProvider = ({
if (!myMembershipEvent) {
throw new Error("Cannot find own membership event");
}
const reaction = await client.sendEvent(
room.roomId,
const reaction = await room.client.sendEvent(
rtcSession.room.roomId,
EventType.Reaction,
{
"m.relates_to": {
@@ -128,14 +116,20 @@ export const ReactionsSenderProvider = ({
}
} else {
try {
await client.redactEvent(room.roomId, myReactionId);
await room.client.redactEvent(rtcSession.room.roomId, myReactionId);
logger.debug("Redacted raise hand event");
} catch (ex) {
logger.error("Failed to redact reaction event", myReactionId, ex);
throw ex;
}
}
}, [myMembershipEvent, myMembershipIdentifier, myRaisedHand, client, room]);
}, [
myMembershipEvent,
myMembershipIdentifier,
myRaisedHand,
rtcSession,
room,
]);
const sendReaction = useCallback(
async (reaction: ReactionOption) => {
@@ -146,16 +140,20 @@ export const ReactionsSenderProvider = ({
if (!myMembershipEvent) {
throw new Error("Cannot find own membership event");
}
await client.sendEvent(room.roomId, ElementCallReactionEventType, {
"m.relates_to": {
rel_type: RelationType.Reference,
event_id: myMembershipEvent,
await room.client.sendEvent(
rtcSession.room.roomId,
ElementCallReactionEventType,
{
"m.relates_to": {
rel_type: RelationType.Reference,
event_id: myMembershipEvent,
},
emoji: reaction.emoji,
name: reaction.name,
},
emoji: reaction.emoji,
name: reaction.name,
});
);
},
[myMembershipEvent, myReaction, client, room, myMembershipIdentifier],
[myMembershipEvent, myReaction, room, myMembershipIdentifier, rtcSession],
);
return (
+3 -2
View File
@@ -6,12 +6,13 @@ Please see LICENSE in the repository root for full details.
*/
import { type FC, type FormEventHandler, useCallback, useState } from "react";
import { type MatrixClient } from "matrix-js-sdk";
import { Trans, useTranslation } from "react-i18next";
import { Button, Heading, Text } from "@vector-im/compound-web";
import styles from "./CallEndedView.module.css";
import feedbackStyle from "../input/FeedbackInput.module.css";
import { type ProfileClientParts, useProfile } from "../profile/useProfile";
import { useProfile } from "../profile/useProfile";
import { Header, HeaderLogo, LeftNav, RightNav } from "../Header";
import { PosthogAnalytics } from "../analytics/PosthogAnalytics";
import { FieldRow, InputField } from "../input/Input";
@@ -21,7 +22,7 @@ import { LeaveToHomeLink } from "../button/LeaveToHomeLink";
import { useLeaveToHome } from "../LeaveToHomeContext";
interface Props {
client: ProfileClientParts;
client: MatrixClient;
isPasswordlessUser: boolean;
hideHeader: boolean;
confineToRoom: boolean;
+80 -46
View File
@@ -19,8 +19,16 @@ import {
vitest,
} from "vitest";
import { render, waitFor, screen, act } from "@testing-library/react";
import { type MatrixClient, JoinRule } from "matrix-js-sdk";
import { type MatrixRTCSession } from "matrix-js-sdk/lib/matrixrtc";
import {
type MatrixClient,
JoinRule,
type RoomState,
UnsupportedStickyEventsEndpointError,
} from "matrix-js-sdk";
import {
MatrixRTCSessionEvent,
type MatrixRTCSession,
} from "matrix-js-sdk/lib/matrixrtc";
import { BrowserRouter } from "react-router-dom";
import userEvent, {
PointerEventsCheckLevel,
@@ -45,7 +53,7 @@ import {
testScope,
} from "../utils/test";
import { CallView } from "./CallView";
import { type CallMatrixBackend } from "../CallMatrixBackend";
import { GroupCallErrorBoundary } from "./GroupCallErrorBoundary";
import {
type HostBridge,
HostBridgeProvider,
@@ -53,10 +61,7 @@ import {
nullHostBridge,
} from "../HostBridge";
import { type JoinCallData } from "../widget";
import {
ConnectionLostError,
MatrixRTCTransportMissingError,
} from "../utils/errors";
import { MatrixRTCTransportMissingError } from "../utils/errors";
import {
BackgroundEffectsProvider,
createAppBackgroundEffects,
@@ -141,6 +146,7 @@ function createCallView(
hostBridge: HostBridge,
joined = true,
options: {
withErrorBoundary?: boolean;
/** Wait for the host to say when to join, rather than joining at once. */
preload?: boolean;
} = {},
@@ -149,7 +155,6 @@ function createCallView(
getByText: ReturnType<typeof render>["getByText"];
} {
const client = {
...mockEmitter(),
getUser: () => null,
getUserId: () => localRtcMember.userId,
getDeviceId: () => localRtcMember.deviceId,
@@ -165,14 +170,28 @@ function createCallView(
client,
roomId,
getMember: (userId) => roomMembers.get(userId) ?? null,
getMembersWithMembership: () => [carol],
getMxcAvatarUrl: () => null,
getJoinRule: () => JoinRule.Invite,
getCanonicalAlias: () => null,
currentState: {
...mockEmitter(),
getJoinRule: () => JoinRule.Invite,
} as Partial<RoomState> as RoomState,
});
const rtcSession = new MockRTCSession(room, []).withMemberships(
constant([localRtcMember]),
);
rtcSession.joined = joined;
const callView = (
<CallView
client={client}
isPasswordlessUser={false}
confineToRoom={false}
preload={options.preload ?? false}
// Straight into the (mocked) call, past the lobby
skipLobby
rtcSession={rtcSession.asMockedSession()}
/>
);
const { getByText } = render(
<BrowserRouter>
<HostBridgeProvider value={hostBridge}>
@@ -181,22 +200,13 @@ function createCallView(
<BackgroundEffectsProvider
effects={createAppBackgroundEffects(testScope())}
>
<CallView
backend={
{
...client,
...room,
clientEmitter: client,
roomEmitter: room,
} as unknown as CallMatrixBackend
}
isPasswordlessUser={false}
confineToRoom={false}
preload={options.preload ?? false}
// Straight into the (mocked) call, past the lobby
skipLobby
rtcSession={rtcSession.asMockedSession()}
/>
{options.withErrorBoundary ? (
<GroupCallErrorBoundary recoveryActionHandler={vi.fn()}>
{callView}
</GroupCallErrorBoundary>
) : (
callView
)}
</BackgroundEffectsProvider>
</MediaDevicesContext>
</TooltipProvider>
@@ -361,28 +371,52 @@ test.skip("CallView shows errors that occur during joining", async () => {
screen.getByText("Call is not supported");
});
test("user can reconnect after losing the connection", async () => {
// What the call throws once the view model reports the connection lost
(ActiveCall as MockedFunction<typeof ActiveCall>).mockImplementation(
({ onLeft }) => {
const [error, setError] = useState<Error | null>(null);
if (error !== null) throw error;
return (
<div>
<button onClick={() => onLeft("user")}>Leave</button>
<button onClick={() => setError(new ConnectionLostError())}>
LoseConnection
</button>
</div>
);
},
test("translates wrapped UnsupportedStickyEventsEndpointError to the StickyEventsRequiredError screen", async () => {
// Mirror the shape the SDK emits: the MembershipManager scheduler wraps
// the original UnsupportedStickyEventsEndpointError in a generic Error
// but preserves the original on `.cause`.
const stickyError = new UnsupportedStickyEventsEndpointError(
"Server does not support the sticky events",
"sendStickyEvent",
);
const user = userEvent.setup();
createCallView(nullHostBridge, true);
await user.click(screen.getByRole("button", { name: "LoseConnection" }));
const wrappedError = new Error(
"The MembershipManager shut down because of the end condition: " +
String(stickyError),
{ cause: stickyError },
);
const { rtcSession } = createCallView(nullHostBridge, true, {
withErrorBoundary: true,
});
await act(() =>
rtcSession.emit(MatrixRTCSessionEvent.MembershipManagerError, wrappedError),
);
await screen.findByText("Homeserver does not support Matrix 2.0 calls");
});
test("falls back to ConnectionLostError for unrecognised membership manager errors", async () => {
const { rtcSession } = createCallView(nullHostBridge, true, {
withErrorBoundary: true,
});
await act(() =>
rtcSession.emit(
MatrixRTCSessionEvent.MembershipManagerError,
new Error("something else broke"),
),
);
await screen.findByText("Connection lost");
// Never joined, so there was no call to leave
expect(playSound).not.toHaveBeenCalled();
});
test("user can reconnect after a membership manager error", async () => {
const user = userEvent.setup();
const { rtcSession } = createCallView(nullHostBridge, true);
await act(() =>
rtcSession.emit(MatrixRTCSessionEvent.MembershipManagerError, undefined),
);
// XXX: Wrapping the following click in act() shouldn't be necessary (the
// async state update should be processed automatically by the waitFor call),
// and yet here we are.
+101 -182
View File
@@ -13,24 +13,21 @@ import {
useMemo,
useState,
} from "react";
import { type MatrixClient, JoinRule, type Room } from "matrix-js-sdk";
import {
type MatrixClient,
JoinRule,
type Room,
UnsupportedStickyEventsEndpointError,
} from "matrix-js-sdk";
import {
Room as LivekitRoom,
isE2EESupported as isE2EESupportedBrowser,
} from "livekit-client";
import { logger } from "matrix-js-sdk/lib/logger";
import {
MatrixRTCSessionEvent,
type MatrixRTCSession,
type Statistics,
} from "matrix-js-sdk/lib/matrixrtc";
import { type Observable } from "rxjs";
import {
createRTCSlot,
type EncryptionSystem,
MembershipManagerError as RTCMembershipManagerError,
type RTCSlot,
withEmitter,
} from "@element-hq/matrixrtc-sdk";
import { type JoinCallData } from "../widget";
import { LobbyView } from "./LobbyView";
@@ -42,6 +39,7 @@ import { findDeviceByName } from "../utils/media";
import { ActiveCall } from "./InCallView";
import { type MuteStates } from "../state/MuteStates";
import { useMediaDevices } from "../MediaDevicesContext";
import { useMatrixRTCSessionMemberships } from "../useMatrixRTCSessionMemberships";
import {
saveKeyForRoom,
useRoomEncryptionSystem,
@@ -59,11 +57,14 @@ import {
} from "./CallEventAudioRenderer";
import { useLatest } from "../useLatest";
import {
ConnectionLostError,
E2EENotSupportedError,
ElementCallError,
StickyEventsRequiredError,
UnknownCallError,
} from "../utils/errors.ts";
import { GroupCallErrorBoundary } from "./GroupCallErrorBoundary.tsx";
import { useTypedEventEmitter } from "../useEvents";
import { muteAllAudio$ } from "../state/MuteAllAudioModel.ts";
import { useAppBarTitle } from "../AppBar.tsx";
import { useBehavior } from "../useBehavior.ts";
@@ -72,9 +73,6 @@ import { useHostBridge } from "../HostBridge.ts";
import { useMuteStates } from "../state/useMuteStates.ts";
import { useLeaveToHome } from "../LeaveToHomeContext.ts";
import { useLocalMedia } from "../useLocalMedia.ts";
import { ObservableScope } from "../state/ObservableScope.ts";
import { type CallNotificationWrapper } from "../state/CallViewModel/CallNotificationLifecycle.ts";
import { type CallMatrixBackend } from "../CallMatrixBackend.ts";
/**
* If there already are this many participants in the call, we automatically mute
@@ -82,19 +80,17 @@ import { type CallMatrixBackend } from "../CallMatrixBackend.ts";
*/
export const MUTE_PARTICIPANT_COUNT = 8;
declare global {
interface Window {
rtcSession?: MatrixRTCSession;
}
}
interface Props {
/** The client and room to place the call with and in. */
backend: CallMatrixBackend;
/**
* The call to join, which the RTC slot is built on.
* Passing undefined -> the SDK will create a inner RTCSession (or in the future a rust equivalent)
* @deprecated: It should not get its rtcSession from here.
* The goal is that the RTC session is fully encapsulated inside the SDK (`createRTCSlot`)
* We keep this as a fallback for the SPA. In that mode we currently expect to manage Our
* own rtcSession with `useLoadGroupCall`. This is an open TODO. Until then we have this
* alternative.
*/
rtcSession?: MatrixRTCSession;
/** The client to place the call with. */
client: MatrixClient;
/** The call to join. */
rtcSession: MatrixRTCSession;
/**
* Whether the user is signed in as a guest, and so should be offered the
* chance to create an account when the call ends.
@@ -125,58 +121,18 @@ interface Props {
* whatever is hosting it — the standalone app's own shell, or an application
* embedding Element Call as a component. Both render this.
*/
export const CallView: FC<Props> = ({
backend,
rtcSession: externalRtcSession,
...props
}): ReactNode => {
// STOP GAP: what is below is still written against a client and a room.
// Both are refactored along with the backend.
const client = useMemo(
() => withEmitter(backend, backend.clientEmitter),
[backend],
);
const room = useMemo(
() => withEmitter(backend, backend.roomEmitter),
[backend],
);
export const CallView: FC<Props> = (props): ReactNode => {
// Whether the user is in the call is the call's own business, not its host's.
// Held here rather than below so that it survives the mute state being
// rebuilt.
const [joined, setJoined] = useState(false);
const muteStates = useMuteStates();
const e2eeSystem = useRoomEncryptionSystem(backend.roomId);
// One slot for every join of this call: leaving and joining again, to
// reconnect or from the lobby, happens on the same one
const [slot, setSlot] = useState<ReturnType<typeof createRTCSlot> | null>(
null,
);
useEffect(() => {
const scope = new ObservableScope();
setSlot(
createRTCSlot(
scope,
backend,
{ encryptionSystem: e2eeSystem },
externalRtcSession,
),
);
return (): void => scope.end();
}, [backend, e2eeSystem, externalRtcSession]);
if (muteStates === null || slot === null) return null;
if (muteStates === null) return null;
return (
<LoadedCallView
{...props}
client={client}
room={room}
sentCallNotification$={slot.sentCallNotification$}
statistics={slot.statistics}
slot={slot}
e2eeSystem={e2eeSystem}
joined={joined}
setJoined={setJoined}
muteStates={muteStates}
@@ -184,98 +140,20 @@ export const CallView: FC<Props> = ({
);
};
// Here we do not pass the rtcSession anymore. This is a huge step!!
// We only rely on our sdk abstraction (+ client and +room but only a
// smaller list of callbacks.)
interface LoadedProps extends Omit<Props, "rtcSession" | "backend"> {
// Narrower than `Props.client`: the `slot` already has what only it and its
// session need, `getRTCTransports`, `delegateDelayedLeave`, `baseUrl`,
// `getSyncState`, `ClientEvent.Sync`, and the session's sending
client: Pick<
MatrixClient,
| "getUserId"
| "getDeviceId"
| "getDomain"
| "getOpenIdToken"
| "getLivekitToken"
| "sendEvent"
| "redactEvent"
| "decryptEventIfNeeded"
| "getUser"
| "setDisplayName"
| "setAvatarUrl"
| "uploadContent"
| "doesServerSupportUnstableFeature"
| "getCrypto"
// `on`/`off`, to listen for `MatrixEventEvent.Decrypted` (reactions once
// decrypted)
| "on"
| "off"
>;
// Narrower than `Props.room`: the `slot` already follows
// `RoomStateEvent.Events` for its status, `RoomStateEvent.Members` for its
// own members, and the timeline its session reads the memberships from
room: Pick<
Room,
| "roomId"
| "name"
| "getMxcAvatarUrl"
| "getJoinRule"
| "getMembersWithMembership"
| "_unstable_getStickyEvents"
| "relations"
// `on`/`off`, to listen for:
// - `RoomEvent.Name` (room name)
// - `RoomStateEvent.Update` and `RoomEvent.CurrentStateUpdated` (avatar
// and join rule hooks)
// - `RoomStateEvent.Members` (call view model members)
// - `RoomEvent.Timeline` and `RoomStickyEventsEvent.Update` (call notifications)
// - `RoomEvent.Timeline`, `RoomEvent.Redaction` and
// `RoomEvent.LocalEchoUpdated` (reactions and raised hands)
| "on"
| "off"
>;
/**
* The call notifications the session sends with our join, so the call can
* show it ringing: the slot's `sentCallNotification$`.
*/
sentCallNotification$: Observable<CallNotificationWrapper>;
/** The session's media key statistics, reported when the call ends. */
statistics: Statistics;
slot: RTCSlot;
e2eeSystem: EncryptionSystem;
interface LoadedProps extends Props {
joined: boolean;
setJoined: (value: boolean) => void;
muteStates: MuteStates;
}
/**
* Whether our membership is live in the slot: seen in the room, and not taken
* down by the membership manager stopping. Leaving the call only makes sense
* then; an error before the join, or one that ended the membership, leaves
* nothing to leave.
*/
function isInCall(slot: RTCSlot): boolean {
const participation = slot.participation$.value;
if (participation?.localMember$.value == null) return false;
const state = participation.state$.value;
return !(
state.kind === "failed" && state.error instanceof RTCMembershipManagerError
);
}
/** {@link CallView}, once it has the slot and mute state everything below needs. */
/** {@link CallView}, once it has the mute state everything below needs. */
const LoadedCallView: FC<LoadedProps> = ({
client,
room,
isPasswordlessUser,
confineToRoom,
preload,
skipLobby,
slot,
sentCallNotification$,
statistics,
e2eeSystem,
rtcSession,
joined,
setJoined,
muteStates,
@@ -284,7 +162,7 @@ const LoadedCallView: FC<LoadedProps> = ({
const [externalError, setExternalError] = useState<ElementCallError | null>(
null,
);
const members = useBehavior(slot.members$);
const memberships = useMatrixRTCSessionMemberships(rtcSession);
const rootElement = useRootElement();
const hostBridge = useHostBridge();
// A host that can close us is a host that decides when we stop existing, so
@@ -303,7 +181,7 @@ const LoadedCallView: FC<LoadedProps> = ({
);
// This should use `useEffectEvent` (only available in experimental versions)
useEffect(() => {
if (members.length >= MUTE_PARTICIPANT_COUNT)
if (memberships.length >= MUTE_PARTICIPANT_COUNT)
muteStates.audio.setEnabled$.value?.(false);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
@@ -325,10 +203,48 @@ const LoadedCallView: FC<LoadedProps> = ({
};
}, [rootElement]);
useEffect(() => {
window.rtcSession = rtcSession;
return (): void => {
delete window.rtcSession;
};
}, [rtcSession]);
// TODO move this into the callViewModel LocalMembership.ts
// We might actually not need this at all. Since we get into fatalError on those errors already?
useTypedEventEmitter(
rtcSession,
MatrixRTCSessionEvent.MembershipManagerError,
(error) => {
// When matrix_rtc_mode=matrix_2_0 is in effect but the homeserver does
// not advertise MSC4354 (sticky events), the SDK throws an
// `UnsupportedStickyEventsEndpointError`. The MembershipManager
// scheduler wraps it and exposes the original via `.cause`.
if (
error instanceof Error &&
error.cause instanceof UnsupportedStickyEventsEndpointError
) {
setExternalError(new StickyEventsRequiredError());
} else {
setExternalError(new ConnectionLostError());
}
},
);
useEffect(() => {
// Sanity check the room object
if (client.getRoom(rtcSession.room.roomId) !== rtcSession.room)
logger.warn(
`We've ended up with multiple rooms for the same ID (${rtcSession.room.roomId}). This indicates a bug in the group call loading code, and may lead to incomplete room state.`,
);
}, [client, rtcSession.room]);
const room = rtcSession.room as Room;
const { displayName, avatarUrl } = useProfile(client);
const roomName = useRoomName(room);
const roomAvatar = useRoomAvatar(room);
const { returnToLobby, password: passwordFromUrl, header } = useUrlParams();
const e2eeSystem = useRoomEncryptionSystem(room.roomId);
// Save the password once we start the groupCallView
useEffect(() => {
@@ -344,6 +260,7 @@ const LoadedCallView: FC<LoadedProps> = ({
avatarUrl: avatarUrl!,
roomId: room.roomId,
roomName,
roomAlias: room.getCanonicalAlias(),
roomAvatar,
e2eeSystem,
};
@@ -351,8 +268,8 @@ const LoadedCallView: FC<LoadedProps> = ({
// Count each member only once, regardless of how many devices they use
const participantCount = useMemo(
() => new Set<string>(members.map((m) => m.userId)).size,
[members],
() => new Set<string>(memberships.map((m) => m.userId!)).size,
[memberships],
);
const mediaDevices = useMediaDevices();
@@ -360,24 +277,27 @@ const LoadedCallView: FC<LoadedProps> = ({
// Opened by the lobby, published by the call: one capture for both
const media = useLocalMedia();
const enterRTCSessionOrError = useCallback(async (): Promise<void> => {
try {
setJoined(true);
// TODO-MULTI-SFU what to do with error handling now that we don't use this function?
// @BillCarsonFr
} catch (e) {
if (e instanceof ElementCallError) {
setExternalError(e);
} else {
logger.error(`Unknown Error while entering RTC session`, e);
const error = new UnknownCallError(
e instanceof Error ? e : new Error("Unknown error", { cause: e }),
);
setExternalError(error);
const enterRTCSessionOrError = useCallback(
async (rtcSession: MatrixRTCSession): Promise<void> => {
try {
setJoined(true);
// TODO-MULTI-SFU what to do with error handling now that we don't use this function?
// @BillCarsonFr
} catch (e) {
if (e instanceof ElementCallError) {
setExternalError(e);
} else {
logger.error(`Unknown Error while entering RTC session`, e);
const error = new UnknownCallError(
e instanceof Error ? e : new Error("Unknown error", { cause: e }),
);
setExternalError(error);
}
}
}
return Promise.resolve();
}, [setJoined]);
return Promise.resolve();
},
[setJoined],
);
useEffect(() => {
const defaultDeviceSetup = async ({
@@ -440,6 +360,7 @@ const LoadedCallView: FC<LoadedProps> = ({
}
}, [
hostBridge,
rtcSession,
preload,
skipLobby,
mediaDevices,
@@ -486,9 +407,9 @@ const LoadedCallView: FC<LoadedProps> = ({
const sendInstantly = hostControlsLifetime;
PosthogAnalytics.instance.eventCallEnded.track(
room.roomId,
slot.members$.value.length,
rtcSession.memberships.length,
sendInstantly,
statistics,
rtcSession,
);
// Unfortunately the PostHog library provides no way to await the
// tracking of an event, but we don't really want it to hold up our
@@ -534,8 +455,7 @@ const LoadedCallView: FC<LoadedProps> = ({
hostBridge,
hostControlsLifetime,
room.roomId,
slot,
statistics,
rtcSession,
isPasswordlessUser,
confineToRoom,
returnToLobby,
@@ -549,7 +469,7 @@ const LoadedCallView: FC<LoadedProps> = ({
hostBridge.setAlwaysOnScreen(true).catch((e) => {
logger.error("Error calling setAlwaysOnScreen(true)", e);
});
}, [hostBridge, joined]);
}, [hostBridge, joined, rtcSession]);
const joinRule = useJoinRule(room);
@@ -610,8 +530,7 @@ const LoadedCallView: FC<LoadedProps> = ({
<ActiveCall
client={client}
matrixInfo={matrixInfo}
slot={slot}
sentCallNotification$={sentCallNotification$}
rtcSession={rtcSession as MatrixRTCSession}
matrixRoom={room}
onLeft={onLeft}
media={media}
@@ -634,7 +553,7 @@ const LoadedCallView: FC<LoadedProps> = ({
if (isPasswordlessUser || PosthogAnalytics.instance.isEnabled()) {
body = (
<CallEndedView
endedCallId={room.roomId}
endedCallId={rtcSession.room.roomId}
client={client}
isPasswordlessUser={isPasswordlessUser}
hideHeader={header === HeaderStyle.None}
@@ -651,7 +570,7 @@ const LoadedCallView: FC<LoadedProps> = ({
// Left, and the host decides what happens next:
body = returnToLobby ? lobbyView : null;
} else if (preload || skipLobby) {
// Not in the call yet: waiting for the host to say when, or about to join
// The RTC session is not joined to yet (`isJoined`), but enterRTCSessionOrError should have been called.
body = null;
} else {
body = lobbyView;
@@ -663,16 +582,16 @@ const LoadedCallView: FC<LoadedProps> = ({
setExternalError(null);
if (action == "reconnect") {
setLeft(false);
await enterRTCSessionOrError().catch((e) => {
await enterRTCSessionOrError(rtcSession).catch((e) => {
logger.error("Error re-entering RTC session", e);
});
}
}}
onError={(_error) => {
if (isInCall(slot)) onLeft("error");
if (rtcSession.isJoined()) onLeft("error");
// If there is an error we need to be dismissible again. This is done in
// `onLeft` as well; we need it here explicitly in case we were not in
// the call.
// `onLeft` as well; we need it here explicitly in case
// rtcSession.isJoined is false.
void hostBridge.setAlwaysOnScreen(false);
}}
>
+66 -67
View File
@@ -15,7 +15,7 @@ import {
vi,
} from "vitest";
import { act, render, type RenderResult } from "@testing-library/react";
import { BehaviorSubject, NEVER } from "rxjs";
import { BehaviorSubject } from "rxjs";
import { BrowserRouter } from "react-router-dom";
import { TooltipProvider } from "@vector-im/compound-web";
import userEvent from "@testing-library/user-event";
@@ -25,6 +25,7 @@ import {
mockMediaDevices,
mockMuteStates,
mockRtcMembership,
type MockRTCSession,
mockRTCSlot,
mockRTCMember,
testScope,
@@ -43,7 +44,7 @@ import { ReactionsSenderProvider } from "../reactions/useReactionsSender";
import { useRoomEncryptionSystem } from "../e2ee/sharedKeyManagement";
import { MemberAudioRenderer } from "../tracks/MemberAudioRenderer";
import type * as MatrixRTCSdk from "@element-hq/matrixrtc-sdk";
import { constant, type RTCSlot } from "@element-hq/matrixrtc-sdk";
import { constant, type ObservableScope } from "@element-hq/matrixrtc-sdk";
import { MediaDevicesContext } from "../MediaDevicesContext";
import { type MediaDevices as ECMediaDevices } from "../state/MediaDevices";
import { AppBar } from "../AppBar";
@@ -72,6 +73,12 @@ vi.mock("../tile/GridTile");
vi.mock("../tile/SpotlightTile");
vi.mock("../e2ee/sharedKeyManagement");
vi.mock("../tracks/MemberAudioRenderer");
// ActiveCall builds the slot itself; the view is what these tests are about
const createRTCSlotMock = vi.hoisted(() => vi.fn());
vi.mock("@element-hq/matrixrtc-sdk", async (importOriginal) => ({
...(await importOriginal<typeof MatrixRTCSdk>()),
createRTCSlot: createRTCSlotMock,
}));
vi.mock("react-use-measure", () => ({
default: (): [() => void, object] => [(): void => {}, {}],
}));
@@ -84,33 +91,32 @@ const matrixInfo = {
avatarUrl: "",
roomId: "",
roomName: "",
roomAlias: null,
roomAvatar: null,
e2eeSystem: { kind: E2eeType.NONE },
} satisfies MatrixInfo;
let useRoomEncryptionSystemMock: MockedFunction<typeof useRoomEncryptionSystem>;
// The view is what these tests are about, so the slot is a stand-in
function mockSlot(): RTCSlot {
return mockRTCSlot(testScope(), {
localMember$: constant(
mockRTCMember(true, {
membership: localRtcMember,
tracks$: constant([]),
}),
),
remoteMembers$: constant([
mockRTCMember(false, {
membership: aliceRtcMember,
tracks$: constant([]),
}),
]),
});
}
beforeEach(() => {
vi.clearAllMocks();
createRTCSlotMock.mockImplementation((scope: ObservableScope) =>
mockRTCSlot(scope, {
localMember$: constant(
mockRTCMember(true, {
membership: localRtcMember,
tracks$: constant([]),
}),
),
remoteMembers$: constant([
mockRTCMember(false, {
membership: aliceRtcMember,
tracks$: constant([]),
}),
]),
}),
);
// MemberAudioRenderer is tested separately.
(
MemberAudioRenderer as MockedFunction<typeof MemberAudioRenderer>
@@ -126,11 +132,12 @@ interface CreateInCallViewArgs {
withAppBar?: boolean;
}
function createInCallView(args: CreateInCallViewArgs = {}): RenderResult & {
rtcSession: MockRTCSession;
vm: CallViewModel;
} {
const mediaDevices = args.mediaDevices ?? mockMediaDevices({});
const muteState = mockMuteStates();
const { vm, footerVm, developerSettingsVm, matrixRoom, client } =
const { vm, footerVm, developerSettingsVm, rtcSession } =
getBasicCallViewModelEnvironment(
[local, alice],
undefined,
@@ -138,15 +145,20 @@ function createInCallView(args: CreateInCallViewArgs = {}): RenderResult & {
args.callViewModelOptions,
);
rtcSession.joined = true;
const room = rtcSession.room;
const client = room.client;
const inCallView = (
<InCallView
client={client}
rtcSession={rtcSession.asMockedSession()}
muteStates={muteState}
vm={vm}
footerVm={footerVm}
developerSettingsVm={developerSettingsVm}
matrixInfo={matrixInfo}
matrixRoom={matrixRoom}
matrixRoom={room}
onShareClick={null}
/>
);
@@ -156,7 +168,10 @@ function createInCallView(args: CreateInCallViewArgs = {}): RenderResult & {
const renderResult = render(
<BrowserRouter>
<MediaDevicesContext value={mediaDevices}>
<ReactionsSenderProvider vm={vm} room={matrixRoom} client={client}>
<ReactionsSenderProvider
vm={vm}
rtcSession={rtcSession.asMockedSession()}
>
<TooltipProvider>{content}</TooltipProvider>
</ReactionsSenderProvider>
</MediaDevicesContext>
@@ -164,6 +179,7 @@ function createInCallView(args: CreateInCallViewArgs = {}): RenderResult & {
);
return {
...renderResult,
rtcSession,
vm,
};
}
@@ -227,53 +243,37 @@ function fakeLocalMedia(): MatrixRTCSdk.LocalMedia {
};
}
function renderActiveCall(slot: RTCSlot): RenderResult {
const { matrixRoom } = getBasicRTCSession([local, alice]);
return render(
<BrowserRouter>
<MediaDevicesContext value={mockMediaDevices({})}>
<BackgroundEffectsProvider
effects={createAppBackgroundEffects(testScope())}
>
<TooltipProvider>
<ActiveCall
client={matrixRoom.client}
slot={slot}
sentCallNotification$={NEVER}
matrixRoom={matrixRoom}
muteStates={mockMuteStates()}
matrixInfo={matrixInfo}
onShareClick={null}
e2eeSystem={{ kind: E2eeType.NONE }}
media={fakeLocalMedia()}
onLeft={(): void => {}}
/>
</TooltipProvider>
</BackgroundEffectsProvider>
</MediaDevicesContext>
</BrowserRouter>,
);
}
describe("ActiveCall", () => {
it("creates the view models and renders the call", async () => {
const { findByTestId } = renderActiveCall(mockSlot());
const mediaDevices = mockMediaDevices({});
const { rtcSession, matrixRoom } = getBasicRTCSession([local, alice]);
const { findByTestId } = render(
<BrowserRouter>
<MediaDevicesContext value={mediaDevices}>
<BackgroundEffectsProvider
effects={createAppBackgroundEffects(testScope())}
>
<TooltipProvider>
<ActiveCall
client={matrixRoom.client}
rtcSession={rtcSession.asMockedSession()}
matrixRoom={matrixRoom}
muteStates={mockMuteStates()}
matrixInfo={matrixInfo}
onShareClick={null}
e2eeSystem={{ kind: E2eeType.NONE }}
media={fakeLocalMedia()}
onLeft={(): void => {}}
/>
</TooltipProvider>
</BackgroundEffectsProvider>
</MediaDevicesContext>
</BrowserRouter>,
);
// Rendering at all proves ActiveCall created all of its view models
expect(await findByTestId("incall_leave")).toBeVisible();
});
it("leaves the slot once gone, so that the next call can join it", async () => {
const slot = mockSlot();
const { findByTestId, unmount } = renderActiveCall(slot);
await findByTestId("incall_leave");
expect(slot.join).toHaveBeenCalled();
const participation = slot.participation$.value!;
expect(participation.leave).not.toHaveBeenCalled();
unmount();
expect(participation.leave).toHaveBeenCalled();
});
it("lays the call out for the size of its root element", async () => {
// jsdom has no layout and no ResizeObserver: the root reports whatever
// size we say, and the observer notifies whenever we tell it to
@@ -296,7 +296,7 @@ describe("ActiveCall", () => {
try {
const mediaDevices = mockMediaDevices({});
const { matrixRoom } = getBasicRTCSession([local, alice]);
const { rtcSession, matrixRoom } = getBasicRTCSession([local, alice]);
const { findByTestId, container } = render(
<BrowserRouter>
<RootElementProvider value={root}>
@@ -307,8 +307,7 @@ describe("ActiveCall", () => {
<TooltipProvider>
<ActiveCall
client={matrixRoom.client}
slot={mockSlot()}
sentCallNotification$={NEVER}
rtcSession={rtcSession.asMockedSession()}
matrixRoom={matrixRoom}
muteStates={mockMuteStates()}
matrixInfo={matrixInfo}
+35 -88
View File
@@ -18,7 +18,7 @@ import {
type ReactNode,
} from "react";
import useMeasure from "react-use-measure";
import { type Observable } from "rxjs";
import { type MatrixRTCSession } from "matrix-js-sdk/lib/matrixrtc";
import classNames from "classnames";
import { logger as rootLogger } from "matrix-js-sdk/lib/logger";
import { useTranslation } from "react-i18next";
@@ -31,11 +31,7 @@ import { useRootElement } from "../RootElementContext";
import { observeElementSize$ } from "../utils/elementSize";
import styles from "./InCallView.module.css";
import { GridTile } from "../tile/GridTile";
import {
SettingsModal,
type SettingsClientParts,
defaultSettingsTab,
} from "../settings/SettingsModal";
import { SettingsModal, defaultSettingsTab } from "../settings/SettingsModal";
import { useRageshakeRequestModal } from "../settings/submit-rageshake";
import { RageshakeRequestModal } from "./RageshakeRequestModal";
import { useWakeLock } from "../useWakeLock";
@@ -74,8 +70,9 @@ import { ReactionsAudioRenderer } from "./ReactionAudioRenderer";
import { ReactionsOverlay } from "./ReactionsOverlay";
import { CallEventAudioRenderer } from "./CallEventAudioRenderer";
import {
createRTCSlot,
type RTCParticipationOptions,
type RTCSlot,
type RTCSlotOptions,
constant,
} from "@element-hq/matrixrtc-sdk";
import {
@@ -97,10 +94,7 @@ import {
screenShareResolution,
} from "../settings/settings";
import { Config } from "../config/Config";
import {
type CallNotificationWrapper,
createSentCallNotification$,
} from "../state/CallViewModel/CallNotificationLifecycle.ts";
import { createSentCallNotification$ } from "../state/CallViewModel/CallNotificationLifecycle.ts";
import { ReactionsReader } from "../reactions/ReactionsReader";
import { MemberAudioRenderer } from "../tracks/MemberAudioRenderer.tsx";
import { muteAllAudio$ } from "../state/MuteAllAudioModel.ts";
@@ -138,56 +132,6 @@ export interface ActiveCallProps extends Omit<
InCallViewProps,
"vm" | "footerVm" | "developerSettingsVm"
> {
client: Pick<
MatrixClient,
// Our own identity: call view model, footer, settings
| "getUserId"
| "getDeviceId"
// Developer settings: the local transport and authenticating with it,
// `getOpenIdToken` for the JWT service flow, `getLivekitToken` for the
// client-server API flow (MSC4195)
| "getDomain"
| "getOpenIdToken"
| "getLivekitToken"
// Reactions and raised hands: sending, lowering, and decrypting
| "sendEvent"
| "redactEvent"
| "decryptEventIfNeeded"
// `on`/`off`, to listen for `MatrixEventEvent.Decrypted` (reactions once
// decrypted)
| "on"
| "off"
// Profile: display name and avatar, for the settings and the call ended view
| "getUser"
| "setDisplayName"
| "setAvatarUrl"
| "uploadContent"
// Developer settings
| "doesServerSupportUnstableFeature"
| "getCrypto"
>;
matrixRoom: Pick<
MatrixRoom,
// Call view model and reactions
| "roomId"
// Joined and invited members, for the call view model
| "getMembersWithMembership"
// Call notifications, for the call view model
| "_unstable_getStickyEvents"
// Raised hands already in the room, for the reactions reader
| "relations"
// `on`/`off`, to listen for:
// - `RoomStateEvent.Members` (call view model members)
// - `RoomEvent.Timeline` and `RoomStickyEventsEvent.Update` (call notifications)
// - `RoomEvent.Timeline`, `RoomEvent.Redaction` and
// `RoomEvent.LocalEchoUpdated` (reactions and raised hands)
| "on"
| "off"
>;
/** The slot to join; the call leaves it again when it ends. */
slot: RTCSlot;
/** The call notifications sent with our join, so the call can show it ringing. */
sentCallNotification$: Observable<CallNotificationWrapper>;
e2eeSystem: EncryptionSystem;
/** The tracks the lobby opened, for the join to publish. */
media: LocalMedia;
@@ -215,17 +159,18 @@ export const ActiveCall: FC<ActiveCallProps> = (props) => {
useEffect(() => {
rootLogger.info("START CALL VIEW SCOPE");
const scope = new ObservableScope();
const reactionsReader = new ReactionsReader(
scope,
props.slot.members$,
props.matrixRoom,
props.client,
);
const reactionsReader = new ReactionsReader(scope, props.rtcSession);
const { autoLeaveWhenOthersLeft, waitForCallPickup, sendNotificationType } =
urlParams;
const capture = captureSettings();
const rtcParticipation = props.slot.join({
const slot = createRTCSlot(
scope,
props.client,
props.matrixRoom,
rtcSlotOptions(props.e2eeSystem),
);
const rtcParticipation = slot.join({
...rtcParticipationOptions(urlParams),
// Without a lobby nothing was captured and the join captures itself
publish:
@@ -244,7 +189,6 @@ export const ActiveCall: FC<ActiveCallProps> = (props) => {
{
rtcParticipation,
matrixRoom: props.matrixRoom,
client: props.client,
mediaDevices,
muteStates: props.muteStates,
encryptionSystem: props.e2eeSystem,
@@ -261,7 +205,7 @@ export const ActiveCall: FC<ActiveCallProps> = (props) => {
waitForCallPickup: waitForCallPickup && sendNotificationType === "ring",
sentCallNotification$: createSentCallNotification$(
scope,
props.sentCallNotification$,
props.rtcSession,
),
},
);
@@ -270,13 +214,10 @@ export const ActiveCall: FC<ActiveCallProps> = (props) => {
vm.leave$.pipe(scope.bind()).subscribe(props.onLeft);
return (): void => {
// The slot outlives the call, and joins again only once this has left
rtcParticipation.leave();
scope.end();
};
}, [
props.slot,
props.sentCallNotification$,
props.rtcSession,
props.matrixRoom,
props.muteStates,
props.e2eeSystem,
@@ -310,6 +251,7 @@ export const ActiveCall: FC<ActiveCallProps> = (props) => {
scope.end();
};
}, [
props.rtcSession,
props.matrixRoom,
props.muteStates,
props.e2eeSystem,
@@ -326,11 +268,7 @@ export const ActiveCall: FC<ActiveCallProps> = (props) => {
if (developerSettingsVm === null) return null;
return (
<ReactionsSenderProvider
vm={vm}
room={props.matrixRoom}
client={props.client}
>
<ReactionsSenderProvider vm={vm} rtcSession={props.rtcSession}>
<InCallView
{...props}
vm={vm}
@@ -342,12 +280,13 @@ export const ActiveCall: FC<ActiveCallProps> = (props) => {
};
export interface InCallViewProps {
client: SettingsClientParts;
client: MatrixClient;
vm: CallViewModel;
footerVm: ViewModel<FooterSnapshot>;
developerSettingsVm: ViewModel<DeveloperSettingsSnapshot>;
matrixInfo: MatrixInfo;
matrixRoom: Pick<MatrixRoom, "roomId">;
rtcSession: MatrixRTCSession;
matrixRoom: MatrixRoom;
muteStates: MuteStates;
onShareClick: (() => void) | null;
}
@@ -859,23 +798,31 @@ export function captureSettings(): CaptureSettings {
}
/**
* What the join needs from Element Call's configuration, settings and URL:
* What the client needs from Element Call's configuration, settings and URL:
* the SDK reads none of them itself. What to publish depends on the mute
* switches and the devices, so the caller adds that. Shared with the legacy
* SDK bundle so that a widget built from it behaves like the app.
*/
export function rtcSlotOptions(
encryptionSystem: EncryptionSystem,
): RTCSlotOptions {
return {
encryptionSystem,
// matrix_rtc_mode in config.json overrides the user's Developer Settings
// choice. We merely sample the current mode here, so the user would need
// to manually rejoin to switch to a different one.
matrixRTCMode:
(Config.get().matrix_rtc_mode as MatrixRTCMode | undefined) ??
matrixRTCModeSetting.value$.value,
};
}
export function rtcParticipationOptions(
urlParams: ReturnType<typeof useUrlParams>,
): Omit<RTCParticipationOptions, "publish"> {
const config = Config.get();
const session = config.matrix_rtc_session;
return {
// matrix_rtc_mode in config.json overrides the user's Developer Settings
// choice. We merely sample the current mode here, so the user would need
// to manually rejoin to switch to a different one.
matrixRTCMode:
(config.matrix_rtc_mode as MatrixRTCMode | undefined) ??
matrixRTCModeSetting.value$.value,
sendNotificationType: urlParams.sendNotificationType,
callIntent: urlParams.callIntent,
timings: {
+1 -1
View File
@@ -29,7 +29,7 @@ import { useRoomEncryptionSystem } from "../e2ee/sharedKeyManagement";
import { QrCode } from "../QrCode";
interface Props {
room: Pick<Room, "roomId" | "name">;
room: Room;
open: boolean;
onDismiss: () => void;
}
+1
View File
@@ -71,6 +71,7 @@ export const KnockLobbyView: FC<Props> = ({
userId: client.getUserId() ?? "",
displayName: profile.displayName,
avatarUrl: profile.avatarUrl,
roomAlias: null,
roomId: roomSummary.room_id,
roomName: roomSummary.name ?? "",
roomAvatar: roomSummary.avatar_url ?? null,
+1
View File
@@ -91,6 +91,7 @@ const matrixInfo = {
avatarUrl: "",
roomId: "!room:example.org",
roomName: "Test Room",
roomAlias: null,
roomAvatar: null,
e2eeSystem: { kind: E2eeType.NONE } satisfies EncryptionSystem,
};
+3 -6
View File
@@ -7,6 +7,7 @@ Please see LICENSE in the repository root for full details.
import { type FC, useCallback, useState, type JSX, useEffect } from "react";
import { useTranslation } from "react-i18next";
import { type MatrixClient } from "matrix-js-sdk";
import { Button } from "@vector-im/compound-web";
import classNames from "classnames";
import { logger } from "matrix-js-sdk/lib/logger";
@@ -22,11 +23,7 @@ import { Header, LeftNav, RightNav, RoomHeaderInfo } from "../Header";
import { type MatrixInfo, VideoPreview } from "./VideoPreview";
import { type MuteStates } from "../state/MuteStates";
import { InviteButton } from "../button/InviteButton";
import {
SettingsModal,
type SettingsClientParts,
defaultSettingsTab,
} from "../settings/SettingsModal";
import { SettingsModal, defaultSettingsTab } from "../settings/SettingsModal";
import { useRootSizeMatches } from "../useRootSize";
import { E2eeType } from "../e2ee/e2eeType";
import { LeaveToHomeLink } from "../button/LeaveToHomeLink";
@@ -49,7 +46,7 @@ import { captureSettings } from "./InCallView";
const noVideoTrack = constant<LocalVideoMediaTrack | null>(null);
interface Props {
client: SettingsClientParts;
client: MatrixClient;
matrixInfo: MatrixInfo;
/** The media to open the devices on; the join takes it over. */
media: LocalMedia;
+4 -29
View File
@@ -6,14 +6,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import {
type FC,
useEffect,
useMemo,
useState,
type ReactNode,
useRef,
} from "react";
import { type FC, useEffect, useState, type ReactNode, useRef } from "react";
import { type MatrixError } from "matrix-js-sdk";
import { logger } from "matrix-js-sdk/lib/logger";
import { Trans, useTranslation } from "react-i18next";
@@ -34,7 +27,6 @@ import { Link } from "../button/Link";
import { ErrorView } from "../ErrorView";
import { usePageTitle } from "../usePageTitle";
import { useRoomName } from "./useRoomName";
import { createCallMatrixBackend } from "../CallMatrixBackend";
export const RoomPage: FC = (): ReactNode => {
const urlParams = useUrlParams();
@@ -59,14 +51,9 @@ export const RoomPage: FC = (): ReactNode => {
// The page title is the page's to set, not the call's: a host embedding the
// call has a title of its own. So it is set here, for whichever room we have
// got as far as knowing about.
const loadedRoom =
groupCallState.kind === "loaded" ? groupCallState.room : null;
const backend = useMemo(
() =>
client && loadedRoom ? createCallMatrixBackend(client, loadedRoom) : null,
[client, loadedRoom],
const roomName = useRoomName(
groupCallState.kind === "loaded" ? groupCallState.rtcSession.room : null,
);
const roomName = useRoomName(loadedRoom);
usePageTitle(
roomName ??
(groupCallState.kind === "canKnock" ||
@@ -103,18 +90,6 @@ export const RoomPage: FC = (): ReactNode => {
if (optInAnalytics === null && setOptInAnalytics) setOptInAnalytics(true);
}, [optInAnalytics, setOptInAnalytics]);
useEffect(() => {
// Sanity check the room object
if (
client &&
loadedRoom !== null &&
client.getRoom(loadedRoom.roomId) !== loadedRoom
)
logger.warn(
`We've ended up with multiple rooms for the same ID (${loadedRoom.roomId}). This indicates a bug in the group call loading code, and may lead to incomplete room state.`,
);
}, [client, loadedRoom]);
const wasInWaitForInviteState = useRef<boolean>(false);
useEffect(() => {
@@ -128,7 +103,7 @@ export const RoomPage: FC = (): ReactNode => {
case "loaded":
return (
<CallView
backend={backend!}
client={client!}
rtcSession={groupCallState.rtcSession}
isPasswordlessUser={passwordlessUser}
confineToRoom={confineToRoom}
+1
View File
@@ -19,6 +19,7 @@ describe("VideoPreview", () => {
roomId: "",
roomName: "",
e2eeSystem: { kind: E2eeType.NONE },
roomAlias: null,
roomAvatar: null,
};
+1
View File
@@ -25,6 +25,7 @@ export type MatrixInfo = {
avatarUrl: string;
roomId: string;
roomName: string;
roomAlias: string | null;
roomAvatar: string | null;
e2eeSystem: EncryptionSystem;
};
+2 -4
View File
@@ -10,11 +10,9 @@ import { useCallback } from "react";
import type { JoinRule, Room } from "matrix-js-sdk";
import { useRoomState } from "./useRoomState";
export function useJoinRule(
room: Pick<Room, "getJoinRule" | "on" | "off">,
): JoinRule {
export function useJoinRule(room: Room): JoinRule {
return useRoomState(
room,
useCallback(() => room.getJoinRule(), [room]),
useCallback((state) => state.getJoinRule(), []),
);
}
+3 -8
View File
@@ -38,7 +38,6 @@ import { useUrlParams } from "../UrlParams";
export type GroupCallLoaded = {
kind: "loaded";
room: Room;
rtcSession: MatrixRTCSession;
};
@@ -328,15 +327,13 @@ export const useLoadGroupCall = (
return room;
};
const fetchOrCreateGroupCall = async (): Promise<
Pick<GroupCallLoaded, "room" | "rtcSession">
> => {
const fetchOrCreateGroupCall = async (): Promise<MatrixRTCSession> => {
const room = await fetchOrCreateRoom();
activeRoom.current = room;
logger.debug(`Fetched / joined room ${roomIdOrAlias}`);
const rtcSession = client.matrixRTC.getRoomSession(room);
return { room, rtcSession };
return rtcSession;
};
const waitForClientSyncing = async (): Promise<void> => {
@@ -370,9 +367,7 @@ export const useLoadGroupCall = (
logger.log("Start loading group call");
waitForClientSyncing()
.then(fetchOrCreateGroupCall)
.then(({ room, rtcSession }) =>
setState({ kind: "loaded", room, rtcSession }),
)
.then((rtcSession) => setState({ kind: "loaded", rtcSession }))
.then(observeMyMembership)
.catch((error) => setState({ kind: "failed", error }));
}
+1 -10
View File
@@ -10,16 +10,7 @@ import { type Room } from "matrix-js-sdk";
import { useRoomState } from "./useRoomState";
export function useRoomAvatar(
room: Pick<
Room,
// The avatar itself
| "getMxcAvatarUrl"
// `on`/`off` for its changes, for `useRoomState`
| "on"
| "off"
>,
): string | null {
export function useRoomAvatar(room: Room): string | null {
return useRoomState(
room,
useCallback(() => room.getMxcAvatarUrl(), [room]),
+3 -5
View File
@@ -12,11 +12,9 @@ import { useCallback, useSyncExternalStore } from "react";
* The room's name, kept up to date. Null when there is no room yet, for a
* caller that only sometimes has one.
*/
type RoomNameParts = Pick<Room, "name" | "on" | "off">;
export function useRoomName(room: RoomNameParts): string;
export function useRoomName(room: RoomNameParts | null): string | null;
export function useRoomName(room: RoomNameParts | null): string | null {
export function useRoomName(room: Room): string;
export function useRoomName(room: Room | null): string | null;
export function useRoomName(room: Room | null): string | null {
const subscribe = useCallback(
(onChange: () => void) => {
if (room === null) return (): void => {};
-44
View File
@@ -1,44 +0,0 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { expect, test } from "vitest";
import { act, renderHook } from "@testing-library/react";
import {
JoinRule,
RoomEvent,
type RoomState,
RoomStateEvent,
} from "matrix-js-sdk";
import { mockMatrixRoom } from "../utils/test";
import { useJoinRule } from "./useJoinRule";
import { useRoomAvatar } from "./useRoomAvatar";
// The hooks read the room, not the state the events carry
const state = {} as RoomState;
test("the join rule follows the room's state updates", () => {
let joinRule = JoinRule.Invite;
const room = mockMatrixRoom({ getJoinRule: () => joinRule });
const { result } = renderHook(() => useJoinRule(room));
expect(result.current).toBe(JoinRule.Invite);
joinRule = JoinRule.Public;
act(() => void room.emit(RoomStateEvent.Update, state));
expect(result.current).toBe(JoinRule.Public);
});
test("the avatar follows the room's state being replaced", () => {
let avatar: string | null = null;
const room = mockMatrixRoom({ getMxcAvatarUrl: () => avatar });
const { result } = renderHook(() => useRoomAvatar(room));
expect(result.current).toBeNull();
avatar = "mxc://example.org/avatar";
act(() => void room.emit(RoomEvent.CurrentStateUpdated, room, state, state));
expect(result.current).toBe("mxc://example.org/avatar");
});
+22 -17
View File
@@ -5,28 +5,33 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { useCallback, useSyncExternalStore } from "react";
import { RoomStateEvent, type Room, RoomEvent } from "matrix-js-sdk";
import { useCallback } from "react";
import {
type RoomState,
RoomStateEvent,
type Room,
RoomEvent,
} from "matrix-js-sdk";
import { useTypedEventEmitterState } from "../useEvents";
/**
* A React hook for values computed from room state.
* @param room The room.
* @param f Reads the computed value off the room.
* @param f A mapping from the current room state to the computed value.
* @returns The computed value.
*/
export function useRoomState<T>(room: Pick<Room, "on" | "off">, f: () => T): T {
const subscribe = useCallback(
(onChange: () => void) => {
// The room re-emits the updates of whichever state is current, and says
// when that state is replaced
room.on(RoomStateEvent.Update, onChange);
room.on(RoomEvent.CurrentStateUpdated, onChange);
return (): void => {
room.off(RoomStateEvent.Update, onChange);
room.off(RoomEvent.CurrentStateUpdated, onChange);
};
},
[room],
export function useRoomState<T>(room: Room, f: (state: RoomState) => T): T {
// TODO: matrix-js-sdk says that Room.currentState is deprecated, but it's not
// clear how to reactively track the current state of the room without it
const currentState = useTypedEventEmitterState(
room,
RoomEvent.CurrentStateUpdated,
useCallback(() => room.currentState, [room]),
);
return useTypedEventEmitterState(
currentState,
RoomStateEvent.Update,
useCallback(() => f(currentState), [f, currentState]),
);
return useSyncExternalStore(subscribe, f);
}
+1 -13
View File
@@ -111,20 +111,8 @@ const KeyRotationStatus: FC<{ info: KeyRotationInfo }> = ({ info }) => (
</p>
);
export type DeveloperSettingsClientParts = Pick<
MatrixClient,
| "doesServerSupportUnstableFeature"
| "getCrypto"
| "getUserId"
| "getDeviceId"
| "getDomain"
// To authenticate with a transport
| "getLivekitToken"
| "getOpenIdToken"
>;
interface Props {
client: DeveloperSettingsClientParts;
client: MatrixClient;
roomId?: string;
/** The transports the client is connected to, while in a call. */
transports?: TransportInfo[];
+3 -2
View File
@@ -6,16 +6,17 @@ Please see LICENSE in the repository root for full details.
*/
import { type FC, useCallback, useEffect, useMemo, useRef } from "react";
import { type MatrixClient } from "matrix-js-sdk";
import { useTranslation } from "react-i18next";
import { logger } from "matrix-js-sdk/lib/logger";
import { type ProfileClientParts, useProfile } from "../profile/useProfile";
import { useProfile } from "../profile/useProfile";
import { FieldRow, InputField, ErrorMessage } from "../input/Input";
import { AvatarInputField } from "../input/AvatarInputField";
import styles from "./ProfileSettingsTab.module.css";
interface Props {
client: ProfileClientParts;
client: MatrixClient;
}
export const ProfileSettingsTab: FC<Props> = ({ client }) => {
const { t } = useTranslation();
+2 -21
View File
@@ -7,8 +7,8 @@ Please see LICENSE in the repository root for full details.
import { type FC, type ReactNode, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { Button, Root as Form, Separator } from "@vector-im/compound-web";
import { type MatrixClient } from "matrix-js-sdk";
import { Button, Root as Form, Separator } from "@vector-im/compound-web";
import { Modal } from "../Modal";
import styles from "./SettingsModal.module.css";
@@ -53,31 +53,12 @@ type SettingsTab =
| "more"
| "developer";
export type SettingsClientParts = Pick<
MatrixClient,
// Profile and developer tabs
| "getUserId"
// Profile tab
| "getUser"
| "setDisplayName"
| "setAvatarUrl"
| "uploadContent"
// Developer tab
| "doesServerSupportUnstableFeature"
| "getCrypto"
| "getDeviceId"
| "getDomain"
// Developer tab, to authenticate with a transport
| "getLivekitToken"
| "getOpenIdToken"
>;
interface Props {
open: boolean;
onDismiss: () => void;
tab: SettingsTab;
onTabChange: (tab: SettingsTab) => void;
client: SettingsClientParts;
client: MatrixClient;
roomId?: string;
/** The transports the client is connected to, while in a call. */
transports?: TransportInfo[];
@@ -63,57 +63,44 @@ export type CallNotificationWrapper = {
event_id: string;
} & IRTCNotificationContent;
/**
* The call notifications the session sends with our join: what a host hands
* the call as `sentCallNotification$`.
*/
export function observeSentCallNotification$(
matrixRTCSession: Pick<MatrixRTCSession, "on" | "off">,
): Observable<CallNotificationWrapper> {
return fromEvent(
matrixRTCSession,
MatrixRTCSessionEvent.DidSendCallNotification,
(notification: CallNotificationWrapper) => notification,
);
}
export function createSentCallNotification$(
scope: ObservableScope,
sentCallNotification$: Observable<CallNotificationWrapper>,
matrixRTCSession: MatrixRTCSession,
): Behavior<CallNotificationWrapper | null> {
return scope.behavior<CallNotificationWrapper | null>(
sentCallNotification$,
const sentCallNotification$ = scope.behavior(
fromEvent(matrixRTCSession, MatrixRTCSessionEvent.DidSendCallNotification),
null,
);
) as Behavior<CallNotificationWrapper | null>;
return sentCallNotification$;
}
export function createReceivedDecline$(
matrixRoom: Pick<MatrixRoom, "_unstable_getStickyEvents" | "on" | "off">,
matrixRoom: MatrixRoom,
): Observable<MatrixEvent> {
// A decline is sticky, so after a gappy sync it arrives only in the sticky
// section, which never reaches the timeline. Racing for the first outcome
// makes a decline seen in both places harmless.
const timeline$ = fromEvent(
matrixRoom,
RoomEvent.Timeline,
(...[event]: Parameters<EventTimelineSetHandlerMap[RoomEvent.Timeline]>) =>
event,
);
const timeline$ = (
fromEvent(matrixRoom, RoomEvent.Timeline) as Observable<
Parameters<EventTimelineSetHandlerMap[RoomEvent.Timeline]>
>
).pipe(map(([event]) => event));
// The decline can arrive via sync before the send of our notification
// resolves, which is when we start listening. To guard against this, we
// include what is already in the sticky section.
const currentSticky$ = defer(() =>
from(matrixRoom._unstable_getStickyEvents()),
);
const sticky$ = fromEvent(
matrixRoom,
RoomStickyEventsEvent.Update,
(
...[added, updated]: Parameters<
RoomStickyEventsMap[RoomStickyEventsEvent.Update]
>
) => [...added, ...updated.map(({ current }) => current)],
).pipe(mergeMap((events) => events));
const sticky$ = (
fromEvent(matrixRoom, RoomStickyEventsEvent.Update) as Observable<
Parameters<RoomStickyEventsMap[RoomStickyEventsEvent.Update]>
>
).pipe(
mergeMap(([added, updated]) => [
...added,
...updated.map(({ current }) => current),
]),
);
return merge(currentSticky$, sticky$, timeline$).pipe(
filter((event) => event.getType() === EventType.RTCDecline),
);
+3 -14
View File
@@ -28,7 +28,6 @@ import {
} from "@element-hq/matrixrtc-sdk";
import {
KnownMembership,
type MatrixClient,
type Room as MatrixRoom,
type RoomMember,
RoomStateEvent,
@@ -194,15 +193,7 @@ export function initialPublishRequests(
export interface CallViewModelProps {
rtcParticipation: RTCParticipation;
/** A call is permanently tied to a single Matrix room. */
matrixRoom: Pick<
MatrixRoom,
| "roomId"
| "getMembersWithMembership"
| "_unstable_getStickyEvents"
| "on"
| "off"
>;
client: Pick<MatrixClient, "getUserId" | "getDeviceId">;
matrixRoom: MatrixRoom;
mediaDevices: MediaDevices;
muteStates: MuteStates;
encryptionSystem: EncryptionSystem;
@@ -501,7 +492,6 @@ export function createCallViewModel$(
{
rtcParticipation,
matrixRoom,
client,
mediaDevices,
muteStates,
encryptionSystem,
@@ -512,6 +502,7 @@ export function createCallViewModel$(
options: CallViewModelOptions = {},
): CallViewModel {
const logger = rootLogger.getChild("[CallViewModel]");
const client = matrixRoom.client;
const userId = client.getUserId();
const deviceId = client.getDeviceId();
if (!(userId && deviceId))
@@ -1655,9 +1646,7 @@ function isPermissionDenied(e: unknown): boolean {
}
/** The members a call can be with: those in the room and those invited. */
function roomMembers(
room: Pick<MatrixRoom, "getMembersWithMembership">,
): RoomMember[] {
function roomMembers(room: MatrixRoom): RoomMember[] {
return room
.getMembersWithMembership(KnownMembership.Join)
.concat(room.getMembersWithMembership(KnownMembership.Invite));
@@ -29,10 +29,7 @@ import {
createCallViewModel$,
type CallViewModelOptions,
} from "./CallViewModel";
import {
createSentCallNotification$,
observeSentCallNotification$,
} from "./CallNotificationLifecycle";
import { createSentCallNotification$ } from "./CallNotificationLifecycle";
import {
mockConfig,
mockMatrixRoom,
@@ -268,7 +265,6 @@ export function withCallViewModel(mode: MatrixRTCMode) {
{
rtcParticipation,
matrixRoom: room,
client: room.client,
mediaDevices,
muteStates,
encryptionSystem: { kind: E2eeType.PER_PARTICIPANT },
@@ -280,7 +276,7 @@ export function withCallViewModel(mode: MatrixRTCMode) {
autoLeaveWhenOthersLeft: false,
sentCallNotification$: createSentCallNotification$(
scope,
observeSentCallNotification$(rtcSession.asMockedSession()),
rtcSession.asMockedSession(),
),
...options,
},
+105
View File
@@ -169,6 +169,111 @@ describe("MuteState", () => {
expect(syncingValues).toContain(true);
expect(syncingValues.at(-1)).toBe(false);
});
// The bug from issue #4403: we join a call without a lobby, and the browser
// tells us about the camera only after the call has started.
// 1. Start with no camera, with the camera set to be on by default.
// 2. Set a handler, like the call does when it starts.
// 3. The camera shows up.
// The handler must be called with "on", so the call publishes the camera,
// and the button must show "on" only after the handler is done.
//
// What it guards: the button and the real camera must never disagree. In
// the bug the button jumped to "on" by itself and nobody told the call, so
// the other side saw "Waiting for media". If someone changes how the
// default is applied and forgets the handler again, this test fails.
test("asks the handler for the default of a device listed after it was set", async () => {
const available$ = new BehaviorSubject(new Map<string, DeviceLabel>());
const muteState = new MuteState(
testScope,
aDevice(available$),
true,
constant(false),
);
const handler = vi.fn(async (desired: boolean) => Promise.resolve(desired));
muteState.setHandler(handler);
expect(muteState.enabled$.value).toBe(false);
available$.next(aCamera);
// On only once the handler has acted on it
expect(muteState.enabled$.value).toBe(false);
await flushPromises();
expect(handler).toHaveBeenLastCalledWith(true);
expect(muteState.enabled$.value).toBe(true);
expect(muteState.syncing$.value).toBe(false);
});
// A camera that is unplugged and plugged back in during a call.
// 1. Start with a camera, on by default, and set a handler.
// 2. The camera goes away, then comes back.
// The handler is told "off" when the camera goes away and is not called
// again. The camera stays off: we must not turn on a camera that the user
// may have turned off before.
//
// What it guards: a privacy problem the fix for #4403 could cause. The fix
// sends the default "on" to the handler. If it did that every time a
// camera shows up, replugging a camera (or leaving earpiece mode) would
// start sending video without the user asking. This test fails if the
// default is ever applied a second time during a call.
test("leaves a device that comes back under a handler off", async () => {
const available$ = new BehaviorSubject(aCamera);
const muteState = new MuteState(
testScope,
aDevice(available$),
true,
constant(false),
);
expect(muteState.enabled$.value).toBe(true);
const handler = vi.fn(async (desired: boolean) => Promise.resolve(desired));
muteState.setHandler(handler);
available$.next(new Map());
available$.next(aCamera);
await flushPromises();
expect(handler.mock.calls).toEqual([[false]]);
expect(muteState.enabled$.value).toBe(false);
});
// The lobby case, which must keep working as before: no handler is set.
// 1. Start with no camera, with the camera set to be on by default.
// 2. The camera shows up.
// The button shows "on" right away, with no waiting.
//
// What it guards: the lobby and the join read the button's value at once
// to decide which devices to open. If the fix for #4403 made the default
// wait for a handler here too, the value would be "off" for a moment and
// the lobby would open no camera. This test fails if that happens.
test("assumes the default at once where no handler is set", () => {
const available$ = new BehaviorSubject(new Map<string, DeviceLabel>());
const muteState = new MuteState(
testScope,
aDevice(available$),
true,
constant(false),
);
available$.next(aCamera);
expect(muteState.enabled$.value).toBe(true);
});
// A device list with one camera in it.
const aCamera = new Map<string, DeviceLabel>([
["fbac11", { type: "name", name: "HD Camera" }],
]);
// A fake camera whose device list the test can change while it runs.
function aDevice(
available$: BehaviorSubject<Map<string, DeviceLabel>>,
): MediaDevice<DeviceLabel, SelectedDevice> {
return {
available$,
selected$: constant({ id: "fbac11" }),
select(): void {},
} as unknown as MediaDevice<DeviceLabel, SelectedDevice>;
}
});
describe("MuteStates", () => {
+26 -10
View File
@@ -53,6 +53,8 @@ export class MuteState<Label, Selected> {
this.handler$.next(defaultHandler);
}
private devicesConnectedBefore = false;
private readonly canControlDevices$ = combineLatest([
this.device.available$,
this.forceMute$,
@@ -86,15 +88,26 @@ export class MuteState<Label, Selected> {
};
}
// Assume the default value only once devices are actually connected
let enabled = this.enabledByDefault;
// defaultHander = no-op hanlder = "no actual device handler available"
const handlerAttached = this.handler$.value !== defaultHandler;
const deviceCameBackInCall =
handlerAttached && this.devicesConnectedBefore;
// Set the flag. A marker, that we reached this code before.
this.devicesConnectedBefore = true;
const initialWanted = deviceCameBackInCall
? false
: this.enabledByDefault;
// With a handler attached we start off and let the sync below actually call
// the handler, so the switch shows "on" only once the track is really there.
// Without one (lobby) enabled = initialWanted -> no call to handler.
let enabled = handlerAttached ? false : initialWanted;
const set$ = new Subject<boolean>();
const toggle$ = new Subject<void>();
const syncing$ = new BehaviorSubject(false);
const desired$ = merge(set$, toggle$.pipe(map(() => !enabled)));
const enabled$ = new Observable<boolean>((subscriber) => {
subscriber.next(enabled);
let latestDesired = this.enabledByDefault;
let latestDesired = initialWanted;
const sync = async (): Promise<void> => {
if (enabled === latestDesired) {
@@ -120,16 +133,19 @@ export class MuteState<Label, Selected> {
}
};
const startSync = (): void => {
syncing$.next(true);
sync().catch((err) => {
// TODO: better error handling
logger.error("MuteState: handler error", err);
});
};
const s = desired$.subscribe((desired) => {
latestDesired = desired;
if (syncing$.value === false) {
syncing$.next(true);
sync().catch((err) => {
// TODO: better error handling
logger.error("MuteState: handler error", err);
});
}
if (syncing$.value === false) startSync();
});
if (enabled !== latestDesired) startSync();
return (): void => {
s.unsubscribe();
syncing$.complete();
+23 -13
View File
@@ -5,10 +5,13 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { test, expect, vi } from "vitest";
import { test, expect } from "vitest";
import { act, render, screen } from "@testing-library/react";
import { axe } from "vitest-axe";
import { RTC_CALL_INTENT_AUDIO } from "matrix-js-sdk/lib/matrixrtc";
import {
RTC_CALL_INTENT_AUDIO,
type MatrixRTCSession,
} from "matrix-js-sdk/lib/matrixrtc";
import { BehaviorSubject } from "rxjs";
import { GridTile } from "./GridTile";
@@ -33,18 +36,25 @@ global.IntersectionObserver = class MockIntersectionObserver {
public disconnect(): void {}
} as unknown as typeof IntersectionObserver;
const fakeRoom = { roomId: "!room:example.org" };
const fakeClient = {
getUserId: () => null,
getDeviceId: () => null,
sendEvent: vi.fn(),
redactEvent: vi.fn(),
};
const fakeRtcSession = {
on: () => {},
off: () => {},
room: {
on: () => {},
off: () => {},
client: {
getUserId: () => null,
getDeviceId: () => null,
on: () => {},
off: () => {},
},
},
memberships: [],
} as unknown as MatrixRTCSession;
const callVm = {
reactions$: constant({}),
handsRaised$: constant({}),
localMember$: constant(null),
} as Partial<CallViewModel> as CallViewModel;
test("GridTile displays remote media", async () => {
@@ -58,7 +68,7 @@ test("GridTile displays remote media", async () => {
);
const { container } = render(
<ReactionsSenderProvider vm={callVm} room={fakeRoom} client={fakeClient}>
<ReactionsSenderProvider vm={callVm} rtcSession={fakeRtcSession}>
<GridTile
vm={new GridTileViewModel(constant(vm))}
onOpenProfile={() => {}}
@@ -89,7 +99,7 @@ test("GridTile displays local media", async () => {
);
const { container } = render(
<ReactionsSenderProvider vm={callVm} room={fakeRoom} client={fakeClient}>
<ReactionsSenderProvider vm={callVm} rtcSession={fakeRtcSession}>
<GridTile
vm={new GridTileViewModel(constant(vm))}
onOpenProfile={() => {}}
@@ -122,7 +132,7 @@ test("GridTile displays ringing media", async () => {
});
const { container } = render(
<ReactionsSenderProvider vm={callVm} room={fakeRoom} client={fakeClient}>
<ReactionsSenderProvider vm={callVm} rtcSession={fakeRtcSession}>
<GridTile
vm={new GridTileViewModel(constant(vm))}
onOpenProfile={() => {}}
+4 -1
View File
@@ -111,7 +111,10 @@ Please see LICENSE in the repository root for full details.
display: flex;
gap: var(--cpd-space-2x);
position: absolute;
inset-block-end: var(--cpd-space-1x);
/* Ensure that the buttons lie within the safe area */
inset-block-end: calc(
var(--call-view-safe-area-inset-bottom, 0px) + var(--cpd-space-1x)
);
inset-inline-end: var(--cpd-space-1x);
z-index: 1;
}
+21 -1
View File
@@ -35,6 +35,26 @@ export function useEventTarget<T extends Event>(
}, [target, eventType, listener, options]);
}
/**
* Shortcut for registering a listener on a TypedEventEmitter.
*/
export function useTypedEventEmitter<
Events extends string,
Arguments extends ListenerMap<Events>,
T extends Events,
>(
emitter: TypedEventEmitter<Events, Arguments>,
eventType: T,
listener: Listener<Events, Arguments, T>,
): void {
useEffect(() => {
emitter.on(eventType, listener);
return (): void => {
emitter.off(eventType, listener);
};
}, [emitter, eventType, listener]);
}
/**
* Reactively tracks a value which is recalculated whenever the provided event
* emitter emits an event. This is useful for bridging state from matrix-js-sdk
@@ -46,7 +66,7 @@ export function useTypedEventEmitterState<
T extends Events,
State,
>(
emitter: Pick<TypedEventEmitter<Events, Arguments>, "on" | "off">,
emitter: TypedEventEmitter<Events, Arguments>,
eventType: T,
getState: () => State,
): State {
+25
View File
@@ -0,0 +1,25 @@
/*
Copyright 2023, 2024 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import {
type CallMembership,
type MatrixRTCSession,
MatrixRTCSessionEvent,
} from "matrix-js-sdk/lib/matrixrtc";
import { useCallback } from "react";
import { useTypedEventEmitterState } from "./useEvents";
export function useMatrixRTCSessionMemberships(
rtcSession: MatrixRTCSession,
): CallMembership[] {
return useTypedEventEmitterState(
rtcSession,
MatrixRTCSessionEvent.MembershipsChanged,
useCallback(() => rtcSession.memberships, [rtcSession]),
);
}
+15 -21
View File
@@ -7,7 +7,6 @@ Please see LICENSE in the repository root for full details.
import { describe, expect, test } from "vitest";
import { ConnectionError } from "livekit-client";
import { UnsupportedStickyEventsEndpointError } from "matrix-js-sdk";
import {
ErrorCode as RTCErrorCode,
FailToGetOpenIdToken as RTCFailToGetOpenIdToken,
@@ -33,10 +32,10 @@ import {
InsufficientCapacityError,
LivekitConnectionError,
MatrixRTCTransportMissingError,
MembershipManagerError,
NoMatrix2AuthorizationService,
PeerConnectionTimeoutError,
SFURoomCreationRestrictedError,
StickyEventsRequiredError,
UnknownCallError,
} from "./errors";
@@ -44,6 +43,17 @@ import {
// rather than the global one, so each needs to come out translated rather than
// as a raw key.
describe("localised errors", () => {
test("MembershipManagerError describes the failure and keeps its cause", () => {
const cause = new Error("the underlying problem");
const error = new MembershipManagerError(cause);
expect(error.code).toBe(ErrorCode.INTERNAL_MEMBERSHIP_MANAGER);
expect(error.category).toBe(ErrorCategory.SYSTEM_FAILURE);
expect(error.localisedTitle).not.toContain("error.");
expect(error.localisedMessage).not.toContain("error.");
expect(error.cause).toBe(cause);
});
test("NoMatrix2AuthorizationService is a configuration problem", () => {
const cause = new Error("404");
const error = new NoMatrix2AuthorizationService(cause);
@@ -90,22 +100,8 @@ describe("fromMatrixRTCError", () => {
{
name: "membership manager failure",
input: new RTCMembershipManagerError(cause),
expected: ConnectionLostError,
code: ErrorCode.CONNECTION_LOST_ERROR,
},
{
name: "membership manager stopped by missing sticky events",
// The membership manager's scheduler wraps the original error
input: new RTCMembershipManagerError(
new Error("The MembershipManager shut down", {
cause: new UnsupportedStickyEventsEndpointError(
"Server does not support the sticky events",
"sendStickyEvent",
),
}),
),
expected: StickyEventsRequiredError,
code: ErrorCode.STICKY_EVENTS_NOT_SUPPORTED,
expected: MembershipManagerError,
code: ErrorCode.INTERNAL_MEMBERSHIP_MANAGER,
},
{
name: "OpenID token failure",
@@ -173,9 +169,7 @@ describe("fromMatrixRTCError", () => {
});
test("keeps the SDK error's cause for reporting", () => {
const error = fromMatrixRTCError(
new RTCNoMatrix2AuthorizationService(cause),
);
const error = fromMatrixRTCError(new RTCMembershipManagerError(cause));
expect(error.cause).toBe(cause);
});
+24 -5
View File
@@ -6,7 +6,6 @@ Please see LICENSE in the repository root for full details.
*/
import { type ConnectionError } from "livekit-client";
import { UnsupportedStickyEventsEndpointError } from "matrix-js-sdk";
import {
ErrorCode as RTCErrorCode,
FailToGetOpenIdToken as RTCFailToGetOpenIdToken,
@@ -29,6 +28,7 @@ export enum ErrorCode {
*/
MISSING_MATRIX_RTC_TRANSPORT = "MISSING_MATRIX_RTC_TRANSPORT",
CONNECTION_LOST_ERROR = "CONNECTION_LOST_ERROR",
INTERNAL_MEMBERSHIP_MANAGER = "INTERNAL_MEMBERSHIP_MANAGER",
FAILED_TO_START_LIVEKIT = "FAILED_TO_START_LIVEKIT",
/** LiveKit indicates that the server has hit its track limits */
INSUFFICIENT_CAPACITY_ERROR = "INSUFFICIENT_CAPACITY_ERROR",
@@ -46,6 +46,7 @@ export enum ErrorCategory {
NETWORK_CONNECTIVITY = "NETWORK_CONNECTIVITY",
CLIENT_CONFIGURATION = "CLIENT_CONFIGURATION",
UNKNOWN = "UNKNOWN",
SYSTEM_FAILURE = "SYSTEM_FAILURE",
// SYSTEM_FAILURE / FEDERATION_FAILURE ..
}
@@ -116,6 +117,27 @@ export class ConnectionLostError extends ElementCallError {
}
}
/**
* Error indicating a failure in the membership manager causing the join call
* operation to fail.
*/
export class MembershipManagerError extends ElementCallError {
/**
* Creates an instance of MembershipManagerError.
*
* @param error - The underlying error that caused the membership manager failure.
*/
public constructor(error: Error) {
super(
i18n.t("error.membership_manager"),
ErrorCode.INTERNAL_MEMBERSHIP_MANAGER,
ErrorCategory.SYSTEM_FAILURE,
i18n.t("error.membership_manager_description"),
error,
);
}
}
/**
* Error indicating that this deployment pins `matrix_rtc_mode=matrix_2_0` in
* config.json but the homeserver does not advertise MSC4354 (sticky events),
@@ -292,10 +314,7 @@ export function fromMatrixRTCError(error: MatrixRTCError): ElementCallError {
if (error instanceof RTCTransportMissingError)
return new MatrixRTCTransportMissingError(error.domain);
if (error instanceof RTCMembershipManagerError)
// The membership manager wraps what made it stop
return causeOf(error).cause instanceof UnsupportedStickyEventsEndpointError
? new StickyEventsRequiredError()
: new ConnectionLostError();
return new MembershipManagerError(causeOf(error));
if (error instanceof RTCFailToGetOpenIdToken)
return new FailToGetOpenIdToken(causeOf(error));
if (error instanceof RTCNoMatrix2AuthorizationService)
+33 -63
View File
@@ -17,8 +17,7 @@ import {
SyncState,
} from "matrix-js-sdk";
import { E2eeType, type RTCMember } from "@element-hq/matrixrtc-sdk";
import { type Behavior } from "../state/Behavior";
import { E2eeType } from "@element-hq/matrixrtc-sdk";
import {
type CallViewModel,
createCallViewModel$,
@@ -40,10 +39,7 @@ import { type RaisedHandInfo, type ReactionInfo } from "../reactions";
import { constant } from "@element-hq/matrixrtc-sdk";
import { map } from "rxjs";
import { createCallFooterViewModel } from "../components/CallFooterViewModel";
import {
createSentCallNotification$,
observeSentCallNotification$,
} from "../state/CallViewModel/CallNotificationLifecycle";
import { createSentCallNotification$ } from "../state/CallViewModel/CallNotificationLifecycle";
import { HeaderStyle } from "../UrlParams";
import { type FooterSnapshot } from "../components/CallFooter";
import { type ViewModel } from "../state/ViewModel";
@@ -58,47 +54,43 @@ export function getBasicRTCSession(
): {
rtcSession: MockRTCSession;
matrixRoom: Room;
client: MatrixClient;
rtcMemberships$: BehaviorSubject<CallMembership[]>;
/** The memberships as the slot's members. */
members$: Behavior<RTCMember[]>;
} {
const matrixRoomId = "!myRoomId:example.com";
const matrixRoomMembers = new Map(members.map((p) => [p.userId, p]));
const roomEmitter = new EventEmitter();
const clientEmitter = new EventEmitter();
const client = {
getUserId: () => localRtcMember.userId,
getDeviceId: () => localRtcMember.deviceId,
getSyncState: () => SyncState.Syncing,
getDomain: () => "example.com",
getAccessToken: () => "fake-token",
sendEvent: vitest.fn().mockResolvedValue({ event_id: "$fake:event" }),
redactEvent: vitest.fn().mockResolvedValue({ event_id: "$fake:event" }),
decryptEventIfNeeded: vitest.fn().mockResolvedValue(undefined),
on: vitest
.fn()
.mockImplementation(
(eventName: string, fn: (...args: unknown[]) => void) => {
clientEmitter.on(eventName, fn);
},
),
emit: (eventName: string, ...args: unknown[]) =>
clientEmitter.emit(eventName, ...args),
off: vitest
.fn()
.mockImplementation(
(eventName: string, fn: (...args: unknown[]) => void) => {
clientEmitter.off(eventName, fn);
},
),
} as Partial<MatrixClient> as MatrixClient;
const matrixRoom = mockMatrixRoom({
relations: {
getChildEventsForEvent: vitest.fn(),
} as Partial<RelationsContainer> as RelationsContainer,
client,
client: {
getUserId: () => localRtcMember.userId,
getDeviceId: () => localRtcMember.deviceId,
getSyncState: () => SyncState.Syncing,
getDomain: () => "example.com",
getAccessToken: () => "fake-token",
sendEvent: vitest.fn().mockResolvedValue({ event_id: "$fake:event" }),
redactEvent: vitest.fn().mockResolvedValue({ event_id: "$fake:event" }),
decryptEventIfNeeded: vitest.fn().mockResolvedValue(undefined),
on: vitest
.fn()
.mockImplementation(
(eventName: string, fn: (...args: unknown[]) => void) => {
clientEmitter.on(eventName, fn);
},
),
emit: (eventName: string, ...args: unknown[]) =>
clientEmitter.emit(eventName, ...args),
off: vitest
.fn()
.mockImplementation(
(eventName: string, fn: (...args: unknown[]) => void) => {
clientEmitter.off(eventName, fn);
},
),
} as Partial<MatrixClient> as MatrixClient,
getMember: (userId) => matrixRoomMembers.get(userId) ?? null,
getMembers: () => Array.from(matrixRoomMembers.values()),
getMembersWithMembership: () => Array.from(matrixRoomMembers.values()),
@@ -130,29 +122,10 @@ export function getBasicRTCSession(
rtcMemberships$,
);
const members$ = testScope().behavior(
rtcMemberships$.pipe(
map((memberships) =>
memberships.map((membership) => {
const inputs = {
membership,
roomMember: matrixRoomMembers.get(membership.userId),
};
return membership.userId === localRtcMember.userId &&
membership.deviceId === localRtcMember.deviceId
? mockRTCMember(true, inputs)
: mockRTCMember(false, inputs);
}),
),
),
);
return {
rtcSession: fakeRtcSession,
matrixRoom,
client,
rtcMemberships$,
members$,
};
}
@@ -173,13 +146,13 @@ export function getBasicCallViewModelEnvironment(
developerSettingsVm: ViewModel<DeveloperSettingsSnapshot>;
rtcMemberships$: BehaviorSubject<CallMembership[]>;
rtcSession: MockRTCSession;
matrixRoom: Room;
client: MatrixClient;
handRaisedSubject$: BehaviorSubject<Record<string, RaisedHandInfo>>;
reactionsSubject$: BehaviorSubject<Record<string, ReactionInfo>>;
} {
const { rtcSession, matrixRoom, client, rtcMemberships$ } =
getBasicRTCSession(members, initialRtcMemberships);
const { rtcSession, matrixRoom, rtcMemberships$ } = getBasicRTCSession(
members,
initialRtcMemberships,
);
const handRaisedSubject$ = new BehaviorSubject({});
const reactionsSubject$ = new BehaviorSubject({});
@@ -226,7 +199,6 @@ export function getBasicCallViewModelEnvironment(
{
rtcParticipation,
matrixRoom,
client,
mediaDevices,
muteStates,
encryptionSystem: { kind: E2eeType.PER_PARTICIPANT },
@@ -237,7 +209,7 @@ export function getBasicCallViewModelEnvironment(
{
sentCallNotification$: createSentCallNotification$(
scope,
observeSentCallNotification$(rtcSession.asMockedSession()),
rtcSession.asMockedSession(),
),
...callViewModelOptions,
},
@@ -257,8 +229,6 @@ export function getBasicCallViewModelEnvironment(
developerSettingsVm: createDeveloperSettingsTabViewModel(testScope(), vm),
rtcMemberships$,
rtcSession,
matrixRoom,
client,
handRaisedSubject$: handRaisedSubject$,
reactionsSubject$: reactionsSubject$,
};
+4 -23
View File
@@ -475,7 +475,6 @@ export function mockRTCMember(
userId: membership.userId,
deviceId: membership.deviceId,
memberId: membership.memberId,
matrixEventId$: constant(membership.eventId),
displayName$: constant(member.rawDisplayName ?? membership.userId),
avatarUrl$: constant(member.getMxcAvatarUrl()),
transport$: constant(transport),
@@ -506,13 +505,6 @@ export interface MockParticipationInputs {
* slot whose members are the same ones without their media.
*/
export function mockRTCParticipation(
scope: ObservableScope,
inputs: MockParticipationInputs = {},
): RTCParticipation {
return mockSlotWithParticipation(scope, inputs).participation;
}
function mockSlotWithParticipation(
scope: ObservableScope,
{
localMember$ = constant(null),
@@ -520,8 +512,8 @@ function mockSlotWithParticipation(
state$ = constant<ParticipationState>({ kind: "connected" }),
keyRotationSuppressed$ = constant(false),
connectedTransports$ = constant([]),
}: MockParticipationInputs,
): { slot: RTCSlot; participation: RTCParticipation } {
}: MockParticipationInputs = {},
): RTCParticipation {
const members$ = scope.behavior<RTCMember[]>(
combineLatest([localMember$, remoteMembers$], (local, remote) =>
local === null ? remote : [local, ...remote],
@@ -558,7 +550,7 @@ function mockSlotWithParticipation(
data$: NEVER,
};
participation$.next(participation);
return { slot, participation };
return participation;
}
/** The slot of a `mockRTCParticipation`, whose `join()` returns that participation. */
@@ -566,7 +558,7 @@ export function mockRTCSlot(
scope: ObservableScope,
inputs: MockParticipationInputs = {},
): RTCSlot {
return mockSlotWithParticipation(scope, inputs).slot;
return mockRTCParticipation(scope, inputs).slot;
}
export function mockConfig(
@@ -611,10 +603,6 @@ export class MockRTCSession extends TypedEventEmitter<
.fn()
.mockResolvedValue(undefined);
public stop: ReturnType<typeof vitest.fn> = vitest
.fn()
.mockResolvedValue(undefined);
public constructor(
public readonly room: Room,
public memberships: CallMembership[] = [],
@@ -627,13 +615,6 @@ export class MockRTCSession extends TypedEventEmitter<
return this.joined;
}
public slotDescription = { application: "m.call", id: "ROOM" };
/** The slot event's content; undefined while the room has none. */
public rtcSlot: ReturnType<MatrixRTCSession["getRtcSlot"]> = undefined;
public getRtcSlot(): ReturnType<MatrixRTCSession["getRtcSlot"]> {
return this.rtcSlot;
}
public isKeyRotationSuppressed = false;
public withMemberships(
+1
View File
@@ -101,6 +101,7 @@ describe("widget", () => {
EventType.CallSDPStreamMetadataChangedPrefix,
EventType.CallReplaces,
EventType.CallEncryptionKeysPrefix,
"m.rtc.encryption_key",
];
expect(createRoomWidgetClientSpy.mock.calls[0][1]).toStrictEqual({
+1
View File
@@ -163,6 +163,7 @@ export const initializeWidget = (
EventType.CallSDPStreamMetadataChangedPrefix,
EventType.CallReplaces,
EventType.CallEncryptionKeysPrefix,
"m.rtc.encryption_key",
];
const client = createRoomWidgetClient(