From 333d71f92bb1bd7c1cb99fa727bec62cef40ae2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Laignel?= Date: Wed, 16 Jan 2019 21:23:56 +0100 Subject: [PATCH] Update functions returning bool to use Result<(), glib::BoolError> See https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/issues/171 --- Gir_Gst.toml | 32 ++++++++++++++++- examples/src/bin/events.rs | 2 +- examples/src/bin/gtksink.rs | 2 +- examples/src/bin/gtkvideooverlay.rs | 2 +- examples/src/bin/launch_glib_main.rs | 2 +- examples/src/bin/queries.rs | 2 +- gstreamer/src/auto/bin.rs | 16 --------- gstreamer/src/auto/bus.rs | 4 +-- gstreamer/src/auto/device.rs | 7 ++-- gstreamer/src/auto/device_monitor.rs | 6 ++-- gstreamer/src/auto/functions.rs | 5 +-- gstreamer/src/auto/pad.rs | 8 ++--- gstreamer/src/bin.rs | 48 ++++++++++++++++++++++++-- gstreamer/src/buffer_pool.rs | 19 +++++----- gstreamer/src/ghost_pad.rs | 37 ++++++++++++-------- gstreamer/src/pad.rs | 40 +++++++++++++++------ gstreamer/src/segment.rs | 30 +++++++++------- gstreamer/src/subclass/bin.rs | 44 ++++++++++++++++------- gstreamer/src/subclass/plugin.rs | 29 +++++++++------- tutorials/src/bin/basic-tutorial-12.rs | 4 +-- 20 files changed, 229 insertions(+), 110 deletions(-) diff --git a/Gir_Gst.toml b/Gir_Gst.toml index 5ca86c57d..a2789b4ce 100644 --- a/Gir_Gst.toml +++ b/Gir_Gst.toml @@ -86,6 +86,11 @@ name = "Gst.Bin" subclassing = true status = "generate" trait_name = "GstBinExt" + [[object.signal]] + name = "do-latency" + # Use Result<(), glib::BoolError> + ignore = true + [[object.function]] name = "add" [object.function.return] @@ -133,6 +138,11 @@ trait = false # More convenient manual implementation ignore = true + [[object.function]] + name = "remove_watch" + [object.function.return] + bool_return_is_error = "Bus has no event source" + [[object.signal]] name = "message" concurrency = "send" @@ -547,6 +557,11 @@ status = "generate" [object.function.return] bool_return_is_error = "Failed to start" + [[object.function]] + name = "remove_filter" + [object.function.return] + bool_return_is_error = "Failed to remove the filter" + [[object]] name = "Gst.Device" status = "generate" @@ -560,6 +575,11 @@ status = "generate" [object.function.return] nullable = false + [[object.function]] + name = "reconfigure_element" + [object.function.return] + bool_return_is_error = "Failed to reconfigure the element to use this device" + [[object]] name = "Gst.Object" # For renaming the trait... @@ -603,7 +623,12 @@ status = "generate" [[object.function]] name = "link_maybe_ghosting" [object.function.return] - bool_return_is_error = "Failed to link pad, possibly ghosting" + bool_return_is_error = "Failed to link pads, possibly ghosting" + + [[object.function]] + name = "link_maybe_ghosting_full" + [object.function.return] + bool_return_is_error = "Failed to link pads, possibly ghosting" [[object.function]] name = "unlink" @@ -1187,6 +1212,11 @@ status = "generate" # wrong mutable for context parameter ignore = true + [[object.function]] + name = "update_registry" + [object.function.return] + bool_return_is_error = "Failed to update the registry" + [[object.function]] name = "util_group_id_next" # newtype wrapper diff --git a/examples/src/bin/events.rs b/examples/src/bin/events.rs index cb02100ae..35f17fe38 100644 --- a/examples/src/bin/events.rs +++ b/examples/src/bin/events.rs @@ -130,7 +130,7 @@ fn example_main() { // Remove the watch function from the bus. // Again: There can always only be one watch function. // Thus we don't have to tell him which function to remove. - bus.remove_watch(); + bus.remove_watch().unwrap(); } fn main() { diff --git a/examples/src/bin/gtksink.rs b/examples/src/bin/gtksink.rs index e2ad45239..06e95bbb5 100644 --- a/examples/src/bin/gtksink.rs +++ b/examples/src/bin/gtksink.rs @@ -146,7 +146,7 @@ fn create_ui(app: >k::Application) { .set_state(gst::State::Null) .expect("Unable to set the pipeline to the `Null` state"); - bus.remove_watch(); + bus.remove_watch().unwrap(); if let Some(timeout_id) = timeout_id.borrow_mut().take() { glib::source_remove(timeout_id); } diff --git a/examples/src/bin/gtkvideooverlay.rs b/examples/src/bin/gtkvideooverlay.rs index 467e3b742..9d6a5241e 100644 --- a/examples/src/bin/gtkvideooverlay.rs +++ b/examples/src/bin/gtkvideooverlay.rs @@ -249,7 +249,7 @@ fn create_ui(app: >k::Application) { .set_state(gst::State::Null) .expect("Unable to set the pipeline to the `Null` state"); - bus.remove_watch(); + bus.remove_watch().unwrap(); if let Some(timeout_id) = timeout_id.borrow_mut().take() { glib::source_remove(timeout_id); } diff --git a/examples/src/bin/launch_glib_main.rs b/examples/src/bin/launch_glib_main.rs index c8820b32a..9e600091e 100644 --- a/examples/src/bin/launch_glib_main.rs +++ b/examples/src/bin/launch_glib_main.rs @@ -68,7 +68,7 @@ fn example_main() { // Here we remove the bus watch we added above. This avoids a memory leak, that might // otherwise happen because we moved a strong reference (clone of main_loop) into the // callback closure above. - bus.remove_watch(); + bus.remove_watch().unwrap(); } fn main() { diff --git a/examples/src/bin/queries.rs b/examples/src/bin/queries.rs index 6e5ad2074..0c66f9212 100644 --- a/examples/src/bin/queries.rs +++ b/examples/src/bin/queries.rs @@ -124,7 +124,7 @@ fn example_main() { .set_state(gst::State::Null) .expect("Unable to set the pipeline to the `Null` state"); - bus.remove_watch(); + bus.remove_watch().unwrap(); glib::source_remove(timeout_id); } diff --git a/gstreamer/src/auto/bin.rs b/gstreamer/src/auto/bin.rs index 952137dfc..b8d02d030 100644 --- a/gstreamer/src/auto/bin.rs +++ b/gstreamer/src/auto/bin.rs @@ -99,8 +99,6 @@ pub trait GstBinExt: 'static { #[cfg(any(feature = "v1_10", feature = "dox"))] fn connect_deep_element_removed(&self, f: F) -> SignalHandlerId; - fn connect_do_latency bool + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId; - fn connect_element_added(&self, f: F) -> SignalHandlerId; fn connect_element_removed(&self, f: F) -> SignalHandlerId; @@ -251,14 +249,6 @@ impl> GstBinExt for O { } } - fn connect_do_latency bool + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId { - unsafe { - let f: Box_ bool + Send + Sync + 'static>> = Box_::new(Box_::new(f)); - connect_raw(self.as_ptr() as *mut _, b"do-latency\0".as_ptr() as *const _, - transmute(do_latency_trampoline:: as usize), Box_::into_raw(f) as *mut _) - } - } - fn connect_element_added(&self, f: F) -> SignalHandlerId { unsafe { let f: Box_> = Box_::new(Box_::new(f)); @@ -306,12 +296,6 @@ where P: IsA { f(&Bin::from_glib_borrow(this).unsafe_cast(), &from_glib_borrow(sub_bin), &from_glib_borrow(element)) } -unsafe extern "C" fn do_latency_trampoline

