BlueGit

ict rusty-teams

Files

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

control.rs

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