Skip to content

Commit 774f1bd

Browse files
committed
fix: publish html5 frames as soon as their draws finish
1 parent 0ec45eb commit 774f1bd

2 files changed

Lines changed: 45 additions & 14 deletions

File tree

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,6 @@ export class Html5Player extends Player {
494494
const frameBytes = Math.trunc(W * scale) * Math.trunc(H * scale) * 4;
495495
const framesInFlight = captureFramesInFlight(navigator.hardwareConcurrency, frameBytes);
496496
const { inFlight } = Html5Player;
497-
const hasRoom = (): boolean =>
498-
inFlight.frames === 0 || (inFlight.frames < framesInFlight && inFlight.bytes + frameBytes <= CAPTURE_MEMORY_BUDGET_BYTES);
499497
// The playhead's frame goes first, so the clip under it plays soonest.
500498
const segments = passSegments(
501499
Array.from({ length: frameCount }, (_, i) => i),
@@ -510,16 +508,32 @@ export class Html5Player extends Player {
510508
this.bakingFrames = frames;
511509
this.bakingScale = scale;
512510
}
513-
const pending: { idx: number; drawn: Promise<Blob> }[] = [];
511+
const pending: { idx: number; drawn: Promise<Blob>; settled?: Blob }[] = [];
512+
const hasRoom = (): boolean =>
513+
pending.length < framesInFlight &&
514+
(inFlight.frames === 0 || (inFlight.frames < framesInFlight && inFlight.bytes + frameBytes <= CAPTURE_MEMORY_BUDGET_BYTES));
514515
// The SVG is a frame's whole input, so a frame that repeats a recent one draws identical bytes and shares its Blob.
515516
const recent = new Map<string, Promise<Blob>>();
516517
let recentStyles = "";
517518
let issued = 0;
518519
let published = 0;
519520
let progressStep = 0;
520521
let sliceStart = -Infinity;
522+
const publish = (idx: number, frame: Blob): void => {
523+
frames[idx] = frame;
524+
published += 1;
525+
this.captureFramesDone += 1;
526+
// Steps, not frames: enough for a ring to fill smoothly, and bounded whatever the frame count.
527+
const step = Math.floor((this.captureFramesDone / frameCount) * CAPTURE_PROGRESS_STEPS);
528+
if (step > progressStep) {
529+
progressStep = step;
530+
this.emitCaptureProgress();
531+
}
532+
};
521533
// Frames publish in the order they were issued, so the oldest in flight gates the window.
522534
while (published < frameCount) {
535+
// Draws finish while the bake yields, so finished frames publish before the next one is drawn.
536+
for (let head = pending[0]?.settled; head; head = pending[0]?.settled) publish(pending.shift()!.idx, head);
523537
if (issued < frameCount && hasRoom()) {
524538
const idx = order[issued];
525539
this.bakeNextIndex = idx;
@@ -558,22 +572,21 @@ export class Html5Player extends Player {
558572
if (recent.size >= REPEAT_WINDOW_FRAMES) recent.delete(recent.keys().next().value!);
559573
recent.set(body, drawn);
560574
}
561-
pending.push({ idx, drawn });
575+
const entry: (typeof pending)[number] = { idx, drawn };
576+
drawn.then(
577+
blob => {
578+
entry.settled = blob;
579+
},
580+
() => undefined
581+
);
582+
pending.push(entry);
562583
issued += 1;
563584
} else if (pending.length > 0) {
564585
const { idx, drawn } = pending.shift()!;
565586
const frame = await drawn;
566587
if (stale()) return null;
567-
frames[idx] = frame;
568-
published += 1;
569-
this.captureFramesDone += 1;
570-
// Steps, not frames: enough for a ring to fill smoothly, and bounded whatever the frame count.
571-
const step = Math.floor((this.captureFramesDone / frameCount) * CAPTURE_PROGRESS_STEPS);
572-
if (step > progressStep) {
573-
progressStep = step;
574-
this.emitCaptureProgress();
575-
}
576-
} else {
588+
publish(idx, frame);
589+
} else if (issued < frameCount) {
577590
// Other bakes' frames fill the window, and they finish without their bakes' turns.
578591
await nextFrame();
579592
if (stale()) return null;

‎tests/html5-player.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,24 @@ const sliceEveryFrame = () => {
869869

870870
const sharedInFlight = () => (Html5Player as unknown as { inFlight: { frames: number; bytes: number } }).inFlight;
871871

872+
it("publishes frames and progress while later frames still draw", async () => {
873+
const { harness, events } = createStillPlayer(); // 2 s at 30 fps: 60 frames
874+
Object.assign(harness, { mode: "capturing" });
875+
sliceEveryFrame();
876+
const steps = () => events.emit.mock.calls.filter(([name]) => name === InternalEvent.CaptureProgressChanged).length;
877+
let atLastSeek = { landed: 0, steps: 0 };
878+
const record = jest.mocked(harness.seekHarness).getMockImplementation()!;
879+
jest.mocked(harness.seekHarness).mockImplementation((seconds, win) => {
880+
record(seconds, win);
881+
if (seconds === 59 / 30) atLastSeek = { landed: harness.bakingFrames!.filter(Boolean).length, steps: steps() };
882+
});
883+
884+
await harness.runCapture();
885+
886+
expect(atLastSeek.landed).toBeGreaterThan(30);
887+
expect(atLastSeek.steps).toBeGreaterThan(10);
888+
});
889+
872890
it("waits for another bake's frames to leave the window before drawing its own", async () => {
873891
const { harness } = createStillPlayer();
874892
Object.assign(harness, { renderedWidth: 3840, renderedHeight: 2160 });

0 commit comments

Comments
 (0)