From c9d07219c877f48a55e1ca2d8d02904254db4d2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Mon, 27 Jun 2022 10:28:28 +0300 Subject: [PATCH] Store whether GStreamer was initialized in an `AtomicBool` Avoids a function call in the normal case. --- gstreamer-allocators/src/lib.rs | 10 +++++++--- gstreamer-audio/src/lib.rs | 9 +++++++-- gstreamer-base/src/lib.rs | 9 +++++++-- gstreamer-check/src/lib.rs | 9 +++++++-- gstreamer-controller/src/lib.rs | 9 +++++++-- gstreamer-editing-services/src/lib.rs | 9 +++++++-- gstreamer-gl/src/lib.rs | 9 +++++++-- gstreamer-net/src/lib.rs | 9 +++++++-- gstreamer-pbutils/src/lib.rs | 9 +++++++-- gstreamer-play/src/lib.rs | 11 ++++++++++- gstreamer-player/src/lib.rs | 11 ++++++++++- gstreamer-rtp/src/lib.rs | 9 +++++++-- gstreamer-rtsp-server/src/lib.rs | 9 +++++++-- gstreamer-rtsp/src/lib.rs | 9 +++++++-- gstreamer-sdp/src/lib.rs | 9 +++++++-- gstreamer-video/src/lib.rs | 10 +++++++--- gstreamer-webrtc/src/lib.rs | 9 +++++++-- gstreamer/src/lib.rs | 14 ++++++++++++-- 18 files changed, 137 insertions(+), 36 deletions(-) diff --git a/gstreamer-allocators/src/lib.rs b/gstreamer-allocators/src/lib.rs index 8f42f72f1..c729e498f 100644 --- a/gstreamer-allocators/src/lib.rs +++ b/gstreamer-allocators/src/lib.rs @@ -11,9 +11,13 @@ pub use gst; macro_rules! assert_initialized_main_thread { () => { - #[allow(unused_unsafe)] - if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { - panic!("GStreamer has not been initialized. Call `gst::init` first."); + 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); + } } }; } diff --git a/gstreamer-audio/src/lib.rs b/gstreamer-audio/src/lib.rs index 357d5b016..13389f11a 100644 --- a/gstreamer-audio/src/lib.rs +++ b/gstreamer-audio/src/lib.rs @@ -12,8 +12,13 @@ pub use gst_base; macro_rules! assert_initialized_main_thread { () => { - if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { - panic!("GStreamer has not been initialized. Call `gst::init` first."); + 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); + } } }; } diff --git a/gstreamer-base/src/lib.rs b/gstreamer-base/src/lib.rs index d3e071108..bfe8b964f 100644 --- a/gstreamer-base/src/lib.rs +++ b/gstreamer-base/src/lib.rs @@ -11,8 +11,13 @@ pub use gst; macro_rules! assert_initialized_main_thread { () => { - if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { - panic!("GStreamer has not been initialized. Call `gst::init` first."); + 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); + } } }; } diff --git a/gstreamer-check/src/lib.rs b/gstreamer-check/src/lib.rs index 43c13e059..a4d3ac4aa 100644 --- a/gstreamer-check/src/lib.rs +++ b/gstreamer-check/src/lib.rs @@ -11,8 +11,13 @@ pub use gst; macro_rules! assert_initialized_main_thread { () => { - if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { - panic!("GStreamer has not been initialized. Call `gst::init` first."); + 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); + } } }; } diff --git a/gstreamer-controller/src/lib.rs b/gstreamer-controller/src/lib.rs index 922b0a49a..56a2a6b03 100644 --- a/gstreamer-controller/src/lib.rs +++ b/gstreamer-controller/src/lib.rs @@ -11,8 +11,13 @@ pub use gst; macro_rules! assert_initialized_main_thread { () => { - if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { - panic!("GStreamer has not been initialized. Call `gst::init` first."); + 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); + } } }; } diff --git a/gstreamer-editing-services/src/lib.rs b/gstreamer-editing-services/src/lib.rs index 7cc21b047..08c1e53e7 100644 --- a/gstreamer-editing-services/src/lib.rs +++ b/gstreamer-editing-services/src/lib.rs @@ -39,8 +39,13 @@ pub unsafe fn deinit() { macro_rules! assert_initialized_main_thread { () => { - if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { - panic!("GStreamer has not been initialized. Call `gst::init` first."); + 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); + } } crate::GES_INIT.call_once(|| { unsafe { ffi::ges_init() }; diff --git a/gstreamer-gl/src/lib.rs b/gstreamer-gl/src/lib.rs index 550ea40df..cf6b5f679 100644 --- a/gstreamer-gl/src/lib.rs +++ b/gstreamer-gl/src/lib.rs @@ -13,8 +13,13 @@ pub use gst_video; macro_rules! assert_initialized_main_thread { () => { - if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { - panic!("GStreamer has not been initialized. Call `gst::init` first."); + 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); + } } }; } diff --git a/gstreamer-net/src/lib.rs b/gstreamer-net/src/lib.rs index bdc643a0d..9bac10558 100644 --- a/gstreamer-net/src/lib.rs +++ b/gstreamer-net/src/lib.rs @@ -11,8 +11,13 @@ pub use gst; macro_rules! assert_initialized_main_thread { () => { - if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { - panic!("GStreamer has not been initialized. Call `gst::init` first."); + 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); + } } }; } diff --git a/gstreamer-pbutils/src/lib.rs b/gstreamer-pbutils/src/lib.rs index 245a0163c..9598dd118 100644 --- a/gstreamer-pbutils/src/lib.rs +++ b/gstreamer-pbutils/src/lib.rs @@ -15,8 +15,13 @@ static PBUTILS_INIT: Once = Once::new(); macro_rules! assert_initialized_main_thread { () => { - if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { - panic!("GStreamer has not been initialized. Call `gst::init` first."); + 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); + } } crate::PBUTILS_INIT.call_once(|| { unsafe { ffi::gst_pb_utils_init() }; diff --git a/gstreamer-play/src/lib.rs b/gstreamer-play/src/lib.rs index 431950b61..4e79dda46 100644 --- a/gstreamer-play/src/lib.rs +++ b/gstreamer-play/src/lib.rs @@ -14,7 +14,16 @@ macro_rules! skip_assert_initialized { } 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); + } + } + }; } #[allow(clippy::needless_borrow)] diff --git a/gstreamer-player/src/lib.rs b/gstreamer-player/src/lib.rs index c3bb0f34f..e211737df 100644 --- a/gstreamer-player/src/lib.rs +++ b/gstreamer-player/src/lib.rs @@ -14,7 +14,16 @@ macro_rules! skip_assert_initialized { } 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); + } + } + }; } #[allow(clippy::unreadable_literal)] diff --git a/gstreamer-rtp/src/lib.rs b/gstreamer-rtp/src/lib.rs index a4eb7abcb..8ab15a976 100644 --- a/gstreamer-rtp/src/lib.rs +++ b/gstreamer-rtp/src/lib.rs @@ -12,8 +12,13 @@ pub use gst; macro_rules! assert_initialized_main_thread { () => { - if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { - panic!("GStreamer has not been initialized. Call `gst::init` first."); + 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); + } } }; } diff --git a/gstreamer-rtsp-server/src/lib.rs b/gstreamer-rtsp-server/src/lib.rs index 7f1907ba7..90c2d8f6a 100644 --- a/gstreamer-rtsp-server/src/lib.rs +++ b/gstreamer-rtsp-server/src/lib.rs @@ -15,8 +15,13 @@ pub use gst_sdp; macro_rules! assert_initialized_main_thread { () => { - if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { - panic!("GStreamer has not been initialized. Call `gst::init` first."); + 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); + } } }; } diff --git a/gstreamer-rtsp/src/lib.rs b/gstreamer-rtsp/src/lib.rs index d096519ac..3ec70625d 100644 --- a/gstreamer-rtsp/src/lib.rs +++ b/gstreamer-rtsp/src/lib.rs @@ -12,8 +12,13 @@ pub use gst_sdp; macro_rules! assert_initialized_main_thread { () => { - if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { - panic!("GStreamer has not been initialized. Call `gst::init` first."); + 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); + } } }; } diff --git a/gstreamer-sdp/src/lib.rs b/gstreamer-sdp/src/lib.rs index 1ec106c1b..3d10203fd 100644 --- a/gstreamer-sdp/src/lib.rs +++ b/gstreamer-sdp/src/lib.rs @@ -11,8 +11,13 @@ pub use gst; macro_rules! assert_initialized_main_thread { () => { - if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { - panic!("GStreamer has not been initialized. Call `gst::init` first."); + 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); + } } }; } diff --git a/gstreamer-video/src/lib.rs b/gstreamer-video/src/lib.rs index 07b02dd44..63cbdae22 100644 --- a/gstreamer-video/src/lib.rs +++ b/gstreamer-video/src/lib.rs @@ -12,9 +12,13 @@ pub use gst_base; macro_rules! assert_initialized_main_thread { () => { - #[allow(unused_unsafe)] - if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { - panic!("GStreamer has not been initialized. Call `gst::init` first."); + 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); + } } }; } diff --git a/gstreamer-webrtc/src/lib.rs b/gstreamer-webrtc/src/lib.rs index 51684f04c..7f4eb8fab 100644 --- a/gstreamer-webrtc/src/lib.rs +++ b/gstreamer-webrtc/src/lib.rs @@ -12,8 +12,13 @@ pub use gst_sdp; macro_rules! assert_initialized_main_thread { () => { - if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { - panic!("GStreamer has not been initialized. Call `gst::init` first."); + 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); + } } }; } diff --git a/gstreamer/src/lib.rs b/gstreamer/src/lib.rs index d921e8b62..a1ef97928 100644 --- a/gstreamer/src/lib.rs +++ b/gstreamer/src/lib.rs @@ -14,10 +14,18 @@ pub use paste; use glib::translate::{from_glib, from_glib_full}; +#[doc(hidden)] +pub static INITIALIZED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); + macro_rules! assert_initialized_main_thread { () => { - if unsafe { ffi::gst_is_initialized() } != glib::ffi::GTRUE { - panic!("GStreamer has not been initialized. Call `gst::init` first."); + if !crate::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { + #[allow(unused_unsafe)] + if unsafe { ffi::gst_is_initialized() } != glib::ffi::GTRUE { + panic!("GStreamer has not been initialized. Call `gst::init` first."); + } else { + crate::INITIALIZED.store(true, std::sync::atomic::Ordering::SeqCst); + } } }; } @@ -263,6 +271,7 @@ pub fn init() -> Result<(), glib::Error> { ptr::null_mut(), &mut error, )) { + crate::INITIALIZED.store(true, std::sync::atomic::Ordering::SeqCst); Ok(()) } else { Err(from_glib_full(error)) @@ -278,6 +287,7 @@ pub fn init() -> Result<(), glib::Error> { /// This must only be called once during the lifetime of the process, once no GStreamer threads /// are running anymore and all GStreamer resources are released. pub unsafe fn deinit() { + crate::INITIALIZED.store(false, std::sync::atomic::Ordering::SeqCst); ffi::gst_deinit(); }