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
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// from gst-gir-files (https://gitlab.freedesktop.org/gstreamer/gir-files-rs.git)
// DO NOT EDIT

use crate::ClockTime;
use crate::ControlBinding;
use glib::object::Cast;
use glib::object::IsA;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use std::boxed::Box as Box_;
use std::fmt;
use std::mem::transmute;

glib::wrapper! {
    /// [`Object`][crate::Object] provides a root for the object hierarchy tree filed in by the
    /// GStreamer library. It is currently a thin wrapper on top of
    /// `GInitiallyUnowned`. It is an abstract class that is not very usable on its own.
    ///
    /// [`Object`][crate::Object] gives us basic refcounting, parenting functionality and locking.
    /// Most of the functions are just extended for special GStreamer needs and can be
    /// found under the same name in the base class of [`Object`][crate::Object] which is [`glib::Object`][crate::glib::Object]
    /// (e.g. `g_object_ref()` becomes `gst_object_ref()`).
    ///
    /// Since [`Object`][crate::Object] derives from `GInitiallyUnowned`, it also inherits the
    /// floating reference. Be aware that functions such as [`GstBinExt::add()`][crate::prelude::GstBinExt::add()] and
    /// [`ElementExt::add_pad()`][crate::prelude::ElementExt::add_pad()] take ownership of the floating reference.
    ///
    /// In contrast to [`glib::Object`][crate::glib::Object] instances, [`Object`][crate::Object] adds a name property. The functions
    /// `gst_object_set_name()` and [`GstObjectExt::name()`][crate::prelude::GstObjectExt::name()] are used to set/get the name
    /// of the object.
    ///
    /// ## controlled properties
    ///
    /// Controlled properties offers a lightweight way to adjust gobject properties
    /// over stream-time. It works by using time-stamped value pairs that are queued
    /// for element-properties. At run-time the elements continuously pull value
    /// changes for the current stream-time.
    ///
    /// What needs to be changed in a [`Element`][crate::Element]?
    /// Very little - it is just two steps to make a plugin controllable!
    ///
    ///  * mark gobject-properties paramspecs that make sense to be controlled,
    ///  by GST_PARAM_CONTROLLABLE.
    ///
    ///  * when processing data (get, chain, loop function) at the beginning call
    ///  gst_object_sync_values(element,timestamp).
    ///  This will make the controller update all GObject properties that are
    ///  under its control with the current values based on the timestamp.
    ///
    /// What needs to be done in applications? Again it's not a lot to change.
    ///
    ///  * create a [`ControlSource`][crate::ControlSource].
    ///  csource = gst_interpolation_control_source_new ();
    ///  g_object_set (csource, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
    ///
    ///  * Attach the [`ControlSource`][crate::ControlSource] on the controller to a property.
    ///  gst_object_add_control_binding (object, gst_direct_control_binding_new (object, "prop1", csource));
    ///
    ///  * Set the control values
    ///  gst_timed_value_control_source_set ((GstTimedValueControlSource *)csource,0 * GST_SECOND, value1);
    ///  gst_timed_value_control_source_set ((GstTimedValueControlSource *)csource,1 * GST_SECOND, value2);
    ///
    ///  * start your pipeline
    ///
    /// This is an Abstract Base Class, you cannot instantiate it.
    ///
    /// # Implements
    ///
    /// [`GstObjectExt`][trait@crate::prelude::GstObjectExt], [`trait@glib::ObjectExt`]
    #[doc(alias = "GstObject")]
    pub struct Object(Object<ffi::GstObject, ffi::GstObjectClass>);

    match fn {
        type_ => || ffi::gst_object_get_type(),
    }
}

impl Object {
    pub const NONE: Option<&'static Object> = None;

    /// Checks to see if there is any object named `name` in `list`. This function
    /// does not do any locking of any kind. You might want to protect the
    /// provided list with the lock of the owner of the list. This function
    /// will lock each [`Object`][crate::Object] in the list to compare the name, so be
    /// careful when passing a list with a locked object.
    /// ## `list`
    /// a list of [`Object`][crate::Object] to
    ///  check through
    /// ## `name`
    /// the name to search for
    ///
    /// # Returns
    ///
    /// [`true`] if a [`Object`][crate::Object] named `name` does not appear in `list`,
    /// [`false`] if it does.
    ///
    /// MT safe. Grabs and releases the LOCK of each object in the list.
    #[doc(alias = "gst_object_check_uniqueness")]
    pub fn check_uniqueness(list: &[Object], name: &str) -> bool {
        assert_initialized_main_thread!();
        unsafe {
            from_glib(ffi::gst_object_check_uniqueness(
                list.to_glib_none().0,
                name.to_glib_none().0,
            ))
        }
    }

