gstreamer/subclass/
uri_handler.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::ptr;
4
5use glib::{prelude::*, subclass::prelude::*, translate::*};
6
7use crate::{ffi, URIHandler, URIType};
8
9pub trait URIHandlerImpl:
10    super::element::ElementImpl + ObjectSubclass<Type: IsA<URIHandler>>
11{
12    const URI_TYPE: URIType;
13    fn protocols() -> &'static [&'static str];
14    fn uri(&self) -> Option<String>;
15    fn set_uri(&self, uri: &str) -> Result<(), glib::Error>;
16}
17
18pub trait URIHandlerImplExt: URIHandlerImpl {
19    fn parent_protocols() -> Vec<String> {
20        unsafe {
21            let type_data = Self::type_data();
22            let parent_iface = type_data.as_ref().parent_interface::<URIHandler>()
23                as *const ffi::GstURIHandlerInterface;
24
25            let func = (*parent_iface)
26                .get_protocols
27                .expect("no parent \"protocols\" implementation");
28            let ret = func(Self::ParentType::static_type().into_glib());
29            FromGlibPtrContainer::from_glib_none(ret)
30        }
31    }
32
33    fn parent_uri(&self) -> Option<String> {
34        unsafe {
35            let type_data = Self::type_data();
36            let parent_iface = type_data.as_ref().parent_interface::<URIHandler>()
37                as *const ffi::GstURIHandlerInterface;
38
39            let func = (*parent_iface)
40                .get_uri
41                .expect("no parent \"uri\" implementation");
42            let ret = func(self.obj().unsafe_cast_ref::<URIHandler>().to_glib_none().0);
43            from_glib_full(ret)
44        }
45    }
46
47    fn parent_set_uri(&self, uri: &str) -> Result<(), glib::Error> {
48        unsafe {
49            let type_data = Self::type_data();
50            let parent_iface = type_data.as_ref().parent_interface::<URIHandler>()
51                as *const ffi::GstURIHandlerInterface;
52
53            let func = (*parent_iface)
54                .set_uri
55                .expect("no parent \"set_uri\" implementation");
56
57            let mut err = ptr::null_mut();
58            func(
59                self.obj().unsafe_cast_ref::<URIHandler>().to_glib_none().0,
60                uri.to_glib_none().0,
61                &mut err,
62            );
63
64            if !err.is_null() {
65                Err(from_glib_full(err))
66            } else {
67                Ok(())
68            }
69        }
70    }
71}
72
73impl<T: URIHandlerImpl> URIHandlerImplExt for T {}
74
75unsafe impl<T: URIHandlerImpl> IsImplementable<T> for URIHandler {
76    fn interface_init(iface: &mut glib::Interface<Self>) {
77        let iface = iface.as_mut();
78
79        // Store the protocols in the interface data for later use
80        unsafe {
81            let mut data = T::type_data();
82            let protocols = T::protocols();
83            let data = data.as_mut();
84            data.set_class_data(Self::static_type(), glib::StrV::from(protocols));
85        }
86
87        iface.get_type = Some(uri_handler_get_type::<T>);
88        iface.get_protocols = Some(uri_handler_get_protocols::<T>);
89        iface.get_uri = Some(uri_handler_get_uri::<T>);
90        iface.set_uri = Some(uri_handler_set_uri::<T>);
91    }
92}
93
94unsafe extern "C" fn uri_handler_get_type<T: URIHandlerImpl>(
95    _type_: glib::ffi::GType,
96) -> ffi::GstURIType {
97    <T as URIHandlerImpl>::URI_TYPE.into_glib()
98}
99
100unsafe extern "C" fn uri_handler_get_protocols<T: URIHandlerImpl>(
101    _type_: glib::ffi::GType,
102) -> *const *const libc::c_char {
103    let data = <T as ObjectSubclassType>::type_data();
104    data.as_ref()
105        .class_data::<glib::StrV>(URIHandler::static_type())
106        .map(|p| p.as_ptr() as *const *const _)
107        .unwrap_or(ptr::null())
108}
109
110unsafe extern "C" fn uri_handler_get_uri<T: URIHandlerImpl>(
111    uri_handler: *mut ffi::GstURIHandler,
112) -> *mut libc::c_char {
113    let instance = &*(uri_handler as *mut T::Instance);
114    let imp = instance.imp();
115
116    imp.uri().to_glib_full()
117}
118
119unsafe extern "C" fn uri_handler_set_uri<T: URIHandlerImpl>(
120    uri_handler: *mut ffi::GstURIHandler,
121    uri: *const libc::c_char,
122    err: *mut *mut glib::ffi::GError,
123) -> glib::ffi::gboolean {
124    let instance = &*(uri_handler as *mut T::Instance);
125    let imp = instance.imp();
126
127    match imp.set_uri(glib::GString::from_glib_borrow(uri).as_str()) {
128        Ok(()) => true.into_glib(),
129        Err(error) => {
130            if !err.is_null() {
131                *err = error.into_glib_ptr();
132            }
133            false.into_glib()
134        }
135    }
136}