@hyperframes/core 0.8.207 → 0.8.209

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