diff --git a/Cargo.lock b/Cargo.lock index e828ff0..693548a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -958,6 +958,7 @@ dependencies = [ "cfb-mode", "cookie-factory", "crossbeam", + "crossbeam-channel 0.5.1", "env_logger", "err-derive", "gstreamer", diff --git a/Cargo.toml b/Cargo.toml index 3b2de33..1912e6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ aes = "0.6" cfb-mode = "0.6" cookie-factory = "0.3" crossbeam = "0.7" +crossbeam-channel = "0.5.1" err-derive = "0.2" env_logger = "*" gstreamer = "0.17" diff --git a/crates/core/src/bc/de.rs b/crates/core/src/bc/de.rs index dd07d29..f91a281 100644 --- a/crates/core/src/bc/de.rs +++ b/crates/core/src/bc/de.rs @@ -431,7 +431,7 @@ mod tests { .. })), }), - }) if version == "1.1" && stream_type == "externStream" + }) if version == "1.1" && stream_type == Some("externStream".to_string()) ); } @@ -475,7 +475,7 @@ mod tests { .. })), }), - }) if version == "1.1" && stream_type == "subStream" + }) if version == "1.1" && stream_type == Some("subStream".to_string()) ); } @@ -519,7 +519,7 @@ mod tests { .. })), }), - }) if version == "1.1" && stream_type == "mainStream" + }) if version == "1.1" && stream_type == Some("mainStream".to_string()) ); } } diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index 37b8f6b..e2992ca 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -10,6 +10,8 @@ pub const MSG_ID_LOGIN: u32 = 1; pub const MSG_ID_LOGOUT: u32 = 2; /// Video and Audio Streams messages have this ID pub const MSG_ID_VIDEO: u32 = 3; +/// ID used to stop the video stream +pub const MSG_ID_VIDEO_STOP: u32 = 4; /// TalkAbility messages have this ID pub const MSG_ID_TALKABILITY: u32 = 10; /// TalkReset messages have this ID diff --git a/crates/core/src/bc/xml.rs b/crates/core/src/bc/xml.rs index f4a216a..ef7bb7f 100644 --- a/crates/core/src/bc/xml.rs +++ b/crates/core/src/bc/xml.rs @@ -199,7 +199,7 @@ pub struct Preview { pub handle: u32, /// Either `"mainStream"` or `"subStream"` #[yaserde(rename = "streamType")] - pub stream_type: String, + pub stream_type: Option, } /// Extension xml diff --git a/crates/core/src/bc_protocol/connection/mod.rs b/crates/core/src/bc_protocol/connection/mod.rs index 4d6a235..504ae23 100644 --- a/crates/core/src/bc_protocol/connection/mod.rs +++ b/crates/core/src/bc_protocol/connection/mod.rs @@ -7,6 +7,7 @@ //! use crate::bc; use crate::bc::model::*; +use crate::bcmedia; use crate::bcudp; use err_derive::Error; use log::*; @@ -45,6 +46,9 @@ pub enum Error { #[error(display = "Deserialization error")] Deserialization(#[error(source)] bc::de::Error), + #[error(display = "BcMedia Deserialization error")] + BcMediaDeserialization(#[error(source)] bcmedia::de::Error), + #[error(display = "Serialization error")] Serialization(#[error(source)] bc::ser::Error), diff --git a/crates/core/src/bc_protocol/stream.rs b/crates/core/src/bc_protocol/stream.rs index 6c0b24e..316c9e9 100644 --- a/crates/core/src/bc_protocol/stream.rs +++ b/crates/core/src/bc_protocol/stream.rs @@ -1,4 +1,4 @@ -use super::{BcCamera, BinarySubscriber, Result}; +use super::{BcCamera, BinarySubscriber, Error, Result, RX_TIMEOUT}; use crate::{ bc::{model::*, xml::*}, bcmedia::model::*, @@ -112,7 +112,7 @@ impl BcCamera { version: xml_ver(), channel_id: self.channel_id, handle, - stream_type: stream_name, + stream_type: Some(stream_name), }), ..Default::default() }, @@ -122,14 +122,90 @@ impl BcCamera { let mut media_sub = BinarySubscriber::from_bc_sub(&sub_video); + let result; + loop { let bc_media = BcMedia::deserialize(&mut media_sub)?; // We now have a complete interesting packet. Send it to on the callback match data_outs.stream_recv(bc_media) { Ok(true) => {} - Ok(false) => return Ok(()), - Err(e) => return Err(e), + Ok(false) => { + result = Ok(()); + break; + } + Err(e) => { + result = Err(e); + break; + } }; } + + println!("Stopping video: {:?}", result); + self.stop_stream(stream)?; + println!("Video stopped"); + result + } + + /// + /// Sends the stop stream message to the camera + /// + pub fn stop_stream(&self, stream: Stream) -> Result<()> { + let connection = self + .connection + .as_ref() + .expect("Must be connected to stop video"); + let sub = connection.subscribe(MSG_ID_VIDEO_STOP)?; + + // Theses are the numbers used with the offical client + // On an E1 and swann cameras: + // - mainStream always has a value of 0 + // - subStream always has a value of 1 + // - There is no externStram + // On a B800: + // - mainStream is 0 + // - subStream is 256 + // - externStram is 1024 + let handle = match stream { + Stream::Main => 0, + Stream::Sub => 256, + Stream::Extern => 1024, + }; + + let msg = Bc::new_from_xml( + BcMeta { + msg_id: MSG_ID_VIDEO_STOP, + channel_id: self.channel_id, + msg_num: self.new_message_num(), + stream_type: 0, + response_code: 0, + class: 0x6414, // IDK why + }, + BcXml { + preview: Some(Preview { + version: xml_ver(), + channel_id: self.channel_id, + handle, + stream_type: None, + }), + ..Default::default() + }, + ); + + sub.send(msg)?; + + let msg = sub.rx.recv_timeout(RX_TIMEOUT)?; + + if let BcMeta { + response_code: 200, .. + } = msg.meta + { + } else { + return Err(Error::UnintelligibleReply { + reply: msg, + why: "The camera did not accept the stream stop command.", + }); + } + + Ok(()) } } diff --git a/src/rtsp/abort.rs b/src/rtsp/abort.rs new file mode 100644 index 0000000..4dab182 --- /dev/null +++ b/src/rtsp/abort.rs @@ -0,0 +1,29 @@ +use std::sync::{ + atomic::{AtomicBool, Ordering}, + Arc, +}; + +#[derive(Clone)] +pub(crate) struct AbortHandle { + live: Arc, +} + +impl AbortHandle { + pub(crate) fn new() -> Self { + Self { + live: Arc::new(AtomicBool::new(true)), + } + } + + pub(crate) fn abort(&self) { + self.live.store(false, Ordering::Relaxed); + } + + pub(crate) fn reset(&self) { + self.live.store(true, Ordering::Relaxed); + } + + pub(crate) fn is_live(&self) -> bool { + self.live.load(Ordering::Relaxed) + } +} diff --git a/src/rtsp/gst.rs b/src/rtsp/gst.rs index 77a67a1..d9a4872 100644 --- a/src/rtsp/gst.rs +++ b/src/rtsp/gst.rs @@ -7,6 +7,8 @@ use gstreamer::prelude::Cast; use gstreamer::{Bin, Structure}; use gstreamer_app::AppSrc; //use gstreamer_rtsp::RTSPLowerTrans; +use super::abort::AbortHandle; +use anyhow::anyhow; use gstreamer_rtsp::RTSPAuthMethod; pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate}; use gstreamer_rtsp_server::glib; @@ -27,6 +29,7 @@ use std::io; use std::io::Write; type Result = std::result::Result; +type AnyResult = std::result::Result; pub(crate) struct RtspServer { server: GstRTSPServer, @@ -38,6 +41,8 @@ pub(crate) struct GstOutputs { video_format: Option, audio_format: Option, factory: RTSPMediaFactory, + live: AbortHandle, + last_iframe: Option>, } // The stream from the camera will be using one of these formats @@ -58,6 +63,7 @@ enum StreamFormat { impl StreamOutput for GstOutputs { fn stream_recv(&mut self, media: BcMedia) -> StreamOutputError { + let mut should_continue = true; match media { BcMedia::Iframe(payload) => { let video_type = match payload.video_type { @@ -66,6 +72,11 @@ impl StreamOutput for GstOutputs { }; self.set_format(Some(video_type)); self.vidsrc.write_all(&payload.data)?; + self.last_iframe = Some(payload.data); + // Only stop on an iframe so we have the + // last frame to show + should_continue = self.live.is_live(); + debug!("Iframe continue: {}", should_continue); } BcMedia::Pframe(payload) => { let video_type = match payload.video_type { @@ -87,8 +98,8 @@ impl StreamOutput for GstOutputs { //Ignore other BcMedia like InfoV1 and InfoV2 } } - - Ok(true) + debug!("Output continue: {}", should_continue); + Ok(should_continue) } } @@ -100,11 +111,31 @@ impl GstOutputs { video_format: None, audio_format: None, factory: RTSPMediaFactory::new(), + live: AbortHandle::new(), + last_iframe: Default::default(), }; result.apply_format(); result } + pub(crate) fn get_abort_handle(&self) -> AbortHandle { + self.live.clone() + } + + pub(crate) fn has_last_iframe(&self) -> bool { + self.last_iframe.is_some() + } + + pub(crate) fn write_last_iframe(&mut self) -> AnyResult<()> { + self.vidsrc.write_all( + self.last_iframe + .as_ref() + .ok_or_else(|| anyhow!("No iframe data avaliable"))? + .as_slice(), + )?; + Ok(()) + } + fn set_format(&mut self, format: Option) { match format { Some(StreamFormat::H264) | Some(StreamFormat::H265) => { @@ -140,8 +171,7 @@ impl GstOutputs { _ => "! fakesink".to_string(), }; - self.factory.set_launch( - &vec![ + let launch_str = &vec![ "( ", "appsrc name=vidsrc is-live=true block=true emit-signals=false max-bytes=52428800 do-timestamp=true format=GST_FORMAT_TIME", // 50MB max size so that it won't grow to infinite if the queue blocks launch_vid, @@ -149,8 +179,9 @@ impl GstOutputs { &launch_aud, ")" ] - .join(" "), - ); + .join(" "); + debug!("Gstreamer launch str: {:?}", launch_str); + self.factory.set_launch(launch_str); } } diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index 06f0a60..e016248 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -30,24 +30,27 @@ /// use anyhow::{Context, Result}; use log::*; -use neolink_core::bc_protocol::{BcCamera, Stream}; +use neolink_core::bc_protocol::{BcCamera, MotionStatus, Stream}; use std::collections::HashSet; use std::fs; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use std::time::Duration; use validator::Validate; // mod adpcm; -/// The command line parameters for this subcommand +// The command line parameters for this subcommand mod cmdline; mod config; -/// The errors this subcommand can raise +// The gstreamer implementation +mod abort; mod gst; +mod motion; use crate::utils::AddressOrUid; pub(crate) use cmdline::Opt; use config::{CameraConfig, Config, UserConfig}; use gst::{GstOutputs, RtspServer, TlsAuthenticationMode}; +use motion::MotionStream; /// Entry point for the rtsp subcommand /// @@ -278,7 +281,99 @@ fn camera_main( "{}: Starting video stream {}", camera_config.name, stream_display_name ); - camera.start_video(outputs, stream_name).with_context(|| format!("Error while streaming {}", camera_config.name)) + + // Used to get the error from the thread + let thread_error: Arc>> = Default::default(); + + crossbeam::scope(|s| { + let (mut motion, rx) = MotionStream::new(); + + + let motionthread_motion_abort = motion.get_abort_handle(); + let motionthread_stream_abort = outputs.get_abort_handle(); + let rxthread_motion_abort = motion.get_abort_handle(); + let rxthread_stream_abort = outputs.get_abort_handle(); + let streamthread_stream_abort = outputs.get_abort_handle(); + let streamthread_motion_abort = motion.get_abort_handle(); + + // Start in the aborted state because motion defaults to still + streamthread_stream_abort.abort(); + + let arc_camera = Arc::new(camera); + + // Motion detection thread + let motion_camera = arc_camera.clone(); + let motion_camname = camera_config.name.to_string(); + let motion_error = thread_error.clone(); + s.spawn(move |_| { + if let Err(e) = motion_camera.listen_on_motion(&mut motion).with_context(|| format!("Error while reading motion {}", motion_camname)) { + // When motion errors set the thread error + *motion_error.lock().unwrap() = Some(e); + } + // Always abort the streams/motion when motion stops regardless of why + // so that this thread can reset the camera + motionthread_motion_abort.abort(); + motionthread_stream_abort.abort(); + debug!("Motion stopped"); + }); + + // Motion rx -> stream thread + s.spawn(move |_| { + // Stops when motion is stopped + // which happens when either motion or stream errors + while rxthread_motion_abort.is_live() { + let msg = rx.try_recv(); + if let Ok(msg) = msg { + match msg { + MotionStatus::Start => { + debug!("Motion start"); + rxthread_stream_abort.reset(); + } + MotionStatus::Stop => { + debug!("Motion stop"); + rxthread_stream_abort.abort(); + } + _ => {} + } + } + } + println!("Motion RX Stopped"); + }); + + // Stream thread + let stream_camera = arc_camera; + let stream_camname = camera_config.name.to_string(); + let stream_error = thread_error.clone(); + s.spawn(move |_| { + loop { + if streamthread_stream_abort.is_live() || ! outputs.has_last_iframe() { + debug!("Stream resumed: waiting for motion to stop"); + // Will block until the rx thread send the abort + if let Err(e) = stream_camera.start_video(outputs, stream_name).with_context(|| format!("Error while streaming {}", stream_camname)) { + // When stream errors set the thread error and abort the motion + *stream_error.lock().unwrap() = Some(e); + break; + } + debug!("Stream paused: waiting for motion to start"); + } else { + // While stream is aborted we send the blank image + if let Err(e) = outputs.write_last_iframe().with_context(|| format!("Failed to write no motion image for {}", stream_camname)) { + *stream_error.lock().unwrap() = Some(e); + break; + } + std::thread::sleep(std::time::Duration::from_millis(50)); + } + } + streamthread_stream_abort.abort(); + streamthread_motion_abort.abort(); + println!("Stream stopped"); + }); + }).unwrap(); + + // Should be the only one holding the thread_error so unwrap it + // all the way to an actual error + Err(Arc::try_unwrap(thread_error).unwrap().into_inner().unwrap().unwrap()) + })().map_err(|e| CameraErr{ connected, login_fail, diff --git a/src/rtsp/motion.rs b/src/rtsp/motion.rs new file mode 100644 index 0000000..920fd1e --- /dev/null +++ b/src/rtsp/motion.rs @@ -0,0 +1,32 @@ +use super::abort::AbortHandle; +use crossbeam_channel::{unbounded, Receiver, Sender}; +use neolink_core::bc_protocol::{MotionOutput, MotionOutputError, MotionStatus}; + +pub(crate) struct MotionStream { + tx: Sender, + live: AbortHandle, +} + +impl MotionStream { + pub(crate) fn new() -> (Self, Receiver) { + let (tx, rx) = unbounded(); + ( + Self { + tx, + live: AbortHandle::new(), + }, + rx, + ) + } + + pub(crate) fn get_abort_handle(&self) -> AbortHandle { + self.live.clone() + } +} + +impl MotionOutput for MotionStream { + fn motion_recv(&mut self, motion_status: MotionStatus) -> MotionOutputError { + let _ = self.tx.send(motion_status); + Ok(self.live.is_live()) + } +}