From 5bf46f170e07b27b2ee21bc47630510b26fd7e06 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 31 Aug 2021 20:26:30 +0700 Subject: [PATCH] Make streams abortable like motion --- crates/core/src/bc_protocol/stream.rs | 20 +++++++--- crates/core/src/lib.rs | 5 --- src/rtsp/gst.rs | 4 +- src/rtsp/mod.rs | 54 ++++++++++++++------------- 4 files changed, 45 insertions(+), 38 deletions(-) diff --git a/crates/core/src/bc_protocol/stream.rs b/crates/core/src/bc_protocol/stream.rs index ba4aebd..27c0c3d 100644 --- a/crates/core/src/bc_protocol/stream.rs +++ b/crates/core/src/bc_protocol/stream.rs @@ -2,17 +2,23 @@ use super::{BcCamera, BinarySubscriber, Result}; use crate::{ bc::{model::*, xml::*}, bcmedia::model::*, - Never, }; /// Convience type for the error raised by the [StreamOutput] trait -pub type StreamOutputError = Result<()>; +pub type StreamOutputError = Result; /// The method [`BcCamera::start_video()`] requires a structure with this trait to pass the /// audio and video data back to pub trait StreamOutput { /// This is the callback raised a complete media packet is received - fn write(&mut self, media: BcMedia) -> StreamOutputError; + /// + /// If result is `Ok(true)` more messages will be sent + /// + /// If result if `Ok(false)` then message will be stopped + /// + /// If result is `Err(E)` then messages be stopped + /// and an error will be thrown + fn stream_recv(&mut self, media: BcMedia) -> StreamOutputError; } impl BcCamera { @@ -29,7 +35,7 @@ impl BcCamera { /// /// This will block forever or return an error when the camera connection is dropped /// - pub fn start_video(&self, data_outs: &mut Outputs, stream_name: &str) -> Result + pub fn start_video(&self, data_outs: &mut Outputs, stream_name: &str) -> Result<()> where Outputs: StreamOutput, { @@ -72,7 +78,11 @@ impl BcCamera { loop { let bc_media = BcMedia::deserialize(&mut media_sub)?; // We now have a complete interesting packet. Send it to on the callback - data_outs.write(bc_media)?; + match data_outs.stream_recv(bc_media) { + Ok(true) => {} + Ok(false) => return Ok(()), + Err(e) => return Err(e), + }; } } } diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 4b6076a..01ce1b6 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -31,11 +31,6 @@ pub mod bc_protocol; /// Contains low level structures and formats for the media substream pub mod bcmedia; -#[derive(Debug)] -/// Certain method just as `start_video` will block forever or return an error -/// In such a case the return type is `Result` -pub enum Never {} - /// This is the top level error structure of the library /// /// Most commands will either return their `Ok(result)` or this `Err(Error)` diff --git a/src/rtsp/gst.rs b/src/rtsp/gst.rs index 2955c07..77a67a1 100644 --- a/src/rtsp/gst.rs +++ b/src/rtsp/gst.rs @@ -57,7 +57,7 @@ enum StreamFormat { } impl StreamOutput for GstOutputs { - fn write(&mut self, media: BcMedia) -> StreamOutputError { + fn stream_recv(&mut self, media: BcMedia) -> StreamOutputError { match media { BcMedia::Iframe(payload) => { let video_type = match payload.video_type { @@ -88,7 +88,7 @@ impl StreamOutput for GstOutputs { } } - Ok(()) + Ok(true) } } diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index 120b614..b7d0fc3 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -30,7 +30,6 @@ /// use log::*; use neolink_core::bc_protocol::BcCamera; -use neolink_core::Never; use std::collections::HashSet; use std::fs; use std::sync::Arc; @@ -118,37 +117,40 @@ fn camera_loop( stream_name: &str, outputs: &mut GstOutputs, manage: bool, -) -> Result { +) -> Result<(), Error> { let min_backoff = Duration::from_secs(1); let max_backoff = Duration::from_secs(15); let mut current_backoff = min_backoff; loop { - let cam_err = camera_main(camera_config, stream_name, outputs, manage).unwrap_err(); - outputs.vidsrc.on_stream_error(); - outputs.audsrc.on_stream_error(); - // Authentication failures are permanent; we retry everything else - if cam_err.connected { - current_backoff = min_backoff; - } - match cam_err.err { - neolink_core::Error::AuthFailed => { - error!( - "Authentication failed to camera {}, not retrying", - camera_config.name - ); - return Err(cam_err.err.into()); + if let Err(cam_err) = camera_main(camera_config, stream_name, outputs, manage) { + outputs.vidsrc.on_stream_error(); + outputs.audsrc.on_stream_error(); + // Authentication failures are permanent; we retry everything else + if cam_err.connected { + current_backoff = min_backoff; + } + match cam_err.err { + neolink_core::Error::AuthFailed => { + error!( + "Authentication failed to camera {}, not retrying", + camera_config.name + ); + return Err(cam_err.err.into()); + } + _ => error!( + "Error streaming from camera {}, will retry in {}s: {:?}", + camera_config.name, + current_backoff.as_secs(), + cam_err.err + ), } - _ => error!( - "Error streaming from camera {}, will retry in {}s: {:?}", - camera_config.name, - current_backoff.as_secs(), - cam_err.err - ), - } - std::thread::sleep(current_backoff); - current_backoff = std::cmp::min(max_backoff, current_backoff * 2); + std::thread::sleep(current_backoff); + current_backoff = std::cmp::min(max_backoff, current_backoff * 2); + } else { + return Ok(()); + } } } @@ -207,7 +209,7 @@ fn camera_main( stream_name: &str, outputs: &mut GstOutputs, manage: bool, -) -> Result { +) -> Result<(), CameraErr> { let mut connected = false; (|| { let mut camera = BcCamera::new_with_addr(&camera_config.camera_addr, camera_config.channel_id)?;