Skip to content

Software-decode path: expose the render synchronizer's timebase, and deliver NativeVideoFrameTimeObserver there too #311

Open

Description

@edde746

What should the engine do?

On the software (VideoToolbox) path, expose the master clock's timebase as public read-only state, and deliver the existing NativeVideoFrameTimeObserver callback there too. Both values already exist inside the engine; neither is reachable from outside the module.

Motivating media or use case

Presenting a frame-exact bitmap overlay — libass (ASS/SSA) subtitles — over software-decoded video. That needs two things: the master clock's timebase to pace the overlay layer against, and the presentation time of each decoded frame to know where the frame boundaries are.

This already works on the native HLS-remux path, where a host joins the AVPlayerItem timebase with a timed AVSampleBufferDisplayLayer and stamps each overlay sample using the per-frame (source, item) pairs from setNativeVideoFrameTimeObserver (the API that landed for #260). A presenter written against a bare CMTimebase and any CALayer needs no AVPlayer and would run unchanged on the software path given the two items below.

Both asks are additive and opt-in: nothing changes for hosts that never install an observer or read the timebase.

Audited against 6.5.6, pinned revision f74dd2412849.

Area

Public API surface

Would you be willing to open a PR?

Maybe, with guidance


Request 1 — a host-reachable timebase

The software path's master clock is AudioOutput.synchronizer, and Native/SoftwarePlaybackHost.swift:509 documents that AudioOutput owns it as the master clock and is created unconditionally. So the clock is always there — but doubly encapsulated. AudioOutput carries no access modifier, i.e. internal to the module, and neither does the property (Audio/AudioOutput.swift:7-10):

final class AudioOutput: @unchecked Sendable {
    let renderer: AVSampleBufferAudioRenderer
    let synchronizer: AVSampleBufferRenderSynchronizer
    // …
}

The instance is then private var audioOutput: AudioOutput? (Native/SoftwarePlaybackHost.swift:85). A host cannot reach it by any route.

Ask: expose the synchronizer — or just its timebase — as a public read-only property on the software path.

Request 2 — per-frame presentation times

setNativeVideoFrameTimeObserver is already public (AetherEngine+FrameTimes.swift:14-16), but it forwards only to nativeVideoSession, i.e. HLSVideoEngine, which hands it to the remux producer (Video/HLSVideoEngine.swift:1953-1954, wiring prod.nativeVideoFrameTimeObserverProvider). The software path has no producer, so the observer never fires there.

Ask: deliver the same existing NativeVideoFrameTimeObserver callback on the software path.

This should be cheap: here the engine decodes and enqueues every frame itself, and already holds the PTS at the enqueue site (Renderer/SampleBufferRenderer.swift:294-297):

var timing = CMSampleTimingInfo(
    duration: .invalid,
    presentationTimeStamp: pts,
    decodeTimeStamp: .invalid
)

The data already exists at one known point; nothing new has to be computed or tracked.

It is also simpler here than on the remux path. Because the engine enqueues the source PTS unchanged, source and presentation time coincide by construction — as the engine's own doc comment says (AetherEngine+FrameTimes.swift:8-9): "The software and audio paths present what they decode, with no shift to reconcile." So no axis/shift mapping is needed. The (source, item) pair exists on the remux path precisely because remuxing changes the axis; here a plain "frame at PTS X was enqueued" callback suffices, and NativeVideoFrameTime's segmentIndex/epoch have no meaning.

Both parts are required

Neither alone unblocks anything: without the timebase there is nothing to pace an overlay layer against, and without the frame times there is no way to know where the frame boundaries are.

Frame boundaries specifically, not cue times — an approximate clock is not enough. The presenter emits one overlay per real video frame. It cannot assume a frame rate, because variable-frame-rate content has none; and ASS animation (\t, \move, karaoke) changes within a cue, so scheduling only at cue boundaries would land between frames and be exact for none of it.

Scope

Software path only. The remote-HLS bypass has the same symptom for a different and harder reason — AVPlayer owns decode there, so the engine never sees a frame and has nothing to report. That one is explicitly not part of this request.

Happy to test a branch.

Activity

  1. added a commit that references this issue on Aug 7, 2026
  2. superuser404notfound commented on Aug 7, 2026

    @superuser404notfound
    Owner

    Both halves shipped in 6.10.0. The report did the work here: file and line references for both asks, the axis argument spelled out, and the scope boundary drawn at the remote-HLS bypass before anyone had to ask about it.

    player.softwarePresentationTimebase               // CMTimebase?, source axis
    player.setSoftwareVideoFrameTimeObserver { frame in
        frame.presentation                            // CMTime, source axis
        frame.generation                              // UInt64, moves on every renderer flush
    }

    One deviation from the request, deliberate. The frame times come back as a new SoftwareVideoFrameTime rather than through NativeVideoFrameTimeObserver. The reasoning is the one already in the report: three of that type's five fields have no meaning on this path. Filling them with placeholders puts a value where a consumer cannot tell a placeholder from a measurement, and segmentIndex: -1 would still typecheck at every call site that reads it.

    generation is not a placeholder either, it keeps the job epoch actually does. Entries recorded under an older generation describe frames the compositor has thrown away, which is what a seek does here. That is worth having rather than dropping: on a backward seek the same presentation time comes round again, so a table keyed on timestamps alone cannot separate the two runs.

    Where the reports come from. The handover to the compositor, not the admission into the reorder buffer. A frame refused for an unschedulable timestamp, skipped after a seek, or lost to a failed sample-buffer creation never reaches that point and is never reported, so every boundary reported is one the compositor was actually given. Being past the reorder buffer has a second consequence worth planning around: unlike the native path's decode-order stream, these arrive in ascending presentation order, so the sort step is unnecessary here.

    Timebase caveat. CMTimebase is mutable and Core Media cannot enforce otherwise, but the engine owns transport on this path. Setting rate or time on it fights play(), pause() and seek(to:) and will desynchronise audio from video.

    Verified against real media rather than only unit tests. aetherctl play --frame-times installs the observer before load(), which is the documented usage since the engine re-arms each new host with it, and reads the timebase alongside. VP9 over a rate-limited origin, seek at t=12:

      timebase: present, time=0.000s rate=0.00
      t=12 ... ft=30 ftLast=12.200s ftGen=0 ooo=0 tb=12.153s
      SEEKLANDED target=6.01 in 2ms
      t=13 ... ft=32 ftLast=7.067s  ftGen=1 ooo=0 tb=7.015s
    frame times: frames=490 outOfOrder=0 generations=[0, 1]
    

    ooo counts reports arriving out of presentation order, so the ordering guarantee above is measured over 490 frames rather than asserted. The roughly 50 ms between ftLast and the timebase reading is the decode cushion, and its size is the practical argument for the two being on one axis.

    Leaving this open for the retest. The interesting confirmation is an ASS animation (\t, \move, karaoke) landing frame-exactly on variable-frame-rate content, since that is the case cue-boundary scheduling cannot reach.

  3. edde746 commented on Aug 8, 2026

    @edde746
    Author

    Both halves are wired into the host as of today, against 6.15.2.

    Shape of the adoption, since two of your design calls did real work here:

    • The frame times land in the same table the native (source, item) pairs do, recorded as
      source == item — taken from the contract you stated ("presents what it decodes, no
      shift to reconcile") rather than guessed, which was the line we didn't want to cross.
    • generation stands in for the producer epoch under the table's existing retire rule.
      That is only sound because of Producer epoch restarts per load, so a host cannot tell one session's frame times from the next's #314's process-wide sequence, and it fell out for free —
      including across a native↔software route change within one player, which we pin in tests
      both directions.
    • softwarePresentationTimebase is joined read-only as the overlay layer's
      controlTimebase (mirroring what our native path does with the item timebase); nothing
      host-side ever sets rate or time on it.
    • Ascending presentation order made the software feed the easy case for a table built for
      decode order.

    One gap surfaced during adoption, flagging rather than filing for now: the engine publishes
    no display size or sane SAR for the software path, and a sample-buffer layer measures no
    videoRect, so the host derives the overlay rect from sourceVideoWidth/Height under the
    active gravity — anamorphic content lays out against a rect off by the pixel aspect. If
    the settled per-load display size (post-PixelAspectPolicy) were public, the rect would be
    exact. Happy to file that separately if you agree it's engine-side.

    Still owed: the retest you actually asked for — ASS animation (\t, \move, karaoke)
    landing frame-exactly on VFR content over a forced-software session. We have the capture
    harness from the native-path work; I'll attach frame captures when it's run.

  4. superuser404notfound commented on Aug 9, 2026

    @superuser404notfound
    Owner

    Engine-side, agreed, and shipped in 6.17.0 as #353. The primitive named in the report is the one that landed: the settled per-load size, not a ratio to re-derive.

    player.softwareDisplaySize          // CGSize?, @Published

    nil off the software path, before the first frame is built, and on sources with no video. It follows a mid-stream format change and is cleared with the session, so a live source that switches resolution re-shapes the rect under a host that already laid out against it, and the next source is never laid out against the previous one's picture.

    Read rather than recomputed. CMVideoFormatDescriptionGetPresentationDimensions on the format description the renderer enqueues, which is the object the layer is handed. Recomputing the same answer from the SAR would have been a second thing that can disagree with the screen, and the disagreement would have been systematic rather than random: the ratio is resolved per frame across three sources (#177) and one whose display aspect is impossible is dropped in favour of square pixels (#290), so any reconstruction outside the decoder is wrong in exactly the cases those gates exist for. The cache that description lives behind already invalidates on a dimension or PAR change, so the republish moment came for free.

    Optional rather than .zero, for the reason SoftwareVideoFrameTime has its own fields: a placeholder in a non-optional field is indistinguishable from a measurement at the call site.

    Two defects surfaced while measuring it, and the second one is worth knowing about for playback rather than layout:

    Measured with aetherctl play --sw --frame-times, which now reports disp=WxH per tick, one synthetic clip encoded four ways:

    Source Decoder Before After
    720x576 SAR 64:45 VT (hevc) 720x576 1024x576
    720x576 SAR 64:45 libavcodec (h264) 1024x576 1024x576
    1280x720 square VT (hevc) 1280x720 1280x720
    1280x720 square libavcodec (h264) 1280x720 1280x720

    Clean aperture is honoured in the same read, though nothing on this path attaches one today.

    This issue stays open for the retest it was left open for: ASS animation (\t, \move, karaoke) landing frame-exactly on VFR content over a forced-software session. The adoption notes above answered something else that was worth having, which is that generation held up as a producer epoch across a native to software route change within one player. That was the part with the least evidence behind it when it shipped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions