BlueGit

ict rusty-teams

fix(calls): 1:1 answers stop waiting for slow TCP and TLS relays

- The answer goes out 200 ms after the first UDP relay candidate instead of after 500 ms without any new candidate
- Late TCP/TLS relays kept resetting that quiet timer; networks without UDP still wait for gathering to finish
- Timeline logs how long gathering took

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

metis Daniel Däschle · pushed by metis

Authored 2026-11-12 · 2 weeks ago

Hash
33b94c34d8b222ebe636fb089238aaf926c8289a
Parent
8d2272df
1 file changed +30 −15
crates/calling/src/call.rs +30 −15
@@ -1,7 +1,7 @@
1 1 use std::collections::BTreeMap;
2 2 use std::path::PathBuf;
3 3 use std::sync::Arc;
4 use std::sync::atomic::{AtomicBool, Ordering};
4 use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
5 5 use std::time::Duration;
6 6
7 7 use chatsvc::TrouterCallback;
@@ -93,7 +93,7 @@ const BOT_PREFIX: &str = "28:";
93 93 const RECORDING_START_LIMIT: Duration = Duration::from_secs(40);
94 94 const SAME_PEER_ANSWER_LIMIT: Duration = Duration::from_secs(40);
95 95 const GATHERING_LIMIT: Duration = Duration::from_secs(5);
96 const GATHERING_QUIET: Duration = Duration::from_millis(500);
96 const RELAY_SETTLE: Duration = Duration::from_millis(200);
97 97 const GATHERING_POLL: Duration = Duration::from_millis(50);
98 98 const NEXT_PEER_LIMIT: Duration = Duration::from_secs(40);
99 99 const GLARE_RESEND_LIMIT: Duration = Duration::from_secs(3);
@@ -286,19 +286,19 @@ struct PeerSession {
286 286 struct Gathered {
287 287 candidates: Arc<std::sync::Mutex<Vec<String>>>,
288 288 complete: Arc<AtomicBool>,
289 relay_seen: Arc<AtomicBool>,
290 last_candidate: Arc<std::sync::Mutex<Option<Instant>>>,
289 relay_seen_at: Arc<std::sync::Mutex<Option<Instant>>>,
290 waited_ms: Arc<AtomicU64>,
291 }
292
293 /// libwebrtc gathers continually here, and TCP/TLS relays trickle in late, so a UDP relay plus a short settle is enough.
294 fn gathering_settled(complete: bool, since_udp_relay: Option<Duration>) -> bool {
295 complete || since_udp_relay.is_some_and(|since| since >= RELAY_SETTLE)
291 296 }
292 297
293 298 impl Gathered {
294 /// libwebrtc gathers continually here, so "done" is a relay candidate followed by a quiet spell.
295 299 fn settled(&self) -> bool {
296 let quiet = self
297 .last_candidate
298 .lock()
299 .expect("candidate time lock")
300 .is_some_and(|at| at.elapsed() >= GATHERING_QUIET);
301 self.complete.load(Ordering::SeqCst) || (self.relay_seen.load(Ordering::SeqCst) && quiet)
300 let since_udp_relay = self.relay_seen_at.lock().expect("relay time lock").map(|at| at.elapsed());
301 gathering_settled(self.complete.load(Ordering::SeqCst), since_udp_relay)
302 302 }
303 303
304 304 async fn candidates(&self) -> Vec<String> {
@@ -307,8 +307,14 @@ impl Gathered {
307 307 tokio::time::sleep(GATHERING_POLL).await;
308 308 }
309 309 self.complete.store(true, Ordering::SeqCst);
310 self.waited_ms.store(waited.elapsed().as_millis() as u64, Ordering::SeqCst);
310 311 self.candidates.lock().expect("candidates lock").clone()
311 312 }
313
314 fn summary(&self) -> String {
315 let count = self.candidates.lock().expect("candidates lock").len();
316 format!("{count} candidate(s) after {} ms", self.waited_ms.load(Ordering::SeqCst))
317 }
312 318 }
313 319
314 320 async fn direct_sdp(teams_sdp: &str, gathered: Option<&Gathered>, kind: SdpKind) -> Result<String> {
@@ -513,15 +519,13 @@ fn open_peer(factory: &PeerConnectionFactory, grant: &RelayGrant, relay_host: &s
513 519 let data_channel = with_data_channel.then(|| open_data_channel(&peer, sender.clone())).transpose()?;
514 520 let gathered = Gathered::default();
515 521 let candidate_sink = gathered.candidates.clone();
516 let relay_seen = gathered.relay_seen.clone();
517 let last_candidate = gathered.last_candidate.clone();
522 let relay_seen_at = gathered.relay_seen_at.clone();
518 523 peer.on_ice_candidate(Some(Box::new(move |candidate| {
519 524 if candidate.sdp_mline_index() == 0 {
520 525 let text = candidate.candidate();
521 526 if is_udp_relay(&text) {
522 relay_seen.store(true, Ordering::SeqCst);
527 relay_seen_at.lock().expect("relay time lock").get_or_insert_with(Instant::now);
523 528 }
524 *last_candidate.lock().expect("candidate time lock") = Some(Instant::now());
525 529 candidate_sink.lock().expect("candidates lock").push(text);
526 530 }
527 531 })));
@@ -958,6 +962,9 @@ async fn drive(
958 962 layout_plan = answered.remote.plan();
959 963 direct_video_mid = answered.camera_mid.clone();
960 964 timeline.record("setRemoteDescription", format!("offer applied, {} signaled line(s)", answered.remote.lines.len()));
965 if one_to_one {
966 timeline.record("ice gathered", current.gathered.summary());
967 }
961 968 timeline.record_sdp("remote offer", &teams_offer);
962 969 timeline.record_sdp("local answer", &answered.answer_sdp);
963 970 dump_sdp(options, "4-teams-answer.sdp", &answered.answer_sdp);
@@ -3697,6 +3704,14 @@ async fn collect_stats(peer: &PeerConnection, share_mids: &[String]) -> StatsSna
3697 3704
3698 3705 #[cfg(test)]
3699 3706 mod tests {
3707 #[test]
3708 fn gathering_settles_shortly_after_a_udp_relay_or_when_complete() {
3709 assert!(!gathering_settled(false, None));
3710 assert!(!gathering_settled(false, Some(Duration::from_millis(50))));
3711 assert!(gathering_settled(false, Some(RELAY_SETTLE)));
3712 assert!(gathering_settled(true, None));
3713 }
3714
3700 3715 #[tokio::test]
3701 3716 async fn a_hanging_setup_step_ends_on_cancel_or_after_the_step_timeout() {
3702 3717 let (handle, mut control) = crate::control::call_channel();
@@ -1,7 +1,7 @@
1use std::collections::BTreeMap;
1use std::collections::BTreeMap;
2use std::path::PathBuf;
2use std::path::PathBuf;
3use std::sync::Arc;
3use std::sync::Arc;
4use std::sync::atomic::{AtomicBool, Ordering};
4use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
5use std::time::Duration;
5use std::time::Duration;
6
6
7use chatsvc::TrouterCallback;
7use chatsvc::TrouterCallback;
@@ -93,7 +93,7 @@ const BOT_PREFIX: &str = "28:";
93const RECORDING_START_LIMIT: Duration = Duration::from_secs(40);
93const RECORDING_START_LIMIT: Duration = Duration::from_secs(40);
94const SAME_PEER_ANSWER_LIMIT: Duration = Duration::from_secs(40);
94const SAME_PEER_ANSWER_LIMIT: Duration = Duration::from_secs(40);
95const GATHERING_LIMIT: Duration = Duration::from_secs(5);
95const GATHERING_LIMIT: Duration = Duration::from_secs(5);
96const GATHERING_QUIET: Duration = Duration::from_millis(500);
96const RELAY_SETTLE: Duration = Duration::from_millis(200);
97const GATHERING_POLL: Duration = Duration::from_millis(50);
97const GATHERING_POLL: Duration = Duration::from_millis(50);
98const NEXT_PEER_LIMIT: Duration = Duration::from_secs(40);
98const NEXT_PEER_LIMIT: Duration = Duration::from_secs(40);
99const GLARE_RESEND_LIMIT: Duration = Duration::from_secs(3);
99const GLARE_RESEND_LIMIT: Duration = Duration::from_secs(3);
@@ -286,19 +286,19 @@ struct PeerSession {
286struct Gathered {
286struct Gathered {
287 candidates: Arc<std::sync::Mutex<Vec<String>>>,
287 candidates: Arc<std::sync::Mutex<Vec<String>>>,
288 complete: Arc<AtomicBool>,
288 complete: Arc<AtomicBool>,
289 relay_seen: Arc<AtomicBool>,
289 relay_seen_at: Arc<std::sync::Mutex<Option<Instant>>>,
290 last_candidate: Arc<std::sync::Mutex<Option<Instant>>>,
290 waited_ms: Arc<AtomicU64>,
291}
292
293/// libwebrtc gathers continually here, and TCP/TLS relays trickle in late, so a UDP relay plus a short settle is enough.
294fn gathering_settled(complete: bool, since_udp_relay: Option<Duration>) -> bool {
295 complete || since_udp_relay.is_some_and(|since| since >= RELAY_SETTLE)
291}
296}
292
297
293impl Gathered {
298impl Gathered {
294 /// libwebrtc gathers continually here, so "done" is a relay candidate followed by a quiet spell.
295 fn settled(&self) -> bool {
299 fn settled(&self) -> bool {
296 let quiet = self
300 let since_udp_relay = self.relay_seen_at.lock().expect("relay time lock").map(|at| at.elapsed());
297 .last_candidate
301 gathering_settled(self.complete.load(Ordering::SeqCst), since_udp_relay)
298 .lock()
299 .expect("candidate time lock")
300 .is_some_and(|at| at.elapsed() >= GATHERING_QUIET);
301 self.complete.load(Ordering::SeqCst) || (self.relay_seen.load(Ordering::SeqCst) && quiet)
302 }
302 }
303
303
304 async fn candidates(&self) -> Vec<String> {
304 async fn candidates(&self) -> Vec<String> {
@@ -307,8 +307,14 @@ impl Gathered {
307 tokio::time::sleep(GATHERING_POLL).await;
307 tokio::time::sleep(GATHERING_POLL).await;
308 }
308 }
309 self.complete.store(true, Ordering::SeqCst);
309 self.complete.store(true, Ordering::SeqCst);
310 self.waited_ms.store(waited.elapsed().as_millis() as u64, Ordering::SeqCst);
310 self.candidates.lock().expect("candidates lock").clone()
311 self.candidates.lock().expect("candidates lock").clone()
311 }
312 }
313
314 fn summary(&self) -> String {
315 let count = self.candidates.lock().expect("candidates lock").len();
316 format!("{count} candidate(s) after {} ms", self.waited_ms.load(Ordering::SeqCst))
317 }
312}
318}
313
319
314async fn direct_sdp(teams_sdp: &str, gathered: Option<&Gathered>, kind: SdpKind) -> Result<String> {
320async fn direct_sdp(teams_sdp: &str, gathered: Option<&Gathered>, kind: SdpKind) -> Result<String> {
@@ -513,15 +519,13 @@ fn open_peer(factory: &PeerConnectionFactory, grant: &RelayGrant, relay_host: &s
513 let data_channel = with_data_channel.then(|| open_data_channel(&peer, sender.clone())).transpose()?;
519 let data_channel = with_data_channel.then(|| open_data_channel(&peer, sender.clone())).transpose()?;
514 let gathered = Gathered::default();
520 let gathered = Gathered::default();
515 let candidate_sink = gathered.candidates.clone();
521 let candidate_sink = gathered.candidates.clone();
516 let relay_seen = gathered.relay_seen.clone();
522 let relay_seen_at = gathered.relay_seen_at.clone();
517 let last_candidate = gathered.last_candidate.clone();
518 peer.on_ice_candidate(Some(Box::new(move |candidate| {
523 peer.on_ice_candidate(Some(Box::new(move |candidate| {
519 if candidate.sdp_mline_index() == 0 {
524 if candidate.sdp_mline_index() == 0 {
520 let text = candidate.candidate();
525 let text = candidate.candidate();
521 if is_udp_relay(&text) {
526 if is_udp_relay(&text) {
522 relay_seen.store(true, Ordering::SeqCst);
527 relay_seen_at.lock().expect("relay time lock").get_or_insert_with(Instant::now);
523 }
528 }
524 *last_candidate.lock().expect("candidate time lock") = Some(Instant::now());
525 candidate_sink.lock().expect("candidates lock").push(text);
529 candidate_sink.lock().expect("candidates lock").push(text);
526 }
530 }
527 })));
531 })));
@@ -958,6 +962,9 @@ async fn drive(
958 layout_plan = answered.remote.plan();
962 layout_plan = answered.remote.plan();
959 direct_video_mid = answered.camera_mid.clone();
963 direct_video_mid = answered.camera_mid.clone();
960 timeline.record("setRemoteDescription", format!("offer applied, {} signaled line(s)", answered.remote.lines.len()));
964 timeline.record("setRemoteDescription", format!("offer applied, {} signaled line(s)", answered.remote.lines.len()));
965 if one_to_one {
966 timeline.record("ice gathered", current.gathered.summary());
967 }
961 timeline.record_sdp("remote offer", &teams_offer);
968 timeline.record_sdp("remote offer", &teams_offer);
962 timeline.record_sdp("local answer", &answered.answer_sdp);
969 timeline.record_sdp("local answer", &answered.answer_sdp);
963 dump_sdp(options, "4-teams-answer.sdp", &answered.answer_sdp);
970 dump_sdp(options, "4-teams-answer.sdp", &answered.answer_sdp);
@@ -3697,6 +3704,14 @@ async fn collect_stats(peer: &PeerConnection, share_mids: &[String]) -> StatsSna
3697
3704
3698#[cfg(test)]
3705#[cfg(test)]
3699mod tests {
3706mod tests {
3707 #[test]
3708 fn gathering_settles_shortly_after_a_udp_relay_or_when_complete() {
3709 assert!(!gathering_settled(false, None));
3710 assert!(!gathering_settled(false, Some(Duration::from_millis(50))));
3711 assert!(gathering_settled(false, Some(RELAY_SETTLE)));
3712 assert!(gathering_settled(true, None));
3713 }
3714
3700 #[tokio::test]
3715 #[tokio::test]
3701 async fn a_hanging_setup_step_ends_on_cancel_or_after_the_step_timeout() {
3716 async fn a_hanging_setup_step_ends_on_cancel_or_after_the_step_timeout() {
3702 let (handle, mut control) = crate::control::call_channel();
3717 let (handle, mut control) = crate::control::call_channel();