gstreamer/subclass/
clock.rs1use glib::{prelude::*, subclass::prelude::*, translate::*};
4
5use super::prelude::*;
6use crate::{ffi, Clock, ClockError, ClockId, ClockReturn, ClockSuccess, ClockTime, ClockTimeDiff};
7
8pub trait ClockImpl: GstObjectImpl + ObjectSubclass<Type: IsA<Clock>> {
9 fn change_resolution(&self, old_resolution: ClockTime, new_resolution: ClockTime) -> ClockTime {
10 self.parent_change_resolution(old_resolution, new_resolution)
11 }
12
13 fn resolution(&self) -> ClockTime {
14 self.parent_resolution()
15 }
16
17 fn internal_time(&self) -> ClockTime {
18 self.parent_internal_time()
19 }
20
21 fn wait(&self, id: &ClockId) -> (Result<ClockSuccess, ClockError>, ClockTimeDiff) {
22 self.parent_wait(id)
23 }
24
25 fn wait_async(&self, id: &ClockId) -> Result<ClockSuccess, ClockError> {
26 self.parent_wait_async(id)
27 }
28
29 fn unschedule(&self, id: &ClockId) {
30 self.parent_unschedule(id)
31 }
32}
33
34pub trait ClockImplExt: ClockImpl {
35 fn parent_change_resolution(
36 &self,
37 old_resolution: ClockTime,
38 new_resolution: ClockTime,
39 ) -> ClockTime;
40
41 fn parent_resolution(&self) -> ClockTime {
42 unsafe {
43 let data = Self::type_data();
44 let parent_class = data.as_ref().parent_class() as *mut ffi::GstClockClass;
45
46 try_from_glib(
47 (*parent_class)
48 .get_resolution
49 .map(|f| f(self.obj().unsafe_cast_ref::<Clock>().to_glib_none().0))
50 .unwrap_or(1),
51 )
52 .expect("undefined resolution")
53 }
54 }
55
56 fn parent_internal_time(&self) -> ClockTime {
57 unsafe {
58 let data = Self::type_data();
59 let parent_class = data.as_ref().parent_class() as *mut ffi::GstClockClass;
60
61 try_from_glib(
62 (*parent_class)
63 .get_internal_time
64 .map(|f| f(self.obj().unsafe_cast_ref::<Clock>().to_glib_none().0))
65 .unwrap_or(0),
66 )
67 .expect("undefined internal_time")
68 }
69 }
70
71 fn parent_wait(&self, id: &ClockId) -> (Result<ClockSuccess, ClockError>, ClockTimeDiff) {
72 unsafe {
73 let data = Self::type_data();
74 let parent_class = data.as_ref().parent_class() as *mut ffi::GstClockClass;
75 let mut jitter = 0;
76
77 (
78 try_from_glib(
79 (*parent_class)
80 .wait
81 .map(|f| {
82 f(
83 self.obj().unsafe_cast_ref::<Clock>().to_glib_none().0,
84 id.as_ptr() as *mut ffi::GstClockEntry,
85 &mut jitter,
86 )
87 })
88 .unwrap_or(ffi::GST_CLOCK_UNSUPPORTED),
89 ),
90 jitter,
91 )
92 }
93 }
94
95 fn parent_wait_async(&self, id: &ClockId) -> Result<ClockSuccess, ClockError> {
96 unsafe {
97 let data = Self::type_data();
98 let parent_class = data.as_ref().parent_class() as *mut ffi::GstClockClass;
99 try_from_glib(
100 (*parent_class)
101 .wait_async
102 .map(|f| {
103 f(
104 self.obj().unsafe_cast_ref::<Clock>().to_glib_none().0,
105 id.as_ptr() as *mut ffi::GstClockEntry,
106 )
107 })
108 .unwrap_or(ffi::GST_CLOCK_UNSUPPORTED),
109 )
110 }
111 }
112
113 fn parent_unschedule(&self, id: &ClockId) {
114 unsafe {
115 let data = Self::type_data();
116 let parent_class = data.as_ref().parent_class() as *mut ffi::GstClockClass;
117 if let Some(func) = (*parent_class).unschedule {
118 func(
119 self.obj().unsafe_cast_ref::<Clock>().to_glib_none().0,
120 id.as_ptr() as *mut ffi::GstClockEntry,
121 );
122 }
123 }
124 }
125
126 fn wake_id(&self, id: &ClockId) {
127 let clock = self.obj();
128 let clock = unsafe { clock.unsafe_cast_ref::<Clock>() };
129
130 cfg_if::cfg_if! {
131 if #[cfg(feature = "v1_16")] {
132 assert!(id.uses_clock(clock));
133 } else {
134 unsafe {
135 let ptr = id.as_ptr() as *mut ffi::GstClockEntry;
136 assert_eq!((*ptr).clock, clock.to_glib_none().0);
137 }
138 }
139 }
140
141 unsafe {
142 let ptr = id.as_ptr() as *mut ffi::GstClockEntry;
143 if let Some(func) = (*ptr).func {
144 func(
145 clock.to_glib_none().0,
146 (*ptr).time,
147 ptr as ffi::GstClockID,
148 (*ptr).user_data,
149 );
150 }
151 if (*ptr).type_ == ffi::GST_CLOCK_ENTRY_PERIODIC {
152 (*ptr).time += (*ptr).interval;
153 }
154 }
155 }
156}
157
158impl<T: ClockImpl> ClockImplExt for T {
159 fn parent_change_resolution(
160 &self,
161 old_resolution: ClockTime,
162 new_resolution: ClockTime,
163 ) -> ClockTime {
164 unsafe {
165 let data = Self::type_data();
166 let parent_class = data.as_ref().parent_class() as *mut ffi::GstClockClass;
167
168 if let Some(func) = (*parent_class).change_resolution {
169 try_from_glib(func(
170 self.obj().unsafe_cast_ref::<Clock>().to_glib_none().0,
171 old_resolution.into_glib(),
172 new_resolution.into_glib(),
173 ))
174 .expect("undefined resolution")
175 } else {
176 self.resolution()
177 }
178 }
179 }
180}
181
182unsafe impl<T: ClockImpl> IsSubclassable<T> for Clock {
183 fn class_init(klass: &mut glib::Class<Self>) {
184 Self::parent_class_init::<T>(klass);
185 let klass = klass.as_mut();
186 klass.change_resolution = Some(clock_change_resolution::<T>);
187 klass.get_resolution = Some(clock_get_resolution::<T>);
188 klass.get_internal_time = Some(clock_get_internal_time::<T>);
189 klass.wait = Some(clock_wait::<T>);
190 klass.wait_async = Some(clock_wait_async::<T>);
191 klass.unschedule = Some(clock_unschedule::<T>);
192 }
193}
194
195unsafe extern "C" fn clock_change_resolution<T: ClockImpl>(
196 ptr: *mut ffi::GstClock,
197 old_resolution: ffi::GstClockTime,
198 new_resolution: ffi::GstClockTime,
199) -> ffi::GstClockTime {
200 let instance = &*(ptr as *mut T::Instance);
201 let imp = instance.imp();
202
203 let old_resolution = match from_glib(old_resolution) {
204 Some(old_resolution) => old_resolution,
205 None => return ffi::GST_CLOCK_TIME_NONE,
206 };
207 let new_resolution = match from_glib(new_resolution) {
208 Some(new_resolution) => new_resolution,
209 None => return ffi::GST_CLOCK_TIME_NONE,
210 };
211
212 imp.change_resolution(old_resolution, new_resolution)
213 .into_glib()
214}
215
216unsafe extern "C" fn clock_get_resolution<T: ClockImpl>(
217 ptr: *mut ffi::GstClock,
218) -> ffi::GstClockTime {
219 let instance = &*(ptr as *mut T::Instance);
220 let imp = instance.imp();
221
222 imp.resolution().into_glib()
223}
224
225unsafe extern "C" fn clock_get_internal_time<T: ClockImpl>(
226 ptr: *mut ffi::GstClock,
227) -> ffi::GstClockTime {
228 let instance = &*(ptr as *mut T::Instance);
229 let imp = instance.imp();
230
231 imp.internal_time().into_glib()
232}
233
234unsafe extern "C" fn clock_wait<T: ClockImpl>(
235 ptr: *mut ffi::GstClock,
236 id: *mut ffi::GstClockEntry,
237 jitter: *mut ffi::GstClockTimeDiff,
238) -> ffi::GstClockReturn {
239 let instance = &*(ptr as *mut T::Instance);
240 let imp = instance.imp();
241
242 let (res, j) = imp.wait(&from_glib_borrow(id as ffi::GstClockID));
243 if !jitter.is_null() {
244 *jitter = j;
245 }
246
247 ClockReturn::from(res).into_glib()
248}
249
250unsafe extern "C" fn clock_wait_async<T: ClockImpl>(
251 ptr: *mut ffi::GstClock,
252 id: *mut ffi::GstClockEntry,
253) -> ffi::GstClockReturn {
254 let instance = &*(ptr as *mut T::Instance);
255 let imp = instance.imp();
256
257 ClockReturn::from(imp.wait_async(&from_glib_borrow(id as ffi::GstClockID))).into_glib()
258}
259
260unsafe extern "C" fn clock_unschedule<T: ClockImpl>(
261 ptr: *mut ffi::GstClock,
262 id: *mut ffi::GstClockEntry,
263) {
264 let instance = &*(ptr as *mut T::Instance);
265 let imp = instance.imp();
266
267 imp.unschedule(&from_glib_borrow(id as ffi::GstClockID));
268}