From 1105e778d47d7f4a1ccd2b9071223cb0e72b1df0 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 10:13:54 -0800 Subject: [PATCH 1/5] fix(publish): cancel superseded capture codec work Close cancelled probes and stop teardown flushes from holding resume or shutdown. Expose explicit camera and microphone retries for capture failure controls. Co-Authored-By: GPT-6.1 Sol --- js/publish/api/index.api.md | 2 + js/publish/src/source/camera.ts | 6 ++ js/publish/src/source/microphone.ts | 6 ++ js/publish/src/source/retry.test.ts | 21 +++++++ js/publish/src/video/encoder.test.ts | 86 ++++++++++++++++++++++++++++ js/publish/src/video/encoder.ts | 53 +++++++++++++---- 6 files changed, 163 insertions(+), 11 deletions(-) diff --git a/js/publish/api/index.api.md b/js/publish/api/index.api.md index 39b742a0458..aeee070dc72 100644 --- a/js/publish/api/index.api.md +++ b/js/publish/api/index.api.md @@ -247,6 +247,7 @@ export namespace Source { readonly device: Source.Device<"video">; readonly in: Signals.Readonlys; readonly out: Signals.Readonlys; + retry(): void; } export type CameraInput = { enabled: Signals.Getter; @@ -311,6 +312,7 @@ export namespace Source { readonly device: Source.Device<"audio">; readonly in: Signals.Readonlys; readonly out: Signals.Readonlys; + retry(): void; } export type MicrophoneInput = { enabled: Signals.Getter; diff --git a/js/publish/src/source/camera.ts b/js/publish/src/source/camera.ts index d79dd29fe29..3ae02c7b146 100644 --- a/js/publish/src/source/camera.ts +++ b/js/publish/src/source/camera.ts @@ -172,6 +172,12 @@ export class Camera { }); } + /** Retry a failed capture after an explicit user action. */ + retry(): void { + this.#out.error.set(undefined); + this.#retry.refund(); + } + /** Stop the capture and release the device. */ close() { this.#signals.close(); diff --git a/js/publish/src/source/microphone.ts b/js/publish/src/source/microphone.ts index 3a3844ac227..336644158e5 100644 --- a/js/publish/src/source/microphone.ts +++ b/js/publish/src/source/microphone.ts @@ -142,6 +142,12 @@ export class Microphone { }); } + /** Retry a failed capture after an explicit user action. */ + retry(): void { + this.#out.error.set(undefined); + this.#retry.refund(); + } + /** Stop the capture and release the device. */ close() { this.#signals.close(); diff --git a/js/publish/src/source/retry.test.ts b/js/publish/src/source/retry.test.ts index f2487d61b4d..6ed1bb86751 100644 --- a/js/publish/src/source/retry.test.ts +++ b/js/publish/src/source/retry.test.ts @@ -339,6 +339,27 @@ for (const [kind, create] of [ }); } + test(`${kind} retries a refusal explicitly without changing the enabled setting`, async () => { + using _time = fakeTime(); + using media = install(new FakeMediaDevices()); + media.denied = true; + const enabled = new Signal(true); + const source = create(enabled); + try { + await settle(); + expect(source.out.error.peek()?.name).toBe("NotAllowedError"); + media.denied = false; + source.retry(); + await settle(); + expect(enabled.peek()).toBe(true); + expect(source.out.error.peek()).toBeUndefined(); + expect(source.out.source.peek()).toBeDefined(); + expect(media.attempts).toBe(2); + } finally { + source.close(); + } + }); + test(`${kind} reports a refusal and stops retrying until capture is reset`, async () => { using time = fakeTime(); using media = install(new FakeMediaDevices()); diff --git a/js/publish/src/video/encoder.test.ts b/js/publish/src/video/encoder.test.ts index 80e4f33739d..b089ccd8a40 100644 --- a/js/publish/src/video/encoder.test.ts +++ b/js/publish/src/video/encoder.test.ts @@ -140,6 +140,43 @@ function installFakeVideoEncoder() { }; } +for (const stage of ["support", "flush"] as const) { + test(`a cancelled ${stage} probe cannot block the next capture`, async () => { + using _videoEncoder = installFakeVideoEncoder(); + const pending = Promise.withResolvers(); + const held = + stage === "support" + ? spyOn(FakeVideoEncoder, "isConfigSupported").mockImplementationOnce(() => pending.promise) + : spyOn(FakeVideoEncoder.prototype, "flush").mockImplementationOnce(() => pending.promise); + const enabled = new Signal(true); + const encoder = new Encoder("video", { + enabled, + capture: { + in: { source: new Signal({ getSettings: () => ({ frameRate: 30 }), getConstraints: () => ({}) }) }, + out: { display: new Signal({ width: 640, height: 480 }), frames: new Signal(undefined) }, + } as never, + config: { codec: "avc1" }, + }); + try { + await settle(); + expect(held).toHaveBeenCalledTimes(1); + enabled.set(false); + await settle(); + if (stage === "flush") expect(FakeVideoEncoder.instances[0]?.state).toBe("closed"); + enabled.set(true); + await settle(); + expect(encoder.out.catalog.peek()?.codec).toStartWith("avc1"); + // The abandoned browser promise still has not resolved. + expect(held.mock.calls.length).toBeGreaterThan(1); + } finally { + encoder.close(); + held.mockRestore(); + pending.reject(new DOMException("closed", "AbortError")); + await settle(); + } + }); +} + test("encoding tracks encoder config in its child effect", async () => { using _videoEncoder = installFakeVideoEncoder(); const warn = spyOn(console, "warn").mockImplementation(() => {}); @@ -1149,6 +1186,55 @@ async function installRecording() { }; } +for (const stop of ["resume", "close"] as const) { + test(`a stalled final black frame cannot block ${stop}`, async () => { + using recording = await installRecording(); + const enabled = new Signal(true); + const track = new Moq.Track.Producer("video").accept({ timescale: Moq.Time.Timescale.MICRO }); + const rendition = { + slot: new Signal(undefined), + config: new Signal(undefined), + stats: new Signal(undefined), + track: new Signal(track), + join: new Signal(undefined), + close: () => {}, + }; + const encoder = new Encoder("video", { + enabled, + capture: recording.capture as never, + broadcast: { video: () => rendition, order: new Order(), join: new Signal(undefined) } as never, + }); + const pending = Promise.withResolvers(); + const closed = spyOn(RecordingVideoEncoder.prototype, "close"); + let flush: ReturnType> | undefined; + try { + await settle(); + await recording.send(0); + flush = spyOn(RecordingVideoEncoder.prototype, "flush").mockImplementationOnce(() => pending.promise); + enabled.set(false); + await settle(); + expect(flush).toHaveBeenCalledTimes(1); + const before = closed.mock.calls.length; + if (stop === "close") encoder.close(); + else enabled.set(true); + await settle(); + expect(closed.mock.calls.length).toBeGreaterThan(before); + if (stop === "resume") { + await recording.send(300); + expect(recording.keys).toContain(300); + } + } finally { + encoder.close(); + track.finish(); + track.close(); + flush?.mockRestore(); + closed.mockRestore(); + pending.resolve(); + await settle(); + } + }); +} + async function recordKeyframes( info: Partial, run: (ctx: { diff --git a/js/publish/src/video/encoder.ts b/js/publish/src/video/encoder.ts index 3e63ac93f3c..d3ed309423e 100644 --- a/js/publish/src/video/encoder.ts +++ b/js/publish/src/video/encoder.ts @@ -8,7 +8,9 @@ import { type Getter, getter, type Inputs, + Once, type Readonlys, + race, readonlys, Signal, } from "@moq/signals"; @@ -599,8 +601,17 @@ export class Encoder { } finally { frame.close(); } - // A rejected flush already reported through the error callback. - await encoder.flush().catch(() => {}); + // A cosmetic frame must never hold the next enable or final close behind a codec + // flush. The old codec is closed below even if the browser never settles it. + const resumed = new Once(); + const unwatch = this.in.enabled.subscribe((enabled) => { + if (enabled && resumed.peek() === undefined) resumed.set(true); + }); + try { + await race([encoder.flush().catch(() => {}), resumed, this.#signals.closed]); + } finally { + unwatch(); + } } } finally { if (encoder.state !== "closed") encoder.close(); @@ -684,7 +695,9 @@ export class Encoder { try { let detected: Probe; try { - detected = await this.#bestCodec(dimensions, target); + const result = await this.#bestCodec(dimensions, target, effect); + if (!result) return; + detected = result; } catch (err) { // A capture that outgrew the accepted ceiling to a size the hardware refuses encodes scaled // down to fit it, until a smaller capture reruns the probe. Re-probing the ceiling with the @@ -702,7 +715,9 @@ export class Encoder { `video ${dimensions.width}x${dimensions.height} unsupported, keeping ${accepted.width}x${accepted.height}:`, err, ); - detected = await this.#bestCodec(accepted, target); + const result = await this.#bestCodec(accepted, target, effect); + if (!result) return; + detected = result; } if (superseded.aborted) return; @@ -817,7 +832,11 @@ export class Encoder { } // Try to determine the best config for the given settings. - async #bestCodec(dimensions: { width: number; height: number }, target: Target): Promise { + async #bestCodec( + dimensions: { width: number; height: number }, + target: Target, + effect: Effect, + ): Promise { // A list of codecs to try, in order of preference. Only full RFC 6381 strings: Chrome, Firefox, // and Safari all refuse a bare `avc1` or `vp09`, and native players can't decode without the profile. const HARDWARE_CODECS = [ @@ -897,10 +916,12 @@ export class Encoder { hevc: codec.startsWith("hev1") ? { format: "annexb" } : undefined, }; - const { supported } = await VideoEncoder.isConfigSupported(config); - if (!supported) continue; + const support = await effect.race(VideoEncoder.isConfigSupported(config)); + if (!support) return; + if (!support.supported) continue; - const probed = await probeEncoder(config); + const probed = await probeEncoder(config, effect); + if (!probed) return; if ("reported" in probed) return { config, reported: probed.reported }; console.warn(`moq-publish: skipping ${codec}: ${probed.refused}`); } @@ -1022,7 +1043,10 @@ const PROBE_FRAMES = 4; // length-prefixed NAL units with an hvcC description, which no subscriber can decode. A // description means length-prefixed output (per the AVC and HEVC WebCodecs registrations), which // also catches a four-byte length that happens to read as a start code, such as `00 00 01 2c`. -async function probeEncoder(config: VideoEncoderConfig): Promise<{ reported: string } | { refused: string }> { +async function probeEncoder( + config: VideoEncoderConfig, + effect: Effect, +): Promise<{ reported: string } | { refused: string } | undefined> { let reported: string | undefined; let reordered = false; let last: number | undefined; @@ -1043,6 +1067,12 @@ async function probeEncoder(config: VideoEncoderConfig): Promise<{ reported: str error: () => {}, }); + // A superseded probe must release its codec even if the browser never settles flush(). + const close = () => { + if (encoder.state !== "closed") encoder.close(); + }; + effect.cleanup(close); + try { encoder.configure(config); @@ -1063,9 +1093,10 @@ async function probeEncoder(config: VideoEncoderConfig): Promise<{ reported: str } } - await encoder.flush(); + await effect.race(encoder.flush()); + if (effect.abort.aborted) return; } finally { - if (encoder.state !== "closed") encoder.close(); + close(); } if (!reported) throw new Error(`${config.codec} encoder reported no codec string`); From 18dbfe0f1a9c92566012976f2eb2573fb795258b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 10:20:32 -0800 Subject: [PATCH 2/5] fix(publish): close stalled codecs when broadcasts detach Cancel cosmetic flushes with the owning track effect and reuse the signal change wait. Cover broadcast detach with a deferred flush that never settles. Co-Authored-By: GPT-6.1 Sol --- js/publish/src/video/encoder.test.ts | 10 ++++++++-- js/publish/src/video/encoder.ts | 13 ++----------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/js/publish/src/video/encoder.test.ts b/js/publish/src/video/encoder.test.ts index b089ccd8a40..3e4f384e702 100644 --- a/js/publish/src/video/encoder.test.ts +++ b/js/publish/src/video/encoder.test.ts @@ -1186,7 +1186,7 @@ async function installRecording() { }; } -for (const stop of ["resume", "close"] as const) { +for (const stop of ["resume", "close", "detach"] as const) { test(`a stalled final black frame cannot block ${stop}`, async () => { using recording = await installRecording(); const enabled = new Signal(true); @@ -1199,10 +1199,15 @@ for (const stop of ["resume", "close"] as const) { join: new Signal(undefined), close: () => {}, }; + const broadcast = new Signal({ + video: () => rendition, + order: new Order(), + join: new Signal(undefined), + } as never); const encoder = new Encoder("video", { enabled, capture: recording.capture as never, - broadcast: { video: () => rendition, order: new Order(), join: new Signal(undefined) } as never, + broadcast, }); const pending = Promise.withResolvers(); const closed = spyOn(RecordingVideoEncoder.prototype, "close"); @@ -1216,6 +1221,7 @@ for (const stop of ["resume", "close"] as const) { expect(flush).toHaveBeenCalledTimes(1); const before = closed.mock.calls.length; if (stop === "close") encoder.close(); + else if (stop === "detach") broadcast.set(undefined); else enabled.set(true); await settle(); expect(closed.mock.calls.length).toBeGreaterThan(before); diff --git a/js/publish/src/video/encoder.ts b/js/publish/src/video/encoder.ts index d3ed309423e..2ff32979093 100644 --- a/js/publish/src/video/encoder.ts +++ b/js/publish/src/video/encoder.ts @@ -8,7 +8,6 @@ import { type Getter, getter, type Inputs, - Once, type Readonlys, race, readonlys, @@ -601,17 +600,9 @@ export class Encoder { } finally { frame.close(); } - // A cosmetic frame must never hold the next enable or final close behind a codec + // A cosmetic frame must never hold the next enable or track close behind a codec // flush. The old codec is closed below even if the browser never settles it. - const resumed = new Once(); - const unwatch = this.in.enabled.subscribe((enabled) => { - if (enabled && resumed.peek() === undefined) resumed.set(true); - }); - try { - await race([encoder.flush().catch(() => {}), resumed, this.#signals.closed]); - } finally { - unwatch(); - } + await race([encoder.flush().catch(() => {}), Signal.race(this.in.enabled), effect.closed]); } } finally { if (encoder.state !== "closed") encoder.close(); From b910c5c4a352f08e88f94c4a49ce91db9369e039 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 11:25:34 -0800 Subject: [PATCH 3/5] fix(publish): preserve recovered capture and disabled flushes Co-Authored-By: GPT-6.1 Sol --- doc/lib/js/publish.md | 2 +- js/publish/README.md | 2 +- js/publish/src/source/camera.ts | 1 + js/publish/src/source/microphone.ts | 1 + js/publish/src/source/retry.test.ts | 5 +++++ js/publish/src/video/encoder.test.ts | 3 +++ js/publish/src/video/encoder.ts | 9 ++++++++- 7 files changed, 20 insertions(+), 3 deletions(-) diff --git a/doc/lib/js/publish.md b/doc/lib/js/publish.md index 134f5c9f148..70dd388794c 100644 --- a/doc/lib/js/publish.md +++ b/doc/lib/js/publish.md @@ -50,7 +50,7 @@ element. `` shows what the browser can encode. Capture failures, such as `NotAllowedError` when permission is refused, land in `out.error` on the camera or microphone in `el.sources.video` and `el.sources.audio`, and `` shows them. A refused capture waits -for a permission, device, or settings change instead of prompting again. +for a permission, device, or settings change, or an explicit `retry()` from the app. ## Stats and feedback diff --git a/js/publish/README.md b/js/publish/README.md index a41af07982d..b2c428eb4f2 100644 --- a/js/publish/README.md +++ b/js/publish/README.md @@ -86,7 +86,7 @@ Camera and microphone failures are observable through `el.sources.video` and `el.sources.audio`. When those signals hold a `Source.Camera` or `Source.Microphone`, read its `out.error` signal. It contains an `Error` (for example, a `NotAllowedError` when permission is refused). A refused capture is not retried until permission, device selection, -constraints, or the source's enabled state changes. `` shows +constraints, or the source's enabled state changes, or the app calls `retry()`. `` shows capture failures in its status badge. ## JavaScript API diff --git a/js/publish/src/source/camera.ts b/js/publish/src/source/camera.ts index 3ae02c7b146..0d957408d32 100644 --- a/js/publish/src/source/camera.ts +++ b/js/publish/src/source/camera.ts @@ -174,6 +174,7 @@ export class Camera { /** Retry a failed capture after an explicit user action. */ retry(): void { + if (!this.#retry.spent()) return; this.#out.error.set(undefined); this.#retry.refund(); } diff --git a/js/publish/src/source/microphone.ts b/js/publish/src/source/microphone.ts index 336644158e5..5674fe79ff0 100644 --- a/js/publish/src/source/microphone.ts +++ b/js/publish/src/source/microphone.ts @@ -144,6 +144,7 @@ export class Microphone { /** Retry a failed capture after an explicit user action. */ retry(): void { + if (!this.#retry.spent()) return; this.#out.error.set(undefined); this.#retry.refund(); } diff --git a/js/publish/src/source/retry.test.ts b/js/publish/src/source/retry.test.ts index 6ed1bb86751..320cf2c532d 100644 --- a/js/publish/src/source/retry.test.ts +++ b/js/publish/src/source/retry.test.ts @@ -207,6 +207,11 @@ for (const [kind, create] of [ expect(media.attempts).toBe(2); expect(published(source.out.source.peek())).toBe(media.latest()); expect(source.out.error.peek()).toBeUndefined(); + const live = source.out.source.peek(); + source.retry(); + await settle(); + expect(media.attempts).toBe(2); + expect(source.out.source.peek()).toBe(live); } finally { source.close(); } diff --git a/js/publish/src/video/encoder.test.ts b/js/publish/src/video/encoder.test.ts index 3e4f384e702..8a31cb28a5d 100644 --- a/js/publish/src/video/encoder.test.ts +++ b/js/publish/src/video/encoder.test.ts @@ -1220,6 +1220,9 @@ for (const stop of ["resume", "close", "detach"] as const) { await settle(); expect(flush).toHaveBeenCalledTimes(1); const before = closed.mock.calls.length; + enabled.set(false, true); + await settle(); + expect(closed.mock.calls.length).toBe(before); if (stop === "close") encoder.close(); else if (stop === "detach") broadcast.set(undefined); else enabled.set(true); diff --git a/js/publish/src/video/encoder.ts b/js/publish/src/video/encoder.ts index 2ff32979093..7f388895a40 100644 --- a/js/publish/src/video/encoder.ts +++ b/js/publish/src/video/encoder.ts @@ -602,7 +602,14 @@ export class Encoder { } // A cosmetic frame must never hold the next enable or track close behind a codec // flush. The old codec is closed below even if the browser never settles it. - await race([encoder.flush().catch(() => {}), Signal.race(this.in.enabled), effect.closed]); + const flushed = encoder.flush().then( + () => true, + () => true, + ); + do { + const done = await race([flushed, Signal.race(this.in.enabled), effect.closed]); + if (done !== false) break; + } while (!this.in.enabled.peek()); } } finally { if (encoder.state !== "closed") encoder.close(); From 1e8dd81489668b86de6dc4e29ec0716df7ab7e9e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 12:38:55 -0800 Subject: [PATCH 4/5] fix(publish): cancel muted flush on track lifecycle changes --- js/publish/src/video/encoder.test.ts | 13 +++++++++++-- js/publish/src/video/encoder.ts | 22 ++++++++++++++++++---- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/js/publish/src/video/encoder.test.ts b/js/publish/src/video/encoder.test.ts index 8a31cb28a5d..ff9c5f4980d 100644 --- a/js/publish/src/video/encoder.test.ts +++ b/js/publish/src/video/encoder.test.ts @@ -1186,7 +1186,7 @@ async function installRecording() { }; } -for (const stop of ["resume", "close", "detach"] as const) { +for (const stop of ["resume", "close", "detach", "track closed", "track replaced"] as const) { test(`a stalled final black frame cannot block ${stop}`, async () => { using recording = await installRecording(); const enabled = new Signal(true); @@ -1199,9 +1199,10 @@ for (const stop of ["resume", "close", "detach"] as const) { join: new Signal(undefined), close: () => {}, }; + const order = new Order(); const broadcast = new Signal({ video: () => rendition, - order: new Order(), + order, join: new Signal(undefined), } as never); const encoder = new Encoder("video", { @@ -1211,6 +1212,7 @@ for (const stop of ["resume", "close", "detach"] as const) { }); const pending = Promise.withResolvers(); const closed = spyOn(RecordingVideoEncoder.prototype, "close"); + const leave = spyOn(order, "leave"); let flush: ReturnType> | undefined; try { await settle(); @@ -1225,9 +1227,15 @@ for (const stop of ["resume", "close", "detach"] as const) { expect(closed.mock.calls.length).toBe(before); if (stop === "close") encoder.close(); else if (stop === "detach") broadcast.set(undefined); + else if (stop === "track closed") track.close(); + else if (stop === "track replaced") + rendition.track.set( + new Moq.Track.Producer("replacement").accept({ timescale: Moq.Time.Timescale.MICRO }), + ); else enabled.set(true); await settle(); expect(closed.mock.calls.length).toBeGreaterThan(before); + expect(leave).toHaveBeenCalled(); if (stop === "resume") { await recording.send(300); expect(recording.keys).toContain(300); @@ -1238,6 +1246,7 @@ for (const stop of ["resume", "close", "detach"] as const) { track.close(); flush?.mockRestore(); closed.mockRestore(); + leave.mockRestore(); pending.resolve(); await settle(); } diff --git a/js/publish/src/video/encoder.ts b/js/publish/src/video/encoder.ts index 7f388895a40..5e89d73f184 100644 --- a/js/publish/src/video/encoder.ts +++ b/js/publish/src/video/encoder.ts @@ -374,7 +374,7 @@ export class Encoder { effect.set(this.#out.active, enabled && !!track && !error, false); if (!enabled || !track || error) return; - this.#encode(track, broadcast.order, broadcast.join, effect); + this.#encode(rendition.track, broadcast.order, broadcast.join, effect); }); // Reserve against the connection while a subscriber is attached. Wait for a ceiling so we @@ -410,7 +410,14 @@ export class Encoder { } // Encode captured frames into the track producer, reconfiguring when the resolved config changes. - #encode(track: Moq.Track.Producer, order: Order, join: Getter, effect: Effect): void { + #encode( + tracks: Getter, + order: Order, + join: Getter, + effect: Effect, + ): void { + const track = effect.get(tracks); + if (!track) return; const capture = effect.get(this.in.capture); if (!capture) return; @@ -607,8 +614,15 @@ export class Encoder { () => true, ); do { - const done = await race([flushed, Signal.race(this.in.enabled), effect.closed]); - if (done !== false) break; + if (track.closed.peek() !== undefined || tracks.peek() !== track) break; + const done = await race([ + flushed, + Signal.race(this.in.enabled), + Signal.race(tracks), + track.closed, + effect.closed, + ]); + if (done === true || done === undefined) break; } while (!this.in.enabled.peek()); } } finally { From 1ceb7ac7624cf8f05e59f4d2cec97ef9e46a3b0b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 14:19:33 -0800 Subject: [PATCH 5/5] fix(publish): release ordering before cosmetic flush Co-Authored-By: GPT-6.1 Sol --- doc/lib/js/publish.md | 3 ++- js/publish/README.md | 2 +- js/publish/api/index.api.md | 2 -- js/publish/src/source/camera.ts | 7 ------- js/publish/src/source/microphone.ts | 7 ------- js/publish/src/source/retry.test.ts | 11 ++++------- js/publish/src/video/encoder.test.ts | 2 ++ js/publish/src/video/encoder.ts | 7 +++---- 8 files changed, 12 insertions(+), 29 deletions(-) diff --git a/doc/lib/js/publish.md b/doc/lib/js/publish.md index 70dd388794c..fa9f23a1f23 100644 --- a/doc/lib/js/publish.md +++ b/doc/lib/js/publish.md @@ -50,7 +50,8 @@ element. `` shows what the browser can encode. Capture failures, such as `NotAllowedError` when permission is refused, land in `out.error` on the camera or microphone in `el.sources.video` and `el.sources.audio`, and `` shows them. A refused capture waits -for a permission, device, or settings change, or an explicit `retry()` from the app. +for a permission, device, or settings change. Apps can toggle the source’s `enabled` +state off and on to retry after a refusal. ## Stats and feedback diff --git a/js/publish/README.md b/js/publish/README.md index b2c428eb4f2..a41af07982d 100644 --- a/js/publish/README.md +++ b/js/publish/README.md @@ -86,7 +86,7 @@ Camera and microphone failures are observable through `el.sources.video` and `el.sources.audio`. When those signals hold a `Source.Camera` or `Source.Microphone`, read its `out.error` signal. It contains an `Error` (for example, a `NotAllowedError` when permission is refused). A refused capture is not retried until permission, device selection, -constraints, or the source's enabled state changes, or the app calls `retry()`. `` shows +constraints, or the source's enabled state changes. `` shows capture failures in its status badge. ## JavaScript API diff --git a/js/publish/api/index.api.md b/js/publish/api/index.api.md index aeee070dc72..39b742a0458 100644 --- a/js/publish/api/index.api.md +++ b/js/publish/api/index.api.md @@ -247,7 +247,6 @@ export namespace Source { readonly device: Source.Device<"video">; readonly in: Signals.Readonlys; readonly out: Signals.Readonlys; - retry(): void; } export type CameraInput = { enabled: Signals.Getter; @@ -312,7 +311,6 @@ export namespace Source { readonly device: Source.Device<"audio">; readonly in: Signals.Readonlys; readonly out: Signals.Readonlys; - retry(): void; } export type MicrophoneInput = { enabled: Signals.Getter; diff --git a/js/publish/src/source/camera.ts b/js/publish/src/source/camera.ts index 0d957408d32..d79dd29fe29 100644 --- a/js/publish/src/source/camera.ts +++ b/js/publish/src/source/camera.ts @@ -172,13 +172,6 @@ export class Camera { }); } - /** Retry a failed capture after an explicit user action. */ - retry(): void { - if (!this.#retry.spent()) return; - this.#out.error.set(undefined); - this.#retry.refund(); - } - /** Stop the capture and release the device. */ close() { this.#signals.close(); diff --git a/js/publish/src/source/microphone.ts b/js/publish/src/source/microphone.ts index 5674fe79ff0..3a3844ac227 100644 --- a/js/publish/src/source/microphone.ts +++ b/js/publish/src/source/microphone.ts @@ -142,13 +142,6 @@ export class Microphone { }); } - /** Retry a failed capture after an explicit user action. */ - retry(): void { - if (!this.#retry.spent()) return; - this.#out.error.set(undefined); - this.#retry.refund(); - } - /** Stop the capture and release the device. */ close() { this.#signals.close(); diff --git a/js/publish/src/source/retry.test.ts b/js/publish/src/source/retry.test.ts index 320cf2c532d..d64cd8ea40e 100644 --- a/js/publish/src/source/retry.test.ts +++ b/js/publish/src/source/retry.test.ts @@ -207,11 +207,6 @@ for (const [kind, create] of [ expect(media.attempts).toBe(2); expect(published(source.out.source.peek())).toBe(media.latest()); expect(source.out.error.peek()).toBeUndefined(); - const live = source.out.source.peek(); - source.retry(); - await settle(); - expect(media.attempts).toBe(2); - expect(source.out.source.peek()).toBe(live); } finally { source.close(); } @@ -344,7 +339,7 @@ for (const [kind, create] of [ }); } - test(`${kind} retries a refusal explicitly without changing the enabled setting`, async () => { + test(`${kind} retries a refusal after toggling enabled`, async () => { using _time = fakeTime(); using media = install(new FakeMediaDevices()); media.denied = true; @@ -354,7 +349,9 @@ for (const [kind, create] of [ await settle(); expect(source.out.error.peek()?.name).toBe("NotAllowedError"); media.denied = false; - source.retry(); + enabled.set(false); + await settle(); + enabled.set(true); await settle(); expect(enabled.peek()).toBe(true); expect(source.out.error.peek()).toBeUndefined(); diff --git a/js/publish/src/video/encoder.test.ts b/js/publish/src/video/encoder.test.ts index ff9c5f4980d..5a3e31d518a 100644 --- a/js/publish/src/video/encoder.test.ts +++ b/js/publish/src/video/encoder.test.ts @@ -1221,6 +1221,8 @@ for (const stop of ["resume", "close", "detach", "track closed", "track replaced enabled.set(false); await settle(); expect(flush).toHaveBeenCalledTimes(1); + // Muting releases sibling catalog delay even while the cosmetic flush is pending. + expect(leave).toHaveBeenCalled(); const before = closed.mock.calls.length; enabled.set(false, true); await settle(); diff --git a/js/publish/src/video/encoder.ts b/js/publish/src/video/encoder.ts index 5e89d73f184..0cfe7d173e5 100644 --- a/js/publish/src/video/encoder.ts +++ b/js/publish/src/video/encoder.ts @@ -468,7 +468,7 @@ export class Encoder { producer.encode(frame, frame.timestamp as Micro, key); // Including a keyframe the encoder placed on its own, which opened a group too. if (key) answer(); - if (this.#estimator.write(frame.timestamp, order, key)) { + if (!tearingDown && this.#estimator.write(frame.timestamp, order, key)) { this.#estimate.set({ ...this.#estimator.estimate }); } }, @@ -590,6 +590,8 @@ export class Encoder { // and shows black instead of a frozen picture. await new Promise((resolve) => effect.abort.addEventListener("abort", resolve, { once: true })); tearingDown = true; + // A cosmetic flush must not hold sibling renditions to our stale delay. + order.leave(this.#estimator); try { const config = configured; if ( @@ -627,9 +629,6 @@ export class Encoder { } } finally { if (encoder.state !== "closed") encoder.close(); - // A stopped rendition stops holding the others' delay back. Not a cleanup: the black - // keyframe above is written after one would run, and would seat this rendition again. - order.leave(this.#estimator); // The broadcast owns this static track across gaps (a disable, or with `onDemand` a demand // gap). Close the current group, marking the break so a later subscriber resumes on the same // track without the pre-gap group reading as live. A fatal encoder error aborts the track instead.