Skip to content
Merged
4 changes: 4 additions & 0 deletions .github/workflows/release-gate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ jobs:
- "js/video/**"
- "js/publish/src/preview.ts"
- "js/publish/src/element.ts"
- "js/watch/src/player.ts"
- "js/watch/src/sync.ts"
- "js/watch/src/audio/**"
- "js/watch/src/video/**"
- "test/video/**"
- "test/justfile"
- "package.json"
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/video.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ name: Video
# the WebGPU path, a lost device, and the Canvas2D fallback only exist in a real
# browser. SwiftShader stands in for the GPU, so no GPU runner is needed.
#
# Narrow trigger, like wasm.yml: the renderer, the harness, and the two files that
# wire the renderer into `<moq-publish>` (which the harness drives). `@moq/watch`
# wires it the same way but needs a relay to drive, so its regressions surface
# nightly instead.
# Narrow trigger, like wasm.yml: the renderer, the harness, and what the harness
# drives: the two files that wire the renderer into `<moq-publish>`, and the
# `@moq/watch` playback path, fed from an in-memory origin so no relay is needed.

permissions:
contents: read
Expand Down
6 changes: 4 additions & 2 deletions doc/lib/js/watch.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ response emitted in one burst with future timestamps, wants the opposite. Set

Durations need a unit; a bare number is rejected. Audio holds the buffer as
encoded frames, so a large one is cheap; video waits as decoded pictures, which
hold decoder memory, so a long video buffer is not. `el.reset()` flushes and re-anchors at the
next frame, which is how a producer interrupts an utterance.
hold decoder memory, so a long video buffer is not. The first video frame shows
as a poster at once, and playback starts once every enabled track has buffered
the delay. `el.reset()` flushes and re-anchors at the next frame, which is how a
producer interrupts an utterance.

## Strict CSP

Expand Down
29 changes: 25 additions & 4 deletions js/watch/src/audio/buffer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Micro, Milli, type Second } from "@moq/clock";
import { Effect, type Getter, Signal } from "@moq/signals";
import { ringHold, ringSamples } from "./latency";
import type { Data, Init, Latency, Reset, State, Truncate } from "./render";
import type { Data, Init, Latency, Playing, Reset, State, Truncate } from "./render";

/**
* Decides when the decode loop may decode a frame, which is how the target delay is split between
Expand Down Expand Up @@ -275,6 +275,7 @@ export class AudioBuffer {
// Bumped per reset or park. A state message posted before the worklet saw it reports the ring
// playing on the old terms, which would open the gate.
#epoch = 0;
#playing?: Getter<boolean>;

#signals = new Effect();

Expand All @@ -287,17 +288,33 @@ export class AudioBuffer {
delay: Milli;
/** Anchor to the first frame and play through, holding the lookahead upstream. */
buffered: boolean;
/** Hold consumption until the shared startup floor is ready. */
playing?: Getter<boolean>;
},
) {
const { channels, rate, buffered } = props;
const { channels, rate, buffered, playing } = props;
this.#playing = props.playing;
this.#worklet = worklet;
this.channels = channels;
this.rate = rate;

this.#backpressure = new Backpressure(rate, buffered, props.delay);

const msg: Init = { type: "init", channels, rate, latency: this.#latency(), buffered };
const msg: Init = {
type: "init",
channels,
rate,
latency: this.#latency(),
buffered,
...(playing && { playing: playing.peek() }),
};
worklet.port.postMessage(msg);
if (playing) {
this.#signals.run((effect) => {
const msg: Playing = { type: "playing", playing: effect.get(playing), epoch: this.#epoch };
worklet.port.postMessage(msg);
});
}

this.#signals.event(worklet.port, "message", (ev: Event) => {
const data = (ev as MessageEvent<State>).data;
Expand Down Expand Up @@ -382,7 +399,11 @@ export class AudioBuffer {
/** Flush buffered samples and re-stall, ready to anchor the next utterance (buffered mode). */
reset(): void {
this.#epoch++;
const msg: Reset = { type: "reset", epoch: this.#epoch };
const msg: Reset = {
type: "reset",
epoch: this.#epoch,
...(this.#playing && { playing: this.#playing.peek() }),
};
this.#worklet.port.postMessage(msg);
this.#backpressure.reset();
}
Expand Down
Loading