Skip to content

feat(whep input): give video outputs to the streams that are sending - #1161

Merged
srperens merged 1 commit into
mainfrom
feat/whep-input-video-slots
Oct 28, 2026
Merged

srperens merged 1 commit into
mainfrom
feat/whep-input-video-slots

Conversation

@srperens

Copy link
Copy Markdown
Collaborator

No issue: follows on from #1085 (WHEP Input video), designed in a working session.

Problem

WHEP Input gave its video outputs to streams in arrival order, and freed an output only when the stream's pad went away. SFUs do not remove the pad of a stream that stopped:

  • Eyevinn Open Intercom (Symphony Media Bridge) starts a new SSRC when the pinned video source changes, while the old SSRC just stops sending. With num_video_tracks=1 the new source was discarded ("video beyond the declared video tracks") and video_out stayed on the old, frozen stream.
  • In GStreamer, whepsrc turns every new SSRC into a new pad and never removes the old one (rtpbin autoremove is off), so a stream that stopped held its output for the rest of the session.

A stream without an output was discarded for good, even after an output freed up.

Change

  • Silence frees an output. A stream that has delivered nothing for video_stream_timeout_ms (default 2000, 100-60000) gives up its output. It keeps running, parked in a fakesink, and takes an output again when one is free.
  • Waiting instead of discarding. Streams without an output wait, undecoded, and take free outputs in the order they became active.
  • video_assignment: fill (default) makes a new stream wait while every output is taken, so parallel sources keep their outputs and a mixer downstream keeps its inputs. newest makes it take over the output held longest. A stream that starts sending again after a silence counts as new. This fits a source that switches, such as an SFU's pinned or active-speaker video.
  • Per output, an input-selector in front of the tee. A switch makes the new decoder's selector pad the active one, instead of relinking the tee while data flows. The new decoder sends an upstream force-key-unit, which becomes a PLI to the sender.
  • decodebin with async-handling. A decoder added to a running flow posted async-start to the pipeline. The pipeline went back to PAUSED and waited for every sink to preroll, and the sink of an output no stream fed never did, so the flow hung in PAUSED. This showed up with three outputs and one stream against intercom. The old code added decoders the same way, but a flow with an unfed output was rare before.
  • The assignment logic is plain bookkeeping in slot_plan.rs, with no GStreamer in it and unit tests of its own. video.rs carries out its actions on the pipeline's async thread, one change at a time under one lock.
  • Activity is counted, not timed. The BUFFER probe does one relaxed fetch_add per buffer. A periodic system-clock callback compares the counts. The callback also reconciles routing that drifted from the plan, such as a change dropped while the flow was stopping or a decoder that failed to start.

Rejected:

  • Detecting a source leaving through pad removal, rtpbin BYE or timeout events. SMB sends no BYE, and the rtpbin timeout is tens of seconds.
  • Mixing video, the counterpart of the audio liveadder. Too heavy, and a vision mixer behind several outputs does it better.
  • Relying on the WHEP SSE active/inactive events. They were dropped from the draft in -03, and almost no server sends them.

Background research on how other WHEP servers deliver multiple sources (MediaMTX, Broadcast Box, Janus, Millicast, ...), summarised: the WHEP spec allows one audio and one video m-line and no renegotiation. Servers either send one stream per URL, rewrite content behind one SSRC, or start a new SSRC on the same m-line, as SMB does. All three end up as pads that stay; the slot model covers the third and is harmless for the first two.

