1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use gst_sys;
use glib;
use glib::translate::*;
use glib::subclass::prelude::*;
use Pad;
use PadClass;
pub trait PadImpl: PadImplExt + ObjectImpl + Send + Sync + 'static {
fn linked(&self, pad: &Pad, peer: &Pad) {
self.parent_linked(pad, peer)
}
fn unlinked(&self, pad: &Pad, peer: &Pad) {
self.parent_unlinked(pad, peer)
}
}
pub trait PadImplExt {
fn parent_linked(&self, pad: &Pad, peer: &Pad);
fn parent_unlinked(&self, pad: &Pad, peer: &Pad);
}
impl<T: PadImpl + ObjectImpl> PadImplExt for T {
fn parent_linked(&self, pad: &Pad, peer: &Pad) {
unsafe {
let data = self.get_type_data();
let parent_class = data.as_ref().get_parent_class() as *mut gst_sys::GstPadClass;
(*parent_class)
.linked
.map(|f| f(pad.to_glib_none().0, peer.to_glib_none().0))
.unwrap_or(())
}
}
fn parent_unlinked(&self, pad: &Pad, peer: &Pad) {
unsafe {
let data = self.get_type_data();
let parent_class = data.as_ref().get_parent_class() as *mut gst_sys::GstPadClass;
(*parent_class)
.unlinked
.map(|f| f(pad.to_glib_none().0, peer.to_glib_none().0))
.unwrap_or(())
}
}
}
unsafe impl<T: ObjectSubclass + PadImpl> IsSubclassable<T> for PadClass {
fn override_vfuncs(&mut self) {
<glib::ObjectClass as IsSubclassable<T>>::override_vfuncs(self);
unsafe {
let klass = &mut *(self as *mut Self as *mut gst_sys::GstPadClass);
klass.linked = Some(pad_linked::<T>);
klass.unlinked = Some(pad_unlinked::<T>);
}
}
}
unsafe extern "C" fn pad_linked<T: ObjectSubclass>(
ptr: *mut gst_sys::GstPad,
peer: *mut gst_sys::GstPad,
) where
T: PadImpl,
{
let instance = &*(ptr as *mut T::Instance);
let imp = instance.get_impl();
let wrap: Borrowed<Pad> = from_glib_borrow(ptr);
imp.linked(&wrap, &from_glib_borrow(peer))
}
unsafe extern "C" fn pad_unlinked<T: ObjectSubclass>(
ptr: *mut gst_sys::GstPad,
peer: *mut gst_sys::GstPad,
) where
T: PadImpl,
{
let instance = &*(ptr as *mut T::Instance);
let imp = instance.get_impl();
let wrap: Borrowed<Pad> = from_glib_borrow(ptr);
imp.unlinked(&wrap, &from_glib_borrow(peer))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::prelude::*;
use glib;
use glib::subclass;
use std::sync::atomic;
struct TestPad {
linked: atomic::AtomicBool,
unlinked: atomic::AtomicBool,
}
impl ObjectSubclass for TestPad {
const NAME: &'static str = "TestPad";
type ParentType = ::Pad;
type Instance = subclass::simple::InstanceStruct<Self>;
type Class = subclass::simple::ClassStruct<Self>;
glib_object_subclass!();
fn new() -> Self {
Self {
linked: atomic::AtomicBool::new(false),
unlinked: atomic::AtomicBool::new(false),
}
}
}
impl ObjectImpl for TestPad {
glib_object_impl!();
}
impl PadImpl for TestPad {
fn linked(&self, pad: &Pad, peer: &Pad) {
self.linked.store(true, atomic::Ordering::SeqCst);
self.parent_linked(pad, peer)
}
fn unlinked(&self, pad: &Pad, peer: &Pad) {
self.unlinked.store(true, atomic::Ordering::SeqCst);
self.parent_unlinked(pad, peer)
}
}
#[test]
fn test_pad_subclass() {
::init().unwrap();
let pad = glib::Object::new(
TestPad::get_type(),
&[("name", &"test"), ("direction", &::PadDirection::Src)],
)
.unwrap()
.downcast::<::Pad>()
.unwrap();
assert_eq!(pad.get_name(), "test");
let otherpad = ::Pad::new(Some("other-test"), ::PadDirection::Sink);
pad.link(&otherpad).unwrap();
pad.unlink(&otherpad).unwrap();
let imp = TestPad::from_instance(&pad);
assert!(imp.linked.load(atomic::Ordering::SeqCst));
assert!(imp.unlinked.load(atomic::Ordering::SeqCst));
}
}