Skip to content

feat(mpegtssrt_input): retime tsdemux output from SRT arrival - #1088

Closed
srperens wants to merge 1 commit into
mainfrom
feat/ts-arrival-clock
Closed

srperens wants to merge 1 commit into
mainfrom
feat/ts-arrival-clock

Conversation

@srperens

Copy link
Copy Markdown
Collaborator

Refs #813 (alternative approach to the same bug)

Problem

With ignore-pcr=true (the block default), tsdemux maps the sender's PTS onto running time from one reference: the arrival of the first PES it parsed. It never takes a new one, so any error in that one sample lasts for the whole connection. Measured locally over SRT (lead = running time out of tsdemux minus arrival, i.e. delay the stream adds downstream):

  • A caller that encoded before connecting is stamped ahead by as long as it waited: ~2 990 ms for the whole connection after a 3 s wait (GStreamer sender). ffmpeg then sends 5 % over real time to catch up, so the error grows by ~50 ms/s (734 → 1 256 ms over 11 s).
  • ffmpeg's default muxing, connecting cleanly, already adds ~350 ms on audio and ~500 ms on video: its send-ahead becomes latency.
  • A sender that stalls 700 ms (ffmpeg) is stamped late for ~5 s while it catches up: 122 buffers late, which a sync sink drops.

PCR mode would track the sender's clock, but too many senders get PCR wrong to rely on it. That is why ignore-pcr is the default.

#813 corrects the stale-head case with a one-shot correction in the first 3 s, above 500 ms. Most of its listed limits follow from that shape: nothing under 500 ms, nothing after 3 s, no drift, no mode=caller.

Change

New gst::ts_arrival_clock, wired into builtin.mpegtssrt_input when ignore_pcr and the new arrival_retiming property are on (both default true). It keeps tsdemux's PTS spacing and replaces only its anchor, from srtsrc's arrival timestamps (within 0–8 ms of real arrival, measured):

  • Data cannot arrive before it was sent, so per stream the largest lead in a sliding 2 s window is the best estimate of the sender's timeline against ours.
  • The stream whose best lead is smallest (it arrives latest for its PTS: AAC packed many frames per PES) sets one shared offset, so A/V sync is kept: offset = margin − min over streams (best lead). margin is the new arrival_margin_ms property, 100 ms by default.
  • A stream more than 1 s behind the earliest one is still on stale data and does not count (MPEG-TS bounds how long data may wait for its PTS to about a second).
  • Late buffers re-anchor every stream once the lateness is confirmed: late buffers spanning 100 ms of the timeline with nothing on time in between (a stall, a slower sender). A stale tail is not confirmed, because live buffers on time arrive in between. For 1 s after caller-added/caller-removed, every late buffer re-anchors at once, which also absorbs the junk buffer tsdemux flushes from the previous caller (one buffer stamped ~1.3 s off, seen in the integration test).
  • A pad's output never steps backwards: a smaller offset is applied only as far as it keeps the buffer at or after the previous one.
  • Applied with gst_pad_set_offset on each tsdemux source pad, only on a change of ≥ 5 ms (or a late buffer). An offset set inside the buffer probe applies to that same buffer (checked).

Rejected along the way (measured):

  • Max lead over all streams (one anchor from the earliest stream): leaves the latest stream (audio) only ~20 ms of margin with ffmpeg.
  • The second-largest lead (robust to one junk buffer): the first live buffer after a stale head then goes out 2 s ahead, and the no-backwards rule holds the stream for 1.2 s.
  • A "newest running time" rule for stale buffers: one junk buffer stamped ahead poisoned it and kept ~300 ms of the next caller late.

Evidence

Real block (MpegTsSrtInputBuilder) over SRT on macOS, GStreamer 1.28.6, passthrough, lead at the block outputs:

