From cc2c6ffc3ba0bd37d00c60d39c72694e4929f86e Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 14 Dec 2022 20:14:44 +0700 Subject: [PATCH 01/10] Add check for gstreamer plugins --- crates/core/src/bc_protocol/motion.rs | 3 - src/rtsp/gst.rs | 116 ++++++++++++++++++++++++-- src/rtsp/mod.rs | 12 ++- 3 files changed, 118 insertions(+), 13 deletions(-) diff --git a/crates/core/src/bc_protocol/motion.rs b/crates/core/src/bc_protocol/motion.rs index 58cd7d9..bc3bf2f 100644 --- a/crates/core/src/bc_protocol/motion.rs +++ b/crates/core/src/bc_protocol/motion.rs @@ -68,9 +68,6 @@ impl MotionData { Err(e) => return Err(Error::from(e)), } } - for res in results.iter() { - log::info!("Mption: {:?}", res); - } if let Some(last) = results.last() { self.last_update = *last; } diff --git a/src/rtsp/gst.rs b/src/rtsp/gst.rs index 37c8770..014369d 100644 --- a/src/rtsp/gst.rs +++ b/src/rtsp/gst.rs @@ -9,7 +9,7 @@ pub(crate) use self::maybe_inputselect::MaybeInputSelect; // use super::adpcm::adpcm_to_pcm; // use super::errors::Error; use gstreamer::prelude::Cast; -use gstreamer::{Bin, Structure}; +use gstreamer::{Bin, ElementFactory, Structure}; use gstreamer_app::AppSrc; //use gstreamer_rtsp::RTSPLowerTrans; use anyhow::{anyhow, Context}; @@ -344,17 +344,121 @@ impl GstOutputs { impl Default for RtspServer { fn default() -> RtspServer { - Self::new() + Self::new().unwrap() } } impl RtspServer { - pub(crate) fn new() -> RtspServer { - gstreamer::init().expect("Gstreamer should not explode"); - RtspServer { + pub(crate) fn new() -> AnyResult { + gstreamer::init().context("Gstreamer failed to initialise")?; + let needed_elements = &[ + ( + "appsrc", + "app (gst-plugins-base)", + "Cannot run without an appsrc", + ), + ( + "audioconvert", + "audioconvert (gst-plugins-base)", + "Required for audio", + ), + ("rtpL16pay", "rtp (gst-plugins-good)", "Required for audio"), + ]; + let sometimes_needed_elements = &[ + ("adpcmdec", "adpcmdec", "Required for audio"), + ( + "h264parse", + "videoparsersbad (gst-plugins-bad)", + "Required for certain types of camera", + ), + ( + "h265parse", + "videoparsersbad (gst-plugins-bad)", + "Required for certain types of camera", + ), + ( + "x264enc", + "x264 (gst-plugins-ugly)", + "Required to paused certain cameras", + ), + ( + "x265enc", + "x265 (gst-plugins-bad)", + "Required to paused certain cameras", + ), + ( + "avdec_h264", + "libav (gst-libav)", + "Required to paused certain cameras", + ), + ( + "avdec_h265", + "libav (gst-libav)", + "Required to paused certain cameras", + ), + ( + "videotestsrc", + "videotestsrc (gst-plugins-base)", + "Required to paused certain cameras", + ), + ( + "imagefreeze", + "imagefreeze (gst-plugins-good)", + "Required to paused certain cameras", + ), + ( + "rtph264pay", + "rtp (gst-plugins-good)", + "Required for certain types of camera", + ), + ( + "rtph265pay", + "rtp (gst-plugins-good)", + "Required for certain types of camera", + ), + ( + "aacparse", + "audioparsers (gst-plugins-good)", + "Required for certain types of camera's audio", + ), + ( + "audiotestsrc", + "audiotestsrc (gst-plugins-base)", + "Required for pausing", + ), + ( + "decodebin", + "playback (gst-plugins-good)", + "Required for pausing", + ), + ]; + let mut fatal = false; + for (element, plugin, msg) in needed_elements.iter() { + if ElementFactory::find(element).is_none() { + error!( + "Missing required gstreamer plugin `{}` for `{}` element. {}", + plugin, element, msg + ); + fatal = true; + } + } + + for (element, plugin, msg) in sometimes_needed_elements.iter() { + if ElementFactory::find(element).is_none() { + warn!( + "Missing the gstreamer plugin `{}` for `{}` element. {}", + plugin, element, msg + ); + } + } + + if fatal { + return Err(anyhow!("Required Gstreamer Elements are missing")); + } + Ok(RtspServer { server: GstRTSPServer::new(), main_loop: glib::MainLoop::new(None, false), - } + }) } pub(crate) fn add_stream>( diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index 577f10e..a38cc4e 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -50,7 +50,7 @@ use gst::{RtspServer, TlsAuthenticationMode}; /// /// Opt is the command line options pub(crate) fn main(_opt: Opt, mut config: Config) -> Result<()> { - let rtsp = Arc::new(RtspServer::new()); + let rtsp = Arc::new(RtspServer::new()?); set_up_tls(&config, &rtsp); @@ -144,6 +144,7 @@ fn camera_main( }; let motion_timeout: Duration = Duration::from_secs_f64(config.pause.motion_timeout); + let mut ready_to_pause_print = false; loop { match camera.get_state() { StateInfo::Streaming => { @@ -153,7 +154,8 @@ fn camera_main( if can_pause { info!("Pause on disconnect"); camera.pause().map_err(CameraFailureKind::Retry)?; - } else { + } else if !ready_to_pause_print { + ready_to_pause_print = true; warn!("Not ready to pause"); } } @@ -169,7 +171,8 @@ fn camera_main( if can_pause { info!("Pause on motion"); camera.pause().map_err(CameraFailureKind::Retry)?; - } else { + } else if !ready_to_pause_print { + ready_to_pause_print = true; warn!("Not ready to pause"); } } @@ -177,7 +180,8 @@ fn camera_main( if can_pause { info!("Pause on motion (start)"); camera.pause().map_err(CameraFailureKind::Retry)?; - } else { + } else if !ready_to_pause_print { + ready_to_pause_print = true; warn!("Not ready to pause"); } } From f710f5385bc7321322012e17e796a443f18de04e Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Thu, 15 Dec 2022 10:34:44 +0700 Subject: [PATCH 02/10] Better prompt on what needs to be done --- src/rtsp/gst.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/rtsp/gst.rs b/src/rtsp/gst.rs index 014369d..338515f 100644 --- a/src/rtsp/gst.rs +++ b/src/rtsp/gst.rs @@ -453,7 +453,9 @@ impl RtspServer { } if fatal { - return Err(anyhow!("Required Gstreamer Elements are missing")); + return Err(anyhow!( + "Required Gstreamer Elements are missing. Ensure gstreamer is installed correctly" + )); } Ok(RtspServer { server: GstRTSPServer::new(), From 1c9be03fd36226c24adbe29c57b87431e7da8e58 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Thu, 15 Dec 2022 10:36:33 +0700 Subject: [PATCH 03/10] Move H265 and H264 parsing and rtp into required --- src/rtsp/gst.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/rtsp/gst.rs b/src/rtsp/gst.rs index 338515f..e1d14ff 100644 --- a/src/rtsp/gst.rs +++ b/src/rtsp/gst.rs @@ -362,9 +362,6 @@ impl RtspServer { "audioconvert (gst-plugins-base)", "Required for audio", ), - ("rtpL16pay", "rtp (gst-plugins-good)", "Required for audio"), - ]; - let sometimes_needed_elements = &[ ("adpcmdec", "adpcmdec", "Required for audio"), ( "h264parse", @@ -376,6 +373,24 @@ impl RtspServer { "videoparsersbad (gst-plugins-bad)", "Required for certain types of camera", ), + ( + "rtph264pay", + "rtp (gst-plugins-good)", + "Required for certain types of camera", + ), + ( + "rtph265pay", + "rtp (gst-plugins-good)", + "Required for certain types of camera", + ), + ( + "aacparse", + "audioparsers (gst-plugins-good)", + "Required for certain types of camera's audio", + ), + ("rtpL16pay", "rtp (gst-plugins-good)", "Required for audio"), + ]; + let sometimes_needed_elements = &[ ( "x264enc", "x264 (gst-plugins-ugly)", @@ -406,21 +421,6 @@ impl RtspServer { "imagefreeze (gst-plugins-good)", "Required to paused certain cameras", ), - ( - "rtph264pay", - "rtp (gst-plugins-good)", - "Required for certain types of camera", - ), - ( - "rtph265pay", - "rtp (gst-plugins-good)", - "Required for certain types of camera", - ), - ( - "aacparse", - "audioparsers (gst-plugins-good)", - "Required for certain types of camera's audio", - ), ( "audiotestsrc", "audiotestsrc (gst-plugins-base)", From 2d7b3e765e191e1c61c68dff81fafde9cc5b7083 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 14 Dec 2022 10:15:59 +0700 Subject: [PATCH 04/10] Change how we check for missing mqtt settings --- src/mqtt/mod.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 67e1339..9616d3b 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -18,28 +18,31 @@ mod event_cam; mod mqttc; use crate::config::{CameraConfig, Config, MqttConfig}; -use anyhow::Result; +use anyhow::{anyhow, Result}; pub(crate) use app::App; pub(crate) use cmdline::Opt; use event_cam::EventCam; pub(crate) use event_cam::Messages; use mqttc::{Mqtt, MqttReplyRef}; -/// Entry point for the reboot subcommand +/// Entry point for the mqtt subcommand /// /// Opt is the command line options pub(crate) fn main(_: Opt, config: Config) -> Result<()> { let app = App::new(); let arc_app = Arc::new(app); - let mut mqtt_count: u8 = 0; + if config.cameras.iter().all(|config| config.mqtt.is_none()) { + return Err(anyhow!( + "MQTT command run, but no cameras configured with MQTT settings. Exiting." + )); + } let _ = crossbeam::scope(|s| { for camera_config in &config.cameras { if let Some(mqtt_config) = camera_config.mqtt.as_ref() { let loop_arc_app = arc_app.clone(); info!("{}: Setting up mqtt", camera_config.name); - mqtt_count = mqtt_count + 1; s.spawn(move |_| { while loop_arc_app.running("app") { let _ = listen_on_camera(camera_config, mqtt_config, loop_arc_app.clone()); @@ -49,10 +52,6 @@ pub(crate) fn main(_: Opt, config: Config) -> Result<()> { } }); - if mqtt_count == 0 { - error!("MQTT command run, but no cameras configured with MQTT settings. Exiting."); - } - Ok(()) } From 9a524afa841048e940f7c94fdcbad1d9f34d1586 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 14 Dec 2022 11:01:20 +0700 Subject: [PATCH 05/10] Start work on new commands --- src/mqtt/event_cam.rs | 146 +++++++++++++++++++++++++++++++----------- src/mqtt/mod.rs | 33 +++++++++- 2 files changed, 141 insertions(+), 38 deletions(-) diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index 69f0ecb..47d1e3e 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -2,7 +2,7 @@ use super::App; use crate::config::CameraConfig; use crate::utils::AddressOrUid; use anyhow::{anyhow, Context, Result}; -use crossbeam_channel::{unbounded, Receiver, Sender, TryRecvError}; +use crossbeam_channel::{bounded, unbounded, Receiver, Sender, TryRecvError}; use log::*; use neolink_core::bc_protocol::{BcCamera, Error as BcError, LightState, MotionStatus}; use std::sync::{Arc, Mutex}; @@ -18,13 +18,32 @@ pub(crate) enum Messages { IRLedOn, IRLedOff, IRLedAuto, + Battery, + Ptz(Direction), +} + +pub(crate) enum Direction { + Up, + Down, + Left, + Right, + In, + Out, +} + +enum ToCamera { + Send(Messages), + SendAndReply { + message: Messages, + reply: Sender, + }, } pub(crate) struct EventCam<'a> { config: &'a CameraConfig, app: Arc, channel_out: Arc>>>, - channel_in: Arc>>>, + channel_in: Arc>>>, } impl<'a> EventCam<'a> { @@ -53,7 +72,7 @@ impl<'a> EventCam<'a> { if let Ok(ref mut channel_in) = self.channel_in.lock() { if let Some(channel_in) = channel_in.as_mut() { channel_in - .send(msg) + .send(ToCamera::Send(msg)) .context("Failed to send message from camera") } else { Err(anyhow!("Failed to send camera data over crossbeam")) @@ -63,6 +82,26 @@ impl<'a> EventCam<'a> { } } + pub(crate) fn send_message_with_reply(&self, msg: Messages) -> Result { + if let Ok(ref mut channel_in) = self.channel_in.lock() { + if let Some(channel_in) = channel_in.as_mut() { + let (tx, rx) = bounded(1); + channel_in + .send(ToCamera::SendAndReply { + message: msg, + reply: tx, + }) + .context("Failed to send message from camera")?; + rx.recv_timeout(std::time::Duration::from_secs(3)) + .context("Failed to recieve reply") + } else { + Err(anyhow!("Failed to send camera data over crossbeam")) + } + } else { + Err(anyhow!("Failed to lock")) + } + } + pub(crate) fn abort(&self) { self.app.abort(&self.config.name); } @@ -94,7 +133,7 @@ impl<'a> EventCam<'a> { fn cam_run( camera_config: &CameraConfig, tx: Arc>>, - rx: Arc>>, + rx: Arc>>, app: Arc, ) -> Result<()> { let camera_addr = @@ -196,7 +235,7 @@ impl MotionThread { struct MessageHandler { app: Arc, - rx: Arc>>, + rx: Arc>>, camera: Arc, name: String, } @@ -207,45 +246,78 @@ impl MessageHandler { // Try and lock don't worry if not if let Ok(ref mut channel_out) = self.rx.try_lock() { match channel_out.try_recv() { - Ok(message) => match message { - Messages::Reboot => { - if self.camera.reboot().is_err() { - error!("Failed to reboot the camera"); - self.abort() + Ok(to_camera) => { + let (replier, message) = match to_camera { + ToCamera::Send(message) => (None, message), + ToCamera::SendAndReply { message, reply } => (Some(reply), message), + }; + let reply = match message { + Messages::Reboot => { + if self.camera.reboot().is_err() { + error!("Failed to reboot the camera"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } } - } - Messages::StatusLedOn => { - if self.camera.led_light_set(true).is_err() { - error!("Failed to turn on the status light"); - self.abort(); + Messages::StatusLedOn => { + if self.camera.led_light_set(true).is_err() { + error!("Failed to turn on the status light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } } - } - Messages::StatusLedOff => { - if self.camera.led_light_set(false).is_err() { - error!("Failed to turn off the status light"); - self.abort(); + Messages::StatusLedOff => { + if self.camera.led_light_set(false).is_err() { + error!("Failed to turn off the status light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } } - } - Messages::IRLedOn => { - if self.camera.irled_light_set(LightState::On).is_err() { - error!("Failed to turn on the status light"); - self.abort(); + Messages::IRLedOn => { + if self.camera.irled_light_set(LightState::On).is_err() { + error!("Failed to turn on the status light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } } - } - Messages::IRLedOff => { - if self.camera.irled_light_set(LightState::Off).is_err() { - error!("Failed to turn on the status light"); - self.abort(); + Messages::IRLedOff => { + if self.camera.irled_light_set(LightState::Off).is_err() { + error!("Failed to turn on the status light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } } - } - Messages::IRLedAuto => { - if self.camera.irled_light_set(LightState::Auto).is_err() { - error!("Failed to turn on the status light"); - self.abort(); + Messages::IRLedAuto => { + if self.camera.irled_light_set(LightState::Auto).is_err() { + error!("Failed to turn on the status light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } } + Messages::Battery => { + unimplemented!() + } + Messages::Ptz(_direction) => { + unimplemented!() + } + _ => "UNKNOWN COMMAND".to_string(), + }; + if let Some(replier) = replier { + let _ = replier.send(reply); } - _ => {} - }, + } Err(TryRecvError::Empty) => (), Err(TryRecvError::Disconnected) => self.abort(), } diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 9616d3b..e264850 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -22,7 +22,7 @@ use anyhow::{anyhow, Result}; pub(crate) use app::App; pub(crate) use cmdline::Opt; use event_cam::EventCam; -pub(crate) use event_cam::Messages; +pub(crate) use event_cam::{Direction, Messages}; use mqttc::{Mqtt, MqttReplyRef}; /// Entry point for the mqtt subcommand @@ -156,6 +156,37 @@ fn listen_on_camera( error!("Failed to set camera status light off"); } } + MqttReplyRef { + topic: "control/ptz", + message, + } => { + let direction = match message.to_lowercase().as_str() { + "up" => Direction::Up, + "down" => Direction::Down, + "left" => Direction::Left, + "right" => Direction::Right, + "in" => Direction::In, + "out" => Direction::Out, + _ => { + error!("Unrecongnized PTZ direction"); + continue; + } + }; + if event_cam.send_message(Messages::Ptz(direction)).is_err() { + error!("Failed to send PTZ"); + } + } + MqttReplyRef { + topic: "query/battery", + .. + } => match event_cam.send_message_with_reply(Messages::Battery) { + Ok(reply) => { + if mqtt.send_message("status/battery", &reply, false).is_err() { + error!("Failed to send battery status reply"); + } + } + Err(_) => error!("Failed to set camera status light off"), + }, _ => {} } } From 9f3c482f5169cb08d993b3004bb5c962ef986c2a Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 14 Dec 2022 11:20:13 +0700 Subject: [PATCH 06/10] Added optional amount to ptz controls --- src/mqtt/event_cam.rs | 12 ++++++------ src/mqtt/mod.rs | 39 ++++++++++++++++++++++++++------------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index 47d1e3e..70873c0 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -23,12 +23,12 @@ pub(crate) enum Messages { } pub(crate) enum Direction { - Up, - Down, - Left, - Right, - In, - Out, + Up(f32), + Down(f32), + Left(f32), + Right(f32), + In(f32), + Out(f32), } enum ToCamera { diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index e264850..65b8907 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -160,20 +160,33 @@ fn listen_on_camera( topic: "control/ptz", message, } => { - let direction = match message.to_lowercase().as_str() { - "up" => Direction::Up, - "down" => Direction::Down, - "left" => Direction::Left, - "right" => Direction::Right, - "in" => Direction::In, - "out" => Direction::Out, - _ => { - error!("Unrecongnized PTZ direction"); - continue; + let lowercase_message = message.to_lowercase(); + let mut words = lowercase_message.split_whitespace(); + if let Some(direction_txt) = words.next() { + let amount = words.next().unwrap_or("32.0"); + if let Ok(amount) = amount.parse::() { + let direction = match direction_txt { + "up" => Direction::Up(amount), + "down" => Direction::Down(amount), + "left" => Direction::Left(amount), + "right" => Direction::Right(amount), + "in" => Direction::In(amount), + "out" => Direction::Out(amount), + _ => { + error!("Unrecongnized PTZ direction"); + continue; + } + }; + if event_cam.send_message(Messages::Ptz(direction)).is_err() { + error!("Failed to send PTZ"); + } + } else { + error!("No PTZ direction speed was not a valid number"); } - }; - if event_cam.send_message(Messages::Ptz(direction)).is_err() { - error!("Failed to send PTZ"); + } else { + error!( + "No PTZ Direction given. Please add up/down/left/right/in/out" + ); } } MqttReplyRef { From 10eecf416cae62702d6d73e99798c86f8a6aad9d Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 14 Dec 2022 11:45:15 +0700 Subject: [PATCH 07/10] Added PTZ controls --- crates/core/src/bc/model.rs | 8 ++- crates/core/src/bc/xml.rs | 22 ++++++- crates/core/src/bc_protocol.rs | 2 + crates/core/src/bc_protocol/pirstate.rs | 6 +- crates/core/src/bc_protocol/ptz.rs | 81 +++++++++++++++++++++++++ src/mqtt/event_cam.rs | 22 ++++++- 6 files changed, 130 insertions(+), 11 deletions(-) create mode 100644 crates/core/src/bc_protocol/ptz.rs diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index e19ef6b..d7de246 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -16,6 +16,8 @@ pub const MSG_ID_VIDEO_STOP: u32 = 4; pub const MSG_ID_TALKABILITY: u32 = 10; /// TalkReset messages have this ID pub const MSG_ID_TALKRESET: u32 = 11; +/// PtzControl messages have this ID +pub const MSG_ID_PTZ_CONTROL: u32 = 18; /// Reboot messages have this ID pub const MSG_ID_REBOOT: u32 = 23; /// Request motion detection messages @@ -50,7 +52,7 @@ pub const EMPTY_LEGACY_PASSWORD: &str = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; /// Top level bc message -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq)] pub struct Bc { /// header part of the message pub meta: BcMeta, @@ -61,7 +63,7 @@ pub struct Bc { /// /// The body of a bc message is either legacy or modern /// -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq)] #[allow(clippy::large_enum_variant)] pub enum BcBody { /// Legacy is unsupported except for login where it is used @@ -86,7 +88,7 @@ pub enum BcBody { /// `extension` or `payload` these are header only messages. This usually occurs to acknoledge receipt /// of a command. In such cases the header `response_code` should be checked. /// -#[derive(Debug, Default, PartialEq, Eq)] +#[derive(Debug, Default, PartialEq)] pub struct ModernMsg { /// Extension describes the following payload such as which channel id it is for pub extension: Option, diff --git a/crates/core/src/bc/xml.rs b/crates/core/src/bc/xml.rs index e5cc699..cd12fa2 100644 --- a/crates/core/src/bc/xml.rs +++ b/crates/core/src/bc/xml.rs @@ -10,7 +10,7 @@ use yaserde_derive::{YaDeserialize, YaSerialize}; use indoc::indoc; /// There are two types of payloads xml and binary -#[derive(PartialEq, Eq, Debug, YaDeserialize)] +#[derive(PartialEq, Debug, YaDeserialize)] #[yaserde(flatten)] #[allow(clippy::large_enum_variant)] pub enum BcPayloads { @@ -31,7 +31,7 @@ impl Default for BcPayloads { } /// The top level BC Xml -#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +#[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)] #[yaserde(rename = "body")] pub struct BcXml { /// Encryption xml is received during login and contain the NONCE @@ -73,6 +73,9 @@ pub struct BcXml { /// Received when motion is detected #[yaserde(rename = "AlarmEventList")] pub alarm_event_list: Option, + /// Sent to move the camera + #[yaserde(rename = "PtzControl")] + pub ptz_control: Option, } impl BcXml { @@ -445,6 +448,21 @@ pub struct AlarmEvent { pub timeStamp: i32, } +/// The Ptz messages used to move the camera +#[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)] +pub struct PtzControl { + /// XML Version + #[yaserde(attribute)] + pub version: String, + /// The channel the event occured on. Usually zero unless from an NVR + #[yaserde(rename = "channelId")] + pub channel_id: u8, + /// The amount of movement to perform + pub speed: f32, + /// The direction to transverse. Known directions: "right" + pub command: String, +} + /// Convience function to return the xml version used throughout the library pub fn xml_ver() -> String { "1.1".to_string() diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index 8b3e3c6..d3e86ba 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -17,6 +17,7 @@ mod logout; mod motion; mod ping; mod pirstate; +mod ptz; mod reboot; mod resolution; mod stream; @@ -31,6 +32,7 @@ pub use errors::Error; pub use ledstate::LightState; pub use motion::{MotionData, MotionStatus}; pub use pirstate::PirState; +pub use ptz::Direction; pub use resolution::*; use std::sync::Arc; pub use stream::{Stream, StreamData}; diff --git a/crates/core/src/bc_protocol/pirstate.rs b/crates/core/src/bc_protocol/pirstate.rs index b02f798..aae4c91 100644 --- a/crates/core/src/bc_protocol/pirstate.rs +++ b/crates/core/src/bc_protocol/pirstate.rs @@ -3,7 +3,7 @@ use crate::bc::{model::*, xml::*}; impl BcCamera { /// Get the [RfAlarmCfg] xml which contains the PIR status of the camera - pub fn get_pirstate(&mut self) -> Result { + pub fn get_pirstate(&self) -> Result { let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub_get = connection.subscribe(msg_num)?; @@ -50,7 +50,7 @@ impl BcCamera { } /// Set the PIR sensor using the [RfAlarmCfg] xml - pub fn set_pirstate(&mut self, rf_alarm_cfg: RfAlarmCfg) -> Result<()> { + pub fn set_pirstate(&self, rf_alarm_cfg: RfAlarmCfg) -> Result<()> { let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub_set = connection.subscribe(msg_num)?; @@ -97,7 +97,7 @@ impl BcCamera { /// This is a convience function to control the PIR status /// True is on and false is off - pub fn pir_set(&mut self, state: bool) -> Result<()> { + pub fn pir_set(&self, state: bool) -> Result<()> { let mut pir_state = self.get_pirstate()?; // println!("{:?}", pir_state); pir_state.enable = match state { diff --git a/crates/core/src/bc_protocol/ptz.rs b/crates/core/src/bc_protocol/ptz.rs new file mode 100644 index 0000000..252cbb2 --- /dev/null +++ b/crates/core/src/bc_protocol/ptz.rs @@ -0,0 +1,81 @@ +use super::{BcCamera, Error, Result, RX_TIMEOUT}; +use crate::bc::{model::*, xml::*}; + +/// Directions used for Ptz +pub enum Direction { + /// To move the camera Up + Up, + /// To move the camera Down + Down, + /// To move the camera Left + Left, + /// To move the camera Right + Right, + /// To zoom the camera In (may be done with cropping depending on camera model) + In, + /// To zoom the camera Out (may be done with cropping depending on camera model) + Out, +} + +impl BcCamera { + /// Send a PTZ message to the camera + pub fn send_ptz(&self, direction: Direction, amount: f32) -> Result<()> { + let connection = self.get_connection(); + let msg_num = self.new_message_num(); + let sub_set = connection.subscribe(msg_num)?; + + let direction_str = match direction { + Direction::Up => "up", + Direction::Down => "down", + Direction::Left => "left", + Direction::Right => "right", + Direction::In => { + unimplemented!() + } + Direction::Out => { + unimplemented!() + } + } + .to_string(); + let send = Bc { + meta: BcMeta { + msg_id: MSG_ID_PTZ_CONTROL, + channel_id: self.channel_id, + msg_num, + response_code: 0, + stream_type: 0, + class: 0x6414, + }, + body: BcBody::ModernMsg(ModernMsg { + extension: Some(Extension { + channel_id: Some(self.channel_id), + ..Default::default() + }), + payload: Some(BcPayloads::BcXml(BcXml { + ptz_control: Some(PtzControl { + version: xml_ver(), + channel_id: self.channel_id, + speed: amount, + command: direction_str, + }), + ..Default::default() + })), + }), + }; + + sub_set.send(send)?; + let msg = sub_set.rx.recv_timeout(RX_TIMEOUT)?; + + if let BcMeta { + response_code: 200, .. + } = msg.meta + { + Ok(()) + } else { + Err(Error::UnintelligibleReply { + reply: Box::new(msg), + why: "The camera did not accept the PtzControl xml", + }) + } + } +} diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index 70873c0..05be67c 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -4,7 +4,9 @@ use crate::utils::AddressOrUid; use anyhow::{anyhow, Context, Result}; use crossbeam_channel::{bounded, unbounded, Receiver, Sender, TryRecvError}; use log::*; -use neolink_core::bc_protocol::{BcCamera, Error as BcError, LightState, MotionStatus}; +use neolink_core::bc_protocol::{ + BcCamera, Direction as BcDirection, Error as BcError, LightState, MotionStatus, +}; use std::sync::{Arc, Mutex}; pub(crate) enum Messages { @@ -309,8 +311,22 @@ impl MessageHandler { Messages::Battery => { unimplemented!() } - Messages::Ptz(_direction) => { - unimplemented!() + Messages::Ptz(direction) => { + let (bc_direction, amount) = match direction { + Direction::Up(amount) => (BcDirection::Up, amount), + Direction::Down(amount) => (BcDirection::Down, amount), + Direction::Left(amount) => (BcDirection::Left, amount), + Direction::Right(amount) => (BcDirection::Right, amount), + Direction::In(amount) => (BcDirection::In, amount), + Direction::Out(amount) => (BcDirection::Out, amount), + }; + if self.camera.send_ptz(bc_direction, amount).is_err() { + error!("Failed to turn on the status light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } } _ => "UNKNOWN COMMAND".to_string(), }; From 90e21e991656a9bab798042cadf0176514290cea Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 14 Dec 2022 12:59:26 +0700 Subject: [PATCH 08/10] Add example configurations in the doc --- src/mqtt/mod.rs | 18 ++++++++++++++++++ src/rtsp/mod.rs | 24 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 65b8907..3ed26a8 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -10,6 +10,24 @@ use log::*; /// neolink mqtt --config=config.toml /// ``` /// +/// # Example Config +/// +/// ```toml +// [[cameras]] +// name = "Cammy" +// username = "****" +// password = "****" +// address = "****:9000" +// [cameras.mqtt] +// server = "127.0.0.1" +// port = 1883 +// credentials = ["username", "password"] +// ``` +// +// `server` is the mqtt server that messages will be sent to +// `port` is the server's mqtt port +// `credentials` are the username and password required to identify with the mqtt server +// use std::sync::Arc; mod app; diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index a38cc4e..c228107 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -28,6 +28,30 @@ /// neolink rtsp --config=config.toml /// ``` /// +/// # Example Config +/// +/// ```toml +// [[cameras]] +// name = "Cammy" +// username = "****" +// password = "****" +// address = "****:9000" +// [cameras.pause] +// on_motion = false +// on_client = false +// mode = "none" +// timeout = 1.0 +// ``` +// +// - When `on_motion` is true the camera will pause streaming when motion is stopped and resume it when motion is started +// - When `on_client` is true the camera will pause while there is no client connected. +// - `timeout` handels how long to wait after motion stops before pausing the stream +// - `mode` has the following values: +// - `"black"`: Switches to a black screen. Requires more cpu as the stream is fully reencoded +// - `"still"`: Switches to a still image. Requires more cpu as the stream is fully reencoded +// - `"test"`: Switches to the gstreamer test image. Requires more cpu as the stream is fully reencoded +// - `"none"`: Resends the last iframe the camera. This does not reencode at all. **Most use cases should use this one as it has the least effort on the cpu and gives what you would expect** +// use anyhow::{anyhow, Context, Result}; use crossbeam::utils::Backoff; use log::*; From 627a10f0accdca652948ba09b6540716d7d7d336 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 14 Dec 2022 14:43:38 +0700 Subject: [PATCH 09/10] Add mqtt messages to the docs --- src/mqtt/mod.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 3ed26a8..a56038a 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -4,6 +4,28 @@ use log::*; /// /// Handles incoming and outgoing MQTT messages /// +/// This acts as a bridge between cameras and MQTT servers +/// +/// Messages are prefixed with `neolink/{CAMERANAME}` +/// +/// Control messages: +/// +/// - `/control/led [on|off]` Turns status LED on/off +/// - `/control/ir [on|off|auti]` Turn IR lights on/off or automatically via light detection +/// - `/control/reboot` Reboot the camera +/// - `/control/ptz` [up|down|left|right|in|out] (amount) Control the PTZ movements, amount defaults to 32.0 +/// +/// Status Messages: +/// +/// `/status offline` Sent when the neolink goes offline this is a LastWill message +/// `/status disconnected` Sent when the camera goes offline +/// `/status/battery` Sent in reply to a `/query/battery` +/// +/// Query Messages: +/// +/// `/query/battery` Request that the camera reports its battery level (Not Yet Implemented) +/// +/// /// # Usage /// /// ```bash @@ -24,8 +46,8 @@ use log::*; // credentials = ["username", "password"] // ``` // -// `server` is the mqtt server that messages will be sent to -// `port` is the server's mqtt port +// `server` is the mqtt server +// `port` is the mqtt server's port // `credentials` are the username and password required to identify with the mqtt server // use std::sync::Arc; From 600a864ffda34705aef6275f882d99474458ae35 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Thu, 15 Dec 2022 12:39:21 +0700 Subject: [PATCH 10/10] Add battrylist xml to messages --- dissector/messages.md | 45 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/dissector/messages.md b/dissector/messages.md index bc0ff2e..cc87cc4 100644 --- a/dissector/messages.md +++ b/dissector/messages.md @@ -2182,7 +2182,7 @@ Message have zero to two payloads. - Client - Extension - + ```xml @@ -2190,7 +2190,7 @@ Message have zero to two payloads. ``` - - Payload + - Payload ```xml @@ -2480,7 +2480,7 @@ Message have zero to two payloads. 0 1 - 1 + 1 2 0 300 @@ -2522,4 +2522,41 @@ Message have zero to two payloads. none - ``` \ No newline at end of file + ``` + +- 252: BatteryList + + - Client + + Camera Only Message. + + - Camera + + - Header + + | Magic | Message ID | Message Length | Encryption Offset | Status Code | Message Class | Payload Offset | + |--------------|--------------|----------------|-------------------|-------------------|---------------|----------------| + | f0 de bc 0a | fc 00 00 00 | 1c 02 00 00 | 00 00 00 00 | c8 00 | 00 00 | 00 00 00 00 | + + - Payload + + ```xml + + + + + 0 + chargeComplete + solarPanel + 3999 + 0 + 21 + 100 + 0 + 2 + + + + ``` + + **Notes**: Sent after login, with a message handle of 0 (usually 0 means not sent in reply to a specific request). TODO: Find out how to request on demand.