Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions js/watch/src/audio/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
});
}
Expand Down Expand Up @@ -495,6 +496,7 @@ export class Decoder {
config: AudioDecoderConfig,
order: Order<FrameNext>,
reads: FrameNext[],
warmup?: Warmup,
): Promise<boolean> {
for (const next of reads) {
if (this.#onNext(next)) {
Expand All @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions js/watch/src/audio/warmup.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand All @@ -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();
}
});
13 changes: 13 additions & 0 deletions js/watch/src/audio/warmup.ts
Original file line number Diff line number Diff line change
@@ -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. */
Expand All @@ -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;
}
}
Loading