gstreamer-app: Assert that GStreamer is initialized before creating instances

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1193>
This commit is contained in:
Sebastian Dröge 2023-01-05 16:39:56 +02:00
parent 80fba8f1e0
commit b7a52b0da0
3 changed files with 15 additions and 0 deletions

View file

@ -273,6 +273,7 @@ impl AppSink {
/// ///
/// This method returns an instance of [`AppSinkBuilder`](crate::builders::AppSinkBuilder) which can be used to create [`AppSink`] objects. /// This method returns an instance of [`AppSinkBuilder`](crate::builders::AppSinkBuilder) which can be used to create [`AppSink`] objects.
pub fn builder() -> AppSinkBuilder { pub fn builder() -> AppSinkBuilder {
assert_initialized_main_thread!();
AppSinkBuilder::default() AppSinkBuilder::default()
} }

View file

@ -210,6 +210,7 @@ impl AppSrc {
/// ///
/// This method returns an instance of [`AppSrcBuilder`](crate::builders::AppSrcBuilder) which can be used to create [`AppSrc`] objects. /// This method returns an instance of [`AppSrcBuilder`](crate::builders::AppSrcBuilder) which can be used to create [`AppSrc`] objects.
pub fn builder() -> AppSrcBuilder { pub fn builder() -> AppSrcBuilder {
assert_initialized_main_thread!();
AppSrcBuilder::default() AppSrcBuilder::default()
} }

View file

@ -10,6 +10,19 @@ 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) {
#[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 { macro_rules! skip_assert_initialized {
() => {}; () => {};
} }