diff --git a/gstreamer-video/src/video_event.rs b/gstreamer-video/src/video_event.rs index 00f16b3bc..135fd9a89 100644 --- a/gstreamer-video/src/video_event.rs +++ b/gstreamer-video/src/video_event.rs @@ -158,7 +158,7 @@ pub struct DownstreamForceKeyUnitEvent { pub fn parse_downstream_force_key_unit_event( event: &gst::EventRef, -) -> Option { +) -> Result { unsafe { let mut timestamp = mem::MaybeUninit::uninit(); let mut stream_time = mem::MaybeUninit::uninit(); @@ -177,7 +177,7 @@ pub fn parse_downstream_force_key_unit_event( ), ); if res { - Some(DownstreamForceKeyUnitEvent { + Ok(DownstreamForceKeyUnitEvent { timestamp: from_glib(timestamp.assume_init()), stream_time: from_glib(stream_time.assume_init()), running_time: from_glib(running_time.assume_init()), @@ -185,7 +185,7 @@ pub fn parse_downstream_force_key_unit_event( count: count.assume_init(), }) } else { - None + Err(glib_bool_error!("Failed to parse GstEvent")) } } } @@ -252,7 +252,7 @@ pub struct UpstreamForceKeyUnitEvent { pub fn parse_upstream_force_key_unit_event( event: &gst::EventRef, -) -> Option { +) -> Result { unsafe { let mut running_time = mem::MaybeUninit::uninit(); let mut all_headers = mem::MaybeUninit::uninit(); @@ -267,13 +267,13 @@ pub fn parse_upstream_force_key_unit_event( ), ); if res { - Some(UpstreamForceKeyUnitEvent { + Ok(UpstreamForceKeyUnitEvent { running_time: from_glib(running_time.assume_init()), all_headers: from_glib(all_headers.assume_init()), count: count.assume_init(), }) } else { - None + Err(glib_bool_error!("Failed to parse GstEvent")) } } } @@ -284,7 +284,9 @@ pub enum ForceKeyUnitEvent { Upstream(UpstreamForceKeyUnitEvent), } -pub fn parse_force_key_unit_event(event: &gst::EventRef) -> Option { +pub fn parse_force_key_unit_event( + event: &gst::EventRef, +) -> Result { if event.is_upstream() { parse_upstream_force_key_unit_event(event).map(ForceKeyUnitEvent::Upstream) } else { @@ -324,7 +326,9 @@ pub struct StillFrameEvent { pub in_still: bool, } -pub fn parse_still_frame_event(event: &gst::EventRef) -> Option { +pub fn parse_still_frame_event( + event: &gst::EventRef, +) -> Result { unsafe { let mut in_still = mem::MaybeUninit::uninit(); @@ -333,11 +337,11 @@ pub fn parse_still_frame_event(event: &gst::EventRef) -> Option in_still.as_mut_ptr(), )); if res { - Some(StillFrameEvent { + Ok(StillFrameEvent { in_still: from_glib(in_still.assume_init()), }) } else { - None + Err(glib_bool_error!("Invalid still-frame event")) } } } diff --git a/gstreamer-video/src/video_overlay_composition.rs b/gstreamer-video/src/video_overlay_composition.rs index 23ab40005..4eb2e1085 100644 --- a/gstreamer-video/src/video_overlay_composition.rs +++ b/gstreamer-video/src/video_overlay_composition.rs @@ -198,12 +198,14 @@ impl fmt::Debug for VideoOverlayCompositionRef { } impl VideoOverlayComposition { - pub fn new<'a, T: IntoIterator>(rects: T) -> Option { + pub fn new<'a, T: IntoIterator>( + rects: T, + ) -> Result { unsafe { let mut iter = rects.into_iter(); let first = match iter.next() { - None => return None, + None => return Err(glib_bool_error!("Failed to create VideoOverlayComposition")), Some(first) => first, }; @@ -218,7 +220,7 @@ impl VideoOverlayComposition { ); } - Some(composition) + Ok(composition) } } } @@ -228,16 +230,19 @@ impl VideoOverlayCompositionRef { unsafe { gst_video_sys::gst_video_overlay_composition_n_rectangles(self.as_mut_ptr()) } } - pub fn get_rectangle(&self, idx: u32) -> Option { + pub fn get_rectangle(&self, idx: u32) -> Result { if idx >= self.n_rectangles() { - return None; + return Err(glib_bool_error!("Invalid index")); } unsafe { - from_glib_none(gst_video_sys::gst_video_overlay_composition_get_rectangle( + match from_glib_none(gst_video_sys::gst_video_overlay_composition_get_rectangle( self.as_mut_ptr(), idx, - )) + )) { + Some(r) => Ok(r), + None => Err(glib_bool_error!("Failed to get rectangle")), + } } } diff --git a/gstreamer-video/src/video_time_code.rs b/gstreamer-video/src/video_time_code.rs index 70b6c81de..9ea10cf93 100644 --- a/gstreamer-video/src/video_time_code.rs +++ b/gstreamer-video/src/video_time_code.rs @@ -77,7 +77,7 @@ impl VideoTimeCode { dt: &glib::DateTime, flags: VideoTimeCodeFlags, field_count: u32, - ) -> Option { + ) -> Result { assert_initialized_main_thread!(); assert!(*fps.denom() > 0); unsafe { @@ -92,9 +92,9 @@ impl VideoTimeCode { ); if res == glib_sys::GFALSE { - None + Err(glib_bool_error!("Failed to init video time code")) } else { - Some(VideoTimeCode(v.assume_init())) + Ok(VideoTimeCode(v.assume_init())) } } } @@ -163,7 +163,7 @@ impl ValidVideoTimeCode { seconds: u32, frames: u32, field_count: u32, - ) -> Option { + ) -> Result { let tc = VideoTimeCode::new( fps, latest_daily_jam, @@ -174,7 +174,10 @@ impl ValidVideoTimeCode { frames, field_count, ); - tc.try_into().ok() + match tc.try_into() { + Ok(v) => Ok(v), + Err(_) => Err(glib_bool_error!("Failed to create new ValidVideoTimeCode")), + } } // #[cfg(any(feature = "v1_16", feature = "dox"))] @@ -195,12 +198,18 @@ impl ValidVideoTimeCode { } #[cfg(any(feature = "v1_12", feature = "dox"))] - pub fn add_interval(&self, tc_inter: &VideoTimeCodeInterval) -> Option { + pub fn add_interval( + &self, + tc_inter: &VideoTimeCodeInterval, + ) -> Result { unsafe { - from_glib_full(gst_video_sys::gst_video_time_code_add_interval( + match from_glib_full(gst_video_sys::gst_video_time_code_add_interval( self.to_glib_none().0, tc_inter.to_glib_none().0, - )) + )) { + Some(i) => Ok(i), + None => Err(glib_bool_error!("Failed to add interval")), + } } } @@ -224,11 +233,16 @@ impl ValidVideoTimeCode { unsafe { gst_video_sys::gst_video_time_code_nsec_since_daily_jam(self.to_glib_none().0) } } - pub fn to_date_time(&self) -> Option { + pub fn to_date_time(&self) -> Result { unsafe { - from_glib_full(gst_video_sys::gst_video_time_code_to_date_time( + match from_glib_full(gst_video_sys::gst_video_time_code_to_date_time( self.to_glib_none().0, - )) + )) { + Some(d) => Ok(d), + None => Err(glib_bool_error!( + "Failed to convert VideoTimeCode to date time" + )), + } } } } @@ -466,15 +480,15 @@ generic_impl!(ValidVideoTimeCode); #[cfg(any(feature = "v1_12", feature = "dox"))] impl str::FromStr for VideoTimeCode { - type Err = (); + type Err = glib::error::BoolError; - fn from_str(s: &str) -> Result { + fn from_str(s: &str) -> Result { assert_initialized_main_thread!(); unsafe { Option::::from_glib_full( gst_video_sys::gst_video_time_code_new_from_string(s.to_glib_none().0), ) - .ok_or(()) + .ok_or_else(|| glib_bool_error!("Failed to create VideoTimeCode from string")) } } } diff --git a/gstreamer-video/src/video_time_code_interval.rs b/gstreamer-video/src/video_time_code_interval.rs index 1e2f28447..648dd7233 100644 --- a/gstreamer-video/src/video_time_code_interval.rs +++ b/gstreamer-video/src/video_time_code_interval.rs @@ -128,15 +128,15 @@ impl fmt::Display for VideoTimeCodeInterval { } impl str::FromStr for VideoTimeCodeInterval { - type Err = (); + type Err = glib::error::BoolError; - fn from_str(s: &str) -> Result { + fn from_str(s: &str) -> Result { assert_initialized_main_thread!(); unsafe { Option::::from_glib_full( gst_video_sys::gst_video_time_code_interval_new_from_string(s.to_glib_none().0), ) - .ok_or(()) + .ok_or_else(|| glib_bool_error!("Failed to create VideoTimeCodeInterval from string")) } } }