@hyperframes/core 0.8.206 → 0.8.208

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,585 +0,0 @@
1
- import { swallow } from "./diagnostics.js";
2
- import { isClipVisibleAt, isInClipWindow } from "./clipWindow.js";
3
- import { mediaElementTime } from "./mediaClock.js";
4
- import { sameInstant } from "../clipFacts.js";
5
- import { interpolateVolumeGain } from "./mediaVolumeEnvelope.js";
6
- import { elementVolumeLaneGain } from "./audioAutomationVolume.js";
7
- import { fadeGain, NO_FADES, readElementFades } from "../audioFade.js";
8
- import { readElementPlaybackRate, readElementRateSpec, readMediaStart } from "./playbackRate.js";
9
- import { rateAt, sourceTimeAt, timeAtSourceTime } from "../speedRamp.js";
10
- import { clampAudioGain, clipGain, gainTimesLane } from "../audioGain.js";
11
- import { isMemberGroupHidden } from "../audioGroups.js";
12
- import { findInjectedRenderFrame } from "./renderFrameSibling.js";
13
- import { isRenderFrameVideo } from "./preloadMedia.js";
14
- import { registerSeekCompletion } from "./adapters/seek-dispatch.js";
15
- import { cutToHostSlots } from "../mediaTiming.js";
16
- export { readElementPlaybackRate, readElementRateSpec, resolveNaturalMediaTimelineDuration, } from "./playbackRate.js";
17
- export function readElementPlaybackStart(el) {
18
- return readMediaStart(el);
19
- }
20
- const HOLD_END_EVENTS = ["seeked", "loadeddata", "error", "emptied", "abort"];
21
- export const HOLD_CAP_MS = 5000;
22
- const releaseHeldVideo = new WeakMap();
23
- // A seeking video still paints its previous frame, and one still fetching its first data paints none (its seek
24
- // waits for metadata without setting `seeking`); frame captures wait on the barrier until it lands. The 5 s cap is
25
- // the only end for a stalled source (`suspend` also fires between range requests); capture phase catches <source>.
26
- function holdSeekBarrierUntilVideoLands(el) {
27
- const loading = el.readyState < el.HAVE_CURRENT_DATA &&
28
- el.networkState === el.NETWORK_LOADING &&
29
- !window.__HF_EXPORT_RENDER_SEEK_CONFIG;
30
- if (el.tagName !== "VIDEO" || !(el.seeking || loading) || findInjectedRenderFrame(el))
31
- return;
32
- releaseHeldVideo.get(el)?.();
33
- registerSeekCompletion(new Promise((resolve) => {
34
- const done = (event) => {
35
- if (event?.type === "loadeddata" && el.seeking)
36
- return;
37
- clearTimeout(cap);
38
- for (const type of HOLD_END_EVENTS)
39
- el.removeEventListener(type, done, true);
40
- if (releaseHeldVideo.get(el) === done)
41
- releaseHeldVideo.delete(el);
42
- resolve();
43
- };
44
- const cap = setTimeout(done, HOLD_CAP_MS);
45
- for (const type of HOLD_END_EVENTS)
46
- el.addEventListener(type, done, true);
47
- releaseHeldVideo.set(el, done);
48
- }), "video-frame");
49
- }
50
- /**
51
- * Resolve a media element's timeline window without conflating a video's
52
- * authored display slot with the amount of source left to decode.
53
- *
54
- * An explicit video slot may outlive its source and holds the final frame.
55
- * Audio remains source-bounded because it has no visual hold state.
56
- */
57
- export function resolveRuntimeMediaClipDuration(params) {
58
- const mediaDuration = params.isVideo
59
- ? (params.explicitDuration ?? params.sourceDuration)
60
- : params.sourceDuration;
61
- const candidates = (params.isVideo
62
- ? [mediaDuration, params.hostRemaining]
63
- : [mediaDuration, params.hostRemaining, params.explicitDuration]).filter((value) => value != null && Number.isFinite(value) && value >= 0);
64
- return candidates.length > 0 ? Math.min(...candidates) : null;
65
- }
66
- export function refreshRuntimeMediaCache(params) {
67
- const mediaEls = params?.elements ??
68
- Array.from(document.querySelectorAll("video, audio"));
69
- const timedMediaEls = params?.shouldIncludeElement
70
- ? mediaEls.filter((el) => params.shouldIncludeElement?.(el))
71
- : mediaEls.filter((el) => el.hasAttribute("data-start"));
72
- const mediaClips = [];
73
- const videoClips = [];
74
- let maxMediaEnd = 0;
75
- for (const el of timedMediaEls) {
76
- let start = params?.resolveStartSeconds
77
- ? params.resolveStartSeconds(el)
78
- : Number.parseFloat(el.dataset.start ?? "0");
79
- if (!Number.isFinite(start))
80
- continue;
81
- const mediaStart = readElementPlaybackStart(el);
82
- const playbackRate = readElementPlaybackRate(el);
83
- const rate = readElementRateSpec(el);
84
- const loop = el.loop;
85
- const sourceDuration = Number.isFinite(el.duration) && el.duration > 0 ? el.duration : null;
86
- let duration = params?.resolveDurationSeconds?.(el) ?? Number.parseFloat(el.dataset.duration ?? "");
87
- if ((!Number.isFinite(duration) || duration < 0) && sourceDuration != null) {
88
- // Effective duration accounts for playback rate:
89
- // at 0.5x, a 10s source plays for 20s on the timeline
90
- duration = Math.max(0, timeAtSourceTime(rate, sourceDuration - mediaStart));
91
- }
92
- const hasKnownDuration = Number.isFinite(duration) && duration >= 0;
93
- let end = hasKnownDuration ? start + duration : Number.POSITIVE_INFINITY;
94
- const origin = start;
95
- const kept = cutToHostSlots({ start, end }, params?.resolveHostSlots?.(el) ?? []);
96
- if (kept.start !== start || kept.end !== end) {
97
- ({ start, end } = kept);
98
- duration = end - start;
99
- }
100
- const volumeRaw = Number.parseFloat(el.dataset.volume ?? "");
101
- const clip = {
102
- el,
103
- start,
104
- origin,
105
- mediaStart,
106
- duration: Number.isFinite(end) ? duration : Number.POSITIVE_INFINITY,
107
- end,
108
- volume: Number.isFinite(volumeRaw) ? volumeRaw : null,
109
- playbackRate,
110
- rate,
111
- loop,
112
- sourceDuration,
113
- fades: readElementFades(el),
114
- };
115
- mediaClips.push(clip);
116
- if (el.tagName === "VIDEO")
117
- videoClips.push(clip);
118
- if (Number.isFinite(end))
119
- maxMediaEnd = Math.max(maxMediaEnd, end);
120
- }
121
- return { timedMediaEls, mediaClips, videoClips, maxMediaEnd };
122
- }
123
- // Per-element timeline→media offset from the previous tick. Used to tell a
124
- // gradual drift (initial buffer catch-up, where offset grows ~16ms/tick) from
125
- // a scrub (where offset jumps in one tick). Cleared when a clip becomes
126
- // inactive so the next activation gets a hard resync on its first tick.
127
- const lastOffset = new WeakMap();
128
- // Desired source time from the previous active tick. Unlike `forceSync`, which
129
- // also covers play/pause and rate changes, a decrease here identifies an actual
130
- // backward transport seek within an audio clip.
131
- const lastRelativeTime = new WeakMap();
132
- const strictDriftSamples = new WeakMap();
133
- // Elements that had a seek past their buffered range (common with streaming
134
- // MP3 where preload="metadata" only fetches the first few seconds). After
135
- // setting preload="auto" and calling load(), we mark the element so subsequent
136
- // ticks don't restart the fetch in a loop while the browser downloads data.
137
- // Cleared when the clip leaves its active window.
138
- const seekLoadRetried = new WeakSet();
139
- // Elements whose play() is in flight. The sync runs on a 50 ms poll and with
140
- // a 1–2 s buffer that would fire 20–40 spurious play() calls per element —
141
- // noise in devtools and, worse, each `.catch(() => {})` would swallow a real
142
- // AbortError / NotAllowedError that should surface. Cleared on the `playing`
143
- // event (actual playback started) or on `pause`/`error` (state ended).
144
- const playRequested = new WeakSet();
145
- const startedEarly = new WeakSet();
146
- function markPlayRequested(el) {
147
- if (playRequested.has(el))
148
- return;
149
- playRequested.add(el);
150
- const clear = () => playRequested.delete(el);
151
- el.addEventListener("playing", clear, { once: true });
152
- el.addEventListener("pause", clear, { once: true });
153
- el.addEventListener("error", clear, { once: true });
154
- }
155
- // HTMLMediaElement.NETWORK_NO_SOURCE — no usable source (404 / unsupported).
156
- const MEDIA_NETWORK_NO_SOURCE = 3;
157
- // An element that errored or has no source can't play; re-issuing play() every
158
- // tick just floods rejections. Skip it until its state changes (src reload).
159
- export function isUnplayable(el) {
160
- return el.error != null || el.networkState === MEDIA_NETWORK_NO_SOURCE;
161
- }
162
- const lastRuntimeAppliedVolume = new WeakMap();
163
- function clampVolume(volume) {
164
- if (!Number.isFinite(volume))
165
- return 1;
166
- return Math.max(0, Math.min(1, volume));
167
- }
168
- /**
169
- * Drop every per-source sync baseline tracked for `el` — offset drift
170
- * samples, the seek-past-buffered-range retry latch, and the last
171
- * runtime-applied volume — so the next `syncRuntimeMedia` tick treats it as
172
- * a first tick (hard resync, fresh drift baseline) instead of comparing
173
- * against state computed for a different file. Used both when a clip leaves
174
- * its active window (below) and by the runtime's proxy-swap helper
175
- * (mediaProxy.ts) right after an in-place `src` swap, which points the same
176
- * element at a different file without ever leaving its active window.
177
- */
178
- export function evictMediaSyncState(el) {
179
- lastOffset.delete(el);
180
- lastRelativeTime.delete(el);
181
- strictDriftSamples.delete(el);
182
- seekLoadRetried.delete(el);
183
- lastRuntimeAppliedVolume.delete(el);
184
- videoSteering.delete(el);
185
- }
186
- /** Test-only seam: whether any per-source sync state is still tracked for `el`. */
187
- export function hasMediaSyncStateForTest(el) {
188
- return (lastOffset.has(el) ||
189
- lastRelativeTime.has(el) ||
190
- strictDriftSamples.has(el) ||
191
- seekLoadRetried.has(el) ||
192
- lastRuntimeAppliedVolume.has(el) ||
193
- videoSteering.has(el));
194
- }
195
- export const MEDIA_HARD_SYNC_SECONDS = 0.5;
196
- export const VIDEO_PRESEEK_SECONDS = 1.5;
197
- export function mediaSourceTimeAt(clip, timeSeconds) {
198
- const elapsed = Math.max(0, Math.min(timeSeconds, clip.end) - (clip.origin ?? clip.start));
199
- const target = clip.mediaStart + sourceTimeAt(clip.rate ?? clip.playbackRate, elapsed);
200
- if (clip.sourceDuration == null)
201
- return target;
202
- const loopLength = clip.sourceDuration - clip.mediaStart;
203
- if (clip.loop && loopLength > 0 && target >= clip.sourceDuration)
204
- return clip.mediaStart + ((target - clip.mediaStart) % loopLength);
205
- // A non-loop video past its source holds the last frame.
206
- if (!clip.loop && clip.el.tagName === "VIDEO")
207
- return Math.min(target, clip.sourceDuration);
208
- return target;
209
- }
210
- function prepareUpcomingVideo(clip, time, playing, rate) {
211
- const { el } = clip;
212
- const dueIn = clip.start - time;
213
- if (!playing ||
214
- el.tagName !== "VIDEO" ||
215
- dueIn <= 0 ||
216
- dueIn > VIDEO_PRESEEK_SECONDS * rate ||
217
- !el.paused ||
218
- el.seeking ||
219
- el.readyState < el.HAVE_METADATA ||
220
- findInjectedRenderFrame(el) ||
221
- ("__HF_EXPORT_RENDER_SEEK_CONFIG" in window && window.__HF_EXPORT_RENDER_SEEK_CONFIG))
222
- return;
223
- const target = mediaSourceTimeAt(clip, clip.start);
224
- if (!Number.isFinite(target) || target < 0 || Math.abs(el.currentTime - target) <= 0.001)
225
- return;
226
- try {
227
- // A trimmed video exposed during its in-point seek has no current frame at the cut.
228
- // Hidden preparation must not hold the visible-frame seek barrier.
229
- el.currentTime = mediaElementTime(target);
230
- }
231
- catch (err) {
232
- swallow("runtime.media.preseek", err);
233
- }
234
- }
235
- /** Drift a playing audio element may carry before sync pulls it back onto the playhead. */
236
- export const MEDIA_SYNC_TOLERANCE_SECONDS = 0.04;
237
- // Chromium refuses a slower playbackRate (NotSupportedError).
238
- const MIN_NATIVE_PLAYBACK_RATE = 1 / 16;
239
- // A playing video is steered back by rate, not seeked (a seek resets its decoder).
240
- // Its rate is written only when steering starts or stops: every write costs a frame.
241
- const VIDEO_STEER = 0.03;
242
- // A video that starts a cut late closes its lag over one clip at 3%; a bigger lag steers harder, in steps so the rate is written rarely.
243
- const VIDEO_STEER_STEPS = [
244
- [0.3, 0.25],
245
- [0.1, 0.1],
246
- ];
247
- const VIDEO_STEER_RELEASE_SECONDS = 0.01;
248
- /** Direction (+1 fast, -1 slow) a video is being steered in; absent when it plays at its authored rate. */
249
- const videoSteering = new WeakMap();
250
- /** Rate for a playing video `offset` seconds behind (+) or ahead (-) of the playhead. */
251
- function steeredVideoRate(el, offset, baseRate) {
252
- const direction = Math.sign(offset);
253
- const steering = videoSteering.get(el);
254
- if (Math.abs(offset) > MEDIA_SYNC_TOLERANCE_SECONDS)
255
- videoSteering.set(el, direction);
256
- else if (steering !== direction || Math.abs(offset) <= VIDEO_STEER_RELEASE_SECONDS) {
257
- videoSteering.delete(el);
258
- return baseRate;
259
- }
260
- const lag = Math.abs(offset);
261
- const steer = VIDEO_STEER_STEPS.find(([from]) => lag > from)?.[1] ?? VIDEO_STEER;
262
- return baseRate * (1 + direction * steer);
263
- }
264
- // fallow-ignore-next-line complexity
265
- export function syncRuntimeMedia(params) {
266
- const forceMuteAll = !!(params.outputMuted || params.userMuted);
267
- for (const clip of params.clips) {
268
- const { el } = clip;
269
- if (!el.isConnected)
270
- continue;
271
- const clipRate = clip.rate ?? clip.playbackRate;
272
- const origin = clip.origin ?? clip.start;
273
- const isNonLoopVideo = el.tagName === "VIDEO" && !clip.loop;
274
- const inWindow = isInClipWindow(params.timeSeconds, clip.start, clip.end);
275
- const dueIn = clip.start - params.timeSeconds;
276
- const startsEarly = el.tagName === "AUDIO" &&
277
- !inWindow &&
278
- dueIn > 0 &&
279
- dueIn * Math.max(1, rateAt(clipRate, clip.start - origin)) <=
280
- Math.max(params.cueAheadSeconds ?? 0, startedEarly.has(el) ? MEDIA_SYNC_TOLERANCE_SECONDS : 0);
281
- if (startsEarly)
282
- startedEarly.add(el);
283
- else
284
- startedEarly.delete(el);
285
- // A video that runs to the composition end stays the visible frame at and past it, so it
286
- // is held on the frame it shows at its own end rather than left on a stale one.
287
- const isTerminalVideo = isNonLoopVideo &&
288
- !inWindow &&
289
- (params.timeSeconds >= clip.end || sameInstant(params.timeSeconds, clip.end)) &&
290
- isClipVisibleAt(params.timeSeconds, clip.start, clip.end, params.getCompositionDuration());
291
- const relTime = mediaSourceTimeAt(clip, params.timeSeconds);
292
- const isHeldVideoTail = isTerminalVideo ||
293
- (isNonLoopVideo && clip.sourceDuration != null && relTime >= clip.sourceDuration && inWindow);
294
- const previousRelativeTime = lastRelativeTime.get(el);
295
- const audioReenteredAfterBackwardSeek = el.tagName === "AUDIO" &&
296
- (previousRelativeTime === undefined || relTime < previousRelativeTime - 0.04);
297
- const canSeekEndedMediaBackward = !clip.loop &&
298
- clip.sourceDuration != null &&
299
- relTime >= clip.mediaStart &&
300
- relTime < clip.sourceDuration &&
301
- (isNonLoopVideo || audioReenteredAfterBackwardSeek);
302
- // Ended media can re-enter playable source after a backward timeline seek
303
- // without depending on the browser having reset `ended` first. Audio needs
304
- // a fresh activation or a measured backward transport seek so ordinary EOF
305
- // and non-seek force-sync transitions cannot replay its tail. A non-loop
306
- // video additionally remains an active visual through
307
- // its authored window, with tail seeks clamped to the final frame.
308
- const isActive = (inWindow || isTerminalVideo || startsEarly) &&
309
- relTime >= 0 &&
310
- (!el.ended || clip.loop || isHeldVideoTail || canSeekEndedMediaBackward);
311
- if (isActive) {
312
- lastRelativeTime.set(el, relTime);
313
- const userVol = clampVolume(params.userVolume ?? 1);
314
- const fallbackAuthorVolume = clampAudioGain(clip.volume ?? 1);
315
- const previousRuntimeVolume = lastRuntimeAppliedVolume.get(el);
316
- const currentElementVolume = clampVolume(el.volume);
317
- let authorVolume;
318
- // An explicit volume lane, times `data-volume`, sets the level. Checked before the probed
319
- // keyframes because the two would otherwise fight, and it is the one the
320
- // author drew — `lint` warns when a track carries both.
321
- // Clip-local, NOT `relTime`. A lane's `t` is "seconds from the start of
322
- // the clip" (see HfAutomationPoint), and the render honours that: the wav
323
- // is already cut with `-ss mediaStart`, so its t=0 IS the clip's start.
324
- // `relTime` is MEDIA time — it carries mediaStart, scales by playbackRate
325
- // and wraps on a loop — so feeding it here played the envelope at a
326
- // different position than it renders, or ran it off the end entirely on a
327
- // trimmed clip. The FX lanes on this same feature use clip-local elapsed;
328
- // there is one time base, and this is it.
329
- const laneGain = elementVolumeLaneGain(el, params.timeSeconds - origin);
330
- const routed = params.isWebAudioRouted?.(el) === true;
331
- const tweened = laneGain === null && (clip.volumeKeyframes?.length ?? 0) > 0;
332
- const preFxGain = routed ? clipGain(fallbackAuthorVolume, tweened) : 1;
333
- if (laneGain !== null) {
334
- authorVolume = routed ? 1 : gainTimesLane(fallbackAuthorVolume, laneGain);
335
- }
336
- else if (clip.volumeKeyframes && clip.volumeKeyframes.length > 0) {
337
- // Keyframes probed from the GSAP timeline — same source as the renderer.
338
- // Use the interpolated envelope value directly; no need to track GSAP changes.
339
- // Index by elapsed time on the TIMELINE since the clip began, which is what
340
- // a normalised envelope is keyed by (and what the renderer's PCM baker uses).
341
- // `relTime` is a position inside the media SOURCE — it carries `mediaStart`
342
- // and the playback rate — so it only coincides with the envelope's time base
343
- // for an untrimmed clip playing at 1x from t=0.
344
- const elapsedInClip = params.timeSeconds - origin;
345
- authorVolume = clampAudioGain(interpolateVolumeGain(clip.volumeKeyframes, elapsedInClip));
346
- }
347
- else if (routed) {
348
- authorVolume = 1;
349
- }
350
- else if (previousRuntimeVolume === undefined) {
351
- // First tick this clip is active. The transport has already seeked GSAP
352
- // to the current time (seekTimelineAndAdapters runs before syncRuntimeMedia),
353
- // so el.volume reflects the animated value — trust it rather than falling
354
- // back to data-volume, which would clobber the GSAP-seeked position.
355
- //
356
- // Except above unity. `el.volume` is spec-bound to [0,1], so it cannot
357
- // represent an authored boost, and reading it back can only lose the
358
- // gain. Without this, a boosted clip opened at 0 dB for one tick and
359
- // then jumped once the unchanged-since-last-tick branch below took over
360
- // — audible, and invisible to any test that ticks more than once.
361
- authorVolume = fallbackAuthorVolume > 1 ? fallbackAuthorVolume : currentElementVolume;
362
- }
363
- else if (Math.abs(currentElementVolume - previousRuntimeVolume) > 0.0001) {
364
- // GSAP (or user code) changed el.volume between ticks — track it.
365
- //
366
- // Unity-capped on purpose, and it is not a hole in the ceiling: this
367
- // reads back through `el.volume`, which the spec pins to [0,1], so it
368
- // cannot observe an above-unity value however wide the clamp gets. A
369
- // clip whose volume is actually animated takes the probed-keyframes
370
- // branch above, which carries the authored gain unclamped; this branch
371
- // is the fallback for elements no probe ran on.
372
- authorVolume = currentElementVolume;
373
- }
374
- else {
375
- // Volume unchanged since last tick — use data-volume as the baseline.
376
- authorVolume = fallbackAuthorVolume;
377
- }
378
- // Clip-local fade on top of the resolved level, matching render's afade-after-volume.
379
- const unfadedVolume = authorVolume;
380
- const fades = clip.fades ?? NO_FADES;
381
- if (fades.fadeIn > 0 || fades.fadeOut > 0) {
382
- authorVolume *= fadeGain(params.timeSeconds - origin, clip.duration + clip.start - origin, fades);
383
- }
384
- // A data-hidden ancestor is silent in the export (audioMixer.ts drops
385
- // it), so preview matches. Folded into the per-tick volume, not
386
- // el.muted (RULES trap: el.muted is the transport's ownership flag).
387
- // Two independent ways to be silent, and the second is not an ancestor
388
- // question: membership lives on the MEMBER's `data-audio-group`, so a
389
- // muted BUS is invisible to `closest()`. The render drops such members
390
- // (`memberGroupHidden`), so without this the export was silent where the
391
- // fallback played at full level.
392
- const silencedByHidden = el.closest("[data-hidden]") !== null || isMemberGroupHidden(el.ownerDocument, el);
393
- const effectiveVolume = silencedByHidden ? 0 : clampVolume(authorVolume * userVol);
394
- if (Math.abs(el.volume - effectiveVolume) > 1e-6)
395
- el.volume = effectiveVolume;
396
- lastRuntimeAppliedVolume.set(el, effectiveVolume);
397
- if (unfadedVolume === authorVolume)
398
- params.onElementVolume?.(el, effectiveVolume, authorVolume, preFxGain);
399
- else
400
- params.onElementVolume?.(el, effectiveVolume, authorVolume, preFxGain, unfadedVolume);
401
- // Mute only when force-muted or the transport owns this element; an unclaimed
402
- // track stays audible via the HTMLMedia fallback.
403
- if ((forceMuteAll || params.isWebAudioOwned?.(el)) && !el.muted)
404
- el.muted = true;
405
- // Ensure full preload for every active media element. Streaming
406
- // formats (MP3) may arrive with preload="metadata", which only
407
- // buffers the first few seconds and causes seeks to silently fail
408
- // past the buffered range. Setting this on every tick is cheap
409
- // (no-op when already "auto") and catches elements whose preload
410
- // was overridden after init.ts set it.
411
- if (el.preload !== "auto" && !isRenderFrameVideo(el))
412
- el.preload = "auto";
413
- // Per-element rate × global transport rate
414
- const baseRate = rateAt(clipRate, params.timeSeconds - origin) * params.playbackRate;
415
- // Too slow for the browser to play: kept paused and stepped onto the playhead by seeks.
416
- const playing = params.playing && baseRate >= MIN_NATIVE_PLAYBACK_RATE;
417
- // Drift correction — three tiers:
418
- //
419
- // 1. Hard sync (0.5s): first tick, timeline jumps (scrub), catastrophic
420
- // drift (>3s). Unconditional seek — accepts brief rebuffer cost.
421
- // Forcing el.currentTime every frame causes audible seek hiccups
422
- // (readyState drops briefly), so we only hard-seek when necessary.
423
- //
424
- // 2. Strict sync (40ms, 2 consecutive samples): catches accumulated
425
- // drift from pause/play toggling or browser media pipeline latency.
426
- // Offset-stabilization guard (4ms/tick) prevents false corrections
427
- // during initial buffering where offset grows naturally.
428
- //
429
- // 3. Force sync (20ms): on play/pause/seek/rate transitions, correct
430
- // any drift >20ms immediately via the forceSync one-shot flag.
431
- //
432
- // The first tick a clip is active has no previous offset to compare —
433
- // treated as hard resync so sub-compositions with non-zero mediaStart
434
- // land on the right frame.
435
- const STRICT_REQUIRED_SAMPLES = 2;
436
- const currentElTime = el.currentTime || 0;
437
- const drift = Math.abs(currentElTime - relTime);
438
- const offset = relTime - currentElTime;
439
- const prevOffset = lastOffset.get(el);
440
- lastOffset.set(el, offset);
441
- const firstTickOfClip = prevOffset === undefined;
442
- const offsetJumped = !firstTickOfClip && Math.abs(offset - prevOffset) > 0.5;
443
- const catastrophicDrift = drift > 3;
444
- // A short audio clip can leave its native element paused just before EOF.
445
- // When the timeline re-enters the clip, rewind stale forward state at the
446
- // strict threshold; do not force cold audio forward while it buffers.
447
- const staleAudioOnFirstTick = el.tagName === "AUDIO" &&
448
- firstTickOfClip &&
449
- currentElTime - relTime > MEDIA_SYNC_TOLERANCE_SECONDS;
450
- const hardSync = (isHeldVideoTail && drift > 0.001) ||
451
- (el.ended && canSeekEndedMediaBackward && drift > 0.001) ||
452
- staleAudioOnFirstTick ||
453
- (drift > MEDIA_HARD_SYNC_SECONDS && (firstTickOfClip || offsetJumped || catastrophicDrift));
454
- // Playing videos use the browser's decoder for timing. Seeking one resets the decoder: a
455
- // ~150ms freeze while it re-buffers, as the monotonic clock advances, which loops into a
456
- // seek→freeze→drift→seek stutter. So a playing video skips strict and force sync; only hard
457
- // sync (>0.5s) warrants the decoder-reset cost. A paused transport pauses this video below,
458
- // so a seek that pauses mid-playback still lands it.
459
- const isPlayingVideo = el.tagName === "VIDEO" && !el.paused && playing;
460
- // Only apply strict sync when offset has stabilized (not growing).
461
- // During initial buffering, offset grows ~16ms/tick as the timeline
462
- // advances while media stays at 0. Accumulated drift from pause/play
463
- // toggling shows up as a stable, non-zero offset (delta near 0).
464
- const heldBelowFloor = params.playing && !playing;
465
- const offsetStabilized = heldBelowFloor || (prevOffset !== undefined && Math.abs(offset - prevOffset) < 0.004);
466
- let strictSync = false;
467
- if (!isPlayingVideo &&
468
- !hardSync &&
469
- !firstTickOfClip &&
470
- offsetStabilized &&
471
- drift > MEDIA_SYNC_TOLERANCE_SECONDS) {
472
- const samples = (strictDriftSamples.get(el) ?? 0) + 1;
473
- strictDriftSamples.set(el, samples);
474
- if (samples >= STRICT_REQUIRED_SAMPLES) {
475
- strictSync = true;
476
- strictDriftSamples.set(el, 0);
477
- }
478
- }
479
- else if (drift <= MEDIA_SYNC_TOLERANCE_SECONDS) {
480
- strictDriftSamples.set(el, 0);
481
- }
482
- const forceSync = !isPlayingVideo && params.forceSync && drift > 0.02;
483
- try {
484
- // A hard sync lands the video on the playhead, so its pre-seek offset says nothing.
485
- if (!isPlayingVideo || hardSync)
486
- videoSteering.delete(el);
487
- const rate = Math.max(MIN_NATIVE_PLAYBACK_RATE, isPlayingVideo && !hardSync ? steeredVideoRate(el, offset, baseRate) : baseRate);
488
- // Some engines read a rate back at lower precision; an equal-enough rate is not rewritten.
489
- if (Math.abs(el.playbackRate - rate) > 1e-6)
490
- el.playbackRate = rate;
491
- }
492
- catch (err) {
493
- // ignore unsupported playbackRate
494
- swallow("runtime.media.site1", err);
495
- }
496
- if (hardSync || strictSync || forceSync) {
497
- // Skip the per-tick seek (and the `el.load()` drift-recovery retry
498
- // below) for `<video>` elements that have a sibling
499
- // `<img id="__render_frame_<id>__">`. The sibling is created only
500
- // by the producer's frame-injection pipeline during render — its
501
- // presence means the visual is painted from the `<img>` and the
502
- // `<video>` is `visibility: hidden`. Audio is mixed by ffmpeg from
503
- // source files in `runAudioStage`, never via Chrome's in-browser
504
- // audio path. So the `<video>`'s `currentTime` has no observable
505
- // effect during render, and the per-tick set just kicks Chrome's
506
- // media pipeline for nothing. Preview is unaffected (the sibling
507
- // only exists during render).
508
- const skipForInjectedVideo = el.tagName === "VIDEO" && !!findInjectedRenderFrame(el);
509
- if (!skipForInjectedVideo) {
510
- try {
511
- el.currentTime = mediaElementTime(relTime);
512
- }
513
- catch (err) {
514
- swallow("runtime.media.site2", err);
515
- }
516
- if (Math.abs(el.currentTime - relTime) > 0.5 && !seekLoadRetried.has(el)) {
517
- seekLoadRetried.add(el);
518
- el.load();
519
- try {
520
- el.currentTime = mediaElementTime(relTime);
521
- }
522
- catch (err) {
523
- swallow("runtime.media.site3", err);
524
- }
525
- }
526
- holdSeekBarrierUntilVideoLands(el);
527
- }
528
- playRequested.delete(el);
529
- }
530
- else if (!playing) {
531
- holdSeekBarrierUntilVideoLands(el);
532
- }
533
- if (isHeldVideoTail) {
534
- if (!el.paused)
535
- el.pause();
536
- }
537
- else if (playing && el.paused && !playRequested.has(el) && !isUnplayable(el)) {
538
- // `HTMLMediaElement.play()` is spec'd to queue playback and resolve
539
- // once enough data is buffered, so we can unconditionally call it —
540
- // no need to gate on `readyState` or defer to a `canplay` listener.
541
- //
542
- // The old `readyState < HAVE_FUTURE_DATA` branch called `el.load()`
543
- // inside the listener, which *aborts* the in-flight fetch that
544
- // `bindMediaMetadataListeners` already started at init time and
545
- // restarts from zero. On slow networks this delayed playback by
546
- // seconds. The canplay listener was also racey — the event could
547
- // fire between `load()` and `addEventListener` attachment, wedging
548
- // the element waiting for a callback that never came.
549
- markPlayRequested(el);
550
- void el.play().catch((err) => {
551
- // If play() rejects — e.g. autoplay blocked, element removed
552
- // mid-flight — drop the in-flight flag so a future sync tick can
553
- // retry rather than getting stuck waiting for `playing`/`pause`.
554
- playRequested.delete(el);
555
- // `NotAllowedError` is the autoplay-gating browser response when
556
- // the iframe has no user activation. Signal the parent exactly
557
- // once so it can promote to parent-frame audio proxies. Retries
558
- // here would be pointless — nothing the runtime does fixes it.
559
- const name = err && typeof err === "object" && "name" in err
560
- ? String(err.name ?? "")
561
- : "";
562
- if (name === "NotAllowedError")
563
- params.onAutoplayBlocked?.();
564
- });
565
- }
566
- else if (!playing && !el.paused) {
567
- el.pause();
568
- }
569
- continue;
570
- }
571
- // Drop drift state when the element is not playable. If native audio EOF
572
- // arrived slightly before the authored boundary, preserve only its desired
573
- // source time while the transport remains inside that boundary. Otherwise
574
- // the next poll would mistake the cleared baseline for a fresh activation
575
- // and replay the tail. A real backward seek still decreases relTime, and a
576
- // true outside-window transition clears every baseline as before.
577
- const remainsInsideAuthoredWindow = isInClipWindow(params.timeSeconds, clip.start, clip.end);
578
- evictMediaSyncState(el);
579
- if (remainsInsideAuthoredWindow)
580
- lastRelativeTime.set(el, relTime);
581
- if (!el.paused)
582
- el.pause();
583
- prepareUpcomingVideo(clip, params.timeSeconds, params.playing, params.playbackRate);
584
- }
585
- }
@@ -1,12 +0,0 @@
1
- type PreloadableMedia = Pick<HTMLMediaElement, "id" | "tagName" | "getAttribute" | "preload" | "readyState" | "networkState" | "load">;
2
- export declare function isRenderFrameVideo(media: Pick<Element, "getAttribute" | "id" | "tagName">): boolean;
3
- export declare function preloadMedia(media: PreloadableMedia): void;
4
- export declare function prepareUpcomingMedia(media: HTMLMediaElement, isUpcoming: () => boolean): void;
5
- /** Ends a fetch in flight for good, which preload none alone does not. */
6
- export declare function stopMediaDownload(media: HTMLMediaElement): void;
7
- export declare function releaseMedia(media: HTMLMediaElement): void;
8
- export declare function lengthIsAuthored(media: Element): boolean;
9
- /** Preview only: later clips parse at preload none, as Chromium ignores a none set mid-fetch.
10
- * One watcher per page: a runtime evaluated again replaces the last one's. */
11
- export declare function deferMediaUntilDue(): void;
12
- export {};