| 1 | use calling::{CallHealth, HealthSample, MediaPath, NetworkLevel}; |
| 2 | |
| 3 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 4 | pub enum HealthBanner { |
| 5 | Network, |
| 6 | Cpu, |
| 7 | } |
| 8 | |
| 9 | #[derive(Debug, Clone)] |
| 10 | pub struct HealthState { |
| 11 | pub latest: Option<CallHealth>, |
| 12 | pub incoming_video: bool, |
| 13 | network_closed: bool, |
| 14 | cpu_closed: bool, |
| 15 | } |
| 16 | |
| 17 | impl Default for HealthState { |
| 18 | fn default() -> Self { |
| 19 | HealthState { |
| 20 | latest: None, |
| 21 | incoming_video: true, |
| 22 | network_closed: false, |
| 23 | cpu_closed: false, |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | impl HealthState { |
| 29 | pub fn apply(&mut self, health: CallHealth) { |
| 30 | if health.level == NetworkLevel::Good { |
| 31 | self.network_closed = false; |
| 32 | } |
| 33 | if !health.cpu_limited { |
| 34 | self.cpu_closed = false; |
| 35 | } |
| 36 | self.latest = Some(health); |
| 37 | } |
| 38 | |
| 39 | pub fn level(&self) -> NetworkLevel { |
| 40 | self.latest |
| 41 | .as_ref() |
| 42 | .map_or(NetworkLevel::Good, |health| health.level) |
| 43 | } |
| 44 | |
| 45 | pub fn banner(&self, effects_on: bool) -> Option<HealthBanner> { |
| 46 | let health = self.latest.as_ref()?; |
| 47 | if health.level != NetworkLevel::Good && self.incoming_video && !self.network_closed { |
| 48 | Some(HealthBanner::Network) |
| 49 | } else if health.cpu_limited && effects_on && !self.cpu_closed { |
| 50 | Some(HealthBanner::Cpu) |
| 51 | } else { |
| 52 | None |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | pub fn close_banner(&mut self, banner: HealthBanner) { |
| 57 | match banner { |
| 58 | HealthBanner::Network => self.network_closed = true, |
| 59 | HealthBanner::Cpu => self.cpu_closed = true, |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | pub fn level_text(level: NetworkLevel) -> &'static str { |
| 65 | match level { |
| 66 | NetworkLevel::Good => "Good", |
| 67 | NetworkLevel::Poor => "Poor", |
| 68 | NetworkLevel::Bad => "Bad", |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | fn rate_text(kilobits_per_second: f64) -> String { |
| 73 | if kilobits_per_second >= 1000. { |
| 74 | format!( |
| 75 | "{} Mbps", |
| 76 | app_base::format::decimal_label(kilobits_per_second / 1000., 1) |
| 77 | ) |
| 78 | } else { |
| 79 | format!("{kilobits_per_second:.0} kbps") |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | pub fn network_rows(sample: &HealthSample) -> Vec<(&'static str, String)> { |
| 84 | vec![ |
| 85 | ( |
| 86 | "Packet loss", |
| 87 | format!( |
| 88 | "{}%", |
| 89 | app_base::format::decimal_label(sample.loss_percent, 1) |
| 90 | ), |
| 91 | ), |
| 92 | ("Round trip", format!("{:.0} ms", sample.round_trip_ms)), |
| 93 | ("Jitter", format!("{:.0} ms", sample.jitter_ms)), |
| 94 | ("Sent", rate_text(sample.sent_kbps)), |
| 95 | ("Received", rate_text(sample.received_kbps)), |
| 96 | ] |
| 97 | } |
| 98 | |
| 99 | pub fn transport_text(health: &CallHealth) -> String { |
| 100 | match health.transport.srtp_cipher.as_str() { |
| 101 | "" => "Transport: DTLS-SRTP".to_owned(), |
| 102 | cipher => format!("Transport: DTLS-SRTP ({cipher})"), |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | pub fn media_text(health: &CallHealth, peer_name: &str) -> Option<String> { |
| 107 | match health.path { |
| 108 | MediaPath::Direct => Some(format!("Media: direct to {peer_name}")), |
| 109 | MediaPath::Mixer => Some("Media: via Microsoft media server".to_owned()), |
| 110 | MediaPath::Pending => None, |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | #[cfg(test)] |
| 115 | mod tests { |
| 116 | use calling::TransportDetails; |
| 117 | |
| 118 | use super::*; |
| 119 | |
| 120 | fn health(level: NetworkLevel, cpu_limited: bool) -> CallHealth { |
| 121 | CallHealth { |
| 122 | level, |
| 123 | cpu_limited, |
| 124 | sample: HealthSample::default(), |
| 125 | transport: TransportDetails { |
| 126 | srtp_cipher: "AEAD_AES_128_GCM".into(), |
| 127 | ..TransportDetails::default() |
| 128 | }, |
| 129 | path: MediaPath::Direct, |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | #[test] |
| 134 | fn the_network_banner_shows_while_unstable_and_stays_closed_until_the_level_recovers() { |
| 135 | let mut state = HealthState::default(); |
| 136 | assert_eq!(state.banner(true), None); |
| 137 | state.apply(health(NetworkLevel::Poor, false)); |
| 138 | assert_eq!(state.banner(true), Some(HealthBanner::Network)); |
| 139 | state.close_banner(HealthBanner::Network); |
| 140 | state.apply(health(NetworkLevel::Bad, false)); |
| 141 | assert_eq!(state.banner(true), None); |
| 142 | state.apply(health(NetworkLevel::Good, false)); |
| 143 | state.apply(health(NetworkLevel::Poor, false)); |
| 144 | assert_eq!(state.banner(true), Some(HealthBanner::Network)); |
| 145 | } |
| 146 | |
| 147 | #[test] |
| 148 | fn the_network_banner_is_pointless_once_incoming_video_is_off() { |
| 149 | let mut state = HealthState::default(); |
| 150 | state.apply(health(NetworkLevel::Poor, false)); |
| 151 | state.incoming_video = false; |
| 152 | assert_eq!(state.banner(true), None); |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn the_cpu_banner_needs_effects_to_turn_off_and_has_its_own_close() { |
| 157 | let mut state = HealthState::default(); |
| 158 | state.apply(health(NetworkLevel::Good, true)); |
| 159 | assert_eq!(state.banner(false), None); |
| 160 | assert_eq!(state.banner(true), Some(HealthBanner::Cpu)); |
| 161 | state.close_banner(HealthBanner::Cpu); |
| 162 | state.apply(health(NetworkLevel::Good, true)); |
| 163 | assert_eq!(state.banner(true), None); |
| 164 | state.apply(health(NetworkLevel::Good, false)); |
| 165 | state.apply(health(NetworkLevel::Good, true)); |
| 166 | assert_eq!(state.banner(true), Some(HealthBanner::Cpu)); |
| 167 | } |
| 168 | |
| 169 | #[test] |
| 170 | fn the_popover_names_the_transport_and_the_media_path_without_claiming_end_to_end() { |
| 171 | let mut found = health(NetworkLevel::Good, false); |
| 172 | assert_eq!( |
| 173 | transport_text(&found), |
| 174 | "Transport: DTLS-SRTP (AEAD_AES_128_GCM)" |
| 175 | ); |
| 176 | assert_eq!( |
| 177 | media_text(&found, "Mara").as_deref(), |
| 178 | Some("Media: direct to Mara") |
| 179 | ); |
| 180 | found.path = MediaPath::Mixer; |
| 181 | assert_eq!( |
| 182 | media_text(&found, "Mara").as_deref(), |
| 183 | Some("Media: via Microsoft media server") |
| 184 | ); |
| 185 | found.path = MediaPath::Pending; |
| 186 | assert_eq!(media_text(&found, "Mara"), None); |
| 187 | } |
| 188 | |
| 189 | #[test] |
| 190 | fn rates_switch_to_megabits_from_a_thousand() { |
| 191 | let sample = HealthSample { |
| 192 | sent_kbps: 420.4, |
| 193 | received_kbps: 1180., |
| 194 | ..HealthSample::default() |
| 195 | }; |
| 196 | let rows = network_rows(&sample); |
| 197 | assert_eq!(rows[3].1, "420 kbps"); |
| 198 | assert_eq!(rows[4].1, "1.2 Mbps"); |
| 199 | } |
| 200 | } |