diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 007e634..e5b119d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -20,18 +20,20 @@ jobs: - if: runner.os == 'Windows' name: Install Gstreamer run: | - choco install -y --no-progress gstreamer --version=1.16.2.20200527 - choco install -y --no-progress gstreamer-devel --version=1.16.2 + choco install -y --no-progress gstreamer --version=1.20.0 + choco install -y --no-progress gstreamer-devel --version=1.20.0 echo "Updating Cargo environment" echo "C:\gstreamer\1.0\x86_64\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append echo "GSTREAMER_1_0_ROOT_X86_64=C:\gstreamer\1.0\x86_64\" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append - if: runner.os == 'macOS' name: Install Gstreamer on macOS run: | - curl -L 'https://gstreamer.freedesktop.org/data/pkg/osx/1.16.2/gstreamer-1.0-devel-1.16.2-x86_64.pkg' -o "$(pwd)/gstreamer-devel.pkg" + curl -L 'https://gstreamer.freedesktop.org/data/pkg/osx/1.20.4/gstreamer-1.0-devel-1.20.4-universal.pkg' -o "$(pwd)/gstreamer-devel.pkg" sudo installer -verbose -pkg "$(pwd)/gstreamer-devel.pkg" -target / PKG_CONFIG_PATH="/Library/Frameworks/GStreamer.framework/Versions/1.0/lib/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}" + PKG_CONFIG_GSTREAMER_1_0_PREFIX /Library/Frameworks/GStreamer.framework/Versions/1.0 echo "PKG_CONFIG_PATH=${PKG_CONFIG_PATH}" >> "${GITHUB_ENV}" + echo "PKG_CONFIG_GSTREAMER_1_0_PREFIX=${PKG_CONFIG_GSTREAMER_1_0_PREFIX}" >> "${GITHUB_ENV}" - name: Cache cargo registry uses: actions/cache@v1 with: @@ -102,10 +104,10 @@ jobs: - name: Install ${{ matrix.arch }} Rust toolchain uses: actions-rs/toolchain@v1 with: - toolchain: stable - override: true - target: ${{ matrix.target }} - profile: minimal + toolchain: stable + override: true + target: ${{ matrix.target }} + profile: minimal - name: Build uses: actions-rs/cargo@v1 with: diff --git a/Cargo.lock b/Cargo.lock index 57e041c..3700018 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -269,9 +269,9 @@ dependencies = [ [[package]] name = "crossbeam-channel" -version = "0.5.1" +version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06ed27e177f16d65f0f0c22a213e17c696ace5dd64b14258b52f9417ccb52db4" +checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521" dependencies = [ "cfg-if", "crossbeam-utils", @@ -1667,18 +1667,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.26" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93119e4feac1cbe6c798c34d3a53ea0026b0b1de6a120deef895137c0529bfe2" +checksum = "10deb33631e3c9018b9baf9dcbbc4f737320d2b576bac10f6aefa048fa407e3e" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.26" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "060d69a0afe7796bf42e9e2ff91f5ee691fb15c53d38b4b62a9a53eb23164745" +checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb" dependencies = [ "proc-macro2", "quote", diff --git a/build.rs b/build.rs index 53ee1d7..4e3ddd4 100644 --- a/build.rs +++ b/build.rs @@ -45,7 +45,7 @@ fn github_ver() -> Option { } } -#[cfg(windows)] +#[cfg(target_os = "windows")] fn platform_cfg() { let gstreamer_dir = env::var_os("GSTREAMER_1_0_ROOT_X86_64") .and_then(|x| x.into_string().ok()) @@ -54,5 +54,15 @@ fn platform_cfg() { println!(r"cargo:rustc-link-search=native={}\lib", gstreamer_dir); } -#[cfg(not(windows))] +#[cfg(target_os = "macos")] +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()); + + println!(r"cargo:rustc-link-search=native={}/lib", gstreamer_dir); + println!(r"cargo:rustc-link-arg=-Wl,-rpath,{}/lib", gstreamer_dir); +} + +#[cfg(all(not(target_os = "macos"), not(target_os = "windows")))] fn platform_cfg() {} diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 85760c1..b444a97 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -10,7 +10,7 @@ aes = "0.6" cfb-mode = "0.6" cookie-factory = "0.3" crc32fast = "1.2.1" -crossbeam-channel = "0.5.1" +crossbeam-channel = "0.5.6" err-derive = "0.2" get_if_addrs = "0.5.3" lazy_static = "1.4" diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index 6b35384..8b3e3c6 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -2,7 +2,10 @@ use crate::{bc, bcmedia}; use log::*; use std::convert::TryInto; use std::net::ToSocketAddrs; -use std::sync::atomic::{AtomicU16, Ordering}; +use std::sync::{ + atomic::{AtomicBool, AtomicU16, Ordering}, + Mutex, +}; use Md5Trunc::*; @@ -26,18 +29,19 @@ use bc::model::*; pub(crate) use connection::*; pub use errors::Error; pub use ledstate::LightState; -pub use motion::{MotionOutput, MotionOutputError, MotionStatus}; +pub use motion::{MotionData, MotionStatus}; pub use pirstate::PirState; pub use resolution::*; -pub use stream::{Stream, StreamOutput, StreamOutputError}; +use std::sync::Arc; +pub use stream::{Stream, StreamData}; type Result = std::result::Result; -impl From for Error { - fn from(k: std::sync::mpsc::RecvTimeoutError) -> Self { +impl From for Error { + fn from(k: crossbeam_channel::RecvTimeoutError) -> Self { match k { - std::sync::mpsc::RecvTimeoutError::Timeout => Error::Timeout, - std::sync::mpsc::RecvTimeoutError::Disconnected => Error::TimeoutDisconnected, + crossbeam_channel::RecvTimeoutError::Timeout => Error::Timeout, + crossbeam_channel::RecvTimeoutError::Disconnected => Error::TimeoutDisconnected, } } } @@ -47,11 +51,11 @@ impl From for Error { /// pub struct BcCamera { channel_id: u8, - connection: Option, - logged_in: bool, + connection: Arc, + logged_in: AtomicBool, message_num: AtomicU16, // Certain commands such as logout require the username/pass in plain text.... why....??? - credentials: Option, + credentials: Mutex>, } // Used for caching the credentials @@ -182,31 +186,29 @@ impl BcCamera { debug!("Success"); let me = Self { - connection: Some(conn), + connection: Arc::new(conn), message_num: AtomicU16::new(0), channel_id, - logged_in: false, - credentials: None, + logged_in: AtomicBool::new(false), + credentials: Mutex::new(None), }; - if let Some(conn) = &me.connection { - if conn.is_udp() { - let keep_alive_msg = Bc { - meta: BcMeta { - msg_id: MSG_ID_UDP_KEEP_ALIVE, - channel_id: me.channel_id, - msg_num: me.new_message_num(), - stream_type: 0, - response_code: 0, - class: 0x6414, - }, - body: BcBody::ModernMsg(ModernMsg { - ..Default::default() - }), - }; + if me.connection.is_udp() { + let keep_alive_msg = Bc { + meta: BcMeta { + msg_id: MSG_ID_UDP_KEEP_ALIVE, + channel_id: me.channel_id, + msg_num: me.new_message_num(), + stream_type: 0, + response_code: 0, + class: 0x6414, + }, + body: BcBody::ModernMsg(ModernMsg { + ..Default::default() + }), + }; - conn.set_keep_alive_msg(keep_alive_msg); - } + me.connection.set_keep_alive_msg(keep_alive_msg); } Ok(me) } @@ -216,35 +218,37 @@ impl BcCamera { self.message_num.fetch_add(1, Ordering::Relaxed) } + fn get_connection(&self) -> Arc { + self.connection.clone() + } + /// This will drop the connection. It will try to send the logout request to the camera /// first pub fn disconnect(&mut self) { - if let Some(connection) = &self.connection { - // Stop polling now. We don't need it for a disconnect - // - // It will also ensure that when we drop the connection we don't - // get an error for read return zero bytes from the polling thread; - connection.stop_polling(); - if let Err(err) = self.logout() { - warn!("Could not log out, ignoring: {}", err); - } + let connection = &self.connection; + // Stop polling now. We don't need it for a disconnect + // + // It will also ensure that when we drop the connection we don't + // get an error for read return zero bytes from the polling thread; + connection.stop_polling(); + if let Err(err) = self.logout() { + warn!("Could not log out, ignoring: {}", err); } - self.connection = None; } // Certains commands like logout need the username and password // this command will return it as a tuple of (Username, Option) // This will only work after login - fn get_credentials(&self) -> &Option { - &self.credentials + fn get_credentials(&self) -> Option { + self.credentials.lock().unwrap().clone() } // This is used to store the credentials it is called during login. - fn set_credentials(&mut self, username: String, password: Option) { - self.credentials = Some(Credentials::new(username, password)); + fn set_credentials(&self, username: String, password: Option) { + *(self.credentials.lock().unwrap()) = Some(Credentials::new(username, password)); } // This is used to clear the stored credentials it is called during logout. - fn clear_credentials(&mut self) { - self.credentials = None; + fn clear_credentials(&self) { + *(self.credentials.lock().unwrap()) = None; } } diff --git a/crates/core/src/bc_protocol/connection/bcconn.rs b/crates/core/src/bc_protocol/connection/bcconn.rs index 3efe3ce..524a773 100644 --- a/crates/core/src/bc_protocol/connection/bcconn.rs +++ b/crates/core/src/bc_protocol/connection/bcconn.rs @@ -1,6 +1,7 @@ use super::{BcSource, BcSubscription, Error, Result, TcpSource}; use crate::bc; use crate::bc::model::*; +use crossbeam_channel::{unbounded, Receiver, Sender}; use log::*; use socket2::{Domain, Socket, Type}; use std::collections::btree_map::Entry; @@ -8,7 +9,6 @@ use std::collections::BTreeMap; use std::error::Error as StdErr; // Just need the traits use std::io::{Error as IoError, Read, Write}; use std::net::{SocketAddr, TcpStream}; -use std::sync::mpsc::{channel, Receiver, Sender}; use std::sync::{atomic::AtomicBool, atomic::Ordering, Arc, Mutex}; use std::thread::JoinHandle; use std::time::{Duration, Instant}; @@ -98,7 +98,7 @@ impl BcConnection { #[allow(clippy::significant_drop_in_scrutinee)] pub fn subscribe(&self, msg_num: u16) -> Result { - let (tx, rx) = channel(); + let (tx, rx) = unbounded(); match self.subscribers.lock().unwrap().entry(msg_num) { Entry::Vacant(vac_entry) => vac_entry.insert(tx), Entry::Occupied(_) => return Err(Error::SimultaneousSubscription { msg_num }), diff --git a/crates/core/src/bc_protocol/connection/bcsub.rs b/crates/core/src/bc_protocol/connection/bcsub.rs index 307cff3..ea33da5 100644 --- a/crates/core/src/bc_protocol/connection/bcsub.rs +++ b/crates/core/src/bc_protocol/connection/bcsub.rs @@ -1,6 +1,6 @@ use super::{BcConnection, Result}; use crate::bc::model::*; -use std::sync::mpsc::Receiver; +use crossbeam_channel::Receiver; pub struct BcSubscription<'a> { pub rx: Receiver, diff --git a/crates/core/src/bc_protocol/connection/binarysub.rs b/crates/core/src/bc_protocol/connection/binarysub.rs index e09552c..e8bbd6d 100644 --- a/crates/core/src/bc_protocol/connection/binarysub.rs +++ b/crates/core/src/bc_protocol/connection/binarysub.rs @@ -3,10 +3,8 @@ use super::{ BcConnection, }; use crate::bc::model::*; -use std::{ - io::{BufRead, Error, ErrorKind, Read}, - sync::mpsc::RecvTimeoutError, -}; +use crossbeam_channel::RecvTimeoutError; +use std::io::{BufRead, Error, ErrorKind, Read}; type Result = std::result::Result; diff --git a/crates/core/src/bc_protocol/connection/mod.rs b/crates/core/src/bc_protocol/connection/mod.rs index 085bdb8..3b3cd2c 100644 --- a/crates/core/src/bc_protocol/connection/mod.rs +++ b/crates/core/src/bc_protocol/connection/mod.rs @@ -9,6 +9,7 @@ use crate::bc; use crate::bc::model::*; use crate::bcmedia; use crate::bcudp; +use crossbeam_channel::{unbounded, Receiver, RecvError, Sender}; use err_derive::Error; use log::*; use socket2::{Domain, Socket, Type}; @@ -16,7 +17,6 @@ use std::collections::btree_map::Entry; use std::collections::BTreeMap; use std::error::Error as StdErr; // Just need the traits use std::net::{SocketAddr, TcpStream}; -use std::sync::mpsc::{channel, Receiver, RecvError, Sender}; use std::sync::{atomic::AtomicBool, atomic::Ordering, Arc, Mutex}; use std::thread::JoinHandle; diff --git a/crates/core/src/bc_protocol/connection/tcpconn.rs b/crates/core/src/bc_protocol/connection/tcpconn.rs index 0f0e4b7..6fdd043 100644 --- a/crates/core/src/bc_protocol/connection/tcpconn.rs +++ b/crates/core/src/bc_protocol/connection/tcpconn.rs @@ -1,6 +1,7 @@ use super::{BcSubscription, Error, Result}; use crate::bc; use crate::bc::model::*; +use crossbeam_channel::{unbounded, Receiver, Sender}; use log::*; use socket2::{Domain, Socket, Type}; use std::collections::btree_map::Entry; @@ -8,7 +9,6 @@ use std::collections::BTreeMap; use std::error::Error as StdErr; // Just need the traits use std::io::{Error as IoError, Read, Write}; use std::net::{SocketAddr, TcpStream}; -use std::sync::mpsc::{channel, Receiver, Sender}; use std::sync::{atomic::AtomicBool, atomic::Ordering, Arc, Mutex}; use std::thread::JoinHandle; use std::time::Duration; diff --git a/crates/core/src/bc_protocol/errors.rs b/crates/core/src/bc_protocol/errors.rs index bd3d5f7..0452303 100644 --- a/crates/core/src/bc_protocol/errors.rs +++ b/crates/core/src/bc_protocol/errors.rs @@ -40,7 +40,11 @@ pub enum Error { /// Raised when a connection is dropped. #[error(display = "Dropped connection")] - DroppedConnection(#[error(source)] std::sync::mpsc::RecvError), + DroppedConnection(#[error(source)] crossbeam_channel::RecvError), + + /// Raised when a connection is dropped during a TryRecv event + #[error(display = "Dropped connection")] + DroppedConnectionTry(#[error(source)] crossbeam_channel::TryRecvError), /// Raised when the RX_TIMEOUT is reach #[error(display = "Timeout")] diff --git a/crates/core/src/bc_protocol/ledstate.rs b/crates/core/src/bc_protocol/ledstate.rs index 75404fc..a3e8aee 100644 --- a/crates/core/src/bc_protocol/ledstate.rs +++ b/crates/core/src/bc_protocol/ledstate.rs @@ -4,10 +4,7 @@ use crate::bc::{model::*, xml::*}; impl BcCamera { /// Get the [LedState] xml which contains the LED status of the camera pub fn get_ledstate(&self) -> Result { - let connection = self - .connection - .as_ref() - .expect("Must be connected to get time"); + let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub_get = connection.subscribe(msg_num)?; let get = Bc { @@ -54,10 +51,7 @@ impl BcCamera { /// Set the led lights using the [LedState] xml pub fn set_ledstate(&self, mut led_state: LedState) -> Result<()> { - let connection = self - .connection - .as_ref() - .expect("Must be connected to get time"); + let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub_set = connection.subscribe(msg_num)?; diff --git a/crates/core/src/bc_protocol/login.rs b/crates/core/src/bc_protocol/login.rs index aec7ba8..a0a0b2d 100644 --- a/crates/core/src/bc_protocol/login.rs +++ b/crates/core/src/bc_protocol/login.rs @@ -1,18 +1,16 @@ use super::{make_aes_key, md5_string, BcCamera, Error, Result, Truncate, ZeroLast, RX_TIMEOUT}; use crate::bc::{model::*, xml::*}; +use std::sync::atomic::Ordering; impl BcCamera { /// Login to the camera. /// /// This should be called before most other commands - pub fn login(&mut self, username: &str, password: Option<&str>) -> Result { + pub fn login(&self, username: &str, password: Option<&str>) -> Result { let device_info; // This { is here due to the connection and set_credentials both requiring a mutable borrow { - let connection = self - .connection - .as_ref() - .expect("Must be connected to log in"); + let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub_login = connection.subscribe(msg_num)?; @@ -119,7 +117,7 @@ impl BcCamera { .. }) => { // Login succeeded! - self.logged_in = true; + self.logged_in.store(true, Ordering::Relaxed); device_info = info; } BcBody::ModernMsg(ModernMsg { diff --git a/crates/core/src/bc_protocol/logout.rs b/crates/core/src/bc_protocol/logout.rs index 246598c..bf6503a 100644 --- a/crates/core/src/bc_protocol/logout.rs +++ b/crates/core/src/bc_protocol/logout.rs @@ -1,15 +1,13 @@ use super::{BcCamera, Result}; use crate::bc::{model::*, xml::*}; +use std::sync::atomic::Ordering; impl BcCamera { /// Logout from the camera - pub fn logout(&mut self) -> Result<()> { - if self.logged_in { + pub fn logout(&self) -> Result<()> { + if self.logged_in.load(Ordering::Relaxed) { if let Some(credentials) = self.get_credentials() { - let connection = self - .connection - .as_ref() - .expect("Must be connected to log in"); + let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub_logout = connection.subscribe(msg_num)?; @@ -41,7 +39,7 @@ impl BcCamera { } } self.clear_credentials(); - self.logged_in = false; + self.logged_in.store(false, Ordering::Relaxed); Ok(()) } } diff --git a/crates/core/src/bc_protocol/motion.rs b/crates/core/src/bc_protocol/motion.rs index bd1a2ea..58cd7d9 100644 --- a/crates/core/src/bc_protocol/motion.rs +++ b/crates/core/src/bc_protocol/motion.rs @@ -1,40 +1,95 @@ use super::{BcCamera, Error, Result, RX_TIMEOUT}; use crate::bc::{model::*, xml::*}; +use crossbeam_channel::{bounded, Receiver, TryRecvError}; +use std::sync::{ + atomic::{AtomicBool, Ordering}, + Arc, +}; +use std::thread::{self, JoinHandle}; +use std::time::{Duration, Instant}; /// Motion Status that the callback can send +#[derive(Clone, Copy, Debug)] pub enum MotionStatus { /// Sent when motion is first detected - Start, + Start(Instant), /// Sent when motion stops - Stop, + Stop(Instant), /// Sent when an Alarm about something other than motion was received - NoChange, + NoChange(Instant), } -/// This is a conveince type for the error of the MotionOutput callback -pub type MotionOutputError = Result; +/// A handle on current motion related events comming from the camera +/// +/// When this object is dropped the motion events are stopped +pub struct MotionData { + handle: Option>>, + rx: Receiver, + abort_handle: Arc, + last_update: MotionStatus, +} -/// Trait used as part of [`listen_on_motion`] to send motion messages -pub trait MotionOutput { - /// This is the callback used when motion is received +impl MotionData { + /// Get if motion has been detected. Returns None if + /// no motion data has yet been recieved from the camera /// - /// If result is `Ok(true)` more messages will be sent + /// An error is raised if the motion connection to the camera is dropped + pub fn motion_detected(&mut self) -> Result> { + self.consume_motion_events()?; + Ok(match &self.last_update { + MotionStatus::Start(_) => Some(true), + MotionStatus::Stop(_) => Some(false), + MotionStatus::NoChange(_) => None, + }) + } + + /// Get if motion has been detected within given duration. Returns None if + /// no motion data has yet been recieved from the camera /// - /// If result if `Ok(false)` then message will be stopped + /// An error is raised if the motion connection to the camera is dropped + pub fn motion_detected_within(&mut self, duration: Duration) -> Result> { + self.consume_motion_events()?; + Ok(match &self.last_update { + MotionStatus::Start(time) => Some((Instant::now() - *time) < duration), + MotionStatus::Stop(time) => Some((Instant::now() - *time) < duration), + MotionStatus::NoChange(_) => None, + }) + } + + /// Consume the motion events diretly /// - /// If result is `Err(E)` then motion messages be stopped - /// and an error will be thrown - fn motion_recv(&mut self, motion_status: MotionStatus) -> MotionOutputError; + /// An error is raised if the motion connection to the camera is dropped + pub fn consume_motion_events(&mut self) -> Result> { + let mut results: Vec = vec![]; + loop { + match self.rx.try_recv() { + Ok(motion) => results.push(motion), + Err(TryRecvError::Empty) => break, + 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; + } + Ok(results) + } +} + +impl Drop for MotionData { + fn drop(&mut self) { + self.abort_handle.store(true, Ordering::Relaxed); + self.handle.take().map(|h| h.join()); + } } impl BcCamera { /// This message tells the camera to send the motion events to us /// Which are the recieved on msgid 33 fn start_motion_query(&self) -> Result { - let connection = self - .connection - .as_ref() - .expect("Must be connected to listen to messages"); + let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub = connection.subscribe(msg_num)?; @@ -69,65 +124,74 @@ impl BcCamera { } } - /// This requests that motion messages be listen to and sent to the - /// output struct. - /// - /// The output structure must implement the [`MotionCallback`] trait - pub fn listen_on_motion(&self, data_out: &mut T) -> Result<()> { + /// This returns a data structure which can be used to + /// query motion events + pub fn listen_on_motion(&self) -> Result { let msg_num = self.start_motion_query()?; - let connection = self - .connection - .as_ref() - .expect("Must be connected to listen to messages"); + let connection = self.get_connection(); // After start_motion_query (MSG_ID 31) the camera sends motion messages // when whenever motion is detected. - let sub = connection.subscribe(msg_num)?; + let abort_handle = Arc::new(AtomicBool::new(false)); + let (tx, rx) = bounded(20); - loop { - // Mostly ignore when timout is reached because these messages are only - // sent when motion is detected which might means hours between messages - // being received - let msg = sub.rx.recv_timeout(RX_TIMEOUT); - let status = match msg { - Ok(motion_msg) => { - if let BcBody::ModernMsg(ModernMsg { - payload: - Some(BcPayloads::BcXml(BcXml { - alarm_event_list: Some(alarm_event_list), - .. - })), - .. - }) = motion_msg.body - { - let mut result = MotionStatus::NoChange; - for alarm_event in &alarm_event_list.alarm_events { - if alarm_event.channel_id == self.channel_id { - if alarm_event.status == "MD" { - result = MotionStatus::Start; - break; - } else if alarm_event.status == "none" { - result = MotionStatus::Stop; - break; + let abort_handle_thread = abort_handle.clone(); + let channel_id = self.channel_id; + let handle = thread::spawn(move || { + let sub = connection.subscribe(msg_num)?; + + while !abort_handle_thread.load(Ordering::Relaxed) { + let msg = sub.rx.recv_timeout(RX_TIMEOUT); + let status = match msg { + Ok(motion_msg) => { + if let BcBody::ModernMsg(ModernMsg { + payload: + Some(BcPayloads::BcXml(BcXml { + alarm_event_list: Some(alarm_event_list), + .. + })), + .. + }) = motion_msg.body + { + let mut result = MotionStatus::NoChange(Instant::now()); + for alarm_event in &alarm_event_list.alarm_events { + if alarm_event.channel_id == channel_id { + if alarm_event.status == "MD" { + result = MotionStatus::Start(Instant::now()); + break; + } else if alarm_event.status == "none" { + result = MotionStatus::Stop(Instant::now()); + break; + } } } + Ok(result) + } else { + Ok(MotionStatus::NoChange(Instant::now())) } - Ok(result) - } else { - Ok(MotionStatus::NoChange) } - } - Err(std::sync::mpsc::RecvTimeoutError::Timeout) => Ok(MotionStatus::NoChange), - // On connection drop we stop - Err(e @ std::sync::mpsc::RecvTimeoutError::Disconnected) => Err(e), - }?; + Err(crossbeam_channel::RecvTimeoutError::Timeout) => { + Ok(MotionStatus::NoChange(Instant::now())) + } + // On connection drop we stop + Err(e @ crossbeam_channel::RecvTimeoutError::Disconnected) => Err(e), + }?; - match data_out.motion_recv(status) { - Ok(true) => {} - Ok(false) => return Ok(()), - Err(e) => return Err(e), + if tx.send(status).is_err() { + // Motion reciever has been dropped + abort_handle_thread.store(true, Ordering::Relaxed); + break; + } } - } + Ok(()) + }); + + Ok(MotionData { + handle: Some(handle), + rx, + abort_handle, + last_update: MotionStatus::NoChange(Instant::now()), + }) } } diff --git a/crates/core/src/bc_protocol/ping.rs b/crates/core/src/bc_protocol/ping.rs index 8c75905..28e290f 100644 --- a/crates/core/src/bc_protocol/ping.rs +++ b/crates/core/src/bc_protocol/ping.rs @@ -5,7 +5,7 @@ impl BcCamera { /// Ping the camera will either return Ok(()) which means a sucess reply /// or error pub fn ping(&self) -> Result<()> { - let connection = self.connection.as_ref().expect("Must be connected to ping"); + let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub_ping = connection.subscribe(msg_num)?; diff --git a/crates/core/src/bc_protocol/pirstate.rs b/crates/core/src/bc_protocol/pirstate.rs index 641fdbb..b02f798 100644 --- a/crates/core/src/bc_protocol/pirstate.rs +++ b/crates/core/src/bc_protocol/pirstate.rs @@ -4,10 +4,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 { - let connection = self - .connection - .as_ref() - .expect("Must be connected to get time"); + let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub_get = connection.subscribe(msg_num)?; let get = Bc { @@ -54,10 +51,7 @@ impl BcCamera { /// Set the PIR sensor using the [RfAlarmCfg] xml pub fn set_pirstate(&mut self, rf_alarm_cfg: RfAlarmCfg) -> Result<()> { - let connection = self - .connection - .as_ref() - .expect("Must be connected to get time"); + let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub_set = connection.subscribe(msg_num)?; diff --git a/crates/core/src/bc_protocol/reboot.rs b/crates/core/src/bc_protocol/reboot.rs index 1ce19cc..6da31dc 100644 --- a/crates/core/src/bc_protocol/reboot.rs +++ b/crates/core/src/bc_protocol/reboot.rs @@ -4,7 +4,7 @@ use crate::bc::model::*; impl BcCamera { /// Reboot the camera pub fn reboot(&self) -> Result<()> { - let connection = self.connection.as_ref().expect("Must be connected to ping"); + let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub = connection.subscribe(msg_num)?; diff --git a/crates/core/src/bc_protocol/stream.rs b/crates/core/src/bc_protocol/stream.rs index 3fdeb9d..1dc0a3c 100644 --- a/crates/core/src/bc_protocol/stream.rs +++ b/crates/core/src/bc_protocol/stream.rs @@ -3,30 +3,15 @@ use crate::{ bc::{model::*, xml::*}, bcmedia::model::*, }; +use crossbeam_channel::{bounded, unbounded, Receiver, TryRecvError}; use std::sync::{ atomic::{AtomicBool, Ordering}, Arc, }; - -/// Convience type for the error raised by the [StreamOutput] trait -pub type StreamOutputError = Result; - -/// The method [`BcCamera::start_video()`] requires a structure with this trait to pass the -/// audio and video data back to -pub trait StreamOutput { - /// This is the callback raised a complete media packet is received - /// - /// If result is `Ok(true)` more messages will be sent - /// - /// If result if `Ok(false)` then message will be stopped - /// - /// If result is `Err(E)` then messages be stopped - /// and an error will be thrown - fn stream_recv(&mut self, media: BcMedia) -> StreamOutputError; -} +use std::thread::{self, JoinHandle}; /// The stream names supported by BC -#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum Stream { /// This is the HD stream Main, @@ -39,142 +24,165 @@ pub enum Stream { Extern, } +/// A handle on currently streaming data +/// +/// The data can be pulled using `get_data` which returns raw BcMedia packets +/// +/// When this object is dropped the streaming is stopped +pub struct StreamData<'a> { + camera: &'a BcCamera, + handle: Option>>, + rx: Receiver>, + abort_handle: Arc, + stream: Stream, +} + +impl<'a> StreamData<'a> { + /// Pull data from the camera's buffer + /// This returns raw BcMedia packets + pub fn get_data(&self) -> Result>> { + let mut results: Vec<_> = vec![]; + loop { + match self.rx.try_recv() { + Ok(data) => results.push(data), + Err(TryRecvError::Empty) => break, + Err(e) => return Err(Error::from(e)), + } + } + Ok(results) + } +} + +impl<'a> Drop for StreamData<'a> { + fn drop(&mut self) { + self.abort_handle.store(true, Ordering::Relaxed); + self.handle.take().map(|h| h.join()); + let _ = self.camera.stop_video(self.stream); + } +} + impl BcCamera { /// /// Starts the video stream /// - /// # Parameters + /// The returned object manages the data stream, when it is dropped + /// the video stop signal is sent to the camera /// - /// * `data_outs` - This should be a struct that implements the [`StreamOutput`] trait + /// To pull frames from the camera's buffer use `recv_data` on the returned object /// - /// * `stream_name` - This is a [`Stream`] that controls the stream to request from the camera. - /// Selecting [`Stream::Main`] will select the HD stream. - /// Selecting [`Stream::Sub`] will select the SD stream. - /// Selecting [`Stream::Extern`] will select the medium quality stream (only on some camera) + /// The buffer represents number of compete messages so 1 would be one complete message + /// which may be a single audio frame or a whole video key frame + /// A value of 0 means unlimited buffer size /// - /// # Returns - /// - /// This will block forever or return an error when the camera connection is dropped - /// - pub fn start_video( - &self, - data_outs: &mut Outputs, - stream: Stream, - abort_handle: Arc, - ) -> Result<()> - where - Outputs: StreamOutput, - { - let connection = self - .connection - .as_ref() - .expect("Must be connected to start video"); + pub fn start_video(&self, stream: Stream, buffer_size: usize) -> Result> { + let connection = self.get_connection(); let msg_num = self.new_message_num(); - let sub_video = connection.subscribe(msg_num)?; + let channel_id = self.channel_id; - // 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 0 - // - externStream is 0 - let stream_code = match stream { - Stream::Main => 0, - Stream::Sub => 1, - Stream::Extern => 0, + let abort_handle = Arc::new(AtomicBool::new(false)); + let (tx, rx) = match buffer_size { + 0 => unbounded(), + n => bounded(n), }; - // 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 abort_handle_thread = abort_handle.clone(); + let handle = thread::spawn(move || { + let sub_video = connection.subscribe(msg_num)?; - let stream_name = match stream { - Stream::Main => "mainStream", - Stream::Sub => "subStream", - Stream::Extern => "externStream", - } - .to_string(); - - let start_video = Bc::new_from_xml( - BcMeta { - msg_id: MSG_ID_VIDEO, - channel_id: self.channel_id, - msg_num, - stream_type: stream_code, - response_code: 0, - class: 0x6414, // IDK why - }, - BcXml { - preview: Some(Preview { - version: xml_ver(), - channel_id: self.channel_id, - handle, - stream_type: Some(stream_name), - }), - ..Default::default() - }, - ); - - sub_video.send(start_video)?; - - let msg = sub_video.rx.recv_timeout(RX_TIMEOUT)?; - if let BcMeta { - response_code: 200, .. - } = msg.meta - { - } else { - return Err(Error::UnintelligibleReply { - reply: Box::new(msg), - why: "The camera did not accept the stream start command.", - }); - } - - let mut media_sub = BinarySubscriber::from_bc_sub(&sub_video); - - let mut result = Ok(()); - - while !abort_handle.load(Ordering::Relaxed) { - 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) => { - result = Ok(()); - break; - } - Err(e) => { - result = Err(e); - break; - } + // 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 0 + // - externStream is 0 + let stream_code = match stream { + Stream::Main => 0, + Stream::Sub => 1, + Stream::Extern => 0, }; - } - // Aborted - println!("Stopping video: {:?}", result); - self.stop_video(stream)?; - println!("Video stopped"); - result + // 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 stream_name = match stream { + Stream::Main => "mainStream", + Stream::Sub => "subStream", + Stream::Extern => "externStream", + } + .to_string(); + + let start_video = Bc::new_from_xml( + BcMeta { + msg_id: MSG_ID_VIDEO, + channel_id, + msg_num, + stream_type: stream_code, + response_code: 0, + class: 0x6414, // IDK why + }, + BcXml { + preview: Some(Preview { + version: xml_ver(), + channel_id, + handle, + stream_type: Some(stream_name), + }), + ..Default::default() + }, + ); + + sub_video.send(start_video)?; + + let msg = sub_video.rx.recv_timeout(RX_TIMEOUT)?; + if let BcMeta { + response_code: 200, .. + } = msg.meta + { + } else { + return Err(Error::UnintelligibleReply { + reply: Box::new(msg), + why: "The camera did not accept the stream start command.", + }); + } + + let mut media_sub = BinarySubscriber::from_bc_sub(&sub_video); + + while !abort_handle_thread.load(Ordering::Relaxed) { + let bc_media = BcMedia::deserialize(&mut media_sub).map_err(Error::from); + // We now have a complete interesting packet. Send it to on the callback + if tx.send(bc_media).is_err() { + break; // Connection dropped + } + } + Ok(()) + }); + + Ok(StreamData { + camera: self, + handle: Some(handle), + rx, + stream, + abort_handle, + }) } /// Stop a camera from sending more stream data. pub fn stop_video(&self, stream: Stream) -> Result<()> { - let connection = self - .connection - .as_ref() - .expect("Must be connected to stop video"); + let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub_video = connection.subscribe(msg_num)?; diff --git a/crates/core/src/bc_protocol/talk.rs b/crates/core/src/bc_protocol/talk.rs index b331c1e..a313992 100644 --- a/crates/core/src/bc_protocol/talk.rs +++ b/crates/core/src/bc_protocol/talk.rs @@ -1,7 +1,7 @@ use super::{BcCamera, Error, Result, RX_TIMEOUT}; use crate::{bc::model::*, bc::xml::*, bcmedia::model::*}; +use crossbeam_channel::Receiver; use std::io::{BufRead, Error as IoError, ErrorKind, Read}; -use std::sync::mpsc::Receiver; type IoResult = std::result::Result; @@ -14,7 +14,7 @@ impl BcCamera { /// It is also sent when the request for talk config returns status code 422 /// pub fn talk_stop(&self) -> Result<()> { - let connection = self.connection.as_ref().expect("Must be connected"); + let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub = connection.subscribe(msg_num)?; @@ -58,10 +58,7 @@ impl BcCamera { /// Requests the [`TalkAbility`] xml /// pub fn talk_ability(&self) -> Result { - let connection = self - .connection - .as_ref() - .expect("Must be connected to get time"); + let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub_get = connection.subscribe(msg_num)?; let get = Bc { @@ -120,7 +117,7 @@ impl BcCamera { /// /// pub fn talk(&self, adpcm: &[u8], talk_config: TalkConfig) -> Result<()> { - let connection = self.connection.as_ref().expect("Must be connected"); + let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub = connection.subscribe(msg_num)?; @@ -261,7 +258,7 @@ impl BcCamera { /// /// pub fn talk_stream(&self, rx: Receiver>, talk_config: TalkConfig) -> Result<()> { - let connection = self.connection.as_ref().expect("Must be connected"); + let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub = connection.subscribe(msg_num)?; diff --git a/crates/core/src/bc_protocol/time.rs b/crates/core/src/bc_protocol/time.rs index 30d6cd5..75b6384 100644 --- a/crates/core/src/bc_protocol/time.rs +++ b/crates/core/src/bc_protocol/time.rs @@ -11,10 +11,7 @@ impl BcCamera { /// returns either an error or an option with the offsetted date time /// pub fn get_time(&self) -> Result> { - let connection = self - .connection - .as_ref() - .expect("Must be connected to get time"); + let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub_get_general = connection.subscribe(msg_num)?; let get = Bc { @@ -98,10 +95,7 @@ impl BcCamera { /// returns Ok(()) or error /// pub fn set_time(&self, timestamp: OffsetDateTime) -> Result<()> { - let connection = self - .connection - .as_ref() - .expect("Must be connected to set time"); + let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub_set_general = connection.subscribe(msg_num)?; let set = Bc::new_from_xml( diff --git a/crates/core/src/bc_protocol/version.rs b/crates/core/src/bc_protocol/version.rs index 1c6056e..3aa9d0f 100644 --- a/crates/core/src/bc_protocol/version.rs +++ b/crates/core/src/bc_protocol/version.rs @@ -4,10 +4,7 @@ use crate::bc::{model::*, xml::*}; impl BcCamera { /// Request the [VersionInfo] xml pub fn version(&self) -> Result { - let connection = self - .connection - .as_ref() - .expect("Must be connected to get version info"); + let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub_version = connection.subscribe(msg_num)?; diff --git a/src/config.rs b/src/config.rs index 77c0428..10d27fc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,7 +2,6 @@ use lazy_static::lazy_static; use regex::Regex; use serde::Deserialize; use std::clone::Clone; -use std::time::Duration; use validator::{Validate, ValidationError}; use validator_derive::Validate; @@ -55,12 +54,6 @@ pub(crate) struct CameraConfig { pub(crate) username: String, pub(crate) password: Option, - // no longer used, but still here so we can warn users: - pub(crate) timeout: Option, - - // no longer used, but still here so we can warn users: - pub(crate) format: Option, - #[validate(regex( path = "RE_STREAM_SRC", message = "Incorrect stream source", diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index 5f6f3d6..69f0ecb 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -4,9 +4,7 @@ use crate::utils::AddressOrUid; use anyhow::{anyhow, Context, Result}; use crossbeam_channel::{unbounded, Receiver, Sender, TryRecvError}; use log::*; -use neolink_core::bc_protocol::{ - BcCamera, Error as BcError, LightState, MotionOutput, MotionOutputError, MotionStatus, -}; +use neolink_core::bc_protocol::{BcCamera, Error as BcError, LightState, MotionStatus}; use std::sync::{Arc, Mutex}; pub(crate) enum Messages { @@ -106,7 +104,7 @@ impl<'a> EventCam<'a> { "{}: Connecting to camera at {}", camera_config.name, camera_addr ); - let mut camera = camera_addr + let camera = camera_addr .connect_camera(camera_config.channel_id) .with_context(|| { format!( @@ -125,27 +123,27 @@ impl<'a> EventCam<'a> { // Shararble cameras let arc_cam = Arc::new(camera); - let cam_motion = arc_cam.clone(); - let cam_mesg = arc_cam; - let mut motion_callback = MotionCallback { + let mut motion_thread = MotionThread { app: app.clone(), tx, + camera: arc_cam.clone(), name: camera_config.name.to_string(), }; let mut message_handler = MessageHandler { app: app.clone(), rx, - camera: cam_mesg, + camera: arc_cam, name: camera_config.name.to_string(), }; let _ = crossbeam::scope(|s| { info!("{}: Listening to Camera Motion", camera_config.name); s.spawn(|_| { - let _ = cam_motion.listen_on_motion(&mut motion_callback); - // If listen_on_motion returns then camera disconnect + if let Err(e) = motion_thread.run() { + error!("Motion thread aborted: {:?}", e); + } app.abort(&camera_config.name); }); @@ -159,38 +157,40 @@ impl<'a> EventCam<'a> { } } -struct MotionCallback { +struct MotionThread { app: Arc, tx: Arc>>, + camera: Arc, name: String, } -impl MotionOutput for MotionCallback { - fn motion_recv(&mut self, motion_status: MotionStatus) -> MotionOutputError { - if self.app.running(&format!("app:{}", self.name)) { - match motion_status { - MotionStatus::Start => { - (self - .tx - .lock() - .map_err(|_| BcError::Other("Failed to lock mutex"))?) - .send(Messages::MotionStart) - .map_err(|_| BcError::Other("Failed to send Message"))?; +impl MotionThread { + fn run(&mut self) -> Result<()> { + let mut motion_data = self.camera.listen_on_motion()?; + while self.app.running(&format!("app:{}", self.name)) { + for motion_status in motion_data.consume_motion_events()?.drain(..) { + match motion_status { + MotionStatus::Start(_) => { + (self + .tx + .lock() + .map_err(|_| BcError::Other("Failed to lock mutex"))?) + .send(Messages::MotionStart) + .map_err(|_| BcError::Other("Failed to send Message"))?; + } + MotionStatus::Stop(_) => { + (self + .tx + .lock() + .map_err(|_| BcError::Other("Failed to lock mutex"))?) + .send(Messages::MotionStop) + .map_err(|_| BcError::Other("Failed to send Message"))?; + } + _ => {} } - MotionStatus::Stop => { - (self - .tx - .lock() - .map_err(|_| BcError::Other("Failed to lock mutex"))?) - .send(Messages::MotionStop) - .map_err(|_| BcError::Other("Failed to send Message"))?; - } - _ => {} } - Ok(true) - } else { - Ok(false) } + Ok(()) } } diff --git a/src/rtsp/abort.rs b/src/rtsp/abort.rs index 1940bb0..e3e8fa4 100644 --- a/src/rtsp/abort.rs +++ b/src/rtsp/abort.rs @@ -3,7 +3,7 @@ use std::sync::{ Arc, }; -#[derive(Clone)] +#[derive(Clone, Default)] pub(crate) struct AbortHandle { live: Arc, } @@ -19,6 +19,10 @@ impl AbortHandle { 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 b693dfb..37c8770 100644 --- a/src/rtsp/gst.rs +++ b/src/rtsp/gst.rs @@ -6,14 +6,13 @@ mod maybe_inputselect; pub(crate) use self::maybe_app_src::MaybeAppSrc; pub(crate) use self::maybe_inputselect::MaybeInputSelect; -use super::state::States; // use super::adpcm::adpcm_to_pcm; // use super::errors::Error; use gstreamer::prelude::Cast; use gstreamer::{Bin, Structure}; use gstreamer_app::AppSrc; //use gstreamer_rtsp::RTSPLowerTrans; -use anyhow::anyhow; +use anyhow::{anyhow, Context}; use gstreamer_rtsp::RTSPAuthMethod; pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate}; use gstreamer_rtsp_server::glib; @@ -24,10 +23,7 @@ use gstreamer_rtsp_server::{ RTSP_TOKEN_MEDIA_FACTORY_ROLE, }; use log::*; -use neolink_core::{ - bc_protocol::{Error, StreamOutput, StreamOutputError}, - bcmedia::model::*, -}; +use neolink_core::bcmedia::model::*; use std::collections::HashSet; use std::fs; use std::io; @@ -38,15 +34,16 @@ type AnyResult = std::result::Result; pub(crate) struct RtspServer { server: GstRTSPServer, + main_loop: glib::MainLoop, } -#[derive(Eq, PartialEq, Debug)] +#[derive(Eq, PartialEq, Debug, Copy, Clone)] pub(crate) enum InputMode { Live, Paused, } -#[derive(Eq, PartialEq, Debug)] +#[derive(Eq, PartialEq, Debug, Copy, Clone)] pub(crate) enum PausedSources { TestSrc, Still, @@ -62,7 +59,6 @@ pub(crate) struct GstOutputs { video_format: Option, audio_format: Option, factory: RTSPMediaFactory, - state: Option, when_paused: PausedSources, last_iframe: Option>, } @@ -83,63 +79,6 @@ enum StreamFormat { Adpcm(u16), } -impl StreamOutput for GstOutputs { - fn stream_recv(&mut self, media: BcMedia) -> StreamOutputError { - let mut should_continue = true; - - // Ensure stream is on cam mode - self.set_input_source(InputMode::Live).map_err(|e| { - Error::OtherString(format!("Cannot set stream input to camera source {:?}", e)) - })?; - - match media { - BcMedia::Iframe(payload) => { - let video_type = match payload.video_type { - VideoType::H264 => StreamFormat::H264, - VideoType::H265 => StreamFormat::H265, - }; - self.set_format(Some(video_type)); - self.vidsrc.write_all(&payload.data).map_err(|e| { - Error::OtherString(format!("Cannot write IFrame to vidsrc {:?}", e)) - })?; - self.last_iframe = Some(payload.data); - // Only stop on an iframe so we have the - // last frame to show - if let Some(state) = self.state.as_ref() { - should_continue = state.should_stream() - } - } - BcMedia::Pframe(payload) => { - let video_type = match payload.video_type { - VideoType::H264 => StreamFormat::H264, - VideoType::H265 => StreamFormat::H265, - }; - self.set_format(Some(video_type)); - self.vidsrc.write_all(&payload.data).map_err(|e| { - Error::OtherString(format!("Cannot write PFrame to vidsrc {:?}", e)) - })?; - } - BcMedia::Aac(payload) => { - self.set_format(Some(StreamFormat::Aac)); - self.audsrc.write_all(&payload.data).map_err(|e| { - Error::OtherString(format!("Cannot write AAC to audsrc {:?}", e)) - })?; - } - BcMedia::Adpcm(payload) => { - self.set_format(Some(StreamFormat::Adpcm(payload.data.len() as u16))); - self.audsrc.write_all(&payload.data).map_err(|e| { - Error::OtherString(format!("Cannot write ADPCM to audsrc {:?}", e)) - })?; - } - _ => { - //Ignore other BcMedia like InfoV1 and InfoV2 - } - } - - Ok(should_continue) - } -} - impl GstOutputs { pub(crate) fn from_appsrcs( vidsrc: MaybeAppSrc, @@ -157,15 +96,53 @@ impl GstOutputs { when_paused: PausedSources::None, factory: RTSPMediaFactory::new(), last_iframe: Default::default(), - state: Default::default(), }; result.apply_format(); result } - pub(crate) fn set_state(&mut self, state: States) { - self.vidsrc.state = Some(state.clone()); - self.state = Some(state) + pub(crate) fn stream_recv(&mut self, media: BcMedia) -> AnyResult<()> { + // Ensure stream is on cam mode + match media { + BcMedia::Iframe(payload) => { + let video_type = match payload.video_type { + VideoType::H264 => StreamFormat::H264, + VideoType::H265 => StreamFormat::H265, + }; + self.set_format(Some(video_type)); + self.vidsrc + .write_all(&payload.data) + .with_context(|| "Cannot write IFrame to vidsrc")?; + self.last_iframe = Some(payload.data); + } + BcMedia::Pframe(payload) => { + let video_type = match payload.video_type { + VideoType::H264 => StreamFormat::H264, + VideoType::H265 => StreamFormat::H265, + }; + self.set_format(Some(video_type)); + self.vidsrc + .write_all(&payload.data) + .with_context(|| "Cannot write PFrame to vidsrc")?; + } + BcMedia::Aac(payload) => { + self.set_format(Some(StreamFormat::Aac)); + self.audsrc + .write_all(&payload.data) + .with_context(|| "Cannot write AAC to audsrc")?; + } + BcMedia::Adpcm(payload) => { + self.set_format(Some(StreamFormat::Adpcm(payload.data.len() as u16))); + self.audsrc + .write_all(&payload.data) + .with_context(|| "Cannot write ADPCM to audsrc")?; + } + _ => { + //Ignore other BcMedia like InfoV1 and InfoV2 + } + } + + Ok(()) } pub(crate) fn set_input_source(&mut self, input_source: InputMode) -> AnyResult<()> { @@ -359,6 +336,10 @@ impl GstOutputs { debug!("Gstreamer launch str: {:?}", launch_str); self.factory.set_launch(launch_str); } + + pub(crate) fn is_connected(&self) -> bool { + self.vidsrc.is_connected() || self.audsrc.is_connected() + } } impl Default for RtspServer { @@ -372,13 +353,14 @@ impl RtspServer { gstreamer::init().expect("Gstreamer should not explode"); RtspServer { server: GstRTSPServer::new(), + main_loop: glib::MainLoop::new(None, false), } } - pub(crate) fn add_stream( + pub(crate) fn add_stream>( &self, paths: &[&str], - permitted_users: &HashSet<&str>, + permitted_users: &HashSet, ) -> Result { let mounts = self .server @@ -407,7 +389,7 @@ impl RtspServer { debug!( "Permitting {} to access {}", // This is hashmap or (iter) equivalent of join, it requres itertools - itertools::Itertools::intersperse(permitted_users.iter().cloned(), ", ") + itertools::Itertools::intersperse(permitted_users.iter().map(|i| i.as_ref()), ", ") .collect::(), paths.join(", ") ); @@ -424,14 +406,14 @@ impl RtspServer { .expect("Media source's element should be a bin"); let app_src = bin .by_name_recurse_up("vidsrc") - .expect("write_src must be present in created bin") + .expect("vidsrc must be present in created bin") .dynamic_cast::() .expect("Source element is expected to be an appsrc!"); let _ = tx.send(app_src); // Receiver may be dropped, don't panic if so let app_src_aud = bin .by_name_recurse_up("audsrc") - .expect("write_src must be present in created bin") + .expect("audsrc must be present in created bin") .dynamic_cast::() .expect("Source element is expected to be an appsrc!"); let _ = tx_aud.send(app_src_aud); // Receiver may be dropped, don't panic if so @@ -454,14 +436,25 @@ impl RtspServer { Ok(outputs) } - pub(crate) fn add_permitted_roles( + pub(crate) fn remove_stream(&self, paths: &[&str]) -> Result<()> { + let mounts = self + .server + .mount_points() + .expect("The server should have mountpoints"); + for path in paths { + mounts.remove_factory(path); + } + Ok(()) + } + + pub(crate) fn add_permitted_roles>( &self, factory: &RTSPMediaFactory, - permitted_roles: &HashSet<&str>, + permitted_roles: &HashSet, ) { for permitted_role in permitted_roles { factory.add_role_from_structure(&Structure::new( - permitted_role, + permitted_role.as_ref(), &[ (*RTSP_PERM_MEDIA_FACTORY_ACCESS, &true), (*RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, &true), @@ -482,7 +475,12 @@ impl RtspServer { // FYI: If no RTSP_PERM_MEDIA_FACTORY_ACCESS then server returns 404 not found // If yes RTSP_PERM_MEDIA_FACTORY_ACCESS but no RTSP_PERM_MEDIA_FACTORY_CONSTRUCT // server returns 401 not authourised - if !permitted_roles.contains(&"anonymous") { + if !permitted_roles + .iter() + .map(|i| i.as_ref()) + .collect::>() + .contains(&"anonymous") + { factory.add_role_from_structure(&Structure::new( "anonymous", &[(*RTSP_PERM_MEDIA_FACTORY_ACCESS, &true)], @@ -534,7 +532,11 @@ impl RtspServer { let _ = self.server.attach(None); // Run the Glib main loop. - let main_loop = glib::MainLoop::new(None, false); - main_loop.run(); + self.main_loop.run(); + } + + #[allow(dead_code)] + pub(crate) fn quit(&self) { + self.main_loop.quit(); } } diff --git a/src/rtsp/gst/maybe_app_src.rs b/src/rtsp/gst/maybe_app_src.rs index d001eb8..7198536 100644 --- a/src/rtsp/gst/maybe_app_src.rs +++ b/src/rtsp/gst/maybe_app_src.rs @@ -1,32 +1,24 @@ use super::*; -use std::sync::mpsc::{sync_channel, Receiver, SyncSender}; +use crossbeam_channel::{bounded, Receiver, Sender}; /// A Write implementation around AppSrc that also allows delaying the creation of the AppSrc /// until later, discarding written data until the AppSrc is provided. pub(crate) struct MaybeAppSrc { rx: Receiver, app_src: Option, - pub(super) state: Option, } impl MaybeAppSrc { /// Creates a MaybeAppSrc. Also returns a Sender that you must use to provide an AppSrc as /// soon as one is available. When it is received, the MaybeAppSrc will start pushing data /// into the AppSrc when write() is called. - pub(crate) fn new_with_tx() -> (Self, SyncSender) { - let (tx, rx) = sync_channel(3); // The sender should not send very often - ( - MaybeAppSrc { - rx, - app_src: None, - state: Default::default(), - }, - tx, - ) + pub(crate) fn new_with_tx() -> (Self, Sender) { + let (tx, rx) = bounded(3); // The sender should not send very often + (MaybeAppSrc { rx, app_src: None }, tx) } - /// Flushes data to Gstreamer on a problem communicating with the underlying video source. - pub(crate) fn on_stream_error(&mut self) { + /// Calls end of stream to Gstreamer, using during drop + pub(crate) fn end_of_stream(&mut self) { if let Some(src) = self.try_get_src() { // Ignore "errors" from Gstreamer such as FLUSHING, which are not really errors. let _ = src.end_of_stream(); @@ -40,12 +32,13 @@ impl MaybeAppSrc { fn try_get_src(&mut self) -> Option<&AppSrc> { while let Ok(src) = self.rx.try_recv() { self.app_src = Some(src); - if let Some(state) = self.state.as_ref() { - state.set_client_connected(true); - } } self.app_src.as_ref() } + + pub(crate) fn is_connected(&self) -> bool { + self.app_src.is_some() + } } impl Write for MaybeAppSrc { @@ -66,9 +59,6 @@ impl Write for MaybeAppSrc { let res = app_src.push_buffer(gst_buf); //.map_err(|e| io::Error::new(io::ErrorKind::Other, Box::new(e)))?; if res.is_err() { self.app_src = None; - if let Some(state) = self.state.as_ref() { - state.set_client_connected(false); - } } Ok(buf.len()) } @@ -77,3 +67,9 @@ impl Write for MaybeAppSrc { Ok(()) } } + +impl Drop for MaybeAppSrc { + fn drop(&mut self) { + self.end_of_stream(); + } +} diff --git a/src/rtsp/gst/maybe_inputselect.rs b/src/rtsp/gst/maybe_inputselect.rs index ecb053e..0366162 100644 --- a/src/rtsp/gst/maybe_inputselect.rs +++ b/src/rtsp/gst/maybe_inputselect.rs @@ -1,9 +1,9 @@ use anyhow::{anyhow, Error as Anyhow}; +use crossbeam_channel::{bounded, Receiver, Sender}; use gstreamer::prelude::ElementExt; use gstreamer::prelude::ObjectExt; use gstreamer::{Element, Pad}; use log::*; -use std::sync::mpsc::{sync_channel, Receiver, SyncSender}; pub(crate) struct MaybeInputSelect { rx: Receiver, @@ -11,8 +11,8 @@ pub(crate) struct MaybeInputSelect { } impl MaybeInputSelect { - pub(crate) fn new_with_tx() -> (Self, SyncSender) { - let (tx, rx) = sync_channel(3); // The sender should not send very often + pub(crate) fn new_with_tx() -> (Self, Sender) { + let (tx, rx) = bounded(3); // The sender should not send very often (MaybeInputSelect { rx, typefind: None }, tx) } diff --git a/src/rtsp/gst/maybe_outputselect.rs b/src/rtsp/gst/maybe_outputselect.rs index ab44cb6..bbdd631 100644 --- a/src/rtsp/gst/maybe_outputselect.rs +++ b/src/rtsp/gst/maybe_outputselect.rs @@ -1,5 +1,5 @@ use gstreamer::Element; -use std::sync::mpsc::{sync_channel, Receiver, SyncSender}; +use crossbeam_channel::{sync_channel, Receiver, SyncSender}; pub(crate) struct MaybeOutputSelect { rx: Receiver, diff --git a/src/rtsp/gst/maybe_typefind.rs b/src/rtsp/gst/maybe_typefind.rs index ca32b97..fbd19b5 100644 --- a/src/rtsp/gst/maybe_typefind.rs +++ b/src/rtsp/gst/maybe_typefind.rs @@ -1,5 +1,5 @@ use gstreamer::Element; -use std::sync::mpsc::{sync_channel, Receiver, SyncSender}; +use crossbeam_channel::{sync_channel, Receiver, SyncSender}; pub(crate) struct MaybeTypeFind { rx: Receiver, diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index ed9d4f3..577f10e 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -28,145 +28,202 @@ /// neolink rtsp --config=config.toml /// ``` /// -use anyhow::{Context, Result}; +use anyhow::{anyhow, Context, Result}; +use crossbeam::utils::Backoff; use log::*; -use neolink_core::bc_protocol::{BcCamera, Stream}; -use std::collections::HashSet; -use std::sync::{ - atomic::{AtomicBool, Ordering}, - Arc, Mutex, -}; +use std::sync::Arc; use std::time::Duration; mod abort; mod cmdline; mod gst; -mod motion; -mod state; +mod states; + +use abort::AbortHandle; +use states::{RtspCamera, StateInfo}; use super::config::{CameraConfig, Config, UserConfig}; -use crate::utils::AddressOrUid; pub(crate) use cmdline::Opt; -use gst::{GstOutputs, InputMode, PausedSources, RtspServer, TlsAuthenticationMode}; -use motion::MotionStream; -use state::States; +use gst::{RtspServer, TlsAuthenticationMode}; /// Entry point for the rtsp subcommand /// /// Opt is the command line options -pub(crate) fn main(_opt: Opt, config: Config) -> Result<()> { - let rtsp = &RtspServer::new(); +pub(crate) fn main(_opt: Opt, mut config: Config) -> Result<()> { + let rtsp = Arc::new(RtspServer::new()); - set_up_tls(&config, rtsp); + set_up_tls(&config, &rtsp); - set_up_users(&config.users, rtsp); + set_up_users(&config.users, &rtsp); if config.certificate.is_none() && !config.users.is_empty() { warn!( "Without a server certificate, usernames and passwords will be exchanged in plaintext!" ) } - crossbeam::scope(|s| { - for camera in config.cameras { - if camera.format.is_some() { - warn!("The format config option of the camera has been removed in favour of auto detection.") - } - // Let subthreads share the camera object; in principle I think they could share - // the object as it sits in the config.cameras block, but I have not figured out the - // syntax for that. - let arc_cam = Arc::new(camera); + let abort = AbortHandle::new(); - let permitted_users = - get_permitted_users(config.users.as_slice(), &arc_cam.permitted_users); + let arc_user_config = Arc::new(config.users); + for camera_config in config.cameras.drain(..) { + // Spawn each camera controller in it's own thread + let user_config = arc_user_config.clone(); + let arc_rtsp = rtsp.clone(); + let abort_handle = abort.clone(); + s.spawn(move |_| { + let backoff = Backoff::new(); - let mut stream_outputs: Vec<(Stream, Arc>)> = vec![]; - - // Set up each main and substream according to all the RTSP mount paths we support - if ["all", "both", "mainStream"].iter().any(|&e| e == arc_cam.stream) { - let paths = &[ - &*format!("/{}", arc_cam.name), - &*format!("/{}/mainStream", arc_cam.name), - ]; - let outputs = rtsp - .add_stream(paths, &permitted_users) - .unwrap(); - stream_outputs.push((Stream::Main, Arc::new(Mutex::new(outputs)))); - } - if ["all", "both", "subStream"].iter().any(|&e| e == arc_cam.stream) { - let paths = &[&*format!("/{}/subStream", arc_cam.name)]; - let outputs = rtsp - .add_stream(paths, &permitted_users) - .unwrap(); - stream_outputs.push((Stream::Sub, Arc::new(Mutex::new(outputs)))); - } - if ["all", "externStream"].iter().any(|&e| e == arc_cam.stream) { - let paths = &[&*format!("/{}/externStream", arc_cam.name)]; - let outputs = rtsp - .add_stream(paths, &permitted_users) - .unwrap(); - stream_outputs.push((Stream::Extern, Arc::new(Mutex::new(outputs)))); - } - - let main_camera = arc_cam.clone(); - s.spawn(move |_| camera_loop(&main_camera, stream_outputs, true)); + while abort_handle.is_live() { + let failure = + camera_main(&camera_config, user_config.as_slice(), arc_rtsp.clone()); + match failure { + Err(CameraFailureKind::Fatal(e)) => { + error!("{}: Fatal error: {:?}", camera_config.name, e); + break; + } + Err(CameraFailureKind::Retry(e)) => { + warn!("{}: Retryable error: {:?}", camera_config.name, e); + backoff.spin(); + } + Ok(()) => { + info!("{}: Shutting down", camera_config.name); + break; + } + } + } + }); } - + info!( + "Starting RTSP Server at {}:{}", + &config.bind_addr, config.bind_port, + ); rtsp.run(&config.bind_addr, config.bind_port); }) - .unwrap(); - + .map_err(|e| anyhow::anyhow!("Thread panicked: {:?}", e))?; Ok(()) } -fn camera_loop( - camera_config: &CameraConfig, - end_points: Vec<(Stream, Arc>)>, - manage: bool, -) -> Result<(), anyhow::Error> { - let min_backoff = Duration::from_secs(1); - let max_backoff = Duration::from_secs(15); - let mut current_backoff = min_backoff; - - loop { - if let Err(cam_err) = camera_main(camera_config, &end_points, manage) { - for (_, outputs) in end_points.iter() { - (outputs.lock().unwrap()).vidsrc.on_stream_error(); - (outputs.lock().unwrap()).audsrc.on_stream_error(); - } - // Authentication failures are permanent; we retry everything else - if cam_err.connected { - current_backoff = min_backoff; - } - if cam_err.login_fail { - error!( - "Authentication failed to camera {}, not retrying", - camera_config.name - ); - return Err(cam_err.err); - } else { - error!( - "Error streaming from camera {}, will retry in {}s: {:?}", - camera_config.name, - current_backoff.as_secs(), - cam_err.err - ) - } - - std::thread::sleep(current_backoff); - current_backoff = std::cmp::min(max_backoff, current_backoff * 2); - } else { - // Should not occur because we don't set the callback up - // in such a way that it requests graceful shutdown - return Ok(()); - } - } +enum CameraFailureKind { + Fatal(anyhow::Error), + Retry(anyhow::Error), } -struct CameraErr { - connected: bool, - login_fail: bool, - err: anyhow::Error, +fn camera_main( + config: &CameraConfig, + user_config: &[UserConfig], + rtsp: Arc, +) -> Result<(), CameraFailureKind> { + // Connect + let mut camera = RtspCamera::new(config, user_config, rtsp) + .with_context(|| format!("{}: Could not connect camera", config.name)) + .map_err(CameraFailureKind::Retry)?; + camera + .login() + .with_context(|| format!("{}: Could not login to camera", config.name)) + .map_err(CameraFailureKind::Fatal)?; + + let _ = camera.manage(); + + camera + .stream() + .with_context(|| format!("{}: Could not start stream", config.name)) + .map_err(CameraFailureKind::Retry)?; + + let backoff = Backoff::new(); + let mut motion = if config.pause.on_motion { + Some( + camera + .motion_data() + .with_context(|| { + "Could not initialise motion detection. Is it supported with this camera?" + }) + .map_err(CameraFailureKind::Retry)?, + ) + } else { + None + }; + + let motion_timeout: Duration = Duration::from_secs_f64(config.pause.motion_timeout); + loop { + match camera.get_state() { + StateInfo::Streaming => { + let can_pause = camera.can_pause(); + if config.pause.on_disconnect { + if let Some(false) = camera.client_connected() { + if can_pause { + info!("Pause on disconnect"); + camera.pause().map_err(CameraFailureKind::Retry)?; + } else { + warn!("Not ready to pause"); + } + } + } + if config.pause.on_motion { + if let Some(motion) = motion.as_mut() { + match motion + .motion_detected_within(motion_timeout) + .with_context(|| "Motion detection unexpectedly stopped") + .map_err(CameraFailureKind::Retry)? + { + Some(false) => { + if can_pause { + info!("Pause on motion"); + camera.pause().map_err(CameraFailureKind::Retry)?; + } else { + warn!("Not ready to pause"); + } + } + None => { + if can_pause { + info!("Pause on motion (start)"); + camera.pause().map_err(CameraFailureKind::Retry)?; + } else { + warn!("Not ready to pause"); + } + } + _ => {} + } + } + } + if !camera.is_running() { + return Err(CameraFailureKind::Retry(anyhow!( + "Camera has unexpectanely stopped the paused state" + ))); + } + } + StateInfo::Paused => { + if config.pause.on_disconnect { + if let Some(true) = camera.client_connected() { + info!("Resume on disconnect"); + camera.stream().map_err(CameraFailureKind::Retry)?; + } + } + if config.pause.on_motion { + if let Some(motion) = motion.as_mut() { + if let Some(true) = motion + .motion_detected_within(motion_timeout) + .with_context(|| "Motion detection unexpectedly stopped") + .map_err(CameraFailureKind::Retry)? + { + info!("Resume on motion"); + camera.stream().map_err(CameraFailureKind::Retry)?; + } + } + } + if !camera.is_running() { + return Err(CameraFailureKind::Retry(anyhow!( + "Camera has unexpectanely stopped the paused state" + ))); + } + } + _ => { + return Err(CameraFailureKind::Retry(anyhow!( + "Camera has unexpectanely stopped the paused state" + ))); + } + } + backoff.spin(); + } } fn set_up_tls(config: &Config, rtsp: &RtspServer) { @@ -191,278 +248,3 @@ fn set_up_users(users: &[UserConfig], rtsp: &RtspServer) { rtsp.set_credentials(&credentials) .expect("Failed to set up users"); } - -fn get_permitted_users<'a>( - users: &'a [UserConfig], - // not idiomatic as a function argument, but this fn translates the config struct directly: - permitted_users: &'a Option>, -) -> HashSet<&'a str> { - // Helper to build hashset of all users in `users`: - let all_users_hash = || users.iter().map(|u| u.name.as_str()).collect(); - - match permitted_users { - // If in the camera config there is the user "anyone", or if none is specified but users - // are defined at all, then we add all users to the camera's allowed list. - Some(p) if p.iter().any(|u| u == "anyone") => all_users_hash(), - None if !users.is_empty() => all_users_hash(), - - // The user specified permitted_users - Some(p) => p.iter().map(String::as_str).collect(), - - // The user didn't specify permitted_users, and there are none defined anyway - None => ["anonymous"].iter().cloned().collect(), - } -} - -fn camera_main( - camera_config: &CameraConfig, - end_points: &[(Stream, Arc>)], - manage: bool, -) -> Result<(), CameraErr> { - let mut connected = false; - let mut login_fail = false; - (|| -> Result<(), anyhow::Error> { - let camera_addr = - AddressOrUid::new(&camera_config.camera_addr, &camera_config.camera_uid).unwrap(); - let mut camera = - camera_addr.connect_camera(camera_config.channel_id) - .with_context(|| { - format!( - "Failed to connect to camera {} at {} on channel {}", - camera_config.name, camera_addr, camera_config.channel_id - ) - })?; - - if camera_config.timeout.is_some() { - warn!("The undocumented `timeout` config option has been removed and is no longer needed."); - warn!("Please update your config file."); - } - - info!( - "{}: Connecting to camera at {}", - camera_config.name, camera_addr - ); - - info!("{}: Logging in", camera_config.name); - camera.login(&camera_config.username, camera_config.password.as_deref()).map_err(|e| - { - if let neolink_core::Error::AuthFailed = e { - login_fail = true; - } - e - } - ).with_context(|| format!("Failed to login to {}", camera_config.name))?; - - connected = true; - info!("{}: Connected and logged in", camera_config.name); - - if manage { - do_camera_management(&mut camera, camera_config).context("Failed to manage the camera settings")?; - } - - let arc_camera_results: Arc>>> = Default::default(); - let arc_camera = Arc::new(camera); - - crossbeam::scope(|s| { - let arc_abort_handle = Arc::new(AtomicBool::new(false)); - for (stream_name, arc_outputs) in end_points.iter() { - let stream_display_name = match stream_name { - Stream::Main => "Main Stream (Clear)", - Stream::Sub => "Sub Stream (Fluent)", - Stream::Extern => "Extern Stream (Balanced)", - }; - - info!( - "{}: Starting video stream {}", - camera_config.name, stream_display_name - ); - - let camera_results = arc_camera_results.clone(); - let camera = arc_camera.clone(); - let outputs = arc_outputs.clone(); - let abort_handle = arc_abort_handle.clone(); - let abort_handle_2 = arc_abort_handle.clone(); - - s.spawn(move |_| { - let camera_result = pausable_camera(camera_config, &camera, &mut outputs.lock().unwrap(), *stream_name, abort_handle).with_context(|| format!("Error while streaming {}", camera_config.name)); - (*camera_results.lock().unwrap()).push(camera_result); - - let _ = camera.stop_video(*stream_name); - abort_handle_2.store(true, Ordering::Relaxed); - }); - } - }).unwrap(); - - let mut camera_results = arc_camera_results.lock().unwrap(); - while camera_results.len() > 0 { - camera_results.pop().unwrap()?; // Need to take ownership - } - unreachable!(); // Should always error before this - - })().map_err(|e| CameraErr{ - connected, - login_fail, - err: e, - }) -} - -fn pausable_camera( - camera_config: &CameraConfig, - camera: &Arc, - data_outs: &mut GstOutputs, - stream: Stream, - abort_handle: Arc, -) -> Result<()> { - if !camera_config.pause.on_motion && !camera_config.pause.on_disconnect { - // Stream is always on - camera - .start_video(data_outs, stream, abort_handle) - .with_context(|| format!("Error while streaming {}", camera_config.name)) - } else { - let pause_config = &camera_config.pause; - // Used to get the error from the thread - let thread_error: Arc>> = Default::default(); - - // Used to abort all sub threads if any of them request closure - // and control the motion/client state info - let state = States::new_from_camera_config(camera_config); - data_outs.set_state(state.clone()); - let paused_source = match pause_config.mode.as_str() { - "test" => PausedSources::TestSrc, - "still" => PausedSources::Still, - "black" => PausedSources::Black, - "none" => PausedSources::None, - _ => { - unreachable!() - } - }; - data_outs.set_paused_source(paused_source); - - crossbeam::scope(|s| { - let mut motion = MotionStream::new(pause_config.motion_timeout, state.clone()); - - 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(); - let motion_state = state.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 - motion_state.abort(); - debug!("Motion stopped"); - }); - - // Stream thread - let stream_camera = arc_camera; - let stream_camname = camera_config.name.to_string(); - let stream_error = thread_error.clone(); - let stream_state = state.clone(); - s.spawn(move |_| { - while stream_state.is_live() { - if stream_state.should_stream() || !data_outs.has_last_iframe() { - if let Err(e) = data_outs.set_input_source(InputMode::Live) { - *stream_error.lock().unwrap() = Some(e); - break; - } - debug!("Stream resumed"); - // Will block until the rx thread send the abort - if let Err(e) = stream_camera - .start_video(data_outs, stream, abort_handle.clone()) - .with_context(|| format!("Error while streaming {}", stream_camname)) - { - error!("Stream error {:?}", e); - // When stream errors set the thread error and abort the motion - *stream_error.lock().unwrap() = Some(e); - break; - } - debug!("Stream paused"); - } else { - if let Err(e) = data_outs.set_input_source(InputMode::Paused) { - *stream_error.lock().unwrap() = Some(e); - break; - } - // While stream is aborted we send the last iframe - if let Err(e) = data_outs.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)); - } - } - stream_state.abort(); - debug!("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()) - } -} - -fn do_camera_management(camera: &mut BcCamera, camera_config: &CameraConfig) -> Result<()> { - let cam_time = camera.get_time()?; - if let Some(time) = cam_time { - info!( - "{}: Camera time is already set: {}", - camera_config.name, time - ); - } else { - use time::OffsetDateTime; - // We'd like now_local() but it's deprecated - try to get the local time, but if no - // time zone, fall back to UTC. - let new_time = - OffsetDateTime::try_now_local().unwrap_or_else(|_| OffsetDateTime::now_utc()); - - warn!( - "{}: Camera has no time set, setting to {}", - camera_config.name, new_time - ); - camera.set_time(new_time)?; - let cam_time = camera.get_time()?; - if let Some(time) = cam_time { - info!("{}: Camera time is now set: {}", camera_config.name, time); - } else { - error!( - "{}: Camera did not accept new time (is {} an admin?)", - camera_config.name, camera_config.username - ); - } - } - - use neolink_core::bc::xml::VersionInfo; - if let Ok(VersionInfo { - firmwareVersion: firmware_version, - .. - }) = camera.version() - { - info!( - "{}: Camera reports firmware version {}", - camera_config.name, firmware_version - ); - } else { - info!( - "{}: Could not fetch version information", - camera_config.name - ); - } - - Ok(()) -} diff --git a/src/rtsp/motion.rs b/src/rtsp/motion.rs deleted file mode 100644 index 12bf35d..0000000 --- a/src/rtsp/motion.rs +++ /dev/null @@ -1,53 +0,0 @@ -use super::abort::AbortHandle; -use super::state::States; -use neolink_core::bc_protocol::{MotionOutput, MotionOutputError, MotionStatus}; -use std::time::Duration; - -pub(crate) struct MotionStream { - state: States, - timeout: Duration, - cooloff: Option, -} - -impl MotionStream { - pub(crate) fn new(timeout_seconds: f64, state: States) -> Self { - Self { - state, - timeout: Duration::from_secs_f64(timeout_seconds), - cooloff: None, - } - } -} - -impl MotionOutput for MotionStream { - fn motion_recv(&mut self, motion_status: MotionStatus) -> MotionOutputError { - match motion_status { - MotionStatus::Start => { - if let Some(handle) = &self.cooloff { - handle.abort(); - self.cooloff = None; - } - self.state.set_motion_detected(true); - } - MotionStatus::Stop => { - // Abort any current one - if let Some(handle) = &self.cooloff { - handle.abort(); - self.cooloff = None; - } - let aborter = AbortHandle::new(); - let thread_duration = self.timeout; - let thread_state = self.state.clone(); - std::thread::spawn(move || { - std::thread::sleep(thread_duration); - if aborter.is_live() { - thread_state.set_motion_detected(false); - } - }); - } - _ => {} - } - - Ok(self.state.is_live()) - } -} diff --git a/src/rtsp/state.rs b/src/rtsp/state.rs deleted file mode 100644 index ad9373d..0000000 --- a/src/rtsp/state.rs +++ /dev/null @@ -1,98 +0,0 @@ -//! Used to handle the stream states -//! -//! When should stream is true then neolink -//! pulls the video from the camera. -//! -//! Otherwise it should use a fallback image -//! -use super::CameraConfig; -use log::*; -use std::sync::{ - atomic::{AtomicBool, Ordering}, - Arc, Mutex, -}; - -enum MotionState { - Motion, - Still, - Ignore, -} - -enum RtspState { - Connected, - Disconnected, - Ignore, -} - -#[derive(Clone)] -pub(crate) struct States { - motion: Arc>, - client: Arc>, - live: Arc, -} - -impl States { - pub(crate) fn new(motion_controlled: bool, client_controlled: bool) -> Self { - let motion = match motion_controlled { - true => MotionState::Still, - false => MotionState::Ignore, - }; - let client = match client_controlled { - true => RtspState::Disconnected, - false => RtspState::Ignore, - }; - - Self { - motion: Arc::new(Mutex::new(motion)), - client: Arc::new(Mutex::new(client)), - live: Arc::new(AtomicBool::new(true)), - } - } - - pub(crate) fn new_from_camera_config(camera_config: &CameraConfig) -> Self { - let motion_controlled = camera_config.pause.on_motion; - let client_controlled = camera_config.pause.on_disconnect; - - Self::new(motion_controlled, client_controlled) - } - - pub(crate) fn should_stream(&self) -> bool { - if let MotionState::Still = *self.motion.lock().unwrap() { - return false; - } - - !matches!(*self.client.lock().unwrap(), RtspState::Disconnected) - } - - pub(crate) fn set_client_connected(&self, value: bool) { - let mut client = self.client.lock().unwrap(); - if let RtspState::Ignore = *client { - return; - } - debug!("Client state: {}", value); - *client = match value { - true => RtspState::Connected, - false => RtspState::Disconnected, - } - } - - pub(crate) fn set_motion_detected(&self, value: bool) { - let mut motion = self.motion.lock().unwrap(); - if let MotionState::Ignore = *motion { - return; - } - debug!("Motion state: {}", value); - *motion = match value { - true => MotionState::Motion, - false => MotionState::Still, - }; - } - - pub(crate) fn abort(&self) { - self.live.store(false, Ordering::Relaxed); - } - - pub(crate) fn is_live(&self) -> bool { - self.live.load(Ordering::Relaxed) - } -} diff --git a/src/rtsp/states/connected.rs b/src/rtsp/states/connected.rs new file mode 100644 index 0000000..c820afd --- /dev/null +++ b/src/rtsp/states/connected.rs @@ -0,0 +1,18 @@ +// Data for the basic connected state +// +// This state has formed the TCP/UDP tunnel +// but has not logged in +use super::{CameraState, Shared}; +use anyhow::{Error, Result}; + +#[derive(Default)] +pub(crate) struct Connected {} + +impl CameraState for Connected { + fn setup(&mut self, _shared: &Shared) -> Result<(), Error> { + Ok(()) + } + fn tear_down(&mut self, _shared: &Shared) -> Result<(), Error> { + Ok(()) + } +} diff --git a/src/rtsp/states/loggedin.rs b/src/rtsp/states/loggedin.rs new file mode 100644 index 0000000..5592e97 --- /dev/null +++ b/src/rtsp/states/loggedin.rs @@ -0,0 +1,26 @@ +// Data for the logged in state +// +// This state is logged in but is not +// streaming data +// +// It can be used to alter settings etc + +use super::{CameraState, Shared}; +use anyhow::Result; + +#[derive(Default)] +pub(crate) struct LoggedIn {} + +impl CameraState for LoggedIn { + fn setup(&mut self, shared: &Shared) -> Result<(), anyhow::Error> { + shared + .camera + .login(&shared.username, shared.password.as_deref())?; + Ok(()) + } + + fn tear_down(&mut self, shared: &Shared) -> Result<(), anyhow::Error> { + shared.camera.logout()?; + Ok(()) + } +} diff --git a/src/rtsp/states/mod.rs b/src/rtsp/states/mod.rs new file mode 100644 index 0000000..a583f46 --- /dev/null +++ b/src/rtsp/states/mod.rs @@ -0,0 +1,314 @@ +//! Handles the camera in it's different states +//! + +use anyhow::{anyhow, Context, Error, Result}; +use log::*; +use std::collections::HashSet; +use std::sync::Arc; + +use neolink_core::bc_protocol::{MotionData, Stream}; + +use crate::config::{CameraConfig, UserConfig}; +use crate::rtsp::gst::RtspServer; +use crate::utils::AddressOrUid; + +mod connected; +mod loggedin; +mod paused; +mod shared; +mod streaming; + +pub(crate) use connected::Connected; +pub(crate) use loggedin::LoggedIn; +pub(crate) use paused::Paused; +pub(crate) use shared::{CameraState, Shared}; +pub(crate) use streaming::Streaming; + +pub(crate) enum StateInfo { + Connected, + LoggedIn, + Streaming, + Paused, +} + +enum State { + Connected(Connected), + LoggedIn(LoggedIn), + Streaming(Streaming), + Paused(Paused), +} + +/// +/// The state machine representation of the camera +/// +pub(crate) struct RtspCamera { + shared: Shared, + state: State, +} + +impl RtspCamera { + pub(crate) fn new( + config: &CameraConfig, + users: &[UserConfig], + rtsp: Arc, + ) -> Result { + let camera_addr = + AddressOrUid::new(&config.camera_addr, &config.camera_uid).with_context(|| { + format!( + "Could not connect to camera {}. Please check UUID or IP:Port in the config", + config.name + ) + })?; + + info!("{}: Connecting to camera at {}", config.name, camera_addr); + let camera = camera_addr + .connect_camera(config.channel_id) + .with_context(|| { + format!( + "Failed to connect to camera {} at {} on channel {}", + config.name, camera_addr, config.channel_id + ) + })?; + + let mut streams: HashSet = Default::default(); + if ["all", "both", "mainStream", "mainstream"] + .iter() + .any(|&e| e == config.stream) + { + streams.insert(Stream::Main); + } + if ["all", "both", "subStream", "substream"] + .iter() + .any(|&e| e == config.stream) + { + streams.insert(Stream::Sub); + } + if ["both", "externStream", "externstream"] + .iter() + .any(|&e| e == config.stream) + { + streams.insert(Stream::Extern); + } + + // Used to build the list of strings to pass to gtreamer for the allowed users + let all_users_hash = || users.iter().map(|u| u.name.clone()).collect(); + let permitted_users = match &config.permitted_users { + // If in the camera config there is the user "anyone", or if none is specified but users + // are defined at all, then we add all users to the camera's allowed list. + Some(p) if p.iter().any(|u| u == "anyone") => all_users_hash(), + None if !users.is_empty() => all_users_hash(), + + // The user specified permitted_users + Some(p) => p.iter().cloned().collect(), + + // The user didn't specify permitted_users, and there are none defined anyway + None => ["anonymous".to_string()].iter().cloned().collect(), + }; + + let shared = Shared { + camera: Arc::new(camera), + addr: camera_addr, + name: config.name.clone(), + channel: config.channel_id, + username: config.username.clone(), + password: config.password.clone(), + pause: config.pause.clone(), + streams, + rtsp, + permitted_users, + }; + + let mut state: Connected = Default::default(); + state.setup(&shared)?; + + Ok(Self { + shared, + state: State::Connected(state), + }) + } + + pub(crate) fn get_state(&self) -> StateInfo { + match &self.state { + State::Connected(_) => StateInfo::Connected, + State::LoggedIn(_) => StateInfo::LoggedIn, + State::Streaming(_) => StateInfo::Streaming, + State::Paused(_) => StateInfo::Paused, + } + } + pub(crate) fn login(&mut self) -> Result<()> { + match &mut self.state { + State::Connected(ref mut connected) => { + info!("{}: Logging in", &self.shared.name); + connected.tear_down(&self.shared)?; + let mut state: LoggedIn = Default::default(); + state.setup(&self.shared)?; + self.state = State::LoggedIn(state); + info!("{}: Successfully logged in", &self.shared.name); + } + State::Streaming(ref mut connected) => { + info!("{}: Logging in", &self.shared.name); + connected.tear_down(&self.shared)?; + let mut state: LoggedIn = Default::default(); + state.setup(&self.shared)?; + self.state = State::LoggedIn(state); + info!("{}: Successfully stopped straming", &self.shared.name); + } + State::Paused(ref mut connected) => { + info!("{}: Logging in", &self.shared.name); + connected.tear_down(&self.shared)?; + let mut state: LoggedIn = Default::default(); + state.setup(&self.shared)?; + self.state = State::LoggedIn(state); + info!( + "{}: Successfully stopped paused streaming", + &self.shared.name + ); + } + State::LoggedIn(_) => { + info!("Already logged in"); + } + } + Ok(()) + } + + pub(crate) fn stream(&mut self) -> Result<()> { + match &mut self.state { + State::Connected(_) => { + return Err(anyhow!("{}: Must login first", &self.shared.name)); + } + State::Streaming(_) => { + info!("{}: Already streaming", &self.shared.name); + } + State::Paused(ref mut paused) => { + info!("{}: Resuming stream", &self.shared.name); + let outputs = paused.take_outputs()?; + + paused.tear_down(&self.shared)?; + let mut state: Streaming = Default::default(); + + state.insert_outputs(outputs)?; + + state.setup(&self.shared)?; + self.state = State::Streaming(state); + info!("{}: Successfully resumed streaming", &self.shared.name); + } + State::LoggedIn(ref mut loggedin) => { + info!("{}: Starting stream", &self.shared.name); + loggedin.tear_down(&self.shared)?; + let mut state: Streaming = Default::default(); + state.setup(&self.shared)?; + self.state = State::Streaming(state); + info!("{}: Successfully started streaming", &self.shared.name); + } + } + Ok(()) + } + + pub(crate) fn pause(&mut self) -> Result<()> { + match &mut self.state { + State::Connected(_) => { + return Err(anyhow!("{}: Must login first", &self.shared.name)); + } + State::Streaming(ref mut old_state) => { + info!("{}: Pausing stream", &self.shared.name); + let outputs = old_state.take_outputs()?; + + old_state.tear_down(&self.shared)?; + let mut state: Paused = Default::default(); + + state.insert_outputs(outputs)?; + + state.setup(&self.shared)?; + self.state = State::Paused(state); + info!("{}: Successfully paused streaming", &self.shared.name); + } + State::Paused(_) => { + info!("{}: Already paused", &self.shared.name); + } + State::LoggedIn(_) => { + info!( + "{}: Cannot pause a stream that has not started", + &self.shared.name + ); + } + } + Ok(()) + } + + pub(crate) fn client_connected(&self) -> Option { + match &self.state { + State::Streaming(state) => Some(state.client_connected()), + State::Paused(state) => Some(state.client_connected()), + _ => None, + } + } + + pub(crate) fn motion_data(&self) -> Result { + self.shared + .camera + .listen_on_motion() + .with_context(|| "Cannot get motion data") + } + + pub(crate) fn is_running(&self) -> bool { + match &self.state { + State::Streaming(state) => state.is_running(), + State::Paused(state) => state.is_running(), + _ => true, + } + } + + pub(crate) fn can_pause(&self) -> bool { + match &self.state { + State::Streaming(state) => state.can_pause(), + _ => false, + } + } + + pub(crate) fn manage(&self) -> Result<()> { + if let State::Connected(_) = self.state { + return Err(anyhow!("Cannot manage a camera that is not logged in")); + } + let cam_time = self.shared.camera.get_time()?; + if let Some(time) = cam_time { + info!("{}: Camera time is already set: {}", self.shared.name, time); + } else { + use time::OffsetDateTime; + // We'd like now_local() but it's deprecated - try to get the local time, but if no + // time zone, fall back to UTC. + let new_time = + OffsetDateTime::try_now_local().unwrap_or_else(|_| OffsetDateTime::now_utc()); + + warn!( + "{}: Camera has no time set, setting to {}", + self.shared.name, new_time + ); + self.shared.camera.set_time(new_time)?; + let cam_time = self.shared.camera.get_time()?; + if let Some(time) = cam_time { + info!("{}: Camera time is now set: {}", self.shared.name, time); + } else { + error!( + "{}: Camera did not accept new time (is {} an admin?)", + self.shared.name, self.shared.username + ); + } + } + + use neolink_core::bc::xml::VersionInfo; + if let Ok(VersionInfo { + firmwareVersion: firmware_version, + .. + }) = self.shared.camera.version() + { + info!( + "{}: Camera reports firmware version {}", + self.shared.name, firmware_version + ); + } else { + info!("{}: Could not fetch version information", self.shared.name); + } + + Ok(()) + } +} diff --git a/src/rtsp/states/paused.rs b/src/rtsp/states/paused.rs new file mode 100644 index 0000000..c8433da --- /dev/null +++ b/src/rtsp/states/paused.rs @@ -0,0 +1,183 @@ +// This is the paused state +// +// Video data is not pulled from the camera +// instead dummy data is sent into the gstreamer source + +use anyhow::{anyhow, Error, Result}; +use crossbeam::utils::Backoff; +use log::*; +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; +use std::thread::{self, JoinHandle}; + +use neolink_core::bc_protocol::Stream; + +use super::{CameraState, Shared}; + +use crate::rtsp::{ + abort::AbortHandle, + gst::{GstOutputs, InputMode, PausedSources}, +}; + +#[derive(Default)] +pub(crate) struct Paused { + handles: HashMap>>, + outputs: HashMap>>, + abort_handle: AbortHandle, +} + +impl CameraState for Paused { + fn setup(&mut self, shared: &Shared) -> Result<(), Error> { + self.abort_handle.reset(); + // Create new gst outputs + // + // Otherwise use those already present + if self.outputs.is_empty() { + let paused_source = match shared.pause.mode.as_str() { + "test" => PausedSources::TestSrc, + "still" => PausedSources::Still, + "black" => PausedSources::Black, + "none" => PausedSources::None, + _ => { + unreachable!() + } + }; + + for stream in shared.streams.iter() { + self.outputs.entry(*stream).or_insert_with_key(|stream| { + let paths = shared.get_paths(stream); + let mut output = shared + .rtsp + .add_stream( + paths + .iter() + .map(|s| s.as_str()) + .collect::>() + .as_slice(), + &shared.permitted_users, + ) + .unwrap(); + output.set_paused_source(paused_source); + Arc::new(Mutex::new(output)) + }); + } + } + + // Start the streams on their own thread with a shared abort handle + let abort_handle = self.abort_handle.clone(); + + for (stream, output) in &self.outputs { + let stream_display_name = match stream { + Stream::Main => "Main Stream (Clear)", + Stream::Sub => "Sub Stream (Fluent)", + Stream::Extern => "Extern Stream (Balanced)", + }; + + // Lock and setup output + { + let mut locked_output = output.lock().unwrap(); + locked_output.set_input_source(InputMode::Paused)?; + } + + info!( + "{}: Starting paused stream {}", + &shared.name, stream_display_name + ); + + let arc_abort_handle = abort_handle.clone(); + let output_thread = output.clone(); + + let handle = thread::spawn(move || { + let backoff = Backoff::new(); + while arc_abort_handle.is_live() { + let mut locked_output = output_thread.lock().unwrap(); + locked_output.write_last_iframe()?; + backoff.spin(); + } + Ok(()) + }); + + self.handles.entry(*stream).or_insert_with(|| handle); + } + + Ok(()) + } + + fn tear_down(&mut self, shared: &Shared) -> Result<(), Error> { + self.abort_handle.abort(); + + if !self.handles.is_empty() { + for path in shared.get_all_paths().iter() { + if let Err(e) = shared.rtsp.remove_stream(&[path]) { + return Err(anyhow!("Failed to shutdown RTSP Path {}: {:?}", path, e)); + } + } + + for (stream, handle) in self.handles.drain() { + info!("{}: Stopping paused stream {:?}", &shared.name, stream); + + match handle.join() { + Ok(Err(e)) => return Err(e), + Err(_) => return Err(anyhow!("Panicked while streaming {:?}", stream)), + Ok(Ok(_)) => {} + } + } + } + + Ok(()) + } +} + +impl Drop for Paused { + fn drop(&mut self) { + self.abort_handle.abort(); + + for (stream, handle) in self.handles.drain() { + info!("During_drop: Aborting paused stream {:?}", stream); + if let Ok(Err(e)) = handle.join() { + warn!("During drop: {:?} did not stop cleanly: {:?}", stream, e); + } else { + warn!("During drop: Panicked while streaming: {:?}", stream); + } + } + } +} + +impl Paused { + pub(crate) fn is_running(&self) -> bool { + self.handles.iter().all(|(_, h)| !h.is_finished()) && self.abort_handle.is_live() + } + + pub(crate) fn take_outputs(&mut self) -> Result> { + self.abort_handle.abort(); + for (stream, handle) in self.handles.drain() { + match handle.join() { + Ok(Err(e)) => return Err(e), + Err(_) => return Err(anyhow!("Panicked while streaming {:?}", stream)), + Ok(Ok(_)) => {} + } + } + let mut result: HashMap<_, _> = Default::default(); + for (stream, arc_mutex_output) in self.outputs.drain() { + let mutex_output = + Arc::try_unwrap(arc_mutex_output).map_err(|_| anyhow!("Failed to unwrap ARC"))?; + let output = mutex_output.into_inner()?; + result.insert(stream, output); + } + Ok(result) + } + + pub(crate) fn insert_outputs(&mut self, mut input: HashMap) -> Result<()> { + self.outputs = input + .drain() + .map(|(s, o)| (s, Arc::new(Mutex::new(o)))) + .collect(); + Ok(()) + } + + pub(crate) fn client_connected(&self) -> bool { + self.outputs + .iter() + .any(|(_, output)| output.lock().unwrap().is_connected()) + } +} diff --git a/src/rtsp/states/shared.rs b/src/rtsp/states/shared.rs new file mode 100644 index 0000000..e82c0b3 --- /dev/null +++ b/src/rtsp/states/shared.rs @@ -0,0 +1,55 @@ +//! +//! Shared data between all states +//! +//! +use anyhow::{Error, Result}; +use std::collections::HashSet; +use std::sync::Arc; + +use neolink_core::bc_protocol::{BcCamera, Stream}; + +use crate::config::PauseConfig; +use crate::rtsp::gst::RtspServer; +use crate::utils::AddressOrUid; + +#[allow(dead_code)] +pub(crate) struct Shared { + pub(super) camera: Arc, + pub(super) name: String, + pub(super) channel: u8, + pub(super) addr: AddressOrUid, + pub(super) username: String, + pub(super) password: Option, + pub(super) streams: HashSet, + pub(super) rtsp: Arc, + pub(super) permitted_users: HashSet, + pub(super) pause: PauseConfig, +} + +impl Shared { + pub(super) fn get_all_paths(&self) -> Vec { + self.streams + .iter() + .flat_map(|s| self.get_paths(s)) + .collect() + } + + pub(super) fn get_paths(&self, stream: &Stream) -> Vec { + match stream { + Stream::Main => vec![ + format!("/{}", &self.name), + format!("/{}/mainStream", &self.name), + ], + Stream::Sub => vec![format!("/{}/subStream", &self.name)], + Stream::Extern => { + vec![format!("/{}/externStream", &self.name)] + } + } + } +} + +pub(crate) trait CameraState: Default { + fn setup(&mut self, shared: &Shared) -> Result<(), Error>; + + fn tear_down(&mut self, shared: &Shared) -> Result<(), Error>; +} diff --git a/src/rtsp/states/streaming.rs b/src/rtsp/states/streaming.rs new file mode 100644 index 0000000..f45912d --- /dev/null +++ b/src/rtsp/states/streaming.rs @@ -0,0 +1,192 @@ +// This is the streaming state +// +// Data is streamed into a gstreamer source + +use anyhow::{anyhow, Error, Result}; +use crossbeam::utils::Backoff; +use log::*; +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; +use std::thread::{self, JoinHandle}; + +use neolink_core::bc_protocol::Stream; + +use super::{CameraState, Shared}; + +use crate::rtsp::{ + abort::AbortHandle, + gst::{GstOutputs, InputMode, PausedSources}, +}; + +#[derive(Default)] +pub(crate) struct Streaming { + handles: HashMap>>, + outputs: HashMap>>, + abort_handle: AbortHandle, +} + +impl CameraState for Streaming { + fn setup(&mut self, shared: &Shared) -> Result<(), Error> { + self.abort_handle.reset(); + // Create new gst outputs + // + // Otherwise use those already present + if self.outputs.is_empty() { + let paused_source = match shared.pause.mode.as_str() { + "test" => PausedSources::TestSrc, + "still" => PausedSources::Still, + "black" => PausedSources::Black, + "none" => PausedSources::None, + _ => { + unreachable!() + } + }; + + for stream in shared.streams.iter() { + self.outputs.entry(*stream).or_insert_with_key(|stream| { + let paths = shared.get_paths(stream); + let mut output = shared + .rtsp + .add_stream( + paths + .iter() + .map(|s| s.as_str()) + .collect::>() + .as_slice(), + &shared.permitted_users, + ) + .unwrap(); + output.set_paused_source(paused_source); + Arc::new(Mutex::new(output)) + }); + } + } + + // Start the streams on their own thread with a shared abort handle + let camera = &shared.camera; + let abort_handle = self.abort_handle.clone(); + + for (stream, output) in &self.outputs { + let stream_display_name = match stream { + Stream::Main => "Main Stream (Clear)", + Stream::Sub => "Sub Stream (Fluent)", + Stream::Extern => "Extern Stream (Balanced)", + }; + + // Lock and setup output + { + let mut locked_output = output.lock().unwrap(); + locked_output.set_input_source(InputMode::Live)?; + } + + info!( + "{}: Starting video stream {}", + &shared.name, stream_display_name + ); + + let arc_camera = camera.clone(); + let arc_abort_handle = abort_handle.clone(); + let output_thread = output.clone(); + + let stream_thead = *stream; + let handle = thread::spawn(move || { + let backoff = Backoff::new(); + let stream_data = arc_camera.start_video(stream_thead, 0)?; + + while arc_abort_handle.is_live() { + let mut data = stream_data.get_data()?; + let mut locked_output = output_thread.lock().unwrap(); + for datum in data.drain(..) { + locked_output.stream_recv(datum?)?; + } + backoff.spin(); + } + Ok(()) + }); + + self.handles.entry(*stream).or_insert_with(|| handle); + } + + Ok(()) + } + fn tear_down(&mut self, shared: &Shared) -> Result<(), Error> { + self.abort_handle.abort(); + + if !self.handles.is_empty() { + for path in shared.get_all_paths().iter() { + if let Err(e) = shared.rtsp.remove_stream(&[path]) { + return Err(anyhow!("Failed to shutdown RTSP Path {}: {:?}", path, e)); + } + } + + for (stream, handle) in self.handles.drain() { + match handle.join() { + Ok(Err(e)) => return Err(e), + Err(_) => return Err(anyhow!("Panicked while streaming {:?}", stream)), + Ok(Ok(_)) => {} + } + } + } + + Ok(()) + } +} + +impl Drop for Streaming { + fn drop(&mut self) { + self.abort_handle.abort(); + + for (stream, handle) in self.handles.drain() { + if let Ok(Err(e)) = handle.join() { + warn!("During drop: {:?} did not stop cleanly: {:?}", stream, e); + } else { + warn!("During drop: Panicked while streaming"); + } + } + } +} + +impl Streaming { + pub(crate) fn is_running(&self) -> bool { + self.handles.iter().all(|(_, h)| !h.is_finished()) && self.abort_handle.is_live() + } + + pub(crate) fn take_outputs(&mut self) -> Result> { + self.abort_handle.abort(); + for (stream, handle) in self.handles.drain() { + match handle.join() { + Ok(Err(e)) => return Err(e), + Err(_) => return Err(anyhow!("Panicked while streaming {:?}", stream)), + Ok(Ok(_)) => {} + } + } + let mut result: HashMap<_, _> = Default::default(); + for (stream, arc_mutex_output) in self.outputs.drain() { + let mutex_output = + Arc::try_unwrap(arc_mutex_output).map_err(|_| anyhow!("Failed to unwrap ARC"))?; + let output = mutex_output.into_inner()?; + result.insert(stream, output); + } + Ok(result) + } + + pub(crate) fn insert_outputs(&mut self, mut input: HashMap) -> Result<()> { + self.outputs = input + .drain() + .map(|(s, o)| (s, Arc::new(Mutex::new(o)))) + .collect(); + Ok(()) + } + + pub(crate) fn client_connected(&self) -> bool { + self.outputs + .iter() + .any(|(_, output)| output.lock().unwrap().is_connected()) + } + + pub(crate) fn can_pause(&self) -> bool { + self.outputs + .iter() + .all(|(_, output)| output.lock().unwrap().has_last_iframe()) + } +} diff --git a/src/talk/gst.rs b/src/talk/gst.rs index d132154..576589d 100644 --- a/src/talk/gst.rs +++ b/src/talk/gst.rs @@ -1,10 +1,10 @@ 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, }; use gstreamer_app::{AppSink, AppSinkCallbacks}; -use std::sync::mpsc::{sync_channel, Receiver, SyncSender}; use byte_slice_cast::*; @@ -20,7 +20,7 @@ pub(super) fn from_input( fn input(pipeline: Pipeline) -> Result>> { let appsink = get_sink(&pipeline)?; - let (tx, rx) = sync_channel(30); + let (tx, rx) = bounded(30); set_data_channel(&appsink, tx); @@ -69,7 +69,7 @@ fn get_sink(pipeline: &Pipeline) -> Result { .map_err(|_| anyhow!("Cannot find appsink in gstreamer, check your gstreamer plugins")) } -fn set_data_channel(appsink: &AppSink, tx: SyncSender>) { +fn set_data_channel(appsink: &AppSink, tx: Sender>) { // Getting data out of the appsink is done by setting callbacks on it. // The appsink will then call those handlers, as soon as data is available. appsink.set_callbacks( diff --git a/src/utils.rs b/src/utils.rs index ae54885..3854786 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -54,7 +54,7 @@ pub(crate) fn connect_and_login(camera_config: &CameraConfig) -> Result