BlueGit

ict rusty-teams

Files

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

control.rs

control.rs 3.1 KiB Download Raw
1use std::future::Future;
2
3use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender, unbounded_channel};
4
5use crate::devices::{DeviceChoice, DeviceLists};
6use crate::error::{Error, Result};
7use crate::mute::MuteCommand;
8use crate::state::CallState;
9
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum CallCommand {
12 Mute(MuteCommand),
13 SelectInput(DeviceChoice),
14 SelectOutput(DeviceChoice),
15 RefreshDevices,
16 Hangup,
17}
18
19#[derive(Debug, Clone, PartialEq)]
20pub enum CallUpdate {
21 State(CallState),
22 Muted(bool),
23 Levels { local: f32, remote: f32 },
24 Devices(DeviceLists),
25 Selected { input: DeviceChoice, output: DeviceChoice },
26 ListenOnly(bool),
27 Stats { inbound_packets: u64, outbound_packets: u64 },
28}
29
30pub struct CallHandle {
31 pub commands: UnboundedSender<CallCommand>,
32 pub updates: UnboundedReceiver<CallUpdate>,
33}
34
35pub struct CallControl {
36 commands: UnboundedReceiver<CallCommand>,
37 updates: UnboundedSender<CallUpdate>,
38 deferred: Vec<CallCommand>,
39}
40
41pub fn call_channel() -> (CallHandle, CallControl) {
42 let (command_sender, command_receiver) = unbounded_channel();
43 let (update_sender, update_receiver) = unbounded_channel();
44 (
45 CallHandle {
46 commands: command_sender,
47 updates: update_receiver,
48 },
49 CallControl {
50 commands: command_receiver,
51 updates: update_sender,
52 deferred: Vec::new(),
53 },
54 )
55}
56
57impl CallControl {
58 pub fn send(&self, update: CallUpdate) {
59 let _ = self.updates.send(update);
60 }
61
62 pub async fn recv(&mut self) -> Option<CallCommand> {
63 if !self.deferred.is_empty() {
64 return Some(self.deferred.remove(0));
65 }
66 self.commands.recv().await
67 }
68
69 /// Runs a setup step; a hangup while it is pending cancels it, other commands wait for the call loop.
70 pub async fn until_hangup<T>(&mut self, step: impl Future<Output = T>) -> Result<T> {
71 let CallControl { commands, deferred, .. } = self;
72 let hangup = async {
73 loop {
74 match commands.recv().await {
75 Some(CallCommand::Hangup) | None => return,
76 Some(other) => deferred.push(other),
77 }
78 }
79 };
80 tokio::select! {
81 output = step => Ok(output),
82 _ = hangup => Err(Error::Cancelled),
83 }
84 }
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90
91 #[tokio::test]
92 async fn hangup_cancels_a_pending_step_and_keeps_other_commands() {
93 let (handle, mut control) = call_channel();
94 handle.commands.send(CallCommand::Mute(MuteCommand::Mute)).unwrap();
95 handle.commands.send(CallCommand::Hangup).unwrap();
96 let outcome = control.until_hangup(std::future::pending::<()>()).await;
97 assert!(matches!(outcome, Err(Error::Cancelled)));
98 assert_eq!(control.recv().await, Some(CallCommand::Mute(MuteCommand::Mute)));
99 }
100
101 #[tokio::test]
102 async fn a_finished_step_returns_its_value() {
103 let (_handle, mut control) = call_channel();
104 assert_eq!(control.until_hangup(async { 7 }).await.unwrap(), 7);
105 }
106}