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-20 12:38:20 +00:00
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
use glib::prelude::*;
|
2018-11-20 12:38:20 +00:00
|
|
|
use glib::subclass::prelude::*;
|
2020-11-14 15:34:41 +00:00
|
|
|
use glib::translate::*;
|
2018-11-20 12:38:20 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::Pad;
|
2018-11-20 12:38:20 +00:00
|
|
|
|
2020-07-25 08:02:04 +00:00
|
|
|
pub trait PadImpl: PadImplExt + ObjectImpl + Send + Sync {
|
2020-11-14 15:34:41 +00:00
|
|
|
fn linked(&self, pad: &Self::Type, peer: &Pad) {
|
2018-11-20 12:38:20 +00:00
|
|
|
self.parent_linked(pad, peer)
|
|
|
|
}
|
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
fn unlinked(&self, pad: &Self::Type, peer: &Pad) {
|
2018-11-20 12:38:20 +00:00
|
|
|
self.parent_unlinked(pad, peer)
|
|
|
|
}
|
2019-02-13 09:57:04 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
pub trait PadImplExt: ObjectSubclass {
|
|
|
|
fn parent_linked(&self, pad: &Self::Type, peer: &Pad);
|
2019-02-13 09:57:04 +00:00
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
fn parent_unlinked(&self, pad: &Self::Type, peer: &Pad);
|
2019-02-13 09:57:04 +00:00
|
|
|
}
|
2018-11-20 12:38:20 +00:00
|
|
|
|
2020-07-25 08:02:04 +00:00
|
|
|
impl<T: PadImpl> PadImplExt for T {
|
2020-11-14 15:34:41 +00:00
|
|
|
fn parent_linked(&self, pad: &Self::Type, peer: &Pad) {
|
2018-11-20 12:38:20 +00:00
|
|
|
unsafe {
|
2021-04-29 20:06:41 +00:00
|
|
|
let data = Self::type_data();
|
2021-04-11 19:39:50 +00:00
|
|
|
let parent_class = data.as_ref().parent_class() as *mut ffi::GstPadClass;
|
2018-11-20 12:38:20 +00:00
|
|
|
|
|
|
|
(*parent_class)
|
|
|
|
.linked
|
2020-11-14 15:34:41 +00:00
|
|
|
.map(|f| {
|
|
|
|
f(
|
|
|
|
pad.unsafe_cast_ref::<Pad>().to_glib_none().0,
|
|
|
|
peer.to_glib_none().0,
|
|
|
|
)
|
|
|
|
})
|
2018-11-20 12:38:20 +00:00
|
|
|
.unwrap_or(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
fn parent_unlinked(&self, pad: &Self::Type, peer: &Pad) {
|
2018-11-20 12:38:20 +00:00
|
|
|
unsafe {
|
2021-04-29 20:06:41 +00:00
|
|
|
let data = Self::type_data();
|
2021-04-11 19:39:50 +00:00
|
|
|
let parent_class = data.as_ref().parent_class() as *mut ffi::GstPadClass;
|
2018-11-20 12:38:20 +00:00
|
|
|
|
|
|
|
(*parent_class)
|
|
|
|
.unlinked
|
2020-11-14 15:34:41 +00:00
|
|
|
.map(|f| {
|
|
|
|
f(
|
|
|
|
pad.unsafe_cast_ref::<Pad>().to_glib_none().0,
|
|
|
|
peer.to_glib_none().0,
|
|
|
|
)
|
|
|
|
})
|
2018-11-20 12:38:20 +00:00
|
|
|
.unwrap_or(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 13:18:19 +00:00
|
|
|
unsafe impl<T: PadImpl> IsSubclassable<T> for Pad {
|
2021-03-08 10:06:56 +00:00
|
|
|
fn class_init(klass: &mut glib::Class<Self>) {
|
2021-08-24 05:55:37 +00:00
|
|
|
Self::parent_class_init::<T>(klass);
|
2020-11-05 17:07:31 +00:00
|
|
|
let klass = klass.as_mut();
|
|
|
|
klass.linked = Some(pad_linked::<T>);
|
|
|
|
klass.unlinked = Some(pad_unlinked::<T>);
|
2018-11-20 12:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe extern "C" fn pad_linked<T: PadImpl>(ptr: *mut ffi::GstPad, peer: *mut ffi::GstPad) {
|
2018-11-20 12:38:20 +00:00
|
|
|
let instance = &*(ptr as *mut T::Instance);
|
2021-04-11 19:39:50 +00:00
|
|
|
let imp = instance.impl_();
|
2020-04-05 14:52:56 +00:00
|
|
|
let wrap: Borrowed<Pad> = from_glib_borrow(ptr);
|
2018-11-20 12:38:20 +00:00
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
imp.linked(wrap.unsafe_cast_ref(), &from_glib_borrow(peer))
|
2018-11-20 12:38:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe extern "C" fn pad_unlinked<T: PadImpl>(ptr: *mut ffi::GstPad, peer: *mut ffi::GstPad) {
|
2018-11-20 12:38:20 +00:00
|
|
|
let instance = &*(ptr as *mut T::Instance);
|
2021-04-11 19:39:50 +00:00
|
|
|
let imp = instance.impl_();
|
2020-04-05 14:52:56 +00:00
|
|
|
let wrap: Borrowed<Pad> = from_glib_borrow(ptr);
|
2018-11-20 12:38:20 +00:00
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
imp.unlinked(wrap.unsafe_cast_ref(), &from_glib_borrow(peer))
|
2018-11-20 12:38:20 +00:00
|
|
|
}
|
2019-05-11 13:43:52 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::prelude::*;
|
2019-12-22 07:59:23 +00:00
|
|
|
use std::sync::atomic;
|
2019-05-11 13:43:52 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::PadDirection;
|
2020-11-14 15:34:41 +00:00
|
|
|
|
2020-11-14 14:59:43 +00:00
|
|
|
pub mod imp {
|
|
|
|
use super::*;
|
|
|
|
|
2021-03-07 11:08:06 +00:00
|
|
|
#[derive(Default)]
|
2020-11-14 14:59:43 +00:00
|
|
|
pub struct TestPad {
|
|
|
|
pub(super) linked: atomic::AtomicBool,
|
|
|
|
pub(super) unlinked: atomic::AtomicBool,
|
|
|
|
}
|
2019-05-11 13:43:52 +00:00
|
|
|
|
2021-03-07 11:08:06 +00:00
|
|
|
#[glib::object_subclass]
|
2020-11-14 14:59:43 +00:00
|
|
|
impl ObjectSubclass for TestPad {
|
|
|
|
const NAME: &'static str = "TestPad";
|
|
|
|
type Type = super::TestPad;
|
2020-11-14 15:34:41 +00:00
|
|
|
type ParentType = Pad;
|
2019-05-11 13:43:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 14:59:43 +00:00
|
|
|
impl ObjectImpl for TestPad {}
|
|
|
|
|
|
|
|
impl PadImpl for TestPad {
|
2020-11-14 15:34:41 +00:00
|
|
|
fn linked(&self, pad: &Self::Type, peer: &Pad) {
|
2020-11-14 14:59:43 +00:00
|
|
|
self.linked.store(true, atomic::Ordering::SeqCst);
|
|
|
|
self.parent_linked(pad, peer)
|
|
|
|
}
|
2019-05-11 13:43:52 +00:00
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
fn unlinked(&self, pad: &Self::Type, peer: &Pad) {
|
2020-11-14 14:59:43 +00:00
|
|
|
self.unlinked.store(true, atomic::Ordering::SeqCst);
|
|
|
|
self.parent_unlinked(pad, peer)
|
|
|
|
}
|
2019-05-11 13:43:52 +00:00
|
|
|
}
|
2020-11-14 14:59:43 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 22:38:06 +00:00
|
|
|
glib::wrapper! {
|
2020-11-21 13:46:48 +00:00
|
|
|
pub struct TestPad(ObjectSubclass<imp::TestPad>) @extends Pad, crate::Object;
|
2020-11-14 14:59:43 +00:00
|
|
|
}
|
2019-05-11 13:43:52 +00:00
|
|
|
|
2020-11-14 14:59:43 +00:00
|
|
|
unsafe impl Send for TestPad {}
|
|
|
|
unsafe impl Sync for TestPad {}
|
|
|
|
|
|
|
|
impl TestPad {
|
2020-11-14 15:34:41 +00:00
|
|
|
pub fn new(name: &str, direction: PadDirection) -> Self {
|
2020-12-17 15:26:47 +00:00
|
|
|
glib::Object::new(&[("name", &name), ("direction", &direction)]).unwrap()
|
2019-05-11 13:43:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pad_subclass() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2019-05-11 13:43:52 +00:00
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
let pad = TestPad::new("test", PadDirection::Src);
|
2019-05-11 13:43:52 +00:00
|
|
|
|
2021-04-11 19:39:50 +00:00
|
|
|
assert_eq!(pad.name(), "test");
|
2019-05-11 13:43:52 +00:00
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
let otherpad = Pad::new(Some("other-test"), PadDirection::Sink);
|
2019-05-11 13:43:52 +00:00
|
|
|
pad.link(&otherpad).unwrap();
|
|
|
|
pad.unlink(&otherpad).unwrap();
|
|
|
|
|
2020-11-14 14:59:43 +00:00
|
|
|
let imp = imp::TestPad::from_instance(&pad);
|
2019-12-22 07:59:23 +00:00
|
|
|
assert!(imp.linked.load(atomic::Ordering::SeqCst));
|
|
|
|
assert!(imp.unlinked.load(atomic::Ordering::SeqCst));
|
2019-05-11 13:43:52 +00:00
|
|
|
}
|
|
|
|
}
|