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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use glib;
#[cfg(any(feature = "v1_18", feature = "dox"))]
use glib::object::Cast;
use glib::object::IsA;
#[cfg(any(feature = "v1_18", feature = "dox"))]
use glib::signal::connect_raw;
#[cfg(any(feature = "v1_18", feature = "dox"))]
use glib::signal::SignalHandlerId;
use glib::translate::*;
#[cfg(any(feature = "v1_18", feature = "dox"))]
use glib::StaticType;
#[cfg(any(feature = "v1_18", feature = "dox"))]
use glib::Value;
#[cfg(any(feature = "v1_18", feature = "dox"))]
use glib_sys;
#[cfg(any(feature = "v1_18", feature = "dox"))]
use gobject_sys;
use gst;
use gst_video_sys;
#[cfg(any(feature = "v1_18", feature = "dox"))]
use std::boxed::Box as Box_;
#[cfg(any(feature = "v1_18", feature = "dox"))]
use std::mem::transmute;
use VideoCodecFrame;

glib_wrapper! {
    /// This base class is for video decoders turning encoded data into raw video
    /// frames.
    ///
    /// The GstVideoDecoder base class and derived subclasses should cooperate as
    /// follows:
    ///
    /// ## Configuration
    ///
    ///  * Initially, GstVideoDecoder calls `start` when the decoder element
    ///  is activated, which allows the subclass to perform any global setup.
    ///
    ///  * GstVideoDecoder calls `set_format` to inform the subclass of caps
    ///  describing input video data that it is about to receive, including
    ///  possibly configuration data.
    ///  While unlikely, it might be called more than once, if changing input
    ///  parameters require reconfiguration.
    ///
    ///  * Incoming data buffers are processed as needed, described in Data
    ///  Processing below.
    ///
    ///  * GstVideoDecoder calls `stop` at end of all processing.
    ///
    /// ## Data processing
    ///
    ///  * The base class gathers input data, and optionally allows subclass
    ///  to parse this into subsequently manageable chunks, typically
    ///  corresponding to and referred to as 'frames'.
    ///
    ///  * Each input frame is provided in turn to the subclass' `handle_frame`
    ///  callback.
    ///  The ownership of the frame is given to the `handle_frame` callback.
    ///
    ///  * If codec processing results in decoded data, the subclass should call
    ///  `VideoDecoder::finish_frame` to have decoded data pushed.
    ///  downstream. Otherwise, the subclass must call
    ///  `VideoDecoder::drop_frame`, to allow the base class to do timestamp
    ///  and offset tracking, and possibly to requeue the frame for a later
    ///  attempt in the case of reverse playback.
    ///
    /// ## Shutdown phase
    ///
    ///  * The GstVideoDecoder class calls `stop` to inform the subclass that data
    ///  parsing will be stopped.
    ///
    /// ## Additional Notes
    ///
    ///  * Seeking/Flushing
    ///
    ///  * When the pipeline is seeked or otherwise flushed, the subclass is
    ///  informed via a call to its `reset` callback, with the hard parameter
    ///  set to true. This indicates the subclass should drop any internal data
    ///  queues and timestamps and prepare for a fresh set of buffers to arrive
    ///  for parsing and decoding.
    ///
    ///  * End Of Stream
    ///
    ///  * At end-of-stream, the subclass `parse` function may be called some final
    ///  times with the at_eos parameter set to true, indicating that the element
    ///  should not expect any more data to be arriving, and it should parse and
    ///  remaining frames and call `VideoDecoder::have_frame` if possible.
    ///
    /// The subclass is responsible for providing pad template caps for
    /// source and sink pads. The pads need to be named "sink" and "src". It also
    /// needs to provide information about the output caps, when they are known.
    /// This may be when the base class calls the subclass' `set_format` function,
    /// though it might be during decoding, before calling
    /// `VideoDecoder::finish_frame`. This is done via
    /// `VideoDecoder::set_output_state`
    ///
    /// The subclass is also responsible for providing (presentation) timestamps
    /// (likely based on corresponding input ones). If that is not applicable
    /// or possible, the base class provides limited framerate based interpolation.
    ///
    /// Similarly, the base class provides some limited (legacy) seeking support
    /// if specifically requested by the subclass, as full-fledged support
    /// should rather be left to upstream demuxer, parser or alike. This simple
    /// approach caters for seeking and duration reporting using estimated input
    /// bitrates. To enable it, a subclass should call
    /// `VideoDecoderExt::set_estimate_rate` to enable handling of incoming
    /// byte-streams.
    ///
    /// The base class provides some support for reverse playback, in particular
    /// in case incoming data is not packetized or upstream does not provide
    /// fragments on keyframe boundaries. However, the subclass should then be
    /// prepared for the parsing and frame processing stage to occur separately
    /// (in normal forward processing, the latter immediately follows the former),
    /// The subclass also needs to ensure the parsing stage properly marks
    /// keyframes, unless it knows the upstream elements will do so properly for
    /// incoming data.
    ///
    /// The bare minimum that a functional subclass needs to implement is:
    ///
    ///  * Provide pad templates
    ///  * Inform the base class of output caps via
    ///  `VideoDecoder::set_output_state`
    ///
    ///  * Parse input data, if it is not considered packetized from upstream
    ///  Data will be provided to `parse` which should invoke
    ///  `VideoDecoderExt::add_to_frame` and `VideoDecoder::have_frame` to
    ///  separate the data belonging to each video frame.
    ///
    ///  * Accept data in `handle_frame` and provide decoded results to
    ///  `VideoDecoder::finish_frame`, or call `VideoDecoder::drop_frame`.
    ///
    /// # Implements
    ///
    /// [`VideoDecoderExt`](trait.VideoDecoderExt.html), [`gst::ElementExt`](../gst/trait.ElementExt.html), [`gst::ObjectExt`](../gst/trait.ObjectExt.html), [`glib::object::ObjectExt`](../glib/object/trait.ObjectExt.html), [`VideoDecoderExtManual`](prelude/trait.VideoDecoderExtManual.html)
    pub struct VideoDecoder(Object<gst_video_sys::GstVideoDecoder, gst_video_sys::GstVideoDecoderClass, VideoDecoderClass>) @extends gst::Element, gst::Object;

    match fn {
        get_type => || gst_video_sys::gst_video_decoder_get_type(),
    }
}

