Skip to content

fix(core): Safari audio frozen after its cut is pulled onto the playhead at 0.5 s behind, not 1 s - #7082

Merged
miguel-heygen merged 3 commits into
mainfrom
fix/safari-frozen-audio-play
Nov 3, 2026
Merged

miguel-heygen merged 3 commits into
mainfrom
fix/safari-frozen-audio-play

Conversation

@miguel-heygen

@miguel-heygen miguel-heygen commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

What

In Safari and on iPhone, a clip's audio that freezes at its in-point right after the cut is now pulled onto the playhead once it is frozen for 0.4 s and more than 0.5 s behind, instead of waiting until it is 1 s behind.

Why

On an iPhone, a pre-seeked audio element was started at a cut, reported playing, and then sat at its in-point for about 1 s until the 1 s valve from #7024 seeked it. That is a late sound of 600 ms or more at about 1 cut in 7 to 1 in 4 in the bad-phone runs.

Related work

Follows #7024 (WebKit audio in-point preparation and the 1 s valve) and #7012.

How

One place routes all media sync: syncRuntimeMedia in media.ts. A WebKit <audio> that is playing, not seeking, reports it can play (readyState 3 or more) and whose currentTime has not moved for 0.4 s of film time now counts as stuck and takes the existing hard sync, which still needs it to be more than 0.5 s behind (the 1 s lag valve stays). An element still filling data (readyState below 3) is waiting, not stuck, and is left alone; its state is dropped when the clip leaves its window. WebKit starts audio about 150 ms after play(), so 0.4 s leaves room for a slow start; a moving element is never touched.

Other arms measured on the phone and dropped: a fresh seek of the pre-seeked audio at the cut, and a silent warm-up play after the pre-seek. Both cost start time.

Test plan

Fails without the fix: with only media.ts reverted, the new frozen-audio test fails (1 failed, 181 passed in media.test.ts); with the fix the whole src/runtime suite gives 2756 passed, exit 0. Two more new tests: a moving WebKit element at the same lag is not seeked, and one still filling data is not seeked (it fails, with init.buffering.test.ts, when the readyState guard is removed).

Start cost on an iPhone, 30 plays per arm, 60 cuts each (hold / first sound / holds of 1 s or more):

Arm Hold First sound Holds >= 1 s
main 118 ms 335 ms 0
this change 119 ms 321 ms 0
fresh seek at the cut (dropped) 322 ms 322 ms 1 (max 1.2 s)
silent warm-up (dropped) 311 ms 312 ms 2 (max 2.4 s)
  • Unit tests added
  • Device: late sound (600 ms or more) was main 27 of 60 against 18 of 60 with this change in one run on a phone in a bad state. The latest run did not freeze at all (0 late on every arm), so the benefit is shown once and not yet re-checked.
  • Comments follow CONTRIBUTING.md

Size

A lone about 70-line fix: one condition in the single media sync path plus its tests; no other open PR touches it.

@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 267 (base branch 267), smooth 121 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Quarantined, measured but not gated (8)

@heygengenesis heygengenesis Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes at 9b1b6d08. CI is red, and the failure comes from this change.

Blocker: init.buffering.test.ts fails because of this change.
resumes for a held clip with data buffered ahead that reports only its current frame, as WebKit's do (line 148) fails in both Test (runtime) and Test: runtime contract: expected { isPlaying: true, buffering: true } to deeply equal { isPlaying: true, buffering: false }. Locally, with only media.ts reverted to main (d89bf35), it passes 3/3; at this head it fails 3/3.

That scenario is a WebKit <audio> at readyState 2 with data buffered ahead. The film resumes, but the element hasn't moved yet. The new frozen rule sees a playing, non-seeking element whose currentTime hasn't changed for 0.4 s of film time and counts it as catastrophicDrift. The resulting hard sync re-seeks an element that is waiting for data, not stuck, and the film drops back into buffering. This is the "do not force cold audio forward while it buffers" case. frozenAudioFor needs to leave alone any element that the buffering hold is waiting on or that is still filling, and still catch the post-cut freeze from the device runs.

What checks out:

  • Fails without the fix: confirmed. With media.ts reverted, pulls a WebKit audio element that sits frozen 0.4 s… fails (1 failed / 181 passed). At head, media.test.ts and preloadMedia.test.ts give 208 passed.
  • The moving-element test and the existing 1 s valve are intact.

Non-blocking notes for the next push:

  • Hard sync is still gated on drift > MEDIA_HARD_SYNC_SECONDS (0.5), so a frozen element is actually pulled once it is more than 0.5 s behind, at 0.6 s in the new test, not at 0.4 s. The title and description overstate this.
  • lastMoved isn't cleared when a clip deactivates, unlike lastOffset. On re-entry, an element that hasn't moved since the last pass inherits a stale at and can count as frozen immediately. This is minor because the 0.5 s drift gate still applies, but clearing it alongside lastOffset would keep it consistent.

@miguel-heygen miguel-heygen changed the title fix(core): Safari audio frozen after its cut is pulled onto the playhead in 0.4 s, not 1 s fix(core): Safari audio frozen after its cut is pulled onto the playhead at 0.5 s behind, not 1 s Nov 3, 2026

@heygengenesis heygengenesis Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved at 1db307e6. My earlier blocker is resolved.

  • Blocker fixed: frozenAudioFor now skips WebKit audio at readyState < 3, so audio that is still filling data is no longer re-seeked. init.buffering.test.ts passes again.
  • Eviction: lastMoved is now cleared in evictMediaSyncState, which runs on proxy swaps and when a clip leaves its window.
  • Verified locally at this head:
    • The whole src/runtime suite passes: 117 files, 2756 tests.
    • With only media.ts reverted to main, the frozen-audio test fails (1 failed / 182 passed), so the "fails without the fix" line holds.
    • With only the readyState guard removed, the new "still filling data" test and the init.buffering WebKit test both fail, as the description says.
  • CI: green at this head. One PR captures run was cancelled as a superseded duplicate; its rerun passed.
  • Description: now says correctly that the hard sync also needs the audio to be more than 0.5 s behind.

Non-blocking risk: the readyState >= 3 gate assumes the frozen-after-cut audio on the phone reports HAVE_FUTURE_DATA. init.buffering.test.ts notes that WebKit audio can report only its current frame (readyState 2) even with data buffered ahead. If the frozen elements on iPhone look like that, this rule won't fire on the device and the 1 s valve still handles them. That is safe, but the gain may be smaller than the 27→18 run suggests. When you re-check on the device (the open box), it's worth logging readyState on a frozen cut.

@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 3, 2026
Merged via the queue into main with commit 47ddded Nov 3, 2026
144 of 145 checks passed
@miguel-heygen
miguel-heygen deleted the fix/safari-frozen-audio-play branch November 3, 2026 11:34
@WaterrrForever WaterrrForever mentioned this pull request Nov 3, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant