Skip to content

Report presentation times on both the source and item axes (native path) - #263

Merged
superuser404notfound merged 1 commit into
mainfrom
feat/issue260-frame-axis
Jul 30, 2026
Merged

superuser404notfound merged 1 commit into
mainfrom
feat/issue260-frame-axis

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

Closes the API gap in #260.

What a host could not do before

Cue times, chapters and sourceTime are source PTS. AVPlayerItem.currentTime() and its timebase read what the producer muxes into the segments. The two differ by the producer shift, and nothing published closed that gap exactly:

Surface Why it falls short
playlistShiftSeconds A scalar. Correct only until the next producer epoch
clock.sourceTime ~10 Hz, and a clock reading is not a frame boundary
Segment.startPts / startSeconds A genuine pair, about six seconds granular
a53CaptionObserver Per frame, but one axis

Two pieces

PresentationAxisMap models the relation as what it actually is, a step function of item time, and replaces the seam array the live path already carried. sourceSeconds(forItemSeconds:) and itemSeconds(forSourceSeconds:) convert at any position; the map is readable off the main actor.

A VOD restart now records a seam at the item-axis position the new producer starts writing from, instead of collapsing the history to one entry. Content below that position was muxed under the previous shift and can still be in AVPlayer's buffer, so it has to keep folding with that shift. A backward restart drops only the seams it rewrites. Live program boundaries are unchanged. (The #65 shift-fold hypotheses were refuted by measurement at the time; that burst ran with an invariant shift, so this is not a retroactive explanation for it.)

setNativeVideoFrameTimeObserver reports NativeVideoFrameTime per muxed frame: both axes, segment index, keyframe flag, and a producer epoch so a consumer can drop entries a restart has rewritten.

Two things the issue's proposed shape would have hit

  • Emitting after writePacket reads a blank packet. av_interleaved_write_frame takes ownership and the packet comes back "as if returned from av_packet_alloc(), even on error", so pts is AV_NOPTS_VALUE there. The written value now comes back out of writePacket. It also matters that it is that value and not a rescale of the source one: the final-stage sanitizer moves timestamps on SSAI splices.
  • Delivery is decode order, so source is not monotonic under B-frames. Documented rather than sorted, since sorting would mean buffering inside the pump.

Both surfaces return nil when no axis has been established, rather than defaulting the shift to zero: at the call site a defaulted shift is indistinguishable from a measured one, which is what #259 cost.

currentAVPlayerItem publishes alongside, since items are swapped in place and a host holding a timebase gets no signal from currentAVPlayer alone.

Verification

  • 1340 tests green, including the new PresentationAxisMapTests (step behaviour, backward-restart truncation, history-cap re-anchoring) and Issue260FrameTimeAxisTests against Fixtures/restart-witness-av.mp4.
  • The frame-time test requires a non-zero shift before asserting anything, so a clip where the axes coincide fails the witness instead of passing vacuously. On that fixture the axes are two frames apart (0.083333 s at 24 fps), which is also why the first muxed frame sits at item time +0.083333 rather than 0: the tfdt anchors on the DTS while the reported time is the PTS.
  • Clean -strict-concurrency=complete build, tvOS Simulator build clean.

Requested by @edde746, who measured the axis gap and offered a PR; the shape here differs in the two points above.

🤖 Generated with Claude Code

https://claude.ai/code/session_01HX5zV3Fcf7Nzq4DYGdXvQE

…260)

Cue times, chapters and sourceTime are source PTS; AVPlayerItem's timebase
reads what the producer muxes. The two differ by the producer shift, and a
host compositing its own overlay had no exact way to cross between them:
playlistShiftSeconds is a scalar that only holds until the next producer
epoch, the clock ticks at ~10 Hz and a clock reading is not a frame
boundary, and the per-segment pair is six seconds granular.

PresentationAxisMap models the relation as what it is, a step function of
item time, and replaces the seam array the live path already carried. A VOD
restart now records a seam at the item position the new producer starts
writing from, instead of collapsing the history onto one entry: content
below it was muxed under the previous shift and can still be in AVPlayer's
buffer. A backward restart drops only the seams it actually rewrites. Live
program boundaries are unchanged. Note the #65 shift-fold hypotheses were
refuted by measurement (that burst ran with an invariant shift), so this is
not a retroactive explanation for it.

setNativeVideoFrameTimeObserver reports both axes per muxed frame, plus
segment index, keyframe flag and producer epoch. The item value comes back
out of writePacket rather than off the packet: av_interleaved_write_frame
blanks it ("as if returned from av_packet_alloc(), even on error"), and the
final-stage sanitizer can move the timestamp, so a rescale of the source
value is not the same number. Delivery is decode order, so source is not
monotonic under B-frames; documented rather than sorted, since sorting would
mean buffering in the pump.

Both surfaces return nil when no axis is established. A defaulted shift is
indistinguishable from a measured one at the call site, which is what #259
cost. currentAVPlayerItem publishes alongside, because items swap in place
and a host holding a timebase otherwise gets no signal.

Requested by edde746 for frame-accurate libass rendering.

Verify: 1340 tests green, strict-concurrency clean build, tvOS Simulator
build clean. The frame-time tests run against Fixtures/restart-witness-av.mp4,
whose two-frame reorder makes the axes distinguishable (a zero shift would
make the witness vacuous, so the test requires a non-zero one).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HX5zV3Fcf7Nzq4DYGdXvQE
@superuser404notfound
superuser404notfound merged commit a69e6f9 into main Jul 30, 2026
5 of 6 checks passed
@superuser404notfound
superuser404notfound deleted the feat/issue260-frame-axis branch July 30, 2026 18:58
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