BlueGit

ict rusty-teams

Files

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

control.rs

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