From 48892344b87a6f560ec22fd52e64f99d6131f475 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Mon, 21 Aug 2023 11:00:45 +0700 Subject: [PATCH] Add pings to the camera while paused --- src/rtsp/states/loggedin.rs | 32 ++++++++++++++++++++++++++------ src/rtsp/states/streaming.rs | 16 +++++++++++++++- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/rtsp/states/loggedin.rs b/src/rtsp/states/loggedin.rs index 84c90a7..f0987bb 100644 --- a/src/rtsp/states/loggedin.rs +++ b/src/rtsp/states/loggedin.rs @@ -7,8 +7,11 @@ use super::{camera::Camera, connected::Connected, streaming::Streaming}; use anyhow::Result; +use futures::stream::StreamExt; use log::*; use neolink_core::bc_protocol::BcCamera; +use tokio::time::{interval, Duration}; +use tokio_stream::wrappers::IntervalStream; pub(crate) struct LoggedIn { pub(crate) camera: BcCamera, @@ -98,12 +101,29 @@ impl Camera { &self.state.camera } - #[allow(unused)] + #[allow(dead_code)] pub(crate) async fn join(&self) -> Result<()> { - self.state - .camera - .join() - .await - .map_err(|e| anyhow::anyhow!("Camera join error: {:?}", e)) + tokio::select! { + v = async { + self.state + .camera + .join() + .await + .map_err(|e| anyhow::anyhow!("Camera join error: {:?}", e)) + } => v, + v = self.keepalive() => {v} + } + } + + async fn keepalive(&self) -> Result<()> { + let mut interval = IntervalStream::new(interval(Duration::from_secs(5))); + while let Some(_update) = interval.next().await { + if self.state.camera.ping().await.is_err() { + break; + } + } + + futures::pending!(); // Never actually finish, has to be aborted + Ok(()) } } diff --git a/src/rtsp/states/streaming.rs b/src/rtsp/states/streaming.rs index 1f6092d..c4ea8cb 100644 --- a/src/rtsp/states/streaming.rs +++ b/src/rtsp/states/streaming.rs @@ -11,7 +11,8 @@ use log::*; use neolink_core::bcmedia::model::BcMedia; use tokio::sync::RwLock; use tokio::task::JoinHandle; -use tokio::time::{timeout, Duration}; +use tokio::time::{interval, timeout, Duration}; +use tokio_stream::wrappers::IntervalStream; use neolink_core::bc_protocol::{BcCamera, StreamKind as Stream}; @@ -157,10 +158,23 @@ impl Camera { Ok(()) } => v, v = self.state.camera.join() => v.map_err(|e| anyhow!("Camera join error: {:?}", e)), + v = self.keepalive() => {v}, }?; Ok(()) } + async fn keepalive(&self) -> Result<()> { + let mut interval = IntervalStream::new(interval(Duration::from_secs(5))); + while let Some(_update) = interval.next().await { + if self.state.camera.ping().await.is_err() { + break; + } + } + + futures::pending!(); // Never actually finish, has to be aborted + Ok(()) + } + pub(crate) fn get_camera(&self) -> &BcCamera { &self.state.camera }