-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathframeCapture.ts
More file actions
792 lines (716 loc) · 29.4 KB
/
Copy pathframeCapture.ts
File metadata and controls
792 lines (716 loc) · 29.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
/**
* Frame Capture Service
*
* Uses Puppeteer to capture frames from any web page implementing the
* window.__hf seek protocol. Navigates to a file server URL, waits for
* the page to expose window.__hf, then captures frames deterministically
* via Chrome's BeginFrame API or Page.captureScreenshot fallback.
*/
import { type Browser, type Page, type Viewport, type ConsoleMessage } from "puppeteer-core";
import { existsSync, mkdirSync, writeFileSync } from "fs";
import { join } from "path";
import { quantizeTimeToFrame } from "@hyperframes/core";
// ── Extracted modules ───────────────────────────────────────────────────────
import {
acquireBrowser,
releaseBrowser,
forceReleaseBrowser,
buildChromeArgs,
resolveHeadlessShellPath,
type CaptureMode,
} from "./browserManager.js";
import {
beginFrameCapture,
getCdpSession,
pageScreenshotCapture,
initTransparentBackground,
} from "./screenshotService.js";
import { DEFAULT_CONFIG, type EngineConfig } from "../config.js";
import type {
CaptureOptions,
CaptureVideoMetadataHint,
CaptureResult,
CaptureBufferResult,
CapturePerfSummary,
} from "../types.js";
export type { CaptureOptions, CaptureResult, CaptureBufferResult, CapturePerfSummary };
/** Called after seeking, before screenshot. Use for video frame injection or other pre-capture work. */
export type BeforeCaptureHook = (page: Page, time: number) => Promise<void>;
export interface CaptureSession {
browser: Browser;
page: Page;
options: CaptureOptions;
serverUrl: string;
outputDir: string;
onBeforeCapture: BeforeCaptureHook | null;
isInitialized: boolean;
// Tracks whether the page/browser handles have already been released by
// closeCaptureSession. Used to make closeCaptureSession idempotent under
// browser-pool semantics (see the function body for the full invariant).
pageReleased?: boolean;
browserReleased?: boolean;
browserConsoleBuffer: string[];
capturePerf: {
frames: number;
seekMs: number;
beforeCaptureMs: number;
screenshotMs: number;
totalMs: number;
};
captureMode: CaptureMode;
// BeginFrame state
beginFrameTimeTicks: number;
beginFrameIntervalMs: number;
beginFrameHasDamageCount: number;
beginFrameNoDamageCount: number;
/** Optional producer config — when set, overrides module-level env var constants. */
config?: Partial<EngineConfig>;
}
// Circular buffer for browser console messages dumped on render failure diagnostics.
// Complex compositions produce 100+ messages; 50 was too small to capture relevant errors.
const BROWSER_CONSOLE_BUFFER_SIZE = 200;
const CAPTURE_SESSION_CLOSE_TIMEOUT_MS = 5_000;
async function waitForCloseWithTimeout(promise: Promise<unknown>): Promise<boolean> {
let timedOut = false;
let timer: ReturnType<typeof setTimeout> | undefined;
await Promise.race([
promise.then(
() => undefined,
() => undefined,
),
new Promise<void>((resolve) => {
timer = setTimeout(() => {
timedOut = true;
resolve();
}, CAPTURE_SESSION_CLOSE_TIMEOUT_MS);
}),
]);
if (timer) clearTimeout(timer);
return !timedOut;
}
export async function createCaptureSession(
serverUrl: string,
outputDir: string,
options: CaptureOptions,
onBeforeCapture: BeforeCaptureHook | null = null,
config?: Partial<EngineConfig>,
): Promise<CaptureSession> {
if (!existsSync(outputDir)) mkdirSync(outputDir, { recursive: true });
// Determine capture mode before building args — BeginFrame flags only apply on Linux.
// BeginFrame's compositor does not preserve alpha; callers that pass
// `options.format === "png"` for transparent capture should also set
// `config.forceScreenshot = true` (the producer's renderOrchestrator does this
// automatically when `RenderConfig.format` is an alpha-capable value).
const headlessShell = resolveHeadlessShellPath(config);
const isLinux = process.platform === "linux";
const forceScreenshot = config?.forceScreenshot ?? DEFAULT_CONFIG.forceScreenshot;
const preMode: CaptureMode =
headlessShell && isLinux && !forceScreenshot ? "beginframe" : "screenshot";
const chromeArgs = buildChromeArgs(
{ width: options.width, height: options.height, captureMode: preMode },
config,
);
const { browser, captureMode } = await acquireBrowser(chromeArgs, config);
const page = await browser.newPage();
// Polyfill esbuild's keepNames helper inside the page.
//
// The engine is published as raw TypeScript (`packages/engine/package.json`
// points `main`/`exports` at `./src/index.ts`) and downstream consumers
// execute it through transpilers that may inject `__name(fn, "name")`
// wrappers around named functions. Empirically, this happens with:
// - tsx (its esbuild loader runs with keepNames=true), used by the
// producer's parity-harness, ad-hoc dev scripts, and the
// `bun run --filter @hyperframes/engine test` Vitest path.
// - any tsup/esbuild build that explicitly enables keepNames.
//
// The HeyGen CLI (`packages/cli`) bundles this engine via tsup with
// keepNames left at its default (false) — verified by grepping
// `packages/cli/dist/cli.js`, where `__name(...)` call sites are absent.
// Bun's TS loader also does not currently inject `__name`. Even so,
// anything that calls `page.evaluate(fn)` with a nested named function
// under tsx (most local development and tests) will serialize bodies
// like `__name(nested,"nested")` and crash with `__name is not defined`
// in the browser. The shim makes such calls a no-op.
//
// An alternative is to load browser-side code as raw text and inject it
// via `page.addScriptTag({ content: ... })` — see
// `packages/cli/src/commands/contrast-audit.browser.js` for that pattern.
// Until every `page.evaluate(fn)` call site migrates, this polyfill is
// the single line of defense. The companion regression test in
// `frameCapture-namePolyfill.test.ts` verifies the shim stays wired up.
await page.evaluateOnNewDocument(() => {
const w = window as unknown as { __name?: <T>(fn: T, _name: string) => T };
if (typeof w.__name !== "function") {
w.__name = <T>(fn: T, _name: string): T => fn;
}
});
// Inject render-time variable overrides before any page script runs, so the
// runtime helper `getVariables()` returns the merged result on its first
// call. Pass the JSON string and parse inside the page so we don't require
// any JSON-incompatible value to round-trip through Puppeteer's serializer.
if (options.variables && Object.keys(options.variables).length > 0) {
const variablesJson = JSON.stringify(options.variables);
await page.evaluateOnNewDocument((json: string) => {
type WindowWithVariables = Window & { __hfVariables?: Record<string, unknown> };
try {
(window as WindowWithVariables).__hfVariables = JSON.parse(json);
} catch {
// The CLI validated the JSON before this point — a parse failure here
// means the page swapped JSON.parse, which is the page's problem.
}
}, variablesJson);
}
const browserVersion = await browser.version();
const expectedMajor = config?.expectedChromiumMajor;
if (Number.isFinite(expectedMajor)) {
const actualChromiumMajor = Number.parseInt(
(browserVersion.match(/(\d+)\./) || [])[1] || "",
10,
);
if (Number.isFinite(actualChromiumMajor) && actualChromiumMajor !== expectedMajor) {
throw new Error(
`[FrameCapture] Chromium major mismatch expected=${expectedMajor} actual=${actualChromiumMajor} raw=${browserVersion}`,
);
}
}
const viewport: Viewport = {
width: options.width,
height: options.height,
deviceScaleFactor: options.deviceScaleFactor || 1,
};
await page.setViewport(viewport);
// Transparent-background setup is intentionally NOT done here. Chrome resets
// the default-background-color override on navigation, and the
// `[data-composition-id]{background:transparent}` stylesheet that
// `initTransparentBackground` injects must land in a real `document.head`.
// See `initializeSession()` below — it calls `initTransparentBackground` for
// PNG captures after `page.goto(...)` and the `window.__hf` readiness poll.
return {
browser,
page,
options,
serverUrl,
outputDir,
onBeforeCapture,
isInitialized: false,
browserConsoleBuffer: [],
capturePerf: {
frames: 0,
seekMs: 0,
beforeCaptureMs: 0,
screenshotMs: 0,
totalMs: 0,
},
captureMode,
beginFrameTimeTicks: 0,
beginFrameIntervalMs: 1000 / Math.max(1, options.fps),
beginFrameHasDamageCount: 0,
beginFrameNoDamageCount: 0,
config,
};
}
/**
* Classify a console "Failed to load resource" error as a font-load failure.
*
* These are expected when deterministic font injection replaces Google Fonts
* @import URLs with embedded base64 — or when the render environment has no
* network access to Google Fonts. Suppressing them reduces noise in render
* output without hiding real asset failures (images, videos, scripts, etc.).
*
* Chrome's `msg.text()` for a failed resource is typically just
* `"Failed to load resource: net::ERR_FAILED"` — the URL is only on
* `msg.location().url`. We match against both so the filter works regardless
* of which form Chrome emits.
*/
export function isFontResourceError(type: string, text: string, locationUrl: string): boolean {
if (type !== "error") return false;
if (!text.startsWith("Failed to load resource")) return false;
return /fonts\.googleapis|fonts\.gstatic|\.(woff2?|ttf|otf)(\b|$)/i.test(
`${locationUrl} ${text}`,
);
}
async function pollPageExpression(
page: Page,
expression: string,
timeoutMs: number,
intervalMs: number = 100,
): Promise<boolean> {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
const ready = Boolean(await page.evaluate(expression));
if (ready) return true;
await new Promise((resolve) => setTimeout(resolve, intervalMs));
}
return Boolean(await page.evaluate(expression));
}
async function applyVideoMetadataHints(
page: Page,
hints: readonly CaptureVideoMetadataHint[] | undefined,
): Promise<void> {
if (!hints || hints.length === 0) return;
await page.evaluate(
(metadataHints: CaptureVideoMetadataHint[]) => {
for (const hint of metadataHints) {
if (
!hint.id ||
!Number.isFinite(hint.width) ||
!Number.isFinite(hint.height) ||
hint.width <= 0 ||
hint.height <= 0
) {
continue;
}
const video = document.getElementById(hint.id) as HTMLVideoElement | null;
if (!video) continue;
if (!video.hasAttribute("width")) video.setAttribute("width", String(hint.width));
if (!video.hasAttribute("height")) video.setAttribute("height", String(hint.height));
const computed = window.getComputedStyle(video);
if (
!video.style.aspectRatio &&
(!computed.aspectRatio || computed.aspectRatio === "auto")
) {
video.style.aspectRatio = `${hint.width} / ${hint.height}`;
}
}
},
[...hints],
);
}
async function waitForOptionalTailwindReady(page: Page, timeoutMs: number): Promise<void> {
const hasTailwindReady = await page.evaluate(
`(() => { const ready = window.__tailwindReady; return !!ready && typeof ready.then === "function"; })()`,
);
if (!hasTailwindReady) return;
const ready = await Promise.race([
page.evaluate(
`Promise.resolve(window.__tailwindReady).then(() => true, () => false)`,
) as Promise<boolean>,
new Promise<boolean>((resolve) => setTimeout(() => resolve(false), timeoutMs)),
]);
if (!ready) {
throw new Error(
`[FrameCapture] window.__tailwindReady not resolved after ${timeoutMs}ms. Tailwind browser runtime must finish before frame capture starts.`,
);
}
}
export async function initializeSession(session: CaptureSession): Promise<void> {
const { page, serverUrl } = session;
// Forward browser console to host with [Browser] prefix
page.on("console", (msg: ConsoleMessage) => {
const type = msg.type();
const text = msg.text();
const locationUrl = msg.location()?.url ?? "";
const isFontLoadError = isFontResourceError(type, text, locationUrl);
// Other "Failed to load resource" 404s are typically non-blocking (e.g.
// favicon, sourcemaps, optional assets). Prefix them so users know they
// are harmless and don't confuse them with real render errors.
const isResourceLoadError =
type === "error" && text.startsWith("Failed to load resource") && !isFontLoadError;
const prefix = isResourceLoadError
? "[non-blocking]"
: type === "error"
? "[Browser:ERROR]"
: type === "warn"
? "[Browser:WARN]"
: "[Browser]";
if (!isFontLoadError) {
console.log(`${prefix} ${text}`);
}
session.browserConsoleBuffer.push(`${prefix} ${text}`);
if (session.browserConsoleBuffer.length > BROWSER_CONSOLE_BUFFER_SIZE) {
session.browserConsoleBuffer.shift();
}
});
page.on("pageerror", (err) => {
const message = err instanceof Error ? err.message : String(err);
const text = `[Browser:PAGEERROR] ${message}`;
// Benign play/pause race during frame capture — suppress terminal noise, keep in buffer.
const isPlayAbort =
/^AbortError:/.test(message) && message.includes("play()") && message.includes("pause()");
if (!isPlayAbort) {
console.error(text);
}
session.browserConsoleBuffer.push(text);
if (session.browserConsoleBuffer.length > BROWSER_CONSOLE_BUFFER_SIZE) {
session.browserConsoleBuffer.shift();
}
});
// Navigate to the file server
const url = `${serverUrl}/index.html`;
if (session.captureMode === "screenshot") {
// Screenshot mode: standard navigation, rAF works normally
await page.goto(url, { waitUntil: "domcontentloaded", timeout: 60000 });
const pageReadyTimeout =
session.config?.playerReadyTimeout ?? DEFAULT_CONFIG.playerReadyTimeout;
const pageReady = await pollPageExpression(
page,
`!!(window.__hf && typeof window.__hf.seek === "function" && window.__hf.duration > 0)`,
pageReadyTimeout,
);
if (!pageReady) {
throw new Error(
`[FrameCapture] window.__hf not ready after ${pageReadyTimeout}ms. Page must expose window.__hf = { duration, seek }.`,
);
}
await applyVideoMetadataHints(page, session.options.videoMetadataHints);
// Wait for all video elements to have decoded their CURRENT frame, not
// just metadata. readyState >= 2 (HAVE_CURRENT_DATA) means a frame is
// actually rasterized and ready to paint — at >= 1 (HAVE_METADATA) we
// only know the dimensions, and the first <video> screenshot can come
// back as a black/blank rectangle. This bites compositions with two
// <video> elements of different codecs (h264 mp4 + VP9 webm) where the
// faster decoder lets the readiness check pass while the slower one
// hasn't painted, producing a black "first frame" for the slower clip.
// skipReadinessVideoIds excludes natively-extracted videos (e.g. HDR HEVC
// sources) whose frames come from ffmpeg out-of-band. videoMetadataHints
// supply intrinsic dimensions for skipped videos whose layout depends on
// aspect ratio, while Chromium may still fail to decode/load metadata.
const skipIdsLiteral = JSON.stringify(session.options.skipReadinessVideoIds ?? []);
const videosReady = await pollPageExpression(
page,
`(() => { const skip = new Set(${skipIdsLiteral}); const vids = Array.from(document.querySelectorAll("video")).filter(v => !skip.has(v.id)); return vids.length === 0 || vids.every(v => v.readyState >= 2); })()`,
pageReadyTimeout,
);
if (!videosReady) {
throw new Error(
`[FrameCapture] video first frame not decoded after ${pageReadyTimeout}ms. Video elements must reach readyState >= 2 (HAVE_CURRENT_DATA) before capture starts.`,
);
}
await page.evaluate(`document.fonts?.ready`);
await waitForOptionalTailwindReady(page, pageReadyTimeout);
// For PNG captures, force the page background fully transparent so the
// captured screenshots carry a real alpha channel. Must run AFTER
// navigation (Chrome resets the override on every goto) and AFTER the
// page is loaded (the injected stylesheet needs a real document.head).
// The override is overridden by `body { background: ... }` and
// `#root { background: ... }` rules — the helper handles that with a
// `[data-composition-id]{background:transparent !important}` injection.
if (session.options.format === "png") {
await initTransparentBackground(session.page);
}
// Warm-up seek: prime the page's render pipeline (WebGL transmission
// buffers, canvas compositing, etc.) before real frame capture begins.
// Without this, parallel workers whose first assigned frame is mid-
// animation can capture an uninitialized render state — typically a
// single flash frame with wrong 3D object orientation or missing
// transmission effects. Two seeks at different times ensure both the
// initial state and mid-animation state are primed.
await page.evaluate(`window.__hf?.seek?.(0)`);
await page.evaluate(`window.__hf?.seek?.(window.__hf?.duration * 0.5 || 0)`);
await page.evaluate(`window.__hf?.seek?.(0)`);
session.isInitialized = true;
return;
}
// In BeginFrame mode, Chrome's event loop is paused until we issue frames.
// Start a warmup loop to drive rAF/setTimeout callbacks during page load.
let warmupRunning = true;
let warmupTicks = 0;
let warmupFrameTime = 0;
const warmupIntervalMs = 33; // ~30fps
let warmupClient: import("puppeteer-core").CDPSession | null = null;
const warmupLoop = async () => {
try {
warmupClient = await getCdpSession(page);
await warmupClient.send("HeadlessExperimental.enable");
} catch {
/* page not ready yet */
}
while (warmupRunning) {
if (warmupClient) {
try {
await warmupClient.send("HeadlessExperimental.beginFrame", {
frameTimeTicks: warmupFrameTime,
interval: warmupIntervalMs,
noDisplayUpdates: true,
});
warmupFrameTime += warmupIntervalMs;
warmupTicks++;
} catch {
/* ignore warmup errors */
}
}
await new Promise((r) => setTimeout(r, warmupIntervalMs));
}
};
warmupLoop().catch(() => {});
await page.goto(url, { waitUntil: "domcontentloaded", timeout: 60000 });
// Poll for window.__hf readiness using manual evaluate loop (waitForFunction
// uses rAF polling internally, which won't fire in beginFrame mode).
const pageReadyTimeout = session.config?.playerReadyTimeout ?? DEFAULT_CONFIG.playerReadyTimeout;
const pollDeadline = Date.now() + pageReadyTimeout;
while (Date.now() < pollDeadline) {
const ready = await page.evaluate(
`!!(window.__hf && typeof window.__hf.seek === "function" && window.__hf.duration > 0)`,
);
if (ready) break;
await new Promise((r) => setTimeout(r, 100));
}
const pageReady = await page.evaluate(
`!!(window.__hf && typeof window.__hf.seek === "function" && window.__hf.duration > 0)`,
);
if (!pageReady) {
warmupRunning = false;
throw new Error(
`[FrameCapture] window.__hf not ready after ${pageReadyTimeout}ms. Page must expose window.__hf = { duration, seek }.`,
);
}
await applyVideoMetadataHints(page, session.options.videoMetadataHints);
// Same readyState contract as the screenshot path above (>= 2 / HAVE_CURRENT_DATA).
const beginframeSkipIdsLiteral = JSON.stringify(session.options.skipReadinessVideoIds ?? []);
const videoDeadline =
Date.now() + (session.config?.playerReadyTimeout ?? DEFAULT_CONFIG.playerReadyTimeout);
while (Date.now() < videoDeadline) {
const videosReady = await page.evaluate(
`(() => { const skip = new Set(${beginframeSkipIdsLiteral}); const vids = Array.from(document.querySelectorAll("video")).filter(v => !skip.has(v.id)); return vids.length === 0 || vids.every(v => v.readyState >= 2); })()`,
);
if (videosReady) break;
await new Promise((r) => setTimeout(r, 100));
}
// Font check (no rAF dependency — uses fonts.ready API directly)
await page.evaluate(`document.fonts?.ready`);
await waitForOptionalTailwindReady(page, pageReadyTimeout);
// Stop warmup
warmupRunning = false;
// Set base frame time ticks past warmup range
session.beginFrameTimeTicks = (warmupTicks + 10) * session.beginFrameIntervalMs;
// For PNG captures, inject the transparent-background override + stylesheet
// (see the screenshot-mode branch above for the rationale). BeginFrame mode
// does not actually preserve alpha through its compositor — callers that
// need transparent output should set `forceScreenshot: true` so this branch
// is bypassed entirely. The call is left here as defense-in-depth for any
// future BeginFrame alpha support.
if (session.options.format === "png") {
await initTransparentBackground(session.page);
}
// Warm-up seek (same rationale as the screenshot-mode branch above).
await page.evaluate(`window.__hf?.seek?.(0)`);
await page.evaluate(`window.__hf?.seek?.(window.__hf?.duration * 0.5 || 0)`);
await page.evaluate(`window.__hf?.seek?.(0)`);
session.isInitialized = true;
}
async function captureFrameErrorDiagnostics(
session: CaptureSession,
frameIndex: number,
time: number,
error: Error,
): Promise<string | null> {
try {
const diagnosticsDir = join(session.outputDir, "diagnostics");
if (!existsSync(diagnosticsDir)) mkdirSync(diagnosticsDir, { recursive: true });
const base = join(diagnosticsDir, `frame-error-${frameIndex}`);
await session.page.screenshot({ path: `${base}.png`, type: "png", fullPage: true });
const html = await session.page.content();
writeFileSync(`${base}.html`, html, "utf-8");
writeFileSync(
`${base}.json`,
JSON.stringify(
{
frameIndex,
time,
error: error.message,
stack: error.stack,
browserConsoleTail: session.browserConsoleBuffer.slice(-30),
},
null,
2,
),
"utf-8",
);
return `${base}.json`;
} catch {
return null;
}
}
/**
* Internal helper: seek timeline and inject video frames.
* Shared by captureFrame (disk) and captureFrameToBuffer (buffer).
* Returns timing breakdown for perf tracking.
*/
async function prepareFrameForCapture(
session: CaptureSession,
frameIndex: number,
time: number,
): Promise<{
quantizedTime: number;
seekMs: number;
beforeCaptureMs: number;
}> {
const { page, options } = session;
if (!session.isInitialized) {
throw new Error("[FrameCapture] Session not initialized");
}
const quantizedTime = quantizeTimeToFrame(time, options.fps);
const seekStart = Date.now();
// Seek via the __hf protocol. The page's seek() implementation handles
// all framework-specific logic (GSAP stepping, CSS animation sync, etc.)
await page.evaluate((t: number) => {
if (window.__hf && typeof window.__hf.seek === "function") {
window.__hf.seek(t);
}
}, quantizedTime);
const seekMs = Date.now() - seekStart;
// Before-capture hook (e.g. video frame injection)
const beforeCaptureStart = Date.now();
if (session.onBeforeCapture) {
await session.onBeforeCapture(page, quantizedTime);
}
const beforeCaptureMs = Date.now() - beforeCaptureStart;
return { quantizedTime, seekMs, beforeCaptureMs };
}
/**
* Internal core: prepare, screenshot, and track perf.
* Shared by captureFrame (disk) and captureFrameToBuffer (buffer).
* Returns the screenshot buffer, quantized time, and total capture time.
*/
async function captureFrameCore(
session: CaptureSession,
frameIndex: number,
time: number,
): Promise<{ buffer: Buffer; quantizedTime: number; captureTimeMs: number }> {
const { page, options } = session;
const startTime = Date.now();
try {
const { quantizedTime, seekMs, beforeCaptureMs } = await prepareFrameForCapture(
session,
frameIndex,
time,
);
const screenshotStart = Date.now();
let screenshotBuffer: Buffer;
if (session.captureMode === "beginframe") {
const frameTimeTicks =
session.beginFrameTimeTicks + frameIndex * session.beginFrameIntervalMs;
const result = await beginFrameCapture(
page,
options,
frameTimeTicks,
session.beginFrameIntervalMs,
);
if (result.hasDamage) session.beginFrameHasDamageCount++;
else session.beginFrameNoDamageCount++;
screenshotBuffer = result.buffer;
} else {
screenshotBuffer = await pageScreenshotCapture(page, options);
}
const screenshotMs = Date.now() - screenshotStart;
const captureTimeMs = Date.now() - startTime;
session.capturePerf.frames += 1;
session.capturePerf.seekMs += seekMs;
session.capturePerf.beforeCaptureMs += beforeCaptureMs;
session.capturePerf.screenshotMs += screenshotMs;
session.capturePerf.totalMs += captureTimeMs;
return { buffer: screenshotBuffer, quantizedTime, captureTimeMs };
} catch (captureError) {
if (session.isInitialized) {
await captureFrameErrorDiagnostics(
session,
frameIndex,
time,
captureError instanceof Error ? captureError : new Error(String(captureError)),
);
}
throw captureError;
}
}
export async function captureFrame(
session: CaptureSession,
frameIndex: number,
time: number,
): Promise<CaptureResult> {
const { options, outputDir } = session;
const { buffer, quantizedTime, captureTimeMs } = await captureFrameCore(
session,
frameIndex,
time,
);
const ext = options.format === "png" ? "png" : "jpg";
const frameName = `frame_${String(frameIndex).padStart(6, "0")}.${ext}`;
const framePath = join(outputDir, frameName);
writeFileSync(framePath, buffer);
return { frameIndex, time: quantizedTime, path: framePath, captureTimeMs };
}
/**
* Capture a frame and return the screenshot as a Buffer instead of writing to disk.
* Used by the streaming encode pipeline to pipe frames directly to FFmpeg stdin.
*/
export async function captureFrameToBuffer(
session: CaptureSession,
frameIndex: number,
time: number,
): Promise<CaptureBufferResult> {
const { buffer, captureTimeMs } = await captureFrameCore(session, frameIndex, time);
return { buffer, captureTimeMs };
}
export async function closeCaptureSession(session: CaptureSession): Promise<void> {
// INVARIANT: closeCaptureSession is idempotent. The renderOrchestrator HDR
// cleanup path tracks a `domSessionClosed` flag and may still re-call this
// in the outer finally if the inner cleanup raised before the flag flipped.
//
// Naive idempotency would be unsafe under pool semantics: releaseBrowser
// decrements pooledBrowserRefCount, so calling it twice for the same
// acquire could close a browser that another session still holds. We make
// it safe by gating each release behind a per-session "released" flag —
// the second call sees the flag already set and skips the release.
//
// We set the flag AFTER (not before) the await so that if a release throws
// midway, the unreleased resource is retried by the outer defensive call.
// Example: page release succeeds, browser release throws → pageReleased=true
// but browserReleased=false → second call no-ops on page and retries browser.
// This matches the orchestrator's intent for HDR cleanup.
if (!session.pageReleased && session.page) {
const pageClosed = await waitForCloseWithTimeout(session.page.close());
if (!pageClosed) {
console.warn("[FrameCapture] Timed out closing page; forcing browser process shutdown");
forceReleaseBrowser(session.browser);
session.browserReleased = true;
}
session.pageReleased = true;
}
if (!session.browserReleased && session.browser) {
const browserClosed = await waitForCloseWithTimeout(
releaseBrowser(session.browser, session.config),
);
if (!browserClosed) {
console.warn("[FrameCapture] Timed out closing browser; forcing browser process shutdown");
forceReleaseBrowser(session.browser);
}
session.browserReleased = true;
}
session.isInitialized = false;
}
export function prepareCaptureSessionForReuse(
session: CaptureSession,
outputDir: string,
onBeforeCapture: BeforeCaptureHook | null,
): void {
if (!existsSync(outputDir)) {
mkdirSync(outputDir, { recursive: true });
}
session.outputDir = outputDir;
session.onBeforeCapture = onBeforeCapture;
session.capturePerf = {
frames: 0,
seekMs: 0,
beforeCaptureMs: 0,
screenshotMs: 0,
totalMs: 0,
};
session.beginFrameHasDamageCount = 0;
session.beginFrameNoDamageCount = 0;
}
export async function getCompositionDuration(session: CaptureSession): Promise<number> {
if (!session.isInitialized) throw new Error("[FrameCapture] Session not initialized");
return session.page.evaluate(() => {
return window.__hf?.duration ?? 0;
});
}
export function getCapturePerfSummary(session: CaptureSession): CapturePerfSummary {
const frames = Math.max(1, session.capturePerf.frames);
return {
frames: session.capturePerf.frames,
avgTotalMs: Math.round(session.capturePerf.totalMs / frames),
avgSeekMs: Math.round(session.capturePerf.seekMs / frames),
avgBeforeCaptureMs: Math.round(session.capturePerf.beforeCaptureMs / frames),
avgScreenshotMs: Math.round(session.capturePerf.screenshotMs / frames),
};
}