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
// 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;
use glib::object::Cast;
use glib::object::IsA;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use glib::GString;
use glib_sys;
use gst_sys;
use libc;
use std::boxed::Box as Box_;
use std::mem::transmute;
use Bus;
use Device;
use DeviceProviderFactory;
use Object;

glib_wrapper! {
    /// A `DeviceProvider` subclass is provided by a plugin that handles devices
    /// if there is a way to programmatically list connected devices. It can also
    /// optionally provide updates to the list of connected devices.
    ///
    /// Each `DeviceProvider` subclass is a singleton, a plugin should
    /// normally provide a single subclass for all devices.
    ///
    /// Applications would normally use a `DeviceMonitor` to monitor devices
    /// from all relevant providers.
    ///
    /// # Implements
    ///
    /// [`DeviceProviderExt`](trait.DeviceProviderExt.html), [`GstObjectExt`](trait.GstObjectExt.html), [`glib::object::ObjectExt`](../glib/object/trait.ObjectExt.html)
    pub struct DeviceProvider(Object<gst_sys::GstDeviceProvider, gst_sys::GstDeviceProviderClass, DeviceProviderClass>) @extends Object;

    match fn {
        get_type => || gst_sys::gst_device_provider_get_type(),
    }
}

unsafe impl Send for DeviceProvider {}
unsafe impl Sync for DeviceProvider {}

pub const NONE_DEVICE_PROVIDER: Option<&DeviceProvider> = None;

/// Trait containing all `DeviceProvider` methods.
///
/// # Implementors
///
/// [`DeviceProvider`](struct.DeviceProvider.html)
pub trait DeviceProviderExt: 'static {
    fn can_monitor(&self) -> bool;

    /// Posts a message on the provider's `Bus` to inform applications that
    /// a new device has been added.
    ///
    /// This is for use by subclasses.
    ///
    /// `device`'s reference count will be incremented, and any floating reference
    /// will be removed (see `Object::ref_sink`).
    /// ## `device`
    /// a `Device` that has been added
    fn device_add<P: IsA<Device>>(&self, device: &P);

    /// This function is used when `changed_device` was modified into its new form
    /// `device`. This will post a `DEVICE_CHANGED` message on the bus to let
    /// the application know that the device was modified. `Device` is immutable
    /// for MT. safety purposes so this is an "atomic" way of letting the application
    /// know when a device was modified.
    ///
    /// Feature: `v1_16`
    ///
    /// ## `device`
    /// the new version of `changed_device`
    /// ## `changed_device`
    /// the old version of the device that has been udpated
    #[cfg(any(feature = "v1_16", feature = "dox"))]
    fn device_changed<P: IsA<Device>, Q: IsA<Device>>(&self, device: &P, changed_device: &Q);

    /// Posts a message on the provider's `Bus` to inform applications that
    /// a device has been removed.
    ///
    /// This is for use by subclasses.
    /// ## `device`
    /// a `Device` that has been removed
    fn device_remove<P: IsA<Device>>(&self, device: &P);

    /// Gets the `Bus` of this `DeviceProvider`
    ///
    /// # Returns
    ///
    /// a `Bus`
    fn get_bus(&self) -> Bus;

    /// Gets a list of devices that this provider understands. This may actually
    /// probe the hardware if the provider is not currently started.
    ///
    /// # Returns
    ///
    /// a `glib::List` of
    ///  `Device`
    fn get_devices(&self) -> Vec<Device>;

    /// Retrieves the factory that was used to create this device provider.
    ///
    /// # Returns
    ///
    /// the `DeviceProviderFactory` used for
    ///  creating this device provider. no refcounting is needed.
    fn get_factory(&self) -> Option<DeviceProviderFactory>;

    /// Get the provider factory names of the `DeviceProvider` instances that
    /// are hidden by `self`.
    ///
    /// # Returns
    ///
    ///
    ///  a list of hidden providers factory names or `None` when
    ///  nothing is hidden by `self`. Free with g_strfreev.
    fn get_hidden_providers(&self) -> Vec<GString>;

    /// Make `self` hide the devices from the factory with `name`.
    ///
    /// This function is used when `self` will also provide the devices reported
    /// by provider factory `name`. A monitor should stop monitoring the
    /// device provider with `name` to avoid duplicate devices.
    /// ## `name`
    /// a provider factory name
    fn hide_provider(&self, name: &str);

    /// Starts providering the devices. This will cause `MessageType::DeviceAdded`
    /// and `MessageType::DeviceRemoved` messages to be posted on the provider's bus
    /// when devices are added or removed from the system.
    ///
    /// Since the `DeviceProvider` is a singleton,
    /// `DeviceProviderExt::start` may already have been called by another
    /// user of the object, `DeviceProviderExt::stop` needs to be called the same
    /// number of times.
    ///
    /// # Returns
    ///
    /// `true` if the device providering could be started
    fn start(&self) -> Result<(), glib::error::BoolError>;

    /// Decreases the use-count by one. If the use count reaches zero, this
    /// `DeviceProvider` will stop providering the devices. This needs to be
    /// called the same number of times that `DeviceProviderExt::start` was called.
    fn stop(&self);

    /// Make `self` unhide the devices from factory `name`.
    ///
    /// This function is used when `self` will no longer provide the devices
    /// reported by provider factory `name`. A monitor should start
    /// monitoring the devices from provider factory `name` in order to see
    /// all devices again.
    /// ## `name`
    /// a provider factory name
    fn unhide_provider(&self, name: &str);

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

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

