BlueGit

ict rusty-teams

Files

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

control.rs

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