mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(core): Safari video picture lands on the cut again, undoing the hidden pre-roll (#7383)
* fix(core): Safari video picture lands on the cut again, undoing the hidden pre-roll Reverts the one-second hidden video pre-roll (#7265): on Safari the pre-rolled video is still behind at the cut and trails it by 30 to 150 ms in every measured run, and some cut flashes never show. * fix(core): shorten a restored comment line to the 120-column limit
This commit is contained in:
@@ -51,8 +51,7 @@ it.each(["video", "audio"])(
|
||||
expect(play).not.toHaveBeenCalled();
|
||||
for (let i = 0; i < 11; i++) step();
|
||||
if (tag === "video") expect(video.style.visibility).toBe("visible");
|
||||
// A video is landed once more where its pre-roll plays from.
|
||||
expect(seek).toHaveBeenCalledTimes(tag === "video" ? 2 : 1);
|
||||
expect(seek).toHaveBeenCalledTimes(1);
|
||||
expect(play).toHaveBeenCalledTimes(1);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -49,14 +49,8 @@ describe("incoming media preparation", () => {
|
||||
expect(el.currentTime).toBe(mediaElementTime(8));
|
||||
expect(el.play).not.toHaveBeenCalled();
|
||||
tick(3);
|
||||
if (tag === "video") {
|
||||
// A video plays from one second before its in-point, so it is landed there once more as it starts.
|
||||
expect(el.currentTime).toBe(mediaElementTime(7));
|
||||
expect(seek).toHaveBeenCalledTimes(2);
|
||||
} else {
|
||||
tick(4);
|
||||
expect(seek).toHaveBeenCalledTimes(1);
|
||||
}
|
||||
tick(4);
|
||||
expect(seek).toHaveBeenCalledTimes(1);
|
||||
expect(el.play).toHaveBeenCalledTimes(1);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
readElementPlaybackRate,
|
||||
readElementPlaybackStart,
|
||||
refreshRuntimeMediaCache,
|
||||
resetVideoStartLatency,
|
||||
resolveRuntimeMediaClipDuration,
|
||||
syncRuntimeMedia as syncRuntimeMediaWithDuration,
|
||||
} from "./media";
|
||||
@@ -1014,7 +1015,7 @@ describe("syncRuntimeMedia", () => {
|
||||
expect(clip.el.play).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("starts no audio early while paused", () => {
|
||||
it("starts nothing early while paused, and never a video", () => {
|
||||
const audio = createMockClip({ start: 5, end: 6 }, "audio");
|
||||
syncRuntimeMedia({
|
||||
clips: [audio],
|
||||
@@ -1023,17 +1024,15 @@ describe("syncRuntimeMedia", () => {
|
||||
playbackRate: 1,
|
||||
cueAheadSeconds: 0.017,
|
||||
});
|
||||
expect(audio.el.play).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("starts no video early while paused", () => {
|
||||
const video = createMockClip({ start: 5, end: 6 }, "video");
|
||||
syncRuntimeMedia({
|
||||
clips: [video],
|
||||
timeSeconds: 4.99,
|
||||
playing: false,
|
||||
playing: true,
|
||||
playbackRate: 1,
|
||||
cueAheadSeconds: 0.017,
|
||||
});
|
||||
expect(audio.el.play).not.toHaveBeenCalled();
|
||||
expect(video.el.play).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -2304,106 +2303,121 @@ describe("syncRuntimeMedia", () => {
|
||||
expect(el.pause).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
describe("video pre-roll", () => {
|
||||
const playing = (clip: RuntimeMediaClip, currentTime: number) => {
|
||||
describe("start latency", () => {
|
||||
afterEach(() => resetVideoStartLatency());
|
||||
|
||||
// Plays `clip` at `playAt`, then has its element `lag` s behind the playhead at `sampleAt`.
|
||||
const startWithLag = (
|
||||
clip: RuntimeMediaClip,
|
||||
playAt: number,
|
||||
sampleAt: number,
|
||||
lag: number,
|
||||
playheadRunning = true,
|
||||
) => {
|
||||
if (playheadRunning)
|
||||
syncRuntimeMedia({
|
||||
clips: [],
|
||||
timeSeconds: playAt,
|
||||
playing: true,
|
||||
playbackRate: 1,
|
||||
getCompositionDuration: () => 30,
|
||||
});
|
||||
tick(clip, playAt);
|
||||
expect(clip.el.play).toHaveBeenCalledTimes(1);
|
||||
Object.defineProperty(clip.el, "paused", {
|
||||
value: false,
|
||||
writable: true,
|
||||
configurable: true,
|
||||
});
|
||||
clip.el.currentTime = currentTime;
|
||||
clip.el.currentTime = clip.mediaStart + (sampleAt - clip.start) - lag;
|
||||
tick(clip, sampleAt);
|
||||
};
|
||||
|
||||
it("starts an upcoming video one second before its clip, from one second before its first frame", () => {
|
||||
const clip = videoClip({ start: 10, end: 20, mediaStart: 5 });
|
||||
tick(clip, 8.9);
|
||||
expect(clip.el.play).not.toHaveBeenCalled();
|
||||
tick(clip, 9);
|
||||
expect(clip.el.play).toHaveBeenCalledTimes(1);
|
||||
expect(clip.el.currentTime).toBe(mediaElementTime(4));
|
||||
});
|
||||
|
||||
it("starts a video with less than a second of source before its first frame from the start of the file", () => {
|
||||
const clip = videoClip({ start: 10, end: 20, mediaStart: 0.3 });
|
||||
tick(clip, 9.5);
|
||||
expect(clip.el.play).not.toHaveBeenCalled();
|
||||
tick(clip, 9.75);
|
||||
expect(clip.el.play).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("starts a video that plays from the very start of its file 50 ms early, on its first frame", () => {
|
||||
it("does not start an upcoming video early before any start is measured", () => {
|
||||
const clip = videoClip({ start: 10, end: 20, mediaStart: 0 });
|
||||
tick(clip, 9.9);
|
||||
tick(clip, 9.99);
|
||||
expect(clip.el.play).not.toHaveBeenCalled();
|
||||
tick(clip, 9.96);
|
||||
expect(clip.el.play).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it.each([0, 0.04])(
|
||||
"plays a from-file-start clip that starts in %d s through its cut without pausing it",
|
||||
(startLatency) => {
|
||||
const clip = videoClip({ start: 10, end: 20, mediaStart: 0 });
|
||||
clip.el.pause = vi.fn();
|
||||
let playedAt: number | null = null;
|
||||
clip.el.play = vi.fn(() => {
|
||||
playedAt = lastTick;
|
||||
Object.defineProperty(clip.el, "paused", {
|
||||
value: false,
|
||||
writable: true,
|
||||
configurable: true,
|
||||
});
|
||||
return Promise.resolve();
|
||||
});
|
||||
let lastTick = 9.9;
|
||||
for (let t = 9.9; t < 10.3; t += 1 / 60) {
|
||||
if (playedAt !== null && t - playedAt >= startLatency)
|
||||
clip.el.currentTime += (1 / 60) * clip.el.playbackRate;
|
||||
tick(clip, t);
|
||||
lastTick = t;
|
||||
}
|
||||
expect(clip.el.play).toHaveBeenCalledTimes(1);
|
||||
expect(clip.el.pause).not.toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
|
||||
it("slows a video that started ahead on its first frame, hidden, instead of holding it", () => {
|
||||
const clip = videoClip({ start: 10, end: 20, mediaStart: 0 });
|
||||
tick(clip, 9.96);
|
||||
playing(clip, 0.1);
|
||||
clip.el.pause = vi.fn();
|
||||
tick(clip, 9.97);
|
||||
expect(clip.el.pause).not.toHaveBeenCalled();
|
||||
expect(clip.el.playbackRate).toBeLessThan(1);
|
||||
it("starts an upcoming video early by the start latency measured on earlier ones", () => {
|
||||
startWithLag(videoClip({ start: 0, end: 10, mediaStart: 0 }), 0, 0.3, 0.06);
|
||||
const due50ms = videoClip({ start: 10, end: 20, mediaStart: 0 });
|
||||
tick(due50ms, 9.95);
|
||||
expect(due50ms.el.play).toHaveBeenCalledTimes(1);
|
||||
const due70ms = videoClip({ start: 10, end: 20, mediaStart: 0 });
|
||||
tick(due70ms, 9.93);
|
||||
expect(due70ms.el.play).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("runs a pre-rolled video that is late up to twice as fast, hidden", () => {
|
||||
const clip = videoClip({ start: 10, end: 20, mediaStart: 5 });
|
||||
tick(clip, 9.5);
|
||||
playing(clip, 4.4); // its target is 4.5
|
||||
tick(clip, 9.5);
|
||||
expect(clip.el.playbackRate).toBeCloseTo(1.5, 9);
|
||||
playing(clip, 4);
|
||||
tick(clip, 9.5);
|
||||
expect(clip.el.playbackRate).toBeCloseTo(2, 9);
|
||||
it("starts an upcoming video on the tick nearest its lead", () => {
|
||||
startWithLag(videoClip({ start: 0, end: 10, mediaStart: 0 }), 0, 0.3, 0.06);
|
||||
const due75ms = videoClip({ start: 10, end: 20, mediaStart: 0 });
|
||||
// Now is 15 ms earlier than the 60 ms lead; the next tick, 40 ms on, would be 25 ms late.
|
||||
syncRuntimeMedia({
|
||||
clips: [due75ms],
|
||||
timeSeconds: 9.925,
|
||||
playing: true,
|
||||
playbackRate: 1,
|
||||
cueAheadSeconds: 0.04,
|
||||
getCompositionDuration: () => 30,
|
||||
});
|
||||
expect(due75ms.el.play).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("runs a pre-rolled video that is ahead at half speed instead of holding it", () => {
|
||||
const clip = videoClip({ start: 10, end: 20, mediaStart: 5 });
|
||||
tick(clip, 9.5);
|
||||
playing(clip, 4.9);
|
||||
clip.el.pause = vi.fn();
|
||||
tick(clip, 9.5);
|
||||
expect(clip.el.pause).not.toHaveBeenCalled();
|
||||
expect(clip.el.playbackRate).toBeCloseTo(0.5, 9);
|
||||
it("learns half the start delay of a video started by Play", () => {
|
||||
startWithLag(videoClip({ start: 0, end: 10, mediaStart: 0 }), 0, 0.3, 0.074, false);
|
||||
const due45ms = videoClip({ start: 10, end: 20, mediaStart: 0 });
|
||||
tick(due45ms, 9.955);
|
||||
expect(due45ms.el.play).not.toHaveBeenCalled();
|
||||
const due35ms = videoClip({ start: 10, end: 20, mediaStart: 0 });
|
||||
tick(due35ms, 9.965);
|
||||
expect(due35ms.el.play).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("steers a video gently once its clip is visible", () => {
|
||||
const clip = videoClip({ start: 10, end: 20, mediaStart: 5 });
|
||||
tick(clip, 9.5);
|
||||
playing(clip, 4.9);
|
||||
tick(clip, 10);
|
||||
tick(clip, 10);
|
||||
expect(clip.el.playbackRate).toBeCloseTo(1.03, 9);
|
||||
it("learns from a video seeked and started in the same tick", () => {
|
||||
const seeked = videoClip({ start: 0, end: 10, mediaStart: 0 });
|
||||
seeked.el.currentTime = 0;
|
||||
startWithLag(seeked, 5, 5.3, 0.06);
|
||||
const due25ms = videoClip({ start: 10, end: 20, mediaStart: 0 });
|
||||
tick(due25ms, 9.975);
|
||||
expect(due25ms.el.play).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("keeps its lead when an early start lands on time", () => {
|
||||
startWithLag(videoClip({ start: 0, end: 10, mediaStart: 0 }), 0, 0.3, 0.06);
|
||||
startWithLag(videoClip({ start: 10, end: 20, mediaStart: 0 }), 9.945, 10.3, 0.005);
|
||||
const due58ms = videoClip({ start: 20, end: 30, mediaStart: 0 });
|
||||
tick(due58ms, 19.942);
|
||||
expect(due58ms.el.play).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("caps the learned start delay at 100 ms", () => {
|
||||
startWithLag(videoClip({ start: 0, end: 10, mediaStart: 0 }), 0, 0.3, 0.3);
|
||||
const due150ms = videoClip({ start: 10, end: 20, mediaStart: 0 });
|
||||
tick(due150ms, 9.85);
|
||||
expect(due150ms.el.play).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not learn from a looping video sampled across a lap wrap", () => {
|
||||
startWithLag(videoClip({ start: 0, end: 10, mediaStart: 0 }), 0, 0.3, 0.06);
|
||||
const loop = videoClip({
|
||||
start: 10,
|
||||
end: 20,
|
||||
mediaStart: 0,
|
||||
loop: true,
|
||||
sourceDuration: 0.2,
|
||||
});
|
||||
tick(loop, 10);
|
||||
Object.defineProperty(loop.el, "paused", {
|
||||
value: false,
|
||||
writable: true,
|
||||
configurable: true,
|
||||
});
|
||||
loop.el.currentTime = 0.19; // the playhead has wrapped to 0.06, the element not yet
|
||||
tick(loop, 10.26);
|
||||
const due50ms = videoClip({ start: 30, end: 40, mediaStart: 0 });
|
||||
tick(due50ms, 29.95);
|
||||
expect(due50ms.el.play).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -317,6 +317,7 @@ export function evictMediaSyncState(el: HTMLMediaElement): void {
|
||||
forgetSteering(el);
|
||||
heldAhead.delete(el);
|
||||
forgetOutPoint(el);
|
||||
videoStarts.delete(el);
|
||||
}
|
||||
|
||||
/** Whether the clip is inside its window at the playhead right now: the sync tick keeps its time while it is. */
|
||||
@@ -448,12 +449,28 @@ function outPointHoldTime(el: HTMLMediaElement, outPoint: number, watched: boole
|
||||
return last?.outPoint === outPoint ? last.at : outPoint;
|
||||
}
|
||||
|
||||
const VIDEO_PREROLL_SECONDS = 1;
|
||||
const VIDEO_MIN_LEAD_SECONDS = VIDEO_LEAD_HOLD_SECONDS / 2;
|
||||
// A muted video runs on its audio track, which starts after play(): an upcoming one starts early by the learned delay.
|
||||
let videoStartLatency: number | null = null;
|
||||
const videoStarts = new WeakMap<
|
||||
HTMLMediaElement,
|
||||
{ offset: number; lead: number; at: number; weight: number }
|
||||
>();
|
||||
let playheadWasRunning = false;
|
||||
|
||||
const isRenderDriven = (el: HTMLMediaElement): boolean =>
|
||||
!!findInjectedRenderFrame(el) ||
|
||||
("__HF_EXPORT_RENDER_SEEK_CONFIG" in window && !!window.__HF_EXPORT_RENDER_SEEK_CONFIG);
|
||||
export function resetVideoStartLatency(): void {
|
||||
videoStartLatency = null;
|
||||
playheadWasRunning = false;
|
||||
}
|
||||
|
||||
/** A quarter second in: its lag plus how early it started, capped at 0.1 s. */
|
||||
function learnVideoStartLatency(el: HTMLMediaElement, time: number, offset: number, rate: number) {
|
||||
const start = videoStarts.get(el);
|
||||
if (!start || time < start.at) return;
|
||||
videoStarts.delete(el);
|
||||
const sample = ((offset - start.offset) / rate + start.lead) * start.weight;
|
||||
const averaged = videoStartLatency === null ? sample : (videoStartLatency + sample) / 2;
|
||||
videoStartLatency = Math.min(0.1, Math.max(0, averaged));
|
||||
}
|
||||
|
||||
// fallow-ignore-next-line complexity
|
||||
export function syncRuntimeMedia(params: {
|
||||
@@ -510,6 +527,8 @@ export function syncRuntimeMedia(params: {
|
||||
getCompositionDuration: () => number;
|
||||
}): void {
|
||||
const forceMuteAll = !!(params.outputMuted || params.userMuted);
|
||||
const startWeight = playheadWasRunning ? 1 : 0.5;
|
||||
playheadWasRunning = params.playing && params.clockAdvancing !== false;
|
||||
for (const clip of params.clips) {
|
||||
const { el } = clip;
|
||||
if (!el.isConnected) continue;
|
||||
@@ -521,11 +540,8 @@ export function syncRuntimeMedia(params: {
|
||||
const cueAhead =
|
||||
el.tagName === "AUDIO"
|
||||
? (params.cueAheadSeconds ?? 0)
|
||||
: params.playing && (el.paused || startedEarly.has(el)) && !isRenderDriven(el)
|
||||
? Math.min(
|
||||
VIDEO_PREROLL_SECONDS,
|
||||
Math.max(VIDEO_MIN_LEAD_SECONDS, clip.mediaStart / rateAt(clipRate, 0)),
|
||||
) * params.playbackRate
|
||||
: params.playing && videoStartLatency !== null
|
||||
? videoStartLatency * params.playbackRate + (params.cueAheadSeconds ?? 0) / 2
|
||||
: 0;
|
||||
const startsEarly =
|
||||
!inWindow &&
|
||||
@@ -541,10 +557,7 @@ export function syncRuntimeMedia(params: {
|
||||
!inWindow &&
|
||||
(params.timeSeconds >= clip.end || sameInstant(params.timeSeconds, clip.end)) &&
|
||||
isClipVisibleAt(params.timeSeconds, clip.start, clip.end, params.getCompositionDuration());
|
||||
const relTime =
|
||||
startsEarly && isNonLoopVideo
|
||||
? Math.max(0, clip.mediaStart - dueIn * rateAt(clipRate, 0))
|
||||
: mediaSourceTimeAt(clip, params.timeSeconds);
|
||||
const relTime = mediaSourceTimeAt(clip, params.timeSeconds);
|
||||
const isHeldVideoTail =
|
||||
isTerminalVideo ||
|
||||
(isNonLoopVideo && clip.sourceDuration != null && relTime >= clip.sourceDuration && inWindow);
|
||||
@@ -736,7 +749,7 @@ export function syncRuntimeMedia(params: {
|
||||
// A loop's playhead wraps before its element, so its offset jumps by a lap: steering owns loops.
|
||||
const ahead =
|
||||
!clip.loop &&
|
||||
((offset < -VIDEO_LEAD_HOLD_SECONDS && !startedEarly.has(el)) ||
|
||||
(offset < -VIDEO_LEAD_HOLD_SECONDS ||
|
||||
atOutPoint ||
|
||||
currentElTime >= outPointHoldTime(el, outPoint, watched));
|
||||
if (!playing || (!ahead && offset >= -MEDIA_SYNC_TOLERANCE_SECONDS)) heldAhead.delete(el);
|
||||
@@ -747,6 +760,7 @@ export function syncRuntimeMedia(params: {
|
||||
}
|
||||
const holdingAhead = heldAhead.has(el);
|
||||
const isPlayingMedia = !el.paused && playing;
|
||||
if (isPlayingMedia) learnVideoStartLatency(el, params.timeSeconds, offset, baseRate);
|
||||
// Only apply strict sync when offset has stabilized (not growing).
|
||||
// During initial buffering, offset grows ~16ms/tick as the timeline
|
||||
// advances while media stays at 0. Accumulated drift from pause/play
|
||||
@@ -774,11 +788,8 @@ export function syncRuntimeMedia(params: {
|
||||
strictDriftSamples.set(el, 0);
|
||||
}
|
||||
const forceSync = !isPlayingMedia && !holdingAhead && params.forceSync && drift > 0.02;
|
||||
const isHiddenPreRoll = el.tagName === "VIDEO" && startedEarly.has(el);
|
||||
const steered =
|
||||
isPlayingMedia && !hardSync
|
||||
? steerMedia(el, offset, baseRate, offsetStabilized, undefined, isHiddenPreRoll)
|
||||
: null;
|
||||
isPlayingMedia && !hardSync ? steerMedia(el, offset, baseRate, offsetStabilized) : null;
|
||||
const steerSeeks = steered?.seek === true;
|
||||
const dipped = steerSeeks && params.seekUnderDip?.(el, relTime) === true;
|
||||
try {
|
||||
@@ -791,13 +802,7 @@ export function syncRuntimeMedia(params: {
|
||||
// ignore unsupported playbackRate
|
||||
swallow("runtime.media.site1", err);
|
||||
}
|
||||
const prerollSeek =
|
||||
startsEarly &&
|
||||
isNonLoopVideo &&
|
||||
el.paused &&
|
||||
!playRequested.has(el) &&
|
||||
drift > MEDIA_SYNC_TOLERANCE_SECONDS;
|
||||
if (hardSync || strictSync || forceSync || prerollSeek || (steerSeeks && !dipped)) {
|
||||
if (hardSync || strictSync || forceSync || (steerSeeks && !dipped)) {
|
||||
// Skip the per-tick seek (and the `el.load()` drift-recovery retry
|
||||
// below) for `<video>` elements that have a sibling
|
||||
// `<img id="__render_frame_<id>__">`. The sibling is created only
|
||||
@@ -831,6 +836,7 @@ export function syncRuntimeMedia(params: {
|
||||
playRequested.delete(el);
|
||||
heldAhead.delete(el);
|
||||
forgetOutPoint(el);
|
||||
videoStarts.delete(el);
|
||||
} else if (!playing) {
|
||||
holdSeekBarrierUntilVideoLands(el);
|
||||
}
|
||||
@@ -857,6 +863,13 @@ export function syncRuntimeMedia(params: {
|
||||
// the element waiting for a callback that never came.
|
||||
markPlayRequested(el);
|
||||
markMediaRestarted(el);
|
||||
if (isNonLoopVideo)
|
||||
videoStarts.set(el, {
|
||||
weight: startWeight,
|
||||
offset: relTime - (el.currentTime || 0),
|
||||
lead: Math.max(0, clip.start - params.timeSeconds) / params.playbackRate,
|
||||
at: Math.max(params.timeSeconds, clip.start) + 0.25 * params.playbackRate,
|
||||
});
|
||||
void el.play().catch((err: unknown) => {
|
||||
// If play() rejects — e.g. autoplay blocked, element removed
|
||||
// mid-flight — drop the in-flight flag so a future sync tick can
|
||||
|
||||
@@ -56,18 +56,4 @@ describe("steerMedia", () => {
|
||||
forgetSteering(el);
|
||||
expect(steerMedia(el, 0.02, 1, true).rate).toBe(1);
|
||||
});
|
||||
it("steers a hidden video in proportion to its lag, up to twice as fast and down to half", () => {
|
||||
const el = media("video");
|
||||
expect(steerMedia(el, 0.1, 1, true, false, true).rate).toBeCloseTo(1.5, 9);
|
||||
expect(steerMedia(el, 0.4, 1, true, false, true).rate).toBeCloseTo(2, 9);
|
||||
expect(steerMedia(el, -0.05, 1, true, false, true).rate).toBeCloseTo(0.75, 9);
|
||||
expect(steerMedia(el, -0.5, 1, true, false, true).rate).toBeCloseTo(0.5, 9);
|
||||
});
|
||||
|
||||
it("steers a hidden video no differently inside the tolerance", () => {
|
||||
expect(steerMedia(media("video"), 0.03, 1, true, false, true)).toEqual({
|
||||
rate: 1,
|
||||
seek: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,8 +15,6 @@ const MEDIA_STEER_RELEASE_SECONDS = 0.01;
|
||||
const AUDIO_MAX_STEER = 0.1;
|
||||
const AUDIO_STEER_LIMIT_SECONDS = 0.1;
|
||||
const WEBKIT_AUDIO_START_LAG_SECONDS = 0.65;
|
||||
// A video nobody sees runs up to twice as fast (half as slow) to reach the playhead: the lag in seconds times this.
|
||||
const HIDDEN_GAIN = 5;
|
||||
|
||||
/** Direction (+1 fast, -1 slow) an element is being steered in; absent when it plays at its authored rate. */
|
||||
const steering = new WeakMap<HTMLMediaElement, number>();
|
||||
@@ -34,7 +32,6 @@ export function steerMedia(
|
||||
baseRate: number,
|
||||
settled: boolean,
|
||||
audioOnly = el.tagName === "AUDIO",
|
||||
hidden = false,
|
||||
): { rate: number; seek: boolean } {
|
||||
const lag = Math.abs(offset);
|
||||
if (audioOnly && isWebKit()) {
|
||||
@@ -49,8 +46,6 @@ export function steerMedia(
|
||||
return { rate: baseRate, seek: true };
|
||||
}
|
||||
const direction = Math.sign(offset);
|
||||
if (hidden && lag > MEDIA_SYNC_TOLERANCE_SECONDS)
|
||||
return steerHidden(el, direction, lag, baseRate);
|
||||
const current = steering.get(el);
|
||||
if (lag > MEDIA_SYNC_TOLERANCE_SECONDS) steering.set(el, direction);
|
||||
else if (current !== direction || lag <= MEDIA_STEER_RELEASE_SECONDS) {
|
||||
@@ -62,12 +57,6 @@ export function steerMedia(
|
||||
return { rate: baseRate * (1 + direction * steer), seek: false };
|
||||
}
|
||||
|
||||
function steerHidden(el: HTMLMediaElement, direction: number, lag: number, baseRate: number) {
|
||||
steering.set(el, direction);
|
||||
const steer = Math.min(direction > 0 ? 1 : 0.5, lag * HIDDEN_GAIN);
|
||||
return { rate: baseRate * (1 + direction * steer), seek: false };
|
||||
}
|
||||
|
||||
/** Drop `el`'s steering: it was seeked, paused or replaced, so its old offset says nothing. */
|
||||
export function forgetSteering(el: HTMLMediaElement): void {
|
||||
steering.delete(el);
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
"unpacked": 885314
|
||||
},
|
||||
"@hyperframes/player": {
|
||||
"packed": 158800,
|
||||
"packed": 157145,
|
||||
"unpacked": 541601
|
||||
},
|
||||
"@hyperframes/producer": {
|
||||
|
||||
Reference in New Issue
Block a user