Added basic motion based streaming
This commit is contained in:
11 files changed
+290
-19
No files matched your search
Generated
+1
@@ -958,6 +958,7 @@ dependencies = [
|
||||
"cfb-mode",
|
||||
"cookie-factory",
|
||||
"crossbeam",
|
||||
"crossbeam-channel 0.5.1",
|
||||
"env_logger",
|
||||
"err-derive",
|
||||
"gstreamer",
|
||||
|
||||
@@ -16,6 +16,7 @@ aes = "0.6"
|
||||
cfb-mode = "0.6"
|
||||
cookie-factory = "0.3"
|
||||
crossbeam = "0.7"
|
||||
crossbeam-channel = "0.5.1"
|
||||
err-derive = "0.2"
|
||||
env_logger = "*"
|
||||
gstreamer = "0.17"
|
||||
|
||||
@@ -431,7 +431,7 @@ mod tests {
|
||||
..
|
||||
})),
|
||||
}),
|
||||
}) if version == "1.1" && stream_type == "externStream"
|
||||
}) if version == "1.1" && stream_type == Some("externStream".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
@@ -475,7 +475,7 @@ mod tests {
|
||||
..
|
||||
})),
|
||||
}),
|
||||
}) if version == "1.1" && stream_type == "subStream"
|
||||
}) if version == "1.1" && stream_type == Some("subStream".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
@@ -519,7 +519,7 @@ mod tests {
|
||||
..
|
||||
})),
|
||||
}),
|
||||
}) if version == "1.1" && stream_type == "mainStream"
|
||||
}) if version == "1.1" && stream_type == Some("mainStream".to_string())
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,8 @@ pub const MSG_ID_LOGIN: u32 = 1;
|
||||
pub const MSG_ID_LOGOUT: u32 = 2;
|
||||
/// Video and Audio Streams messages have this ID
|
||||
pub const MSG_ID_VIDEO: u32 = 3;
|
||||
/// ID used to stop the video stream
|
||||
pub const MSG_ID_VIDEO_STOP: u32 = 4;
|
||||
/// TalkAbility messages have this ID
|
||||
pub const MSG_ID_TALKABILITY: u32 = 10;
|
||||
/// TalkReset messages have this ID
|
||||
|
||||
@@ -199,7 +199,7 @@ pub struct Preview {
|
||||
pub handle: u32,
|
||||
/// Either `"mainStream"` or `"subStream"`
|
||||
#[yaserde(rename = "streamType")]
|
||||
pub stream_type: String,
|
||||
pub stream_type: Option<String>,
|
||||
}
|
||||
|
||||
/// Extension xml
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
//!
|
||||
use crate::bc;
|
||||
use crate::bc::model::*;
|
||||
use crate::bcmedia;
|
||||
use crate::bcudp;
|
||||
use err_derive::Error;
|
||||
use log::*;
|
||||
@@ -45,6 +46,9 @@ pub enum Error {
|
||||
#[error(display = "Deserialization error")]
|
||||
Deserialization(#[error(source)] bc::de::Error),
|
||||
|
||||
#[error(display = "BcMedia Deserialization error")]
|
||||
BcMediaDeserialization(#[error(source)] bcmedia::de::Error),
|
||||
|
||||
#[error(display = "Serialization error")]
|
||||
Serialization(#[error(source)] bc::ser::Error),
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::{BcCamera, BinarySubscriber, Result};
|
||||
use super::{BcCamera, BinarySubscriber, Error, Result, RX_TIMEOUT};
|
||||
use crate::{
|
||||
bc::{model::*, xml::*},
|
||||
bcmedia::model::*,
|
||||
@@ -112,7 +112,7 @@ impl BcCamera {
|
||||
version: xml_ver(),
|
||||
channel_id: self.channel_id,
|
||||
handle,
|
||||
stream_type: stream_name,
|
||||
stream_type: Some(stream_name),
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
@@ -122,14 +122,90 @@ impl BcCamera {
|
||||
|
||||
let mut media_sub = BinarySubscriber::from_bc_sub(&sub_video);
|
||||
|
||||
let result;
|
||||
|
||||
loop {
|
||||
let bc_media = BcMedia::deserialize(&mut media_sub)?;
|
||||
// We now have a complete interesting packet. Send it to on the callback
|
||||
match data_outs.stream_recv(bc_media) {
|
||||
Ok(true) => {}
|
||||
Ok(false) => return Ok(()),
|
||||
Err(e) => return Err(e),
|
||||
Ok(false) => {
|
||||
result = Ok(());
|
||||
break;
|
||||
}
|
||||
Err(e) => {
|
||||
result = Err(e);
|
||||
break;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
println!("Stopping video: {:?}", result);
|
||||
self.stop_stream(stream)?;
|
||||
println!("Video stopped");
|
||||
result
|
||||
}
|
||||
|
||||
///
|
||||
/// Sends the stop stream message to the camera
|
||||
///
|
||||
pub fn stop_stream(&self, stream: Stream) -> Result<()> {
|
||||
let connection = self
|
||||
.connection
|
||||
.as_ref()
|
||||
.expect("Must be connected to stop video");
|
||||
let sub = connection.subscribe(MSG_ID_VIDEO_STOP)?;
|
||||
|
||||
// Theses are the numbers used with the offical client
|
||||
// On an E1 and swann cameras:
|
||||
// - mainStream always has a value of 0
|
||||
// - subStream always has a value of 1
|
||||
// - There is no externStram
|
||||
// On a B800:
|
||||
// - mainStream is 0
|
||||
// - subStream is 256
|
||||
// - externStram is 1024
|
||||
let handle = match stream {
|
||||
Stream::Main => 0,
|
||||
Stream::Sub => 256,
|
||||
Stream::Extern => 1024,
|
||||
};
|
||||
|
||||
let msg = Bc::new_from_xml(
|
||||
BcMeta {
|
||||
msg_id: MSG_ID_VIDEO_STOP,
|
||||
channel_id: self.channel_id,
|
||||
msg_num: self.new_message_num(),
|
||||
stream_type: 0,
|
||||
response_code: 0,
|
||||
class: 0x6414, // IDK why
|
||||
},
|
||||
BcXml {
|
||||
preview: Some(Preview {
|
||||
version: xml_ver(),
|
||||
channel_id: self.channel_id,
|
||||
handle,
|
||||
stream_type: None,
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
sub.send(msg)?;
|
||||
|
||||
let msg = sub.rx.recv_timeout(RX_TIMEOUT)?;
|
||||
|
||||
if let BcMeta {
|
||||
response_code: 200, ..
|
||||
} = msg.meta
|
||||
{
|
||||
} else {
|
||||
return Err(Error::UnintelligibleReply {
|
||||
reply: msg,
|
||||
why: "The camera did not accept the stream stop command.",
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -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
@@ -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
@@ -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,
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user