From b07aa2ddfe1cc02f27933bd2c640eb7d0ae4f46d Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Thu, 4 May 2023 09:10:52 +0700 Subject: [PATCH] Jump to live if more than 15s difference --- crates/core/src/bc/codex.rs | 32 ++++++++++----------- src/rtsp/gst/sender.rs | 56 ++++++++++++++++++------------------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/crates/core/src/bc/codex.rs b/crates/core/src/bc/codex.rs index 6ad3525..efba7e6 100644 --- a/crates/core/src/bc/codex.rs +++ b/crates/core/src/bc/codex.rs @@ -7,7 +7,7 @@ use crate::bc::model::*; use crate::bc::xml::*; use crate::{Credentials, Error, Result}; use bytes::BytesMut; -use std::io; +use nom::AsBytes; use tokio_util::codec::{Decoder, Encoder}; pub(crate) struct BcCodex { @@ -48,24 +48,24 @@ impl Decoder for BcCodex { type Error = Error; fn decode_eof(&mut self, buf: &mut BytesMut) -> Result> { - // match self.decode(buf)? { - // Some(frame) => Ok(Some(frame)), - // None => { - // if buf.is_empty() { - // Ok(None) - // } else { - // Err(io::Error::new( - // io::ErrorKind::Other, - // format!("bytes remaining on BC stream: {:X?}", buf), - // ) - // .into()) - // } - // } - // } match self.decode(buf)? { Some(frame) => Ok(Some(frame)), - None => Ok(None), + None => { + if buf.is_empty() { + Ok(None) + } else { + log::debug!("bytes remaining on BC stream: {:X?}", buf.as_bytes()); + // Right after this we seem to get an issue with the camera dropping us + // Needs probing + // \xf0\xde\xbc\n\x03\0\0\0@\x9c\0\0\0\x01\x04\0\xc8\0\0\0\0\0\0\0\t\x11\xc6\xa0\x12\xdb\xf7:E\x96* + Ok(None) + } + } } + // match self.decode(buf)? { + // Some(frame) => Ok(Some(frame)), + // None => Ok(None), + // } } fn decode(&mut self, src: &mut BytesMut) -> Result> { diff --git a/src/rtsp/gst/sender.rs b/src/rtsp/gst/sender.rs index 38833cb..eae7765 100644 --- a/src/rtsp/gst/sender.rs +++ b/src/rtsp/gst/sender.rs @@ -296,11 +296,11 @@ impl NeoMediaSenders { occ.remove(); continue; } - // if let Err(e) = occ.get_mut().process_jump_to_live(&self.buffer).await { - // debug!("Could not handle jump to live: {:?}", e); - // occ.remove(); - // continue; - // } + if let Err(e) = occ.get_mut().process_jump_to_live(&self.buffer).await { + debug!("Could not handle jump to live: {:?}", e); + occ.remove(); + continue; + } if let Err(e) = occ.get_mut().process_buffer(&self.buffer).await { debug!("Could not send client data: {:?}", e); occ.remove(); @@ -563,26 +563,26 @@ impl NeoMediaSender { Ok(last_sent_time) } - // async fn process_jump_to_live(&mut self, buffer: &NeoBuffer) -> AnyResult<()> { - // if self.inited { - // if let (Some(buffer_start), Some(buffer_end)) = (buffer.start_time(), buffer.end_time()) - // { - // let runtime = self.get_buftime().unwrap_or(self.last_sent_time); - // if runtime < buffer_start.saturating_sub(LATENCY * 2) - // || runtime > buffer_end.saturating_add(LATENCY * 2) - // { - // debug!( - // "Outside buffer jumping to live: {:?} < {:?} < {:?}", - // Duration::from_micros(buffer_start.try_into().unwrap_or(0)), - // Duration::from_micros(runtime.try_into().unwrap_or(0)), - // Duration::from_micros(buffer_end.try_into().unwrap_or(0)) - // ); - // self.jump_to_live(buffer).await?; - // } - // } - // } - // Ok(()) - // } + async fn process_jump_to_live(&mut self, buffer: &NeoBuffer) -> AnyResult<()> { + if self.inited { + if let (Some(buffer_start), Some(buffer_end)) = (buffer.start_time(), buffer.end_time()) + { + let runtime = self.get_buftime().unwrap_or(self.last_sent_time); + if runtime < buffer_start.saturating_sub(15 * SECS) + || runtime > buffer_end.saturating_add(15 * SECS) + { + debug!( + "Outside buffer jumping to live: {:?} < {:?} < {:?}", + Duration::from_micros(buffer_start.try_into().unwrap_or(0)), + Duration::from_micros(runtime.try_into().unwrap_or(0)), + Duration::from_micros(buffer_end.try_into().unwrap_or(0)) + ); + self.jump_to_live(buffer).await?; + } + } + } + Ok(()) + } fn get_runtime(&self) -> Option { if let Some(appsrc) = self.vid.as_ref() { @@ -598,9 +598,9 @@ impl NeoMediaSender { None } - // fn get_buftime(&self) -> Option { - // self.get_runtime().map(|time| self.runtime_to_buftime(time)) - // } + fn get_buftime(&self) -> Option { + self.get_runtime().map(|time| self.runtime_to_buftime(time)) + } async fn send_buffer(&mut self, media: &BcMedia) -> AnyResult { if self.inited && self.playing {