Skip to content

fix(recording): start a recording at zero, not at the broadcast clock (#560) - #574

Merged
superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
tschuegy:fix/recording-timestamp-origin
Sep 20, 2026
Merged

superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
tschuegy:fix/recording-timestamp-origin

Conversation

@tschuegy

Copy link
Copy Markdown
Contributor

A recording written by LiveRecordingWriter carries the broadcast's own clock, so its first presentation timestamp lies hours past its own beginning.

What happens

The writer copies the source's pts/dts verbatim. A live channel's clock is wherever it has got to — on a stream that came up in the morning, six or seven hours in — so the resulting file starts at, say, 24549.835 s rather than at zero.

That is legal, and this engine copes with it on the VOD path since #107. But a recording on a NAS is opened by whatever the household happens to use, and a player that treats the first timestamp as the origin reads the file as hours of nothing before the picture. A duration probe reports the offset rather than the length.

It is also what turned #107's missing axis conversion into a hard freeze — see the companion PR — which is how this surfaced at all.

The change

The origin is latched once, from the first packet written (the arming keyframe), preferring dts because that is the order the muxer writes in. The same instant is subtracted from every stream, so the A/V relationship is preserved exactly.

  • AV_NOPTS_VALUE travels through untouched, as OutputTimestampSanitizer treats it.
  • A reordered frame sitting just before the arming keyframe's DTS clamps to zero rather than going negative, which the muxer would refuse. At most the first few frames share instant zero; nothing is reordered.
  • Nothing is decoded and nothing is re-encoded. Only the numbering changes, so the "never decodes, never encodes" property of the writer still holds.

A question rather than an assumption

If the broadcast timeline is worth keeping, this is the wrong trade and I would rather you said so. A metadata tag would carry the original origin without putting it on the presentation axis, and the bytes would stay byte-identical to the source. Say the word and I will redo it that way — I could not tell from the code whether the verbatim copy was a decision or an oversight.

Test plan

  • swift test — 3146 tests green, including three new cases: the shift itself, AV_NOPTS_VALUE and a packet with no origin yet passing through untouched, and the clamp for a reordered frame. The existing container assertion still holds, so the output is still a whole number of 188 byte packets.
  • Device: Apple TV 4K (3rd generation), tvOS 26, recording a live MPEG-TS channel whose clock stood at 24549.835 s to an SMB share.

`LiveRecordingWriter` copied the source's presentation timestamps
verbatim. A live channel's clock is wherever it has got to — six or
seven hours in on a stream that came up in the morning — so a recording
of it is a file whose first timestamp lies hours past its own beginning.

That is legal, and this engine copes with it on the VOD path since superuser404notfound#107,
but it is not what a file is. A player that treats the first timestamp
as the origin reads such a recording as hours of nothing before the
picture; a duration probe reports the offset rather than the length; and
a recording on a NAS is opened by whatever the household happens to use,
not only by this engine.

The origin is latched once, from the first packet written, preferring
`dts` because that is the order the muxer writes in, and the same
instant is subtracted from every stream — the A/V relationship is
preserved exactly. `AV_NOPTS_VALUE` travels through untouched, as
`OutputTimestampSanitizer` treats it, and a reordered frame just before
the arming keyframe clamps to zero rather than going negative, which the
muxer would refuse. Nothing is decoded and nothing is re-encoded; only
the numbering changes.

If the broadcast timeline is worth keeping, a metadata tag would carry
it without putting it on the presentation axis — happy to do that
instead if you would rather the bytes stayed byte-identical to the
source.

Test plan
- `swift test`: 3135 tests, all green, including three new cases on the
  rebasing (the shift, `AV_NOPTS_VALUE` and a no-origin packet passing
  through untouched, and the clamp for a reordered frame). The existing
  container assertion still holds, so the output is still a whole number
  of 188 byte packets.
- Device: Apple TV 4K (3rd gen), tvOS 26, recording a live MPEG-TS
  channel whose clock stood at 24549.835 s to an SMB share.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@superuser404notfound
superuser404notfound merged commit 0619457 into superuser404notfound:main Sep 20, 2026
7 checks passed
@superuser404notfound

Copy link
Copy Markdown
Owner

Merged, thank you, and the question at the end is the right one to ask: no, the broadcast timeline is not worth keeping on the presentation axis. A recording is a file, and a file begins at zero. If the original origin is ever wanted it belongs in a metadata tag, exactly as suggested, and that can be added later without moving the timestamps back onto the broadcast clock.

One thing is being changed on top, in #577, and it is worth explaining rather than just doing.

The origin is latched from the first packet WRITTEN, which is the arming video keyframe. An MPEG-TS interleaves the audio belonging to a picture ahead of that picture, so the audio packets arriving just after the keyframe carry timestamps EARLIER than the origin, and they are what meets the clamp. Measured here on a source whose audio leads by 100 ms: four AC-3 frames come out stamped at instant zero while the picture sits at zero as well, so the head of the recording loses precisely the offset a rebase is supposed to preserve. The rest of the file is unaffected, it is the first tenth of a second that flattens.

libavformat already has this: avoid_negative_ts=make_zero peeks into the interleaving queue for the lowest timestamp across every stream, latches a single offset from it and applies that offset to all streams for the rest of the file. Same result for the case in the report, nothing to clamp, and both invariants from the PR hold by construction rather than by care: AV_NOPTS_VALUE travels through, and nothing is reordered.

The tests moved with it and now assert on the written file rather than on the arithmetic: one records a channel whose clock stands at 24549.835 s and reads the first timestamp back as zero, the other records audio leading the keyframe by 100 ms and reads that lead back intact. The second one fails on this PR as merged, which is what made the case worth catching.

Thanks for both this and #573. The recording path and the VOD axis conversion were found the same afternoon from the same stream, which is not a coincidence anyone could have guessed at from the code.

superuser404notfound added a commit that referenced this pull request Sep 20, 2026
…n-muxer-rebase

fix(recording): the recording's zero is the lowest timestamp, not the arming keyframe (#560, #574)
@superuser404notfound

Copy link
Copy Markdown
Owner

Follow-up is in: #577, merged as bba7efa8. The file still begins at zero, the lead of the interleaved audio survives, and the two tests now assert on the written file rather than on the arithmetic. Nothing else about this PR changed.

ijuniorfu pushed a commit to ijuniorfu/AetherEngine that referenced this pull request Sep 21, 2026
… arming keyframe (superuser404notfound#560, superuser404notfound#574)

superuser404notfound#574 rebased a recording onto zero by latching the origin from the first packet written and
subtracting it from every stream. That packet is the arming video keyframe, and a TS interleaves
the audio belonging to a picture ahead of the picture itself, so those audio packets are earlier
than the origin and have to be clamped to stay non-negative.

Measured on a source whose audio led by 100 ms: four AC-3 frames came out stamped at instant zero
while the picture sat at zero as well, so the head of the recording lost exactly the offset the
rebase is meant to preserve.

The muxer already does this correctly. `avoid_negative_ts=make_zero` peeks into the interleaving
queue for the lowest timestamp across every stream, latches one offset from it and applies that
same offset to all streams for the rest of the file. Nothing is clamped, the file still begins at
zero, and the hand-rolled origin arithmetic goes away with it.

The two unit tests are end-to-end on the written file rather than on the arithmetic: one records a
channel whose clock stands at 24549.835 s and reads the first timestamp back as zero, the other
records audio leading the arming keyframe by 100 ms and reads that lead back intact. The second
one fails on superuser404notfound#574 as merged.

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.

2 participants