Evidence

  • A silent holder gives the output to a waiting stream (fill): stream::tests::fill_hands_the_output_to_a_waiting_stream_when_the_holder_goes_silent. With the release of silent streams removed from slot_plan.rs, it fails: B never reaches the output.
  • A new stream takes over at once under newest, well before the timeout: stream::tests::newest_hands_the_output_to_a_new_stream_at_once. With the take-over removed, it fails.
  • Adding a decoder keeps the flow PLAYING: stream::tests::adding_a_decoder_keeps_the_flow_playing. Without async-handling it fails with "the flow went back to PAUSED when the decoder was added".
  • A stream whose pad goes away frees its output and its decoder is removed: stream::tests::a_video_stream_that_comes_back_decodes_into_its_slot_again. With the decoder removal in stream_ended dropped, it fails with "timed out waiting for the old decoder to be removed".
  • The assignment rules (order, timeout, take-over, resume, removal, activity judged at call time): 10 unit tests in slot_plan::tests. They test the bookkeeping, not GStreamer.
  • Against Eyevinn Open Intercom locally (upstream manager main, SMB, num_video_tracks=3, fill, a separate Strom on another port): the stream decodes into video_out (thumbnail shows the publisher's camera) and the flow stays PLAYING. Before the async-handling change, the same flow hung in PAUSED.
  • Several parallel video streams from one WHEP session: unverified live. SMB sends a WHEP session one video stream, so intercom cannot produce the case, and MediaMTX and Broadcast Box send one per URL. The fill and newest guards cover it with real GStreamer elements.
  • The races found in review (assign against a stream ending, decoder removal against pad-added, a dropped assignment while stopping): fixed by serialising routing changes under one lock, a removed flag held across pad linking, and the reconcile step. Not reproduced by a test; they need a pad removal at the same instant as another routing change.

Tests

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

  • cargo test --lib whep::input: 23 passed. The four GStreamer guards in stream::tests were run five times in a row, and passed every time.
  • cargo test --test whep_input_video_test --test pipeline_lifecycle_test --test openapi_test: all passed (3, 4, 1).
  • cargo clippy --all-targets: clean.

Skipped: none. The guards need valve and input-selector (core), plus VP8 (vp8enc/vp8dec, plugins-good) as the existing WHEP tests do. All are in the CI package list.

Not run: the full test suite; Linux and Windows (CI covers them).

For the reviewer

  • The video stream timeout default is 2000 ms. It is a trade-off: lower makes fill hand over faster after a source stops, higher tolerates sources that send rarely (a screen share of a still screen). With newest the switch to a new source does not wait for it.
  • fill is the default because it keeps outputs stable for a mixer. Against intercom, newest is the better choice: a re-pinned source shows at once.
  • Flows saved before this keep their behaviour for the first stream on each output. The change is that a second stream now waits, where before it was discarded for good.
  • New strom-types constants: DEFAULT_/MIN_/MAX_WHEP_VIDEO_STREAM_TIMEOUT_MS, WHEP_VIDEO_ASSIGNMENT_FILL/NEWEST. No API or WebSocket contract changes.

Out of scope

  • Passthrough (decode=false) of encoded video. Switching then has to wait for a keyframe of the new stream and handle a codec change. This is the next step on top of the same slot model.
  • Audio: still mixed into audio_out. An audio stream whose pad goes away still stays in the liveadder; removing it is a follow-up. Per-stream audio outputs only pay off with a server that sends audio per source; SMB's rewritten audio SSRCs are speaker slots, not people.
  • Knowing which source is on an output: WHEP has no standard source id (one msid per session).

🤖 Generated with Claude Code

A video stream keeps its output while it sends. One that has been silent
for video_stream_timeout_ms (default 2000) gives it to a waiting stream:
SFUs leave the pad of a stream that stopped in place, so silence is the
signal. Streams without an output wait undecoded instead of being
discarded for the rest of the session.

video_assignment chooses what a new stream does when every output is
taken: fill (default) waits, so parallel sources keep their outputs;
newest takes over the output held longest, so a source that switches
(an SFU's pinned or active-speaker video) shows at once.

Each output is an input-selector in front of its tee, so a switch
changes the active pad instead of relinking the tee. The new decoder
asks the sender for a keyframe. decodebin runs with async-handling: a
decoder added to a running flow no longer puts the flow back to PAUSED,
which hung the flow when another output had no stream.

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

@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: WHEP Input, video routing to outputs (slot assignment, teardown)

When a stream goes silent it now gives up its video output, and streams with no output wait instead of being dropped. fill/newest choose who takes an output, with one input-selector per output. The approach looks right and is careful about lifetimes:

  • Shared state holds elements weakly.
  • The BUFFER probe only does a relaxed fetch_add (video.rs:360-366).
  • The clock timer holds a weak ref and stops itself.
  • Nothing is keyed on the block id alone.

The guards in stream::tests go through the real VideoSlots and run under STROM_REQUIRE_GST_PLUGINS.

Neighbours: #1088 and #1089 (drafts) also add to types/src/block.rs. #953 is in the same area: the new end-of-stream path (connect_unlinked, video.rs:617) relies on the source unlinking a pad when its session goes away. A trial merge into current main is clean.

  1. A decoder that fails to be added leaves the stream marked as decoding into a dead decoder, and reconcile cannot repair that (inline).
  2. Changes run one at a time but not necessarily in the order they were scheduled, so a stale batch can briefly put an older stream on an output under newest (inline, not verified).

let Some(identity) = identity else {
return;
};
if let Err(e) = pipeline.add(&decodebin) {

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.

The route is switched to Feed::Decoding (lines 521-525) before pipeline.add and sync_state_with_parent. If the add fails, the record points at a decoder that is not in the pipeline, while the stream still feeds its fakesink. reconcile re-assigns only parked holders (465-471), so it never repairs this, although its doc names "a decoder that failed to start". The opposite case, make_decodebin failing at 500, leaves the stream parked, so every tick retries and logs error! again. Marking it as decoding only after add and state sync succeed, and rate-limiting that error, would cover both. Traced, not reproduced; rare.

let Some(me) = self.me.upgrade() else {
return;
};
pipeline.call_async(move |pipeline| {

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.

ops keeps batches from overlapping, but call_async work on the shared pool is not guaranteed to run in the order it was scheduled. A batch that tick computed from an older plan can run after a newer one from register. Under newest that can decode an older stream into the slot for up to one tick, until reconcile releases it. If so, the module doc at lines 17-19 should say it. Not verified against GStreamer's pool.

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