(this: *mut ffi::GstBin, f: glib_ffi::gpointer) -> glib_ffi::gboolean -where P: IsA { - let f: &&(Fn(&P) -> bool + Send + Sync + 'static) = transmute(f); - f(&Bin::from_glib_borrow(this).unsafe_cast()).to_glib() -} - unsafe extern "C" fn element_added_trampoline

(this: *mut ffi::GstBin, element: *mut ffi::GstElement, f: glib_ffi::gpointer) where P: IsA { let f: &&(Fn(&P, &Element) + Send + Sync + 'static) = transmute(f); diff --git a/gstreamer/src/auto/bus.rs b/gstreamer/src/auto/bus.rs index dbf5608f1..f8ce37118 100644 --- a/gstreamer/src/auto/bus.rs +++ b/gstreamer/src/auto/bus.rs @@ -96,9 +96,9 @@ impl Bus { } } - pub fn remove_watch(&self) -> bool { + pub fn remove_watch(&self) -> Result<(), glib::error::BoolError> { unsafe { - from_glib(ffi::gst_bus_remove_watch(self.to_glib_none().0)) + glib_result_from_gboolean!(ffi::gst_bus_remove_watch(self.to_glib_none().0), "Bus has no event source") } } diff --git a/gstreamer/src/auto/device.rs b/gstreamer/src/auto/device.rs index 4b972af20..7073b2712 100644 --- a/gstreamer/src/auto/device.rs +++ b/gstreamer/src/auto/device.rs @@ -7,6 +7,7 @@ use Element; use Object; use Structure; use ffi; +use glib; use glib::GString; use glib::object::Cast; use glib::object::IsA; @@ -45,7 +46,7 @@ pub trait DeviceExt: 'static { fn has_classesv(&self, classes: &[&str]) -> bool; - fn reconfigure_element>(&self, element: &P) -> bool; + fn reconfigure_element>(&self, element: &P) -> Result<(), glib::error::BoolError>; fn connect_removed(&self, f: F) -> SignalHandlerId; } @@ -94,9 +95,9 @@ impl> DeviceExt for O { } } - fn reconfigure_element>(&self, element: &P) -> bool { + fn reconfigure_element>(&self, element: &P) -> Result<(), glib::error::BoolError> { unsafe { - from_glib(ffi::gst_device_reconfigure_element(self.as_ref().to_glib_none().0, element.as_ref().to_glib_none().0)) + glib_result_from_gboolean!(ffi::gst_device_reconfigure_element(self.as_ref().to_glib_none().0, element.as_ref().to_glib_none().0), "Failed to reconfigure the element to use this device") } } diff --git a/gstreamer/src/auto/device_monitor.rs b/gstreamer/src/auto/device_monitor.rs index 2bba094b7..f89a7506e 100644 --- a/gstreamer/src/auto/device_monitor.rs +++ b/gstreamer/src/auto/device_monitor.rs @@ -45,7 +45,7 @@ pub trait DeviceMonitorExt: 'static { fn get_show_all_devices(&self) -> bool; - fn remove_filter(&self, filter_id: u32) -> bool; + fn remove_filter(&self, filter_id: u32) -> Result<(), glib::error::BoolError>; fn set_show_all_devices(&self, show_all: bool); @@ -93,9 +93,9 @@ impl> DeviceMonitorExt for O { } } - fn remove_filter(&self, filter_id: u32) -> bool { + fn remove_filter(&self, filter_id: u32) -> Result<(), glib::error::BoolError> { unsafe { - from_glib(ffi::gst_device_monitor_remove_filter(self.as_ref().to_glib_none().0, filter_id)) + glib_result_from_gboolean!(ffi::gst_device_monitor_remove_filter(self.as_ref().to_glib_none().0, filter_id), "Failed to remove the filter") } } diff --git a/gstreamer/src/auto/functions.rs b/gstreamer/src/auto/functions.rs index 61a02a5b2..6f8aa8f44 100644 --- a/gstreamer/src/auto/functions.rs +++ b/gstreamer/src/auto/functions.rs @@ -11,6 +11,7 @@ use Error; #[cfg(any(feature = "v1_12", feature = "dox"))] use StackTraceFlags; use ffi; +use glib; use glib::GString; use glib::object::IsA; use glib::translate::*; @@ -177,10 +178,10 @@ pub fn parse_launchv(argv: &[&str]) -> Result { } } -pub fn update_registry() -> bool { +pub fn update_registry() -> Result<(), glib::error::BoolError> { assert_initialized_main_thread!(); unsafe { - from_glib(ffi::gst_update_registry()) + glib_result_from_gboolean!(ffi::gst_update_registry(), "Failed to update the registry") } } diff --git a/gstreamer/src/auto/pad.rs b/gstreamer/src/auto/pad.rs index 3dad68f68..004699ecd 100644 --- a/gstreamer/src/auto/pad.rs +++ b/gstreamer/src/auto/pad.rs @@ -125,7 +125,7 @@ pub trait PadExt: 'static { fn link_maybe_ghosting>(&self, sink: &P) -> Result<(), glib::error::BoolError>; #[cfg(any(feature = "v1_10", feature = "dox"))] - fn link_maybe_ghosting_full>(&self, sink: &P, flags: PadLinkCheck) -> bool; + fn link_maybe_ghosting_full>(&self, sink: &P, flags: PadLinkCheck) -> Result<(), glib::error::BoolError>; fn mark_reconfigure(&self); @@ -357,14 +357,14 @@ impl> PadExt for O { #[cfg(any(feature = "v1_10", feature = "dox"))] fn link_maybe_ghosting>(&self, sink: &P) -> Result<(), glib::error::BoolError> { unsafe { - glib_result_from_gboolean!(ffi::gst_pad_link_maybe_ghosting(self.as_ref().to_glib_none().0, sink.as_ref().to_glib_none().0), "Failed to link pad, possibly ghosting") + glib_result_from_gboolean!(ffi::gst_pad_link_maybe_ghosting(self.as_ref().to_glib_none().0, sink.as_ref().to_glib_none().0), "Failed to link pads, possibly ghosting") } } #[cfg(any(feature = "v1_10", feature = "dox"))] - fn link_maybe_ghosting_full>(&self, sink: &P, flags: PadLinkCheck) -> bool { + fn link_maybe_ghosting_full>(&self, sink: &P, flags: PadLinkCheck) -> Result<(), glib::error::BoolError> { unsafe { - from_glib(ffi::gst_pad_link_maybe_ghosting_full(self.as_ref().to_glib_none().0, sink.as_ref().to_glib_none().0, flags.to_glib())) + glib_result_from_gboolean!(ffi::gst_pad_link_maybe_ghosting_full(self.as_ref().to_glib_none().0, sink.as_ref().to_glib_none().0, flags.to_glib()), "Failed to link pads, possibly ghosting") } } diff --git a/gstreamer/src/bin.rs b/gstreamer/src/bin.rs index f79bbfbe7..680e31bac 100644 --- a/gstreamer/src/bin.rs +++ b/gstreamer/src/bin.rs @@ -10,18 +10,28 @@ use Bin; use Element; use glib; +use glib::object::Cast; use glib::object::IsA; -use glib::translate::{from_glib, from_glib_full, FromGlibPtrContainer, ToGlib, ToGlibPtr}; +use glib::signal::connect_raw; +use glib::signal::SignalHandlerId; +use glib::translate::*; use glib::GString; use ffi; +use std::boxed::Box as Box_; +use std::mem::transmute; use std::path; pub trait GstBinExtManual: 'static { fn add_many>(&self, elements: &[&E]) -> Result<(), glib::BoolError>; fn remove_many>(&self, elements: &[&E]) -> Result<(), glib::BoolError>; + fn connect_do_latency Result<(), glib::BoolError> + Send + Sync + 'static>( + &self, + f: F, + ) -> SignalHandlerId; + fn iterate_all_by_interface(&self, iface: glib::types::Type) -> ::Iterator; fn iterate_elements(&self) -> ::Iterator; fn iterate_recurse(&self) -> ::Iterator; @@ -64,7 +74,7 @@ impl> GstBinExtManual for O { e.as_ref().to_glib_none().0, )); if !ret { - return Err(glib_bool_error!("Failed to add elements")); + return Err(glib_bool_error!("Failed to remove elements")); } } } @@ -72,6 +82,22 @@ impl> GstBinExtManual for O { Ok(()) } + fn connect_do_latency Result<(), glib::BoolError> + Send + Sync + 'static>( + &self, + f: F, + ) -> SignalHandlerId { + unsafe { + let f: Box_ Result<(), glib::BoolError> + Send + Sync + 'static>> = + Box_::new(Box_::new(f)); + connect_raw( + self.as_ptr() as *mut _, + b"do-latency\0".as_ptr() as *const _, + transmute(do_latency_trampoline:: as usize), + Box_::into_raw(f) as *mut _, + ) + } + } + fn iterate_all_by_interface(&self, iface: glib::types::Type) -> ::Iterator { unsafe { from_glib_full(ffi::gst_bin_iterate_all_by_interface( @@ -130,6 +156,24 @@ impl> GstBinExtManual for O { } } +unsafe extern "C" fn do_latency_trampoline

( + this: *mut ffi::GstBin, + f: glib_ffi::gpointer, +) -> glib_ffi::gboolean +where + P: IsA, +{ + let f: &&(Fn(&P) -> Result<(), glib::BoolError> + Send + Sync + 'static) = transmute(f); + match f(&Bin::from_glib_borrow(this).unsafe_cast()) { + Ok(()) => true, + Err(err) => { + gst_error!(::CAT_RUST, obj: &Bin::from_glib_borrow(this), "{}", err); + false + } + } + .to_glib() +} + #[cfg(test)] mod tests { use super::*; diff --git a/gstreamer/src/buffer_pool.rs b/gstreamer/src/buffer_pool.rs index e105aa778..f00c611ac 100644 --- a/gstreamer/src/buffer_pool.rs +++ b/gstreamer/src/buffer_pool.rs @@ -131,17 +131,20 @@ impl BufferPoolConfig { size: u32, min_buffers: u32, max_buffers: u32, - ) -> bool { + ) -> Result<(), glib::BoolError> { let caps = caps.into(); unsafe { - from_glib(ffi::gst_buffer_pool_config_validate_params( - self.0.to_glib_none().0, - caps.to_glib_none().0, - size, - min_buffers, - max_buffers, - )) + glib_result_from_gboolean!( + ffi::gst_buffer_pool_config_validate_params( + self.0.to_glib_none().0, + caps.to_glib_none().0, + size, + min_buffers, + max_buffers, + ), + "Parameters are not valid in this context" + ) } } diff --git a/gstreamer/src/ghost_pad.rs b/gstreamer/src/ghost_pad.rs index 8efd98b81..29b51470d 100644 --- a/gstreamer/src/ghost_pad.rs +++ b/gstreamer/src/ghost_pad.rs @@ -58,16 +58,19 @@ impl GhostPad { parent: R, mode: PadMode, active: bool, - ) -> bool { + ) -> Result<(), glib::BoolError> { skip_assert_initialized!(); let parent = parent.into(); unsafe { - from_glib(ffi::gst_ghost_pad_activate_mode_default( - pad.to_glib_none().0 as *mut ffi::GstPad, - parent.map(|p| p.as_ref()).to_glib_none().0, - mode.to_glib(), - active.to_glib(), - )) + glib_result_from_gboolean!( + ffi::gst_ghost_pad_activate_mode_default( + pad.to_glib_none().0 as *mut ffi::GstPad, + parent.map(|p| p.as_ref()).to_glib_none().0, + mode.to_glib(), + active.to_glib(), + ), + "Failed to invoke the default activate mode function of the ghost pad" + ) } } @@ -81,16 +84,22 @@ impl GhostPad { parent: R, mode: PadMode, active: bool, - ) -> bool { + ) -> Result<(), glib::BoolError> { skip_assert_initialized!(); let parent = parent.into(); unsafe { - from_glib(ffi::gst_ghost_pad_internal_activate_mode_default( - pad.to_glib_none().0 as *mut ffi::GstPad, - parent.map(|p| p.as_ref()).to_glib_none().0, - mode.to_glib(), - active.to_glib(), - )) + glib_result_from_gboolean!( + ffi::gst_ghost_pad_internal_activate_mode_default( + pad.to_glib_none().0 as *mut ffi::GstPad, + parent.map(|p| p.as_ref()).to_glib_none().0, + mode.to_glib(), + active.to_glib(), + ), + concat!( + "Failed to invoke the default activate mode function of a proxy pad ", + "that is owned by the ghost pad" + ) + ) } } } diff --git a/gstreamer/src/pad.rs b/gstreamer/src/pad.rs index dcd7305fb..042db7a1a 100644 --- a/gstreamer/src/pad.rs +++ b/gstreamer/src/pad.rs @@ -161,11 +161,14 @@ pub trait PadExtManual: 'static { fn set_activate_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>) -> bool + Send + Sync + 'static; + F: Fn(&Pad, &Option<::Object>) -> Result<(), glib::BoolError> + Send + Sync + 'static; fn set_activatemode_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, ::PadMode, bool) -> bool + Send + Sync + 'static; + F: Fn(&Pad, &Option<::Object>, ::PadMode, bool) -> Result<(), glib::BoolError> + + Send + + Sync + + 'static; fn set_chain_function(&self, func: F) where @@ -510,12 +513,13 @@ impl> PadExtManual for O { fn set_activate_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>) -> bool + Send + Sync + 'static, + F: Fn(&Pad, &Option<::Object>) -> Result<(), glib::BoolError> + Send + Sync + 'static, { #[cfg_attr(feature = "cargo-clippy", allow(type_complexity))] unsafe { - let func_box: Box) -> bool + Send + Sync + 'static> = - Box::new(func); + let func_box: Box< + Fn(&Pad, &Option<::Object>) -> Result<(), glib::BoolError> + Send + Sync + 'static, + > = Box::new(func); ffi::gst_pad_set_activate_function_full( self.as_ref().to_glib_none().0, Some(trampoline_activate_function), @@ -527,12 +531,18 @@ impl> PadExtManual for O { fn set_activatemode_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, ::PadMode, bool) -> bool + Send + Sync + 'static, + F: Fn(&Pad, &Option<::Object>, ::PadMode, bool) -> Result<(), glib::BoolError> + + Send + + Sync + + 'static, { #[cfg_attr(feature = "cargo-clippy", allow(type_complexity))] unsafe { let func_box: Box< - Fn(&Pad, &Option<::Object>, ::PadMode, bool) -> bool + Send + Sync + 'static, + Fn(&Pad, &Option<::Object>, ::PadMode, bool) -> Result<(), glib::BoolError> + + Send + + Sync + + 'static, > = Box::new(func); ffi::gst_pad_set_activatemode_function_full( self.as_ref().to_glib_none().0, @@ -1106,10 +1116,20 @@ unsafe extern "C" fn trampoline_activate_function( parent: *mut ffi::GstObject, ) -> glib_ffi::gboolean { #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let func: &&(Fn(&Pad, &Option<::Object>) -> bool + Send + Sync + 'static) = - transmute((*pad).activatedata); + let func: &&(Fn(&Pad, &Option<::Object>) -> Result<(), glib::BoolError> + + Send + + Sync + + 'static) = transmute((*pad).activatedata); - func(&from_glib_borrow(pad), &from_glib_borrow(parent)).to_glib() + let pad: Pad = from_glib_borrow(pad); + match func(&pad, &from_glib_borrow(parent)) { + Ok(()) => true, + Err(err) => { + gst_error!(::CAT_RUST, obj: &pad, "{}", err); + false + } + } + .to_glib() } unsafe extern "C" fn trampoline_activatemode_function( diff --git a/gstreamer/src/segment.rs b/gstreamer/src/segment.rs index 13e1a936e..c703fb886 100644 --- a/gstreamer/src/segment.rs +++ b/gstreamer/src/segment.rs @@ -171,13 +171,16 @@ impl FormattedSegment { } } - pub fn offset_running_time(&mut self, offset: i64) -> bool { + pub fn offset_running_time(&mut self, offset: i64) -> Result<(), glib::BoolError> { unsafe { - from_glib(ffi::gst_segment_offset_running_time( - &mut self.0, - self.get_format().to_glib(), - offset, - )) + glib_result_from_gboolean!( + ffi::gst_segment_offset_running_time( + &mut self.0, + self.get_format().to_glib(), + offset, + ), + "Offset is not in the segment" + ) } } @@ -257,7 +260,7 @@ impl FormattedSegment { } } - pub fn set_running_time>(&mut self, running_time: V) -> bool { + pub fn set_running_time>(&mut self, running_time: V) -> Result<(), glib::BoolError> { let running_time = running_time.into(); if T::get_default_format() == Format::Undefined { @@ -265,11 +268,14 @@ impl FormattedSegment { } unsafe { - from_glib(ffi::gst_segment_set_running_time( - &mut self.0, - self.get_format().to_glib(), - running_time.to_raw_value() as u64, - )) + glib_result_from_gboolean!( + ffi::gst_segment_set_running_time( + &mut self.0, + self.get_format().to_glib(), + running_time.to_raw_value() as u64, + ), + "Running time is not in the segment" + ) } } diff --git a/gstreamer/src/subclass/bin.rs b/gstreamer/src/subclass/bin.rs index 9ece1a8c2..ae03f0858 100644 --- a/gstreamer/src/subclass/bin.rs +++ b/gstreamer/src/subclass/bin.rs @@ -20,11 +20,11 @@ use Element; use Message; pub trait BinImpl: ElementImpl + Send + Sync + 'static { - fn add_element(&self, bin: &Bin, element: &Element) -> bool { + fn add_element(&self, bin: &Bin, element: &Element) -> Result<(), glib::BoolError> { self.parent_add_element(bin, element) } - fn remove_element(&self, bin: &Bin, element: &Element) -> bool { + fn remove_element(&self, bin: &Bin, element: &Element) -> Result<(), glib::BoolError> { self.parent_remove_element(bin, element) } @@ -32,25 +32,31 @@ pub trait BinImpl: ElementImpl + Send + Sync + 'static { self.parent_handle_message(bin, message) } - fn parent_add_element(&self, bin: &Bin, element: &Element) -> bool { + fn parent_add_element(&self, bin: &Bin, element: &Element) -> Result<(), glib::BoolError> { unsafe { let data = self.get_type_data(); let parent_class = data.as_ref().get_parent_class() as *mut ffi::GstBinClass; - (*parent_class) + let f = (*parent_class) .add_element - .map(|f| from_glib(f(bin.to_glib_none().0, element.to_glib_none().0))) - .unwrap_or(false) + .ok_or_else(|| glib_bool_error!("Parent function `add_element` is not defined"))?; + glib_result_from_gboolean!( + f(bin.to_glib_none().0, element.to_glib_none().0), + "Failed to add the element using the parent function" + ) } } - fn parent_remove_element(&self, bin: &Bin, element: &Element) -> bool { + fn parent_remove_element(&self, bin: &Bin, element: &Element) -> Result<(), glib::BoolError> { unsafe { let data = self.get_type_data(); let parent_class = data.as_ref().get_parent_class() as *mut ffi::GstBinClass; - (*parent_class) - .remove_element - .map(|f| from_glib(f(bin.to_glib_none().0, element.to_glib_none().0))) - .unwrap_or(false) + let f = (*parent_class).remove_element.ok_or_else(|| { + glib_bool_error!("Parent function `remove_element` is not defined") + })?; + glib_result_from_gboolean!( + f(bin.to_glib_none().0, element.to_glib_none().0), + "Failed to remove the element using the parent function" + ) } } @@ -94,7 +100,13 @@ where let wrap: Bin = from_glib_borrow(ptr); gst_panic_to_error!(&wrap, &instance.panicked(), false, { - imp.add_element(&wrap, &from_glib_borrow(element)) + match imp.add_element(&wrap, &from_glib_borrow(element)) { + Ok(()) => true, + Err(err) => { + gst_error!(::CAT_RUST, obj: &wrap, "{}", err); + false + } + } }) .to_glib() } @@ -113,7 +125,13 @@ where let wrap: Bin = from_glib_borrow(ptr); gst_panic_to_error!(&wrap, &instance.panicked(), false, { - imp.remove_element(&wrap, &from_glib_borrow(element)) + match imp.remove_element(&wrap, &from_glib_borrow(element)) { + Ok(()) => true, + Err(err) => { + gst_error!(::CAT_RUST, obj: &wrap, "{}", err); + false + } + } }) .to_glib() } diff --git a/gstreamer/src/subclass/plugin.rs b/gstreamer/src/subclass/plugin.rs index 386919d02..d773ed265 100644 --- a/gstreamer/src/subclass/plugin.rs +++ b/gstreamer/src/subclass/plugin.rs @@ -48,20 +48,23 @@ macro_rules! gst_plugin_define( _gst_reserved: [0 as $crate::glib_ffi::gpointer; 4], }); - pub fn plugin_register_static() -> bool { + pub fn plugin_register_static() -> Result<(), glib::BoolError> { unsafe { - from_glib($crate::ffi::gst_plugin_register_static( - $crate::subclass::plugin::MAJOR_VERSION, - $crate::subclass::plugin::MINOR_VERSION, - concat!($name, "\0") as *const str as *const _, - concat!($description, "\0") as *const str as _, - Some(plugin_init_trampoline), - concat!($version, "\0") as *const str as *const _, - concat!($license, "\0") as *const str as *const _, - concat!($source, "\0") as *const str as *const _, - concat!($package, "\0") as *const str as *const _, - concat!($origin, "\0") as *const str as *const _, - )) + glib_result_from_gboolean!( + $crate::ffi::gst_plugin_register_static( + $crate::subclass::plugin::MAJOR_VERSION, + $crate::subclass::plugin::MINOR_VERSION, + concat!($name, "\0") as *const str as *const _, + concat!($description, "\0") as *const str as _, + Some(plugin_init_trampoline), + concat!($version, "\0") as *const str as *const _, + concat!($license, "\0") as *const str as *const _, + concat!($source, "\0") as *const str as *const _, + concat!($package, "\0") as *const str as *const _, + concat!($origin, "\0") as *const str as *const _, + ), + "Failed to register the plugin" + ) } } diff --git a/tutorials/src/bin/basic-tutorial-12.rs b/tutorials/src/bin/basic-tutorial-12.rs index 13e141575..c35952140 100644 --- a/tutorials/src/bin/basic-tutorial-12.rs +++ b/tutorials/src/bin/basic-tutorial-12.rs @@ -80,8 +80,8 @@ fn tutorial_main() -> Result<(), Error> { main_loop.run(); - bus.remove_watch(); - let _ = pipeline.set_state(gst::State::Null); + bus.remove_watch()?; + pipeline.set_state(gst::State::Null)?; Ok(()) }