From 38ca3f262e7347aafb4340955f0b6ed9993a71c5 Mon Sep 17 00:00:00 2001 From: George Hilliard Date: Wed, 29 Apr 2020 23:58:44 -0500 Subject: [PATCH 1/9] Git: Do not attempt to diff/merge Cargo.lock --- .gitattributes | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..c353fc3 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +Cargo.lock binary From 1a373e1acdbcc247393f00b9d99113cfc45ddd13 Mon Sep 17 00:00:00 2001 From: George Hilliard Date: Wed, 29 Apr 2020 23:59:19 -0500 Subject: [PATCH 2/9] Implement exponential backoff/retry in case of errors from camera --- Cargo.lock | 161 ++++++++++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 1 + src/main.rs | 23 +++++++- 3 files changed, 179 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 64f6583..5930ce2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,12 +38,28 @@ dependencies = [ "winapi", ] +[[package]] +name = "autocfg" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2" + [[package]] name = "autocfg" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" +[[package]] +name = "backoff" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66483433ebf577e03c6546df761778e4cc40b00e5a1ea7ed850733ffb555d71f" +dependencies = [ + "instant", + "rand", +] + [[package]] name = "bitflags" version = "1.2.1" @@ -71,6 +87,15 @@ dependencies = [ "vec_map", ] +[[package]] +name = "cloudabi" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +dependencies = [ + "bitflags", +] + [[package]] name = "cookie-factory" version = "0.3.1" @@ -83,7 +108,7 @@ version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" dependencies = [ - "autocfg", + "autocfg 1.0.0", "cfg-if", "lazy_static", ] @@ -115,6 +140,12 @@ dependencies = [ "synstructure", ] +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + [[package]] name = "futures-channel" version = "0.3.4" @@ -601,6 +632,12 @@ dependencies = [ "unindent", ] +[[package]] +name = "instant" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7152d2aed88aa566e7a342250f21ba2222c1ae230ad577499dbfa3c18475b80" + [[package]] name = "lazy_static" version = "1.4.0" @@ -658,6 +695,7 @@ checksum = "0419348c027fa7be448d2ae7ea0e4e04c2334c31dc4e74ab29f00a2a7ca69204" name = "neolink" version = "0.1.0" dependencies = [ + "backoff", "cookie-factory", "crossbeam-utils", "env_logger", @@ -703,7 +741,7 @@ version = "0.1.42" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba" dependencies = [ - "autocfg", + "autocfg 1.0.0", "num-traits", ] @@ -713,7 +751,7 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef" dependencies = [ - "autocfg", + "autocfg 1.0.0", "num-integer", "num-traits", ] @@ -724,7 +762,7 @@ version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096" dependencies = [ - "autocfg", + "autocfg 1.0.0", ] [[package]] @@ -823,6 +861,121 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" +dependencies = [ + "autocfg 0.1.7", + "libc", + "rand_chacha", + "rand_core 0.4.2", + "rand_hc", + "rand_isaac", + "rand_jitter", + "rand_os", + "rand_pcg", + "rand_xorshift", + "winapi", +] + +[[package]] +name = "rand_chacha" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef" +dependencies = [ + "autocfg 0.1.7", + "rand_core 0.3.1", +] + +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + +[[package]] +name = "rand_hc" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4" +dependencies = [ + "rand_core 0.3.1", +] + +[[package]] +name = "rand_isaac" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08" +dependencies = [ + "rand_core 0.3.1", +] + +[[package]] +name = "rand_jitter" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b" +dependencies = [ + "libc", + "rand_core 0.4.2", + "winapi", +] + +[[package]] +name = "rand_os" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" +dependencies = [ + "cloudabi", + "fuchsia-cprng", + "libc", + "rand_core 0.4.2", + "rdrand", + "winapi", +] + +[[package]] +name = "rand_pcg" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44" +dependencies = [ + "autocfg 0.1.7", + "rand_core 0.4.2", +] + +[[package]] +name = "rand_xorshift" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" +dependencies = [ + "rand_core 0.3.1", +] + +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", +] + [[package]] name = "regex" version = "1.3.7" diff --git a/Cargo.toml b/Cargo.toml index 1d6598d..3bf7e0e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +backoff = "0.1" cookie-factory = "0.3" crossbeam-utils = "0.7" env_logger = "*" diff --git a/src/main.rs b/src/main.rs index c9f7a65..f5bd2db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ mod config; mod cmdline; mod gst; +use backoff::{ExponentialBackoff, Operation}; use bc_protocol::BcCamera; use config::{Config, CameraConfig}; use cmdline::Opt; @@ -13,6 +14,7 @@ use crossbeam_utils::thread; use err_derive::Error; use gst::RtspServer; use std::fs; +use std::time::Duration; use std::io::Write; use structopt::StructOpt; @@ -37,7 +39,7 @@ fn main() -> Result<(), Error> { s.spawn(move |_| { // TODO handle these errors let mut output = rtsp.add_stream(&camera.name).unwrap(); // TODO - camera_main(&camera, &mut output) + camera_loop(&camera, &mut output) }); } @@ -47,7 +49,24 @@ fn main() -> Result<(), Error> { Ok(()) } -fn camera_main(camera_config: &CameraConfig, output: &mut dyn Write) -> 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), + ..Default::default() + }; + let mut op = || { + camera_main(camera_config, output) + }; + op.retry(&mut backoff).map_err(|backoff_err| -> Error { + match backoff_err { + backoff::Error::Transient(e) | + backoff::Error::Permanent(e) => e.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)?; println!("{}: Connecting to camera at {}", camera_config.name, camera_config.camera_addr); From 00afbee06965b6c990eac50d0ff29fa890d2202f Mon Sep 17 00:00:00 2001 From: George Hilliard Date: Thu, 30 Apr 2020 18:08:04 -0500 Subject: [PATCH 3/9] Bc: Implement convenience function for constructing commands --- src/bc/model.rs | 8 ++++++++ src/bc_protocol.rs | 50 +++++++++++++++++++--------------------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/bc/model.rs b/src/bc/model.rs index 555a87c..ce522fd 100644 --- a/src/bc/model.rs +++ b/src/bc/model.rs @@ -68,6 +68,14 @@ pub struct BcContext { pub(super) in_bin_mode: HashSet, } +impl Bc { + /// Convenience function that constructs a modern Bc message from the given meta and XML, with + /// no binary payload. + pub fn new_from_xml(meta: BcMeta, xml: BcXml) -> Bc { + Bc { meta, body: BcBody::ModernMsg(ModernMsg { xml: Some(xml), binary: None }) } + } +} + impl BcContext { pub fn new() -> BcContext { BcContext { diff --git a/src/bc_protocol.rs b/src/bc_protocol.rs index 42f0492..b46210f 100644 --- a/src/bc_protocol.rs +++ b/src/bc_protocol.rs @@ -126,27 +126,23 @@ impl BcCamera { let md5_username = md5_string(&concat_username, Truncate); let md5_password = md5_string(&concat_password, Truncate); - let modern_login = Bc { - meta: BcMeta { + let modern_login = Bc::new_from_xml( + BcMeta { msg_id: MSG_ID_LOGIN, client_idx: 0, // TODO encrypted: true, class: 0x6414, }, - body: BcBody::ModernMsg(ModernMsg { - xml: Some(BcXml { - login_user: Some(LoginUser { - version: xml_ver(), - user_name: md5_username, - password: md5_password, - user_ver: 1, - }), - login_net: Some(LoginNet::default()), - ..Default::default() + BcXml { + login_user: Some(LoginUser { + version: xml_ver(), + user_name: md5_username, + password: md5_password, + user_ver: 1, }), - binary: None, - }), - }; + login_net: Some(LoginNet::default()), + ..Default::default() + }); modern_login.serialize(connection)?; @@ -186,26 +182,22 @@ impl BcCamera { pub fn start_video(&mut self, data_out: &mut dyn Write) -> Result<()> { let connection = self.connection.as_ref().expect("Must be connected to start video"); - let start_video = Bc { - meta: BcMeta { + let start_video = Bc::new_from_xml( + BcMeta { msg_id: MSG_ID_VIDEO, client_idx: 0, // TODO encrypted: true, class: 0x6414, // IDK why }, - body: BcBody::ModernMsg(ModernMsg { - xml: Some(BcXml { - preview: Some(Preview { - version: xml_ver(), - channel_id: 0, - handle: 0, - stream_type: "mainStream".to_string(), - }), - ..Default::default() + BcXml { + preview: Some(Preview { + version: xml_ver(), + channel_id: 0, + handle: 0, + stream_type: "mainStream".to_string(), }), - binary: None, - }), - }; + ..Default::default() + }); start_video.serialize(connection)?; From 0b8484b9b6f5a3abf869abaa4ab174a517bb71e8 Mon Sep 17 00:00:00 2001 From: George Hilliard Date: Thu, 30 Apr 2020 21:49:09 -0500 Subject: [PATCH 4/9] Refactor bc protocol to use connection management thread --- src/bc_protocol.rs | 82 ++++++++++++++++++------------- src/bc_protocol/connection.rs | 91 +++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 34 deletions(-) create mode 100644 src/bc_protocol/connection.rs diff --git a/src/bc_protocol.rs b/src/bc_protocol.rs index b46210f..79332a8 100644 --- a/src/bc_protocol.rs +++ b/src/bc_protocol.rs @@ -1,41 +1,59 @@ +use crate::bc; +use crate::bc::{model::*, xml::*}; use err_derive::Error; use log::*; use md5; +use self::connection::BcConnection; use std::io::Write; -use std::net::{SocketAddr, ToSocketAddrs, TcpStream}; -use super::bc::{model::*, xml::*}; +use std::net::{SocketAddr, ToSocketAddrs}; +use std::time::Duration; use Md5Trunc::*; +mod connection; + pub struct BcCamera { address: SocketAddr, bc_context: BcContext, - connection: Option, + connection: Option, logged_in: bool, } +type Result = std::result::Result; + #[derive(Debug, Error)] pub enum Error { #[error(display="Communication error")] CommunicationError(#[error(source)] std::io::Error), + #[error(display="Deserialization error")] - DeserializationError(#[error(source)] super::bc::de::Error), + DeserializationError(#[error(source)] bc::de::Error), + #[error(display="Serialization error")] - SerializationError(#[error(source)] super::bc::ser::Error), + SerializationError(#[error(source)] bc::ser::Error), + + #[error(display="Connection error")] + ConnectionError(#[error(source)] self::connection::Error), + #[error(display="Communication error")] UnintelligibleReply { - request: Bc, reply: Bc, why: &'static str, }, + + #[error(display="Dropped connection")] + DroppedConnection(#[error(source)] std::sync::mpsc::RecvError), + + #[error(display="Timeout")] + Timeout(#[error(source)] std::sync::mpsc::RecvTimeoutError), + #[error(display="Credential error")] AuthFailed, - #[error(display="Other error")] - OtherError(&'static str), -} -type Result = std::result::Result; + #[error(display="Other error")] + Other(&'static str), +} impl Drop for BcCamera { fn drop(&mut self) { @@ -46,7 +64,7 @@ impl Drop for BcCamera { impl BcCamera { pub fn new_with_addr(hostname: T) -> Result { let address = hostname.to_socket_addrs()?.next() - .ok_or(Error::OtherError("Address resolution failed"))?; + .ok_or(Error::Other("Address resolution failed"))?; Ok(Self { address, @@ -57,7 +75,7 @@ impl BcCamera { } pub fn connect(&mut self) -> Result<()> { - self.connection = Some(TcpStream::connect(self.address)?); + self.connection = Some(BcConnection::new(self.address)?); Ok(()) } @@ -70,6 +88,7 @@ impl BcCamera { pub fn login(&mut self, username: &str, password: Option<&str>) -> Result { let connection = self.connection.as_ref().expect("Must be connected to log in"); + let sub_login = connection.subscribe(MSG_ID_LOGIN)?; // Login flow is: Send legacy login message, expect back a modern message with Encryption // details. Then, re-send the login as a modern login message. Expect back a device info @@ -96,9 +115,9 @@ impl BcCamera { }) }; - legacy_login.serialize(connection)?; + sub_login.send(legacy_login)?; - let legacy_reply = Bc::deserialize(&mut self.bc_context, connection)?; + let legacy_reply = sub_login.rx.recv()?; let nonce; match legacy_reply.body { BcBody::ModernMsg(ModernMsg { @@ -107,7 +126,6 @@ impl BcCamera { nonce = encryption.nonce; } _ => return Err(Error::UnintelligibleReply { - request: legacy_login, reply: legacy_reply, why: "Expected an Encryption message back" }) @@ -144,9 +162,9 @@ impl BcCamera { ..Default::default() }); - modern_login.serialize(connection)?; + sub_login.send(modern_login)?; + let modern_reply = sub_login.rx.recv()?; - let modern_reply = Bc::deserialize(&mut self.bc_context, connection)?; let device_info; match modern_reply.body { BcBody::ModernMsg(ModernMsg { @@ -162,7 +180,6 @@ impl BcCamera { return Err(Error::AuthFailed) } _ => return Err(Error::UnintelligibleReply { - request: modern_login, reply: modern_reply, why: "Expected a DeviceInfo message back from login" }) @@ -179,8 +196,9 @@ impl BcCamera { Ok(()) } - pub fn start_video(&mut self, data_out: &mut dyn Write) -> Result<()> { + pub fn start_video(&self, data_out: &mut dyn Write) -> Result<()> { let connection = self.connection.as_ref().expect("Must be connected to start video"); + let sub_video = connection.subscribe(MSG_ID_VIDEO)?; let start_video = Bc::new_from_xml( BcMeta { @@ -199,24 +217,20 @@ impl BcCamera { ..Default::default() }); - start_video.serialize(connection)?; + sub_video.send(start_video)?; - loop { - let msg = Bc::deserialize(&mut self.bc_context, connection)?; - - match msg { - Bc { meta: BcMeta { msg_id: MSG_ID_VIDEO, .. }, - body: BcBody::ModernMsg(ModernMsg { binary: Some(binary), .. }) - } => { - debug!("Got {} bytes of video data", binary.len()); - data_out.write_all(binary.as_slice())?; - } - _ => { - info!("Ignoring uninteresting message ID {}", msg.meta.msg_id); - debug!("Contents: {:?}", msg); - } + for msg in sub_video.rx.iter() { + if let Bc { body: BcBody::ModernMsg(ModernMsg { binary: Some(binary), .. }), .. } = msg { + debug!("Got {} bytes of video data", binary.len()); + data_out.write_all(binary.as_slice())?; + } + else { + warn!("Ignoring weird video message"); + debug!("Contents: {:?}", msg); } } + + Err(Error::Other("Video stream stopped")) } } diff --git a/src/bc_protocol/connection.rs b/src/bc_protocol/connection.rs new file mode 100644 index 0000000..86ebee0 --- /dev/null +++ b/src/bc_protocol/connection.rs @@ -0,0 +1,91 @@ +use crate::bc; +use crate::bc::{model::*}; +use err_derive::Error; +use log::*; +use std::collections::BTreeMap; +use std::collections::btree_map::Entry; +use std::sync::Mutex; +use std::sync::mpsc::{channel, Sender, Receiver}; +use std::net::{TcpStream, SocketAddr}; + +pub struct BcConnection { + connection: TcpStream, + context: BcContext, + subscribers: Mutex>>, +} + +pub struct BcSubscription<'a> { + pub rx: Receiver, + conn: &'a BcConnection, + msg_id: u32, +} + +type Result = std::result::Result; + +#[derive(Debug, Error)] +pub enum Error { + #[error(display="Communication error")] + CommunicationError(#[error(source)] std::io::Error), + #[error(display="Deserialization error")] + DeserializationError(#[error(source)] bc::de::Error), + #[error(display="Serialization error")] + SerializationError(#[error(source)] bc::ser::Error), +} + +impl BcConnection { + pub fn new(addr: SocketAddr) -> Result { + let connection = TcpStream::connect(addr)?; + + Ok(BcConnection { + connection, + context: BcContext::new(), + subscribers: Mutex::new(BTreeMap::new()), + }) + } + + pub fn subscribe(&self, msg_id: u32) -> Result { + let (tx, rx) = channel(); + { + let mut locked_subs = self.subscribers.lock().unwrap(); + locked_subs.insert(msg_id, tx); + } + Ok(BcSubscription { rx, conn: self, msg_id }) + } + + pub fn poll(&mut self) -> Result<()> { + // TODO if the connection hangs up, hang up on all subscribers and delete our TcpStream + let response = Bc::deserialize(&mut self.context, &self.connection)?; + let msg_id = response.meta.msg_id; + + let mut locked_subs = self.subscribers.lock().unwrap(); + match locked_subs.entry(msg_id) { + Entry::Occupied(mut occ) => { + if let Err(_) = occ.get_mut().send(response) { + occ.remove(); + warn!("Subscriber to ID {} dropped their channel", msg_id); + } + } + Entry::Vacant(_) => { + info!("Ignoring uninteresting message ID {}", msg_id); + debug!("Contents: {:?}", response); + } + } + + Ok(()) + } +} + +impl<'a> BcSubscription<'a> { + pub fn send(&self, bc: Bc) -> Result<()> { + // TODO Use channel for writing, too + bc.serialize(&self.conn.connection)?; + Ok(()) + } +} + +/// Makes it difficult to avoid unsubscribing when you're finished +impl<'a> Drop for BcSubscription<'a> { + fn drop(&mut self) { + self.conn.subscribers.lock().unwrap().remove(&self.msg_id); + } +} From 5500ca4b286a9f5c2650630cc693b283821a42f3 Mon Sep 17 00:00:00 2001 From: George Hilliard Date: Thu, 7 May 2020 21:09:39 -0500 Subject: [PATCH 5/9] bc: Implement message subscription channels --- Cargo.lock | 83 ++++++++++++++++++++++++++++++++++- Cargo.toml | 2 +- src/bc_protocol.rs | 4 -- src/bc_protocol/connection.rs | 45 ++++++++++++------- src/main.rs | 8 ++-- 5 files changed, 115 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5930ce2..d4cfa6e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -102,6 +102,66 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41f21b581d2f0cb891554812435667bb9610d74feb1a4c6415bf09c28ff0381d" +[[package]] +name = "crossbeam" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69323bff1fb41c635347b8ead484a5ca6c3f11914d784170b158d8449ab07f8e" +dependencies = [ + "cfg-if", + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-epoch", + "crossbeam-queue", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-channel" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cced8691919c02aac3cb0a1bc2e9b73d89e832bf9a06fc579d4e71b68a2da061" +dependencies = [ + "crossbeam-utils", + "maybe-uninit", +] + +[[package]] +name = "crossbeam-deque" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f02af974daeee82218205558e51ec8768b48cf524bd01d550abe5573a608285" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", + "maybe-uninit", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" +dependencies = [ + "autocfg 1.0.0", + "cfg-if", + "crossbeam-utils", + "lazy_static", + "maybe-uninit", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-queue" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c695eeca1e7173472a32221542ae469b3e9aac3a4fc81f7696bcad82029493db" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + [[package]] name = "crossbeam-utils" version = "0.7.2" @@ -673,6 +733,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "maybe-uninit" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" + [[package]] name = "md5" version = "0.7.0" @@ -685,6 +751,15 @@ version = "2.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" +[[package]] +name = "memoffset" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4fc2c02a7e374099d4ee95a193111f72d2110197fe200272371758f6c3643d8" +dependencies = [ + "autocfg 1.0.0", +] + [[package]] name = "muldiv" version = "0.2.1" @@ -697,7 +772,7 @@ version = "0.1.0" dependencies = [ "backoff", "cookie-factory", - "crossbeam-utils", + "crossbeam", "env_logger", "err-derive", "glib", @@ -1020,6 +1095,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed3d612bc64430efeb3f7ee6ef26d590dce0c43249217bddc62112540c7941e1" +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + [[package]] name = "semver" version = "0.9.0" diff --git a/Cargo.toml b/Cargo.toml index 3bf7e0e..86beb93 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ edition = "2018" [dependencies] backoff = "0.1" cookie-factory = "0.3" -crossbeam-utils = "0.7" +crossbeam = "0.7" env_logger = "*" err-derive = "0.2" glib = "*" diff --git a/src/bc_protocol.rs b/src/bc_protocol.rs index 79332a8..1ef3247 100644 --- a/src/bc_protocol.rs +++ b/src/bc_protocol.rs @@ -6,7 +6,6 @@ use md5; use self::connection::BcConnection; use std::io::Write; use std::net::{SocketAddr, ToSocketAddrs}; -use std::time::Duration; use Md5Trunc::*; @@ -14,8 +13,6 @@ mod connection; pub struct BcCamera { address: SocketAddr, - - bc_context: BcContext, connection: Option, logged_in: bool, } @@ -70,7 +67,6 @@ impl BcCamera { address, connection: None, logged_in: false, - bc_context: BcContext::new(), }) } diff --git a/src/bc_protocol/connection.rs b/src/bc_protocol/connection.rs index 86ebee0..eac68bb 100644 --- a/src/bc_protocol/connection.rs +++ b/src/bc_protocol/connection.rs @@ -1,23 +1,23 @@ use crate::bc; -use crate::bc::{model::*}; +use crate::bc::model::*; use err_derive::Error; use log::*; use std::collections::BTreeMap; use std::collections::btree_map::Entry; -use std::sync::Mutex; +use std::sync::{Arc, Mutex}; use std::sync::mpsc::{channel, Sender, Receiver}; use std::net::{TcpStream, SocketAddr}; +/// A shareable connection to a camera. Handles serialization of messages. pub struct BcConnection { - connection: TcpStream, - context: BcContext, - subscribers: Mutex>>, + connection: Arc, + subscribers: Arc>>>, } pub struct BcSubscription<'a> { pub rx: Receiver, + pub msg_id: u32, conn: &'a BcConnection, - msg_id: u32, } type Result = std::result::Result; @@ -26,21 +26,30 @@ type Result = std::result::Result; pub enum Error { #[error(display="Communication error")] CommunicationError(#[error(source)] std::io::Error), + #[error(display="Deserialization error")] DeserializationError(#[error(source)] bc::de::Error), + #[error(display="Serialization error")] SerializationError(#[error(source)] bc::ser::Error), } impl BcConnection { pub fn new(addr: SocketAddr) -> Result { - let connection = TcpStream::connect(addr)?; + let bc_conn = BcConnection { + connection: Arc::new(TcpStream::connect(addr)?), + subscribers: Default::default(), + }; - Ok(BcConnection { - connection, - context: BcContext::new(), - subscribers: Mutex::new(BTreeMap::new()), - }) + let mut conn = bc_conn.connection.clone(); + let mut subs = bc_conn.subscribers.clone(); + + let rx_poller = std::thread::spawn(move || { + let mut context = BcContext::new(); + while let Ok(_) = BcConnection::poll(&mut context, &mut conn, &mut subs) {} + }); + + Ok(bc_conn) } pub fn subscribe(&self, msg_id: u32) -> Result { @@ -52,12 +61,16 @@ impl BcConnection { Ok(BcSubscription { rx, conn: self, msg_id }) } - pub fn poll(&mut self) -> Result<()> { + fn poll( + context: &mut BcContext, + connection: &mut Arc, + subscribers: &mut Arc>>> + ) -> Result<()> { // TODO if the connection hangs up, hang up on all subscribers and delete our TcpStream - let response = Bc::deserialize(&mut self.context, &self.connection)?; + let response = Bc::deserialize(context, &**connection)?; let msg_id = response.meta.msg_id; - let mut locked_subs = self.subscribers.lock().unwrap(); + let mut locked_subs = subscribers.lock().unwrap(); match locked_subs.entry(msg_id) { Entry::Occupied(mut occ) => { if let Err(_) = occ.get_mut().send(response) { @@ -78,7 +91,7 @@ impl BcConnection { impl<'a> BcSubscription<'a> { pub fn send(&self, bc: Bc) -> Result<()> { // TODO Use channel for writing, too - bc.serialize(&self.conn.connection)?; + bc.serialize(&*self.conn.connection)?; Ok(()) } } diff --git a/src/main.rs b/src/main.rs index f5bd2db..45aad04 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,6 @@ use backoff::{ExponentialBackoff, Operation}; use bc_protocol::BcCamera; use config::{Config, CameraConfig}; use cmdline::Opt; -use crossbeam_utils::thread; use err_derive::Error; use gst::RtspServer; use std::fs; @@ -34,7 +33,7 @@ fn main() -> Result<(), Error> { let rtsp = &RtspServer::new(); - thread::scope(|s| { + crossbeam::scope(|s| { for camera in config.cameras { s.spawn(move |_| { // TODO handle these errors @@ -70,13 +69,12 @@ fn camera_main(camera_config: &CameraConfig, output: &mut dyn Write) -> Result<( let mut camera = BcCamera::new_with_addr(camera_config.camera_addr)?; println!("{}: Connecting to camera at {}", camera_config.name, camera_config.camera_addr); - camera.connect()?; + camera.login(&camera_config.username, camera_config.password.as_deref())?; println!("{}: Connected to camera, starting video stream", camera_config.name); - camera.start_video(output)?; - Ok(()) + unreachable!() } From e26b7413c3ca4bed40200d8350fe67e34e4000e4 Mon Sep 17 00:00:00 2001 From: George Hilliard Date: Fri, 8 May 2020 00:47:39 -0500 Subject: [PATCH 6/9] Auth failures are permanent --- src/main.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 45aad04..8cd7a0b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -71,7 +71,12 @@ fn camera_main(camera_config: &CameraConfig, output: &mut dyn Write) -> Result<( println!("{}: Connecting to camera at {}", camera_config.name, camera_config.camera_addr); camera.connect()?; - camera.login(&camera_config.username, camera_config.password.as_deref())?; + camera.login(&camera_config.username, camera_config.password.as_deref()).map_err(|e| { + match e { + bc_protocol::Error::AuthFailed => backoff::Error::Permanent(e), + _ => backoff::Error::Transient(e) + } + })?; println!("{}: Connected to camera, starting video stream", camera_config.name); camera.start_video(output)?; From d653a84e5d532bbafe4f927febe79385ccbc5df9 Mon Sep 17 00:00:00 2001 From: George Hilliard Date: Fri, 8 May 2020 00:50:40 -0500 Subject: [PATCH 7/9] Actually use logging --- src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.rs b/src/main.rs index 8cd7a0b..3febf66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,6 +28,7 @@ pub enum Error { } fn main() -> Result<(), Error> { + env_logger::init(); let opt = Opt::from_args(); let config: Config = toml::from_str(&fs::read_to_string(opt.config)?)?; From 934caa20b089f518c96b06a74b3a8eb913fb6940 Mon Sep 17 00:00:00 2001 From: George Hilliard Date: Fri, 8 May 2020 01:13:01 -0500 Subject: [PATCH 8/9] Log errors in camera_loop() --- Cargo.toml | 2 +- src/bc_protocol/connection.rs | 8 ++++---- src/main.rs | 18 +++++++++++++----- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 86beb93..3ba9d35 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ gstreamer-app = "0.15" gstreamer-player = "0.15" gstreamer-rtsp = "0.15" gstreamer-rtsp-server = "0.15" -log = { version = "0.4", features = ["release_max_level_warn"] } +log = { version = "0.4", features = ["release_max_level_info"] } indoc = "0.3" md5 = "0.7" nom = "5.1" diff --git a/src/bc_protocol/connection.rs b/src/bc_protocol/connection.rs index eac68bb..ac2dcec 100644 --- a/src/bc_protocol/connection.rs +++ b/src/bc_protocol/connection.rs @@ -48,16 +48,14 @@ impl BcConnection { let mut context = BcContext::new(); while let Ok(_) = BcConnection::poll(&mut context, &mut conn, &mut subs) {} }); + // TODO join on drop Ok(bc_conn) } pub fn subscribe(&self, msg_id: u32) -> Result { let (tx, rx) = channel(); - { - let mut locked_subs = self.subscribers.lock().unwrap(); - locked_subs.insert(msg_id, tx); - } + self.subscribers.lock().unwrap().insert(msg_id, tx); Ok(BcSubscription { rx, conn: self, msg_id }) } @@ -90,6 +88,8 @@ impl BcConnection { impl<'a> BcSubscription<'a> { pub fn send(&self, bc: Bc) -> Result<()> { + assert!(bc.meta.msg_id == self.msg_id); + // TODO Use channel for writing, too bc.serialize(&*self.conn.connection)?; Ok(()) diff --git a/src/main.rs b/src/main.rs index 3febf66..e583313 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,12 +6,13 @@ mod config; mod cmdline; mod gst; -use backoff::{ExponentialBackoff, Operation}; +use backoff::{Error::*, ExponentialBackoff, Operation}; use bc_protocol::BcCamera; use config::{Config, CameraConfig}; use cmdline::Opt; use err_derive::Error; use gst::RtspServer; +use log::error; use std::fs; use std::time::Duration; use std::io::Write; @@ -60,8 +61,14 @@ fn camera_loop(camera_config: &CameraConfig, output: &mut dyn Write) -> Result<( }; op.retry(&mut backoff).map_err(|backoff_err| -> Error { match backoff_err { - backoff::Error::Transient(e) | - backoff::Error::Permanent(e) => e.into() + 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() + } } }) } @@ -72,10 +79,11 @@ fn camera_main(camera_config: &CameraConfig, output: &mut dyn Write) -> Result<( println!("{}: Connecting to camera at {}", camera_config.name, camera_config.camera_addr); camera.connect()?; + // Authentication failures are permanent; we retry everything else camera.login(&camera_config.username, camera_config.password.as_deref()).map_err(|e| { match e { - bc_protocol::Error::AuthFailed => backoff::Error::Permanent(e), - _ => backoff::Error::Transient(e) + bc_protocol::Error::AuthFailed => Permanent(e), + _ => Transient(e) } })?; From 2685e1fe43fac9385f355790bdea65b57b14393f Mon Sep 17 00:00:00 2001 From: George Hilliard Date: Fri, 8 May 2020 01:17:47 -0500 Subject: [PATCH 9/9] Clean imports slightly --- Cargo.lock | 67 ------------------------------------------------------ Cargo.toml | 1 - 2 files changed, 68 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d4cfa6e..402c425 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -460,37 +460,6 @@ dependencies = [ "pkg-config", ] -[[package]] -name = "gstreamer-player" -version = "0.15.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11840a08baf63d5e2b4c94cc25c023cbfccb39f02e920c75b0550b975898be3b" -dependencies = [ - "bitflags", - "glib", - "glib-sys", - "gobject-sys", - "gstreamer", - "gstreamer-player-sys", - "gstreamer-sys", - "gstreamer-video", - "libc", -] - -[[package]] -name = "gstreamer-player-sys" -version = "0.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cc38a730aed3442cad1c8df589b4abe856d14671058df7099a5a030fd61413c" -dependencies = [ - "glib-sys", - "gobject-sys", - "gstreamer-sys", - "gstreamer-video-sys", - "libc", - "pkg-config", -] - [[package]] name = "gstreamer-rtsp" version = "0.15.0" @@ -607,41 +576,6 @@ dependencies = [ "pkg-config", ] -[[package]] -name = "gstreamer-video" -version = "0.15.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bb63938bcd2e33285a6f195db8d719f3a13f568870d5bf168e2e3df18c1ea05" -dependencies = [ - "bitflags", - "futures-channel", - "futures-util", - "glib", - "glib-sys", - "gobject-sys", - "gstreamer", - "gstreamer-base", - "gstreamer-base-sys", - "gstreamer-sys", - "gstreamer-video-sys", - "lazy_static", - "libc", -] - -[[package]] -name = "gstreamer-video-sys" -version = "0.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "615f4298f842f4b4581606e13cf52e1710e2130d989bb99161a5665aa3ccb7cc" -dependencies = [ - "glib-sys", - "gobject-sys", - "gstreamer-base-sys", - "gstreamer-sys", - "libc", - "pkg-config", -] - [[package]] name = "heck" version = "0.3.1" @@ -778,7 +712,6 @@ dependencies = [ "glib", "gstreamer", "gstreamer-app", - "gstreamer-player", "gstreamer-rtsp", "gstreamer-rtsp-server", "indoc", diff --git a/Cargo.toml b/Cargo.toml index 3ba9d35..0efebd5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,6 @@ err-derive = "0.2" glib = "*" gstreamer = "0.15" gstreamer-app = "0.15" -gstreamer-player = "0.15" gstreamer-rtsp = "0.15" gstreamer-rtsp-server = "0.15" log = { version = "0.4", features = ["release_max_level_info"] }