From 1febc504c201c8c7249eaf12217a010ec5c7af28 Mon Sep 17 00:00:00 2001 From: selikhov Date: Sat, 3 Oct 2026 06:35:32 +0300 Subject: [PATCH] Refuse streams without a common codec; voelinctl streams VP8 by default On a machine with hardware encoders, voelinctl's streamer chose H.264 (the first hardware codec) and, sending through a FrameSource, offered it alone. A viewer that cannot decode H.264 (no OpenH264 library) answered with a video section that listed no payload type at all; the streamer could not parse that answer and dropped the viewer, which only saw "removed by the streamer (Failed)". A viewer now refuses such an offer with the codecs on both sides, and voelinctl's single-codec offer defaults to VP8, which every client decodes; --codec or a named --encoder still pick another. The engine's own streams are unaffected: they offer several codecs. Co-Authored-By: Claude Opus 5.5 --- crates/voelin-stream/src/peer.rs | 44 +++++++++++++++++++++++++++++--- tools/voelinctl/src/stream.rs | 9 ++++--- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/crates/voelin-stream/src/peer.rs b/crates/voelin-stream/src/peer.rs index e1e99e9..320e1c9 100644 --- a/crates/voelin-stream/src/peer.rs +++ b/crates/voelin-stream/src/peer.rs @@ -178,6 +178,11 @@ pub enum PeerError { Io(#[from] std::io::Error), #[error("invalid SDP: {0}")] Sdp(String), + /// The offer's video uses none of the codecs we decode. + #[error( + "no video codec in common: the stream offers {offered}, this client decodes {accepted}" + )] + NoCommonCodec { offered: String, accepted: String }, #[error("WebRTC: {0}")] Rtc(#[from] RtcError), #[error("no usable local address")] @@ -304,10 +309,24 @@ impl Peer { /// Viewer side: accept the streamer's offer; returns our SDP answer. pub async fn answer(config: &PeerConfig, offer: &str) -> Result<(Self, String), PeerError> { // Our codecs in the order of the offer, then the rest. - let mut codecs: Vec<_> = offered_video_codecs(offer) - .into_iter() - .filter(|c| config.accept_video_codecs.contains(c)) - .collect(); + let offered = offered_video_codecs(offer); + let mut codecs: Vec<_> = + offered.iter().copied().filter(|c| config.accept_video_codecs.contains(c)).collect(); + // Without one, the answer's video section would list no payload type, + // which no peer (str0m included) can parse. + if config.video + && codecs.is_empty() + && offer.lines().any(|l| l.trim_start().starts_with("m=video ")) + { + let names = |codecs: &[VideoCodec]| match codecs { + [] => "none we know".to_owned(), + _ => codecs.iter().map(|c| c.sdp_name()).collect::>().join(", "), + }; + return Err(PeerError::NoCommonCodec { + offered: names(&offered), + accepted: names(&config.accept_video_codecs), + }); + } for c in &config.accept_video_codecs { if !codecs.contains(c) { codecs.push(*c); @@ -924,6 +943,23 @@ mod tests { assert_eq!(offered_video_codecs(&answer), [VideoCodec::Vp8], "{answer}"); } + /// An offer of codecs we cannot decode is refused with the reason, not + /// answered with a video section the streamer cannot parse. + #[tokio::test] + async fn no_common_codec_is_refused() { + let streamer = + PeerConfig { video_codecs: vec![VideoCodec::H264], ..PeerConfig::loopback() }; + let (_peer, offer) = Peer::offer(&streamer, "s").await.unwrap(); + let vp8_only = + PeerConfig { accept_video_codecs: vec![VideoCodec::Vp8], ..PeerConfig::loopback() }; + let error = Peer::answer(&vp8_only, &offer).await.err().expect("refused"); + assert!(matches!(error, PeerError::NoCommonCodec { .. }), "{error}"); + assert_eq!( + error.to_string(), + "no video codec in common: the stream offers H264, this client decodes VP8" + ); + } + /// Several codecs offered: the streamer learns which one each viewer's /// answer chose; H.264 carries the level the stream needs. #[tokio::test] diff --git a/tools/voelinctl/src/stream.rs b/tools/voelinctl/src/stream.rs index e882a6f..93a046a 100644 --- a/tools/voelinctl/src/stream.rs +++ b/tools/voelinctl/src/stream.rs @@ -14,7 +14,7 @@ use tokio::time::{Instant, Interval, sleep_until}; use tsclientlib::prelude::*; use tsclientlib::{ClientId, Connection, MessageHandle, StreamItem}; use voelin_core::media::voelin_media::capture::SourceId; -use voelin_core::media::voelin_media::{Codecs, VideoFrame, convert}; +use voelin_core::media::voelin_media::{Codec, Codecs, VideoFrame, convert}; use voelin_core::media::{ CaptureBackend, EncodedSource, EncoderPreference, Latest, Streamer, StreamerConfig, VideoPipeline, peer_config, preferred_codec, stream_codec, video_codec, @@ -96,8 +96,8 @@ pub enum StreamCommand { /// bitrate, no encoder) instead of capturing. #[arg(long, conflicts_with_all = ["source", "synthetic"])] placeholder: bool, - /// Video codec: vp8, vp9, h264, av1 [default: the first the encoder - /// choice gives: hardware first, else VP8]. + /// Video codec: vp8, vp9, h264, av1 [default: VP8, which every client + /// decodes; with --encoder, that encoder's codec]. One codec is offered. #[arg(long)] codec: Option, /// Encoder: `auto`, `software` or a backend name from `voelinctl @@ -177,6 +177,9 @@ pub async fn run(con: &mut Connection, args: &StreamArgs) -> Result<()> { } else { let configured = match codec { Some(codec) => Some(codec.parse().map_err(|e| anyhow::anyhow!("{e}"))?), + // One codec is offered (below): by default the one every + // client decodes. A named encoder brings its own codec. + None if encoder == "auto" => Some(Codec::Vp8), None => None, }; if let Some(codec) = preferred_codec(&codecs, configured) {