From cc2c6ffc3ba0bd37d00c60d39c72694e4929f86e Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 14 Dec 2022 20:14:44 +0700 Subject: [PATCH] Add check for gstreamer plugins --- crates/core/src/bc_protocol/motion.rs | 3 - src/rtsp/gst.rs | 116 ++++++++++++++++++++++++-- src/rtsp/mod.rs | 12 ++- 3 files changed, 118 insertions(+), 13 deletions(-) diff --git a/crates/core/src/bc_protocol/motion.rs b/crates/core/src/bc_protocol/motion.rs index 58cd7d9..bc3bf2f 100644 --- a/crates/core/src/bc_protocol/motion.rs +++ b/crates/core/src/bc_protocol/motion.rs @@ -68,9 +68,6 @@ impl MotionData { Err(e) => return Err(Error::from(e)), } } - for res in results.iter() { - log::info!("Mption: {:?}", res); - } if let Some(last) = results.last() { self.last_update = *last; } diff --git a/src/rtsp/gst.rs b/src/rtsp/gst.rs index 37c8770..014369d 100644 --- a/src/rtsp/gst.rs +++ b/src/rtsp/gst.rs @@ -9,7 +9,7 @@ pub(crate) use self::maybe_inputselect::MaybeInputSelect; // use super::adpcm::adpcm_to_pcm; // use super::errors::Error; use gstreamer::prelude::Cast; -use gstreamer::{Bin, Structure}; +use gstreamer::{Bin, ElementFactory, Structure}; use gstreamer_app::AppSrc; //use gstreamer_rtsp::RTSPLowerTrans; use anyhow::{anyhow, Context}; @@ -344,17 +344,121 @@ impl GstOutputs { impl Default for RtspServer { fn default() -> RtspServer { - Self::new() + Self::new().unwrap() } } impl RtspServer { - pub(crate) fn new() -> RtspServer { - gstreamer::init().expect("Gstreamer should not explode"); - RtspServer { + pub(crate) fn new() -> AnyResult { + gstreamer::init().context("Gstreamer failed to initialise")?; + let needed_elements = &[ + ( + "appsrc", + "app (gst-plugins-base)", + "Cannot run without an appsrc", + ), + ( + "audioconvert", + "audioconvert (gst-plugins-base)", + "Required for audio", + ), + ("rtpL16pay", "rtp (gst-plugins-good)", "Required for audio"), + ]; + let sometimes_needed_elements = &[ + ("adpcmdec", "adpcmdec", "Required for audio"), + ( + "h264parse", + "videoparsersbad (gst-plugins-bad)", + "Required for certain types of camera", + ), + ( + "h265parse", + "videoparsersbad (gst-plugins-bad)", + "Required for certain types of camera", + ), + ( + "x264enc", + "x264 (gst-plugins-ugly)", + "Required to paused certain cameras", + ), + ( + "x265enc", + "x265 (gst-plugins-bad)", + "Required to paused certain cameras", + ), + ( + "avdec_h264", + "libav (gst-libav)", + "Required to paused certain cameras", + ), + ( + "avdec_h265", + "libav (gst-libav)", + "Required to paused certain cameras", + ), + ( + "videotestsrc", + "videotestsrc (gst-plugins-base)", + "Required to paused certain cameras", + ), + ( + "imagefreeze", + "imagefreeze (gst-plugins-good)", + "Required to paused certain cameras", + ), + ( + "rtph264pay", + "rtp (gst-plugins-good)", + "Required for certain types of camera", + ), + ( + "rtph265pay", + "rtp (gst-plugins-good)", + "Required for certain types of camera", + ), + ( + "aacparse", + "audioparsers (gst-plugins-good)", + "Required for certain types of camera's audio", + ), + ( + "audiotestsrc", + "audiotestsrc (gst-plugins-base)", + "Required for pausing", + ), + ( + "decodebin", + "playback (gst-plugins-good)", + "Required for pausing", + ), + ]; + let mut fatal = false; + for (element, plugin, msg) in needed_elements.iter() { + if ElementFactory::find(element).is_none() { + error!( + "Missing required gstreamer plugin `{}` for `{}` element. {}", + plugin, element, msg + ); + fatal = true; + } + } + + for (element, plugin, msg) in sometimes_needed_elements.iter() { + if ElementFactory::find(element).is_none() { + warn!( + "Missing the gstreamer plugin `{}` for `{}` element. {}", + plugin, element, msg + ); + } + } + + if fatal { + return Err(anyhow!("Required Gstreamer Elements are missing")); + } + Ok(RtspServer { server: GstRTSPServer::new(), main_loop: glib::MainLoop::new(None, false), - } + }) } pub(crate) fn add_stream>( diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index 577f10e..a38cc4e 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -50,7 +50,7 @@ use gst::{RtspServer, TlsAuthenticationMode}; /// /// Opt is the command line options pub(crate) fn main(_opt: Opt, mut config: Config) -> Result<()> { - let rtsp = Arc::new(RtspServer::new()); + let rtsp = Arc::new(RtspServer::new()?); set_up_tls(&config, &rtsp); @@ -144,6 +144,7 @@ fn camera_main( }; let motion_timeout: Duration = Duration::from_secs_f64(config.pause.motion_timeout); + let mut ready_to_pause_print = false; loop { match camera.get_state() { StateInfo::Streaming => { @@ -153,7 +154,8 @@ fn camera_main( if can_pause { info!("Pause on disconnect"); camera.pause().map_err(CameraFailureKind::Retry)?; - } else { + } else if !ready_to_pause_print { + ready_to_pause_print = true; warn!("Not ready to pause"); } } @@ -169,7 +171,8 @@ fn camera_main( if can_pause { info!("Pause on motion"); camera.pause().map_err(CameraFailureKind::Retry)?; - } else { + } else if !ready_to_pause_print { + ready_to_pause_print = true; warn!("Not ready to pause"); } } @@ -177,7 +180,8 @@ fn camera_main( if can_pause { info!("Pause on motion (start)"); camera.pause().map_err(CameraFailureKind::Retry)?; - } else { + } else if !ready_to_pause_print { + ready_to_pause_print = true; warn!("Not ready to pause"); } }