BlueGit

ict rusty-teams

Files

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

control.rs

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