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>
This commit is contained in:
Timo K.andClaude Fable 5.2 committed 2026-11-20 14:14:40 +01:00
1 parent ffd6360277
commit b08f59affd
3 files changed
+167 -10

No files matched your search

+75
View File
@@ -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<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();
};
});
}
+70
View File
@@ -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<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);
});
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<string, DeviceLabel>());
const muteState = new MuteState(
testScope,
aDevice(available$),
true,
constant(false),
);
available$.next(aCamera);
expect(muteState.enabled$.value).toBe(true);
});
const aCamera = new Map<string, DeviceLabel>([
["fbac11", { type: "name", name: "HD Camera" }],
]);
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", () => {
+22 -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,22 @@ export class MuteState<Label, Selected> {
};
}
// 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<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 = wanted;
const sync = async (): Promise<void> => {
if (enabled === latestDesired) {
@@ -120,16 +129,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();