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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Take a look at the license at the top of the repository in the LICENSE file.

use std::{mem, ptr};

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

use crate::{
    ffi,
    utils::HasStreamLock,
    video_codec_state::{InNegotiation, Readable, VideoCodecState, VideoCodecStateContext},
    VideoCodecFrame, VideoEncoder,
};
mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::VideoEncoder>> Sealed for T {}
}

pub trait VideoEncoderExtManual: sealed::Sealed + IsA<VideoEncoder> + 'static {
    /// Helper function that allocates a buffer to hold an encoded video frame for `self`'s
    /// current [`VideoCodecState`][crate::VideoCodecState]. Subclass should already have configured video
    /// state and set src pad caps.
    ///
    /// The buffer allocated here is owned by the frame and you should only
    /// keep references to the frame, not the buffer.
    /// ## `frame`
    /// a [`VideoCodecFrame`][crate::VideoCodecFrame]
    /// ## `size`
    /// size of the buffer
    ///
    /// # Returns
    ///
    /// [`gst::FlowReturn::Ok`][crate::gst::FlowReturn::Ok] if an output buffer could be allocated
    #[doc(alias = "gst_video_encoder_allocate_output_frame")]
    fn allocate_output_frame(
        &self,
        frame: &mut VideoCodecFrame,
        size: usize,
    ) -> Result<gst::FlowSuccess, gst::FlowError> {
        unsafe {
            try_from_glib(ffi::gst_video_encoder_allocate_output_frame(
                self.as_ref().to_glib_none().0,
                frame.to_glib_none().0,
                size,
            ))
        }
    }

    /// Get a pending unfinished [`VideoCodecFrame`][crate::VideoCodecFrame]
    /// ## `frame_number`
    /// system_frame_number of a frame
    ///
    /// # Returns
    ///
    /// pending unfinished [`VideoCodecFrame`][crate::VideoCodecFrame] identified by `frame_number`.
    #[doc(alias = "get_frame")]
    #[doc(alias = "gst_video_encoder_get_frame")]
    fn frame(&self, frame_number: i32) -> Option<VideoCodecFrame> {
        let frame = unsafe {
            ffi::gst_video_encoder_get_frame(self.as_ref().to_glib_none().0, frame_number)
        };

        if frame.is_null() {
            None
        } else {
            unsafe { Some(VideoCodecFrame::new(frame, self.as_ref())) }
        }
    }

    /// Get all pending unfinished [`VideoCodecFrame`][crate::VideoCodecFrame]
    ///
    /// # Returns
    ///
    /// pending unfinished [`VideoCodecFrame`][crate::VideoCodecFrame].
    #[doc(alias = "get_frames")]
    #[doc(alias = "gst_video_encoder_get_frames")]
    fn frames(&self) -> Vec<VideoCodecFrame> {
        unsafe {
            let frames = ffi::gst_video_encoder_get_frames(self.as_ref().to_glib_none().0);
            let mut iter: *const glib::ffi::GList = frames;
            let mut vec = Vec::new();

            while !iter.is_null() {
                let frame_ptr = Ptr::from((*iter).data);
                /* transfer ownership of the frame */
                let frame = VideoCodecFrame::new(frame_ptr, self.as_ref());
                vec.push(frame);
                iter = (*iter).next;
            }

            glib::ffi::g_list_free(frames);
            vec
        }
    }

    /// Get the oldest unfinished pending [`VideoCodecFrame`][crate::VideoCodecFrame]
    ///
    /// # Returns
    ///
    /// oldest unfinished pending [`VideoCodecFrame`][crate::VideoCodecFrame]
    #[doc(alias = "get_oldest_frame")]
    #[doc(alias = "gst_video_encoder_get_oldest_frame")]
    fn oldest_frame(&self) -> Option<VideoCodecFrame> {
        let frame =
            unsafe { ffi::gst_video_encoder_get_oldest_frame(self.as_ref().to_glib_none().0) };

        if frame.is_null() {
            None
        } else {
            unsafe { Some(VideoCodecFrame::new(frame, self.as_ref())) }
        }
    }

    /// Lets [`VideoEncoder`][crate::VideoEncoder] sub-classes to know the memory `allocator`
    /// used by the base class and its `params`.
    ///
    /// Unref the `allocator` after use it.
    ///
    /// # Returns
    ///
    ///
    /// ## `allocator`
    /// the [`gst::Allocator`][crate::gst::Allocator]
    /// used
    ///
    /// ## `params`
    /// the
    /// [`gst::AllocationParams`][crate::gst::AllocationParams] of `allocator`
    #[doc(alias = "get_allocator")]
    #[doc(alias = "gst_video_encoder_get_allocator")]
    fn allocator(&self) -> (Option<gst::Allocator>, gst::AllocationParams) {
        unsafe {
            let mut allocator = ptr::null_mut();
            let mut params = mem::MaybeUninit::uninit();
            ffi::gst_video_encoder_get_allocator(
                self.as_ref().to_glib_none().0,
                &mut allocator,
                params.as_mut_ptr(),
            );
            (from_glib_full(allocator), params.assume_init().into())
        }
    }

    /// If multiple subframes are produced for one input frame then use this method
    /// for each subframe, except for the last one. Before calling this function,
    /// you need to fill frame->output_buffer with the encoded buffer to push.
    ///
    /// You must call [`VideoEncoderExt::finish_frame()`][crate::prelude::VideoEncoderExt::finish_frame()] for the last sub-frame
    /// to tell the encoder that the frame has been fully encoded.
    ///
    /// This function will change the metadata of `frame` and frame->output_buffer
    /// will be pushed downstream.
    /// ## `frame`
    /// a [`VideoCodecFrame`][crate::VideoCodecFrame] being encoded
    ///
    /// # Returns
    ///
    /// a [`gst::FlowReturn`][crate::gst::FlowReturn] resulting from pushing the buffer downstream.
    #[cfg(feature = "v1_18")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
    #[doc(alias = "gst_video_encoder_finish_subframe")]
    fn finish_subframe(&self, frame: &VideoCodecFrame) -> Result<gst::FlowSuccess, gst::FlowError> {
        unsafe {
            try_from_glib(ffi::gst_video_encoder_finish_subframe(
                self.as_ref().to_glib_none().0,
                frame.to_glib_none().0,
            ))
        }
    }

    /// Query the configured encoding latency. Results will be returned via
    /// `min_latency` and `max_latency`.
    ///
    /// # Returns
    ///
    ///
    /// ## `min_latency`
    /// address of variable in which to store the
    ///  configured minimum latency, or [`None`]
    ///
    /// ## `max_latency`
    /// address of variable in which to store the
    ///  configured maximum latency, or [`None`]
    #[doc(alias = "get_latency")]
    #[doc(alias = "gst_video_encoder_get_latency")]
    fn latency(&self) -> (gst::ClockTime, Option<gst::ClockTime>) {
        let mut min_latency = gst::ffi::GST_CLOCK_TIME_NONE;
        let mut max_latency = gst::ffi::GST_CLOCK_TIME_NONE;

        unsafe {
            ffi::gst_video_encoder_get_latency(
                self.as_ref().to_glib_none().0,
                &mut min_latency,
                &mut max_latency,
            );

            (
                try_from_glib(min_latency).expect("undefined min_latency"),
                from_glib(max_latency),
            )
        }
    }

    /// Informs baseclass of encoding latency. If the provided values changed from
    /// previously provided ones, this will also post a LATENCY message on the bus
    /// so the pipeline can reconfigure its global latency.
    /// ## `min_latency`
    /// minimum latency
    /// ## `max_latency`
    /// maximum latency
    #[doc(alias = "gst_video_encoder_set_latency")]
    fn set_latency(
        &self,
        min_latency: gst::ClockTime,
        max_latency: impl Into<Option<gst::ClockTime>>,
    ) {
        unsafe {
            ffi::gst_video_encoder_set_latency(
                self.as_ref().to_glib_none().0,
                min_latency.into_glib(),
                max_latency.into().into_glib(),
            );
        }
    }
    /// Get the current [`VideoCodecState`][crate::VideoCodecState]
    ///
    /// # Returns
    ///
    /// [`VideoCodecState`][crate::VideoCodecState] describing format of video data.
    #[doc(alias = "get_output_state")]
    #[doc(alias = "gst_video_encoder_get_output_state")]
    fn output_state(&self) -> Option<VideoCodecState<'static, Readable>> {
        let state =
            unsafe { ffi::gst_video_encoder_get_output_state(self.as_ref().to_glib_none().0) };

        if state.is_null() {
            None
        } else {
            unsafe { Some(VideoCodecState::<Readable>::new(state)) }
        }
    }

    /// Creates a new [`VideoCodecState`][crate::VideoCodecState] with the specified caps as the output state
    /// for the encoder.
    /// Any previously set output state on `self` will be replaced by the newly
    /// created one.
    ///
    /// The specified `caps` should not contain any resolution, pixel-aspect-ratio,
    /// framerate, codec-data, .... Those should be specified instead in the returned
    /// [`VideoCodecState`][crate::VideoCodecState].
    ///
    /// If the subclass wishes to copy over existing fields (like pixel aspect ratio,
    /// or framerate) from an existing [`VideoCodecState`][crate::VideoCodecState], it can be provided as a
    /// `reference`.
    ///
    /// If the subclass wishes to override some fields from the output state (like
    /// pixel-aspect-ratio or framerate) it can do so on the returned [`VideoCodecState`][crate::VideoCodecState].
    ///
    /// The new output state will only take effect (set on pads and buffers) starting
    /// from the next call to [`VideoEncoderExt::finish_frame()`][crate::prelude::VideoEncoderExt::finish_frame()].
    /// ## `caps`
    /// the [`gst::Caps`][crate::gst::Caps] to use for the output
    /// ## `reference`
    /// An optional reference [`VideoCodecState`][crate::VideoCodecState]
    ///
    /// # Returns
    ///
    /// the newly configured output state.
    #[doc(alias = "gst_video_encoder_set_output_state")]
    fn set_output_state(
        &self,
        caps: gst::Caps,
        reference: Option<&VideoCodecState<Readable>>,
    ) -> Result<VideoCodecState<InNegotiation>, gst::FlowError> {
        let state = unsafe {
            let reference = match reference {
                Some(reference) => reference.as_mut_ptr(),
                None => ptr::null_mut(),
            };
            ffi::gst_video_encoder_set_output_state(
                self.as_ref().to_glib_none().0,
                caps.into_glib_ptr(),
                reference,
            )
        };

        if state.is_null() {
            Err(gst::FlowError::NotNegotiated)
        } else {
            unsafe { Ok(VideoCodecState::<InNegotiation>::new(state, self.as_ref())) }
        }
    }

    /// Negotiate with downstream elements to currently configured [`VideoCodecState`][crate::VideoCodecState].
    /// Unmark GST_PAD_FLAG_NEED_RECONFIGURE in any case. But mark it again if
    /// negotiate fails.
    ///
    /// # Returns
    ///
    /// [`true`] if the negotiation succeeded, else [`false`].
    #[doc(alias = "gst_video_encoder_negotiate")]
    fn negotiate<'a>(
        &'a self,
        output_state: VideoCodecState<'a, InNegotiation<'a>>,
    ) -> Result<(), gst::FlowError> {
        // Consume output_state so user won't be able to modify it anymore
        let self_ptr = self.to_glib_none().0 as *const gst::ffi::GstElement;
        assert_eq!(output_state.context.element_as_ptr(), self_ptr);

        let ret = unsafe {
            from_glib(ffi::gst_video_encoder_negotiate(
                self.as_ref().to_glib_none().0,
            ))
        };
        if ret {
            Ok(())
        } else {
            Err(gst::FlowError::NotNegotiated)
        }
    }

    /// Set the codec headers to be sent downstream whenever requested.
    /// ## `headers`
    /// a list of [`gst::Buffer`][crate::gst::Buffer] containing the codec header
    #[doc(alias = "gst_video_encoder_set_headers")]
    fn set_headers(&self, headers: impl IntoIterator<Item = gst::Buffer>) {
        unsafe {
            ffi::gst_video_encoder_set_headers(
                self.as_ref().to_glib_none().0,
                headers
                    .into_iter()
                    .collect::<glib::List<_>>()
                    .into_glib_ptr(),
            );
        }
    }

    fn sink_pad(&self) -> &gst::Pad {
        unsafe {
            let elt = &*(self.as_ptr() as *const ffi::GstVideoEncoder);
            &*(&elt.sinkpad as *const *mut gst::ffi::GstPad as *const gst::Pad)
        }
    }

    fn src_pad(&self) -> &gst::Pad {
        unsafe {
            let elt = &*(self.as_ptr() as *const ffi::GstVideoEncoder);
            &*(&elt.srcpad as *const *mut gst::ffi::GstPad as *const gst::Pad)
        }
    }

    fn input_segment(&self) -> gst::Segment {
        unsafe {
            let ptr: &ffi::GstVideoDecoder = &*(self.as_ptr() as *const _);
            glib::ffi::g_rec_mutex_lock(mut_override(&ptr.stream_lock));
            let segment = ptr.input_segment;
            glib::ffi::g_rec_mutex_unlock(mut_override(&ptr.stream_lock));
            from_glib_none(&segment as *const gst::ffi::GstSegment)
        }
    }

    fn output_segment(&self) -> gst::Segment {
        unsafe {
            let ptr: &ffi::GstVideoDecoder = &*(self.as_ptr() as *const _);
            glib::ffi::g_rec_mutex_lock(mut_override(&ptr.stream_lock));
            let segment = ptr.output_segment;
            glib::ffi::g_rec_mutex_unlock(mut_override(&ptr.stream_lock));
            from_glib_none(&segment as *const gst::ffi::GstSegment)
        }
    }
}

impl<O: IsA<VideoEncoder>> VideoEncoderExtManual for O {}

impl HasStreamLock for VideoEncoder {
    fn stream_lock(&self) -> *mut glib::ffi::GRecMutex {
        let encoder_sys: *const ffi::GstVideoEncoder = self.to_glib_none().0;
        unsafe { mut_override(&(*encoder_sys).stream_lock) }
    }

    fn element_as_ptr(&self) -> *const gst::ffi::GstElement {
        self.as_ptr() as *const gst::ffi::GstElement
    }
}