BlueGit

ict rusty-teams

Files

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

control.rs

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