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