2019-09-11 19:59:49 +00:00
|
|
|
// Copyright (C) 2019 Sebastian Dröge <sebastian@centricular.com>
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use glib_sys;
|
|
|
|
use gst_sys;
|
|
|
|
|
|
|
|
use glib::translate::*;
|
|
|
|
|
|
|
|
use glib::subclass::prelude::*;
|
|
|
|
|
|
|
|
use Device;
|
|
|
|
use DeviceProvider;
|
|
|
|
use LoggableError;
|
|
|
|
|
2020-07-25 08:02:04 +00:00
|
|
|
pub trait DeviceProviderImpl: DeviceProviderImplExt + ObjectImpl + Send + Sync {
|
2019-09-11 19:59:49 +00:00
|
|
|
fn probe(&self, device_provider: &DeviceProvider) -> Vec<Device> {
|
|
|
|
self.parent_probe(device_provider)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start(&self, device_provider: &DeviceProvider) -> Result<(), LoggableError> {
|
|
|
|
self.parent_start(device_provider)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stop(&self, device_provider: &DeviceProvider) {
|
|
|
|
self.parent_stop(device_provider)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait DeviceProviderImplExt {
|
|
|
|
fn parent_probe(&self, device_provider: &DeviceProvider) -> Vec<Device>;
|
|
|
|
|
|
|
|
fn parent_start(&self, device_provider: &DeviceProvider) -> Result<(), LoggableError>;
|
|
|
|
|
|
|
|
fn parent_stop(&self, device_provider: &DeviceProvider);
|
|
|
|
}
|
|
|
|
|
2020-07-25 08:02:04 +00:00
|
|
|
impl<T: DeviceProviderImpl> DeviceProviderImplExt for T {
|
2019-09-11 19:59:49 +00:00
|
|
|
fn parent_probe(&self, device_provider: &DeviceProvider) -> Vec<Device> {
|
|
|
|
unsafe {
|
2020-07-25 08:02:04 +00:00
|
|
|
let data = T::type_data();
|
2019-09-11 19:59:49 +00:00
|
|
|
let parent_class =
|
|
|
|
data.as_ref().get_parent_class() as *mut gst_sys::GstDeviceProviderClass;
|
|
|
|
if let Some(f) = (*parent_class).probe {
|
|
|
|
FromGlibPtrContainer::from_glib_full(f(device_provider.to_glib_none().0))
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parent_start(&self, device_provider: &DeviceProvider) -> Result<(), LoggableError> {
|
|
|
|
unsafe {
|
2020-07-25 08:02:04 +00:00
|
|
|
let data = T::type_data();
|
2019-09-11 19:59:49 +00:00
|
|
|
let parent_class =
|
|
|
|
data.as_ref().get_parent_class() as *mut gst_sys::GstDeviceProviderClass;
|
|
|
|
let f = (*parent_class).start.ok_or_else(|| {
|
|
|
|
gst_loggable_error!(::CAT_RUST, "Parent function `start` is not defined")
|
|
|
|
})?;
|
|
|
|
gst_result_from_gboolean!(
|
|
|
|
f(device_provider.to_glib_none().0),
|
|
|
|
::CAT_RUST,
|
|
|
|
"Failed to start the device provider using the parent function"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parent_stop(&self, device_provider: &DeviceProvider) {
|
|
|
|
unsafe {
|
2020-07-25 08:02:04 +00:00
|
|
|
let data = T::type_data();
|
2019-09-11 19:59:49 +00:00
|
|
|
let parent_class =
|
|
|
|
data.as_ref().get_parent_class() as *mut gst_sys::GstDeviceProviderClass;
|
|
|
|
if let Some(f) = (*parent_class).stop {
|
|
|
|
f(device_provider.to_glib_none().0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe trait DeviceProviderClassSubclassExt: Sized + 'static {
|
|
|
|
fn set_metadata(
|
|
|
|
&mut self,
|
|
|
|
long_name: &str,
|
|
|
|
classification: &str,
|
|
|
|
description: &str,
|
|
|
|
author: &str,
|
|
|
|
) {
|
|
|
|
unsafe {
|
|
|
|
gst_sys::gst_device_provider_class_set_metadata(
|
|
|
|
self as *mut Self as *mut gst_sys::GstDeviceProviderClass,
|
|
|
|
long_name.to_glib_none().0,
|
|
|
|
classification.to_glib_none().0,
|
|
|
|
description.to_glib_none().0,
|
|
|
|
author.to_glib_none().0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_metadata(&mut self, key: &str, value: &str) {
|
|
|
|
unsafe {
|
|
|
|
gst_sys::gst_device_provider_class_add_metadata(
|
|
|
|
self as *mut Self as *mut gst_sys::GstDeviceProviderClass,
|
|
|
|
key.to_glib_none().0,
|
|
|
|
value.to_glib_none().0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-07 10:49:19 +00:00
|
|
|
unsafe impl DeviceProviderClassSubclassExt for glib::Class<DeviceProvider> {}
|
2019-09-11 19:59:49 +00:00
|
|
|
|
2020-11-05 13:18:19 +00:00
|
|
|
unsafe impl<T: DeviceProviderImpl> IsSubclassable<T> for DeviceProvider {
|
2020-11-05 16:48:17 +00:00
|
|
|
fn override_vfuncs(klass: &mut glib::Class<Self>) {
|
2020-11-05 13:18:19 +00:00
|
|
|
<glib::Object as IsSubclassable<T>>::override_vfuncs(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>);
|
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>(
|
2019-09-11 19:59:49 +00:00
|
|
|
ptr: *mut gst_sys::GstDeviceProvider,
|
2020-07-25 08:02:04 +00:00
|
|
|
) -> *mut glib_sys::GList {
|
2019-09-11 19:59:49 +00:00
|
|
|
let instance = &*(ptr as *mut T::Instance);
|
|
|
|
let imp = instance.get_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
|
|
|
|
|
|
|
imp.probe(&wrap).to_glib_full()
|
|
|
|
}
|
|
|
|
|
2020-07-25 08:02:04 +00:00
|
|
|
unsafe extern "C" fn device_provider_start<T: DeviceProviderImpl>(
|
2019-09-11 19:59:49 +00:00
|
|
|
ptr: *mut gst_sys::GstDeviceProvider,
|
2020-07-25 08:02:04 +00:00
|
|
|
) -> glib_sys::gboolean {
|
2019-09-11 19:59:49 +00:00
|
|
|
let instance = &*(ptr as *mut T::Instance);
|
|
|
|
let imp = instance.get_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
|
|
|
|
|
|
|
match imp.start(&wrap) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.to_glib()
|
|
|
|
}
|
|
|
|
|
2020-07-25 08:02:04 +00:00
|
|
|
unsafe extern "C" fn device_provider_stop<T: DeviceProviderImpl>(
|
|
|
|
ptr: *mut gst_sys::GstDeviceProvider,
|
|
|
|
) {
|
2019-09-11 19:59:49 +00:00
|
|
|
let instance = &*(ptr as *mut T::Instance);
|
|
|
|
let imp = instance.get_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
|
|
|
|
|
|
|
imp.stop(&wrap);
|
|
|
|
}
|