use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Instant;
use tokio::sync::watch;
use tokio::task::JoinHandle;
use crate::{Error, Result};
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct BridgeStats {
pub received: u64,
pub sent: u64,
pub dropped: u64,
pub last_received: Option<Instant>,
}
pub struct Bridge {
task: JoinHandle<Result<()>>,
stats: watch::Receiver<BridgeStats>,
teardown: Teardown,
bitrate: Option<BitrateControl>,
}
impl Bridge {
pub(crate) fn spawn<F>(run: impl FnOnce(StatsSender) -> F) -> Self
where
F: Future<Output = Result<()>> + Send + 'static,
{
let (sender, stats) = watch::channel(BridgeStats::default());
Self {
task: tokio::spawn(run(StatsSender(sender))),
stats,
teardown: Teardown::default(),
bitrate: None,
}
}
pub(crate) fn with_bitrate_control(mut self, bitrate: BitrateControl) -> Self {
self.bitrate = Some(bitrate);
self
}
pub fn bitrate_control(&self) -> Option<BitrateControl> {
self.bitrate.clone()
}
pub(crate) fn with_teardown(mut self, teardown: Teardown) -> Self {
self.teardown = teardown;
self
}
pub fn stats(&self) -> BridgeStats {
*self.stats.borrow()
}
pub fn watch_stats(&self) -> watch::Receiver<BridgeStats> {
self.stats.clone()
}
pub fn is_finished(&self) -> bool {
self.task.is_finished()
}
pub fn abort(&self) {
self.task.abort();
}
}
impl Future for Bridge {
type Output = BridgeEnd;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let result = match Pin::new(&mut self.task).poll(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(Ok(result)) => result,
Poll::Ready(Err(err)) if err.is_cancelled() => Err(Error::BridgeAborted),
Poll::Ready(Err(err)) => Err(Error::BridgePanicked(err.to_string())),
};
let stats = *self.stats.borrow();
Poll::Ready(BridgeEnd {
result,
stats,
teardown: std::mem::take(&mut self.teardown),
})
}
}
#[non_exhaustive]
pub struct BridgeEnd {
pub result: Result<()>,
pub stats: BridgeStats,
pub teardown: Teardown,
}
#[derive(Clone, Debug)]
pub struct BitrateControl(watch::Sender<u32>);
impl BitrateControl {
pub(crate) fn new(bitrate: u32) -> (Self, watch::Receiver<u32>) {
let (sender, receiver) = watch::channel(bitrate);
(Self(sender), receiver)
}
pub fn set(&self, bitrate: u32) {
self.0.send_replace(bitrate);
}
pub fn get(&self) -> u32 {
*self.0.borrow()
}
}
#[derive(Default)]
pub struct Teardown(Option<TeardownFn>);
type TeardownFn = Box<dyn FnOnce(&mut r2r::Node) + Send>;
impl Teardown {
pub(crate) fn new(teardown: impl FnOnce(&mut r2r::Node) + Send + 'static) -> Self {
Self(Some(Box::new(teardown)))
}
pub fn run(self, node: &mut r2r::Node) {
if let Some(teardown) = self.0 {
teardown(node);
}
}
}
#[derive(Clone)]
pub(crate) struct StatsSender(watch::Sender<BridgeStats>);
impl StatsSender {
pub(crate) fn received(&self) {
self.0.send_modify(|stats| {
stats.received += 1;
stats.last_received = Some(Instant::now());
});
}
pub(crate) fn sent(&self) {
self.0.send_modify(|stats| stats.sent += 1);
}
pub(crate) fn dropped(&self) {
self.0.send_modify(|stats| stats.dropped += 1);
}
pub(crate) fn snapshot(&self) -> BridgeStats {
*self.0.borrow()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn reports_live_and_final_counts() {
let (go, wait) = tokio::sync::oneshot::channel::<()>();
let bridge = Bridge::spawn(|stats| async move {
stats.received();
stats.sent();
stats.received();
stats.dropped();
wait.await.unwrap();
Ok(())
});
let mut watch = bridge.watch_stats();
watch.wait_for(|stats| stats.dropped == 1).await.unwrap();
let live = bridge.stats();
assert_eq!((live.received, live.sent, live.dropped), (2, 1, 1));
assert!(live.last_received.is_some());
go.send(()).unwrap();
let end = bridge.await;
assert!(end.result.is_ok());
assert_eq!(end.stats, live);
}
#[tokio::test]
async fn a_failed_bridge_keeps_its_counts() {
let bridge = Bridge::spawn(|stats| async move {
stats.received();
Err(Error::NoVideo("camera".into()))
});
let end = bridge.await;
assert!(matches!(end.result, Err(Error::NoVideo(_))));
assert_eq!(end.stats.received, 1);
}
#[tokio::test]
async fn abort_cancels_the_task() {
let bridge = Bridge::spawn(|_| std::future::pending());
bridge.abort();
assert!(matches!(bridge.await.result, Err(Error::BridgeAborted)));
}
#[tokio::test]
async fn a_panic_ends_the_bridge_with_an_error() {
let bridge = Bridge::spawn(|_| async { panic!("boom") });
assert!(matches!(bridge.await.result, Err(Error::BridgePanicked(_))));
}
}