gstreamer_audio/subclass/
audio_aggregator.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::ptr;
4
5use glib::translate::*;
6use gst_base::{prelude::*, subclass::prelude::*};
7
8use crate::{ffi, AudioAggregator, AudioAggregatorPad};
9
10pub trait AudioAggregatorImpl: AudioAggregatorImplExt + AggregatorImpl {
11    /// Create a new output buffer contains num_frames frames.
12    fn create_output_buffer(&self, num_frames: u32) -> Option<gst::Buffer> {
13        self.parent_create_output_buffer(num_frames)
14    }
15
16    /// Aggregates one input buffer to the output
17    ///  buffer. The in_offset and out_offset are in "frames", which is
18    ///  the size of a sample times the number of channels. Returns TRUE if
19    ///  any non-silence was added to the buffer
20    #[allow(clippy::too_many_arguments)]
21    fn aggregate_one_buffer(
22        &self,
23        pad: &AudioAggregatorPad,
24        inbuf: &gst::BufferRef,
25        in_offset: u32,
26        outbuf: &mut gst::BufferRef,
27        out_offset: u32,
28        num_frames: u32,
29    ) -> bool {
30        self.parent_aggregate_one_buffer(pad, inbuf, in_offset, outbuf, out_offset, num_frames)
31    }
32}
33
34mod sealed {
35    pub trait Sealed {}
36    impl<T: super::AudioAggregatorImplExt> Sealed for T {}
37}
38
39pub trait AudioAggregatorImplExt: sealed::Sealed + ObjectSubclass {
40    fn parent_create_output_buffer(&self, num_frames: u32) -> Option<gst::Buffer> {
41        unsafe {
42            let data = Self::type_data();
43            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioAggregatorClass;
44            let f = (*parent_class)
45                .create_output_buffer
46                .expect("Missing parent function `create_output_buffer`");
47
48            from_glib_full(f(
49                self.obj()
50                    .unsafe_cast_ref::<AudioAggregator>()
51                    .to_glib_none()
52                    .0,
53                num_frames,
54            ))
55        }
56    }
57
58    fn parent_aggregate_one_buffer(
59        &self,
60        pad: &AudioAggregatorPad,
61        inbuf: &gst::BufferRef,
62        in_offset: u32,
63        outbuf: &mut gst::BufferRef,
64        out_offset: u32,
65        num_frames: u32,
66    ) -> bool {
67        unsafe {
68            let data = Self::type_data();
69            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioAggregatorClass;
70            let f = (*parent_class)
71                .aggregate_one_buffer
72                .expect("Missing parent function `aggregate_one_buffer`");
73
74            from_glib(f(
75                self.obj()
76                    .unsafe_cast_ref::<AudioAggregator>()
77                    .to_glib_none()
78                    .0,
79                pad.to_glib_none().0,
80                inbuf.as_mut_ptr(),
81                in_offset,
82                outbuf.as_mut_ptr(),
83                out_offset,
84                num_frames,
85            ))
86        }
87    }
88}
89
90impl<T: AudioAggregatorImpl> AudioAggregatorImplExt for T {}
91
92unsafe impl<T: AudioAggregatorImpl> IsSubclassable<T> for AudioAggregator {
93    fn class_init(klass: &mut glib::Class<Self>) {
94        Self::parent_class_init::<T>(klass);
95
96        let klass = klass.as_mut();
97        klass.create_output_buffer = Some(audio_aggregator_create_output_buffer::<T>);
98        klass.aggregate_one_buffer = Some(audio_aggregator_aggregate_one_buffer::<T>);
99    }
100}
101
102unsafe extern "C" fn audio_aggregator_create_output_buffer<T: AudioAggregatorImpl>(
103    ptr: *mut ffi::GstAudioAggregator,
104    num_frames: u32,
105) -> *mut gst::ffi::GstBuffer {
106    let instance = &*(ptr as *mut T::Instance);
107    let imp = instance.imp();
108
109    gst::panic_to_error!(imp, None, { imp.create_output_buffer(num_frames) })
110        .map(|buffer| buffer.into_glib_ptr())
111        .unwrap_or(ptr::null_mut())
112}
113
114unsafe extern "C" fn audio_aggregator_aggregate_one_buffer<T: AudioAggregatorImpl>(
115    ptr: *mut ffi::GstAudioAggregator,
116    pad: *mut ffi::GstAudioAggregatorPad,
117    inbuf: *mut gst::ffi::GstBuffer,
118    in_offset: u32,
119    outbuf: *mut gst::ffi::GstBuffer,
120    out_offset: u32,
121    num_frames: u32,
122) -> glib::ffi::gboolean {
123    let instance = &*(ptr as *mut T::Instance);
124    let imp = instance.imp();
125
126    gst::panic_to_error!(imp, true, {
127        imp.aggregate_one_buffer(
128            &from_glib_borrow(pad),
129            gst::BufferRef::from_ptr(inbuf),
130            in_offset,
131            gst::BufferRef::from_mut_ptr(outbuf),
132            out_offset,
133            num_frames,
134        )
135    })
136    .into_glib()
137}