Merge pull request #2816 from heygen-com/fix/2809-static-volume-live-seek

fix(core): avoid live volume probe during render
This commit is contained in:
James Russo
2026-07-26 18:11:12 -07:00
committed by GitHub
11 changed files with 219 additions and 1 deletions
+4
View File
@@ -1834,6 +1834,10 @@ export function initSandboxRuntimeModular(): void {
state.capturedTimeline,
getSafeTimelineDurationSeconds(state.capturedTimeline, 0),
volumeKeyframeCache,
{
allowLiveTimelineSeek: !(window as Window & { __HF_RENDER_CAPTURE_MODE?: boolean })
.__HF_RENDER_CAPTURE_MODE,
},
);
};
@@ -3,6 +3,32 @@ import { describe, expect, it } from "vitest";
import { probeAndCacheElementVolume } from "./mediaVolumeEnvelope";
describe("probeAndCacheElementVolume", () => {
it("does not seek or cache when live timeline probing is disabled", () => {
const audio = document.createElement("audio");
audio.dataset.volume = "1";
document.body.append(audio);
let seekCount = 0;
const timeline = {
totalTime(next?: number) {
if (next !== undefined) {
seekCount += 1;
audio.volume = next >= 1 ? 0 : 1;
}
return 0.75;
},
};
const cache = new WeakMap<HTMLMediaElement, { time: number; volume: number }[]>();
probeAndCacheElementVolume(audio, timeline, 1, cache, {
allowLiveTimelineSeek: false,
});
expect(seekCount).toBe(0);
expect(audio.volume).toBe(1);
expect(cache.has(audio)).toBe(false);
});
it("restores the timeline playhead after sampling volume automation", () => {
const audio = document.createElement("audio");
audio.dataset.volume = "1";
@@ -129,6 +129,18 @@ export function probeElementVolumeKeyframes(
export type RuntimeTimelineRef = Partial<Pick<RuntimeTimelineLike, "totalTime" | "seek">>;
export interface VolumeProbeOptions {
/**
* Render/probe pages must not sample the live visual timeline during runtime
* initialization. The producer discovers audio automation in its own
* isolated pass and bakes it before frame capture, so seeking here is both
* redundant and capable of materializing future zero-duration GSAP state.
*
* Preview callers omit this option and retain live automation discovery.
*/
allowLiveTimelineSeek?: boolean;
}
/**
* Probe a media element and, if volume automation is detected, store the
* keyframes in `cache`. Safe to call with a null timeline — returns early.
@@ -138,7 +150,9 @@ export function probeAndCacheElementVolume(
timeline: RuntimeTimelineRef | null | undefined,
compositionDuration: number,
cache: WeakMap<HTMLMediaElement, VolumeKeyframe[]>,
options: VolumeProbeOptions = {},
): void {
if (options.allowLiveTimelineSeek === false) return;
if (!timeline) return;
if (!(mediaEl instanceof HTMLAudioElement) && !(mediaEl instanceof HTMLVideoElement)) return;
if (compositionDuration <= 0) return;
@@ -5,6 +5,7 @@ import { tmpdir } from "node:os";
import {
closeFileServerSafely,
createFileServer,
RENDER_CAPTURE_MODE_SHIM,
HF_BRIDGE_SCRIPT,
HF_EARLY_STUB,
injectScriptsAtHeadStart,
@@ -315,6 +316,33 @@ describe("parseRangeHeader", () => {
});
describe("createFileServer", () => {
it("marks producer pages as render capture before the runtime loads", async () => {
const projectDir = mkdtempSync(join(tmpdir(), "hf-file-server-render-mode-"));
try {
writeFileSync(
join(projectDir, "index.html"),
"<!doctype html><html><head></head><body></body></html>",
);
const runtimeScript = "globalThis.__hfRuntimeLoaded = true;";
const server = await createFileServer({
projectDir,
preHeadScripts: [],
headScripts: [runtimeScript],
});
try {
const response = await fetch(`${server.url}/index.html`);
expect(response.status).toBe(200);
const html = await response.text();
expect(html).toContain(RENDER_CAPTURE_MODE_SHIM);
expect(html.indexOf(RENDER_CAPTURE_MODE_SHIM)).toBeLessThan(html.indexOf(runtimeScript));
} finally {
server.close();
}
} finally {
rmSync(projectDir, { recursive: true, force: true });
}
});
it("serves ES modules with a JavaScript MIME type", async () => {
const projectDir = mkdtempSync(join(tmpdir(), "hf-file-server-mjs-"));
try {
+13 -1
View File
@@ -677,6 +677,14 @@ export interface FileServerHandle {
addPreHeadScript: (script: string) => void;
}
/**
* Set before the Hyperframes runtime executes so render/probe pages can avoid
* preview-only initialization work that mutates the live visual timeline.
* Audio automation is discovered by the producer in an isolated pass and
* baked before frame capture.
*/
export const RENDER_CAPTURE_MODE_SHIM = "globalThis.__HF_RENDER_CAPTURE_MODE = true;";
/**
* Close a file server handle, swallowing and logging any error.
*
@@ -710,7 +718,11 @@ export function createFileServer(options: FileServerOptions): Promise<FileServer
// bodyScripts later upgrades this stub with `seek` / `duration` once the
// Hyperframe runtime's __player is ready, while preserving any fields
// already written.
const preHeadScripts = [HF_EARLY_STUB, ...(options.preHeadScripts ?? [])];
const preHeadScripts = [
HF_EARLY_STUB,
RENDER_CAPTURE_MODE_SHIM,
...(options.preHeadScripts ?? []),
];
// Default scripts: Hyperframe runtime in <head>, render mode in </body>
const headScripts = options.headScripts ?? [getVerifiedHyperframeRuntimeSource()];
const bodyScripts = options.bodyScripts ?? [buildRenderModeScript(options.fps), HF_BRIDGE_SCRIPT];
@@ -0,0 +1,13 @@
{
"name": "Distributed: static audio must not materialize future nested GSAP state",
"description": "Regression for #2809. A static-volume audio element must not trigger live-timeline envelope probing in a non-zero-start chunk and prematurely apply a nested composition's future terminal visibility set.",
"tags": ["distributed", "audio", "sub-composition", "regression"],
"minPsnr": 40,
"maxFrameFailures": 0,
"minAudioCorrelation": 0,
"maxAudioLagWindows": 120,
"renderConfig": {
"fps": 24,
"chunkSize": 120
}
}
@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:44f07badd4fc8b99f7430ea23d7009263787656ac725d0a70a9c68f5c15c9ab1
size 87166
@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:29abd7d2f00d654654348b10cdef4c9c6f91e52451c1a8f007605bef7ab12017
size 61986
@@ -0,0 +1,56 @@
<template id="child-template">
<div
id="child-root"
data-composition-id="child"
data-start="0"
data-duration="10"
data-width="640"
data-height="360"
>
<div class="future-state">
<strong>MUST BE VISIBLE</strong>
<span>static audio cannot seek visual state</span>
</div>
</div>
<style>
[data-composition-id="child"] {
position: absolute;
inset: 0;
display: grid;
place-items: center;
width: 640px;
height: 360px;
}
[data-composition-id="child"] .future-state {
display: grid;
gap: 14px;
min-width: 420px;
padding: 42px;
border: 8px solid #d8ffe5;
border-radius: 28px;
color: #f4fff7;
background: #087443;
box-shadow: 0 24px 70px rgb(0 0 0 / 45%);
font: 22px/1.3 Arial, sans-serif;
text-align: center;
visibility: visible;
}
[data-composition-id="child"] .future-state strong {
font-size: 38px;
letter-spacing: 0.04em;
}
</style>
<script>
(function () {
window.__timelines = window.__timelines || {};
const timeline = gsap.timeline({ paused: true });
timeline.to(".future-state", { opacity: 0.2, duration: 0.25 }, 9.4);
timeline.set(".future-state", { visibility: "hidden" }, 9.65);
window.__timelines.child = timeline;
})();
</script>
</template>
@@ -0,0 +1,59 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Static volume with future nested set</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/gsap.min.js"></script>
<style>
html,
body {
width: 640px;
height: 360px;
margin: 0;
overflow: hidden;
background: #07111f;
}
#root,
#child-host {
position: absolute;
inset: 0;
width: 640px;
height: 360px;
}
</style>
</head>
<body>
<div
id="root"
data-composition-id="main"
data-start="0"
data-duration="12"
data-width="640"
data-height="360"
>
<div
id="child-host"
data-composition-id="child"
data-composition-src="compositions/child.html"
data-start="2"
data-duration="10"
data-track-index="0"
></div>
<audio
id="bed"
src="assets/silence.wav"
data-start="0"
data-duration="12"
data-track-index="1"
data-volume="1"
></audio>
</div>
<script>
window.__timelines = window.__timelines || {};
const timeline = gsap.timeline({ paused: true });
timeline.to({}, { duration: 12 }, 0);
window.__timelines.main = timeline;
</script>
</body>
</html>