diff --git a/src/bc_protocol.rs b/src/bc_protocol.rs index 02c8094..aa03fdb 100644 --- a/src/bc_protocol.rs +++ b/src/bc_protocol.rs @@ -6,6 +6,7 @@ use md5; use self::connection::BcConnection; use std::io::Write; use std::net::{SocketAddr, ToSocketAddrs}; +use std::time::Duration; use Md5Trunc::*; @@ -15,6 +16,7 @@ pub struct BcCamera { address: SocketAddr, connection: Option, logged_in: bool, + rx_timeout: Duration, } type Result = std::result::Result; @@ -67,9 +69,14 @@ impl BcCamera { address, connection: None, logged_in: false, + rx_timeout: Duration::from_secs(1), }) } + pub fn set_rx_timeout(&mut self, timeout: Duration) { + self.rx_timeout = timeout; + } + pub fn connect(&mut self) -> Result<()> { self.connection = Some(BcConnection::new(self.address)?); Ok(()) @@ -113,7 +120,7 @@ impl BcCamera { sub_login.send(legacy_login)?; - let legacy_reply = sub_login.rx.recv()?; + let legacy_reply = sub_login.rx.recv_timeout(self.rx_timeout)?; let nonce; match legacy_reply.body { BcBody::ModernMsg(ModernMsg { @@ -159,7 +166,7 @@ impl BcCamera { }); sub_login.send(modern_login)?; - let modern_reply = sub_login.rx.recv()?; + let modern_reply = sub_login.rx.recv_timeout(self.rx_timeout)?; let device_info; match modern_reply.body { @@ -238,9 +245,11 @@ impl BcCamera { sub_video.send(start_video)?; - for msg in sub_video.rx.iter() { + loop { + trace!("Getting video message..."); + let msg = sub_video.rx.recv_timeout(self.rx_timeout)?; if let Bc { body: BcBody::ModernMsg(ModernMsg { binary: Some(binary), .. }), .. } = msg { - debug!("Got {} bytes of video data", binary.len()); + trace!("Got {} bytes of video data", binary.len()); data_out.write_all(binary.as_slice())?; } else { @@ -248,8 +257,6 @@ impl BcCamera { debug!("Contents: {:?}", msg); } } - - Err(Error::Other("Video stream stopped")) } } diff --git a/src/config.rs b/src/config.rs index 428ffa2..f792cad 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,6 @@ use serde::Deserialize; use std::net::SocketAddr; +use std::time::Duration; #[derive(Debug, Deserialize)] pub struct Config { @@ -18,6 +19,8 @@ pub struct CameraConfig { pub username: String, pub password: Option, + + pub timeout: Option, } fn default_bind_addr() -> String { diff --git a/src/main.rs b/src/main.rs index e583313..55c4a0a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ use config::{Config, CameraConfig}; use cmdline::Opt; use err_derive::Error; use gst::RtspServer; -use log::error; +use log::*; use std::fs; use std::time::Duration; use std::io::Write; @@ -52,29 +52,31 @@ fn main() -> Result<(), Error> { fn camera_loop(camera_config: &CameraConfig, output: &mut dyn Write) -> Result<(), Error> { let mut backoff = ExponentialBackoff { - initial_interval: Duration::from_secs(5), - max_interval: Duration::from_secs(60), + initial_interval: Duration::from_secs(1), + max_interval: Duration::from_secs(10), ..Default::default() }; let mut op = || { camera_main(camera_config, output) }; - op.retry(&mut backoff).map_err(|backoff_err| -> Error { - match backoff_err { - Transient(e) => { - error!("Error streaming from camera {}, will retry: {}", camera_config.name, e); - e.into() - } - Permanent(e) => { - error!("Permanent error sreaming from camera {}: {}", camera_config.name, e); - e.into() - } - } - }) + let res = op.retry_notify(&mut backoff, |err, next: Duration| { + error!("Error streaming from camera {}, will retry in {}s: {}", camera_config.name, next.as_secs(), err); + }); + + let err = match res { + Ok(_) => unreachable!("should never return on success"), + Err(backoff::Error::Transient(e)) | + Err(backoff::Error::Permanent(e)) => e, + }; + + Err(err.into()) } fn camera_main(camera_config: &CameraConfig, output: &mut dyn Write) -> Result<(), backoff::Error> { let mut camera = BcCamera::new_with_addr(camera_config.camera_addr)?; + if let Some(timeout) = camera_config.timeout { + camera.set_rx_timeout(timeout); + } println!("{}: Connecting to camera at {}", camera_config.name, camera_config.camera_addr); camera.connect()?;