mirror of
https://github.com/vector-im/element-call.git
synced 2026-11-22 09:41:09 +08:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
df1156b385 |
@@ -1,78 +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, 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]);
|
||||
}
|
||||
@@ -1,95 +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, 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
-3
@@ -5,7 +5,7 @@
|
||||
"schedule:monthly",
|
||||
":enableVulnerabilityAlertsWithLabel(security)"
|
||||
],
|
||||
"addLabels": ["dependencies"],
|
||||
"labels": ["dependencies"],
|
||||
"minimumReleaseAge": "5 days",
|
||||
"packageRules": [
|
||||
{
|
||||
@@ -16,7 +16,8 @@
|
||||
{
|
||||
"groupName": "GitHub Actions",
|
||||
"matchDepTypes": ["action"],
|
||||
"pinDigests": true
|
||||
"pinDigests": true,
|
||||
"labels": ["PR-Task"]
|
||||
},
|
||||
{
|
||||
"description": "Disable Renovate for packages we want to monitor ourselves",
|
||||
@@ -69,7 +70,8 @@
|
||||
"matchUpdateTypes": ["digest"],
|
||||
"schedule": ["* 0-3 * * *"],
|
||||
"minimumReleaseAge": null,
|
||||
"automerge": true
|
||||
"automerge": true,
|
||||
"labels": ["PR-Task"]
|
||||
}
|
||||
],
|
||||
"semanticCommits": "disabled",
|
||||
|
||||
@@ -24,7 +24,7 @@ describe("createLocalMedia", () => {
|
||||
]);
|
||||
|
||||
expect(getUserMedia).toHaveBeenCalledExactlyOnceWith({
|
||||
audio: { deviceId: { exact: "mic" } },
|
||||
audio: { deviceId: "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: { exact: "other" } },
|
||||
video: { deviceId: "other" },
|
||||
});
|
||||
expect(first.stop).toHaveBeenCalled();
|
||||
expect(processor.restart).toHaveBeenCalledOnce();
|
||||
@@ -133,53 +133,7 @@ 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: { 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" },
|
||||
deviceId: "other",
|
||||
});
|
||||
});
|
||||
|
||||
@@ -273,10 +227,6 @@ 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 {
|
||||
|
||||
+14
-83
@@ -78,14 +78,11 @@ export function createLocalMedia(
|
||||
);
|
||||
bySource.set(request.source, request);
|
||||
}
|
||||
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 constraints: MediaStreamConstraints = {};
|
||||
for (const request of bySource.values())
|
||||
constraints[request.source === "microphone" ? "audio" : "video"] =
|
||||
trackConstraints(request);
|
||||
const stream = await getUserMedia(constraints);
|
||||
const captured: CapturedTrack[] = [];
|
||||
for (const track of stream.getTracks()) {
|
||||
const source: Source = track.kind === "audio" ? "microphone" : "camera";
|
||||
@@ -94,7 +91,7 @@ export function createLocalMedia(
|
||||
const captured_ = new CapturedTrack(
|
||||
source,
|
||||
track,
|
||||
constraints[source]!,
|
||||
trackConstraints(request),
|
||||
getUserMedia,
|
||||
);
|
||||
capturedTracks.set(source, captured_);
|
||||
@@ -142,50 +139,9 @@ 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 {
|
||||
// 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 },
|
||||
};
|
||||
const device = request.deviceId && { deviceId: request.deviceId };
|
||||
if (request.source === "microphone") return { ...device, ...request.capture };
|
||||
const resolution = request.capture?.resolution;
|
||||
return {
|
||||
@@ -228,12 +184,7 @@ 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();
|
||||
// `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 } }),
|
||||
};
|
||||
const constraints = { ...this.constraints, ...(deviceId && { deviceId }) };
|
||||
this.finish();
|
||||
return { track, constraints, muted: this.muted$.value, processor };
|
||||
}
|
||||
@@ -294,28 +245,12 @@ class CapturedTrack {
|
||||
for (const element of this.elements) play(element, this.shown());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
private async reacquire(constraints: MediaTrackConstraints): Promise<void> {
|
||||
this.live();
|
||||
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.constraints = { ...this.constraints, ...constraints };
|
||||
const stream = await this.getUserMedia({
|
||||
[this.track.kind]: this.constraints,
|
||||
});
|
||||
this.track.stop();
|
||||
this.track = stream.getTracks()[0];
|
||||
this.track.enabled = !this.muted$.value;
|
||||
@@ -353,11 +288,7 @@ class CapturedTrack {
|
||||
},
|
||||
setDevice: async (deviceId: string): Promise<void> => {
|
||||
if (this.track.getSettings().deviceId === deviceId) return;
|
||||
// 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 } });
|
||||
await this.reacquire({ deviceId });
|
||||
},
|
||||
};
|
||||
if (this.source === "microphone")
|
||||
|
||||
@@ -143,10 +143,7 @@ export interface LocalMediaTrack {
|
||||
* from the request where the device could not be used.
|
||||
*/
|
||||
setEnabled(enabled: boolean): Promise<boolean>;
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
/** Captures from another device. Rejects for a screen share, which has none. */
|
||||
setDevice(deviceId: string): Promise<void>;
|
||||
}
|
||||
|
||||
|
||||
@@ -210,7 +210,11 @@ 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");
|
||||
expect(switchActiveDevice).toHaveBeenCalledWith(
|
||||
"audioinput",
|
||||
"mic-2",
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
// LiveKit's media stream track is the processed one while a processor runs,
|
||||
@@ -221,23 +225,19 @@ 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: openDevice }),
|
||||
getSettings: () => ({ deviceId: "cam-1" }),
|
||||
},
|
||||
// What LiveKit shows as the track, with no device of its own
|
||||
processor: {
|
||||
processedTrack: { label: "", getSettings: () => ({}) },
|
||||
},
|
||||
}) as LocalVideoTrack;
|
||||
const switchActiveDevice = vi.fn(async (_kind: string, id: string) => {
|
||||
openDevice = id;
|
||||
return Promise.resolve(true);
|
||||
});
|
||||
const switchActiveDevice = vi.fn().mockResolvedValue(true);
|
||||
const { participant, publication, room } = fakes({
|
||||
track: camera as unknown as FakeTrack,
|
||||
});
|
||||
@@ -253,38 +253,11 @@ describe("createLivekitMediaTrack", () => {
|
||||
|
||||
await track.setDevice(deviceId);
|
||||
expect(switchActiveDevice.mock.calls).toEqual(
|
||||
switches ? [["videoinput", deviceId]] : [],
|
||||
switches ? [["videoinput", deviceId, false]] : [],
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
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,27 +208,11 @@ 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 };
|
||||
|
||||
@@ -169,111 +169,6 @@ 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", () => {
|
||||
|
||||
+10
-26
@@ -53,8 +53,6 @@ export class MuteState<Label, Selected> {
|
||||
this.handler$.next(defaultHandler);
|
||||
}
|
||||
|
||||
private devicesConnectedBefore = false;
|
||||
|
||||
private readonly canControlDevices$ = combineLatest([
|
||||
this.device.available$,
|
||||
this.forceMute$,
|
||||
@@ -88,26 +86,15 @@ export class MuteState<Label, Selected> {
|
||||
};
|
||||
}
|
||||
|
||||
// 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;
|
||||
// Assume the default value only once devices are actually connected
|
||||
let enabled = this.enabledByDefault;
|
||||
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 = initialWanted;
|
||||
let latestDesired = this.enabledByDefault;
|
||||
|
||||
const sync = async (): Promise<void> => {
|
||||
if (enabled === latestDesired) {
|
||||
@@ -133,19 +120,16 @@ 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) startSync();
|
||||
if (syncing$.value === false) {
|
||||
syncing$.next(true);
|
||||
sync().catch((err) => {
|
||||
// TODO: better error handling
|
||||
logger.error("MuteState: handler error", err);
|
||||
});
|
||||
}
|
||||
});
|
||||
if (enabled !== latestDesired) startSync();
|
||||
return (): void => {
|
||||
s.unsubscribe();
|
||||
syncing$.complete();
|
||||
|
||||
Reference in New Issue
Block a user