Skip to content

Commit 7d16635

Browse files
committed
fix(engine): a chunk's first frame shows its own time, not the composition's opening frame
1 parent 5e7ba0b commit 7d16635

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { afterEach, describe, expect, it, vi } from "vitest";
2+
import { mkdtempSync, rmSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
5+
import { captureFrameToBuffer, createCaptureSession } from "./frameCapture.js";
6+
import { beginFrameCapture } from "./screenshotService.js";
7+
8+
const calls: string[] = [];
9+
10+
vi.mock("./browserManager.js", async (importOriginal) => ({
11+
...(await importOriginal<typeof import("./browserManager.js")>()),
12+
resolveBrowserGpuMode: async () => "software",
13+
acquireBrowser: async () => ({
14+
captureMode: "beginframe",
15+
browser: {
16+
version: async () => "HeadlessChrome/150.0.0.0",
17+
newPage: async () => ({
18+
mainFrame: () => ({}),
19+
evaluateOnNewDocument: async () => {},
20+
setViewport: async () => {},
21+
evaluate: async () => undefined,
22+
createCDPSession: async () => ({
23+
send: async (
24+
method: string,
25+
params: { frameTimeTicks?: number; noDisplayUpdates?: boolean },
26+
) => {
27+
calls.push(
28+
`${method} ticks=${params.frameTimeTicks} noDisplayUpdates=${params.noDisplayUpdates}`,
29+
);
30+
return {};
31+
},
32+
}),
33+
}),
34+
},
35+
}),
36+
}));
37+
vi.mock("./screenshotService.js", async (importOriginal) => ({
38+
...(await importOriginal<typeof import("./screenshotService.js")>()),
39+
beginFrameCapture: vi.fn(async (_page: unknown, _options: unknown, frameTimeTicks: number) => {
40+
calls.push(`capture ticks=${frameTimeTicks}`);
41+
return { buffer: Buffer.alloc(8), hasDamage: true };
42+
}),
43+
}));
44+
45+
const dirs: string[] = [];
46+
afterEach(() => {
47+
calls.length = 0;
48+
vi.mocked(beginFrameCapture).mockClear();
49+
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
50+
});
51+
52+
async function beginFrameSession(baseTicks: number) {
53+
const dir = mkdtempSync(join(tmpdir(), "hf-first-beginframe-"));
54+
dirs.push(dir);
55+
const session = await createCaptureSession(
56+
"http://localhost:0",
57+
dir,
58+
{ width: 1920, height: 1080, fps: { num: 30, den: 1 }, format: "jpeg" },
59+
null,
60+
{},
61+
);
62+
session.isInitialized = true;
63+
session.captureMode = "beginframe";
64+
session.beginFrameTimeTicks = baseTicks;
65+
return session;
66+
}
67+
68+
describe("first BeginFrame capture of a session", () => {
69+
// Measured on Linux headless-shell: without this, a chunk's first frame came back as the
70+
// frame composited during init (the composition at t=0) in about half the runs.
71+
it("displays the seek once before the first capture, between the probe tick and the capture tick", async () => {
72+
const session = await beginFrameSession(2000);
73+
const interval = session.beginFrameIntervalMs;
74+
75+
await captureFrameToBuffer(session, 0, 70 / 30);
76+
await captureFrameToBuffer(session, 1, 71 / 30);
77+
78+
expect(calls).toEqual([
79+
`HeadlessExperimental.beginFrame ticks=${2000 - interval / 2} noDisplayUpdates=false`,
80+
"capture ticks=2000",
81+
`capture ticks=${2000 + interval}`,
82+
]);
83+
// The producer's liveness probe runs at base - 5 intervals; ticks must stay monotonic.
84+
expect(2000 - interval / 2).toBeGreaterThan(2000 - 5 * interval);
85+
});
86+
87+
it("does it again on a fresh session (worker or retry page)", async () => {
88+
await captureFrameToBuffer(await beginFrameSession(2000), 0, 0);
89+
await captureFrameToBuffer(await beginFrameSession(2000), 0, 0);
90+
expect(calls.filter((c) => c.startsWith("HeadlessExperimental.beginFrame"))).toHaveLength(2);
91+
});
92+
});

‎packages/engine/src/services/frameCapture.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ export interface CaptureSession {
236236
beginFrameIntervalMs: number;
237237
beginFrameHasDamageCount: number;
238238
beginFrameNoDamageCount: number;
239+
beginFrameSeekPresented?: boolean;
239240
/** Optional producer config — when set, overrides module-level env var constants. */
240241
config?: Partial<EngineConfig>;
241242
/** True if running on SwiftShader (detected at init). Undefined before init. */
@@ -3995,6 +3996,17 @@ async function captureFrameSurface(
39953996

39963997
if (session.captureMode === "beginframe") {
39973998
const frameTimeTicks = session.beginFrameTimeTicks + frameIndex * session.beginFrameIntervalMs;
3999+
if (!session.beginFrameSeekPresented) {
4000+
// A session's first capture can return the frame composited during init instead of
4001+
// this seek, so display the seek once at a tick between the probe and this capture.
4002+
const client = await getCdpSession(page);
4003+
await client.send("HeadlessExperimental.beginFrame", {
4004+
frameTimeTicks: frameTimeTicks - session.beginFrameIntervalMs / 2,
4005+
interval: session.beginFrameIntervalMs,
4006+
noDisplayUpdates: false,
4007+
});
4008+
session.beginFrameSeekPresented = true;
4009+
}
39984010
const result = await beginFrameCapture(
39994011
page,
40004012
options,

0 commit comments

Comments
 (0)