| 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::breakout::BreakoutMove; |
| 8 | use crate::camera::CameraDevice; |
| 9 | use crate::captions::{CaptionEntry, CaptionState}; |
| 10 | use crate::devices::{DeviceChoice, DeviceLists}; |
| 11 | use crate::diagnostics::{CallDiagnostics, StatsSample}; |
| 12 | use crate::effects::CameraEffects; |
| 13 | use crate::error::{Error, Result}; |
| 14 | use crate::health::CallHealth; |
| 15 | use crate::hold::HoldState; |
| 16 | use crate::meeting_options::{MeetingOptions, OptionWrite}; |
| 17 | use crate::mic_processing::MicSettings; |
| 18 | use crate::mute::MuteCommand; |
| 19 | use crate::organizer::{MeetingCapability, Modality}; |
| 20 | use crate::reaction::Reaction; |
| 21 | use crate::recording::{RecordingKind, StopScope}; |
| 22 | use crate::roster::{MeetingRole, RosterEntry}; |
| 23 | use crate::screen::ShareSource; |
| 24 | use crate::signaling::{Callee, MeetingTarget}; |
| 25 | use crate::state::CallState; |
| 26 | use crate::transfer::Replacement; |
| 27 | use crate::video_frame::{VideoHub, VideoKey, VideoPicture}; |
| 28 | use crate::video_layout::VideoWant; |
| 29 | use crate::whiteboard::ContentShare; |
| 30 | |
| 31 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 32 | pub enum CallCommand { |
| 33 | Mute(MuteCommand), |
| 34 | DisableAudio, |
| 35 | SelectInput(DeviceChoice), |
| 36 | SelectOutput(DeviceChoice), |
| 37 | SetMicSettings(MicSettings), |
| 38 | RefreshDevices, |
| 39 | SetCamera(bool), |
| 40 | SelectCamera(DeviceChoice), |
| 41 | StartShare(ShareSource), |
| 42 | StopShare, |
| 43 | SetShareSound(bool), |
| 44 | SetShareMotion(bool), |
| 45 | SetShareStandout(bool), |
| 46 | RefreshShareSources, |
| 47 | SetHand(bool), |
| 48 | LowerHand { |
| 49 | mri: String, |
| 50 | }, |
| 51 | LowerAllHands, |
| 52 | Admit { |
| 53 | mri: String, |
| 54 | }, |
| 55 | AdmitAll, |
| 56 | Deny { |
| 57 | mri: String, |
| 58 | }, |
| 59 | MuteParticipant { |
| 60 | mri: String, |
| 61 | }, |
| 62 | MuteAll, |
| 63 | Spotlight { |
| 64 | mri: String, |
| 65 | }, |
| 66 | StopSpotlight { |
| 67 | mri: String, |
| 68 | }, |
| 69 | RemoveParticipant { |
| 70 | mri: String, |
| 71 | }, |
| 72 | SetLock(bool), |
| 73 | SetRole { |
| 74 | mri: String, |
| 75 | role: MeetingRole, |
| 76 | }, |
| 77 | SetAttendeeModality { |
| 78 | mri: String, |
| 79 | modality: Modality, |
| 80 | allowed: bool, |
| 81 | }, |
| 82 | SetAttendeeRestrictions { |
| 83 | mic_off: bool, |
| 84 | camera_off: bool, |
| 85 | }, |
| 86 | Invite { |
| 87 | callees: Vec<Callee>, |
| 88 | group_thread: Option<String>, |
| 89 | }, |
| 90 | LoadMeetingOptions, |
| 91 | SaveMeetingOptions(Vec<OptionWrite>), |
| 92 | SetCaptions(bool), |
| 93 | SetBackground(BackgroundChoice), |
| 94 | SetIncomingVideo(bool), |
| 95 | SetVideoWants(Vec<VideoWant>), |
| 96 | SetCameraEffects(CameraEffects), |
| 97 | SendReaction(Reaction), |
| 98 | StartRecording { |
| 99 | kind: RecordingKind, |
| 100 | title: String, |
| 101 | }, |
| 102 | StopRecording { |
| 103 | scope: StopScope, |
| 104 | }, |
| 105 | SetCaptionLanguage(String), |
| 106 | ConsentToRecording, |
| 107 | Hold(bool), |
| 108 | Transfer { |
| 109 | target: Callee, |
| 110 | replaces: Option<Replacement>, |
| 111 | }, |
| 112 | OpenWhiteboard { |
| 113 | title: String, |
| 114 | }, |
| 115 | Hangup, |
| 116 | EndMeeting, |
| 117 | } |
| 118 | |
| 119 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 120 | pub enum Progress { |
| 121 | Calling, |
| 122 | Ringing, |
| 123 | } |
| 124 | |
| 125 | #[derive(Debug, Clone, PartialEq)] |
| 126 | pub enum CallUpdate { |
| 127 | State(CallState), |
| 128 | Muted(bool), |
| 129 | Levels { |
| 130 | local: f32, |
| 131 | remote: f32, |
| 132 | }, |
| 133 | Devices(DeviceLists), |
| 134 | Selected { |
| 135 | input: DeviceChoice, |
| 136 | output: DeviceChoice, |
| 137 | }, |
| 138 | ListenOnly(bool), |
| 139 | Stats { |
| 140 | inbound_packets: u64, |
| 141 | outbound_packets: u64, |
| 142 | }, |
| 143 | Sample(StatsSample), |
| 144 | Diagnostics(Box<CallDiagnostics>), |
| 145 | Progress(Progress), |
| 146 | Roster(Vec<RosterEntry>), |
| 147 | Speakers(Vec<String>), |
| 148 | Lobby(bool), |
| 149 | OwnIdentity { |
| 150 | mri: String, |
| 151 | }, |
| 152 | VideoReady, |
| 153 | VideoStopped(VideoKey), |
| 154 | ScreenShare(Option<String>), |
| 155 | Camera(bool), |
| 156 | Cameras(Vec<CameraDevice>), |
| 157 | LocalShare(Option<String>), |
| 158 | ShareSources(Vec<ShareSource>), |
| 159 | ShareThumbnail { |
| 160 | source: ShareSource, |
| 161 | picture: Arc<VideoPicture>, |
| 162 | }, |
| 163 | ShareSound(bool), |
| 164 | OwnHand(Option<u64>), |
| 165 | Reaction { |
| 166 | mri: String, |
| 167 | reaction: Reaction, |
| 168 | }, |
| 169 | Captions(CaptionState), |
| 170 | Caption(CaptionEntry), |
| 171 | BlurTiming(f32), |
| 172 | Health(CallHealth), |
| 173 | IncomingVideo(bool), |
| 174 | Recording(bool), |
| 175 | RecordingPending(bool), |
| 176 | RecordingAudioOnly(bool), |
| 177 | Transcribing(bool), |
| 178 | ConsentRequired(bool), |
| 179 | Hold(HoldState), |
| 180 | Whiteboard(Option<ContentShare>), |
| 181 | WhiteboardUrl(String), |
| 182 | BreakoutMove(BreakoutMove), |
| 183 | BreakoutRoom { |
| 184 | main: MeetingTarget, |
| 185 | }, |
| 186 | ReplacementLink(Replacement), |
| 187 | TransferResult { |
| 188 | succeeded: bool, |
| 189 | }, |
| 190 | Capability(MeetingCapability), |
| 191 | MeetingOptions(std::result::Result<MeetingOptions, String>), |
| 192 | MeetingOptionsSaved(std::result::Result<MeetingOptions, String>), |
| 193 | Organizer { |
| 194 | action: String, |
| 195 | outcome: std::result::Result<u16, String>, |
| 196 | }, |
| 197 | MeetingChat(String), |
| 198 | MeetingTenant(String), |
| 199 | Notice(String), |
| 200 | } |
| 201 | |
| 202 | pub struct CallHandle { |
| 203 | pub commands: UnboundedSender<CallCommand>, |
| 204 | pub updates: UnboundedReceiver<CallUpdate>, |
| 205 | pub video: Arc<VideoHub>, |
| 206 | } |
| 207 | |
| 208 | pub struct CallControl { |
| 209 | commands: UnboundedReceiver<CallCommand>, |
| 210 | updates: UnboundedSender<CallUpdate>, |
| 211 | deferred: Vec<CallCommand>, |
| 212 | pub video: Arc<VideoHub>, |
| 213 | } |
| 214 | |
| 215 | pub fn call_channel() -> (CallHandle, CallControl) { |
| 216 | let (command_sender, command_receiver) = unbounded_channel(); |
| 217 | let (update_sender, update_receiver) = unbounded_channel(); |
| 218 | let video = VideoHub::new(update_sender.clone()); |
| 219 | ( |
| 220 | CallHandle { |
| 221 | commands: command_sender, |
| 222 | updates: update_receiver, |
| 223 | video: video.clone(), |
| 224 | }, |
| 225 | CallControl { |
| 226 | commands: command_receiver, |
| 227 | updates: update_sender, |
| 228 | deferred: Vec::new(), |
| 229 | video, |
| 230 | }, |
| 231 | ) |
| 232 | } |
| 233 | |
| 234 | impl CallControl { |
| 235 | pub fn send(&self, update: CallUpdate) { |
| 236 | let _ = self.updates.send(update); |
| 237 | } |
| 238 | |
| 239 | pub fn updates(&self) -> UnboundedSender<CallUpdate> { |
| 240 | self.updates.clone() |
| 241 | } |
| 242 | |
| 243 | pub fn defer(&mut self, command: CallCommand) { |
| 244 | self.deferred.push(command); |
| 245 | } |
| 246 | |
| 247 | pub fn try_recv_command(&mut self) -> Option<CallCommand> { |
| 248 | if !self.deferred.is_empty() { |
| 249 | return Some(self.deferred.remove(0)); |
| 250 | } |
| 251 | self.commands.try_recv().ok() |
| 252 | } |
| 253 | |
| 254 | pub async fn recv(&mut self) -> Option<CallCommand> { |
| 255 | if !self.deferred.is_empty() { |
| 256 | return Some(self.deferred.remove(0)); |
| 257 | } |
| 258 | self.commands.recv().await |
| 259 | } |
| 260 | |
| 261 | /// Runs a setup step; a hangup while it is pending cancels it, other commands wait for the call loop. |
| 262 | pub async fn until_hangup<T>(&mut self, step: impl Future<Output = T>) -> Result<T> { |
| 263 | let CallControl { |
| 264 | commands, deferred, .. |
| 265 | } = self; |
| 266 | let hangup = async { |
| 267 | loop { |
| 268 | match commands.recv().await { |
| 269 | Some(CallCommand::Hangup) | None => return, |
| 270 | Some(other) => deferred.push(other), |
| 271 | } |
| 272 | } |
| 273 | }; |
| 274 | tokio::select! { |
| 275 | output = step => Ok(output), |
| 276 | _ = hangup => Err(Error::Cancelled), |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | #[cfg(test)] |
| 282 | mod tests { |
| 283 | use super::*; |
| 284 | |
| 285 | #[tokio::test] |
| 286 | async fn hangup_cancels_a_pending_step_and_keeps_other_commands() { |
| 287 | let (handle, mut control) = call_channel(); |
| 288 | handle |
| 289 | .commands |
| 290 | .send(CallCommand::Mute(MuteCommand::Mute)) |
| 291 | .unwrap(); |
| 292 | handle.commands.send(CallCommand::Hangup).unwrap(); |
| 293 | let outcome = control.until_hangup(std::future::pending::<()>()).await; |
| 294 | assert!(matches!(outcome, Err(Error::Cancelled))); |
| 295 | assert_eq!( |
| 296 | control.recv().await, |
| 297 | Some(CallCommand::Mute(MuteCommand::Mute)) |
| 298 | ); |
| 299 | } |
| 300 | |
| 301 | #[tokio::test] |
| 302 | async fn a_finished_step_returns_its_value() { |
| 303 | let (_handle, mut control) = call_channel(); |
| 304 | assert_eq!(control.until_hangup(async { 7 }).await.unwrap(), 7); |
| 305 | } |
| 306 | } |