Skip to content

Producer epoch restarts per load, so a host cannot tell one session's frame times from the next's #314

Closed

Description

@edde746

What should the engine do?

Make the producer epoch reported in NativeVideoFrameTime strictly increasing across load() calls, not just within one session — or carry a session identity alongside it. Today the counter is per HLSVideoEngine instance, so each load restarts it at 1 and a host cannot tell the outgoing session's frames from the incoming one's.

Motivating media or use case

Any in-place source switch during playback — audio track, subtitle track, quality or version — on a host that consumes setNativeVideoFrameTimeObserver to build a source→item lookup for frame-exact overlay rendering (the use case #260 added the API for).

The epoch is documented as the signal that retires stale entries: "a new epoch retires every earlier entry". That works within a session. Across a load it inverts, and the host has no way to compensate:

  • AetherEngine.load builds a new HLSVideoEngine (AetherEngine+Loading.swift:402), whose producerEpochCounter starts at 0 (Video/HLSVideoEngine.swift:164), so its first producer is epoch 1.
  • The observer is stored on AetherEngine and pushed to whichever session is current (AetherEngine+FrameTimes.swift), and is documented to outlive a load(). The outgoing session's producer keeps reporting until load stops it, and the session's own slot is never cleared.
  • So a host that retires its table at the start of a load can be handed a frame from the outgoing producer at epoch N, adopt N as newest, and then reject every frame of the new session (epochs 1, 2, …) as stale for the rest of the item.

epoch is the only discriminator available: source and item both restart near zero on a new item, and segmentIndex is per session too. A host-side fence is not constructible from it. Ordering by epoch cannot work when the values are drawn from two different counters, and clearing the observer around the load loses the new session's own early reports instead — on VOD those ranges are only produced once, so the host's lookup ends up with a hole at the start of the item rather than a wrong mapping.

Area

Public API surface

Would you be willing to open a PR?

Maybe, with guidance


Smallest sufficient change

Draw the counter process-wide rather than per instance — producerEpochCounter becomes static behind the existing producerEpochLock (Video/HLSVideoEngine.swift:163-170):

private static var producerEpochCounter: UInt64 = 0

private func nextProducerEpoch() -> UInt64 {
    Self.producerEpochLock.lock(); defer { Self.producerEpochLock.unlock() }
    Self.producerEpochCounter &+= 1
    return Self.producerEpochCounter
}

That alone makes the documented contract hold end to end: a straggler from a superseded producer is always lower than anything the next session reports, so the existing "higher epoch retires, lower epoch is dropped" rule becomes correct across loads as well as within one. No API change, no new field, and hosts that only compare epochs relatively need no changes at all.

A session id on NativeVideoFrameTime would also work and is more explicit, at the cost of a public struct change.

Scope

Native (loopback HLS) path only, which is the only path that reports these times.

Happy to test a branch.

Activity

  1. edde746 commented on Aug 6, 2026

    @edde746
    Author

    Correction to the snippet above: making only the counter static is not sufficient, and as written it would not compile.

    producerEpochLock is an instance property, so a process-wide counter has to come with a process-wide allocator — otherwise two overlapping sessions (the outgoing one still unwinding while the next brings up its first producer) mutate shared state under two different locks.

    private static let producerEpochLock = NSLock()
    private static var producerEpochCounter: UInt64 = 0
    
    private func nextProducerEpoch() -> UInt64 {
        Self.producerEpochLock.lock(); defer { Self.producerEpochLock.unlock() }
        Self.producerEpochCounter &+= 1
        return Self.producerEpochCounter
    }

    Worth being explicit about what that does and does not buy, too. It removes the permanent failure — a straggler's epoch can no longer sit above everything the next session will ever report, so the new session's first report always supersedes it. It is not a strict session fence: a straggler can still land in a host's freshly reset table and be dropped a moment later by the new producer's first report. For a host doing frame-exact overlay work that transient is harmless, since a lookup miss is already defined as "not yet known, retry".

    If strict isolation is preferred over the smaller change, a session identifier on NativeVideoFrameTime gives it directly, at the cost of a public struct change.

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

    @superuser404notfound
    Owner

    Fixed on main in 157ed97 (PR #327), unreleased so far.

    The reading holds, including the correction: a process-wide counter needs a process-wide allocator, since the outgoing session and the incoming one overlap while the first unwinds. Both draws now go through one FrameTimeSequence with its own lock.

    The software path carried the same defect against the same contract, so it went in the same pass. SoftwareVideoFrameTime.generation came from a per-renderer counter, and a load builds a new SampleBufferRenderer just as it builds a new HLSVideoEngine. There the initial value matters as much as the flush value: a renderer starting at zero would come up level with, or below, the one it replaces, so the generation is drawn at init too.

    No public struct change. A session id would have been the more explicit shape, but the only thing a consumer could not construct was the ordering, and the ordering is what the allocator restores. Both doc comments now state the guarantee and its limit: strictly increasing process-wide, across load() as well as within a session, and not consecutive, so order the values rather than counting on them.

    On the point that this is not a strict fence: stopInternal also detaches the outgoing session and the outgoing software host from the observer before stopping them. That does not replace the ordering guarantee, since a pump can have snapshotted the observer already, it only makes the ordinary case silence instead of a correctly-sorted straggler. The transient you describe stays possible in principle and stays harmless for a lookup whose miss already means "not known yet, retry".

    Witnesses in Issue314FrameTimeSequenceTests: the allocator under 8 concurrent drawers, a new renderer outranking the one it replaces (with the straggler ranking below the new item's first report), and two HLSVideoEngine instances over the witness clip as the two sides of a load. The last one fails on the old code with epoch 1 against epoch 1, which is the single value ordering cannot separate. A restart inside the new session still opens above its own first epoch, so #260's within-session behaviour is unchanged.

    The offer to test a branch is appreciated. It is on main now rather than a branch; if you want to confirm the source switch you were building for before the next release goes out, that is the commit to pin.

  4. edde746 commented on Aug 8, 2026

    @edde746
    Author

    Confirmed on 6.15.2 (fix present since 6.12.1). The source-switch case this was filed for
    works: the outgoing session's straggler now always ranks below the incoming session's first
    report, so the host table retires cleanly instead of adopting the straggler as newest and
    rejecting the whole new session. The transient you both predicted — a straggler landing in
    a freshly reset table and being retired a beat later — behaves as expected and is harmless
    for a lookup whose miss means "retry".

    One thing the process-wide sequence bought us that the issue didn't ask for: the software
    path's generation and the native path's epoch now share a single retire rule inside one
    shared host-side table (we wired #311 this week — the two observers write to the same
    grid). That's only sound because both counters draw from one allocator; our suite pins the
    cross-kind retire in both directions. So the #314 guarantee is now load-bearing across
    routes here, not just across loads. Close away.

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