Skip to content

fix(latency): bridge appsrcs report the latency they can hold, gate repeated bus warnings - #1072

Merged
srperens merged 2 commits into
mainfrom
fix/bridge-appsrc-latency
Oct 19, 2026
Merged

srperens merged 2 commits into
mainfrom
fix/bridge-appsrc-latency

Conversation

@srperens

Copy link
Copy Markdown
Collaborator

Refs: no issue. Found on a production demo flow.

Problem

A vision mixer + audio mixer flow with three SRT inputs, a WHIP guest and a Media Player clip burned ~280% CPU. Every aggregator in both mixer blocks (audiomixer, aux0_mixer, the GL mixer, mv_comp) failed its latency configuration ~1400 times per second:

Impossible to configure latency: max 0:00:01.000000000 < min 0:00:02.000000000
  • The minimum of 2 s comes from one SRT input at latency: 2000.
  • The maximum of 1 s comes from WHIP Input and Media Player. Both hand media into the flow through a live appsrc left at its default latency. A live appsrc at its defaults reports max = 0, as if it could not hold a buffer, but these appsrcs queue up to APPSRC_MAX_TIME (10 s). After the mixer's default queue (1 s), the mixer's maximum becomes 1.000 s.

The aggregator re-queries latency upstream on every cycle while it is unconfigured, and posts a warning each time. Strom logs every warning and broadcasts a PipelineWarning to every client, so the proxy in front of Strom loaded up too. Any flow that mixes an input with more than ~1 s of latency and a WHIP or Media Player input hits this.

Change

  • blocks::live_bridge_appsrc(max_bytes) builds a live bridge appsrc. It reports its queue (APPSRC_MAX_TIME) as maximum latency and keeps the minimum at 0, so existing flows keep the latency they had. WHIP Input slots (audio and video) and Media Player outputs use it. Those are the only production appsrcs that bridge live media into a flow; the vision mixer overlay appsrc is not live and does not take part in latency. intersrc already passes latency on correctly (min from the producer, max NONE).
  • The bus handler lets the same warning (same source, message and debug) through once per 10 s, with the count it held back appended to the text. This applies to both the log and the PipelineWarning event. A warning that loops can no longer turn into a CPU storm. Errors and infos are unchanged.
  • docs/BLOCK_GUIDELINES.md gains a "Reported Latency" section. A source reports the latency it can actually hold, and any block that bridges through an appsrc uses live_bridge_appsrc.

Rejected:

  • Raising queue max-size-time in front of the mixers. That hides the wrong report, costs memory on every input, and goes against the default-queue rule.
  • Reporting max = NONE. The appsrc does drop data past APPSRC_MAX_TIME, so 10 s is the honest value.

Evidence

  • The mixer configures latency with a bridge input next to a 2 s input: test_a_bridge_input_next_to_a_two_second_input_configures_latency builds the real mixer block (100 ms latency, 2000 ms min-upstream-latency, as on the flow) and feeds channel 0 through rtpjitterbuffer latency=2000 and channel 1 through live_bridge_appsrc. 0 warnings. With the two latency lines removed from the helper it fails with left: 7240 right: 0 (warnings in 4 s). The same test also asserts that a default live appsrc still triggers the warning, so the setup keeps showing the failure it guards.
  • WHIP Input slot appsrcs report 10 s max latency: slot_appsrcs_report_the_latency_their_queue_can_hold. With the fix reverted it fails with appsrc_audio_0 ... left: None right: Some(0:00:10.000000000).
  • Media Player output appsrcs report 10 s max latency: every_output_appsrc_reports_the_latency_its_queue_can_hold. With the fix reverted it fails with appsrc_video ... left: None.
  • Repeated warnings are gated: the gst::pipeline::warning_gate unit tests (first passes, repeats held, released after the interval with the count, distinct keys separate, map bounded). They exercise the gate itself, not the bus handler wiring.
  • The production numbers above came from the flow's log and debug graph. The fix was reproduced first in a standalone Python/GStreamer 1.28 script: default appsrc gave 1082 warnings in 4 s, max-latency=10s gave 0, and the min stayed the same.

Tests

Ran on macOS (GStreamer 1.28.6):

  • cargo test --lib: 980 passed, 6 ignored.
  • cargo test --test pipeline_lifecycle_test --test media_player_control_test --test media_player_hls_test --test bus_signal_watch_balance_test --test drop_on_latency_test: all passed.
  • cargo clippy --all-targets: clean.
  • The three guard tests, run again with the latency lines removed: all three failed as quoted above.

