gstreamer/subclass/
device_provider.rs1use std::borrow::Cow;
4
5use glib::{prelude::*, subclass::prelude::*, translate::*};
6
7use super::prelude::*;
8use crate::{ffi, Device, DeviceProvider, LoggableError};
9
10#[derive(Debug, Clone)]
11pub struct DeviceProviderMetadata {
12 long_name: Cow<'static, str>,
13 classification: Cow<'static, str>,
14 description: Cow<'static, str>,
15 author: Cow<'static, str>,
16 additional: Cow<'static, [(Cow<'static, str>, Cow<'static, str>)]>,
17}
18
19impl DeviceProviderMetadata {
20 pub fn new(long_name: &str, classification: &str, description: &str, author: &str) -> Self {
21 Self {
22 long_name: Cow::Owned(long_name.into()),
23 classification: Cow::Owned(classification.into()),
24 description: Cow::Owned(description.into()),
25 author: Cow::Owned(author.into()),
26 additional: Cow::Borrowed(&[]),
27 }
28 }
29
30 pub fn with_additional(
31 long_name: &str,
32 classification: &str,
33 description: &str,
34 author: &str,
35 additional: &[(&str, &str)],
36 ) -> Self {
37 Self {
38 long_name: Cow::Owned(long_name.into()),
39 classification: Cow::Owned(classification.into()),
40 description: Cow::Owned(description.into()),
41 author: Cow::Owned(author.into()),
42 additional: additional
43 .iter()
44 .copied()
45 .map(|(key, value)| (Cow::Owned(key.into()), Cow::Owned(value.into())))
46 .collect(),
47 }
48 }
49
50 pub const fn with_cow(
51 long_name: Cow<'static, str>,
52 classification: Cow<'static, str>,
53 description: Cow<'static, str>,
54 author: Cow<'static, str>,
55 additional: Cow<'static, [(Cow<'static, str>, Cow<'static, str>)]>,
56 ) -> Self {
57 Self {
58 long_name,
59 classification,
60 description,
61 author,
62 additional,
63 }
64 }
65}
66
67pub trait DeviceProviderImpl: GstObjectImpl + ObjectSubclass<Type: IsA<DeviceProvider>> {
68 fn metadata() -> Option<&'static DeviceProviderMetadata> {
69 None
70 }
71
72 fn probe(&self) -> Vec<Device> {
73 self.parent_probe()
74 }
75
76 fn start(&self) -> Result<(), LoggableError> {
77 self.parent_start()
78 }
79
80 fn stop(&self) {
81 self.parent_stop()
82 }
83}
84
85pub trait DeviceProviderImplExt: DeviceProviderImpl {
86 fn parent_probe(&self) -> Vec<Device> {
87 unsafe {
88 let data = Self::type_data();
89 let parent_class = data.as_ref().parent_class() as *mut ffi::GstDeviceProviderClass;
90 if let Some(f) = (*parent_class).probe {
91 FromGlibPtrContainer::from_glib_full(f(self
92 .obj()
93 .unsafe_cast_ref::<DeviceProvider>()
94 .to_glib_none()
95 .0))
96 } else {
97 Vec::new()
98 }
99 }
100 }
101
102 fn parent_start(&self) -> Result<(), LoggableError> {
103 unsafe {
104 let data = Self::type_data();
105 let parent_class = data.as_ref().parent_class() as *mut ffi::GstDeviceProviderClass;
106 let f = (*parent_class).start.ok_or_else(|| {
107 loggable_error!(crate::CAT_RUST, "Parent function `start` is not defined")
108 })?;
109 result_from_gboolean!(
110 f(self
111 .obj()
112 .unsafe_cast_ref::<DeviceProvider>()
113 .to_glib_none()
114 .0),
115 crate::CAT_RUST,
116 "Failed to start the device provider using the parent function"
117 )
118 }
119 }
120
121 fn parent_stop(&self) {
122 unsafe {
123 let data = Self::type_data();
124 let parent_class = data.as_ref().parent_class() as *mut ffi::GstDeviceProviderClass;
125 if let Some(f) = (*parent_class).stop {
126 f(self
127 .obj()
128 .unsafe_cast_ref::<DeviceProvider>()
129 .to_glib_none()
130 .0);
131 }
132 }
133 }
134}
135
136impl<T: DeviceProviderImpl> DeviceProviderImplExt for T {}
137
138unsafe impl<T: DeviceProviderImpl> IsSubclassable<T> for DeviceProvider {
139 fn class_init(klass: &mut glib::Class<Self>) {
140 Self::parent_class_init::<T>(klass);
141 let klass = klass.as_mut();
142 klass.probe = Some(device_provider_probe::<T>);
143 klass.start = Some(device_provider_start::<T>);
144 klass.stop = Some(device_provider_stop::<T>);
145
146 unsafe {
147 if let Some(metadata) = T::metadata() {
148 ffi::gst_device_provider_class_set_metadata(
149 klass,
150 metadata.long_name.to_glib_none().0,
151 metadata.classification.to_glib_none().0,
152 metadata.description.to_glib_none().0,
153 metadata.author.to_glib_none().0,
154 );
155
156 for (key, value) in metadata.additional.iter() {
157 ffi::gst_device_provider_class_add_metadata(
158 klass,
159 key.to_glib_none().0,
160 value.to_glib_none().0,
161 );
162 }
163 }
164 }
165 }
166}
167
168unsafe extern "C" fn device_provider_probe<T: DeviceProviderImpl>(
169 ptr: *mut ffi::GstDeviceProvider,
170) -> *mut glib::ffi::GList {
171 let instance = &*(ptr as *mut T::Instance);
172 let imp = instance.imp();
173
174 imp.probe().to_glib_full()
175}
176
177unsafe extern "C" fn device_provider_start<T: DeviceProviderImpl>(
178 ptr: *mut ffi::GstDeviceProvider,
179) -> glib::ffi::gboolean {
180 let instance = &*(ptr as *mut T::Instance);
181 let imp = instance.imp();
182
183 match imp.start() {
184 Ok(()) => true,
185 Err(err) => {
186 err.log_with_imp(imp);
187 false
188 }
189 }
190 .into_glib()
191}
192
193unsafe extern "C" fn device_provider_stop<T: DeviceProviderImpl>(ptr: *mut ffi::GstDeviceProvider) {
194 let instance = &*(ptr as *mut T::Instance);
195 let imp = instance.imp();
196
197 imp.stop();
198}