Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2484b8753b | ||
|
|
9730114172 | ||
|
|
c45a5033b9 | ||
|
|
b2e43c605e | ||
|
|
e3b057f915 | ||
|
|
2f557f777d |
No files matched your search
Generated
+421
-482
File diff suppressed because it is too large.
Load diff
+2
-2
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "neolink"
|
||||
description = "A standards-compliant bridge to Reolink IP cameras"
|
||||
version = "0.5.15"
|
||||
version = "0.5.16"
|
||||
authors = ["George Hilliard <thirtythreeforty@gmail.com>", "Andrew King <sheepchaan@gmail.com>"]
|
||||
edition = "2018"
|
||||
license = "AGPL-3.0-or-later"
|
||||
@@ -29,7 +29,7 @@ is_sorted = "0.1.1"
|
||||
itertools = "0.11.0"
|
||||
lazy_static = "1.4.0"
|
||||
log = { version = "0.4.17", features = [ "release_max_level_debug" ] }
|
||||
neolink_core = { path = "crates/core", version = "0.5.15" }
|
||||
neolink_core = { path = "crates/core", version = "0.5.16" }
|
||||
regex = "1.7.3"
|
||||
rumqttc = "0.22.0"
|
||||
serde = { version = "1.0.160", features = ["derive"] }
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "neolink_core"
|
||||
description = "Core services and structure for Reolink IP cameras"
|
||||
version = "0.5.15"
|
||||
version = "0.5.16"
|
||||
authors = ["George Hilliard <thirtythreeforty@gmail.com>", "Andrew King <sheepchaan@gmail.com>"]
|
||||
edition = "2018"
|
||||
license = "AGPL-3.0-or-later"
|
||||
|
||||
+104
-63
@@ -185,70 +185,101 @@ impl BcCamera {
|
||||
DiscoveryMethods::Debug => (false, false, false, true),
|
||||
};
|
||||
|
||||
let reg_result = if allow_remote || allow_map || allow_relay {
|
||||
Some(discovery.get_registration(uid).await?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let res = tokio::select! {
|
||||
v = async {
|
||||
let uid_local = uid.clone();
|
||||
info!("{}: Trying local discovery", options.name);
|
||||
let result = discovery.local(&uid_local, Some(sockets)).await;
|
||||
match result {
|
||||
Ok(disc) => {
|
||||
info!(
|
||||
"{}: Local discovery success {} at {}",
|
||||
options.name,
|
||||
uid_local,
|
||||
disc.get_addr()
|
||||
);
|
||||
Ok(CameraLocation::Udp(disc))
|
||||
},
|
||||
Err(e) => Err(e)
|
||||
}
|
||||
}, if allow_local => v,
|
||||
v = async {
|
||||
let reg_result;
|
||||
// Registration is looped as it seems that reolink
|
||||
// only updates the registration lazily when someone attempts
|
||||
// to connect. The first few connects fails until the server data
|
||||
// is updated
|
||||
//
|
||||
// We loop infinitly and allow the caller to timeout at the
|
||||
// interval they desire
|
||||
loop {
|
||||
tokio::task::yield_now().await;
|
||||
if let Ok(result) = discovery.get_registration(uid).await {
|
||||
reg_result = result;
|
||||
break;
|
||||
}
|
||||
log::debug!("Registration failed. Retrying");
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
|
||||
};
|
||||
tokio::select! {
|
||||
v = async {
|
||||
let uid_remote = uid.clone();
|
||||
info!("{}: Trying remote discovery", options.name);
|
||||
let result = discovery
|
||||
.remote(&uid_remote, ®_result)
|
||||
.await;
|
||||
match result {
|
||||
Ok(disc) => {
|
||||
info!(
|
||||
"{}: Remote discovery success {} at {}",
|
||||
options.name,
|
||||
uid_remote,
|
||||
disc.get_addr()
|
||||
);
|
||||
Ok(CameraLocation::Udp(disc))
|
||||
},
|
||||
Err(e) => Err(e)
|
||||
}
|
||||
}, if allow_remote => v,
|
||||
v = async {
|
||||
let uid_map = uid.clone();
|
||||
info!("{}: Trying map discovery", options.name);
|
||||
let result = discovery.map(®_result).await;
|
||||
match result {
|
||||
Ok(disc) => {
|
||||
info!(
|
||||
"{}: Map success {} at {}",
|
||||
options.name,
|
||||
uid_map,
|
||||
disc.get_addr()
|
||||
);
|
||||
Ok(CameraLocation::Udp(disc))
|
||||
},
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}, if allow_map => v,
|
||||
v = async {
|
||||
let uid_relay = uid.clone();
|
||||
info!("{}: Trying relay discovery", options.name);
|
||||
let result = discovery.relay(®_result).await;
|
||||
match result {
|
||||
Ok(disc) => {
|
||||
info!(
|
||||
"{}: Relay success {} at {}",
|
||||
options.name,
|
||||
uid_relay,
|
||||
disc.get_addr()
|
||||
);
|
||||
Ok(CameraLocation::Udp(disc))
|
||||
},
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}, if allow_relay => v,
|
||||
}
|
||||
}, if allow_remote || allow_map || allow_relay => v,
|
||||
}?;
|
||||
|
||||
if allow_local {
|
||||
let uid_local = uid.clone();
|
||||
info!("{}: Trying local discovery", options.name);
|
||||
let result = discovery.local(&uid_local, Some(sockets)).await;
|
||||
if let Ok(disc) = result {
|
||||
info!(
|
||||
"{}: Local discovery success {} at {}",
|
||||
options.name,
|
||||
uid_local,
|
||||
disc.get_addr()
|
||||
);
|
||||
return Ok(CameraLocation::Udp(disc));
|
||||
}
|
||||
}
|
||||
if allow_remote {
|
||||
let uid_remote = uid.clone();
|
||||
info!("{}: Trying remote discovery", options.name);
|
||||
let result = discovery
|
||||
.remote(&uid_remote, reg_result.as_ref().unwrap())
|
||||
.await;
|
||||
if let Ok(disc) = result {
|
||||
info!(
|
||||
"{}: Remote discovery success {} at {}",
|
||||
options.name,
|
||||
uid_remote,
|
||||
disc.get_addr()
|
||||
);
|
||||
return Ok(CameraLocation::Udp(disc));
|
||||
}
|
||||
}
|
||||
if allow_map {
|
||||
let uid_map = uid.clone();
|
||||
info!("{}: Trying map discovery", options.name);
|
||||
let result = discovery.map(reg_result.as_ref().unwrap()).await;
|
||||
if let Ok(disc) = result {
|
||||
info!(
|
||||
"{}: Map success {} at {}",
|
||||
options.name,
|
||||
uid_map,
|
||||
disc.get_addr()
|
||||
);
|
||||
return Ok(CameraLocation::Udp(disc));
|
||||
}
|
||||
}
|
||||
if allow_relay {
|
||||
let uid_relay = uid.clone();
|
||||
info!("{}: Trying relay discovery", options.name);
|
||||
let result = discovery.relay(reg_result.as_ref().unwrap()).await;
|
||||
if let Ok(disc) = result {
|
||||
info!(
|
||||
"{}: Relay success {} at {}",
|
||||
options.name,
|
||||
uid_relay,
|
||||
disc.get_addr()
|
||||
);
|
||||
return Ok(CameraLocation::Udp(disc));
|
||||
}
|
||||
}
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
info!("{}: Discovery failed", options.name);
|
||||
@@ -365,6 +396,16 @@ impl BcCamera {
|
||||
pub async fn join(&self) -> Result<()> {
|
||||
self.connection.join().await
|
||||
}
|
||||
|
||||
/// Disconnect from the camera. This is done by dropping the reciever
|
||||
/// so further communications are not possible.
|
||||
pub async fn disconnect(self) -> Result<()> {
|
||||
let connection = Arc::try_unwrap(self.connection)
|
||||
.map_err(|_| Error::Other("Connection still active"))?;
|
||||
connection.disconnect().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// The Baichuan library has a very peculiar behavior where it always zeros the last byte. I
|
||||
|
||||
@@ -147,6 +147,15 @@ impl BcConnection {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn disconnect(mut self) -> Result<()> {
|
||||
self.poll_commander.send(PollCommand::Disconnect).await?;
|
||||
drop(self.sink);
|
||||
drop(self.poll_commander);
|
||||
let locked_threads = self.rx_thread.get_mut();
|
||||
while locked_threads.join_next().await.is_some() {}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
enum PollCommand {
|
||||
@@ -154,6 +163,7 @@ enum PollCommand {
|
||||
AddHandler(u32, Arc<MsgHandler>),
|
||||
RemoveHandler(u32),
|
||||
AddSubscriber(u32, Option<u16>, Sender<Result<Bc>>),
|
||||
Disconnect,
|
||||
}
|
||||
|
||||
struct Poller {
|
||||
@@ -292,6 +302,9 @@ impl Poller {
|
||||
}
|
||||
};
|
||||
}
|
||||
PollCommand::Disconnect => {
|
||||
return Err(Error::DroppedConnection);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -445,9 +445,18 @@ impl Discoverer {
|
||||
}),
|
||||
..
|
||||
},
|
||||
} if (dev.as_ref().map(|d| !d.ip.is_empty()).unwrap_or(false)
|
||||
|| dmap.as_ref().map(|d| !d.ip.is_empty()).unwrap_or(false)
|
||||
|| relay.as_ref().map(|d| !d.ip.is_empty()).unwrap_or(false))
|
||||
} if (dev
|
||||
.as_ref()
|
||||
.map(|d| !d.ip.is_empty() && d.port > 0)
|
||||
.unwrap_or(false)
|
||||
|| dmap
|
||||
.as_ref()
|
||||
.map(|d| !d.ip.is_empty() && d.port > 0)
|
||||
.unwrap_or(false)
|
||||
|| relay
|
||||
.as_ref()
|
||||
.map(|d| !d.ip.is_empty() && d.port > 0)
|
||||
.unwrap_or(false))
|
||||
&& rsp != -1 =>
|
||||
{
|
||||
Some(Ok((sid, dev, dmap, relay)))
|
||||
@@ -466,9 +475,18 @@ impl Discoverer {
|
||||
}),
|
||||
..
|
||||
},
|
||||
} if (dev.as_ref().map(|d| !d.ip.is_empty()).unwrap_or(false)
|
||||
|| dmap.as_ref().map(|d| !d.ip.is_empty()).unwrap_or(false)
|
||||
|| relay.as_ref().map(|d| !d.ip.is_empty()).unwrap_or(false))
|
||||
} if (dev
|
||||
.as_ref()
|
||||
.map(|d| !d.ip.is_empty() && d.port > 0)
|
||||
.unwrap_or(false)
|
||||
|| dmap
|
||||
.as_ref()
|
||||
.map(|d| !d.ip.is_empty() && d.port > 0)
|
||||
.unwrap_or(false)
|
||||
|| relay
|
||||
.as_ref()
|
||||
.map(|d| !d.ip.is_empty() && d.port > 0)
|
||||
.unwrap_or(false))
|
||||
&& rsp == -1 =>
|
||||
{
|
||||
Some(Err(Error::RegisterError))
|
||||
|
||||
@@ -4,12 +4,9 @@ use crate::{
|
||||
bcmedia::model::*,
|
||||
};
|
||||
use futures::stream::StreamExt;
|
||||
use std::sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
};
|
||||
use tokio::sync::mpsc::{channel, Receiver};
|
||||
use tokio::task::{self, JoinHandle};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
/// The stream names supported by BC
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||
@@ -33,7 +30,7 @@ pub enum StreamKind {
|
||||
pub struct StreamData {
|
||||
handle: Option<JoinHandle<Result<()>>>,
|
||||
rx: Receiver<Result<BcMedia>>,
|
||||
abort_handle: Arc<AtomicBool>,
|
||||
abort_handle: CancellationToken,
|
||||
}
|
||||
|
||||
impl StreamData {
|
||||
@@ -42,12 +39,12 @@ impl StreamData {
|
||||
pub async fn get_data(&mut self) -> Result<Result<BcMedia>> {
|
||||
if let Some(handle) = self.handle.as_mut() {
|
||||
if handle.is_finished() {
|
||||
self.abort_handle.store(true, Ordering::Relaxed);
|
||||
self.abort_handle.cancel();
|
||||
handle.await??;
|
||||
return Err(Error::DroppedConnection);
|
||||
}
|
||||
} else {
|
||||
self.abort_handle.store(true, Ordering::Relaxed);
|
||||
self.abort_handle.cancel();
|
||||
return Err(Error::DroppedConnection);
|
||||
}
|
||||
// debug!("StreamData: Get");
|
||||
@@ -58,16 +55,26 @@ impl StreamData {
|
||||
}
|
||||
None => {
|
||||
// debug!("StreamData: Drop");
|
||||
self.abort_handle.store(true, Ordering::Relaxed);
|
||||
self.abort_handle.cancel();
|
||||
Err(Error::DroppedConnection)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempts to gracefully shutdown this will cancel the background task and send
|
||||
/// the Stop command to the camera
|
||||
pub async fn shutdown(&mut self) -> Result<()> {
|
||||
self.abort_handle.cancel();
|
||||
if let Some(handle) = self.handle.take() {
|
||||
let _ = handle.await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for StreamData {
|
||||
fn drop(&mut self) {
|
||||
self.abort_handle.store(true, Ordering::Relaxed);
|
||||
self.abort_handle.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,7 +107,7 @@ impl BcCamera {
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
|
||||
let abort_handle = Arc::new(AtomicBool::new(false));
|
||||
let abort_handle = CancellationToken::new();
|
||||
let abort_handle_thread = abort_handle.clone();
|
||||
|
||||
if buffer_size == 0 {
|
||||
@@ -186,20 +193,20 @@ impl BcCamera {
|
||||
{
|
||||
let mut media_sub = sub_video.bcmedia_stream(strict);
|
||||
|
||||
while !abort_handle_thread.load(Ordering::Relaxed) {
|
||||
// debug!("Stream: Get");
|
||||
if let Some(bc_media) = media_sub.next().await {
|
||||
// debug!("Stream: Got");
|
||||
// We now have a complete interesting packet. Send it to on the callback
|
||||
// debug!("Stream: Send");
|
||||
if tx.send(bc_media).await.is_err() {
|
||||
// debug!("Stream: Dropped");
|
||||
break; // Connection dropped
|
||||
tokio::select! {
|
||||
_ = abort_handle_thread.cancelled() => {},
|
||||
_ = async {
|
||||
while let Some(bc_media) = media_sub.next().await {
|
||||
// debug!("Stream: Got");
|
||||
// We now have a complete interesting packet. Send it to on the callback
|
||||
// debug!("Stream: Send");
|
||||
if tx.send(bc_media).await.is_err() {
|
||||
// debug!("Stream: Dropped");
|
||||
break; // Connection dropped
|
||||
}
|
||||
// debug!("Stream: Sent");
|
||||
}
|
||||
// debug!("Stream: Sent");
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+5
-1
@@ -20,7 +20,6 @@ enum GstControl {
|
||||
|
||||
pub(super) struct GstSender {
|
||||
sender: Sender<GstControl>,
|
||||
#[allow(dead_code)] // It is used for its automatic drop
|
||||
set: JoinSet<Result<()>>,
|
||||
finished: sync::oneshot::Receiver<Result<()>>,
|
||||
}
|
||||
@@ -49,6 +48,11 @@ impl GstSender {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn join(mut self) -> Result<()> {
|
||||
while self.set.join_next().await.is_some() {}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn from_input<T: AsRef<Path>>(
|
||||
|
||||
@@ -81,6 +81,9 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> {
|
||||
}
|
||||
debug!("Sending EOS");
|
||||
let _ = sender.eos().await; // Ignore return because if pipeline is finished this will error
|
||||
let _ = sender.join().await;
|
||||
|
||||
let _ = stream_data.shutdown().await;
|
||||
} else {
|
||||
// Simply use the snap command
|
||||
debug!("Using the snap command");
|
||||
@@ -90,5 +93,8 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> {
|
||||
buffer.write_all(jpeg_data.as_slice()).await?;
|
||||
}
|
||||
|
||||
let _ = camera.logout().await;
|
||||
camera.disconnect().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user