fix(core): a far clip the runtime lets go of keeps counting in the film's length (#8480)

* fix(core): a far clip the player lets go of keeps its measured length

* test(studio): a capture page keeps a far clip's length while the runtime lets go of it

* fix(core): a far clip the runtime lets go of stays in Studio's clip list
This commit is contained in:
Miguel Ángel
2026-11-22 10:29:19 +00:00
committed by GitHub
parent 835e660e0e
commit 5ae70826ed
10 changed files with 128 additions and 10 deletions
+3 -2
View File
@@ -10,6 +10,7 @@
* absolute start) when a caller needs them.
*/
import { sourceLengthOf } from "./sourceLength";
import type { RuntimeTimelineLike } from "./types";
import {
parseStrictFiniteTimingNumber,
@@ -74,8 +75,8 @@ function durationFromTimeline(
function durationFromMedia(el: Element): number | null {
if (isMediaElement(el)) {
return Number.isFinite(el.duration)
? resolveNaturalMediaTimelineDuration(el, el.duration)
return Number.isFinite(sourceLengthOf(el))
? resolveNaturalMediaTimelineDuration(el, sourceLengthOf(el))
: null;
}
return resolveTimedImageDurationSeconds(el);
+4 -2
View File
@@ -1,4 +1,5 @@
// fallow-ignore-file code-duplication complexity
import { rememberSourceLength, sourceLengthOf } from "./sourceLength";
import { wallClearTimeout, wallSetTimeout } from "./wallClock";
import { setPreviewRasterScale } from "./previewRasterHints";
import { createFarSceneSkip } from "./farScenes";
@@ -1557,6 +1558,7 @@ export function initSandboxRuntimeModular(): void {
const invalidateOnMediaTimingEvent = (event: Event) => {
const target = event.target;
if (isMediaElement(target)) rememberSourceLength(target);
if (isMediaElement(target) && lengthIsAuthored(target)) return;
invalidateCompositionTimingCaches();
};
@@ -3655,8 +3657,8 @@ export function initSandboxRuntimeModular(): void {
context.inheritedDuration > 0
? Math.max(0, context.inheritedStart + context.inheritedDuration - start)
: null;
const sourceDuration = Number.isFinite(element.duration)
? resolveNaturalMediaTimelineDuration(element, element.duration)
const sourceDuration = Number.isFinite(sourceLengthOf(element))
? resolveNaturalMediaTimelineDuration(element, sourceLengthOf(element))
: null;
// The element's own data-duration is an explicit clip-length trim
// (the studio writes it when you drag the clip edge). It must bound
+3 -1
View File
@@ -1,3 +1,4 @@
import { sourceLengthOf } from "./sourceLength";
import { wallClearTimeout, wallSetTimeout } from "./wallClock";
import { swallow } from "./diagnostics";
import { isClipVisibleAt, isInClipWindow } from "./clipWindow";
@@ -149,7 +150,8 @@ export function refreshRuntimeMediaCache(params?: {
const playbackRate = readElementPlaybackRate(el);
const rate = readElementRateSpec(el);
const loop = el.loop;
const sourceDuration = Number.isFinite(el.duration) && el.duration > 0 ? el.duration : null;
const length = sourceLengthOf(el);
const sourceDuration = Number.isFinite(length) && length > 0 ? length : null;
let duration =
params?.resolveDurationSeconds?.(el) ?? Number.parseFloat(el.dataset.duration ?? "");
if ((!Number.isFinite(duration) || duration < 0) && sourceDuration != null) {
+3 -2
View File
@@ -1,3 +1,4 @@
import { sourceLengthOf } from "./sourceLength";
import {
clampPlaybackRate,
readAuthoredDurationSeconds,
@@ -62,12 +63,12 @@ export function resolveMediaElementDurationSeconds(
const resolved = resolveMediaDuration({
tag: "video", // video and audio resolve identically
authoredDurationSeconds: readDataDurationSeconds((name) => el.getAttribute(name)),
sourceDurationSeconds: Number.isFinite(el.duration) ? el.duration : null,
sourceDurationSeconds: Number.isFinite(sourceLengthOf(el)) ? sourceLengthOf(el) : null,
mediaStartSeconds: readMediaStart(el),
playbackRate: readElementPlaybackRate(el),
});
return resolved.source === "media"
? resolveNaturalMediaTimelineDuration(el, el.duration)
? resolveNaturalMediaTimelineDuration(el, sourceLengthOf(el))
: resolved.seconds;
}
+13
View File
@@ -0,0 +1,13 @@
const measured = new WeakMap<object, { src: string; seconds: number }>();
export function rememberSourceLength(media: HTMLMediaElement): void {
if (Number.isFinite(media.duration) && media.duration > 0)
measured.set(media, { src: media.src, seconds: media.duration });
}
/** The source length: the element's own, else the one measured for the same `src` before a reload emptied it; NaN. */
export function sourceLengthOf(media: { duration: number; src?: string }): number {
if (Number.isFinite(media.duration)) return media.duration;
const kept = measured.get(media);
return kept !== undefined && kept.src === media.src ? kept.seconds : Number.NaN;
}
+2 -1
View File
@@ -1,3 +1,4 @@
import { sourceLengthOf } from "./sourceLength";
import type { RuntimeTimelineLike } from "./types";
import { readTimelineDurationSeconds } from "./timelineDuration";
import { resolveAuthoredTimingWindow } from "./authoredTiming";
@@ -100,7 +101,7 @@ export function createRuntimeStartTimeResolver(params: {
resolveAuthoredDuration(element) ??
positive(
isMediaElement(element)
? resolveNaturalMediaTimelineDuration(element, element.duration)
? resolveNaturalMediaTimelineDuration(element, sourceLengthOf(element))
: null,
) ??
positive(resolveTimedImageDurationSeconds(element)) ??
@@ -6,6 +6,7 @@ import {
runtimeElementWindow,
} from "./timeline";
import { createRuntimeStartTimeResolver } from "./startResolver";
import { rememberSourceLength } from "./sourceLength";
type TimelineTestWindow = Window & {
__timelines?: Record<string, { duration: () => number }>;
@@ -309,6 +310,26 @@ describe("collectRuntimeTimelinePayload", () => {
);
});
// fails without the fix: a far clip the runtime let go of dropped out of the clip list.
it("keeps an untrimmed clip the runtime let go of, at its measured length", () => {
const root = document.createElement("div");
root.setAttribute("data-composition-id", "main");
document.body.appendChild(root);
const video = document.createElement("video");
video.id = "far";
video.src = "far.mp4";
video.setAttribute("data-start", "300");
root.appendChild(video);
Object.defineProperty(video, "duration", { value: 600, writable: true, configurable: true });
rememberSourceLength(video);
video.duration = Number.NaN;
const far = collectRuntimeTimelinePayload(defaultParams).clips.find(
(clip) => clip.id === "far",
);
expect(far?.duration).toBe(600);
});
it("identifies video clips by tag", () => {
const root = document.createElement("div");
root.setAttribute("data-composition-id", "main");
+3 -2
View File
@@ -1,3 +1,4 @@
import { sourceLengthOf } from "./sourceLength";
import type {
RuntimeTimelineClip,
RuntimeTimelineMessage,
@@ -447,8 +448,8 @@ export function collectRuntimeTimelinePayload(params: {
duration = resolveTimelineDurationSeconds(nodeCompositionId);
}
if (duration == null && isMediaElement(node)) {
if (Number.isFinite(node.duration)) {
duration = resolveNaturalMediaTimelineDuration(node, node.duration);
if (Number.isFinite(sourceLengthOf(node))) {
duration = resolveNaturalMediaTimelineDuration(node, sourceLengthOf(node));
}
}
if (duration == null) duration = resolveTimedImageDurationSeconds(node, start);
@@ -0,0 +1,76 @@
// Real Chrome: a capture page's film length while the runtime lets go of a far clip it has already measured.
import { readFileSync } from "node:fs";
import { createRequire } from "node:module";
import type { Browser } from "puppeteer-core";
import { afterAll, beforeAll, expect, it } from "vitest";
import {
STUDIO_PREVIEW_CAPTURE_MODE,
STUDIO_PREVIEW_MARK_META,
} from "@hyperframes/core/studio-preview-mark";
import { closeTestChrome, launchTestChrome } from "../../tests/chromeTestUtils";
const require = createRequire(import.meta.url);
const ORIGIN = "https://fixture.hyperframes.test";
const CLIP = readFileSync(new URL("../../tests/fixtures/clip-600s.webm", import.meta.url));
/** The clip trimmed to 37.43 s at 0, and untrimmed (600 s) at 300 s: the film is 900 s long. */
function filmHtml(): string {
const gsap = readFileSync(require.resolve("gsap/dist/gsap.min.js"), "utf8");
const runtime = readFileSync(require.resolve("@hyperframes/core/runtime"), "utf8");
return `<!doctype html><html><head><meta charset="UTF-8">
<meta name="${STUDIO_PREVIEW_MARK_META}" content="${STUDIO_PREVIEW_CAPTURE_MODE}">
<script>${gsap}</script><script>${runtime}</script></head><body>
<div id="root" data-composition-id="main" data-start="0" data-width="1920" data-height="1080">
<video class="clip" src="${ORIGIN}/clip-600s.webm" data-start="0" data-duration="37.43" data-track-index="0" muted></video>
<video id="far" class="clip" src="${ORIGIN}/clip-600s.webm" data-start="300" data-track-index="1" muted></video>
</div>
<script>window.__timelines = window.__timelines || {}; window.__timelines.main = gsap.timeline({ paused: true });</script>
</body></html>`;
}
let browser: Browser;
beforeAll(async () => {
browser = await launchTestChrome();
});
afterAll(() => closeTestChrome(browser));
// fails without the fix: each time the runtime let go of the far clip, the film fell back to 37.43 s.
it("keeps a far clip's measured length while the runtime lets go of it", async () => {
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on("request", (request) => {
const url = request.url();
if (url === `${ORIGIN}/index.html`)
return void request.respond({ status: 200, contentType: "text/html", body: filmHtml() });
if (url === `${ORIGIN}/clip-600s.webm`)
return void request.respond({ status: 200, contentType: "video/webm", body: CLIP });
void request.abort();
});
await page.goto(`${ORIGIN}/index.html`);
await page.waitForFunction(
() => (window as unknown as { __playerReady?: boolean }).__playerReady === true,
);
// Asks the far clip to load as a length read does, and notes the film's length every 16 ms for 2 s.
const lengths = await page.evaluate(
() =>
new Promise<number[]>((resolve) => {
const far = document.getElementById("far") as HTMLVideoElement;
const player = (window as unknown as { __player: { getDuration(): number } }).__player;
const seen: number[] = [];
const started = performance.now();
(function poll() {
if (far.readyState < 1 && far.networkState !== 2) {
far.preload = "metadata";
far.load();
}
seen.push(player.getDuration());
if (performance.now() - started > 2000) return resolve(seen);
setTimeout(poll, 16);
})();
}),
);
await page.close();
const measured = lengths.indexOf(900);
expect(measured).toBeGreaterThanOrEqual(0);
expect(lengths.slice(measured).filter((seconds) => seconds !== 900)).toEqual([]);
}, 30_000);
Binary file not shown.