| 1 | use std::collections::BTreeMap; |
| 2 | |
| 3 | use calling::{CallHealth, Limitation, NetworkLevel, StatsSample}; |
| 4 | |
| 5 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 6 | pub struct Series { |
| 7 | pub min: f64, |
| 8 | pub max: f64, |
| 9 | pub last: f64, |
| 10 | pub samples: u32, |
| 11 | } |
| 12 | |
| 13 | impl Series { |
| 14 | fn new(value: f64) -> Self { |
| 15 | Series { |
| 16 | min: value, |
| 17 | max: value, |
| 18 | last: value, |
| 19 | samples: 1, |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | fn observe(&mut self, value: f64) { |
| 24 | self.min = self.min.min(value); |
| 25 | self.max = self.max.max(value); |
| 26 | self.last = value; |
| 27 | self.samples += 1; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | #[derive(Debug, Clone, Default, PartialEq)] |
| 32 | pub struct StatsSeries { |
| 33 | values: BTreeMap<&'static str, Series>, |
| 34 | pub stalled_audio_in: u32, |
| 35 | pub stalled_audio_out: u32, |
| 36 | pub level_samples: [u32; 3], |
| 37 | pub limitation_samples: [u32; 3], |
| 38 | pub cpu_limited_samples: u32, |
| 39 | pub level_changes: Vec<(u128, &'static str)>, |
| 40 | pub last_level: Option<NetworkLevel>, |
| 41 | } |
| 42 | |
| 43 | pub fn level_name(level: NetworkLevel) -> &'static str { |
| 44 | match level { |
| 45 | NetworkLevel::Good => "good", |
| 46 | NetworkLevel::Poor => "poor", |
| 47 | NetworkLevel::Bad => "bad", |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub fn limitation_name(limitation: Limitation) -> &'static str { |
| 52 | match limitation { |
| 53 | Limitation::None => "none", |
| 54 | Limitation::Cpu => "cpu", |
| 55 | Limitation::Bandwidth => "bandwidth", |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl StatsSeries { |
| 60 | pub fn values(&self) -> &BTreeMap<&'static str, Series> { |
| 61 | &self.values |
| 62 | } |
| 63 | |
| 64 | fn observe(&mut self, name: &'static str, value: f64) { |
| 65 | match self.values.get_mut(name) { |
| 66 | Some(series) => series.observe(value), |
| 67 | None => { |
| 68 | self.values.insert(name, Series::new(value)); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | pub fn observe_sample( |
| 74 | &mut self, |
| 75 | sample: &StatsSample, |
| 76 | others_unmuted: bool, |
| 77 | own_unmuted: bool, |
| 78 | ) { |
| 79 | let video = &sample.video; |
| 80 | if let Some(before) = self.values.get("audio_in_packets") |
| 81 | && others_unmuted |
| 82 | && sample.audio_in_packets <= before.last as u64 |
| 83 | { |
| 84 | self.stalled_audio_in += 1; |
| 85 | } |
| 86 | if let Some(before) = self.values.get("audio_out_packets") |
| 87 | && own_unmuted |
| 88 | && sample.audio_out_packets <= before.last as u64 |
| 89 | { |
| 90 | self.stalled_audio_out += 1; |
| 91 | } |
| 92 | let readings: [(&'static str, f64); 16] = [ |
| 93 | ("audio_in_packets", sample.audio_in_packets as f64), |
| 94 | ("audio_out_packets", sample.audio_out_packets as f64), |
| 95 | ("audio_in_level", sample.audio_in_level), |
| 96 | ("audio_out_level", sample.audio_out_level), |
| 97 | ("video_in_packets", video.inbound_packets as f64), |
| 98 | ( |
| 99 | "video_in_frames_decoded", |
| 100 | video.inbound_frames_decoded as f64, |
| 101 | ), |
| 102 | ("video_in_width", f64::from(video.inbound_width)), |
| 103 | ("video_in_height", f64::from(video.inbound_height)), |
| 104 | ("video_out_packets", video.outbound_packets as f64), |
| 105 | ( |
| 106 | "video_out_frames_encoded", |
| 107 | video.outbound_frames_encoded as f64, |
| 108 | ), |
| 109 | ("video_out_width", f64::from(video.outbound_width)), |
| 110 | ("video_out_height", f64::from(video.outbound_height)), |
| 111 | ("video_remote_round_trip_ms", video.remote_round_trip_ms), |
| 112 | ("share_in_packets", sample.share_in_packets as f64), |
| 113 | ("share_out_packets", sample.share_out_packets as f64), |
| 114 | ("video_frames_published", video.frames_published as f64), |
| 115 | ]; |
| 116 | for (name, value) in readings { |
| 117 | self.observe(name, value); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | pub fn observe_health(&mut self, health: &CallHealth, at_ms: u128) { |
| 122 | let sample = &health.sample; |
| 123 | for (name, value) in [ |
| 124 | ("loss_percent", sample.loss_percent), |
| 125 | ("round_trip_ms", sample.round_trip_ms), |
| 126 | ("jitter_ms", sample.jitter_ms), |
| 127 | ("sent_kbps", sample.sent_kbps), |
| 128 | ("received_kbps", sample.received_kbps), |
| 129 | ] { |
| 130 | self.observe(name, value); |
| 131 | } |
| 132 | self.level_samples[health.level as usize] += 1; |
| 133 | self.limitation_samples[sample.limitation as usize] += 1; |
| 134 | if health.cpu_limited { |
| 135 | self.cpu_limited_samples += 1; |
| 136 | } |
| 137 | if self.last_level != Some(health.level) { |
| 138 | self.level_changes.push((at_ms, level_name(health.level))); |
| 139 | self.last_level = Some(health.level); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | #[cfg(test)] |
| 145 | mod tests { |
| 146 | use calling::{HealthSample, MediaPath, TransportDetails, call::VideoReport}; |
| 147 | |
| 148 | use super::*; |
| 149 | |
| 150 | fn sample(audio_in: u64) -> StatsSample { |
| 151 | StatsSample { |
| 152 | audio_in_packets: audio_in, |
| 153 | audio_out_packets: audio_in * 2, |
| 154 | video: VideoReport { |
| 155 | inbound_width: 1280, |
| 156 | inbound_height: 720, |
| 157 | ..VideoReport::default() |
| 158 | }, |
| 159 | ..StatsSample::default() |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | fn health(level: NetworkLevel, loss_percent: f64) -> CallHealth { |
| 164 | CallHealth { |
| 165 | level, |
| 166 | cpu_limited: false, |
| 167 | sample: HealthSample { |
| 168 | loss_percent, |
| 169 | ..HealthSample::default() |
| 170 | }, |
| 171 | transport: TransportDetails::default(), |
| 172 | path: MediaPath::Direct, |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | #[test] |
| 177 | fn samples_keep_min_max_last_and_count_stalls() { |
| 178 | let mut series = StatsSeries::default(); |
| 179 | for packets in [10, 20, 20, 35] { |
| 180 | series.observe_sample(&sample(packets), true, true); |
| 181 | } |
| 182 | let audio_in = series.values()["audio_in_packets"]; |
| 183 | assert_eq!( |
| 184 | (audio_in.min, audio_in.max, audio_in.last, audio_in.samples), |
| 185 | (10., 35., 35., 4) |
| 186 | ); |
| 187 | assert_eq!(series.stalled_audio_in, 1); |
| 188 | assert_eq!(series.values()["video_in_width"].last, 1280.); |
| 189 | } |
| 190 | |
| 191 | #[test] |
| 192 | fn stalls_only_count_while_the_sender_could_be_heard() { |
| 193 | let mut series = StatsSeries::default(); |
| 194 | for packets in [10, 10, 10] { |
| 195 | series.observe_sample(&sample(packets), false, false); |
| 196 | } |
| 197 | assert_eq!((series.stalled_audio_in, series.stalled_audio_out), (0, 0)); |
| 198 | series.observe_sample(&sample(10), true, false); |
| 199 | assert_eq!((series.stalled_audio_in, series.stalled_audio_out), (1, 0)); |
| 200 | series.observe_sample(&sample(10), false, true); |
| 201 | assert_eq!((series.stalled_audio_in, series.stalled_audio_out), (1, 1)); |
| 202 | } |
| 203 | |
| 204 | #[test] |
| 205 | fn health_keeps_ranges_and_level_changes() { |
| 206 | let mut series = StatsSeries::default(); |
| 207 | series.observe_health(&health(NetworkLevel::Good, 0.), 2000); |
| 208 | series.observe_health(&health(NetworkLevel::Poor, 7.), 4000); |
| 209 | series.observe_health(&health(NetworkLevel::Poor, 6.), 6000); |
| 210 | assert_eq!(series.values()["loss_percent"].max, 7.); |
| 211 | assert_eq!(series.level_samples, [1, 2, 0]); |
| 212 | assert_eq!(series.level_changes, [(2000, "good"), (4000, "poor")]); |
| 213 | } |
| 214 | } |