Skip to content

fix(whep): pin raw audio before whepserversink so a start-up renegotiation cannot fail the flow - #1030

Open
Lufftre wants to merge 1 commit into
mainfrom
fix/start-with-live-input
Open

Lufftre wants to merge 1 commit into
mainfrom
fix/start-with-live-input

Conversation

@Lufftre

@Lufftre Lufftre commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A flow that starts while an SRT input is already receiving sometimes fails to reach Playing. whepserversink posts:

There is no codec present that can handle the stream's type.
Failed to look up output caps: No caps found for stream audio_0

Strom then answers 500 "Failed to start flow". Open Live passes that through, so the production falls back to inactive. A real encoder that is already sending when the operator clicks Activate hits the same failure. In the Open Live testbed, ingest_early_caller_delay failed this way in 6 of 7 CI runs on Oct 7, against 1 of 10 runs before Oct 3.

The cause, from a GST_DEBUG=webrtcsink:5 log of a failing start:

  1. WHEP Output passed raw audio straight to the sink, which accepts any raw format. So the sink got whatever upstream negotiated. With an audio mixer in front, that was F32LE at 44.1 kHz, then S32LE about 100 ms later. Every start does this, including the ones that succeed.
  2. webrtcsink starts a codec discovery for each set of caps and feeds every incoming buffer to both.
  3. Around the switch, the timestamps step back slightly: audioresample reports a discontinuity of 813501413649709 samples, which is about 2^64 ns. The Opus encoder in a discovery pipeline then fails with buffer going too far back in time.
  4. A failed discovery is fatal, so the flow stops in Paused.

Change

A new gst::audio_input_bridge, installed on each WHEP Output audio queue's src pad. On the first raw CAPS event, it splices audioconvert ! audioresample ! capsfilter between the queue and the sink. The capsfilter is fixed to S16LE, 48 kHz, stereo, which is what WebRTC Opus carries anyway. After that, an upstream renegotiation is absorbed by the converters, and the sink sees one set of caps for the life of the pad. Encoded audio (Opus) is left linked straight to the sink. If the stream switches between raw and encoded mid-stream, the bridge posts an element error rather than failing silently with not-negotiated. It's the audio counterpart of video_input_bridge, and it follows the BLOCK_GUIDELINES rule to decide from negotiated caps when the input is only known at runtime.

Rejected alternatives:

  • A static chain built in at build time. It would refuse Opus input, which output blocks must pass through.
  • Fixing the audio mixer's output caps. That fixes this one producer, not the consumer; any other raw-audio producer that renegotiates would still break WHEP.
  • Patching webrtcsink to tolerate a caps change during discovery. That's upstream gst-plugins-rs, and the converters solve it on our side today.

Evidence

  • An upstream format switch no longer reaches the sink. gst::audio_input_bridge::tests::upstream_format_switch_does_not_reach_the_sink checks this. With the bridge disabled, it fails with: the sink saw more than one caps: [... "F32LE" ... 44100 ..., ... "S32LE" ... 44100 ...].
  • Encoded audio is not touched. encoded_audio_passes_through checks this. It passes before and after the change, so it guards against a regression, not the fix itself.
  • The flow failure is gone. Rig run on macOS / Docker Desktop (arm64), strom-full:0.6.11 image, Open Live main, with an SRT caller already sending into source 0 before activation:
    • Unpatched: at least 6 of 26 starts failed with the error above (2 of 12 and at least 4 of 14 in two runs).
    • This branch's binary in the same image: 0 of 30 failed, and every WHEP audio_0 caps event was S16LE, 48000.

Tests

  • Ran on macOS arm64, GStreamer 1.28.1:
    • cargo test --lib: 898 passed, 5 ignored.
    • cargo test --test pipeline_lifecycle_test: 4 passed.
    • cargo fmt --check and cargo clippy --all-targets: clean in the changed files.
  • Skipped: none. Both new tests need audiotestsrc, audioconvert, audioresample and opusenc, which gstreamer1.0-plugins-base provides in CI.
  • Not run: the GPU / NVIDIA path. The rig run used the CPU image.

For the reviewer

  • Every raw-audio WHEP Output is now converted once to S16LE, 48 kHz, stereo. Multichannel raw audio is downmixed to stereo. Before this change, webrtcsink converted per consumer to whatever Opus negotiated, which was stereo in practice.
  • New elements in the pipeline: the spliced elements are named <audio_queue id>_pin_{convert,resample,caps}.

