diff --git a/js/watch/src/audio/decoder.ts b/js/watch/src/audio/decoder.ts index 706044c389a..12672283f35 100644 --- a/js/watch/src/audio/decoder.ts +++ b/js/watch/src/audio/decoder.ts @@ -360,7 +360,7 @@ export class Decoder { next.end !== undefined || next.discontinuity !== (discontinuity ?? next.discontinuity) ) { - if (!(await this.#play(effect, decoder, decoderConfig, order, order.flush()))) return; + if (!(await this.#play(effect, decoder, decoderConfig, order, order.flush(), warmup))) return; } if (!next) break; discontinuity = next.discontinuity; @@ -382,7 +382,8 @@ export class Decoder { bytesReceived: (stats?.bytesReceived ?? 0) + frame.payload.byteLength, })); - if (!(await this.#play(effect, decoder, decoderConfig, order, order.push({ ...next, frame })))) return; + if (!(await this.#play(effect, decoder, decoderConfig, order, order.push({ ...next, frame }), warmup))) + return; } }); } @@ -495,6 +496,7 @@ export class Decoder { config: AudioDecoderConfig, order: Order, reads: FrameNext[], + warmup?: Warmup, ): Promise { for (const next of reads) { if (this.#onNext(next)) { @@ -507,7 +509,8 @@ export class Decoder { // Backpressure: in buffered mode this holds the encoded frame until the playhead nears // it, keeping the lookahead above the floor as Opus instead of decoded PCM. No-op live. - await this.#ring?.wait(frame.timestamp as Time.Micro, effect.abort); + // A frame whose output the warm-up discards fills no ring space, so it skips the wait. + if (!warmup?.ungated()) await this.#ring?.wait(frame.timestamp as Time.Micro, effect.abort); // A fatal decode error closes the decoder, so decoding again throws InvalidStateError out // of the loop. Stop instead: the error callback already reported the real failure. diff --git a/js/watch/src/audio/warmup.test.ts b/js/watch/src/audio/warmup.test.ts index acf5bc559e7..548e5b9984b 100644 --- a/js/watch/src/audio/warmup.test.ts +++ b/js/watch/src/audio/warmup.test.ts @@ -1,4 +1,8 @@ import { expect, test } from "bun:test"; +import type { Time } from "@moq/net"; +import { createAudioBuffer } from "./buffer"; +import type { InitShared } from "./render"; +import { SharedRingBuffer } from "./shared-ring-buffer"; import { Warmup } from "./warmup"; test("decoder warm-up drops only the configured initial callbacks", () => { @@ -12,3 +16,27 @@ test("decoder warm-up drops only the configured initial callbacks", () => { false, ]); }); + +// The legacy decode loop, as `#play` and the decoder's output callback run it: a 40 ms floor of 20 ms +// frames. Gated, the warm-up inputs would spend the ring's whole refill on audio that is discarded, +// leaving the ring empty and the gate waiting on it forever. +test("warm-up inputs skip the ring's backpressure, so the ring still fills to its floor", () => { + const sent: InitShared[] = []; + const worklet = { port: { postMessage: (msg: InitShared) => sent.push(msg) } } as unknown as AudioWorkletNode; + const buffer = createAudioBuffer(worklet, 1, 1000, 40, true); + const signal = new AbortController().signal; + const warmup = new Warmup(3); + try { + const decoded: Time.Micro[] = []; + for (let timestamp = 0; ; timestamp += 20_000) { + if (!warmup.ungated() && Bun.peek.status(buffer.wait(timestamp as Time.Micro, signal)) === "pending") break; + decoded.push(timestamp as Time.Micro); + } + for (const timestamp of decoded) { + if (!warmup.drop()) buffer.insert(timestamp, [new Float32Array(20)]); + } + expect(new SharedRingBuffer(sent[0]).stalled).toBe(false); + } finally { + buffer.close(); + } +}); diff --git a/js/watch/src/audio/warmup.ts b/js/watch/src/audio/warmup.ts index a7d74850734..f24acd57f62 100644 --- a/js/watch/src/audio/warmup.ts +++ b/js/watch/src/audio/warmup.ts @@ -1,10 +1,12 @@ /** Tracks decoder callbacks discarded during initial playback setup. */ export class Warmup { #remaining: number; + #ungated: number; /** Create a warm-up window containing `callbacks` decoder callbacks. */ constructor(callbacks: number) { this.#remaining = callbacks; + this.#ungated = callbacks; } /** Consume one callback and report whether it belongs to the warm-up window. */ @@ -13,4 +15,15 @@ export class Warmup { this.#remaining--; return true; } + + /** + * Consume one decoder input and report whether its output falls in the warm-up window. That + * output never reaches the ring, so the input skips the ring's backpressure: gating it would + * spend the ring's refill on audio that is discarded, and the ring would never anchor. + */ + ungated(): boolean { + if (this.#ungated === 0) return false; + this.#ungated--; + return true; + } }