gstreamer/subclass/
clock.rs
1use glib::{prelude::*, subclass::prelude::*, translate::*};
4
5use super::prelude::*;
6use crate::{ffi, Clock, ClockError, ClockId, ClockReturn, ClockSuccess, ClockTime, ClockTimeDiff};
7
8pub trait ClockImpl: ClockImplExt + GstObjectImpl + Send + Sync {
9 fn change_resolution(&self, old_resolution: ClockTime, new_resolution: ClockTime) -> ClockTime {
20 self.parent_change_resolution(old_resolution, new_resolution)
21 }
22
23 fn resolution(&self) -> ClockTime {
30 self.parent_resolution()
31 }
32
33 fn internal_time(&self) -> ClockTime {
41 self.parent_internal_time()
42 }
43
44 fn wait(&self, id: &ClockId) -> (Result<ClockSuccess, ClockError>, ClockTimeDiff) {
59 self.parent_wait(id)
60 }
61
62 fn wait_async(&self, id: &ClockId) -> Result<ClockSuccess, ClockError> {
70 self.parent_wait_async(id)
71 }
72
73 fn unschedule(&self, id: &ClockId) {
77 self.parent_unschedule(id)
78 }
79}
80
81mod sealed {
82 pub trait Sealed {}
83 impl<T: super::ClockImplExt> Sealed for T {}
84}
85
86pub trait ClockImplExt: sealed::Sealed + ObjectSubclass {
87 fn parent_change_resolution(
88 &self,
89 old_resolution: ClockTime,
90 new_resolution: ClockTime,
91 ) -> ClockTime;
92
93 fn parent_resolution(&self) -> ClockTime {
94 unsafe {
95 let data = Self::type_data();
96 let parent_class = data.as_ref().parent_class() as *mut ffi::GstClockClass;
97
98 try_from_glib(
99 (*parent_class)
100 .get_resolution
101 .map(|f| f(self.obj().unsafe_cast_ref::<Clock>().to_glib_none().0))
102 .unwrap_or(1),
103 )
104 .expect("undefined resolution")
105 }
106 }
107
108 fn parent_internal_time(&self) -> ClockTime {
109 unsafe {
110 let data = Self::type_data();
111 let parent_class = data.as_ref().parent_class() as *mut ffi::GstClockClass;
112
113 try_from_glib(
114 (*parent_class)
115 .get_internal_time
116 .map(|f| f(self.obj().unsafe_cast_ref::<Clock>().to_glib_none().0))
117 .unwrap_or(0),
118 )
119 .expect("undefined internal_time")
120 }
121 }
122
123 fn parent_wait(&self, id: &ClockId) -> (Result<ClockSuccess, ClockError>, ClockTimeDiff) {
124 unsafe {
125 let data = Self::type_data();
126 let parent_class = data.as_ref().parent_class() as *mut ffi::GstClockClass;
127 let mut jitter = 0;
128
129 (
130 try_from_glib(
131 (*parent_class)
132 .wait
133 .map(|f| {
134 f(
135 self.obj().unsafe_cast_ref::<Clock>().to_glib_none().0,
136 id.as_ptr() as *mut ffi::GstClockEntry,
137 &mut jitter,
138 )
139 })
140 .unwrap_or(ffi::GST_CLOCK_UNSUPPORTED),
141 ),
142 jitter,
143 )
144 }
145 }
146
147 fn parent_wait_async(&self, id: &ClockId) -> Result<ClockSuccess, ClockError> {
148 unsafe {
149 let data = Self::type_data();
150 let parent_class = data.as_ref().parent_class() as *mut ffi::GstClockClass;
151 try_from_glib(
152 (*parent_class)
153 .wait_async
154 .map(|f| {
155 f(
156 self.obj().unsafe_cast_ref::<Clock>().to_glib_none().0,
157 id.as_ptr() as *mut ffi::GstClockEntry,
158 )
159 })
160 .unwrap_or(ffi::GST_CLOCK_UNSUPPORTED),
161 )
162 }
163 }
164
165 fn parent_unschedule(&self, id: &ClockId) {
166 unsafe {
167 let data = Self::type_data();
168 let parent_class = data.as_ref().parent_class() as *mut ffi::GstClockClass;
169 if let Some(func) = (*parent_class).unschedule {
170 func(
171 self.obj().unsafe_cast_ref::<Clock>().to_glib_none().0,
172 id.as_ptr() as *mut ffi::GstClockEntry,
173 );
174 }
175 }
176 }
177
178 fn wake_id(&self, id: &ClockId) {
179 let clock = self.obj();
180 let clock = unsafe { clock.unsafe_cast_ref::<Clock>() };
181
182 cfg_if::cfg_if! {
183 if #[cfg(feature = "v1_16")] {
184 assert!(id.uses_clock(clock));
185 } else {
186 unsafe {
187 let ptr = id.as_ptr() as *mut ffi::GstClockEntry;
188 assert_eq!((*ptr).clock, clock.to_glib_none().0);
189 }
190 }
191 }
192
193 unsafe {
194 let ptr = id.as_ptr() as *mut ffi::GstClockEntry;
195 if let Some(func) = (*ptr).func {
196 func(
197 clock.to_glib_none().0,
198 (*ptr).time,
199 ptr as ffi::GstClockID,
200 (*ptr).user_data,
201 );
202 }
203 if (*ptr).type_ == ffi::GST_CLOCK_ENTRY_PERIODIC {
204 (*ptr).time += (*ptr).interval;
205 }
206 }
207 }
208}
209
210impl<T: ClockImpl> ClockImplExt for T {
211 fn parent_change_resolution(
212 &self,
213 old_resolution: ClockTime,
214 new_resolution: ClockTime,
215 ) -> ClockTime {
216 unsafe {
217 let data = Self::type_data();
218 let parent_class = data.as_ref().parent_class() as *mut ffi::GstClockClass;
219
220 if let Some(func) = (*parent_class).change_resolution {
221 try_from_glib(func(
222 self.obj().unsafe_cast_ref::<Clock>().to_glib_none().0,
223 old_resolution.into_glib(),
224 new_resolution.into_glib(),
225 ))
226 .expect("undefined resolution")
227 } else {
228 self.resolution()
229 }
230 }
231 }
232}
233
234unsafe impl<T: ClockImpl> IsSubclassable<T> for Clock {
235 fn class_init(klass: &mut glib::Class<Self>) {
236 Self::parent_class_init::<T>(klass);
237 let klass = klass.as_mut();
238 klass.change_resolution = Some(clock_change_resolution::<T>);
239 klass.get_resolution = Some(clock_get_resolution::<T>);
240 klass.get_internal_time = Some(clock_get_internal_time::<T>);
241 klass.wait = Some(clock_wait::<T>);
242 klass.wait_async = Some(clock_wait_async::<T>);
243 klass.unschedule = Some(clock_unschedule::<T>);
244 }
245}
246
247unsafe extern "C" fn clock_change_resolution<T: ClockImpl>(
248 ptr: *mut ffi::GstClock,
249 old_resolution: ffi::GstClockTime,
250 new_resolution: ffi::GstClockTime,
251) -> ffi::GstClockTime {
252 let instance = &*(ptr as *mut T::Instance);
253 let imp = instance.imp();
254
255 let old_resolution = match from_glib(old_resolution) {
256 Some(old_resolution) => old_resolution,
257 None => return ffi::GST_CLOCK_TIME_NONE,
258 };
259 let new_resolution = match from_glib(new_resolution) {
260 Some(new_resolution) => new_resolution,
261 None => return ffi::GST_CLOCK_TIME_NONE,
262 };
263
264 imp.change_resolution(old_resolution, new_resolution)
265 .into_glib()
266}
267
268unsafe extern "C" fn clock_get_resolution<T: ClockImpl>(
269 ptr: *mut ffi::GstClock,
270) -> ffi::GstClockTime {
271 let instance = &*(ptr as *mut T::Instance);
272 let imp = instance.imp();
273
274 imp.resolution().into_glib()
275}
276
277unsafe extern "C" fn clock_get_internal_time<T: ClockImpl>(
278 ptr: *mut ffi::GstClock,
279) -> ffi::GstClockTime {
280 let instance = &*(ptr as *mut T::Instance);
281 let imp = instance.imp();
282
283 imp.internal_time().into_glib()
284}
285
286unsafe extern "C" fn clock_wait<T: ClockImpl>(
287 ptr: *mut ffi::GstClock,
288 id: *mut ffi::GstClockEntry,
289 jitter: *mut ffi::GstClockTimeDiff,
290) -> ffi::GstClockReturn {
291 let instance = &*(ptr as *mut T::Instance);
292 let imp = instance.imp();
293
294 let (res, j) = imp.wait(&from_glib_borrow(id as ffi::GstClockID));
295 if !jitter.is_null() {
296 *jitter = j;
297 }
298
299 ClockReturn::from(res).into_glib()
300}
301
302unsafe extern "C" fn clock_wait_async<T: ClockImpl>(
303 ptr: *mut ffi::GstClock,
304 id: *mut ffi::GstClockEntry,
305) -> ffi::GstClockReturn {
306 let instance = &*(ptr as *mut T::Instance);
307 let imp = instance.imp();
308
309 ClockReturn::from(imp.wait_async(&from_glib_borrow(id as ffi::GstClockID))).into_glib()
310}
311
312unsafe extern "C" fn clock_unschedule<T: ClockImpl>(
313 ptr: *mut ffi::GstClock,
314 id: *mut ffi::GstClockEntry,
315) {
316 let instance = &*(ptr as *mut T::Instance);
317 let imp = instance.imp();
318
319 imp.unschedule(&from_glib_borrow(id as ffi::GstClockID));
320}