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
// 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 Bus;
use Device;
use DeviceProviderFactory;
use Object;
use Plugin;
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 libc;
use std::boxed::Box as Box_;
use std::mem;
use std::mem::transmute;
use std::ptr;

glib_wrapper! {
    /// A `DeviceProvider` subclass is provided by a plugin that handles devices
    /// if there is a way to programatically 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<ffi::GstDeviceProvider, ffi::GstDeviceProviderClass>): Object;

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

impl DeviceProvider {
    /// Create a new device providerfactory capable of instantiating objects of the
    /// `type_` and add the factory to `plugin`.
    /// ## `plugin`
    /// `Plugin` to register the device provider with, or `None` for
    ///  a static device provider.
    /// ## `name`
    /// name of device providers of this type
    /// ## `rank`
    /// rank of device provider (higher rank means more importance when autoplugging)
    /// ## `type_`
    /// GType of device provider to register
    ///
    /// # Returns
    ///
    /// `true`, if the registering succeeded, `false` on error
    pub fn register<'a, P: Into<Option<&'a Plugin>>>(plugin: P, name: &str, rank: u32, type_: glib::types::Type) -> bool {
        assert_initialized_main_thread!();
        let plugin = plugin.into();
        let plugin = plugin.to_glib_none();
        unsafe {
            from_glib(ffi::gst_device_provider_register(plugin.0, name.to_glib_none().0, rank, type_.to_glib()))
        }
    }
}

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

/// Trait containing all `DeviceProvider` methods.
///
/// # Implementors
///
/// [`DeviceProvider`](struct.DeviceProvider.html)
pub trait DeviceProviderExt {
    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(&self, device: &Device);

    /// 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(&self, device: &Device);

    /// 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<String>;

    /// 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> + IsA<glib::object::Object>> DeviceProviderExt for O {
    fn can_monitor(&self) -> bool {
        unsafe {
            from_glib(ffi::gst_device_provider_can_monitor(self.to_glib_none().0))
        }
    }

    fn device_add(&self, device: &Device) {
        unsafe {
            ffi::gst_device_provider_device_add(self.to_glib_none().0, device.to_glib_none().0);
        }
    }

    fn device_remove(&self, device: &Device) {
        unsafe {
            ffi::gst_device_provider_device_remove(self.to_glib_none().0, device.to_glib_none().0);
        }
    }

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

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

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

    fn get_hidden_providers(&self) -> Vec<String> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(ffi::gst_device_provider_get_hidden_providers(self.to_glib_none().0))
        }
    }

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

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

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

    fn unhide_provider(&self, name: &str) {
        unsafe {
            ffi::gst_device_provider_unhide_provider(self.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 {
            let f: Box_<Box_<Fn(&Self, &str) + Send + Sync + 'static>> = Box_::new(Box_::new(f));
            connect(self.to_glib_none().0, "provider-hidden",
                transmute(provider_hidden_trampoline::<Self> as usize), Box_::into_raw(f) as *mut _)
        }
    }

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

unsafe extern "C" fn provider_hidden_trampoline<P>(this: *mut ffi::GstDeviceProvider, object: *mut libc::c_char, f: glib_ffi::gpointer)
where P: IsA<DeviceProvider> {
    let f: &&(Fn(&P, &str) + Send + Sync + 'static) = transmute(f);
    f(&DeviceProvider::from_glib_borrow(this).downcast_unchecked(), &String::from_glib_none(object))
}

unsafe extern "C" fn provider_unhidden_trampoline<P>(this: *mut ffi::GstDeviceProvider, object: *mut libc::c_char, f: glib_ffi::gpointer)
where P: IsA<DeviceProvider> {
    let f: &&(Fn(&P, &str) + Send + Sync + 'static) = transmute(f);
    f(&DeviceProvider::from_glib_borrow(this).downcast_unchecked(), &String::from_glib_none(object))
}