diff --git a/README.md b/README.md index 8026ae5..948b186 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,15 @@ In the `[[cameras]]` section of the toml. Possible values are `local`, `remote`, `map`, `relay` later values implictly enable prior methods. +#### Cellular + +Cellular cameras should select `"cellular"` which only enables `map` and +`relay` since `local` and `remote` will always fail + +```toml +discovery = "cellular" +``` + ### MQTT To use mqtt you will to adjust your config file as such: @@ -240,7 +249,6 @@ passed by a script for processing. [Docker](https://hub.docker.com/r/quantumentangledandy/neolink) builds are also provided in multiple architectures. The latest tag tracks master while each - branch gets it's own tag. ```bash diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index d76487f..9b76be5 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -139,14 +139,16 @@ impl BcCamera { } } } - info!("{}: Trying TCP discovery", options.name); - for socket in sockets.drain(..) { - let channel_id: u8 = options.channel_id; - if let Ok(addr) = Discovery::check_tcp(socket, channel_id).await.map(|_| { - info!("{}: TCP Discovery success at {:?}", options.name, &socket); - socket - }) { - return Ok(CameraLocation::Tcp(addr)); + if !sockets.is_empty() { + info!("{}: Trying TCP discovery", options.name); + for socket in sockets.drain(..) { + let channel_id: u8 = options.channel_id; + if let Ok(addr) = Discovery::check_tcp(socket, channel_id).await.map(|_| { + info!("{}: TCP Discovery success at {:?}", options.name, &socket); + socket + }) { + return Ok(CameraLocation::Tcp(addr)); + } } } } @@ -176,6 +178,7 @@ impl BcCamera { DiscoveryMethods::Remote => (true, true, false, false), DiscoveryMethods::Map => (true, true, true, false), DiscoveryMethods::Relay => (true, true, true, true), + DiscoveryMethods::Cellular => (false, false, true, true), DiscoveryMethods::Debug => (false, false, false, true), }; diff --git a/crates/core/src/bc_protocol/connection/discovery.rs b/crates/core/src/bc_protocol/connection/discovery.rs index 076928c..741a7e8 100644 --- a/crates/core/src/bc_protocol/connection/discovery.rs +++ b/crates/core/src/bc_protocol/connection/discovery.rs @@ -18,6 +18,7 @@ use local_ip_address::local_ip; use log::*; use rand::{seq::SliceRandom, thread_rng, Rng}; use std::collections::{btree_map::Entry, BTreeMap}; +use std::convert::TryInto; use std::net::{Ipv4Addr, SocketAddr, ToSocketAddrs}; use std::sync::Arc; use tokio::{ @@ -35,9 +36,9 @@ use tokio_util::udp::UdpFramed; #[derive(Debug, Clone)] struct RegisterResult { reg: SocketAddr, - dev: SocketAddr, - dmap: SocketAddr, - relay: SocketAddr, + dev: Option, + dmap: Option, + relay: Option, client_id: i32, sid: u32, } @@ -463,17 +464,32 @@ impl Discoverer { }), .. }, - } if !dev.ip.is_empty() && dev.port > 0 && rsp != -1 => { + } if (dev.as_ref().map(|d| !d.ip.is_empty()).unwrap_or(false) + || dmap.as_ref().map(|d| !d.ip.is_empty()).unwrap_or(false) + || relay.as_ref().map(|d| !d.ip.is_empty()).unwrap_or(false)) + && rsp != -1 => + { Some(Ok((sid, dev, dmap, relay))) } UdpDiscovery { tid: _, payload: UdpXml { - r2c_c_r: Some(R2cCr { dev, rsp, .. }), + r2c_c_r: + Some(R2cCr { + dev, + dmap, + relay, + rsp, + .. + }), .. }, - } if !dev.ip.is_empty() && dev.port > 0 && rsp == -1 => { + } if (dev.as_ref().map(|d| !d.ip.is_empty()).unwrap_or(false) + || dmap.as_ref().map(|d| !d.ip.is_empty()).unwrap_or(false) + || relay.as_ref().map(|d| !d.ip.is_empty()).unwrap_or(false)) + && rsp == -1 => + { Some(Err(Error::RegisterError)) } _ => None, @@ -484,9 +500,9 @@ impl Discoverer { reg: lookup.reg, sid, client_id, - dev: SocketAddr::new(dev.ip.parse()?, dev.port), - dmap: SocketAddr::new(dmap.ip.parse()?, dmap.port), - relay: SocketAddr::new(relay.ip.parse()?, relay.port), + dev: dev.and_then(|d| d.try_into().ok()), + dmap: dmap.and_then(|d| d.try_into().ok()), + relay: relay.and_then(|d| d.try_into().ok()), }) } @@ -521,7 +537,10 @@ impl Discoverer { }, ) if cid == register_result.client_id && &sid == register_sid - && &addr == register_dmap + && register_dmap + .as_ref() + .map(|dmap| &addr == dmap) + .unwrap_or(false) && &conn == "local" => { Some((addr, tid, did)) @@ -635,7 +654,10 @@ impl Discoverer { }, ) if cid == register_result.client_id && &sid == register_sid - && &addr == register_dmap + && register_dmap + .as_ref() + .map(|dmap| &addr == dmap) + .unwrap_or(false) && &conn == "map" => { Some((addr, tid, did)) @@ -724,7 +746,7 @@ impl Discoverer { ) -> Result { let tid = generate_tid(); - let dev_addr = register_result.dev; + let dev_addr = register_result.dev.ok_or(Error::NoDev)?; let msg = UdpDiscovery { tid, payload: UdpXml { @@ -789,6 +811,7 @@ impl Discoverer { ) -> Result { let tid = generate_tid(); + let relay_addr = register_result.relay.ok_or(Error::NoDev)?; let msg = UdpDiscovery { tid, payload: UdpXml { @@ -803,7 +826,7 @@ impl Discoverer { }; let (final_addr, local_did) = self - .retry_send(msg, register_result.relay, |bc, addr| match bc { + .retry_send(msg, relay_addr, |bc, addr| match bc { UdpDiscovery { tid: _, payload: diff --git a/crates/core/src/bc_protocol/resolution.rs b/crates/core/src/bc_protocol/resolution.rs index f442209..32643c1 100644 --- a/crates/core/src/bc_protocol/resolution.rs +++ b/crates/core/src/bc_protocol/resolution.rs @@ -38,6 +38,10 @@ pub enum DiscoveryMethods { /// **This also enabled `Local`, `Map` and `Remote` discovery** #[serde(alias = "relay")] Relay, + /// Cellular camera only support relay and map, by choosing this option + /// only those are tried + #[serde(alias = "cellular")] + Cellular, #[doc(hidden)] #[serde(alias = "debug")] /// Used for debugging it is set to whatever the dev is currently testing diff --git a/crates/core/src/bc_protocol/stream.rs b/crates/core/src/bc_protocol/stream.rs index 902dfeb..226c309 100644 --- a/crates/core/src/bc_protocol/stream.rs +++ b/crates/core/src/bc_protocol/stream.rs @@ -91,7 +91,12 @@ impl BcCamera { mut buffer_size: usize, strict: bool, ) -> Result { - self.has_ability_rw("preview").await?; + if let Err(e) = self.has_ability_rw("preview").await { + if self.has_ability_ro("streamTable").await.is_err() { + return Err(e); + } + } + let connection = self.get_connection(); let msg_num = self.new_message_num(); @@ -256,7 +261,11 @@ impl BcCamera { /// Stop a camera from sending more stream data. pub async fn stop_video(&self, stream: StreamKind) -> Result<()> { - self.has_ability_rw("preview").await?; + if let Err(e) = self.has_ability_rw("preview").await { + if self.has_ability_ro("streamTable").await.is_err() { + return Err(e); + } + } let connection = self.get_connection(); let msg_num = self.new_message_num(); let mut sub_video = connection.subscribe(msg_num).await?; diff --git a/crates/core/src/bcudp/xml.rs b/crates/core/src/bcudp/xml.rs index 39e0ad6..1dc1f44 100644 --- a/crates/core/src/bcudp/xml.rs +++ b/crates/core/src/bcudp/xml.rs @@ -240,6 +240,17 @@ pub struct IpPort { pub port: u16, } +impl std::convert::TryFrom for std::net::SocketAddr { + type Error = crate::Error; + + fn try_from(src: IpPort) -> Result { + Ok(src + .ip + .parse::() + .map(|ip| std::net::SocketAddr::new(ip, src.port))?) + } +} + /// C2R_C xml /// /// This is from client to the register reolink server @@ -290,11 +301,11 @@ pub struct R2cT { #[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)] pub struct R2cCr { /// Dev camera location (actual local ip) - pub dev: IpPort, + pub dev: Option, /// Dmap camera location - pub dmap: IpPort, + pub dmap: Option, /// The location of the relay - pub relay: IpPort, + pub relay: Option, /// The nat type. Known values `"NULL"` pub nat: String, /// The camera SID diff --git a/sample_config.toml b/sample_config.toml index 4fb6735..da37fb6 100644 --- a/sample_config.toml +++ b/sample_config.toml @@ -61,6 +61,8 @@ address = "192.168.1.187:9000" # "remote" # Register our local ip address with reolink but only permit same network connections (useful if broadcast is not possible) # "local" # Do not contact reolink servers at all. Rely soley on local UDP broadcast based discovery # +# "cellular" # Cellular camera only support Relay and Map to speed up connecting to them this option will skip the local/remote +# # discovery = "relay" # Certain types of camera emit status messages (such as battery levels) diff --git a/src/rtsp/gst/sender.rs b/src/rtsp/gst/sender.rs index 92d9174..ad4ccc3 100644 --- a/src/rtsp/gst/sender.rs +++ b/src/rtsp/gst/sender.rs @@ -635,9 +635,12 @@ impl NeoMediaSender { async fn seek( &mut self, _original_runtime: Option, - _target_runtime: FrameTime, + target_runtime: FrameTime, _master_buffer: &NeoBuffer, ) -> AnyResult<()> { + if let Some(runtime) = self.get_raw_runtime() { + self.live_offset = target_runtime - runtime; + } self.jump_to_live().await?; Ok(()) } @@ -702,28 +705,13 @@ impl NeoMediaSender { Ok(()) } - fn get_runtime(&self) -> Option { + fn get_raw_runtime(&self) -> Option { if let Some(appsrc) = self.vid.as_ref() { if let Some(clock) = appsrc.clock() { if let Some(time) = clock.time() { if let Some(base_time) = appsrc.base_time() { let runtime = time.saturating_sub(base_time); - let res = Some( - (runtime.useconds() as FrameTime) - .saturating_add(self.live_offset) - .max(0), - ); - // debug!("base_time: {:?}", base_time); - // debug!("time: {:?}", time); - // debug!("runtime: {:?}", runtime); - // debug!("Final runtime: {:?}", res); - trace!( - "Runtime: {:?}, Offset: {:?}, Offseted Runtime: {:?}", - runtime, - self.live_offset, - res - ); - return res; + return Some(runtime.useconds() as FrameTime); } } } @@ -731,6 +719,11 @@ impl NeoMediaSender { None } + fn get_runtime(&self) -> Option { + self.get_raw_runtime() + .map(|runtime| runtime.saturating_add(self.live_offset).max(0)) + } + fn get_buftime(&self) -> Option { self.get_runtime().map(|time| self.runtime_to_buftime(time)) }