unsafe impl Send for VideoDecoder {}
unsafe impl Sync for VideoDecoder {}

pub const NONE_VIDEO_DECODER: Option<&VideoDecoder> = None;

/// Trait containing all `VideoDecoder` methods.
///
/// # Implementors
///
/// [`VideoDecoder`](struct.VideoDecoder.html)
pub trait VideoDecoderExt: 'static {
    /// Removes next `n_bytes` of input data and adds it to currently parsed frame.
    /// ## `n_bytes`
    /// the number of bytes to add
    fn add_to_frame(&self, n_bytes: i32);

    /// Helper function that allocates a buffer to hold a video frame for `self`'s
    /// current `VideoCodecState`.
    ///
    /// You should use `VideoDecoder::allocate_output_frame` instead of this
    /// function, if possible at all.
    ///
    /// # Returns
    ///
    /// allocated buffer, or NULL if no buffer could be
    ///  allocated (e.g. when downstream is flushing or shutting down)
    fn allocate_output_buffer(&self) -> Result<gst::Buffer, glib::BoolError>;

    ///
    /// # Returns
    ///
    /// the instance of the `gst::BufferPool` used
    /// by the decoder; free it after use it
    fn get_buffer_pool(&self) -> Option<gst::BufferPool>;

    ///
    /// # Returns
    ///
    /// currently configured byte to time conversion setting
    fn get_estimate_rate(&self) -> i32;

    /// Determines maximum possible decoding time for `frame` that will
    /// allow it to decode and arrive in time (as determined by QoS events).
    /// In particular, a negative result means decoding in time is no longer possible
    /// and should therefore occur as soon/skippy as possible.
    /// ## `frame`
    /// a `VideoCodecFrame`
    ///
    /// # Returns
    ///
    /// max decoding time.
    fn get_max_decode_time(&self, frame: &VideoCodecFrame) -> gst::ClockTimeDiff;

    ///
    /// # Returns
    ///
    /// currently configured decoder tolerated error count.
    fn get_max_errors(&self) -> i32;

    /// Queries decoder required format handling.
    ///
    /// # Returns
    ///
    /// `true` if required format handling is enabled.
    fn get_needs_format(&self) -> bool;

    /// Queries whether input data is considered packetized or not by the
    /// base class.
    ///
    /// # Returns
    ///
    /// TRUE if input data is considered packetized.
    fn get_packetized(&self) -> bool;

    /// Returns the number of bytes previously added to the current frame
    /// by calling `VideoDecoderExt::add_to_frame`.
    ///
    /// # Returns
    ///
    /// The number of bytes pending for the current frame
    fn get_pending_frame_size(&self) -> usize;

    ///
    /// # Returns
    ///
    /// The current QoS proportion.
    fn get_qos_proportion(&self) -> f64;

    /// Sets the audio decoder tags and how they should be merged with any
    /// upstream stream tags. This will override any tags previously-set
    /// with `gst_audio_decoder_merge_tags`.
    ///
    /// Note that this is provided for convenience, and the subclass is
    /// not required to use this and can still do tag handling on its own.
    ///
    /// MT safe.
    /// ## `tags`
    /// a `gst::TagList` to merge, or NULL to unset
    ///  previously-set tags
    /// ## `mode`
    /// the `gst::TagMergeMode` to use, usually `gst::TagMergeMode::Replace`
    fn merge_tags(&self, tags: Option<&gst::TagList>, mode: gst::TagMergeMode);

    /// Returns caps that express `caps` (or sink template caps if `caps` == NULL)
    /// restricted to resolution/format/... combinations supported by downstream
    /// elements.
    /// ## `caps`
    /// initial caps
    /// ## `filter`
    /// filter caps
    ///
    /// # Returns
    ///
    /// a `gst::Caps` owned by caller
    fn proxy_getcaps(&self, caps: Option<&gst::Caps>, filter: Option<&gst::Caps>) -> gst::Caps;

    /// Allows baseclass to perform byte to time estimated conversion.
    /// ## `enabled`
    /// whether to enable byte to time conversion
    fn set_estimate_rate(&self, enabled: bool);

    /// Sets numbers of tolerated decoder errors, where a tolerated one is then only
    /// warned about, but more than tolerated will lead to fatal error. You can set
    /// -1 for never returning fatal errors. Default is set to
    /// GST_VIDEO_DECODER_MAX_ERRORS.
    ///
    /// The '-1' option was added in 1.4
    /// ## `num`
    /// max tolerated errors
    fn set_max_errors(&self, num: i32);

    /// Configures decoder format needs. If enabled, subclass needs to be
    /// negotiated with format caps before it can process any data. It will then
    /// never be handed any data before it has been configured.
    /// Otherwise, it might be handed data without having been configured and
    /// is then expected being able to do so either by default
    /// or based on the input data.
    /// ## `enabled`
    /// new state
    fn set_needs_format(&self, enabled: bool);

    /// Allows baseclass to consider input data as packetized or not. If the
    /// input is packetized, then the `parse` method will not be called.
    /// ## `packetized`
    /// whether the input data should be considered as packetized.
    fn set_packetized(&self, packetized: bool);

    /// Lets `VideoDecoder` sub-classes decide if they want the sink pad
    /// to use the default pad query handler to reply to accept-caps queries.
    ///
    /// By setting this to true it is possible to further customize the default
    /// handler with `GST_PAD_SET_ACCEPT_INTERSECT` and
    /// `GST_PAD_SET_ACCEPT_TEMPLATE`
    /// ## `use_`
    /// if the default pad accept-caps query handling should be used
    fn set_use_default_pad_acceptcaps(&self, use_: bool);

    /// If set to `true` the decoder will handle QoS events received
    /// from downstream elements.
    /// This includes dropping output frames which are detected as late
    /// using the metrics reported by those events.
    ///
    /// Feature: `v1_18`
    ///
    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn get_property_qos(&self) -> bool;

    /// If set to `true` the decoder will handle QoS events received
    /// from downstream elements.
    /// This includes dropping output frames which are detected as late
    /// using the metrics reported by those events.
    ///
    /// Feature: `v1_18`
    ///
    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn set_property_qos(&self, qos: bool);

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn connect_property_max_errors_notify<F: Fn(&Self) + Send + Sync + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn connect_property_qos_notify<F: Fn(&Self) + Send + Sync + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;
}

