Add a still mode to paused streams
This commit is contained in:
4 files changed
+137
-86
No files matched your search
+38
-8
@@ -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)$").unwrap();
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Validate, Clone)]
|
||||
@@ -75,10 +76,8 @@ pub(crate) struct CameraConfig {
|
||||
pub(crate) channel_id: u8,
|
||||
|
||||
#[validate]
|
||||
pub(crate) motion: Option<MotionConfig>,
|
||||
|
||||
#[serde(default = "default_on_client")]
|
||||
pub(crate) on_client: bool,
|
||||
#[serde(default = "default_pause")]
|
||||
pub(crate) pause: PauseConfig,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Validate, Clone)]
|
||||
@@ -92,9 +91,23 @@ pub(crate) struct UserConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Validate, Clone)]
|
||||
pub(crate) struct MotionConfig {
|
||||
#[serde(default = "default_motion_timeout", alias = "timeout_seconds")]
|
||||
pub(crate) timeout: f64,
|
||||
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 {
|
||||
@@ -125,10 +138,27 @@ fn default_motion_timeout() -> f64 {
|
||||
1.
|
||||
}
|
||||
|
||||
fn default_on_client() -> bool {
|
||||
fn default_on_disconnect() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn default_on_motion() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn default_pause_mode() -> String {
|
||||
"still".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() {
|
||||
|
||||
+27
-3
@@ -44,6 +44,8 @@ pub(crate) struct RtspServer {
|
||||
pub(crate) enum InputSources {
|
||||
Cam,
|
||||
TestSrc,
|
||||
Still,
|
||||
Black,
|
||||
}
|
||||
|
||||
pub(crate) struct GstOutputs {
|
||||
@@ -159,6 +161,8 @@ impl GstOutputs {
|
||||
match &input_source {
|
||||
InputSources::Cam => self.vid_inputselect.set_input(0),
|
||||
InputSources::TestSrc => self.vid_inputselect.set_input(1),
|
||||
InputSources::Still => self.vid_inputselect.set_input(2),
|
||||
InputSources::Black => self.vid_inputselect.set_input(3),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,10 +217,20 @@ impl GstOutputs {
|
||||
|
||||
let launch_alt_source = match self.video_format {
|
||||
Some(StreamFormat::H264) => {
|
||||
"videotestsrc ! x264enc ! video/x-h264,width=1920,height=1080,framerate=25/1 ! h264parse"
|
||||
"videotestsrc ! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! x264enc ! video/x-h264,width=896,height=512,framerate=25/1 ! h264parse"
|
||||
}
|
||||
Some(StreamFormat::H265) => {
|
||||
"videotestsrc ! x265enc ! video/x-h265,width=1920,height=1080,framerate=25/1 ! h265parse"
|
||||
"videotestsrc ! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! x265enc ! video/x-h265,width=896,height=512,framerate=25/1 ! h265parse"
|
||||
}
|
||||
_ => "",
|
||||
};
|
||||
|
||||
let launch_imgfreeze_source = match self.video_format {
|
||||
Some(StreamFormat::H264) => {
|
||||
"avdec_h264 ! imagefreeze allow-replace=true is-live=true ! x264enc ! video/x-h264,width=896,height=512,framerate=25/1 ! h264parse"
|
||||
}
|
||||
Some(StreamFormat::H265) => {
|
||||
"decodebin ! imagefreeze allow-replace=true is-live=true ! x265enc ! video/x-h265,width=896,height=512,framerate=25/1 ! h265parse"
|
||||
}
|
||||
_ => "",
|
||||
};
|
||||
@@ -238,13 +252,23 @@ impl GstOutputs {
|
||||
"(",
|
||||
"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,
|
||||
"! vid_inputselect.sink_0",
|
||||
"! tee name=vid_src_tee",
|
||||
"(",
|
||||
"vid_src_tee. ! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! vid_inputselect.sink_0",
|
||||
")",
|
||||
|
||||
")",
|
||||
// Test vid source
|
||||
"(",
|
||||
launch_alt_source,
|
||||
"! vid_inputselect.sink_1",
|
||||
")",
|
||||
// Image freeze
|
||||
"(",
|
||||
"vid_src_tee. ! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 !",
|
||||
launch_imgfreeze_source,
|
||||
" ! vid_inputselect.sink_2",
|
||||
")",
|
||||
// Audio pipe
|
||||
"appsrc name=audsrc is-live=true block=true emit-signals=false max-bytes=52428800 do-timestamp=true format=GST_FORMAT_TIME",
|
||||
&launch_aud,
|
||||
|
||||
+69
-72
@@ -281,86 +281,83 @@ fn camera_main(
|
||||
camera_config.name, stream_display_name
|
||||
);
|
||||
|
||||
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();
|
||||
if ! camera_config.pause.on_motion && ! camera_config.pause.on_disconnect {
|
||||
// Stream is always on
|
||||
camera.start_video(outputs, stream_name).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<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());
|
||||
// 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 = MotionStream::new(motion_config.timeout, state.clone());
|
||||
crossbeam::scope(|s| {
|
||||
let mut motion = MotionStream::new(pause_config.motion_timeout, state.clone());
|
||||
|
||||
let arc_camera = Arc::new(camera);
|
||||
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");
|
||||
});
|
||||
// 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() || ! outputs.has_last_iframe() {
|
||||
if let Err(e) = outputs.set_input_source(InputSources::Cam) {
|
||||
*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(outputs, stream_name).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) = outputs.set_input_source(InputSources::TestSrc) {
|
||||
*stream_error.lock().unwrap() = Some(e);
|
||||
break;
|
||||
}
|
||||
// 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));
|
||||
// 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() || ! outputs.has_last_iframe() {
|
||||
if let Err(e) = outputs.set_input_source(InputSources::Cam) {
|
||||
*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(outputs, stream_name).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 {
|
||||
let input_source = match pause_config.mode.as_str() {
|
||||
"test" => {InputSources::TestSrc},
|
||||
"still" => {InputSources::Still},
|
||||
"black" => {InputSources::Black},
|
||||
_ => {unreachable!()}
|
||||
};
|
||||
if let Err(e) = outputs.set_input_source(input_source) {
|
||||
*stream_error.lock().unwrap() = Some(e);
|
||||
break;
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
}
|
||||
stream_state.abort();
|
||||
debug!("Stream stopped");
|
||||
});
|
||||
}).unwrap();
|
||||
}
|
||||
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())
|
||||
}
|
||||
None => {
|
||||
// No motion config
|
||||
//
|
||||
// Just stream always
|
||||
camera.start_video(outputs, stream_name).with_context(|| format!("Error while streaming {}", camera_config.name))
|
||||
}
|
||||
// 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{
|
||||
|
||||
+3
-3
@@ -50,8 +50,8 @@ impl States {
|
||||
}
|
||||
|
||||
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;
|
||||
let motion_controlled = camera_config.pause.on_motion;
|
||||
let client_controlled = camera_config.pause.on_disconnect;
|
||||
|
||||
Self::new(motion_controlled, client_controlled)
|
||||
}
|
||||
@@ -85,7 +85,7 @@ impl States {
|
||||
*motion = match value {
|
||||
true => MotionState::Motion,
|
||||
false => MotionState::Still,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) fn abort(&self) {
|
||||
|
||||
Reference in new issue
Block a user