BlueGit

ict rusty-teams

Files

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

control.rs

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