From b08f59affdd4cf6029eee7b3b670563d4d4debf2 Mon Sep 17 00:00:00 2001 From: "Timo K." Date: Fri, 20 Nov 2026 14:14:40 +0100 Subject: [PATCH] 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 --- playwright/widget/late-device.spec.ts | 75 +++++++++++++++++++++++++++ src/state/MuteStates.test.ts | 70 +++++++++++++++++++++++++ src/state/MuteStates.ts | 32 ++++++++---- 3 files changed, 167 insertions(+), 10 deletions(-) create mode 100644 playwright/widget/late-device.spec.ts diff --git a/playwright/widget/late-device.spec.ts b/playwright/widget/late-device.spec.ts new file mode 100644 index 000000000..4b919b0dc --- /dev/null +++ b/playwright/widget/late-device.spec.ts @@ -0,0 +1,75 @@ +/* +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" }); + +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. + */ +async function listDevicesOnlyInTheCall(page: Page): Promise { + await page.addInitScript(() => { + if (window.parent === window) return; + const devices = navigator.mediaDevices; + const enumerate = devices.enumerateDevices.bind(devices); + const inTheCall = new Promise((resolve) => { + const poll = setInterval(() => { + if (document.querySelector("[data-testid='videoTile']") === null) + return; + clearInterval(poll); + setTimeout(resolve, 500); + }, 50); + }); + devices.enumerateDevices = async (): Promise => { + await inTheCall; + return enumerate(); + }; + }); +} diff --git a/src/state/MuteStates.test.ts b/src/state/MuteStates.test.ts index efccd70be..98b2f22ee 100644 --- a/src/state/MuteStates.test.ts +++ b/src/state/MuteStates.test.ts @@ -169,6 +169,76 @@ describe("MuteState", () => { expect(syncingValues).toContain(true); expect(syncingValues.at(-1)).toBe(false); }); + + test("asks the handler for the default of a device listed after it was set", async () => { + const available$ = new BehaviorSubject(new Map()); + 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); + }); + + 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); + }); + + test("assumes the default at once where no handler is set", () => { + const available$ = new BehaviorSubject(new Map()); + const muteState = new MuteState( + testScope, + aDevice(available$), + true, + constant(false), + ); + + available$.next(aCamera); + + expect(muteState.enabled$.value).toBe(true); + }); + + const aCamera = new Map([ + ["fbac11", { type: "name", name: "HD Camera" }], + ]); + + function aDevice( + available$: BehaviorSubject>, + ): MediaDevice { + return { + available$, + selected$: constant({ id: "fbac11" }), + select(): void {}, + } as unknown as MediaDevice; + } }); describe("MuteStates", () => { diff --git a/src/state/MuteStates.ts b/src/state/MuteStates.ts index 59413b036..222c8f8ad 100644 --- a/src/state/MuteStates.ts +++ b/src/state/MuteStates.ts @@ -53,6 +53,8 @@ export class MuteState { this.handler$.next(defaultHandler); } + private devicesConnectedBefore = false; + private readonly canControlDevices$ = combineLatest([ this.device.available$, this.forceMute$, @@ -86,15 +88,22 @@ export class MuteState { }; } - // Assume the default value only once devices are actually connected - let enabled = this.enabledByDefault; + // Assume the default value only once devices are actually connected. + // Under a handler the default is asked of it like any other request, + // so that what follows the switch acts on it. Devices that come back + // then stay off: the user may have turned them off + const handled = this.handler$.value !== defaultHandler; + const wanted = + this.enabledByDefault && !(handled && this.devicesConnectedBefore); + this.devicesConnectedBefore = true; + let enabled = handled ? false : wanted; const set$ = new Subject(); const toggle$ = new Subject(); const syncing$ = new BehaviorSubject(false); const desired$ = merge(set$, toggle$.pipe(map(() => !enabled))); const enabled$ = new Observable((subscriber) => { subscriber.next(enabled); - let latestDesired = this.enabledByDefault; + let latestDesired = wanted; const sync = async (): Promise => { if (enabled === latestDesired) { @@ -120,16 +129,19 @@ export class MuteState { } }; + 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();