| 1 | use std::future::Future; |
| 2 | |
| 3 | use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender, unbounded_channel}; |
| 4 | |
| 5 | use crate::devices::{DeviceChoice, DeviceLists}; |
| 6 | use crate::error::{Error, Result}; |
| 7 | use crate::mute::MuteCommand; |
| 8 | use crate::state::CallState; |
| 9 | |
| 10 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 11 | pub enum CallCommand { |
| 12 | Mute(MuteCommand), |
| 13 | SelectInput(DeviceChoice), |
| 14 | SelectOutput(DeviceChoice), |
| 15 | RefreshDevices, |
| 16 | Hangup, |
| 17 | } |
| 18 | |
| 19 | #[derive(Debug, Clone, PartialEq)] |
| 20 | pub 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 | |
| 30 | pub struct CallHandle { |
| 31 | pub commands: UnboundedSender<CallCommand>, |
| 32 | pub updates: UnboundedReceiver<CallUpdate>, |
| 33 | } |
| 34 | |
| 35 | pub struct CallControl { |
| 36 | commands: UnboundedReceiver<CallCommand>, |
| 37 | updates: UnboundedSender<CallUpdate>, |
| 38 | deferred: Vec<CallCommand>, |
| 39 | } |
| 40 | |
| 41 | pub 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 | |
| 57 | impl 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)] |
| 88 | mod 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 | } |