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
// 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 ClockTime;
use Error;
use ffi;
use glib;
use glib::object::Downcast;
use glib::object::IsA;
use glib::signal::SignalHandlerId;
use glib::signal::connect;
use glib::translate::*;
use glib_ffi;
use gobject_ffi;
use std::boxed::Box as Box_;
use std::mem;
use std::mem::transmute;
use std::ptr;

glib_wrapper! {
    /// `Object` provides a root for the object hierarchy tree filed in by the
    /// GStreamer library. It is currently a thin wrapper on top of
    /// `gobject::InitiallyUnowned`. It is an abstract class that is not very usable on its own.
    ///
    /// `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` which is `gobject::Object`
    /// (e.g. `gobject::ObjectExt::ref` becomes `GstObjectExt::ref`).
    ///
    /// Since `Object` derives from `gobject::InitiallyUnowned`, it also inherits the
    /// floating reference. Be aware that functions such as `BinExt::add` and
    /// `ElementExt::add_pad` take ownership of the floating reference.
    ///
    /// In contrast to `gobject::Object` instances, `Object` adds a name property. The functions
    /// `GstObjectExt::set_name` and `GstObjectExt::get_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`?
    /// 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`.
    ///  csource = gst_interpolation_control_source_new ();
    ///  g_object_set (csource, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
    ///
    ///  * Attach the `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
    ///
    /// # Implements
    ///
    /// [`GstObjectExt`](trait.GstObjectExt.html), [`glib::object::ObjectExt`](../glib/object/trait.ObjectExt.html)
    pub struct Object(Object<ffi::GstObject, ffi::GstObjectClass>);

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

impl Object {
    /// 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` in the list to compare the name, so be
    /// careful when passing a list with a locked object.
    /// ## `list`
    /// a list of `Object` to
    ///  check through
    /// ## `name`
    /// the name to search for
    ///
    /// # Returns
    ///
    /// `true` if a `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.
    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))
        }
    }

    //pub fn default_deep_notify<P: IsA<glib::Object>, Q: IsA<Object>, R: IsA</*Ignored*/glib::ParamSpec>>(object: &P, orig: &Q, pspec: &R, excluded_props: &[&str]) {
    //    unsafe { TODO: call ffi::gst_object_default_deep_notify() }
    //}

    //pub fn ref_sink<P: Into<Option</*Unimplemented*/Fundamental: Pointer>>>(object: P) -> /*Unimplemented*/Option<Fundamental: Pointer> {
    //    unsafe { TODO: call ffi::gst_object_ref_sink() }
    //}

    //pub fn replace<'a, 'b, P: IsA<Object> + 'a, Q: Into<Option<&'a P>>, R: IsA<Object> + 'b, S: Into<Option<&'b R>>>(oldobj: Q, newobj: S) -> bool {
    //    unsafe { TODO: call ffi::gst_object_replace() }
    //}
}

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

/// Trait containing all `Object` methods.
///
/// # Implementors
///
/// [`BufferPool`](struct.BufferPool.html), [`Bus`](struct.Bus.html), [`Clock`](struct.Clock.html), [`DeviceMonitor`](struct.DeviceMonitor.html), [`DeviceProvider`](struct.DeviceProvider.html), [`Device`](struct.Device.html), [`Element`](struct.Element.html), [`Object`](struct.Object.html), [`PadTemplate`](struct.PadTemplate.html), [`Pad`](struct.Pad.html), [`PluginFeature`](struct.PluginFeature.html), [`Plugin`](struct.Plugin.html), [`Registry`](struct.Registry.html), [`StreamCollection`](struct.StreamCollection.html), [`Stream`](struct.Stream.html)
pub trait GstObjectExt {
    //fn add_control_binding(&self, binding: /*Ignored*/&ControlBinding) -> bool;

    /// A default error function that uses `g_printerr` to display the error message
    /// and the optional debug sting..
    ///
    /// The default handler will simply print the error string using g_print.
    /// ## `error`
    /// the GError.
    /// ## `debug`
    /// an additional debug information string, or `None`
    fn default_error<'a, P: Into<Option<&'a str>>>(&self, error: &Error, debug: P);

    //fn get_control_binding(&self, property_name: &str) -> /*Ignored*/Option<ControlBinding>;

    /// Obtain the control-rate for this `self`. Audio processing `Element`
    /// objects will use this rate to sub-divide their processing loop and call
    /// `GstObjectExt::sync_values` inbetween. 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` or `State::Playing`.
    ///
    /// # Returns
    ///
    /// the control rate in nanoseconds
    fn get_control_rate(&self) -> ClockTime;

    //fn get_g_value_array(&self, property_name: &str, timestamp: ClockTime, interval: ClockTime, values: /*Ignored*/&[&glib::Value]) -> bool;

    /// 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.
    fn get_name(&self) -> String;

