1// Take a look at the license at the top of the repository in the LICENSE file.
23use std::ptr;
45use glib::{prelude::*, subclass::prelude::*, translate::*};
67use super::prelude::*;
8use crate::{ffi, Device, Element, LoggableError};
910pub trait DeviceImpl: DeviceImplExt + GstObjectImpl + Send + Sync {
11/// Creates the element with all of the required parameters set to use
12 /// this device.
13 /// ## `name`
14 /// name of new element, or [`None`] to automatically
15 /// create a unique name.
16 ///
17 /// # Returns
18 ///
19 /// a new [`Element`][crate::Element] configured to use
20 /// this device
21fn create_element(&self, name: Option<&str>) -> Result<Element, LoggableError> {
22self.parent_create_element(name)
23 }
2425/// Tries to reconfigure an existing element to use the device. If this
26 /// function fails, then one must destroy the element and create a new one
27 /// using [`DeviceExt::create_element()`][crate::prelude::DeviceExt::create_element()].
28 ///
29 /// Note: This should only be implemented for elements can change their
30 /// device in the PLAYING state.
31 /// ## `element`
32 /// a [`Element`][crate::Element]
33 ///
34 /// # Returns
35 ///
36 /// [`true`] if the element could be reconfigured to use this device,
37 /// [`false`] otherwise.
38fn reconfigure_element(&self, element: &Element) -> Result<(), LoggableError> {
39self.parent_reconfigure_element(element)
40 }
41}
4243mod sealed {
44pub trait Sealed {}
45impl<T: super::DeviceImplExt> Sealed for T {}
46}
4748pub trait DeviceImplExt: sealed::Sealed + ObjectSubclass {
49fn parent_create_element(&self, name: Option<&str>) -> Result<Element, LoggableError> {
50unsafe {
51let data = Self::type_data();
52let parent_class = data.as_ref().parent_class() as *mut ffi::GstDeviceClass;
53if let Some(f) = (*parent_class).create_element {
54let ptr = f(
55self.obj().unsafe_cast_ref::<Device>().to_glib_none().0,
56 name.to_glib_none().0,
57 );
5859// Don't steal floating reference here but pass it further to the caller
60Option::<_>::from_glib_full(ptr).ok_or_else(|| {
61loggable_error!(
62crate::CAT_RUST,
63"Failed to create element using the parent function"
64)
65 })
66 } else {
67Err(loggable_error!(
68crate::CAT_RUST,
69"Parent function `create_element` is not defined"
70))
71 }
72 }
73 }
7475fn parent_reconfigure_element(&self, element: &Element) -> Result<(), LoggableError> {
76unsafe {
77let data = Self::type_data();
78let parent_class = data.as_ref().parent_class() as *mut ffi::GstDeviceClass;
79let f = (*parent_class).reconfigure_element.ok_or_else(|| {
80loggable_error!(
81crate::CAT_RUST,
82"Parent function `reconfigure_element` is not defined"
83)
84 })?;
85result_from_gboolean!(
86 f(
87self.obj().unsafe_cast_ref::<Device>().to_glib_none().0,
88 element.to_glib_none().0
89),
90crate::CAT_RUST,
91"Failed to reconfigure the element using the parent function"
92)
93 }
94 }
95}
9697impl<T: DeviceImpl> DeviceImplExt for T {}
9899unsafe impl<T: DeviceImpl> IsSubclassable<T> for Device {
100fn class_init(klass: &mut glib::Class<Self>) {
101Self::parent_class_init::<T>(klass);
102let klass = klass.as_mut();
103 klass.create_element = Some(device_create_element::<T>);
104 klass.reconfigure_element = Some(device_reconfigure_element::<T>);
105 }
106}
107108unsafe extern "C" fn device_create_element<T: DeviceImpl>(
109 ptr: *mut ffi::GstDevice,
110 name: *const libc::c_char,
111) -> *mut ffi::GstElement {
112let instance = &*(ptr as *mut T::Instance);
113let imp = instance.imp();
114115match imp.create_element(
116Option::<glib::GString>::from_glib_borrow(name)
117 .as_ref()
118 .as_ref()
119 .map(|s| s.as_str()),
120 ) {
121Ok(element) => {
122// The reference we're going to return, the initial reference is going to
123 // be dropped here now
124let element = element.into_glib_ptr();
125// See https://gitlab.freedesktop.org/gstreamer/gstreamer/issues/444
126glib::gobject_ffi::g_object_force_floating(element as *mut glib::gobject_ffi::GObject);
127 element
128 }
129Err(err) => {
130 err.log_with_imp(imp);
131 ptr::null_mut()
132 }
133 }
134}
135136unsafe extern "C" fn device_reconfigure_element<T: DeviceImpl>(
137 ptr: *mut ffi::GstDevice,
138 element: *mut ffi::GstElement,
139) -> glib::ffi::gboolean {
140let instance = &*(ptr as *mut T::Instance);
141let imp = instance.imp();
142143match imp.reconfigure_element(&from_glib_borrow(element)) {
144Ok(()) => true,
145Err(err) => {
146 err.log_with_imp(imp);
147false
148}
149 }
150 .into_glib()
151}