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
// Take a look at the license at the top of the repository in the LICENSE file.

use std::ptr;

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

use crate::{ffi, AudioAggregator, AudioAggregatorPad};

pub trait AudioAggregatorImpl: AudioAggregatorImplExt + AggregatorImpl {
    /// Create a new output buffer contains num_frames frames.
    fn create_output_buffer(&self, num_frames: u32) -> Option<gst::Buffer> {
        self.parent_create_output_buffer(num_frames)
    }

    /// Aggregates one input buffer to the output
    ///  buffer. The in_offset and out_offset are in "frames", which is
    ///  the size of a sample times the number of channels. Returns TRUE if
    ///  any non-silence was added to the buffer
    #[allow(clippy::too_many_arguments)]
    fn aggregate_one_buffer(
        &self,
        pad: &AudioAggregatorPad,
        inbuf: &gst::BufferRef,
        in_offset: u32,
        outbuf: &mut gst::BufferRef,
        out_offset: u32,
        num_frames: u32,
    ) -> bool {
        self.parent_aggregate_one_buffer(pad, inbuf, in_offset, outbuf, out_offset, num_frames)
    }
}

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

pub trait AudioAggregatorImplExt: sealed::Sealed + ObjectSubclass {
    fn parent_create_output_buffer(&self, num_frames: u32) -> Option<gst::Buffer> {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioAggregatorClass;
            let f = (*parent_class)
                .create_output_buffer
                .expect("Missing parent function `create_output_buffer`");

            from_glib_full(f(
                self.obj()
                    .unsafe_cast_ref::<AudioAggregator>()
                    .to_glib_none()
                    .0,
                num_frames,
            ))
        }
    }

    fn parent_aggregate_one_buffer(
        &self,
        pad: &AudioAggregatorPad,
        inbuf: &gst::BufferRef,
        in_offset: u32,
        outbuf: &mut gst::BufferRef,
        out_offset: u32,
        num_frames: u32,
    ) -> bool {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioAggregatorClass;
            let f = (*parent_class)
                .aggregate_one_buffer
                .expect("Missing parent function `aggregate_one_buffer`");

            from_glib(f(
                self.obj()
                    .unsafe_cast_ref::<AudioAggregator>()
                    .to_glib_none()
                    .0,
                pad.to_glib_none().0,
                inbuf.as_mut_ptr(),
                in_offset,
                outbuf.as_mut_ptr(),
                out_offset,
                num_frames,
            ))
        }
    }
}

impl<T: AudioAggregatorImpl> AudioAggregatorImplExt for T {}

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

        let klass = klass.as_mut();
        klass.create_output_buffer = Some(audio_aggregator_create_output_buffer::<T>);
        klass.aggregate_one_buffer = Some(audio_aggregator_aggregate_one_buffer::<T>);
    }
}

unsafe extern "C" fn audio_aggregator_create_output_buffer<T: AudioAggregatorImpl>(
    ptr: *mut ffi::GstAudioAggregator,
    num_frames: u32,
) -> *mut gst::ffi::GstBuffer {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    gst::panic_to_error!(imp, None, { imp.create_output_buffer(num_frames) })
        .map(|buffer| buffer.into_glib_ptr())
        .unwrap_or(ptr::null_mut())
}

unsafe extern "C" fn audio_aggregator_aggregate_one_buffer<T: AudioAggregatorImpl>(
    ptr: *mut ffi::GstAudioAggregator,
    pad: *mut ffi::GstAudioAggregatorPad,
    inbuf: *mut gst::ffi::GstBuffer,
    in_offset: u32,
    outbuf: *mut gst::ffi::GstBuffer,
    out_offset: u32,
    num_frames: u32,
) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    gst::panic_to_error!(imp, true, {
        imp.aggregate_one_buffer(
            &from_glib_borrow(pad),
            gst::BufferRef::from_ptr(inbuf),
            in_offset,
            gst::BufferRef::from_mut_ptr(outbuf),
            out_offset,
            num_frames,
        )
    })
    .into_glib()
}