Using a sharable struct to track stream state

This commit is contained in:
QuantumEntangledAndy committed 2021-10-02 14:35:00 +07:00
1 parent 13690d723f
commit 982d7aa199
10 files changed
+281 -156

No files matched your search

@@ -46,7 +46,7 @@ pub enum Error {
#[error(display = "Deserialization error")]
Deserialization(#[error(source)] bc::de::Error),
#[error(display = "BcMedia Deserialization error")]
#[error(display = "BcMedia Deserialization error (conn)")]
BcMediaDeserialization(#[error(source)] bcmedia::de::Error),
#[error(display = "Serialization error")]
-4
View File
@@ -19,10 +19,6 @@ 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)
}
+7
View File
@@ -76,6 +76,9 @@ pub(crate) struct CameraConfig {
#[validate]
pub(crate) motion: Option<MotionConfig>,
#[serde(default = "default_on_client")]
pub(crate) on_client: bool,
}
#[derive(Debug, Deserialize, Validate, Clone)]
@@ -122,6 +125,10 @@ fn default_motion_timeout() -> f64 {
1.
}
fn default_on_client() -> bool {
false
}
pub(crate) static RESERVED_NAMES: &[&str] = &["anyone", "anonymous"];
fn validate_username(name: &str) -> Result<(), ValidationError> {
if name.trim().is_empty() {
+12 -74
View File
@@ -1,13 +1,16 @@
//! This module provides an "RtspServer" abstraction that allows consumers of its API to feed it
//! data using an ordinary std::io::Write interface.
mod maybe_app_src;
pub(crate) use self::maybe_app_src::MaybeAppSrc;
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 super::abort::AbortHandle;
use anyhow::anyhow;
use gstreamer_rtsp::RTSPAuthMethod;
pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate};
@@ -41,7 +44,7 @@ pub(crate) struct GstOutputs {
video_format: Option<StreamFormat>,
audio_format: Option<StreamFormat>,
factory: RTSPMediaFactory,
live: AbortHandle,
state: Option<States>,
last_iframe: Option<Vec<u8>>,
}
@@ -75,7 +78,9 @@ impl StreamOutput for GstOutputs {
self.last_iframe = Some(payload.data);
// Only stop on an iframe so we have the
// last frame to show
should_continue = self.live.is_live();
if let Some(state) = self.state.as_ref() {
should_continue = state.should_stream()
}
}
BcMedia::Pframe(payload) => {
let video_type = match payload.video_type {
@@ -110,15 +115,16 @@ impl GstOutputs {
video_format: None,
audio_format: None,
factory: RTSPMediaFactory::new(),
live: AbortHandle::new(),
last_iframe: Default::default(),
state: Default::default(),
};
result.apply_format();
result
}
pub(crate) fn get_abort_handle(&self) -> AbortHandle {
self.live.clone()
pub(crate) fn set_state(&mut self, state: States) {
self.vidsrc.state = Some(state.clone());
self.state = Some(state)
}
pub(crate) fn has_last_iframe(&self) -> bool {
@@ -343,71 +349,3 @@ impl RtspServer {
main_loop.run();
}
}
mod maybe_app_src {
use super::*;
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
/// 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<AppSrc>,
app_src: Option<AppSrc>,
}
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<AppSrc>) {
let (tx, rx) = sync_channel(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) {
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();
}
}
/// Attempts to retrieve the AppSrc that should be passed in by the caller of new_with_tx
/// at some point after this struct has been created. At that point, we swap over to
/// owning the AppSrc directly. This function handles either case and returns the AppSrc,
/// or None if the caller has not yet sent one.
fn try_get_src(&mut self) -> Option<&AppSrc> {
while let Some(src) = self.rx.try_recv().ok() {
self.app_src = Some(src);
}
self.app_src.as_ref()
}
}
impl Write for MaybeAppSrc {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
// If we have no AppSrc yet, throw away the data and claim that it was written
let app_src = match self.try_get_src() {
Some(src) => src,
None => return Ok(buf.len()),
};
let mut gst_buf = gstreamer::Buffer::with_size(buf.len()).unwrap();
{
let gst_buf_mut = gst_buf.get_mut().unwrap();
let mut gst_buf_data = gst_buf_mut.map_writable().unwrap();
gst_buf_data.copy_from_slice(buf);
}
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;
}
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
}
+79
View File
@@ -0,0 +1,79 @@
use super::*;
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
/// 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<AppSrc>,
app_src: Option<AppSrc>,
pub(super) state: Option<States>,
}
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<AppSrc>) {
let (tx, rx) = sync_channel(3); // The sender should not send very often
(
MaybeAppSrc {
rx,
app_src: None,
state: Default::default(),
},
tx,
)
}
/// Flushes data to Gstreamer on a problem communicating with the underlying video source.
pub(crate) fn on_stream_error(&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();
}
}
/// Attempts to retrieve the AppSrc that should be passed in by the caller of new_with_tx
/// at some point after this struct has been created. At that point, we swap over to
/// owning the AppSrc directly. This function handles either case and returns the AppSrc,
/// or None if the caller has not yet sent one.
fn try_get_src(&mut self) -> Option<&AppSrc> {
while let Some(src) = self.rx.try_recv().ok() {
self.app_src = Some(src);
if let Some(state) = self.state.as_ref() {
state.set_client_connected(true);
}
}
self.app_src.as_ref()
}
}
impl Write for MaybeAppSrc {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
// If we have no AppSrc yet, throw away the data and claim that it was written
let app_src = match self.try_get_src() {
Some(src) => src,
None => return Ok(buf.len()),
};
let mut gst_buf = gstreamer::Buffer::with_size(buf.len()).unwrap();
{
let gst_buf_mut = gst_buf.get_mut().unwrap();
let mut gst_buf_data = gst_buf_mut.map_writable().unwrap();
gst_buf_data.copy_from_slice(buf);
}
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())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
+25
View File
@@ -0,0 +1,25 @@
use gstreamer::Element;
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
pub(crate) struct MaybeOutputSelect {
rx: Receiver<Element>,
typefind: Option<Element>,
}
impl MaybeOutputSelect {
pub(crate) fn new_with_tx() -> (Self, SyncSender<Element>) {
let (tx, rx) = sync_channel(3); // The sender should not send very often
(MaybeOutputSelect { rx, typefind: None }, tx)
}
pub(crate) fn try_get_src(&mut self) -> Option<&Element> {
while let Some(src) = self.rx.try_recv().ok() {
self.typefind = Some(src);
}
self.typefind.as_ref()
}
pub(crate) fn clear(&mut self) {
self.typefind = None;
}
}
+25
View File
@@ -0,0 +1,25 @@
use gstreamer::Element;
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
pub(crate) struct MaybeTypeFind {
rx: Receiver<Element>,
typefind: Option<Element>,
}
impl MaybeTypeFind {
pub(crate) fn new_with_tx() -> (Self, SyncSender<Element>) {
let (tx, rx) = sync_channel(3); // The sender should not send very often
(MaybeTypeFind { rx, typefind: None }, tx)
}
pub(crate) fn try_get_src(&mut self) -> Option<&Element> {
while let Some(src) = self.rx.try_recv().ok() {
self.typefind = Some(src);
}
self.typefind.as_ref()
}
pub(crate) fn clear(&mut self) {
self.typefind = None;
}
}
+21 -51
View File
@@ -30,27 +30,26 @@
///
use anyhow::{Context, Result};
use log::*;
use neolink_core::bc_protocol::{BcCamera, MotionStatus, Stream};
use neolink_core::bc_protocol::{BcCamera, Stream};
use std::collections::HashSet;
use std::fs;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use validator::Validate;
// mod adpcm;
// The command line parameters for this subcommand
mod abort;
mod cmdline;
mod config;
// The gstreamer implementation
mod abort;
mod gst;
mod motion;
mod state;
use crate::utils::AddressOrUid;
pub(crate) use cmdline::Opt;
use config::{CameraConfig, Config, UserConfig};
use gst::{GstOutputs, RtspServer, TlsAuthenticationMode};
use motion::MotionStream;
use state::States;
/// Entry point for the rtsp subcommand
///
@@ -282,24 +281,18 @@ fn camera_main(
camera_config.name, stream_display_name
);
// Used to get the error from the thread
let thread_error: Arc<Mutex<Option<anyhow::Error>>> = Default::default();
match &camera_config.motion {
Some(motion_config) => {
// Used to get the error from the thread
let thread_error: Arc<Mutex<Option<anyhow::Error>>> = 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);
outputs.set_state(state.clone());
crossbeam::scope(|s| {
let (mut motion, rx) = MotionStream::new(motion_config.timeout);
let motionthread_motion_abort = motion.get_abort_handle();
let motionthread_stream_abort = outputs.get_abort_handle();
let rxthread_motion_abort = motion.get_abort_handle();
let rxthread_stream_abort = outputs.get_abort_handle();
let streamthread_stream_abort = outputs.get_abort_handle();
let streamthread_motion_abort = motion.get_abort_handle();
// Start in the aborted state because motion defaults to still
streamthread_stream_abort.abort();
let mut motion = MotionStream::new(motion_config.timeout, state.clone());
let arc_camera = Arc::new(camera);
@@ -307,6 +300,7 @@ fn camera_main(
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
@@ -314,49 +308,26 @@ fn camera_main(
}
// Always abort the streams/motion when motion stops regardless of why
// so that this thread can reset the camera
motionthread_motion_abort.abort();
motionthread_stream_abort.abort();
motion_state.abort();
debug!("Motion stopped");
});
// Motion rx -> stream thread
s.spawn(move |_| {
// Stops when motion is stopped
// which happens when either motion or stream errors
while rxthread_motion_abort.is_live() {
let msg = rx.try_recv();
if let Ok(msg) = msg {
match msg {
MotionStatus::Start => {
debug!("Motion start");
rxthread_stream_abort.reset();
}
MotionStatus::Stop => {
debug!("Motion stop");
rxthread_stream_abort.abort();
}
_ => {}
}
}
}
println!("Motion RX Stopped");
});
// Stream thread
let stream_camera = arc_camera;
let stream_camname = camera_config.name.to_string();
let stream_error = thread_error.clone();
let stream_state = state.clone();
s.spawn(move |_| {
loop {
if streamthread_stream_abort.is_live() || ! outputs.has_last_iframe() {
debug!("Stream resumed: waiting for motion to stop");
while stream_state.is_live() {
if stream_state.should_stream() || ! outputs.has_last_iframe() {
debug!("Stream resumed");
// Will block until the rx thread send the abort
if let Err(e) = stream_camera.start_video(outputs, stream_name).with_context(|| format!("Error while streaming {}", stream_camname)) {
// When stream errors set the thread error and abort the motion
*stream_error.lock().unwrap() = Some(e);
break;
}
debug!("Stream paused: waiting for motion to start");
debug!("Stream paused");
} else {
// While stream is aborted we send the blank image
if let Err(e) = outputs.write_last_iframe().with_context(|| format!("Failed to write no motion image for {}", stream_camname)) {
@@ -366,8 +337,7 @@ fn camera_main(
std::thread::sleep(std::time::Duration::from_millis(50));
}
}
streamthread_stream_abort.abort();
streamthread_motion_abort.abort();
stream_state.abort();
println!("Stream stopped");
});
}).unwrap();
+13 -26
View File
@@ -1,31 +1,21 @@
use super::abort::AbortHandle;
use crossbeam_channel::{unbounded, Receiver, Sender};
use super::state::States;
use neolink_core::bc_protocol::{MotionOutput, MotionOutputError, MotionStatus};
use std::time::Duration;
pub(crate) struct MotionStream {
tx: Sender<MotionStatus>,
live: AbortHandle,
state: States,
timeout: Duration,
cooloff: Option<AbortHandle>,
}
impl MotionStream {
pub(crate) fn new(timeout_seconds: f64) -> (Self, Receiver<MotionStatus>) {
let (tx, rx) = unbounded();
(
Self {
tx,
live: AbortHandle::new(),
timeout: Duration::from_secs_f64(timeout_seconds),
cooloff: None,
},
rx,
)
}
pub(crate) fn get_abort_handle(&self) -> AbortHandle {
self.live.clone()
pub(crate) fn new(timeout_seconds: f64, state: States) -> Self {
Self {
state,
timeout: Duration::from_secs_f64(timeout_seconds),
cooloff: None,
}
}
}
@@ -37,7 +27,7 @@ impl MotionOutput for MotionStream {
handle.abort();
self.cooloff = None;
}
let _ = self.tx.send(motion_status);
self.state.set_motion_detected(true);
}
MotionStatus::Stop => {
// Abort any current one
@@ -47,20 +37,17 @@ impl MotionOutput for MotionStream {
}
let aborter = AbortHandle::new();
let thread_duration = self.timeout;
let thread_tx = self.tx.clone();
let thread_message = MotionStatus::Stop;
let thread_state = self.state.clone();
std::thread::spawn(move || {
std::thread::sleep(thread_duration);
if aborter.is_live() {
let _ = thread_tx.send(thread_message);
thread_state.set_motion_detected(false);
}
});
}
_ => {
let _ = self.tx.send(motion_status);
}
_ => {}
}
Ok(self.live.is_live())
Ok(self.state.is_live())
}
}
+98
View File
@@ -0,0 +1,98 @@
//! 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<Mutex<MotionState>>,
client: Arc<Mutex<RtspState>>,
live: Arc<AtomicBool>,
}
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.motion.is_some();
let client_controlled = camera_config.on_client;
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)
}
}