diff --git a/crates/core/src/bc/xml.rs b/crates/core/src/bc/xml.rs index 1b0a1e4..3f79ef6 100644 --- a/crates/core/src/bc/xml.rs +++ b/crates/core/src/bc/xml.rs @@ -746,6 +746,9 @@ pub struct LinkType { /// The Snap contains the binary jpeg image details #[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] pub struct Snap { + /// The snap xml version. Observed values "1.1" + #[yaserde(attribute)] + pub version: String, #[yaserde(rename = "channelId")] /// The channel id to get the snapshot from pub channel_id: u8, diff --git a/crates/core/src/bc_protocol/snap.rs b/crates/core/src/bc_protocol/snap.rs index b1dd9cf..4983b60 100644 --- a/crates/core/src/bc_protocol/snap.rs +++ b/crates/core/src/bc_protocol/snap.rs @@ -25,6 +25,7 @@ impl BcCamera { }), payload: Some(BcPayloads::BcXml(BcXml { snap: Some(Snap { + version: "1.1".to_string(), channel_id: self.channel_id, logic_channel: Some(self.channel_id), time: 0, diff --git a/src/common/instance.rs b/src/common/instance.rs index eeac953..23fbe03 100644 --- a/src/common/instance.rs +++ b/src/common/instance.rs @@ -128,17 +128,37 @@ impl NeoInstance { camera = None; None }, + Ok(neolink_core::Error::Io(e)) => { + log::debug!("Std IO Error"); + // Check if the inner error is the Other type and then the discomnect + let is_dropped = e.get_ref().is_some_and(|e| { + log::debug!("Std IO Error: Inner: {:?}", e); + matches!(e.downcast_ref::(), + Some(neolink_core::Error::DroppedConnection) | Some(neolink_core::Error::TimeoutDisconnected) + ) + }); + if is_dropped { + // Retry is a None + camera = None; + None + } else { + Some(Err(e.into())) + } + } Ok(e) => Some(Err(e.into())), Err(e) => { // Check if it is an io error + log::debug!("Other Error: {:?}", e); match e.downcast::() { Ok(e) => { - // Check if the inner error is the Other type and then the discomnect - let is_dropped = e.get_ref().is_some_and(|e| + log::debug!("Std IO Error"); + // Check if the inner error is the Other type and then the discomnect + let is_dropped = e.get_ref().is_some_and(|e| { + log::debug!("Std IO Error: Inner: {:?}", e); matches!(e.downcast_ref::(), Some(neolink_core::Error::DroppedConnection) | Some(neolink_core::Error::TimeoutDisconnected) ) - ); + }); if is_dropped { // Retry is a None camera = None; @@ -147,7 +167,10 @@ impl NeoInstance { Some(Err(e.into())) } }, - Err(e) =>Some(Err(e)) + Err(e) => { + log::debug!("Other Error: {:?}", e); + Some(Err(e)) + } } }, } diff --git a/src/image/mod.rs b/src/image/mod.rs index 1a740a1..9b3a4df 100644 --- a/src/image/mod.rs +++ b/src/image/mod.rs @@ -93,7 +93,11 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> { let mut buffer = File::create(file_path).await?; let jpeg_data = camera .run_task(|camera| Box::pin(async move { Ok(camera.get_snapshot().await?) })) - .await?; + .await; + if jpeg_data.is_err() { + log::debug!("jpeg_data: {:?}", jpeg_data); + } + let jpeg_data = jpeg_data?; buffer.write_all(jpeg_data.as_slice()).await?; } diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index b6f90bf..6d3665a 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -67,7 +67,7 @@ use tokio::{ watch::channel as watch, }, task::JoinSet, - time::{interval, sleep, Duration}, + time::{interval, sleep, Duration, Instant}, }; use tokio_stream::wrappers::IntervalStream; use tokio_stream::{wrappers::BroadcastStream, Stream, StreamExt}; @@ -1099,6 +1099,22 @@ async fn handle_data> + Unpin, E>( let mut rt = Duration::ZERO; let appsrc = app.clone(); + let (tx, mut rx) = mpsc(150); + let thread_appsrc = appsrc.clone(); + tokio::task::spawn(async move { + while let Some((time, buf)) = rx.recv().await { + let now = Instant::now(); + if now < time { + sleep(time - now).await; + } + thread_appsrc + .push_buffer(buf) + .map(|_| ()) + .map_err(|_| anyhow!("Could not push buffer to appsrc"))?; + } + AnyResult::Ok(()) + }); + while let Some(Ok(data)) = data_rx.next().await { check_live(app)?; // Stop if appsrc is dropped // Cache the last runtime @@ -1127,8 +1143,14 @@ async fn handle_data> + Unpin, E>( // Sync ft to rt if > 1500ms difference const MAX_DELTA_T: Duration = Duration::from_millis(1500); + let mut push_at = if ft > rt { + Instant::now() + (ft - rt) + } else { + Instant::now() + }; let delta_t = if rt > ft { rt - ft } else { ft - rt }; if delta_t > MAX_DELTA_T { + push_at = Instant::now(); ft = rt; } @@ -1146,10 +1168,8 @@ async fn handle_data> + Unpin, E>( gst_buf }; - appsrc - .push_buffer(buf) - .map(|_| ()) - .map_err(|_| anyhow!("Could not push buffer to appsrc"))?; + let send = (push_at, buf); + tx.send(send).await?; } } } @@ -1226,7 +1246,7 @@ fn build_unknown(bin: &Element) -> Result<()> { } fn build_h264(bin: &Element, stream_config: &StreamConfig) -> Result { - let buffer_size = stream_config.bitrate * 15 / 8; + let buffer_size = buffer_size(stream_config.bitrate); log::debug!("buffer_size: {buffer_size}"); let bin = bin .clone() @@ -1260,7 +1280,7 @@ fn build_h264(bin: &Element, stream_config: &StreamConfig) -> Result { } fn build_h265(bin: &Element, stream_config: &StreamConfig) -> Result { - let buffer_size = stream_config.bitrate * 15 / 8; + let buffer_size = buffer_size(stream_config.bitrate); log::debug!("buffer_size: {buffer_size}"); let bin = bin .clone() @@ -1293,7 +1313,7 @@ fn build_h265(bin: &Element, stream_config: &StreamConfig) -> Result { } fn build_aac(bin: &Element, stream_config: &StreamConfig) -> Result { - let buffer_size = stream_config.bitrate * 15 / 8; + let buffer_size = buffer_size(stream_config.bitrate); log::debug!("buffer_size: {buffer_size}"); let bin = bin .clone() @@ -1334,7 +1354,7 @@ fn build_aac(bin: &Element, stream_config: &StreamConfig) -> Result { } fn build_adpcm(bin: &Element, block_size: u32, stream_config: &StreamConfig) -> Result { - let buffer_size = stream_config.bitrate * 15 / 8; + let buffer_size = buffer_size(stream_config.bitrate); log::debug!("buffer_size: {buffer_size}"); let bin = bin .clone() @@ -1468,3 +1488,7 @@ fn make_queue(name: &str, buffer_size: u32) -> AnyResult { .map_err(|_| anyhow!("Cannot convert bin"))?; Ok(bin) } + +fn buffer_size(bitrate: u32) -> u32 { + std::cmp::max(bitrate * 15u32 / 8u32, 4u32 * 1024u32 * 1024u32) +}