2020-12-15 10:53:31 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2019-09-11 19:59:49 +00:00
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
use glib::prelude::*;
|
2019-09-11 19:59:49 +00:00
|
|
|
use glib::subclass::prelude::*;
|
2020-11-14 15:34:41 +00:00
|
|
|
use glib::translate::*;
|
2019-09-11 19:59:49 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::Device;
|
|
|
|
use crate::DeviceProvider;
|
|
|
|
use crate::LoggableError;
|
2019-09-11 19:59:49 +00:00
|
|
|
|
2020-10-20 20:55:20 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct DeviceProviderMetadata {
|
|
|
|
long_name: String,
|
|
|
|
classification: String,
|
|
|
|
description: String,
|
|
|
|
author: String,
|
|
|
|
additional: Vec<(String, String)>,
|
|
|
|
}
|
|
|
|
|
2020-07-25 08:02:04 +00:00
|
|
|
pub trait DeviceProviderImpl: DeviceProviderImplExt + ObjectImpl + Send + Sync {
|
2020-10-20 20:55:20 +00:00
|
|
|
fn metadata() -> Option<&'static DeviceProviderMetadata> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
fn probe(&self, device_provider: &Self::Type) -> Vec<Device> {
|
2019-09-11 19:59:49 +00:00
|
|
|
self.parent_probe(device_provider)
|
|
|
|
}
|
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
fn start(&self, device_provider: &Self::Type) -> Result<(), LoggableError> {
|
2019-09-11 19:59:49 +00:00
|
|
|
self.parent_start(device_provider)
|
|
|
|
}
|
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
fn stop(&self, device_provider: &Self::Type) {
|
2019-09-11 19:59:49 +00:00
|
|
|
self.parent_stop(device_provider)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
pub trait DeviceProviderImplExt: ObjectSubclass {
|
|
|
|
fn parent_probe(&self, device_provider: &Self::Type) -> Vec<Device>;
|
2019-09-11 19:59:49 +00:00
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
fn parent_start(&self, device_provider: &Self::Type) -> Result<(), LoggableError>;
|
2019-09-11 19:59:49 +00:00
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
fn parent_stop(&self, device_provider: &Self::Type);
|
2019-09-11 19:59:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 08:02:04 +00:00
|
|
|
impl<T: DeviceProviderImpl> DeviceProviderImplExt for T {
|
2020-11-14 15:34:41 +00:00
|
|
|
fn parent_probe(&self, device_provider: &Self::Type) -> Vec<Device> {
|
2019-09-11 19:59:49 +00:00
|
|
|
unsafe {
|
2021-04-29 20:06:41 +00:00
|
|
|
let data = Self::type_data();
|
2021-04-11 19:39:50 +00:00
|
|
|
let parent_class = data.as_ref().parent_class() as *mut ffi::GstDeviceProviderClass;
|
2019-09-11 19:59:49 +00:00
|
|
|
if let Some(f) = (*parent_class).probe {
|
2020-11-14 15:34:41 +00:00
|
|
|
FromGlibPtrContainer::from_glib_full(f(device_provider
|
|
|
|
.unsafe_cast_ref::<DeviceProvider>()
|
|
|
|
.to_glib_none()
|
|
|
|
.0))
|
2019-09-11 19:59:49 +00:00
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
fn parent_start(&self, device_provider: &Self::Type) -> Result<(), LoggableError> {
|
2019-09-11 19:59:49 +00:00
|
|
|
unsafe {
|
2021-04-29 20:06:41 +00:00
|
|
|
let data = Self::type_data();
|
2021-04-11 19:39:50 +00:00
|
|
|
let parent_class = data.as_ref().parent_class() as *mut ffi::GstDeviceProviderClass;
|
2019-09-11 19:59:49 +00:00
|
|
|
let f = (*parent_class).start.ok_or_else(|| {
|
2020-12-20 15:09:22 +00:00
|
|
|
loggable_error!(crate::CAT_RUST, "Parent function `start` is not defined")
|
2019-09-11 19:59:49 +00:00
|
|
|
})?;
|
2020-12-20 15:09:22 +00:00
|
|
|
result_from_gboolean!(
|
2020-11-14 15:34:41 +00:00
|
|
|
f(device_provider
|
|
|
|
.unsafe_cast_ref::<DeviceProvider>()
|
|
|
|
.to_glib_none()
|
|
|
|
.0),
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::CAT_RUST,
|
2019-09-11 19:59:49 +00:00
|
|
|
"Failed to start the device provider using the parent function"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
fn parent_stop(&self, device_provider: &Self::Type) {
|
2019-09-11 19:59:49 +00:00
|
|
|
unsafe {
|
2021-04-29 20:06:41 +00:00
|
|
|
let data = Self::type_data();
|
2021-04-11 19:39:50 +00:00
|
|
|
let parent_class = data.as_ref().parent_class() as *mut ffi::GstDeviceProviderClass;
|
2019-09-11 19:59:49 +00:00
|
|
|
if let Some(f) = (*parent_class).stop {
|
2020-11-14 15:34:41 +00:00
|
|
|
f(device_provider
|
|
|
|
.unsafe_cast_ref::<DeviceProvider>()
|
|
|
|
.to_glib_none()
|
|
|
|
.0);
|
2019-09-11 19:59:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 13:18:19 +00:00
|
|
|
unsafe impl<T: DeviceProviderImpl> IsSubclassable<T> for DeviceProvider {
|
2021-03-08 10:06:56 +00:00
|
|
|
fn class_init(klass: &mut glib::Class<Self>) {
|
|
|
|
<glib::Object as IsSubclassable<T>>::class_init(klass);
|
2020-11-05 17:07:31 +00:00
|
|
|
let klass = klass.as_mut();
|
|
|
|
klass.probe = Some(device_provider_probe::<T>);
|
|
|
|
klass.start = Some(device_provider_start::<T>);
|
|
|
|
klass.stop = Some(device_provider_stop::<T>);
|
2020-10-20 20:55:20 +00:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
if let Some(metadata) = T::metadata() {
|
|
|
|
ffi::gst_device_provider_class_set_metadata(
|
|
|
|
klass,
|
|
|
|
metadata.long_name.to_glib_none().0,
|
|
|
|
metadata.classification.to_glib_none().0,
|
|
|
|
metadata.description.to_glib_none().0,
|
|
|
|
metadata.author.to_glib_none().0,
|
|
|
|
);
|
|
|
|
|
|
|
|
for (key, value) in &metadata.additional {
|
|
|
|
ffi::gst_device_provider_class_add_metadata(
|
|
|
|
klass,
|
|
|
|
key.to_glib_none().0,
|
|
|
|
value.to_glib_none().0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-11 19:59:49 +00:00
|
|
|
}
|
2021-03-09 11:50:32 +00:00
|
|
|
|
|
|
|
fn instance_init(instance: &mut glib::subclass::InitializingObject<T>) {
|
|
|
|
<glib::Object as IsSubclassable<T>>::instance_init(instance);
|
|
|
|
}
|
2019-09-11 19:59:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 08:02:04 +00:00
|
|
|
unsafe extern "C" fn device_provider_probe<T: DeviceProviderImpl>(
|
2020-11-21 13:46:48 +00:00
|
|
|
ptr: *mut ffi::GstDeviceProvider,
|
|
|
|
) -> *mut glib::ffi::GList {
|
2019-09-11 19:59:49 +00:00
|
|
|
let instance = &*(ptr as *mut T::Instance);
|
2021-04-11 19:39:50 +00:00
|
|
|
let imp = instance.impl_();
|
2020-04-05 14:52:56 +00:00
|
|
|
let wrap: Borrowed<DeviceProvider> = from_glib_borrow(ptr);
|
2019-09-11 19:59:49 +00:00
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
imp.probe(wrap.unsafe_cast_ref()).to_glib_full()
|
2019-09-11 19:59:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 08:02:04 +00:00
|
|
|
unsafe extern "C" fn device_provider_start<T: DeviceProviderImpl>(
|
2020-11-21 13:46:48 +00:00
|
|
|
ptr: *mut ffi::GstDeviceProvider,
|
|
|
|
) -> glib::ffi::gboolean {
|
2019-09-11 19:59:49 +00:00
|
|
|
let instance = &*(ptr as *mut T::Instance);
|
2021-04-11 19:39:50 +00:00
|
|
|
let imp = instance.impl_();
|
2020-04-05 14:52:56 +00:00
|
|
|
let wrap: Borrowed<DeviceProvider> = from_glib_borrow(ptr);
|
2019-09-11 19:59:49 +00:00
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
match imp.start(wrap.unsafe_cast_ref()) {
|
2019-09-11 19:59:49 +00:00
|
|
|
Ok(()) => true,
|
|
|
|
Err(err) => {
|
2020-04-05 14:52:56 +00:00
|
|
|
err.log_with_object(&*wrap);
|
2019-09-11 19:59:49 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2021-04-27 15:15:46 +00:00
|
|
|
.into_glib()
|
2019-09-11 19:59:49 +00:00
|
|
|
}
|
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe extern "C" fn device_provider_stop<T: DeviceProviderImpl>(ptr: *mut ffi::GstDeviceProvider) {
|
2019-09-11 19:59:49 +00:00
|
|
|
let instance = &*(ptr as *mut T::Instance);
|
2021-04-11 19:39:50 +00:00
|
|
|
let imp = instance.impl_();
|
2020-04-05 14:52:56 +00:00
|
|
|
let wrap: Borrowed<DeviceProvider> = from_glib_borrow(ptr);
|
2019-09-11 19:59:49 +00:00
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
imp.stop(wrap.unsafe_cast_ref());
|
2019-09-11 19:59:49 +00:00
|
|
|
}
|