Compare commits

...
18 Commits
Author SHA1 Message Date
QuantumEntangledAndy 4f2a010e33 0.5.3rc 2023-02-27 09:21:09 +07:00
QuantumEntangledAndy f1c35a1809 Prepare for merge 2023-02-27 09:14:07 +07:00
QuantumEntangledAndy a2bec57251 fmt 2023-02-27 09:14:07 +07:00
QuantumEntangledAndy 8ead7ad2fc Keep sending data until jpeg is made 2023-02-27 09:14:07 +07:00
QuantumEntangledAndy bd2a3b4ebc Add the file too 2023-02-27 09:14:07 +07:00
QuantumEntangledAndy c37cedf183 Added the unhandler 2023-02-27 09:14:07 +07:00
QuantumEntangledAndy 1fe1ce5046 Setup a mechanism to respond to camera message, and use it for keepalive 2023-02-27 09:14:07 +07:00
QuantumEntangledAndy 7ebe73b226 Added encryption configuration and handle empty ack packets 2023-02-27 09:14:07 +07:00
QuantumEntangledAndy 7dae880bb2 Rework rensend wait logic in discovery 2023-02-27 09:14:07 +07:00
QuantumEntangledAndy 438d2a71e1 Retry send super bump 2023-02-27 09:14:07 +07:00
QuantumEntangledAndy 0f76622b17 Fix merge issues 2023-02-27 09:14:07 +07:00
QuantumEntangledAndy 9945b02fd9 Macos store file cleanup 2023-02-27 09:14:07 +07:00
QuantumEntangledAndy f514733706 Log cleanup 2023-02-27 09:14:07 +07:00
QuantumEntangledAndy bd9da2620b Added still image command 2023-02-27 09:14:07 +07:00
QuantumEntangledAndy 6d0d290644 Swap invalid chatacter in dockerhub step 2023-02-27 09:14:07 +07:00
QuantumEntangledAndy a3aa3c4556 Handle aes encrypt during deser too 2023-02-24 07:25:37 +07:00
QuantumEntangledAndy f253100ea7 Rmove DS_Store 2023-02-24 07:25:37 +07:00
QuantumEntangledAndy ad5d09045c Fix issue with BcPayloadStream not waking for next poll 2023-02-23 21:01:27 +07:00
34 changed files with 618 additions and 134 deletions

No files matched your search

