gstreamer/clock: Creating new clock ids can't fail gracefully but invalid arguments should be checked

This commit is contained in:
Sebastian Dröge 2020-10-17 20:25:54 +03:00
parent 501934a29b
commit 0207e41160

View file

@ -381,11 +381,7 @@ impl Clock {
} }
pub trait ClockExtManual: 'static { pub trait ClockExtManual: 'static {
fn new_periodic_id( fn new_periodic_id(&self, start_time: ClockTime, interval: ClockTime) -> PeriodicClockId;
&self,
start_time: ClockTime,
interval: ClockTime,
) -> Result<PeriodicClockId, glib::BoolError>;
fn periodic_id_reinit( fn periodic_id_reinit(
&self, &self,
@ -394,7 +390,7 @@ pub trait ClockExtManual: 'static {
interval: ClockTime, interval: ClockTime,
) -> Result<(), glib::BoolError>; ) -> Result<(), glib::BoolError>;
fn new_single_shot_id(&self, time: ClockTime) -> Result<SingleShotClockId, glib::BoolError>; fn new_single_shot_id(&self, time: ClockTime) -> SingleShotClockId;
fn single_shot_id_reinit( fn single_shot_id_reinit(
&self, &self,
@ -410,19 +406,17 @@ pub trait ClockExtManual: 'static {
} }
impl<O: IsA<Clock>> ClockExtManual for O { impl<O: IsA<Clock>> ClockExtManual for O {
fn new_periodic_id( fn new_periodic_id(&self, start_time: ClockTime, interval: ClockTime) -> PeriodicClockId {
&self, assert!(start_time.is_some());
start_time: ClockTime, assert!(interval.is_some());
interval: ClockTime, assert_ne!(interval, ::ClockTime::from(0));
) -> Result<PeriodicClockId, glib::BoolError> {
unsafe { unsafe {
Option::<_>::from_glib_full(gst_sys::gst_clock_new_periodic_id( PeriodicClockId(from_glib_full(gst_sys::gst_clock_new_periodic_id(
self.as_ref().to_glib_none().0, self.as_ref().to_glib_none().0,
start_time.to_glib(), start_time.to_glib(),
interval.to_glib(), interval.to_glib(),
)) )))
.map(PeriodicClockId)
.ok_or_else(|| glib_bool_error!("Failed to create new periodic clock id"))
} }
} }
@ -448,14 +442,14 @@ impl<O: IsA<Clock>> ClockExtManual for O {
} }
} }
fn new_single_shot_id(&self, time: ClockTime) -> Result<SingleShotClockId, glib::BoolError> { fn new_single_shot_id(&self, time: ClockTime) -> SingleShotClockId {
assert!(time.is_some());
unsafe { unsafe {
Option::<_>::from_glib_full(gst_sys::gst_clock_new_single_shot_id( SingleShotClockId(from_glib_full(gst_sys::gst_clock_new_single_shot_id(
self.as_ref().to_glib_none().0, self.as_ref().to_glib_none().0,
time.to_glib(), time.to_glib(),
)) )))
.map(SingleShotClockId)
.ok_or_else(|| glib_bool_error!("Failed to create new single shot clock id"))
} }
} }
@ -515,7 +509,7 @@ mod tests {
let clock = SystemClock::obtain(); let clock = SystemClock::obtain();
let now = clock.get_time(); let now = clock.get_time();
let id = clock.new_single_shot_id(now + 20 * ::MSECOND).unwrap(); let id = clock.new_single_shot_id(now + 20 * ::MSECOND);
let (res, _) = id.wait(); let (res, _) = id.wait();
assert!(res == Ok(ClockSuccess::Ok) || res == Err(ClockError::Early)); assert!(res == Ok(ClockSuccess::Ok) || res == Err(ClockError::Early));
@ -529,7 +523,7 @@ mod tests {
let clock = SystemClock::obtain(); let clock = SystemClock::obtain();
let now = clock.get_time(); let now = clock.get_time();
let id = clock.new_single_shot_id(now + 20 * ::MSECOND).unwrap(); let id = clock.new_single_shot_id(now + 20 * ::MSECOND);
let res = id.wait_async(move |_, _, _| { let res = id.wait_async(move |_, _, _| {
sender.send(()).unwrap(); sender.send(()).unwrap();
}); });
@ -545,9 +539,7 @@ mod tests {
let clock = SystemClock::obtain(); let clock = SystemClock::obtain();
let now = clock.get_time(); let now = clock.get_time();
let id = clock let id = clock.new_periodic_id(now + 20 * ::MSECOND, 20 * ::MSECOND);
.new_periodic_id(now + 20 * ::MSECOND, 20 * ::MSECOND)
.unwrap();
let (res, _) = id.wait(); let (res, _) = id.wait();
assert!(res == Ok(ClockSuccess::Ok) || res == Err(ClockError::Early)); assert!(res == Ok(ClockSuccess::Ok) || res == Err(ClockError::Early));
@ -564,9 +556,7 @@ mod tests {
let clock = SystemClock::obtain(); let clock = SystemClock::obtain();
let now = clock.get_time(); let now = clock.get_time();
let id = clock let id = clock.new_periodic_id(now + 20 * ::MSECOND, 20 * ::MSECOND);
.new_periodic_id(now + 20 * ::MSECOND, 20 * ::MSECOND)
.unwrap();
let res = id.wait_async(move |_, _, _| { let res = id.wait_async(move |_, _, _| {
let _ = sender.send(()); let _ = sender.send(());
}); });