gstreamer/device_provider.rs
1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::ffi::CStr;
4
5use glib::{prelude::*, translate::*};
6
7use crate::{ffi, DeviceProvider, Plugin, Rank};
8
9impl DeviceProvider {
10 /// Create a new device providerfactory capable of instantiating objects of the
11 /// `type_` and add the factory to `plugin`.
12 /// ## `plugin`
13 /// [`Plugin`][crate::Plugin] to register the device provider with, or [`None`] for
14 /// a static device provider.
15 /// ## `name`
16 /// name of device providers of this type
17 /// ## `rank`
18 /// rank of device provider (higher rank means more importance when autoplugging)
19 /// ## `type_`
20 /// GType of device provider to register
21 ///
22 /// # Returns
23 ///
24 /// [`true`], if the registering succeeded, [`false`] on error
25 #[doc(alias = "gst_device_provider_register")]
26 pub fn register(
27 plugin: Option<&Plugin>,
28 name: &str,
29 rank: Rank,
30 type_: glib::types::Type,
31 ) -> Result<(), glib::error::BoolError> {
32 skip_assert_initialized!();
33 unsafe {
34 glib::result_from_gboolean!(
35 ffi::gst_device_provider_register(
36 plugin.to_glib_none().0,
37 name.to_glib_none().0,
38 rank.into_glib() as u32,
39 type_.into_glib()
40 ),
41 "Failed to register device provider factory"
42 )
43 }
44 }
45}
46
47pub trait DeviceProviderExtManual: IsA<DeviceProvider> + 'static {
48 /// Get metadata with `key` in `self`.
49 /// ## `key`
50 /// the key to get
51 ///
52 /// # Returns
53 ///
54 /// the metadata for `key`.
55 #[doc(alias = "get_metadata")]
56 #[doc(alias = "gst_device_provider_class_get_metadata")]
57 fn metadata<'a>(&self, key: &str) -> Option<&'a str> {
58 unsafe {
59 self.unsafe_cast_ref::<DeviceProvider>()
60 .class()
61 .metadata(key)
62 }
63 }
64
65 /// Gets a list of devices that this provider understands. This may actually
66 /// probe the hardware if the provider is not currently started.
67 ///
68 /// If the provider has been started, this will returned the same [`Device`][crate::Device]
69 /// objedcts that have been returned by the `GST_MESSAGE_DEVICE_ADDED` messages.
70 ///
71 /// # Returns
72 ///
73 /// a `GList` of
74 /// [`Device`][crate::Device]
75 #[doc(alias = "gst_device_provider_get_devices")]
76 #[doc(alias = "get_devices")]
77 fn devices(&self) -> glib::List<crate::Device> {
78 unsafe {
79 FromGlibPtrContainer::from_glib_full(ffi::gst_device_provider_get_devices(
80 self.as_ref().to_glib_none().0,
81 ))
82 }
83 }
84}
85
86impl<O: IsA<DeviceProvider>> DeviceProviderExtManual for O {}
87
88pub unsafe trait DeviceProviderClassExt {
89 /// Get metadata with `key` in `self`.
90 /// ## `key`
91 /// the key to get
92 ///
93 /// # Returns
94 ///
95 /// the metadata for `key`.
96 #[doc(alias = "get_metadata")]
97 #[doc(alias = "gst_device_provider_class_get_metadata")]
98 fn metadata<'a>(&self, key: &str) -> Option<&'a str> {
99 unsafe {
100 let klass = self as *const _ as *const ffi::GstDeviceProviderClass;
101
102 let ptr = ffi::gst_device_provider_class_get_metadata(
103 mut_override(klass),
104 key.to_glib_none().0,
105 );
106
107 if ptr.is_null() {
108 None
109 } else {
110 Some(CStr::from_ptr(ptr).to_str().unwrap())
111 }
112 }
113 }
114}
115
116unsafe impl<T: IsA<DeviceProvider> + glib::object::IsClass> DeviceProviderClassExt
117 for glib::object::Class<T>
118{
119}