Skip to content

feat(native): serve VOD loopback segments while they are written - #721

Merged
superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
yipengfei329:feat/progressive-segment-delivery
Oct 8, 2026
Merged

superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
yipengfei329:feat/progressive-segment-delivery

Conversation

@yipengfei329

Copy link
Copy Markdown
Contributor

Summary

AVPlayer cannot use a loopback segment until it has the whole of it, and the producer only hands a segment over at its cut. On a slow link that is a whole segment at the link's rate before the first frame, and again after every seek outside the buffer. This adds an opt-in LoadOptions.progressiveSegmentDelivery that serves each VOD segment while it is being written. Off by default, so no host's behaviour changes. This is the progressive-delivery piece of #703 (MovieClaw P57) that you named as a candidate.

What changed

  • Producer / muxer. With the option on, the producer registers each VOD segment's staging file with the cache as it opens it (SegmentCache.beginInProgress) and the muxer flushes a fragment every 0.5 s instead of holding up to 8 s. A failed cut, a failed final finalize and a discarded muxer abandon the segment (abandonInProgress). MP4SegmentMuxer.stagingURL exposes the current staging file.
  • Cache. fetchSource(index:timeout:progressive:) answers a segment being written with a ProgressiveSegmentReader over its staging file. The muxer only appends to that file and adoption renames it as it stands, so an open descriptor reads a growing prefix of the final segment and the rename does not disturb it. The reader ends at the byte count recorded for the adoption, and reports .abandoned when the producer gives the segment up or writes nothing for 60 s. The cache remembers which staging file each recent adoption came from, so a reader never takes the same index produced again by a later epoch for its own.
  • Server. HLSSegmentProvider.mediaSegmentSource(at:onSlow:) (defaulted from mediaSegment) lets the provider return that reader; HLSLocalServer sends each read as a chunk over the existing chunked response and the final chunk at the seal. An abandoned segment closes the connection without the final chunk, so AVPlayer retries the segment instead of taking a partial body for a whole one.
  • Presented frame drifts ~6 s ahead of currentTime/sourceTime after a rapid bidirectional seek burst (loopback-HLS native path) — frameAhead/producerShift/hostShift all report 0 #65 wedge watchdog. AVPlayer receiving a segment over one long connection sends no new request, and the watchdog counts requests. Each chunk the server sends now counts as fetch activity (didDeliverProgressiveChunk); a player that really stops reading fills the socket, and the count stops.
  • Scope. VOD only: live keeps its window and blocking-reload contracts. Not applied on nativeRemoteHLS or the software path, which serve no loopback segments. The option is a tuning field for reloadAtCurrentPosition(applying:).
  • aetherctl play --progressive-segments, documented in docs/cli.md; the option is documented in docs/api.md.

Test plan

  • Device / OS: MacBook Pro (Mac14,5, M2 Max), macOS 27.0.1, Xcode 27.0 (27A266a).

  • Source media: 1080p HEVC Main MKV, 2.7 Mbit/s, keyframes every 3-9 s, served over HTTP with byte ranges from a local origin paced to 6 Mbit/s (738 KB/s measured with curl).

  • aetherctl play --seconds 30, three runs each:

    first frame PHASE playing position at 30 s non-playing ticks
    off 2.20 / 2.26 / 2.21 s 2.24 / 2.30 / 2.25 s 26.5-27.4 s 3-4
    --progressive-segments 0.91 / 0.91 / 0.80 s 1.74 / 1.61 / 1.65 s 29.9 s 1
  • Fragment cadence: at 1 s fragments the same runs played after 1.90 / 1.90 / 1.87 s, so 0.5 s was kept.

  • Seeks on the same origin (--seek-every 8 --seek-count 3 --seek-pattern 1800,1790,3600): landings 561 / 6512 / 1265 ms with the option against 1199 / 6992 / 2253 ms without; no wedge declared, verdict OK in both.

  • Local file (H.264 4:2:2 10-bit MKV) with the option: same first frame (0.20 s vs 0.19 s) and seek landing (69 ms vs 70 ms) as without, and no segment was served while written, as expected when the producer is ahead of AVPlayer.

  • A 25.5 Mbit/s 4K title over a link well below its rate never showed a frame within 40 s without the option and showed one at 3.5 s with it; it cannot play through there either way.

  • New ProgressiveSegmentDeliveryTests (8): the reader follows a growing staging file to the seal, survives the adoption rename, reports an abandoned segment and an idle producer; the option off waits for the cut; over real sockets the server sends the head before the cut and the complete chunked body at the seal, and ends an abandoned segment without the final chunk; the option defaults off and is a tuning field.

  • Full swift test --jobs 8: 718 XCTest cases (1 skipped) and 4,092 Swift Testing cases in 568 suites, all passing. Scripts/check-doc-links.py passes.

