Skip to content

Commit 7d958fe

Browse files
fix(core): slow clips stay on the playhead at reduced preview speeds (#5440)
* fix(core): slow clips stay on the playhead at reduced preview speeds * fix(core): keep a slow clip in step on low frame rate previews
1 parent 81f60db commit 7d958fe

2 files changed

Lines changed: 54 additions & 7 deletions

File tree

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,44 @@ describe("syncRuntimeMedia", () => {
19811981
});
19821982
});
19831983

1984+
it.each([
1985+
[60, 0.25],
1986+
[10, 0.5],
1987+
])(
1988+
"keeps a video slower than the browser can play on the playhead at %i fps, preview speed %f",
1989+
(fps, speed) => {
1990+
// A 0.1x clip at 0.25x or 0.5x preview speed is below Chromium's 1/16 rate floor.
1991+
const clip = createMockClip({ start: 0, end: 30, duration: 30, playbackRate: 0.1 });
1992+
const el = clip.el;
1993+
let rate = speed;
1994+
let paused = true;
1995+
Object.defineProperty(el, "playbackRate", {
1996+
configurable: true,
1997+
get: () => rate,
1998+
set: (v: number) => {
1999+
if (v < 0.0625) throw new DOMException("unsupported rate", "NotSupportedError");
2000+
rate = v;
2001+
},
2002+
});
2003+
Object.defineProperty(el, "paused", { configurable: true, get: () => paused });
2004+
el.play = vi.fn(() => ((paused = false), Promise.resolve()));
2005+
el.pause = vi.fn(() => void (paused = true));
2006+
const dt = 1 / fps;
2007+
let timeline = 0;
2008+
for (let i = 0; i < fps * 4; i++) {
2009+
timeline += speed * dt;
2010+
if (!paused) el.currentTime += rate * dt;
2011+
syncRuntimeMedia({
2012+
clips: [clip],
2013+
timeSeconds: timeline,
2014+
playing: true,
2015+
playbackRate: speed,
2016+
});
2017+
}
2018+
expect(Math.abs(el.currentTime - 0.1 * timeline)).toBeLessThan(0.05);
2019+
},
2020+
);
2021+
19842022
// A seek while playing pauses and syncs in one pass, before the video element has paused.
19852023
it("a seek that pauses mid-playback lands a lagging playing video on the new time", () => {
19862024
const clip = createMockClip({ start: 3.85, end: 5.6, duration: 1.75 });

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ export const MEDIA_HARD_SYNC_SECONDS = 0.5;
256256
/** Drift a playing audio element may carry before sync pulls it back onto the playhead. */
257257
export const MEDIA_SYNC_TOLERANCE_SECONDS = 0.04;
258258

259+
// Chromium refuses a slower playbackRate (NotSupportedError).
260+
const MIN_NATIVE_PLAYBACK_RATE = 1 / 16;
261+
259262
// A playing video is steered back by rate, not seeked (a seek resets its decoder).
260263
// Its rate is written only when steering starts or stops: every write costs a frame.
261264
const VIDEO_STEER = 0.03;
@@ -473,6 +476,8 @@ export function syncRuntimeMedia(params: {
473476
if (el.preload !== "auto") el.preload = "auto";
474477
// Per-element rate × global transport rate
475478
const baseRate = rateAt(clipRate, params.timeSeconds - clip.start) * params.playbackRate;
479+
// Too slow for the browser to play: kept paused and stepped onto the playhead by seeks.
480+
const playing = params.playing && baseRate >= MIN_NATIVE_PLAYBACK_RATE;
476481
// Drift correction — three tiers:
477482
//
478483
// 1. Hard sync (0.5s): first tick, timeline jumps (scrub), catastrophic
@@ -518,12 +523,14 @@ export function syncRuntimeMedia(params: {
518523
// seek→freeze→drift→seek stutter. So a playing video skips strict and force sync; only hard
519524
// sync (>0.5s) warrants the decoder-reset cost. A paused transport pauses this video below,
520525
// so a seek that pauses mid-playback still lands it.
521-
const isPlayingVideo = el.tagName === "VIDEO" && !el.paused && params.playing;
526+
const isPlayingVideo = el.tagName === "VIDEO" && !el.paused && playing;
522527
// Only apply strict sync when offset has stabilized (not growing).
523528
// During initial buffering, offset grows ~16ms/tick as the timeline
524529
// advances while media stays at 0. Accumulated drift from pause/play
525530
// toggling shows up as a stable, non-zero offset (delta near 0).
526-
const offsetStabilized = prevOffset !== undefined && Math.abs(offset - prevOffset) < 0.004;
531+
const heldBelowFloor = params.playing && !playing;
532+
const offsetStabilized =
533+
heldBelowFloor || (prevOffset !== undefined && Math.abs(offset - prevOffset) < 0.004);
527534
let strictSync = false;
528535
if (
529536
!isPlayingVideo &&
@@ -545,8 +552,10 @@ export function syncRuntimeMedia(params: {
545552
try {
546553
// A hard sync lands the video on the playhead, so its pre-seek offset says nothing.
547554
if (!isPlayingVideo || hardSync) videoSteering.delete(el);
548-
const rate =
549-
isPlayingVideo && !hardSync ? steeredVideoRate(el, offset, baseRate) : baseRate;
555+
const rate = Math.max(
556+
MIN_NATIVE_PLAYBACK_RATE,
557+
isPlayingVideo && !hardSync ? steeredVideoRate(el, offset, baseRate) : baseRate,
558+
);
550559
// Some engines read a rate back at lower precision; an equal-enough rate is not rewritten.
551560
if (Math.abs(el.playbackRate - rate) > 1e-6) el.playbackRate = rate;
552561
} catch (err) {
@@ -584,12 +593,12 @@ export function syncRuntimeMedia(params: {
584593
holdSeekBarrierUntilVideoLands(el);
585594
}
586595
playRequested.delete(el);
587-
} else if (!params.playing) {
596+
} else if (!playing) {
588597
holdSeekBarrierUntilVideoLands(el);
589598
}
590599
if (isHeldVideoTail) {
591600
if (!el.paused) el.pause();
592-
} else if (params.playing && el.paused && !playRequested.has(el) && !isUnplayable(el)) {
601+
} else if (playing && el.paused && !playRequested.has(el) && !isUnplayable(el)) {
593602
// `HTMLMediaElement.play()` is spec'd to queue playback and resolve
594603
// once enough data is buffered, so we can unconditionally call it —
595604
// no need to gate on `readyState` or defer to a `canplay` listener.
@@ -617,7 +626,7 @@ export function syncRuntimeMedia(params: {
617626
: "";
618627
if (name === "NotAllowedError") params.onAutoplayBlocked?.();
619628
});
620-
} else if (!params.playing && !el.paused) {
629+
} else if (!playing && !el.paused) {
621630
el.pause();
622631
}
623632
continue;

0 commit comments

Comments
 (0)