Skip to content
Merged
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
67 changes: 67 additions & 0 deletions js/watch/src/video/decoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,73 @@ describe("Decoder stalls", () => {
}
});

it("holds the last picture through a declared end without a stall", async () => {
const { clock, feeds, playback, stalls, feedback, close } = await cold();
try {
feeds.push(0, sample(0n, 0));
for (let ms = 100; ms <= 2000; ms += 100) {
await clock.advance(100);
feeds.push(0, sample(0n, ms * 1000, false));
await microtasks();
}
// The publisher ends the media, and the last picture holds well past the buffering timeout.
feeds.push(0, marker(0n, 2_100_000));
for (let i = 0; i < 30; i++) await clock.advance(100);
expect(playback.decoder.out.frame.peek()?.timestamp).toBe(2_000_000);
expect(playback.decoder.out.stalled.peek()).toBe(false);
expect(stalls()).toBe(0);
expect(feedback()).toBe(0);
} finally {
await close();
}
});

it("a declared end clears a stall already raised and stops its stalled time", async () => {
const { clock, feeds, playback, stalls, feedback, close } = await cold();
const stalled = () => playback.decoder.out.stats.peek()?.stalled ?? 0;
try {
feeds.push(0, sample(0n, 0));
for (let ms = 100; ms <= 2000; ms += 100) {
await clock.advance(100);
feeds.push(0, sample(0n, ms * 1000, false));
await microtasks();
}
// The frames stop, and the last picture freezes into a stall.
for (let i = 0; i < 16; i++) await clock.advance(100);
expect(playback.decoder.out.stalled.peek()).toBe(true);
expect(stalls()).toBe(1);
expect(stalled()).toBe(0);

// The publisher then declares the end: the stall closes and its time stops growing.
feeds.push(0, marker(0n, 2_100_000));
await microtasks();
expect(playback.decoder.out.stalled.peek()).toBe(false);
const closed = stalled();
expect(closed).toBeGreaterThan(0);
for (let i = 0; i < 30; i++) await clock.advance(100);
expect(playback.decoder.out.stalled.peek()).toBe(false);
expect(stalled()).toBe(closed);
expect(stalls()).toBe(1);
expect(feedback()).toBe(1);
} finally {
await close();
}
});

it("a declared end before the first picture ends startup buffering", async () => {
const { feeds, playback, stalls, close } = await cold();
try {
expect(playback.decoder.out.stalled.peek()).toBe(true);
feeds.push(0, marker(0n, 0));
await microtasks();
expect(playback.decoder.out.frame.peek()).toBeUndefined();
expect(playback.decoder.out.stalled.peek()).toBe(false);
expect(stalls()).toBe(0);
} finally {
await close();
}
});

it("clears the buffering state while disabled", async () => {
const { playback, close } = await cold();
try {
Expand Down
33 changes: 26 additions & 7 deletions js/watch/src/video/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ type Selection = { kind: "none" } | { kind: "selected"; rendition: Rendition } |

// One run of the active rendition's playback. A poster may show while it starts, and only a picture
// that played and then froze is a stall.
type Playback = "starting" | "playing" | "stalled";
// A picture held through the publisher's declared end is no stall either.
type Playback = "starting" | "playing" | "ended" | "stalled";

/** Signals the decoder reads. */
export type DecoderInput = {
Expand Down Expand Up @@ -456,14 +457,20 @@ export class Decoder {
return;
}

// After the publisher's declared end no picture is coming, whether one shows or not.
const active = effect.get(this.#active);
if (active && effect.get(active.ended) !== undefined) {
this.#out.stalled.set(false);
return;
}

const frame = effect.get(this.#out.frame);
if (!frame) {
this.#out.stalled.set(true);
return;
}

this.#out.stalled.set(false);

effect.timer(() => {
this.#out.stalled.set(true);
}, Nano.fromMilli(BUFFERING));
Expand All @@ -483,6 +490,10 @@ export class Decoder {
playback.set("starting");
return;
}
if (inner.get(active.ended) !== undefined) {
playback.set("ended");
return;
}
playback.set("playing");
inner.timer(() => playback.set("stalled"), Nano.fromMilli(BUFFERING));
});
Expand Down Expand Up @@ -604,8 +615,9 @@ class DecoderTrack {

// A hole or timeline restart makes the next decoded frame decide whether the codec resets.
#resetPending = false;
// A declared endpoint stays in force through holes until a later group starts a new timeline.
#endGroup?: bigint;
// The group the publisher declared the media ends with. It stays in force through holes until a
// later group starts a new timeline.
readonly ended = new Signal<bigint | undefined>(undefined);

// The newest consumer reading this track, whose estimate a replacement continues. The predecessor
// is only held until the first one opens.
Expand Down Expand Up @@ -958,9 +970,11 @@ class DecoderTrack {
if (read.kind === "gap") {
if (latest !== undefined) this.#resetPending = true;
} else if (read.kind === "end") {
if (latest === undefined || read.group >= latest) this.#endGroup = read.group;
} else if (this.#endGroup !== undefined && read.group > this.#endGroup) {
this.#endGroup = undefined;
if (latest === undefined || read.group >= latest) this.ended.set(read.group);
} else {
const ended = this.ended.peek();
if (ended === undefined || read.group <= ended) return;
this.ended.set(undefined);
this.#onBreak(read.group);
}
}
Expand Down Expand Up @@ -1177,6 +1191,11 @@ class Rendition {
return this.min.spread;
}

// The group the publisher declared the clock-driving decoder's media ends with.
get ended(): Getter<bigint | undefined> {
return this.min.ended;
}

// Show the max-delay picture when it is at or past the min-delay one, so it wins ties, else the
// min-delay picture. Only pictures read from `start` on, the min-delay decoder's timeline, are
// candidates: one held from before a restart neither shows nor sets the floor. Not `caughtUp`: its
Expand Down
1 change: 0 additions & 1 deletion quest/m1/explicit-state/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ which collapses that state and waits on [Drop set](/quest/m1/drop-set.md).
- [Track and group end-of-life](/quest/m1/explicit-state/track-end.md) - moq-net's track and group outcomes are one enum each, in Rust and JS
- [moq-net phase data](/quest/m1/explicit-state/net-phases.md) - moq-net's session and subscription state machines keep each phase's data in its variant
- [moq-mux and moq-gst lifecycles](/quest/m1/explicit-state/mux-gst.md) - export sources, TS and fMP4 export tracks, and gst pads are explicit state
- [Video's declared-end freeze is not a stall](/quest/m1/explicit-state/video-declared-end.md) - video holding its last picture through a declared end counts no stall, like audio's declared silence

## Related

Expand Down
20 changes: 0 additions & 20 deletions quest/m1/explicit-state/video-declared-end.md

This file was deleted.