Checklist

  • CHANGELOG.md updated
  • Commit messages follow Conventional Commits (feat(...), fix(...), chore(...))
  • The fix lives in the engine, not in a host-side workaround
  • Public API changes are intentional and documented (LoadOptions.progressiveSegmentDelivery)

🤖 Generated with Claude Code

@yipengfei329
yipengfei329 marked this pull request as ready for review October 7, 2026 14:13
@yipengfei329
yipengfei329 force-pushed the feat/progressive-segment-delivery branch from 767c5af to 44eacba Compare October 7, 2026 17:07
LoadOptions.progressiveSegmentDelivery (off by default) lets AVPlayer use a
segment's first fragments instead of waiting for its cut, which on a slow
link is the time to first frame.

- The producer registers each VOD segment's staging file with the cache as
  it opens it, and the muxer flushes a fragment every 0.5 s instead of
  holding up to 8 s.
- SegmentCache.fetchSource answers a segment being written with a
  ProgressiveSegmentReader over its staging file; the file is only appended
  to and renamed as it stands on adoption, so the reader sees a growing
  prefix of the final segment.
- HLSLocalServer sends each read as a chunk and ends at the seal; an
  abandoned segment closes the connection without the final chunk so
  AVPlayer asks for it again.
- Each chunk sent counts as fetch activity for the superuser404notfound#65 wedge watchdog,
  which otherwise reads one long transfer as AVPlayer no longer fetching.
- aetherctl play --progressive-segments drives the option.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@yipengfei329
yipengfei329 force-pushed the feat/progressive-segment-delivery branch from 44eacba to e49f8c5 Compare October 7, 2026 17:14
@superuser404notfound
superuser404notfound merged commit 6263d42 into superuser404notfound:main Oct 8, 2026
4 checks passed
superuser404notfound pushed a commit that referenced this pull request Oct 8, 2026
…hat races their stat (#721)

The cache remembered sealed staging files keyed by segment index and evicted
the lowest indices first, so once 64 segments were adopted every later
adoption below them evicted itself at once: after a backward seek each
progressive reader read its own seal as abandoned, closed without the final
chunk, and AVPlayer fetched every segment a second time. The memory is now
keyed by staging file and evicted in adoption order.

The reader judged a seal against the stat it took before asking for the
state, so a last fragment written together with the seal in between read as
abandoned. It stats again before giving up.

Also moves sendChunkedBody's doc comment back onto it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WJcXkGQrMiBPixVEFTSBJS
@superuser404notfound

Copy link
Copy Markdown
Owner

Thanks a lot for this, merged as 6263d42. It is exactly the focused, opt-in shape we discussed in #703, and the measurements in the test plan made it easy to review. I merged current main in on my side (only CHANGELOG conflicted) and the full suite passed on the result.

Two things I found in review and fixed right after in 2c2b892:

  • sealedFromStaging evicted the lowest indices first. Once 64 segments had been adopted, every later adoption below them evicted itself immediately, so after a backward seek each progressive reader read its own seal as .abandoned, closed without the final chunk, and AVPlayer fetched every segment twice. The memory is now keyed by staging file and evicted in adoption order, with a regression test in ProgressiveSegmentDeliveryTests.
  • In ProgressiveSegmentReader.next() the .sealed branch compared against the fstat taken before the state lookup, so a last fragment written together with the seal in between read as abandoned. It now stats again before giving up.

Not released yet; it goes out with the next engine release.

@superuser404notfound

Copy link
Copy Markdown
Owner

Released in 7.32.0 (4b317b7).

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.

2 participants