1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Take a look at the license at the top of the repository in the LICENSE file.

use std::{mem, ptr};

use glib::translate::*;
use gst_base::{prelude::*, subclass::prelude::*};

use crate::{ffi, subclass::AggregateFramesToken, VideoAggregator, VideoAggregatorPad};

pub trait VideoAggregatorPadImpl: VideoAggregatorPadImplExt + AggregatorPadImpl {
    /// Called when either the input or output formats
    ///  have changed.
    fn update_conversion_info(&self) {
        self.parent_update_conversion_info()
    }

    /// Prepare the frame from the pad buffer and sets it to prepared_frame.
    ///  Implementations should always return TRUE. Returning FALSE will cease
    ///  iteration over subsequent pads.
    fn prepare_frame(
        &self,
        aggregator: &crate::VideoAggregator,
        token: &AggregateFramesToken,
        buffer: &gst::Buffer,
    ) -> Option<crate::VideoFrame<crate::video_frame::Readable>> {
        self.parent_prepare_frame(aggregator, token, buffer)
    }

    /// clean the frame previously prepared in prepare_frame
    fn clean_frame(
        &self,
        aggregator: &crate::VideoAggregator,
        token: &AggregateFramesToken,
        frame: Option<crate::VideoFrame<crate::video_frame::Readable>>,
    ) {
        self.parent_clean_frame(aggregator, token, frame)
    }
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::VideoAggregatorPadImplExt> Sealed for T {}
}

pub trait VideoAggregatorPadImplExt: ObjectSubclass + sealed::Sealed {
    fn parent_update_conversion_info(&self) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstVideoAggregatorPadClass;
            if let Some(f) = (*parent_class).update_conversion_info {
                f(self
                    .obj()
                    .unsafe_cast_ref::<VideoAggregatorPad>()
                    .to_glib_none()
                    .0);
            }
        }
    }

    fn parent_prepare_frame(
        &self,
        aggregator: &crate::VideoAggregator,
        token: &AggregateFramesToken,
        buffer: &gst::Buffer,
    ) -> Option<crate::VideoFrame<crate::video_frame::Readable>> {
        assert_eq!(aggregator.as_ptr(), token.0.as_ptr());

        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstVideoAggregatorPadClass;
            if let Some(f) = (*parent_class).prepare_frame {
                let mut prepared_frame = mem::MaybeUninit::zeroed();

                f(
                    self.obj()
                        .unsafe_cast_ref::<VideoAggregatorPad>()
                        .to_glib_none()
                        .0,
                    aggregator.to_glib_none().0,
                    buffer.as_mut_ptr(),
                    prepared_frame.as_mut_ptr(),
                );

                let prepared_frame = prepared_frame.assume_init();
                if prepared_frame.buffer.is_null() {
                    None
                } else {
                    Some(crate::VideoFrame::from_glib_full(prepared_frame))
                }
            } else {
                None
            }
        }
    }

    fn parent_clean_frame(
        &self,
        aggregator: &crate::VideoAggregator,
        token: &AggregateFramesToken,
        frame: Option<crate::VideoFrame<crate::video_frame::Readable>>,
    ) {
        assert_eq!(aggregator.as_ptr(), token.0.as_ptr());

        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstVideoAggregatorPadClass;
            if let Some(f) = (*parent_class).clean_frame {
                let mut prepared_frame = if let Some(frame) = frame {
                    frame.into_raw()
                } else {
                    mem::zeroed()
                };

                f(
                    self.obj()
                        .unsafe_cast_ref::<VideoAggregatorPad>()
                        .to_glib_none()
                        .0,
                    aggregator.to_glib_none().0,
                    &mut prepared_frame,
                );
            }
        }
    }
}

impl<T: VideoAggregatorPadImpl> VideoAggregatorPadImplExt for T {}

unsafe impl<T: VideoAggregatorPadImpl> IsSubclassable<T> for VideoAggregatorPad {
    fn class_init(klass: &mut glib::Class<Self>) {
        Self::parent_class_init::<T>(klass);

        let klass = klass.as_mut();
        klass.update_conversion_info = Some(video_aggregator_pad_update_conversion_info::<T>);
        klass.prepare_frame = Some(video_aggregator_pad_prepare_frame::<T>);
        klass.clean_frame = Some(video_aggregator_pad_clean_frame::<T>);
    }
}

unsafe extern "C" fn video_aggregator_pad_update_conversion_info<T: VideoAggregatorPadImpl>(
    ptr: *mut ffi::GstVideoAggregatorPad,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.update_conversion_info();
}

unsafe extern "C" fn video_aggregator_pad_prepare_frame<T: VideoAggregatorPadImpl>(
    ptr: *mut ffi::GstVideoAggregatorPad,
    aggregator: *mut ffi::GstVideoAggregator,
    buffer: *mut gst::ffi::GstBuffer,
    prepared_frame: *mut ffi::GstVideoFrame,
) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();
    let aggregator: Borrowed<VideoAggregator> = from_glib_borrow(aggregator);

    let token = AggregateFramesToken(&aggregator);

    match imp.prepare_frame(&aggregator, &token, &from_glib_borrow(buffer)) {
        Some(frame) => {
            *prepared_frame = frame.into_raw();
        }
        None => {
            ptr::write(prepared_frame, mem::zeroed());
        }
    }

    glib::ffi::GTRUE
}

unsafe extern "C" fn video_aggregator_pad_clean_frame<T: VideoAggregatorPadImpl>(
    ptr: *mut ffi::GstVideoAggregatorPad,
    aggregator: *mut ffi::GstVideoAggregator,
    prepared_frame: *mut ffi::GstVideoFrame,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();
    let aggregator: Borrowed<VideoAggregator> = from_glib_borrow(aggregator);

    let token = AggregateFramesToken(&aggregator);

    let frame = if (*prepared_frame).buffer.is_null() {
        None
    } else {
        let frame = crate::VideoFrame::from_glib_full(*prepared_frame);
        ptr::write(prepared_frame, mem::zeroed());
        Some(frame)
    };

    imp.clean_frame(&aggregator, &token, frame);
}