Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions packages/producer/src/services/htmlCompiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3025,6 +3025,57 @@ describe("sub-composition variable injection (render path, #2064)", () => {
});
});

describe("variable-bound media src (render path)", () => {
function compileBoundClip(opts: { defaultSrc: string; hostVars?: string }) {
const projectDir = mkdtempSync(join(tmpdir(), "hf-var-src-"));
mkdirSync(join(projectDir, "compositions"), { recursive: true });
writeFileSync(
join(projectDir, "compositions", "clip.html"),
`<!DOCTYPE html>
<html data-composition-variables='[{"id":"clip","type":"string","label":"Clip","default":"${opts.defaultSrc}"}]'>
<body>
<div data-composition-id="clip" data-width="320" data-height="240">
<video id="v" src="a.mp4" data-var-src="clip" data-start="0" data-duration="3" muted></video>
</div>
</body>
</html>`,
);
writeFileSync(
join(projectDir, "index.html"),
`<!DOCTYPE html>
<html>
<body>
<div id="root" data-composition-id="host" data-start="0" data-duration="3" data-width="320" data-height="240">
<div data-composition-id="clip-1" data-composition-src="compositions/clip.html" data-start="0" data-duration="3" data-track-index="1" ${opts.hostVars ?? ""}></div>
</div>
</body>
</html>`,
);
return compileForRender(projectDir, join(projectDir, "index.html"), projectDir);
}

it("marks a clip whose host value differs from its static src", async () => {
const compiled = await compileBoundClip({
defaultSrc: "a.mp4",
hostVars: `data-variable-values='{"clip":"b.mp4"}'`,
});
expect(compiled.html).toContain("data-hf-var-src-changed");
});

it("marks a clip whose declared default differs from its static src", async () => {
const compiled = await compileBoundClip({ defaultSrc: "b.mp4" });
expect(compiled.html).toContain("data-hf-var-src-changed");
});

it("leaves a clip whose resolved value is its static src", async () => {
const compiled = await compileBoundClip({
defaultSrc: "b.mp4",
hostVars: `data-variable-values='{"clip":"a.mp4"}'`,
});
expect(compiled.html).not.toContain("data-hf-var-src-changed");
});
});

// ── Markup payload sniff (STUDIO-5433) ─────────────────────────────────────
//
// Producer's `resolveMediaDuration` is a two-step pipeline (download → probe)
Expand Down
44 changes: 43 additions & 1 deletion packages/producer/src/services/htmlCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,46 @@ function coalesceHeadStylesAndBodyScripts(html: string): string {
return document.toString();
}

/** Stamped on bound media whose variable gives a src other than its static one. */
export const VARIABLE_SRC_CHANGED_ATTR = "data-hf-var-src-changed";

/**
* Stamp bound audio and video that any declared default or host
* data-variable-values in their scope chain would give a src other than the
* static one media extraction reads. Runs before remote sources are localized,
* while both still read as authored.
*/
function markChangedVariableMediaSrc(
document: Document,
variablesByComp: Record<string, Record<string, unknown>>,
): boolean {
const topLevel: Record<string, unknown> = {};
for (const declarer of document.querySelectorAll("[data-composition-variables]")) {
Object.assign(topLevel, readDeclaredDefaults(declarer));
}
let marked = false;
for (const el of document.querySelectorAll(
"audio[data-var-src], video[data-var-src], source[data-var-src]",
)) {
const id = el.getAttribute("data-var-src")!.trim();
const values = [topLevel[id]];
for (let scope = el.closest("[data-composition-id]"); scope; ) {
values.push(variablesByComp[scope.getAttribute("data-composition-id")!.trim()]?.[id]);
scope = scope.parentElement?.closest("[data-composition-id]") ?? null;
}
const changed = values.some((value) => {
const url =
value !== null && typeof value === "object" ? (value as { url?: unknown }).url : value;
return typeof url === "string" && url !== "" && url !== el.getAttribute("src");
});
if (changed) {
el.setAttribute(VARIABLE_SRC_CHANGED_ATTR, "");
marked = true;
}
}
return marked;
}

/**
* Inline sub-composition HTML into the main document using the shared
* inlining logic from @hyperframes/core. This wrapper handles the
Expand Down Expand Up @@ -1023,7 +1063,8 @@ function inlineSubCompositions(
{},
variableOverrides,
);
return emitted ? document.toString() : html;
const marked = markChangedVariableMediaSrc(document as unknown as Document, {});
return emitted || marked ? document.toString() : html;
}

// Assign per-instance runtime composition ids before each host is inlined,
Expand Down Expand Up @@ -1181,6 +1222,7 @@ function inlineSubCompositions(
// document-unique key here, while the merged document is in hand and before
// anything downstream keys media on an id. See core's mediaRenderIds.ts.
assignMediaRenderIds(document as unknown as Document);
markChangedVariableMediaSrc(document as unknown as Document, result.variablesByComp);

return document.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,14 @@ describe("hasVariableBoundMedia", () => {
expect(hasVariableBoundMedia(audio, { title: "Row 02" })).toBe(false);
expect(hasVariableBoundMedia(image, { hero_src: "row-02.png" })).toBe(true);
});

it("probes media the compiler marked as bound to a different src", () => {
const marked = `<video src="a.mp4" data-var-src="clip" data-hf-var-src-changed></video>`;
const unmarked = `<video src="a.mp4" data-var-src="clip"></video>`;

expect(hasVariableBoundMedia(marked, undefined)).toBe(true);
expect(hasVariableBoundMedia(unmarked, undefined)).toBe(false);
});
});

describe("runProbeStage — forceScreenshot threading", () => {
Expand Down
8 changes: 6 additions & 2 deletions packages/producer/src/services/render/stages/probeStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
discoverVideoVisibilityFromTimeline,
recompileWithResolutions,
resolveCompositionDurations,
VARIABLE_SRC_CHANGED_ATTR,
} from "../../htmlCompiler.js";
import {
closeFileServerSafely,
Expand Down Expand Up @@ -172,14 +173,17 @@ export function hasAutoStartVideos(html: string): boolean {
/**
* Variable-bound image/audio/video sources are resolved by the browser runtime, not
* the static compiler. Probe them whenever the current render overrides the
* referenced variable so media extraction follows the resolved row value.
* referenced variable so media extraction follows the resolved row value, or
* when the compiler marked bound audio/video whose declared default or host
* data-variable-values differ from the static src.
*/
export function hasVariableBoundMedia(
html: string,
variables: Record<string, unknown> | undefined,
): boolean {
if (!variables || Object.keys(variables).length === 0) return false;
const { document } = parseHTML(html);
if (document.querySelector(`[${VARIABLE_SRC_CHANGED_ATTR}]`)) return true;
if (!variables || Object.keys(variables).length === 0) return false;
return Array.from(
document.querySelectorAll(
"img[data-var-src], audio[data-var-src], video[data-var-src], source[data-var-src]",
Expand Down
Loading