From 09212add47633f089bc9885e53618717a82f0ce2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Sun, 9 Feb 2020 20:45:29 +0200 Subject: [PATCH] base/base_transform: Add before_transform/copy_metadata/transform_meta support for subclasses --- gstreamer-base/src/subclass/base_transform.rs | 187 ++++++++++++++++++ 1 file changed, 187 insertions(+) diff --git a/gstreamer-base/src/subclass/base_transform.rs b/gstreamer-base/src/subclass/base_transform.rs index 9e1d0dacb..a5a5facf9 100644 --- a/gstreamer-base/src/subclass/base_transform.rs +++ b/gstreamer-base/src/subclass/base_transform.rs @@ -127,6 +127,29 @@ pub trait BaseTransformImpl: BaseTransformImplExt + ElementImpl + Send + Sync + self.parent_transform_ip_passthrough(element, buf) } + fn copy_metadata( + &self, + element: &BaseTransform, + inbuf: &gst::BufferRef, + outbuf: &mut gst::BufferRef, + ) -> Result<(), gst::LoggableError> { + self.parent_copy_metadata(element, inbuf, outbuf) + } + + fn transform_meta<'a>( + &self, + element: &BaseTransform, + outbuf: &mut gst::BufferRef, + meta: gst::MetaRef<'a, gst::Meta>, + inbuf: &'a gst::BufferRef, + ) -> bool { + self.parent_transform_meta(element, outbuf, meta, inbuf) + } + + fn before_transform(&self, element: &BaseTransform, inbuf: &gst::BufferRef) { + self.parent_before_transform(element, inbuf); + } + fn submit_input_buffer( &self, element: &BaseTransform, @@ -217,6 +240,23 @@ pub trait BaseTransformImplExt { buf: &gst::Buffer, ) -> Result; + fn parent_copy_metadata( + &self, + element: &BaseTransform, + inbuf: &gst::BufferRef, + outbuf: &mut gst::BufferRef, + ) -> Result<(), gst::LoggableError>; + + fn parent_transform_meta<'a>( + &self, + element: &BaseTransform, + outbuf: &mut gst::BufferRef, + meta: gst::MetaRef<'a, gst::Meta>, + inbuf: &'a gst::BufferRef, + ) -> bool; + + fn parent_before_transform(&self, element: &BaseTransform, inbuf: &gst::BufferRef); + fn parent_submit_input_buffer( &self, element: &BaseTransform, @@ -584,6 +624,68 @@ impl BaseTransformImplExt for T { } } + fn parent_copy_metadata( + &self, + element: &BaseTransform, + inbuf: &gst::BufferRef, + outbuf: &mut gst::BufferRef, + ) -> Result<(), gst::LoggableError> { + unsafe { + let data = self.get_type_data(); + let parent_class = + data.as_ref().get_parent_class() as *mut gst_base_sys::GstBaseTransformClass; + if let Some(ref f) = (*parent_class).copy_metadata { + gst_result_from_gboolean!( + f( + element.to_glib_none().0, + inbuf.as_ptr() as *mut _, + outbuf.as_mut_ptr() + ), + gst::CAT_RUST, + "Parent function `copy_metadata` failed" + ) + } else { + Ok(()) + } + } + } + + fn parent_transform_meta<'a>( + &self, + element: &BaseTransform, + outbuf: &mut gst::BufferRef, + meta: gst::MetaRef<'a, gst::Meta>, + inbuf: &'a gst::BufferRef, + ) -> bool { + unsafe { + let data = self.get_type_data(); + let parent_class = + data.as_ref().get_parent_class() as *mut gst_base_sys::GstBaseTransformClass; + (*parent_class) + .transform_meta + .map(|f| { + from_glib(f( + element.to_glib_none().0, + outbuf.as_mut_ptr(), + meta.as_ptr() as *mut _, + inbuf.as_ptr() as *mut _, + )) + }) + .unwrap_or(false) + } + } + + fn parent_before_transform(&self, element: &BaseTransform, inbuf: &gst::BufferRef) { + unsafe { + let data = self.get_type_data(); + let parent_class = + data.as_ref().get_parent_class() as *mut gst_base_sys::GstBaseTransformClass; + (*parent_class) + .before_transform + .map(|f| f(element.to_glib_none().0, inbuf.as_ptr() as *mut _)); + } + } + fn parent_submit_input_buffer( &self, element: &BaseTransform, @@ -692,6 +794,9 @@ where klass.get_unit_size = Some(base_transform_get_unit_size::); klass.sink_event = Some(base_transform_sink_event::); klass.src_event = Some(base_transform_src_event::); + klass.transform_meta = Some(base_transform_transform_meta::); + klass.copy_metadata = Some(base_transform_copy_metadata::); + klass.before_transform = Some(base_transform_before_transform::); klass.submit_input_buffer = Some(base_transform_submit_input_buffer::); klass.generate_output = Some(base_transform_generate_output::); } @@ -1061,6 +1166,88 @@ where .to_glib() } +unsafe extern "C" fn base_transform_transform_meta( + ptr: *mut gst_base_sys::GstBaseTransform, + outbuf: *mut gst_sys::GstBuffer, + meta: *mut gst_sys::GstMeta, + inbuf: *mut gst_sys::GstBuffer, +) -> glib_sys::gboolean +where + T: BaseTransformImpl, + T::Instance: PanicPoison, +{ + let instance = &*(ptr as *mut T::Instance); + let imp = instance.get_impl(); + let wrap: BaseTransform = from_glib_borrow(ptr); + + let inbuf = gst::BufferRef::from_ptr(inbuf); + + gst_panic_to_error!(&wrap, &instance.panicked(), false, { + imp.transform_meta( + &wrap, + gst::BufferRef::from_mut_ptr(outbuf), + gst::Meta::from_ptr(inbuf, meta), + inbuf, + ) + }) + .to_glib() +} + +unsafe extern "C" fn base_transform_copy_metadata( + ptr: *mut gst_base_sys::GstBaseTransform, + inbuf: *mut gst_sys::GstBuffer, + outbuf: *mut gst_sys::GstBuffer, +) -> glib_sys::gboolean +where + T: BaseTransformImpl, + T::Instance: PanicPoison, +{ + let instance = &*(ptr as *mut T::Instance); + let imp = instance.get_impl(); + let wrap: BaseTransform = from_glib_borrow(ptr); + + if gst_sys::gst_mini_object_is_writable(outbuf as *mut _) == glib_sys::GFALSE { + gst_warning!( + gst::CAT_RUST, + obj: &wrap, + "buffer {:?} not writable", + outbuf + ); + return glib_sys::GFALSE; + } + + gst_panic_to_error!(&wrap, &instance.panicked(), true, { + match imp.copy_metadata( + &wrap, + gst::BufferRef::from_ptr(inbuf), + gst::BufferRef::from_mut_ptr(outbuf), + ) { + Ok(_) => true, + Err(err) => { + err.log_with_object(&wrap); + false + } + } + }) + .to_glib() +} + +unsafe extern "C" fn base_transform_before_transform( + ptr: *mut gst_base_sys::GstBaseTransform, + inbuf: *mut gst_sys::GstBuffer, +) where + T: BaseTransformImpl, + T::Instance: PanicPoison, +{ + let instance = &*(ptr as *mut T::Instance); + let imp = instance.get_impl(); + let wrap: BaseTransform = from_glib_borrow(ptr); + + gst_panic_to_error!(&wrap, &instance.panicked(), (), { + imp.before_transform(&wrap, gst::BufferRef::from_ptr(inbuf)); + }) +} + unsafe extern "C" fn base_transform_submit_input_buffer( ptr: *mut gst_base_sys::GstBaseTransform, is_discont: glib_sys::gboolean,