gstreamer-video: Change functions from returning Option to Result

Partial work for:
https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/issues/216
This commit is contained in:
Tony Jinwoo Ahn 2019-12-17 08:41:51 +00:00 committed by Sebastian Dröge
parent c3b7f0f353
commit 700332dd3e
4 changed files with 57 additions and 34 deletions

View file

@ -158,7 +158,7 @@ pub struct DownstreamForceKeyUnitEvent {
pub fn parse_downstream_force_key_unit_event( pub fn parse_downstream_force_key_unit_event(
event: &gst::EventRef, event: &gst::EventRef,
) -> Option<DownstreamForceKeyUnitEvent> { ) -> Result<DownstreamForceKeyUnitEvent, glib::error::BoolError> {
unsafe { unsafe {
let mut timestamp = mem::MaybeUninit::uninit(); let mut timestamp = mem::MaybeUninit::uninit();
let mut stream_time = mem::MaybeUninit::uninit(); let mut stream_time = mem::MaybeUninit::uninit();
@ -177,7 +177,7 @@ pub fn parse_downstream_force_key_unit_event(
), ),
); );
if res { if res {
Some(DownstreamForceKeyUnitEvent { Ok(DownstreamForceKeyUnitEvent {
timestamp: from_glib(timestamp.assume_init()), timestamp: from_glib(timestamp.assume_init()),
stream_time: from_glib(stream_time.assume_init()), stream_time: from_glib(stream_time.assume_init()),
running_time: from_glib(running_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(), count: count.assume_init(),
}) })
} else { } 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( pub fn parse_upstream_force_key_unit_event(
event: &gst::EventRef, event: &gst::EventRef,
) -> Option<UpstreamForceKeyUnitEvent> { ) -> Result<UpstreamForceKeyUnitEvent, glib::error::BoolError> {
unsafe { unsafe {
let mut running_time = mem::MaybeUninit::uninit(); let mut running_time = mem::MaybeUninit::uninit();
let mut all_headers = mem::MaybeUninit::uninit(); let mut all_headers = mem::MaybeUninit::uninit();
@ -267,13 +267,13 @@ pub fn parse_upstream_force_key_unit_event(
), ),
); );
if res { if res {
Some(UpstreamForceKeyUnitEvent { Ok(UpstreamForceKeyUnitEvent {
running_time: from_glib(running_time.assume_init()), running_time: from_glib(running_time.assume_init()),
all_headers: from_glib(all_headers.assume_init()), all_headers: from_glib(all_headers.assume_init()),
count: count.assume_init(), count: count.assume_init(),
}) })
} else { } else {
None Err(glib_bool_error!("Failed to parse GstEvent"))
} }
} }
} }
@ -284,7 +284,9 @@ pub enum ForceKeyUnitEvent {
Upstream(UpstreamForceKeyUnitEvent), Upstream(UpstreamForceKeyUnitEvent),
} }
pub fn parse_force_key_unit_event(event: &gst::EventRef) -> Option<ForceKeyUnitEvent> { pub fn parse_force_key_unit_event(
event: &gst::EventRef,
) -> Result<ForceKeyUnitEvent, glib::error::BoolError> {
if event.is_upstream() { if event.is_upstream() {
parse_upstream_force_key_unit_event(event).map(ForceKeyUnitEvent::Upstream) parse_upstream_force_key_unit_event(event).map(ForceKeyUnitEvent::Upstream)
} else { } else {
@ -324,7 +326,9 @@ pub struct StillFrameEvent {
pub in_still: bool, pub in_still: bool,
} }
pub fn parse_still_frame_event(event: &gst::EventRef) -> Option<StillFrameEvent> { pub fn parse_still_frame_event(
event: &gst::EventRef,
) -> Result<StillFrameEvent, glib::error::BoolError> {
unsafe { unsafe {
let mut in_still = mem::MaybeUninit::uninit(); let mut in_still = mem::MaybeUninit::uninit();
@ -333,11 +337,11 @@ pub fn parse_still_frame_event(event: &gst::EventRef) -> Option<StillFrameEvent>
in_still.as_mut_ptr(), in_still.as_mut_ptr(),
)); ));
if res { if res {
Some(StillFrameEvent { Ok(StillFrameEvent {
in_still: from_glib(in_still.assume_init()), in_still: from_glib(in_still.assume_init()),
}) })
} else { } else {
None Err(glib_bool_error!("Invalid still-frame event"))
} }
} }
} }

View file