impl<O: IsA<DeviceProvider>> DeviceProviderExt for O {
    fn can_monitor(&self) -> bool {
        unsafe {
            from_glib(gst_sys::gst_device_provider_can_monitor(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn device_add<P: IsA<Device>>(&self, device: &P) {
        unsafe {
            gst_sys::gst_device_provider_device_add(
                self.as_ref().to_glib_none().0,
                device.as_ref().to_glib_none().0,
            );
        }
    }

    #[cfg(any(feature = "v1_16", feature = "dox"))]
    fn device_changed<P: IsA<Device>, Q: IsA<Device>>(&self, device: &P, changed_device: &Q) {
        unsafe {
            gst_sys::gst_device_provider_device_changed(
                self.as_ref().to_glib_none().0,
                device.as_ref().to_glib_none().0,
                changed_device.as_ref().to_glib_none().0,
            );
        }
    }

    fn device_remove<P: IsA<Device>>(&self, device: &P) {
        unsafe {
            gst_sys::gst_device_provider_device_remove(
                self.as_ref().to_glib_none().0,
                device.as_ref().to_glib_none().0,
            );
        }
    }

    fn get_bus(&self) -> Bus {
        unsafe {
            from_glib_full(gst_sys::gst_device_provider_get_bus(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_devices(&self) -> Vec<Device> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(gst_sys::gst_device_provider_get_devices(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_factory(&self) -> Option<DeviceProviderFactory> {
        unsafe {
            from_glib_none(gst_sys::gst_device_provider_get_factory(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_hidden_providers(&self) -> Vec<GString> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(gst_sys::gst_device_provider_get_hidden_providers(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn hide_provider(&self, name: &str) {
        unsafe {
            gst_sys::gst_device_provider_hide_provider(
                self.as_ref().to_glib_none().0,
                name.to_glib_none().0,
            );
        }
    }

    fn start(&self) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib_result_from_gboolean!(
                gst_sys::gst_device_provider_start(self.as_ref().to_glib_none().0),
                "Failed to start"
            )
        }
    }

    fn stop(&self) {
        unsafe {
            gst_sys::gst_device_provider_stop(self.as_ref().to_glib_none().0);
        }
    }

    fn unhide_provider(&self, name: &str) {
        unsafe {
            gst_sys::gst_device_provider_unhide_provider(
                self.as_ref().to_glib_none().0,
                name.to_glib_none().0,
            );
        }
    }

    fn connect_provider_hidden<F: Fn(&Self, &str) + Send + Sync + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn provider_hidden_trampoline<
            P,
            F: Fn(&P, &str) + Send + Sync + 'static,
        >(
            this: *mut gst_sys::GstDeviceProvider,
            object: *mut libc::c_char,
            f: glib_sys::gpointer,
        ) where
            P: IsA<DeviceProvider>,
        {
            let f: &F = &*(f as *const F);
            f(
                &DeviceProvider::from_glib_borrow(this).unsafe_cast(),
                &GString::from_glib_borrow(object),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"provider-hidden\0".as_ptr() as *const _,
                Some(transmute(provider_hidden_trampoline::<Self, F> as usize)),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_provider_unhidden<F: Fn(&Self, &str) + Send + Sync + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn provider_unhidden_trampoline<
            P,
            F: Fn(&P, &str) + Send + Sync + 'static,
        >(
            this: *mut gst_sys::GstDeviceProvider,
            object: *mut libc::c_char,
            f: glib_sys::gpointer,
        ) where
            P: IsA<DeviceProvider>,
        {
            let f: &F = &*(f as *const F);
            f(
                &DeviceProvider::from_glib_borrow(this).unsafe_cast(),
                &GString::from_glib_borrow(object),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"provider-unhidden\0".as_ptr() as *const _,
                Some(transmute(provider_unhidden_trampoline::<Self, F> as usize)),
                Box_::into_raw(f),
            )
        }
    }
}