diff --git a/bun.lock b/bun.lock index a449b64b9ea..d10f5e5b029 100644 --- a/bun.lock +++ b/bun.lock @@ -324,7 +324,7 @@ }, "js/watch": { "name": "@moq/watch", - "version": "0.6.4", + "version": "0.6.5", "dependencies": { "@moq/hang": "workspace:^", "@moq/json": "workspace:^", diff --git a/js/watch/package.json b/js/watch/package.json index d3233b45d0f..5eee56083f8 100644 --- a/js/watch/package.json +++ b/js/watch/package.json @@ -1,7 +1,7 @@ { "name": "@moq/watch", "type": "module", - "version": "0.6.4", + "version": "0.6.5", "jsr": false, "description": "Watch Media over QUIC broadcasts", "license": "(MIT OR Apache-2.0)", diff --git a/js/watch/src/audio/decoder.ts b/js/watch/src/audio/decoder.ts index daaaf4ed917..ce24b928a18 100644 --- a/js/watch/src/audio/decoder.ts +++ b/js/watch/src/audio/decoder.ts @@ -345,12 +345,15 @@ export class Decoder { }; decoder.configure(decoderConfig); + // The newest frame sent to the decoder since it was last reset. + let newest: Time.Micro | undefined; for (;;) { const next = await nextMedia(consumer); if (!next) break; if (this.#onNext(next)) { decoder.reset(); decoder.configure(decoderConfig); + newest = undefined; } if (next.end !== undefined) { continue; @@ -367,6 +370,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 as Time.Micro) < newest) continue; + newest = frame.timestamp as Time.Micro; + // 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); @@ -436,6 +446,8 @@ export class Decoder { }; decoder.configure(decoderConfig); + // The newest frame sent to the decoder since it was last reset. + let newest: Time.Micro | undefined; for (;;) { const next = await nextMedia(consumer); if (!next) break; @@ -444,6 +456,7 @@ export class Decoder { if (this.#onNext(next)) { decoder.reset(); decoder.configure(decoderConfig); + newest = undefined; } const { frame } = next; @@ -456,6 +469,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; + // 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, effect.abort);