BlueGit

ict rusty-teams

Files

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

control.rs

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