1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gstreamer.Clock;
26 
27 private import gobject.ObjectG;
28 private import gstreamer.ObjectGst;
29 private import gstreamerc.gstreamer;
30 public  import gstreamerc.gstreamertypes;
31 
32 
33 /**
34  * GStreamer uses a global clock to synchronize the plugins in a pipeline.
35  * Different clock implementations are possible by implementing this abstract
36  * base class or, more conveniently, by subclassing #GstSystemClock.
37  * 
38  * The #GstClock returns a monotonically increasing time with the method
39  * gst_clock_get_time(). Its accuracy and base time depend on the specific
40  * clock implementation but time is always expressed in nanoseconds. Since the
41  * baseline of the clock is undefined, the clock time returned is not
42  * meaningful in itself, what matters are the deltas between two clock times.
43  * The time returned by a clock is called the absolute time.
44  * 
45  * The pipeline uses the clock to calculate the running time. Usually all
46  * renderers synchronize to the global clock using the buffer timestamps, the
47  * newsegment events and the element's base time, see #GstPipeline.
48  * 
49  * A clock implementation can support periodic and single shot clock
50  * notifications both synchronous and asynchronous.
51  * 
52  * One first needs to create a #GstClockID for the periodic or single shot
53  * notification using gst_clock_new_single_shot_id() or
54  * gst_clock_new_periodic_id().
55  * 
56  * To perform a blocking wait for the specific time of the #GstClockID use the
57  * gst_clock_id_wait(). To receive a callback when the specific time is reached
58  * in the clock use gst_clock_id_wait_async(). Both these calls can be
59  * interrupted with the gst_clock_id_unschedule() call. If the blocking wait is
60  * unscheduled a return value of #GST_CLOCK_UNSCHEDULED is returned.
61  * 
62  * Periodic callbacks scheduled async will be repeatedly called automatically
63  * until it is unscheduled. To schedule a sync periodic callback,
64  * gst_clock_id_wait() should be called repeatedly.
65  * 
66  * The async callbacks can happen from any thread, either provided by the core
67  * or from a streaming thread. The application should be prepared for this.
68  * 
69  * A #GstClockID that has been unscheduled cannot be used again for any wait
70  * operation, a new #GstClockID should be created and the old unscheduled one
71  * should be destroyed with gst_clock_id_unref().
72  * 
73  * It is possible to perform a blocking wait on the same #GstClockID from
74  * multiple threads. However, registering the same #GstClockID for multiple
75  * async notifications is not possible, the callback will only be called for
76  * the thread registering the entry last.
77  * 
78  * None of the wait operations unref the #GstClockID, the owner is responsible
79  * for unreffing the ids itself. This holds for both periodic and single shot
80  * notifications. The reason being that the owner of the #GstClockID has to
81  * keep a handle to the #GstClockID to unblock the wait on FLUSHING events or
82  * state changes and if the entry would be unreffed automatically, the handle
83  * might become invalid without any notification.
84  * 
85  * These clock operations do not operate on the running time, so the callbacks
86  * will also occur when not in PLAYING state as if the clock just keeps on
87  * running. Some clocks however do not progress when the element that provided
88  * the clock is not PLAYING.
89  * 
90  * When a clock has the #GST_CLOCK_FLAG_CAN_SET_MASTER flag set, it can be
91  * slaved to another #GstClock with the gst_clock_set_master(). The clock will
92  * then automatically be synchronized to this master clock by repeatedly
93  * sampling the master clock and the slave clock and recalibrating the slave
94  * clock with gst_clock_set_calibration(). This feature is mostly useful for
95  * plugins that have an internal clock but must operate with another clock
96  * selected by the #GstPipeline.  They can track the offset and rate difference
97  * of their internal clock relative to the master clock by using the
98  * gst_clock_get_calibration() function.
99  * 
100  * The master/slave synchronisation can be tuned with the #GstClock:timeout,
101  * #GstClock:window-size and #GstClock:window-threshold properties.
102  * The #GstClock:timeout property defines the interval to sample the master
103  * clock and run the calibration functions. #GstClock:window-size defines the
104  * number of samples to use when calibrating and #GstClock:window-threshold
105  * defines the minimum number of samples before the calibration is performed.
106  */
107 public class Clock : ObjectGst
108 {
109 	/** the main Gtk struct */
110 	protected GstClock* gstClock;
111 
112 	/** Get the main Gtk struct */
113 	public GstClock* getClockStruct()
114 	{
115 		return gstClock;
116 	}
117 
118 	/** the main Gtk struct as a void* */
119 	protected override void* getStruct()
120 	{
121 		return cast(void*)gstClock;
122 	}
123 
124 	protected override void setStruct(GObject* obj)
125 	{
126 		gstClock = cast(GstClock*)obj;
127 		super.setStruct(obj);
128 	}
129 
130 	/**
131 	 * Sets our main struct and passes it to the parent class.
132 	 */
133 	public this (GstClock* gstClock, bool ownedRef = false)
134 	{
135 		this.gstClock = gstClock;
136 		super(cast(GstObject*)gstClock, ownedRef);
137 	}
138 
139 	/**
140 	 */
141 
142 	public static GType getType()
143 	{
144 		return gst_clock_get_type();
145 	}
146 
147 	/**
148 	 * Compares the two #GstClockID instances. This function can be used
149 	 * as a GCompareFunc when sorting ids.
150 	 *
151 	 * Params:
152 	 *     id1 = A #GstClockID
153 	 *     id2 = A #GstClockID to compare with
154 	 *
155 	 * Return: negative value if a < b; zero if a = b; positive value if a > b
156 	 *
157 	 *     MT safe.
158 	 */
159 	public static int idCompareFunc(void* id1, void* id2)
160 	{
161 		return gst_clock_id_compare_func(id1, id2);
162 	}
163 
164 	/**
165 	 * Get the time of the clock ID
166 	 *
167 	 * Params:
168 	 *     id = The #GstClockID to query
169 	 *
170 	 * Return: the time of the given clock id.
171 	 *
172 	 *     MT safe.
173 	 */
174 	public static GstClockTime idGetTime(GstClockID id)
175 	{
176 		return gst_clock_id_get_time(id);
177 	}
178 
179 	/**
180 	 * Increase the refcount of given @id.
181 	 *
182 	 * Params:
183 	 *     id = The #GstClockID to ref
184 	 *
185 	 * Return: The same #GstClockID with increased refcount.
186 	 *
187 	 *     MT safe.
188 	 */
189 	public static GstClockID idRef(GstClockID id)
190 	{
191 		return gst_clock_id_ref(id);
192 	}
193 
194 	/**
195 	 * Unref given @id. When the refcount reaches 0 the
196 	 * #GstClockID will be freed.
197 	 *
198 	 * MT safe.
199 	 *
200 	 * Params:
201 	 *     id = The #GstClockID to unref
202 	 */
203 	public static void idUnref(GstClockID id)
204 	{
205 		gst_clock_id_unref(id);
206 	}
207 
208 	/**
209 	 * Cancel an outstanding request with @id. This can either
210 	 * be an outstanding async notification or a pending sync notification.
211 	 * After this call, @id cannot be used anymore to receive sync or
212 	 * async notifications, you need to create a new #GstClockID.
213 	 *
214 	 * MT safe.
215 	 *
216 	 * Params:
217 	 *     id = The id to unschedule
218 	 */
219 	public static void idUnschedule(GstClockID id)
220 	{
221 		gst_clock_id_unschedule(id);
222 	}
223 
224 	/**
225 	 * Perform a blocking wait on @id.
226 	 * @id should have been created with gst_clock_new_single_shot_id()
227 	 * or gst_clock_new_periodic_id() and should not have been unscheduled
228 	 * with a call to gst_clock_id_unschedule().
229 	 *
230 	 * If the @jitter argument is not %NULL and this function returns #GST_CLOCK_OK
231 	 * or #GST_CLOCK_EARLY, it will contain the difference
232 	 * against the clock and the time of @id when this method was
233 	 * called.
234 	 * Positive values indicate how late @id was relative to the clock
235 	 * (in which case this function will return #GST_CLOCK_EARLY).
236 	 * Negative values indicate how much time was spent waiting on the clock
237 	 * before this function returned.
238 	 *
239 	 * Params:
240 	 *     id = The #GstClockID to wait on
241 	 *     jitter = a pointer that will contain the jitter,
242 	 *         can be %NULL.
243 	 *
244 	 * Return: the result of the blocking wait. #GST_CLOCK_EARLY will be returned
245 	 *     if the current clock time is past the time of @id, #GST_CLOCK_OK if
246 	 *     @id was scheduled in time. #GST_CLOCK_UNSCHEDULED if @id was
247 	 *     unscheduled with gst_clock_id_unschedule().
248 	 *
249 	 *     MT safe.
250 	 */
251 	public static GstClockReturn idWait(GstClockID id, out GstClockTimeDiff jitter)
252 	{
253 		return gst_clock_id_wait(id, &jitter);
254 	}
255 
256 	/**
257 	 * Register a callback on the given #GstClockID @id with the given
258 	 * function and user_data. When passing a #GstClockID with an invalid
259 	 * time to this function, the callback will be called immediately
260 	 * with  a time set to GST_CLOCK_TIME_NONE. The callback will
261 	 * be called when the time of @id has been reached.
262 	 *
263 	 * The callback @func can be invoked from any thread, either provided by the
264 	 * core or from a streaming thread. The application should be prepared for this.
265 	 *
266 	 * Params:
267 	 *     id = a #GstClockID to wait on
268 	 *     func = The callback function
269 	 *     userData = User data passed in the callback
270 	 *     destroyData = #GDestroyNotify for user_data
271 	 *
272 	 * Return: the result of the non blocking wait.
273 	 *
274 	 *     MT safe.
275 	 */
276 	public static GstClockReturn idWaitAsync(GstClockID id, GstClockCallback func, void* userData, GDestroyNotify destroyData)
277 	{
278 		return gst_clock_id_wait_async(id, func, userData, destroyData);
279 	}
280 
281 	/**
282 	 * The time @master of the master clock and the time @slave of the slave
283 	 * clock are added to the list of observations. If enough observations
284 	 * are available, a linear regression algorithm is run on the
285 	 * observations and @clock is recalibrated.
286 	 *
287 	 * If this functions returns %TRUE, @r_squared will contain the
288 	 * correlation coefficient of the interpolation. A value of 1.0
289 	 * means a perfect regression was performed. This value can
290 	 * be used to control the sampling frequency of the master and slave
291 	 * clocks.
292 	 *
293 	 * Params:
294 	 *     slave = a time on the slave
295 	 *     master = a time on the master
296 	 *     rSquared = a pointer to hold the result
297 	 *
298 	 * Return: %TRUE if enough observations were added to run the
299 	 *     regression algorithm.
300 	 *
301 	 *     MT safe.
302 	 */
303 	public bool addObservation(GstClockTime slave, GstClockTime master, out double rSquared)
304 	{
305 		return gst_clock_add_observation(gstClock, slave, master, &rSquared) != 0;
306 	}
307 
308 	/**
309 	 * Converts the given @internal clock time to the external time, adjusting for the
310 	 * rate and reference time set with gst_clock_set_calibration() and making sure
311 	 * that the returned time is increasing. This function should be called with the
312 	 * clock's OBJECT_LOCK held and is mainly used by clock subclasses.
313 	 *
314 	 * This function is the reverse of gst_clock_unadjust_unlocked().
315 	 *
316 	 * Params:
317 	 *     internal = a clock time
318 	 *
319 	 * Return: the converted time of the clock.
320 	 */
321 	public GstClockTime adjustUnlocked(GstClockTime internal)
322 	{
323 		return gst_clock_adjust_unlocked(gstClock, internal);
324 	}
325 
326 	/**
327 	 * Gets the internal rate and reference time of @clock. See
328 	 * gst_clock_set_calibration() for more information.
329 	 *
330 	 * @internal, @external, @rate_num, and @rate_denom can be left %NULL if the
331 	 * caller is not interested in the values.
332 	 *
333 	 * MT safe.
334 	 *
335 	 * Params:
336 	 *     internal = a location to store the internal time
337 	 *     external = a location to store the external time
338 	 *     rateNum = a location to store the rate numerator
339 	 *     rateDenom = a location to store the rate denominator
340 	 */
341 	public void getCalibration(out GstClockTime internal, out GstClockTime external, out GstClockTime rateNum, out GstClockTime rateDenom)
342 	{
343 		gst_clock_get_calibration(gstClock, &internal, &external, &rateNum, &rateDenom);
344 	}
345 
346 	/**
347 	 * Gets the current internal time of the given clock. The time is returned
348 	 * unadjusted for the offset and the rate.
349 	 *
350 	 * Return: the internal time of the clock. Or GST_CLOCK_TIME_NONE when
351 	 *     given invalid input.
352 	 *
353 	 *     MT safe.
354 	 */
355 	public GstClockTime getInternalTime()
356 	{
357 		return gst_clock_get_internal_time(gstClock);
358 	}
359 
360 	/**
361 	 * Get the master clock that @clock is slaved to or %NULL when the clock is
362 	 * not slaved to any master clock.
363 	 *
364 	 * Return: a master #GstClock or %NULL
365 	 *     when this clock is not slaved to a master clock. Unref after
366 	 *     usage.
367 	 *
368 	 *     MT safe.
369 	 */
370 	public Clock getMaster()
371 	{
372 		auto p = gst_clock_get_master(gstClock);
373 		
374 		if(p is null)
375 		{
376 			return null;
377 		}
378 		
379 		return ObjectG.getDObject!(Clock)(cast(GstClock*) p, true);
380 	}
381 
382 	/**
383 	 * Get the accuracy of the clock. The accuracy of the clock is the granularity
384 	 * of the values returned by gst_clock_get_time().
385 	 *
386 	 * Return: the resolution of the clock in units of #GstClockTime.
387 	 *
388 	 *     MT safe.
389 	 */
390 	public GstClockTime getResolution()
391 	{
392 		return gst_clock_get_resolution(gstClock);
393 	}
394 
395 	/**
396 	 * Gets the current time of the given clock. The time is always
397 	 * monotonically increasing and adjusted according to the current
398 	 * offset and rate.
399 	 *
400 	 * Return: the time of the clock. Or GST_CLOCK_TIME_NONE when
401 	 *     given invalid input.
402 	 *
403 	 *     MT safe.
404 	 */
405 	public GstClockTime getTime()
406 	{
407 		return gst_clock_get_time(gstClock);
408 	}
409 
410 	/**
411 	 * Get the amount of time that master and slave clocks are sampled.
412 	 *
413 	 * Return: the interval between samples.
414 	 */
415 	public GstClockTime getTimeout()
416 	{
417 		return gst_clock_get_timeout(gstClock);
418 	}
419 
420 	/**
421 	 * Get an ID from @clock to trigger a periodic notification.
422 	 * The periodic notifications will start at time @start_time and
423 	 * will then be fired with the given @interval. @id should be unreffed
424 	 * after usage.
425 	 *
426 	 * Free-function: gst_clock_id_unref
427 	 *
428 	 * Params:
429 	 *     startTime = the requested start time
430 	 *     interval = the requested interval
431 	 *
432 	 * Return: a #GstClockID that can be used to request the
433 	 *     time notification.
434 	 *
435 	 *     MT safe.
436 	 */
437 	public GstClockID newPeriodicId(GstClockTime startTime, GstClockTime interval)
438 	{
439 		return gst_clock_new_periodic_id(gstClock, startTime, interval);
440 	}
441 
442 	/**
443 	 * Get a #GstClockID from @clock to trigger a single shot
444 	 * notification at the requested time. The single shot id should be
445 	 * unreffed after usage.
446 	 *
447 	 * Free-function: gst_clock_id_unref
448 	 *
449 	 * Params:
450 	 *     time = the requested time
451 	 *
452 	 * Return: a #GstClockID that can be used to request the
453 	 *     time notification.
454 	 *
455 	 *     MT safe.
456 	 */
457 	public GstClockID newSingleShotId(GstClockTime time)
458 	{
459 		return gst_clock_new_single_shot_id(gstClock, time);
460 	}
461 
462 	/**
463 	 * Reinitializes the provided periodic @id to the provided start time and
464 	 * interval. Does not modify the reference count.
465 	 *
466 	 * Params:
467 	 *     id = a #GstClockID
468 	 *     startTime = the requested start time
469 	 *     interval = the requested interval
470 	 *
471 	 * Return: %TRUE if the GstClockID could be reinitialized to the provided
472 	 *     @time, else %FALSE.
473 	 */
474 	public bool periodicIdReinit(GstClockID id, GstClockTime startTime, GstClockTime interval)
475 	{
476 		return gst_clock_periodic_id_reinit(gstClock, id, startTime, interval) != 0;
477 	}
478 
479 	/**
480 	 * Adjusts the rate and time of @clock. A rate of 1/1 is the normal speed of
481 	 * the clock. Values bigger than 1/1 make the clock go faster.
482 	 *
483 	 * @internal and @external are calibration parameters that arrange that
484 	 * gst_clock_get_time() should have been @external at internal time @internal.
485 	 * This internal time should not be in the future; that is, it should be less
486 	 * than the value of gst_clock_get_internal_time() when this function is called.
487 	 *
488 	 * Subsequent calls to gst_clock_get_time() will return clock times computed as
489 	 * follows:
490 	 *
491 	 * <programlisting>
492 	 * time = (internal_time - internal) * rate_num / rate_denom + external
493 	 * </programlisting>
494 	 *
495 	 * This formula is implemented in gst_clock_adjust_unlocked(). Of course, it
496 	 * tries to do the integer arithmetic as precisely as possible.
497 	 *
498 	 * Note that gst_clock_get_time() always returns increasing values so when you
499 	 * move the clock backwards, gst_clock_get_time() will report the previous value
500 	 * until the clock catches up.
501 	 *
502 	 * MT safe.
503 	 *
504 	 * Params:
505 	 *     internal = a reference internal time
506 	 *     external = a reference external time
507 	 *     rateNum = the numerator of the rate of the clock relative to its
508 	 *         internal time
509 	 *     rateDenom = the denominator of the rate of the clock
510 	 */
511 	public void setCalibration(GstClockTime internal, GstClockTime external, GstClockTime rateNum, GstClockTime rateDenom)
512 	{
513 		gst_clock_set_calibration(gstClock, internal, external, rateNum, rateDenom);
514 	}
515 
516 	/**
517 	 * Set @master as the master clock for @clock. @clock will be automatically
518 	 * calibrated so that gst_clock_get_time() reports the same time as the
519 	 * master clock.
520 	 *
521 	 * A clock provider that slaves its clock to a master can get the current
522 	 * calibration values with gst_clock_get_calibration().
523 	 *
524 	 * @master can be %NULL in which case @clock will not be slaved anymore. It will
525 	 * however keep reporting its time adjusted with the last configured rate
526 	 * and time offsets.
527 	 *
528 	 * Params:
529 	 *     master = a master #GstClock
530 	 *
531 	 * Return: %TRUE if the clock is capable of being slaved to a master clock.
532 	 *     Trying to set a master on a clock without the
533 	 *     #GST_CLOCK_FLAG_CAN_SET_MASTER flag will make this function return %FALSE.
534 	 *
535 	 *     MT safe.
536 	 */
537 	public bool setMaster(Clock master)
538 	{
539 		return gst_clock_set_master(gstClock, (master is null) ? null : master.getClockStruct()) != 0;
540 	}
541 
542 	/**
543 	 * Set the accuracy of the clock. Some clocks have the possibility to operate
544 	 * with different accuracy at the expense of more resource usage. There is
545 	 * normally no need to change the default resolution of a clock. The resolution
546 	 * of a clock can only be changed if the clock has the
547 	 * #GST_CLOCK_FLAG_CAN_SET_RESOLUTION flag set.
548 	 *
549 	 * Params:
550 	 *     resolution = The resolution to set
551 	 *
552 	 * Return: the new resolution of the clock.
553 	 */
554 	public GstClockTime setResolution(GstClockTime resolution)
555 	{
556 		return gst_clock_set_resolution(gstClock, resolution);
557 	}
558 
559 	/**
560 	 * Set the amount of time, in nanoseconds, to sample master and slave
561 	 * clocks
562 	 *
563 	 * Params:
564 	 *     timeout = a timeout
565 	 */
566 	public void setTimeout(GstClockTime timeout)
567 	{
568 		gst_clock_set_timeout(gstClock, timeout);
569 	}
570 
571 	/**
572 	 * Reinitializes the provided single shot @id to the provided time. Does not
573 	 * modify the reference count.
574 	 *
575 	 * Params:
576 	 *     id = a #GstClockID
577 	 *     time = The requested time.
578 	 *
579 	 * Return: %TRUE if the GstClockID could be reinitialized to the provided
580 	 *     @time, else %FALSE.
581 	 */
582 	public bool singleShotIdReinit(GstClockID id, GstClockTime time)
583 	{
584 		return gst_clock_single_shot_id_reinit(gstClock, id, time) != 0;
585 	}
586 
587 	/**
588 	 * Converts the given @external clock time to the internal time of @clock,
589 	 * using the rate and reference time set with gst_clock_set_calibration().
590 	 * This function should be called with the clock's OBJECT_LOCK held and
591 	 * is mainly used by clock subclasses.
592 	 *
593 	 * This function is the reverse of gst_clock_adjust_unlocked().
594 	 *
595 	 * Params:
596 	 *     external = an external clock time
597 	 *
598 	 * Return: the internal time of the clock corresponding to @external.
599 	 */
600 	public GstClockTime unadjustUnlocked(GstClockTime external)
601 	{
602 		return gst_clock_unadjust_unlocked(gstClock, external);
603 	}
604 }