| 1 | use std::future::Future; |
| 2 | use std::sync::Arc; |
| 3 | |
| 4 | use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender, unbounded_channel}; |
| 5 | |
| 6 | use crate::devices::{DeviceChoice, DeviceLists}; |
| 7 | use crate::error::{Error, Result}; |
| 8 | use crate::mute::MuteCommand; |
| 9 | use crate::roster::RosterEntry; |
| 10 | use crate::camera::CameraDevice; |
| 11 | use crate::screen::ShareSource; |
| 12 | use crate::state::CallState; |
| 13 | use crate::video_frame::VideoHub; |
| 14 | |
| 15 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 16 | pub enum CallCommand { |
| 17 | Mute(MuteCommand), |
| 18 | SelectInput(DeviceChoice), |
| 19 | SelectOutput(DeviceChoice), |
| 20 | RefreshDevices, |
| 21 | SetCamera(bool), |
| 22 | SelectCamera(DeviceChoice), |
| 23 | StartShare(ShareSource), |
| 24 | StopShare, |
| 25 | RefreshShareSources, |
| 26 | Hangup, |
| 27 | EndMeeting, |
| 28 | } |
| 29 | |
| 30 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 31 | pub enum Progress { |
| 32 | Calling, |
| 33 | Ringing, |
| 34 | } |
| 35 | |
| 36 | #[derive(Debug, Clone, PartialEq)] |
| 37 | pub enum CallUpdate { |
| 38 | State(CallState), |
| 39 | Muted(bool), |
| 40 | Levels { local: f32, remote: f32 }, |
| 41 | Devices(DeviceLists), |
| 42 | Selected { input: DeviceChoice, output: DeviceChoice }, |
| 43 | ListenOnly(bool), |
| 44 | Stats { inbound_packets: u64, outbound_packets: u64 }, |
| 45 | Progress(Progress), |
| 46 | Roster(Vec<RosterEntry>), |
| 47 | Speakers(Vec<String>), |
| 48 | Lobby(bool), |
| 49 | OwnIdentity { mri: String }, |
| 50 | VideoReady, |
| 51 | ScreenShare(Option<String>), |
| 52 | Camera(bool), |
| 53 | Cameras(Vec<CameraDevice>), |
| 54 | LocalShare(Option<String>), |
| 55 | ShareSources(Vec<ShareSource>), |
| 56 | Notice(String), |
| 57 | } |
| 58 | |
| 59 | pub struct CallHandle { |
| 60 | pub commands: UnboundedSender<CallCommand>, |
| 61 | pub updates: UnboundedReceiver<CallUpdate>, |
| 62 | pub video: Arc<VideoHub>, |
| 63 | } |
| 64 | |
| 65 | pub struct CallControl { |
| 66 | commands: UnboundedReceiver<CallCommand>, |
| 67 | updates: UnboundedSender<CallUpdate>, |
| 68 | deferred: Vec<CallCommand>, |
| 69 | pub video: Arc<VideoHub>, |
| 70 | } |
| 71 | |
| 72 | pub fn call_channel() -> (CallHandle, CallControl) { |
| 73 | let (command_sender, command_receiver) = unbounded_channel(); |
| 74 | let (update_sender, update_receiver) = unbounded_channel(); |
| 75 | let video = VideoHub::new(update_sender.clone()); |
| 76 | ( |
| 77 | CallHandle { |
| 78 | commands: command_sender, |
| 79 | updates: update_receiver, |
| 80 | video: video.clone(), |
| 81 | }, |
| 82 | CallControl { |
| 83 | commands: command_receiver, |
| 84 | updates: update_sender, |
| 85 | deferred: Vec::new(), |
| 86 | video, |
| 87 | }, |
| 88 | ) |
| 89 | } |
| 90 | |
| 91 | impl CallControl { |
| 92 | pub fn send(&self, update: CallUpdate) { |
| 93 | let _ = self.updates.send(update); |
| 94 | } |
| 95 | |
| 96 | pub fn updates(&self) -> UnboundedSender<CallUpdate> { |
| 97 | self.updates.clone() |
| 98 | } |
| 99 | |
| 100 | pub async fn recv(&mut self) -> Option<CallCommand> { |
| 101 | if !self.deferred.is_empty() { |
| 102 | return Some(self.deferred.remove(0)); |
| 103 | } |
| 104 | self.commands.recv().await |
| 105 | } |
| 106 | |
| 107 | /// Runs a setup step; a hangup while it is pending cancels it, other commands wait for the call loop. |
| 108 | pub async fn until_hangup<T>(&mut self, step: impl Future<Output = T>) -> Result<T> { |
| 109 | let CallControl { commands, deferred, .. } = self; |
| 110 | let hangup = async { |
| 111 | loop { |
| 112 | match commands.recv().await { |
| 113 | Some(CallCommand::Hangup) | None => return, |
| 114 | Some(other) => deferred.push(other), |
| 115 | } |
| 116 | } |
| 117 | }; |
| 118 | tokio::select! { |
| 119 | output = step => Ok(output), |
| 120 | _ = hangup => Err(Error::Cancelled), |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | #[cfg(test)] |
| 126 | mod tests { |
| 127 | use super::*; |
| 128 | |
| 129 | #[tokio::test] |
| 130 | async fn hangup_cancels_a_pending_step_and_keeps_other_commands() { |
| 131 | let (handle, mut control) = call_channel(); |
| 132 | handle.commands.send(CallCommand::Mute(MuteCommand::Mute)).unwrap(); |
| 133 | handle.commands.send(CallCommand::Hangup).unwrap(); |
| 134 | let outcome = control.until_hangup(std::future::pending::<()>()).await; |
| 135 | assert!(matches!(outcome, Err(Error::Cancelled))); |
| 136 | assert_eq!(control.recv().await, Some(CallCommand::Mute(MuteCommand::Mute))); |
| 137 | } |
| 138 | |
| 139 | #[tokio::test] |
| 140 | async fn a_finished_step_returns_its_value() { |
| 141 | let (_handle, mut control) = call_channel(); |
| 142 | assert_eq!(control.until_hangup(async { 7 }).await.unwrap(), 7); |
| 143 | } |
| 144 | } |