Skip to content

Commit b1fbc16

Browse files
fix(cli): check finishes on video-heavy projects, its geometry seeks skip only video-frame waits (#5715)
* fix(cli): check's dense geometry seeks stop waiting for video frames they never read * test(cli): race the dense seek against the barrier ask instead of a timer * fix(cli): keep the geometry seek's comment share at its base * fix(cli): dense geometry seeks still wait for frame sources, only video frames are skipped
1 parent 13ea834 commit b1fbc16

8 files changed

Lines changed: 76 additions & 17 deletions

File tree

‎packages/cli/src/capture/captureCompositionFrame.test.ts‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function fakeSeekPage() {
2727
const evaluate = vi.fn(
2828
async (
2929
_pageFunction: Parameters<CompositionSeekPage["evaluate"]>[0],
30-
_value?: number,
30+
_value?: Parameters<CompositionSeekPage["evaluate"]>[1],
3131
_fallbackToBridgeAndTimelines?: boolean,
3232
): Promise<unknown> => undefined,
3333
);
@@ -108,7 +108,7 @@ describe("seekCompositionTimeline", () => {
108108
expect(waitForFunction).not.toHaveBeenCalled();
109109
expect(evaluate).toHaveBeenCalledTimes(4);
110110
expect(evaluate).toHaveBeenNthCalledWith(1, expect.any(Function), 1.25, false, false);
111-
expect(evaluate).toHaveBeenNthCalledWith(2, expect.any(Function));
111+
expect(evaluate).toHaveBeenNthCalledWith(2, expect.any(Function), { videoFrames: true });
112112
expect(evaluate.mock.calls[2]?.[0]).toContain("window.setTimeout(finish, 100)");
113113
// Post-seek font settle: a seek can reveal glyphs whose unicode-range
114114
// subsets only start loading after the next layout (CJK snapshot reports).
@@ -230,7 +230,7 @@ describe("seekCompositionTimeline", () => {
230230

231231
expect(evaluate).toHaveBeenCalledTimes(4);
232232
expect(evaluate).toHaveBeenNthCalledWith(1, expect.any(Function), 4, true, false);
233-
expect(evaluate).toHaveBeenNthCalledWith(2, expect.any(Function));
233+
expect(evaluate).toHaveBeenNthCalledWith(2, expect.any(Function), { videoFrames: true });
234234
expect(evaluate).toHaveBeenNthCalledWith(3, expect.any(Function));
235235
expect(evaluate).toHaveBeenNthCalledWith(4, expect.any(Function), 500);
236236
});
@@ -471,6 +471,26 @@ describe("installPageFunctionGuard", () => {
471471
});
472472

473473
describe("DENSE_GEOMETRY_SEEK_OPTIONS", () => {
474+
// A 79 s project of 1080p H.264 clips: each of the 600 dense seeks waited ~185 ms for a video frame it never reads,
475+
// so `check` ran past its deadline. Frame sources and hf-seek work can move the DOM, so those still hold it.
476+
it("skips only the seek barrier's video-frame holds, while the audit seek waits for all", async () => {
477+
const barrierCalls = async (options: Parameters<typeof seekCompositionTimeline>[2]) => {
478+
const { page, evaluate } = fakeSeekPage();
479+
const barrier = vi.fn(async (_options?: { videoFrames?: boolean }) => {});
480+
vi.stubGlobal("window", { __hfWaitForSeekCompletion: barrier });
481+
evaluate.mockImplementation(async (pageFunction, value) =>
482+
typeof pageFunction === "function" && typeof value === "object"
483+
? Reflect.apply(pageFunction, undefined, [value])
484+
: undefined,
485+
);
486+
await seekCompositionTimeline(page, 3, options);
487+
return barrier.mock.calls;
488+
};
489+
490+
expect(await barrierCalls(DENSE_GEOMETRY_SEEK_OPTIONS)).toEqual([[{ videoFrames: false }]]);
491+
expect(await barrierCalls(AUDIT_SEEK_OPTIONS)).toEqual([[{ videoFrames: true }]]);
492+
});
493+
474494
it("is genuinely geometry-only — no post-seek settle waits at the 600-sample cap", () => {
475495
// Dense pass must not inherit AUDIT's post-seek waits — geometry is valid synchronously after setTime, and waits multiply by sample count.
476496
expect(DENSE_GEOMETRY_SEEK_OPTIONS.animationFrameSettle).toBe("none");

‎packages/cli/src/capture/captureCompositionFrame.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const DENSE_GEOMETRY_SEEK_OPTIONS = {
4040
animationFrameSettle: "none",
4141
waitForFontsMs: 0,
4242
settleMs: 0,
43+
waitForVideoFrames: false,
4344
} as const;
4445

4546
export interface SeekCompositionTimelineOptions {
@@ -49,18 +50,22 @@ export interface SeekCompositionTimelineOptions {
4950
animationFrameSettle?: "race" | "double" | "none";
5051
waitForFontsMs?: number;
5152
settleMs?: number;
53+
waitForVideoFrames?: boolean;
5254
}
5355

56+
type SeekBarrierOptions = { videoFrames: boolean };
57+
5458
type CompositionPageFunction =
5559
| string
5660
| (() => unknown)
5761
| ((value: number) => unknown)
62+
| ((barrier: SeekBarrierOptions) => unknown)
5863
| ((value: number, fallbackToBridgeAndTimelines: boolean, exactTime: boolean) => unknown);
5964

6065
export interface CompositionEvaluationPage {
6166
evaluate(
6267
pageFunction: CompositionPageFunction,
63-
value?: number,
68+
value?: number | SeekBarrierOptions,
6469
fallbackToBridgeAndTimelines?: boolean,
6570
exactTime?: boolean,
6671
): Promise<unknown>;
@@ -341,12 +346,15 @@ export async function seekCompositionTimeline(
341346
options.exactTime === true,
342347
);
343348

344-
await page.evaluate(async () => {
345-
const waitForCompletion = Reflect.get(window, "__hfWaitForSeekCompletion");
346-
if (typeof waitForCompletion === "function") {
347-
await Reflect.apply(waitForCompletion, window, []);
348-
}
349-
});
349+
await page.evaluate(
350+
async (barrier: SeekBarrierOptions) => {
351+
const waitForCompletion = Reflect.get(window, "__hfWaitForSeekCompletion");
352+
if (typeof waitForCompletion === "function") {
353+
await Reflect.apply(waitForCompletion, window, [barrier]);
354+
}
355+
},
356+
{ videoFrames: options.waitForVideoFrames !== false },
357+
);
350358

351359
const animationFrameSettle = options.animationFrameSettle ?? "race";
352360
if (animationFrameSettle === "race") {

‎packages/core/src/runtime/adapters/seek-dispatch.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { swallow } from "../diagnostics";
2121
let _lastDispatchedTime = -1;
2222
type SeekCompletionResult = { status: "fulfilled" } | { status: "rejected"; reason: unknown };
2323
let _pendingCompletions = new Set<Promise<SeekCompletionResult>>();
24+
let _videoFrameCompletions = new WeakSet<Promise<SeekCompletionResult>>();
2425
let _pendingFailure: { reason: unknown } | undefined;
2526
let _activeCompletionBarriers = 0;
2627

@@ -62,11 +63,12 @@ function dispatch(time: number): void {
6263
* its preview-side capture here, so the CLI seek paths that already await this
6364
* barrier wait for a `self` chain's paint instead of racing it.
6465
*/
65-
export function registerSeekCompletion(promise: Promise<unknown>): void {
66+
export function registerSeekCompletion(promise: Promise<unknown>, kind?: "video-frame"): void {
6667
const completion = promise
6768
.then<SeekCompletionResult>(() => ({ status: "fulfilled" }))
6869
.catch<SeekCompletionResult>((reason: unknown) => ({ status: "rejected", reason }));
6970
_pendingCompletions.add(completion);
71+
if (kind === "video-frame") _videoFrameCompletions.add(completion);
7072
void completion.then((result) => {
7173
if (!_pendingCompletions.delete(completion)) return;
7274
if (result.status === "rejected" && _pendingFailure === undefined) {
@@ -100,15 +102,20 @@ export function isSeekCompletionBarrierActive(): boolean {
100102
return _activeCompletionBarriers > 0;
101103
}
102104

103-
export async function waitForSeekCompletion(): Promise<void> {
105+
/** `videoFrames: false` skips only the video-frame holds: a geometry read needs every other completion. */
106+
export async function waitForSeekCompletion(options?: { videoFrames?: boolean }): Promise<void> {
107+
const awaited = () =>
108+
[..._pendingCompletions].filter(
109+
(completion) => options?.videoFrames !== false || !_videoFrameCompletions.has(completion),
110+
);
104111
_activeCompletionBarriers += 1;
105112
let failed = _pendingFailure;
106113
try {
107114
// Let concurrently started barriers snapshot the same retained failure
108115
// before either one consumes it.
109116
await Promise.resolve();
110-
while (_pendingCompletions.size > 0) {
111-
const results = await Promise.all([..._pendingCompletions]);
117+
for (let pending = awaited(); pending.length > 0; pending = awaited()) {
118+
const results = await Promise.all(pending);
112119
const rejected = results.find((result) => result.status === "rejected");
113120
if (failed === undefined && rejected?.status === "rejected") {
114121
failed = { reason: rejected.reason };
@@ -131,6 +138,7 @@ export async function waitForSeekCompletion(): Promise<void> {
131138
export function resetSeekDispatchState(): void {
132139
_lastDispatchedTime = -1;
133140
_pendingCompletions = new Set();
141+
_videoFrameCompletions = new WeakSet();
134142
_pendingFailure = undefined;
135143
_activeCompletionBarriers = 0;
136144
}

‎packages/core/src/runtime/frameSources.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ afterEach(() => {
4242
});
4343

4444
describe("frame sources", () => {
45+
it("holds a geometry read that skips video frames until its asynchronous draw lands", async () => {
46+
const frame = deferred();
47+
disposers.push(registerFrameSource({ element: mount(), render: () => frame.promise }));
48+
adapter().seek({ time: 2 });
49+
let captured = false;
50+
const capture = waitForSeekCompletion({ videoFrames: false }).then(() => (captured = true));
51+
await Promise.resolve();
52+
expect(captured).toBe(false);
53+
frame.resolve();
54+
await capture;
55+
expect(captured).toBe(true);
56+
});
57+
4558
it("holds capture through setup and asynchronous drawing", async () => {
4659
const setup = deferred();
4760
const frame = deferred();

‎packages/core/src/runtime/media.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,12 @@ describe("syncRuntimeMedia", () => {
742742
expect(await barrierSettled(barrier)).toBe(true);
743743
});
744744

745+
it("does not hold a geometry read for a seeking video's frame", async () => {
746+
seekColdVideo();
747+
expect(await barrierSettled(waitForSeekCompletion({ videoFrames: false }))).toBe(true);
748+
expect(await barrierSettled(waitForSeekCompletion())).toBe(false);
749+
});
750+
745751
function seekLoadingVideo(): RuntimeMediaClip {
746752
const clip = createMockClip({ start: 7.1, end: 18.24 });
747753
Object.defineProperty(clip.el, "readyState", { value: 0, configurable: true });

‎packages/core/src/runtime/media.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function holdSeekBarrierUntilVideoLands(el: HTMLMediaElement): void {
4848
for (const type of HOLD_END_EVENTS) el.addEventListener(type, done, true);
4949
releaseHeldVideo.set(el, done);
5050
}),
51+
"video-frame",
5152
);
5253
}
5354

‎packages/core/src/runtime/mediaProxy.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,10 @@ export function swapToProxy(
347347
});
348348
});
349349
// A frame capture holds for the copy as it held for a loading video before, up to the same cap.
350-
registerSeekCompletion(Promise.race([swap, new Promise((cap) => setTimeout(cap, HOLD_CAP_MS))]));
350+
registerSeekCompletion(
351+
Promise.race([swap, new Promise((cap) => setTimeout(cap, HOLD_CAP_MS))]),
352+
"video-frame",
353+
);
351354
const codecName = entry?.codecName ?? null;
352355
const details: Record<string, RuntimeJson> = {
353356
asset: originalSrc,

‎packages/core/src/runtime/window.d.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ declare global {
132132
__hfReseekGpu?: (time: number) => void;
133133
/**
134134
* Await GPU work registered synchronously by `hf-seek` listeners through
135-
* `event.detail.waitUntil(...)`.
135+
* `event.detail.waitUntil(...)`; `videoFrames: false` skips the video-frame holds.
136136
*/
137-
__hfWaitForSeekCompletion?: () => Promise<void>;
137+
__hfWaitForSeekCompletion?: (options?: { videoFrames?: boolean }) => Promise<void>;
138138
/**
139139
* Canonical root-timeline start for a media element. Snapshot capture uses
140140
* this runtime-owned resolver so reference expressions, authored timing

0 commit comments

Comments
 (0)