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
// Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::ptr;

use glib_ffi;
use gobject_ffi;
use gst_ffi;

use glib;
use glib::translate::*;
use gst;
use libc;

use gobject_subclass::anyimpl::*;
use gobject_subclass::object::*;

pub trait URIHandlerImpl: AnyImpl + Send + Sync + 'static {
    fn get_uri(&self, element: &gst::URIHandler) -> Option<String>;
    fn set_uri(&self, element: &gst::URIHandler, uri: Option<String>) -> Result<(), glib::Error>;
}

any_impl!(URIHandlerImpl);

pub trait URIHandlerImplStatic<T: ObjectType>: Send + Sync + 'static {
    fn get_impl<'a>(&self, imp: &'a T::ImplType) -> &'a URIHandlerImpl;
    fn get_type(&self) -> gst::URIType;
    fn get_protocols(&self) -> Vec<String>;
}

struct URIHandlerStatic<T: ObjectType> {
    imp_static: *const URIHandlerImplStatic<T>,
    protocols: *const Vec<*const libc::c_char>,
}

unsafe extern "C" fn uri_handler_get_type<T: ObjectType>(
    type_: glib_ffi::GType,
) -> gst_ffi::GstURIType {
    let klass = gobject_ffi::g_type_class_peek(type_);
    let klass = &*(klass as *const ClassStruct<T>);
    let interface_static = klass.get_interface_static(gst_ffi::gst_uri_handler_get_type())
        as *const URIHandlerStatic<T>;
    (*(*interface_static).imp_static).get_type().to_glib()
}

unsafe extern "C" fn uri_handler_get_protocols<T: ObjectType>(
    type_: glib_ffi::GType,
) -> *const *const libc::c_char {
    let klass = gobject_ffi::g_type_class_peek(type_);
    let klass = &*(klass as *const ClassStruct<T>);
    let interface_static = klass.get_interface_static(gst_ffi::gst_uri_handler_get_type())
        as *const URIHandlerStatic<T>;
    (*(*interface_static).protocols).as_ptr()
}

unsafe extern "C" fn uri_handler_get_uri<T: ObjectType>(
    uri_handler: *mut gst_ffi::GstURIHandler,
) -> *mut libc::c_char {
    floating_reference_guard!(uri_handler);

    let klass = &**(uri_handler as *const *const ClassStruct<T>);
    let interface_static = klass.get_interface_static(gst_ffi::gst_uri_handler_get_type())
        as *const URIHandlerStatic<T>;

    let instance = &*(uri_handler as *const T::InstanceStructType);
    let imp = instance.get_impl();
    let imp = (*(*interface_static).imp_static).get_impl(imp);

    imp.get_uri(&from_glib_borrow(uri_handler)).to_glib_full()
}

unsafe extern "C" fn uri_handler_set_uri<T: ObjectType>(
    uri_handler: *mut gst_ffi::GstURIHandler,
    uri: *const libc::c_char,
    err: *mut *mut glib_ffi::GError,
) -> glib_ffi::gboolean {
    floating_reference_guard!(uri_handler);

    let klass = &**(uri_handler as *const *const ClassStruct<T>);
    let interface_static = klass.get_interface_static(gst_ffi::gst_uri_handler_get_type())
        as *const URIHandlerStatic<T>;

    let instance = &*(uri_handler as *const T::InstanceStructType);
    let imp = instance.get_impl();
    let imp = (*(*interface_static).imp_static).get_impl(imp);

    match imp.set_uri(&from_glib_borrow(uri_handler), from_glib_none(uri)) {
        Ok(()) => true.to_glib(),
        Err(error) => {
            *err = error.to_glib_full() as *mut _;
            false.to_glib()
        }
    }
}

unsafe extern "C" fn uri_handler_init<T: ObjectType>(
    iface: glib_ffi::gpointer,
    iface_data: glib_ffi::gpointer,
) {
    let uri_handler_iface = &mut *(iface as *mut gst_ffi::GstURIHandlerInterface);

    let iface_type = (*(iface as *const gobject_ffi::GTypeInterface)).g_type;
    let type_ = (*(iface as *const gobject_ffi::GTypeInterface)).g_instance_type;
    let klass = &mut *(gobject_ffi::g_type_class_ref(type_) as *mut ClassStruct<T>);
    let interfaces_static = &mut *(klass.interfaces_static as *mut Vec<_>);
    interfaces_static.push((iface_type, iface_data));

    uri_handler_iface.get_type = Some(uri_handler_get_type::<T>);
    uri_handler_iface.get_protocols = Some(uri_handler_get_protocols::<T>);
    uri_handler_iface.get_uri = Some(uri_handler_get_uri::<T>);
    uri_handler_iface.set_uri = Some(uri_handler_set_uri::<T>);
}

pub fn register_uri_handler<T: ObjectType, I: URIHandlerImplStatic<T>>(
    _: &TypeInitToken,
    type_: glib::Type,
    imp: &I,
) {
    unsafe {
        let mut protocols: Vec<_> = imp
            .get_protocols()
            .iter()
            .map(|s| s.to_glib_full())
            .collect();
        protocols.push(ptr::null());

        let imp = imp as &URIHandlerImplStatic<T> as *const URIHandlerImplStatic<T>;
        let interface_static = Box::new(URIHandlerStatic {
            imp_static: imp,
            protocols: Box::into_raw(Box::new(protocols)),
        });

        let iface_info = gobject_ffi::GInterfaceInfo {
            interface_init: Some(uri_handler_init::<T>),
            interface_finalize: None,
            interface_data: Box::into_raw(interface_static) as glib_ffi::gpointer,
        };
        gobject_ffi::g_type_add_interface_static(
            type_.to_glib(),
            gst_ffi::gst_uri_handler_get_type(),
            &iface_info,
        );
    }
}