gstreamer/
device_monitor.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::num::NonZeroU32;
4
5use glib::{prelude::*, translate::*};
6
7use crate::{ffi, Caps, DeviceMonitor};
8
9#[derive(Debug, PartialEq, Eq)]
10pub struct DeviceMonitorFilterId(NonZeroU32);
11
12impl IntoGlib for DeviceMonitorFilterId {
13    type GlibType = libc::c_uint;
14
15    #[inline]
16    fn into_glib(self) -> libc::c_uint {
17        self.0.get()
18    }
19}
20
21impl FromGlib<libc::c_uint> for DeviceMonitorFilterId {
22    #[inline]
23    unsafe fn from_glib(val: libc::c_uint) -> DeviceMonitorFilterId {
24        skip_assert_initialized!();
25        debug_assert_ne!(val, 0);
26        DeviceMonitorFilterId(NonZeroU32::new_unchecked(val))
27    }
28}
29mod sealed {
30    pub trait Sealed {}
31    impl<T: super::IsA<super::DeviceMonitor>> Sealed for T {}
32}
33
34pub trait DeviceMonitorExtManual: sealed::Sealed + IsA<DeviceMonitor> + 'static {
35    /// Adds a filter for which [`Device`][crate::Device] will be monitored, any device that matches
36    /// all these classes and the [`Caps`][crate::Caps] will be returned.
37    ///
38    /// If this function is called multiple times to add more filters, each will be
39    /// matched independently. That is, adding more filters will not further restrict
40    /// what devices are matched.
41    ///
42    /// The [`Caps`][crate::Caps] supported by the device as returned by [`DeviceExt::caps()`][crate::prelude::DeviceExt::caps()] are
43    /// not intersected with caps filters added using this function.
44    ///
45    /// Filters must be added before the [`DeviceMonitor`][crate::DeviceMonitor] is started.
46    /// ## `classes`
47    /// device classes to use as filter or [`None`] for any class
48    /// ## `caps`
49    /// the [`Caps`][crate::Caps] to filter or [`None`] for ANY
50    ///
51    /// # Returns
52    ///
53    /// The id of the new filter or 0 if no provider matched the filter's
54    ///  classes.
55    #[doc(alias = "gst_device_monitor_add_filter")]
56    fn add_filter(
57        &self,
58        classes: Option<&str>,
59        caps: Option<&Caps>,
60    ) -> Option<DeviceMonitorFilterId> {
61        let id = unsafe {
62            ffi::gst_device_monitor_add_filter(
63                self.as_ref().to_glib_none().0,
64                classes.to_glib_none().0,
65                caps.to_glib_none().0,
66            )
67        };
68
69        if id == 0 {
70            None
71        } else {
72            Some(unsafe { from_glib(id) })
73        }
74    }
75
76    /// Removes a filter from the [`DeviceMonitor`][crate::DeviceMonitor] using the id that was returned
77    /// by [`add_filter()`][Self::add_filter()].
78    /// ## `filter_id`
79    /// the id of the filter
80    ///
81    /// # Returns
82    ///
83    /// [`true`] of the filter id was valid, [`false`] otherwise
84    #[doc(alias = "gst_device_monitor_remove_filter")]
85    fn remove_filter(
86        &self,
87        filter_id: DeviceMonitorFilterId,
88    ) -> Result<(), glib::error::BoolError> {
89        unsafe {
90            glib::result_from_gboolean!(
91                ffi::gst_device_monitor_remove_filter(
92                    self.as_ref().to_glib_none().0,
93                    filter_id.into_glib()
94                ),
95                "Failed to remove the filter"
96            )
97        }
98    }
99
100    /// Gets a list of devices from all of the relevant monitors. This may actually
101    /// probe the hardware if the monitor is not currently started.
102    ///
103    /// # Returns
104    ///
105    /// a `GList` of
106    ///  [`Device`][crate::Device]
107    #[doc(alias = "gst_device_monitor_get_devices")]
108    #[doc(alias = "get_devices")]
109    fn devices(&self) -> glib::List<crate::Device> {
110        unsafe {
111            FromGlibPtrContainer::from_glib_full(ffi::gst_device_monitor_get_devices(
112                self.as_ref().to_glib_none().0,
113            ))
114        }
115    }
116}
117
118impl<O: IsA<DeviceMonitor>> DeviceMonitorExtManual for O {}