BlueGit

ict rusty-teams

Files

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

control.rs

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