1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Take a look at the license at the top of the repository in the LICENSE file.

use std::ptr;

use glib::{prelude::*, subclass::prelude::*, translate::*};

use super::prelude::*;
use crate::{Device, Element, LoggableError};

pub trait DeviceImpl: DeviceImplExt + GstObjectImpl + Send + Sync {
    fn create_element(&self, name: Option<&str>) -> Result<Element, LoggableError> {
        self.parent_create_element(name)
    }

    fn reconfigure_element(&self, element: &Element) -> Result<(), LoggableError> {
        self.parent_reconfigure_element(element)
    }
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::DeviceImplExt> Sealed for T {}
}

pub trait DeviceImplExt: sealed::Sealed + ObjectSubclass {
    fn parent_create_element(&self, name: Option<&str>) -> Result<Element, LoggableError> {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstDeviceClass;
            if let Some(f) = (*parent_class).create_element {
                let ptr = f(
                    self.obj().unsafe_cast_ref::<Device>().to_glib_none().0,
                    name.to_glib_none().0,
                );

                // Don't steal floating reference here but pass it further to the caller
                Option::<_>::from_glib_full(ptr).ok_or_else(|| {
                    loggable_error!(
                        crate::CAT_RUST,
                        "Failed to create element using the parent function"
                    )
                })
            } else {
                Err(loggable_error!(
                    crate::CAT_RUST,
                    "Parent function `create_element` is not defined"
                ))
            }
        }
    }

    fn parent_reconfigure_element(&self, element: &Element) -> Result<(), LoggableError> {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstDeviceClass;
            let f = (*parent_class).reconfigure_element.ok_or_else(|| {
                loggable_error!(
                    crate::CAT_RUST,
                    "Parent function `reconfigure_element` is not defined"
                )
            })?;
            result_from_gboolean!(
                f(
                    self.obj().unsafe_cast_ref::<Device>().to_glib_none().0,
                    element.to_glib_none().0
                ),
                crate::CAT_RUST,
                "Failed to reconfigure the element using the parent function"
            )
        }
    }
}

impl<T: DeviceImpl> DeviceImplExt for T {}

unsafe impl<T: DeviceImpl> IsSubclassable<T> for Device {
    fn class_init(klass: &mut glib::Class<Self>) {
        Self::parent_class_init::<T>(klass);
        let klass = klass.as_mut();
        klass.create_element = Some(device_create_element::<T>);
        klass.reconfigure_element = Some(device_reconfigure_element::<T>);
    }
}

unsafe extern "C" fn device_create_element<T: DeviceImpl>(
    ptr: *mut ffi::GstDevice,
    name: *const libc::c_char,
) -> *mut ffi::GstElement {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    match imp.create_element(
        Option::<glib::GString>::from_glib_borrow(name)
            .as_ref()
            .as_ref()
            .map(|s| s.as_str()),
    ) {
        Ok(element) => {
            // The reference we're going to return, the initial reference is going to
            // be dropped here now
            let element = element.into_glib_ptr();
            // See https://gitlab.freedesktop.org/gstreamer/gstreamer/issues/444
            glib::gobject_ffi::g_object_force_floating(element as *mut glib::gobject_ffi::GObject);
            element
        }
        Err(err) => {
            err.log_with_imp(imp);
            ptr::null_mut()
        }
    }
}

unsafe extern "C" fn device_reconfigure_element<T: DeviceImpl>(
    ptr: *mut ffi::GstDevice,
    element: *mut ffi::GstElement,
) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    match imp.reconfigure_element(&from_glib_borrow(element)) {
        Ok(()) => true,
        Err(err) => {
            err.log_with_imp(imp);
            false
        }
    }
    .into_glib()
}