Skip to content

fix(hls): remux finite HEVC MPEG-TS VOD - #269

Merged
superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
qoli:agent/hevc-hls-vod-remux
Jul 31, 2026
Merged

superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
qoli:agent/hevc-hls-vod-remux

Conversation

@qoli

@qoli qoli commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Fixes #268.

Problem

AVPlayer can mount finite HEVC-in-MPEG-TS HLS as audio-only / black video. This is a long-standing HEVC HLS playback-path problem, not a device-generation-specific issue.

The existing HLS reroute sends VOD back to native remote HLS. That is correct for ordinary HLS, but preserves the black-screen path for this carriage. Reusing the live ingest is also not suitable because its rolling-window contract breaks VOD seek semantics.

Change

  • classify the source from playlist and segment content before choosing the native route
  • require #EXT-X-ENDLIST, MPEG-TS carriage, and PMT stream type 0x24 (HEVC)
  • accept direct media playlists and the highest-bandwidth muxed master variant
  • feed confirmed inputs through a bounded, ordered TS ingest
  • expose duration and restart the finite playlist at the segment preceding a time seek
  • preserve HTTP headers, resume position, forward/backward seek, and the existing demux/decoder/remux path
  • leave H.264, fMP4, live, audio-only, encrypted/inconclusive, and unsupported playlist shapes on their existing route

This is content-gated; it does not infer codecs from the .m3u8 suffix or retry from an AVPlayer error string.

Validation

  • swift test --filter Issue268HLSVODIngestTests — 4 passed
  • swift build -Xswiftc -strict-concurrency=complete — passed
  • xcodebuild -scheme AetherEngine -destination 'generic/platform=tvOS' build CODE_SIGNING_ALLOWED=NO — passed
  • swift test --skip-build --skip Issue220SoftwareDecoderDrainTests — 1361 tests in 206 suites passed

A full unfiltered run has one failure in Issue220SoftwareDecoderDrainTests (expected drain counter >= 24, observed 17). The same failure reproduces in a clean worktree at upstream 83dca0b, so it is not introduced by this branch.

The same reader/demux seam was physically validated in a downstream integration with finite duration, forward seek, post-seek progress, and 2x playback. This PR reconstructs that workaround against current upstream main; direct physical-device acceptance of this exact branch remains pending, hence the draft status.

@superuser404notfound
superuser404notfound marked this pull request as ready for review July 31, 2026 16:21
@superuser404notfound
superuser404notfound merged commit 296e040 into superuser404notfound:main Jul 31, 2026
superuser404notfound pushed a commit that referenced this pull request Jul 31, 2026
Hardening on top of the HEVC-in-MPEG-TS VOD ingest from PR #269.

