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::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 SetShareMotion(bool),
39 SetShareStandout(bool),
40 RefreshShareSources,
41 SetHand(bool),
42 LowerHand { mri: String },
43 LowerAllHands,
44 Admit { mri: String },
45 AdmitAll,
46 Deny { mri: String },
47 MuteParticipant { mri: String },
48 MuteAll,
49 Spotlight { mri: String },
50 StopSpotlight { mri: String },
51 RemoveParticipant { mri: String },
52 SetCaptions(bool),
53 SetBackground(BackgroundChoice),
54 SetIncomingVideo(bool),
55 SetVideoWants(Vec<VideoWant>),
56 SetCameraEffects(CameraEffects),
57 SendReaction(Reaction),
58 SetRecording { on: bool, title: 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 ConsentRequired(bool),
103 Hold(HoldState),
104 Whiteboard(Option<ContentShare>),
105 WhiteboardUrl(String),
106 BreakoutMove(BreakoutMove),
107 BreakoutRoom { main: MeetingTarget },
108 ReplacementLink(String),
109 Organizer { action: String, outcome: std::result::Result<u16, String> },
110 MeetingChat(String),
111 Notice(String),
112}
113
114pub struct CallHandle {
115 pub commands: UnboundedSender<CallCommand>,
116 pub updates: UnboundedReceiver<CallUpdate>,
117 pub video: Arc<VideoHub>,
118}
119
120pub struct CallControl {
121 commands: UnboundedReceiver<CallCommand>,
122 updates: UnboundedSender<CallUpdate>,
123 deferred: Vec<CallCommand>,
124 pub video: Arc<VideoHub>,
125}
126
127pub fn call_channel() -> (CallHandle, CallControl) {
128 let (command_sender, command_receiver) = unbounded_channel();
129 let (update_sender, update_receiver) = unbounded_channel();
130 let video = VideoHub::new(update_sender.clone());
131 (
132 CallHandle {
133 commands: command_sender,
134 updates: update_receiver,
135 video: video.clone(),
136 },
137 CallControl {
138 commands: command_receiver,
139 updates: update_sender,
140 deferred: Vec::new(),
141 video,
142 },
143 )
144}
145
146impl CallControl {
147 pub fn send(&self, update: CallUpdate) {
148 let _ = self.updates.send(update);
149 }
150
151 pub fn updates(&self) -> UnboundedSender<CallUpdate> {
152 self.updates.clone()
153 }
154
155 pub fn defer(&mut self, command: CallCommand) {
156 self.deferred.push(command);
157 }
158
159 pub fn try_recv_command(&mut self) -> Option<CallCommand> {
160 if !self.deferred.is_empty() {
161 return Some(self.deferred.remove(0));
162 }
163 self.commands.try_recv().ok()
164 }
165
166 pub async fn recv(&mut self) -> Option<CallCommand> {
167 if !self.deferred.is_empty() {
168 return Some(self.deferred.remove(0));
169 }
170 self.commands.recv().await
171 }
172
173 /// Runs a setup step; a hangup while it is pending cancels it, other commands wait for the call loop.
174 pub async fn until_hangup<T>(&mut self, step: impl Future<Output = T>) -> Result<T> {
175 let CallControl { commands, deferred, .. } = self;
176 let hangup = async {
177 loop {
178 match commands.recv().await {
179 Some(CallCommand::Hangup) | None => return,
180 Some(other) => deferred.push(other),
181 }
182 }
183 };
184 tokio::select! {
185 output = step => Ok(output),
186 _ = hangup => Err(Error::Cancelled),
187 }
188 }
189}
190
191#[cfg(test)]
192mod tests {
193 use super::*;
194
195 #[tokio::test]
196 async fn hangup_cancels_a_pending_step_and_keeps_other_commands() {
197 let (handle, mut control) = call_channel();
198 handle.commands.send(CallCommand::Mute(MuteCommand::Mute)).unwrap();
199 handle.commands.send(CallCommand::Hangup).unwrap();
200 let outcome = control.until_hangup(std::future::pending::<()>()).await;
201 assert!(matches!(outcome, Err(Error::Cancelled)));
202 assert_eq!(control.recv().await, Some(CallCommand::Mute(MuteCommand::Mute)));
203 }
204
205 #[tokio::test]
206 async fn a_finished_step_returns_its_value() {
207 let (_handle, mut control) = call_channel();
208 assert_eq!(control.until_hangup(async { 7 }).await.unwrap(), 7);
209 }
210}