gstreamer_video/subclass/
video_sink.rs

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