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: BaseSinkImpl + ObjectSubclass<Type: IsA<VideoSink>> {
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
18pub trait VideoSinkImplExt: VideoSinkImpl {
19    fn parent_show_frame(&self, buffer: &gst::Buffer) -> Result<gst::FlowSuccess, gst::FlowError> {
20        unsafe {
21            let data = Self::type_data();
22            let parent_class = data.as_ref().parent_class() as *mut ffi::GstVideoSinkClass;
23            (*parent_class)
24                .show_frame
25                .map(|f| {
26                    try_from_glib(f(
27                        self.obj().unsafe_cast_ref::<VideoSink>().to_glib_none().0,
28                        buffer.to_glib_none().0,
29                    ))
30                })
31                .unwrap_or(Err(gst::FlowError::Error))
32        }
33    }
34}
35
36impl<T: VideoSinkImpl> VideoSinkImplExt for T {}
37
38unsafe impl<T: VideoSinkImpl> IsSubclassable<T> for VideoSink {
39    fn class_init(klass: &mut glib::Class<Self>) {
40        Self::parent_class_init::<T>(klass);
41        let klass = klass.as_mut();
42        klass.show_frame = Some(video_sink_show_frame::<T>);
43    }
44}
45
46unsafe extern "C" fn video_sink_show_frame<T: VideoSinkImpl>(
47    ptr: *mut ffi::GstVideoSink,
48    buffer: *mut gst::ffi::GstBuffer,
49) -> gst::ffi::GstFlowReturn {
50    let instance = &*(ptr as *mut T::Instance);
51    let imp = instance.imp();
52    let buffer = from_glib_borrow(buffer);
53
54    gst::panic_to_error!(imp, gst::FlowReturn::Error, {
55        imp.show_frame(&buffer).into()
56    })
57    .into_glib()
58}