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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Take a look at the license at the top of the repository in the LICENSE file.

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

use super::prelude::*;
use crate::{ffi, Bin, Element, LoggableError, Message};

pub trait BinImpl: BinImplExt + ElementImpl {
    /// Method to add an element to the bin.
    /// ## `element`
    /// the element to be added
    ///
    /// # Returns
    ///
    /// [`true`] if the `element` was added
    fn add_element(&self, element: &Element) -> Result<(), LoggableError> {
        self.parent_add_element(element)
    }

    /// Method to remove an element from the bin.
    /// ## `element`
    /// the element to be removed
    ///
    /// # Returns
    ///
    /// [`true`] if the `element` was removed
    fn remove_element(&self, element: &Element) -> Result<(), LoggableError> {
        self.parent_remove_element(element)
    }

    fn do_latency(&self) -> Result<(), LoggableError> {
        self.parent_do_latency()
    }

    /// Method to handle a message from the children.
    /// ## `message`
    /// the message to be handled
    fn handle_message(&self, message: Message) {
        self.parent_handle_message(message)
    }
}

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

pub trait BinImplExt: sealed::Sealed + ObjectSubclass {
    fn parent_add_element(&self, element: &Element) -> Result<(), LoggableError> {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstBinClass;
            let f = (*parent_class).add_element.ok_or_else(|| {
                loggable_error!(
                    crate::CAT_RUST,
                    "Parent function `add_element` is not defined"
                )
            })?;
            result_from_gboolean!(
                f(
                    self.obj().unsafe_cast_ref::<crate::Bin>().to_glib_none().0,
                    element.to_glib_none().0
                ),
                crate::CAT_RUST,
                "Failed to add the element using the parent function"
            )
        }
    }

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

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

    fn parent_handle_message(&self, message: Message) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstBinClass;
            if let Some(ref f) = (*parent_class).handle_message {
                f(
                    self.obj().unsafe_cast_ref::<crate::Bin>().to_glib_none().0,
                    message.into_glib_ptr(),
                );
            }
        }
    }
}

impl<T: BinImpl> BinImplExt for T {}

unsafe impl<T: BinImpl> IsSubclassable<T> for Bin {
    fn class_init(klass: &mut glib::Class<Self>) {
        Self::parent_class_init::<T>(klass);
        let klass = klass.as_mut();
        klass.add_element = Some(bin_add_element::<T>);
        klass.remove_element = Some(bin_remove_element::<T>);
        klass.do_latency = Some(bin_do_latency::<T>);
        klass.handle_message = Some(bin_handle_message::<T>);
    }
}

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

    panic_to_error!(imp, false, {
        match imp.add_element(&from_glib_none(element)) {
            Ok(()) => true,
            Err(err) => {
                err.log_with_imp(imp);
                false
            }
        }
    })
    .into_glib()
}

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

    // If we get a floating reference passed simply return FALSE here. It can't be
    // stored inside this bin, and if we continued to use it we would take ownership
    // of this floating reference.
    if glib::gobject_ffi::g_object_is_floating(element as *mut glib::gobject_ffi::GObject)
        != glib::ffi::GFALSE
    {
        return glib::ffi::GFALSE;
    }

    panic_to_error!(imp, false, {
        match imp.remove_element(&from_glib_none(element)) {
            Ok(()) => true,
            Err(err) => {
                err.log_with_imp(imp);
                false
            }
        }
    })
    .into_glib()
}

unsafe extern "C" fn bin_do_latency<T: BinImpl>(ptr: *mut ffi::GstBin) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    panic_to_error!(imp, false, {
        match imp.do_latency() {
            Ok(()) => true,
            Err(err) => {
                err.log_with_imp(imp);
                false
            }
        }
    })
    .into_glib()
}

unsafe extern "C" fn bin_handle_message<T: BinImpl>(
    ptr: *mut ffi::GstBin,
    message: *mut ffi::GstMessage,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    panic_to_error!(imp, (), { imp.handle_message(from_glib_full(message)) });
}