sender, scenario arrival_retiming=false arrival_retiming=true
GStreamer, encoded 2 s before connecting ~1 955–2 000 ms throughout 82–128 ms from 1 s
same, decode mode ~1 900–2 015 ms 9–120 ms from 1 s (after decode)
GStreamer, clean connect −13…+32 ms 83–130 ms
ffmpeg, clean connect audio ~340, video ~495 ms audio 66–104, video ~245 ms, 0 late
ffmpeg, encoded 3 s before connecting 433 → 916 ms over 8 s, growing audio ~90, video ~250 ms, steady, 0 late
ffmpeg, 700 ms stall 122 buffers late, for ~5 s 3 late (the 100 ms confirmation)
clean caller after a corrected one – 0 late from its first buffer (87–123 ms)

A clean GStreamer caller now leads ~100 ms where it led ~0 before: that is the margin. For ffmpeg streams the margin is far less than what tsdemux added before. Whether 100 ms is the right default, and whether to report it in the latency query instead of stamping it, is open (see below).

Tests

Ran on macOS (arm64, GStreamer 1.28.6), STROM_REQUIRE_GST_PLUGINS=1:

  • cargo test --lib ts_arrival_clock: 11 unit tests (healthy A/V spread, stale head, stale tail, stall, drift 0.5 %, caller change, junk buffer at a caller change, unannounced reference jump, stream that stops, no backwards step, no offset churn on jitter). All pass.
  • cargo test --test mpegtssrt_arrival_retiming_test: 4 integration tests over real SRT with an in-process H.264 + AAC caller. 10 consecutive runs green after widening the decode-mode lower bound (one earlier run hit −79 ms after the decoder against a −50 bound).
  • Revert checks: with arrival_retiming=false both early-caller tests fail (~2 000 ms). With the caller-added/caller-removed hookup removed, a_clean_caller_after_a_corrected_one_is_not_late fails (its first ~100 ms go out late). a_clean_caller_stays_within_the_margin passes with retiming off too: it guards the margin bound, not the bug.
  • Also green: mpegtssrt_absent_caller_test, mpegtssrt_input_program_change_test, mpegtssrt_streamheader_test, pipeline_lifecycle_test, openapi_test, clippy (-D warnings, with and without --features efp), fmt.
  • The ffmpeg rows above came from a temporary ignored test (CI has no ffmpeg), not committed. The scenarios were first measured outside Strom with a Python receiver and an offline simulation of the algorithm.

