diff --git a/gstreamer-app/src/app_sink.rs b/gstreamer-app/src/app_sink.rs index 40823cf15..275f66ac0 100644 --- a/gstreamer-app/src/app_sink.rs +++ b/gstreamer-app/src/app_sink.rs @@ -29,6 +29,8 @@ impl AppSinkCallbacks { G: Fn(&AppSink) -> gst::FlowReturn + Send + Sync + 'static, H: Fn(&AppSink) -> gst::FlowReturn + Send + Sync + 'static, { + skip_assert_initialized!(); + AppSinkCallbacks { eos: Box::new(eos), new_preroll: Box::new(new_preroll), diff --git a/gstreamer-app/src/app_src.rs b/gstreamer-app/src/app_src.rs index 292745127..9d23afa57 100644 --- a/gstreamer-app/src/app_src.rs +++ b/gstreamer-app/src/app_src.rs @@ -28,6 +28,8 @@ impl AppSrcCallbacks { G: Fn(&AppSrc) + Send + Sync + 'static, H: Fn(&AppSrc, u64) -> bool + Send + Sync + 'static, { + skip_assert_initialized!(); + AppSrcCallbacks { need_data: Box::new(need_data), enough_data: Box::new(enough_data), diff --git a/gstreamer-video/src/lib.rs b/gstreamer-video/src/lib.rs index 903a026ec..e94cedfc8 100644 --- a/gstreamer-video/src/lib.rs +++ b/gstreamer-video/src/lib.rs @@ -18,6 +18,14 @@ extern crate gstreamer as gst; #[macro_use] extern crate glib; +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."); + } + ) +} + macro_rules! skip_assert_initialized { () => ( ) diff --git a/gstreamer-video/src/video_format.rs b/gstreamer-video/src/video_format.rs index 14570fb73..37b85764c 100644 --- a/gstreamer-video/src/video_format.rs +++ b/gstreamer-video/src/video_format.rs @@ -23,6 +23,8 @@ pub enum VideoEndianness { impl FromGlib for VideoEndianness { fn from_glib(value: i32) -> Self { + skip_assert_initialized!(); + match value { 1234 => VideoEndianness::LittleEndian, 4321 => VideoEndianness::BigEndian, @@ -45,10 +47,14 @@ impl ToGlib for VideoEndianness { impl ::VideoFormat { pub fn from_string(s: &str) -> ::VideoFormat { + assert_initialized_main_thread!(); + unsafe { from_glib(ffi::gst_video_format_from_string(s.to_glib_none().0)) } } pub fn from_fourcc(fourcc: u32) -> ::VideoFormat { + assert_initialized_main_thread!(); + unsafe { from_glib(ffi::gst_video_format_from_fourcc(fourcc)) } } @@ -61,6 +67,8 @@ impl ::VideoFormat { green_mask: u32, alpha_mask: u32, ) -> ::VideoFormat { + assert_initialized_main_thread!(); + unsafe { from_glib(ffi::gst_video_format_from_masks( depth as i32, @@ -87,6 +95,8 @@ impl str::FromStr for ::VideoFormat { type Err = (); fn from_str(s: &str) -> Result { + skip_assert_initialized!(); + let format = Self::from_string(s); if format == ::VideoFormat::Unknown { Err(()) diff --git a/gstreamer-video/src/video_format_info.rs b/gstreamer-video/src/video_format_info.rs index 4f5d61a5b..03b86196c 100644 --- a/gstreamer-video/src/video_format_info.rs +++ b/gstreamer-video/src/video_format_info.rs @@ -19,6 +19,8 @@ pub struct VideoFormatInfo(&'static ffi::GstVideoFormatInfo); impl VideoFormatInfo { pub fn from_format(format: ::VideoFormat) -> VideoFormatInfo { + assert_initialized_main_thread!(); + unsafe { let info = ffi::gst_video_format_get_info(format.to_glib()); assert!(!info.is_null()); @@ -173,6 +175,7 @@ impl str::FromStr for ::VideoFormatInfo { type Err = (); fn from_str(s: &str) -> Result { + skip_assert_initialized!(); let format = s.parse()?; Ok(VideoFormatInfo::from_format(format)) } @@ -180,6 +183,7 @@ impl str::FromStr for ::VideoFormatInfo { impl From<::VideoFormat> for VideoFormatInfo { fn from(f: ::VideoFormat) -> Self { + skip_assert_initialized!(); Self::from_format(f) } } diff --git a/gstreamer-video/src/video_frame.rs b/gstreamer-video/src/video_frame.rs index 8978549a3..aaa0fb8e2 100644 --- a/gstreamer-video/src/video_frame.rs +++ b/gstreamer-video/src/video_frame.rs @@ -74,6 +74,8 @@ impl VideoFrame { dest: &mut VideoFrame, plane: u32, ) -> Result<(), glib::BoolError> { + skip_assert_initialized!(); + unsafe { let res: bool = from_glib(ffi::gst_video_frame_copy_plane(&mut dest.0, &self.0, plane)); if res { @@ -177,6 +179,8 @@ impl VideoFrame { buffer: gst::Buffer, info: &::VideoInfo, ) -> Result, gst::Buffer> { + assert_initialized_main_thread!(); + unsafe { let mut frame = mem::zeroed(); let res: bool = from_glib(ffi::gst_video_frame_map( @@ -202,6 +206,8 @@ impl VideoFrame { id: i32, info: &::VideoInfo, ) -> Result, gst::Buffer> { + assert_initialized_main_thread!(); + unsafe { let mut frame = mem::zeroed(); let res: bool = from_glib(ffi::gst_video_frame_map_id( @@ -233,6 +239,8 @@ impl VideoFrame { buffer: gst::Buffer, info: &::VideoInfo, ) -> Result, gst::Buffer> { + assert_initialized_main_thread!(); + unsafe { let mut frame = mem::zeroed(); let res: bool = from_glib(ffi::gst_video_frame_map( @@ -259,6 +267,8 @@ impl VideoFrame { id: i32, info: &::VideoInfo, ) -> Result, gst::Buffer> { + assert_initialized_main_thread!(); + unsafe { let mut frame = mem::zeroed(); let res: bool = from_glib(ffi::gst_video_frame_map_id( diff --git a/gstreamer-video/src/video_info.rs b/gstreamer-video/src/video_info.rs index 92a240db2..c3534738c 100644 --- a/gstreamer-video/src/video_info.rs +++ b/gstreamer-video/src/video_info.rs @@ -92,6 +92,7 @@ impl VideoColorimetry { transfer: ::VideoTransferFunction, primaries: ::VideoColorPrimaries, ) -> Self { + assert_initialized_main_thread!(); let colorimetry = unsafe { let mut colorimetry: ffi::GstVideoColorimetry = mem::zeroed(); @@ -112,6 +113,8 @@ impl VideoColorimetry { } pub fn from_string(s: &str) -> Option { + assert_initialized_main_thread!(); + unsafe { let mut colorimetry = mem::zeroed(); let valid: bool = from_glib(ffi::gst_video_colorimetry_from_string( @@ -145,6 +148,7 @@ impl str::FromStr for ::VideoColorimetry { type Err = (); fn from_str(s: &str) -> Result { + skip_assert_initialized!(); Self::from_string(s).ok_or(()) } } @@ -370,6 +374,8 @@ impl<'a> VideoInfoBuilder<'a> { impl VideoInfo { pub fn new<'a>(format: ::VideoFormat, width: u32, height: u32) -> VideoInfoBuilder<'a> { + assert_initialized_main_thread!(); + #[cfg(not(feature = "v1_12"))] { VideoInfoBuilder { @@ -414,6 +420,8 @@ impl VideoInfo { } pub fn from_caps(caps: &gst::Caps) -> Option { + assert_initialized_main_thread!(); + unsafe { let mut info = mem::uninitialized(); if from_glib(ffi::gst_video_info_from_caps(&mut info, caps.as_ptr())) { @@ -547,6 +555,8 @@ impl VideoInfo { src_val: i64, dest_fmt: gst::Format, ) -> Option { + skip_assert_initialized!(); + unsafe { let mut dest_val = mem::uninitialized(); if from_glib(ffi::gst_video_info_convert( @@ -668,6 +678,8 @@ impl ::VideoFieldOrder { } pub fn from_string(s: &str) -> Self { + assert_initialized_main_thread!(); + unsafe { from_glib(ffi::gst_video_field_order_from_string(s.to_glib_none().0)) } } } @@ -677,6 +689,7 @@ impl str::FromStr for ::VideoFieldOrder { type Err = (); fn from_str(s: &str) -> Result { + skip_assert_initialized!(); Ok(Self::from_string(s)) } } @@ -694,6 +707,8 @@ impl ::VideoInterlaceMode { } pub fn from_string(s: &str) -> Self { + assert_initialized_main_thread!(); + unsafe { from_glib(ffi::gst_video_interlace_mode_from_string( s.to_glib_none().0, @@ -706,6 +721,7 @@ impl str::FromStr for ::VideoInterlaceMode { type Err = (); fn from_str(s: &str) -> Result { + skip_assert_initialized!(); Ok(Self::from_string(s)) } } diff --git a/gstreamer/src/lib.rs b/gstreamer/src/lib.rs index 62d4682d3..c0715a516 100644 --- a/gstreamer/src/lib.rs +++ b/gstreamer/src/lib.rs @@ -35,7 +35,9 @@ macro_rules! callback_guard { macro_rules! assert_initialized_main_thread { () => ( - assert_eq!(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."); + } ) }