BlueGit

ict rusty-teams

Files

rusty-teams / crates / calling / src / control.rs

control.rs

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