| 1 | use std::time::{Duration, Instant}; |
| 2 | |
| 3 | use calling::{ |
| 4 | CallCommand, CallState, CallUpdate, DeviceChoice, DeviceLists, EndReason, SpeakingDetector, |
| 5 | }; |
| 6 | use tokio::sync::mpsc::UnboundedSender; |
| 7 | |
| 8 | pub const TEST_CALL_TITLE: &str = "Test call"; |
| 9 | |
| 10 | pub struct ActiveCall { |
| 11 | pub id: u64, |
| 12 | pub model: CallModel, |
| 13 | pub commands: UnboundedSender<CallCommand>, |
| 14 | pub viewing: bool, |
| 15 | } |
| 16 | |
| 17 | #[derive(Debug, Clone)] |
| 18 | pub struct CallModel { |
| 19 | pub title: String, |
| 20 | pub state: CallState, |
| 21 | pub muted: bool, |
| 22 | pub listen_only: bool, |
| 23 | pub input: DeviceChoice, |
| 24 | pub output: DeviceChoice, |
| 25 | pub devices: DeviceLists, |
| 26 | pub local_speaking: bool, |
| 27 | pub remote_speaking: bool, |
| 28 | pub connected_since: Option<Instant>, |
| 29 | local_detector: SpeakingDetector, |
| 30 | remote_detector: SpeakingDetector, |
| 31 | } |
| 32 | |
| 33 | impl CallModel { |
| 34 | pub fn new(title: &str) -> Self { |
| 35 | CallModel { |
| 36 | title: title.to_owned(), |
| 37 | state: CallState::Connecting, |
| 38 | muted: false, |
| 39 | listen_only: false, |
| 40 | input: DeviceChoice::SystemDefault, |
| 41 | output: DeviceChoice::SystemDefault, |
| 42 | devices: DeviceLists::default(), |
| 43 | local_speaking: false, |
| 44 | remote_speaking: false, |
| 45 | connected_since: None, |
| 46 | local_detector: SpeakingDetector::default(), |
| 47 | remote_detector: SpeakingDetector::default(), |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub fn apply(&mut self, update: CallUpdate) { |
| 52 | match update { |
| 53 | CallUpdate::State(state) => { |
| 54 | if let Some(since) = state.started_at() { |
| 55 | self.connected_since = Some(since); |
| 56 | } |
| 57 | if !matches!(state, CallState::Connected { .. }) { |
| 58 | self.silence(); |
| 59 | } |
| 60 | self.state = state; |
| 61 | } |
| 62 | CallUpdate::Muted(muted) => { |
| 63 | self.muted = muted; |
| 64 | if muted { |
| 65 | self.local_speaking = false; |
| 66 | self.local_detector.reset(); |
| 67 | } |
| 68 | } |
| 69 | CallUpdate::Levels { local, remote } => { |
| 70 | self.local_speaking = !self.muted && self.local_detector.update(local); |
| 71 | self.remote_speaking = self.remote_detector.update(remote); |
| 72 | } |
| 73 | CallUpdate::Devices(devices) => self.devices = devices, |
| 74 | CallUpdate::Selected { input, output } => { |
| 75 | self.input = input; |
| 76 | self.output = output; |
| 77 | } |
| 78 | CallUpdate::ListenOnly(listen_only) => self.listen_only = listen_only, |
| 79 | CallUpdate::Stats { .. } => {} |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | fn silence(&mut self) { |
| 84 | self.local_speaking = false; |
| 85 | self.remote_speaking = false; |
| 86 | self.local_detector.reset(); |
| 87 | self.remote_detector.reset(); |
| 88 | } |
| 89 | |
| 90 | pub fn elapsed(&self, now: Instant) -> Option<Duration> { |
| 91 | self.connected_since |
| 92 | .map(|since| now.saturating_duration_since(since)) |
| 93 | } |
| 94 | |
| 95 | pub fn is_connecting(&self) -> bool { |
| 96 | matches!(self.state, CallState::Connecting) |
| 97 | } |
| 98 | |
| 99 | pub fn is_reconnecting(&self) -> bool { |
| 100 | matches!(self.state, CallState::Reconnecting { .. }) |
| 101 | } |
| 102 | |
| 103 | pub fn ended_reason(&self) -> Option<&EndReason> { |
| 104 | match &self.state { |
| 105 | CallState::Ended { reason } => Some(reason), |
| 106 | _ => None, |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | pub fn timer_text(&self, now: Instant) -> String { |
| 111 | self.elapsed(now).map(format_elapsed).unwrap_or_default() |
| 112 | } |
| 113 | |
| 114 | pub fn can_unmute(&self) -> bool { |
| 115 | !self.listen_only |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | pub fn format_elapsed(elapsed: Duration) -> String { |
| 120 | let seconds = elapsed.as_secs(); |
| 121 | let (hours, minutes, seconds) = (seconds / 3600, seconds / 60 % 60, seconds % 60); |
| 122 | if hours > 0 { |
| 123 | format!("{hours}:{minutes:02}:{seconds:02}") |
| 124 | } else { |
| 125 | format!("{minutes:02}:{seconds:02}") |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | pub fn ended_notice(reason: &EndReason, elapsed: Option<Duration>) -> String { |
| 130 | match (reason, elapsed) { |
| 131 | (EndReason::Cancelled, _) => "Call cancelled".to_owned(), |
| 132 | (EndReason::Dropped, _) => "Call dropped".to_owned(), |
| 133 | (EndReason::Failed(message), _) => format!("Test call failed: {message}"), |
| 134 | (EndReason::LocalHangup | EndReason::RemoteEnded, Some(elapsed)) => { |
| 135 | format!("Call ended {}", format_elapsed(elapsed)) |
| 136 | } |
| 137 | (EndReason::LocalHangup | EndReason::RemoteEnded, None) => "Call ended".to_owned(), |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | #[cfg(test)] |
| 142 | mod tests { |
| 143 | use super::*; |
| 144 | |
| 145 | fn connected_model() -> CallModel { |
| 146 | let mut model = CallModel::new(TEST_CALL_TITLE); |
| 147 | model.apply(CallUpdate::State(CallState::Connected { |
| 148 | since: Instant::now(), |
| 149 | })); |
| 150 | model |
| 151 | } |
| 152 | |
| 153 | #[test] |
| 154 | fn elapsed_text_is_minutes_and_seconds() { |
| 155 | assert_eq!(format_elapsed(Duration::from_secs(42)), "00:42"); |
| 156 | assert_eq!(format_elapsed(Duration::from_secs(61)), "01:01"); |
| 157 | assert_eq!(format_elapsed(Duration::from_secs(3725)), "1:02:05"); |
| 158 | } |
| 159 | |
| 160 | #[test] |
| 161 | fn levels_drive_the_speaking_rings() { |
| 162 | let mut model = connected_model(); |
| 163 | model.apply(CallUpdate::Levels { |
| 164 | local: 0.0, |
| 165 | remote: 0.4, |
| 166 | }); |
| 167 | assert!(model.remote_speaking); |
| 168 | assert!(!model.local_speaking); |
| 169 | model.apply(CallUpdate::Levels { |
| 170 | local: 0.4, |
| 171 | remote: 0.0, |
| 172 | }); |
| 173 | assert!(model.local_speaking); |
| 174 | } |
| 175 | |
| 176 | #[test] |
| 177 | fn muting_switches_the_local_ring_off_at_once() { |
| 178 | let mut model = connected_model(); |
| 179 | model.apply(CallUpdate::Levels { |
| 180 | local: 0.5, |
| 181 | remote: 0.0, |
| 182 | }); |
| 183 | assert!(model.local_speaking); |
| 184 | model.apply(CallUpdate::Muted(true)); |
| 185 | assert!(!model.local_speaking); |
| 186 | model.apply(CallUpdate::Levels { |
| 187 | local: 0.5, |
| 188 | remote: 0.0, |
| 189 | }); |
| 190 | assert!(!model.local_speaking); |
| 191 | } |
| 192 | |
| 193 | #[test] |
| 194 | fn reconnecting_keeps_the_timer_and_clears_the_rings() { |
| 195 | let mut model = connected_model(); |
| 196 | let since = model.connected_since.unwrap(); |
| 197 | model.apply(CallUpdate::Levels { |
| 198 | local: 0.5, |
| 199 | remote: 0.5, |
| 200 | }); |
| 201 | model.apply(CallUpdate::State(CallState::Reconnecting { since })); |
| 202 | assert!(model.is_reconnecting()); |
| 203 | assert_eq!(model.connected_since, Some(since)); |
| 204 | assert!(!model.remote_speaking && !model.local_speaking); |
| 205 | } |
| 206 | |
| 207 | #[test] |
| 208 | fn notices_for_every_end_reason() { |
| 209 | let elapsed = Some(Duration::from_secs(42)); |
| 210 | assert_eq!(ended_notice(&EndReason::LocalHangup, elapsed), "Call ended 00:42"); |
| 211 | assert_eq!(ended_notice(&EndReason::RemoteEnded, elapsed), "Call ended 00:42"); |
| 212 | assert_eq!(ended_notice(&EndReason::Cancelled, None), "Call cancelled"); |
| 213 | assert_eq!(ended_notice(&EndReason::Dropped, elapsed), "Call dropped"); |
| 214 | assert_eq!( |
| 215 | ended_notice(&EndReason::Failed("no route".into()), None), |
| 216 | "Test call failed: no route" |
| 217 | ); |
| 218 | } |
| 219 | |
| 220 | #[test] |
| 221 | fn listen_only_blocks_unmuting() { |
| 222 | let mut model = connected_model(); |
| 223 | assert!(model.can_unmute()); |
| 224 | model.apply(CallUpdate::ListenOnly(true)); |
| 225 | assert!(!model.can_unmute()); |
| 226 | } |
| 227 | } |