| 1 | use std::time::{Duration, Instant}; |
| 2 | |
| 3 | use calling::video_frame::bgra_from_i420; |
| 4 | use calling::video_pattern::{PatternKind, pattern_frame}; |
| 5 | use calling::{ |
| 6 | AudioDevice, BackgroundChoice, BreakoutMove, CallCommand, CallControl, CallHandle, CallState, CallUpdate, Caller, CaptionEntry, CaptionState, |
| 7 | ContentShare, DeviceChoice, DeviceLists, EndReason, HoldState, IncomingRing, MeetingTarget, Progress, PublishedState, RaisedHand, Reaction, RosterEntry, |
| 8 | ShareKind, ShareSource, VideoKey, call_channel, |
| 9 | }; |
| 10 | use calling::CameraDevice; |
| 11 | |
| 12 | use super::demo_scene::self_view; |
| 13 | use tokio::time::{interval, sleep}; |
| 14 | |
| 15 | use super::model::{ECHO_MRI, ECHO_NAME}; |
| 16 | |
| 17 | const CONNECT_DELAY: Duration = Duration::from_millis(2200); |
| 18 | const RING_START: Duration = Duration::from_millis(1400); |
| 19 | const ANSWER_DELAY: Duration = Duration::from_millis(3200); |
| 20 | const INCOMING_CONNECT_DELAY: Duration = Duration::from_millis(600); |
| 21 | const LOBBY_WAIT: Duration = Duration::from_secs(6); |
| 22 | const LEVEL_PERIOD: Duration = Duration::from_millis(200); |
| 23 | const CYCLE_TICKS: u32 = 40; |
| 24 | const REMOTE_SPEAKS: std::ops::Range<u32> = 0..18; |
| 25 | const LOCAL_SPEAKS: std::ops::Range<u32> = 24..32; |
| 26 | const SPEAKING_LEVEL: f32 = 0.3; |
| 27 | const SPEAKER_TICKS: u32 = 10; |
| 28 | const VIDEO_PERIOD: Duration = Duration::from_millis(125); |
| 29 | const DEMO_CAMERAS: usize = 4; |
| 30 | const SCREEN_EVERY_TICKS: u32 = 4; |
| 31 | const SHARE_START: Duration = Duration::from_secs(10); |
| 32 | const SHARE_END: Duration = Duration::from_secs(45); |
| 33 | const REACTION_EVERY_TICKS: u32 = 20; |
| 34 | const DEMO_FIRST_HANDS: [(usize, u64); 2] = [(2, 1), (0, 2)]; |
| 35 | const DEMO_WAITING: [&str; 3] = ["Anja Vogel", "Matteo Conti", "Hannah Weiss"]; |
| 36 | const CAPTION_WORDS_PER_TICK: usize = 2; |
| 37 | const CAPTION_HOLD_TICKS: usize = 14; |
| 38 | const DEMO_CAPTIONS: [(usize, &str); 4] = [ |
| 39 | (0, "Thanks everyone for joining, let us start with the release status."), |
| 40 | (1, "The build is green and the new call controls are ready for review."), |
| 41 | (2, "Can we admit the two people from the lobby before we begin?"), |
| 42 | (0, "Yes, I am letting them in now and we will share the plan on screen."), |
| 43 | ]; |
| 44 | const GUEST_PHASE: u32 = 37; |
| 45 | const SELF_PHASE: u32 = 211; |
| 46 | pub const DEMO_OWN_MRI: &str = "8:orgid:demo-me"; |
| 47 | pub const DEMO_RING_ID: u64 = 9001; |
| 48 | const DEMO_CALLER: (&str, &str) = ("8:orgid:demo-mara", "Mara Lindqvist"); |
| 49 | const DEMO_GUESTS: [&str; 11] = [ |
| 50 | "Mara Lindqvist", |
| 51 | "Jonas Ortega", |
| 52 | "Priya Nair", |
| 53 | "Lea Schneider", |
| 54 | "Tobias Klein", |
| 55 | "Ines Duarte", |
| 56 | "Noor Haddad", |
| 57 | "Felix Brandt", |
| 58 | "Yuki Tanaka", |
| 59 | "Carlos Mendez", |
| 60 | "Sofia Rossi", |
| 61 | ]; |
| 62 | |
| 63 | pub type Person = (String, String); |
| 64 | |
| 65 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 66 | pub enum DemoScene { |
| 67 | #[default] |
| 68 | Plain, |
| 69 | Call, |
| 70 | Recording, |
| 71 | Consent, |
| 72 | Hold, |
| 73 | Held, |
| 74 | Breakout, |
| 75 | Whiteboard, |
| 76 | } |
| 77 | |
| 78 | impl DemoScene { |
| 79 | pub fn from_name(name: &str) -> Option<Self> { |
| 80 | Some(match name.trim() { |
| 81 | "call" => DemoScene::Call, |
| 82 | "recording" => DemoScene::Recording, |
| 83 | "consent" => DemoScene::Consent, |
| 84 | "hold" => DemoScene::Hold, |
| 85 | "held" => DemoScene::Held, |
| 86 | "breakout" => DemoScene::Breakout, |
| 87 | "whiteboard" => DemoScene::Whiteboard, |
| 88 | _ => return None, |
| 89 | }) |
| 90 | } |
| 91 | |
| 92 | pub fn is_one_to_one(self) -> bool { |
| 93 | matches!(self, DemoScene::Call | DemoScene::Hold | DemoScene::Held) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | const BREAKOUT_AT_TICKS: u32 = 20; |
| 98 | const WHITEBOARD_AT_TICKS: u32 = 5; |
| 99 | const DEMO_BOARD_URL: &str = "https://app.whiteboard.microsoft.com/me/whiteboards/demo"; |
| 100 | const DEMO_REPLACEMENT_LINK: &str = "https://demo.invalid/replacement"; |
| 101 | const DEMO_ROOM_THREAD: &str = "19:meeting_demo-room-1@thread.v2"; |
| 102 | |
| 103 | pub enum DemoCall { |
| 104 | Test, |
| 105 | People(Vec<Person>), |
| 106 | Meeting { guests: Vec<Person>, lobby: bool, organizer: bool }, |
| 107 | Incoming(Person), |
| 108 | } |
| 109 | |
| 110 | pub fn demo_guests(count: usize) -> Vec<Person> { |
| 111 | DEMO_GUESTS |
| 112 | .iter() |
| 113 | .take(count) |
| 114 | .enumerate() |
| 115 | .map(|(index, name)| (format!("8:orgid:demo-guest-{index}"), (*name).to_owned())) |
| 116 | .collect() |
| 117 | } |
| 118 | |
| 119 | pub fn demo_caller() -> Person { |
| 120 | (DEMO_CALLER.0.to_owned(), DEMO_CALLER.1.to_owned()) |
| 121 | } |
| 122 | |
| 123 | pub fn demo_video_ring() -> IncomingRing { |
| 124 | IncomingRing { video: true, ..demo_ring() } |
| 125 | } |
| 126 | |
| 127 | pub fn demo_ring() -> IncomingRing { |
| 128 | let (mri, display_name) = demo_caller(); |
| 129 | IncomingRing { |
| 130 | ring_id: DEMO_RING_ID, |
| 131 | caller: Caller { mri, display_name }, |
| 132 | thread_id: Some("demo-chat-mara".to_owned()), |
| 133 | video: false, |
| 134 | is_group: false, |
| 135 | subject: None, |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | fn device(index: u16, name: &str) -> AudioDevice { |
| 140 | AudioDevice { |
| 141 | index, |
| 142 | guid: format!("demo-{index}"), |
| 143 | name: name.to_owned(), |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | fn demo_cameras() -> Vec<CameraDevice> { |
| 148 | vec![ |
| 149 | CameraDevice { key: "0".into(), name: "Integrated Camera".into() }, |
| 150 | CameraDevice { key: "1".into(), name: "Studio Webcam".into() }, |
| 151 | ] |
| 152 | } |
| 153 | |
| 154 | fn demo_share_sources() -> Vec<ShareSource> { |
| 155 | let window = |id: u64, title: &str| ShareSource { id, kind: ShareKind::Window, title: title.to_owned() }; |
| 156 | vec![ |
| 157 | ShareSource { id: 1, kind: ShareKind::Screen, title: String::new() }, |
| 158 | window(2, "Release notes - Docs"), |
| 159 | window(3, "Standup board"), |
| 160 | window(4, "Terminal"), |
| 161 | ] |
| 162 | } |
| 163 | |
| 164 | fn demo_devices() -> DeviceLists { |
| 165 | DeviceLists { |
| 166 | inputs: vec![device(0, "Headset Microphone"), device(1, "Built-in Microphone")], |
| 167 | outputs: vec![device(0, "Headset"), device(1, "Speakers")], |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | pub fn start_demo_call(script: DemoCall) -> CallHandle { |
| 172 | start_demo_scene_call(script, DemoScene::Plain) |
| 173 | } |
| 174 | |
| 175 | pub fn start_demo_scene_call(script: DemoCall, scene: DemoScene) -> CallHandle { |
| 176 | let (handle, control) = call_channel(); |
| 177 | crate::runtime::handle().spawn(run_demo_call(control, script, scene)); |
| 178 | handle |
| 179 | } |
| 180 | |
| 181 | fn roster_of(people: &[Person]) -> Vec<RosterEntry> { |
| 182 | people |
| 183 | .iter() |
| 184 | .enumerate() |
| 185 | .map(|(index, (mri, name))| RosterEntry { |
| 186 | mri: mri.clone(), |
| 187 | display_name: name.clone(), |
| 188 | muted: index % 3 == 1, |
| 189 | in_lobby: false, |
| 190 | has_video: index < DEMO_CAMERAS, |
| 191 | sharing: false, |
| 192 | hand: None, |
| 193 | spotlight: None, |
| 194 | organizer: false, |
| 195 | }) |
| 196 | .collect() |
| 197 | } |
| 198 | |
| 199 | async fn run_demo_call(mut control: CallControl, script: DemoCall, scene: DemoScene) { |
| 200 | control.send(CallUpdate::OwnIdentity { |
| 201 | mri: DEMO_OWN_MRI.to_owned(), |
| 202 | }); |
| 203 | let (people, lobby) = match &script { |
| 204 | DemoCall::Test => (vec![(ECHO_MRI.to_owned(), ECHO_NAME.to_owned())], false), |
| 205 | DemoCall::People(callees) => (callees.clone(), false), |
| 206 | DemoCall::Meeting { guests, lobby, .. } => (guests.clone(), *lobby), |
| 207 | DemoCall::Incoming(caller) => (vec![caller.clone()], false), |
| 208 | }; |
| 209 | if !ring_until_answered(&mut control, &script).await { |
| 210 | control.send(CallUpdate::State(CallState::Ended { |
| 211 | reason: EndReason::Cancelled, |
| 212 | })); |
| 213 | return; |
| 214 | } |
| 215 | control.send(CallUpdate::ListenOnly(false)); |
| 216 | control.send(CallUpdate::Devices(demo_devices())); |
| 217 | control.send(CallUpdate::Cameras(demo_cameras())); |
| 218 | control.send(CallUpdate::ShareSources(demo_share_sources())); |
| 219 | control.send(CallUpdate::Selected { |
| 220 | input: DeviceChoice::SystemDefault, |
| 221 | output: DeviceChoice::SystemDefault, |
| 222 | }); |
| 223 | control.send(CallUpdate::State(CallState::Connected { |
| 224 | since: Instant::now(), |
| 225 | })); |
| 226 | if lobby { |
| 227 | control.send(CallUpdate::Lobby(true)); |
| 228 | if control.until_hangup(sleep(LOBBY_WAIT)).await.is_err() { |
| 229 | end(&control, EndReason::LocalHangup); |
| 230 | return; |
| 231 | } |
| 232 | control.send(CallUpdate::Lobby(false)); |
| 233 | } |
| 234 | let mut roster = roster_of(&people); |
| 235 | if matches!(script, DemoCall::People(_)) { |
| 236 | control.send(CallUpdate::ReplacementLink(DEMO_REPLACEMENT_LINK.to_owned())); |
| 237 | } |
| 238 | if matches!(script, DemoCall::Meeting { organizer: true, .. }) { |
| 239 | roster.push(demo_organizer()); |
| 240 | roster.extend(DEMO_WAITING.iter().enumerate().map(|(index, name)| demo_waiting(index, name))); |
| 241 | } |
| 242 | if matches!(script, DemoCall::Meeting { .. }) { |
| 243 | for (index, rank) in DEMO_FIRST_HANDS { |
| 244 | if let Some(entry) = roster.get_mut(index) { |
| 245 | entry.hand = Some(demo_hand(rank)); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | control.send(CallUpdate::Roster(roster.clone())); |
| 250 | let video = !matches!(script, DemoCall::Test); |
| 251 | let sharer = matches!(script, DemoCall::Meeting { .. }).then(|| people.get(1).map(|person| person.0.clone())).flatten(); |
| 252 | start_scene(&control, scene); |
| 253 | speak_until_hangup(control, people, roster, video, sharer, scene).await; |
| 254 | } |
| 255 | |
| 256 | async fn ring_until_answered(control: &mut CallControl, script: &DemoCall) -> bool { |
| 257 | let steps: Vec<(Option<Progress>, Duration)> = match script { |
| 258 | DemoCall::People(_) => vec![ |
| 259 | (Some(Progress::Calling), RING_START), |
| 260 | (Some(Progress::Ringing), ANSWER_DELAY - RING_START), |
| 261 | ], |
| 262 | DemoCall::Incoming(_) => vec![(None, INCOMING_CONNECT_DELAY)], |
| 263 | DemoCall::Test | DemoCall::Meeting { .. } => vec![(None, CONNECT_DELAY)], |
| 264 | }; |
| 265 | for (progress, wait) in steps { |
| 266 | if let Some(progress) = progress { |
| 267 | control.send(CallUpdate::Progress(progress)); |
| 268 | } |
| 269 | if control.until_hangup(sleep(wait)).await.is_err() { |
| 270 | return false; |
| 271 | } |
| 272 | } |
| 273 | true |
| 274 | } |
| 275 | |
| 276 | fn publish_demo_video(control: &CallControl, people: &[Person], tick: u32, sharing: bool, sharer: Option<&str>, camera: Option<bool>) { |
| 277 | if let Some(blur) = camera { |
| 278 | control.video.publish(VideoKey::LocalCamera, self_view((tick + SELF_PHASE) as usize, blur)); |
| 279 | } |
| 280 | for (index, (mri, _)) in people.iter().take(DEMO_CAMERAS).enumerate() { |
| 281 | let frame = pattern_frame(PatternKind::Camera, tick + index as u32 * GUEST_PHASE); |
| 282 | control.video.publish(VideoKey::Person(mri.clone()), bgra_from_i420(frame)); |
| 283 | } |
| 284 | if sharing && sharer.is_some() && tick.is_multiple_of(SCREEN_EVERY_TICKS) { |
| 285 | control.video.publish(VideoKey::Screen, bgra_from_i420(pattern_frame(PatternKind::Screen, tick))); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | fn start_scene(control: &CallControl, scene: DemoScene) { |
| 290 | match scene { |
| 291 | DemoScene::Recording => control.send(CallUpdate::Recording(true)), |
| 292 | DemoScene::Consent => { |
| 293 | control.send(CallUpdate::Recording(true)); |
| 294 | control.send(CallUpdate::ConsentRequired(true)); |
| 295 | } |
| 296 | DemoScene::Hold => control.send(CallUpdate::Hold(HoldState::Local)), |
| 297 | DemoScene::Held => control.send(CallUpdate::Hold(HoldState::Remote)), |
| 298 | DemoScene::Call | DemoScene::Breakout | DemoScene::Whiteboard | DemoScene::Plain => {} |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | fn scene_tick(control: &CallControl, scene: DemoScene, ticks: u32, people: &[Person]) { |
| 303 | match scene { |
| 304 | DemoScene::Breakout if ticks == BREAKOUT_AT_TICKS => control.send(CallUpdate::BreakoutMove(BreakoutMove { |
| 305 | room_name: "Room 1".to_owned(), |
| 306 | target: MeetingTarget { thread_id: DEMO_ROOM_THREAD.to_owned(), tenant_id: "demo-tenant".to_owned(), organizer_id: "demo-me".to_owned(), meeting_data: None }, |
| 307 | returning: false, |
| 308 | })), |
| 309 | DemoScene::Whiteboard if ticks == WHITEBOARD_AT_TICKS => control.send(CallUpdate::Whiteboard(Some(ContentShare { |
| 310 | session_id: "demo-whiteboard".to_owned(), |
| 311 | presenter: people.first().map(|person| person.0.clone()), |
| 312 | subject: "Whiteboard".to_owned(), |
| 313 | url: Some(DEMO_BOARD_URL.to_owned()), |
| 314 | whiteboard: true, |
| 315 | }))), |
| 316 | _ => {} |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | async fn speak_until_hangup(mut control: CallControl, people: Vec<Person>, mut roster: Vec<RosterEntry>, video: bool, sharer: Option<String>, scene: DemoScene) { |
| 321 | let started = Instant::now(); |
| 322 | let mut video_ticks = 0u32; |
| 323 | let mut sharing = false; |
| 324 | let mut camera_on = false; |
| 325 | let mut blur = false; |
| 326 | let mut captions_on = false; |
| 327 | let mut caption_ticks = 0usize; |
| 328 | let mut video_tick = interval(VIDEO_PERIOD); |
| 329 | let mut muted = false; |
| 330 | let mut input = DeviceChoice::SystemDefault; |
| 331 | let mut output = DeviceChoice::SystemDefault; |
| 332 | let mut ticks = 0u32; |
| 333 | let mut share_sound = false; |
| 334 | let mut tick = interval(LEVEL_PERIOD); |
| 335 | loop { |
| 336 | tokio::select! { |
| 337 | _ = video_tick.tick(), if video || camera_on => { |
| 338 | let elapsed = started.elapsed(); |
| 339 | let share_now = sharer.is_some() && (SHARE_START..SHARE_END).contains(&elapsed); |
| 340 | if share_now != sharing { |
| 341 | sharing = share_now; |
| 342 | control.send(CallUpdate::ScreenShare(sharer.clone().filter(|_| sharing))); |
| 343 | } |
| 344 | publish_demo_video(&control, &people, video_ticks, sharing, sharer.as_deref(), camera_on.then_some(blur)); |
| 345 | video_ticks += 1; |
| 346 | } |
| 347 | _ = tick.tick() => { |
| 348 | let phase = ticks % CYCLE_TICKS; |
| 349 | let remote_speaks = REMOTE_SPEAKS.contains(&phase); |
| 350 | control.send(CallUpdate::Levels { |
| 351 | local: if LOCAL_SPEAKS.contains(&phase) && !muted { SPEAKING_LEVEL } else { 0.0 }, |
| 352 | remote: if remote_speaks { SPEAKING_LEVEL } else { 0.0 }, |
| 353 | }); |
| 354 | if people.len() > 1 && ticks.is_multiple_of(SPEAKER_TICKS) { |
| 355 | let speaker = &people[(ticks / SPEAKER_TICKS) as usize % people.len()]; |
| 356 | control.send(CallUpdate::Speakers(vec![speaker.0.clone()])); |
| 357 | } |
| 358 | if ticks % REACTION_EVERY_TICKS == REACTION_EVERY_TICKS / 2 |
| 359 | && let Some((mri, _)) = people.get((ticks / REACTION_EVERY_TICKS) as usize % people.len().max(1)) |
| 360 | { |
| 361 | let reaction = Reaction::ALL[(ticks / REACTION_EVERY_TICKS) as usize % Reaction::ALL.len()]; |
| 362 | control.send(CallUpdate::Reaction { mri: mri.clone(), reaction }); |
| 363 | } |
| 364 | if captions_on { |
| 365 | if let Some(entry) = demo_caption(&people, caption_ticks) { |
| 366 | control.send(CallUpdate::Caption(entry)); |
| 367 | } |
| 368 | caption_ticks += 1; |
| 369 | } |
| 370 | scene_tick(&control, scene, ticks, &people); |
| 371 | ticks += 1; |
| 372 | } |
| 373 | command = control.recv() => match command { |
| 374 | Some(CallCommand::Mute(command)) => { |
| 375 | muted = command.target(muted); |
| 376 | control.send(CallUpdate::Muted(muted)); |
| 377 | } |
| 378 | Some(CallCommand::SelectInput(choice)) => { |
| 379 | input = choice; |
| 380 | control.send(CallUpdate::Selected { input: input.clone(), output: output.clone() }); |
| 381 | } |
| 382 | Some(CallCommand::SelectOutput(choice)) => { |
| 383 | output = choice; |
| 384 | control.send(CallUpdate::Selected { input: input.clone(), output: output.clone() }); |
| 385 | } |
| 386 | Some(CallCommand::RefreshDevices) => control.send(CallUpdate::Devices(demo_devices())), |
| 387 | Some(CallCommand::SetCamera(on)) => { |
| 388 | camera_on = on; |
| 389 | control.send(CallUpdate::Camera(on)); |
| 390 | } |
| 391 | Some(CallCommand::SelectCamera(_)) => {} |
| 392 | Some(CallCommand::StartShare(source)) => { |
| 393 | control.send(CallUpdate::LocalShare(Some(source.label()))); |
| 394 | if share_sound { |
| 395 | control.send(CallUpdate::ShareSound(true)); |
| 396 | } |
| 397 | } |
| 398 | Some(CallCommand::StopShare) => control.send(CallUpdate::LocalShare(None)), |
| 399 | Some(CallCommand::SetShareSound(on)) => share_sound = on, |
| 400 | Some(CallCommand::SetHand(raised)) => { |
| 401 | set_demo_hand(&mut roster, DEMO_OWN_MRI, raised); |
| 402 | control.send(CallUpdate::Roster(roster.clone())); |
| 403 | } |
| 404 | Some(CallCommand::LowerHand { mri }) => { |
| 405 | set_demo_hand(&mut roster, &mri, false); |
| 406 | control.send(CallUpdate::Roster(roster.clone())); |
| 407 | } |
| 408 | Some(CallCommand::LowerAllHands) => { |
| 409 | roster.iter_mut().for_each(|entry| entry.hand = None); |
| 410 | control.send(CallUpdate::Roster(roster.clone())); |
| 411 | } |
| 412 | Some(CallCommand::SendReaction(reaction)) => { |
| 413 | control.send(CallUpdate::Reaction { mri: DEMO_OWN_MRI.to_owned(), reaction }); |
| 414 | } |
| 415 | Some(CallCommand::Admit { mri }) => { |
| 416 | roster.iter_mut().filter(|entry| entry.mri == mri).for_each(|entry| entry.in_lobby = false); |
| 417 | control.send(CallUpdate::Roster(roster.clone())); |
| 418 | } |
| 419 | Some(CallCommand::AdmitAll) => { |
| 420 | roster.iter_mut().for_each(|entry| entry.in_lobby = false); |
| 421 | control.send(CallUpdate::Roster(roster.clone())); |
| 422 | } |
| 423 | Some(CallCommand::Deny { mri } | CallCommand::RemoveParticipant { mri }) => { |
| 424 | roster.retain(|entry| entry.mri != mri); |
| 425 | control.send(CallUpdate::Roster(roster.clone())); |
| 426 | } |
| 427 | Some(CallCommand::MuteParticipant { mri }) => { |
| 428 | roster.iter_mut().filter(|entry| entry.mri == mri).for_each(|entry| entry.muted = true); |
| 429 | control.send(CallUpdate::Roster(roster.clone())); |
| 430 | } |
| 431 | Some(CallCommand::MuteAll) => { |
| 432 | roster.iter_mut().filter(|entry| entry.mri != DEMO_OWN_MRI).for_each(|entry| entry.muted = true); |
| 433 | control.send(CallUpdate::Roster(roster.clone())); |
| 434 | } |
| 435 | Some(CallCommand::Spotlight { mri }) => { |
| 436 | let rank = roster.iter().filter_map(|entry| entry.spotlight.as_ref().map(|state| state.rank)).max().unwrap_or(0) + 1; |
| 437 | if let Some(entry) = roster.iter_mut().find(|entry| entry.mri == mri) { |
| 438 | entry.spotlight = Some(demo_spotlight(rank)); |
| 439 | } |
| 440 | control.send(CallUpdate::Roster(roster.clone())); |
| 441 | } |
| 442 | Some(CallCommand::StopSpotlight { mri }) => { |
| 443 | roster.iter_mut().filter(|entry| entry.mri == mri).for_each(|entry| entry.spotlight = None); |
| 444 | control.send(CallUpdate::Roster(roster.clone())); |
| 445 | } |
| 446 | Some(CallCommand::SetCaptions(on)) => { |
| 447 | captions_on = on; |
| 448 | caption_ticks = 0; |
| 449 | control.send(CallUpdate::Captions(if on { CaptionState::On } else { CaptionState::Off })); |
| 450 | } |
| 451 | Some(CallCommand::SetBackground(choice)) => blur = choice != BackgroundChoice::None, |
| 452 | Some(CallCommand::SetRecording { on, .. }) => control.send(CallUpdate::Recording(on)), |
| 453 | Some(CallCommand::ConsentToRecording) => control.send(CallUpdate::ConsentRequired(false)), |
| 454 | Some(CallCommand::Hold(hold)) => control.send(CallUpdate::Hold(if hold { HoldState::Local } else { HoldState::Active })), |
| 455 | Some(CallCommand::Transfer { .. }) => { |
| 456 | control.send(CallUpdate::Notice("Call transferred".to_owned())); |
| 457 | end(&control, EndReason::LocalHangup); |
| 458 | return; |
| 459 | } |
| 460 | Some(CallCommand::OpenWhiteboard { .. }) => control.send(CallUpdate::WhiteboardUrl(DEMO_BOARD_URL.to_owned())), |
| 461 | Some(CallCommand::RefreshShareSources) => control.send(CallUpdate::ShareSources(demo_share_sources())), |
| 462 | Some(CallCommand::Hangup | CallCommand::EndMeeting) | None => { |
| 463 | end(&control, EndReason::LocalHangup); |
| 464 | return; |
| 465 | } |
| 466 | }, |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | fn demo_organizer() -> RosterEntry { |
| 472 | RosterEntry { |
| 473 | mri: DEMO_OWN_MRI.to_owned(), |
| 474 | display_name: "You".to_owned(), |
| 475 | muted: false, |
| 476 | in_lobby: false, |
| 477 | has_video: false, |
| 478 | sharing: false, |
| 479 | hand: None, |
| 480 | spotlight: None, |
| 481 | organizer: true, |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | fn demo_waiting(index: usize, name: &str) -> RosterEntry { |
| 486 | RosterEntry { |
| 487 | mri: format!("8:orgid:demo-waiting-{index}"), |
| 488 | display_name: name.to_owned(), |
| 489 | muted: false, |
| 490 | in_lobby: true, |
| 491 | has_video: false, |
| 492 | sharing: false, |
| 493 | hand: None, |
| 494 | spotlight: None, |
| 495 | organizer: false, |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | fn demo_spotlight(rank: u64) -> PublishedState { |
| 500 | PublishedState { state_id: format!("demo-spotlight-{rank}"), rank } |
| 501 | } |
| 502 | |
| 503 | fn demo_caption(people: &[Person], tick: usize) -> Option<CaptionEntry> { |
| 504 | let line = tick / (CAPTION_HOLD_TICKS * 2) % DEMO_CAPTIONS.len(); |
| 505 | let within = tick % (CAPTION_HOLD_TICKS * 2); |
| 506 | let (speaker, text) = DEMO_CAPTIONS[line]; |
| 507 | let words: Vec<&str> = text.split(' ').collect(); |
| 508 | let shown = ((within + 1) * CAPTION_WORDS_PER_TICK).min(words.len()); |
| 509 | let (mri, name) = people.get(speaker % people.len().max(1))?; |
| 510 | Some(CaptionEntry { |
| 511 | id: format!("demo-caption-{}", tick / (CAPTION_HOLD_TICKS * 2)), |
| 512 | user_id: mri.clone(), |
| 513 | display_name: name.clone(), |
| 514 | text: words[..shown].join(" "), |
| 515 | is_final: shown == words.len(), |
| 516 | }) |
| 517 | } |
| 518 | |
| 519 | fn demo_hand(rank: u64) -> RaisedHand { |
| 520 | RaisedHand { state_id: format!("demo-hand-{rank}"), rank } |
| 521 | } |
| 522 | |
| 523 | fn set_demo_hand(roster: &mut Vec<RosterEntry>, mri: &str, raised: bool) { |
| 524 | let next_rank = roster.iter().filter_map(|entry| entry.hand.as_ref().map(|hand| hand.rank)).max().unwrap_or(0) + 1; |
| 525 | if !raised { |
| 526 | if let Some(entry) = roster.iter_mut().find(|entry| entry.mri == mri) { |
| 527 | entry.hand = None; |
| 528 | } |
| 529 | roster.retain(|entry| entry.mri != DEMO_OWN_MRI || entry.hand.is_some()); |
| 530 | return; |
| 531 | } |
| 532 | match roster.iter_mut().find(|entry| entry.mri == mri) { |
| 533 | Some(entry) => entry.hand = Some(demo_hand(next_rank)), |
| 534 | None => roster.push(RosterEntry { |
| 535 | mri: mri.to_owned(), |
| 536 | display_name: "You".to_owned(), |
| 537 | muted: false, |
| 538 | in_lobby: false, |
| 539 | has_video: false, |
| 540 | sharing: false, |
| 541 | hand: Some(demo_hand(next_rank)), |
| 542 | spotlight: None, |
| 543 | organizer: false, |
| 544 | }), |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | fn end(control: &CallControl, reason: EndReason) { |
| 549 | control.send(CallUpdate::State(CallState::Ended { reason })); |
| 550 | } |
| 551 | |
| 552 | #[cfg(test)] |
| 553 | mod tests { |
| 554 | use super::*; |
| 555 | |
| 556 | #[test] |
| 557 | fn demo_guests_have_distinct_mris_and_names() { |
| 558 | let guests = demo_guests(11); |
| 559 | assert_eq!(guests.len(), 11); |
| 560 | let mut mris: Vec<&String> = guests.iter().map(|guest| &guest.0).collect(); |
| 561 | mris.sort(); |
| 562 | mris.dedup(); |
| 563 | assert_eq!(mris.len(), 11); |
| 564 | } |
| 565 | |
| 566 | #[test] |
| 567 | fn demo_hands_queue_in_order_and_lowering_drops_the_own_entry() { |
| 568 | let mut roster = roster_of(&demo_guests(3)); |
| 569 | set_demo_hand(&mut roster, "8:orgid:demo-guest-1", true); |
| 570 | set_demo_hand(&mut roster, DEMO_OWN_MRI, true); |
| 571 | let ranks: Vec<u64> = roster.iter().filter_map(|entry| entry.hand.as_ref().map(|hand| hand.rank)).collect(); |
| 572 | assert_eq!(ranks, vec![1, 2]); |
| 573 | set_demo_hand(&mut roster, DEMO_OWN_MRI, false); |
| 574 | assert!(roster.iter().all(|entry| entry.mri != DEMO_OWN_MRI)); |
| 575 | } |
| 576 | |
| 577 | #[test] |
| 578 | fn the_demo_ring_names_a_caller_and_a_chat() { |
| 579 | let ring = demo_ring(); |
| 580 | assert_eq!(ring.caller.display_name, "Mara Lindqvist"); |
| 581 | assert_eq!(ring.thread_id.as_deref(), Some("demo-chat-mara")); |
| 582 | } |
| 583 | } |