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