Not run: Linux CI (this PR's run will be the first), the rest of the integration suite, the Open Live rig, a real network (all measurements are on localhost).

For the reviewer

  • Default on. Every MPEG-TS/SRT input with ignore_pcr changes timing. Clean GStreamer callers gain ~100 ms (the margin), and ffmpeg-muxed callers lose ~250 ms. arrival_retiming=false restores today's behaviour.
  • The margin is stamped, not reported. It shifts timestamps by margin ahead of arrival rather than reporting it in the latency query. Reporting would be more GStreamer-like. Stamping keeps the change local to the block, matches what the measurements validated, and lets late-buffer detection work against zero.
  • set_offset in a BUFFER probe takes the pad's object lock, but only when the offset changes (≥ 5 ms, or a late buffer); everything else is atomics. In the clean-connect test the offset changes a handful of times per connection.
  • Supersedes fix(mpegtssrt_input): re-anchor tsdemux stamped ahead by a stale head #813 if this approach is accepted. fix(mpegtssrt_input): re-anchor tsdemux stamped ahead by a stale head #813's tests and rig data were the starting point for the scenarios here.

Out of scope

  • Other MPEG-TS inputs (EFP/SRT, UDP TS). The helper is generic, but only this block uses it.
  • Measuring on the Open Live rig and over a real network, which is the next step before this leaves draft.
  • Tuning the margin automatically from the observed jitter.

🤖 Generated with Claude Code

https://claude.ai/code/session_015zM1LedicrqbhDqxh6B5UV

With ignore-pcr, tsdemux anchors its timestamps once, on the first PES it
parses, and keeps any error in that sample for the whole connection. A
caller that encoded before connecting is stamped seconds ahead, a sender
that stalls is stamped late for seconds, and ffmpeg's default muxing adds
its send-ahead (~350 ms audio, ~500 ms video) as latency.

gst::ts_arrival_clock keeps tsdemux's PTS spacing and replaces only its
anchor, from srtsrc's arrival timestamps: per stream the largest lead in a
2 s window, the latest-arriving stream setting one shared pad offset, late
buffers re-anchoring once confirmed (at once after a caller change), and
output never stepping backwards. On by default with ignore_pcr; block
properties arrival_retiming and arrival_margin_ms (100 ms).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015zM1LedicrqbhDqxh6B5UV
@srperens

Copy link
Copy Markdown
Collaborator Author

Superseded by #1272: rebased onto main, with two fixes on top (latency budget paid twice, B-frames out of decode order), measured in the PR.

@srperens srperens closed this Nov 17, 2026
srperens added a commit that referenced this pull request Nov 18, 2026
…ency budget (#1272)

* feat(mpegtssrt_input): retime tsdemux output from SRT arrival

With ignore-pcr, tsdemux anchors its timestamps once, on the first PES it
parses, and keeps any error in that sample for the whole connection. A
caller that encoded before connecting is stamped seconds ahead, a sender
that stalls is stamped late for seconds, and ffmpeg's default muxing adds
its send-ahead (~350 ms audio, ~500 ms video) as latency.

gst::ts_arrival_clock keeps tsdemux's PTS spacing and replaces only its
anchor, from srtsrc's arrival timestamps: per stream the largest lead in a
2 s window, the latest-arriving stream setting one shared pad offset, late
buffers re-anchoring once confirmed (at once after a caller change), and
output never stepping backwards. On by default with ignore_pcr; block
properties arrival_retiming and arrival_margin_ms (100 ms).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015zM1LedicrqbhDqxh6B5UV

* fix(mpegtssrt_input): retime against the deadline, keep B-frames in decode order

The arrival retiming from #1088 stamped the latest stream a margin after
its arrival, on top of the SRT latency srtsrc reports, so the budget was
paid twice. Measure lead against the deadline instead: the pipeline's
running time at arrival minus the reported SRT + tsdemux latency.

Arrival is the pipeline clock's running time, from Instant calibrated
against the clock every 256 srtsrc buffers. srtsrc's own stamp is up to
1.3 s behind the clock in a caller's connect burst.

The never-step-backwards rule compared PTS. Video with B-frames goes out
in decode order, so every B-frame raised the video offset and the next
P-frame lowered it: B-frames went out at the wrong time and video
drifted 30-120 ms behind audio. Compare DTS where the buffer has one.

Refs #1265

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(mpegtssrt_input): anchor arrival retiming on decode order, review fixes

Review findings on #1272:
- Lead and lateness use the decode-order running time (DTS, else PTS).
  Anchored on PTS, every B-frame after a P-frame missed its deadline
  whenever video set the anchor, and the on-time P-frames kept that
  from being confirmed.
- Late runs are kept per stream, so two late buffers of different
  streams cannot confirm 100 ms between them. An on-time buffer of any
  stream still ends every stream's run.
- A DTS before the segment origin gives a smaller running time instead
  of none.
- The budget counts 700 ms for a negative tsdemux latency, as tsdemux
  itself does.
- The trust window after a caller change starts at the first arrival,
  so a caller that connects before any data gets it.
- Calibrate against the pipeline clock by time (500 ms), not one buffer
  in 256: a deliberate, bounded exception to the probe lock rule.
- A per-slot generation stops a removed pad's probes from writing into
  a slot a new pad has claimed.
- The integration test measures lead against the pipeline clock, not
  srtsrc's stamp, and picks listener ports below the ephemeral range.
- Margin description and doc comments corrected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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