diff --git a/src/common/streamthread.rs b/src/common/streamthread.rs index 61a227d..f1a0eb4 100644 --- a/src/common/streamthread.rs +++ b/src/common/streamthread.rs @@ -407,6 +407,7 @@ pub(crate) struct StreamConfig { #[derive(Debug, Clone)] pub(crate) struct StampedData { + pub(crate) keyframe: bool, pub(crate) data: Arc>, pub(crate) ts: Duration, } @@ -552,6 +553,8 @@ impl StreamData { let watchdog_tx = watchdog_tx.clone(); log::debug!("Running Stream Instance Task"); Box::pin(async move { + let mut recieved_iframe = false; + let mut aud_keyframe = false; let res = async { let mut prev_ts = Duration::ZERO; let mut stream_data = camera.start_video(name, 0, strict).await?; @@ -636,9 +639,10 @@ impl StreamData { } match data { - BcMedia::Iframe(BcMediaIframe{data, microseconds, ..}) | BcMedia::Pframe(BcMediaPframe{data, microseconds,..}) => { + BcMedia::Iframe(BcMediaIframe{data, microseconds, ..}) => { prev_ts = Duration::from_micros(microseconds as u64); let d = StampedData{ + keyframe: true, data: Arc::new(data), ts: prev_ts }; @@ -649,13 +653,33 @@ impl StreamData { history.pop_front(); } }); + recieved_iframe = true; + aud_keyframe = true; + log::trace!("Sent Vid Key Frame"); + }, + BcMedia::Pframe(BcMediaPframe{data, microseconds,..}) if recieved_iframe => { + prev_ts = Duration::from_micros(microseconds as u64); + let d = StampedData{ + keyframe: false, + data: Arc::new(data), + ts: prev_ts + }; + let _ = vid_tx.send(d.clone()); + vid_history.send_modify(|history| { + history.push_back(d); + while history.len() > 100 { + history.pop_front(); + } + }); log::trace!("Sent Vid Frame"); } - BcMedia::Aac(BcMediaAac{data, ..}) | BcMedia::Adpcm(BcMediaAdpcm{data,..}) => { + BcMedia::Aac(BcMediaAac{data, ..}) | BcMedia::Adpcm(BcMediaAdpcm{data,..}) if recieved_iframe => { let d = StampedData{ - data: Arc::new(data), - ts: prev_ts, + keyframe: aud_keyframe, + data: Arc::new(data), + ts: prev_ts, }; + aud_keyframe = false; let _ = aud_tx.send(d.clone())?; aud_history.send_modify(|history| { history.push_back(d); diff --git a/src/image/mod.rs b/src/image/mod.rs index 3c0006a..761c577 100644 --- a/src/image/mod.rs +++ b/src/image/mod.rs @@ -52,8 +52,15 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> { let mut stream = BroadcastStream::new(stream_data.vid.resubscribe()) .filter(|f| futures::future::ready(f.is_ok())); // Filter to ignore lagged let buf = loop { - if let Some(Ok(StampedData { data, ts: _ })) = stream.next().await { - break data; + if let Some(Ok(StampedData { + data, + ts: _, + keyframe, + })) = stream.next().await + { + if keyframe { + break data; + } } }; diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index 8c29ce0..db4c892 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -678,13 +678,17 @@ async fn stream_run( _ = thread_stream_cancel.cancelled() => AnyResult::Ok(()), v = async { // Send Initial + let mut found_key_frame = false; for data in thread_vid_history.borrow().iter() { - thread_vid_data_tx.send( - StreamData::Media { - data: data.data.clone(), - ts: Duration::ZERO, - } - )?; + if data.keyframe || found_key_frame { + found_key_frame = true; + thread_vid_data_tx.send( + StreamData::Media { + data: data.data.clone(), + ts: Duration::ZERO, + } + )?; + } } // Send new @@ -716,13 +720,17 @@ async fn stream_run( _ = thread_stream_cancel.cancelled() => AnyResult::Ok(()), v = async { // Send Initial + let mut found_key_frame = false; for data in thread_aud_history.borrow().iter() { - thread_aud_data_tx.send( - StreamData::Media { - data: data.data.clone(), - ts: Duration::ZERO, - } - )?; + if data.keyframe || found_key_frame { + thread_aud_data_tx.send( + StreamData::Media { + data: data.data.clone(), + ts: Duration::ZERO, + } + )?; + found_key_frame = true; + } } // Send new while let Some(data) = audstream.next().await {