gst_plugin/
uri_handler.rs

1// Copyright (C) 2017 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 std::ptr;
9
10use glib_ffi;
11use gobject_ffi;
12use gst_ffi;
13
14use glib;
15use glib::translate::*;
16use gst;
17use libc;
18
19use gobject_subclass::anyimpl::*;
20use gobject_subclass::object::*;
21
22pub trait URIHandlerImpl: AnyImpl + Send + Sync + 'static {
23    fn get_uri(&self, element: &gst::URIHandler) -> Option<String>;
24    fn set_uri(&self, element: &gst::URIHandler, uri: Option<String>) -> Result<(), glib::Error>;
25}
26
27any_impl!(URIHandlerImpl);
28
29pub trait URIHandlerImplStatic<T: ObjectType>: Send + Sync + 'static {
30    fn get_impl<'a>(&self, imp: &'a T::ImplType) -> &'a URIHandlerImpl;
31    fn get_type(&self) -> gst::URIType;
32    fn get_protocols(&self) -> Vec<String>;
33}
34
35struct URIHandlerStatic<T: ObjectType> {
36    imp_static: *const URIHandlerImplStatic<T>,
37    protocols: *const Vec<*const libc::c_char>,
38}
39
40unsafe extern "C" fn uri_handler_get_type<T: ObjectType>(
41    type_: glib_ffi::GType,
42) -> gst_ffi::GstURIType {
43    let klass = gobject_ffi::g_type_class_peek(type_);
44    let klass = &*(klass as *const ClassStruct<T>);
45    let interface_static = klass.get_interface_static(gst_ffi::gst_uri_handler_get_type())
46        as *const URIHandlerStatic<T>;
47    (*(*interface_static).imp_static).get_type().to_glib()
48}
49
50unsafe extern "C" fn uri_handler_get_protocols<T: ObjectType>(
51    type_: glib_ffi::GType,
52) -> *const *const libc::c_char {
53    let klass = gobject_ffi::g_type_class_peek(type_);
54    let klass = &*(klass as *const ClassStruct<T>);
55    let interface_static = klass.get_interface_static(gst_ffi::gst_uri_handler_get_type())
56        as *const URIHandlerStatic<T>;
57    (*(*interface_static).protocols).as_ptr()
58}
59
60unsafe extern "C" fn uri_handler_get_uri<T: ObjectType>(
61    uri_handler: *mut gst_ffi::GstURIHandler,
62) -> *mut libc::c_char {
63    floating_reference_guard!(uri_handler);
64
65    let klass = &**(uri_handler as *const *const ClassStruct<T>);
66    let interface_static = klass.get_interface_static(gst_ffi::gst_uri_handler_get_type())
67        as *const URIHandlerStatic<T>;
68
69    let instance = &*(uri_handler as *const T::InstanceStructType);
70    let imp = instance.get_impl();
71    let imp = (*(*interface_static).imp_static).get_impl(imp);
72
73    imp.get_uri(&from_glib_borrow(uri_handler)).to_glib_full()
74}
75
76unsafe extern "C" fn uri_handler_set_uri<T: ObjectType>(
77    uri_handler: *mut gst_ffi::GstURIHandler,
78    uri: *const libc::c_char,
79    err: *mut *mut glib_ffi::GError,
80) -> glib_ffi::gboolean {
81    floating_reference_guard!(uri_handler);
82
83    let klass = &**(uri_handler as *const *const ClassStruct<T>);
84    let interface_static = klass.get_interface_static(gst_ffi::gst_uri_handler_get_type())
85        as *const URIHandlerStatic<T>;
86
87    let instance = &*(uri_handler as *const T::InstanceStructType);
88    let imp = instance.get_impl();
89    let imp = (*(*interface_static).imp_static).get_impl(imp);
90
91    match imp.set_uri(&from_glib_borrow(uri_handler), from_glib_none(uri)) {
92        Ok(()) => true.to_glib(),
93        Err(error) => {
94            *err = error.to_glib_full() as *mut _;
95            false.to_glib()
96        }
97    }
98}
99
100unsafe extern "C" fn uri_handler_init<T: ObjectType>(
101    iface: glib_ffi::gpointer,
102    iface_data: glib_ffi::gpointer,
103) {
104    let uri_handler_iface = &mut *(iface as *mut gst_ffi::GstURIHandlerInterface);
105
106    let iface_type = (*(iface as *const gobject_ffi::GTypeInterface)).g_type;
107    let type_ = (*(iface as *const gobject_ffi::GTypeInterface)).g_instance_type;
108    let klass = &mut *(gobject_ffi::g_type_class_ref(type_) as *mut ClassStruct<T>);
109    let interfaces_static = &mut *(klass.interfaces_static as *mut Vec<_>);
110    interfaces_static.push((iface_type, iface_data));
111
112    uri_handler_iface.get_type = Some(uri_handler_get_type::<T>);
113    uri_handler_iface.get_protocols = Some(uri_handler_get_protocols::<T>);
114    uri_handler_iface.get_uri = Some(uri_handler_get_uri::<T>);
115    uri_handler_iface.set_uri = Some(uri_handler_set_uri::<T>);
116}
117
118pub fn register_uri_handler<T: ObjectType, I: URIHandlerImplStatic<T>>(
119    _: &TypeInitToken,
120    type_: glib::Type,
121    imp: &I,
122) {
123    unsafe {
124        let mut protocols: Vec<_> = imp
125            .get_protocols()
126            .iter()
127            .map(|s| s.to_glib_full())
128            .collect();
129        protocols.push(ptr::null());
130
131        let imp = imp as &URIHandlerImplStatic<T> as *const URIHandlerImplStatic<T>;
132        let interface_static = Box::new(URIHandlerStatic {
133            imp_static: imp,
134            protocols: Box::into_raw(Box::new(protocols)),
135        });
136
137        let iface_info = gobject_ffi::GInterfaceInfo {
138            interface_init: Some(uri_handler_init::<T>),
139            interface_finalize: None,
140            interface_data: Box::into_raw(interface_static) as glib_ffi::gpointer,
141        };
142        gobject_ffi::g_type_add_interface_static(
143            type_.to_glib(),
144            gst_ffi::gst_uri_handler_get_type(),
145            &iface_info,
146        );
147    }
148}