BlueGit

ict rusty-teams

Files

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

control.rs

control.rs 5.0 KiB Download Raw
1use std::future::Future;
2use std::sync::Arc;
3
4use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender, unbounded_channel};
5
6use crate::captions::{CaptionEntry, CaptionState};
7use crate::devices::{DeviceChoice, DeviceLists};
8use crate::error::{Error, Result};
9use crate::mute::MuteCommand;
10use crate::reaction::Reaction;
11use crate::roster::RosterEntry;
12use crate::camera::CameraDevice;
13use crate::screen::ShareSource;
14use crate::state::CallState;
15use crate::video_frame::VideoHub;
16
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub enum CallCommand {
19 Mute(MuteCommand),
20 SelectInput(DeviceChoice),
21 SelectOutput(DeviceChoice),
22 RefreshDevices,
23 SetCamera(bool),
24 SelectCamera(DeviceChoice),
25 StartShare(ShareSource),
26 StopShare,
27 SetShareSound(bool),
28 RefreshShareSources,
29 SetHand(bool),
30 LowerHand { mri: String },
31 LowerAllHands,
32 Admit { mri: String },
33 AdmitAll,
34 Deny { mri: String },
35 MuteParticipant { mri: String },
36 MuteAll,
37 Spotlight { mri: String },
38 StopSpotlight { mri: String },
39 RemoveParticipant { mri: String },
40 SetCaptions(bool),
41 SetBlur(bool),
42 SendReaction(Reaction),
43 Hangup,
44 EndMeeting,
45}
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub enum Progress {
49 Calling,
50 Ringing,
51}
52
53#[derive(Debug, Clone, PartialEq)]
54pub enum CallUpdate {
55 State(CallState),
56 Muted(bool),
57 Levels { local: f32, remote: f32 },
58 Devices(DeviceLists),
59 Selected { input: DeviceChoice, output: DeviceChoice },
60 ListenOnly(bool),
61 Stats { inbound_packets: u64, outbound_packets: u64 },
62 Progress(Progress),
63 Roster(Vec<RosterEntry>),
64 Speakers(Vec<String>),
65 Lobby(bool),
66 OwnIdentity { mri: String },
67 VideoReady,
68 ScreenShare(Option<String>),
69 Camera(bool),
70 Cameras(Vec<CameraDevice>),
71 LocalShare(Option<String>),
72 ShareSources(Vec<ShareSource>),
73 ShareSound(bool),
74 Reaction { mri: String, reaction: Reaction },
75 Captions(CaptionState),
76 Caption(CaptionEntry),
77 BlurTiming(f32),
78 Organizer { action: String, outcome: std::result::Result<u16, String> },
79 MeetingChat(String),
80 Notice(String),
81}
82
83pub struct CallHandle {
84 pub commands: UnboundedSender<CallCommand>,
85 pub updates: UnboundedReceiver<CallUpdate>,
86 pub video: Arc<VideoHub>,
87}
88
89pub struct CallControl {
90 commands: UnboundedReceiver<CallCommand>,
91 updates: UnboundedSender<CallUpdate>,
92 deferred: Vec<CallCommand>,
93 pub video: Arc<VideoHub>,
94}
95
96pub fn call_channel() -> (CallHandle, CallControl) {
97 let (command_sender, command_receiver) = unbounded_channel();
98 let (update_sender, update_receiver) = unbounded_channel();
99 let video = VideoHub::new(update_sender.clone());
100 (
101 CallHandle {
102 commands: command_sender,
103 updates: update_receiver,
104 video: video.clone(),
105 },
106 CallControl {
107 commands: command_receiver,
108 updates: update_sender,
109 deferred: Vec::new(),
110 video,
111 },
112 )
113}
114
115impl CallControl {
116 pub fn send(&self, update: CallUpdate) {
117 let _ = self.updates.send(update);
118 }
119
120 pub fn updates(&self) -> UnboundedSender<CallUpdate> {
121 self.updates.clone()
122 }
123
124 pub fn try_recv_command(&mut self) -> Option<CallCommand> {
125 if !self.deferred.is_empty() {
126 return Some(self.deferred.remove(0));
127 }
128 self.commands.try_recv().ok()
129 }
130
131 pub async fn recv(&mut self) -> Option<CallCommand> {
132 if !self.deferred.is_empty() {
133 return Some(self.deferred.remove(0));
134 }
135 self.commands.recv().await
136 }
137
138 /// Runs a setup step; a hangup while it is pending cancels it, other commands wait for the call loop.
139 pub async fn until_hangup<T>(&mut self, step: impl Future<Output = T>) -> Result<T> {
140 let CallControl { commands, deferred, .. } = self;
141 let hangup = async {
142 loop {
143 match commands.recv().await {
144 Some(CallCommand::Hangup) | None => return,
145 Some(other) => deferred.push(other),
146 }
147 }
148 };
149 tokio::select! {
150 output = step => Ok(output),
151 _ = hangup => Err(Error::Cancelled),
152 }
153 }
154}
155
156#[cfg(test)]
157mod tests {
158 use super::*;
159
160 #[tokio::test]
161 async fn hangup_cancels_a_pending_step_and_keeps_other_commands() {
162 let (handle, mut control) = call_channel();
163 handle.commands.send(CallCommand::Mute(MuteCommand::Mute)).unwrap();
164 handle.commands.send(CallCommand::Hangup).unwrap();
165 let outcome = control.until_hangup(std::future::pending::<()>()).await;
166 assert!(matches!(outcome, Err(Error::Cancelled)));
167 assert_eq!(control.recv().await, Some(CallCommand::Mute(MuteCommand::Mute)));
168 }
169
170 #[tokio::test]
171 async fn a_finished_step_returns_its_value() {
172 let (_handle, mut control) = call_channel();
173 assert_eq!(control.until_hangup(async { 7 }).await.unwrap(), 7);
174 }
175}