gstreamer: Move initialization panic into a separate function

This keeps the number of duplicated panics all over the codebase lower
and reduces generated code size.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1184>
This commit is contained in:
Sebastian Dröge 2023-01-05 16:40:15 +02:00
parent 7b279b9d6c
commit ba1d8c5ce6
24 changed files with 58 additions and 133 deletions

View file

@ -12,12 +12,7 @@ pub use gst;
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
#[allow(unused_unsafe)] gst::assert_initialized();
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);
}
} }
}; };
} }

View file

@ -10,6 +10,14 @@ pub use glib;
pub use gst; pub use gst;
pub use gst_base; pub use gst_base;
macro_rules! assert_initialized_main_thread {
() => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
gst::assert_initialized();
}
};
}
macro_rules! skip_assert_initialized { macro_rules! skip_assert_initialized {
() => {}; () => {};
} }

View file

@ -13,12 +13,7 @@ pub use gst_base;
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
#[allow(unused_unsafe)] gst::assert_initialized();
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);
}
} }
}; };
} }

View file

@ -12,12 +12,7 @@ pub use gst;
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
#[allow(unused_unsafe)] gst::assert_initialized();
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);
}
} }
}; };
} }

View file

@ -12,12 +12,7 @@ pub use gst;
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
#[allow(unused_unsafe)] gst::assert_initialized();
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);
}
} }
}; };
} }

View file

@ -12,12 +12,7 @@ pub use gst;
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
#[allow(unused_unsafe)] gst::assert_initialized();
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);
}
} }
}; };
} }

View file

@ -39,12 +39,7 @@ pub unsafe fn deinit() {
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
#[allow(unused_unsafe)] gst::assert_initialized();
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(|| { crate::GES_INIT.call_once(|| {
unsafe { ffi::ges_init() }; unsafe { ffi::ges_init() };

View file

@ -15,8 +15,8 @@ pub use gst_gl;
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
panic!("GStreamer has not been initialized. Call `gst::init` first."); gst::assert_initialized();
} }
}; };
} }

View file

@ -14,12 +14,7 @@ pub use gst_video;
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
#[allow(unused_unsafe)] gst::assert_initialized();
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);
}
} }
}; };
} }

View file

@ -15,8 +15,8 @@ pub use gst_gl;
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
panic!("GStreamer has not been initialized. Call `gst::init` first."); gst::assert_initialized();
} }
}; };
} }

View file

@ -15,8 +15,8 @@ pub use gst_gl;
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if unsafe { gst::ffi::gst_is_initialized() } != glib::ffi::GTRUE { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
panic!("GStreamer has not been initialized. Call `gst::init` first."); gst::assert_initialized();
} }
}; };
} }

View file

@ -13,12 +13,7 @@ static MPEGTS_INIT: Once = Once::new();
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
#[allow(unused_unsafe)] gst::assert_initialized();
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::MPEGTS_INIT.call_once(|| unsafe { ffi::gst_mpegts_initialize() }); crate::MPEGTS_INIT.call_once(|| unsafe { ffi::gst_mpegts_initialize() });
}; };

View file

@ -12,12 +12,7 @@ pub use gst;
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
#[allow(unused_unsafe)] gst::assert_initialized();
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);
}
} }
}; };
} }

View file

@ -16,12 +16,7 @@ static PBUTILS_INIT: Once = Once::new();
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
#[allow(unused_unsafe)] gst::assert_initialized();
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(|| { crate::PBUTILS_INIT.call_once(|| {
unsafe { ffi::gst_pb_utils_init() }; unsafe { ffi::gst_pb_utils_init() };

View file

@ -16,12 +16,7 @@ macro_rules! skip_assert_initialized {
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
#[allow(unused_unsafe)] gst::assert_initialized();
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);
}
} }
}; };
} }

View file

@ -16,12 +16,7 @@ macro_rules! skip_assert_initialized {
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
#[allow(unused_unsafe)] gst::assert_initialized();
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);
}
} }
}; };
} }

View file

@ -13,12 +13,7 @@ pub use gst;
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
#[allow(unused_unsafe)] gst::assert_initialized();
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);
}
} }
}; };
} }

View file

@ -16,12 +16,7 @@ pub use gst_sdp;
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
#[allow(unused_unsafe)] gst::assert_initialized();
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);
}
} }
}; };
} }

View file

@ -13,12 +13,7 @@ pub use gst_sdp;
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
#[allow(unused_unsafe)] gst::assert_initialized();
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);
}
} }
}; };
} }

View file

@ -12,12 +12,7 @@ pub use gst;
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
#[allow(unused_unsafe)] gst::assert_initialized();
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);
}
} }
}; };
} }

View file

@ -8,25 +8,25 @@
#[doc(hidden)] #[doc(hidden)]
pub static INITIALIZED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); pub static INITIALIZED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
macro_rules! assert_initialized_main_thread { #[cold]
() => { #[inline(never)]
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { #[track_caller]
#[allow(unused_unsafe)] pub fn assert_initialized() {
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);
}
}
if !crate::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
if unsafe { ffi::gst_validate_is_initialized() } != glib::ffi::GTRUE { if unsafe { ffi::gst_validate_is_initialized() } != glib::ffi::GTRUE {
panic!( panic!("GStreamer Validate has not been initialized. Call `gst_validate::init` first.");
"GStreamer Validate has not been initialized. Call `gst_validate::init` first."
);
} else { } else {
crate::INITIALIZED.store(true, std::sync::atomic::Ordering::SeqCst); crate::INITIALIZED.store(true, std::sync::atomic::Ordering::SeqCst);
} }
}
macro_rules! assert_initialized_main_thread {
() => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
gst::assert_initialized();
}
if !crate::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
$crate::assert_initialized();
} }
}; };
} }

View file

@ -13,12 +13,7 @@ pub use gst_base;
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
#[allow(unused_unsafe)] gst::assert_initialized();
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);
}
} }
}; };
} }

View file

@ -13,12 +13,7 @@ pub use gst_sdp;
macro_rules! assert_initialized_main_thread { macro_rules! assert_initialized_main_thread {
() => { () => {
if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { if !gst::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
#[allow(unused_unsafe)] gst::assert_initialized();
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);
}
} }
}; };
} }

View file

@ -15,15 +15,22 @@ pub use paste;
#[doc(hidden)] #[doc(hidden)]
pub static INITIALIZED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); pub static INITIALIZED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
macro_rules! assert_initialized_main_thread { #[cold]
() => { #[inline(never)]
if !crate::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) { #[track_caller]
pub fn assert_initialized() {
#[allow(unused_unsafe)] #[allow(unused_unsafe)]
if unsafe { ffi::gst_is_initialized() } != glib::ffi::GTRUE { if unsafe { ffi::gst_is_initialized() } != glib::ffi::GTRUE {
panic!("GStreamer has not been initialized. Call `gst::init` first."); panic!("GStreamer has not been initialized. Call `gst::init` first.");
} else { } else {
crate::INITIALIZED.store(true, std::sync::atomic::Ordering::SeqCst); crate::INITIALIZED.store(true, std::sync::atomic::Ordering::SeqCst);
} }
}
macro_rules! assert_initialized_main_thread {
() => {
if !crate::INITIALIZED.load(std::sync::atomic::Ordering::SeqCst) {
$crate::assert_initialized();
} }
}; };
} }