From 1f8701db0bfac875406694ff8e72d00106bd7c01 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 22 Oct 2026 23:58:29 -0700 Subject: [PATCH] fix(watch): decode a reordered subscription head in timestamp order A subscription's head groups can arrive newest-first. Chrome's AudioDecoder stamps every output contiguously from its first input, so decoding the older frames after the newest shifts the whole track forward by the reorder. With a fixed 100 ms delay that pushes the gate past the ring's capacity and overflows it on every frame. Skip frames older than the newest one already decoded: the ring, anchored at the first insert, drops them anyway. Co-Authored-By: Claude Opus 5.5 --- js/watch/src/audio/decoder.test.ts | 19 +++++++++++++++++++ js/watch/src/audio/decoder.ts | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/js/watch/src/audio/decoder.test.ts b/js/watch/src/audio/decoder.test.ts index 9426b55a095..17f97a060fa 100644 --- a/js/watch/src/audio/decoder.test.ts +++ b/js/watch/src/audio/decoder.test.ts @@ -326,6 +326,25 @@ it("decodes an audio packet inside a group after a gap", async () => { } }); +// A subscription's head arrives newest-first. Chrome's AudioDecoder stamps every output from its +// first input, so decoding the older frames after it shifts the rest of the track. +for (const cmaf of [false, true]) { + it(`decodes a ${cmaf ? "CMAF" : "legacy"} subscription head in timestamp order`, async () => { + const playback = await play(Milli(100), true, cmaf); + try { + for (const ms of [140, 120, 100, 80, 60, 40, 20, 0, 160]) { + frameTimestamp = (ms - 20) * 1000; + playback.push(frame()); + await microtasks(); + } + expect(playback.inserted()).toEqual([140_000, 160_000]); + } finally { + playback.close(); + await microtasks(); + } + }); +} + describe("Decoder across a broadcast disable", () => { it("releases the graph when its broadcast is disabled and rebuilds on return", async () => { const playback = await play(Milli(100)); diff --git a/js/watch/src/audio/decoder.ts b/js/watch/src/audio/decoder.ts index 51d4c12694a..cc6d6018f03 100644 --- a/js/watch/src/audio/decoder.ts +++ b/js/watch/src/audio/decoder.ts @@ -476,12 +476,15 @@ export class Decoder { }; decoder.configure(decoderConfig); + // The newest frame sent to the decoder since it was last reset. + let newest: Micro | undefined; for (;;) { const next = await nextMedia(consumer); if (!next) break; if (this.#onRead(next)) { decoder.reset(); decoder.configure(decoderConfig); + newest = undefined; } if (next.kind === "gap") this.#arrivals.write(gap(next)); if (next.kind !== "frame") continue; @@ -499,6 +502,13 @@ export class Decoder { bytesReceived: (stats?.bytesReceived ?? 0) + frame.payload.byteLength, })); + // Decode in timestamp order. A subscription's head groups arrive after the live edge served + // alongside them, and the ring, anchored at the first decoded frame, cannot place audio + // before it. Chrome's AudioDecoder would also stamp every later output from its first + // input, shifting the rest of the track by the reorder and overflowing a buffered ring. + if (newest !== undefined && frame.timestamp < newest) continue; + newest = frame.timestamp; + const ring = await this.#ready(effect); if (!ring) break; @@ -570,6 +580,8 @@ export class Decoder { }; decoder.configure(decoderConfig); + // The newest frame sent to the decoder since it was last reset. + let newest: Micro | undefined; for (;;) { const next = await nextMedia(consumer); if (!next) break; @@ -578,6 +590,7 @@ export class Decoder { if (this.#onRead(next)) { decoder.reset(); decoder.configure(decoderConfig); + newest = undefined; } if (next.kind === "gap") this.#arrivals.write(gap(next)); if (next.kind !== "frame") continue; @@ -594,6 +607,13 @@ export class Decoder { bytesReceived: (stats?.bytesReceived ?? 0) + frame.payload.byteLength, })); + // Decode in timestamp order. A subscription's head groups arrive after the live edge served + // alongside them, and the ring, anchored at the first decoded frame, cannot place audio + // before it. Chrome's AudioDecoder would also stamp every later output from its first + // input, shifting the rest of the track by the reorder and overflowing a buffered ring. + if (newest !== undefined && frame.timestamp < newest) continue; + newest = frame.timestamp; + const ring = await this.#ready(effect); if (!ring) break;