impl<O: IsA<VideoDecoder>> VideoDecoderExt for O {
    fn add_to_frame(&self, n_bytes: i32) {
        unsafe {
            gst_video_sys::gst_video_decoder_add_to_frame(self.as_ref().to_glib_none().0, n_bytes);
        }
    }

    fn allocate_output_buffer(&self) -> Result<gst::Buffer, glib::BoolError> {
        unsafe {
            Option::<_>::from_glib_full(gst_video_sys::gst_video_decoder_allocate_output_buffer(
                self.as_ref().to_glib_none().0,
            ))
            .ok_or_else(|| glib_bool_error!("Failed to allocate output buffer"))
        }
    }

    fn get_buffer_pool(&self) -> Option<gst::BufferPool> {
        unsafe {
            from_glib_full(gst_video_sys::gst_video_decoder_get_buffer_pool(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_estimate_rate(&self) -> i32 {
        unsafe {
            gst_video_sys::gst_video_decoder_get_estimate_rate(self.as_ref().to_glib_none().0)
        }
    }

    fn get_max_decode_time(&self, frame: &VideoCodecFrame) -> gst::ClockTimeDiff {
        unsafe {
            gst_video_sys::gst_video_decoder_get_max_decode_time(
                self.as_ref().to_glib_none().0,
                frame.to_glib_none().0,
            )
        }
    }

    fn get_max_errors(&self) -> i32 {
        unsafe { gst_video_sys::gst_video_decoder_get_max_errors(self.as_ref().to_glib_none().0) }
    }

    fn get_needs_format(&self) -> bool {
        unsafe {
            from_glib(gst_video_sys::gst_video_decoder_get_needs_format(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_packetized(&self) -> bool {
        unsafe {
            from_glib(gst_video_sys::gst_video_decoder_get_packetized(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_pending_frame_size(&self) -> usize {
        unsafe {
            gst_video_sys::gst_video_decoder_get_pending_frame_size(self.as_ref().to_glib_none().0)
        }
    }

    fn get_qos_proportion(&self) -> f64 {
        unsafe {
            gst_video_sys::gst_video_decoder_get_qos_proportion(self.as_ref().to_glib_none().0)
        }
    }

    fn merge_tags(&self, tags: Option<&gst::TagList>, mode: gst::TagMergeMode) {
        unsafe {
            gst_video_sys::gst_video_decoder_merge_tags(
                self.as_ref().to_glib_none().0,
                tags.to_glib_none().0,
                mode.to_glib(),
            );
        }
    }

    fn proxy_getcaps(&self, caps: Option<&gst::Caps>, filter: Option<&gst::Caps>) -> gst::Caps {
        unsafe {
            from_glib_full(gst_video_sys::gst_video_decoder_proxy_getcaps(
                self.as_ref().to_glib_none().0,
                caps.to_glib_none().0,
                filter.to_glib_none().0,
            ))
        }
    }

    fn set_estimate_rate(&self, enabled: bool) {
        unsafe {
            gst_video_sys::gst_video_decoder_set_estimate_rate(
                self.as_ref().to_glib_none().0,
                enabled.to_glib(),
            );
        }
    }

    fn set_max_errors(&self, num: i32) {
        unsafe {
            gst_video_sys::gst_video_decoder_set_max_errors(self.as_ref().to_glib_none().0, num);
        }
    }

    fn set_needs_format(&self, enabled: bool) {
        unsafe {
            gst_video_sys::gst_video_decoder_set_needs_format(
                self.as_ref().to_glib_none().0,
                enabled.to_glib(),
            );
        }
    }

    fn set_packetized(&self, packetized: bool) {
        unsafe {
            gst_video_sys::gst_video_decoder_set_packetized(
                self.as_ref().to_glib_none().0,
                packetized.to_glib(),
            );
        }
    }

    fn set_use_default_pad_acceptcaps(&self, use_: bool) {
        unsafe {
            gst_video_sys::gst_video_decoder_set_use_default_pad_acceptcaps(
                self.as_ref().to_glib_none().0,
                use_.to_glib(),
            );
        }
    }

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn get_property_qos(&self) -> bool {
        unsafe {
            let mut value = Value::from_type(<bool as StaticType>::static_type());
            gobject_sys::g_object_get_property(
                self.to_glib_none().0 as *mut gobject_sys::GObject,
                b"qos\0".as_ptr() as *const _,
                value.to_glib_none_mut().0,
            );
            value
                .get()
                .expect("Return Value for property `qos` getter")
                .unwrap()
        }
    }

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn set_property_qos(&self, qos: bool) {
        unsafe {
            gobject_sys::g_object_set_property(
                self.to_glib_none().0 as *mut gobject_sys::GObject,
                b"qos\0".as_ptr() as *const _,
                Value::from(&qos).to_glib_none().0,
            );
        }
    }

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn connect_property_max_errors_notify<F: Fn(&Self) + Send + Sync + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn notify_max_errors_trampoline<P, F: Fn(&P) + Send + Sync + 'static>(
            this: *mut gst_video_sys::GstVideoDecoder,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<VideoDecoder>,
        {
            let f: &F = &*(f as *const F);
            f(&VideoDecoder::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::max-errors\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_max_errors_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn connect_property_qos_notify<F: Fn(&Self) + Send + Sync + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn notify_qos_trampoline<P, F: Fn(&P) + Send + Sync + 'static>(
            this: *mut gst_video_sys::GstVideoDecoder,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<VideoDecoder>,
        {
            let f: &F = &*(f as *const F);
            f(&VideoDecoder::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::qos\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_qos_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}