BlueGit

ict rusty-teams

Files

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

control.rs

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