2020-12-15 10:53:31 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2018-11-18 14:10:58 +00:00
|
|
|
|
|
|
|
use glib::prelude::*;
|
|
|
|
use glib::translate::*;
|
|
|
|
|
|
|
|
use glib::subclass::prelude::*;
|
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::URIHandler;
|
|
|
|
use crate::URIType;
|
2018-11-18 14:10:58 +00:00
|
|
|
|
2020-07-25 08:02:04 +00:00
|
|
|
pub trait URIHandlerImpl: super::element::ElementImpl {
|
2020-10-24 17:10:13 +00:00
|
|
|
const URI_TYPE: URIType;
|
|
|
|
fn get_protocols() -> &'static [&'static str];
|
2020-11-14 15:34:41 +00:00
|
|
|
fn get_uri(&self, element: &Self::Type) -> Option<String>;
|
|
|
|
fn set_uri(&self, element: &Self::Type, uri: &str) -> Result<(), glib::Error>;
|
2018-11-18 14:10:58 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 15:40:45 +00:00
|
|
|
// Send+Sync wrapper around a NULL-terminated C string array
|
|
|
|
struct CStrV(*const *const libc::c_char);
|
|
|
|
unsafe impl Send for CStrV {}
|
|
|
|
unsafe impl Sync for CStrV {}
|
|
|
|
|
2020-07-25 08:02:04 +00:00
|
|
|
unsafe impl<T: URIHandlerImpl> IsImplementable<T> for URIHandler {
|
2018-12-18 23:15:23 +00:00
|
|
|
unsafe extern "C" fn interface_init(
|
2020-11-21 13:46:48 +00:00
|
|
|
iface: glib::ffi::gpointer,
|
|
|
|
_iface_data: glib::ffi::gpointer,
|
2018-12-18 23:15:23 +00:00
|
|
|
) {
|
2020-11-21 13:46:48 +00:00
|
|
|
let uri_handler_iface = &mut *(iface as *mut ffi::GstURIHandlerInterface);
|
2018-12-18 23:15:23 +00:00
|
|
|
|
|
|
|
// Store the protocols in the interface data for later use
|
|
|
|
let mut data = T::type_data();
|
|
|
|
let protocols = T::get_protocols();
|
2021-02-07 15:40:45 +00:00
|
|
|
let protocols = protocols.to_glib_full();
|
2018-12-18 23:15:23 +00:00
|
|
|
let data = data.as_mut();
|
2021-02-07 15:40:45 +00:00
|
|
|
|
|
|
|
data.set_class_data(URIHandler::static_type(), CStrV(protocols));
|
2018-12-18 23:15:23 +00:00
|
|
|
|
|
|
|
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>);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-25 08:02:04 +00:00
|
|
|
unsafe extern "C" fn uri_handler_get_type<T: URIHandlerImpl>(
|
2020-11-21 13:46:48 +00:00
|
|
|
_type_: glib::ffi::GType,
|
|
|
|
) -> ffi::GstURIType {
|
2020-10-24 17:10:13 +00:00
|
|
|
<T as URIHandlerImpl>::URI_TYPE.to_glib()
|
2018-11-18 14:10:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 08:02:04 +00:00
|
|
|
unsafe extern "C" fn uri_handler_get_protocols<T: URIHandlerImpl>(
|
2020-11-21 13:46:48 +00:00
|
|
|
_type_: glib::ffi::GType,
|
2020-07-25 08:02:04 +00:00
|
|
|
) -> *const *const libc::c_char {
|
2018-11-18 14:10:58 +00:00
|
|
|
let data = <T as ObjectSubclass>::type_data();
|
|
|
|
data.as_ref()
|
2021-02-07 15:40:45 +00:00
|
|
|
.get_class_data::<CStrV>(URIHandler::static_type())
|
|
|
|
.unwrap_or(&CStrV(std::ptr::null()))
|
|
|
|
.0
|
2018-11-18 14:10:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 08:02:04 +00:00
|
|
|
unsafe extern "C" fn uri_handler_get_uri<T: URIHandlerImpl>(
|
2020-11-21 13:46:48 +00:00
|
|
|
uri_handler: *mut ffi::GstURIHandler,
|
2020-07-25 08:02:04 +00:00
|
|
|
) -> *mut libc::c_char {
|
2018-11-18 14:10:58 +00:00
|
|
|
let instance = &*(uri_handler as *mut T::Instance);
|
|
|
|
let imp = instance.get_impl();
|
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
imp.get_uri(&from_glib_borrow::<_, URIHandler>(uri_handler).unsafe_cast_ref())
|
|
|
|
.to_glib_full()
|
2018-11-18 14:10:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 08:02:04 +00:00
|
|
|
unsafe extern "C" fn uri_handler_set_uri<T: URIHandlerImpl>(
|
2020-11-21 13:46:48 +00:00
|
|
|
uri_handler: *mut ffi::GstURIHandler,
|
2018-11-18 14:10:58 +00:00
|
|
|
uri: *const libc::c_char,
|
2020-11-21 13:46:48 +00:00
|
|
|
err: *mut *mut glib::ffi::GError,
|
|
|
|
) -> glib::ffi::gboolean {
|
2018-11-18 14:10:58 +00:00
|
|
|
let instance = &*(uri_handler as *mut T::Instance);
|
|
|
|
let imp = instance.get_impl();
|
|
|
|
|
2019-05-25 17:10:36 +00:00
|
|
|
match imp.set_uri(
|
2020-11-14 15:34:41 +00:00
|
|
|
&from_glib_borrow::<_, URIHandler>(uri_handler).unsafe_cast_ref(),
|
2019-05-25 17:10:36 +00:00
|
|
|
glib::GString::from_glib_borrow(uri).as_str(),
|
|
|
|
) {
|
2018-11-18 14:10:58 +00:00
|
|
|
Ok(()) => true.to_glib(),
|
|
|
|
Err(error) => {
|
|
|
|
*err = error.to_glib_full() as *mut _;
|
|
|
|
false.to_glib()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|