From b7a52b0da0a1064fe659c23cda247f3e355f589c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Thu, 5 Jan 2023 16:39:56 +0200 Subject: [PATCH] gstreamer-app: Assert that GStreamer is initialized before creating instances Part-of: --- gstreamer-app/src/app_sink.rs | 1 + gstreamer-app/src/app_src.rs | 1 + gstreamer-app/src/lib.rs | 13 +++++++++++++ 3 files changed, 15 insertions(+) diff --git a/gstreamer-app/src/app_sink.rs b/gstreamer-app/src/app_sink.rs index be1dea64b..c00e42c32 100644 --- a/gstreamer-app/src/app_sink.rs +++ b/gstreamer-app/src/app_sink.rs @@ -273,6 +273,7 @@ impl AppSink { /// /// This method returns an instance of [`AppSinkBuilder`](crate::builders::AppSinkBuilder) which can be used to create [`AppSink`] objects. pub fn builder() -> AppSinkBuilder { + assert_initialized_main_thread!(); AppSinkBuilder::default() } diff --git a/gstreamer-app/src/app_src.rs b/gstreamer-app/src/app_src.rs index 7169a5eca..fb366714d 100644 --- a/gstreamer-app/src/app_src.rs +++ b/gstreamer-app/src/app_src.rs @@ -210,6 +210,7 @@ impl AppSrc { /// /// This method returns an instance of [`AppSrcBuilder`](crate::builders::AppSrcBuilder) which can be used to create [`AppSrc`] objects. pub fn builder() -> AppSrcBuilder { + assert_initialized_main_thread!(); AppSrcBuilder::default() } diff --git a/gstreamer-app/src/lib.rs b/gstreamer-app/src/lib.rs index 1d8bfc167..52bf150e4 100644 --- a/gstreamer-app/src/lib.rs +++ b/gstreamer-app/src/lib.rs @@ -10,6 +10,19 @@ pub use glib; pub use gst; pub use gst_base; +macro_rules! assert_initialized_main_thread { + () => { + if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { + #[allow(unused_unsafe)] + if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { + panic!("GStreamer has not been initialized. Call `gst::init` first."); + } else { + gst::INITIALIZED.store(true, std::sync::atomic::Ordering::SeqCst); + } + } + }; +} + macro_rules! skip_assert_initialized { () => {}; }