Out of scope

  • webrtcsink upstream: a discovery that fails on a caps change during start-up is still fatal there. Worth an issue in gst-plugins-rs.
  • Why the mixer's format changes right after start: not investigated.

🤖 Generated with Claude Code

…ation cannot fail the flow

A flow started while an SRT input is already receiving intermittently
failed to reach Playing with "There is no codec present that can handle
the stream's type" from the WHEP Output's whepserversink.

The WHEP Output passed raw audio straight to the sink, so it got whatever
upstream negotiated. An audio mixer in front of it settles on F32LE at
44.1 kHz and switches to S32LE about 100 ms later. webrtcsink starts a
codec discovery for each set of caps and feeds every buffer to both;
around the switch the timestamps step back, the Opus encoder in one
discovery fails with "buffer going too far back in time", and a failed
discovery is fatal.

A new audio_input_bridge splices audioconvert ! audioresample !
capsfilter (S16LE, 48 kHz, stereo) in front of the sink on the first raw
CAPS event, so the sink sees one set of caps for the life of the pad.
Encoded audio passes through untouched.

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

@srperens srperens left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Verdict: Comment. The bridge is the right consumer-side shape. It mirrors video_input_bridge, and it follows BLOCK_GUIDELINES: decide from the negotiated caps, pass encoded audio through. Two gaps keep this at Comment. The WHEP wiring has no test, and the rig evidence comes from an image built before the mixer's rate pin landed.

Requested changes

  1. Add a block-level test in backend/src/blocks/builtin/whep.rs, next to the existing video one:
    backend/src/blocks/builtin/whep.rs:2876 — fn video_input_is_converted_before_the_sink() {
    It should build build_whepserversink with one audio track, feed raw F32LE/44.1 kHz into :audio_queue, and assert that :audio_queue_pin_caps sits in front of audio_0. Today, deleting this line fails no test:
    backend/src/blocks/builtin/whep.rs:1604 — audio_input_bridge::install_audio_input_bridge(&audio_queue_src, &audio_queue_id);
    The two module tests guard audio_input_bridge.rs itself, and they run (Check (Linux): both ok).

Claims

Claim Verdict Evidence
Splice decided per CAPS event, never per buffer CONFIRMED EVENT_DOWNSTREAM probe. The Mutex is taken per event only, which CLAUDE.md allows outside BUFFER probes. The closure holds no element
An upstream format switch no longer reaches the sink CONFIRMED upstream_format_switch_does_not_reach_the_sink asserts that the unbridged run sees ≥2 caps, so it fails if the bridge goes inert
The start-up failure reproduces on current main UNVERIFIED The rig ran strom-full:0.6.11. v0.6.11 (f1a46dd, 2026-09-29) does not contain d78c3ee (#946, 2026-10-02), which pins every mixer bus to one rate. So the 44.1 kHz leg seen in the log cannot appear on main. A format-only switch still can, because the pin fixes the rate and nothing else
webrtcsink discovery is fatal on a caps change EXTERNAL Upstream gst-plugins-rs, taken from the author's trace

backend/src/blocks/builtin/mixer/elements.rs:121 — .field("rate", rate as i32)

Diagnosis. Coverage is BOUNDED. It closes the "second caps during discovery" trigger for every raw producer, which is the consumer-side fix the guidelines ask for. It does not explain why the mixer renegotiates, or why timestamps step back by about 2^64 ns. Those still reach our own audioresample, which resyncs on a discontinuity rather than failing. Neither is worse after this change. The mixer side deserves an issue; the body's "Out of scope" already names it.

Behaviour change (disclosed). Every raw WHEP audio track is now S16LE/48 kHz/stereo: F32 input loses its float headroom before Opus, and multichannel input is downmixed once. PINNED_RAW_AUDIO is a pub const and not a block property, which matches the stereo-Opus target. It is a deliberate hardcode, though, so say so if a property is wanted later.

Radius. SHARED: every WHEP Output with raw audio, on every slot (audio_queue_N_pin_*), on CPU and GPU images alike. Opus input is untouched. The spliced elements live in the block's bin and are torn down with the pipeline. No thread, no timer.

Overlaps. #896, #899 and #814 edit whep.rs in the per-viewer video and appsink paths, not the audio queue, so none of them is on this code path. No open issue owns the audio format.

Tests & CI. At f0d2943: Linux, ARM64, WASM, API contract and JS are green. macOS and Windows are skipped, which is fine: the code here is platform-neutral.

Confidence: HIGH

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