Skip to content

Commit deb3a24

Browse files
committed
feat: hand html5 bakes turns by the frame playback needs next
1 parent a474456 commit deb3a24

4 files changed

Lines changed: 85 additions & 34 deletions

File tree

‎src/components/canvas/players/bake-queue.ts‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
/** Where a waiting bake's clip sits on the timeline. Read at each pick, so seeks and edits count. */
1+
/** Where a waiting bake stands on the timeline. Read at each pick, so seeks, edits and progress count. */
22
export interface BakeSlot {
3-
start(): number;
4-
end(): number;
3+
/** Timeline time at which the next frame this bake draws leaves the screen. */
4+
nextFrameEnd(): number;
55
playhead(): number;
66
}
77

8-
/** The clip under the playhead goes first, then those ahead in start order, then those already passed. */
8+
/** Frames playback still needs go first, earliest first, then frames it has already passed. */
99
function rank(slot: BakeSlot): [number, number] {
10-
const playhead = slot.playhead();
11-
const start = slot.start();
12-
if (start <= playhead && playhead < slot.end()) return [0, start];
13-
return [start > playhead ? 1 : 2, start];
10+
const nextFrameEnd = slot.nextFrameEnd();
11+
return [nextFrameEnd <= slot.playhead() ? 1 : 0, nextFrameEnd];
1412
}
1513

