Skip to content

feat(recording): record a live stream from the session's existing connection (#560) - #570

Merged
superuser404notfound merged 2 commits into
mainfrom
feat/live-recording
Sep 20, 2026
Merged

superuser404notfound merged 2 commits into
mainfrom
feat/live-recording

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

Implements #560.

A host can now record the currently playing live stream to a file, fed from the connection the engine already holds:

try await engine.startRecording(to: url)
await engine.stopRecording()
engine.$recordingState        // .idle / .recording / .ended / .failed

No second connection to the origin is opened. That is the point of the feature rather than a detail of it: an IPTV plan commonly caps an account at 1 to 3 simultaneous connections, so a host that opens its own connection to record either fails outright or knocks the viewer off the channel it is recording.

What it does

The output is MPEG-TS, a stream copy of the source packets taken before any audio bridging. Nothing is decoded, nothing is re-encoded. A bridged TrueHD or DTS channel therefore records its original audio while playback is listening to FLAC. Writing opens on the first video keyframe after the call, so the file starts on a decodable picture.

Recording follows the source rather than the playhead, so pausing or scrubbing back inside the DVR window does not interrupt it. That falls out of where the taps sit rather than being special-cased.

Where the taps sit, and why

route tap
.loopback HLSSegmentProducer.readNextSourcePacket, the single funnel through which main and side-audio demuxer packets pass already merged into decode order, upstream of bridge.feed
.software both of SoftwarePlaybackHost's read sites, above the DVR ring branch
.remoteBypass none possible, see below

Two choices in there are load-bearing:

  • Not Demuxer.readPacket, which would have been one tap for both routes. A packed AAC audio rendition in live HLS opens a side demuxer (HLSVideoEngine.swift:1584), and a tap on the main demuxer alone would record those channels silent.
  • Above the ring branch on the software path, because that branch is gated on if let ring, and a live session need not have asked for dvrWindowSeconds. A tap inside it would record nothing for those sessions while every test stayed green.

Two non-goals, stated so nobody has to infer them

  • No recording on .remoteBypass (LoadOptions.nativeRemoteHLS). AVFoundation holds the source connection there and the engine never sees a byte, so there is nothing to tap without opening the second connection the feature exists to avoid. startRecording throws .unsupportedRoute(.remoteBypass) rather than producing an empty file. The escape is a reload with nativeRemoteHLS: false; the engine does not take it by itself, because pressing Record would then visibly interrupt the picture and the routing decision belongs to the host.
  • No "record from the start of what is buffered." On .loopback what is retained is remuxed fMP4 with bridged audio, not source packets, so prepending it would produce one file whose audio codec changes in the middle. The software packet ring could do it, and shipping it on one route only would be worse than not shipping it.

The backpressure contract

Writes go to a bounded byte-budgeted queue drained off the demux thread. When it fills, the engine drops the recording rather than the picture (.failed(.writeTooSlow)). Nothing in this path can block the demux thread: the failure teardown is scheduled elsewhere rather than run there, and the drain queue never calls back into itself.

Verification

Full suite green on both runners (XCTest 624, swift-testing 3128). 24 new tests, including two that pin bugs found during implementation.

Measured with aetherctl play --record against a 5 minute H.264 + AAC MPEG-TS source:

  • 42 MB / 280 s recorded, h264 + aac preserved, route loopback
  • a strict decode of the head is clean; before the keyframe-arming fix the same check read non-existing PPS 0 referenced for the whole first GOP
  • after SIGKILL mid-write the file still reads as valid mpegts and decodes to the cut with no errors, which is the reason the container is TS and not fragmented MP4

Three arms still want real sources and are not covered here: a bridged TrueHD or DTS channel (the arm that proves the tap is on the right side of the bridge), an MPEG-2 channel for the software route, and a live HLS master with a packed AAC rendition for the side-demuxer case.

🤖 Generated with Claude Code

https://claude.ai/code/session_015PM3xUJB6ZQyqnmGK1fp6F

Vincent Herbst and others added 2 commits September 20, 2026 07:34
…nection (#560)

A host can now record the currently playing live stream to a file, fed from the
connection the engine already holds, so no second connection to the origin is
opened. On IPTV that is the whole point: provider plans commonly cap an account
at 1 to 3 simultaneous connections, so a host opening its own connection to
record either fails or knocks the viewer off the channel.

    try await engine.startRecording(to: url)
    await engine.stopRecording()
    engine.$recordingState        // .idle / .recording / .ended / .failed

The output is MPEG-TS, a stream copy of the SOURCE packets taken before any
audio bridging, so a TrueHD or DTS channel records its original audio while
playback listens to the bridged FLAC rendition. Nothing is decoded, nothing is
re-encoded. Writing opens on the first video keyframe after the call.

Two taps, one per engine-owned route, and both sit on the demux side so the
recording follows the source rather than the playhead: pause and DVR scrubbing
do not interrupt it.

  - loopback: HLSSegmentProducer.readNextSourcePacket, the single funnel through
    which main and side-audio demuxer packets pass already merged into decode
    order. NOT Demuxer.readPacket, which would miss the side demuxer that a
    packed AAC rendition in live HLS opens, and record those channels silent.
  - software: both of SoftwarePlaybackHost's read sites, above the DVR ring
    branch, because that branch is gated on a dvrWindowSeconds a live session
    need not have asked for.

.remoteBypass cannot record and says so (.unsupportedRoute): AVFoundation holds
the source connection there and the engine never sees a byte. The escape is a
reload with nativeRemoteHLS: false, and the engine does not take it by itself,
because pressing Record would then visibly interrupt the picture.

Writes go to a bounded queue drained off the demux thread. When it fills, the
engine drops the RECORDING rather than the picture (.writeTooSlow). Nothing on
the demux thread in this path can block: the failure teardown is scheduled, not
run there, and the drain queue never calls back into itself.

A reset or a reload ends the recording cleanly (.ended(.sourceReset)) rather
than writing past the seam, because the source can come back with different
codecs or a different program.

Not implemented, and documented as such: recording from the start of what is
already buffered. The loopback cache holds remuxed, bridged fMP4 rather than
source packets, so prepending it would change audio codec mid-file.

Verified with aetherctl play --record against a 5 minute MPEG-TS source:
42 MB / 280 s recorded, h264 + aac preserved, and a strict decode of the head
is clean. After SIGKILL mid-write the file still reads as valid mpegts and
decodes to the cut with no errors, which is why the container is TS and not
fragmented MP4.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_015PM3xUJB6ZQyqnmGK1fp6F
…log (#560)

PublicAPIDocumentationTests exists because a shipping downstream host once read
the whole README API tour and came away without a surface (#374). A recording
API that lives only in docs/api.md repeats that, so the tour gets the three
symbols, the connection argument that motivates the feature, and the one route
that cannot record.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_015PM3xUJB6ZQyqnmGK1fp6F
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