    /// Returns the parent of `self`. This function increases the refcount
    /// of the parent object so you should `GstObjectExt::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.
    fn get_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`'s LOCK for all objects
    ///  in the hierarchy.
    fn get_path_string(&self) -> String;

    //fn get_value(&self, property_name: &str, timestamp: ClockTime) -> /*Ignored*/Option<glib::Value>;

    //fn get_value_array<P: Into<Option</*Unimplemented*/Fundamental: Pointer>>>(&self, property_name: &str, timestamp: ClockTime, interval: ClockTime, n_values: u32, values: P) -> bool;

    /// Check if the `self` has active controlled properties.
    ///
    /// # Returns
    ///
    /// `true` if the object has active controlled properties
    fn has_active_control_bindings(&self) -> bool;

    /// Check if `self` has an ancestor `ancestor` somewhere up in
    /// the hierarchy. One can e.g. check if a `Element` is inside a `Pipeline`.
    ///
    /// # Deprecated
    ///
    /// Use `GstObjectExt::has_as_ancestor` instead.
    ///
    /// MT safe. Grabs and releases `self`'s locks.
    /// ## `ancestor`
    /// a `Object` to check as ancestor
    ///
    /// # Returns
    ///
    /// `true` if `ancestor` is an ancestor of `self`.
    fn has_ancestor<P: IsA<Object>>(&self, ancestor: &P) -> bool;

    /// Check if `self` has an ancestor `ancestor` somewhere up in
    /// the hierarchy. One can e.g. check if a `Element` is inside a `Pipeline`.
    /// ## `ancestor`
    /// a `Object` to check as ancestor
    ///
    /// # Returns
    ///
    /// `true` if `ancestor` is an ancestor of `self`.
    ///
    /// MT safe. Grabs and releases `self`'s locks.
    fn has_as_ancestor<P: IsA<Object>>(&self, ancestor: &P) -> bool;

    /// Check if `parent` is the parent of `self`.
    /// E.g. a `Element` can check if it owns a given `Pad`.
    /// ## `parent`
    /// a `Object` to check as parent
    ///
    /// # Returns
    ///
    /// `false` if either `self` or `parent` is `None`. `true` if `parent` is
    ///  the parent of `self`. Otherwise `false`.
    ///
    /// MT safe. Grabs and releases `self`'s locks.
    fn has_as_parent<P: IsA<Object>>(&self, parent: &P) -> bool;

    //fn remove_control_binding(&self, binding: /*Ignored*/&ControlBinding) -> bool;

    /// This function is used to disable the control bindings on a property for
    /// some time, i.e. `GstObjectExt::sync_values` will do nothing for the
    /// property.
    /// ## `property_name`
    /// property to disable
    /// ## `disabled`
    /// boolean that specifies whether to disable the controller
    /// or not.
    fn set_control_binding_disabled(&self, property_name: &str, disabled: bool);

    /// This function is used to disable all controlled properties of the `self` for
    /// some time, i.e. `GstObjectExt::sync_values` will do nothing.
    /// ## `disabled`
    /// boolean that specifies whether to disable the controller
    /// or not.
    fn set_control_bindings_disabled(&self, disabled: bool);

    /// Change the control-rate for this `self`. Audio processing `Element`
    /// objects will use this rate to sub-divide their processing loop and call
    /// `GstObjectExt::sync_values` inbetween. The length of the processing segment
    /// should be up to `control`-rate nanoseconds.
    ///
    /// The control-rate should not change if the element is in `State::Paused` or
    /// `State::Playing`.
    /// ## `control_rate`
    /// the new control-rate in nanoseconds.
    fn set_control_rate(&self, control_rate: ClockTime);

    /// Sets the name of `self`, or gives `self` a guaranteed unique
    /// name (if `name` is `None`).
    /// This function makes a copy of the provided name, so the caller
    /// retains ownership of the name it sent.
    /// ## `name`
    /// new name of object
    ///
    /// # Returns
    ///
    /// `true` if the name could be set. Since Objects that have
    /// a parent cannot be renamed, this function returns `false` in those
    /// cases.
    ///
    /// MT safe. This function grabs and releases `self`'s LOCK.
    fn set_name(&self, name: &str) -> Result<(), glib::error::BoolError>;

    /// Sets the parent of `self` to `parent`. The object's reference count will
    /// be incremented, and any floating reference will be removed (see `Object::ref_sink`).
    /// ## `parent`
    /// new parent of object
    ///
    /// # Returns
    ///
    /// `true` if `parent` could be set or `false` when `self`
    /// already had a parent or `self` and `parent` are the same.
    ///
    /// MT safe. Grabs and releases `self`'s LOCK.
    fn set_parent<P: IsA<Object>>(&self, parent: &P) -> Result<(), glib::error::BoolError>;

    /// Returns a suggestion for timestamps where buffers should be split
    /// to get best controller results.
    ///
    /// # Returns
    ///
    /// Returns the suggested timestamp or `GST_CLOCK_TIME_NONE`
    /// if no control-rate was set.
    fn suggest_next_sync(&self) -> ClockTime;

    /// Sets the properties of the object, according to the `GstControlSources` that
    /// (maybe) handle them and for the given timestamp.
    ///
    /// If this function fails, it is most likely the application developers fault.
    /// Most probably the control sources are not setup correctly.
    /// ## `timestamp`
    /// the time that should be processed
    ///
    /// # Returns
    ///
    /// `true` if the controller values could be applied to the object
    /// properties, `false` otherwise
    fn sync_values(&self, timestamp: ClockTime) -> Result<(), glib::error::BoolError>;

    /// Clear the parent of `self`, removing the associated reference.
    /// This function decreases the refcount of `self`.
    ///
    /// MT safe. Grabs and releases `self`'s lock.
    fn unparent(&self);

    //fn connect_deep_notify<Unsupported or ignored types>(&self, f: F) -> SignalHandlerId;

    fn connect_property_name_notify<F: Fn(&Self) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId;

    fn connect_property_parent_notify<F: Fn(&Self) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<Object> + IsA<glib::object::Object>> GstObjectExt for O {
    //fn add_control_binding(&self, binding: /*Ignored*/&ControlBinding) -> bool {
    //    unsafe { TODO: call ffi::gst_object_add_control_binding() }
    //}

    fn default_error<'a, P: Into<Option<&'a str>>>(&self, error: &Error, debug: P) {
        let debug = debug.into();
        let debug = debug.to_glib_none();
        unsafe {
            ffi::gst_object_default_error(self.to_glib_none().0, error.to_glib_none().0, debug.0);
        }
    }

    //fn get_control_binding(&self, property_name: &str) -> /*Ignored*/Option<ControlBinding> {
    //    unsafe { TODO: call ffi::gst_object_get_control_binding() }
    //}

    fn get_control_rate(&self) -> ClockTime {
        unsafe {
            from_glib(ffi::gst_object_get_control_rate(self.to_glib_none().0))
        }
    }

    //fn get_g_value_array(&self, property_name: &str, timestamp: ClockTime, interval: ClockTime, values: /*Ignored*/&[&glib::Value]) -> bool {
    //    unsafe { TODO: call ffi::gst_object_get_g_value_array() }
    //}

    fn get_name(&self) -> String {
        unsafe {
            from_glib_full(ffi::gst_object_get_name(self.to_glib_none().0))
        }
    }

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

    fn get_path_string(&self) -> String {
        unsafe {
            from_glib_full(ffi::gst_object_get_path_string(self.to_glib_none().0))
        }
    }

    //fn get_value(&self, property_name: &str, timestamp: ClockTime) -> /*Ignored*/Option<glib::Value> {
    //    unsafe { TODO: call ffi::gst_object_get_value() }
    //}

    //fn get_value_array<P: Into<Option</*Unimplemented*/Fundamental: Pointer>>>(&self, property_name: &str, timestamp: ClockTime, interval: ClockTime, n_values: u32, values: P) -> 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.to_glib_none().0))
        }
    }

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

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

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

    //fn remove_control_binding(&self, binding: /*Ignored*/&ControlBinding) -> bool {
    //    unsafe { TODO: call ffi::gst_object_remove_control_binding() }
    //}

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

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

    fn set_control_rate(&self, control_rate: ClockTime) {
        unsafe {
            ffi::gst_object_set_control_rate(self.to_glib_none().0, control_rate.to_glib());
        }
    }

    fn set_name(&self, name: &str) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib::error::BoolError::from_glib(ffi::gst_object_set_name(self.to_glib_none().0, name.to_glib_none().0), "Failed to set object name")
        }
    }

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

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

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

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

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

    fn connect_property_name_notify<F: Fn(&Self) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe {
            let f: Box_<Box_<Fn(&Self) + Send + Sync + 'static>> = Box_::new(Box_::new(f));
            connect(self.to_glib_none().0, "notify::name",
                transmute(notify_name_trampoline::<Self> as usize), Box_::into_raw(f) as *mut _)
        }
    }

    fn connect_property_parent_notify<F: Fn(&Self) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe {
            let f: Box_<Box_<Fn(&Self) + Send + Sync + 'static>> = Box_::new(Box_::new(f));
            connect(self.to_glib_none().0, "notify::parent",
                transmute(notify_parent_trampoline::<Self> as usize), Box_::into_raw(f) as *mut _)
        }
    }
}

unsafe extern "C" fn notify_name_trampoline<P>(this: *mut ffi::GstObject, _param_spec: glib_ffi::gpointer, f: glib_ffi::gpointer)
where P: IsA<Object> {
    let f: &&(Fn(&P) + Send + Sync + 'static) = transmute(f);
    f(&Object::from_glib_borrow(this).downcast_unchecked())
}

unsafe extern "C" fn notify_parent_trampoline<P>(this: *mut ffi::GstObject, _param_spec: glib_ffi::gpointer, f: glib_ffi::gpointer)
where P: IsA<Object> {
    let f: &&(Fn(&P) + Send + Sync + 'static) = transmute(f);
    f(&Object::from_glib_borrow(this).downcast_unchecked())
}