diff --git a/src/rtsp/gst/sender.rs b/src/rtsp/gst/sender.rs index 2e660f6..b6ff7b2 100644 --- a/src/rtsp/gst/sender.rs +++ b/src/rtsp/gst/sender.rs @@ -44,17 +44,6 @@ struct NeoBuffer { impl NeoBuffer { fn push(&mut self, item: Arc) { - let end_time = self.end_time(); - let frame_time = item.time; - if let Some(end_time) = end_time { - let delta_time = end_time - frame_time; - let delta_duration = Duration::from_micros(delta_time.unsigned_abs()); - if delta_duration > Duration::from_secs(1) { - debug!("Clearing buffer due to jump"); - self.buf.clear(); - } - } - self.buf.push_back(item); while self.buf.len() > BUFFER_SIZE { if self.buf.pop_front().is_none() { @@ -243,6 +232,18 @@ impl NeoMediaSenders { for client_data in self.client_data.values_mut() { client_data.add_data(data.clone()).await?; } + + let end_time = self.buffer.end_time(); + let frame_time = data.time; + if let Some(end_time) = end_time { + let delta_time = end_time - frame_time; + let delta_duration = Duration::from_micros(delta_time.unsigned_abs()); + if delta_duration > Duration::from_secs(1) { + debug!("Clearing buffer due to jump"); + self.clear_buffer().await?; + } + } + self.buffer.push(data); self.shared @@ -310,6 +311,7 @@ impl NeoMediaSenders { // Set them into the non init state // This will make them wait for the // buffer to be enough then jump to live + client.buffer.buf.clear(); client.inited = false; } Ok(()) @@ -495,6 +497,10 @@ impl NeoMediaSender { self.buffer.push(frame.clone()); } debug!("Buffer filled"); + + self.jump_to_live().await?; + } else if !self.inited { + debug!("Buffer not ready to init: {}", buffer.buf.len()); } Ok(()) } @@ -537,6 +543,14 @@ impl NeoMediaSender { async fn update(&mut self) -> AnyResult<()> { self.update_starttime().await?; + if !self + .vid + .as_ref() + .map(|x| x.pads().iter().all(|pad| pad.is_linked())) + .unwrap_or(false) + { + return Err(anyhow!("Vid src is closed")); + } if let Some(buftime) = self.get_buftime() { // debug!("Update: buftime: {}", buftime); while self diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index 8db2491..ced56da 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -284,13 +284,29 @@ async fn camera_main(camera: Camera) -> Result<(), CameraFailureKi .map_err(CameraFailureKind::Retry)?; // Wait for reason to restart tokio::select! { + v = async { + // Wait for motion start and client for start + let mut motion = paused.get_camera().listen_on_motion().await?; + let mut inter = tokio::time::interval(tokio::time::Duration::from_secs_f32(0.01)); + loop { + motion.await_start(Duration::ZERO).await?; + let total_clients = tags.iter().map(|tag| rtsp_thread.get_number_of_clients(tag)).collect::>().fold(0usize, |acc, f| acc + f.unwrap_or(0usize)).await; + if total_clients > 0 { + return Result::<()>::Ok(()); + } + inter.tick().await; + } + }, if paused.get_config().pause.on_motion && paused.get_config().pause.on_disconnect => { + info!("Motion and Client Resume"); + v.with_context(|| "Error while processing motion/client messages") + }, v = async { // Wait for motion start let mut motion = paused.get_camera().listen_on_motion().await?; motion.await_start(Duration::ZERO).await - }, if paused.get_config().pause.on_motion => { + }, if paused.get_config().pause.on_motion && !paused.get_config().pause.on_disconnect => { info!("Motion Resume"); - v.map_err(|e| anyhow!("Error while processing motion messages: {:?}", e)) + v.with_context(|| "Error while processing motion messages") }, v = async { // Wait for client to connect @@ -303,7 +319,7 @@ async fn camera_main(camera: Camera) -> Result<(), CameraFailureKi return Ok(()) } } - }, if paused.get_config().pause.on_disconnect => { + }, if paused.get_config().pause.on_disconnect && !paused.get_config().pause.on_motion => { info!("Client Resume"); v },