Make calls into the parent class struct safer and use more useful object wrapper type (T) for virtual methods

This commit is contained in:
Sebastian Dröge 2017-09-26 01:43:56 +03:00
parent a354116336
commit 41d6343682
8 changed files with 189 additions and 218 deletions

View file

@ -25,36 +25,32 @@ use gst_base::prelude::*;
use object::*;
use element::*;
pub trait BaseSinkImpl<T: ObjectType>
pub trait BaseSinkImpl<T: BaseSink>
: mopa::Any + ObjectImpl<T> + ElementImpl<T> + Send + Sync + 'static {
fn start(&self, _element: &gst_base::BaseSink) -> bool {
fn start(&self, _element: &T) -> bool {
true
}
fn stop(&self, _element: &gst_base::BaseSink) -> bool {
fn stop(&self, _element: &T) -> bool {
true
}
fn render(&self, element: &gst_base::BaseSink, buffer: &gst::BufferRef) -> gst::FlowReturn;
fn query(&self, element: &gst_base::BaseSink, query: &mut gst::QueryRef) -> bool {
fn render(&self, element: &T, buffer: &gst::BufferRef) -> gst::FlowReturn;
fn query(&self, element: &T, query: &mut gst::QueryRef) -> bool {
element.parent_query(query)
}
fn event(&self, element: &gst_base::BaseSink, event: &gst::Event) -> bool {
fn event(&self, element: &T, event: &gst::Event) -> bool {
element.parent_event(event)
}
}
mopafy_object_impl!(BaseSinkImpl);
mopafy_object_impl!(BaseSink, BaseSinkImpl);
pub unsafe trait BaseSink: IsA<gst_base::BaseSink> {
pub unsafe trait BaseSink
: IsA<gst::Element> + IsA<gst_base::BaseSink> + ObjectType {
fn parent_query(&self, query: &mut gst::QueryRef) -> bool {
unsafe {
// Our class
let klass = *(self.to_glib_none().0 as *const glib_ffi::gpointer);
// The parent class, RsElement or any other first-level Rust implementation
let parent_klass = gobject_ffi::g_type_class_peek_parent(klass);
// The actual parent class as defined in C
let parent_klass = &*(gobject_ffi::g_type_class_peek_parent(parent_klass) as
*const gst_base_ffi::GstBaseSinkClass);
parent_klass
let klass = self.get_class();
let parent_klass = (*klass).get_parent_class() as *const gst_base_ffi::GstBaseSinkClass;
(*parent_klass)
.query
.map(|f| from_glib(f(self.to_glib_none().0, query.as_mut_ptr())))
.unwrap_or(false)
@ -63,14 +59,9 @@ pub unsafe trait BaseSink: IsA<gst_base::BaseSink> {
fn parent_event(&self, event: &gst::Event) -> bool {
unsafe {
// Our class
let klass = *(self.to_glib_none().0 as *const glib_ffi::gpointer);
// The parent class, RsElement or any other first-level Rust implementation
let parent_klass = gobject_ffi::g_type_class_peek_parent(klass);
// The actual parent class as defined in C
let parent_klass = &*(gobject_ffi::g_type_class_peek_parent(parent_klass) as
*const gst_base_ffi::GstBaseSinkClass);
parent_klass
let klass = self.get_class();
let parent_klass = (*klass).get_parent_class() as *const gst_base_ffi::GstBaseSinkClass;
(*parent_klass)
.event
.map(|f| {
from_glib(f(self.to_glib_none().0, event.to_glib_none().0))
@ -80,9 +71,8 @@ pub unsafe trait BaseSink: IsA<gst_base::BaseSink> {
}
}
pub unsafe trait BaseSinkClass<T: ObjectType>
pub unsafe trait BaseSinkClass<T: BaseSink>
where
T: IsA<gst_base::BaseSink>,
T::ImplType: BaseSinkImpl<T>,
{
fn override_vfuncs(&mut self) {
@ -107,7 +97,7 @@ glib_wrapper! {
}
}
unsafe impl<T: IsA<gst_base::BaseSink>> BaseSink for T {}
unsafe impl<T: IsA<gst::Element> + IsA<gst_base::BaseSink> + ObjectType> BaseSink for T {}
pub type RsBaseSinkClass = ClassStruct<RsBaseSink>;
// FIXME: Boilerplate
@ -119,27 +109,27 @@ macro_rules! box_base_sink_impl(
($name:ident) => {
box_element_impl!($name);
impl<T: ObjectType> BaseSinkImpl<T> for Box<$name<T>> {
fn start(&self, element: &gst_base::BaseSink) -> bool {
impl<T: BaseSink> BaseSinkImpl<T> for Box<$name<T>> {
fn start(&self, element: &T) -> bool {
let imp: &$name<T> = self.as_ref();
imp.start(element)
}
fn stop(&self, element: &gst_base::BaseSink) -> bool {
fn stop(&self, element: &T) -> bool {
let imp: &$name<T> = self.as_ref();
imp.stop(element)
}
fn render(&self, element: &gst_base::BaseSink, buffer: &gst::BufferRef) -> gst::FlowReturn {
fn render(&self, element: &T, buffer: &gst::BufferRef) -> gst::FlowReturn {
let imp: &$name<T> = self.as_ref();
imp.render(element, buffer)
}
fn query(&self, element: &gst_base::BaseSink, query: &mut gst::QueryRef) -> bool {
fn query(&self, element: &T, query: &mut gst::QueryRef) -> bool {
let imp: &$name<T> = self.as_ref();
imp.query(element, query)
}
fn event(&self, element: &gst_base::BaseSink, event: &gst::Event) -> bool {
fn event(&self, element: &T, event: &gst::Event) -> bool {
let imp: &$name<T> = self.as_ref();
imp.event(element, event)
}
@ -163,52 +153,51 @@ impl ObjectType for RsBaseSink {
ElementClass::override_vfuncs(klass);
BaseSinkClass::override_vfuncs(klass);
}
object_type_fns!();
}
unsafe extern "C" fn base_sink_start<T: ObjectType>(
unsafe extern "C" fn base_sink_start<T: BaseSink>(
ptr: *mut gst_base_ffi::GstBaseSink,
) -> glib_ffi::gboolean
where
T: IsA<gst_base::BaseSink>,
T::ImplType: BaseSinkImpl<T>,
{
callback_guard!();
floating_reference_guard!(ptr);
let element = &*(ptr as *mut InstanceStruct<T>);
let wrap: gst_base::BaseSink = from_glib_borrow(ptr);
let wrap: T = from_glib_borrow(ptr as *mut InstanceStruct<T>);
let imp = &*element.imp;
panic_to_error!(&wrap, &element.panicked, false, { imp.start(&wrap) }).to_glib()
}
unsafe extern "C" fn base_sink_stop<T: ObjectType>(
unsafe extern "C" fn base_sink_stop<T: BaseSink>(
ptr: *mut gst_base_ffi::GstBaseSink,
) -> glib_ffi::gboolean
where
T: IsA<gst_base::BaseSink>,
T::ImplType: BaseSinkImpl<T>,
{
callback_guard!();
floating_reference_guard!(ptr);
let element = &*(ptr as *mut InstanceStruct<T>);
let wrap: gst_base::BaseSink = from_glib_borrow(ptr);
let wrap: T = from_glib_borrow(ptr as *mut InstanceStruct<T>);
let imp = &*element.imp;
panic_to_error!(&wrap, &element.panicked, false, { imp.stop(&wrap) }).to_glib()
}
unsafe extern "C" fn base_sink_render<T: ObjectType>(
unsafe extern "C" fn base_sink_render<T: BaseSink>(
ptr: *mut gst_base_ffi::GstBaseSink,
buffer: *mut gst_ffi::GstBuffer,
) -> gst_ffi::GstFlowReturn
where
T: IsA<gst_base::BaseSink>,
T::ImplType: BaseSinkImpl<T>,
{
callback_guard!();
floating_reference_guard!(ptr);
let element = &*(ptr as *mut InstanceStruct<T>);
let wrap: gst_base::BaseSink = from_glib_borrow(ptr);
let wrap: T = from_glib_borrow(ptr as *mut InstanceStruct<T>);
let imp = &*element.imp;
let buffer = gst::BufferRef::from_ptr(buffer);
@ -217,36 +206,34 @@ where
}).to_glib()
}
unsafe extern "C" fn base_sink_query<T: ObjectType>(
unsafe extern "C" fn base_sink_query<T: BaseSink>(
ptr: *mut gst_base_ffi::GstBaseSink,
query_ptr: *mut gst_ffi::GstQuery,
) -> glib_ffi::gboolean
where
T: IsA<gst_base::BaseSink>,
T::ImplType: BaseSinkImpl<T>,
{
callback_guard!();
floating_reference_guard!(ptr);
let element = &*(ptr as *mut InstanceStruct<T>);
let wrap: gst_base::BaseSink = from_glib_borrow(ptr);
let wrap: T = from_glib_borrow(ptr as *mut InstanceStruct<T>);
let imp = &*element.imp;
let query = gst::QueryRef::from_mut_ptr(query_ptr);
panic_to_error!(&wrap, &element.panicked, false, { imp.query(&wrap, query) }).to_glib()
}
unsafe extern "C" fn base_sink_event<T: ObjectType>(
unsafe extern "C" fn base_sink_event<T: BaseSink>(
ptr: *mut gst_base_ffi::GstBaseSink,
event_ptr: *mut gst_ffi::GstEvent,
) -> glib_ffi::gboolean
where
T: IsA<gst_base::BaseSink>,
T::ImplType: BaseSinkImpl<T>,
{
callback_guard!();
floating_reference_guard!(ptr);
let element = &*(ptr as *mut InstanceStruct<T>);
let wrap: gst_base::BaseSink = from_glib_borrow(ptr);
let wrap: T = from_glib_borrow(ptr as *mut InstanceStruct<T>);
let imp = &*element.imp;
panic_to_error!(&wrap, &element.panicked, false, {

View file

@ -25,51 +25,47 @@ use gst_base::prelude::*;
use object::*;
use element::*;
pub trait BaseSrcImpl<T: ObjectType>
pub trait BaseSrcImpl<T: BaseSrc>
: mopa::Any + ObjectImpl<T> + ElementImpl<T> + Send + Sync + 'static {
fn start(&self, _element: &gst_base::BaseSrc) -> bool {
fn start(&self, _element: &T) -> bool {
true
}
fn stop(&self, _element: &gst_base::BaseSrc) -> bool {
fn stop(&self, _element: &T) -> bool {
true
}
fn is_seekable(&self, _element: &gst_base::BaseSrc) -> bool {
fn is_seekable(&self, _element: &T) -> bool {
false
}
fn get_size(&self, _element: &gst_base::BaseSrc) -> Option<u64> {
fn get_size(&self, _element: &T) -> Option<u64> {
None
}
fn fill(
&self,
element: &gst_base::BaseSrc,
element: &T,
offset: u64,
length: u32,
buffer: &mut gst::BufferRef,
) -> gst::FlowReturn;
fn do_seek(&self, element: &gst_base::BaseSrc, segment: &mut gst::Segment) -> bool {
fn do_seek(&self, element: &T, segment: &mut gst::Segment) -> bool {
element.parent_do_seek(segment)
}
fn query(&self, element: &gst_base::BaseSrc, query: &mut gst::QueryRef) -> bool {
fn query(&self, element: &T, query: &mut gst::QueryRef) -> bool {
element.parent_query(query)
}
fn event(&self, element: &gst_base::BaseSrc, event: &gst::Event) -> bool {
fn event(&self, element: &T, event: &gst::Event) -> bool {
element.parent_event(event)
}
}
mopafy_object_impl!(BaseSrcImpl);
mopafy_object_impl!(BaseSrc, BaseSrcImpl);
pub unsafe trait BaseSrc: IsA<gst_base::BaseSrc> {
pub unsafe trait BaseSrc
: IsA<gst::Element> + IsA<gst_base::BaseSrc> + ObjectType {
fn parent_do_seek(&self, segment: &mut gst::Segment) -> bool {
unsafe {
// Our class
let klass = *(self.to_glib_none().0 as *const glib_ffi::gpointer);
// The parent class, RsElement or any other first-level Rust implementation
let parent_klass = gobject_ffi::g_type_class_peek_parent(klass);
// The actual parent class as defined in C
let parent_klass = &*(gobject_ffi::g_type_class_peek_parent(parent_klass) as
*const gst_base_ffi::GstBaseSrcClass);
parent_klass
let klass = self.get_class();
let parent_klass = (*klass).get_parent_class() as *const gst_base_ffi::GstBaseSrcClass;
(*parent_klass)
.do_seek
.map(|f| {
from_glib(f(self.to_glib_none().0, segment.to_glib_none_mut().0))
@ -80,14 +76,9 @@ pub unsafe trait BaseSrc: IsA<gst_base::BaseSrc> {
fn parent_query(&self, query: &mut gst::QueryRef) -> bool {
unsafe {
// Our class
let klass = *(self.to_glib_none().0 as *const glib_ffi::gpointer);
// The parent class, RsElement or any other first-level Rust implementation
let parent_klass = gobject_ffi::g_type_class_peek_parent(klass);
// The actual parent class as defined in C
let parent_klass = &*(gobject_ffi::g_type_class_peek_parent(parent_klass) as
*const gst_base_ffi::GstBaseSrcClass);
parent_klass
let klass = self.get_class();
let parent_klass = (*klass).get_parent_class() as *const gst_base_ffi::GstBaseSrcClass;
(*parent_klass)
.query
.map(|f| from_glib(f(self.to_glib_none().0, query.as_mut_ptr())))
.unwrap_or(false)
@ -96,14 +87,9 @@ pub unsafe trait BaseSrc: IsA<gst_base::BaseSrc> {
fn parent_event(&self, event: &gst::Event) -> bool {
unsafe {
// Our class
let klass = *(self.to_glib_none().0 as *const glib_ffi::gpointer);
// The parent class, RsElement or any other first-level Rust implementation
let parent_klass = gobject_ffi::g_type_class_peek_parent(klass);
// The actual parent class as defined in C
let parent_klass = &*(gobject_ffi::g_type_class_peek_parent(parent_klass) as
*const gst_base_ffi::GstBaseSrcClass);
parent_klass
let klass = self.get_class();
let parent_klass = (*klass).get_parent_class() as *const gst_base_ffi::GstBaseSrcClass;
(*parent_klass)
.event
.map(|f| {
from_glib(f(self.to_glib_none().0, event.to_glib_none().0))
@ -113,9 +99,8 @@ pub unsafe trait BaseSrc: IsA<gst_base::BaseSrc> {
}
}
pub unsafe trait BaseSrcClass<T: ObjectType>
pub unsafe trait BaseSrcClass<T: BaseSrc>
where
T: IsA<gst_base::BaseSrc>,
T::ImplType: BaseSrcImpl<T>,
{
fn override_vfuncs(&mut self) {
@ -144,7 +129,7 @@ glib_wrapper! {
}
// FIXME: This is wrong: it must be a basesrc but also backed by ObjectType<T>
unsafe impl<T: IsA<gst_base::BaseSrc>> BaseSrc for T {}
unsafe impl<T: IsA<gst::Element> + IsA<gst_base::BaseSrc> + ObjectType> BaseSrc for T {}
pub type RsBaseSrcClass = ClassStruct<RsBaseSrc>;
// FIXME: Boilerplate
@ -156,30 +141,30 @@ macro_rules! box_base_src_impl(
($name:ident) => {
box_element_impl!($name);
impl<T: ObjectType> BaseSrcImpl<T> for Box<$name<T>> {
fn start(&self, element: &gst_base::BaseSrc) -> bool {
impl<T: BaseSrc> BaseSrcImpl<T> for Box<$name<T>> {
fn start(&self, element: &T) -> bool {
let imp: &$name<T> = self.as_ref();
imp.start(element)
}
fn stop(&self, element: &gst_base::BaseSrc) -> bool {
fn stop(&self, element: &T) -> bool {
let imp: &$name<T> = self.as_ref();
imp.stop(element)
}
fn is_seekable(&self, element: &gst_base::BaseSrc) -> bool {
fn is_seekable(&self, element: &T) -> bool {
let imp: &$name<T> = self.as_ref();
imp.is_seekable(element)
}
fn get_size(&self, element: &gst_base::BaseSrc) -> Option<u64> {
fn get_size(&self, element: &T) -> Option<u64> {
let imp: &$name<T> = self.as_ref();
imp.get_size(element)
}
fn fill(
&self,
element: &gst_base::BaseSrc,
element: &T,
offset: u64,
length: u32,
buffer: &mut gst::BufferRef,
@ -188,16 +173,16 @@ macro_rules! box_base_src_impl(
imp.fill(element, offset, length, buffer)
}
fn do_seek(&self, element: &gst_base::BaseSrc, segment: &mut gst::Segment) -> bool {
fn do_seek(&self, element: &T, segment: &mut gst::Segment) -> bool {
let imp: &$name<T> = self.as_ref();
imp.do_seek(element, segment)
}
fn query(&self, element: &gst_base::BaseSrc, query: &mut gst::QueryRef) -> bool {
fn query(&self, element: &T, query: &mut gst::QueryRef) -> bool {
let imp: &$name<T> = self.as_ref();
imp.query(element, query)
}
fn event(&self, element: &gst_base::BaseSrc, event: &gst::Event) -> bool {
fn event(&self, element: &T, event: &gst::Event) -> bool {
let imp: &$name<T> = self.as_ref();
imp.event(element, event)
}
@ -220,68 +205,66 @@ impl ObjectType for RsBaseSrc {
ElementClass::override_vfuncs(klass);
BaseSrcClass::override_vfuncs(klass);
}
object_type_fns!();
}
unsafe extern "C" fn base_src_start<T: ObjectType>(
unsafe extern "C" fn base_src_start<T: BaseSrc>(
ptr: *mut gst_base_ffi::GstBaseSrc,
) -> glib_ffi::gboolean
where
T: IsA<gst_base::BaseSrc>,
T::ImplType: BaseSrcImpl<T>,
{
callback_guard!();
floating_reference_guard!(ptr);
let element = &*(ptr as *mut InstanceStruct<T>);
let wrap: gst_base::BaseSrc = from_glib_borrow(ptr);
let wrap: T = from_glib_borrow(ptr as *mut InstanceStruct<T>);
let imp = &*element.imp;
panic_to_error!(&wrap, &element.panicked, false, { imp.start(&wrap) }).to_glib()
}
unsafe extern "C" fn base_src_stop<T: ObjectType>(
unsafe extern "C" fn base_src_stop<T: BaseSrc>(
ptr: *mut gst_base_ffi::GstBaseSrc,
) -> glib_ffi::gboolean
where
T: IsA<gst_base::BaseSrc>,
T::ImplType: BaseSrcImpl<T>,
{
callback_guard!();
floating_reference_guard!(ptr);
let element = &*(ptr as *mut InstanceStruct<T>);
let wrap: gst_base::BaseSrc = from_glib_borrow(ptr);
let wrap: T = from_glib_borrow(ptr as *mut InstanceStruct<T>);
let imp = &*element.imp;
panic_to_error!(&wrap, &element.panicked, false, { imp.stop(&wrap) }).to_glib()
}
unsafe extern "C" fn base_src_is_seekable<T: ObjectType>(
unsafe extern "C" fn base_src_is_seekable<T: BaseSrc>(
ptr: *mut gst_base_ffi::GstBaseSrc,
) -> glib_ffi::gboolean
where
T: IsA<gst_base::BaseSrc>,
T::ImplType: BaseSrcImpl<T>,
{
callback_guard!();
floating_reference_guard!(ptr);
let element = &*(ptr as *mut InstanceStruct<T>);
let wrap: gst_base::BaseSrc = from_glib_borrow(ptr);
let wrap: T = from_glib_borrow(ptr as *mut InstanceStruct<T>);
let imp = &*element.imp;
panic_to_error!(&wrap, &element.panicked, false, { imp.is_seekable(&wrap) }).to_glib()
}
unsafe extern "C" fn base_src_get_size<T: ObjectType>(
unsafe extern "C" fn base_src_get_size<T: BaseSrc>(
ptr: *mut gst_base_ffi::GstBaseSrc,
size: *mut u64,
) -> glib_ffi::gboolean
where
T: IsA<gst_base::BaseSrc>,
T::ImplType: BaseSrcImpl<T>,
{
callback_guard!();
floating_reference_guard!(ptr);
let element = &*(ptr as *mut InstanceStruct<T>);
let wrap: gst_base::BaseSrc = from_glib_borrow(ptr);
let wrap: T = from_glib_borrow(ptr as *mut InstanceStruct<T>);
let imp = &*element.imp;
panic_to_error!(&wrap, &element.panicked, false, {
@ -295,20 +278,19 @@ where
}).to_glib()
}
unsafe extern "C" fn base_src_fill<T: ObjectType>(
unsafe extern "C" fn base_src_fill<T: BaseSrc>(
ptr: *mut gst_base_ffi::GstBaseSrc,
offset: u64,
length: u32,
buffer: *mut gst_ffi::GstBuffer,
) -> gst_ffi::GstFlowReturn
where
T: IsA<gst_base::BaseSrc>,
T::ImplType: BaseSrcImpl<T>,
{
callback_guard!();
floating_reference_guard!(ptr);
let element = &*(ptr as *mut InstanceStruct<T>);
let wrap: gst_base::BaseSrc = from_glib_borrow(ptr);
let wrap: T = from_glib_borrow(ptr as *mut InstanceStruct<T>);
let imp = &*element.imp;
let buffer = gst::BufferRef::from_mut_ptr(buffer);
@ -317,18 +299,17 @@ where
}).to_glib()
}
unsafe extern "C" fn base_src_do_seek<T: ObjectType>(
unsafe extern "C" fn base_src_do_seek<T: BaseSrc>(
ptr: *mut gst_base_ffi::GstBaseSrc,
segment: *mut gst_ffi::GstSegment,
) -> glib_ffi::gboolean
where
T: IsA<gst_base::BaseSrc>,
T::ImplType: BaseSrcImpl<T>,
{
callback_guard!();
floating_reference_guard!(ptr);
let element = &*(ptr as *mut InstanceStruct<T>);
let wrap: gst_base::BaseSrc = from_glib_borrow(ptr);
let wrap: T = from_glib_borrow(ptr as *mut InstanceStruct<T>);
let imp = &*element.imp;
panic_to_error!(&wrap, &element.panicked, false, {
@ -336,36 +317,34 @@ where
}).to_glib()
}
unsafe extern "C" fn base_src_query<T: ObjectType>(
unsafe extern "C" fn base_src_query<T: BaseSrc>(
ptr: *mut gst_base_ffi::GstBaseSrc,
query_ptr: *mut gst_ffi::GstQuery,
) -> glib_ffi::gboolean
where
T: IsA<gst_base::BaseSrc>,
T::ImplType: BaseSrcImpl<T>,
{
callback_guard!();
floating_reference_guard!(ptr);
let element = &*(ptr as *mut InstanceStruct<T>);
let wrap: gst_base::BaseSrc = from_glib_borrow(ptr);
let wrap: T = from_glib_borrow(ptr as *mut InstanceStruct<T>);
let imp = &*element.imp;
let query = gst::QueryRef::from_mut_ptr(query_ptr);
panic_to_error!(&wrap, &element.panicked, false, { imp.query(&wrap, query) }).to_glib()
}
unsafe extern "C" fn base_src_event<T: ObjectType>(
unsafe extern "C" fn base_src_event<T: BaseSrc>(
ptr: *mut gst_base_ffi::GstBaseSrc,
event_ptr: *mut gst_ffi::GstEvent,
) -> glib_ffi::gboolean
where
T: IsA<gst_base::BaseSrc>,
T::ImplType: BaseSrcImpl<T>,
{
callback_guard!();
floating_reference_guard!(ptr);
let element = &*(ptr as *mut InstanceStruct<T>);
let wrap: gst_base::BaseSrc = from_glib_borrow(ptr);
let wrap: T = from_glib_borrow(ptr as *mut InstanceStruct<T>);
let imp = &*element.imp;
panic_to_error!(&wrap, &element.panicked, false, {

View file

@ -643,7 +643,7 @@ impl ObjectImpl<RsElement> for Demuxer {}
impl ElementImpl<RsElement> for Demuxer {
fn change_state(
&self,
element: &gst::Element,
element: &RsElement,
transition: gst::StateChange,
) -> gst::StateChangeReturn {
let mut ret = gst::StateChangeReturn::Success;

View file

@ -21,30 +21,21 @@ use gst::prelude::*;
use object::*;
pub trait ElementImpl<T: ObjectType>
pub trait ElementImpl<T: Element>
: ObjectImpl<T> + mopa::Any + Send + Sync + 'static {
fn change_state(
&self,
element: &gst::Element,
transition: gst::StateChange,
) -> gst::StateChangeReturn {
fn change_state(&self, element: &T, transition: gst::StateChange) -> gst::StateChangeReturn {
element.parent_change_state(transition)
}
}
mopafy_object_impl!(ElementImpl);
mopafy_object_impl!(Element, ElementImpl);
pub unsafe trait Element: IsA<gst::Element> {
pub unsafe trait Element: IsA<gst::Element> + ObjectType {
fn parent_change_state(&self, transition: gst::StateChange) -> gst::StateChangeReturn {
unsafe {
// Our class
let klass = *(self.to_glib_none().0 as *const glib_ffi::gpointer);
// The parent class, RsElement or any other first-level Rust implementation
let parent_klass = gobject_ffi::g_type_class_peek_parent(klass);
// The actual parent class as defined in C
let parent_klass = &*(gobject_ffi::g_type_class_peek_parent(parent_klass) as
*const gst_ffi::GstElementClass);
parent_klass
let klass = self.get_class();
let parent_klass = (*klass).get_parent_class() as *const gst_ffi::GstElementClass;
(*parent_klass)
.change_state
.map(|f| {
from_glib(f(self.to_glib_none().0, transition.to_glib()))
@ -54,9 +45,8 @@ pub unsafe trait Element: IsA<gst::Element> {
}
}
pub unsafe trait ElementClass<T: ObjectType>
pub unsafe trait ElementClass<T: Element>
where
T: IsA<gst::Element>,
T::ImplType: ElementImpl<T>,
{
fn add_pad_template(&mut self, pad_template: gst::PadTemplate) {
@ -103,17 +93,7 @@ glib_wrapper! {
}
}
impl RsElement {
pub fn get_impl(&self) -> &ElementImpl<RsElement> {
unsafe {
let stash = self.to_glib_none();
let ptr: *mut InstanceStruct<RsElement> = stash.0;
(*ptr).get_impl().as_ref()
}
}
}
unsafe impl<T: IsA<gst::Element>> Element for T {}
unsafe impl<T: IsA<gst::Element> + ObjectType> Element for T {}
pub type RsElementClass = ClassStruct<RsElement>;
// FIXME: Boilerplate
@ -124,10 +104,10 @@ macro_rules! box_element_impl(
($name:ident) => {
box_object_impl!($name);
impl<T: ObjectType> ElementImpl<T> for Box<$name<T>> {
impl<T: Element> ElementImpl<T> for Box<$name<T>> {
fn change_state(
&self,
element: &gst::Element,
element: &T,
transition: gst::StateChange,
) -> gst::StateChangeReturn {
let imp: &$name<T> = self.as_ref();
@ -152,20 +132,21 @@ impl ObjectType for RsElement {
fn class_init(klass: &mut RsElementClass) {
klass.override_vfuncs();
}
object_type_fns!();
}
unsafe extern "C" fn element_change_state<T: ObjectType>(
unsafe extern "C" fn element_change_state<T: Element>(
ptr: *mut gst_ffi::GstElement,
transition: gst_ffi::GstStateChange,
) -> gst_ffi::GstStateChangeReturn
where
T: IsA<gst::Element>,
T::ImplType: ElementImpl<T>,
{
callback_guard!();
floating_reference_guard!(ptr);
let element = &*(ptr as *mut InstanceStruct<T>);
let wrap: gst::Element = from_glib_borrow(ptr);
let wrap: T = from_glib_borrow(ptr as *mut InstanceStruct<T>);
let imp = &*element.imp;
panic_to_error!(&wrap, &element.panicked, gst::StateChangeReturn::Failure, {

View file

@ -12,7 +12,6 @@ extern crate gstreamer_base_sys as gst_base_ffi;
#[macro_use]
extern crate lazy_static;
extern crate libc;
#[macro_use]
extern crate mopa;
extern crate url;
pub extern crate glib_sys as glib_ffi;
@ -62,8 +61,8 @@ impl Drop for FloatingReferenceGuard {
// mopafy! macro to work with generic traits over T: ObjectType
macro_rules! mopafy_object_impl {
($trait:ident) => {
impl<T: ObjectType> $trait<T> {
($bound:ident, $trait:ident) => {
impl<T: $bound> $trait<T> {
#[inline]
pub fn downcast_ref<U: $trait<T>>(&self) -> Option<&U> {
if self.is::<U>() {

View file

@ -84,8 +84,27 @@ where
fn get_property(_obj: &Self, _id: u32) -> Result<glib::Value, ()> {
unimplemented!()
}
unsafe fn get_instance(&self) -> *mut InstanceStruct<Self>;
fn get_impl(&self) -> &Self::ImplType {
unsafe { (*self.get_instance()).get_impl() }
}
unsafe fn get_class(&self) -> *const ClassStruct<Self> {
(*self.get_instance()).get_class()
}
}
#[macro_export]
macro_rules! object_type_fns(
() => {
unsafe fn get_instance(&self) -> *mut InstanceStruct<Self> {
self.to_glib_none().0
}
}
);
#[repr(C)]
pub struct InstanceStruct<T: ObjectType> {
pub parent: T::GlibType,
@ -97,15 +116,26 @@ impl<T: ObjectType> InstanceStruct<T> {
pub fn get_impl(&self) -> &T::ImplType {
unsafe { &*self.imp }
}
pub unsafe fn get_class(&self) -> *const ClassStruct<T> {
*(self as *const _ as *const *const ClassStruct<T>)
}
}
#[repr(C)]
pub struct ClassStruct<T: ObjectType> {
pub parent: T::GlibClassType,
pub imp_static: *const Box<ImplTypeStatic<T>>,
pub parent_class: *const T::GlibClassType,
pub interfaces_static: *const Vec<(glib_ffi::GType, glib_ffi::gpointer)>,
}
impl<T: ObjectType> ClassStruct<T> {
pub unsafe fn get_parent_class(&self) -> *const T::GlibClassType {
self.parent_class
}
}
impl<T: ObjectType> ClassStruct<T> {
pub fn get_interface_static(&self, type_: glib_ffi::GType) -> glib_ffi::gpointer {
unsafe {
@ -346,7 +376,13 @@ unsafe extern "C" fn class_init<T: ObjectType>(
gobject_klass.get_property = Some(get_property::<T>);
}
T::class_init(&mut *(klass as *mut ClassStruct<T>));
{
let klass = &mut *(klass as *mut ClassStruct<T>);
klass.parent_class = gobject_ffi::g_type_class_peek_parent(
klass as *mut _ as glib_ffi::gpointer,
) as *const T::GlibClassType;
T::class_init(klass);
}
}
unsafe extern "C" fn finalize<T: ObjectType>(obj: *mut gobject_ffi::GObject) {

View file

@ -158,10 +158,8 @@ impl ObjectImpl<RsBaseSink> for Sink {
impl ElementImpl<RsBaseSink> for Sink {}
impl BaseSinkImpl<RsBaseSink> for Sink {
fn start(&self, element: &gst_base::BaseSink) -> bool {
let sink = element.clone().downcast::<RsBaseSink>().unwrap();
gst_debug!(self.cat, obj: &sink, "Starting");
fn start(&self, sink: &RsBaseSink) -> bool {
gst_debug!(self.cat, obj: sink, "Starting");
// Don't keep the URI locked while we call start later
let uri = match *self.uri.lock().unwrap() {
@ -170,62 +168,60 @@ impl BaseSinkImpl<RsBaseSink> for Sink {
uri.clone()
}
(None, _) => {
gst_error!(self.cat, obj: &sink, "No URI given");
error_msg!(gst::ResourceError::OpenRead, ["No URI given"]).post(&sink);
gst_error!(self.cat, obj: sink, "No URI given");
error_msg!(gst::ResourceError::OpenRead, ["No URI given"]).post(sink);
return false;
}
};
let sink_impl = &mut self.imp.lock().unwrap();
match sink_impl.start(&sink, uri) {
match sink_impl.start(sink, uri) {
Ok(..) => {
gst_trace!(self.cat, obj: &sink, "Started successfully");
gst_trace!(self.cat, obj: sink, "Started successfully");
true
}
Err(ref msg) => {
gst_error!(self.cat, obj: &sink, "Failed to start: {:?}", msg);
gst_error!(self.cat, obj: sink, "Failed to start: {:?}", msg);
self.uri.lock().unwrap().1 = false;
msg.post(&sink);
msg.post(sink);
false
}
}
}
fn stop(&self, element: &gst_base::BaseSink) -> bool {
let sink = element.clone().downcast::<RsBaseSink>().unwrap();
fn stop(&self, sink: &RsBaseSink) -> bool {
let sink_impl = &mut self.imp.lock().unwrap();
gst_debug!(self.cat, obj: &sink, "Stopping");
gst_debug!(self.cat, obj: sink, "Stopping");
match sink_impl.stop(&sink) {
match sink_impl.stop(sink) {
Ok(..) => {
gst_trace!(self.cat, obj: &sink, "Stopped successfully");
gst_trace!(self.cat, obj: sink, "Stopped successfully");
self.uri.lock().unwrap().1 = false;
true
}
Err(ref msg) => {
gst_error!(self.cat, obj: &sink, "Failed to stop: {:?}", msg);
gst_error!(self.cat, obj: sink, "Failed to stop: {:?}", msg);
msg.post(&sink);
msg.post(sink);
false
}
}
}
fn render(&self, element: &gst_base::BaseSink, buffer: &gst::BufferRef) -> gst::FlowReturn {
let sink = element.clone().downcast::<RsBaseSink>().unwrap();
fn render(&self, sink: &RsBaseSink, buffer: &gst::BufferRef) -> gst::FlowReturn {
let sink_impl = &mut self.imp.lock().unwrap();
gst_trace!(self.cat, obj: &sink, "Rendering buffer {:?}", buffer,);
gst_trace!(self.cat, obj: sink, "Rendering buffer {:?}", buffer,);
match sink_impl.render(&sink, buffer) {
match sink_impl.render(sink, buffer) {
Ok(()) => gst::FlowReturn::Ok,
Err(flow_error) => {
gst_error!(self.cat, obj: &sink, "Failed to render: {:?}", flow_error);
gst_error!(self.cat, obj: sink, "Failed to render: {:?}", flow_error);
match flow_error {
FlowError::NotNegotiated(ref msg) | FlowError::Error(ref msg) => {
msg.post(&sink);
msg.post(sink);
}
_ => (),
}

View file

@ -172,10 +172,8 @@ impl ObjectImpl<RsBaseSrc> for Source {
impl ElementImpl<RsBaseSrc> for Source {}
impl BaseSrcImpl<RsBaseSrc> for Source {
fn start(&self, element: &gst_base::BaseSrc) -> bool {
let src = element.clone().downcast::<RsBaseSrc>().unwrap();
gst_debug!(self.cat, obj: &src, "Starting");
fn start(&self, src: &RsBaseSrc) -> bool {
gst_debug!(self.cat, obj: src, "Starting");
// Don't keep the URI locked while we call start later
let uri = match *self.uri.lock().unwrap() {
@ -184,50 +182,49 @@ impl BaseSrcImpl<RsBaseSrc> for Source {
uri.clone()
}
(None, _) => {
gst_error!(self.cat, obj: &src, "No URI given");
error_msg!(gst::ResourceError::OpenRead, ["No URI given"]).post(&src);
gst_error!(self.cat, obj: src, "No URI given");
error_msg!(gst::ResourceError::OpenRead, ["No URI given"]).post(src);
return false;
}
};
let source_impl = &mut self.imp.lock().unwrap();
match source_impl.start(&src, uri) {
match source_impl.start(src, uri) {
Ok(..) => {
gst_trace!(self.cat, obj: &src, "Started successfully");
gst_trace!(self.cat, obj: src, "Started successfully");
true
}
Err(ref msg) => {
gst_error!(self.cat, obj: &src, "Failed to start: {:?}", msg);
gst_error!(self.cat, obj: src, "Failed to start: {:?}", msg);
self.uri.lock().unwrap().1 = false;
msg.post(&src);
msg.post(src);
false
}
}
}
fn stop(&self, element: &gst_base::BaseSrc) -> bool {
let src = element.clone().downcast::<RsBaseSrc>().unwrap();
fn stop(&self, src: &RsBaseSrc) -> bool {
let source_impl = &mut self.imp.lock().unwrap();
gst_debug!(self.cat, obj: &src, "Stopping");
gst_debug!(self.cat, obj: src, "Stopping");
match source_impl.stop(&src) {
match source_impl.stop(src) {
Ok(..) => {
gst_trace!(self.cat, obj: &src, "Stopped successfully");
gst_trace!(self.cat, obj: src, "Stopped successfully");
self.uri.lock().unwrap().1 = false;
true
}
Err(ref msg) => {
gst_error!(self.cat, obj: &src, "Failed to stop: {:?}", msg);
gst_error!(self.cat, obj: src, "Failed to stop: {:?}", msg);
msg.post(&src);
msg.post(src);
false
}
}
}
fn query(&self, element: &gst_base::BaseSrc, query: &mut gst::QueryRef) -> bool {
fn query(&self, src: &RsBaseSrc, query: &mut gst::QueryRef) -> bool {
use gst::QueryView;
match query.view_mut() {
@ -239,35 +236,34 @@ impl BaseSrcImpl<RsBaseSrc> for Source {
_ => (),
}
element.parent_query(query)
src.parent_query(query)
}
fn fill(
&self,
element: &gst_base::BaseSrc,
src: &RsBaseSrc,
offset: u64,
length: u32,
buffer: &mut gst::BufferRef,
) -> gst::FlowReturn {
let src = element.clone().downcast::<RsBaseSrc>().unwrap();
let source_impl = &mut self.imp.lock().unwrap();
gst_trace!(
self.cat,
obj: &src,
obj: src,
"Filling buffer {:?} with offset {} and length {}",
buffer,
offset,
length
);
match source_impl.fill(&src, offset, length, buffer) {
match source_impl.fill(src, offset, length, buffer) {
Ok(()) => gst::FlowReturn::Ok,
Err(flow_error) => {
gst_error!(self.cat, obj: &src, "Failed to fill: {:?}", flow_error);
gst_error!(self.cat, obj: src, "Failed to fill: {:?}", flow_error);
match flow_error {
FlowError::NotNegotiated(ref msg) | FlowError::Error(ref msg) => {
msg.post(&src);
msg.post(src);
}
_ => (),
}
@ -276,8 +272,7 @@ impl BaseSrcImpl<RsBaseSrc> for Source {
}
}
fn do_seek(&self, element: &gst_base::BaseSrc, segment: &mut gst::Segment) -> bool {
let src = element.clone().downcast::<RsBaseSrc>().unwrap();
fn do_seek(&self, src: &RsBaseSrc, segment: &mut gst::Segment) -> bool {
let source_impl = &mut self.imp.lock().unwrap();
let start = segment.get_start();
@ -286,28 +281,26 @@ impl BaseSrcImpl<RsBaseSrc> for Source {
stop @ _ => Some(stop),
};
gst_debug!(self.cat, obj: &src, "Seeking to {:?}-{:?}", start, stop);
gst_debug!(self.cat, obj: src, "Seeking to {:?}-{:?}", start, stop);
match source_impl.seek(&src, start, stop) {
match source_impl.seek(src, start, stop) {
Ok(..) => true,
Err(ref msg) => {
gst_error!(self.cat, obj: &src, "Failed to seek {:?}", msg);
msg.post(&src);
gst_error!(self.cat, obj: src, "Failed to seek {:?}", msg);
msg.post(src);
false
}
}
}
fn is_seekable(&self, element: &gst_base::BaseSrc) -> bool {
let src = element.clone().downcast::<RsBaseSrc>().unwrap();
fn is_seekable(&self, src: &RsBaseSrc) -> bool {
let source_impl = &self.imp.lock().unwrap();
source_impl.is_seekable(&src)
source_impl.is_seekable(src)
}
fn get_size(&self, element: &gst_base::BaseSrc) -> Option<u64> {
let src = element.clone().downcast::<RsBaseSrc>().unwrap();
fn get_size(&self, src: &RsBaseSrc) -> Option<u64> {
let source_impl = &self.imp.lock().unwrap();
source_impl.get_size(&src)
source_impl.get_size(src)
}
}