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
194
195
196
197
198
199
200
201
202
203
204
// Take a look at the license at the top of the repository in the LICENSE file.

use std::ptr;

use glib::translate::*;
use gst::subclass::prelude::*;

use crate::{ffi, prelude::*, RTPBaseDepayload};

pub trait RTPBaseDepayloadImpl: RTPBaseDepayloadImplExt + ElementImpl {
    /// configure the depayloader
    fn set_caps(&self, caps: &gst::Caps) -> Result<(), gst::LoggableError> {
        self.parent_set_caps(caps)
    }

    /// custom event handling
    fn handle_event(&self, event: gst::Event) -> bool {
        self.parent_handle_event(event)
    }

    /// signal the depayloader about packet loss
    fn packet_lost(&self, event: &gst::EventRef) -> bool {
        self.parent_packet_lost(event)
    }

    /// Same as the process virtual function, but slightly more
    /// efficient, since it is passed the rtp buffer structure that has already
    /// been mapped (with GST_MAP_READ) by the base class and thus does not have
    /// to be mapped again by the subclass. Can be used by the subclass to process
    /// incoming rtp packets. If the subclass returns a buffer without a valid
    /// timestamp, the timestamp of the input buffer will be applied to the result
    /// buffer and the output buffer will be pushed out. If this function returns
    /// [`None`], nothing is pushed out. Since: 1.6.
    fn process_rtp_packet(
        &self,
        rtp_buffer: &crate::RTPBuffer<crate::rtp_buffer::Readable>,
    ) -> Option<gst::Buffer> {
        self.parent_process_rtp_packet(rtp_buffer)
    }
}

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

pub trait RTPBaseDepayloadImplExt: sealed::Sealed + ObjectSubclass {
    fn parent_set_caps(&self, caps: &gst::Caps) -> Result<(), gst::LoggableError> {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstRTPBaseDepayloadClass;
            (*parent_class)
                .set_caps
                .map(|f| {
                    gst::result_from_gboolean!(
                        f(
                            self.obj()
                                .unsafe_cast_ref::<RTPBaseDepayload>()
                                .to_glib_none()
                                .0,
                            caps.to_glib_none().0
                        ),
                        gst::CAT_RUST,
                        "Parent function `set_caps` failed"
                    )
                })
                .unwrap_or(Ok(()))
        }
    }

    fn parent_handle_event(&self, event: gst::Event) -> bool {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstRTPBaseDepayloadClass;
            (*parent_class)
                .handle_event
                .map(|f| {
                    from_glib(f(
                        self.obj()
                            .unsafe_cast_ref::<RTPBaseDepayload>()
                            .to_glib_none()
                            .0,
                        event.into_glib_ptr(),
                    ))
                })
                .unwrap_or(false)
        }
    }

    fn parent_packet_lost(&self, event: &gst::EventRef) -> bool {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstRTPBaseDepayloadClass;
            (*parent_class)
                .packet_lost
                .map(|f| {
                    from_glib(f(
                        self.obj()
                            .unsafe_cast_ref::<RTPBaseDepayload>()
                            .to_glib_none()
                            .0,
                        event.as_mut_ptr(),
                    ))
                })
                .unwrap_or(true)
        }
    }

    fn parent_process_rtp_packet(
        &self,
        rtp_buffer: &crate::RTPBuffer<crate::rtp_buffer::Readable>,
    ) -> Option<gst::Buffer> {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstRTPBaseDepayloadClass;

            let f = (*parent_class)
                .process_rtp_packet
                .expect("no parent \"process\" implementation");

            from_glib_full(f(
                self.obj()
                    .unsafe_cast_ref::<crate::RTPBaseDepayload>()
                    .to_glib_none()
                    .0,
                mut_override(rtp_buffer.as_ptr()),
            ))
        }
    }
}

impl<T: RTPBaseDepayloadImpl> RTPBaseDepayloadImplExt for T {}

unsafe impl<T: RTPBaseDepayloadImpl> IsSubclassable<T> for RTPBaseDepayload {
    fn class_init(klass: &mut glib::Class<Self>) {
        Self::parent_class_init::<T>(klass);
        let klass = klass.as_mut();

        klass.process = None;
        klass.process_rtp_packet = Some(rtp_base_depayload_process_rtp_packet::<T>);
        klass.set_caps = Some(rtp_base_depayload_set_caps::<T>);
        klass.handle_event = Some(rtp_base_depayload_handle_event::<T>);
        klass.packet_lost = Some(rtp_base_depayload_packet_lost::<T>);
    }
}

unsafe extern "C" fn rtp_base_depayload_set_caps<T: RTPBaseDepayloadImpl>(
    ptr: *mut ffi::GstRTPBaseDepayload,
    caps: *mut gst::ffi::GstCaps,
) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();
    let caps = from_glib_borrow(caps);

    gst::panic_to_error!(imp, false, {
        match imp.set_caps(&caps) {
            Ok(()) => true,
            Err(err) => {
                err.log_with_imp(imp);
                false
            }
        }
    })
    .into_glib()
}

unsafe extern "C" fn rtp_base_depayload_handle_event<T: RTPBaseDepayloadImpl>(
    ptr: *mut ffi::GstRTPBaseDepayload,
    event: *mut gst::ffi::GstEvent,
) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    gst::panic_to_error!(imp, false, { imp.handle_event(from_glib_full(event)) }).into_glib()
}

unsafe extern "C" fn rtp_base_depayload_packet_lost<T: RTPBaseDepayloadImpl>(
    ptr: *mut ffi::GstRTPBaseDepayload,
    event: *mut gst::ffi::GstEvent,
) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    gst::panic_to_error!(imp, false, {
        imp.packet_lost(gst::EventRef::from_ptr(event))
    })
    .into_glib()
}

unsafe extern "C" fn rtp_base_depayload_process_rtp_packet<T: RTPBaseDepayloadImpl>(
    ptr: *mut ffi::GstRTPBaseDepayload,
    rtp_packet: *mut ffi::GstRTPBuffer,
) -> *mut gst::ffi::GstBuffer {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    gst::panic_to_error!(imp, ptr::null_mut(), {
        let bufwrap = crate::RTPBuffer::<crate::rtp_buffer::Readable>::from_glib_borrow(rtp_packet);

        imp.process_rtp_packet(&bufwrap)
            .map(|buffer| buffer.into_glib_ptr())
            .unwrap_or(ptr::null_mut())
    })
}