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