BlueGit

ict rusty-teams

Files

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

control.rs

control.rs 5.6 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 Reaction { mri: String, reaction: Reaction },
85 Captions(CaptionState),
86 Caption(CaptionEntry),
87 BlurTiming(f32),
88 Recording(bool),
89 ConsentRequired(bool),
90 Hold(HoldState),
91 Whiteboard(Option<ContentShare>),
92 WhiteboardUrl(String),
93 BreakoutMove(BreakoutMove),
94 BreakoutRoom { main: MeetingTarget },
95 ReplacementLink(String),
96 Organizer { action: String, outcome: std::result::Result<u16, String> },
97 MeetingChat(String),
98 Notice(String),
99}
100
101pub struct CallHandle {
102 pub commands: UnboundedSender<CallCommand>,
103 pub updates: UnboundedReceiver<CallUpdate>,
104 pub video: Arc<VideoHub>,
105}
106
107pub struct CallControl {
108 commands: UnboundedReceiver<CallCommand>,
109 updates: UnboundedSender<CallUpdate>,
110 deferred: Vec<CallCommand>,
111 pub video: Arc<VideoHub>,
112}
113
114pub fn call_channel() -> (CallHandle, CallControl) {
115 let (command_sender, command_receiver) = unbounded_channel();
116 let (update_sender, update_receiver) = unbounded_channel();
117 let video = VideoHub::new(update_sender.clone());
118 (
119 CallHandle {
120 commands: command_sender,
121 updates: update_receiver,
122 video: video.clone(),
123 },
124 CallControl {
125 commands: command_receiver,
126 updates: update_sender,
127 deferred: Vec::new(),
128 video,
129 },
130 )
131}
132
133impl CallControl {
134 pub fn send(&self, update: CallUpdate) {
135 let _ = self.updates.send(update);
136 }
137
138 pub fn updates(&self) -> UnboundedSender<CallUpdate> {
139 self.updates.clone()
140 }
141
142 pub fn try_recv_command(&mut self) -> Option<CallCommand> {
143 if !self.deferred.is_empty() {
144 return Some(self.deferred.remove(0));
145 }
146 self.commands.try_recv().ok()
147 }
148
149 pub async fn recv(&mut self) -> Option<CallCommand> {
150 if !self.deferred.is_empty() {
151 return Some(self.deferred.remove(0));
152 }
153 self.commands.recv().await
154 }
155
156 /// Runs a setup step; a hangup while it is pending cancels it, other commands wait for the call loop.
157 pub async fn until_hangup<T>(&mut self, step: impl Future<Output = T>) -> Result<T> {
158 let CallControl { commands, deferred, .. } = self;
159 let hangup = async {
160 loop {
161 match commands.recv().await {
162 Some(CallCommand::Hangup) | None => return,
163 Some(other) => deferred.push(other),
164 }
165 }
166 };
167 tokio::select! {
168 output = step => Ok(output),
169 _ = hangup => Err(Error::Cancelled),
170 }
171 }
172}
173
174#[cfg(test)]
175mod tests {
176 use super::*;
177
178 #[tokio::test]
179 async fn hangup_cancels_a_pending_step_and_keeps_other_commands() {
180 let (handle, mut control) = call_channel();
181 handle.commands.send(CallCommand::Mute(MuteCommand::Mute)).unwrap();
182 handle.commands.send(CallCommand::Hangup).unwrap();
183 let outcome = control.until_hangup(std::future::pending::<()>()).await;
184 assert!(matches!(outcome, Err(Error::Cancelled)));
185 assert_eq!(control.recv().await, Some(CallCommand::Mute(MuteCommand::Mute)));
186 }
187
188 #[tokio::test]
189 async fn a_finished_step_returns_its_value() {
190 let (_handle, mut control) = call_channel();
191 assert_eq!(control.until_hangup(async { 7 }).await.unwrap(), 7);
192 }
193}