Skipped: none of the new tests have an early return. rtpL16pay, rtpjitterbuffer and rtpL16depay are in gstreamer1.0-plugins-good, which is in the CI package list.

Not run: the full flow on a server. Not deployed yet.

For the reviewer

  • With this fix the affected flow runs, and everything in it runs at ~2.2 s latency, set by its slowest input. That includes the WHEP return feed to the WHIP guest. That is what an SRT input at 2000 ms means in a mixer, but operators may not expect it. A follow-up will tell the operator which input sets the mixer's minimum and which sets its maximum when latency configuration fails.
  • The PipelineWarning event shape is unchanged. Only its text gains "(repeated N times in the last 10 s)" when warnings were held back.

Out of scope

  • The bridges still report min = 0, not the latency of the pipeline they bridge from (WHIP's 200 ms jitterbuffer). Unchanged from today.
  • The operator diagnostic described above (separate PR).

🤖 Generated with Claude Code

https://claude.ai/code/session_019c8J2sWW25auY4wAjxpoTN

@srperens srperens left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: Comment. The latency fix is right, sits at the right layer, and all seven new tests ran in CI. It is Comment only because the warning gate changes the main bus handler of every flow (GLOBAL), and the protocol leaves a change that wide to a maintainer. I found nothing that should block the merge.

Suggested (optional)

  1. backend/src/gst/pipeline/warning_gate.rs: when the map is full and nothing is stale, evict the oldest entry, or clear the map. See the gate under Diagnosis.

Claims

Claim Verdict Evidence
WHIP slots and Media Player outputs are the only production live bridge appsrcs CONFIRMED Every other AppSrc::builder() in backend/src is test-only; the overlay one is not live: backend/src/blocks/builtin/vision_mixer/builder/pipeline_cpu.rs:275 — .is_live(false)
The helper reports the queue as the max and keeps min at 0 CONFIRMED backend/src/blocks/mod.rs:46 — .max_latency(APPSRC_MAX_TIME.nseconds() as i64). Used at backend/src/blocks/builtin/whip/input/slots.rs:291 — let appsrc = live_bridge_appsrc(APPSRC_MAX_BYTES_AUDIO), video slot and Media Player builder likewise
An explicit min of 0 gives the same reported min as today's default EXTERNAL This depends on appsrc/basesrc query semantics. Assumed from the author's GStreamer 1.28 reproduction
The guard tests fail if the fix is reverted CONFIRMED The mixer test asserts both directions: the bridge gives 0 warnings and a default live appsrc gives more than 0 (backend/src/blocks/builtin/mixer/tests.rs:1633). The two property tests assert Some(APPSRC_MAX_TIME)

Diagnosis. The cause explains the symptom. The aggregator takes the smallest max among its inputs, a live appsrc reports a max of 0, and the 1 s default queue after it caps the mixer below the 2 s SRT minimum. Coverage is BOUNDED to appsrc bridges. A non-appsrc live source that understates its max would still trip this, and only the gate limits the cost of that. Fixing the reporting input, rather than raising queue sizes, is correct under the default-queue rule.

Gate: the bus handler is a signal watch, not a probe, so the Mutex and format! are fine there. Its bound is weaker than 256, though. retain only drops entries older than 10 s (backend/src/gst/pipeline/warning_gate.rs:44 — if self.seen.len() >= MAX_TRACKED {). A warning whose debug text changes on every message then grows the map to rate × 10 s entries, with an O(n) retain on each insert. Those pass the gate anyway, so no regression, but the gate cannot help there. Also, a storm's final held-back count is never reported.

Radius. GLOBAL. The gate runs in every flow's main handler (backend/src/gst/pipeline/bus.rs:128 — let Some(suppressed) = warning_gate.lock().unwrap().check(key, Instant::now())). PipelineWarning consumers only display it; shape unchanged. Block bus handlers are separate, so the early return skips nothing else.

Overlaps

  • #1073 is stacked on this PR (it contains f989f53) and edits bus.rs. Land this one first.
  • Draft #1025 edits the same use crate::blocks::{...} block in slots.rs and blocks/mod.rs. Expect a trivial rebase.
  • #1024 ("decreasing DTS") is qtmux debug-log output, not a bus warning, so the gate does not touch it.

Tests & CI. At f989f53: Check (Linux), Build (Linux x86_64/ARM64), Check & Build (WASM) and API Contract Check pass. The Check (Linux) log shows all seven new tests ... ok. rtpL16pay, rtpjitterbuffer and rtpL16depay come from gstreamer1.0-plugins-good, which is in the ci.yml package list. macOS/Windows skipped; nothing platform-specific, Linux-green suffices.

Design record. Rejected options are in the body. The new BLOCK_GUIDELINES.md section is a contract change by the maintainer; noted only.

Confidence: HIGH

srperens and others added 2 commits October 19, 2026 13:29
…epeated bus warnings

WHIP Input and Media Player hand media into the flow through a live appsrc
left at its default latency, which reports a maximum of 0 although it queues
up to 10 s. After the mixer's default queue that caps the mixer's maximum at
1 s. With an SRT input at 2000 ms in the same mixer, every aggregator in the
block failed its latency configuration on every cycle: ~1400 "Impossible to
configure latency" warnings per second, each logged and broadcast, and the
mixers re-querying latency upstream each time. A production flow burned
~280% CPU this way.

- blocks::live_bridge_appsrc builds those appsrcs and reports
  APPSRC_MAX_TIME as their maximum latency; the minimum stays 0.
- The bus handler lets the same warning from the same source through once
  per 10 s, with the count it held back.
- BLOCK_GUIDELINES gains a Reported Latency section.

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

A warning whose debug text carries a drifting value ("min 0:00:02.023...")
got a new gate key on every change and passed the gate each time. Digits in
the debug text are now masked in the key; the source name and message are
kept as they are.

When the map is full and nothing in it is stale, it is cleared, so it can
no longer grow past MAX_TRACKED.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@srperens
srperens force-pushed the fix/bridge-appsrc-latency branch from f989f53 to 55b5ae2 Compare October 19, 2026 11:33
@srperens

Copy link
Copy Markdown
Collaborator Author

Review findings addressed in 55b5ae2, rebased onto current main:

  • Drifting values bypassed the gate. The key included the raw debug text, so a warning whose numbers change between repeats (a jitterbuffer's reported min drifting, as feat(latency): name the inputs behind a mixer's latency failure #1073's test shows with "2023 ms") got a new key each time. Digits in the debug text are now masked in the key (mask_digits). Source name and message are kept as they are, so queue3 and queue4 stay separate.
  • Map bound. When the map is full and nothing is stale, it is now cleared, so it cannot grow past MAX_TRACKED.
  • Not changed: the held-back count of a storm that stops is never reported. That is a log detail, not a cost problem.

Also verified the EXTERNAL claim against GStreamer 1.26: appsrc has no get_times, so basesrc's startup latency is 0 and its default query reports min = max = 0 (gstbasesrc.c gst_base_src_query_latency). An explicit min-latency=0 gives the same min as before, and the default max of 0 is the root cause.

Ran locally (macOS, GStreamer 1.28.6): cargo test --lib warning_gate (6 passed, 2 new), the three guard tests (test_a_bridge_input_next_to_a_two_second_input_configures_latency, slot_appsrcs_report_the_latency_their_queue_can_hold, every_output_appsrc_reports_the_latency_its_queue_can_hold), clippy -D warnings. The new unit test guards mask_digits, not its use in bus.rs; no integration test drives a drifting warning.

@srperens srperens left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Area: Pipeline bus warnings, latency of live bridges

This reviews only the new commit 55b5ae2: the gate key masks digit runs in the debug text, and the map is cleared when it is full and nothing in it is stale. Both answer the earlier review: the drifting-value key and the bound. The rest is unchanged from f989f53, which git range-diff shows as identical.

Neighbours: #1073 is stacked on this PR and was only rebased onto the new commit. Its own patch is unchanged, so it was not reviewed again.

Found nothing blocking. Checked:

  • bus.rs masks only debug_info, and the source name stays in the key, so queue3 and queue4 stay apart.
  • check() cannot hold more than MAX_TRACKED entries after the clear(). The new unit test covers it.
  • Check (Linux) passed at this head and runs --lib, so both new unit tests ran.

One cost, small: clear() also drops the held-back counts of warnings still inside their 10 s window. Those counts are never reported, as before for the last burst of any warning. It only matters under a storm of more than 256 distinct keys, so this is a note, not a finding.

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