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