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: 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    let instance = &*(ptr as *mut T::Instance);
108    let imp = instance.imp();
109
110    match imp.create_element(
111        Option::<glib::GString>::from_glib_borrow(name)
112            .as_ref()
113            .as_ref()
114            .map(|s| s.as_str()),
115    ) {
116        Ok(element) => {
117            // The reference we're going to return, the initial reference is going to
118            // be dropped here now
119            let element = element.into_glib_ptr();
120            // See https://gitlab.freedesktop.org/gstreamer/gstreamer/issues/444
121            glib::gobject_ffi::g_object_force_floating(element as *mut glib::gobject_ffi::GObject);
122            element
123        }
124        Err(err) => {
125            err.log_with_imp(imp);
126            ptr::null_mut()
127        }
128    }
129}
130
131unsafe extern "C" fn device_reconfigure_element<T: DeviceImpl>(
132    ptr: *mut ffi::GstDevice,
133    element: *mut ffi::GstElement,
134) -> glib::ffi::gboolean {
135    let instance = &*(ptr as *mut T::Instance);
136    let imp = instance.imp();
137
138    match imp.reconfigure_element(&from_glib_borrow(element)) {
139        Ok(()) => true,
140        Err(err) => {
141            err.log_with_imp(imp);
142            false
143        }
144    }
145    .into_glib()
146}