BlueGit

ict rusty-teams

Files

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

control.rs

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