    //#[doc(alias = "gst_object_default_deep_notify")]
    //pub fn default_deep_notify(object: &impl IsA<glib::Object>, orig: &impl IsA<Object>, pspec: /*Ignored*/&glib::ParamSpec, excluded_props: &[&str]) {
    //    unsafe { TODO: call ffi:gst_object_default_deep_notify() }
    //}

    //#[doc(alias = "gst_object_replace")]
    //pub fn replace(oldobj: Option<impl IsA<Object>>, newobj: Option<&impl IsA<Object>>) -> bool {
    //    unsafe { TODO: call ffi:gst_object_replace() }
    //}
}

impl fmt::Display for Object {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(&GstObjectExt::name(self))
    }
}

unsafe impl Send for Object {}
unsafe impl Sync for Object {}

/// Trait containing all [`struct@Object`] methods.
///
/// # Implementors
///
/// [`Allocator`][struct@crate::Allocator], [`BufferPool`][struct@crate::BufferPool], [`Bus`][struct@crate::Bus], [`Clock`][struct@crate::Clock], [`ControlBinding`][struct@crate::ControlBinding], [`ControlSource`][struct@crate::ControlSource], [`DeviceMonitor`][struct@crate::DeviceMonitor], [`DeviceProvider`][struct@crate::DeviceProvider], [`Device`][struct@crate::Device], [`Element`][struct@crate::Element], [`Object`][struct@crate::Object], [`PadTemplate`][struct@crate::PadTemplate], [`Pad`][struct@crate::Pad], [`PluginFeature`][struct@crate::PluginFeature], [`Plugin`][struct@crate::Plugin], [`Registry`][struct@crate::Registry], [`StreamCollection`][struct@crate::StreamCollection], [`Stream`][struct@crate::Stream], [`TaskPool`][struct@crate::TaskPool], [`Task`][struct@crate::Task], [`Tracer`][struct@crate::Tracer]
pub trait GstObjectExt: 'static {
    /// Attach the [`ControlBinding`][crate::ControlBinding] to the object. If there already was a
    /// [`ControlBinding`][crate::ControlBinding] for this property it will be replaced.
    ///
    /// The object's reference count will be incremented, and any floating
    /// reference will be removed (see `gst_object_ref_sink()`)
    /// ## `binding`
    /// the [`ControlBinding`][crate::ControlBinding] that should be used
    ///
    /// # Returns
    ///
    /// [`false`] if the given `binding` has not been setup for this object or
    /// has been setup for a non suitable property, [`true`] otherwise.
    #[doc(alias = "gst_object_add_control_binding")]
    fn add_control_binding(
        &self,
        binding: &impl IsA<ControlBinding>,
    ) -> Result<(), glib::error::BoolError>;

    /// A default error function that uses `g_printerr()` to display the error message
    /// and the optional debug string..
    ///
    /// The default handler will simply print the error string using g_print.
    /// ## `error`
    /// the GError.
    /// ## `debug`
    /// an additional debug information string, or [`None`]
    #[doc(alias = "gst_object_default_error")]
    fn default_error(&self, error: &glib::Error, debug: Option<&str>);

    /// Gets the corresponding [`ControlBinding`][crate::ControlBinding] for the property. This should be
    /// unreferenced again after use.
    /// ## `property_name`
    /// name of the property
    ///
    /// # Returns
    ///
    /// the [`ControlBinding`][crate::ControlBinding] for
    /// `property_name` or [`None`] if the property is not controlled.
    #[doc(alias = "gst_object_get_control_binding")]
    #[doc(alias = "get_control_binding")]
    fn control_binding(&self, property_name: &str) -> Option<ControlBinding>;

    /// Obtain the control-rate for this `self`. Audio processing [`Element`][crate::Element]
    /// objects will use this rate to sub-divide their processing loop and call
    /// [`sync_values()`][Self::sync_values()] in between. The length of the processing segment
    /// should be up to `control`-rate nanoseconds.
    ///
    /// If the `self` is not under property control, this will return
    /// `GST_CLOCK_TIME_NONE`. This allows the element to avoid the sub-dividing.
    ///
    /// The control-rate is not expected to change if the element is in
    /// [`State::Paused`][crate::State::Paused] or [`State::Playing`][crate::State::Playing].
    ///
    /// # Returns
    ///
    /// the control rate in nanoseconds
    #[doc(alias = "gst_object_get_control_rate")]
    #[doc(alias = "get_control_rate")]
    fn control_rate(&self) -> Option<ClockTime>;

    /// Returns a copy of the name of `self`.
    /// Caller should `g_free()` the return value after usage.
    /// For a nameless object, this returns [`None`], which you can safely `g_free()`
    /// as well.
    ///
    /// Free-function: g_free
    ///
    /// # Returns
    ///
    /// the name of `self`. `g_free()`
    /// after usage.
    ///
    /// MT safe. This function grabs and releases `self`'s LOCK.
    #[doc(alias = "gst_object_get_name")]
    #[doc(alias = "get_name")]
    fn name(&self) -> glib::GString;

    /// Returns the parent of `self`. This function increases the refcount
    /// of the parent object so you should `gst_object_unref()` it after usage.
    ///
    /// # Returns
    ///
    /// parent of `self`, this can be
    ///  [`None`] if `self` has no parent. unref after usage.
    ///
    /// MT safe. Grabs and releases `self`'s LOCK.
    #[doc(alias = "gst_object_get_parent")]
    #[doc(alias = "get_parent")]
    #[must_use]
    fn parent(&self) -> Option<Object>;

    /// Generates a string describing the path of `self` in
    /// the object hierarchy. Only useful (or used) for debugging.
    ///
    /// Free-function: g_free
    ///
    /// # Returns
    ///
    /// a string describing the path of `self`. You must
    ///  `g_free()` the string after usage.
    ///
    /// MT safe. Grabs and releases the [`Object`][crate::Object]'s LOCK for all objects
    ///  in the hierarchy.
    #[doc(alias = "gst_object_get_path_string")]
    #[doc(alias = "get_path_string")]
    fn path_string(&self) -> glib::GString;

    /// Gets the value for the given controlled property at the requested time.
    /// ## `property_name`
    /// the name of the property to get
    /// ## `timestamp`
    /// the time the control-change should be read from
    ///
    /// # Returns
    ///
    /// the GValue of the property at the given time,
    /// or [`None`] if the property isn't controlled.
    #[doc(alias = "gst_object_get_value")]
    #[doc(alias = "get_value")]
    fn value(
        &self,
        property_name: &str,
        timestamp: impl Into<Option<ClockTime>>,
    ) -> Option<glib::Value>;

    //#[doc(alias = "gst_object_get_value_array")]
    //#[doc(alias = "get_value_array")]
    //fn is_value_array(&self, property_name: &str, timestamp: impl Into<Option<ClockTime>>, interval: impl Into<Option<ClockTime>>, values: /*Unimplemented*/&[&Basic: Pointer]) -> bool;

    #[doc(alias = "gst_object_has_active_control_bindings")]
    fn has_active_control_bindings(&self) -> bool;

    #[doc(alias = "gst_object_has_ancestor")]
    fn has_ancestor(&self, ancestor: &impl IsA<Object>) -> bool;

    #[doc(alias = "gst_object_has_as_ancestor")]
    fn has_as_ancestor(&self, ancestor: &impl IsA<Object>) -> bool;

    #[doc(alias = "gst_object_has_as_parent")]
    fn has_as_parent(&self, parent: &impl IsA<Object>) -> bool;

    #[doc(alias = "gst_object_remove_control_binding")]
    fn remove_control_binding(&self, binding: &impl IsA<ControlBinding>) -> bool;

    #[doc(alias = "gst_object_set_control_binding_disabled")]
    fn set_control_binding_disabled(&self, property_name: &str, disabled: bool);

    #[doc(alias = "gst_object_set_control_bindings_disabled")]
    fn set_control_bindings_disabled(&self, disabled: bool);

    #[doc(alias = "gst_object_set_control_rate")]
    fn set_control_rate(&self, control_rate: impl Into<Option<ClockTime>>);

    #[doc(alias = "gst_object_set_parent")]
    fn set_parent(&self, parent: &impl IsA<Object>) -> Result<(), glib::error::BoolError>;

    #[doc(alias = "gst_object_suggest_next_sync")]
    fn suggest_next_sync(&self) -> Option<ClockTime>;

    #[doc(alias = "gst_object_sync_values")]
    fn sync_values(&self, timestamp: ClockTime) -> Result<(), glib::error::BoolError>;

    #[doc(alias = "gst_object_unparent")]
    fn unparent(&self);

    //#[doc(alias = "deep-notify")]
    //fn connect_deep_notify<Unsupported or ignored types>(&self, detail: Option<&str>, f: F) -> SignalHandlerId;

    #[doc(alias = "parent")]
    fn connect_parent_notify<F: Fn(&Self) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<Object>> GstObjectExt for O {
    fn add_control_binding(
        &self,
        binding: &impl IsA<ControlBinding>,
    ) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib::result_from_gboolean!(
                ffi::gst_object_add_control_binding(
                    self.as_ref().to_glib_none().0,
                    binding.as_ref().to_glib_none().0
                ),
                "Failed to add control binding"
            )
        }
    }

    fn default_error(&self, error: &glib::Error, debug: Option<&str>) {
        unsafe {
            ffi::gst_object_default_error(
                self.as_ref().to_glib_none().0,
                error.to_glib_none().0,
                debug.to_glib_none().0,
            );
        }
    }

    fn control_binding(&self, property_name: &str) -> Option<ControlBinding> {
        unsafe {
            from_glib_full(ffi::gst_object_get_control_binding(
                self.as_ref().to_glib_none().0,
                property_name.to_glib_none().0,
            ))
        }
    }

    fn control_rate(&self) -> Option<ClockTime> {
        unsafe {
            from_glib(ffi::gst_object_get_control_rate(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn name(&self) -> glib::GString {
        unsafe { from_glib_full(ffi::gst_object_get_name(self.as_ref().to_glib_none().0)) }
    }

    fn parent(&self) -> Option<Object> {
        unsafe { from_glib_full(ffi::gst_object_get_parent(self.as_ref().to_glib_none().0)) }
    }

    fn path_string(&self) -> glib::GString {
        unsafe {
            from_glib_full(ffi::gst_object_get_path_string(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn value(
        &self,
        property_name: &str,
        timestamp: impl Into<Option<ClockTime>>,
    ) -> Option<glib::Value> {
        unsafe {
            from_glib_full(ffi::gst_object_get_value(
                self.as_ref().to_glib_none().0,
                property_name.to_glib_none().0,
                timestamp.into().into_glib(),
            ))
        }
    }

    //fn is_value_array(&self, property_name: &str, timestamp: impl Into<Option<ClockTime>>, interval: impl Into<Option<ClockTime>>, values: /*Unimplemented*/&[&Basic: Pointer]) -> bool {
    //    unsafe { TODO: call ffi:gst_object_get_value_array() }
    //}

    fn has_active_control_bindings(&self) -> bool {
        unsafe {
            from_glib(ffi::gst_object_has_active_control_bindings(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn has_ancestor(&self, ancestor: &impl IsA<Object>) -> bool {
        unsafe {
            from_glib(ffi::gst_object_has_ancestor(
                self.as_ref().to_glib_none().0,
                ancestor.as_ref().to_glib_none().0,
            ))
        }
    }

    fn has_as_ancestor(&self, ancestor: &impl IsA<Object>) -> bool {
        unsafe {
            from_glib(ffi::gst_object_has_as_ancestor(
                self.as_ref().to_glib_none().0,
                ancestor.as_ref().to_glib_none().0,
            ))
        }
    }

    fn has_as_parent(&self, parent: &impl IsA<Object>) -> bool {
        unsafe {
            from_glib(ffi::gst_object_has_as_parent(
                self.as_ref().to_glib_none().0,
                parent.as_ref().to_glib_none().0,
            ))
        }
    }

    fn remove_control_binding(&self, binding: &impl IsA<ControlBinding>) -> bool {
        unsafe {
            from_glib(ffi::gst_object_remove_control_binding(
                self.as_ref().to_glib_none().0,
                binding.as_ref().to_glib_none().0,
            ))
        }
    }

    fn set_control_binding_disabled(&self, property_name: &str, disabled: bool) {
        unsafe {
            ffi::gst_object_set_control_binding_disabled(
                self.as_ref().to_glib_none().0,
                property_name.to_glib_none().0,
                disabled.into_glib(),
            );
        }
    }

    fn set_control_bindings_disabled(&self, disabled: bool) {
        unsafe {
            ffi::gst_object_set_control_bindings_disabled(
                self.as_ref().to_glib_none().0,
                disabled.into_glib(),
            );
        }
    }

    fn set_control_rate(&self, control_rate: impl Into<Option<ClockTime>>) {
        unsafe {
            ffi::gst_object_set_control_rate(
                self.as_ref().to_glib_none().0,
                control_rate.into().into_glib(),
            );
        }
    }

    fn set_parent(&self, parent: &impl IsA<Object>) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib::result_from_gboolean!(
                ffi::gst_object_set_parent(
                    self.as_ref().to_glib_none().0,
                    parent.as_ref().to_glib_none().0
                ),
                "Failed to set parent object"
            )
        }
    }

    fn suggest_next_sync(&self) -> Option<ClockTime> {
        unsafe {
            from_glib(ffi::gst_object_suggest_next_sync(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn sync_values(&self, timestamp: ClockTime) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib::result_from_gboolean!(
                ffi::gst_object_sync_values(self.as_ref().to_glib_none().0, timestamp.into_glib()),
                "Failed to sync values"
            )
        }
    }

    fn unparent(&self) {
        unsafe {
            ffi::gst_object_unparent(self.as_ref().to_glib_none().0);
        }
    }

    //fn connect_deep_notify<Unsupported or ignored types>(&self, detail: Option<&str>, f: F) -> SignalHandlerId {
    //    Ignored prop: GObject.ParamSpec
    //}

    fn connect_parent_notify<F: Fn(&Self) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_parent_trampoline<
            P: IsA<Object>,
            F: Fn(&P) + Send + Sync + 'static,
        >(
            this: *mut ffi::GstObject,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Object::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::parent\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_parent_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}