Skip to content

Commit d09145f

Browse files
fix(producer): seek once per step when discovering video visibility (#3233)
Seek the GSAP timeline once per timestep and sample every auto-start video, instead of re-walking per media element. Keeps probe cost proportional to duration, not video count.
1 parent a6a9e2f commit d09145f

2 files changed

Lines changed: 96 additions & 10 deletions

File tree

‎packages/producer/src/services/htmlCompiler.test.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
detectThreeDTransformUsage,
1919
discoverMediaFromBrowser,
2020
discoverAudioVolumeAutomationFromTimeline,
21+
discoverVideoVisibilityFromTimeline,
2122
inlineExternalScripts,
2223
localizeRemoteMediaSources,
2324
localizeRemoteImageSources,
@@ -2200,6 +2201,75 @@ describe("resolveCompositionDurations strict literal timing", () => {
22002201
});
22012202
});
22022203

2204+
describe("discoverVideoVisibilityFromTimeline", () => {
2205+
it("returns scene visibility windows for auto-start videos", async () => {
2206+
const duration = 2;
2207+
const sampleStep = 0.1;
2208+
const windows = [
2209+
{ id: "v0", start: 0.2, end: 0.7 },
2210+
{ id: "v1", start: 0.5, end: 1.2 },
2211+
{ id: "v2", start: 1.0, end: 1.8 },
2212+
{ id: "v3", start: 0.0, end: 0.4 },
2213+
];
2214+
2215+
type SceneEl = { opacityAt: (t: number) => number };
2216+
const videos = windows.map((win) => {
2217+
const sceneEl: SceneEl = {
2218+
opacityAt: (t) => (t >= win.start && t <= win.end ? 1 : 0),
2219+
};
2220+
return {
2221+
id: win.id,
2222+
closest: (selector: string) => (selector === ".scene" ? sceneEl : null),
2223+
};
2224+
});
2225+
2226+
let currentTime = 0;
2227+
const previousWindow = globalThis.window;
2228+
const previousDocument = globalThis.document;
2229+
2230+
globalThis.window = {
2231+
__timelines: {
2232+
root: {
2233+
totalTime: (time: number) => {
2234+
currentTime = time;
2235+
},
2236+
},
2237+
},
2238+
getComputedStyle: (el: SceneEl) => ({
2239+
opacity: String(el.opacityAt(currentTime)),
2240+
}),
2241+
} as typeof globalThis.window;
2242+
globalThis.document = {
2243+
querySelectorAll: (selector: string) =>
2244+
selector === "video[data-hf-auto-start]" ? videos : [],
2245+
querySelector: (selector: string) =>
2246+
selector === "[data-composition-id]"
2247+
? { getAttribute: (name: string) => (name === "data-composition-id" ? "root" : null) }
2248+
: null,
2249+
} as typeof globalThis.document;
2250+
2251+
try {
2252+
const page = {
2253+
evaluate: async (fn: (arg: number) => unknown, arg: number) => fn(arg),
2254+
};
2255+
2256+
const result = await discoverVideoVisibilityFromTimeline(page as never, duration);
2257+
2258+
expect(result).toHaveLength(windows.length);
2259+
for (const win of windows) {
2260+
const found = result.find((entry) => entry.videoId === win.id);
2261+
expect(found).toBeDefined();
2262+
expect(found!.visibleStart).toBeGreaterThanOrEqual(win.start - sampleStep);
2263+
expect(found!.visibleStart).toBeLessThanOrEqual(win.start + sampleStep);
2264+
expect(found!.visibleEnd).toBeGreaterThanOrEqual(win.end - sampleStep);
2265+
expect(found!.visibleEnd).toBeLessThanOrEqual(win.end + sampleStep);
2266+
}
2267+
} finally {
2268+
globalThis.window = previousWindow;
2269+
globalThis.document = previousDocument;
2270+
}
2271+
});
2272+
});
22032273
describe("sub-composition variable injection (render path, #2064)", () => {
22042274
function writeSubCompVarProject(hostVars: string): string {
22052275
const projectDir = mkdtempSync(join(tmpdir(), "hf-subvar-"));

‎packages/producer/src/services/htmlCompiler.ts‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,24 +2346,40 @@ export async function discoverVideoVisibilityFromTimeline(
23462346
const SAMPLE_STEP = 0.1;
23472347
const BINARY_PRECISION = 1 / 60;
23482348

2349+
// Seek once per timestep and sample every video — seeking dominates and is
2350+
// independent of which element we read.
2351+
const entries: {
2352+
id: string;
2353+
sceneEl: Element;
2354+
firstVisible: number | null;
2355+
lastVisible: number | null;
2356+
}[] = [];
23492357
for (const videoEl of videos) {
23502358
const id = videoEl.id;
23512359
if (!id) continue;
2360+
entries.push({
2361+
id,
2362+
sceneEl: videoEl.closest(".scene") || videoEl,
2363+
firstVisible: null,
2364+
lastVisible: null,
2365+
});
2366+
}
2367+
if (entries.length === 0) return results;
23522368

2353-
const sceneEl = videoEl.closest(".scene") || videoEl;
2354-
2355-
let firstVisible: number | null = null;
2356-
let lastVisible: number | null = null;
2357-
2358-
for (let t = 0; t <= duration; t += SAMPLE_STEP) {
2359-
seekTl(t);
2360-
const opacity = parseFloat(window.getComputedStyle(sceneEl).opacity);
2369+
for (let t = 0; t <= duration; t += SAMPLE_STEP) {
2370+
seekTl(t);
2371+
for (const entry of entries) {
2372+
const opacity = parseFloat(window.getComputedStyle(entry.sceneEl).opacity);
23612373
if (opacity > 0) {
2362-
if (firstVisible === null) firstVisible = t;
2363-
lastVisible = t;
2374+
if (entry.firstVisible === null) entry.firstVisible = t;
2375+
entry.lastVisible = t;
23642376
}
23652377
}
2378+
}
23662379

2380+
// Per-video boundary refinement (cheap: O(log(step)) seeks each).
2381+
for (const entry of entries) {
2382+
const { id, sceneEl, firstVisible, lastVisible } = entry;
23672383
if (firstVisible === null || lastVisible === null) continue;
23682384

23692385
// Binary search left boundary

0 commit comments

Comments
 (0)