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