BlueGit

ict rusty-teams

Files

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

control.rs

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