Repository navigation
feat(whep input): give video outputs to the streams that are sending - #1161
Conversation
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
left a comment
There was a problem hiding this comment.
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.
- A decoder that fails to be added leaves the stream marked as decoding into a dead decoder, and
reconcilecannot repair that (inline). - 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) { |
There was a problem hiding this comment.
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| { |
There was a problem hiding this comment.
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.
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:
num_video_tracks=1the new source was discarded ("video beyond the declared video tracks") andvideo_outstayed on the old, frozen stream.whepsrcturns every new SSRC into a new pad and never removes the old one (rtpbinautoremoveis 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
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.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.newestmakes 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.input-selectorin 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.decodebinwithasync-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.slot_plan.rs, with no GStreamer in it and unit tests of its own.video.rscarries out its actions on the pipeline's async thread, one change at a time under one lock.fetch_addper 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:
active/inactiveevents. 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
fill):stream::tests::fill_hands_the_output_to_a_waiting_stream_when_the_holder_goes_silent. With the release of silent streams removed fromslot_plan.rs, it fails: B never reaches the output.newest, well before the timeout:stream::tests::newest_hands_the_output_to_a_new_stream_at_once. With the take-over removed, it fails.stream::tests::adding_a_decoder_keeps_the_flow_playing. Withoutasync-handlingit fails with "the flow went back to PAUSED when the decoder was added".stream::tests::a_video_stream_that_comes_back_decodes_into_its_slot_again. With the decoder removal instream_endeddropped, it fails with "timed out waiting for the old decoder to be removed".slot_plan::tests. They test the bookkeeping, not GStreamer.main, SMB,num_video_tracks=3,fill, a separate Strom on another port): the stream decodes intovideo_out(thumbnail shows the publisher's camera) and the flow stays PLAYING. Before theasync-handlingchange, the same flow hung in PAUSED.fillandnewestguards cover it with real GStreamer elements.assignagainst a stream ending, decoder removal againstpad-added, a dropped assignment while stopping): fixed by serialising routing changes under one lock, aremovedflag 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 instream::testswere 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
valveandinput-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
fillhand over faster after a source stops, higher tolerates sources that send rarely (a screen share of a still screen). Withnewestthe switch to a new source does not wait for it.fillis the default because it keeps outputs stable for a mixer. Against intercom,newestis the better choice: a re-pinned source shows at once.DEFAULT_/MIN_/MAX_WHEP_VIDEO_STREAM_TIMEOUT_MS,WHEP_VIDEO_ASSIGNMENT_FILL/NEWEST. No API or WebSocket contract changes.Out of scope
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_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.🤖 Generated with Claude Code