BlueGit

ict rusty-teams

Files

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

control.rs

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