| 1 | use std::collections::VecDeque; |
| 2 | use std::time::{Duration, Instant}; |
| 3 | |
| 4 | use libwebrtc::stats::{QualityLimitationReason, RtcStats}; |
| 5 | |
| 6 | use crate::renegotiation::MediaPath; |
| 7 | |
| 8 | const SUSTAIN: Duration = Duration::from_secs(5); |
| 9 | const RECOVERY: Duration = Duration::from_secs(10); |
| 10 | const LIMIT_WINDOW: Duration = Duration::from_secs(10); |
| 11 | const LIMIT_SHARE: f32 = 0.5; |
| 12 | const POOR_LOSS_PERCENT: f64 = 5.; |
| 13 | const POOR_ROUND_TRIP_MS: f64 = 400.; |
| 14 | const POOR_JITTER_MS: f64 = 30.; |
| 15 | const BAD_LOSS_PERCENT: f64 = 10.; |
| 16 | const BAD_ROUND_TRIP_MS: f64 = 500.; |
| 17 | |
| 18 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
| 19 | pub enum NetworkLevel { |
| 20 | Good, |
| 21 | Poor, |
| 22 | Bad, |
| 23 | } |
| 24 | |
| 25 | impl NetworkLevel { |
| 26 | fn better(self) -> Self { |
| 27 | match self { |
| 28 | NetworkLevel::Bad => NetworkLevel::Poor, |
| 29 | _ => NetworkLevel::Good, |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 35 | pub enum Limitation { |
| 36 | #[default] |
| 37 | None, |
| 38 | Cpu, |
| 39 | Bandwidth, |
| 40 | } |
| 41 | |
| 42 | #[derive(Debug, Clone, Copy, PartialEq, Default)] |
| 43 | pub struct HealthSample { |
| 44 | pub loss_percent: f64, |
| 45 | pub round_trip_ms: f64, |
| 46 | pub jitter_ms: f64, |
| 47 | pub sent_kbps: f64, |
| 48 | pub received_kbps: f64, |
| 49 | pub limitation: Limitation, |
| 50 | } |
| 51 | |
| 52 | impl HealthSample { |
| 53 | fn is_bad(&self) -> bool { |
| 54 | self.loss_percent > BAD_LOSS_PERCENT || self.round_trip_ms > BAD_ROUND_TRIP_MS |
| 55 | } |
| 56 | |
| 57 | fn is_poor(&self) -> bool { |
| 58 | self.is_bad() |
| 59 | || self.loss_percent > POOR_LOSS_PERCENT |
| 60 | || self.round_trip_ms > POOR_ROUND_TRIP_MS |
| 61 | || self.jitter_ms > POOR_JITTER_MS |
| 62 | } |
| 63 | |
| 64 | fn instant_level(&self) -> NetworkLevel { |
| 65 | if self.is_bad() { |
| 66 | NetworkLevel::Bad |
| 67 | } else if self.is_poor() { |
| 68 | NetworkLevel::Poor |
| 69 | } else { |
| 70 | NetworkLevel::Good |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | #[derive(Debug, Clone, PartialEq, Eq, Default)] |
| 76 | pub struct TransportDetails { |
| 77 | pub dtls_state: String, |
| 78 | pub tls_version: String, |
| 79 | pub dtls_cipher: String, |
| 80 | pub srtp_cipher: String, |
| 81 | pub local_fingerprint: String, |
| 82 | pub remote_fingerprint: String, |
| 83 | } |
| 84 | |
| 85 | #[derive(Debug, Clone, PartialEq)] |
| 86 | pub struct CallHealth { |
| 87 | pub level: NetworkLevel, |
| 88 | pub cpu_limited: bool, |
| 89 | pub sample: HealthSample, |
| 90 | pub transport: TransportDetails, |
| 91 | pub path: MediaPath, |
| 92 | } |
| 93 | |
| 94 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 95 | pub struct Quality { |
| 96 | pub level: NetworkLevel, |
| 97 | pub cpu_limited: bool, |
| 98 | } |
| 99 | |
| 100 | pub struct QualityTracker { |
| 101 | level: NetworkLevel, |
| 102 | poor_since: Option<Instant>, |
| 103 | bad_since: Option<Instant>, |
| 104 | recovering_since: Option<Instant>, |
| 105 | limits: VecDeque<(Instant, Limitation)>, |
| 106 | } |
| 107 | |
| 108 | impl Default for QualityTracker { |
| 109 | fn default() -> Self { |
| 110 | QualityTracker { |
| 111 | level: NetworkLevel::Good, |
| 112 | poor_since: None, |
| 113 | bad_since: None, |
| 114 | recovering_since: None, |
| 115 | limits: VecDeque::new(), |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | fn run_since(since: &mut Option<Instant>, holds: bool, now: Instant) -> bool { |
| 121 | if !holds { |
| 122 | *since = None; |
| 123 | return false; |
| 124 | } |
| 125 | now.duration_since(*since.get_or_insert(now)) >= SUSTAIN |
| 126 | } |
| 127 | |
| 128 | impl QualityTracker { |
| 129 | pub fn observe(&mut self, now: Instant, sample: &HealthSample) -> Quality { |
| 130 | self.limits.push_back((now, sample.limitation)); |
| 131 | while self |
| 132 | .limits |
| 133 | .front() |
| 134 | .is_some_and(|(at, _)| now.duration_since(*at) > LIMIT_WINDOW) |
| 135 | { |
| 136 | self.limits.pop_front(); |
| 137 | } |
| 138 | let bad = run_since(&mut self.bad_since, sample.is_bad(), now); |
| 139 | let poor = run_since(&mut self.poor_since, sample.is_poor(), now); |
| 140 | let target = if bad { |
| 141 | NetworkLevel::Bad |
| 142 | } else if poor { |
| 143 | NetworkLevel::Poor |
| 144 | } else { |
| 145 | NetworkLevel::Good |
| 146 | }; |
| 147 | if target > self.level { |
| 148 | self.level = target; |
| 149 | self.recovering_since = None; |
| 150 | } else if sample.instant_level() < self.level { |
| 151 | let since = *self.recovering_since.get_or_insert(now); |
| 152 | if now.duration_since(since) >= RECOVERY { |
| 153 | self.level = self.level.better(); |
| 154 | self.recovering_since = None; |
| 155 | } |
| 156 | } else { |
| 157 | self.recovering_since = None; |
| 158 | } |
| 159 | Quality { |
| 160 | level: self.level, |
| 161 | cpu_limited: self.share_of(Limitation::Cpu, now) > LIMIT_SHARE, |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | fn share_of(&self, limitation: Limitation, now: Instant) -> f32 { |
| 166 | let spans_window = self |
| 167 | .limits |
| 168 | .front() |
| 169 | .is_some_and(|(at, _)| now.duration_since(*at) >= SUSTAIN); |
| 170 | if !spans_window { |
| 171 | return 0.; |
| 172 | } |
| 173 | let matching = self |
| 174 | .limits |
| 175 | .iter() |
| 176 | .filter(|(_, found)| *found == limitation) |
| 177 | .count(); |
| 178 | matching as f32 / self.limits.len() as f32 |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | #[derive(Debug, Clone, Copy, PartialEq, Default)] |
| 183 | pub struct HealthCounters { |
| 184 | pub packets_received: u64, |
| 185 | pub packets_lost: u64, |
| 186 | pub bytes_sent: u64, |
| 187 | pub bytes_received: u64, |
| 188 | pub jitter_ms: f64, |
| 189 | pub round_trip_ms: f64, |
| 190 | pub outbound_loss_percent: f64, |
| 191 | pub limitation: Limitation, |
| 192 | } |
| 193 | |
| 194 | #[derive(Default)] |
| 195 | pub struct HealthSampler { |
| 196 | previous: Option<(Instant, HealthCounters)>, |
| 197 | } |
| 198 | |
| 199 | impl HealthSampler { |
| 200 | pub fn sample(&mut self, now: Instant, counters: HealthCounters) -> Option<HealthSample> { |
| 201 | let (then, before) = self.previous.replace((now, counters))?; |
| 202 | let seconds = now.duration_since(then).as_secs_f64(); |
| 203 | if seconds <= 0. { |
| 204 | return None; |
| 205 | } |
| 206 | let received = counters |
| 207 | .packets_received |
| 208 | .saturating_sub(before.packets_received); |
| 209 | let lost = counters.packets_lost.saturating_sub(before.packets_lost); |
| 210 | let inbound_loss_percent = if received + lost == 0 { |
| 211 | 0. |
| 212 | } else { |
| 213 | lost as f64 * 100. / (received + lost) as f64 |
| 214 | }; |
| 215 | let kbps = |
| 216 | |after: u64, before: u64| after.saturating_sub(before) as f64 * 8. / seconds / 1000.; |
| 217 | Some(HealthSample { |
| 218 | loss_percent: inbound_loss_percent.max(counters.outbound_loss_percent), |
| 219 | round_trip_ms: counters.round_trip_ms, |
| 220 | jitter_ms: counters.jitter_ms, |
| 221 | sent_kbps: kbps(counters.bytes_sent, before.bytes_sent), |
| 222 | received_kbps: kbps(counters.bytes_received, before.bytes_received), |
| 223 | limitation: counters.limitation, |
| 224 | }) |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | fn limitation_of(reason: QualityLimitationReason) -> Limitation { |
| 229 | match reason { |
| 230 | QualityLimitationReason::Cpu => Limitation::Cpu, |
| 231 | QualityLimitationReason::Bandwidth => Limitation::Bandwidth, |
| 232 | _ => Limitation::None, |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | pub fn health_from_stats(stats: &[RtcStats]) -> (HealthCounters, TransportDetails) { |
| 237 | let mut counters = HealthCounters::default(); |
| 238 | let mut transport = TransportDetails::default(); |
| 239 | let mut certificate_ids = (String::new(), String::new()); |
| 240 | let mut selected_pair_id = String::new(); |
| 241 | let mut remote_round_trip_ms = 0f64; |
| 242 | for entry in stats { |
| 243 | match entry { |
| 244 | RtcStats::InboundRtp(inbound) => { |
| 245 | counters.packets_received += inbound.received.packets_received; |
| 246 | counters.packets_lost += inbound.received.packets_lost.max(0) as u64; |
| 247 | counters.bytes_received += inbound.inbound.bytes_received; |
| 248 | if inbound.stream.kind == "audio" { |
| 249 | counters.jitter_ms = counters.jitter_ms.max(inbound.received.jitter * 1000.); |
| 250 | } |
| 251 | } |
| 252 | RtcStats::OutboundRtp(outbound) => { |
| 253 | counters.bytes_sent += outbound.sent.bytes_sent; |
| 254 | if outbound.stream.kind == "video" && outbound.outbound.active { |
| 255 | let limitation = limitation_of(outbound.outbound.quality_limitation_reason); |
| 256 | counters.limitation = counters.limitation.max_with(limitation); |
| 257 | } |
| 258 | } |
| 259 | RtcStats::RemoteInboundRtp(remote) => { |
| 260 | counters.outbound_loss_percent = counters |
| 261 | .outbound_loss_percent |
| 262 | .max(remote.remote_inbound.fraction_lost * 100.); |
| 263 | remote_round_trip_ms = |
| 264 | remote_round_trip_ms.max(remote.remote_inbound.round_trip_time * 1000.); |
| 265 | } |
| 266 | RtcStats::Transport(found) => { |
| 267 | transport.dtls_state = found |
| 268 | .transport |
| 269 | .dtls_state |
| 270 | .map(|state| format!("{state:?}")) |
| 271 | .unwrap_or_default(); |
| 272 | transport.tls_version = found.transport.tls_version.clone(); |
| 273 | transport.dtls_cipher = found.transport.dtls_cipher.clone(); |
| 274 | transport.srtp_cipher = found.transport.srtp_cipher.clone(); |
| 275 | certificate_ids = ( |
| 276 | found.transport.local_certificate_id.clone(), |
| 277 | found.transport.remote_certificate_id.clone(), |
| 278 | ); |
| 279 | selected_pair_id = found.transport.selected_candidate_pair_id.clone(); |
| 280 | } |
| 281 | _ => {} |
| 282 | } |
| 283 | } |
| 284 | let fingerprint = |certificate_id: &str| { |
| 285 | stats |
| 286 | .iter() |
| 287 | .find_map(|entry| match entry { |
| 288 | RtcStats::Certificate(certificate) if certificate.rtc.id == certificate_id => { |
| 289 | Some(format!( |
| 290 | "{} {}", |
| 291 | certificate.certificate.fingerprint_algorithm, |
| 292 | certificate.certificate.fingerprint |
| 293 | )) |
| 294 | } |
| 295 | _ => None, |
| 296 | }) |
| 297 | .unwrap_or_default() |
| 298 | }; |
| 299 | transport.local_fingerprint = fingerprint(&certificate_ids.0); |
| 300 | transport.remote_fingerprint = fingerprint(&certificate_ids.1); |
| 301 | let pair_round_trip_ms = stats.iter().find_map(|entry| match entry { |
| 302 | RtcStats::CandidatePair(pair) if pair.rtc.id == selected_pair_id => { |
| 303 | Some(pair.candidate_pair.current_round_trip_time * 1000.) |
| 304 | } |
| 305 | _ => None, |
| 306 | }); |
| 307 | counters.round_trip_ms = pair_round_trip_ms |
| 308 | .filter(|milliseconds| *milliseconds > 0.) |
| 309 | .unwrap_or(remote_round_trip_ms); |
| 310 | (counters, transport) |
| 311 | } |
| 312 | |
| 313 | impl Limitation { |
| 314 | fn max_with(self, other: Limitation) -> Limitation { |
| 315 | match (self, other) { |
| 316 | (Limitation::Cpu, _) | (_, Limitation::Cpu) => Limitation::Cpu, |
| 317 | (Limitation::Bandwidth, _) | (_, Limitation::Bandwidth) => Limitation::Bandwidth, |
| 318 | _ => Limitation::None, |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | #[cfg(test)] |
| 324 | mod tests { |
| 325 | use libwebrtc::stats::dictionaries::{ |
| 326 | CandidatePairStats, CertificateStats, InboundRtpStreamStats, OutboundRtpStreamStats, |
| 327 | ReceivedRtpStreamStats, RtcStats as RtcStatsBase, RtpStreamStats, SentRtpStreamStats, |
| 328 | TransportStats as TransportDictionary, |
| 329 | }; |
| 330 | use libwebrtc::stats::{ |
| 331 | CandidatePairStats as PairEntry, CertificateStats as CertificateEntry, InboundRtpStats, |
| 332 | OutboundRtpStats, TransportStats, |
| 333 | }; |
| 334 | |
| 335 | use super::*; |
| 336 | |
| 337 | fn at(start: Instant, seconds: u64) -> Instant { |
| 338 | start + Duration::from_secs(seconds) |
| 339 | } |
| 340 | |
| 341 | fn lossy(loss_percent: f64) -> HealthSample { |
| 342 | HealthSample { |
| 343 | loss_percent, |
| 344 | ..HealthSample::default() |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | fn feed( |
| 349 | tracker: &mut QualityTracker, |
| 350 | start: Instant, |
| 351 | from: u64, |
| 352 | to: u64, |
| 353 | sample: &HealthSample, |
| 354 | ) -> Quality { |
| 355 | let mut quality = Quality { |
| 356 | level: NetworkLevel::Good, |
| 357 | cpu_limited: false, |
| 358 | }; |
| 359 | for seconds in (from..=to).step_by(2) { |
| 360 | quality = tracker.observe(at(start, seconds), sample); |
| 361 | } |
| 362 | quality |
| 363 | } |
| 364 | |
| 365 | #[test] |
| 366 | fn a_brief_spike_does_not_degrade_the_level() { |
| 367 | let (mut tracker, start) = (QualityTracker::default(), Instant::now()); |
| 368 | assert_eq!( |
| 369 | feed(&mut tracker, start, 0, 2, &lossy(8.)).level, |
| 370 | NetworkLevel::Good |
| 371 | ); |
| 372 | assert_eq!( |
| 373 | feed(&mut tracker, start, 4, 20, &lossy(0.)).level, |
| 374 | NetworkLevel::Good |
| 375 | ); |
| 376 | } |
| 377 | |
| 378 | #[test] |
| 379 | fn poor_needs_five_seconds_of_loss_over_five_percent() { |
| 380 | let (mut tracker, start) = (QualityTracker::default(), Instant::now()); |
| 381 | assert_eq!( |
| 382 | feed(&mut tracker, start, 0, 4, &lossy(6.)).level, |
| 383 | NetworkLevel::Good |
| 384 | ); |
| 385 | assert_eq!( |
| 386 | feed(&mut tracker, start, 6, 6, &lossy(6.)).level, |
| 387 | NetworkLevel::Poor |
| 388 | ); |
| 389 | } |
| 390 | |
| 391 | #[test] |
| 392 | fn round_trip_and_jitter_also_make_it_poor() { |
| 393 | for sample in [ |
| 394 | HealthSample { |
| 395 | round_trip_ms: 450., |
| 396 | ..Default::default() |
| 397 | }, |
| 398 | HealthSample { |
| 399 | jitter_ms: 35., |
| 400 | ..Default::default() |
| 401 | }, |
| 402 | ] { |
| 403 | let (mut tracker, start) = (QualityTracker::default(), Instant::now()); |
| 404 | assert_eq!( |
| 405 | feed(&mut tracker, start, 0, 6, &sample).level, |
| 406 | NetworkLevel::Poor |
| 407 | ); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | #[test] |
| 412 | fn bad_needs_five_seconds_over_ten_percent_loss_or_500_ms() { |
| 413 | let (mut tracker, start) = (QualityTracker::default(), Instant::now()); |
| 414 | assert_eq!( |
| 415 | feed(&mut tracker, start, 0, 6, &lossy(12.)).level, |
| 416 | NetworkLevel::Bad |
| 417 | ); |
| 418 | let (mut tracker, start) = (QualityTracker::default(), Instant::now()); |
| 419 | let slow = HealthSample { |
| 420 | round_trip_ms: 600., |
| 421 | ..Default::default() |
| 422 | }; |
| 423 | assert_eq!( |
| 424 | feed(&mut tracker, start, 0, 6, &slow).level, |
| 425 | NetworkLevel::Bad |
| 426 | ); |
| 427 | } |
| 428 | |
| 429 | #[test] |
| 430 | fn the_level_drops_fast_and_recovers_one_step_per_ten_seconds() { |
| 431 | let (mut tracker, start) = (QualityTracker::default(), Instant::now()); |
| 432 | assert_eq!( |
| 433 | feed(&mut tracker, start, 0, 6, &lossy(12.)).level, |
| 434 | NetworkLevel::Bad |
| 435 | ); |
| 436 | assert_eq!( |
| 437 | feed(&mut tracker, start, 8, 16, &lossy(0.)).level, |
| 438 | NetworkLevel::Bad |
| 439 | ); |
| 440 | assert_eq!( |
| 441 | feed(&mut tracker, start, 18, 18, &lossy(0.)).level, |
| 442 | NetworkLevel::Poor |
| 443 | ); |
| 444 | assert_eq!( |
| 445 | feed(&mut tracker, start, 20, 28, &lossy(0.)).level, |
| 446 | NetworkLevel::Poor |
| 447 | ); |
| 448 | assert_eq!( |
| 449 | feed(&mut tracker, start, 30, 30, &lossy(0.)).level, |
| 450 | NetworkLevel::Good |
| 451 | ); |
| 452 | } |
| 453 | |
| 454 | #[test] |
| 455 | fn a_relapse_restarts_the_recovery_timer() { |
| 456 | let (mut tracker, start) = (QualityTracker::default(), Instant::now()); |
| 457 | feed(&mut tracker, start, 0, 6, &lossy(7.)); |
| 458 | feed(&mut tracker, start, 8, 16, &lossy(0.)); |
| 459 | feed(&mut tracker, start, 18, 18, &lossy(7.)); |
| 460 | assert_eq!( |
| 461 | feed(&mut tracker, start, 20, 28, &lossy(0.)).level, |
| 462 | NetworkLevel::Poor |
| 463 | ); |
| 464 | assert_eq!( |
| 465 | feed(&mut tracker, start, 30, 30, &lossy(0.)).level, |
| 466 | NetworkLevel::Good |
| 467 | ); |
| 468 | } |
| 469 | |
| 470 | #[test] |
| 471 | fn bandwidth_limits_alone_keep_it_good() { |
| 472 | let (mut tracker, start) = (QualityTracker::default(), Instant::now()); |
| 473 | let limited = HealthSample { |
| 474 | limitation: Limitation::Bandwidth, |
| 475 | ..Default::default() |
| 476 | }; |
| 477 | assert_eq!( |
| 478 | feed(&mut tracker, start, 0, 30, &limited).level, |
| 479 | NetworkLevel::Good |
| 480 | ); |
| 481 | } |
| 482 | |
| 483 | #[test] |
| 484 | fn cpu_limits_raise_their_own_flag_without_touching_the_level() { |
| 485 | let (mut tracker, start) = (QualityTracker::default(), Instant::now()); |
| 486 | let limited = HealthSample { |
| 487 | limitation: Limitation::Cpu, |
| 488 | ..Default::default() |
| 489 | }; |
| 490 | let quality = feed(&mut tracker, start, 0, 6, &limited); |
| 491 | assert!(quality.cpu_limited); |
| 492 | assert_eq!(quality.level, NetworkLevel::Good); |
| 493 | assert!(!feed(&mut tracker, start, 8, 20, &HealthSample::default()).cpu_limited); |
| 494 | } |
| 495 | |
| 496 | #[test] |
| 497 | fn the_sampler_turns_counter_deltas_into_loss_and_bitrate() { |
| 498 | let (mut sampler, start) = (HealthSampler::default(), Instant::now()); |
| 499 | let first = HealthCounters { |
| 500 | packets_received: 100, |
| 501 | packets_lost: 0, |
| 502 | bytes_sent: 1000, |
| 503 | bytes_received: 2000, |
| 504 | ..Default::default() |
| 505 | }; |
| 506 | assert_eq!(sampler.sample(start, first), None); |
| 507 | let second = HealthCounters { |
| 508 | packets_received: 190, |
| 509 | packets_lost: 10, |
| 510 | bytes_sent: 51_000, |
| 511 | bytes_received: 27_000, |
| 512 | round_trip_ms: 80., |
| 513 | jitter_ms: 4., |
| 514 | ..first |
| 515 | }; |
| 516 | let sample = sampler.sample(at(start, 2), second).unwrap(); |
| 517 | assert_eq!(sample.loss_percent, 10.); |
| 518 | assert_eq!(sample.sent_kbps, 200.); |
| 519 | assert_eq!(sample.received_kbps, 100.); |
| 520 | assert_eq!((sample.round_trip_ms, sample.jitter_ms), (80., 4.)); |
| 521 | } |
| 522 | |
| 523 | #[test] |
| 524 | fn the_sampler_reports_outbound_loss_from_the_remote_report() { |
| 525 | let (mut sampler, start) = (HealthSampler::default(), Instant::now()); |
| 526 | sampler.sample(start, HealthCounters::default()); |
| 527 | let counters = HealthCounters { |
| 528 | outbound_loss_percent: 7., |
| 529 | ..Default::default() |
| 530 | }; |
| 531 | assert_eq!( |
| 532 | sampler.sample(at(start, 2), counters).unwrap().loss_percent, |
| 533 | 7. |
| 534 | ); |
| 535 | } |
| 536 | |
| 537 | #[test] |
| 538 | fn stats_parse_into_counters_and_transport_details() { |
| 539 | let stats = vec![ |
| 540 | RtcStats::InboundRtp(InboundRtpStats { |
| 541 | stream: RtpStreamStats { |
| 542 | kind: "audio".into(), |
| 543 | ..Default::default() |
| 544 | }, |
| 545 | received: ReceivedRtpStreamStats { |
| 546 | packets_received: 50, |
| 547 | packets_lost: 5, |
| 548 | jitter: 0.012, |
| 549 | }, |
| 550 | inbound: InboundRtpStreamStats { |
| 551 | bytes_received: 4000, |
| 552 | ..Default::default() |
| 553 | }, |
| 554 | ..Default::default() |
| 555 | }), |
| 556 | RtcStats::OutboundRtp(OutboundRtpStats { |
| 557 | stream: RtpStreamStats { |
| 558 | kind: "video".into(), |
| 559 | ..Default::default() |
| 560 | }, |
| 561 | sent: SentRtpStreamStats { |
| 562 | bytes_sent: 9000, |
| 563 | ..Default::default() |
| 564 | }, |
| 565 | outbound: OutboundRtpStreamStats { |
| 566 | active: true, |
| 567 | quality_limitation_reason: QualityLimitationReason::Cpu, |
| 568 | ..Default::default() |
| 569 | }, |
| 570 | ..Default::default() |
| 571 | }), |
| 572 | RtcStats::Transport(TransportStats { |
| 573 | transport: TransportDictionary { |
| 574 | dtls_cipher: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256".into(), |
| 575 | srtp_cipher: "AEAD_AES_128_GCM".into(), |
| 576 | tls_version: "FEFD".into(), |
| 577 | local_certificate_id: "CF1".into(), |
| 578 | remote_certificate_id: "CF2".into(), |
| 579 | selected_candidate_pair_id: "CP1".into(), |
| 580 | ..Default::default() |
| 581 | }, |
| 582 | ..Default::default() |
| 583 | }), |
| 584 | RtcStats::CandidatePair(PairEntry { |
| 585 | rtc: RtcStatsBase { |
| 586 | id: "CP1".into(), |
| 587 | ..Default::default() |
| 588 | }, |
| 589 | candidate_pair: CandidatePairStats { |
| 590 | current_round_trip_time: 0.045, |
| 591 | ..Default::default() |
| 592 | }, |
| 593 | }), |
| 594 | RtcStats::Certificate(CertificateEntry { |
| 595 | rtc: RtcStatsBase { |
| 596 | id: "CF1".into(), |
| 597 | ..Default::default() |
| 598 | }, |
| 599 | certificate: CertificateStats { |
| 600 | fingerprint: "AA:BB".into(), |
| 601 | fingerprint_algorithm: "sha-256".into(), |
| 602 | ..Default::default() |
| 603 | }, |
| 604 | }), |
| 605 | ]; |
| 606 | let (counters, transport) = health_from_stats(&stats); |
| 607 | assert_eq!((counters.packets_received, counters.packets_lost), (50, 5)); |
| 608 | assert_eq!((counters.bytes_received, counters.bytes_sent), (4000, 9000)); |
| 609 | assert_eq!(counters.jitter_ms, 12.); |
| 610 | assert_eq!(counters.round_trip_ms, 45.); |
| 611 | assert_eq!(counters.limitation, Limitation::Cpu); |
| 612 | assert_eq!(transport.srtp_cipher, "AEAD_AES_128_GCM"); |
| 613 | assert_eq!(transport.local_fingerprint, "sha-256 AA:BB"); |
| 614 | assert_eq!(transport.remote_fingerprint, ""); |
| 615 | } |
| 616 | |
| 617 | #[test] |
| 618 | fn inactive_video_streams_do_not_count_as_limited() { |
| 619 | let stats = vec![RtcStats::OutboundRtp(OutboundRtpStats { |
| 620 | stream: RtpStreamStats { |
| 621 | kind: "video".into(), |
| 622 | ..Default::default() |
| 623 | }, |
| 624 | outbound: OutboundRtpStreamStats { |
| 625 | active: false, |
| 626 | quality_limitation_reason: QualityLimitationReason::Bandwidth, |
| 627 | ..Default::default() |
| 628 | }, |
| 629 | ..Default::default() |
| 630 | })]; |
| 631 | assert_eq!(health_from_stats(&stats).0.limitation, Limitation::None); |
| 632 | } |
| 633 | } |