Skip to main content

gstreamer/subclass/
device.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::ptr;
4
5use glib::{prelude::*, subclass::prelude::*, translate::*};
6
7use super::prelude::*;
8use crate::{Device, Element, LoggableError, ffi};
9
10pub 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
21    fn create_element(&self, name: Option<&str>) -> Result<Element, LoggableError> {
22        self.parent_create_element(name)
23    }
24
25    /// 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.
38    fn reconfigure_element(&self, element: &Element) -> Result<(), LoggableError> {
39        self.parent_reconfigure_element(element)
40    }
41}
42
43pub trait DeviceImplExt: DeviceImpl {
44    fn parent_create_element(&self, name: Option<&str>) -> Result<Element, LoggableError> {
45        unsafe {
46            let data = Self::type_data();
47            let parent_class = data.as_ref().parent_class() as *mut ffi::GstDeviceClass;
48            if let Some(f) = (*parent_class).create_element {
49                let ptr = f(
50                    self.obj().unsafe_cast_ref::<Device>().to_glib_none().0,
51                    name.to_glib_none().0,
52                );
53
54                // Don't steal floating reference here but pass it further to the caller
55                Option::<_>::from_glib_full(ptr).ok_or_else(|| {
56                    loggable_error!(
57                        crate::CAT_RUST,
58                        "Failed to create element using the parent function"
59                    )
60                })
61            } else {
62                Err(loggable_error!(
63                    crate::CAT_RUST,
64                    "Parent function `create_element` is not defined"
65                ))
66            }
67        }
68    }
69
70    fn parent_reconfigure_element(&self, element: &Element) -> Result<(), LoggableError> {
71        unsafe {
72            let data = Self::type_data();
73            let parent_class = data.as_ref().parent_class() as *mut ffi::GstDeviceClass;
74            let f = (*parent_class).reconfigure_element.ok_or_else(|| {
75                loggable_error!(
76                    crate::CAT_RUST,
77                    "Parent function `reconfigure_element` is not defined"
78                )
79            })?;
80            result_from_gboolean!(
81                f(
82                    self.obj().unsafe_cast_ref::<Device>().to_glib_none().0,
83                    element.to_glib_none().0
84                ),
85                crate::CAT_RUST,
86                "Failed to reconfigure the element using the parent function"
87            )
88        }
89    }
90}
91
92impl<T: DeviceImpl> DeviceImplExt for T {}
93
94unsafe impl<T: DeviceImpl> IsSubclassable<T> for Device {
95    fn class_init(klass: &mut glib::Class<Self>) {
96        Self::parent_class_init::<T>(klass);
97        let klass = klass.as_mut();
98        klass.create_element = Some(device_create_element::<T>);
99        klass.reconfigure_element = Some(device_reconfigure_element::<T>);
100    }
101}
102
103unsafe extern "C" fn device_create_element<T: DeviceImpl>(
104    ptr: *mut ffi::GstDevice,
105    name: *const libc::c_char,
106) -> *mut ffi::GstElement {
107    unsafe {
108        let instance = &*(ptr as *mut T::Instance);
109        let imp = instance.imp();
110
111        match imp.create_element(
112            Option::<glib::GString>::from_glib_borrow(name)
113                .as_ref()
114                .as_ref()
115                .map(|s| s.as_str()),
116        ) {
117            Ok(element) => {
118                // The reference we're going to return, the initial reference is going to
119                // be dropped here now
120                let element = element.into_glib_ptr();
121                // See https://gitlab.freedesktop.org/gstreamer/gstreamer/issues/444
122                glib::gobject_ffi::g_object_force_floating(
123                    element as *mut glib::gobject_ffi::GObject,
124                );
125                element
126            }
127            Err(err) => {
128                err.log_with_imp(imp);
129                ptr::null_mut()
130            }
131        }
132    }
133}
134
135unsafe extern "C" fn device_reconfigure_element<T: DeviceImpl>(
136    ptr: *mut ffi::GstDevice,
137    element: *mut ffi::GstElement,
138) -> glib::ffi::gboolean {
139    unsafe {
140        let instance = &*(ptr as *mut T::Instance);
141        let imp = instance.imp();
142
143        match imp.reconfigure_element(&from_glib_borrow(element)) {
144            Ok(()) => true,
145            Err(err) => {
146                err.log_with_imp(imp);
147                false
148            }
149        }
150        .into_glib()
151    }
152}