Skip to main content

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