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