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
205
206
207
208
209
210
211
// Take a look at the license at the top of the repository in the LICENSE file.

use std::mem;

use glib::{prelude::*, translate::*};
use gst::{
    format::{FormattedValue, SpecificFormattedValueFullRange},
    prelude::*,
};

use crate::{BaseParse, BaseParseFrame};

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

pub trait BaseParseExtManual: sealed::Sealed + IsA<BaseParse> + 'static {
    #[doc(alias = "get_sink_pad")]
    fn sink_pad(&self) -> &gst::Pad {
        unsafe {
            let elt = &*(self.as_ptr() as *const ffi::GstBaseParse);
            &*(&elt.sinkpad as *const *mut gst::ffi::GstPad as *const gst::Pad)
        }
    }

    #[doc(alias = "get_src_pad")]
    fn src_pad(&self) -> &gst::Pad {
        unsafe {
            let elt = &*(self.as_ptr() as *const ffi::GstBaseParse);
            &*(&elt.srcpad as *const *mut gst::ffi::GstPad as *const gst::Pad)
        }
    }

    fn segment(&self) -> gst::Segment {
        unsafe {
            let ptr: &ffi::GstBaseParse = &*(self.as_ptr() as *const _);
            let sinkpad = self.sink_pad();
            let _guard = sinkpad.stream_lock();
            from_glib_none(&ptr.segment as *const gst::ffi::GstSegment)
        }
    }

    fn lost_sync(&self) -> bool {
        unsafe {
            let ptr: &ffi::GstBaseParse = &*(self.as_ptr() as *const _);
            let sinkpad = self.sink_pad();
            let _guard = sinkpad.stream_lock();
            ptr.flags & ffi::GST_BASE_PARSE_FLAG_LOST_SYNC as u32 != 0
        }
    }

    fn is_draining(&self) -> bool {
        unsafe {
            let ptr: &ffi::GstBaseParse = &*(self.as_ptr() as *const _);
            let sinkpad = self.sink_pad();
            let _guard = sinkpad.stream_lock();
            ptr.flags & ffi::GST_BASE_PARSE_FLAG_DRAINING as u32 != 0
        }
    }

    /// Sets the duration of the currently playing media. Subclass can use this
    /// when it is able to determine duration and/or notices a change in the media
    /// duration. Alternatively, if `interval` is non-zero (default), then stream
    /// duration is determined based on estimated bitrate, and updated every `interval`
    /// frames.
    /// ## `fmt`
    /// [`gst::Format`][crate::gst::Format].
    /// ## `duration`
    /// duration value.
    /// ## `interval`
    /// how often to update the duration estimate based on bitrate, or 0.
    #[doc(alias = "gst_base_parse_set_duration")]
    fn set_duration(&self, duration: impl FormattedValue, interval: u32) {
        unsafe {
            ffi::gst_base_parse_set_duration(
                self.as_ref().to_glib_none().0,
                duration.format().into_glib(),
                duration.into_raw_value(),
                interval as i32,
            );
        }
    }

    /// If frames per second is configured, parser can take care of buffer duration
    /// and timestamping. When performing segment clipping, or seeking to a specific
    /// location, a corresponding decoder might need an initial `lead_in` and a
    /// following `lead_out` number of frames to ensure the desired segment is
    /// entirely filled upon decoding.
    /// ## `fps_num`
    /// frames per second (numerator).
    /// ## `fps_den`
    /// frames per second (denominator).
    /// ## `lead_in`
    /// frames needed before a segment for subsequent decode
    /// ## `lead_out`
    /// frames needed after a segment
    #[doc(alias = "gst_base_parse_set_frame_rate")]
    fn set_frame_rate(&self, fps: gst::Fraction, lead_in: u32, lead_out: u32) {
        let (fps_num, fps_den) = fps.into();
        unsafe {
            ffi::gst_base_parse_set_frame_rate(
                self.as_ref().to_glib_none().0,
                fps_num as u32,
                fps_den as u32,
                lead_in,
                lead_out,
            );
        }
    }

    /// Default implementation of `GstBaseParseClass::convert`.
    /// ## `src_format`
    /// [`gst::Format`][crate::gst::Format] describing the source format.
    /// ## `src_value`
    /// Source value to be converted.
    /// ## `dest_format`
    /// [`gst::Format`][crate::gst::Format] defining the converted format.
    ///
    /// # Returns
    ///
    /// [`true`] if conversion was successful.
    ///
    /// ## `dest_value`
    /// Pointer where the conversion result will be put.
    #[doc(alias = "gst_base_parse_convert_default")]
    fn convert_default<U: SpecificFormattedValueFullRange>(
        &self,
        src_val: impl FormattedValue,
    ) -> Option<U> {
        unsafe {
            let mut dest_val = mem::MaybeUninit::uninit();
            let ret = from_glib(ffi::gst_base_parse_convert_default(
                self.as_ref().to_glib_none().0,
                src_val.format().into_glib(),
                src_val.into_raw_value(),
                U::default_format().into_glib(),
                dest_val.as_mut_ptr(),
            ));
            if ret {
                Some(U::from_raw(U::default_format(), dest_val.assume_init()))
            } else {
                None
            }
        }
    }

    fn convert_default_generic(
        &self,
        src_val: impl FormattedValue,
        dest_format: gst::Format,
    ) -> Option<gst::GenericFormattedValue> {
        unsafe {
            let mut dest_val = mem::MaybeUninit::uninit();
            let ret = from_glib(ffi::gst_base_parse_convert_default(
                self.as_ref().to_glib_none().0,
                src_val.format().into_glib(),
                src_val.into_raw_value(),
                dest_format.into_glib(),
                dest_val.as_mut_ptr(),
            ));
            if ret {
                Some(gst::GenericFormattedValue::new(
                    dest_format,
                    dest_val.assume_init(),
                ))
            } else {
                None
            }
        }
    }

    /// Collects parsed data and pushes it downstream.
    /// Source pad caps must be set when this is called.
    ///
    /// If `frame`'s out_buffer is set, that will be used as subsequent frame data,
    /// and `size` amount will be flushed from the input data. The output_buffer size
    /// can differ from the consumed size indicated by `size`.
    ///
    /// Otherwise, `size` samples will be taken from the input and used for output,
    /// and the output's metadata (timestamps etc) will be taken as (optionally)
    /// set by the subclass on `frame`'s (input) buffer (which is otherwise
    /// ignored for any but the above purpose/information).
    ///
    /// Note that the latter buffer is invalidated by this call, whereas the
    /// caller retains ownership of `frame`.
    /// ## `frame`
    /// a [`BaseParseFrame`][crate::BaseParseFrame]
    /// ## `size`
    /// consumed input data represented by frame
    ///
    /// # Returns
    ///
    /// a [`gst::FlowReturn`][crate::gst::FlowReturn] that should be escalated to caller (of caller)
    #[doc(alias = "gst_base_parse_finish_frame")]
    fn finish_frame(
        &self,
        frame: BaseParseFrame,
        size: u32,
    ) -> Result<gst::FlowSuccess, gst::FlowError> {
        unsafe {
            try_from_glib(ffi::gst_base_parse_finish_frame(
                self.as_ref().to_glib_none().0,
                frame.to_glib_none().0,
                i32::try_from(size).expect("size higher than i32::MAX"),
            ))
        }
    }
}

impl<O: IsA<BaseParse>> BaseParseExtManual for O {}