| 1 | // Take a look at the license at the top of the repository in the LICENSE file. |
| 2 | |
| 3 | use glib::{prelude::*, translate::*}; |
| 4 | use gst_base::subclass::prelude::*; |
| 5 | |
| 6 | use crate::{ffi, VideoSink}; |
| 7 | |
| 8 | pub trait VideoSinkImpl: VideoSinkImplExt + BaseSinkImpl + ElementImpl { |
| 9 | fn show_frame(&self, buffer: &gst::Buffer) -> Result<gst::FlowSuccess, gst::FlowError> { |
| 10 | self.parent_show_frame(buffer) |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | mod sealed { |
| 15 | pub trait Sealed {} |
| 16 | impl<T: super::VideoSinkImplExt> Sealed for T {} |
| 17 | } |
| 18 | |
| 19 | pub trait VideoSinkImplExt: sealed::Sealed + ObjectSubclass { |
| 20 | fn parent_show_frame(&self, buffer: &gst::Buffer) -> Result<gst::FlowSuccess, gst::FlowError> { |
| 21 | unsafe { |
| 22 | let data: NonNull = Self::type_data(); |
| 23 | let parent_class: *mut GstVideoSinkClass = data.as_ref().parent_class() as *mut ffi::GstVideoSinkClass; |
| 24 | (*parent_class) |
| 25 | .show_frame |
| 26 | .map(|f| { |
| 27 | try_from_glib(f( |
| 28 | self.obj().unsafe_cast_ref::<VideoSink>().to_glib_none().0, |
| 29 | buffer.to_glib_none().0, |
| 30 | )) |
| 31 | }) |
| 32 | .unwrap_or(default:Err(gst::FlowError::Error)) |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl<T: VideoSinkImpl> VideoSinkImplExt for T {} |
| 38 | |
| 39 | unsafe impl<T: VideoSinkImpl> IsSubclassable<T> for VideoSink { |
| 40 | fn class_init(klass: &mut glib::Class<Self>) { |
| 41 | Self::parent_class_init::<T>(class:klass); |
| 42 | let klass: &mut GstVideoSinkClass = klass.as_mut(); |
| 43 | klass.show_frame = Some(video_sink_show_frame::<T>); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | unsafe extern "C" fn video_sink_show_frame<T: VideoSinkImpl>( |
| 48 | ptr: *mut ffi::GstVideoSink, |
| 49 | buffer: *mut gst::ffi::GstBuffer, |
| 50 | ) -> gst::ffi::GstFlowReturn { |
| 51 | let instance: &::Instance = &*(ptr as *mut T::Instance); |
| 52 | let imp: &T = instance.imp(); |
| 53 | let buffer: Borrowed = from_glib_borrow(ptr:buffer); |
| 54 | |
| 55 | gstFlowReturn::panic_to_error!(imp, gst::FlowReturn::Error, { |
| 56 | imp.show_frame(&buffer).into() |
| 57 | }) |
| 58 | .into_glib() |
| 59 | } |
| 60 | |