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
|
|
|
|
2023-01-03 18:58:25 +00:00
|
|
|
use glib::{prelude::*, subclass::prelude::*, translate::*};
|
2018-11-20 12:38:20 +00:00
|
|
|
|
2023-01-03 18:58:25 +00:00
|
|
|
use super::prelude::*;
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::Pad;
|
2018-11-20 12:38:20 +00:00
|
|
|
|
2021-08-24 06:12:27 +00:00
|
|
|
pub trait PadImpl: PadImplExt + GstObjectImpl + Send + Sync {
|
2022-10-08 18:50:06 +00:00
|
|
|
fn linked(&self, peer: &Pad) {
|
|
|
|
self.parent_linked(peer)
|
2018-11-20 12:38:20 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 18:50:06 +00:00
|
|
|
fn unlinked(&self, peer: &Pad) {
|
|
|
|
self.parent_unlinked(peer)
|
2018-11-20 12:38:20 +00:00
|
|
|
}
|
2019-02-13 09:57:04 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 15:34:41 +00:00
|
|
|
pub trait PadImplExt: ObjectSubclass {
|
2022-10-08 18:50:06 +00:00
|
|
|
fn parent_linked(&self, peer: &Pad);
|
2019-02-13 09:57:04 +00:00
|
|
|
|
2022-10-08 18:50:06 +00:00
|
|
|
fn parent_unlinked(&self, 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 {
|
2022-10-08 18:50:06 +00:00
|
|
|
fn parent_linked(&self, 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(
|
2022-10-23 19:59:23 +00:00
|
|
|
self.obj().unsafe_cast_ref::<Pad>().to_glib_none().0,
|
2020-11-14 15:34:41 +00:00
|
|
|
peer.to_glib_none().0,
|
|
|
|
)
|
|
|
|
})
|
2018-11-20 12:38:20 +00:00
|
|
|
.unwrap_or(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-08 18:50:06 +00:00
|
|
|
fn parent_unlinked(&self, 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(
|
2022-10-23 19:59:23 +00:00
|
|
|
self.obj().unsafe_cast_ref::<Pad>().to_glib_none().0,
|
2020-11-14 15:34:41 +00:00
|
|
|
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-12-28 15:50:06 +00:00
|
|
|
let imp = instance.imp();
|
2018-11-20 12:38:20 +00:00
|
|
|
|
2022-10-08 18:50:06 +00:00
|
|
|
imp.linked(&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-12-28 15:50:06 +00:00
|
|
|
let imp = instance.imp();
|
2018-11-20 12:38:20 +00:00
|
|
|
|
2022-10-08 18:50:06 +00:00
|
|
|
imp.unlinked(&from_glib_borrow(peer))
|
2018-11-20 12:38:20 +00:00
|
|
|
}
|
2019-05-11 13:43:52 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-12-22 07:59:23 +00:00
|
|
|
use std::sync::atomic;
|
2019-05-11 13:43:52 +00:00
|
|
|
|
2023-01-03 18:58:25 +00:00
|
|
|
use super::*;
|
|
|
|
use crate::{prelude::*, 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 {}
|
|
|
|
|
2021-08-24 06:12:27 +00:00
|
|
|
impl GstObjectImpl for TestPad {}
|
|
|
|
|
2020-11-14 14:59:43 +00:00
|
|
|
impl PadImpl for TestPad {
|
2022-10-08 18:50:06 +00:00
|
|
|
fn linked(&self, peer: &Pad) {
|
2020-11-14 14:59:43 +00:00
|
|
|
self.linked.store(true, atomic::Ordering::SeqCst);
|
2022-10-08 18:50:06 +00:00
|
|
|
self.parent_linked(peer)
|
2020-11-14 14:59:43 +00:00
|
|
|
}
|
2019-05-11 13:43:52 +00:00
|
|
|
|
2022-10-08 18:50:06 +00:00
|
|
|
fn unlinked(&self, peer: &Pad) {
|
2020-11-14 14:59:43 +00:00
|
|
|
self.unlinked.store(true, atomic::Ordering::SeqCst);
|
2022-10-08 18:50:06 +00:00
|
|
|
self.parent_unlinked(peer)
|
2020-11-14 14:59:43 +00:00
|
|
|
}
|
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
|
|
|
impl TestPad {
|
2020-11-14 15:34:41 +00:00
|
|
|
pub fn new(name: &str, direction: PadDirection) -> Self {
|
2023-01-22 07:48:51 +00:00
|
|
|
glib::Object::builder()
|
|
|
|
.property("name", name)
|
|
|
|
.property("direction", direction)
|
|
|
|
.build()
|
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();
|
|
|
|
|
2022-01-17 17:34:06 +00:00
|
|
|
let imp = pad.imp();
|
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
|
|
|
}
|
|
|
|
}
|