Skip to content

Commit af08299

Browse files
committed
fix(core): steering for hidden video is its own function; trim comments and format
1 parent e3fa1bb commit af08299

3 files changed

Lines changed: 15 additions & 18 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2272,7 +2272,11 @@ describe("syncRuntimeMedia", () => {
22722272
let playedAt: number | null = null;
22732273
clip.el.play = vi.fn(() => {
22742274
playedAt = lastTick;
2275-
Object.defineProperty(clip.el, "paused", { value: false, writable: true, configurable: true });
2275+
Object.defineProperty(clip.el, "paused", {
2276+
value: false,
2277+
writable: true,
2278+
configurable: true,
2279+
});
22762280
return Promise.resolve();
22772281
});
22782282
let lastTick = 9.9;

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,9 @@ function outPointHoldTime(el: HTMLMediaElement, outPoint: number, watched: boole
393393
return last?.outPoint === outPoint ? last.at : outPoint;
394394
}
395395

396-
// A muted video starts 0.04 to 0.4 s after play() and cannot be timed from the last start, so an upcoming
397-
// video is played from its pre-roll, hidden, and catches up on the playhead at a speed the eye never sees.
398396
const VIDEO_PREROLL_SECONDS = 1;
399-
// A clip with no source before its first frame starts on that frame this far ahead; half the lead hold keeps a
400-
// clip that starts instantly under it, since the hold would pause the clip at the cut and it would restart late.
401397
const VIDEO_MIN_LEAD_SECONDS = VIDEO_LEAD_HOLD_SECONDS / 2;
402398

403-
/** A render (export or frame-injected) paints video from its own frames, so a pre-roll would only seek it. */
404399
const isRenderDriven = (el: HTMLMediaElement): boolean =>
405400
!!findInjectedRenderFrame(el) ||
406401
("__HF_EXPORT_RENDER_SEEK_CONFIG" in window && !!window.__HF_EXPORT_RENDER_SEEK_CONFIG);
@@ -491,8 +486,6 @@ export function syncRuntimeMedia(params: {
491486
!inWindow &&
492487
(params.timeSeconds >= clip.end || sameInstant(params.timeSeconds, clip.end)) &&
493488
isClipVisibleAt(params.timeSeconds, clip.start, clip.end, params.getCompositionDuration());
494-
// A pre-rolled video's target runs up to its first frame from before it (not before the file's start),
495-
// where the clip's own time is clamped.
496489
const relTime =
497490
startsEarly && isNonLoopVideo
498491
? Math.max(0, clip.mediaStart - dueIn * rateAt(clipRate, 0))
@@ -726,10 +719,10 @@ export function syncRuntimeMedia(params: {
726719
strictDriftSamples.set(el, 0);
727720
}
728721
const forceSync = !isPlayingMedia && !holdingAhead && params.forceSync && drift > 0.02;
729-
// A pre-rolled video is steered hard, which assumes the visibility pass hides it until its clip starts.
722+
const isHiddenPreRoll = el.tagName === "VIDEO" && startedEarly.has(el);
730723
const steered =
731724
isPlayingMedia && !hardSync
732-
? steerMedia(el, offset, baseRate, offsetStabilized, undefined, startedEarly.has(el))
725+
? steerMedia(el, offset, baseRate, offsetStabilized, undefined, isHiddenPreRoll)
733726
: null;
734727
const steerSeeks = steered?.seek === true;
735728
const dipped = steerSeeks && params.seekUnderDip?.(el, relTime) === true;
@@ -743,7 +736,6 @@ export function syncRuntimeMedia(params: {
743736
// ignore unsupported playbackRate
744737
swallow("runtime.media.site1", err);
745738
}
746-
// A pre-rolled video is seeked to its pre-roll before it plays, once.
747739
const prerollSeek =
748740
startsEarly &&
749741
isNonLoopVideo &&

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,8 @@ export function steerMedia(
4949
return { rate: baseRate, seek: true };
5050
}
5151
const direction = Math.sign(offset);
52-
if (hidden && !audioOnly && lag > MEDIA_SYNC_TOLERANCE_SECONDS) {
53-
steering.set(el, direction);
54-
return {
55-
rate: baseRate * (1 + direction * Math.min(direction > 0 ? 1 : 0.5, lag * HIDDEN_GAIN)),
56-
seek: false,
57-
};
58-
}
52+
if (hidden && lag > MEDIA_SYNC_TOLERANCE_SECONDS)
53+
return steerHidden(el, direction, lag, baseRate);
5954
const current = steering.get(el);
6055
if (lag > MEDIA_SYNC_TOLERANCE_SECONDS) steering.set(el, direction);
6156
else if (current !== direction || lag <= MEDIA_STEER_RELEASE_SECONDS) {
@@ -67,6 +62,12 @@ export function steerMedia(
6762
return { rate: baseRate * (1 + direction * steer), seek: false };
6863
}
6964

65+
function steerHidden(el: HTMLMediaElement, direction: number, lag: number, baseRate: number) {
66+
steering.set(el, direction);
67+
const steer = Math.min(direction > 0 ? 1 : 0.5, lag * HIDDEN_GAIN);
68+
return { rate: baseRate * (1 + direction * steer), seek: false };
69+
}
70+
7071
/** Drop `el`'s steering: it was seeked, paused or replaced, so its old offset says nothing. */
7172
export function forgetSteering(el: HTMLMediaElement): void {
7273
steering.delete(el);

0 commit comments

Comments
 (0)