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