Skip to content

Commit 79144f9

Browse files
fix(core): a film stops at its declared length instead of playing its animation past it (#4530)
* fix(core): a film stops at its declared length instead of playing its animation past it * docs(core): the root-length floor comment names what it still does * fix(core): report the real length of a film shorter than one second * test(core): pin sub-second lengths with and without timed clips * fix(core): any positive declared length stops the film, even a single frame * test(core): pin every declared length in one table and read the player test by its fields
1 parent 7c017a0 commit 79144f9

6 files changed

Lines changed: 90 additions & 13 deletions

File tree

‎packages/core/src/runtime/init.test.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -842,12 +842,13 @@ describe("initSandboxRuntimeModular", () => {
842842
"visible",
843843
]);
844844

845+
// 79.4 s is now the film's end, which a clip running to the end rests on (the render stops before it).
845846
setDuration(79.4);
846847
window.__player?.renderSeek(finalSample);
847848
expect([root, ctaHost, cta].map((element) => element.style.visibility)).toEqual([
848-
"hidden",
849-
"hidden",
850-
"hidden",
849+
"visible",
850+
"visible",
851+
"visible",
851852
]);
852853

853854
setDuration(79.41666666666667);
@@ -905,12 +906,14 @@ describe("initSandboxRuntimeModular", () => {
905906
// prepareFrameForCapture -> window.__hf.seek path before reading pixels.
906907
// Lock the visibility state at that common pre-capture boundary; the
907908
// unavailable private project is still required for buffer/encode proof.
908-
for (const { host, child, tailFrame } of seams) {
909+
for (const [index, { host, child, tailFrame }] of seams.entries()) {
909910
window.__player?.renderSeek(tailFrame / fps);
910911
expect([host.style.visibility, child.style.visibility]).toEqual(["visible", "visible"]);
911912

913+
// Past the last seam is past the film's end, where its final clip rests.
914+
const after = index === seams.length - 1 ? "visible" : "hidden";
912915
window.__player?.renderSeek((tailFrame + 1) / fps);
913-
expect([host.style.visibility, child.style.visibility]).toEqual(["hidden", "hidden"]);
916+
expect([host.style.visibility, child.style.visibility]).toEqual([after, after]);
914917
}
915918
});
916919

@@ -1347,7 +1350,7 @@ describe("initSandboxRuntimeModular", () => {
13471350
expect(window.__player?.getDuration()).toBe(250.5);
13481351
});
13491352

1350-
it("keeps the timeline duration when it exceeds the root's declared data-duration", () => {
1353+
it("cuts a timeline that runs past the root's declared data-duration to the declared length", () => {
13511354
const root = document.createElement("div");
13521355
root.setAttribute("data-composition-id", "main");
13531356
root.setAttribute("data-root", "true");
@@ -1363,7 +1366,7 @@ describe("initSandboxRuntimeModular", () => {
13631366

13641367
initSandboxRuntimeModular();
13651368

1366-
expect(window.__player?.getDuration()).toBe(12);
1369+
expect(window.__player?.getDuration()).toBe(10);
13671370
});
13681371

13691372
// #6: a single timeline registered under a key that does NOT match the root's

‎packages/core/src/runtime/init.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -876,11 +876,9 @@ export function initSandboxRuntimeModular(): void {
876876
timelineRegistry: timelines,
877877
includeAuthoredTimingAttrs: true,
878878
});
879-
// The root's own data-duration is the authored source of truth for
880-
// composition length. Without it in the floor, a GSAP timeline that ends
881-
// even slightly short of the declared duration shrinks the playable
882-
// window — and duration-gated consumers (e.g. the studio's adapter
883-
// selection) silently reject the runtime player, losing audio playback.
879+
// getSafeTimelineDurationSeconds returns the declared length first; here it only sizes the
880+
// stand-in timeline that resolveRootTimelineFromDocument builds for a root timeline with no
881+
// length.
884882
const rootDeclaredSeconds = parseStrictFiniteTimingNumber(rootEl.getAttribute("data-duration"));
885883
const subCompositionEnds: number[] = [];
886884
const compositionNodes = Array.from(
@@ -1223,6 +1221,16 @@ export function initSandboxRuntimeModular(): void {
12231221
fallback = 0,
12241222
timingRevision?: number,
12251223
): number => {
1224+
// The root's declared length is the film's length, as in the render: a longer timeline is cut
1225+
// off.
1226+
const declaredDuration = parseStrictFiniteTimingNumber(
1227+
resolveRootCompositionElement()?.getAttribute("data-duration"),
1228+
);
1229+
// Any positive length counts, even one frame: the render accepts it too.
1230+
if (declaredDuration !== null && declaredDuration > 0) {
1231+
if (window.__hf?.durationSource) delete window.__hf.durationSource;
1232+
return declaredDuration;
1233+
}
12261234
const timelineDuration = getTimelineDurationSeconds(timeline);
12271235
const { media: mediaFloor, authoredComposition: authoredCompositionFloor } =
12281236
resolveDurationFloors(timingRevision);

‎packages/core/src/runtime/timeline.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,22 @@ describe("collectRuntimeTimelinePayload", () => {
520520
expect(result.durationInFrames).toBe(210);
521521
});
522522

523+
it("reports the real length of a film shorter than one second", () => {
524+
document.body.innerHTML = `<div data-composition-id="main" data-duration="0.2"><div id="clip" data-start="0" data-duration="0.2"></div></div>`;
525+
(window as TimelineTestWindow).__timelines = { main: { duration: () => 0.2 } };
526+
527+
const result = collectRuntimeTimelinePayload(defaultParams);
528+
expect(result.durationSeconds).toBe(0.2);
529+
expect(result.durationInFrames).toBe(6);
530+
});
531+
532+
it("reports the real length of a sub-second root with no timed clips", () => {
533+
document.body.innerHTML = `<div data-composition-id="main" data-duration="0.2"></div>`;
534+
535+
const result = collectRuntimeTimelinePayload(defaultParams);
536+
expect(result.durationSeconds).toBe(0.2);
537+
});
538+
523539
it("defaults composition dimensions to 1920x1080", () => {
524540
const result = collectRuntimeTimelinePayload(defaultParams);
525541
expect(result.compositionWidth).toBe(1920);

‎packages/core/src/runtime/timeline.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,8 @@ export function collectRuntimeTimelinePayload(params: {
695695
// hide structural/background tracks from the timeline UI; if we collapse the
696696
// payload duration down to the last visible clip end, the controls jump even
697697
// though playback still runs for the full authored root duration.
698-
const safeDuration = Math.max(1, maxEnd || 1, rootCompositionDuration ?? 0);
698+
const knownDuration = Math.max(maxEnd || 0, rootCompositionDuration ?? 0);
699+
const safeDuration = knownDuration > 0 ? knownDuration : 1;
699700
const durationInFrames = Math.max(1, Math.ceil(safeDuration * Math.max(1, params.canonicalFps)));
700701
return {
701702
...runtimeProtocolMetadata(params.canonicalFps),

‎packages/core/src/runtime/transportPark.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,33 @@ describe("parked transport loop", () => {
200200
expect(states.at(-1)).toMatchObject({ isPlaying: false, ended: true, frame: 149 });
201201
});
202202

203+
// The render stops at the root's declared length too; a longer animation is cut off.
204+
it.each([
205+
["3 s, shorter than its animation", "3", 90],
206+
["under a second", "0.2", 6],
207+
["one 60 fps frame", String(1 / 60), 1],
208+
])("stops a film declared %s at that length, and says so", (_label, declared, frame) => {
209+
const seconds = Number(declared);
210+
let nowMs = 1000;
211+
vi.spyOn(performance, "now").mockImplementation(() => nowMs);
212+
document.body.innerHTML = `<div id="root" data-composition-id="main" data-root="true" data-start="0" data-duration="${declared}"></div>`;
213+
window.__timelines = { main: createMockTimeline(5) };
214+
initSandboxRuntimeModular();
215+
quiesce();
216+
217+
window.__player!.play();
218+
for (let step = 0; step < 400 && window.__player!.isPlaying(); step += 1) {
219+
nowMs += 10;
220+
raf.step(10);
221+
}
222+
223+
const states = posted.filter((m) => m["type"] === "state");
224+
expect(states.at(-1)).toMatchObject({ isPlaying: false, ended: true, frame });
225+
expect(window.__player!.getDuration()).toBeCloseTo(seconds, 9);
226+
const timeline = posted.filter((m) => m["type"] === "timeline").at(-1);
227+
expect(timeline?.["durationSeconds"]).toBeCloseTo(seconds, 9);
228+
});
229+
203230
it("keeps asking for animation frames while playing", () => {
204231
mount();
205232
initSandboxRuntimeModular();
@@ -330,6 +357,8 @@ describe("parked transport loop", () => {
330357

331358
it("delivers media metadata that arrives after the loop parked", async () => {
332359
mount(`<video id="v" data-start="0" src="a.mp4"></video>`);
360+
// A declared length would hold the film at 5 s; only an inferred one can grow.
361+
document.getElementById("root")!.removeAttribute("data-duration");
333362
initSandboxRuntimeModular();
334363
quiesce();
335364

@@ -513,6 +542,7 @@ describe("parked transport loop", () => {
513542

514543
it("delivers an adapter duration that grows while parked, with no DOM mutation and no event", () => {
515544
mount();
545+
document.getElementById("root")!.removeAttribute("data-duration");
516546
initSandboxRuntimeModular();
517547
quiesce();
518548
const before = window.__player!.getDuration();

‎packages/player/src/hyperframes-player.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,25 @@ describe("HyperframesPlayer loop end-state handling", () => {
14641464
expect(player._currentTime).toBe(player._duration);
14651465
});
14661466

1467+
it("takes a length under a second from the runtime and ends there", () => {
1468+
const ended = vi.fn();
1469+
player.addEventListener("ended", ended);
1470+
player.loop = false;
1471+
player._onMessage(
1472+
new MessageEvent("message", {
1473+
source: frameWindow,
1474+
data: { source: "hf-preview", type: "timeline", durationSeconds: 0.2, durationInFrames: 6 },
1475+
}),
1476+
);
1477+
expect(player._duration).toBe(0.2);
1478+
player._paused = false;
1479+
1480+
postState(6, false, 0.2, true);
1481+
1482+
expect(ended).toHaveBeenCalledTimes(1);
1483+
expect(player._currentTime).toBe(0.2);
1484+
});
1485+
14671486
it("loops a film whose length falls between two frames", () => {
14681487
const seek = vi.spyOn(player, "seek");
14691488
player.loop = true;

0 commit comments

Comments
 (0)