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
// 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::Bus;
use crate::Device;
use crate::DeviceProviderFactory;
use crate::Object;
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::mem::transmute;

glib::wrapper! {
    /// A [`crate::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 [`crate::DeviceProvider`] subclass is a singleton, a plugin should
    /// normally provide a single subclass for all devices.
    ///
    /// Applications would normally use a [`crate::DeviceMonitor`] to monitor devices
    /// from all relevant providers.
    ///
    /// This is an Abstract Base Class, you cannot instantiate it.
    ///
    /// # Implements
    ///
    /// [`trait@crate::prelude::DeviceProviderExt`], [`trait@crate::prelude::GstObjectExt`], [`trait@glib::object::ObjectExt`], [`trait@crate::prelude::DeviceProviderExtManual`]
    pub struct DeviceProvider(Object<ffi::GstDeviceProvider, ffi::GstDeviceProviderClass>) @extends Object;

    match fn {
        type_ => || ffi::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
///
/// [`struct@crate::DeviceProvider`]
pub trait DeviceProviderExt: 'static {
    #[doc(alias = "gst_device_provider_can_monitor")]
    fn can_monitor(&self) -> bool;

    /// Posts a message on the provider's [`crate::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 [`crate::Object::ref_sink()`]).
    /// ## `device`
    /// a [`crate::Device`] that has been added
    #[doc(alias = "gst_device_provider_device_add")]
    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. [`crate::Device`] is immutable
    /// for MT. safety purposes so this is an "atomic" way of letting the application
    /// know when a device was modified.
    /// ## `device`
    /// the new version of `changed_device`
    /// ## `changed_device`
    /// the old version of the device that has been updated
    #[cfg(any(feature = "v1_16", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_16")))]
    #[doc(alias = "gst_device_provider_device_changed")]
    fn device_changed<P: IsA<Device>, Q: IsA<Device>>(&self, device: &P, changed_device: &Q);

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

    /// Gets the [`crate::Bus`] of this [`crate::DeviceProvider`]
    ///
    /// # Returns
    ///
    /// a [`crate::Bus`]
    #[doc(alias = "gst_device_provider_get_bus")]
    #[doc(alias = "get_bus")]
    fn 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.
    ///
    /// If the provider has been started, this will returned the same [`crate::Device`]
    /// objedcts that have been returned by the [`crate::MessageType::DeviceAdded`] messages.
    ///
    /// # Returns
    ///
    /// a [`crate::glib::List`] of
    ///  [`crate::Device`]
    #[doc(alias = "gst_device_provider_get_devices")]
    #[doc(alias = "get_devices")]
    fn devices(&self) -> Vec<Device>;

    /// Retrieves the factory that was used to create this device provider.
    ///
    /// # Returns
    ///
    /// the [`crate::DeviceProviderFactory`] used for
    ///  creating this device provider. no refcounting is needed.
    #[doc(alias = "gst_device_provider_get_factory")]
    #[doc(alias = "get_factory")]
    fn factory(&self) -> Option<DeviceProviderFactory>;

    /// Get the provider factory names of the [`crate::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.
    #[doc(alias = "gst_device_provider_get_hidden_providers")]
    #[doc(alias = "get_hidden_providers")]
    fn hidden_providers(&self) -> Vec<glib::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
    #[doc(alias = "gst_device_provider_hide_provider")]
    fn hide_provider(&self, name: &str);

    /// Starts providering the devices. This will cause [`crate::MessageType::DeviceAdded`]
    /// and [`crate::MessageType::DeviceRemoved`] messages to be posted on the provider's bus
    /// when devices are added or removed from the system.
    ///
    /// Since the [`crate::DeviceProvider`] is a singleton,
    /// [`Self::start()`] may already have been called by another
    /// user of the object, [`Self::stop()`] needs to be called the same
    /// number of times.
    ///
    /// After this function has been called, [`Self::get_devices()`] will
    /// return the same objects that have been received from the
    /// [`crate::MessageType::DeviceAdded`] messages and will no longer probe.
    ///
    /// # Returns
    ///
    /// [`true`] if the device providering could be started
    #[doc(alias = "gst_device_provider_start")]
    fn start(&self) -> Result<(), glib::error::BoolError>;

    /// Decreases the use-count by one. If the use count reaches zero, this
    /// [`crate::DeviceProvider`] will stop providering the devices. This needs to be
    /// called the same number of times that [`Self::start()`] was called.
    #[doc(alias = "gst_device_provider_stop")]
    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
    #[doc(alias = "gst_device_provider_unhide_provider")]
    fn unhide_provider(&self, name: &str);

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

    #[doc(alias = "provider-unhidden")]
    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(ffi::gst_device_provider_can_monitor(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn device_add<P: IsA<Device>>(&self, device: &P) {
        unsafe {
            ffi::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"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_16")))]
    fn device_changed<P: IsA<Device>, Q: IsA<Device>>(&self, device: &P, changed_device: &Q) {
        unsafe {
            ffi::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 {
            ffi::gst_device_provider_device_remove(
                self.as_ref().to_glib_none().0,
                device.as_ref().to_glib_none().0,
            );
        }
    }

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

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

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

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

    fn hide_provider(&self, name: &str) {
        unsafe {
            ffi::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!(
                ffi::gst_device_provider_start(self.as_ref().to_glib_none().0),
                "Failed to start"
            )
        }
    }

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

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

    #[doc(alias = "provider-hidden")]
    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 ffi::GstDeviceProvider,
            object: *mut libc::c_char,
            f: glib::ffi::gpointer,
        ) where
            P: IsA<DeviceProvider>,
        {
            let f: &F = &*(f as *const F);
            f(
                &DeviceProvider::from_glib_borrow(this).unsafe_cast_ref(),
                &glib::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::<_, unsafe extern "C" fn()>(
                    provider_hidden_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "provider-unhidden")]
    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 ffi::GstDeviceProvider,
            object: *mut libc::c_char,
            f: glib::ffi::gpointer,
        ) where
            P: IsA<DeviceProvider>,
        {
            let f: &F = &*(f as *const F);
            f(
                &DeviceProvider::from_glib_borrow(this).unsafe_cast_ref(),
                &glib::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::<_, unsafe extern "C" fn()>(
                    provider_unhidden_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}