Repository navigation
feat(recording): record a live stream from the session's existing connection (#560) - #570
Merged
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements #560.
A host can now record the currently playing live stream to a file, fed from the connection the engine already holds:
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
.loopbackHLSSegmentProducer.readNextSourcePacket, the single funnel through which main and side-audio demuxer packets pass already merged into decode order, upstream ofbridge.feed.softwareSoftwarePlaybackHost's read sites, above the DVR ring branch.remoteBypassTwo choices in there are load-bearing:
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.if let ring, and a live session need not have asked fordvrWindowSeconds. 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
.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.startRecordingthrows.unsupportedRoute(.remoteBypass)rather than producing an empty file. The escape is a reload withnativeRemoteHLS: 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..loopbackwhat 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 --recordagainst a 5 minute H.264 + AAC MPEG-TS source:h264+aacpreserved, routeloopbacknon-existing PPS 0 referencedfor the whole first GOPSIGKILLmid-write the file still reads as validmpegtsand decodes to the cut with no errors, which is the reason the container is TS and not fragmented MP4Three 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