gst_plugin/
child_proxy.rs

1// Copyright (C) 2018 Sebastian Dröge <sebastian@centricular.com>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8use glib_ffi;
9use gobject_ffi;
10use gst_ffi;
11
12use glib;
13use glib::translate::*;
14use gst;
15use libc;
16
17use gobject_subclass::anyimpl::*;
18use gobject_subclass::object::*;
19
20pub trait ChildProxyImpl: AnyImpl + Send + Sync + 'static {
21    fn get_child_by_name(&self, object: &gst::ChildProxy, name: &str) -> Option<glib::Object> {
22        unsafe {
23            let type_ = gst_ffi::gst_child_proxy_get_type();
24            let iface = gobject_ffi::g_type_default_interface_ref(type_)
25                as *mut gst_ffi::GstChildProxyInterface;
26            assert!(!iface.is_null());
27
28            let ret = ((*iface).get_child_by_name.as_ref().unwrap())(
29                object.to_glib_none().0,
30                name.to_glib_none().0,
31            );
32
33            gobject_ffi::g_type_default_interface_unref(iface as glib_ffi::gpointer);
34
35            from_glib_full(ret)
36        }
37    }
38
39    fn get_child_by_index(&self, object: &gst::ChildProxy, index: u32) -> Option<glib::Object>;
40    fn get_children_count(&self, object: &gst::ChildProxy) -> u32;
41
42    fn child_added(&self, object: &gst::ChildProxy, child: &glib::Object, name: &str);
43    fn child_removed(&self, object: &gst::ChildProxy, child: &glib::Object, name: &str);
44}
45
46any_impl!(ChildProxyImpl);
47
48pub trait ChildProxyImplStatic<T: ObjectType>: Send + Sync + 'static {
49    fn get_impl<'a>(&self, imp: &'a T::ImplType) -> &'a ChildProxyImpl;
50}
51
52struct ChildProxyStatic<T: ObjectType> {
53    imp_static: *const ChildProxyImplStatic<T>,
54}
55
56unsafe extern "C" fn child_proxy_get_child_by_name<T: ObjectType>(
57    child_proxy: *mut gst_ffi::GstChildProxy,
58    name: *const libc::c_char,
59) -> *mut gobject_ffi::GObject {
60    floating_reference_guard!(child_proxy);
61
62    let klass = &**(child_proxy as *const *const ClassStruct<T>);
63    let interface_static = klass.get_interface_static(gst_ffi::gst_child_proxy_get_type())
64        as *const ChildProxyStatic<T>;
65
66    let instance = &*(child_proxy as *const T::InstanceStructType);
67    let imp = instance.get_impl();
68    let imp = (*(*interface_static).imp_static).get_impl(imp);
69
70    imp.get_child_by_name(
71        &from_glib_borrow(child_proxy),
72        String::from_glib_none(name).as_str(),
73    )
74    .to_glib_full()
75}
76
77unsafe extern "C" fn child_proxy_get_child_by_index<T: ObjectType>(
78    child_proxy: *mut gst_ffi::GstChildProxy,
79    index: u32,
80) -> *mut gobject_ffi::GObject {
81    floating_reference_guard!(child_proxy);
82
83    let klass = &**(child_proxy as *const *const ClassStruct<T>);
84    let interface_static = klass.get_interface_static(gst_ffi::gst_child_proxy_get_type())
85        as *const ChildProxyStatic<T>;
86
87    let instance = &*(child_proxy as *const T::InstanceStructType);
88    let imp = instance.get_impl();
89    let imp = (*(*interface_static).imp_static).get_impl(imp);
90
91    imp.get_child_by_index(&from_glib_borrow(child_proxy), index)
92        .to_glib_full()
93}
94
95unsafe extern "C" fn child_proxy_get_children_count<T: ObjectType>(
96    child_proxy: *mut gst_ffi::GstChildProxy,
97) -> u32 {
98    floating_reference_guard!(child_proxy);
99
100    let klass = &**(child_proxy as *const *const ClassStruct<T>);
101    let interface_static = klass.get_interface_static(gst_ffi::gst_child_proxy_get_type())
102        as *const ChildProxyStatic<T>;
103
104    let instance = &*(child_proxy as *const T::InstanceStructType);
105    let imp = instance.get_impl();
106    let imp = (*(*interface_static).imp_static).get_impl(imp);
107
108    imp.get_children_count(&from_glib_borrow(child_proxy))
109}
110
111unsafe extern "C" fn child_proxy_child_added<T: ObjectType>(
112    child_proxy: *mut gst_ffi::GstChildProxy,
113    child: *mut gobject_ffi::GObject,
114    name: *const libc::c_char,
115) {
116    floating_reference_guard!(child_proxy);
117
118    let klass = &**(child_proxy as *const *const ClassStruct<T>);
119    let interface_static = klass.get_interface_static(gst_ffi::gst_child_proxy_get_type())
120        as *const ChildProxyStatic<T>;
121
122    let instance = &*(child_proxy as *const T::InstanceStructType);
123    let imp = instance.get_impl();
124    let imp = (*(*interface_static).imp_static).get_impl(imp);
125
126    imp.child_added(
127        &from_glib_borrow(child_proxy),
128        &from_glib_borrow(child),
129        String::from_glib_none(name).as_str(),
130    )
131}
132
133unsafe extern "C" fn child_proxy_child_removed<T: ObjectType>(
134    child_proxy: *mut gst_ffi::GstChildProxy,
135    child: *mut gobject_ffi::GObject,
136    name: *const libc::c_char,
137) {
138    floating_reference_guard!(child_proxy);
139
140    let klass = &**(child_proxy as *const *const ClassStruct<T>);
141    let interface_static = klass.get_interface_static(gst_ffi::gst_child_proxy_get_type())
142        as *const ChildProxyStatic<T>;
143
144    let instance = &*(child_proxy as *const T::InstanceStructType);
145    let imp = instance.get_impl();
146    let imp = (*(*interface_static).imp_static).get_impl(imp);
147
148    imp.child_removed(
149        &from_glib_borrow(child_proxy),
150        &from_glib_borrow(child),
151        String::from_glib_none(name).as_str(),
152    )
153}
154
155unsafe extern "C" fn child_proxy_init<T: ObjectType>(
156    iface: glib_ffi::gpointer,
157    iface_data: glib_ffi::gpointer,
158) {
159    let child_proxy_iface = &mut *(iface as *mut gst_ffi::GstChildProxyInterface);
160
161    let iface_type = (*(iface as *const gobject_ffi::GTypeInterface)).g_type;
162    let type_ = (*(iface as *const gobject_ffi::GTypeInterface)).g_instance_type;
163    let klass = &mut *(gobject_ffi::g_type_class_ref(type_) as *mut ClassStruct<T>);
164    let interfaces_static = &mut *(klass.interfaces_static as *mut Vec<_>);
165    interfaces_static.push((iface_type, iface_data));
166
167    child_proxy_iface.get_child_by_name = Some(child_proxy_get_child_by_name::<T>);
168    child_proxy_iface.get_child_by_index = Some(child_proxy_get_child_by_index::<T>);
169    child_proxy_iface.get_children_count = Some(child_proxy_get_children_count::<T>);
170    child_proxy_iface.child_added = Some(child_proxy_child_added::<T>);
171    child_proxy_iface.child_removed = Some(child_proxy_child_removed::<T>);
172}
173
174pub fn register_child_proxy<T: ObjectType, I: ChildProxyImplStatic<T>>(
175    _: &TypeInitToken,
176    type_: glib::Type,
177    imp: &I,
178) {
179    unsafe {
180        let imp = imp as &ChildProxyImplStatic<T> as *const ChildProxyImplStatic<T>;
181        let interface_static = Box::new(ChildProxyStatic { imp_static: imp });
182
183        let iface_info = gobject_ffi::GInterfaceInfo {
184            interface_init: Some(child_proxy_init::<T>),
185            interface_finalize: None,
186            interface_data: Box::into_raw(interface_static) as glib_ffi::gpointer,
187        };
188        gobject_ffi::g_type_add_interface_static(
189            type_.to_glib(),
190            gst_ffi::gst_child_proxy_get_type(),
191            &iface_info,
192        );
193    }
194}