diff --git a/src/common/streamthread.rs b/src/common/streamthread.rs index 11e93b5..cccd39e 100644 --- a/src/common/streamthread.rs +++ b/src/common/streamthread.rs @@ -147,9 +147,7 @@ impl NeoCamStreamThread { } let mut streams = self.streams.iter_mut().filter_map(|(name, stream)| if streams.contains(name) { Some(async move { - Result::<_, anyhow::Error>::Ok( - StreamInstance::new(&stream).await? - ) + StreamInstance::new(stream).await }) } else { None @@ -391,6 +389,10 @@ impl StreamInstance { pub(crate) async fn deactivate(&mut self) -> Result<()> { self.in_use.deactivate().await } + + pub(crate) async fn activator_handle(&mut self) -> CountUses { + self.in_use.subscribe() + } } impl Drop for StreamInstance { @@ -574,13 +576,12 @@ impl StreamData { _ => {}, } } - Result::<(),anyhow::Error>::Ok(()) }.await; Ok(res) }) }) => { match result { - Ok(Ok(())) => { + Ok(AnyResult::Ok(())) => { log::debug!("Video Stream Stopped due to no listeners"); break Ok(()); }, diff --git a/src/config.rs b/src/config.rs index d617090..e41b437 100644 --- a/src/config.rs +++ b/src/config.rs @@ -260,7 +260,7 @@ fn default_maxenc() -> String { "Aes".to_string() } -#[derive(Debug, Deserialize, Validate, Clone)] +#[derive(Debug, Deserialize, Validate, Clone, PartialEq)] pub(crate) struct PauseConfig { #[serde(default = "default_on_motion")] pub(crate) on_motion: bool, diff --git a/src/image/gst.rs b/src/image/gst.rs index bce2bb5..a47d6dd 100644 --- a/src/image/gst.rs +++ b/src/image/gst.rs @@ -3,7 +3,6 @@ use std::path::Path; use anyhow::{anyhow, Context, Result}; use gstreamer::{parse_launch, prelude::*, ClockTime, MessageView, Pipeline, State}; use gstreamer_app::AppSrc; -use neolink_core::bcmedia::model::VideoType; use tokio::{ sync::{ self, diff --git a/src/image/mod.rs b/src/image/mod.rs index 7e6bdb9..d05b488 100644 --- a/src/image/mod.rs +++ b/src/image/mod.rs @@ -20,7 +20,7 @@ use anyhow::{Context, Result}; use futures::stream::StreamExt; use log::*; -use neolink_core::{bc_protocol::*, bcmedia::model::*}; +use neolink_core::bc_protocol::*; use tokio::{fs::File, io::AsyncWriteExt}; use tokio_stream::wrappers::BroadcastStream; diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index 6738d07..cde9320 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -57,7 +57,7 @@ use gstreamer::{Bin, Caps, ClockTime, Element, ElementFactory}; use gstreamer_app::{AppSrc, AppSrcCallbacks, AppStreamType}; use gstreamer_rtsp_server::prelude::*; use log::*; -use neolink_core::{bc_protocol::StreamKind, bcmedia::model::*}; +use neolink_core::bc_protocol::StreamKind; use std::collections::{HashMap, HashSet}; use std::sync::Arc; use tokio::{ @@ -401,6 +401,8 @@ async fn stream_main( users: &HashSet, paths: &[String], ) -> Result<()> { + let mut camera_config = camera.config().await?.clone(); + let mut curr_pause; loop { // Wait for a valid stream format to be detected stream_instance @@ -412,14 +414,36 @@ async fn stream_main( }) .await?; + stream_instance.activate().await?; + + curr_pause = camera_config.borrow().pause.clone(); + let vid = stream_instance.vid.resubscribe(); let aud = stream_instance.aud.resubscribe(); let thread_stream_config = stream_instance.config.clone(); let mut thread_stream_config2 = stream_instance.config.clone(); + let mut set = JoinSet::>::new(); // Handles the on off of the stream with the client pause let client_counter = UseCounter::new().await; let client_count = client_counter.create_deactivated().await?; + if curr_pause.on_disconnect { + // Take over activation + let mut client_activator = stream_instance.activator_handle().await; + client_activator.activate().await?; + stream_instance.deactivate().await?; + let client_count = client_counter.create_deactivated().await?; + set.spawn(async move { + loop { + log::trace!("Activating Client"); + client_activator.activate().await?; + client_count.dropped_users().await?; + log::trace!("Pausing Client"); + client_activator.deactivate().await?; + client_count.aquired_users().await?; + } + }); + } // This select is for the streams config updates break tokio::select! { @@ -427,22 +451,10 @@ async fn stream_main( // If stream config changes we reload the stream continue; }, - v = async { - let client_count = client_counter.create_deactivated().await?; - while let Ok(()) = { - log::trace!("Activating Client"); - stream_instance.activate().await?; - client_count.dropped_users().await?; - log::trace!("Pausing Client"); - stream_instance.deactivate().await?; - client_count.aquired_users().await?; - AnyResult::Ok(()) - } {} - AnyResult::Ok(()) - } => { - log::trace!("Client Pause: {v:?}"); + _ = camera_config.wait_for(|new_conf| new_conf.pause != curr_pause ) => { + // If pause config changes restart continue; - } + }, v = stream_run(vid, aud, rtsp, thread_stream_config.borrow().clone(), users, paths, client_count) => v, }; }