The reader addresses the EXTINF sum, but every engine seek target is an ABSOLUTE
source PTS, and MPEG-TS carries an arbitrary PTS origin: ffmpeg writes 1.4 s by
default, broadcast-derived VOD routinely starts hours in. Handing the raw target
to the reader therefore mapped a seek to item second 5 of a 1000 s-origin source
onto the LAST segment of its playlist, the producer restarted on content 580 s
away from the one it asked for, and the session clock left the item's own
duration (`cur=1592.60 dur=600.0` on Fixtures/user/hls-hevc-vod-long-offset).
`Demuxer` now strips the origin (the anchoring stream's `start_time`, else the
container's) before positioning a `TimeSeekableIOReader`, and the protocol says
which axis it speaks.

`seekBounded` takes the same seam. It is the deadline-bounded form of a byte
binary search that this source cannot serve at all, and on an index-less MPEG-TS
that search is the one #112 round 10 already latched off as wedged or broken. The
VOD cue prewarm in turn skips a segmented source: there is no index for it to
load, and each reposition would refetch a segment for the uniform-stride plan it
ends up building anyway.

Also:

- The carriage probe reads packet-aligned chunks and stops at its first verdict
  instead of accumulating up to 512 KiB byte by byte. It runs on every HLS VOD
  reroute, including the H.264 sources that then stay native, where the per-byte
  loop measured ~75 ms of CPU per load against a loopback origin versus ~2 ms for
  a bulk read. The ceiling drops to 128 KiB with it.
- Byte seeks stop reporting a rewind that never happened; only a reposition that
  does not move succeeds, which is all the `CustomIOReaderBridge` capability
  handshake needs.
- The prefetch window narrows with the observed segment size, so four 4K
  segments in flight cannot peak near 100 MB (AE#255: bound by bytes, not by
  count), and the FIFO matches the live ingest at 16 MB.
- `makeIndependentReader` returns nil rather than opening a second full HTTP
  ingest of the same VOD for side-demuxed subtitles or scrub previews.
- A reposition onto the segment the producer already opened at, with nothing
  consumed since, resolves without refetching it (scrub churn).
- The two reroute call sites collapse into one helper.

Verified: full suite green (405 XCTest, 1365 Swift Testing in 207 suites),
strict-concurrency build clean, tvOS BUILD SUCCEEDED. End to end through
`aetherctl play` against a local range-capable origin serving the new fixtures:
HEVC-in-TS VOD plays with duration 600.04 s and six forward/backward seeks land
exactly (52-100 ms), each mapping to the segment before its target; the same run
on the 1000 s-origin fixture now restarts at segment 0 for a seek to item second
0 instead of segment 98; the H.264 control keeps the AE#154 native route.
Fixture recipes are in Scripts/fetch-fixtures.sh.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HX5zV3Fcf7Nzq4DYGdXvQE
@superuser404notfound

Copy link
Copy Markdown
Owner

Merged, thank you. The shape is right: the decision comes from the playlist and the PMT, the ingest is seekable, and every carriage AVPlayer handles itself stays on the native route. Follow-up hardening is on main in d8cc273.

One real defect, found before the merge. Demuxer.seek hands the reader an ABSOLUTE source PTS, while the reader addresses elapsed playlist time. MPEG-TS carries an arbitrary PTS origin: ffmpeg writes 1.4 s by default, which is invisible because landing early is covered by the lower-bound contract, but broadcast-derived VOD routinely starts hours in. On a fixture muxed with -output_ts_offset 1000:

[HLSVODIngest]   seek target=1006.74s segment=98 origin=588.00s
[HLSVideoEngine] producer restarted at idx=1 (seek=1006.74s [absolute source-PTS])
[AetherEngine]   VOD shift published: 1586.701s (prev 1002.741s, delta 583.960s)
  t=30 state=playing cur=1592.60 src=1008.64 dur=600.0

A seek to item second 5 positioned the ingest at the last segment of a 600 s playlist, and the session clock left the item's own duration. Demuxer now strips the origin (the anchoring stream's start_time, else the container's) before positioning a TimeSeekableIOReader, and the protocol documents which axis it speaks. Same fixture afterwards: seek elapsed=0.00s segment=0.

Rest of the follow-up:

  • seekBounded takes the same seam. It was still reaching avformat_seek_file (VOD cue prewarm, side reader positioning), which is exactly the byte binary search this source cannot serve. The prewarm now skips a segmented source outright: there is no index to load, and each reposition would refetch a segment for the uniform-stride plan it ends up building anyway.
  • The carriage probe reads packet-aligned chunks and stops at its first verdict instead of accumulating up to 512 KiB byte by byte. That path runs on every HLS VOD reroute, including the H.264 sources that then stay native, where the per-byte loop measured ~75 ms of CPU per load against a loopback origin versus ~2 ms for a bulk read. The ceiling is 128 KiB now.
  • seek(offset: 0, whence: SEEK_SET) no longer reports a rewind that never happened; only a reposition that does not move succeeds. As a side effect, the isSeekable line in CustomIOReaderBridge was a no-op before, because the old handshake already answered 0.
  • The prefetch window narrows with the observed segment size, so four 4K segments in flight cannot peak near 100 MB (ChunkFetchDelegate reserves the whole file's Content-Length on a bodyless HEAD probe: malloc returns NULL and the force-unwrap kills the app on large sources #255), and the FIFO matches the live ingest at 16 MB.
  • makeIndependentReader returns nil rather than opening a second full HTTP ingest of the same VOD for side-demuxed subtitles or scrub previews.
  • Tests for the restart mapping, the byte-seek refusal, the prefetch bound and the probe verdicts, and the two reroute call sites collapsed into one helper.

Verification here: full suite green (405 XCTest, 1365 Swift Testing in 207 suites), strict-concurrency build clean, tvOS BUILD SUCCEEDED, plus an end to end run through aetherctl play against a local range-capable origin. HEVC-in-TS VOD plays with duration 600.04 s, six forward and backward seeks land exactly in 52 to 100 ms, each restarting at the segment before its target, and the H.264 control keeps the AE#154 native route. Scripts/fetch-fixtures.sh now carries the fixture recipes (default origin, 1000 s origin, H.264 control) so the run is reproducible without a private source.

Issue220SoftwareDecoderDrainTests passed here on this branch, so that failure looks machine dependent rather than something the branch introduced.

The open piece is a confirmation on real hardware against the original source, so I reopened #268 for it.

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.

HEVC-in-MPEG-TS HLS VOD direct media playlist routes to AVPlayer with 0x0 video

2 participants