diff --git a/src/common/instance.rs b/src/common/instance.rs index d025c12..30a028f 100644 --- a/src/common/instance.rs +++ b/src/common/instance.rs @@ -5,13 +5,14 @@ //! The camera watch is used as an event to be triggered //! whenever the camera is lost/updated use anyhow::{anyhow, Context}; -use futures::TryFutureExt; +use futures::{stream::FuturesUnordered, FutureExt, StreamExt, TryFutureExt}; use std::sync::{Arc, Weak}; #[cfg(feature = "pushnoti")] use tokio::sync::watch::channel as watch; use tokio::{ sync::{ - mpsc::Sender as MpscSender, oneshot::channel as oneshot, watch::Receiver as WatchReceiver, + mpsc::Receiver as MpscReceiver, mpsc::Sender as MpscSender, oneshot::channel as oneshot, + watch::Receiver as WatchReceiver, }, time::{sleep, Duration}, }; @@ -19,9 +20,12 @@ use tokio_util::sync::CancellationToken; #[cfg(feature = "pushnoti")] use super::PushNoti; -use super::{MdState, NeoCamCommand, NeoCamThreadState, Permit}; +use super::{MdState, NeoCamCommand, NeoCamThreadState, Permit, UseCounter}; use crate::{config::CameraConfig, AnyResult, Result}; -use neolink_core::bc_protocol::BcCamera; +use neolink_core::{ + bc_protocol::{BcCamera, StreamKind}, + bcmedia::model::BcMedia, +}; #[cfg(feature = "gstreamer")] /// This instance is the primary interface used throughout the app @@ -342,6 +346,202 @@ impl NeoInstance { timeout, } } + + /// Streams a camera source while not paused + pub(crate) async fn stream_while_live( + &self, + stream: StreamKind, + ) -> AnyResult> { + let config = self.config().await?.borrow().clone(); + let name = config.name.clone(); + + let media_rx = if config.pause.on_motion { + let (media_tx, media_rx) = tokio::sync::mpsc::channel(100); + let counter = UseCounter::new().await; + + let mut md = self.motion().await?; + let mut tasks = FuturesUnordered::new(); + // Stream for 5s on a new client always + // This lets us negotiate the camera stream type + let init_permit = counter.create_activated().await?; + tokio::spawn( + async { + tokio::time::sleep(tokio::time::Duration::from_secs(5)).await; + drop(init_permit); + } + .map(|e| { + log::debug!("Init permit thread stopped {e:?}"); + e + }), + ); + + // Create the permit for controlling the motion + let mut md_permit = { + let md_state = md.borrow_and_update().clone(); + match md_state { + MdState::Start(_) => { + log::info!("{name}::{stream:?}: Starting with Motion"); + counter.create_activated().await? + } + MdState::Stop(_) | MdState::Unknown => { + log::info!("{name}::{stream:?}: Waiting with Motion"); + counter.create_deactivated().await? + } + } + }; + // Now listen to the motion + let thread_name = name.clone(); + tasks.push(tokio::spawn( + async move { + loop { + match md.changed().await { + Ok(_) => { + let md_state: MdState = md.borrow_and_update().clone(); + match md_state { + MdState::Start(_) => { + log::info!("{thread_name}::{stream:?}: Motion Started"); + md_permit.activate().await?; + } + MdState::Stop(_) => { + log::info!("{thread_name}::{stream:?}: Motion Stopped"); + md_permit.deactivate().await?; + } + MdState::Unknown => {} + } + } + Err(e) => { + // Use break here so we can define the full type on the async closure + break AnyResult::Err(e.into()); + } + } + }?; + AnyResult::Ok(()) + } + .map(|e| { + log::debug!("Motion thread stopped {e:?}"); + e + }), + )); + + #[cfg(pushnoti)] + { + // Creates a permit for controlling based on the PN + let pn_permit = counter.create_deactivated().await?; + let mut pn = self.push_notifications().await?; + pn.borrow_and_update(); // Ignore any PNs that have already been sent before this + let thread_name = name.clone(); + tasks.push(tokio::spawn( + async move { + loop { + let noti: Option = pn.borrow_and_update().clone(); + if let Some(noti) = noti { + if noti.message.contains("MOTION") { + log::info!( + "{thread_name}::{stream:?}: Push Notification Recieved" + ); + let mut new_pn_permit = pn_permit.subscribe(); + new_pn_permit.activate().await?; + tokio::spawn(async move { + tokio::time::sleep(tokio::time::Duration::from_secs(30)) + .await; + drop(new_pn_permit); + }); + } + } + if let Err(e) = pn.changed().await { + break Err(e); + } + }?; + AnyResult::Ok(()) + } + .map(|e| { + log::debug!("PN thread stopped {e:?}"); + e + }), + )); + } + + // Send the camera when the pemit is active + let camera_permit = counter.create_deactivated().await?; + let thread_camera = self.clone(); + tokio::spawn( + async move { + loop { + if let Err(e) = camera_permit.aquired_users().await { + break AnyResult::Err(e); + } + log::debug!("Starting stream"); + tokio::select! { + v = camera_permit.dropped_users() => { + log::debug!("Dropped users: {v:?}"); + v + }, + v = async { + log::debug!("Getting stream"); + let mut stream = thread_camera.stream(stream).await?; + log::debug!("Got stream"); + while let Some(media) = stream.recv().await { + media_tx.send(media).await?; + } + AnyResult::Ok(()) + } => { + log::debug!("Stopped stream: {v:?}"); + v + }, + v = tasks.next() => { + log::debug!("Task failed: {v:?}"); + Err(anyhow!("Task ended prematurly: {v:?}")) + } + }?; + log::debug!("Pausing stream"); + }?; + drop(counter); // Make sure counter is owned by this thread + AnyResult::Ok(()) + } + .map(|e| { + log::debug!("Stream thread stopped {e:?}"); + e + }), + ); + + Ok(media_rx) + } else { + self.stream(stream).await + }?; + + Ok(media_rx) + } + + /// Streams a camera source + pub(crate) async fn stream(&self, stream: StreamKind) -> AnyResult> { + let (media_tx, media_rx) = tokio::sync::mpsc::channel(100); + let config = self.config().await?.borrow().clone(); + let strict = config.strict; + let thread_camera = self.clone(); + tokio::task::spawn( + tokio::task::spawn(async move { + thread_camera + .run_task(move |cam| { + let media_tx = media_tx.clone(); + Box::pin(async move { + let mut media_stream = cam.start_video(stream, 0, strict).await?; + log::trace!("Camera started"); + while let Ok(media) = media_stream.get_data().await? { + media_tx.send(media).await?; + } + AnyResult::Ok(()) + }) + }) + .await + }) + .and_then(|res| async move { + log::debug!("Camera finished streaming: {res:?}"); + Ok(()) + }), + ); + + Ok(media_rx) + } } // A task that is run on a camera when the structure is dropped diff --git a/src/common/mdthread.rs b/src/common/mdthread.rs index 2c060e4..03748b1 100644 --- a/src/common/mdthread.rs +++ b/src/common/mdthread.rs @@ -17,6 +17,7 @@ use super::NeoInstance; use crate::{AnyResult, Result}; use neolink_core::bc_protocol::MotionStatus; +#[derive(Clone, Debug)] #[allow(dead_code)] pub(crate) enum MdState { Start(Instant), diff --git a/src/rtsp/factory.rs b/src/rtsp/factory.rs index af78dc9..9ba058e 100644 --- a/src/rtsp/factory.rs +++ b/src/rtsp/factory.rs @@ -1,4 +1,3 @@ -use futures::TryFutureExt; use gstreamer::ClockTime; use std::{collections::HashMap, time::Duration}; @@ -168,38 +167,13 @@ pub(super) async fn make_factory( let name = name.clone(); tokio::task::spawn(async move { clear_bin(&element)?; - log::debug!("{name}::{stream}: Starting camera"); + log::trace!("{name}::{stream}: Starting camera"); // Start the camera - let (media_tx, mut media_rx) = tokio::sync::mpsc::channel(100); let config = camera.config().await?.borrow().clone(); - let strict = config.strict; - let thread_camera = camera.clone(); - tokio::task::spawn( - tokio::task::spawn(async move { - thread_camera - .run_task(move |cam| { - let media_tx = media_tx.clone(); - Box::pin(async move { - let mut media_stream = - cam.start_video(stream, 0, strict).await?; - log::debug!("Camera started"); - while let Ok(media) = media_stream.get_data().await? { - log::debug!("Sending media frame"); - media_tx.send(media).await?; - } - AnyResult::Ok(()) - }) - }) - .await - }) - .and_then(|res| async move { - log::debug!("Camera finished streaming: {res:?}"); - Ok(()) - }), - ); + let mut media_rx = camera.stream_while_live(stream).await?; - log::debug!("{name}::{stream}: Learning camera stream type"); + log::trace!("{name}::{stream}: Learning camera stream type"); // Learn the camera data type let mut buffer = vec![]; let mut frame_count = 0usize; @@ -207,7 +181,6 @@ pub(super) async fn make_factory( let mut stream_config = StreamConfig::new(&camera, stream).await?; while let Some(media) = media_rx.recv().await { stream_config.update_from_media(&media); - buffer.push(media); if frame_count > 10 || (stream_config.vid_type.is_some() @@ -218,7 +191,7 @@ pub(super) async fn make_factory( frame_count += 1; } - log::debug!("{name}::{stream}: Building the pipeline"); + log::trace!("{name}::{stream}: Building the pipeline"); // Build the right video pipeline let vid_src = match stream_config.vid_type.as_ref() { Some(VideoType::H264) => { @@ -263,7 +236,7 @@ pub(super) async fn make_factory( ); } - log::debug!("{name}::{stream}: Sending pipeline to gstreamer"); + log::trace!("{name}::{stream}: Sending pipeline to gstreamer"); // Send the pipeline back to the factory so it can start let _ = reply.send(element); @@ -274,7 +247,7 @@ pub(super) async fn make_factory( let mut vid_ts = 0u32; let mut pools = Default::default(); - log::debug!("{name}::{stream}: Sending buffered frames"); + log::trace!("{name}::{stream}: Sending buffered frames"); for buffered in buffer.drain(..) { send_to_sources( buffered, @@ -287,7 +260,7 @@ pub(super) async fn make_factory( )?; } - log::debug!("{name}::{stream}: Sending new frames"); + log::trace!("{name}::{stream}: Sending new frames"); while let Some(data) = media_rx.blocking_recv() { let r = send_to_sources( data, @@ -303,7 +276,7 @@ pub(super) async fn make_factory( } r?; } - log::info!("All media recieved"); + log::trace!("All media recieved"); AnyResult::Ok(()) }); AnyResult::Ok(()) @@ -355,7 +328,7 @@ fn send_to_sources( .duration() .expect("Could not calculate ADPCM duration"); if let Some(aud_src) = aud_src.as_ref() { - log::debug!("Sending ADPCM: {:?}", Duration::from_micros(*aud_ts as u64)); + log::trace!("Sending ADPCM: {:?}", Duration::from_micros(*aud_ts as u64)); send_to_appsrc( aud_src, adpcm.data, @@ -368,7 +341,7 @@ fn send_to_sources( BcMedia::Iframe(BcMediaIframe { data, .. }) | BcMedia::Pframe(BcMediaPframe { data, .. }) => { if let Some(vid_src) = vid_src.as_ref() { - log::debug!("Sending VID: {:?}", Duration::from_micros(*vid_ts as u64)); + log::trace!("Sending VID: {:?}", Duration::from_micros(*vid_ts as u64)); send_to_appsrc(vid_src, data, Duration::from_micros(*vid_ts as u64), pools)?; } const MICROSECONDS: u32 = 1000000; @@ -408,31 +381,37 @@ fn send_to_appsrc( } } let buf = { - // let mut gst_buf = pool.acquire_buffer(None).unwrap(); let msg_size = data.len(); + + // Get or create a pool of this len let pool = pools.entry(msg_size).or_insert_with_key(|size| { let pool = gstreamer::BufferPool::new(); let mut pool_config = pool.config(); - pool_config.set_params(None, (*size) as u32, 8, 0); + // Set a max buffers to ensure we don't grow in memory endlessly + pool_config.set_params(None, (*size) as u32, 8, 32); pool.set_config(pool_config).unwrap(); - // let (allocator, alloc_parms) = pool.allocator().unwrap(); pool.set_active(true).unwrap(); pool }); - let mut gst_buf = pool.acquire_buffer(None).unwrap(); - // let mut gst_buf = gstreamer::Buffer::with_size(data.data.len()).unwrap(); - { - let gst_buf_mut = gst_buf.get_mut().unwrap(); + + // Get a buffer from the pool and then copy in the data + let gst_buf = { + let mut new_buf = pool.acquire_buffer(None).unwrap(); + let gst_buf_mut = new_buf.get_mut().unwrap(); let time = ClockTime::from_useconds(ts.as_micros() as u64); - // gst_buf_mut.set_dts(ClockTime::from_useconds(dts)); gst_buf_mut.set_dts(time); gst_buf_mut.set_pts(time); let mut gst_buf_data = gst_buf_mut.map_writable().unwrap(); gst_buf_data.copy_from_slice(data.as_slice()); - } + drop(gst_buf_data); + new_buf + }; + + // Return the new buffer with the data gst_buf }; + // Push buffer into the appsrc match appsrc.push_buffer(buf) { Ok(_) => { // log::info!( @@ -453,6 +432,7 @@ fn send_to_appsrc( } Err(e) => Err(anyhow!("Error in streaming: {e:?}")), }?; + // Check if we need to pause if appsrc.current_level_bytes() >= appsrc.max_bytes() * 2 / 3 && matches!(appsrc.current_state(), gstreamer::State::Paused) {