Added basic motion based streaming

This commit is contained in:
QuantumEntangledAndy committed 2021-09-16 11:45:09 +07:00
1 parent 02bd3fb606
commit 62a694fbe4
11 files changed
+290 -19

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
#[derive(Clone)]
pub(crate) struct AbortHandle {
live: Arc<AtomicBool>,
}
impl AbortHandle {
pub(crate) fn new() -> Self {
Self {
live: Arc::new(AtomicBool::new(true)),
}
}
pub(crate) fn abort(&self) {
self.live.store(false, Ordering::Relaxed);
}
pub(crate) fn reset(&self) {
self.live.store(true, Ordering::Relaxed);
}
pub(crate) fn is_live(&self) -> bool {
self.live.load(Ordering::Relaxed)
}
}
+37 -6
View File
@@ -7,6 +7,8 @@ use gstreamer::prelude::Cast;
use gstreamer::{Bin, Structure};
use gstreamer_app::AppSrc;
//use gstreamer_rtsp::RTSPLowerTrans;
use super::abort::AbortHandle;
use anyhow::anyhow;
use gstreamer_rtsp::RTSPAuthMethod;
pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate};
use gstreamer_rtsp_server::glib;
@@ -27,6 +29,7 @@ use std::io;
use std::io::Write;
type Result<T> = std::result::Result<T, ()>;
type AnyResult<T> = std::result::Result<T, anyhow::Error>;
pub(crate) struct RtspServer {
server: GstRTSPServer,
@@ -38,6 +41,8 @@ pub(crate) struct GstOutputs {
video_format: Option<StreamFormat>,
audio_format: Option<StreamFormat>,
factory: RTSPMediaFactory,
live: AbortHandle,
last_iframe: Option<Vec<u8>>,
}
// The stream from the camera will be using one of these formats
@@ -58,6 +63,7 @@ enum StreamFormat {
impl StreamOutput for GstOutputs {
fn stream_recv(&mut self, media: BcMedia) -> StreamOutputError {
let mut should_continue = true;
match media {
BcMedia::Iframe(payload) => {
let video_type = match payload.video_type {
@@ -66,6 +72,11 @@ impl StreamOutput for GstOutputs {
};
self.set_format(Some(video_type));
self.vidsrc.write_all(&payload.data)?;
self.last_iframe = Some(payload.data);
// Only stop on an iframe so we have the
// last frame to show
should_continue = self.live.is_live();
debug!("Iframe continue: {}", should_continue);
}
BcMedia::Pframe(payload) => {
let video_type = match payload.video_type {
@@ -87,8 +98,8 @@ impl StreamOutput for GstOutputs {
//Ignore other BcMedia like InfoV1 and InfoV2
}
}
Ok(true)
debug!("Output continue: {}", should_continue);
Ok(should_continue)
}
}
@@ -100,11 +111,31 @@ impl GstOutputs {
video_format: None,
audio_format: None,
factory: RTSPMediaFactory::new(),
live: AbortHandle::new(),
last_iframe: Default::default(),
};
result.apply_format();
result
}
pub(crate) fn get_abort_handle(&self) -> AbortHandle {
self.live.clone()
}
pub(crate) fn has_last_iframe(&self) -> bool {
self.last_iframe.is_some()
}
pub(crate) fn write_last_iframe(&mut self) -> AnyResult<()> {
self.vidsrc.write_all(
self.last_iframe
.as_ref()
.ok_or_else(|| anyhow!("No iframe data avaliable"))?
.as_slice(),
)?;
Ok(())
}
fn set_format(&mut self, format: Option<StreamFormat>) {
match format {
Some(StreamFormat::H264) | Some(StreamFormat::H265) => {
@@ -140,8 +171,7 @@ impl GstOutputs {
_ => "! fakesink".to_string(),
};
self.factory.set_launch(
&vec![
let launch_str = &vec![
"( ",
"appsrc name=vidsrc is-live=true block=true emit-signals=false max-bytes=52428800 do-timestamp=true format=GST_FORMAT_TIME", // 50MB max size so that it won't grow to infinite if the queue blocks
launch_vid,
@@ -149,8 +179,9 @@ impl GstOutputs {
&launch_aud,
")"
]
.join(" "),
);
.join(" ");
debug!("Gstreamer launch str: {:?}", launch_str);
self.factory.set_launch(launch_str);
}
}
+100 -5
View File
@@ -30,24 +30,27 @@
///
use anyhow::{Context, Result};
use log::*;
use neolink_core::bc_protocol::{BcCamera, Stream};
use neolink_core::bc_protocol::{BcCamera, MotionStatus, Stream};
use std::collections::HashSet;
use std::fs;
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use validator::Validate;
// mod adpcm;
/// The command line parameters for this subcommand
// The command line parameters for this subcommand
mod cmdline;
mod config;
/// The errors this subcommand can raise
// The gstreamer implementation
mod abort;
mod gst;
mod motion;
use crate::utils::AddressOrUid;
pub(crate) use cmdline::Opt;
use config::{CameraConfig, Config, UserConfig};
use gst::{GstOutputs, RtspServer, TlsAuthenticationMode};
use motion::MotionStream;
/// Entry point for the rtsp subcommand
///
@@ -278,7 +281,99 @@ fn camera_main(
"{}: Starting video stream {}",
camera_config.name, stream_display_name
);
camera.start_video(outputs, stream_name).with_context(|| format!("Error while streaming {}", camera_config.name))
// Used to get the error from the thread
let thread_error: Arc<Mutex<Option<anyhow::Error>>> = Default::default();
crossbeam::scope(|s| {
let (mut motion, rx) = MotionStream::new();
let motionthread_motion_abort = motion.get_abort_handle();
let motionthread_stream_abort = outputs.get_abort_handle();
let rxthread_motion_abort = motion.get_abort_handle();
let rxthread_stream_abort = outputs.get_abort_handle();
let streamthread_stream_abort = outputs.get_abort_handle();
let streamthread_motion_abort = motion.get_abort_handle();
// Start in the aborted state because motion defaults to still
streamthread_stream_abort.abort();
let arc_camera = Arc::new(camera);
// Motion detection thread
let motion_camera = arc_camera.clone();
let motion_camname = camera_config.name.to_string();
let motion_error = thread_error.clone();
s.spawn(move |_| {
if let Err(e) = motion_camera.listen_on_motion(&mut motion).with_context(|| format!("Error while reading motion {}", motion_camname)) {
// When motion errors set the thread error
*motion_error.lock().unwrap() = Some(e);
}
// Always abort the streams/motion when motion stops regardless of why
// so that this thread can reset the camera
motionthread_motion_abort.abort();
motionthread_stream_abort.abort();
debug!("Motion stopped");
});
// Motion rx -> stream thread
s.spawn(move |_| {
// Stops when motion is stopped
// which happens when either motion or stream errors
while rxthread_motion_abort.is_live() {
let msg = rx.try_recv();
if let Ok(msg) = msg {
match msg {
MotionStatus::Start => {
debug!("Motion start");
rxthread_stream_abort.reset();
}
MotionStatus::Stop => {
debug!("Motion stop");
rxthread_stream_abort.abort();
}
_ => {}
}
}
}
println!("Motion RX Stopped");
});
// Stream thread
let stream_camera = arc_camera;
let stream_camname = camera_config.name.to_string();
let stream_error = thread_error.clone();
s.spawn(move |_| {
loop {
if streamthread_stream_abort.is_live() || ! outputs.has_last_iframe() {
debug!("Stream resumed: waiting for motion to stop");
// Will block until the rx thread send the abort
if let Err(e) = stream_camera.start_video(outputs, stream_name).with_context(|| format!("Error while streaming {}", stream_camname)) {
// When stream errors set the thread error and abort the motion
*stream_error.lock().unwrap() = Some(e);
break;
}
debug!("Stream paused: waiting for motion to start");
} else {
// While stream is aborted we send the blank image
if let Err(e) = outputs.write_last_iframe().with_context(|| format!("Failed to write no motion image for {}", stream_camname)) {
*stream_error.lock().unwrap() = Some(e);
break;
}
std::thread::sleep(std::time::Duration::from_millis(50));
}
}
streamthread_stream_abort.abort();
streamthread_motion_abort.abort();
println!("Stream stopped");
});
}).unwrap();
// Should be the only one holding the thread_error so unwrap it
// all the way to an actual error
Err(Arc::try_unwrap(thread_error).unwrap().into_inner().unwrap().unwrap())
})().map_err(|e| CameraErr{
connected,
login_fail,
+32
View File
@@ -0,0 +1,32 @@
use super::abort::AbortHandle;
use crossbeam_channel::{unbounded, Receiver, Sender};
use neolink_core::bc_protocol::{MotionOutput, MotionOutputError, MotionStatus};
pub(crate) struct MotionStream {
tx: Sender<MotionStatus>,
live: AbortHandle,
}
impl MotionStream {
pub(crate) fn new() -> (Self, Receiver<MotionStatus>) {
let (tx, rx) = unbounded();
(
Self {
tx,
live: AbortHandle::new(),
},
rx,
)
}
pub(crate) fn get_abort_handle(&self) -> AbortHandle {
self.live.clone()
}
}
impl MotionOutput for MotionStream {
fn motion_recv(&mut self, motion_status: MotionStatus) -> MotionOutputError {
let _ = self.tx.send(motion_status);
Ok(self.live.is_live())
}
}