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: AggregatorImpl + ObjectSubclass<Type: IsA<AudioAggregator>> {
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
34pub trait AudioAggregatorImplExt: AudioAggregatorImpl {
35    fn parent_create_output_buffer(&self, num_frames: u32) -> Option<gst::Buffer> {
36        unsafe {
37            let data = Self::type_data();
38            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioAggregatorClass;
39            let f = (*parent_class)
40                .create_output_buffer
41                .expect("Missing parent function `create_output_buffer`");
42
43            from_glib_full(f(
44                self.obj()
45                    .unsafe_cast_ref::<AudioAggregator>()
46                    .to_glib_none()
47                    .0,
48                num_frames,
49            ))
50        }
51    }
52
53    fn parent_aggregate_one_buffer(
54        &self,
55        pad: &AudioAggregatorPad,
56        inbuf: &gst::BufferRef,
57        in_offset: u32,
58        outbuf: &mut gst::BufferRef,
59        out_offset: u32,
60        num_frames: u32,
61    ) -> bool {
62        unsafe {
63            let data = Self::type_data();
64            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioAggregatorClass;
65            let f = (*parent_class)
66                .aggregate_one_buffer
67                .expect("Missing parent function `aggregate_one_buffer`");
68
69            from_glib(f(
70                self.obj()
71                    .unsafe_cast_ref::<AudioAggregator>()
72                    .to_glib_none()
73                    .0,
74                pad.to_glib_none().0,
75                inbuf.as_mut_ptr(),
76                in_offset,
77                outbuf.as_mut_ptr(),
78                out_offset,
79                num_frames,
80            ))
81        }
82    }
83}
84
85impl<T: AudioAggregatorImpl> AudioAggregatorImplExt for T {}
86
87unsafe impl<T: AudioAggregatorImpl> IsSubclassable<T> for AudioAggregator {
88    fn class_init(klass: &mut glib::Class<Self>) {
89        Self::parent_class_init::<T>(klass);
90
91        let klass = klass.as_mut();
92        klass.create_output_buffer = Some(audio_aggregator_create_output_buffer::<T>);
93        klass.aggregate_one_buffer = Some(audio_aggregator_aggregate_one_buffer::<T>);
94    }
95}
96
97unsafe extern "C" fn audio_aggregator_create_output_buffer<T: AudioAggregatorImpl>(
98    ptr: *mut ffi::GstAudioAggregator,
99    num_frames: u32,
100) -> *mut gst::ffi::GstBuffer {
101    let instance = &*(ptr as *mut T::Instance);
102    let imp = instance.imp();
103
104    gst::panic_to_error!(imp, None, { imp.create_output_buffer(num_frames) })
105        .map(|buffer| buffer.into_glib_ptr())
106        .unwrap_or(ptr::null_mut())
107}
108
109unsafe extern "C" fn audio_aggregator_aggregate_one_buffer<T: AudioAggregatorImpl>(
110    ptr: *mut ffi::GstAudioAggregator,
111    pad: *mut ffi::GstAudioAggregatorPad,
112    inbuf: *mut gst::ffi::GstBuffer,
113    in_offset: u32,
114    outbuf: *mut gst::ffi::GstBuffer,
115    out_offset: u32,
116    num_frames: u32,
117) -> glib::ffi::gboolean {
118    let instance = &*(ptr as *mut T::Instance);
119    let imp = instance.imp();
120
121    gst::panic_to_error!(imp, true, {
122        imp.aggregate_one_buffer(
123            &from_glib_borrow(pad),
124            gst::BufferRef::from_ptr(inbuf),
125            in_offset,
126            gst::BufferRef::from_mut_ptr(outbuf),
127            out_offset,
128            num_frames,
129        )
130    })
131    .into_glib()
132}