Merge branch 'multithread'

This commit is contained in:
George Hilliard committed 2020-05-08 01:17:59 -05:00
commit ed110611bd
7 files changed
+462 -149

No files matched your search

+1
View File
@@ -0,0 +1 @@
Cargo.lock binary
Generated
+239 -72
View File
@@ -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,19 +87,88 @@ 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"
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"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8"
dependencies = [
"autocfg",
"autocfg 1.0.0",
"cfg-if",
"lazy_static",
]
@@ -115,6 +200,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"
@@ -369,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"
@@ -516,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"
@@ -601,6 +626,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"
@@ -636,6 +667,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"
@@ -648,6 +685,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"
@@ -658,14 +704,14 @@ checksum = "0419348c027fa7be448d2ae7ea0e4e04c2334c31dc4e74ab29f00a2a7ca69204"
name = "neolink"
version = "0.1.0"
dependencies = [
"backoff",
"cookie-factory",
"crossbeam-utils",
"crossbeam",
"env_logger",
"err-derive",
"glib",
"gstreamer",
"gstreamer-app",
"gstreamer-player",
"gstreamer-rtsp",
"gstreamer-rtsp-server",
"indoc",
@@ -703,7 +749,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 +759,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 +770,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 +869,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"
@@ -867,6 +1028,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"
+3 -3
View File
@@ -7,17 +7,17 @@ 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"
crossbeam = "0.7"
env_logger = "*"
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_warn"] }
log = { version = "0.4", features = ["release_max_level_info"] }
indoc = "0.3"
md5 = "0.7"
nom = "5.1"
+8
View File
@@ -68,6 +68,14 @@ pub struct BcContext {
pub(super) in_bin_mode: HashSet<u32>,
}
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 {
+68 -66
View File
@@ -1,41 +1,56 @@
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 Md5Trunc::*;
mod connection;
pub struct BcCamera {
address: SocketAddr,
bc_context: BcContext,
connection: Option<TcpStream>,
connection: Option<BcConnection>,
logged_in: bool,
}
type Result<T> = std::result::Result<T, Error>;
#[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<T> = std::result::Result<T, Error>;
#[error(display="Other error")]
Other(&'static str),
}
impl Drop for BcCamera {
fn drop(&mut self) {
@@ -46,18 +61,17 @@ impl Drop for BcCamera {
impl BcCamera {
pub fn new_with_addr<T: ToSocketAddrs>(hostname: T) -> Result<Self> {
let address = hostname.to_socket_addrs()?.next()
.ok_or(Error::OtherError("Address resolution failed"))?;
.ok_or(Error::Other("Address resolution failed"))?;
Ok(Self {
address,
connection: None,
logged_in: false,
bc_context: BcContext::new(),
})
}
pub fn connect(&mut self) -> Result<()> {
self.connection = Some(TcpStream::connect(self.address)?);
self.connection = Some(BcConnection::new(self.address)?);
Ok(())
}
@@ -70,6 +84,7 @@ impl BcCamera {
pub fn login(&mut self, username: &str, password: Option<&str>) -> Result<DeviceInfo> {
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 +111,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 +122,6 @@ impl BcCamera {
nonce = encryption.nonce;
}
_ => return Err(Error::UnintelligibleReply {
request: legacy_login,
reply: legacy_reply,
why: "Expected an Encryption message back"
})
@@ -126,31 +140,27 @@ 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)?;
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 {
@@ -166,7 +176,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"
})
@@ -183,48 +192,41 @@ 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 {
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)?;
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"))
}
}
+104
View File
@@ -0,0 +1,104 @@
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::{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: Arc<TcpStream>,
subscribers: Arc<Mutex<BTreeMap<u32, Sender<Bc>>>>,
}
pub struct BcSubscription<'a> {
pub rx: Receiver<Bc>,
pub msg_id: u32,
conn: &'a BcConnection,
}
type Result<T> = std::result::Result<T, Error>;
#[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<BcConnection> {
let bc_conn = BcConnection {
connection: Arc::new(TcpStream::connect(addr)?),
subscribers: Default::default(),
};
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) {}
});
// TODO join on drop
Ok(bc_conn)
}
pub fn subscribe(&self, msg_id: u32) -> Result<BcSubscription> {
let (tx, rx) = channel();
self.subscribers.lock().unwrap().insert(msg_id, tx);
Ok(BcSubscription { rx, conn: self, msg_id })
}
fn poll(
context: &mut BcContext,
connection: &mut Arc<TcpStream>,
subscribers: &mut Arc<Mutex<BTreeMap<u32, Sender<Bc>>>>
) -> Result<()> {
// TODO if the connection hangs up, hang up on all subscribers and delete our TcpStream
let response = Bc::deserialize(context, &**connection)?;
let msg_id = response.meta.msg_id;
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) {
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<()> {
assert!(bc.meta.msg_id == self.msg_id);
// 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);
}
}
+39 -8
View File
@@ -6,13 +6,15 @@ mod config;
mod cmdline;
mod gst;
use backoff::{Error::*, 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 log::error;
use std::fs;
use std::time::Duration;
use std::io::Write;
use structopt::StructOpt;
@@ -27,17 +29,18 @@ 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)?)?;
let rtsp = &RtspServer::new();
thread::scope(|s| {
crossbeam::scope(|s| {
for camera in config.cameras {
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,17 +50,45 @@ 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 {
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()
}
}
})
}
fn camera_main(camera_config: &CameraConfig, output: &mut dyn Write) -> Result<(), backoff::Error<bc_protocol::Error>> {
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())?;
// 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 => Permanent(e),
_ => Transient(e)
}
})?;
println!("{}: Connected to camera, starting video stream", camera_config.name);
camera.start_video(output)?;
Ok(())
unreachable!()
}