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