Merge branch 'motion_stream'

This commit is contained in:
QuantumEntangledAndy committed 2022-11-28 10:17:08 +07:00
commit 2114af085d
17 files changed
+708 -41

No files matched your search

+1
View File
@@ -16,6 +16,7 @@ aes = "0.6"
cfb-mode = "0.6"
cookie-factory = "0.3"
crossbeam = "0.8"
crossbeam-channel = "0.5.1"
err-derive = "0.2"
env_logger = "*"
gstreamer = "0.17"
+3 -3
View File
@@ -471,7 +471,7 @@ mod tests {
..
})),
}),
}) if version == "1.1" && stream_type == "externStream"
}) if version == "1.1" && stream_type == Some("externStream".to_string())
);
}
@@ -515,7 +515,7 @@ mod tests {
..
})),
}),
}) if version == "1.1" && stream_type == "subStream"
}) if version == "1.1" && stream_type == Some("subStream".to_string())
);
}
@@ -559,7 +559,7 @@ mod tests {
..
})),
}),
}) if version == "1.1" && stream_type == "mainStream"
}) if version == "1.1" && stream_type == Some("mainStream".to_string())
);
}
}
+1 -1
View File
@@ -10,7 +10,7 @@ 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;
/// Video and Audio Streams messages have this ID when a stop is requested
/// 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;
+1 -1
View File
@@ -203,7 +203,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 (conn)")]
BcMediaDeserialization(#[error(source)] bcmedia::de::Error),
#[error(display = "Serialization error")]
Serialization(#[error(source)] bc::ser::Error),
-4
View File
@@ -5,10 +5,6 @@ use err_derive::Error;
#[derive(Debug, Error)]
#[allow(clippy::large_enum_variant)]
pub enum Error {
/// Error raised when IO fails such as when the connection is lost
#[error(display = "Communication error")]
Communication(#[error(source)] std::io::Error),
/// Error raised during deserlization
#[error(display = "Deserialization error")]
Deserialization(#[error(source)] super::bc::de::Error),
+22 -4
View File
@@ -1,4 +1,4 @@
use super::{BcCamera, BinarySubscriber, Result};
use super::{BcCamera, BinarySubscriber, Error, Result, RX_TIMEOUT};
use crate::{
bc::{model::*, xml::*},
bcmedia::model::*,
@@ -122,7 +122,7 @@ impl BcCamera {
version: xml_ver(),
channel_id: self.channel_id,
handle,
stream_type: stream_name,
stream_type: Some(stream_name),
}),
..Default::default()
},
@@ -130,6 +130,18 @@ impl BcCamera {
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: msg,
why: "The camera did not accept the stream start command.",
});
}
let mut media_sub = BinarySubscriber::from_bc_sub(&sub_video);
while !abort_handle.load(Ordering::Relaxed) {
@@ -137,8 +149,14 @@ impl BcCamera {
// 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;
}
};
}
+51
View File
@@ -10,6 +10,7 @@ lazy_static! {
static ref RE_STREAM_SRC: Regex =
Regex::new(r"^(mainStream|subStream|externStream|both|all)$").unwrap();
static ref RE_TLS_CLIENT_AUTH: Regex = Regex::new(r"^(none|request|require)$").unwrap();
static ref RE_PAUSE_MODE: Regex = Regex::new(r"^(black|still|test|none)$").unwrap();
}
#[derive(Debug, Deserialize, Validate, Clone)]
@@ -77,6 +78,10 @@ pub(crate) struct CameraConfig {
#[validate]
#[serde(default = "default_mqtt")]
pub(crate) mqtt: Option<MqttConfig>,
#[validate]
#[serde(default = "default_pause")]
pub(crate) pause: PauseConfig,
}
#[derive(Debug, Deserialize, Validate, Clone)]
@@ -89,6 +94,7 @@ pub(crate) struct UserConfig {
pub(crate) pass: String,
}
#[derive(Debug, Deserialize, Clone, Validate)]
#[validate(schema(function = "validate_mqtt_config", skip_on_field_errors = true))]
pub(crate) struct MqttConfig {
@@ -119,6 +125,26 @@ fn validate_mqtt_config(config: &MqttConfig) -> Result<(), ValidationError> {
fn default_mqtt() -> Option<MqttConfig> {
None
#[derive(Debug, Deserialize, Validate, Clone)]
pub(crate) struct PauseConfig {
#[serde(default = "default_on_motion")]
pub(crate) on_motion: bool,
#[serde(default = "default_on_disconnect")]
pub(crate) on_disconnect: bool,
#[serde(default = "default_motion_timeout", alias = "timeout")]
pub(crate) motion_timeout: f64,
#[serde(default = "default_pause_mode")]
#[validate(regex(
path = "RE_PAUSE_MODE",
message = "Incorrect pause mode",
code = "mode"
))]
pub(crate) mode: String,
}
fn default_bind_addr() -> String {
@@ -145,6 +171,31 @@ fn default_channel_id() -> u8 {
0
}
fn default_motion_timeout() -> f64 {
1.
}
fn default_on_disconnect() -> bool {
false
}
fn default_on_motion() -> bool {
false
}
fn default_pause_mode() -> String {
"none".to_string()
}
fn default_pause() -> PauseConfig {
PauseConfig {
on_motion: default_on_motion(),
on_disconnect: default_on_disconnect(),
motion_timeout: default_motion_timeout(),
mode: default_pause_mode(),
}
}
pub(crate) static RESERVED_NAMES: &[&str] = &["anyone", "anonymous"];
fn validate_username(name: &str) -> Result<(), ValidationError> {
if name.trim().is_empty() {
+25
View File
@@ -0,0 +1,25 @@
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 is_live(&self) -> bool {
self.live.load(Ordering::Relaxed)
}
}
+249 -24
View File
@@ -1,12 +1,19 @@
//! 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;
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 gstreamer_rtsp::RTSPAuthMethod;
pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate};
use gstreamer_rtsp_server::glib;
@@ -18,7 +25,7 @@ use gstreamer_rtsp_server::{
};
use log::*;
use neolink_core::{
bc_protocol::{StreamOutput, StreamOutputError},
bc_protocol::{Error, StreamOutput, StreamOutputError},
bcmedia::model::*,
};
use std::collections::HashSet;
@@ -27,17 +34,37 @@ 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,
}
#[derive(Eq, PartialEq, Debug)]
pub(crate) enum InputMode {
Live,
Paused,
}
#[derive(Eq, PartialEq, Debug)]
pub(crate) enum PausedSources {
TestSrc,
Still,
Black,
None,
}
pub(crate) struct GstOutputs {
pub(crate) audsrc: MaybeAppSrc,
pub(crate) vidsrc: MaybeAppSrc,
vid_inputselect: MaybeInputSelect,
aud_inputselect: MaybeInputSelect,
video_format: Option<StreamFormat>,
audio_format: Option<StreamFormat>,
factory: RTSPMediaFactory,
state: Option<States>,
when_paused: PausedSources,
last_iframe: Option<Vec<u8>>,
}
// The stream from the camera will be using one of these formats
@@ -58,6 +85,13 @@ enum StreamFormat {
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 {
@@ -65,7 +99,15 @@ impl StreamOutput for GstOutputs {
VideoType::H265 => StreamFormat::H265,
};
self.set_format(Some(video_type));
self.vidsrc.write_all(&payload.data)?;
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 {
@@ -73,38 +115,108 @@ impl StreamOutput for GstOutputs {
VideoType::H265 => StreamFormat::H265,
};
self.set_format(Some(video_type));
self.vidsrc.write_all(&payload.data)?;
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)?;
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)?;
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(true)
Ok(should_continue)
}
}
impl GstOutputs {
pub(crate) fn from_appsrcs(vidsrc: MaybeAppSrc, audsrc: MaybeAppSrc) -> GstOutputs {
pub(crate) fn from_appsrcs(
vidsrc: MaybeAppSrc,
audsrc: MaybeAppSrc,
vid_inputselect: MaybeInputSelect,
aud_inputselect: MaybeInputSelect,
) -> GstOutputs {
let result = GstOutputs {
vidsrc,
audsrc,
vid_inputselect,
aud_inputselect,
video_format: None,
audio_format: None,
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 set_input_source(&mut self, input_source: InputMode) -> AnyResult<()> {
// PausedSources::None is exceptional in that it dosen't swap the stream
// to an alterntive source. It just repeats the last buffer meaning not re-encoding
// at the cost some clients not handeling the repeating buffers well
if self.when_paused != PausedSources::None {
match &input_source {
InputMode::Live => {
self.vid_inputselect.set_input(0)?;
self.aud_inputselect.set_input(0)?;
}
InputMode::Paused => {
self.vid_inputselect.set_input(1)?;
self.aud_inputselect.set_input(1)?;
}
}
} else {
// For PausedSources::None we still want to use the silence as we don't want to
// repeat the audio buffers
match &input_source {
InputMode::Live => {
self.aud_inputselect.set_input(0)?;
}
InputMode::Paused => {
self.aud_inputselect.set_input(1)?;
}
}
}
Ok(())
}
pub(crate) fn set_paused_source(&mut self, paused_source: PausedSources) {
self.when_paused = paused_source;
self.apply_format();
}
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) => {
@@ -124,33 +236,128 @@ impl GstOutputs {
}
fn apply_format(&self) {
let launch_vid = match self.video_format {
Some(StreamFormat::H264) => {
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! h264parse ! rtph264pay name=pay0"
// This is the final sink prior to rtsp
// - In the case of PausedSources::None we just pass through without encoding
// - In the case of all other pause sources we need to recencode in order
// to get smooth streams when the stream is paused/resumed
let launch_vid_select = match self.when_paused {
PausedSources::None => match self.video_format {
Some(StreamFormat::H264) => "! rtph264pay name=pay0",
Some(StreamFormat::H265) => "! rtph265pay name=pay0",
_ => "! fakesink",
},
_ => match self.video_format {
Some(StreamFormat::H264) => "! x264enc ! rtph264pay name=pay0",
Some(StreamFormat::H265) => "! x265enc ! rtph265pay name=pay0",
_ => "! fakesink",
},
};
// This is the part that deals with input from camera
// - In the case of PausedSources::None it just passes the stream
// - In the case of other Paused sources we decode it so that we can manipulate
// the stream smoothly and swap the source on demand
let launch_app_src = match self.when_paused {
PausedSources::None => {
match self.video_format {
Some(StreamFormat::H264) => {
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! h264parse"
}
Some(StreamFormat::H265) => {
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! h265parse"
}
_ => "",
}
}
Some(StreamFormat::H265) => {
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! h265parse ! rtph265pay name=pay0"
_ => {
match self.video_format {
Some(StreamFormat::H264) => {
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! h264parse ! avdec_h264"
}
Some(StreamFormat::H265) => {
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! h265parse ! avdec_h265"
}
_ => "",
}
}
};
// This controls the alternaive source used when in paused state
// PausedSources::None is the exception in that it is never swapped to this state
let launch_alt_source = match self.when_paused {
PausedSources::TestSrc => "videotestsrc ! video/x-raw,width=896,height=512,framerate=25/1",
PausedSources::Black => "videotestsrc pattern=black ! video/x-raw,width=896,height=512,framerate=25/1",
PausedSources::Still => "vid_src_tee. ! imagefreeze allow-replace=true is-live=true ! video/x-raw,framerate=25/1",
PausedSources::None => "",
};
// This is the final pipeline for the audio prior to rtsp
let launch_aud_select = match self.audio_format {
Some(StreamFormat::Adpcm(_)) | Some(StreamFormat::Aac) => {
"! audioconvert ! rtpL16pay name=pay1"
}
_ => "! fakesink",
};
// This is the audio source from the camera. We decode to xraw always as some clients
// like blue iris only support raw audio
let launch_aud = match self.audio_format {
Some(StreamFormat::Adpcm(block_size)) => format!("caps=audio/x-adpcm,layout=dvi,block_align={},channels=1,rate=8000 ! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! adpcmdec ! audioconvert ! rtpL16pay name=pay1", block_size), // DVI4 is converted to pcm in the appsrc
Some(StreamFormat::Aac) => "! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! aacparse ! decodebin ! audioconvert ! rtpL16pay name=pay1 name=pay1".to_string(),
_ => "! fakesink".to_string(),
Some(StreamFormat::Adpcm(block_size)) => format!("caps=audio/x-adpcm,layout=dvi,block_align={},channels=1,rate=8000 ! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! adpcmdec", block_size), // DVI4 is converted to pcm in the appsrc
Some(StreamFormat::Aac) => "! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! aacparse ! decodebin".to_string(),
_ => "".to_string(),
};
self.factory.set_launch(
&vec![
// The alternaive audio source is silence
let launch_aud_alt = "audiotestsrc wave=silence";
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,
"appsrc name=audsrc 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_aud,
// Video out pipe
"(",
"input-selector name=vid_inputselect",
launch_vid_select,
")",
// Camera vid source
"(",
"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_app_src,
// Pipe it though a tee so that the image freeze can grab it
"! tee name=vid_src_tee",
"(",
"vid_src_tee. ! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! vid_inputselect.sink_0",
")",
")",
// Alternaive vid source
//
// Used during paused mode
"(",
launch_alt_source,
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! vid_inputselect.sink_1",
")",
// Image freeze
// Audio pipe
// Audio out pipe
"(",
"input-selector name=aud_inputselect",
launch_aud_select,
")",
// Camera aud source
"(",
"appsrc name=audsrc is-live=true block=true emit-signals=false max-bytes=52428800 do-timestamp=true format=GST_FORMAT_TIME",
&launch_aud,
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! aud_inputselect.sink_0",
")",
// Camera aud alt source
"(",
launch_aud_alt,
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! aud_inputselect.sink_1",
")",
")"
]
.join(" "),
);
.join(" ");
debug!("Gstreamer launch str: {:?}", launch_str);
self.factory.set_launch(launch_str);
}
}
@@ -185,7 +392,15 @@ impl RtspServer {
let (maybe_app_src, tx) = MaybeAppSrc::new_with_tx();
let (maybe_app_src_aud, tx_aud) = MaybeAppSrc::new_with_tx();
let outputs = GstOutputs::from_appsrcs(maybe_app_src, maybe_app_src_aud);
let (maybe_vid_inputselect, tx_vid_inputselect) = MaybeInputSelect::new_with_tx();
let (maybe_aud_inputselect, tx_aud_inputselect) = MaybeInputSelect::new_with_tx();
let outputs = GstOutputs::from_appsrcs(
maybe_app_src,
maybe_app_src_aud,
maybe_vid_inputselect,
maybe_aud_inputselect,
);
let factory = &outputs.factory;
@@ -220,6 +435,16 @@ impl RtspServer {
.dynamic_cast::<AppSrc>()
.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
let maybe_vid_inputselect = bin
.by_name_recurse_up("vid_inputselect")
.expect("vid_inputselect must be present in created bin");
let _ = tx_vid_inputselect.send(maybe_vid_inputselect); // Receiver may be dropped, don't panic if so
let maybe_aud_inputselect = bin
.by_name_recurse_up("aud_inputselect")
.expect("aud_inputselect must be present in created bin");
let _ = tx_aud_inputselect.send(maybe_aud_inputselect); // Receiver may be dropped, don't panic if so
});
for path in paths {
+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(())
}
}
+65
View File
@@ -0,0 +1,65 @@
use anyhow::{anyhow, Error as Anyhow};
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<Element>,
typefind: Option<Element>,
}
impl MaybeInputSelect {
pub(crate) fn new_with_tx() -> (Self, SyncSender<Element>) {
let (tx, rx) = sync_channel(3); // The sender should not send very often
(MaybeInputSelect { 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 set_input(&mut self, path_num: u32) -> Result<(), Anyhow> {
if let Some(element) = self.try_get_src() {
let new_pad = element
.static_pad(&format!("sink_{}", path_num))
.ok_or_else(|| anyhow!("Unable to set input pad"))?;
match element.property("active-pad").map(|e| e.get::<'_, Pad>()) {
Ok(Ok(active_pad)) => {
if active_pad != new_pad {
debug!("Pad need changing");
if let Err(e) = element.set_property("active-pad", new_pad) {
debug!("Element is invalid: {:?}", e);
self.clear();
} else {
debug!("Pad chanaged to {}", path_num);
}
}
}
Ok(Err(e)) => {
debug!("Pad need changing: Active pad invalid: {:?}", e);
if let Err(e) = element.set_property("active-pad", new_pad) {
debug!("Element is invalid: {:?}", e);
self.clear();
} else {
debug!("Pad chanaged to {}", path_num);
}
}
Err(e) => {
debug!("Element is invalid: Err({:?})", e);
self.clear();
}
}
}
Ok(())
}
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 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;
}
}
+6 -4
View File
@@ -38,16 +38,18 @@ use std::sync::{
};
use std::time::Duration;
// mod adpcm;
/// The command line parameters for this subcommand
mod abort;
mod cmdline;
/// The errors this subcommand can raise
mod gst;
mod motion;
mod state;
use super::config::{CameraConfig, Config, UserConfig};
use crate::utils::AddressOrUid;
pub(crate) use cmdline::Opt;
use gst::{GstOutputs, RtspServer, TlsAuthenticationMode};
use gst::{GstOutputs, InputMode, PausedSources, RtspServer, TlsAuthenticationMode};
use motion::MotionStream;
use state::States;
/// Entry point for the rtsp subcommand
///
+53
View File
@@ -0,0 +1,53 @@
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<AbortHandle>,
}
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())
}
}
+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.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)
}
}