Step down when a connection connects but carries no media
A connection could reach "connected" and still show nothing: an SRTP library that negotiates a profile it then cannot use (official clients reportedly failed that way with AES-GCM), or a streamer that has no encoder for the codec our answer took. Nothing noticed; the viewer waited forever. Now each peer checks once per connection whether media gets through: a viewer's peer reports NoVideo (with whether audio came) when no video arrived within PeerConfig::stall_timeout (5 s), a streamer's peer reports NoFeedback when, after twice that, none of the viewer's receiver reports (or REMB) mentioned our video. Keyframe requests do not count: a viewer that gets nothing keeps asking. The streamer waits longer so a Voelin viewer steps down first. The sessions then ask for a new connection with the next lower set, logging each step: without the SRTP profile that was negotiated (both AEAD profiles when it was one of them), or, when audio came but video did not, without the video codec. A viewer sends `reconnect`; a streamer sends a `reconnectOffer` itself, for viewers that do not step down. What was left out stays out for that viewer; when nothing is left the step is logged and nothing loops. Proven with two sessions through a relay that passes the handshake and drops SRTP once AES-GCM was selected (and with a source whose video is only VP8 while the answer took VP9): each side falls back to AES_CM_128_HMAC_SHA1_80 (or VP8), the media arrives, and the working connection does not step again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c8e04cb1ac
commit
4b83b14517
4 files changed
+463
-35
No files matched your search
@@ -40,7 +40,10 @@ use crate::discovery::{ClientState, Discovery, StreamLookup};
|
||||
use crate::dtls::SrtpProfile;
|
||||
use crate::feedback::LayerFeedback;
|
||||
use crate::layer::{self, LayerId, LayerSet, LayerSpec};
|
||||
use crate::peer::{MediaFrame, OfferOptions, Peer, PeerConfig, PeerEvent, VideoCodec, VideoFormat};
|
||||
use crate::peer::{
|
||||
MediaFrame, OfferOptions, Peer, PeerConfig, PeerEvent, VideoCodec, VideoFormat,
|
||||
offered_video_codecs,
|
||||
};
|
||||
use crate::proto::{self, LeaveReason, StreamInfo, StreamNotification, StreamSetup};
|
||||
use crate::signal::Signal;
|
||||
use crate::source::EncodedFrame;
|
||||
@@ -555,6 +558,74 @@ impl LayerChoice {
|
||||
}
|
||||
}
|
||||
|
||||
/// The ladder of a connection that connects but carries no media: the next
|
||||
/// one leaves out the SRTP profile it negotiated (every AEAD profile when
|
||||
/// that was one: an SRTP library has both or neither), or the video codec
|
||||
/// it carried, as long as another is left. What was left out stays out for
|
||||
/// the later connections of that viewer or watched stream.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
struct Fallback {
|
||||
srtp: Vec<SrtpProfile>,
|
||||
codecs: Vec<VideoCodec>,
|
||||
}
|
||||
|
||||
impl Fallback {
|
||||
/// `config` without what was left out.
|
||||
fn apply(&self, config: &PeerConfig) -> PeerConfig {
|
||||
let mut config = config.clone();
|
||||
config.srtp_profiles = self.srtp_left(&config);
|
||||
config.video_codecs.retain(|c| !self.codecs.contains(c));
|
||||
config.accept_video_codecs.retain(|c| !self.codecs.contains(c));
|
||||
config
|
||||
}
|
||||
|
||||
/// The profiles of `config`'s order not left out yet.
|
||||
fn srtp_left(&self, config: &PeerConfig) -> Vec<SrtpProfile> {
|
||||
let order: &[SrtpProfile] = match &config.srtp_profiles[..] {
|
||||
[] => &SrtpProfile::DEFAULT_ORDER,
|
||||
order => order,
|
||||
};
|
||||
order.iter().copied().filter(|p| !self.srtp.contains(p)).collect()
|
||||
}
|
||||
|
||||
/// Leave out `failed` (and the other AEAD profile with an AEAD one); the
|
||||
/// profiles left, `None` (nothing left out) when none would be.
|
||||
fn without_srtp(
|
||||
&mut self,
|
||||
config: &PeerConfig,
|
||||
failed: SrtpProfile,
|
||||
) -> Option<Vec<SrtpProfile>> {
|
||||
let same = |p: &SrtpProfile| *p == failed || (p.is_aead() && failed.is_aead());
|
||||
let left: Vec<_> = self.srtp_left(config).into_iter().filter(|p| !same(p)).collect();
|
||||
if left.is_empty() {
|
||||
return None;
|
||||
}
|
||||
self.srtp.extend(SrtpProfile::ALL.into_iter().filter(same));
|
||||
Some(left)
|
||||
}
|
||||
|
||||
/// Leave out `failed` of `codecs`; those left, `None` (nothing left
|
||||
/// out) when none would be.
|
||||
fn without_codec(
|
||||
&mut self,
|
||||
codecs: &[VideoCodec],
|
||||
failed: VideoCodec,
|
||||
) -> Option<Vec<VideoCodec>> {
|
||||
let left: Vec<_> =
|
||||
codecs.iter().copied().filter(|c| *c != failed && !self.codecs.contains(c)).collect();
|
||||
if left.is_empty() {
|
||||
return None;
|
||||
}
|
||||
self.codecs.push(failed);
|
||||
Some(left)
|
||||
}
|
||||
}
|
||||
|
||||
/// `items` for a log line.
|
||||
fn list<T: std::fmt::Display>(items: &[T]) -> String {
|
||||
items.iter().map(T::to_string).collect::<Vec<_>>().join(", ")
|
||||
}
|
||||
|
||||
struct ViewerSlot {
|
||||
state: ViewerState,
|
||||
message: String,
|
||||
@@ -572,6 +643,8 @@ struct ViewerSlot {
|
||||
/// The layer the viewer asked for ([`Signal::Layer`]); `None`: its
|
||||
/// estimate decides.
|
||||
pinned: Option<LayerId>,
|
||||
/// What its connections leave out after one carried nothing.
|
||||
fallback: Fallback,
|
||||
}
|
||||
|
||||
impl ViewerSlot {
|
||||
@@ -586,6 +659,7 @@ impl ViewerSlot {
|
||||
srtp_profile: None,
|
||||
codec: None,
|
||||
pinned: None,
|
||||
fallback: Fallback::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -716,7 +790,7 @@ impl StreamerSession {
|
||||
StreamNotification::JoinRequest { viewer, message, remove: false, .. } => {
|
||||
self.viewers.insert(viewer.0, ViewerSlot::new(message.clone()));
|
||||
if self.options.auto_accept {
|
||||
self.offer(*viewer, false, out).await;
|
||||
self.offer(*viewer, false, out);
|
||||
} else {
|
||||
out.event(StreamEvent::Streamer(StreamerEvent::Request {
|
||||
viewer: *viewer,
|
||||
@@ -769,7 +843,7 @@ impl StreamerSession {
|
||||
}
|
||||
Signal::Reconnect => {
|
||||
if slot.state != ViewerState::Requested {
|
||||
self.offer(viewer, true, out).await;
|
||||
self.offer(viewer, true, out);
|
||||
}
|
||||
}
|
||||
Signal::Layer { layer } => self.pin(viewer, layer, out),
|
||||
@@ -821,8 +895,9 @@ impl StreamerSession {
|
||||
}
|
||||
|
||||
/// Create a peer for `viewer` and send our offer: in `respondjoinstreamrequest`,
|
||||
/// or as `reconnectOffer` for a viewer that lost its connection.
|
||||
async fn offer(&mut self, viewer: ClientId, reconnect: bool, out: &mut Outbox) {
|
||||
/// or as `reconnectOffer` for a viewer that lost its connection (or
|
||||
/// whose connection carried nothing, see [`Fallback`]).
|
||||
fn offer(&mut self, viewer: ClientId, reconnect: bool, out: &mut Outbox) {
|
||||
let Some(id) = self.id.clone() else { return };
|
||||
let start = self.start_bitrate();
|
||||
let layer = self.layers.specs[self.layers.fitting(start)].id;
|
||||
@@ -841,7 +916,11 @@ impl StreamerSession {
|
||||
desired_bitrate: Some(desired_bitrate),
|
||||
layers: &self.layers.specs,
|
||||
};
|
||||
match Peer::offer_with(&self.config, &id, &options).await {
|
||||
let config = match self.viewers.get(&viewer.0) {
|
||||
Some(slot) => slot.fallback.apply(&self.config),
|
||||
None => self.config.clone(),
|
||||
};
|
||||
match Peer::offer_now(&config, &id, &options) {
|
||||
Ok((peer, sdp)) => {
|
||||
// Voelin viewers learn the layers and may pick one (other
|
||||
// clients skip the attribute); not with RID simulcast, where
|
||||
@@ -894,7 +973,7 @@ impl StreamerSession {
|
||||
_ => return Err(SessionError::NoRequest(viewer.0)),
|
||||
}
|
||||
if accept {
|
||||
self.offer(viewer, false, out).await;
|
||||
self.offer(viewer, false, out);
|
||||
} else {
|
||||
self.viewers.remove(&viewer.0);
|
||||
out.request(Request::Respond { id, viewer, offer: None, accept: false });
|
||||
@@ -1152,7 +1231,31 @@ impl StreamerSession {
|
||||
slot.codec = Some(format);
|
||||
self.emit_viewers(out);
|
||||
}
|
||||
PeerEvent::Media(_) | PeerEvent::LayerMedia { .. } => {}
|
||||
PeerEvent::NoFeedback => {
|
||||
// Its SRTP fails, or nothing reaches it: offer a new
|
||||
// connection with the next lower set. A Voelin viewer steps
|
||||
// down by itself first (it waits half as long).
|
||||
let profile = slot.peer.as_ref().and_then(Peer::srtp_profile);
|
||||
let left = profile.and_then(|p| slot.fallback.without_srtp(&self.config, p));
|
||||
match (profile, left) {
|
||||
(Some(profile), Some(left)) => {
|
||||
warn!(
|
||||
viewer = viewer.0,
|
||||
"connected, but the viewer reports none of our video; offering a \
|
||||
new connection without SRTP {profile} (left: {})",
|
||||
list(&left)
|
||||
);
|
||||
self.offer(viewer, true, out);
|
||||
}
|
||||
_ => warn!(
|
||||
viewer = viewer.0,
|
||||
?profile,
|
||||
"connected, but the viewer reports none of our video; nothing left to \
|
||||
fall back to"
|
||||
),
|
||||
}
|
||||
}
|
||||
PeerEvent::Media(_) | PeerEvent::LayerMedia { .. } | PeerEvent::NoVideo { .. } => {}
|
||||
PeerEvent::Closed => {
|
||||
// The viewer may ask for a new offer (`reconnect`); it stays
|
||||
// until it leaves.
|
||||
@@ -1306,6 +1409,11 @@ pub struct ViewerSession {
|
||||
layers: Option<Vec<LayerSpec>>,
|
||||
/// The layer we asked for; `None`: the streamer's estimate decides.
|
||||
layer: Option<LayerId>,
|
||||
/// The streamer's last offer, and the codec our answer took from it.
|
||||
offer: Option<String>,
|
||||
codec: Option<VideoCodec>,
|
||||
/// What our connections leave out after one carried nothing.
|
||||
fallback: Fallback,
|
||||
}
|
||||
|
||||
impl ViewerSession {
|
||||
@@ -1330,6 +1438,9 @@ impl ViewerSession {
|
||||
layer_rid: None,
|
||||
layers: None,
|
||||
layer: None,
|
||||
offer: None,
|
||||
codec: None,
|
||||
fallback: Fallback::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1382,7 +1493,11 @@ impl ViewerSession {
|
||||
Ok(Signal::Offer { sdp, reconnect: false }) if self.peer.is_some() => {
|
||||
let peer = self.peer.as_ref().expect("checked above");
|
||||
match peer.renegotiate(&sdp).await {
|
||||
Ok(sdp) => self.signal(Signal::Answer { sdp }, out),
|
||||
Ok(answer) => {
|
||||
self.codec = offered_video_codecs(&answer).first().copied();
|
||||
self.offer = Some(sdp);
|
||||
self.signal(Signal::Answer { sdp: answer }, out);
|
||||
}
|
||||
Err(e) => {
|
||||
self.fail(format!("cannot answer the streamer's new offer: {e}"), out)
|
||||
}
|
||||
@@ -1407,10 +1522,13 @@ impl ViewerSession {
|
||||
|
||||
/// Answer an offer (the first one or a `reconnectOffer`) with a new peer.
|
||||
async fn answer(&mut self, offer: &str, out: &mut Outbox) {
|
||||
match Peer::answer(&self.config, offer).await {
|
||||
match Peer::answer(&self.fallback.apply(&self.config), offer).await {
|
||||
Ok((peer, sdp)) => {
|
||||
self.peer = Some(peer);
|
||||
self.state = WatchState::Connecting;
|
||||
// The streamer sends the first codec of our answer.
|
||||
self.codec = offered_video_codecs(&sdp).first().copied();
|
||||
self.offer = Some(offer.to_owned());
|
||||
self.signal(Signal::Answer { sdp }, out);
|
||||
let layers = layer::from_sdp(offer).filter(|l| l.len() > 1);
|
||||
if layers != self.layers {
|
||||
@@ -1479,6 +1597,46 @@ impl ViewerSession {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Connected, but no video came ([`PeerEvent::NoVideo`]): ask for a new
|
||||
/// connection with the next lower set ([`Fallback`]), without the SRTP
|
||||
/// profile this one negotiated when nothing came at all, without its
|
||||
/// video codec when audio did (or no other profile is left).
|
||||
fn fall_back(&mut self, audio: bool, out: &mut Outbox) {
|
||||
let config = self.fallback.apply(&self.config);
|
||||
let profile = self.peer.as_ref().and_then(Peer::srtp_profile);
|
||||
let mut step = None;
|
||||
if !audio && let Some(profile) = profile {
|
||||
step = self
|
||||
.fallback
|
||||
.without_srtp(&config, profile)
|
||||
.map(|left| format!("without SRTP {profile} (left: {})", list(&left)));
|
||||
}
|
||||
if step.is_none()
|
||||
&& let Some(codec) = self.codec
|
||||
{
|
||||
let offered: Vec<VideoCodec> =
|
||||
offered_video_codecs(self.offer.as_deref().unwrap_or(""))
|
||||
.into_iter()
|
||||
.filter(|c| config.accept_video_codecs.contains(c))
|
||||
.collect();
|
||||
step = self
|
||||
.fallback
|
||||
.without_codec(&offered, codec)
|
||||
.map(|left| format!("without {codec} (left: {})", list(&left)));
|
||||
}
|
||||
let Some(step) = step else {
|
||||
warn!(stream = self.id, audio, "connected, but no video; nothing left to fall back to");
|
||||
return;
|
||||
};
|
||||
warn!(
|
||||
stream = self.id,
|
||||
audio, "connected, but no video; asking for a new connection {step}"
|
||||
);
|
||||
self.peer = None;
|
||||
self.state = WatchState::Connecting;
|
||||
self.signal(Signal::Reconnect, out);
|
||||
}
|
||||
|
||||
/// Drop the connection and ask the streamer for a new offer (`reconnect`).
|
||||
pub fn reconnect(&mut self, out: &mut Outbox) {
|
||||
if matches!(self.state, WatchState::Connecting | WatchState::Connected) {
|
||||
@@ -1558,11 +1716,13 @@ impl ViewerSession {
|
||||
self.event(WatchEvent::Frame(frame), out);
|
||||
}
|
||||
}
|
||||
PeerEvent::NoVideo { audio } => self.fall_back(audio, out),
|
||||
PeerEvent::KeyframeRequest
|
||||
| PeerEvent::LayerKeyframeRequest(_)
|
||||
| PeerEvent::BitrateEstimate(_)
|
||||
| PeerEvent::Simulcast(_)
|
||||
| PeerEvent::VideoCodec(_) => {}
|
||||
| PeerEvent::VideoCodec(_)
|
||||
| PeerEvent::NoFeedback => {}
|
||||
PeerEvent::Closed => {
|
||||
if self.peer.take().is_none() {
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user