From c2698cbc1cfdbf29da841a842def10b5f9458e38 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 09:56:02 -0800 Subject: [PATCH 1/8] chore(quest): claim quest/m0/media-toggles.md Quest-Claim: e7e78dda8827133a From f17c545842951a2e15de7948e61ac25b097406b5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 10:24:25 -0800 Subject: [PATCH 2/8] fix: recover camera and microphone input controls Pin cancellation-safe MoQ, show capture progress and errors, and offer explicit retry after denied capture. Exercise actual Hang input controls through a separate relay-connected Chromium viewer in the check suite. Co-Authored-By: GPT-6.1 Sol --- quest/m0/README.md | 1 - quest/m0/media-toggles.md | 42 --------------------------------------- 2 files changed, 43 deletions(-) delete mode 100644 quest/m0/media-toggles.md diff --git a/quest/m0/README.md b/quest/m0/README.md index 3799fdc..2e84a1a 100644 --- a/quest/m0/README.md +++ b/quest/m0/README.md @@ -12,6 +12,5 @@ verified presence and volume bugs. Each child is independently shippable. ## Required -- [Recover media toggles](/quest/m0/media-toggles.md) - audio and video resume without reloading - [Correct favorite presence](/quest/m0/favorite-presence.md) - each room and the app badge show current participants - [Preserve speaker gain](/quest/m0/speaker-volume.md) - supported volume survives reload and mute cycles diff --git a/quest/m0/media-toggles.md b/quest/m0/media-toggles.md deleted file mode 100644 index b707577..0000000 --- a/quest/m0/media-toggles.md +++ /dev/null @@ -1,42 +0,0 @@ -# [M] Recover audio and video after repeated input toggles - -## Goal - -Enabling a camera or microphone after disabling it resumes local capture and -remote playback without reloading or rejoining. Pending permission prompts, -failed capture, device changes, and teardown must not leave the input stuck. - -## Plan - -The maintainer reports that toggling audio/video inputs can leave them stuck -forever. This was not reproduced end to end during the dependency migration; -do not close this quest based only on the newer MoQ version or passing source tests. -The AFK instruction authorizes this recommended m0 scope. - -Trace `app/src/room/local.ts`, `app/src/components/profile.tsx`, and -`app/src/controls.tsx` from requested settings through `Local.core` to -capture tracks, encoder state, catalog changes, and a second participant's decoder. -Record which stage stops progressing in a reproducible browser case. - -The upgraded MoQ keeps muted microphones subscribed and disabled video renditions -in the catalog. Its Effects await spawned tasks before rerunning, so every -pending capture/read needs unconditional cancellation cleanup. Start from the -pinned `moq/js/publish/src/source`, `moq/js/publish/src/{audio,video}/capture.ts`, -and `moq/js/room/src/local.ts`; upstream shared lifecycle fixes in MoQ rather than -copying the pipeline into Hang. - -The upgraded source's `retry.test.ts` passes, including cancellation and explicit -reenable after errors. Reuse that coverage: it does not exercise Hang's controls, -the encoder graph, or a remote player's recovery. - -Cover repeated off/on, a toggle while getUserMedia is pending, rejection followed -by retry, switching/removing a device, and leaving while capture is pending. -Verify the next successful enable yields fresh tracks and actual remote media, -and disabled/closed tracks and graphs are released. Use deterministic deferred -capture tests for races plus a browser integration regression for capture through -remote decode. A camera preview alone is insufficient evidence for remote audio. - -Distinguish requested, pending, live, and failed state in the UI using capture -outputs and errors. A terminal failure should offer a working retry rather than -an endless spinner. Choose the smallest fix supported by the reproduction; -do not add a retry loop or arbitrary timeout to conceal an unresolved wait. From d08aa5a9d622257f2d3838ee61376621dccf0d16 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 10:24:44 -0800 Subject: [PATCH 3/8] fix: restore input capture and remote playback after toggles Use the upstream cancellation fix, distinguish requested and actual capture state, and provide explicit camera and microphone retry controls. Add a Chromium regression through a local relay and separate viewer to the CI check suite. Co-Authored-By: GPT-6.1 Sol --- app/package.json | 6 +- app/src/controls.tsx | 92 ++++++++++++++++++-- app/test/media.browser.ts | 172 ++++++++++++++++++++++++++++++++++++++ app/test/media.html | 2 + app/test/media.tsx | 126 ++++++++++++++++++++++++++++ app/test/tsconfig.json | 7 ++ bun.lock | 5 ++ flake.lock | 8 +- flake.nix | 3 +- justfile | 1 + moq | 2 +- 11 files changed, 407 insertions(+), 17 deletions(-) create mode 100644 app/test/media.browser.ts create mode 100644 app/test/media.html create mode 100644 app/test/media.tsx create mode 100644 app/test/tsconfig.json diff --git a/app/package.json b/app/package.json index e010a93..5278d96 100644 --- a/app/package.json +++ b/app/package.json @@ -11,7 +11,8 @@ "deps": "bun run --cwd ../api build && bun run --cwd ../moq/js/libav build", "fix": "biome check --fix", "tauri": "tauri", - "test": "bun test src/room/metadata.test.ts" + "test": "bun test src/room/metadata.test.ts", + "test:media": "bun run deps && tsc --project test/tsconfig.json && bun test ./test/media.browser.ts" }, "license": "MIT", "dependencies": { @@ -67,7 +68,8 @@ "vite-plugin-wrangler": "^0.1.1", "wrangler": "^4.33.0", "@types/bun": "^1.4.0", - "rolldown": "^1.2.15" + "rolldown": "^1.2.15", + "playwright": "1.64.1" }, "packageManager": "bun@1.4.2" } diff --git a/app/src/controls.tsx b/app/src/controls.tsx index 315f432..ce0b957 100644 --- a/app/src/controls.tsx +++ b/app/src/controls.tsx @@ -250,11 +250,46 @@ export function Controls(props: { room: Room; local: Local; canvas: Canvas }): J ); } +function CaptureStatus(props: { + kind: "Camera" | "Microphone"; + pending: boolean; + error: Error | undefined; + retry: () => void; +}): JSX.Element { + return ( + <> + + + Starting {props.kind.toLowerCase()}… + + + + {(error) => ( + + )} + + + ); +} + export function Microphone(props: { local: Local }): JSX.Element { const toggle = () => { props.local.core.microphoneEnabled.update((prev: boolean) => !prev); }; const root = solid(props.local.camera.audio.root); + const source = solid(props.local.microphone.out.source); + const error = solid(props.local.microphone.out.error); const [showMenu, setShowMenu] = createSignal(false); const [deviceChangeIndicator, setDeviceChangeIndicator] = createSignal(false); @@ -264,6 +299,7 @@ export function Microphone(props: { local: Local }): JSX.Element { // Use device signals from the Device API const device = props.local.microphone.device; const enabled = solid(props.local.core.microphoneEnabled); + const state = createMemo(() => (!enabled() ? "off" : error() ? "failed" : source() ? "live" : "pending")); const available = solid(device.out.available); const requested = createSelector(solid(device.out.requested)); const active = createSelector(solid(device.out.active)); @@ -328,13 +364,21 @@ export function Microphone(props: { local: Local }): JSX.Element { return (
- + props.local.microphone.retry()} + /> + - + (!enabled() ? "off" : error() ? "failed" : media() ? "live" : "pending")); const [showMenu, setShowMenu] = createSignal(false); const [deviceChangeIndicator, setDeviceChangeIndicator] = createSignal(false); @@ -540,7 +596,7 @@ export function Camera(props: { local: Local; room?: Room }): JSX.Element { return (
- +
+ props.local.webcam.retry()} + /> - 1}> + 1}> { + const server = Net.createServer(); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const address = server.address(); + if (!address || typeof address === "string") throw new Error("Missing test port"); + await new Promise((resolve, reject) => server.close((error) => (error ? reject(error) : resolve()))); + return address.port; +} + +test("input controls recover capture and remote decoded media", async () => { + await Fs.mkdir(Path.join(root, ".tmp"), { recursive: true }); + const scratch = await Fs.mkdtemp(Path.join(root, ".tmp/media-")); + const relayPort = await port(); + const relay = Bun.spawn( + [ + "moq-relay", + "--listen", + "127.0.0.1:0", + "--listen-tls-generate", + "localhost", + "--web-http-listen", + `127.0.0.1:${relayPort}`, + "--auth-public", + "**", + "--drain-timeout", + "0s", + ], + { stdout: Bun.file(Path.join(scratch, "relay.log")), stderr: Bun.file(Path.join(scratch, "relay.log")) }, + ); + let server: Vite.ViteDevServer | undefined; + let browser: Playwright.Browser | undefined; + try { + server = await Vite.createServer({ + root: app, + configFile: Path.join(app, "vite.config.ts"), + server: { host: "127.0.0.1", port: 0, strictPort: false, open: false }, + }); + await server.listen(); + browser = await Playwright.chromium.launch({ + channel: "chromium", + headless: true, + args: [ + "--use-fake-device-for-media-stream", + "--use-fake-ui-for-media-stream", + "--autoplay-policy=no-user-gesture-required", + ], + }); + const publisher = await browser.newPage(); + const viewer = await browser.newPage(); + const errors: string[] = []; + for (const page of [publisher, viewer]) page.on("pageerror", (error) => errors.push(error.message)); + const url = new URL("test/media.html", server.resolvedUrls?.local[0]); + url.searchParams.set("relay", `ws://127.0.0.1:${relayPort}`); + await publisher.goto(url.href); + url.searchParams.set("viewer", ""); + await viewer.goto(url.href); + await Promise.all([publisher, viewer].map((page) => page.waitForFunction(() => window.mediaTest))); + const control = (kind: Kind) => publisher.getByRole("switch", { name: `Toggle ${kind}`, exact: true }); + const live = async (kind: Kind) => { + await publisher.waitForFunction((kind) => !!window.mediaTest.state()[kind], kind); + expect(await control(kind).getAttribute("data-state")).toBe("live"); + }; + const off = async (kind: Kind) => { + await control(kind).click(); + await publisher.waitForFunction((kind) => !window.mediaTest.state()[kind], kind); + expect(await control(kind).getAttribute("aria-checked")).toBe("false"); + }; + const decoded = async (kind: Kind) => { + const before = await viewer.evaluate(() => window.mediaTest.state()); + await viewer.waitForFunction( + ({ kind, before }) => { + const state = window.mediaTest.state(); + return kind === "camera" + ? (state.video ?? 0) > (before.video ?? 0) + 100 && + (state.videoFrames ?? 0) > (before.videoFrames ?? 0) + : (state.audio ?? 0) > (before.audio ?? 0) + 100 && + (state.audioBytes ?? 0) > (before.audioBytes ?? 0) && + state.rms > 0.001; + }, + { kind, before }, + { timeout: 15_000 }, + ); + }; + for (let cycle = 0; cycle < 3; cycle++) { + for (const kind of ["camera", "microphone"] as const) { + await control(kind).click(); + await live(kind); + } + await decoded("camera"); + await decoded("microphone"); + for (const kind of ["camera", "microphone"] as const) await off(kind); + await publisher.waitForFunction(() => window.mediaTest.state().liveTracks === 0); + } + for (const kind of ["camera", "microphone"] as const) { + // New intent must win even when the old browser permission request never settles. + await publisher.evaluate((kind) => window.mediaTest.mode(kind, "hold"), kind); + await control(kind).click(); + await publisher.waitForFunction(() => window.mediaTest.state().held === 1); + expect(await control(kind).getAttribute("data-state")).toBe("pending"); + await off(kind); + await control(kind).click(); + await live(kind); + await decoded(kind); + await publisher.evaluate(() => window.mediaTest.release()); + await publisher.waitForFunction(() => window.mediaTest.state().liveTracks === 1); + await off(kind); + // A terminal refusal has an explicit retry without a reload or a two-click reset. + await publisher.evaluate((kind) => window.mediaTest.mode(kind, "deny"), kind); + await control(kind).click(); + const retry = publisher.getByRole("button", { name: `Retry ${kind}`, exact: true }); + await retry.waitFor({ state: "visible" }); + expect(await control(kind).getAttribute("data-state")).toBe("failed"); + await retry.click(); + await live(kind); + await decoded(kind); + // Switching between browser selection and an explicit device restarts capture too. + const previous = await publisher.evaluate((kind) => window.mediaTest.state()[kind], kind); + await publisher.evaluate((kind) => window.mediaTest.choose(kind), kind); + await publisher.waitForFunction( + ({ kind, previous }) => { + const track = window.mediaTest.state()[kind]; + return track && track !== previous; + }, + { kind, previous }, + ); + await decoded(kind); + // Device loss opens a fresh track and the existing remote decoder follows it. + const before = await publisher.evaluate((kind) => window.mediaTest.state()[kind], kind); + await publisher.evaluate((kind) => window.mediaTest.end(kind), kind); + await publisher.waitForFunction( + ({ kind, before }) => { + const track = window.mediaTest.state()[kind]; + return track && track !== before; + }, + { kind, before }, + ); + await decoded(kind); + await off(kind); + } + for (const kind of ["camera", "microphone"] as const) { + await publisher.evaluate((kind) => window.mediaTest.mode(kind, "hold"), kind); + await control(kind).click(); + } + await publisher.waitForFunction(() => window.mediaTest.state().held === 2); + await publisher.evaluate(() => { + window.mediaTest.close(); + window.mediaTest.release(); + }); + await publisher.waitForFunction(() => window.mediaTest.state().liveTracks === 0); + const final = await viewer.evaluate(() => window.mediaTest.state()); + expect(final.videoError).toBeUndefined(); + expect(final.audioError).toBeUndefined(); + await viewer.evaluate(() => window.mediaTest.close()); + expect(errors).toEqual([]); + } finally { + await browser?.close(); + await server?.close(); + relay.kill(); + await relay.exited; + } +}, 90_000); diff --git a/app/test/media.html b/app/test/media.html new file mode 100644 index 0000000..acd544c --- /dev/null +++ b/app/test/media.html @@ -0,0 +1,2 @@ + +
diff --git a/app/test/media.tsx b/app/test/media.tsx new file mode 100644 index 0000000..92f1e9b --- /dev/null +++ b/app/test/media.tsx @@ -0,0 +1,126 @@ +import * as Moq from "@moq/net"; +import * as Publish from "@moq/publish"; +import { Effect } from "@moq/signals"; +import * as Watch from "@moq/watch"; +import { render } from "solid-js/web"; +import { Camera, Microphone } from "../src/controls"; +import { Local } from "../src/room/local"; + +type Kind = "camera" | "microphone"; +type Mode = "live" | "hold" | "deny"; +const params = new URLSearchParams(location.search); +const connection = new Moq.Connection({ url: new URL(params.get("relay") ?? ""), share: false }); + +const native = navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices); +const modes: Record = { camera: "live", microphone: "live" }; +const tracks: MediaStreamTrack[] = []; +const held: (() => void)[] = []; +navigator.mediaDevices.getUserMedia = async (constraints) => { + const kind = constraints?.video ? "camera" : "microphone"; + const mode = modes[kind]; + modes[kind] = "live"; + if (mode === "deny") throw new DOMException("Capture permission denied", "NotAllowedError"); + const stream = await native(constraints); + tracks.push(...stream.getTracks()); + if (mode === "hold") await new Promise((resolve) => held.push(resolve)); + return stream; +}; + +const player = params.has("viewer") + ? new Watch.Player({ + origin: connection.origin, + name: Moq.Path.from("alice/camera.hang"), + video: { canvas: document.querySelector("canvas") ?? undefined, visible: "always", backend: "2d" }, + }) + : undefined; +const local = player ? undefined : new Local({ connection, identity: Moq.Path.from("alice") }); +let dispose: (() => void) | undefined; +if (local) { + Local.join.set(true); + local.core.microphone.constraints.set({ echoCancellation: false, noiseSuppression: false, autoGainControl: false }); + const controls = document.getElementById("controls"); + if (!controls) throw new Error("Missing controls"); + dispose = render( + () => ( + <> + + + + ), + controls, + ); +} +const effects = new Effect(); +let analyser: AnalyserNode | undefined; +if (player) + effects.run((effect) => { + const root = effect.get(player.audio.out.root); + if (!root) return; + const node = root.context.createAnalyser(); + root.connect(node); + analyser = node; + effect.cleanup(() => { + root.disconnect(node); + analyser = undefined; + }); + }); +function track(kind: Kind): MediaStreamTrack | undefined { + if (kind === "camera") { + const source = local?.webcam.out.source.peek()?.video; + return source && !("frames" in source) ? Publish.Video.normalizeSource(source).track : undefined; + } + const source = local?.microphone.out.source.peek()?.audio; + return source && !Publish.Audio.isSampleSource(source) ? Publish.Audio.normalizeSource(source).track : undefined; +} +const harness = { + mode(kind: Kind, mode: Mode) { + modes[kind] = mode; + }, + release() { + for (const resume of held.splice(0)) resume(); + }, + choose(kind: Kind) { + const device = kind === "camera" ? local?.webcam.device : local?.microphone.device; + const id = device?.out.available.peek()?.[0]?.deviceId; + if (!device || !id) throw new Error("Missing capture device"); + device.preferred.set(device.preferred.peek() === id ? undefined : id); + }, + end(kind: Kind) { + const active = track(kind); + active?.stop(); + active?.dispatchEvent(new Event("ended")); + }, + state() { + const samples = new Float32Array(analyser?.fftSize ?? 0); + analyser?.getFloatTimeDomainData(samples); + const rms = samples.length + ? Math.sqrt(samples.reduce((sum, sample) => sum + sample * sample, 0) / samples.length) + : 0; + return { + rms, + camera: track("camera")?.id, + microphone: track("microphone")?.id, + liveTracks: tracks.filter((track) => track.readyState === "live").length, + held: held.length, + video: player?.video.out.timestamp.peek(), + audio: player?.audio.out.timestamp.peek(), + videoFrames: player?.video.out.stats.peek()?.frameCount, + audioBytes: player?.audio.out.stats.peek()?.bytesReceived, + videoError: player?.video.out.error.peek()?.message, + audioError: player?.audio.out.error.peek()?.message, + }; + }, + close() { + effects.close(); + dispose?.(); + local?.close(); + player?.close(); + connection.close(); + }, +}; +window.mediaTest = harness; +declare global { + interface Window { + mediaTest: typeof harness; + } +} diff --git a/app/test/tsconfig.json b/app/test/tsconfig.json new file mode 100644 index 0000000..3902db1 --- /dev/null +++ b/app/test/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../tsconfig.json", + "include": [".", "../../moq/js/common/worklet.d.ts", "../src/vite-env.d.ts"], + "compilerOptions": { + "rootDir": "../.." + } +} diff --git a/bun.lock b/bun.lock index 1b5acd9..5358ef3 100644 --- a/bun.lock +++ b/bun.lock @@ -80,6 +80,7 @@ "@typescript/lib-dom": "npm:@types/web@^0.0.345", "globals": "^17.4.0", "madge": "^8.0.0", + "playwright": "1.64.1", "rolldown": "^1.2.15", "ts-expect": "^1.3.0", "tsx": "^4.20.5", @@ -1254,6 +1255,10 @@ "platform": ["platform@1.3.6", "", {}, "sha512-fnWVljUchTro6RiCFvCXBbNhJc2NijN7oIQxbwsyL0buWJPG85v81ehlHI9fXrJsMNgTofEoWIQeClKpgxFLrg=="], + "playwright": ["playwright@1.64.1", "", { "dependencies": { "playwright-core": "1.64.1" }, "bin": { "playwright": "cli.js" } }, "sha512-HCrVhHr5QQuwYoswEUGTb7gGp522yflbv/ST8nF4sFWGuLjQCt17Q/EubpGGSNKYn1tsTCPZxqHhHCfVURu7UA=="], + + "playwright-core": ["playwright-core@1.64.1", "", { "bin": { "playwright-core": "cli.js" } }, "sha512-BC4s8y6SEVA6Q1rSxZTto8hI1KUI3xXlHGZgvGqP4IqPdI/a6FX9H29IDNz7vKo9l21b6LfYosScPL6M3xxyDQ=="], + "pluralize": ["pluralize@8.0.0", "", {}, "sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA=="], "postcss": ["postcss@8.5.32", "", { "dependencies": { "nanoid": "^3.3.20", "picocolors": "^1.1.1", "source-map-js": "^1.2.2" } }, "sha512-LJlKCCAUFKOuZxipUdYw1NzIITEsPcgyOG9LkvRJvuKj/bRzbuRY0ARXvL0pWGpsZvn8jku+LMep2eU0seHp8g=="], diff --git a/flake.lock b/flake.lock index 1aad929..d994e5b 100644 --- a/flake.lock +++ b/flake.lock @@ -42,17 +42,17 @@ "rust-overlay": "rust-overlay" }, "locked": { - "lastModified": 1794039699, - "narHash": "sha256-/Ea23xExFej6K0mlGx30/M3N82jh8RESLO1MtZlOLNg=", + "lastModified": 1794075632, + "narHash": "sha256-t8aIk5589+TIX33GeQ+b59bRr2VmmZg4/oDPv7x1zvQ=", "owner": "moq-dev", "repo": "moq", - "rev": "1feb24e3ca98e687b7111ae6c2533ae19dac33a5", + "rev": "18dbfe0f1a9c92566012976f2eb2573fb795258b", "type": "github" }, "original": { "owner": "moq-dev", "repo": "moq", - "rev": "1feb24e3ca98e687b7111ae6c2533ae19dac33a5", + "rev": "18dbfe0f1a9c92566012976f2eb2573fb795258b", "type": "github" } }, diff --git a/flake.nix b/flake.nix index af62748..0047982 100644 --- a/flake.nix +++ b/flake.nix @@ -10,7 +10,7 @@ }; inputs = { - moq.url = "github:moq-dev/moq/1feb24e3ca98e687b7111ae6c2533ae19dac33a5"; + moq.url = "github:moq-dev/moq/18dbfe0f1a9c92566012976f2eb2573fb795258b"; nixpkgs.follows = "moq/nixpkgs"; flake-utils.follows = "moq/flake-utils"; rust-overlay.follows = "moq/rust-overlay"; @@ -37,6 +37,7 @@ in { devShells.default = pkgs.mkShell { + PLAYWRIGHT_BROWSERS_PATH = "${pkgs.playwright-driver.browsers}"; nativeBuildInputs = with pkgs; [ cargo-sort cargo-shear diff --git a/justfile b/justfile index 5afb397..3c8b3a7 100644 --- a/justfile +++ b/justfile @@ -26,6 +26,7 @@ check: bun run check bun run --cwd api test bun run --cwd app test + bun run --cwd app test:media just native check # Automatically fix some issues. diff --git a/moq b/moq index 1feb24e..18dbfe0 160000 --- a/moq +++ b/moq @@ -1 +1 @@ -Subproject commit 1feb24e3ca98e687b7111ae6c2533ae19dac33a5 +Subproject commit 18dbfe0f1a9c92566012976f2eb2573fb795258b From 92516465b00cb81605fef0549243e0adf278965a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 10:30:34 -0800 Subject: [PATCH 4/8] test: recover remote video while native codec promises stay pending Inject held support and flush promises around actual native codecs, then verify a separate relay-connected viewer decodes fresh video before releasing the old promise. This reproduces the stuck effect on the previous encoder implementation. Co-Authored-By: GPT-6.1 Sol --- app/test/media.browser.ts | 24 ++++++++++++++++++++++++ app/test/media.tsx | 28 ++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/app/test/media.browser.ts b/app/test/media.browser.ts index 5301a2f..33eba32 100644 --- a/app/test/media.browser.ts +++ b/app/test/media.browser.ts @@ -102,6 +102,30 @@ test("input controls recover capture and remote decoded media", async () => { for (const kind of ["camera", "microphone"] as const) await off(kind); await publisher.waitForFunction(() => window.mediaTest.state().liveTracks === 0); } + for (const stage of ["support", "flush", "final flush"] as const) { + // Hold native codec promises while retaining real encoding, transport, and decoding. + console.info(`Recovering from a held native codec ${stage}`); + if (stage === "final flush") { + await control("camera").click(); + await live("camera"); + await decoded("camera"); + await publisher.evaluate(() => window.mediaTest.codec("flush")); + await off("camera"); + } else { + await publisher.evaluate((stage) => window.mediaTest.codec(stage), stage); + await control("camera").click(); + await publisher.waitForFunction(() => window.mediaTest.state().held > 0); + await off("camera"); + } + await publisher.waitForFunction(() => window.mediaTest.state().held > 0); + await publisher.evaluate(() => window.mediaTest.codec(undefined)); + await control("camera").click(); + await live("camera"); + await decoded("camera"); + expect(await publisher.evaluate(() => window.mediaTest.state().held)).toBeGreaterThan(0); + await publisher.evaluate(() => window.mediaTest.release()); + await off("camera"); + } for (const kind of ["camera", "microphone"] as const) { // New intent must win even when the old browser permission request never settles. await publisher.evaluate((kind) => window.mediaTest.mode(kind, "hold"), kind); diff --git a/app/test/media.tsx b/app/test/media.tsx index 92f1e9b..ddb6013 100644 --- a/app/test/media.tsx +++ b/app/test/media.tsx @@ -8,6 +8,7 @@ import { Local } from "../src/room/local"; type Kind = "camera" | "microphone"; type Mode = "live" | "hold" | "deny"; +type CodecStage = "support" | "flush"; const params = new URLSearchParams(location.search); const connection = new Moq.Connection({ url: new URL(params.get("relay") ?? ""), share: false }); @@ -15,6 +16,21 @@ const native = navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices); const modes: Record = { camera: "live", microphone: "live" }; const tracks: MediaStreamTrack[] = []; const held: (() => void)[] = []; +const hold = () => new Promise((resolve) => held.push(resolve)); +let codec: CodecStage | undefined; +const supported = VideoEncoder.isConfigSupported.bind(VideoEncoder); +VideoEncoder.isConfigSupported = async (config) => { + const pending = codec === "support"; + const result = await supported(config); + if (pending) await hold(); + return result; +}; +const flush = VideoEncoder.prototype.flush; +VideoEncoder.prototype.flush = async function (this: VideoEncoder) { + const pending = codec === "flush"; + await flush.call(this); + if (pending) await hold(); +}; navigator.mediaDevices.getUserMedia = async (constraints) => { const kind = constraints?.video ? "camera" : "microphone"; const mode = modes[kind]; @@ -22,7 +38,7 @@ navigator.mediaDevices.getUserMedia = async (constraints) => { if (mode === "deny") throw new DOMException("Capture permission denied", "NotAllowedError"); const stream = await native(constraints); tracks.push(...stream.getTracks()); - if (mode === "hold") await new Promise((resolve) => held.push(resolve)); + if (mode === "hold") await hold(); return stream; }; @@ -30,7 +46,12 @@ const player = params.has("viewer") ? new Watch.Player({ origin: connection.origin, name: Moq.Path.from("alice/camera.hang"), - video: { canvas: document.querySelector("canvas") ?? undefined, visible: "always", backend: "2d" }, + video: { + canvas: document.querySelector("canvas") ?? undefined, + visible: "always", + backend: "2d", + target: { name: "video/hd" }, + }, }) : undefined; const local = player ? undefined : new Local({ connection, identity: Moq.Path.from("alice") }); @@ -73,6 +94,9 @@ function track(kind: Kind): MediaStreamTrack | undefined { return source && !Publish.Audio.isSampleSource(source) ? Publish.Audio.normalizeSource(source).track : undefined; } const harness = { + codec(stage: CodecStage | undefined) { + codec = stage; + }, mode(kind: Kind, mode: Mode) { modes[kind] = mode; }, From 4b7b4498af5b4a31a8bc18789fbae79514450039 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 10:39:45 -0800 Subject: [PATCH 5/8] fix: handle unavailable inputs and cancel pending device selections Keep accessible capture status mounted, make the selected-device action follow requested state, and cover absent devices and pending menu cancellation. Bind the browser fixture relay ephemerally and await its listening event while draining one log stream. Co-Authored-By: GPT-6.1 Sol --- app/src/controls.tsx | 47 +++-- app/test/media.browser.ts | 421 ++++++++++++++++++++++---------------- app/test/media.tsx | 35 +++- 3 files changed, 299 insertions(+), 204 deletions(-) diff --git a/app/src/controls.tsx b/app/src/controls.tsx index ce0b957..b0d5740 100644 --- a/app/src/controls.tsx +++ b/app/src/controls.tsx @@ -6,6 +6,7 @@ import { createMemo, createSelector, createSignal, + createUniqueId, Match, onCleanup, onMount, @@ -253,16 +254,22 @@ export function Controls(props: { room: Room; local: Local; canvas: Canvas }): J function CaptureStatus(props: { kind: "Camera" | "Microphone"; pending: boolean; + unavailable: boolean; error: Error | undefined; retry: () => void; }): JSX.Element { + const status = createUniqueId(); return ( <> - - - Starting {props.kind.toLowerCase()}… - - + + {props.error + ? `${props.kind} could not start. ${props.error.message}` + : props.unavailable + ? `No ${props.kind.toLowerCase()} found.` + : props.pending + ? `Starting ${props.kind.toLowerCase()}…` + : ""} + {(error) => ( )} @@ -299,8 +304,18 @@ export function Microphone(props: { local: Local }): JSX.Element { // Use device signals from the Device API const device = props.local.microphone.device; const enabled = solid(props.local.core.microphoneEnabled); - const state = createMemo(() => (!enabled() ? "off" : error() ? "failed" : source() ? "live" : "pending")); const available = solid(device.out.available); + const state = createMemo(() => + !enabled() + ? "off" + : error() + ? "failed" + : source() + ? "live" + : available()?.length === 0 + ? "unavailable" + : "pending", + ); const requested = createSelector(solid(device.out.requested)); const active = createSelector(solid(device.out.active)); @@ -351,8 +366,8 @@ export function Microphone(props: { local: Local }): JSX.Element { // Handle device selection const selectDevice = (deviceId: string) => { - if (root() && deviceId === Settings.microphone.device.peek()) { - // Same device selected and mic is enabled - disable it + if (enabled() && deviceId === Settings.microphone.device.peek()) { + // Selecting the requested device again disables even a pending capture. props.local.core.microphoneEnabled.set(false); Settings.microphone.device.set(undefined); } else { @@ -367,6 +382,7 @@ export function Microphone(props: { local: Local }): JSX.Element { props.local.microphone.retry()} /> @@ -522,7 +538,6 @@ export function Camera(props: { local: Local; room?: Room }): JSX.Element { const media = solid(props.local.webcam.out.source); const enabled = solid(props.local.core.cameraEnabled); const error = solid(props.local.webcam.out.error); - const state = createMemo(() => (!enabled() ? "off" : error() ? "failed" : media() ? "live" : "pending")); const [showMenu, setShowMenu] = createSignal(false); const [deviceChangeIndicator, setDeviceChangeIndicator] = createSignal(false); @@ -532,6 +547,9 @@ export function Camera(props: { local: Local; room?: Room }): JSX.Element { const device = props.local.webcam.device; const available = solid(device.out.available); const noCamera = createMemo(() => enabled() && available()?.length === 0 && !media()); + const state = createMemo(() => + !enabled() ? "off" : error() ? "failed" : noCamera() ? "unavailable" : media() ? "live" : "pending", + ); const requested = createSelector(solid(device.out.requested)); const active = createSelector(solid(device.out.active)); @@ -583,8 +601,8 @@ export function Camera(props: { local: Local; room?: Room }): JSX.Element { // Handle device selection const selectDevice = (deviceId: string) => { - if (media() && deviceId === Settings.camera.device.peek()) { - // Same device selected and camera is enabled - disable it + if (enabled() && deviceId === Settings.camera.device.peek()) { + // Selecting the requested device again disables even a pending capture. props.local.core.cameraEnabled.set(false); Settings.camera.device.set(undefined); } else { @@ -607,6 +625,7 @@ export function Camera(props: { local: Local; room?: Room }): JSX.Element { props.local.webcam.retry()} /> diff --git a/app/test/media.browser.ts b/app/test/media.browser.ts index 33eba32..982a635 100644 --- a/app/test/media.browser.ts +++ b/app/test/media.browser.ts @@ -1,196 +1,259 @@ -import { expect, test } from "bun:test"; -import * as Fs from "node:fs/promises"; -import * as Net from "node:net"; +import * as Test from "bun:test"; import * as Path from "node:path"; +import * as Process from "node:process"; import * as Playwright from "playwright"; import * as Vite from "vite"; type Kind = "camera" | "microphone"; const app = Path.resolve(import.meta.dir, ".."); -const root = Path.resolve(app, ".."); - -async function port(): Promise { - const server = Net.createServer(); - await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); - const address = server.address(); - if (!address || typeof address === "string") throw new Error("Missing test port"); - await new Promise((resolve, reject) => server.close((error) => (error ? reject(error) : resolve()))); - return address.port; -} - -test("input controls recover capture and remote decoded media", async () => { - await Fs.mkdir(Path.join(root, ".tmp"), { recursive: true }); - const scratch = await Fs.mkdtemp(Path.join(root, ".tmp/media-")); - const relayPort = await port(); - const relay = Bun.spawn( - [ - "moq-relay", - "--listen", - "127.0.0.1:0", - "--listen-tls-generate", - "localhost", - "--web-http-listen", - `127.0.0.1:${relayPort}`, - "--auth-public", - "**", - "--drain-timeout", - "0s", - ], - { stdout: Bun.file(Path.join(scratch, "relay.log")), stderr: Bun.file(Path.join(scratch, "relay.log")) }, - ); - let server: Vite.ViteDevServer | undefined; - let browser: Playwright.Browser | undefined; - try { - server = await Vite.createServer({ - root: app, - configFile: Path.join(app, "vite.config.ts"), - server: { host: "127.0.0.1", port: 0, strictPort: false, open: false }, - }); - await server.listen(); - browser = await Playwright.chromium.launch({ - channel: "chromium", - headless: true, - args: [ - "--use-fake-device-for-media-stream", - "--use-fake-ui-for-media-stream", - "--autoplay-policy=no-user-gesture-required", - ], - }); - const publisher = await browser.newPage(); - const viewer = await browser.newPage(); - const errors: string[] = []; - for (const page of [publisher, viewer]) page.on("pageerror", (error) => errors.push(error.message)); - const url = new URL("test/media.html", server.resolvedUrls?.local[0]); - url.searchParams.set("relay", `ws://127.0.0.1:${relayPort}`); - await publisher.goto(url.href); - url.searchParams.set("viewer", ""); - await viewer.goto(url.href); - await Promise.all([publisher, viewer].map((page) => page.waitForFunction(() => window.mediaTest))); - const control = (kind: Kind) => publisher.getByRole("switch", { name: `Toggle ${kind}`, exact: true }); - const live = async (kind: Kind) => { - await publisher.waitForFunction((kind) => !!window.mediaTest.state()[kind], kind); - expect(await control(kind).getAttribute("data-state")).toBe("live"); - }; - const off = async (kind: Kind) => { - await control(kind).click(); - await publisher.waitForFunction((kind) => !window.mediaTest.state()[kind], kind); - expect(await control(kind).getAttribute("aria-checked")).toBe("false"); - }; - const decoded = async (kind: Kind) => { - const before = await viewer.evaluate(() => window.mediaTest.state()); - await viewer.waitForFunction( - ({ kind, before }) => { - const state = window.mediaTest.state(); - return kind === "camera" - ? (state.video ?? 0) > (before.video ?? 0) + 100 && - (state.videoFrames ?? 0) > (before.videoFrames ?? 0) - : (state.audio ?? 0) > (before.audio ?? 0) + 100 && - (state.audioBytes ?? 0) > (before.audioBytes ?? 0) && - state.rms > 0.001; +Test.test( + "input controls recover capture and remote decoded media", + async () => { + let relay: Bun.Subprocess<"ignore", "ignore", "pipe"> | undefined; + let relayOutput: Promise | undefined; + let relayLog = ""; + let server: Vite.ViteDevServer | undefined; + let browser: Playwright.Browser | undefined; + let cleanup: PromiseSettledResult[] = []; + try { + const ready = Promise.withResolvers(); + relay = Bun.spawn( + [ + "moq-relay", + "--listen", + "127.0.0.1:0", + "--listen-tls-generate", + "localhost", + "--web-http-listen", + "127.0.0.1:0", + "--auth-public", + "**", + "--drain-timeout", + "0s", + ], + { + stdin: "ignore", + stdout: "ignore", + stderr: "pipe", + env: { ...Process.env, RUST_LOG: "warn,moq_relay::web=info" }, }, - { kind, before }, - { timeout: 15_000 }, ); - }; - for (let cycle = 0; cycle < 3; cycle++) { + const stderr = relay.stderr; + relayOutput = (async () => { + try { + const decoder = new TextDecoder(); + for await (const chunk of stderr) { + relayLog += decoder.decode(chunk, { stream: true }); + const address = /listening addr=(127\.0\.0\.1:\d+) kind="http"/.exec(relayLog)?.[1]; + if (address) ready.resolve(`ws://${address}`); + } + ready.reject(new Error(`Relay exited before listening: ${relayLog}`)); + } catch (error) { + ready.reject(error); + } + })(); + const deadline = setTimeout(() => ready.reject(new Error(`Relay did not start: ${relayLog}`)), 10_000); + let relayUrl: string; + try { + relayUrl = await ready.promise; + } finally { + clearTimeout(deadline); + } + server = await Vite.createServer({ + root: app, + configFile: Path.join(app, "vite.config.ts"), + server: { host: "127.0.0.1", port: 0, strictPort: false, open: false }, + }); + await server.listen(); + browser = await Playwright.chromium.launch({ + channel: "chromium", + headless: true, + args: [ + "--use-fake-device-for-media-stream", + "--use-fake-ui-for-media-stream", + "--autoplay-policy=no-user-gesture-required", + ], + }); + const publisher = await browser.newPage(); + const viewer = await browser.newPage(); + const errors: string[] = []; + for (const page of [publisher, viewer]) page.on("pageerror", (error) => errors.push(error.message)); + const url = new URL("test/media.html", server.resolvedUrls?.local[0]); + url.searchParams.set("relay", relayUrl); for (const kind of ["camera", "microphone"] as const) { + const missing = await browser.newPage(); + url.searchParams.set("missing", kind); + await missing.goto(url.href); + await missing.waitForFunction(() => window.mediaTest); + const input = missing.getByRole("switch", { name: `Toggle ${kind}`, exact: true }); + await input.click(); + await missing.waitForFunction( + (kind) => + document.querySelector(`[aria-label="Toggle ${kind}"]`)?.getAttribute("data-state") === + "unavailable", + kind, + ); + Test.expect(await input.getAttribute("aria-busy")).toBe("false"); + await missing.evaluate(() => window.mediaTest.close()); + await missing.close(); + } + url.searchParams.delete("missing"); + await publisher.goto(url.href); + url.searchParams.set("viewer", ""); + await viewer.goto(url.href); + await Promise.all([publisher, viewer].map((page) => page.waitForFunction(() => window.mediaTest))); + const control = (kind: Kind) => publisher.getByRole("switch", { name: `Toggle ${kind}`, exact: true }); + const live = async (kind: Kind) => { + await publisher.waitForFunction((kind) => !!window.mediaTest.state()[kind], kind); + Test.expect(await control(kind).getAttribute("data-state")).toBe("live"); + }; + const off = async (kind: Kind) => { await control(kind).click(); - await live(kind); + await publisher.waitForFunction((kind) => !window.mediaTest.state()[kind], kind); + Test.expect(await control(kind).getAttribute("aria-checked")).toBe("false"); + }; + const decoded = async (kind: Kind) => { + const before = await viewer.evaluate(() => window.mediaTest.state()); + await viewer.waitForFunction( + ({ kind, before }) => { + const state = window.mediaTest.state(); + return kind === "camera" + ? (state.video ?? 0) > (before.video ?? 0) + 100 && + (state.videoFrames ?? 0) > (before.videoFrames ?? 0) + : (state.audio ?? 0) > (before.audio ?? 0) + 100 && + (state.audioBytes ?? 0) > (before.audioBytes ?? 0) && + state.rms > 0.001; + }, + { kind, before }, + { timeout: 15_000 }, + ); + }; + for (let cycle = 0; cycle < 3; cycle++) { + for (const kind of ["camera", "microphone"] as const) { + await control(kind).click(); + await live(kind); + } + await decoded("camera"); + await decoded("microphone"); + for (const kind of ["camera", "microphone"] as const) await off(kind); + await publisher.waitForFunction(() => window.mediaTest.state().liveTracks === 0); } - await decoded("camera"); - await decoded("microphone"); - for (const kind of ["camera", "microphone"] as const) await off(kind); - await publisher.waitForFunction(() => window.mediaTest.state().liveTracks === 0); - } - for (const stage of ["support", "flush", "final flush"] as const) { - // Hold native codec promises while retaining real encoding, transport, and decoding. - console.info(`Recovering from a held native codec ${stage}`); - if (stage === "final flush") { + for (const stage of ["support", "flush", "final flush"] as const) { + // Hold native codec promises while retaining real encoding, transport, and decoding. + console.info(`Recovering from a held native codec ${stage}`); + if (stage === "final flush") { + await control("camera").click(); + await live("camera"); + await decoded("camera"); + await publisher.evaluate(() => window.mediaTest.codec("flush")); + await off("camera"); + } else { + await publisher.evaluate((stage) => window.mediaTest.codec(stage), stage); + await control("camera").click(); + await publisher.waitForFunction(() => window.mediaTest.state().held > 0); + await off("camera"); + } + await publisher.waitForFunction(() => window.mediaTest.state().held > 0); + await publisher.evaluate(() => window.mediaTest.codec(undefined)); await control("camera").click(); await live("camera"); await decoded("camera"); - await publisher.evaluate(() => window.mediaTest.codec("flush")); - await off("camera"); - } else { - await publisher.evaluate((stage) => window.mediaTest.codec(stage), stage); - await control("camera").click(); - await publisher.waitForFunction(() => window.mediaTest.state().held > 0); + Test.expect(await publisher.evaluate(() => window.mediaTest.state().held)).toBeGreaterThan(0); + await publisher.evaluate(() => window.mediaTest.release()); await off("camera"); } - await publisher.waitForFunction(() => window.mediaTest.state().held > 0); - await publisher.evaluate(() => window.mediaTest.codec(undefined)); - await control("camera").click(); - await live("camera"); - await decoded("camera"); - expect(await publisher.evaluate(() => window.mediaTest.state().held)).toBeGreaterThan(0); - await publisher.evaluate(() => window.mediaTest.release()); - await off("camera"); - } - for (const kind of ["camera", "microphone"] as const) { - // New intent must win even when the old browser permission request never settles. - await publisher.evaluate((kind) => window.mediaTest.mode(kind, "hold"), kind); - await control(kind).click(); - await publisher.waitForFunction(() => window.mediaTest.state().held === 1); - expect(await control(kind).getAttribute("data-state")).toBe("pending"); - await off(kind); - await control(kind).click(); - await live(kind); - await decoded(kind); - await publisher.evaluate(() => window.mediaTest.release()); - await publisher.waitForFunction(() => window.mediaTest.state().liveTracks === 1); - await off(kind); - // A terminal refusal has an explicit retry without a reload or a two-click reset. - await publisher.evaluate((kind) => window.mediaTest.mode(kind, "deny"), kind); - await control(kind).click(); - const retry = publisher.getByRole("button", { name: `Retry ${kind}`, exact: true }); - await retry.waitFor({ state: "visible" }); - expect(await control(kind).getAttribute("data-state")).toBe("failed"); - await retry.click(); - await live(kind); - await decoded(kind); - // Switching between browser selection and an explicit device restarts capture too. - const previous = await publisher.evaluate((kind) => window.mediaTest.state()[kind], kind); - await publisher.evaluate((kind) => window.mediaTest.choose(kind), kind); - await publisher.waitForFunction( - ({ kind, previous }) => { - const track = window.mediaTest.state()[kind]; - return track && track !== previous; - }, - { kind, previous }, - ); - await decoded(kind); - // Device loss opens a fresh track and the existing remote decoder follows it. - const before = await publisher.evaluate((kind) => window.mediaTest.state()[kind], kind); - await publisher.evaluate((kind) => window.mediaTest.end(kind), kind); - await publisher.waitForFunction( - ({ kind, before }) => { - const track = window.mediaTest.state()[kind]; - return track && track !== before; - }, - { kind, before }, - ); - await decoded(kind); - await off(kind); - } - for (const kind of ["camera", "microphone"] as const) { - await publisher.evaluate((kind) => window.mediaTest.mode(kind, "hold"), kind); - await control(kind).click(); + for (const kind of ["camera", "microphone"] as const) { + // New intent must win even when the old browser permission request never settles. + await publisher.evaluate((kind) => window.mediaTest.mode(kind, "hold"), kind); + await control(kind).click(); + await publisher.waitForFunction(() => window.mediaTest.state().held === 1); + Test.expect(await control(kind).getAttribute("data-state")).toBe("pending"); + await off(kind); + await control(kind).click(); + await live(kind); + await decoded(kind); + await publisher.evaluate(() => window.mediaTest.release()); + await publisher.waitForFunction(() => window.mediaTest.state().liveTracks === 1); + await off(kind); + // A terminal refusal has an explicit retry without a reload or a two-click reset. + await publisher.evaluate((kind) => window.mediaTest.mode(kind, "deny"), kind); + await control(kind).click(); + const retry = publisher.getByRole("button", { name: `Retry ${kind}`, exact: true }); + await retry.waitFor({ state: "visible" }); + Test.expect(await control(kind).getAttribute("data-state")).toBe("failed"); + Test.expect( + await retry.evaluate( + (button) => document.getElementById(button.getAttribute("aria-describedby") ?? "")?.textContent, + ), + ).toContain("Capture permission denied"); + await retry.click(); + await live(kind); + await decoded(kind); + // Switching between browser selection and an explicit device restarts capture too. + const previous = await publisher.evaluate((kind) => window.mediaTest.state()[kind], kind); + await publisher.evaluate((kind) => window.mediaTest.choose(kind), kind); + await publisher.waitForFunction( + ({ kind, previous }) => { + const track = window.mediaTest.state()[kind]; + return track && track !== previous; + }, + { kind, previous }, + ); + await decoded(kind); + if (kind === "microphone") { + // Selecting the current device can also cancel a pending request from the menu. + const selected = await publisher.evaluate(() => window.mediaTest.state().selectedMicrophone); + if (!selected) throw new Error("Missing selected microphone"); + await off(kind); + await publisher.evaluate(() => window.mediaTest.mode("microphone", "hold")); + await control(kind).click(); + await publisher.waitForFunction(() => window.mediaTest.state().held === 1); + await publisher.getByLabel("Microphone settings", { exact: true }).click(); + await publisher.getByRole("button", { name: selected, exact: true }).click(); + Test.expect(await control(kind).getAttribute("aria-checked")).toBe("false"); + await publisher.evaluate(() => window.mediaTest.release()); + await publisher.waitForFunction(() => window.mediaTest.state().liveTracks === 0); + await control(kind).click(); + await live(kind); + await decoded(kind); + } + // Device loss opens a fresh track and the existing remote decoder follows it. + const before = await publisher.evaluate((kind) => window.mediaTest.state()[kind], kind); + await publisher.evaluate((kind) => window.mediaTest.end(kind), kind); + await publisher.waitForFunction( + ({ kind, before }) => { + const track = window.mediaTest.state()[kind]; + return track && track !== before; + }, + { kind, before }, + ); + await decoded(kind); + await off(kind); + } + for (const kind of ["camera", "microphone"] as const) { + await publisher.evaluate((kind) => window.mediaTest.mode(kind, "hold"), kind); + await control(kind).click(); + } + await publisher.waitForFunction(() => window.mediaTest.state().held === 2); + await publisher.evaluate(() => { + window.mediaTest.close(); + window.mediaTest.release(); + }); + await publisher.waitForFunction(() => window.mediaTest.state().liveTracks === 0); + const final = await viewer.evaluate(() => window.mediaTest.state()); + Test.expect(final.videoError).toBeUndefined(); + Test.expect(final.audioError).toBeUndefined(); + await viewer.evaluate(() => window.mediaTest.close()); + Test.expect(errors).toEqual([]); + } catch (error) { + console.error(relayLog); + throw error; + } finally { + relay?.kill(); + cleanup = await Promise.allSettled([browser?.close(), server?.close(), relay?.exited, relayOutput]); } - await publisher.waitForFunction(() => window.mediaTest.state().held === 2); - await publisher.evaluate(() => { - window.mediaTest.close(); - window.mediaTest.release(); - }); - await publisher.waitForFunction(() => window.mediaTest.state().liveTracks === 0); - const final = await viewer.evaluate(() => window.mediaTest.state()); - expect(final.videoError).toBeUndefined(); - expect(final.audioError).toBeUndefined(); - await viewer.evaluate(() => window.mediaTest.close()); - expect(errors).toEqual([]); - } finally { - await browser?.close(); - await server?.close(); - relay.kill(); - await relay.exited; - } -}, 90_000); + const failures = cleanup.filter((result) => result.status === "rejected").map((result) => result.reason); + if (failures.length) throw new AggregateError(failures, "Media test cleanup failed"); + }, + 90_000, +); diff --git a/app/test/media.tsx b/app/test/media.tsx index ddb6013..863704e 100644 --- a/app/test/media.tsx +++ b/app/test/media.tsx @@ -1,10 +1,11 @@ import * as Moq from "@moq/net"; import * as Publish from "@moq/publish"; -import { Effect } from "@moq/signals"; +import * as Signals from "@moq/signals"; import * as Watch from "@moq/watch"; -import { render } from "solid-js/web"; -import { Camera, Microphone } from "../src/controls"; -import { Local } from "../src/room/local"; +import * as SolidWeb from "solid-js/web"; +import * as Controls from "../src/controls"; +import * as Room from "../src/room/local"; +import * as Settings from "../src/settings"; type Kind = "camera" | "microphone"; type Mode = "live" | "hold" | "deny"; @@ -12,8 +13,16 @@ type CodecStage = "support" | "flush"; const params = new URLSearchParams(location.search); const connection = new Moq.Connection({ url: new URL(params.get("relay") ?? ""), share: false }); +if (params.has("missing")) { + const enumerate = navigator.mediaDevices.enumerateDevices.bind(navigator.mediaDevices); + navigator.mediaDevices.enumerateDevices = async () => + (await enumerate()).filter( + (device) => device.kind !== (params.get("missing") === "camera" ? "videoinput" : "audioinput"), + ); +} const native = navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices); const modes: Record = { camera: "live", microphone: "live" }; +if (params.has("missing")) modes[params.get("missing") === "camera" ? "camera" : "microphone"] = "hold"; const tracks: MediaStreamTrack[] = []; const held: (() => void)[] = []; const hold = () => new Promise((resolve) => held.push(resolve)); @@ -54,24 +63,24 @@ const player = params.has("viewer") }, }) : undefined; -const local = player ? undefined : new Local({ connection, identity: Moq.Path.from("alice") }); +const local = player ? undefined : new Room.Local({ connection, identity: Moq.Path.from("alice") }); let dispose: (() => void) | undefined; if (local) { - Local.join.set(true); + Room.Local.join.set(true); local.core.microphone.constraints.set({ echoCancellation: false, noiseSuppression: false, autoGainControl: false }); const controls = document.getElementById("controls"); if (!controls) throw new Error("Missing controls"); - dispose = render( + dispose = SolidWeb.render( () => ( <> - - + + ), controls, ); } -const effects = new Effect(); +const effects = new Signals.Effect(); let analyser: AnalyserNode | undefined; if (player) effects.run((effect) => { @@ -107,7 +116,8 @@ const harness = { const device = kind === "camera" ? local?.webcam.device : local?.microphone.device; const id = device?.out.available.peek()?.[0]?.deviceId; if (!device || !id) throw new Error("Missing capture device"); - device.preferred.set(device.preferred.peek() === id ? undefined : id); + const preferred = Settings.default[kind].device; + preferred.set(preferred.peek() === id ? undefined : id); }, end(kind: Kind) { const active = track(kind); @@ -123,6 +133,9 @@ const harness = { return { rms, camera: track("camera")?.id, + selectedMicrophone: local?.microphone.device.out.available + .peek() + ?.find((device) => device.deviceId === Settings.default.microphone.device.peek())?.label, microphone: track("microphone")?.id, liveTracks: tracks.filter((track) => track.readyState === "live").length, held: held.length, From 43829fea46f89ff899a1ace578f692d5dd83f697 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 10:42:58 -0800 Subject: [PATCH 6/8] fix: announce missing camera once Keep the persistent capture live region as the sole camera availability announcement, while retaining the visible no-camera hint. Co-Authored-By: GPT-6.1 Sol --- app/src/controls.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/controls.tsx b/app/src/controls.tsx index b0d5740..3cbe709 100644 --- a/app/src/controls.tsx +++ b/app/src/controls.tsx @@ -616,7 +616,6 @@ export function Camera(props: { local: Local; room?: Room }): JSX.Element {
No camera found. Connect a webcam to turn on video. From 0207acc5ccacf5ffa2b8fcf553918d26f46933d2 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 10:43:34 -0800 Subject: [PATCH 7/8] style: format camera availability hint Co-Authored-By: GPT-6.1 Sol --- app/src/controls.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/src/controls.tsx b/app/src/controls.tsx index 3cbe709..43c5577 100644 --- a/app/src/controls.tsx +++ b/app/src/controls.tsx @@ -615,9 +615,7 @@ export function Camera(props: { local: Local; room?: Room }): JSX.Element { return (
-
+
No camera found. Connect a webcam to turn on video.
From c54f9c08e7664d502b942ac19f5258ecf5e0ee18 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 14:50:22 -0800 Subject: [PATCH 8/8] fix: align capture recovery with revised MoQ lifecycle Wait for the disabled capture acknowledgement before enabling Retry, cancel superseded handshakes, and parse terminal-styled relay startup records in CI. Co-Authored-By: GPT-6.1 Sol --- app/src/capture.test.ts | 74 +++++++++++++++++++++++++++++++++++++++ app/src/capture.ts | 46 ++++++++++++++++++++++++ app/src/controls.tsx | 21 +++++++++-- app/test/media.browser.ts | 18 +++++++++- flake.lock | 8 ++--- flake.nix | 2 +- moq | 2 +- 7 files changed, 162 insertions(+), 9 deletions(-) create mode 100644 app/src/capture.test.ts create mode 100644 app/src/capture.ts diff --git a/app/src/capture.test.ts b/app/src/capture.test.ts new file mode 100644 index 0000000..4dabe79 --- /dev/null +++ b/app/src/capture.test.ts @@ -0,0 +1,74 @@ +import * as Test from "bun:test"; +import * as Signals from "@moq/signals"; +import * as Capture from "./capture"; + +// Drain signal notifications without wall-clock sleeps or production deadlines. +async function flush(): Promise { + for (let i = 0; i < 20; i++) await Promise.resolve(); +} + +function fixture() { + const enabled = new Signals.Signal(true); + const error = new Signals.Signal(new Error("Permission denied")); + const device = new Signals.Signal("first"); + const constraints = new Signals.Signal({ width: 640 }); + const retry = new Capture.Retry({ enabled, error, settings: [device, constraints] }); + return { enabled, error, device, constraints, retry }; +} + +Test.test("retry waits for the capture to process disabled before enabling", async () => { + const capture = fixture(); + try { + capture.retry.start(); + await flush(); + Test.expect(capture.enabled.peek()).toBe(false); + capture.error.set(undefined); + await flush(); + Test.expect(capture.enabled.peek()).toBe(true); + } finally { + capture.retry.close(); + } +}); + +Test.test("closed and superseded retries never re-enable a late acknowledgement", async () => { + for (const supersede of ["close", "enable", "device", "constraints"] as const) { + const capture = fixture(); + try { + capture.retry.start(); + await flush(); + if (supersede === "close") capture.retry.close(); + if (supersede === "enable") capture.enabled.set(true); + if (supersede === "device") capture.device.set("second"); + if (supersede === "constraints") capture.constraints.set({ width: 1280 }); + await flush(); + capture.enabled.set(false); + capture.error.set(undefined); + await flush(); + Test.expect(capture.enabled.peek()).toBe(false); + } finally { + capture.retry.close(); + } + } +}); + +Test.test("a stale retry click cannot restart a recovered or disabled capture", async () => { + const capture = fixture(); + try { + capture.error.set(undefined); + capture.retry.start(); + Test.expect(capture.enabled.peek()).toBe(true); + capture.enabled.set(false); + capture.error.set(new Error("Old refusal")); + capture.retry.start(); + capture.error.set(undefined); + await flush(); + Test.expect(capture.enabled.peek()).toBe(false); + capture.retry.close(); + capture.enabled.set(true); + capture.error.set(new Error("Old refusal")); + capture.retry.start(); + Test.expect(capture.enabled.peek()).toBe(true); + } finally { + capture.retry.close(); + } +}); diff --git a/app/src/capture.ts b/app/src/capture.ts new file mode 100644 index 0000000..85f201e --- /dev/null +++ b/app/src/capture.ts @@ -0,0 +1,46 @@ +import * as Signals from "@moq/signals"; + +// A failed capture refunds its retry budget only after processing enabled=false. +// Wait for its cleared error as that acknowledgement before enabling it again. +interface Props { + enabled: Signals.Signal; + error: Signals.Getter; + settings: readonly Signals.Getter[]; +} + +export class Retry { + #attempt: Signals.Effect | undefined; + #closed = false; + #props: Props; + + constructor(props: Props) { + this.#props = props; + } + + start(): void { + this.#attempt?.close(); + const { enabled, error, settings } = this.#props; + if (this.#closed || !enabled.peek() || !error.peek()) return; + + const attempt = new Signals.Effect(); + this.#attempt = attempt; + const requested = settings.map((setting) => setting.peek()); + for (const setting of settings) attempt.cleanup(setting.subscribe(() => attempt.close())); + attempt.cleanup(enabled.subscribe((value) => value && attempt.close())); + attempt.cleanup( + error.subscribe((value) => { + if (value || attempt.abort.aborted) return; + const current = + !enabled.peek() && settings.every((setting, index) => setting.peek() === requested[index]); + attempt.close(); + if (current) enabled.set(true); + }), + ); + enabled.set(false); + } + + close(): void { + this.#closed = true; + this.#attempt?.close(); + } +} diff --git a/app/src/controls.tsx b/app/src/controls.tsx index 43c5577..dedd2c9 100644 --- a/app/src/controls.tsx +++ b/app/src/controls.tsx @@ -14,6 +14,7 @@ import { Switch, } from "solid-js"; import type { JSX } from "solid-js/jsx-runtime"; +import * as Capture from "./capture"; import { MemeSelector } from "./components/meme-selector"; import Tooltip from "./components/tooltip"; import { Tutorial } from "./components/tutorial"; @@ -289,6 +290,16 @@ function CaptureStatus(props: { } export function Microphone(props: { local: Local }): JSX.Element { + const retry = new Capture.Retry({ + enabled: props.local.core.microphoneEnabled, + error: props.local.microphone.out.error, + settings: [ + Settings.microphone.device, + props.local.microphone.device.preferred, + props.local.microphone.constraints, + ], + }); + onCleanup(() => retry.close()); const toggle = () => { props.local.core.microphoneEnabled.update((prev: boolean) => !prev); }; @@ -384,7 +395,7 @@ export function Microphone(props: { local: Local }): JSX.Element { pending={state() === "pending"} unavailable={state() === "unavailable"} error={enabled() ? error() : undefined} - retry={() => props.local.microphone.retry()} + retry={() => retry.start()} />