BlueGit

ict rusty-teams

Files

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

control.rs

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 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 VideoStopped(VideoKey),
106 ScreenShare(Option<String>),
107 Camera(bool),
108 Cameras(Vec<CameraDevice>),
109 LocalShare(Option<String>),
110 ShareSources(Vec<ShareSource>),
111 ShareThumbnail { source: ShareSource, picture: Arc<VideoPicture> },
112 ShareSound(bool),
113 OwnHand(Option<u64>),
114 Reaction { mri: String, reaction: Reaction },
115 Captions(CaptionState),
116 Caption(CaptionEntry),
117 BlurTiming(f32),
118 Health(CallHealth),
119 IncomingVideo(bool),
120 Recording(bool),
121 RecordingPending(bool),
122 RecordingAudioOnly(bool),
123 Transcribing(bool),
124 ConsentRequired(bool),
125 Hold(HoldState),
126 Whiteboard(Option<ContentShare>),
127 WhiteboardUrl(String),
128 BreakoutMove(BreakoutMove),
129 BreakoutRoom { main: MeetingTarget },
130 ReplacementLink(Replacement),
131 TransferResult { succeeded: bool },
132 Capability(MeetingCapability),
133 MeetingOptions(std::result::Result<MeetingOptions, String>),
134 MeetingOptionsSaved(std::result::Result<MeetingOptions, String>),
135 Organizer { action: String, outcome: std::result::Result<u16, String> },
136 MeetingChat(String),
137 MeetingTenant(String),
138 Notice(String),
139}
140
141pub struct CallHandle {
142 pub commands: UnboundedSender<CallCommand>,
143 pub updates: UnboundedReceiver<CallUpdate>,
144 pub video: Arc<VideoHub>,
145}
146
147pub struct CallControl {
148 commands: UnboundedReceiver<CallCommand>,
149 updates: UnboundedSender<CallUpdate>,
150 deferred: Vec<CallCommand>,
151 pub video: Arc<VideoHub>,
152}
153
154pub fn call_channel() -> (CallHandle, CallControl) {
155 let (command_sender, command_receiver) = unbounded_channel();
156 let (update_sender, update_receiver) = unbounded_channel();
157 let video = VideoHub::new(update_sender.clone());
158 (
159 CallHandle {
160 commands: command_sender,
161 updates: update_receiver,
162 video: video.clone(),
163 },
164 CallControl {
165 commands: command_receiver,
166 updates: update_sender,
167 deferred: Vec::new(),
168 video,
169 },
170 )
171}
172
173impl CallControl {
174 pub fn send(&self, update: CallUpdate) {
175 let _ = self.updates.send(update);
176 }
177
178 pub fn updates(&self) -> UnboundedSender<CallUpdate> {
179 self.updates.clone()
180 }
181
182 pub fn defer(&mut self, command: CallCommand) {
183 self.deferred.push(command);
184 }
185
186 pub fn try_recv_command(&mut self) -> Option<CallCommand> {
187 if !self.deferred.is_empty() {
188 return Some(self.deferred.remove(0));
189 }
190 self.commands.try_recv().ok()
191 }
192
193 pub async fn recv(&mut self) -> Option<CallCommand> {
194 if !self.deferred.is_empty() {
195 return Some(self.deferred.remove(0));
196 }
197 self.commands.recv().await
198 }
199
200 /// Runs a setup step; a hangup while it is pending cancels it, other commands wait for the call loop.
201 pub async fn until_hangup<T>(&mut self, step: impl Future<Output = T>) -> Result<T> {
202 let CallControl { commands, deferred, .. } = self;
203 let hangup = async {
204 loop {
205 match commands.recv().await {
206 Some(CallCommand::Hangup) | None => return,
207 Some(other) => deferred.push(other),
208 }
209 }
210 };
211 tokio::select! {
212 output = step => Ok(output),
213 _ = hangup => Err(Error::Cancelled),
214 }
215 }
216}
217
218#[cfg(test)]
219mod tests {
220 use super::*;
221
222 #[tokio::test]
223 async fn hangup_cancels_a_pending_step_and_keeps_other_commands() {
224 let (handle, mut control) = call_channel();
225 handle.commands.send(CallCommand::Mute(MuteCommand::Mute)).unwrap();
226 handle.commands.send(CallCommand::Hangup).unwrap();
227 let outcome = control.until_hangup(std::future::pending::<()>()).await;
228 assert!(matches!(outcome, Err(Error::Cancelled)));
229 assert_eq!(control.recv().await, Some(CallCommand::Mute(MuteCommand::Mute)));
230 }
231
232 #[tokio::test]
233 async fn a_finished_step_returns_its_value() {
234 let (_handle, mut control) = call_channel();
235 assert_eq!(control.until_hangup(async { 7 }).await.unwrap(), 7);
236 }
237}