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: GstObjectImpl + ObjectSubclass<Type: IsA<Device>> {
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}
4243pub trait DeviceImplExt: DeviceImpl {
44fn parent_create_element(&self, name: Option<&str>) -> Result<Element, LoggableError> {
45unsafe {
46let data = Self::type_data();
47let parent_class = data.as_ref().parent_class() as *mut ffi::GstDeviceClass;
48if let Some(f) = (*parent_class).create_element {
49let ptr = f(
50self.obj().unsafe_cast_ref::<Device>().to_glib_none().0,
51 name.to_glib_none().0,
52 );
5354// Don't steal floating reference here but pass it further to the caller
55Option::<_>::from_glib_full(ptr).ok_or_else(|| {
56loggable_error!(
57crate::CAT_RUST,
58"Failed to create element using the parent function"
59)
60 })
61 } else {
62Err(loggable_error!(
63crate::CAT_RUST,
64"Parent function `create_element` is not defined"
65))
66 }
67 }
68 }
6970fn parent_reconfigure_element(&self, element: &Element) -> Result<(), LoggableError> {
71unsafe {
72let data = Self::type_data();
73let parent_class = data.as_ref().parent_class() as *mut ffi::GstDeviceClass;
74let f = (*parent_class).reconfigure_element.ok_or_else(|| {
75loggable_error!(
76crate::CAT_RUST,
77"Parent function `reconfigure_element` is not defined"
78)
79 })?;
80result_from_gboolean!(
81 f(
82self.obj().unsafe_cast_ref::<Device>().to_glib_none().0,
83 element.to_glib_none().0
84),
85crate::CAT_RUST,
86"Failed to reconfigure the element using the parent function"
87)
88 }
89 }
90}
9192impl<T: DeviceImpl> DeviceImplExt for T {}
9394unsafe impl<T: DeviceImpl> IsSubclassable<T> for Device {
95fn class_init(klass: &mut glib::Class<Self>) {
96Self::parent_class_init::<T>(klass);
97let klass = klass.as_mut();
98 klass.create_element = Some(device_create_element::<T>);
99 klass.reconfigure_element = Some(device_reconfigure_element::<T>);
100 }
101}
102103unsafe extern "C" fn device_create_element<T: DeviceImpl>(
104 ptr: *mut ffi::GstDevice,
105 name: *const libc::c_char,
106) -> *mut ffi::GstElement {
107let instance = &*(ptr as *mut T::Instance);
108let imp = instance.imp();
109110match imp.create_element(
111Option::<glib::GString>::from_glib_borrow(name)
112 .as_ref()
113 .as_ref()
114 .map(|s| s.as_str()),
115 ) {
116Ok(element) => {
117// The reference we're going to return, the initial reference is going to
118 // be dropped here now
119let element = element.into_glib_ptr();
120// See https://gitlab.freedesktop.org/gstreamer/gstreamer/issues/444
121glib::gobject_ffi::g_object_force_floating(element as *mut glib::gobject_ffi::GObject);
122 element
123 }
124Err(err) => {
125 err.log_with_imp(imp);
126 ptr::null_mut()
127 }
128 }
129}
130131unsafe extern "C" fn device_reconfigure_element<T: DeviceImpl>(
132 ptr: *mut ffi::GstDevice,
133 element: *mut ffi::GstElement,
134) -> glib::ffi::gboolean {
135let instance = &*(ptr as *mut T::Instance);
136let imp = instance.imp();
137138match imp.reconfigure_element(&from_glib_borrow(element)) {
139Ok(()) => true,
140Err(err) => {
141 err.log_with_imp(imp);
142false
143}
144 }
145 .into_glib()
146}