| 1 | use std::future::Future; |
| 2 | use std::sync::Arc; |
| 3 | |
| 4 | use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender, unbounded_channel}; |
| 5 | |
| 6 | use crate::background::BackgroundChoice; |
| 7 | use crate::effects::CameraEffects; |
| 8 | use crate::breakout::BreakoutMove; |
| 9 | use crate::captions::{CaptionEntry, CaptionState}; |
| 10 | use crate::devices::{DeviceChoice, DeviceLists}; |
| 11 | use crate::error::{Error, Result}; |
| 12 | use crate::health::CallHealth; |
| 13 | use crate::hold::HoldState; |
| 14 | use crate::mic_processing::MicSettings; |
| 15 | use crate::mute::MuteCommand; |
| 16 | use crate::reaction::Reaction; |
| 17 | use crate::roster::RosterEntry; |
| 18 | use crate::camera::CameraDevice; |
| 19 | use crate::screen::ShareSource; |
| 20 | use crate::signaling::{Callee, MeetingTarget}; |
| 21 | use crate::state::CallState; |
| 22 | use crate::video_frame::VideoHub; |
| 23 | use crate::whiteboard::ContentShare; |
| 24 | |
| 25 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 26 | pub enum CallCommand { |
| 27 | Mute(MuteCommand), |
| 28 | SelectInput(DeviceChoice), |
| 29 | SelectOutput(DeviceChoice), |
| 30 | SetMicSettings(MicSettings), |
| 31 | RefreshDevices, |
| 32 | SetCamera(bool), |
| 33 | SelectCamera(DeviceChoice), |
| 34 | StartShare(ShareSource), |
| 35 | StopShare, |
| 36 | SetShareSound(bool), |
| 37 | RefreshShareSources, |
| 38 | SetHand(bool), |
| 39 | LowerHand { mri: String }, |
| 40 | LowerAllHands, |
| 41 | Admit { mri: String }, |
| 42 | AdmitAll, |
| 43 | Deny { mri: String }, |
| 44 | MuteParticipant { mri: String }, |
| 45 | MuteAll, |
| 46 | Spotlight { mri: String }, |
| 47 | StopSpotlight { mri: String }, |
| 48 | RemoveParticipant { mri: String }, |
| 49 | SetCaptions(bool), |
| 50 | SetBackground(BackgroundChoice), |
| 51 | SetIncomingVideo(bool), |
| 52 | SetCameraEffects(CameraEffects), |
| 53 | SendReaction(Reaction), |
| 54 | SetRecording { on: bool, title: String }, |
| 55 | ConsentToRecording, |
| 56 | Hold(bool), |
| 57 | Transfer { target: Callee, replaces: Option<String> }, |
| 58 | OpenWhiteboard { title: String }, |
| 59 | Hangup, |
| 60 | EndMeeting, |
| 61 | } |
| 62 | |
| 63 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 64 | pub enum Progress { |
| 65 | Calling, |
| 66 | Ringing, |
| 67 | } |
| 68 | |
| 69 | #[derive(Debug, Clone, PartialEq)] |
| 70 | pub enum CallUpdate { |
| 71 | State(CallState), |
| 72 | Muted(bool), |
| 73 | Levels { local: f32, remote: f32 }, |
| 74 | Devices(DeviceLists), |
| 75 | Selected { input: DeviceChoice, output: DeviceChoice }, |
| 76 | ListenOnly(bool), |
| 77 | Stats { inbound_packets: u64, outbound_packets: u64 }, |
| 78 | Progress(Progress), |
| 79 | Roster(Vec<RosterEntry>), |
| 80 | Speakers(Vec<String>), |
| 81 | Lobby(bool), |
| 82 | OwnIdentity { mri: String }, |
| 83 | VideoReady, |
| 84 | ScreenShare(Option<String>), |
| 85 | Camera(bool), |
| 86 | Cameras(Vec<CameraDevice>), |
| 87 | LocalShare(Option<String>), |
| 88 | ShareSources(Vec<ShareSource>), |
| 89 | ShareSound(bool), |
| 90 | OwnHand(Option<u64>), |
| 91 | Reaction { mri: String, reaction: Reaction }, |
| 92 | Captions(CaptionState), |
| 93 | Caption(CaptionEntry), |
| 94 | BlurTiming(f32), |
| 95 | Health(CallHealth), |
| 96 | IncomingVideo(bool), |
| 97 | Recording(bool), |
| 98 | ConsentRequired(bool), |
| 99 | Hold(HoldState), |
| 100 | Whiteboard(Option<ContentShare>), |
| 101 | WhiteboardUrl(String), |
| 102 | BreakoutMove(BreakoutMove), |
| 103 | BreakoutRoom { main: MeetingTarget }, |
| 104 | ReplacementLink(String), |
| 105 | Organizer { action: String, outcome: std::result::Result<u16, String> }, |
| 106 | MeetingChat(String), |
| 107 | Notice(String), |
| 108 | } |
| 109 | |
| 110 | pub struct CallHandle { |
| 111 | pub commands: UnboundedSender<CallCommand>, |
| 112 | pub updates: UnboundedReceiver<CallUpdate>, |
| 113 | pub video: Arc<VideoHub>, |
| 114 | } |
| 115 | |
| 116 | pub struct CallControl { |
| 117 | commands: UnboundedReceiver<CallCommand>, |
| 118 | updates: UnboundedSender<CallUpdate>, |
| 119 | deferred: Vec<CallCommand>, |
| 120 | pub video: Arc<VideoHub>, |
| 121 | } |
| 122 | |
| 123 | pub fn call_channel() -> (CallHandle, CallControl) { |
| 124 | let (command_sender, command_receiver) = unbounded_channel(); |
| 125 | let (update_sender, update_receiver) = unbounded_channel(); |
| 126 | let video = VideoHub::new(update_sender.clone()); |
| 127 | ( |
| 128 | CallHandle { |
| 129 | commands: command_sender, |
| 130 | updates: update_receiver, |
| 131 | video: video.clone(), |
| 132 | }, |
| 133 | CallControl { |
| 134 | commands: command_receiver, |
| 135 | updates: update_sender, |
| 136 | deferred: Vec::new(), |
| 137 | video, |
| 138 | }, |
| 139 | ) |
| 140 | } |
| 141 | |
| 142 | impl CallControl { |
| 143 | pub fn send(&self, update: CallUpdate) { |
| 144 | let _ = self.updates.send(update); |
| 145 | } |
| 146 | |
| 147 | pub fn updates(&self) -> UnboundedSender<CallUpdate> { |
| 148 | self.updates.clone() |
| 149 | } |
| 150 | |
| 151 | pub fn defer(&mut self, command: CallCommand) { |
| 152 | self.deferred.push(command); |
| 153 | } |
| 154 | |
| 155 | pub fn try_recv_command(&mut self) -> Option<CallCommand> { |
| 156 | if !self.deferred.is_empty() { |
| 157 | return Some(self.deferred.remove(0)); |
| 158 | } |
| 159 | self.commands.try_recv().ok() |
| 160 | } |
| 161 | |
| 162 | pub async fn recv(&mut self) -> Option<CallCommand> { |
| 163 | if !self.deferred.is_empty() { |
| 164 | return Some(self.deferred.remove(0)); |
| 165 | } |
| 166 | self.commands.recv().await |
| 167 | } |
| 168 | |
| 169 | /// Runs a setup step; a hangup while it is pending cancels it, other commands wait for the call loop. |
| 170 | pub async fn until_hangup<T>(&mut self, step: impl Future<Output = T>) -> Result<T> { |
| 171 | let CallControl { commands, deferred, .. } = self; |
| 172 | let hangup = async { |
| 173 | loop { |
| 174 | match commands.recv().await { |
| 175 | Some(CallCommand::Hangup) | None => return, |
| 176 | Some(other) => deferred.push(other), |
| 177 | } |
| 178 | } |
| 179 | }; |
| 180 | tokio::select! { |
| 181 | output = step => Ok(output), |
| 182 | _ = hangup => Err(Error::Cancelled), |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | #[cfg(test)] |
| 188 | mod tests { |
| 189 | use super::*; |
| 190 | |
| 191 | #[tokio::test] |
| 192 | async fn hangup_cancels_a_pending_step_and_keeps_other_commands() { |
| 193 | let (handle, mut control) = call_channel(); |
| 194 | handle.commands.send(CallCommand::Mute(MuteCommand::Mute)).unwrap(); |
| 195 | handle.commands.send(CallCommand::Hangup).unwrap(); |
| 196 | let outcome = control.until_hangup(std::future::pending::<()>()).await; |
| 197 | assert!(matches!(outcome, Err(Error::Cancelled))); |
| 198 | assert_eq!(control.recv().await, Some(CallCommand::Mute(MuteCommand::Mute))); |
| 199 | } |
| 200 | |
| 201 | #[tokio::test] |
| 202 | async fn a_finished_step_returns_its_value() { |
| 203 | let (_handle, mut control) = call_channel(); |
| 204 | assert_eq!(control.until_hangup(async { 7 }).await.unwrap(), 7); |
| 205 | } |
| 206 | } |