Vendored
BIN
View File
Binary file not shown.
+1 -1
View File
@@ -183,7 +183,7 @@ jobs:
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
id: tags
run: |
branch="${GITHUB_REF#refs/heads/}"
branch="$(echo "${GITHUB_REF#refs/heads/}" | sed -E 's|[/]|-|g')"
tags="${REPO_NAME}:${branch}"
if [ "${branch}" == "master" ]; then
tags="${tags},${REPO_NAME}:latest"
+6
View File
@@ -14,3 +14,9 @@
# Committing IntelliJ project files is a violation of intergalactic law
.idea/
# MacOS
.DS_Store
._.DS_Store
**/.DS_Store
**/._.DS_Store
Generated
+2 -2
View File
@@ -1120,7 +1120,7 @@ dependencies = [
[[package]]
name = "neolink"
version = "0.5.2"
version = "0.5.3"
dependencies = [
"aes",
"anyhow",
@@ -1160,7 +1160,7 @@ dependencies = [
[[package]]
name = "neolink_core"
version = "0.5.2"
version = "0.5.3"
dependencies = [
"aes",
"assert_matches",
+2 -2
View File
@@ -1,7 +1,7 @@
[package]
name = "neolink"
description = "A standards-compliant bridge to Reolink IP cameras"
version = "0.5.2"
version = "0.5.3"
authors = ["George Hilliard <thirtythreeforty@gmail.com>", "Andrew King <sheepchaan@gmail.com>"]
edition = "2018"
@@ -11,7 +11,7 @@ members = [
]
[dependencies]
neolink_core = { path = "crates/core", version = "0.5.2" }
neolink_core = { path = "crates/core", version = "0.5.3" }
aes = "0.6"
cfb-mode = "0.6"
cookie-factory = "0.3"
+15
View File
@@ -26,6 +26,7 @@ with additional features not yet in upstream master.
- MQTT
- Motion Detection
- Paused Streams (when no rtsp client or no motion detected)
- Save a still image to disk
**Minor Features**:
@@ -167,6 +168,20 @@ in multiple architectures. The latest tag tracks master while each branch gets i
docker pull quantumentangledandy/neolink
```
### Image
You can write an image from the stream to disk using:
```bash
neolink image --config=config.toml --file-path=filepath CameraName
```
Where filepath is the path to save the image to and CameraName is the name of the camera
from the config to save the image from.
File is always jpeg and the extension given in filepath will be added or changed to reflect this.
## License
Neolink is free software, released under the GNU Affero General Public License
+1 -1
View File
@@ -1,7 +1,7 @@
[package]
name = "neolink_core"
description = "Core services and structure for Reolink IP cameras"
version = "0.5.2"
version = "0.5.3"
authors = ["George Hilliard <thirtythreeforty@gmail.com>", "Andrew King <sheepchaan@gmail.com>"]
edition = "2018"
+9 -1
View File
@@ -27,7 +27,15 @@ impl Encoder<Bc> for BcCodex {
fn encode(&mut self, item: Bc, dst: &mut BytesMut) -> Result<()> {
// let context = self.context.read().unwrap();
let buf: Vec<u8> = Default::default();
let buf = item.serialize(buf, self.context.get_encrypted())?;
let enc_protocol: EncryptionProtocol = match self.context.get_encrypted() {
EncryptionProtocol::Aes(_) if item.meta.msg_id == 1 => {
// During login the encyption protocol cannot go higher than BCEncrypt
// even if we support AES. (BUt it can go lower i.e. None)
EncryptionProtocol::BCEncrypt
}
n => *n,
};
let buf = item.serialize(buf, &enc_protocol)?;
dst.reserve(buf.len());
dst.extend_from_slice(buf.as_slice());
Ok(())
+7 -1
View File
@@ -160,7 +160,13 @@ fn bc_modern_msg<'a>(
response_code,
..
} if (response_code & 0xff) == 0x02 => EncryptionProtocol::BCEncrypt, // This is AES but the first packet with the NONCE is BCEcrypt, since the NONCE in this packet is required to build the AES key
_ => context.get_encrypted().clone(),
BcHeader { msg_id: 1, .. }
if matches!(context.get_encrypted(), EncryptionProtocol::Aes(_)) =>
{
// Maximum encryption level is BCEncrypt during login... Not sure why Reolink did it like that
EncryptionProtocol::BCEncrypt
}
_ => *context.get_encrypted(),
};
let processed_payload_buf =
+1 -1
View File
@@ -169,7 +169,7 @@ pub(super) struct BcSendInfo {
/// These are the encyption modes supported by the camera
///
/// The mode is negotiated during login
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub enum EncryptionProtocol {
/// Older camera use no encryption
Unencrypted,
+3
View File
@@ -8,6 +8,7 @@ use Md5Trunc::*;
mod connection;
mod errors;
mod keepalive;
mod ledstate;
mod login;
mod logout;
@@ -25,6 +26,7 @@ mod version;
pub(crate) use connection::*;
pub use errors::Error;
pub use ledstate::LightState;
pub use login::MaxEncryption;
pub use motion::{MotionData, MotionStatus};
pub use pirstate::PirState;
pub use ptz::Direction;
@@ -297,6 +299,7 @@ impl BcCamera {
logged_in: AtomicBool::new(false),
credentials: Credentials::new(username, passwd),
};
me.keepalive().await?;
Ok(me)
}
Binary file not shown.
@@ -11,7 +11,15 @@ use tokio::sync::Mutex;
use tokio::{sync::RwLock, task::JoinSet};
type Subscriber = Arc<RwLock<BTreeMap<u16, Sender<Bc>>>>;
type MsgHandler = Box<dyn Fn(&Bc) -> Option<Bc> + Send + Sync>;
#[derive(Default)]
struct Subscriber {
/// Subscribers based on their Num
num: RwLock<BTreeMap<u16, Sender<Bc>>>,
/// Subscribers based on their ID
id: RwLock<BTreeMap<u32, MsgHandler>>,
}
pub(crate) type BcConnSink = Box<dyn Sink<Bc, Error = Error> + Send + Sync + Unpin>;
pub(crate) type BcConnSource = Box<dyn Stream<Item = Result<Bc>> + Send + Sync + Unpin>;
@@ -22,19 +30,20 @@ pub(crate) type BcConnSource = Box<dyn Stream<Item = Result<Bc>> + Send + Sync +
/// There can be only one subscriber per kind of message at a time.
pub struct BcConnection {
sink: Arc<Mutex<BcConnSink>>,
subscribers: Subscriber,
subscribers: Arc<Subscriber>,
#[allow(dead_code)] // Not dead we just need to hold a reference to keep it alive
rx_thread: JoinSet<()>,
}
impl BcConnection {
pub async fn new(sink: BcConnSink, mut source: BcConnSource) -> Result<BcConnection> {
let subscribers: Subscriber = Default::default();
let subscribers: Arc<Subscriber> = Default::default();
let sink = Arc::new(Mutex::new(sink));
let subs = subscribers.clone();
let mut rx_thread = JoinSet::new();
let sink_thread = sink.clone();
rx_thread.spawn(async move {
loop {
let packet = source.next().await;
@@ -47,7 +56,7 @@ impl BcConnection {
None => continue,
};
if let Err(e) = Self::poll(bc, &subs).await {
if let Err(e) = Self::poll(bc, &subs, &sink_thread).await {
error!("Subscription error: {:?}", e);
break;
}
@@ -55,7 +64,7 @@ impl BcConnection {
});
Ok(BcConnection {
sink: Arc::new(Mutex::new(sink)),
sink,
subscribers,
rx_thread,
})
@@ -70,44 +79,79 @@ impl BcConnection {
pub async fn subscribe(&self, msg_num: u16) -> Result<BcSubscription> {
let (tx, rx) = channel(100);
match self.subscribers.write().await.entry(msg_num) {
Entry::Vacant(vac_entry) => vac_entry.insert(tx),
Entry::Occupied(_) => return Err(Error::SimultaneousSubscription { msg_num }),
match self.subscribers.num.write().await.entry(msg_num) {
Entry::Vacant(vac_entry) => {
vac_entry.insert(tx);
}
Entry::Occupied(mut occ_entry) => {
if occ_entry.get().is_closed() {
occ_entry.insert(tx);
} else {
return Err(Error::SimultaneousSubscription { msg_num });
}
}
};
Ok(BcSubscription::new(rx, msg_num, self))
Ok(BcSubscription::new(rx, msg_num as u32, self))
}
pub fn unsubscribe(&self, msg_num: u16) -> Result<()> {
let subs = self.subscribers.clone();
tokio::task::spawn(async move {
subs.write().await.remove(&msg_num);
});
/// Some messages are initiated by the camera. This creates a handler for them
/// It requires a closure that will be used to handle the message
/// and return either None or Some(Bc) reply
pub async fn handle_msg<T>(&self, msg_id: u32, handler: T) -> Result<()>
where
T: Fn(&Bc) -> Option<Bc> + Send + Sync + 'static,
{
match self.subscribers.id.write().await.entry(msg_id) {
Entry::Vacant(vac_entry) => {
vac_entry.insert(Box::new(handler));
}
Entry::Occupied(_) => {
return Err(Error::SimultaneousSubscriptionId { msg_id });
}
};
Ok(())
}
async fn poll(response: Bc, subscribers: &Subscriber) -> Result<()> {
/// Stop a message handler created using [`handle_msg`]
#[allow(dead_code)] // Currently unused but added for future use
pub async fn unhandle_msg(&self, msg_id: u32) -> Result<()> {
self.subscribers.id.write().await.remove(&msg_id);
Ok(())
}
async fn poll(response: Bc, subscribers: &Subscriber, sink: &Mutex<BcConnSink>) -> Result<()> {
// Don't hold the lock during deserialization so we don't poison the subscribers mutex if
// something goes wrong
let msg_num = response.meta.msg_num;
let msg_id = response.meta.msg_id;
let mut remove_it = false;
match subscribers.read().await.get(&msg_num) {
Some(occ) => {
if occ.send(response).await.is_err() {
// Exceedingly unlikely, unless you mishandle the subscription object
warn!("Subscriber to ID {} dropped their channel", msg_id);
remove_it = true;
let mut remove_it_num = false;
match (
subscribers.id.read().await.get(&msg_id),
subscribers.num.read().await.get(&msg_num),
) {
(Some(occ), _) => {
if let Some(reply) = occ(&response) {
assert!(reply.meta.msg_num == response.meta.msg_num);
sink.lock().await.send(reply).await?;
}
}
None => {
debug!("Ignoring uninteresting message num {}", msg_id);
(None, Some(occ)) => {
trace!("occ.full: {}/{}", occ.capacity(), occ.max_capacity());
if occ.send(response).await.is_err() {
remove_it_num = true;
}
}
(None, None) => {
debug!(
"Ignoring uninteresting message id {} (number: {})",
msg_id, msg_num
);
trace!("Contents: {:?}", response);
}
}
if remove_it {
subscribers.write().await.remove(&msg_num);
if remove_it_num {
subscribers.num.write().await.remove(&msg_num);
}
Ok(())
@@ -12,7 +12,7 @@ use tokio_util::compat::{Compat, FuturesAsyncReadCompatExt};
pub struct BcSubscription<'a> {
rx: ReceiverStream<Bc>,
msg_num: u16,
msg_num: u32,
conn: &'a BcConnection,
}
@@ -50,7 +50,6 @@ impl<'a> Stream for BcPayloadStream<'a> {
) -> Poll<Option<IoResult<Vec<u8>>>> {
match Pin::new(&mut self.rx).poll_next(cx) {
Poll::Ready(Some(bc)) => {
log::info!("Got Bc Packet");
match bc {
Bc {
body:
@@ -60,7 +59,10 @@ impl<'a> Stream for BcPayloadStream<'a> {
}),
..
} => Poll::Ready(Some(Ok(data))),
_ => Poll::Pending,
_ => {
cx.waker().wake_by_ref(); // Make it wake in next frame for another attempt at rx.poll_next(cx)
Poll::Pending
}
}
}
Poll::Ready(None) => Poll::Ready(None),
@@ -72,7 +74,7 @@ impl<'a> Stream for BcPayloadStream<'a> {
pub type BcMediaStream<'b> = FramedRead<Compat<IntoAsyncRead<BcPayloadStream<'b>>>, BcMediaCodex>;
impl<'a> BcSubscription<'a> {
pub fn new(rx: Receiver<Bc>, msg_num: u16, conn: &'a BcConnection) -> BcSubscription<'a> {
pub fn new(rx: Receiver<Bc>, msg_num: u32, conn: &'a BcConnection) -> BcSubscription<'a> {
BcSubscription {
rx: ReceiverStream::new(rx),
msg_num,
@@ -81,14 +83,14 @@ impl<'a> BcSubscription<'a> {
}
pub async fn send(&self, bc: Bc) -> Result<()> {
assert!(bc.meta.msg_num == self.msg_num);
assert!(bc.meta.msg_num as u32 == self.msg_num);
self.conn.send(bc).await?;
Ok(())
}
pub async fn recv(&mut self) -> Result<Bc> {
let bc = self.rx.next().await.ok_or(Error::DroppedSubscriber)?;
assert!(bc.meta.msg_num == self.msg_num);
assert!(bc.meta.msg_num as u32 == self.msg_num);
Ok(bc)
}
@@ -110,11 +112,3 @@ impl<'a> BcSubscription<'a> {
FramedRead::new(async_read, BcMediaCodex::new())
}
}
/// Makes it difficult to avoid unsubscribing when you're finished
impl<'a> Drop for BcSubscription<'a> {
fn drop(&mut self) {
// It's fine if we can't unsubscribe as that means we already have
let _ = self.conn.unsubscribe(self.msg_num);
}
}
@@ -25,7 +25,7 @@ use tokio::{
Mutex, RwLock,
},
task::JoinSet,
time::{interval, timeout, Duration},
time::{interval, Duration},
};
use tokio_stream::wrappers::ReceiverStream;
use tokio_util::udp::UdpFramed;
@@ -216,17 +216,10 @@ impl Discoverer {
// Get what ever completes first
while let Some(result) = set.next().await {
match result {
Ok(ret) => {
// info!("Future pop OK");
return Ok(ret);
}
Err(_e) => {
// info!("Future pop Err: {:?}", e);
}
if let Ok(ret) = result {
return Ok(ret);
}
}
// info!("set: empty");
Err(Error::DiscoveryTimeout)
}
@@ -246,22 +239,40 @@ impl Discoverer {
let mut reply = ReceiverStream::new(self.subscribe(target_tid).await?);
let msg = BcUdp::Discovery(disc);
let mut inter = interval(Duration::from_millis(500));
let maximum_wait = Duration::from_secs(60);
let resend_wait = Duration::from_millis(500);
let mut inter = interval(resend_wait);
for _i in 0..5 {
inter.tick().await;
self.send(msg.clone(), dest).await?;
if let Ok(Some(Ok((reply, addr)))) =
timeout(Duration::from_millis(500), reply.next()).await
{
if let Some(result) = map(reply, addr) {
return Ok(result);
let result = tokio::select! {
v = async {
// Recv while channel is viable
while let Some(msg) = reply.next().await {
if let Ok((reply, addr)) = msg {
if let Some(result) = map(reply, addr) {
return Ok(result);
}
}
}
Err(Error::DroppedConnection)
} => {v},
v = async {
// Send every inter for ever or until channel is no longer viable
loop {
inter.tick().await;
if let Err(e) = self.send(msg.clone(), dest).await {
return e;
}
}
} => {Err::<T, Error>(v)},
_ = {
// Sleep then emit Timeout
tokio::time::sleep(maximum_wait)
} => {
Err::<T, Error>(Error::DiscoveryTimeout)
}
}
Err(Error::DiscoveryTimeout)
};
result
}
// This function will contact the p2p relay servers
@@ -217,29 +217,33 @@ pub(crate) struct UdpPayloadSource {
impl UdpPayloadSource {
fn build_send_ack(&self) -> UdpAck {
assert!(self.packets_want > 0);
let start: u32 = self.packets_want - 1;
let missing_ids = if let Some(end) = self.recieved.keys().max() {
let mut vec = vec![];
// From last contiguous packet to last recieved packet
// create a payload of `00` (unreceived) and `01` (received)
// that can be used to form the `UdpAck` packet
for i in (start + 1)..(end + 1) {
if self.recieved.contains_key(&i) {
vec.push(1)
} else {
vec.push(0)
if self.packets_want > 0 {
let start: u32 = self.packets_want - 1;
let missing_ids = if let Some(end) = self.recieved.keys().max() {
let mut vec = vec![];
// From last contiguous packet to last recieved packet
// create a payload of `00` (unreceived) and `01` (received)
// that can be used to form the `UdpAck` packet
for i in (start + 1)..(end + 1) {
if self.recieved.contains_key(&i) {
vec.push(1)
} else {
vec.push(0)
}
}
}
vec
} else {
vec![]
};
vec
} else {
vec![]
};
UdpAck {
connection_id: self.camera_id,
packet_id: start,
payload: missing_ids,
UdpAck {
connection_id: self.camera_id,
packet_id: start,
group_id: 0,
payload: missing_ids,
}
} else {
UdpAck::empty(self.camera_id)
}
}
@@ -254,6 +258,21 @@ impl UdpPayloadSource {
}
}
}
fn maintanence(&mut self, cx: &mut Context<'_>) {
// Check for periodic resends
if self.interval.poll_tick(cx).is_ready() {
trace!("UDPSource.RecievedPacket: Resend");
for (_, resend) in self.sent.iter() {
self.send_buffer.push_back(BcUdp::Data(resend.clone()));
}
let ack = BcUdp::Ack(self.build_send_ack());
trace!("UDPSource.RecievedPacket: QueueingAck");
self.send_buffer.push_back(ack);
self.state = State::Flushing;
}
}
}
impl Stream for UdpPayloadSource {
@@ -270,27 +289,12 @@ impl Stream for UdpPayloadSource {
if let Some(payload) = this.recieved.remove(&this.packets_want) {
trace!("UDPSource.Data: PreReady: {}", this.packets_want);
this.packets_want += 1;
let ack = BcUdp::Ack(this.build_send_ack());
this.send_buffer.push_back(ack);
return Poll::Ready(Some(Ok(payload)));
}
// Check for periodic resends
if this.interval.poll_tick(cx).is_ready() {
trace!("UDPSource.RecievedPacker: Resend");
for (_, resend) in this.sent.iter() {
this.send_buffer.push_back(BcUdp::Data(resend.clone()));
}
if this.packets_want == 0 {
// When no packets have been recieved the
// ACK is different specifically unknown_b
// is set to -1 and packet_id is also set to
// -1
// TODO Add this style of ACK
} else {
let ack = BcUdp::Ack(this.build_send_ack());
this.send_buffer.push_back(ack);
}
this.state = State::Flushing;
continue;
}
// Handle resend events
this.maintanence(cx);
// Normal behaviors
match this.inner.poll_next_unpin(cx) {
Poll::Ready(Some(Ok((
@@ -307,6 +311,8 @@ impl Stream for UdpPayloadSource {
if packet_id == this.packets_want {
trace!("UDPSource.RecievedPacker: NewData: {}", this.packets_want);
this.packets_want += 1;
let ack = BcUdp::Ack(this.build_send_ack());
this.send_buffer.push_back(ack);
return Poll::Ready(Some(Ok(payload)));
} else {
trace!("UDPSource.RecievedPacker: OtherData");
+8 -1
View File
@@ -63,13 +63,20 @@ pub enum Error {
#[error(display = "Cookie GenError")]
GenError(#[error(source)] std::sync::Arc<cookie_factory::GenError>),
/// Raised when a connection is subscrbed to more than once
/// Raised when a connection is subscrbed to more than once for msg_num
#[error(display = "Simultaneous subscription, {}", _0)]
SimultaneousSubscription {
/// The message number that was subscribed to
msg_num: u16,
},
/// Raised when a connection is subscrbed to more than once for msg_id
#[error(display = "Simultaneous subscription, {}", _0)]
SimultaneousSubscriptionId {
/// The message number that was subscribed to
msg_id: u32,
},
/// Raised when a new encyrption byte is observed
#[error(display = "Unknown encryption: {:x?}", _0)]
UnknownEncryption(usize),
+30
View File
@@ -0,0 +1,30 @@
use super::{BcCamera, Result};
use crate::bc::model::*;
impl BcCamera {
/// Create a handller to respond to keep alive messages
/// These messages are sent by the camera so we listen to
/// a message ID rather than setting a message number and
/// responding to it
pub async fn keepalive(&self) -> Result<()> {
let connection = self.get_connection();
connection
.handle_msg(MSG_ID_UDP_KEEP_ALIVE, |bc| {
Some(Bc {
meta: BcMeta {
msg_id: MSG_ID_UDP_KEEP_ALIVE,
channel_id: bc.meta.channel_id,
msg_num: bc.meta.msg_num,
stream_type: bc.meta.stream_type,
response_code: 200,
class: 0x6414,
},
body: BcBody::ModernMsg(ModernMsg {
..Default::default()
}),
})
})
.await?;
Ok(())
}
}
+30 -1
View File
@@ -2,11 +2,35 @@ use super::{md5_string, BcCamera, Error, Result, Truncate, ZeroLast};
use crate::bc::{model::*, xml::*};
use std::sync::atomic::Ordering;
/// The requested encryption level to request
/// to the camera
///
/// The camera may use a lower one depending on support
///
/// Note the reolink camera only encrypt the control messages
/// the camera feed is always accessible
#[derive(Debug, Clone, Copy)]
pub enum MaxEncryption {
/// No encryption
None,
/// BCEncrypt is a simple XOR algortirhm with a fixed key
/// used in many older models
BcEncrypt,
/// AES is used in newer model
Aes,
}
impl BcCamera {
/// Login to the camera.
///
/// This should be called before most other commands
pub async fn login(&self) -> Result<DeviceInfo> {
self.login_with_maxenc(MaxEncryption::Aes).await
}
/// Login to the camera.
///
/// This should be called before most other commands
pub async fn login_with_maxenc(&self, max_encryption: MaxEncryption) -> Result<DeviceInfo> {
let device_info;
// This { is here due to the connection and set_credentials both requiring a mutable borrow
{
@@ -31,13 +55,18 @@ impl BcCamera {
.map(|p| md5_string(p, ZeroLast))
.unwrap_or_else(|| EMPTY_LEGACY_PASSWORD.to_owned());
let enc_byte = match max_encryption {
MaxEncryption::None => 0xdc00,
MaxEncryption::BcEncrypt => 0xdc01,
MaxEncryption::Aes => 0xdc02,
};
let legacy_login = Bc {
meta: BcMeta {
msg_id: MSG_ID_LOGIN,
channel_id: self.channel_id,
msg_num,
stream_type: 0,
response_code: 0xdc02,
response_code: enc_byte,
class: 0x6514,
},
body: BcBody::LegacyMsg(LegacyMsg::LoginMsg {
+1 -6
View File
@@ -31,6 +31,7 @@ pub enum StreamKind {
///
/// When this object is dropped the streaming is stopped
pub struct StreamData {
#[allow(dead_code)]
handle: Option<JoinHandle<Result<()>>>,
rx: Receiver<Result<BcMedia>>,
abort_handle: Arc<AtomicBool>,
@@ -50,12 +51,6 @@ impl StreamData {
impl Drop for StreamData {
fn drop(&mut self) {
self.abort_handle.store(true, Ordering::Relaxed);
if let Some(h) = self.handle.take() {
let backoff = crossbeam::utils::Backoff::new();
while !h.is_finished() {
backoff.snooze();
}
}
}
}
+5 -2
View File
@@ -89,8 +89,10 @@ fn udp_ack(buf: &[u8]) -> IResult<&[u8], UdpAck> {
let (buf, connection_id) = error_context("ACK: Missing connect ID", le_i32)(buf)?;
let (buf, _unknown_a) =
error_context("ACK: Unable to verify UnknowA", verify(le_u32, |&x| x == 0))(buf)?;
let (buf, _unknown_b) =
error_context("ACK: Unable to verify UnknowB", verify(le_u32, |&x| x == 0))(buf)?;
let (buf, group_id) = error_context(
"ACK: Unable to verify UnknowB",
verify(le_u32, |&x| x == 0 || x == 0xffffffff),
)(buf)?;
let (buf, packet_id) = error_context("Missing packet_id", le_u32)(buf)?; // This is the point at which the camera has contigious
// packets to
let (buf, _unknown) = error_context("ACK: Missing unknown", le_u32)(buf)?;
@@ -109,6 +111,7 @@ fn udp_ack(buf: &[u8]) -> IResult<&[u8], UdpAck> {
let data = UdpAck {
connection_id,
packet_id,
group_id,
payload,
};
Ok((buf, data))
+17 -2
View File
@@ -63,7 +63,9 @@ pub struct UdpAck {
/// register and then download the same connection id from the register_address
/// it comes back in an xml that is encoded as i32 (i.e. can be negative string)
pub connection_id: i32,
// Unknown 4 bytes always 0
/// Unknown 4 bytes always 0 for normal ack or -1 for signalling no packets received yet
/// Tentativly assigned as a kinda group id
pub group_id: u32,
/// The ID of the last data packet [`UdpData`]
pub packet_id: u32,
// 2 Bytes Unknown: Observed values `00000000`, `d6010000`, `d7160000` `09e00000`
@@ -76,6 +78,19 @@ pub struct UdpAck {
pub payload: Vec<u8>,
}
impl UdpAck {
/// Create an empty ack packet to signifiy no packets recieved yet
/// These are a little different in that they also set `group_id`
pub fn empty(connection_id: i32) -> Self {
Self {
connection_id,
group_id: 0xffffffff,
packet_id: 0xffffffff,
payload: vec![],
}
}
}
/// Magic for the UDP Data packet
pub const MAGIC_HEADER_UDP_DATA: u32 = 0x2a87cf10;
@@ -107,7 +122,7 @@ impl std::fmt::Debug for UdpData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_map()
.entry(&"connection_id", &self.connection_id)
.entry(&"packet_id", &self.connection_id)
.entry(&"packet_id", &self.packet_id)
.entry(&"payload_len", &self.payload.len())
.finish()
}
+1 -1
View File
@@ -50,7 +50,7 @@ fn bcudp_ack<'a, W: 'a + Write>(
le_u32(MAGIC_HEADER_UDP_ACK),
le_i32(payload.connection_id),
le_u32(0),
le_u32(0),
le_u32(payload.group_id),
le_u32(payload.packet_id),
le_u32(0),
le_u32(binary_payload.len() as u32),
+1
View File
@@ -23,4 +23,5 @@ pub enum Command {
Pir(super::pir::Opt),
Talk(super::talk::Opt),
Mqtt(super::mqtt::Opt),
Image(super::image::Opt),
}
+14
View File
@@ -11,6 +11,8 @@ lazy_static! {
static ref RE_TLS_CLIENT_AUTH: Regex = Regex::new(r"^(none|request|require)$").unwrap();
static ref RE_PAUSE_MODE: Regex = Regex::new(r"^(black|still|test|none)$").unwrap();
static ref RE_DISC_SRC: Regex = Regex::new(r"^([nN]one|[lL]ocal|[rR]emote|[rR]elay)$").unwrap();
static ref RE_MAXENC_SRC: Regex =
Regex::new(r"^([nN]one|[Aa][Ee][Ss]|[Bb][Cc][Ee][Nn][Cc][Rr][Yy][Pp][Tt])$").unwrap();
}
#[derive(Debug, Deserialize, Validate, Clone)]
@@ -84,6 +86,14 @@ pub(crate) struct CameraConfig {
code = "discovery"
))]
pub(crate) discovery: String,
#[serde(default = "default_maxenc")]
#[validate(regex(
path = "RE_MAXENC_SRC",
message = "Invalid maximum encryption method",
code = "max_encryption"
))]
pub(crate) max_encryption: String,
}
#[derive(Debug, Deserialize, Validate, Clone)]
@@ -132,6 +142,10 @@ fn default_discovery() -> String {
"Relay".to_string()
}
fn default_maxenc() -> String {
"Aes".to_string()
}
#[derive(Debug, Deserialize, Validate, Clone)]
pub(crate) struct PauseConfig {
#[serde(default = "default_on_motion")]
+12
View File
@@ -0,0 +1,12 @@
use std::path::PathBuf;
use structopt::StructOpt;
/// The image command will dump a still image from the camera
#[derive(StructOpt, Debug)]
pub struct Opt {
/// The name of the camera to get the image from. Must be a name in the config
pub camera: String,
/// The path of the output.
#[structopt(short, long, parse(from_os_str))]
pub file_path: PathBuf,
}
+191
View File
@@ -0,0 +1,191 @@
use std::path::Path;
use anyhow::{anyhow, Context, Result};
use gstreamer::{parse_launch, prelude::*, ClockTime, MessageView, Pipeline, State};
use gstreamer_app::AppSrc;
use neolink_core::bcmedia::model::VideoType;
use tokio::{
sync::{
self,
mpsc::{channel, Sender},
},
task::{self, JoinSet},
};
#[derive(Debug)]
enum GstControl {
Data(Vec<u8>),
Eos,
}
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<()>>,
}
impl GstSender {
pub(super) async fn send(&self, buf: Vec<u8>) -> Result<()> {
self.sender
.send(GstControl::Data(buf))
.await
.map_err(|e| anyhow!("Failed to send buffer: {:?}", e))
}
pub(super) async fn eos(&self) -> Result<()> {
self.sender
.send(GstControl::Eos)
.await
.map_err(|e| anyhow!("Failed to send eos: {:?}", e))
}
pub(super) async fn is_finished(&mut self) -> Option<Result<()>> {
match self.finished.try_recv() {
Ok(res) => Some(res),
Err(sync::oneshot::error::TryRecvError::Empty) => None,
Err(sync::oneshot::error::TryRecvError::Closed) => {
Some(Err(anyhow!("Gstreamer finished channel is closed")))
}
}
}
}
pub(super) async fn from_input<T: AsRef<Path>>(
format: VideoType,
out_file: T,
) -> Result<GstSender> {
let pipeline = create_pipeline(format, out_file.as_ref())?;
output(pipeline).await
}
async fn output(pipeline: Pipeline) -> Result<GstSender> {
let source = get_source(&pipeline)?;
let (sender, mut reciever) = channel::<GstControl>(100);
let mut set = JoinSet::new();
set.spawn(async move {
while let Some(control) = reciever.recv().await {
match control {
GstControl::Data(buf) => {
let mut gst_buf = gstreamer::Buffer::with_size(buf.len()).unwrap();
{
let gst_buf_mut = gst_buf.get_mut().unwrap();
let mut gst_buf_data = gst_buf_mut.map_writable().unwrap();
gst_buf_data.copy_from_slice(&buf);
}
source.push_buffer(gst_buf)?;
}
GstControl::Eos => {
source.end_of_stream()?;
break;
}
}
}
Ok(())
});
let (tx, finished) = sync::oneshot::channel();
task::spawn_blocking(move || {
let res = start_pipeline(pipeline);
if let Err(e) = &res {
log::error!("Failed to run pipeline: {:?}", e);
}
tx.send(res)
});
Ok(GstSender {
sender,
set,
finished,
})
}
fn start_pipeline(pipeline: Pipeline) -> Result<()> {
pipeline.set_state(State::Playing)?;
let bus = pipeline
.bus()
.expect("Pipeline without bus. Shouldn't happen!");
for msg in bus.iter_timed(ClockTime::NONE) {
match msg.view() {
MessageView::Eos(..) => break,
MessageView::Error(err) => {
pipeline
.set_state(State::Null)
.context("Error in gstreamer when setting state to Null")?;
log::warn!(
"Error from gstreamer when setting the play state {:?} setting to Null instead",
err
);
}
_ => (),
}
}
pipeline
.set_state(State::Null)
.context("Error in gstreamer when setting state to Null")?;
Ok(())
}
fn get_source(pipeline: &Pipeline) -> Result<AppSrc> {
let source = pipeline
.by_name("thesource")
.expect("There shoud be a `thesource`");
source
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot find appsource in gstreamer, check your gstreamer plugins"))
}
fn create_pipeline(format: VideoType, file_path: &Path) -> Result<Pipeline> {
gstreamer::init()
.context("Unable to start gstreamer ensure it and all plugins are installed")?;
let file_path = file_path.with_extension("jpeg");
let launch_str = match format {
VideoType::H264 => {
format!(
"appsrc name=thesource \
! h264parse \
! decodebin \
! jpegenc snapshot=TRUE
! filesink location={}",
file_path.display()
)
}
VideoType::H265 => {
format!(
"appsrc name=thesource \
! h265parse \
! decodebin \
! jpegenc snapshot=TRUE
! filesink location={}",
file_path.display()
)
}
};
log::info!("{}", launch_str);
// Parse the pipeline we want to probe from a static in-line string.
// Here we give our audiotestsrc a name, so we can retrieve that element
// from the resulting pipeline.
let pipeline = parse_launch(&launch_str)
.context("Unable to load gstreamer pipeline ensure all gstramer plugins are installed")?;
let pipeline = pipeline.dynamic_cast::<Pipeline>().map_err(|_| {
anyhow!("Unable to create gstreamer pipeline ensure all gstramer plugins are installed")
})?;
// let appssource = get_source(&pipeline)?;
// Tell the appsink what format we produce.
// let caps = match format {
// VideoType::H264 => Caps::new_simple("video/x-h264", &[("parsed", &false)]),
// VideoType::H265 => Caps::new_simple("video/x-h265", &[("parsed", &false)]),
// };
// appssource.set_caps(Some(&caps));
Ok(pipeline)
}
+75
View File
@@ -0,0 +1,75 @@
///
/// # Neolink Image
///
/// This module can be used to dump a still image from the camera
///
///
/// # Usage
/// ```bash
/// neolink image --config=config.toml --file-path=filepath CameraName
/// ```
///
use anyhow::{Context, Result};
use log::*;
use neolink_core::{bc_protocol::*, bcmedia::model::*};
mod cmdline;
mod gst;
use super::config::Config;
use crate::utils::{connect_and_login, find_camera_by_name};
pub(crate) use cmdline::Opt;
/// Entry point for the image subcommand
///
/// Opt is the command line options
pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> {
let camera_config = find_camera_by_name(&config, &opt.camera).context(
"failed to load config file or find the camera. Check the path and name of the camera.",
)?;
// Set up camera to recieve the stream
let camera = connect_and_login(camera_config)
.await
.context("Failed to connect to the camera, check credentials and network")?;
let mut stream_data = camera
.start_video(StreamKind::Main, 0)
.await
.context("Failed to start video")?;
// Get one iframe at the start while also getting the the video type
let buf;
let vid_type;
loop {
if let BcMedia::Iframe(frame) = stream_data.get_data().await?? {
vid_type = frame.video_type;
buf = frame.data;
break;
}
}
let mut sender = gst::from_input(vid_type, &opt.file_path).await?;
sender.send(buf).await?; // Send first iframe
// Keep sending both IFrame or PFrame until finished
while sender.is_finished().await.is_none() {
let buf = match stream_data.get_data().await?? {
BcMedia::Iframe(frame) => frame.data,
BcMedia::Pframe(frame) => frame.data,
_ => {
continue;
}
};
debug!("Sending frame data to gstreamer");
if sender.send(buf).await.is_err() {
// Assume that the sender is closed
// because the pipeline is finished
break;
}
}
debug!("Sending EOS");
let _ = sender.eos().await; // Ignore return because if pipeline is finished this will error
Ok(())
}
+4
View File
@@ -16,6 +16,7 @@ use validator::Validate;
mod cmdline;
mod config;
mod image;
mod mqtt;
mod pir;
mod reboot;
@@ -76,6 +77,9 @@ async fn main() -> Result<()> {
Some(Command::Mqtt(opts)) => {
mqtt::main(opts, config).await?;
}
Some(Command::Image(opts)) => {
image::main(opts, config).await?;
}
}
Ok(())
-2
View File
@@ -218,8 +218,6 @@ impl GstOutputs {
// - In the case of PausedSources::None we just pass through without encoding
// - In the case of all other pause sources we need to recencode in order
// to get smooth streams when the stream is paused/resumed
// info!("self.video_format: {:?}", self.video_format);
// info!("self.audio_format: {:?}", self.audio_format);
let launch_vid_select = match self.when_paused {
PausedSources::None => match self.video_format {
Some(StreamFormat::H264) => "! rtph264pay name=pay0",
+4 -1
View File
@@ -15,7 +15,10 @@ pub(crate) struct LoggedIn {}
#[async_trait]
impl CameraState for LoggedIn {
async fn setup(&mut self, shared: &Shared) -> Result<(), anyhow::Error> {
shared.camera.login().await?;
shared
.camera
.login_with_maxenc(shared.max_encryption)
.await?;
Ok(())
}
+8 -1
View File
@@ -6,7 +6,7 @@ use log::*;
use std::collections::HashSet;
use std::sync::Arc;
use neolink_core::bc_protocol::{MotionData, StreamKind as Stream};
use neolink_core::bc_protocol::{MaxEncryption, MotionData, StreamKind as Stream};
use crate::config::{CameraConfig, UserConfig};
use crate::rtsp::gst::RtspServer;
@@ -111,6 +111,12 @@ impl RtspCamera {
None => ["anonymous".to_string()].iter().cloned().collect(),
};
let max_encryption = match config.max_encryption.to_lowercase().as_str() {
"none" => MaxEncryption::None,
"bcencrypt" => MaxEncryption::BcEncrypt,
"aes" => MaxEncryption::Aes,
_ => MaxEncryption::Aes,
};
let shared = Shared {
camera: Arc::new(camera),
addr: camera_addr,
@@ -122,6 +128,7 @@ impl RtspCamera {
streams,
rtsp,
permitted_users,
max_encryption,
};
let mut state: Connected = Default::default();
+2 -1
View File
@@ -7,7 +7,7 @@ use async_trait::async_trait;
use std::collections::HashSet;
use std::sync::Arc;
use neolink_core::bc_protocol::{BcCamera, StreamKind as Stream};
use neolink_core::bc_protocol::{BcCamera, MaxEncryption, StreamKind as Stream};
use crate::config::PauseConfig;
use crate::rtsp::gst::RtspServer;
@@ -25,6 +25,7 @@ pub(crate) struct Shared {
pub(super) rtsp: Arc<RtspServer>,
pub(super) permitted_users: HashSet<String>,
pub(super) pause: PauseConfig,
pub(super) max_encryption: MaxEncryption,
}
impl Shared {
+8 -2
View File
@@ -4,7 +4,7 @@ use log::*;
use super::config::{CameraConfig, Config};
use anyhow::{anyhow, Context, Error, Result};
use neolink_core::bc_protocol::{BcCamera, DiscoveryMethods};
use neolink_core::bc_protocol::{BcCamera, DiscoveryMethods, MaxEncryption};
use std::fmt::{Display, Error as FmtError, Formatter};
pub(crate) enum AddressOrUid {
@@ -97,9 +97,15 @@ pub(crate) async fn connect_and_login(camera_config: &CameraConfig) -> Result<Bc
)
})?;
let max_encryption = match camera_config.max_encryption.to_lowercase().as_str() {
"none" => MaxEncryption::None,
"bcencrypt" => MaxEncryption::BcEncrypt,
"aes" => MaxEncryption::Aes,
_ => MaxEncryption::Aes,
};
info!("{}: Logging in", camera_config.name);
camera
.login()
.login_with_maxenc(max_encryption)
.await
.with_context(|| format!("Failed to login to {}", camera_config.name))?;