BlueGit

ict rusty-teams

Files

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

control.rs

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