Merge pull request #230 from QuantumEntangledAndy/feature/shared_gst

Update deps
This commit is contained in:
Andrew King authored and GitHub committed 2024-04-05 15:15:05 +07:00
commit c0a6123740
44 files changed
+1888 -1842

No files matched your search

File renamed without changes.
+22 -8
View File
@@ -17,8 +17,8 @@ jobs:
uses: fkirc/skip-duplicate-actions@v5
with:
# All of these options are optional, so you can remove them if you are happy with the defaults
concurrent_skipping: 'same_content_newer'
skip_after_successful_duplicate: 'false'
concurrent_skipping: "same_content_newer"
skip_after_successful_duplicate: "false"
native:
needs: pre_job
@@ -45,6 +45,10 @@ jobs:
choco install -y --no-progress gstreamer --version=1.20.0
choco install -y --no-progress gstreamer-devel --version=1.20.0
$env:GSTREAMER_1_0_ROOT_MSVC_X86_64=$env:SYSTEMDRIVE + '\gstreamer\1.0\msvc_x86_64\'
# Github runners work on both C or D drive and figuring out which was used is difficult
if (-not (Test-Path -Path "$env:GSTREAMER_1_0_ROOT_MSVC_X86_64" -PathType Container)) {
$env:GSTREAMER_1_0_ROOT_MSVC_X86_64='D:\\gstreamer\1.0\msvc_x86_64\'
}
echo "GSTREAMER_1_0_ROOT_MSVC_X86_64=$env:GSTREAMER_1_0_ROOT_MSVC_X86_64"
# Proto buffers
@@ -53,9 +57,19 @@ jobs:
# Open SSL
choco install -y --no-progress openssl --version=1.1.1.2100
$env:OPENSSL_DIR=$env:SYSTEMDRIVE + '\Program Files\OpenSSL-Win64\'
# Alternative openssl location (depends on version that gets installed by choco)
if (-not (Test-Path -Path "$env:OPENSSL_DIR" -PathType Container)) {
$env:OPENSSL_DIR=$env:SYSTEMDRIVE + '\Program Files\OpenSSL\'
}
# Github runners work on both C or D drive and figuring out which was used is difficult
if (-not (Test-Path -Path "$env:OPENSSL_DIR" -PathType Container)) {
$env:OPENSSL_DIR='D:\\Program Files\OpenSSL-Win64\'
}
# Or course we could be on alternative location and drive....
if (-not (Test-Path -Path "$env:OPENSSL_DIR" -PathType Container)) {
$env:OPENSSL_DIR='D:\\Program Files\OpenSSL\'
}
# Set github vars
Add-Content -Path $env:GITHUB_ENV -Value "GSTREAMER_1_0_ROOT_MSVC_X86_64=$env:GSTREAMER_1_0_ROOT_MSVC_X86_64"
@@ -190,9 +204,9 @@ jobs:
id: vars
shell: bash
run: |
unset HAS_SECRET
if [ -n $SECRET ]; then HAS_SECRET='true' ; fi
echo "HAS_SECRET_TOKEN=${HAS_SECRET}" >> $GITHUB_OUTPUT
unset HAS_SECRET
if [ -n $SECRET ]; then HAS_SECRET='true' ; fi
echo "HAS_SECRET_TOKEN=${HAS_SECRET}" >> $GITHUB_OUTPUT
env:
SECRET: "${{ secrets.DOCKER_TOKEN }}"
- name: Check out the repo
@@ -268,7 +282,7 @@ jobs:
REPO_NAME: ${{ steps.docker_repo.outputs.DOCKER_NWO }}
- name: Install latest rust
run: |
rustup toolchain install stable
rustup toolchain install stable
- name: Install toml-cli
run: |
cargo install toml-cli
@@ -327,7 +341,7 @@ jobs:
uses: actions/checkout@v3
- name: Install latest rust
run: |
rustup toolchain install stable
rustup toolchain install stable
- name: Install toml-cli
run: |
cargo install toml-cli
@@ -401,4 +415,4 @@ jobs:
body: |
Neolink Release ${{steps.toml.outputs.version}}
draft: true
prerelease: ${{ contains(github.ref_name, 'rc') }}
prerelease: ${{ contains(github.ref_name, 'rc') }}
Generated
+685 -905
View File
File diff suppressed because it is too large. Load diff
+16 -16
View File
@@ -1,7 +1,7 @@
[package]
name = "neolink"
description = "A standards-compliant bridge to Reolink IP cameras"
version = "0.6.3-rc.1"
version = "0.6.3-rc.2"
authors = ["George Hilliard <thirtythreeforty@gmail.com>", "Andrew King <sheepchaan@gmail.com>"]
edition = "2018"
license = "AGPL-3.0-or-later"
@@ -14,35 +14,35 @@ members = [
[dependencies]
anyhow = "1.0.70"
async-stream = "0.3.5"
base64 = "0.21.2"
base64 = "0.22.0"
byte-slice-cast = "1.2.2"
bytes = "1.6.0"
clap = { version = "4.2.2", features = ["derive", "cargo"] }
console-subscriber = "0.2.0"
crossbeam-channel = "0.5.8"
dirs = "5.0.1"
env_logger = "0.10.0"
fcm-push-listener = "2.0.1"
env_logger = "0.11.3"
fcm-push-listener = "2.0.3"
futures = "0.3.28"
gstreamer = "0.21.0"
gstreamer-app = { version = "0.21.0", features = ["v1_18"] }
gstreamer-rtsp = { version = "0.21.0", features = ["v1_18"] }
gstreamer-rtsp-server = { version = "0.21.0", features = ["v1_18"] }
heck = "0.4.1"
lazy_static = "1.4.0"
gstreamer = "0.22.3"
gstreamer-app = { version = "0.22.0", features = ["v1_18"] }
gstreamer-rtsp = { version = "0.22.0", features = ["v1_18"] }
gstreamer-rtsp-server = { version = "0.22.0", features = ["v1_18"] }
heck = "0.5.0"
log = { version = "0.4.17", features = [ "release_max_level_debug" ] }
md5 = "0.7.0"
neolink_core = { path = "crates/core", version = "0.6.3-rc.1" }
neolink_core = { path = "crates/core", version = "0.6.3-rc.2" }
once_cell = "1.19.0"
quick-xml = { version = "0.31.0", features = ["serialize"] }
regex = "1.7.3"
rumqttc = "0.22.0"
rumqttc = "0.24.0"
serde = { version = "1.0.160", features = ["derive"] }
serde_json = "1.0.96"
tokio = { version = "1.27.0", features = ["rt-multi-thread", "macros", "io-util", "tracing"] }
tokio-stream = "0.1.12"
tokio-util = { version = "0.7.7", features = ["full", "tracing"] }
toml = "0.8.2"
validator = "0.16.0"
validator_derive = "0.16.0"
yaserde = "0.8.0"
validator = "0.17.0"
validator_derive = "0.17.0"
[target.'cfg(not(target_env = "msvc"))'.dependencies]
tikv-jemallocator = "0.5"
+11 -2
View File
@@ -63,9 +63,18 @@ fn platform_cfg() {
let gstreamer_dir = env::var_os("GSTREAMER_1_0_ROOT_MACOSX")
.and_then(|x| x.into_string().ok())
.unwrap_or_else(|| r"/Library/Frameworks/GStreamer.framework/Versions/1.0".to_string());
let openssl_dir = env::var_os("OPENSSL_1_1_ROOT_MACOSX")
.and_then(|x| x.into_string().ok())
.unwrap_or_else(|| r"/usr/local/opt/openssl@1.1".to_string());
println!(r"cargo:rustc-link-search=native={}/lib", gstreamer_dir);
println!(r"cargo:rustc-link-arg=-Wl,-rpath,{}/lib", gstreamer_dir);
println!(
r"cargo:rustc-link-search=native={}/lib;{}/lib",
gstreamer_dir, openssl_dir
);
println!(
r"cargo:rustc-link-arg=-Wl,-rpath,{}/lib;{}/lib/",
gstreamer_dir, openssl_dir
);
}
#[cfg(all(not(target_os = "macos"), not(target_os = "windows")))]
+4 -5
View File
@@ -1,7 +1,7 @@
[package]
name = "neolink_core"
description = "Core services and structure for Reolink IP cameras"
version = "0.6.3-rc.1"
version = "0.6.3-rc.2"
authors = ["George Hilliard <thirtythreeforty@gmail.com>", "Andrew King <sheepchaan@gmail.com>"]
edition = "2018"
license = "AGPL-3.0-or-later"
@@ -13,23 +13,22 @@ cfb-mode = "0.8.2"
cookie-factory = "0.3.2"
crc32fast = "1.3.2"
crossbeam-channel = "0.5.8"
delegate = "0.10.0"
err-derive = "0.3.1"
delegate = "0.12.0"
futures = "0.3.28"
get_if_addrs = "0.5.3"
lazy_static = "1.4.0"
log = "0.4.17"
md5 = "0.7.0"
nom = { version = "7.1.3", features = ["alloc"] }
quick-xml = { version = "0.31.0", features = ["serialize"] }
rand = "0.8.5"
regex = "1.7.3"
serde = { version = "1.0.106", features = ["derive"] }
thiserror = "1.0.58"
time = { version = "0.3.20" , features = [ "macros", "parsing", "local-offset" ]}
tokio = { version = "1.27.0", features = ["full"] }
tokio-stream = { version = "0.1.12", features = ["sync", "time", "net"] }
tokio-util = { version = "0.7.7", features = ["full", "tracing"] }
yaserde = "0.8.0"
yaserde_derive = "0.8.0"
[dev-dependencies]
assert_matches = "1.5.0"
+48 -33
View File
@@ -1,5 +1,4 @@
use super::model::*;
use super::xml::{BcPayloads, BcXml};
use super::xml_crypto;
use crate::Error;
use bytes::{Buf, BytesMut};
@@ -84,10 +83,10 @@ fn bc_modern_msg<'a>(
Err,
};
fn make_error<I, E: ParseError<I>>(input: I, ctx: &'static str, kind: ErrorKind) -> E
fn make_error<I, E>(input: I, ctx: &'static str, kind: ErrorKind) -> E
where
I: std::marker::Copy,
E: ContextError<I>,
E: ParseError<I> + ContextError<I>,
{
E::add_context(input, ctx, E::from_error_kind(input, kind))
}
@@ -199,13 +198,14 @@ fn bc_modern_msg<'a>(
.unwrap_or("Not Text".to_string())
);
}
let xml = BcXml::try_parse(processed_payload_buf.as_slice()).map_err(|_| {
let xml = BcXml::try_parse(processed_payload_buf.as_slice()).map_err(|e| {
error!("header.msg_id: {}", header.msg_id);
error!(
"processed_payload_buf: {:X?}::{:?}",
processed_payload_buf,
std::str::from_utf8(&processed_payload_buf)
);
log::error!("e: {:?}", e);
Err::Error(make_error(
buf,
"Unable to parse Payload XML",
@@ -259,9 +259,18 @@ mod tests {
use super::*;
use crate::bc::xml::*;
use assert_matches::assert_matches;
use env_logger::Env;
fn init() {
let _ = env_logger::Builder::from_env(Env::default().default_filter_or("info"))
.is_test(true)
.try_init();
}
#[test]
fn test_bc_modern_login() {
init();
let sample = include_bytes!("samples/model_sample_modern_login.bin");
let context = BcContext::new_with_encryption(EncryptionProtocol::BCEncrypt);
@@ -290,45 +299,39 @@ mod tests {
#[test]
// This is an 0xdd03 encryption from an Argus 2
//
// It is currently unsupported
fn test_03_enc_login() {
init();
let sample = include_bytes!("samples/battery_enc.bin");
let context = BcContext::new_with_encryption(EncryptionProtocol::BCEncrypt);
let (buf, header) = bc_header(&sample[..]).unwrap();
assert!(bc_body(&context, &header, buf).is_err());
// It should error because we don't support it
//
// The following would be its contents if we
// did support it (maybe one day :) left it here
// for then)
//
//
// let (_, body) = bc_body(&mut context, &header, buf).unwrap();
// assert_eq!(header.msg_id, 1);
// assert_eq!(header.body_len, 175);
// assert_eq!(header.channel_id, 0);
// assert_eq!(header.stream_type, 0);
// assert_eq!(header.payload_offset, None);
// assert_eq!(header.response_code, 0xdd03);
// assert_eq!(header.class, 0x6614);
// match body {
// BcBody::ModernMsg(ModernMsg {
// payload:
// Some(BcPayloads::BcXml(BcXml {
// encryption: Some(encryption),
// ..
// })),
// ..
// }) => assert_eq!(encryption.nonce, "0-AhnEZyUg6eKrJFIWgXPF"),
// _ => panic!(),
// }
let (_, body) = bc_body(&context, &header, buf).unwrap();
assert_eq!(header.msg_id, 1);
assert_eq!(header.body_len, 175);
assert_eq!(header.channel_id, 0);
assert_eq!(header.stream_type, 0);
assert_eq!(header.payload_offset, None);
assert_eq!(header.response_code, 0xdd03);
assert_eq!(header.class, 0x6614);
match body {
BcBody::ModernMsg(ModernMsg {
payload:
Some(BcPayloads::BcXml(BcXml {
encryption: Some(encryption),
..
})),
..
}) => assert_eq!(encryption.nonce, "0-AhnEZyUg6eKrJFIWgXPF"),
_ => panic!(),
}
}
#[test]
fn test_bc_legacy_login() {
init();
let sample = include_bytes!("samples/model_sample_legacy_login.bin");
let context = BcContext::new_with_encryption(EncryptionProtocol::BCEncrypt);
@@ -353,6 +356,8 @@ mod tests {
#[test]
fn test_bc_modern_login_failed() {
init();
let sample = include_bytes!("samples/modern_login_failed.bin");
let context = BcContext::new_with_encryption(EncryptionProtocol::BCEncrypt);
@@ -377,6 +382,8 @@ mod tests {
#[test]
fn test_bc_modern_login_success() {
init();
let sample = include_bytes!("samples/modern_login_success.bin");
let context = BcContext::new_with_encryption(EncryptionProtocol::BCEncrypt);
@@ -404,6 +411,8 @@ mod tests {
#[test]
fn test_bc_binary_mode() {
init();
let sample1 = include_bytes!("samples/modern_video_start1.bin");
let sample2 = include_bytes!("samples/modern_video_start2.bin");
@@ -443,6 +452,8 @@ mod tests {
//
// They also have extra streams
fn test_bc_b800_externstream() {
init();
let sample = include_bytes!("samples/xml_externstream_b800.bin");
let context = BcContext::new_with_encryption(EncryptionProtocol::BCEncrypt);
@@ -485,6 +496,8 @@ mod tests {
//
// They also have extra streams
fn test_bc_b800_substream() {
init();
let sample = include_bytes!("samples/xml_substream_b800.bin");
let context = BcContext::new_with_encryption(EncryptionProtocol::BCEncrypt);
@@ -527,6 +540,8 @@ mod tests {
//
// They also have extra streams
fn test_bc_b800_mainstream() {
init();
let sample = include_bytes!("samples/xml_mainstream_b800.bin");
let context = BcContext::new_with_encryption(EncryptionProtocol::BCEncrypt);
+4 -1
View File
@@ -1,5 +1,4 @@
use super::model::*;
use super::xml::BcPayloads;
use super::xml_crypto;
use cookie_factory::bytes::*;
use cookie_factory::sequence::tuple;
@@ -92,6 +91,10 @@ fn bc_payload<W: Write>(
let payload_bytes = match payload {
BcPayloads::BcXml(x) => {
let xml_bytes = x.serialize(vec![]).unwrap();
// log::debug!(
// "Seialised as: {:?}",
// std::str::from_utf8(xml_bytes.as_ref())
// );
xml_crypto::encrypt(enc_offset, &xml_bytes, encryption_protocol)
}
BcPayloads::Binary(x) => x.to_owned(),
+485 -341
View File
File diff suppressed because it is too large. Load diff
+3 -2
View File
@@ -54,8 +54,9 @@ impl BcCamera {
/// Populate ability list of the camera
pub async fn polulate_abilities(&self) -> Result<()> {
let info = self.get_abilityinfo().await?;
let info_res = yaserde::ser::serialize_with_writer(&info, vec![], &Default::default());
if let Ok(Ok(info_str)) = info_res.map(String::from_utf8) {
let mut ser_buf = bytes::BytesMut::new();
let info_res = quick_xml::se::to_writer(&mut ser_buf, &info).map(|_| ser_buf);
if let Ok(Ok(info_str)) = info_res.map(|b| std::str::from_utf8(&b).map(|a| a.to_owned())) {
debug!("Abilities: {}", info_str);
}
+7 -8
View File
@@ -57,14 +57,13 @@ impl BcCamera {
);
}
PrintFormat::Xml => {
let bat_ser = String::from_utf8(
yaserde::ser::serialize_with_writer(
battery,
vec![],
&Default::default(),
)
.expect("Should Ser the struct"),
)
let bat_ser = String::from_utf8({
let mut ser_buf = bytes::BytesMut::new();
let parsed =
quick_xml::se::to_writer(&mut ser_buf, &battery)
.map(|_| ser_buf);
parsed.expect("Could not serialise data").to_vec()
})
.expect("Should be UTF8");
println!("{}", bat_ser);
}
@@ -350,28 +350,21 @@ impl Discoverer {
let port = self.local_addr().port();
let msg = UdpDiscovery {
tid,
payload: UdpXml {
c2d_c: Some(C2dC {
uid: uid.to_string(),
cli: ClientList { port: port as u32 },
cid: client_id,
mtu: MTU,
debug: false,
os: "MAC".to_string(),
}),
..Default::default()
},
payload: UdpXml::C2dC(C2dC {
uid: uid.to_string(),
cli: ClientList { port: port as u32 },
cid: client_id,
mtu: MTU,
debug: false,
os: "MAC".to_string(),
}),
};
let (camera_address, camera_id) = self
.retry_send(msg, addr, |bc, addr| match bc {
UdpDiscovery {
tid: _,
payload:
UdpXml {
d2c_c_r: Some(D2cCr { did, cid, .. }),
..
},
payload: UdpXml::D2cCr(D2cCr { did, cid, .. }),
} if cid == client_id => Some((addr, did)),
_ => None,
})
@@ -422,24 +415,17 @@ impl Discoverer {
async fn uid_lookup(&self, uid: &str, addr: SocketAddr) -> Result<UidLookupResults> {
let msg = UdpDiscovery {
tid: 0,
payload: UdpXml {
c2m_q: Some(C2mQ {
uid: uid.to_string(),
os: "MAC".to_string(),
}),
..Default::default()
},
payload: UdpXml::C2mQ(C2mQ {
uid: uid.to_string(),
os: "MAC".to_string(),
}),
};
trace!("Sending look up {:?}", msg);
let (packet, _) = self
.retry_send(msg, addr, |bc, addr| match bc {
UdpDiscovery {
tid: _,
payload:
UdpXml {
m2c_q_r: Some(m2c_q_r),
..
},
payload: UdpXml::M2cQr(m2c_q_r),
} if valid_addr(&m2c_q_r.reg) => Some((m2c_q_r, addr)),
_ => None,
})
@@ -469,28 +455,27 @@ impl Discoverer {
let msg = UdpDiscovery {
tid,
payload: UdpXml {
c2r_c: Some(C2rC {
uid: uid.to_string(),
cli: IpPort {
ip: local_ip.to_string(),
port: local_port,
},
relay: IpPort {
ip: lookup.relay.ip().to_string(),
port: lookup.relay.port(),
},
cid: client_id,
family: local_family,
debug: false,
os: "MAC".to_string(),
revision: Some(3),
}),
..Default::default()
},
payload: UdpXml::C2rC(C2rC {
uid: uid.to_string(),
cli: IpPort {
ip: local_ip.to_string(),
port: local_port,
},
relay: IpPort {
ip: lookup.relay.ip().to_string(),
port: lookup.relay.port(),
},
cid: client_id,
family: local_family,
debug: false,
os: "MAC".to_string(),
revision: Some(3),
}),
};
trace!("Registering: {:?}", msg.payload.c2r_c);
if let UdpXml::C2rC(ref c2r_c) = msg.payload {
trace!("Registering: {:?}", c2r_c);
}
// Send and await acceptance
let (sid, dev, dmap, relay) = self
@@ -500,18 +485,14 @@ impl Discoverer {
UdpDiscovery {
tid: _,
payload:
UdpXml {
r2c_c_r:
Some(R2cCr {
dmap,
dev,
relay,
sid,
rsp,
..
}),
UdpXml::R2cCr(R2cCr {
dmap,
dev,
relay,
sid,
rsp,
..
},
}),
} if (dev.as_ref().map(valid_addr).unwrap_or(false)
|| dmap.as_ref().map(valid_addr).unwrap_or(false)
|| relay.as_ref().map(valid_addr).unwrap_or(false))
@@ -553,17 +534,13 @@ impl Discoverer {
UdpDiscovery {
tid: _,
payload:
UdpXml {
r2c_c_r:
Some(R2cCr {
dev,
dmap,
relay,
rsp,
..
}),
UdpXml::R2cCr(R2cCr {
dev,
dmap,
relay,
rsp,
..
},
}),
} if (dev.as_ref().map(valid_addr).unwrap_or(false)
|| dmap.as_ref().map(valid_addr).unwrap_or(false)
|| relay.as_ref().map(valid_addr).unwrap_or(false))
@@ -598,17 +575,13 @@ impl Discoverer {
UdpDiscovery {
tid,
payload:
UdpXml {
d2c_t:
Some(D2cT {
sid,
cid,
did,
conn,
..
}),
UdpXml::D2cT(D2cT {
sid,
cid,
did,
conn,
..
},
}),
},
RegisterResult {
dmap: register_dmap,
@@ -632,16 +605,13 @@ impl Discoverer {
let msg = UdpDiscovery {
tid: local_tid,
payload: UdpXml {
c2d_a: Some(C2dA {
sid: register_result.sid,
conn: "local".to_string(),
cid: register_result.client_id,
did: local_did,
mtu: MTU,
}),
..Default::default()
},
payload: UdpXml::C2dA(C2dA {
sid: register_result.sid,
conn: "local".to_string(),
cid: register_result.client_id,
did: local_did,
mtu: MTU,
}),
};
let permit = self
@@ -657,17 +627,13 @@ impl Discoverer {
UdpDiscovery {
tid: _,
payload:
UdpXml {
d2c_cfm:
Some(D2cCfm {
sid,
cid,
did,
conn,
..
}),
UdpXml::D2cCfm(D2cCfm {
sid,
cid,
did,
conn,
..
},
}),
} if sid == register_result.sid
&& did == local_did
&& cid == register_result.client_id
@@ -690,16 +656,13 @@ impl Discoverer {
// Confirm local to register
let msg = UdpDiscovery {
tid: 0,
payload: UdpXml {
c2r_cfm: Some(C2rCfm {
sid: result.sid,
cid: result.client_id,
did: result.camera_id,
rsp: 0,
conn: "local".to_string(),
}),
..Default::default()
},
payload: UdpXml::C2rCfm(C2rCfm {
sid: result.sid,
cid: result.client_id,
did: result.camera_id,
rsp: 0,
conn: "local".to_string(),
}),
};
self.send_and_forget(msg, register_result.reg).await?;
@@ -723,17 +686,13 @@ impl Discoverer {
UdpDiscovery {
tid,
payload:
UdpXml {
d2c_t:
Some(D2cT {
sid,
cid,
did,
conn,
..
}),
UdpXml::D2cT(D2cT {
sid,
cid,
did,
conn,
..
},
}),
},
RegisterResult {
dmap: register_dmap,
@@ -757,16 +716,13 @@ impl Discoverer {
let msg = UdpDiscovery {
tid: local_tid,
payload: UdpXml {
c2d_a: Some(C2dA {
sid: register_result.sid,
conn: "map".to_string(),
cid: register_result.client_id,
did: local_did,
mtu: MTU,
}),
..Default::default()
},
payload: UdpXml::C2dA(C2dA {
sid: register_result.sid,
conn: "map".to_string(),
cid: register_result.client_id,
did: local_did,
mtu: MTU,
}),
};
let permit = self
@@ -782,17 +738,13 @@ impl Discoverer {
UdpDiscovery {
tid: _,
payload:
UdpXml {
d2c_cfm:
Some(D2cCfm {
sid,
cid,
did,
conn,
..
}),
UdpXml::D2cCfm(D2cCfm {
sid,
cid,
did,
conn,
..
},
}),
} if sid == register_result.sid
&& did == local_did
&& cid == register_result.client_id
@@ -815,16 +767,13 @@ impl Discoverer {
// Confirm map to register
let msg = UdpDiscovery {
tid: 0,
payload: UdpXml {
c2r_cfm: Some(C2rCfm {
sid: result.sid,
cid: result.client_id,
did: result.camera_id,
rsp: 0,
conn: "map".to_string(),
}),
..Default::default()
},
payload: UdpXml::C2rCfm(C2rCfm {
sid: result.sid,
cid: result.client_id,
did: result.camera_id,
rsp: 0,
conn: "map".to_string(),
}),
};
self.send_and_forget(msg, register_result.reg).await?;
@@ -845,26 +794,19 @@ impl Discoverer {
let dev_addr = register_result.dev.ok_or(Error::NoDev)?;
let msg = UdpDiscovery {
tid,
payload: UdpXml {
c2d_t: Some(C2dT {
sid: register_result.sid,
cid: register_result.client_id,
mtu: MTU,
conn: "local".to_string(),
}),
..Default::default()
},
payload: UdpXml::C2dT(C2dT {
sid: register_result.sid,
cid: register_result.client_id,
mtu: MTU,
conn: "local".to_string(),
}),
};
let (final_addr, local_did) = self
.retry_send(msg, dev_addr, |bc, addr| match bc {
UdpDiscovery {
tid: _,
payload:
UdpXml {
d2c_cfm: Some(D2cCfm { cid, did, sid, .. }),
..
},
payload: UdpXml::D2cCfm(D2cCfm { cid, did, sid, .. }),
} if cid == register_result.client_id && sid == register_result.sid => {
Some((addr, did))
}
@@ -888,16 +830,13 @@ impl Discoverer {
// Confirm local to register
let msg = UdpDiscovery {
tid: 0,
payload: UdpXml {
c2r_cfm: Some(C2rCfm {
sid: result.sid,
cid: result.client_id,
did: result.camera_id,
conn: "local".to_string(),
rsp: 0,
}),
..Default::default()
},
payload: UdpXml::C2rCfm(C2rCfm {
sid: result.sid,
cid: result.client_id,
did: result.camera_id,
conn: "local".to_string(),
rsp: 0,
}),
};
self.send_and_forget(msg, register_result.reg).await?;
@@ -918,15 +857,12 @@ impl Discoverer {
let relay_addr = register_result.relay.ok_or(Error::NoDev)?;
let msg = UdpDiscovery {
tid,
payload: UdpXml {
c2d_t: Some(C2dT {
sid: register_result.sid,
cid: register_result.client_id,
mtu: MTU,
conn: "relay".to_string(),
}),
..Default::default()
},
payload: UdpXml::C2dT(C2dT {
sid: register_result.sid,
cid: register_result.client_id,
mtu: MTU,
conn: "relay".to_string(),
}),
};
let permit = self
@@ -940,17 +876,13 @@ impl Discoverer {
UdpDiscovery {
tid: _,
payload:
UdpXml {
d2c_cfm:
Some(D2cCfm {
cid,
did,
sid,
conn,
..
}),
UdpXml::D2cCfm(D2cCfm {
cid,
did,
sid,
conn,
..
},
}),
} if cid == register_result.client_id
&& sid == register_result.sid
&& &conn == "relay" =>
@@ -971,16 +903,13 @@ impl Discoverer {
// Confirm relay to register
let msg = UdpDiscovery {
tid: 0,
payload: UdpXml {
c2r_cfm: Some(C2rCfm {
sid: result.sid,
cid: result.client_id,
did: result.camera_id,
conn: "relay".to_string(),
rsp: 0,
}),
..Default::default()
},
payload: UdpXml::C2rCfm(C2rCfm {
sid: result.sid,
cid: result.client_id,
did: result.camera_id,
conn: "relay".to_string(),
rsp: 0,
}),
};
self.send_and_forget(msg, register_result.reg).await?;
@@ -1009,13 +938,10 @@ impl Discoverer {
interval.tick().await;
let msg = BcUdp::Discovery(UdpDiscovery {
tid,
payload: UdpXml {
c2d_hb: Some(C2dHb {
payload: UdpXml::C2dHb(C2dHb {
cid: client_id,
did: camera_id,
}),
..Default::default()
},
});
if sender.send((msg, addr)).await.is_err() {
break Result::Ok(());
@@ -1044,14 +970,11 @@ impl Discoverer {
interval.tick().await;
let msg = BcUdp::Discovery(UdpDiscovery {
tid,
payload: UdpXml {
c2r_hb: Some(C2rHb {
payload: UdpXml::C2rHb(C2rHb {
sid,
cid: client_id,
did: camera_id,
}),
..Default::default()
},
});
if sender.send((msg, addr)).await.is_err() {
break Result::Ok(());
@@ -1167,7 +1090,7 @@ impl Discovery {
) -> Result<DiscoveryResult> {
let mut dests = get_broadcasts(&[2015, 2018])?;
if let Some(mut optional_addrs) = optional_addrs.take() {
trace!("Also sending to {:?}", optional_addrs);
debug!("Also sending to {:?}", optional_addrs);
dests.append(&mut optional_addrs);
}
let discoverer_ref = &self.discoverer;
@@ -384,13 +384,10 @@ impl UdpPayloadInner {
let mut rng = thread_rng();
(rng.gen::<u8>()) as u32
},
payload: UdpXml {
c2d_hb: Some(C2dHb {
payload: UdpXml::C2dHb(C2dHb {
cid: thread_client_id,
did: thread_camera_id,
}),
..Default::default()
},
});
let _ = tokio::time::timeout(tokio::time::Duration::from_millis(250), inner.send((msg, thread_camera_addr))).await;
}
@@ -449,13 +446,10 @@ impl UdpPayloadInner {
let mut rng = thread_rng();
(rng.gen::<u8>()) as u32
},
payload: UdpXml {
c2d_hb: Some(C2dHb {
payload: UdpXml::C2dHb(C2dHb {
cid: thread_client_id,
did: thread_camera_id,
}),
..Default::default()
},
});
if thread_sender.send(msg).await.is_err() {
break Result::Ok(());
+52 -57
View File
@@ -1,33 +1,33 @@
use super::bc::model::Bc;
use crate::NomErrorType;
use err_derive::Error;
use thiserror::Error;
/// This is the primary error type of the library
#[derive(Debug, Error, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum Error {
/// Underlying IO errors
#[error(display = "IO Error: {:?}", _0)]
Io(#[error(source)] std::sync::Arc<std::io::Error>),
#[error("IO Error: {:?}", _0)]
Io(#[from] std::sync::Arc<std::io::Error>),
/// Raised when fails to parse time from the camera
#[error(display = "Error in time coversion: {:?}", _0)]
TimeRange(#[error(source)] time::error::ComponentRange),
#[error("Error in time coversion: {:?}", _0)]
TimeRange(#[from] time::error::ComponentRange),
/// Raised when fails to parse time from the camera
#[error(display = "Error in time parsing")]
#[error("Error in time parsing")]
TimeParse,
/// Raised when fails to parse time from the camera
#[error(display = "Error in try from NonZeroInt")]
TryFromInt(#[error(source)] std::num::TryFromIntError),
#[error("Error in try from NonZeroInt")]
TryFromInt(#[from] std::num::TryFromIntError),
/// /// Raised when fails to parse time from the camera
#[error(display = "Error in time conversion")]
TimeTryFrom(#[error(source)] time::error::TryFromParsed),
#[error("Error in time conversion")]
TimeTryFrom(#[from] time::error::TryFromParsed),
/// Raised when a Bc reply was not understood
#[error(display = "Communication error")]
#[error("Communication error")]
UnintelligibleReply {
/// The Bc packet that was not understood
reply: std::sync::Arc<Box<Bc>>,
@@ -36,140 +36,135 @@ pub enum Error {
},
/// Raised when the camera responds with a status code over than OK
#[error(display = "Camera responded with Service Unavailable: {}", _0)]
#[error("Camera responded with Service Unavaliable: {0}")]
CameraServiceUnavailable(u16),
/// Raised when the camera responds with a status code over than OK during login
#[error(display = "Camera responded with Err during login")]
#[error("Camera responded with Err during login")]
CameraLoginFail,
/// Raised when a connection is dropped.
#[error(display = "Dropped connection")]
#[error("Dropped connection")]
DroppedConnection,
/// Raised when a connection is dropped during a tokio mpsc TryRecv event
#[error(display = "Dropped connection (TryRecv)")]
DroppedConnectionTry(#[error(source)] tokio::sync::mpsc::error::TryRecvError),
#[error("Dropped connection (TryRecv)")]
DroppedConnectionTry(#[from] tokio::sync::mpsc::error::TryRecvError),
/// Raised when a connection is dropped during a TryRecv event
#[error(display = "Dropped connection (Broadcast TryRecv)")]
BroadcastDroppedConnectionTry(#[error(source)] tokio::sync::broadcast::error::TryRecvError),
#[error("Dropped connection (Broadcast TryRecv)")]
BroadcastDroppedConnectionTry(#[from] tokio::sync::broadcast::error::TryRecvError),
/// Raised when a connection is dropped during a TryRecv event
#[error(display = "Send Error")]
#[error("Send Error")]
TokioBcSendError,
/// Raised when the TIMEOUT is reach
#[error(display = "Timeout")]
Timeout(#[error(source)] std::sync::Arc<tokio::time::error::Elapsed>),
#[error("Timeout")]
Timeout(#[from] std::sync::Arc<tokio::time::error::Elapsed>),
/// Raised when a timeout fails in a non standard way such as timeout during shutdown
#[error(display = "TimeoutError")]
TimeoutError(#[error(source)] tokio::time::error::Error),
#[error("TimeoutError")]
TimeoutError(#[from] tokio::time::error::Error),
/// Raised when connection is dropped because the timeout is reach
#[error(display = "Dropped connection (Timeout)")]
#[error("Dropped connection (Timeout)")]
TimeoutDisconnected,
/// Raised when a camera cannot be connected to ay any of the given addresses
#[error(display = "Cannot contact camera at given address")]
#[error("Cannot contact camera at given address")]
CannotInitCamera,
/// Raised when failed to login to the camera
#[error(display = "Credential error")]
#[error("Credential error")]
AuthFailed,
/// Raised when the given camera url could not be resolved
#[error(display = "Failed to translate camera address")]
#[error("Failed to translate camera address")]
AddrResolutionError,
/// Raised non adpcm data is sent to the talk command
#[error(display = "Talk data is not ADPCM")]
#[error("Talk data is not ADPCM")]
UnknownTalkEncoding,
/// Raised when dicovery times out waiting for a reply
#[error(display = "Timed out while waiting for camera reply")]
#[error("Timed out while waiting for camera reply")]
DiscoveryTimeout,
/// Raised during a (de)seralisation error
#[error(display = "Cookie GenError")]
GenError(#[error(source)] std::sync::Arc<cookie_factory::GenError>),
#[error("Cookie GenError")]
GenError(#[from] std::sync::Arc<cookie_factory::GenError>),
/// Raised when a connection is subscrbed to more than once for msg_num
#[error(display = "Simultaneous subscription, {:?}", _0)]
#[error("Simultaneous subscription, {msg_num:?}")]
SimultaneousSubscription {
/// The message number that was subscribed to
msg_num: Option<u16>,
},
/// Raised when a connection is subscrbed to more than once for msg_id
#[error(display = "Simultaneous subscription, {}", _0)]
#[error("Simultaneous subscription, {msg_id}")]
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)]
#[error("Unknown encryption: {0:x?}")]
UnknownEncryption(usize),
/// Raised when the camera cannot be found
#[error(display = "Camera Not Findable")]
#[error("Camera Not Findable")]
ConnectionUnavailable,
/// Raised when the subscription id dropped too soon
#[error(display = "Dropped Subscriber")]
#[error("Dropped Subscriber")]
DroppedSubscriber,
/// Raised when a unknown connection ID attempts to connect with us over UDP
#[error(display = "Connection with unknown connectionID: {:?}", _0)]
#[error("Connection with unknown connectionID: {0:?}")]
UnknownConnectionId(i32),
/// Raised when a unknown SocketAddr attempts to connect with us over UDP
#[error(display = "Connection from unknown source: {:?}", _0)]
#[error("Connection from unknown source: {0:?}")]
UnknownSource(std::net::SocketAddr),
/// Raised when the IP/Hostname cannot be understood
#[error(display = "Could not parse as IP")]
AddrParseError(#[error(source)] std::net::AddrParseError),
#[error("Could not parse as IP")]
AddrParseError(#[from] std::net::AddrParseError),
/// Raised when a relay connection is not possible
/// usually happens if the camera has not contacted reolink yet
#[error(display = "Cannot perform relay connection with this camera")]
#[error("Cannot perform relay connection with this camera")]
NoDmap,
/// Raised when a dev connection is not possible
/// usually happens if the camera has not contacted reolink yet
#[error(display = "Cannot perform lookup with this camera against reolink servers")]
#[error("Cannot perform lookup with this camera against reolink servers")]
NoDev,
/// Raised when a discovery fails to be accepted by the register
#[error(display = "Register refuses to accept us")]
#[error("Register refuses to accept us")]
RegisterError,
/// Raised when a the relay terminates the connection by sending a R2C_DISC
#[error(display = "Relay terminated the connection")]
#[error("Relay terminated the connection")]
RelayTerminate,
/// Raised when a the camera terminates the connection by sending a D2C_DISC
#[error(display = "Camera terminated the connection")]
#[error("Camera terminated the connection")]
CameraTerminate,
/// Raised when the stream is not enough to complete a message
#[error(display = "Nom Parsing incomplete: {}", _0)]
#[error("Nom Parsing incomplete: {0}")]
NomIncomplete(usize),
/// Raised when a stream cannot be decoded
#[error(display = "Nom Parsing error: {}", _0)]
#[error("Nom Parsing error: {0}")]
NomError(String),
/// Raised when a camera/user lacks an ability
#[error(
display = "Missing ability: {} with {} permission has only {}",
name,
requested,
actual
)]
#[error("Missing ability: {name} with {requested} permission has only {actual}")]
MissingAbility {
/// Name of the ability
name: String,
@@ -180,15 +175,15 @@ pub enum Error {
},
/// Raised when a thread panics
#[error(display = "Thread panicked")]
JoinError(#[error(source)] std::sync::Arc<tokio::task::JoinError>),
#[error("Thread panicked")]
JoinError(#[from] std::sync::Arc<tokio::task::JoinError>),
/// A generic catch all error
#[error(display = "Other error: {}", _0)]
#[error("Other error: {0}")]
Other(&'static str),
/// A generic catch all error
#[error(display = "Other error: {}", _0)]
#[error("Other error: {0}")]
OtherString(String),
}
-1
View File
@@ -405,7 +405,6 @@ impl BcCamera {
expected_stream_end += play_length;
}
let _ = sub.recv().await?;
}
// Chunks are still being played, while talk_stop will interrupt them. Wait until we expect
+1 -1
View File
@@ -50,7 +50,7 @@ impl Decoder for BcMediaCodex {
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>> {
loop {
match { BcMedia::deserialize(src) } {
match BcMedia::deserialize(src) {
Ok(bc) => {
if self.amount_skipped > 0 {
debug!("Amount skipped to restore stream: {}", self.amount_skipped);
+31 -8
View File
@@ -1,4 +1,3 @@
use super::model::BcMediaIframe;
use super::model::*;
use crate::Error;
use bytes::{Buf, BytesMut};
@@ -332,14 +331,20 @@ mod tests {
]
.concat();
let mut buf = BytesMut::from(&sample[..]);
// Should derealise all of this
loop {
let e = BcMedia::deserialize(&mut BytesMut::from(&sample[..]));
let e = BcMedia::deserialize(&mut buf);
match e {
Err(Error::Io(e)) if e.kind() == ErrorKind::UnexpectedEof => {
// Reach end of files
break;
}
Err(Error::NomIncomplete(_)) if buf.is_empty() => {
// EOF still (but parser looking for next magic)
break;
}
Err(e) => {
error!("{:?}", e);
panic!();
@@ -365,14 +370,19 @@ mod tests {
]
.concat();
let mut buf = BytesMut::from(&sample[..]);
// Should derealise all of this
loop {
let e = BcMedia::deserialize(&mut BytesMut::from(&sample[..]));
let e = BcMedia::deserialize(&mut buf);
match e {
Err(Error::Io(e)) if e.kind() == ErrorKind::UnexpectedEof => {
// Reach end of files
break;
}
Err(Error::NomIncomplete(_)) if buf.is_empty() => {
// EOF still (but parser looking for next magic)
break;
}
Err(e) => {
error!("{:?}", e);
panic!();
@@ -411,14 +421,20 @@ mod tests {
]
.concat();
let mut buf = BytesMut::from(&sample[..]);
// Should derealise all of this
loop {
let e = BcMedia::deserialize(&mut BytesMut::from(&sample[..]));
let e = BcMedia::deserialize(&mut buf);
match e {
Err(Error::Io(e)) if e.kind() == ErrorKind::UnexpectedEof => {
// Reach end of files
break;
}
Err(Error::NomIncomplete(_)) if buf.is_empty() => {
// EOF still (but parser looking for next magic)
break;
}
Err(e) => {
error!("{:?}", e);
panic!();
@@ -435,7 +451,9 @@ mod tests {
let sample = include_bytes!("samples/info_v1.raw");
let e = BcMedia::deserialize(&mut BytesMut::from(&sample[..]));
let mut buf = BytesMut::from(&sample[..]);
let e = BcMedia::deserialize(&mut buf);
assert!(matches!(
e,
Ok(BcMedia::InfoV1(BcMediaInfoV1 {
@@ -471,7 +489,9 @@ mod tests {
]
.concat();
let e = BcMedia::deserialize(&mut BytesMut::from(&sample[..]));
let mut buf = BytesMut::from(&sample[..]);
let e = BcMedia::deserialize(&mut buf);
if let Ok(BcMedia::Iframe(BcMediaIframe {
video_type: VideoType::H264,
microseconds: 3557705112,
@@ -495,7 +515,9 @@ mod tests {
]
.concat();
let e = BcMedia::deserialize(&mut BytesMut::from(&sample[..]));
let mut buf = BytesMut::from(&sample[..]);
let e = BcMedia::deserialize(&mut buf);
if let Ok(BcMedia::Pframe(BcMediaPframe {
video_type: VideoType::H264,
microseconds: 3557767112,
@@ -513,8 +535,9 @@ mod tests {
init();
let sample = include_bytes!("samples/adpcm_0.raw");
let mut buf = BytesMut::from(&sample[..]);
let e = BcMedia::deserialize(&mut BytesMut::from(&sample[..]));
let e = BcMedia::deserialize(&mut buf);
if let Ok(BcMedia::Adpcm(BcMediaAdpcm { data: d })) = e {
assert_eq!(d.len(), 244);
} else {
+3 -7
View File
@@ -48,20 +48,16 @@ impl Decoder for BcUdpCodex {
if src.is_empty() {
return Ok(None);
}
match { BcUdp::deserialize(src) } {
match BcUdp::deserialize(src) {
Ok(BcUdp::Discovery(UdpDiscovery {
payload: UdpXml {
r2c_disc: Some(_), ..
},
payload: UdpXml::R2cDisc(_),
..
})) => {
log::trace!(" Decoding: Relay terminate");
Err(Error::RelayTerminate)
}
Ok(BcUdp::Discovery(UdpDiscovery {
payload: UdpXml {
d2c_disc: Some(_), ..
},
payload: UdpXml::D2cDisc(_),
..
})) => {
log::trace!(" Decoding:Camera terminate");
+18 -24
View File
@@ -11,10 +11,10 @@ use nom::{
type IResult<I, O, E = nom::error::VerboseError<I>> = Result<(I, O), nom::Err<E>>;
fn make_error<I, E: ParseError<I>>(input: I, ctx: &'static str, kind: ErrorKind) -> E
fn make_error<I, E>(input: I, ctx: &'static str, kind: ErrorKind) -> E
where
I: std::marker::Copy,
E: ContextError<I>,
E: ContextError<I> + ParseError<I>,
{
E::add_context(input, ctx, E::from_error_kind(input, kind))
}
@@ -74,7 +74,12 @@ fn udp_disc(buf: &[u8]) -> IResult<&[u8], UdpDiscovery> {
assert_eq!(checksum, actual_checksum);
let decrypted_payload = decrypt(tid, enc_data_slice);
let payload = UdpXml::try_parse(decrypted_payload.as_slice()).map_err(|_| {
log::error!(
"decrypted_payload: {:?}",
std::str::from_utf8(&decrypted_payload)
);
let payload = UdpXml::try_parse(decrypted_payload.as_slice()).map_err(|e| {
log::error!("e: {:?}", e);
Err::Error(make_error(
buf,
"DISC: Unable to decode UDPXml",
@@ -164,13 +169,10 @@ mod tests {
e,
Ok(BcUdp::Discovery(UdpDiscovery {
tid: 96,
payload: UdpXml {
c2d_disc: Some(C2dDisc {
cid: 82000,
did: 80,
}),
..
},
payload: UdpXml::C2dDisc(C2dDisc {
cid: 82000,
did: 80,
}),
}))
);
}
@@ -187,15 +189,12 @@ mod tests {
e,
Ok(BcUdp::Discovery(UdpDiscovery {
tid: 113,
payload: UdpXml {
d2c_t: Some(D2cT {
payload: UdpXml::D2cT(D2cT {
sid: 62098713,
conn: conn_str,
cid: 82001,
did: 96,
}),
..
},
})) if &conn_str == "local"
);
}
@@ -212,15 +211,12 @@ mod tests {
e,
Ok(BcUdp::Discovery(UdpDiscovery {
tid: 1101,
payload: UdpXml {
c2d_t: Some(C2dT {
payload: UdpXml::C2dT(C2dT {
sid: 62098713,
conn: conn_str,
cid: 82001,
mtu: 1350,
}),
..
},
})) if &conn_str == "local"
);
}
@@ -237,8 +233,7 @@ mod tests {
e,
Ok(BcUdp::Discovery(UdpDiscovery {
tid: 1101,
payload: UdpXml {
d2c_cfm: Some(D2cCfm {
payload: UdpXml::D2cCfm(D2cCfm {
sid: 62098713,
conn: conn_str,
rsp: 0,
@@ -246,8 +241,6 @@ mod tests {
did: 96,
time_r: 0,
}),
..
},
})) if &conn_str == "local"
);
}
@@ -307,15 +300,16 @@ mod tests {
]
.concat();
let mut buf = BytesMut::from(&sample[..]);
// Should derealise all of this
loop {
let e = BcUdp::deserialize(&mut BytesMut::from(&sample[..]));
let e = BcUdp::deserialize(&mut buf);
match e {
Err(Error::Io(e)) if e.kind() == ErrorKind::UnexpectedEof => {
// Reach end of files
break;
}
Err(Error::NomIncomplete(_)) => {
Err(Error::NomIncomplete(_)) if buf.is_empty() => {
// Reach end of files
break;
}
+4 -4
View File
@@ -96,7 +96,7 @@ mod tests {
let msg2 = BcUdp::deserialize(&mut BytesMut::from(ser_buf.as_slice())).unwrap();
assert_eq!(msg, msg2);
// Raw samples don't quite match exactly
// because the yaserde for xml puts spaces and new lines in different places
// because the serde for xml puts spaces and new lines in different places
// then the raw data from the camera so we skip this last assert
//assert_eq!(&sample[..], ser_buf.as_slice());
}
@@ -113,7 +113,7 @@ mod tests {
let msg2 = BcUdp::deserialize(&mut BytesMut::from(ser_buf.as_slice())).unwrap();
assert_eq!(msg, msg2);
// Raw samples don't quite match exactly
// because the yaserde for xml puts spaces and new lines in different places
// because the serde for xml puts spaces and new lines in different places
// then the raw data from the camera so we skip this last assert
//assert_eq!(&sample[..], ser_buf.as_slice());
}
@@ -130,7 +130,7 @@ mod tests {
let msg2 = BcUdp::deserialize(&mut BytesMut::from(ser_buf.as_slice())).unwrap();
assert_eq!(msg, msg2);
// Raw samples don't quite match exactly
// because the yaserde for xml puts spaces and new lines in different places
// because the serde for xml puts spaces and new lines in different places
// then the raw data from the camera so we skip this last assert
//assert_eq!(&sample[..], ser_buf.as_slice());
}
@@ -147,7 +147,7 @@ mod tests {
let msg2 = BcUdp::deserialize(&mut BytesMut::from(ser_buf.as_slice())).unwrap();
assert_eq!(msg, msg2);
// Raw samples don't quite match exactly
// because the yaserde for xml puts spaces and new lines in different places
// because the serde for xml puts spaces and new lines in different places
// then the raw data from the camera so we skip this last assert
//assert_eq!(&sample[..], ser_buf.as_slice());
}
+97 -75
View File
@@ -1,76 +1,93 @@
// YaSerde currently macro-expands names like __type__value from type_
use std::io::{Read, Write};
// YaSerde is currently naming the traits and the derive macros identically
use yaserde::ser::Config;
use yaserde_derive::{YaDeserialize, YaSerialize};
use serde::{Deserialize, Serialize};
use std::io::BufRead;
use std::io::Write;
/// The top level of the UDP xml is P2P
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[yaserde(rename = "P2P")]
pub struct UdpXml {
#[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Clone)]
#[serde(rename = "P2P")]
pub enum UdpXml {
/// C2D_S xml Discovery of any client
#[yaserde(rename = "C2D_S")]
pub c2d_s: Option<C2dS>,
#[serde(rename = "C2D_S")]
C2dS(C2dS),
/// C2D_S xml Discovery of client with a UID
#[yaserde(rename = "C2D_C")]
pub c2d_c: Option<C2dC>,
#[serde(rename = "C2D_C")]
C2dC(C2dC),
/// D2C_C_C xml Reply from discovery
#[yaserde(rename = "D2C_C_R")]
pub d2c_c_r: Option<D2cCr>,
#[serde(rename = "D2C_C_R")]
D2cCr(D2cCr),
/// D2C_T xml
#[yaserde(rename = "D2C_T")]
pub d2c_t: Option<D2cT>,
#[serde(rename = "D2C_T")]
D2cT(D2cT),
/// C2D_T xml
#[yaserde(rename = "C2D_T")]
pub c2d_t: Option<C2dT>,
#[serde(rename = "C2D_T")]
C2dT(C2dT),
/// D2C_CFM xml
#[yaserde(rename = "D2C_CFM")]
pub d2c_cfm: Option<D2cCfm>,
#[serde(rename = "D2C_CFM")]
D2cCfm(D2cCfm),
/// C2D_DISC xml Disconnect
#[yaserde(rename = "C2D_DISC")]
pub c2d_disc: Option<C2dDisc>,
#[serde(rename = "C2D_DISC")]
C2dDisc(C2dDisc),
/// D2C_DISC xml Disconnect
#[yaserde(rename = "D2C_DISC")]
pub d2c_disc: Option<D2cDisc>,
#[serde(rename = "D2C_DISC")]
D2cDisc(D2cDisc),
/// R2C_DISC xml Disconnect
#[yaserde(rename = "R2C_DISC")]
pub r2c_disc: Option<R2cDisc>,
#[serde(rename = "R2C_DISC")]
R2cDisc(R2cDisc),
/// C2M_Q xml client to middle man query
#[yaserde(rename = "C2M_Q")]
pub c2m_q: Option<C2mQ>,
#[serde(rename = "C2M_Q")]
C2mQ(C2mQ),
/// M2C_Q_R xml middle man to client query reply
#[yaserde(rename = "M2C_Q_R")]
pub m2c_q_r: Option<M2cQr>,
#[serde(rename = "M2C_Q_R")]
M2cQr(M2cQr),
/// C2R_C xml client to register connect
#[yaserde(rename = "C2R_C")]
pub c2r_c: Option<C2rC>,
#[serde(rename = "C2R_C")]
C2rC(C2rC),
/// R2C_T xml register to clinet with device ID etc
#[yaserde(rename = "R2C_T")]
pub r2c_t: Option<R2cT>,
#[serde(rename = "R2C_T")]
R2cT(R2cT),
/// R2C_T xml register to clinet with device ID etc handled over dmap ONLY
#[yaserde(rename = "R2C_C_R")]
pub r2c_c_r: Option<R2cCr>,
#[serde(rename = "R2C_C_R")]
R2cCr(R2cCr),
/// C2R_CFM xml client to register CFM
#[yaserde(rename = "C2R_CFM")]
pub c2r_cfm: Option<C2rCfm>,
#[serde(rename = "C2R_CFM")]
C2rCfm(C2rCfm),
/// C2D_A xml client to device accept
#[yaserde(rename = "C2D_A")]
pub c2d_a: Option<C2dA>,
#[serde(rename = "C2D_A")]
C2dA(C2dA),
/// C2D_HB xml client to device heartbeat. This is the keep alive
#[yaserde(rename = "C2D_HB")]
pub c2d_hb: Option<C2dHb>,
#[serde(rename = "C2D_HB")]
C2dHb(C2dHb),
/// C2D_HB xml client to device heartbeat. This is the keep alive
#[yaserde(rename = "C2R_HB")]
pub c2r_hb: Option<C2rHb>,
#[serde(rename = "C2R_HB")]
C2rHb(C2rHb),
}
/// The top level holder for P2P we auto add/remove this at serde
#[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Clone)]
struct P2P {
#[serde(rename = "$value")]
xml: UdpXml,
}
impl UdpXml {
pub(crate) fn try_parse(s: impl Read) -> Result<Self, String> {
yaserde::de::from_reader(s)
pub(crate) fn try_parse(s: impl BufRead) -> Result<Self, quick_xml::de::DeError> {
let p2p: Result<P2P, _> = quick_xml::de::from_reader(s);
p2p.map(|i| i.xml)
}
pub(crate) fn serialize<W: Write>(&self, w: W) -> Result<W, String> {
yaserde::ser::serialize_with_writer(self, w, &Config::default())
pub(crate) fn serialize<W: Write>(&self, mut w: W) -> Result<W, quick_xml::de::DeError> {
let mut writer = quick_xml::writer::Writer::new(&mut w);
// No header on a UdpXml
// writer.write_event(quick_xml::events::Event::Decl(
// quick_xml::events::BytesDecl::new("1.0", Some("UTF-8"), None),
// ))?;
writer
.create_element("P2P")
.write_inner_content::<_, quick_xml::de::DeError>(|writer| {
writer.write_serializable("", &self)?;
Ok(())
})?;
Ok(w)
}
}
@@ -80,14 +97,14 @@ impl UdpXml {
/// to whoever it gets this message from
///
/// It should be broadcasted to port 2015
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2dS {
/// The destination to reply to
pub to: PortList,
}
/// Port list xml
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct PortList {
/// Port to open udp connections with
pub port: u32,
@@ -97,7 +114,7 @@ pub struct PortList {
///
/// This will start a connection with any camera that has this UID
/// It should be broadcasted to port 2018
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2dC {
/// UID of the camera the client wants to connect with
pub uid: String,
@@ -110,12 +127,12 @@ pub struct C2dC {
/// Debug mode. Purpose unknown
pub debug: bool,
/// Os of the machine known values are `"MAC"`, `"WIN"`
#[yaserde(rename = "p")]
#[serde(rename = "p")]
pub os: String,
}
/// Client List xml
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct ClientList {
/// Port to start udp communication with
pub port: u32,
@@ -125,7 +142,7 @@ pub struct ClientList {
///
/// This will start a connection with any camera that has this UID
/// It should be broadcasted to port 2018
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct D2cCr {
/// Called timer but not sure what it is a timer of
pub timer: Timer,
@@ -138,7 +155,7 @@ pub struct D2cCr {
}
/// Timer provided by D2C_C_R
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct Timer {
/// Unknown
def: u32,
@@ -149,7 +166,7 @@ pub struct Timer {
}
/// C2D_DISC xml
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2dDisc {
/// The client connection ID
pub cid: i32,
@@ -158,7 +175,7 @@ pub struct C2dDisc {
}
/// D2C_DISC xml
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct D2cDisc {
/// The client connection ID
pub cid: i32,
@@ -167,14 +184,14 @@ pub struct D2cDisc {
}
/// R2C_DISC xml
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct R2cDisc {
/// The sid
pub sid: u32,
}
/// D2C_T xml
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct D2cT {
/// The camera SID
pub sid: u32,
@@ -187,7 +204,7 @@ pub struct D2cT {
}
/// C2D_T xml
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2dT {
/// The camera SID
pub sid: u32,
@@ -204,12 +221,12 @@ pub struct C2dT {
/// This is from client to a reolink middle man server
///
/// It should be sent to a reolink p2p sever on port 9999
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2mQ {
/// UID to look up
pub uid: String,
/// Os of the machine known values are `"MAC"`, `"WIN"`
#[yaserde(rename = "p")]
#[serde(rename = "p")]
pub os: String,
}
@@ -217,7 +234,7 @@ pub struct C2mQ {
///
/// This is from middle man reolink server to client
///
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct M2cQr {
/// The register server location
pub reg: IpPort,
@@ -232,7 +249,7 @@ pub struct M2cQr {
/// Used as part of M2C_Q_R to provide the host and port
///
/// of the register, relay and log servers
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct IpPort {
/// Ip of the service
pub ip: String,
@@ -255,7 +272,7 @@ impl std::convert::TryFrom<IpPort> for std::net::SocketAddr {
///
/// This is from client to the register reolink server
///
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2rC {
/// The UID to register connecition request with
pub uid: String,
@@ -270,10 +287,10 @@ pub struct C2rC {
/// Inet family. Observed values `4`
pub family: u8,
/// Os of the machine known values are `"MAC"`, `"WIN"`
#[yaserde(rename = "p")]
#[serde(rename = "p")]
pub os: String,
/// The revision. Known values None and 3
#[yaserde(rename = "r")]
#[serde(rename = "r", skip_serializing_if = "Option::is_none")]
pub revision: Option<i32>,
}
@@ -281,11 +298,13 @@ pub struct C2rC {
///
/// This is from register reolink server to clinet with device ip and did etc
///
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct R2cT {
/// The location of the camera
#[serde(skip_serializing_if = "Option::is_none")]
pub dmap: Option<IpPort>,
/// The location of the camera
#[serde(skip_serializing_if = "Option::is_none")]
pub dev: Option<IpPort>,
/// The client id
pub cid: i32,
@@ -298,13 +317,16 @@ pub struct R2cT {
/// This is from register reolink server to clinet with device ip and did etc
/// during a relay
///
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct R2cCr {
/// Dev camera location (actual local ip)
#[serde(skip_serializing_if = "Option::is_none")]
pub dev: Option<IpPort>,
/// Dmap camera location
#[serde(skip_serializing_if = "Option::is_none")]
pub dmap: Option<IpPort>,
/// The location of the relay
#[serde(skip_serializing_if = "Option::is_none")]
pub relay: Option<IpPort>,
/// The nat type. Known values `"NULL"`
pub nat: String,
@@ -319,7 +341,7 @@ pub struct R2cCr {
/// D2C_CFM xml
///
/// Device to client, with connection started from middle man server
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct D2cCfm {
/// The camera SID
pub sid: u32,
@@ -338,7 +360,7 @@ pub struct D2cCfm {
/// C2R_CFM xml
///
/// Client to register
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2rCfm {
/// The camera SID
pub sid: u32,
@@ -356,7 +378,7 @@ pub struct C2rCfm {
///
/// Client to device accept.
/// Sent it reply to a D2C_T
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2dA {
/// The camera SID
pub sid: u32,
@@ -374,7 +396,7 @@ pub struct C2dA {
///
/// Client to device heart beat.
/// Seems to act as a keep alive
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2dHb {
/// The client connection ID
pub cid: i32,
@@ -386,7 +408,7 @@ pub struct C2dHb {
///
/// Client to device heart beat.
/// Seems to act as a keep alive
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2rHb {
/// The connection ID
pub sid: u32,
+39 -7
View File
@@ -9,18 +9,50 @@
//! A camera can be initialised with
//!
//! ```no_run
//! use neolink_core::bc_protocol::BcCamera;
//! let channel_id = 0; // Usually zero but can be non zero if uses a reolink NVR
//! let mut camera = BcCamera::new_with_addr("camera_ip_address", channel_id).unwrap();
//! # tokio::runtime::Runtime::new().unwrap().block_on(async {
//! use neolink_core::bc_protocol::{BcCamera, BcCameraOpt, DiscoveryMethods, ConnectionProtocol, Credentials};
//! let options = BcCameraOpt {
//! name: "CamName".to_string(),
//! channel_id: 0,
//! addrs: ["192.168.1.1".parse().unwrap()].to_vec(),
//! port: Some(9000),
//! uid: Some("CAMUID".to_string()),
//! protocol: ConnectionProtocol::TcpUdp,
//! discovery: DiscoveryMethods::Relay,
//! credentials: Credentials {
//! username: "username".to_string(),
//! password: Some("password".to_string()),
//! },
//! debug: false,
//! max_discovery_retries: 10,
//! };
//! let mut camera = BcCamera::new(&options).await.unwrap();
//! # })
//! ```
//!
//! After that login can be conducted with
//!
//! ```no_run
//! # use neolink_core::bc_protocol::BcCamera;
//! # let channel_id = 0;
//! # let mut camera = BcCamera::new_with_addr("camera_ip_address", channel_id).unwrap();
//! camera.login("username", Some("password"));
//! # tokio::runtime::Runtime::new().unwrap().block_on(async {
//! # use neolink_core::bc_protocol::{BcCamera, BcCameraOpt, DiscoveryMethods, ConnectionProtocol, Credentials};
//! # let options = BcCameraOpt {
//! # name: "CamName".to_string(),
//! # channel_id: 0,
//! # addrs: ["192.168.1.1".parse().unwrap()].to_vec(),
//! # port: Some(9000),
//! # uid: Some("CAMUID".to_string()),
//! # protocol: ConnectionProtocol::TcpUdp,
//! # discovery: DiscoveryMethods::Relay,
//! # credentials: Credentials {
//! # username: "username".to_string(),
//! # password: Some("password".to_string()),
//! # },
//! # debug: false,
//! # max_discovery_retries: 10,
//! # };
//! # let mut camera = BcCamera::new(&options).await.unwrap();
//! camera.login().await;
//! # })
//! ```
//! For further commands see the [`bc_protocol::BcCamera`] struct.
//!
+1 -1
View File
@@ -1,7 +1,7 @@
[package]
name = "neolink_decoder"
description = "A cli decoder for the AES encrypted packets"
version = "0.0.1"
version = "0.6.3-rc.2"
authors = ["Andrew King <sheepchaan@gmail.com>"]
edition = "2018"
license = "AGPL-3.0-or-later"
+2 -2
View File
@@ -1,6 +1,6 @@
[package]
name = "pushnoti"
version = "0.1.0"
version = "0.6.3-rc.2"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -12,7 +12,7 @@ env_logger = "0.10.0"
fcm-push-listener = "2.0.1"
lazy_static = "1.4.0"
log = { version = "0.4.17", features = [ "release_max_level_debug" ] }
neolink_core = { path = "../core", version = "0.6.3-rc.1" }
neolink_core = { path = "../core", version = "0.6.3-rc.2" }
regex = "1.8.1"
serde = "1.0.163"
tokio = { version = "1.28.1", features = ["full"] }
+2
View File
@@ -15,7 +15,9 @@ use std::{
pub(crate) enum AddressOrUid {
Address(String),
#[allow(dead_code)]
Uid(String, DiscoveryMethods),
#[allow(dead_code)]
AddressWithUid(String, String, DiscoveryMethods),
}
+5 -4
View File
@@ -35,10 +35,11 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
})
.await?;
let ser = String::from_utf8(
yaserde::ser::serialize_with_writer(&state, vec![], &Default::default())
.expect("Should Ser the struct"),
)
let ser = String::from_utf8({
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &state).expect("Should Ser the struct");
buf.to_vec()
})
.expect("Should be UTF8");
println!("{}", ser);
+9 -3
View File
@@ -261,10 +261,16 @@ impl NeoInstance {
Ok(instance_rx.await?)
}
pub(crate) async fn push_notifications(&self) -> Result<WatchReceiver<Option<PushNoti>>> {
let uid = self
.run_task(|cam| Box::pin(async move { Ok(cam.uid().await?) }))
pub(crate) async fn uid(&self) -> Result<String> {
let (reply_tx, reply_rx) = oneshot();
self.camera_control
.send(NeoCamCommand::GetUid(reply_tx))
.await?;
Ok(reply_rx.await?)
}
pub(crate) async fn push_notifications(&self) -> Result<WatchReceiver<Option<PushNoti>>> {
let uid = self.uid().await?;
let (instance_tx, instance_rx) = oneshot();
self.camera_control
.send(NeoCamCommand::PushNoti(instance_tx))
+1
View File
@@ -18,6 +18,7 @@ use crate::{AnyResult, Result};
use neolink_core::bc_protocol::MotionStatus;
pub(crate) enum MdState {
#[allow(dead_code)]
Start(Instant),
Stop(Instant),
Unknown,
+30
View File
@@ -41,6 +41,7 @@ pub(crate) enum NeoCamCommand {
State(OneshotSender<NeoCamThreadState>),
GetPermit(OneshotSender<Permit>),
PushNoti(OneshotSender<WatchReceiver<Option<PushNoti>>>),
GetUid(OneshotSender<String>),
}
/// The underlying camera binding
pub(crate) struct NeoCam {
@@ -62,6 +63,7 @@ impl NeoCam {
let (stream_request_tx, stream_request_rx) = mpsc(100);
let (md_request_tx, md_request_rx) = mpsc(100);
let (state_tx, state_rx) = watch(NeoCamThreadState::Connected);
let (uid_tx, uid_rx) = watch(config.camera_uid.clone());
let set = JoinSet::new();
let users = UseCounter::new().await;
@@ -175,6 +177,14 @@ impl NeoCam {
}
).await?;
},
NeoCamCommand::GetUid(sender) => {
let mut uid_rx = uid_rx.clone();
tokio::task::spawn(async move {
let uid = uid_rx.wait_for(|v| v.is_some()).await?.clone().unwrap();
let _ = sender.send(uid);
AnyResult::Ok(())
});
},
}
}
log::debug!("Control thread Senders dropped");
@@ -275,6 +285,26 @@ impl NeoCam {
}
});
// This thread will update the UID by asking the camera.
// We cache this in the uid_rx
let uid_instance = instance.clone();
let uid_cancel = me.cancel.clone();
me.set.spawn(async move {
tokio::select! {
_ = uid_cancel.cancelled() => {
AnyResult::Ok(())
},
v = async {
let uid = uid_instance.run_task(|cam| Box::pin(async move {
let uid = cam.uid().await?;
Ok(uid)
})).await?;
uid_tx.send_replace(Some(uid));
AnyResult::Ok(())
} => v,
}
});
// Handles push notifications
let pn_root_instance = instance.subscribe().await?;
let pn_cancel = me.cancel.clone();
+5
View File
@@ -67,6 +67,11 @@ impl PushNotiThread {
let sender_id = "743639030586"; // andriod
// let sender_id = "696841269229"; // ios
// let firebase_app_id = "1:743639030586:android:86f60a4fb7143876";
// let firebase_project_id = "reolink-login";
// let firebase_api_key = "AIzaSyBEUIuWHnnOEwFahxWgQB4Yt4NsgOmkPyE";
// let vapid_key = "????";
let token_path = dirs::config_dir().map(|mut d| {
d.push("./neolink_token.toml");
d
+20 -5
View File
@@ -339,20 +339,35 @@ impl StreamData {
if let Some(encode) =
infos.iter().find(|encode| encode.name == name.to_string())
{
let bitrate_table = encode
.bitrate_table
.split(',')
.filter_map(|c| {
let i: Result<u32, _> = c.parse();
i.ok()
})
.collect::<Vec<u32>>();
let framerate_table = encode
.framerate_table
.split(',')
.filter_map(|c| {
let i: Result<u32, _> = c.parse();
i.ok()
})
.collect::<Vec<u32>>();
Ok((
[encode.resolution.width, encode.resolution.height],
encode
.bitrate_table
bitrate_table
.get(encode.default_bitrate as usize)
.copied()
.unwrap_or(encode.default_bitrate)
* 1024,
encode
.framerate_table
framerate_table
.get(encode.default_framerate as usize)
.copied()
.unwrap_or(encode.default_framerate),
encode.framerate_table.clone(),
framerate_table.clone(),
))
} else {
Ok(([0, 0], 0, 0, vec![]))
+12 -12
View File
@@ -1,19 +1,19 @@
use crate::mqtt::Discoveries;
use lazy_static::lazy_static;
use neolink_core::bc_protocol::{DiscoveryMethods, PrintFormat, StreamKind};
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::clone::Clone;
use std::collections::HashSet;
use validator::{Validate, ValidationError};
use validator::ValidationError;
use validator_derive::Validate;
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_MAXENC_SRC: Regex =
Regex::new(r"^([nN]one|[Aa][Ee][Ss]|[Bb][Cc][Ee][Nn][Cc][Rr][Yy][Pp][Tt])$").unwrap();
}
static RE_TLS_CLIENT_AUTH: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^(none|request|require)$").unwrap());
static RE_PAUSE_MODE: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(black|still|test|none)$").unwrap());
static RE_MAXENC_SRC: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^([nN]one|[Aa][Ee][Ss]|[Bb][Cc][Ee][Nn][Cc][Rr][Yy][Pp][Tt])$").unwrap()
});
#[derive(Debug, Deserialize, Serialize, Validate, Clone, PartialEq)]
pub(crate) struct Config {
@@ -37,7 +37,7 @@ pub(crate) struct Config {
pub(crate) mqtt: Option<MqttServerConfig>,
#[validate(regex(
path = "RE_TLS_CLIENT_AUTH",
path = *RE_TLS_CLIENT_AUTH,
message = "Incorrect tls auth",
code = "tls_client_auth"
))]
@@ -159,7 +159,7 @@ pub(crate) struct CameraConfig {
#[serde(default = "default_maxenc")]
#[validate(regex(
path = "RE_MAXENC_SRC",
path = *RE_MAXENC_SRC,
message = "Invalid maximum encryption method",
code = "max_encryption"
))]
@@ -212,7 +212,7 @@ pub(crate) struct CameraConfig {
#[derive(Debug, Deserialize, Serialize, Validate, Clone, PartialEq, Eq, Hash)]
pub(crate) struct UserConfig {
#[validate(custom = "validate_username")]
#[validate(custom(function = "validate_username"))]
#[serde(alias = "username")]
pub(crate) name: String,
@@ -329,7 +329,7 @@ pub(crate) struct PauseConfig {
#[serde(default = "default_pause_mode")]
#[validate(regex(
path = "RE_PAUSE_MODE",
path = *RE_PAUSE_MODE,
message = "Incorrect pause mode",
code = "mode"
))]
+11 -11
View File
@@ -1,16 +1,16 @@
use err_derive::Error;
use thiserror::Error;
#[derive(Debug, Error)]
#[allow(clippy::large_enum_variant)]
pub enum Error {
#[error(display = "RTSP Error")]
Rtsp(#[error(source)] super::rtsp::Error),
#[error(display = "Status LED Error")]
StatusLight(#[error(source)] super::statusled::Error),
#[error(display = "Reboot Error")]
Reboot(#[error(source)] super::reboot::Error),
#[error(display = "Talk Error")]
Talk(#[error(source)] super::talk::Error),
#[error(display = "Talk Error")]
Mqtt(#[error(source)] super::mqtt::Error),
#[error("RTSP Error")]
Rtsp(#[from] super::rtsp::Error),
#[error("Status LED Error")]
StatusLight(#[from] super::statusled::Error),
#[error("Reboot Error")]
Reboot(#[from] super::reboot::Error),
#[error("Talk Error")]
Talk(#[from] super::talk::Error),
#[error("Talk Error")]
Mqtt(#[from] super::mqtt::Error),
}
+4 -2
View File
@@ -1,7 +1,9 @@
use std::path::Path;
use anyhow::{anyhow, Context, Result};
use gstreamer::{parse_launch, prelude::*, ClockTime, MessageView, Pipeline, State};
use gstreamer::{
parse::launch_full, prelude::*, ClockTime, MessageView, ParseFlags, Pipeline, State,
};
use gstreamer_app::AppSrc;
use tokio::{
sync::{
@@ -203,7 +205,7 @@ fn create_pipeline(format: VidFormat, file_path: &Path) -> Result<Pipeline> {
// 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)
let pipeline = launch_full(&launch_str, None, ParseFlags::empty())
.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")
-16
View File
@@ -53,21 +53,9 @@ mod utils;
use cmdline::{Command, Opt};
use common::NeoReactor;
use config::Config;
use console_subscriber as _;
pub(crate) type AnyResult<T> = Result<T, anyhow::Error>;
#[cfg(tokio_unstable)]
fn tokio_console_enable() {
info!("Tokio Console Enabled");
console_subscriber::init();
}
#[cfg(not(tokio_unstable))]
fn tokio_console_enable() {
debug!("Tokio Console Disabled");
}
#[tokio::main]
async fn main() -> Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
@@ -91,10 +79,6 @@ async fn main() -> Result<()> {
.validate()
.with_context(|| format!("Failed to validate the {:?} config file", conf_path))?;
if config.tokio_console {
tokio_console_enable();
}
let neo_reactor = NeoReactor::new(config.clone()).await;
match opt.cmd {
+17 -11
View File
@@ -123,7 +123,7 @@ pub(crate) async fn main(_: Opt, reactor: NeoReactor) -> Result<()> {
config_names = thread_config.borrow().clone().cameras.iter().filter(|a| a.enabled).map(|cam_config| cam_config.name.clone()).collect::<HashSet<_>>();
for name in config_names.iter() {
log::info!("{name}: MQTT Staring");
log::info!("{name}: MQTT Starting");
if ! cameras.contains_key(name) {
let local_cancel = CancellationToken::new();
cameras.insert(name.clone(),local_cancel.clone());
@@ -565,7 +565,7 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
} => v,
// Handle the floodlight task activation
v = async {
let flt_status = camera_floodlight_tasks.run_task(|cam| Box::pin(async move {
let flt_status = camera_floodlight_tasks.run_passive_task(|cam| Box::pin(async move {
Ok(cam.is_flightlight_tasks_enabled().await?)
})).await;
if flt_status.is_err() {
@@ -1156,9 +1156,11 @@ async fn handle_mqtt_message(
"FAIL"
}
Ok(xml) => {
let bytes_res =
yaserde::ser::serialize_with_writer(&xml, vec![], &Default::default());
match bytes_res {
let ser_xml = {
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &xml).map(|_| buf.to_vec())
};
match ser_xml {
Ok(bytes) => match String::from_utf8(bytes) {
Ok(str) => {
mqtt.send_message("status/battery", &str, false)
@@ -1200,9 +1202,11 @@ async fn handle_mqtt_message(
"FAIL"
}
Ok(xml) => {
let bytes_res =
yaserde::ser::serialize_with_writer(&xml, vec![], &Default::default());
match bytes_res {
let ser_xml = {
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &xml).map(|_| buf.to_vec())
};
match ser_xml {
Ok(bytes) => match String::from_utf8(bytes) {
Ok(str) => {
mqtt.send_message("status/pir", &str, false)
@@ -1245,9 +1249,11 @@ async fn handle_mqtt_message(
"FAIL"
}
Ok(xml) => {
let bytes_res =
yaserde::ser::serialize_with_writer(&xml, vec![], &Default::default());
match bytes_res {
let ser_xml = {
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &xml).map(|_| buf.to_vec())
};
match ser_xml {
Ok(bytes) => match String::from_utf8(bytes) {
Ok(str) => {
mqtt.send_message("status/ptz", &str, false)
+4 -4
View File
@@ -6,8 +6,8 @@ use anyhow::{anyhow, Context, Result};
use futures::future::FutureExt;
use log::*;
use rumqttc::{
AsyncClient, ConnectReturnCode, Event, Incoming, Key, LastWill, MqttOptions, QoS,
TlsConfiguration, Transport,
AsyncClient, ConnectReturnCode, Event, Incoming, LastWill, MqttOptions, QoS, TlsConfiguration,
Transport,
};
use std::sync::Arc;
use tokio::task::JoinSet;
@@ -140,7 +140,7 @@ impl<'a> MqttBackend<'a> {
if let (Ok(cert_buf), Ok(key_buf)) =
(std::fs::read(cert_path), std::fs::read(key_path))
{
Some((cert_buf, Key::RSA(key_buf)))
Some((cert_buf, key_buf))
} else {
error!("Failed to set client tls");
None
@@ -534,7 +534,7 @@ impl LastWillMqtt {
if let (Ok(cert_buf), Ok(key_buf)) =
(std::fs::read(cert_path), std::fs::read(key_path))
{
Some((cert_buf, Key::RSA(key_buf)))
Some((cert_buf, key_buf))
} else {
error!("Failed to set client tls");
None
+5 -2
View File
@@ -47,8 +47,11 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
})
.await?;
let pir_ser = String::from_utf8(
yaserde::ser::serialize_with_writer(&pir_state, vec![], &Default::default())
.expect("Should Ser the struct"),
{
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &pir_state).map(|_| buf.to_vec())
}
.expect("Should Ser the struct"),
)
.expect("Should be UTF8");
println!("{}", pir_ser);
+25 -12
View File
@@ -1,7 +1,6 @@
use anyhow::{anyhow, Context, Result};
use gstreamer::{prelude::*, Bin, Caps, Element, ElementFactory, GhostPad};
use gstreamer_app::{AppSrc, AppSrcCallbacks, AppStreamType};
use log::*;
use tokio::sync::mpsc::{channel as mpsc, Receiver as MpscReceiver};
use crate::{
@@ -173,7 +172,7 @@ fn build_h264(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
source.set_block(false);
source.set_min_latency(0);
source.set_property("emit-signals", false);
source.set_max_bytes(buffer_size as u64);
source.set_max_bytes(buffer_size as u64 * 3);
source.set_do_timestamp(true);
source.set_stream_type(AppStreamType::Seekable);
@@ -208,7 +207,7 @@ fn build_h265(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
source.set_block(false);
source.set_min_latency(0);
source.set_property("emit-signals", false);
source.set_max_bytes(buffer_size as u64);
source.set_max_bytes(buffer_size as u64 * 3);
source.set_do_timestamp(true);
source.set_stream_type(AppStreamType::Seekable);
@@ -244,7 +243,7 @@ fn build_aac(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
source.set_block(false);
source.set_min_latency(0);
source.set_property("emit-signals", false);
source.set_max_bytes(buffer_size as u64);
source.set_max_bytes(buffer_size as u64 * 3);
source.set_do_timestamp(true);
source.set_stream_type(AppStreamType::Seekable);
@@ -316,7 +315,7 @@ fn build_adpcm(bin: &Element, block_size: u32, stream_config: &StreamConfig) ->
source.set_block(false);
source.set_min_latency(0);
source.set_property("emit-signals", false);
source.set_max_bytes(buffer_size as u64);
source.set_max_bytes(buffer_size as u64 * 3);
source.set_do_timestamp(true);
source.set_stream_type(AppStreamType::Seekable);
@@ -342,7 +341,6 @@ fn build_adpcm(bin: &Element, block_size: u32, stream_config: &StreamConfig) ->
Element::link_many([&source, &queue, &decoder])?;
Element::link_many([&encoder, &payload])?;
decoder.connect_pad_added(move |_element, pad| {
debug!("Linking encoder to decoder: {:?}", pad.caps());
let sink_pad = encoder
.static_pad("sink")
.expect("Encoder is missing its pad");
@@ -387,7 +385,9 @@ fn make_element(kind: &str, name: &str) -> AnyResult<Element> {
)
})
}
fn make_queue(name: &str, buffer_size: u32) -> AnyResult<Element> {
#[allow(dead_code)]
fn make_dbl_queue(name: &str, buffer_size: u32) -> AnyResult<Element> {
let queue = make_element("queue", &format!("queue1_{}", name))?;
queue.set_property("max-size-bytes", buffer_size);
queue.set_property("max-size-buffers", 0u32);
@@ -402,11 +402,11 @@ fn make_queue(name: &str, buffer_size: u32) -> AnyResult<Element> {
queue2.set_property("max-size-bytes", buffer_size * 2u32 / 3u32);
queue.set_property("max-size-buffers", 0u32);
queue.set_property("max-size-time", 0u64);
// queue2.set_property(
// "max-size-time",
// std::convert::TryInto::<u64>::try_into(tokio::time::Duration::from_secs(5).as_nanos())
// .unwrap_or(0),
// );
queue2.set_property(
"max-size-time",
std::convert::TryInto::<u64>::try_into(tokio::time::Duration::from_secs(5).as_nanos())
.unwrap_or(0),
);
queue2.set_property("use-buffering", false);
let bin = gstreamer::Bin::builder().name(name).build();
@@ -433,6 +433,19 @@ fn make_queue(name: &str, buffer_size: u32) -> AnyResult<Element> {
Ok(bin)
}
fn make_queue(name: &str, buffer_size: u32) -> AnyResult<Element> {
let queue = make_element("queue", &format!("queue1_{}", name))?;
queue.set_property("max-size-bytes", buffer_size);
queue.set_property("max-size-buffers", 0u32);
queue.set_property("max-size-time", 0u64);
queue.set_property(
"max-size-time",
std::convert::TryInto::<u64>::try_into(tokio::time::Duration::from_secs(5).as_nanos())
.unwrap_or(0),
);
Ok(queue)
}
fn buffer_size(bitrate: u32) -> u32 {
std::cmp::max(bitrate * 15u32 / 8u32, 4u32 * 1024u32 * 1024u32)
}
+1 -2
View File
@@ -5,7 +5,6 @@
use super::AnyResult;
use gstreamer::glib::object_subclass;
use gstreamer::glib::subclass::types::ObjectSubclass;
use gstreamer::Element;
use gstreamer::{
glib::{self, Object},
@@ -36,7 +35,7 @@ impl Default for NeoMediaFactory {
impl NeoMediaFactory {
fn new() -> Self {
let factory = Object::new::<NeoMediaFactory>();
factory.set_shared(false);
factory.set_shared(true);
factory.set_eos_shutdown(false);
factory.set_stop_on_disconnect(false);
// factory.set_publish_clock_mode(gstreamer_rtsp_server::RTSPPublishClockMode::Clock);
+1 -1
View File
@@ -7,7 +7,7 @@ use super::AnyResult;
use crate::config::*;
use anyhow::Context;
use gstreamer::glib::{self, object_subclass, subclass::types::ObjectSubclass, MainLoop, Object};
use gstreamer::glib::{self, object_subclass, MainLoop, Object};
use gstreamer_rtsp::RTSPAuthMethod;
use gstreamer_rtsp_server::{
gio::{TlsAuthenticationMode, TlsCertificate},
+62 -27
View File
@@ -7,7 +7,7 @@ use std::sync::Arc;
use tokio::{
sync::{broadcast::channel as broadcast, watch::channel as watch},
task::JoinSet,
time::{sleep, sleep_until, Duration, Instant},
time::{interval, sleep, Duration},
};
use tokio_stream::{wrappers::BroadcastStream, Stream, StreamExt};
use tokio_util::sync::CancellationToken;
@@ -430,8 +430,8 @@ async fn stream_run(
let mut thread_client_count = client_count.subscribe();
log::debug!("stream_config.fps: {}", stream_config.fps);
// let fallback_time = Duration::from_secs(3);
// let fallback_framerate =
// Duration::from_millis(1000u64 / std::cmp::max(stream_config.fps as u64, 5u64));
let framerate =
Duration::from_millis(1000u64 / std::cmp::max(stream_config.fps as u64, 5u64));
if let Some(thread_vid) = thread_vid {
set.spawn(async move {
thread_client_count.activate().await?;
@@ -442,14 +442,15 @@ async fn stream_run(
v = send_to_appsrc(
// repeat_keyframe(
frametime_stream(
hold_stream(
ensure_order(
wait_for_keyframe(
vid_data_rx,
)
)
),
framerate
),
// fallback_time,
// fallback_framerate,
// framerate,
// ),
&thread_vid) => {
v
@@ -466,6 +467,8 @@ async fn stream_run(
let thread_stream_cancel = stream_cancel.clone();
let aud_data_rx = BroadcastStream::new(aud_data_rx).filter(|f| f.is_ok()); // Filter to ignore lagged
let thread_aud = aud.clone();
let aud_framerate =
Duration::from_millis(1000u64 / std::cmp::max(stream_config.fps as u64, 5u64));
if let Some(thread_aud) = thread_aud {
set.spawn(async move {
let r = tokio::select! {
@@ -474,11 +477,12 @@ async fn stream_run(
},
v = send_to_appsrc(
frametime_stream(
hold_stream(
ensure_order(
wait_for_keyframe(
aud_data_rx
)
)
),
aud_framerate
), &thread_aud) => {
v
},
@@ -542,6 +546,7 @@ fn wait_for_keyframe<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
})
}
#[allow(dead_code)]
// Take a stream of stamped data and release them
// in waves when a new key frame is found
// this ensure that the last frame sent is always an IFrame
@@ -569,33 +574,51 @@ fn hold_stream<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
})
}
// Take a stream of stamped data and reorder it
// in case they are out of order
// this also releases frames in waves of keyframe so it should replace `hold_stream`
fn ensure_order<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
mut stream: T,
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
Box::pin(async_stream::stream! {
let mut frame_buffer: Vec<StampedData> = vec![];
while let Some(frame) = stream.next().await {
if let Ok(frame) = frame {
if ! frame.keyframe {
// Buffer until keyframe and reorder
let mut reorder_buffer = vec![];
while frame_buffer.last().is_some_and(|v| v.ts > frame.ts) {
log::debug!("Reorder: {:?}", frame.ts);
reorder_buffer.push(frame_buffer.pop().unwrap());
}
frame_buffer.push(frame);
while let Some(frame) = reorder_buffer.pop() {
frame_buffer.push(frame);
}
} else {
// On key frame flush out
for frame in frame_buffer.drain(..) {
yield Ok(frame);
}
yield(Ok(frame));
}
}
}
})
}
// Take a stream of stamped data pause until
// it is time to display it
fn frametime_stream<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
mut stream: T,
expected_frame_rate: Duration,
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
Box::pin(async_stream::stream! {
const MIN_FPS_DELTA: Duration = Duration::from_millis(1000/5);
let mut last_release = Instant::now();
let mut cached_prev_ts = None;
let mut waiter = interval(expected_frame_rate);
waiter.set_missed_tick_behavior( tokio::time::MissedTickBehavior::Skip);
while let Some(frame) = stream.next().await {
if let Ok(frame) = frame {
let curr_ts = frame.ts;
let mut prev_ts = cached_prev_ts.unwrap_or(curr_ts);
// Check if we have gone backwards
if curr_ts < prev_ts {
// If we have reset things
prev_ts = curr_ts;
}
let delta_ts = std::cmp::min(curr_ts - prev_ts, MIN_FPS_DELTA);
// log::debug!("curr_ts: {curr_ts:?}, {prev_ts:?} delta_ts: {delta_ts:?}");
sleep_until(last_release + delta_ts).await;
last_release = Instant::now();
cached_prev_ts = Some(curr_ts);
waiter.tick().await;
yield Ok(frame);
}
}
@@ -665,8 +688,17 @@ async fn send_to_appsrc<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
appsrc: &AppSrc,
) -> AnyResult<()> {
let mut rt = Duration::ZERO;
let mut wait_for_iframe = true;
while let Some(Ok(data)) = stream.next().await {
check_live(appsrc)?; // Stop if appsrc is dropped
// Start on iframes
if wait_for_iframe && !data.keyframe {
continue;
} else if wait_for_iframe {
wait_for_iframe = false;
}
if let Some(rt_i) = get_runtime(appsrc) {
rt = rt_i;
}
@@ -688,6 +720,9 @@ async fn send_to_appsrc<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
Ok(_) => Ok(()),
Err(FlowError::Flushing) => {
// Buffer is full just skip
//
// But ensure we start with an iframe to reduce gray screens
wait_for_iframe = true;
Ok(())
}
Err(e) => Err(anyhow!("Error in streaming: {e:?}")),
+3 -3
View File
@@ -2,8 +2,8 @@ use crate::AnyResult;
use anyhow::{anyhow, Context, Result};
use crossbeam_channel::{bounded, Receiver, Sender};
use gstreamer::{
element_error, parse_launch, prelude::*, Caps, ClockTime, FlowError, FlowSuccess, MessageView,
Pipeline, ResourceError, State,
element_error, parse::launch_full, prelude::*, Caps, ClockTime, FlowError, FlowSuccess,
MessageView, ParseFlags, Pipeline, ResourceError, State,
};
use gstreamer_app::{AppSink, AppSinkCallbacks};
use tokio::task::JoinSet;
@@ -159,7 +159,7 @@ fn create_pipeline(
// 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)
let pipeline = launch_full(&launch_str, None, ParseFlags::empty())
.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")
+2
View File
@@ -22,7 +22,9 @@ where
pub(crate) enum AddressOrUid {
Address(String),
#[allow(dead_code)]
Uid(String, DiscoveryMethods),
#[allow(dead_code)]
AddressWithUid(String, String, DiscoveryMethods),
}