1614
export function nextBake(waiting: readonly BakeSlot[]): number {
1715
let best = 0;
1816
let bestRank = rank(waiting[0]);
1917
for (let i = 1; i < waiting.length; i += 1) {
2018
const candidate = rank(waiting[i]);
21-
// Strictly better only, so clips that tie keep their arrival order.
19+
// Strictly better only, so bakes that tie keep their arrival order.
2220
if (candidate[0] < bestRank[0] || (candidate[0] === bestRank[0] && candidate[1] < bestRank[1])) {
2321
best = i;
2422
bestRank = candidate;
@@ -28,18 +26,25 @@ export function nextBake(waiting: readonly BakeSlot[]): number {
2826
}
2927

3028
/**
31-
* Runs bakes one at a time. Each pick waits a task, so clips that join together are all
32-
* waiting when it is made, and it reads the playhead then.
29+
* Runs bakes one at a time. A bake hands its turn back by awaiting `turn` and rejoins the end of the
30+
* waiting list, so bakes whose frames overlap alternate. Each pick waits a task, so clips that join
31+
* together are all waiting when it is made, and it reads the playhead then.
3332
*/
3433
export class BakeQueue {
3534
private readonly waiting: { slot: BakeSlot; run: () => void }[] = [];
3635
private running = false;
3736
private scheduled = false;
3837

39-
public add<T>(slot: BakeSlot, bake: () => Promise<T>): Promise<T> {
38+
public add<T>(slot: BakeSlot, bake: (turn: () => Promise<void>) => Promise<T>): Promise<T> {
4039
return new Promise<T>((resolve, reject) => {
40+
const turn = (): Promise<void> =>
41+
new Promise<void>(resume => {
42+
this.waiting.push({ slot, run: resume });
43+
this.running = false;
44+
this.schedule();
45+
});
4146
const run = (): void => {
42-
bake()
47+
bake(turn)
4348
.then(resolve, reject)
4449
.finally(() => {
4550
this.running = false;

‎src/components/canvas/players/html5-player.ts‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ export class Html5Player extends Player {
156156
private liveStillFrame: number = -1;
157157
private liveStillInFlight: boolean = false;
158158
private stillRequest: number | null = null;
159-
private readonly bakeSlot = { start: () => this.getStart(), end: () => this.getEnd(), playhead: () => this.edit.playbackTime };
159+
// The frame the running bake draws next. Null between bakes.
160+
private bakeNextIndex: number | null = null;
161+
private readonly bakeSlot = { nextFrameEnd: () => this.nextFrameEnd(), playhead: () => this.edit.playbackTime };
160162

161163
constructor(edit: Edit, clipConfiguration: ResolvedClip) {
162164
super(edit, clipConfiguration, PlayerType.Html5);
@@ -405,10 +407,22 @@ export class Html5Player extends Player {
405407
return Html5Player.bakes.add(this.bakeSlot, async () => {
406408
// A content change while this waited in the queue would bake the new content at this capture's floor.
407409
if (this.disposed || this.contentHash !== queuedFor) return null;
408-
return this.runCapture(minScale);
410+
try {
411+
return await this.runCapture(minScale);
412+
} finally {
413+
this.bakeNextIndex = null;
414+
}
409415
});
410416
}
411417

418+
// A bake that hasn't started begins at the playhead's frame, or at the clip's first once the playhead has passed it.
419+
private nextFrameEnd(): number {
420+
const start = this.getStart();
421+
const { playbackTime } = this.edit;
422+
const idx = this.bakeNextIndex ?? (playbackTime >= this.getEnd() ? 0 : Math.max(0, Math.floor((playbackTime - start) * this.captureFps)));
423+
return start + (idx + 1) / this.captureFps;
424+
}
425+
412426
// After a tick, an active clip's global transform is the size the canvas shows it at. An inactive clip's own
413427
// transform isn't applied, so its bake reads only the viewport's zoom and may re-bake once the clip is active.
414428
private async bakeScale(minScale: number): Promise<number> {
@@ -472,6 +486,7 @@ export class Html5Player extends Player {
472486
const recent = new Map<string, Promise<Blob>>();
473487
let recentStyles = "";
474488
let issued = 0;
489+
this.bakeNextIndex = 0;
475490
let progressStep = 0;
476491
let sliceStart = -Infinity;
477492
// Frames publish in order, so the oldest in flight gates the window.
@@ -502,6 +517,7 @@ export class Html5Player extends Player {
502517
}
503518
pending.push(drawn);
504519
issued += 1;
520+
this.bakeNextIndex = issued;
505521
} else {
506522
const frame = await pending.shift()!;
507523
if (stale()) return null;

‎tests/bake-queue.test.ts‎

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,88 @@
11
import { BakeQueue, nextBake } from "@canvas/players/bake-queue";
22

3-
const at = (start: number, end: number, playhead: () => number = () => 0) => ({ start: () => start, end: () => end, playhead });
3+
const at = (next: number, playhead: () => number = () => 0) => ({ nextFrameEnd: () => next, playhead });
44

55
const settle = () =>
66
new Promise(resolve => {
77
setTimeout(resolve, 0);
88
});
99

1010
describe("nextBake", () => {
11-
it("takes the clip under the playhead, then those ahead in start order, then those it has passed", () => {
11+
it("takes the earliest next frame still ahead of the playhead, then frames it has passed", () => {
1212
const playhead = () => 30;
13-
const waiting = [at(54.4, 80.37, playhead), at(0, 24.4, playhead), at(24.4, 54.4, playhead), at(40, 45, playhead)];
13+
const waiting = [at(54.4, playhead), at(24.4, playhead), at(30.1, playhead), at(40, playhead)];
1414
const order: number[] = [];
15-
while (waiting.length > 0) order.push(waiting.splice(nextBake(waiting), 1)[0].start());
15+
while (waiting.length > 0) order.push(waiting.splice(nextBake(waiting), 1)[0].nextFrameEnd());
1616

17-
expect(order).toEqual([24.4, 40, 54.4, 0]);
17+
expect(order).toEqual([30.1, 40, 54.4, 24.4]);
1818
});
1919

20-
it("keeps arrival order between clips that start together", () => {
21-
expect(nextBake([at(10, 20), at(10, 15)])).toBe(0);
20+
it("counts a frame that leaves the screen as the playhead reaches it as passed", () => {
21+
expect(nextBake([at(30, () => 30), at(31, () => 30)])).toBe(1);
22+
});
23+
24+
it("keeps arrival order between bakes whose next frames coincide", () => {
25+
expect(nextBake([at(10), at(10)])).toBe(0);
2226
});
2327
});
2428

2529
describe("BakeQueue", () => {
2630
it("bakes clips that arrive together from the playhead, whatever their arrival order", async () => {
2731
const queue = new BakeQueue();
2832
const ran: number[] = [];
29-
await Promise.all([54.4, 24.4, 0].map(start => queue.add(at(start, start + 20), async () => ran.push(start))));
33+
await Promise.all([54.4, 24.4, 0.1].map(next => queue.add(at(next), async () => ran.push(next))));
3034

31-
expect(ran).toEqual([0, 24.4, 54.4]);
35+
expect(ran).toEqual([0.1, 24.4, 54.4]);
3236
});
3337

34-
it("runs one bake at a time", async () => {
38+
it("runs one bake at a time, between turns too", async () => {
3539
const queue = new BakeQueue();
3640
let running = 0;
3741
let most = 0;
38-
const bake = async () => {
42+
const bake = async (turn: () => Promise<void>) => {
43+
running += 1;
44+
most = Math.max(most, running);
45+
await settle();
46+
running -= 1;
47+
await turn();
3948
running += 1;
4049
most = Math.max(most, running);
4150
await settle();
4251
running -= 1;
4352
};
44-
await Promise.all([0, 10, 20].map(start => queue.add(at(start, start + 10), bake)));
53+
await Promise.all([1, 11, 21].map(next => queue.add(at(next), bake)));
4554

4655
expect(most).toBe(1);
4756
});
4857

58+
it("alternates bakes whose frames overlap, turn by turn", async () => {
59+
const queue = new BakeQueue();
60+
const ran: string[] = [];
61+
const bake = (name: string) => {
62+
let next = 1;
63+
return queue.add({ nextFrameEnd: () => next, playhead: () => 0 }, async turn => {
64+
const step = async () => {
65+
ran.push(name);
66+
next += 1;
67+
await turn();
68+
};
69+
await step();
70+
await step();
71+
await step();
72+
});
73+
};
74+
await Promise.all([bake("A"), bake("B")]);
75+
76+
expect(ran).toEqual(["A", "B", "A", "B", "A", "B"]);
77+
});
78+
4979
it("re-sorts what's waiting when the playhead moves during a bake", async () => {
5080
const queue = new BakeQueue();
5181
let playhead = 0;
5282
const ran: string[] = [];
5383
let finishFirst = () => {};
5484
const first = queue.add(
55-
at(0, 10, () => playhead),
85+
at(0.1, () => playhead),
5686
() => {
5787
ran.push("A");
5888
return new Promise<void>(resolve => {
@@ -63,11 +93,11 @@ describe("BakeQueue", () => {
6393
await settle();
6494
const rest = [
6595
queue.add(
66-
at(10, 20, () => playhead),
96+
at(10.1, () => playhead),
6797
async () => ran.push("B")
6898
),
6999
queue.add(
70-
at(20, 30, () => playhead),
100+
at(25.1, () => playhead),
71101
async () => ran.push("C")
72102
)
73103
];
@@ -80,10 +110,10 @@ describe("BakeQueue", () => {
80110

81111
it("moves on after a bake fails, and hands the failure to its caller", async () => {
82112
const queue = new BakeQueue();
83-
const failed = queue.add(at(0, 10), async () => {
113+
const failed = queue.add(at(0.1), async () => {
84114
throw new Error("bake failed");
85115
});
86-
const next = queue.add(at(10, 20), async () => "baked");
116+
const next = queue.add(at(10.1), async () => "baked");
87117

88118
await expect(failed).rejects.toThrow("bake failed");
89119
await expect(next).resolves.toBe("baked");

‎tests/html5-player.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,8 +1150,8 @@ function holdCaptureQueue(): { drain: () => void; restore: () => void } {
11501150
const gate = new Promise<void>(resolve => {
11511151
drain = resolve;
11521152
});
1153-
// Under the playhead from the start, so it runs ahead of any clip that joins after it.
1154-
held.add({ start: () => 0, end: () => Infinity, playhead: () => 0 }, () => gate);
1153+
// Ahead of the playhead and earliest, so it runs ahead of any clip that joins after it.
1154+
held.add({ nextFrameEnd: () => 0, playhead: () => -Infinity }, () => gate);
11551155
Object.assign(Html5Player, { bakes: held });
11561156
return { drain, restore: () => Object.assign(Html5Player, { bakes }) };
11571157
}

0 commit comments

Comments
 (0)