| 1 | use std::sync::Arc; |
| 2 | use std::time::{Duration, Instant}; |
| 3 | |
| 4 | use calling::{ |
| 5 | CallCommand, CallState, CallUpdate, CameraDevice, DeviceChoice, DeviceLists, EndKind, EndReason, Progress, RosterEntry, |
| 6 | ShareKind, ShareSource, SpeakingDetector, VideoHub, |
| 7 | }; |
| 8 | use tokio::sync::mpsc::UnboundedSender; |
| 9 | |
| 10 | use super::pictures::CallPictures; |
| 11 | |
| 12 | pub const TEST_CALL_TITLE: &str = "Test call"; |
| 13 | pub const ECHO_NAME: &str = "Teams echo"; |
| 14 | pub const ECHO_MRI: &str = calling::signaling::ECHO_BOT_MRI; |
| 15 | pub const MAX_TILES: usize = 9; |
| 16 | const ORGID_PREFIX: &str = "8:orgid:"; |
| 17 | const WIDE_TILES: usize = 2; |
| 18 | const MEDIUM_TILES: usize = 4; |
| 19 | |
| 20 | pub struct ActiveCall { |
| 21 | pub id: u64, |
| 22 | pub model: CallModel, |
| 23 | pub commands: UnboundedSender<CallCommand>, |
| 24 | pub viewing: bool, |
| 25 | pub conversation_id: Option<String>, |
| 26 | pub video: Arc<VideoHub>, |
| 27 | pub pictures: CallPictures, |
| 28 | pub stage_fullscreen: bool, |
| 29 | } |
| 30 | |
| 31 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 32 | pub enum CallKind { |
| 33 | Test, |
| 34 | Direct, |
| 35 | Group, |
| 36 | Meeting, |
| 37 | } |
| 38 | |
| 39 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 40 | pub enum TileState { |
| 41 | Invited, |
| 42 | Present, |
| 43 | InLobby, |
| 44 | Left, |
| 45 | } |
| 46 | |
| 47 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 48 | pub struct Tile { |
| 49 | pub mri: String, |
| 50 | pub user_id: Option<String>, |
| 51 | pub name: String, |
| 52 | pub muted: bool, |
| 53 | pub state: TileState, |
| 54 | pub invited: bool, |
| 55 | pub has_video: bool, |
| 56 | } |
| 57 | |
| 58 | impl Tile { |
| 59 | pub fn invited(mri: &str, name: &str) -> Tile { |
| 60 | Tile { |
| 61 | mri: mri.to_owned(), |
| 62 | user_id: mri.strip_prefix(ORGID_PREFIX).map(str::to_owned), |
| 63 | name: name.to_owned(), |
| 64 | muted: false, |
| 65 | state: TileState::Invited, |
| 66 | invited: true, |
| 67 | has_video: false, |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 73 | pub struct GridLayout { |
| 74 | pub shown_others: usize, |
| 75 | pub hidden: usize, |
| 76 | } |
| 77 | |
| 78 | pub fn grid_layout(others: usize) -> GridLayout { |
| 79 | let total = others + 1; |
| 80 | if total <= MAX_TILES { |
| 81 | return GridLayout { |
| 82 | shown_others: others, |
| 83 | hidden: 0, |
| 84 | }; |
| 85 | } |
| 86 | let shown_others = MAX_TILES - 2; |
| 87 | GridLayout { |
| 88 | shown_others, |
| 89 | hidden: others - shown_others, |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 94 | pub struct TileSize { |
| 95 | pub width: f32, |
| 96 | pub height: f32, |
| 97 | pub avatar: f32, |
| 98 | } |
| 99 | |
| 100 | pub fn tile_size(cells: usize) -> TileSize { |
| 101 | if cells <= WIDE_TILES { |
| 102 | TileSize { |
| 103 | width: 260., |
| 104 | height: 210., |
| 105 | avatar: 96., |
| 106 | } |
| 107 | } else if cells <= MEDIUM_TILES { |
| 108 | TileSize { |
| 109 | width: 220., |
| 110 | height: 180., |
| 111 | avatar: 72., |
| 112 | } |
| 113 | } else { |
| 114 | TileSize { |
| 115 | width: 190., |
| 116 | height: 150., |
| 117 | avatar: 56., |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | #[derive(Debug, Clone)] |
| 123 | pub struct CallModel { |
| 124 | pub title: String, |
| 125 | pub kind: CallKind, |
| 126 | pub peer_name: String, |
| 127 | pub state: CallState, |
| 128 | pub progress: Option<Progress>, |
| 129 | pub muted: bool, |
| 130 | pub listen_only: bool, |
| 131 | pub lobby: bool, |
| 132 | pub can_end_meeting: bool, |
| 133 | pub incoming: bool, |
| 134 | pub input: DeviceChoice, |
| 135 | pub output: DeviceChoice, |
| 136 | pub devices: DeviceLists, |
| 137 | pub local_speaking: bool, |
| 138 | pub remote_speaking: bool, |
| 139 | pub speaking_mris: Vec<String>, |
| 140 | pub connected_since: Option<Instant>, |
| 141 | pub own_mri: Option<String>, |
| 142 | pub tiles: Vec<Tile>, |
| 143 | pub screen_sharer: Option<String>, |
| 144 | pub camera_on: bool, |
| 145 | pub cameras: Vec<CameraDevice>, |
| 146 | pub camera: DeviceChoice, |
| 147 | pub local_share: Option<String>, |
| 148 | pub share_sources: Vec<ShareSource>, |
| 149 | local_detector: SpeakingDetector, |
| 150 | remote_detector: SpeakingDetector, |
| 151 | } |
| 152 | |
| 153 | impl CallModel { |
| 154 | fn base(kind: CallKind, title: &str, peer_name: &str, tiles: Vec<Tile>) -> Self { |
| 155 | CallModel { |
| 156 | title: title.to_owned(), |
| 157 | kind, |
| 158 | peer_name: peer_name.to_owned(), |
| 159 | state: CallState::Connecting, |
| 160 | progress: None, |
| 161 | muted: false, |
| 162 | listen_only: false, |
| 163 | lobby: false, |
| 164 | can_end_meeting: false, |
| 165 | incoming: false, |
| 166 | input: DeviceChoice::SystemDefault, |
| 167 | output: DeviceChoice::SystemDefault, |
| 168 | devices: DeviceLists::default(), |
| 169 | local_speaking: false, |
| 170 | remote_speaking: false, |
| 171 | speaking_mris: Vec::new(), |
| 172 | connected_since: None, |
| 173 | own_mri: None, |
| 174 | tiles, |
| 175 | screen_sharer: None, |
| 176 | camera_on: false, |
| 177 | cameras: Vec::new(), |
| 178 | camera: DeviceChoice::SystemDefault, |
| 179 | local_share: None, |
| 180 | share_sources: Vec::new(), |
| 181 | local_detector: SpeakingDetector::default(), |
| 182 | remote_detector: SpeakingDetector::default(), |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | pub fn test() -> Self { |
| 187 | Self::base( |
| 188 | CallKind::Test, |
| 189 | TEST_CALL_TITLE, |
| 190 | ECHO_NAME, |
| 191 | vec![Tile::invited(ECHO_MRI, ECHO_NAME)], |
| 192 | ) |
| 193 | } |
| 194 | |
| 195 | pub fn people(title: &str, callees: &[(String, String)]) -> Self { |
| 196 | let kind = if callees.len() == 1 { CallKind::Direct } else { CallKind::Group }; |
| 197 | let peer_name = match callees { |
| 198 | [(_, name)] => name.clone(), |
| 199 | _ => title.to_owned(), |
| 200 | }; |
| 201 | let tiles = callees.iter().map(|(mri, name)| Tile::invited(mri, name)).collect(); |
| 202 | Self::base(kind, title, &peer_name, tiles) |
| 203 | } |
| 204 | |
| 205 | pub fn meeting(title: &str, can_end_meeting: bool) -> Self { |
| 206 | let mut model = Self::base(CallKind::Meeting, title, title, Vec::new()); |
| 207 | model.can_end_meeting = can_end_meeting; |
| 208 | model |
| 209 | } |
| 210 | |
| 211 | pub fn incoming(title: &str, caller_mri: &str, caller_name: &str) -> Self { |
| 212 | let mut model = Self::base( |
| 213 | CallKind::Direct, |
| 214 | title, |
| 215 | caller_name, |
| 216 | vec![Tile { |
| 217 | state: TileState::Present, |
| 218 | ..Tile::invited(caller_mri, caller_name) |
| 219 | }], |
| 220 | ); |
| 221 | model.incoming = true; |
| 222 | model |
| 223 | } |
| 224 | |
| 225 | pub fn apply(&mut self, update: CallUpdate) { |
| 226 | match update { |
| 227 | CallUpdate::State(state) => { |
| 228 | if let Some(since) = state.started_at() { |
| 229 | self.connected_since = Some(since); |
| 230 | } |
| 231 | if !matches!(state, CallState::Connected { .. }) { |
| 232 | self.silence(); |
| 233 | } |
| 234 | self.state = state; |
| 235 | } |
| 236 | CallUpdate::Muted(muted) => { |
| 237 | self.muted = muted; |
| 238 | if muted { |
| 239 | self.local_speaking = false; |
| 240 | self.local_detector.reset(); |
| 241 | } |
| 242 | } |
| 243 | CallUpdate::Levels { local, remote } => { |
| 244 | self.local_speaking = !self.muted && self.local_detector.update(local); |
| 245 | self.remote_speaking = self.remote_detector.update(remote); |
| 246 | } |
| 247 | CallUpdate::Devices(devices) => self.devices = devices, |
| 248 | CallUpdate::Selected { input, output } => { |
| 249 | self.input = input; |
| 250 | self.output = output; |
| 251 | } |
| 252 | CallUpdate::ListenOnly(listen_only) => self.listen_only = listen_only, |
| 253 | CallUpdate::Stats { .. } => {} |
| 254 | CallUpdate::Progress(progress) => self.progress = Some(progress), |
| 255 | CallUpdate::Roster(entries) => self.apply_roster(&entries), |
| 256 | CallUpdate::Speakers(mris) => self.speaking_mris = mris, |
| 257 | CallUpdate::Lobby(lobby) => self.lobby = lobby, |
| 258 | CallUpdate::OwnIdentity { mri } => self.own_mri = Some(mri), |
| 259 | CallUpdate::ScreenShare(sharer) => self.screen_sharer = sharer, |
| 260 | CallUpdate::VideoReady | CallUpdate::Notice(_) => {} |
| 261 | CallUpdate::Camera(on) => self.camera_on = on, |
| 262 | CallUpdate::Cameras(cameras) => self.cameras = cameras, |
| 263 | CallUpdate::LocalShare(label) => self.local_share = label, |
| 264 | CallUpdate::ShareSources(sources) => self.share_sources = sources, |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | fn silence(&mut self) { |
| 269 | self.local_speaking = false; |
| 270 | self.remote_speaking = false; |
| 271 | self.speaking_mris.clear(); |
| 272 | self.local_detector.reset(); |
| 273 | self.remote_detector.reset(); |
| 274 | } |
| 275 | |
| 276 | fn apply_roster(&mut self, entries: &[RosterEntry]) { |
| 277 | for entry in entries.iter().filter(|entry| Some(&entry.mri) != self.own_mri.as_ref()) { |
| 278 | let state = if entry.in_lobby { TileState::InLobby } else { TileState::Present }; |
| 279 | match self.tiles.iter_mut().find(|tile| tile.mri == entry.mri) { |
| 280 | Some(tile) => { |
| 281 | tile.state = state; |
| 282 | tile.muted = entry.muted; |
| 283 | tile.has_video = entry.has_video; |
| 284 | if tile.name.is_empty() { |
| 285 | tile.name = entry.display_name.clone(); |
| 286 | } |
| 287 | } |
| 288 | None => self.tiles.push(Tile { |
| 289 | mri: entry.mri.clone(), |
| 290 | user_id: entry.mri.strip_prefix(ORGID_PREFIX).map(str::to_owned), |
| 291 | name: entry.display_name.clone(), |
| 292 | muted: entry.muted, |
| 293 | state, |
| 294 | invited: false, |
| 295 | has_video: entry.has_video, |
| 296 | }), |
| 297 | } |
| 298 | } |
| 299 | let own_mri = self.own_mri.clone(); |
| 300 | let listed = |mri: &str| entries.iter().any(|entry| entry.mri == mri); |
| 301 | self.tiles.retain_mut(|tile| { |
| 302 | if listed(&tile.mri) || Some(&tile.mri) == own_mri.as_ref() { |
| 303 | return true; |
| 304 | } |
| 305 | match (tile.invited, tile.state) { |
| 306 | (true, TileState::Invited) => true, |
| 307 | (true, _) => { |
| 308 | tile.state = TileState::Left; |
| 309 | true |
| 310 | } |
| 311 | (false, _) => false, |
| 312 | } |
| 313 | }); |
| 314 | } |
| 315 | |
| 316 | pub fn visible_tiles(&self) -> Vec<&Tile> { |
| 317 | let mut tiles: Vec<&Tile> = self.tiles.iter().filter(|tile| tile.state != TileState::InLobby).collect(); |
| 318 | tiles.sort_by_key(|tile| match tile.state { |
| 319 | TileState::Present => 0, |
| 320 | TileState::Invited => 1, |
| 321 | _ => 2, |
| 322 | }); |
| 323 | tiles |
| 324 | } |
| 325 | |
| 326 | pub fn strip_tiles(&self) -> Vec<&Tile> { |
| 327 | let mut tiles = self.visible_tiles(); |
| 328 | tiles.sort_by_key(|tile| !self.tile_speaking(tile)); |
| 329 | tiles |
| 330 | } |
| 331 | |
| 332 | pub fn sharing_label(&self) -> Option<String> { |
| 333 | let sharer = self.screen_sharer.as_ref()?; |
| 334 | let name = self |
| 335 | .tiles |
| 336 | .iter() |
| 337 | .find(|tile| &tile.mri == sharer) |
| 338 | .map(|tile| tile.name.as_str()) |
| 339 | .filter(|name| !name.is_empty()) |
| 340 | .unwrap_or("Someone"); |
| 341 | Some(format!("{name} is sharing")) |
| 342 | } |
| 343 | |
| 344 | pub fn tile_speaking(&self, tile: &Tile) -> bool { |
| 345 | if self.speaking_mris.contains(&tile.mri) { |
| 346 | return true; |
| 347 | } |
| 348 | let only_remote = self.tiles.len() == 1; |
| 349 | only_remote && tile.state != TileState::Left && self.remote_speaking |
| 350 | } |
| 351 | |
| 352 | pub fn tile_caption(&self, tile: &Tile) -> Option<&'static str> { |
| 353 | match tile.state { |
| 354 | TileState::Invited => Some(match self.progress { |
| 355 | Some(Progress::Ringing) => "Ringing", |
| 356 | _ => "Calling...", |
| 357 | }), |
| 358 | TileState::Left => Some("Left"), |
| 359 | TileState::Present | TileState::InLobby => None, |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | pub fn elapsed(&self, now: Instant) -> Option<Duration> { |
| 364 | self.connected_since |
| 365 | .map(|since| now.saturating_duration_since(since)) |
| 366 | } |
| 367 | |
| 368 | pub fn connecting_text(&self) -> &'static str { |
| 369 | match (self.kind, self.progress) { |
| 370 | (CallKind::Meeting, _) => "Joining...", |
| 371 | (_, _) if self.incoming => "Connecting...", |
| 372 | (_, Some(Progress::Ringing)) => "Ringing", |
| 373 | _ => "Calling...", |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | pub fn is_connecting(&self) -> bool { |
| 378 | matches!(self.state, CallState::Connecting) |
| 379 | } |
| 380 | |
| 381 | pub fn is_reconnecting(&self) -> bool { |
| 382 | matches!(self.state, CallState::Reconnecting { .. }) |
| 383 | } |
| 384 | |
| 385 | pub fn ended_reason(&self) -> Option<&EndReason> { |
| 386 | match &self.state { |
| 387 | CallState::Ended { reason } => Some(reason), |
| 388 | _ => None, |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | pub fn timer_text(&self, now: Instant) -> String { |
| 393 | self.elapsed(now).map(format_elapsed).unwrap_or_default() |
| 394 | } |
| 395 | |
| 396 | pub fn can_use_camera(&self) -> bool { |
| 397 | !self.cameras.is_empty() |
| 398 | } |
| 399 | |
| 400 | pub fn can_share(&self) -> bool { |
| 401 | !self.share_sources.is_empty() && self.is_live() |
| 402 | } |
| 403 | |
| 404 | fn is_live(&self) -> bool { |
| 405 | matches!(self.state, CallState::Connected { .. } | CallState::Reconnecting { .. }) |
| 406 | } |
| 407 | |
| 408 | pub fn screens(&self) -> Vec<&ShareSource> { |
| 409 | self.share_sources.iter().filter(|source| source.kind == ShareKind::Screen).collect() |
| 410 | } |
| 411 | |
| 412 | pub fn windows(&self) -> Vec<&ShareSource> { |
| 413 | self.share_sources.iter().filter(|source| source.kind == ShareKind::Window).collect() |
| 414 | } |
| 415 | |
| 416 | pub fn can_unmute(&self) -> bool { |
| 417 | !self.listen_only |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | pub fn format_elapsed(elapsed: Duration) -> String { |
| 422 | let seconds = elapsed.as_secs(); |
| 423 | let (hours, minutes, seconds) = (seconds / 3600, seconds / 60 % 60, seconds % 60); |
| 424 | if hours > 0 { |
| 425 | format!("{hours}:{minutes:02}:{seconds:02}") |
| 426 | } else { |
| 427 | format!("{minutes:02}:{seconds:02}") |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | pub fn ended_notice(reason: &EndReason, elapsed: Option<Duration>, peer_name: &str) -> String { |
| 432 | let ended = || match elapsed { |
| 433 | Some(elapsed) => format!("Call ended {}", format_elapsed(elapsed)), |
| 434 | None => "Call ended".to_owned(), |
| 435 | }; |
| 436 | match reason { |
| 437 | EndReason::Cancelled => "Call cancelled".to_owned(), |
| 438 | EndReason::Dropped => "Call dropped".to_owned(), |
| 439 | EndReason::Failed(message) => format!("Call failed: {message}"), |
| 440 | EndReason::LocalHangup => ended(), |
| 441 | EndReason::Remote(EndKind::Declined) => format!("{peer_name} declined"), |
| 442 | EndReason::Remote(EndKind::NoAnswer) => "No answer".to_owned(), |
| 443 | EndReason::Remote(EndKind::Unavailable) => format!("{peer_name} is not available"), |
| 444 | EndReason::Remote(_) => ended(), |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | #[cfg(test)] |
| 449 | mod tests { |
| 450 | use super::*; |
| 451 | |
| 452 | fn connected_model() -> CallModel { |
| 453 | let mut model = CallModel::test(); |
| 454 | model.apply(CallUpdate::State(CallState::Connected { |
| 455 | since: Instant::now(), |
| 456 | })); |
| 457 | model |
| 458 | } |
| 459 | |
| 460 | fn entry(mri: &str, name: &str, muted: bool, in_lobby: bool) -> RosterEntry { |
| 461 | RosterEntry { |
| 462 | mri: mri.into(), |
| 463 | display_name: name.into(), |
| 464 | muted, |
| 465 | in_lobby, |
| 466 | has_video: false, |
| 467 | sharing: false, |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | fn meeting_with_me() -> CallModel { |
| 472 | let mut model = CallModel::meeting("Standup", false); |
| 473 | model.apply(CallUpdate::OwnIdentity { mri: "8:orgid:me".into() }); |
| 474 | model |
| 475 | } |
| 476 | |
| 477 | #[test] |
| 478 | fn elapsed_text_is_minutes_and_seconds() { |
| 479 | assert_eq!(format_elapsed(Duration::from_secs(42)), "00:42"); |
| 480 | assert_eq!(format_elapsed(Duration::from_secs(61)), "01:01"); |
| 481 | assert_eq!(format_elapsed(Duration::from_secs(3725)), "1:02:05"); |
| 482 | } |
| 483 | |
| 484 | #[test] |
| 485 | fn levels_drive_the_speaking_rings() { |
| 486 | let mut model = connected_model(); |
| 487 | model.apply(CallUpdate::Levels { |
| 488 | local: 0.0, |
| 489 | remote: 0.4, |
| 490 | }); |
| 491 | assert!(model.remote_speaking); |
| 492 | assert!(!model.local_speaking); |
| 493 | model.apply(CallUpdate::Levels { |
| 494 | local: 0.4, |
| 495 | remote: 0.0, |
| 496 | }); |
| 497 | assert!(model.local_speaking); |
| 498 | } |
| 499 | |
| 500 | #[test] |
| 501 | fn muting_switches_the_local_ring_off_at_once() { |
| 502 | let mut model = connected_model(); |
| 503 | model.apply(CallUpdate::Levels { |
| 504 | local: 0.5, |
| 505 | remote: 0.0, |
| 506 | }); |
| 507 | assert!(model.local_speaking); |
| 508 | model.apply(CallUpdate::Muted(true)); |
| 509 | assert!(!model.local_speaking); |
| 510 | model.apply(CallUpdate::Levels { |
| 511 | local: 0.5, |
| 512 | remote: 0.0, |
| 513 | }); |
| 514 | assert!(!model.local_speaking); |
| 515 | } |
| 516 | |
| 517 | #[test] |
| 518 | fn reconnecting_keeps_the_timer_and_clears_the_rings() { |
| 519 | let mut model = connected_model(); |
| 520 | let since = model.connected_since.unwrap(); |
| 521 | model.apply(CallUpdate::Levels { |
| 522 | local: 0.5, |
| 523 | remote: 0.5, |
| 524 | }); |
| 525 | model.apply(CallUpdate::State(CallState::Reconnecting { since })); |
| 526 | assert!(model.is_reconnecting()); |
| 527 | assert_eq!(model.connected_since, Some(since)); |
| 528 | assert!(!model.remote_speaking && !model.local_speaking); |
| 529 | } |
| 530 | |
| 531 | #[test] |
| 532 | fn notices_for_every_end_reason() { |
| 533 | let elapsed = Some(Duration::from_secs(42)); |
| 534 | let notice = |reason: EndReason, elapsed| ended_notice(&reason, elapsed, "Bea"); |
| 535 | assert_eq!(notice(EndReason::LocalHangup, elapsed), "Call ended 00:42"); |
| 536 | assert_eq!(notice(EndReason::Remote(EndKind::Normal), elapsed), "Call ended 00:42"); |
| 537 | assert_eq!(notice(EndReason::Remote(EndKind::Normal), None), "Call ended"); |
| 538 | assert_eq!(notice(EndReason::Cancelled, None), "Call cancelled"); |
| 539 | assert_eq!(notice(EndReason::Dropped, elapsed), "Call dropped"); |
| 540 | assert_eq!(notice(EndReason::Failed("no route".into()), None), "Call failed: no route"); |
| 541 | assert_eq!(notice(EndReason::Remote(EndKind::Declined), None), "Bea declined"); |
| 542 | assert_eq!(notice(EndReason::Remote(EndKind::NoAnswer), None), "No answer"); |
| 543 | assert_eq!(notice(EndReason::Remote(EndKind::Unavailable), None), "Bea is not available"); |
| 544 | assert_eq!(notice(EndReason::Remote(EndKind::Cancelled), None), "Call ended"); |
| 545 | assert_eq!(notice(EndReason::Remote(EndKind::Other), None), "Call ended"); |
| 546 | assert_eq!(notice(EndReason::Remote(EndKind::AnsweredElsewhere), None), "Call ended"); |
| 547 | } |
| 548 | |
| 549 | #[test] |
| 550 | fn the_connecting_text_follows_the_kind_of_call() { |
| 551 | let mut direct = CallModel::people("Bea", &[("8:orgid:b".into(), "Bea".into())]); |
| 552 | assert_eq!(direct.connecting_text(), "Calling..."); |
| 553 | direct.apply(CallUpdate::Progress(Progress::Ringing)); |
| 554 | assert_eq!(direct.connecting_text(), "Ringing"); |
| 555 | assert_eq!(CallModel::meeting("Standup", false).connecting_text(), "Joining..."); |
| 556 | assert_eq!(CallModel::incoming("Bea", "8:orgid:b", "Bea").connecting_text(), "Connecting..."); |
| 557 | assert_eq!(CallModel::test().connecting_text(), "Calling..."); |
| 558 | } |
| 559 | |
| 560 | #[test] |
| 561 | fn listen_only_blocks_unmuting() { |
| 562 | let mut model = connected_model(); |
| 563 | assert!(model.can_unmute()); |
| 564 | model.apply(CallUpdate::ListenOnly(true)); |
| 565 | assert!(!model.can_unmute()); |
| 566 | } |
| 567 | |
| 568 | #[test] |
| 569 | fn callees_start_invited_and_follow_the_progress() { |
| 570 | let mut model = CallModel::people("Bea", &[("8:orgid:b".into(), "Bea".into())]); |
| 571 | assert_eq!(model.kind, CallKind::Direct); |
| 572 | assert_eq!(model.peer_name, "Bea"); |
| 573 | assert_eq!(model.tile_caption(&model.tiles[0]), Some("Calling...")); |
| 574 | model.apply(CallUpdate::Progress(Progress::Ringing)); |
| 575 | assert_eq!(model.tile_caption(&model.tiles[0]), Some("Ringing")); |
| 576 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:b", "Bea", false, false)])); |
| 577 | assert_eq!(model.tiles[0].state, TileState::Present); |
| 578 | assert_eq!(model.tile_caption(&model.tiles[0]), None); |
| 579 | } |
| 580 | |
| 581 | #[test] |
| 582 | fn several_callees_make_a_group_call_named_after_the_chat() { |
| 583 | let model = CallModel::people( |
| 584 | "Retro team", |
| 585 | &[("8:orgid:b".into(), "Bea".into()), ("8:orgid:c".into(), "Cy".into())], |
| 586 | ); |
| 587 | assert_eq!(model.kind, CallKind::Group); |
| 588 | assert_eq!(model.peer_name, "Retro team"); |
| 589 | assert_eq!(model.tiles.len(), 2); |
| 590 | } |
| 591 | |
| 592 | #[test] |
| 593 | fn roster_adds_guests_updates_mute_and_drops_leavers() { |
| 594 | let mut model = meeting_with_me(); |
| 595 | model.apply(CallUpdate::Roster(vec![ |
| 596 | entry("8:orgid:me", "Me", false, false), |
| 597 | entry("8:orgid:a", "Ana", true, false), |
| 598 | entry("8:orgid:b", "Bo", false, false), |
| 599 | ])); |
| 600 | assert_eq!(model.tiles.len(), 2); |
| 601 | assert!(model.tiles.iter().find(|tile| tile.name == "Ana").unwrap().muted); |
| 602 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:me", "Me", false, false), entry("8:orgid:b", "Bo", false, false)])); |
| 603 | assert_eq!(model.tiles.len(), 1); |
| 604 | assert_eq!(model.tiles[0].name, "Bo"); |
| 605 | } |
| 606 | |
| 607 | #[test] |
| 608 | fn an_invited_callee_who_leaves_stays_as_left() { |
| 609 | let mut model = CallModel::people("Bea", &[("8:orgid:b".into(), "Bea".into())]); |
| 610 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:b", "Bea", false, false)])); |
| 611 | model.apply(CallUpdate::Roster(Vec::new())); |
| 612 | assert_eq!(model.tiles[0].state, TileState::Left); |
| 613 | assert_eq!(model.tile_caption(&model.tiles[0]), Some("Left")); |
| 614 | } |
| 615 | |
| 616 | #[test] |
| 617 | fn lobby_guests_get_no_tile() { |
| 618 | let mut model = meeting_with_me(); |
| 619 | model.apply(CallUpdate::Roster(vec![ |
| 620 | entry("8:orgid:a", "Ana", false, false), |
| 621 | entry("8:orgid:g", "Guest", false, true), |
| 622 | ])); |
| 623 | let names: Vec<&str> = model.visible_tiles().iter().map(|tile| tile.name.as_str()).collect(); |
| 624 | assert_eq!(names, vec!["Ana"]); |
| 625 | } |
| 626 | |
| 627 | #[test] |
| 628 | fn the_lobby_flag_follows_the_call() { |
| 629 | let mut model = meeting_with_me(); |
| 630 | model.apply(CallUpdate::Lobby(true)); |
| 631 | assert!(model.lobby); |
| 632 | model.apply(CallUpdate::Lobby(false)); |
| 633 | assert!(!model.lobby); |
| 634 | } |
| 635 | |
| 636 | #[test] |
| 637 | fn speakers_light_their_own_tile_and_a_lone_remote_follows_the_level() { |
| 638 | let mut model = meeting_with_me(); |
| 639 | model.apply(CallUpdate::Roster(vec![ |
| 640 | entry("8:orgid:a", "Ana", false, false), |
| 641 | entry("8:orgid:b", "Bo", false, false), |
| 642 | ])); |
| 643 | model.apply(CallUpdate::State(CallState::Connected { since: Instant::now() })); |
| 644 | model.apply(CallUpdate::Speakers(vec!["8:orgid:b".into()])); |
| 645 | let tiles: Vec<Tile> = model.tiles.clone(); |
| 646 | assert!(!model.tile_speaking(&tiles[0])); |
| 647 | assert!(model.tile_speaking(&tiles[1])); |
| 648 | let mut lone = CallModel::people("Bea", &[("8:orgid:b".into(), "Bea".into())]); |
| 649 | lone.apply(CallUpdate::State(CallState::Connected { since: Instant::now() })); |
| 650 | lone.apply(CallUpdate::Levels { local: 0.0, remote: 0.5 }); |
| 651 | assert!(lone.tile_speaking(&lone.tiles[0].clone())); |
| 652 | } |
| 653 | |
| 654 | #[test] |
| 655 | fn more_than_nine_cells_collapse_into_a_plus_n_tile() { |
| 656 | assert_eq!(grid_layout(0), GridLayout { shown_others: 0, hidden: 0 }); |
| 657 | assert_eq!(grid_layout(8), GridLayout { shown_others: 8, hidden: 0 }); |
| 658 | assert_eq!(grid_layout(9), GridLayout { shown_others: 7, hidden: 2 }); |
| 659 | assert_eq!(grid_layout(30), GridLayout { shown_others: 7, hidden: 23 }); |
| 660 | } |
| 661 | |
| 662 | #[test] |
| 663 | fn crowded_calls_get_smaller_tiles() { |
| 664 | assert_eq!(tile_size(2).avatar, 96.); |
| 665 | assert_eq!(tile_size(4).avatar, 72.); |
| 666 | assert_eq!(tile_size(9).avatar, 56.); |
| 667 | } |
| 668 | |
| 669 | #[test] |
| 670 | fn the_stage_label_names_the_sharer_and_the_strip_puts_the_speaker_first() { |
| 671 | let mut model = meeting_with_me(); |
| 672 | model.apply(CallUpdate::Roster(vec![ |
| 673 | entry("8:orgid:a", "Ana", false, false), |
| 674 | entry("8:orgid:b", "Bo", false, false), |
| 675 | ])); |
| 676 | assert_eq!(model.sharing_label(), None); |
| 677 | model.apply(CallUpdate::ScreenShare(Some("8:orgid:b".into()))); |
| 678 | assert_eq!(model.sharing_label().as_deref(), Some("Bo is sharing")); |
| 679 | model.apply(CallUpdate::State(CallState::Connected { since: Instant::now() })); |
| 680 | model.apply(CallUpdate::Speakers(vec!["8:orgid:b".into()])); |
| 681 | let names: Vec<&str> = model.strip_tiles().iter().map(|tile| tile.name.as_str()).collect(); |
| 682 | assert_eq!(names, vec!["Bo", "Ana"]); |
| 683 | model.apply(CallUpdate::ScreenShare(None)); |
| 684 | assert_eq!(model.sharing_label(), None); |
| 685 | } |
| 686 | |
| 687 | #[test] |
| 688 | fn roster_entries_carry_the_camera_flag_to_the_tile() { |
| 689 | let mut model = meeting_with_me(); |
| 690 | let mut with_camera = entry("8:orgid:a", "Ana", false, false); |
| 691 | with_camera.has_video = true; |
| 692 | model.apply(CallUpdate::Roster(vec![with_camera])); |
| 693 | assert!(model.tiles[0].has_video); |
| 694 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:a", "Ana", false, false)])); |
| 695 | assert!(!model.tiles[0].has_video); |
| 696 | } |
| 697 | |
| 698 | #[test] |
| 699 | fn present_guests_sort_before_waiting_ones() { |
| 700 | let mut model = CallModel::people( |
| 701 | "Group", |
| 702 | &[("8:orgid:b".into(), "Bea".into()), ("8:orgid:c".into(), "Cy".into())], |
| 703 | ); |
| 704 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:c", "Cy", false, false)])); |
| 705 | let names: Vec<&str> = model.visible_tiles().iter().map(|tile| tile.name.as_str()).collect(); |
| 706 | assert_eq!(names, vec!["Cy", "Bea"]); |
| 707 | } |
| 708 | } |