@ -198,12 +198,14 @@ impl fmt::Debug for VideoOverlayCompositionRef {
} }
impl VideoOverlayComposition { impl VideoOverlayComposition {
pub fn new<'a, T: IntoIterator<Item = &'a VideoOverlayRectangle>>(rects: T) -> Option<Self> { pub fn new<'a, T: IntoIterator<Item = &'a VideoOverlayRectangle>>(
rects: T,
) -> Result<Self, glib::error::BoolError> {
unsafe { unsafe {
let mut iter = rects.into_iter(); let mut iter = rects.into_iter();
let first = match iter.next() { let first = match iter.next() {
None => return None, None => return Err(glib_bool_error!("Failed to create VideoOverlayComposition")),
Some(first) => first, 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()) } unsafe { gst_video_sys::gst_video_overlay_composition_n_rectangles(self.as_mut_ptr()) }
} }
pub fn get_rectangle(&self, idx: u32) -> Option<VideoOverlayRectangle> { pub fn get_rectangle(&self, idx: u32) -> Result<VideoOverlayRectangle, glib::error::BoolError> {
if idx >= self.n_rectangles() { if idx >= self.n_rectangles() {
return None; return Err(glib_bool_error!("Invalid index"));
} }
unsafe { 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(), self.as_mut_ptr(),
idx, idx,
)) )) {
Some(r) => Ok(r),
None => Err(glib_bool_error!("Failed to get rectangle")),
}
} }
} }

View file

@ -77,7 +77,7 @@ impl VideoTimeCode {
dt: &glib::DateTime, dt: &glib::DateTime,
flags: VideoTimeCodeFlags, flags: VideoTimeCodeFlags,
field_count: u32, field_count: u32,
) -> Option<VideoTimeCode> { ) -> Result<VideoTimeCode, glib::error::BoolError> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
assert!(*fps.denom() > 0); assert!(*fps.denom() > 0);
unsafe { unsafe {
@ -92,9 +92,9 @@ impl VideoTimeCode {
); );
if res == glib_sys::GFALSE { if res == glib_sys::GFALSE {
None Err(glib_bool_error!("Failed to init video time code"))
} else { } else {
Some(VideoTimeCode(v.assume_init())) Ok(VideoTimeCode(v.assume_init()))
} }
} }
} }
@ -163,7 +163,7 @@ impl ValidVideoTimeCode {
seconds: u32, seconds: u32,
frames: u32, frames: u32,
field_count: u32, field_count: u32,
) -> Option<Self> { ) -> Result<Self, glib::error::BoolError> {
let tc = VideoTimeCode::new( let tc = VideoTimeCode::new(
fps, fps,
latest_daily_jam, latest_daily_jam,
@ -174,7 +174,10 @@ impl ValidVideoTimeCode {
frames, frames,
field_count, 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"))] // #[cfg(any(feature = "v1_16", feature = "dox"))]
@ -195,12 +198,18 @@ impl ValidVideoTimeCode {
} }
#[cfg(any(feature = "v1_12", feature = "dox"))] #[cfg(any(feature = "v1_12", feature = "dox"))]
pub fn add_interval(&self, tc_inter: &VideoTimeCodeInterval) -> Option<VideoTimeCode> { pub fn add_interval(
&self,
tc_inter: &VideoTimeCodeInterval,
) -> Result<VideoTimeCode, glib::error::BoolError> {
unsafe { 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, self.to_glib_none().0,
tc_inter.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) } unsafe { gst_video_sys::gst_video_time_code_nsec_since_daily_jam(self.to_glib_none().0) }
} }
pub fn to_date_time(&self) -> Option<glib::DateTime> { pub fn to_date_time(&self) -> Result<glib::DateTime, glib::error::BoolError> {
unsafe { 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, 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"))] #[cfg(any(feature = "v1_12", feature = "dox"))]
impl str::FromStr for VideoTimeCode { impl str::FromStr for VideoTimeCode {
type Err = (); type Err = glib::error::BoolError;
fn from_str(s: &str) -> Result<Self, ()> { fn from_str(s: &str) -> Result<Self, glib::error::BoolError> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
Option::<VideoTimeCode>::from_glib_full( Option::<VideoTimeCode>::from_glib_full(
gst_video_sys::gst_video_time_code_new_from_string(s.to_glib_none().0), 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"))
} }
} }
} }

View file

@ -128,15 +128,15 @@ impl fmt::Display for VideoTimeCodeInterval {
} }
impl str::FromStr for VideoTimeCodeInterval { impl str::FromStr for VideoTimeCodeInterval {
type Err = (); type Err = glib::error::BoolError;
fn from_str(s: &str) -> Result<Self, ()> { fn from_str(s: &str) -> Result<Self, glib::error::BoolError> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
Option::<VideoTimeCodeInterval>::from_glib_full( Option::<VideoTimeCodeInterval>::from_glib_full(
gst_video_sys::gst_video_time_code_interval_new_from_string(s.to_glib_none().0), 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"))
} }
} }
} }