fix(core): a looped sound plays its whole slot in preview (#8751)

* fix(core): a looped sound plays its whole slot in preview

A looped audio clip in a slot longer than its source stopped after one
pass in preview: its window was capped at the source length, so media
sync paused it there. It now fills its authored slot, as the render does.
A playing loop is also no longer seeked a lap back at each wrap, which
left Safari holding it at the start for seconds.

* fix(core): a trimmed loop is seeked back to its trim-in at each wrap

A native loop restarts at the file's start, not at the clip's trim-in, so laps are folded out of the offset only for an untrimmed loop. A trimmed one is seeked back as before.
This commit is contained in:
Miguel Ángel
2026-11-24 20:03:31 +00:00
committed by GitHub
parent 179ebc1011
commit 7b78460d05
5 changed files with 149 additions and 6 deletions
+38
View File
@@ -3500,6 +3500,44 @@ describe("initSandboxRuntimeModular", () => {
expect(video.currentTime).toBe(0);
});
describe("a looped sound in a slot longer than its source", () => {
function audioIn(loop: boolean): HTMLAudioElement {
const root = document.createElement("div");
root.setAttribute("data-composition-id", "main");
root.setAttribute("data-root", "true");
root.setAttribute("data-width", "1920");
root.setAttribute("data-height", "1080");
document.body.appendChild(root);
const audio = document.createElement("audio");
audio.setAttribute("data-start", "2");
audio.setAttribute("data-duration", "18");
audio.loop = loop;
root.appendChild(audio);
Object.defineProperty(audio, "duration", { value: 4, writable: true, configurable: true });
Object.defineProperty(audio, "paused", { value: true, writable: true, configurable: true });
Object.defineProperty(audio, "readyState", { value: 4, writable: true, configurable: true });
Object.defineProperty(audio, "currentTime", { value: 0, writable: true, configurable: true });
audio.load = () => {};
audio.pause = () => {};
window.__timelines = { main: createMockTimeline(20) };
initSandboxRuntimeModular();
return audio;
}
// fails without the fix: the clip ended at its 4 s source, so preview paused it after one pass.
it("is placed on its second pass at 7 s", () => {
const audio = audioIn(true);
window.__player?.seek(7);
expect(audio.currentTime).toBeCloseTo(1, 3);
});
it("still ends with its source when it does not loop", () => {
const audio = audioIn(false);
window.__player?.seek(7);
expect(audio.currentTime).toBe(0);
});
});
it("seeks sub-composition timelines at data-start near 0 during renderSeek", () => {
// Regression: sub-compositions starting at or near t=0 kept their initial CSS state
// in render. Each scene now gets its own seek, so the paused root need not reach it.
+1
View File
@@ -3744,6 +3744,7 @@ export function initSandboxRuntimeModular(): void {
const explicitDuration = ownDuration != null && ownDuration > 0 ? ownDuration : null;
return resolveRuntimeMediaClipDuration({
isVideo: element.tagName === "VIDEO",
loops: element.loop,
sourceDuration,
hostRemaining,
explicitDuration,
+89
View File
@@ -265,6 +265,7 @@ describe("resolveRuntimeMediaClipDuration", () => {
expect(
resolveRuntimeMediaClipDuration({
isVideo: true,
loops: false,
sourceDuration: 1,
hostRemaining: 8,
explicitDuration: 5,
@@ -278,6 +279,7 @@ describe("resolveRuntimeMediaClipDuration", () => {
expect(
resolveRuntimeMediaClipDuration({
isVideo: true,
loops: true,
sourceDuration: 1,
hostRemaining: null,
explicitDuration: 10,
@@ -285,10 +287,37 @@ describe("resolveRuntimeMediaClipDuration", () => {
).toBe(10);
});
// fails without the fix: a looped 4 s sound in an 18 s slot ended at 4 s, so preview paused it after one pass.
it("fills an explicit slot for looping audio, pass after pass, as the render does", () => {
expect(
resolveRuntimeMediaClipDuration({
isVideo: false,
loops: true,
sourceDuration: 4,
hostRemaining: null,
explicitDuration: 18,
}),
).toBe(18);
});
it("keeps looping audio inside its host window and to one pass without a slot", () => {
const loopingAudio = (hostRemaining: number | null, explicitDuration: number | null) =>
resolveRuntimeMediaClipDuration({
isVideo: false,
loops: true,
sourceDuration: 4,
hostRemaining,
explicitDuration,
});
expect(loopingAudio(10, 18)).toBe(10);
expect(loopingAudio(null, null)).toBe(4);
});
it("keeps audio bounded by its playable source", () => {
expect(
resolveRuntimeMediaClipDuration({
isVideo: false,
loops: false,
sourceDuration: 1,
hostRemaining: 8,
explicitDuration: 5,
@@ -300,6 +329,7 @@ describe("resolveRuntimeMediaClipDuration", () => {
expect(
resolveRuntimeMediaClipDuration({
isVideo: true,
loops: false,
sourceDuration: 1,
hostRemaining: 8,
explicitDuration: null,
@@ -311,6 +341,7 @@ describe("resolveRuntimeMediaClipDuration", () => {
expect(
resolveRuntimeMediaClipDuration({
isVideo: true,
loops: false,
sourceDuration: 1,
hostRemaining: null,
explicitDuration: null,
@@ -322,6 +353,7 @@ describe("resolveRuntimeMediaClipDuration", () => {
expect(
resolveRuntimeMediaClipDuration({
isVideo: true,
loops: false,
sourceDuration: 0,
hostRemaining: 8,
explicitDuration: null,
@@ -2005,6 +2037,63 @@ describe("syncRuntimeMedia", () => {
expect(clip.el.currentTime).toBe(mediaElementTime(1));
});
// fails without the fix: the lap read as 4 s of drift, so the playing sound was seeked back to 0 just as it
// looped itself, and Safari then held it at 0 for seconds.
it("lets a playing looped sound wrap on its own instead of seeking it a lap back", () => {
const clip = createMockClip(
{ start: 2, end: 20, mediaStart: 0, loop: true, sourceDuration: 4 },
"audio",
);
Object.defineProperty(clip.el, "readyState", { value: 4, writable: true });
Object.defineProperty(clip.el, "paused", { value: false, writable: true, configurable: true });
let elementTime = 0;
const seeks: number[] = [];
Object.defineProperty(clip.el, "currentTime", {
configurable: true,
get: () => elementTime,
set: (value: number) => {
seeks.push(value);
elementTime = value;
},
});
for (let time = 5; time < 5.99; time += 0.016) {
elementTime = time - 2;
syncRuntimeMedia({ clips: [clip], timeSeconds: time, playing: true, playbackRate: 1 });
}
seeks.length = 0;
elementTime = 3.99; // still at its end; the playhead has wrapped to 0.02
syncRuntimeMedia({ clips: [clip], timeSeconds: 6.02, playing: true, playbackRate: 1 });
expect(seeks).toEqual([]);
});
it("seeks a trimmed looped sound back to its trim-in when the element wraps to the file's start", () => {
const clip = createMockClip(
{ start: 0, end: 20, mediaStart: 2, loop: true, sourceDuration: 4 },
"audio",
);
Object.defineProperty(clip.el, "readyState", { value: 4, writable: true });
Object.defineProperty(clip.el, "paused", { value: false, writable: true, configurable: true });
let elementTime = 0;
const seeks: number[] = [];
Object.defineProperty(clip.el, "currentTime", {
configurable: true,
get: () => elementTime,
set: (value: number) => {
seeks.push(value);
elementTime = value;
},
});
for (let time = 1; time < 1.99; time += 0.016) {
elementTime = time + 2;
syncRuntimeMedia({ clips: [clip], timeSeconds: time, playing: true, playbackRate: 1 });
}
seeks.length = 0;
elementTime = 0.02; // a native loop restarts at 0, inside the trimmed head
syncRuntimeMedia({ clips: [clip], timeSeconds: 2.02, playing: true, playbackRate: 1 });
expect(seeks).toHaveLength(1);
expect(seeks[0]).toBeCloseTo(2.02, 3);
});
it("wraps loop with mediaStart offset", () => {
// Source is 10s, mediaStart=5, so loop length is 5s (5-10)
const clip = createMockClip({
+20 -6
View File
@@ -90,20 +90,22 @@ function holdSeekBarrierUntilVideoLands(el: HTMLMediaElement): void {
* Resolve a media element's timeline window without conflating a video's
* authored display slot with the amount of source left to decode.
*
* An explicit video slot may outlive its source and holds the final frame.
* Audio remains source-bounded because it has no visual hold state.
* An explicit video slot may outlive its source and hold the final frame; a loop fills its slot.
* Other audio remains source-bounded because it has no visual hold state.
*/
export function resolveRuntimeMediaClipDuration(params: {
isVideo: boolean;
loops: boolean;
sourceDuration: number | null;
hostRemaining: number | null;
explicitDuration: number | null;
}): number | null {
const mediaDuration = params.isVideo
const fillsSlot = params.isVideo || params.loops;
const mediaDuration = fillsSlot
? (params.explicitDuration ?? params.sourceDuration)
: params.sourceDuration;
const candidates = (
params.isVideo
fillsSlot
? [mediaDuration, params.hostRemaining]
: [mediaDuration, params.hostRemaining, params.explicitDuration]
).filter((value): value is number => value != null && Number.isFinite(value) && value >= 0);
@@ -730,8 +732,20 @@ export function syncRuntimeMedia(params: {
const STRICT_REQUIRED_SAMPLES = 2;
const currentElTime = el.currentTime || 0;
const drift = Math.abs(currentElTime - relTime);
const offset = relTime - currentElTime;
const nativeLoopLap =
clip.loop &&
clip.mediaStart === 0 &&
clip.sourceDuration != null &&
params.playing &&
!el.paused
? clip.sourceDuration
: 0;
const offsetWithLaps = relTime - currentElTime;
const offset =
nativeLoopLap > 0
? offsetWithLaps - Math.round(offsetWithLaps / nativeLoopLap) * nativeLoopLap
: offsetWithLaps;
const drift = Math.abs(offset);
const prevOffset = lastOffset.get(el);
lastOffset.set(el, offset);
const firstTickOfClip = prevOffset === undefined;
@@ -361,6 +361,7 @@ describe("resolveVideoExtractionDuration", () => {
const planned = { ...parsed, start };
const runtimeDuration = resolveRuntimeMediaClipDuration({
isVideo: true,
loops: loop,
sourceDuration: 3,
hostRemaining: 15 - start,
explicitDuration: null,