Skip to content

Commit 0ec45eb

Browse files
committed
feat: play html5 frames while their clip is still baking
1 parent 0897dc8 commit 0ec45eb

2 files changed

Lines changed: 112 additions & 2 deletions

File tree

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ export class Html5Player extends Player {
126126
private renderedHeight: number = 0;
127127
private capturedFrames: Blob[] | null = null;
128128
private capturedScale: number = 1;
129+
// A first bake's frames by index as they land, for the stills they replace. Export never reads them.
130+
private bakingFrames: Array<Blob | undefined> | null = null;
131+
private bakingScale: number = 1;
129132
private readonly globalTransform = new pixi.Matrix();
130133
// Stays set after a failed re-bake, so a failure isn't retried every frame.
131134
private rebakeStarted: boolean = false;
@@ -381,6 +384,7 @@ export class Html5Player extends Player {
381384
private transitionToFailed(): void {
382385
if (this.disposed) return;
383386
this.mode = "failed";
387+
this.bakingFrames = null;
384388
this.removeLoadingGraphic();
385389
this.removeLiveStill();
386390
this.createFallbackGraphic();
@@ -501,6 +505,11 @@ export class Html5Player extends Player {
501505
// Where each segment after the first starts in the issue order.
502506
const freshPageAt = new Set(segments.slice(0, -1).map((_, i) => segments.slice(0, i + 1).flat().length));
503507
const frames: Array<Blob | undefined> = new Array(frameCount);
508+
// A re-bake leaves complete frames on screen until it finishes, so only a first bake shows frames as they land.
509+
if (this.mode === "capturing") {
510+
this.bakingFrames = frames;
511+
this.bakingScale = scale;
512+
}
504513
const pending: { idx: number; drawn: Promise<Blob> }[] = [];
505514
// The SVG is a frame's whole input, so a frame that repeats a recent one draws identical bytes and shares its Blob.
506515
const recent = new Map<string, Promise<Blob>>();
@@ -903,6 +912,7 @@ export class Html5Player extends Player {
903912
this.decodedFrames.clear();
904913
this.capturedFrames = null;
905914
this.capturedHash = null;
915+
this.bakingFrames = null;
906916
this.lastFrameIdx = -1;
907917
this.decodeInFlight = null;
908918
this.rebakeStarted = false;
@@ -1088,9 +1098,10 @@ export class Html5Player extends Player {
10881098
if (!this.disposed) console.warn("[Html5Player] still failed:", err);
10891099
return null;
10901100
});
1091-
let texture = await attempt();
1101+
const baked = this.bakingFrames?.[frame];
1102+
let texture = baked ? await this.bakingTexture(baked) : await attempt();
10921103
// A page can load too slowly once, so a failed still gets one more try while nothing newer is waiting.
1093-
if (!texture && this.liveStillFrame === frame && this.stillRequest === null) texture = await attempt();
1104+
if (!texture && !baked && this.liveStillFrame === frame && this.stillRequest === null) texture = await attempt();
10941105
const unbaked = this.mode === "editing" || this.mode === "stale" || this.mode === "capturing";
10951106
if (!texture || this.disposed || !unbaked || this.contentHash !== hash) {
10961107
texture?.destroy(true);
@@ -1111,6 +1122,11 @@ export class Html5Player extends Player {
11111122
}
11121123
}
11131124

1125+
// The resolution makes a scaled frame's texture report the clip's full size.
1126+
private async bakingTexture(blob: Blob): Promise<pixi.Texture> {
1127+
return pixi.Texture.from({ resource: await createImageBitmap(blob), resolution: this.bakingScale });
1128+
}
1129+
11141130
private removeLiveStill(): void {
11151131
this.stillRequest = null;
11161132
this.liveStillFrame = -1;

‎tests/html5-player.test.ts‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ type PlayerHarness = {
121121
renderStill(seconds: number): Promise<unknown>;
122122
transitionToPlayback(): Promise<void>;
123123
transitionToFailed(): void;
124+
bakingFrames: Array<Blob | undefined> | null;
125+
reloadAsset(): Promise<void>;
124126
};
125127

126128
function createPlayer() {
@@ -1488,6 +1490,98 @@ it("keeps showing stills after one fails to mount", async () => {
14881490
expect(warn).toHaveBeenCalledWith("[Html5Player] still failed:", expect.objectContaining({ message: "fit failed" }));
14891491
});
14901492

1493+
const bakedAt = (...indices: number[]) => Array.from({ length: 60 }, (_, i) => (indices.includes(i) ? new Blob([`baked ${i}`]) : undefined));
1494+
1495+
it("plays a frame from a bake still in progress instead of rendering a still", async () => {
1496+
const { player, harness, seeks } = createStillPlayer();
1497+
Object.assign(player, { getPlaybackTime: () => 0.5 });
1498+
baking(harness);
1499+
harness.beginCapture();
1500+
Object.assign(harness, { bakingFrames: bakedAt(15), bakingScale: 0.5 });
1501+
player.update(0, 0);
1502+
await until(stillShown(harness));
1503+
1504+
expect(await frameMarkup(layer(harness)[0].texture.source.blob)).toBe("baked 15");
1505+
expect((layer(harness)[0].texture as { resolution?: number }).resolution).toBe(0.5);
1506+
expect(seeks.some(seek => seek.startsWith("still"))).toBe(false);
1507+
});
1508+
1509+
it("renders a still where the bake in progress hasn't reached", async () => {
1510+
const { player, harness } = createStillPlayer();
1511+
Object.assign(player, { getPlaybackTime: () => 0.5 });
1512+
baking(harness);
1513+
harness.beginCapture();
1514+
Object.assign(harness, { bakingFrames: bakedAt(...Array.from({ length: 10 }, (_, i) => i)), bakingScale: 1 });
1515+
player.update(0, 0);
1516+
await until(stillShown(harness));
1517+
1518+
expect(await frameMarkup(layer(harness)[0].texture.source.blob)).toContain(">t=0.5<");
1519+
});
1520+
1521+
it("renders an export's still mid-bake, never a frame from the bake in progress", async () => {
1522+
const { player, harness } = createStillPlayer();
1523+
Object.assign(player, { getPlaybackTime: () => 0.5 });
1524+
baking(harness);
1525+
harness.beginCapture();
1526+
Object.assign(harness, { bakingFrames: bakedAt(...Array.from({ length: 60 }, (_, i) => i)), bakingScale: 1 });
1527+
const serialise = jest.spyOn(harness, "captureIframeAsForeignObjectSvg");
1528+
1529+
await player.prepareStaticRender();
1530+
1531+
expect(serialise).toHaveBeenCalledTimes(1);
1532+
expect(await frameMarkup((harness.contentContainer.children[0] as StaticSprite).texture.source.blob)).toContain(">t=0.5<");
1533+
player.endStaticRender();
1534+
});
1535+
1536+
it("forgets a bake's frames once the clip's content changes", async () => {
1537+
const { player, harness } = createStillPlayer();
1538+
baking(harness);
1539+
harness.beginCapture();
1540+
Object.assign(harness, { bakingFrames: bakedAt(0, 1, 2) });
1541+
jest.spyOn(harness, "hashAsset").mockResolvedValue("changed");
1542+
1543+
await player.reloadAsset();
1544+
1545+
expect(harness.bakingFrames).toBeNull();
1546+
});
1547+
1548+
it("forgets a failed bake's frames", () => {
1549+
const { harness } = createStillPlayer();
1550+
Object.assign(harness, { bakingFrames: bakedAt(0, 1, 2) });
1551+
1552+
harness.transitionToFailed();
1553+
1554+
expect(harness.bakingFrames).toBeNull();
1555+
});
1556+
1557+
it("keeps a sharper re-bake's frames hidden until it finishes", async () => {
1558+
const { harness } = createStillPlayer();
1559+
Object.assign(harness, { mode: "playback", capturedFrames: [new Blob(["baked 0"])], capturedHash: "original@0.5", capturedScale: 0.5 });
1560+
const { held, releaseAll } = holdToBlob();
1561+
const capture = harness.runCapture(1);
1562+
await until(() => held.length > 0);
1563+
1564+
expect(harness.bakingFrames).toBeNull();
1565+
releaseAll();
1566+
await capture;
1567+
});
1568+
1569+
it("shows a first bake's frames as they land", async () => {
1570+
const { harness } = createStillPlayer();
1571+
baking(harness);
1572+
harness.beginCapture();
1573+
const { held, releaseAll } = holdToBlob();
1574+
const capture = harness.runCapture();
1575+
await until(() => held.length > 1);
1576+
held.shift()!();
1577+
await until(() => harness.bakingFrames?.[0] !== undefined);
1578+
1579+
expect(harness.bakingFrames).toHaveLength(60);
1580+
expect(harness.bakingFrames!.filter(Boolean)).toHaveLength(1);
1581+
releaseAll();
1582+
await capture;
1583+
});
1584+
14911585
it("swaps the still for baked frames once the bake finishes", async () => {
14921586
const { player, harness } = createStillPlayer();
14931587
let finish: (frames: Blob[]) => void = () => {};

0 commit comments

Comments
 (0)