2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2004 Wim Taymans <wim@fluendo.com>
6 * gstclock.c: Clock subsystem for maintaining time sync
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
26 * @short_description: Abstract class for global clocks
27 * @see_also: #GstSystemClock, #GstPipeline
29 * GStreamer uses a global clock to synchronize the plugins in a pipeline.
30 * Different clock implementations are possible by implementing this abstract
31 * base class or, more conveniently, by subclassing #GstSystemClock.
33 * The #GstClock returns a monotonically increasing time with the method
34 * gst_clock_get_time(). Its accuracy and base time depend on the specific
35 * clock implementation but time is always expressed in nanoseconds. Since the
36 * baseline of the clock is undefined, the clock time returned is not
37 * meaningful in itself, what matters are the deltas between two clock times.
38 * The time returned by a clock is called the absolute time.
40 * The pipeline uses the clock to calculate the running time. Usually all
41 * renderers synchronize to the global clock using the buffer timestamps, the
42 * newsegment events and the element's base time, see #GstPipeline.
44 * A clock implementation can support periodic and single shot clock
45 * notifications both synchronous and asynchronous.
47 * One first needs to create a #GstClockID for the periodic or single shot
48 * notification using gst_clock_new_single_shot_id() or
49 * gst_clock_new_periodic_id().
51 * To perform a blocking wait for the specific time of the #GstClockID use the
52 * gst_clock_id_wait(). To receive a callback when the specific time is reached
53 * in the clock use gst_clock_id_wait_async(). Both these calls can be
54 * interrupted with the gst_clock_id_unschedule() call. If the blocking wait is
55 * unscheduled a return value of #GST_CLOCK_UNSCHEDULED is returned.
57 * Periodic callbacks scheduled async will be repeatedly called automatically
58 * until it is unscheduled. To schedule a sync periodic callback,
59 * gst_clock_id_wait() should be called repeatedly.
61 * The async callbacks can happen from any thread, either provided by the core
62 * or from a streaming thread. The application should be prepared for this.
64 * A #GstClockID that has been unscheduled cannot be used again for any wait
65 * operation, a new #GstClockID should be created and the old unscheduled one
66 * should be destroyed with gst_clock_id_unref().
68 * It is possible to perform a blocking wait on the same #GstClockID from
69 * multiple threads. However, registering the same #GstClockID for multiple
70 * async notifications is not possible, the callback will only be called for
71 * the thread registering the entry last.
73 * None of the wait operations unref the #GstClockID, the owner is responsible
74 * for unreffing the ids itself. This holds for both periodic and single shot
75 * notifications. The reason being that the owner of the #GstClockID has to
76 * keep a handle to the #GstClockID to unblock the wait on FLUSHING events or
77 * state changes and if the entry would be unreffed automatically, the handle
78 * might become invalid without any notification.
80 * These clock operations do not operate on the running time, so the callbacks
81 * will also occur when not in PLAYING state as if the clock just keeps on
82 * running. Some clocks however do not progress when the element that provided
83 * the clock is not PLAYING.
85 * When a clock has the #GST_CLOCK_FLAG_CAN_SET_MASTER flag set, it can be
86 * slaved to another #GstClock with the gst_clock_set_master(). The clock will
87 * then automatically be synchronized to this master clock by repeatedly
88 * sampling the master clock and the slave clock and recalibrating the slave
89 * clock with gst_clock_set_calibration(). This feature is mostly useful for
90 * plugins that have an internal clock but must operate with another clock
91 * selected by the #GstPipeline. They can track the offset and rate difference
92 * of their internal clock relative to the master clock by using the
93 * gst_clock_get_calibration() function.
95 * The master/slave synchronisation can be tuned with the #GstClock:timeout,
96 * #GstClock:window-size and #GstClock:window-threshold properties.
97 * The #GstClock:timeout property defines the interval to sample the master
98 * clock and run the calibration functions. #GstClock:window-size defines the
99 * number of samples to use when calibrating and #GstClock:window-threshold
100 * defines the minimum number of samples before the calibration is performed.
103 #include "gst_private.h"
106 #include "gstclock.h"
108 #include "gstutils.h"
109 #include "glib-compat-private.h"
111 /* #define DEBUGGING_ENABLED */
113 #define DEFAULT_WINDOW_SIZE 32
114 #define DEFAULT_WINDOW_THRESHOLD 4
115 #define DEFAULT_TIMEOUT GST_SECOND / 10
121 PROP_WINDOW_THRESHOLD,
131 #define GST_CLOCK_SLAVE_LOCK(clock) g_mutex_lock (&GST_CLOCK_CAST (clock)->priv->slave_lock)
132 #define GST_CLOCK_SLAVE_UNLOCK(clock) g_mutex_unlock (&GST_CLOCK_CAST (clock)->priv->slave_lock)
134 struct _GstClockPrivate
136 GMutex slave_lock; /* order: SLAVE_LOCK, OBJECT_LOCK */
141 GstClockTime internal_calibration;
142 GstClockTime external_calibration;
143 GstClockTime rate_numerator;
144 GstClockTime rate_denominator;
145 GstClockTime last_time;
148 GstClockTime resolution;
150 /* for master/slave clocks */
153 /* with SLAVE_LOCK */
156 gint window_threshold;
158 GstClockTime timeout;
160 GstClockTime *times_temp;
170 #define read_seqbegin(clock) \
171 g_atomic_int_get (&clock->priv->post_count);
173 static inline gboolean
174 read_seqretry (GstClock * clock, gint seq)
176 /* no retry if the seqnum did not change */
177 if (G_LIKELY (seq == g_atomic_int_get (&clock->priv->pre_count)))
180 /* wait for the writer to finish and retry */
181 GST_OBJECT_LOCK (clock);
182 GST_OBJECT_UNLOCK (clock);
186 #define write_seqlock(clock) \
188 GST_OBJECT_LOCK (clock); \
189 g_atomic_int_inc (&clock->priv->pre_count); \
192 #define write_sequnlock(clock) \
194 g_atomic_int_inc (&clock->priv->post_count); \
195 GST_OBJECT_UNLOCK (clock); \
198 #ifndef GST_DISABLE_GST_DEBUG
200 gst_clock_return_get_name (GstClockReturn ret)
205 case GST_CLOCK_EARLY:
207 case GST_CLOCK_UNSCHEDULED:
208 return "unscheduled";
211 case GST_CLOCK_BADTIME:
213 case GST_CLOCK_ERROR:
215 case GST_CLOCK_UNSUPPORTED:
216 return "unsupported";
225 #endif /* GST_DISABLE_GST_DEBUG */
227 static void gst_clock_dispose (GObject * object);
228 static void gst_clock_finalize (GObject * object);
230 static void gst_clock_set_property (GObject * object, guint prop_id,
231 const GValue * value, GParamSpec * pspec);
232 static void gst_clock_get_property (GObject * object, guint prop_id,
233 GValue * value, GParamSpec * pspec);
235 static guint gst_clock_signals[SIGNAL_LAST] = { 0 };
238 gst_clock_entry_new (GstClock * clock, GstClockTime time,
239 GstClockTime interval, GstClockEntryType type)
241 GstClockEntry *entry;
243 entry = g_slice_new (GstClockEntry);
245 /* FIXME: add tracer hook for struct allocations such as clock entries */
247 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
248 "created entry %p, time %" GST_TIME_FORMAT, entry, GST_TIME_ARGS (time));
251 entry->clock = clock;
254 entry->interval = interval;
255 entry->status = GST_CLOCK_OK;
257 entry->user_data = NULL;
258 entry->destroy_data = NULL;
259 entry->unscheduled = FALSE;
260 entry->woken_up = FALSE;
262 return (GstClockID) entry;
265 /* WARNING : Does not modify the refcount
266 * WARNING : Do not use if a pending clock operation is happening on that entry */
268 gst_clock_entry_reinit (GstClock * clock, GstClockEntry * entry,
269 GstClockTime time, GstClockTime interval, GstClockEntryType type)
271 g_return_val_if_fail (entry->status != GST_CLOCK_BUSY, FALSE);
272 g_return_val_if_fail (entry->clock == clock, FALSE);
276 entry->interval = interval;
277 entry->status = GST_CLOCK_OK;
278 entry->unscheduled = FALSE;
279 entry->woken_up = FALSE;
285 * gst_clock_single_shot_id_reinit:
286 * @clock: a #GstClock
288 * @time: The requested time.
290 * Reinitializes the provided single shot @id to the provided time. Does not
291 * modify the reference count.
293 * Returns: %TRUE if the GstClockID could be reinitialized to the provided
294 * @time, else %FALSE.
297 gst_clock_single_shot_id_reinit (GstClock * clock, GstClockID id,
300 return gst_clock_entry_reinit (clock, (GstClockEntry *) id, time,
301 GST_CLOCK_TIME_NONE, GST_CLOCK_ENTRY_SINGLE);
305 * gst_clock_periodic_id_reinit:
306 * @clock: a #GstClock
308 * @start_time: the requested start time
309 * @interval: the requested interval
311 * Reinitializes the provided periodic @id to the provided start time and
312 * interval. Does not modify the reference count.
314 * Returns: %TRUE if the GstClockID could be reinitialized to the provided
315 * @time, else %FALSE.
318 gst_clock_periodic_id_reinit (GstClock * clock, GstClockID id,
319 GstClockTime start_time, GstClockTime interval)
321 return gst_clock_entry_reinit (clock, (GstClockEntry *) id, start_time,
322 interval, GST_CLOCK_ENTRY_PERIODIC);
327 * @id: The #GstClockID to ref
329 * Increase the refcount of given @id.
331 * Returns: (transfer full): The same #GstClockID with increased refcount.
336 gst_clock_id_ref (GstClockID id)
338 g_return_val_if_fail (id != NULL, NULL);
340 g_atomic_int_inc (&((GstClockEntry *) id)->refcount);
346 _gst_clock_id_free (GstClockID id)
348 GstClockEntry *entry;
349 g_return_if_fail (id != NULL);
351 GST_CAT_DEBUG (GST_CAT_CLOCK, "freed entry %p", id);
352 entry = (GstClockEntry *) id;
353 if (entry->destroy_data)
354 entry->destroy_data (entry->user_data);
356 /* FIXME: add tracer hook for struct allocations such as clock entries */
358 g_slice_free (GstClockEntry, id);
362 * gst_clock_id_unref:
363 * @id: (transfer full): The #GstClockID to unref
365 * Unref given @id. When the refcount reaches 0 the
366 * #GstClockID will be freed.
371 gst_clock_id_unref (GstClockID id)
375 g_return_if_fail (id != NULL);
377 zero = g_atomic_int_dec_and_test (&((GstClockEntry *) id)->refcount);
378 /* if we ended up with the refcount at zero, free the id */
380 _gst_clock_id_free (id);
385 * gst_clock_new_single_shot_id:
386 * @clock: The #GstClockID to get a single shot notification from
387 * @time: the requested time
389 * Get a #GstClockID from @clock to trigger a single shot
390 * notification at the requested time. The single shot id should be
391 * unreffed after usage.
393 * Free-function: gst_clock_id_unref
395 * Returns: (transfer full): a #GstClockID that can be used to request the
401 gst_clock_new_single_shot_id (GstClock * clock, GstClockTime time)
403 g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);
405 return gst_clock_entry_new (clock,
406 time, GST_CLOCK_TIME_NONE, GST_CLOCK_ENTRY_SINGLE);
410 * gst_clock_new_periodic_id:
411 * @clock: The #GstClockID to get a periodic notification id from
412 * @start_time: the requested start time
413 * @interval: the requested interval
415 * Get an ID from @clock to trigger a periodic notification.
416 * The periodic notifications will start at time @start_time and
417 * will then be fired with the given @interval. @id should be unreffed
420 * Free-function: gst_clock_id_unref
422 * Returns: (transfer full): a #GstClockID that can be used to request the
428 gst_clock_new_periodic_id (GstClock * clock, GstClockTime start_time,
429 GstClockTime interval)
431 g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);
432 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (start_time), NULL);
433 g_return_val_if_fail (interval != 0, NULL);
434 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), NULL);
436 return gst_clock_entry_new (clock,
437 start_time, interval, GST_CLOCK_ENTRY_PERIODIC);
441 * gst_clock_id_compare_func:
442 * @id1: A #GstClockID
443 * @id2: A #GstClockID to compare with
445 * Compares the two #GstClockID instances. This function can be used
446 * as a GCompareFunc when sorting ids.
448 * Returns: negative value if a < b; zero if a = b; positive value if a > b
453 gst_clock_id_compare_func (gconstpointer id1, gconstpointer id2)
455 GstClockEntry *entry1, *entry2;
457 entry1 = (GstClockEntry *) id1;
458 entry2 = (GstClockEntry *) id2;
460 if (GST_CLOCK_ENTRY_TIME (entry1) > GST_CLOCK_ENTRY_TIME (entry2)) {
463 if (GST_CLOCK_ENTRY_TIME (entry1) < GST_CLOCK_ENTRY_TIME (entry2)) {
470 * gst_clock_id_get_time:
471 * @id: The #GstClockID to query
473 * Get the time of the clock ID
475 * Returns: the time of the given clock id.
480 gst_clock_id_get_time (GstClockID id)
482 g_return_val_if_fail (id != NULL, GST_CLOCK_TIME_NONE);
484 return GST_CLOCK_ENTRY_TIME ((GstClockEntry *) id);
489 * @id: The #GstClockID to wait on
490 * @jitter: (out) (allow-none): a pointer that will contain the jitter,
493 * Perform a blocking wait on @id.
494 * @id should have been created with gst_clock_new_single_shot_id()
495 * or gst_clock_new_periodic_id() and should not have been unscheduled
496 * with a call to gst_clock_id_unschedule().
498 * If the @jitter argument is not %NULL and this function returns #GST_CLOCK_OK
499 * or #GST_CLOCK_EARLY, it will contain the difference
500 * against the clock and the time of @id when this method was
502 * Positive values indicate how late @id was relative to the clock
503 * (in which case this function will return #GST_CLOCK_EARLY).
504 * Negative values indicate how much time was spent waiting on the clock
505 * before this function returned.
507 * Returns: the result of the blocking wait. #GST_CLOCK_EARLY will be returned
508 * if the current clock time is past the time of @id, #GST_CLOCK_OK if
509 * @id was scheduled in time. #GST_CLOCK_UNSCHEDULED if @id was
510 * unscheduled with gst_clock_id_unschedule().
515 gst_clock_id_wait (GstClockID id, GstClockTimeDiff * jitter)
517 GstClockEntry *entry;
520 GstClockTime requested;
521 GstClockClass *cclass;
523 g_return_val_if_fail (id != NULL, GST_CLOCK_ERROR);
525 entry = (GstClockEntry *) id;
526 requested = GST_CLOCK_ENTRY_TIME (entry);
528 clock = GST_CLOCK_ENTRY_CLOCK (entry);
530 /* can't sync on invalid times */
531 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (requested)))
534 cclass = GST_CLOCK_GET_CLASS (clock);
536 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock, "waiting on clock entry %p", id);
538 /* if we have a wait_jitter function, use that */
539 if (G_UNLIKELY (cclass->wait == NULL))
542 res = cclass->wait (clock, entry, jitter);
544 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
545 "done waiting entry %p, res: %d (%s)", id, res,
546 gst_clock_return_get_name (res));
548 if (entry->type == GST_CLOCK_ENTRY_PERIODIC)
549 entry->time = requested + entry->interval;
556 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
557 "invalid time requested, returning _BADTIME");
558 return GST_CLOCK_BADTIME;
562 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock, "clock wait is not supported");
563 return GST_CLOCK_UNSUPPORTED;
568 * gst_clock_id_wait_async:
569 * @id: a #GstClockID to wait on
570 * @func: The callback function
571 * @user_data: User data passed in the callback
572 * @destroy_data: #GDestroyNotify for user_data
574 * Register a callback on the given #GstClockID @id with the given
575 * function and user_data. When passing a #GstClockID with an invalid
576 * time to this function, the callback will be called immediately
577 * with a time set to GST_CLOCK_TIME_NONE. The callback will
578 * be called when the time of @id has been reached.
580 * The callback @func can be invoked from any thread, either provided by the
581 * core or from a streaming thread. The application should be prepared for this.
583 * Returns: the result of the non blocking wait.
588 gst_clock_id_wait_async (GstClockID id,
589 GstClockCallback func, gpointer user_data, GDestroyNotify destroy_data)
591 GstClockEntry *entry;
594 GstClockClass *cclass;
595 GstClockTime requested;
597 g_return_val_if_fail (id != NULL, GST_CLOCK_ERROR);
598 g_return_val_if_fail (func != NULL, GST_CLOCK_ERROR);
600 entry = (GstClockEntry *) id;
601 requested = GST_CLOCK_ENTRY_TIME (entry);
602 clock = GST_CLOCK_ENTRY_CLOCK (entry);
604 /* can't sync on invalid times */
605 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (requested)))
608 cclass = GST_CLOCK_GET_CLASS (clock);
610 if (G_UNLIKELY (cclass->wait_async == NULL))
614 entry->user_data = user_data;
615 entry->destroy_data = destroy_data;
617 res = cclass->wait_async (clock, entry);
624 (func) (clock, GST_CLOCK_TIME_NONE, id, user_data);
625 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
626 "invalid time requested, returning _BADTIME");
627 return GST_CLOCK_BADTIME;
631 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock, "clock wait is not supported");
632 return GST_CLOCK_UNSUPPORTED;
637 * gst_clock_id_unschedule:
638 * @id: The id to unschedule
640 * Cancel an outstanding request with @id. This can either
641 * be an outstanding async notification or a pending sync notification.
642 * After this call, @id cannot be used anymore to receive sync or
643 * async notifications, you need to create a new #GstClockID.
648 gst_clock_id_unschedule (GstClockID id)
650 GstClockEntry *entry;
652 GstClockClass *cclass;
654 g_return_if_fail (id != NULL);
656 entry = (GstClockEntry *) id;
657 clock = entry->clock;
659 cclass = GST_CLOCK_GET_CLASS (clock);
661 if (G_LIKELY (cclass->unschedule))
662 cclass->unschedule (clock, entry);
667 * GstClock abstract base class implementation
669 #define gst_clock_parent_class parent_class
670 G_DEFINE_ABSTRACT_TYPE (GstClock, gst_clock, GST_TYPE_OBJECT);
673 gst_clock_class_init (GstClockClass * klass)
675 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
677 gobject_class->dispose = gst_clock_dispose;
678 gobject_class->finalize = gst_clock_finalize;
679 gobject_class->set_property = gst_clock_set_property;
680 gobject_class->get_property = gst_clock_get_property;
682 g_object_class_install_property (gobject_class, PROP_WINDOW_SIZE,
683 g_param_spec_int ("window-size", "Window size",
684 "The size of the window used to calculate rate and offset", 2, 1024,
685 DEFAULT_WINDOW_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
686 g_object_class_install_property (gobject_class, PROP_WINDOW_THRESHOLD,
687 g_param_spec_int ("window-threshold", "Window threshold",
688 "The threshold to start calculating rate and offset", 2, 1024,
689 DEFAULT_WINDOW_THRESHOLD,
690 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
691 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
692 g_param_spec_uint64 ("timeout", "Timeout",
693 "The amount of time, in nanoseconds, to sample master and slave clocks",
694 0, G_MAXUINT64, DEFAULT_TIMEOUT,
695 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
700 * @synced: if the clock is synced now
702 * Signaled on clocks with GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC set once
703 * the clock is synchronized, or when it completely lost synchronization.
704 * This signal will not be emitted on clocks without the flag.
706 * This signal will be emitted from an arbitrary thread, most likely not
707 * the application's main thread.
711 gst_clock_signals[SIGNAL_SYNCED] =
712 g_signal_new ("synced", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
714 g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
716 g_type_class_add_private (klass, sizeof (GstClockPrivate));
720 gst_clock_init (GstClock * clock)
722 GstClockPrivate *priv;
725 G_TYPE_INSTANCE_GET_PRIVATE (clock, GST_TYPE_CLOCK, GstClockPrivate);
729 priv->internal_calibration = 0;
730 priv->external_calibration = 0;
731 priv->rate_numerator = 1;
732 priv->rate_denominator = 1;
734 g_mutex_init (&priv->slave_lock);
735 g_cond_init (&priv->sync_cond);
736 priv->window_size = DEFAULT_WINDOW_SIZE;
737 priv->window_threshold = DEFAULT_WINDOW_THRESHOLD;
738 priv->filling = TRUE;
739 priv->time_index = 0;
740 priv->timeout = DEFAULT_TIMEOUT;
741 priv->times = g_new0 (GstClockTime, 4 * priv->window_size);
742 priv->times_temp = priv->times + 2 * priv->window_size;
744 /* clear floating flag */
745 gst_object_ref_sink (clock);
749 gst_clock_dispose (GObject * object)
751 GstClock *clock = GST_CLOCK (object);
754 GST_OBJECT_LOCK (clock);
755 master_p = &clock->priv->master;
756 gst_object_replace ((GstObject **) master_p, NULL);
757 GST_OBJECT_UNLOCK (clock);
759 G_OBJECT_CLASS (parent_class)->dispose (object);
763 gst_clock_finalize (GObject * object)
765 GstClock *clock = GST_CLOCK (object);
767 GST_CLOCK_SLAVE_LOCK (clock);
768 if (clock->priv->clockid) {
769 gst_clock_id_unschedule (clock->priv->clockid);
770 gst_clock_id_unref (clock->priv->clockid);
771 clock->priv->clockid = NULL;
773 g_free (clock->priv->times);
774 clock->priv->times = NULL;
775 clock->priv->times_temp = NULL;
776 GST_CLOCK_SLAVE_UNLOCK (clock);
778 g_mutex_clear (&clock->priv->slave_lock);
779 g_cond_clear (&clock->priv->sync_cond);
781 G_OBJECT_CLASS (parent_class)->finalize (object);
785 * gst_clock_set_resolution:
786 * @clock: a #GstClock
787 * @resolution: The resolution to set
789 * Set the accuracy of the clock. Some clocks have the possibility to operate
790 * with different accuracy at the expense of more resource usage. There is
791 * normally no need to change the default resolution of a clock. The resolution
792 * of a clock can only be changed if the clock has the
793 * #GST_CLOCK_FLAG_CAN_SET_RESOLUTION flag set.
795 * Returns: the new resolution of the clock.
798 gst_clock_set_resolution (GstClock * clock, GstClockTime resolution)
800 GstClockPrivate *priv;
801 GstClockClass *cclass;
803 g_return_val_if_fail (GST_IS_CLOCK (clock), 0);
804 g_return_val_if_fail (resolution != 0, 0);
806 cclass = GST_CLOCK_GET_CLASS (clock);
809 if (cclass->change_resolution)
811 cclass->change_resolution (clock, priv->resolution, resolution);
813 return priv->resolution;
817 * gst_clock_get_resolution:
818 * @clock: a #GstClock
820 * Get the accuracy of the clock. The accuracy of the clock is the granularity
821 * of the values returned by gst_clock_get_time().
823 * Returns: the resolution of the clock in units of #GstClockTime.
828 gst_clock_get_resolution (GstClock * clock)
830 GstClockClass *cclass;
832 g_return_val_if_fail (GST_IS_CLOCK (clock), 0);
834 cclass = GST_CLOCK_GET_CLASS (clock);
836 if (cclass->get_resolution)
837 return cclass->get_resolution (clock);
842 /* FIXME 2.0: Remove clock parameter below */
844 * gst_clock_adjust_with_calibration:
845 * @clock: (allow-none): a #GstClock to use
846 * @internal_target: a clock time
847 * @cinternal: a reference internal time
848 * @cexternal: a reference external time
849 * @cnum: the numerator of the rate of the clock relative to its
851 * @cdenom: the denominator of the rate of the clock
853 * Converts the given @internal_target clock time to the external time,
854 * using the passed calibration parameters. This function performs the
855 * same calculation as gst_clock_adjust_unlocked() when called using the
856 * current calibration parameters, but doesn't ensure a monotonically
857 * increasing result as gst_clock_adjust_unlocked() does.
859 * Note: The @clock parameter is unused and can be NULL
861 * Returns: the converted time of the clock.
866 gst_clock_adjust_with_calibration (GstClock * clock,
867 GstClockTime internal_target, GstClockTime cinternal,
868 GstClockTime cexternal, GstClockTime cnum, GstClockTime cdenom)
872 /* avoid divide by 0 */
873 if (G_UNLIKELY (cdenom == 0))
876 /* The formula is (internal - cinternal) * cnum / cdenom + cexternal
878 * Since we do math on unsigned 64-bit ints we have to special case for
879 * internal < cinternal to get the sign right. this case is not very common,
882 if (G_LIKELY (internal_target >= cinternal)) {
883 ret = internal_target - cinternal;
884 ret = gst_util_uint64_scale (ret, cnum, cdenom);
887 ret = cinternal - internal_target;
888 ret = gst_util_uint64_scale (ret, cnum, cdenom);
890 if (G_LIKELY (cexternal > ret))
891 ret = cexternal - ret;
900 * gst_clock_adjust_unlocked:
901 * @clock: a #GstClock to use
902 * @internal: a clock time
904 * Converts the given @internal clock time to the external time, adjusting for the
905 * rate and reference time set with gst_clock_set_calibration() and making sure
906 * that the returned time is increasing. This function should be called with the
907 * clock's OBJECT_LOCK held and is mainly used by clock subclasses.
909 * This function is the reverse of gst_clock_unadjust_unlocked().
911 * Returns: the converted time of the clock.
914 gst_clock_adjust_unlocked (GstClock * clock, GstClockTime internal)
916 GstClockTime ret, cinternal, cexternal, cnum, cdenom;
917 GstClockPrivate *priv = clock->priv;
919 /* get calibration values for readability */
920 cinternal = priv->internal_calibration;
921 cexternal = priv->external_calibration;
922 cnum = priv->rate_numerator;
923 cdenom = priv->rate_denominator;
926 gst_clock_adjust_with_calibration (clock, internal, cinternal, cexternal,
929 /* make sure the time is increasing */
930 priv->last_time = MAX (ret, priv->last_time);
932 return priv->last_time;
935 /* FIXME 2.0: Remove clock parameter below */
937 * gst_clock_unadjust_with_calibration:
938 * @clock: (allow-none): a #GstClock to use
939 * @external_target: a clock time
940 * @cinternal: a reference internal time
941 * @cexternal: a reference external time
942 * @cnum: the numerator of the rate of the clock relative to its
944 * @cdenom: the denominator of the rate of the clock
946 * Converts the given @external_target clock time to the internal time,
947 * using the passed calibration parameters. This function performs the
948 * same calculation as gst_clock_unadjust_unlocked() when called using the
949 * current calibration parameters.
951 * Note: The @clock parameter is unused and can be NULL
953 * Returns: the converted time of the clock.
958 gst_clock_unadjust_with_calibration (GstClock * clock,
959 GstClockTime external_target, GstClockTime cinternal,
960 GstClockTime cexternal, GstClockTime cnum, GstClockTime cdenom)
964 /* avoid divide by 0 */
965 if (G_UNLIKELY (cnum == 0))
968 /* The formula is (external - cexternal) * cdenom / cnum + cinternal */
969 if (G_LIKELY (external_target >= cexternal)) {
970 ret = external_target - cexternal;
971 ret = gst_util_uint64_scale (ret, cdenom, cnum);
974 ret = cexternal - external_target;
975 ret = gst_util_uint64_scale (ret, cdenom, cnum);
976 if (G_LIKELY (cinternal > ret))
977 ret = cinternal - ret;
986 * gst_clock_unadjust_unlocked:
987 * @clock: a #GstClock to use
988 * @external: an external clock time
990 * Converts the given @external clock time to the internal time of @clock,
991 * using the rate and reference time set with gst_clock_set_calibration().
992 * This function should be called with the clock's OBJECT_LOCK held and
993 * is mainly used by clock subclasses.
995 * This function is the reverse of gst_clock_adjust_unlocked().
997 * Returns: the internal time of the clock corresponding to @external.
1000 gst_clock_unadjust_unlocked (GstClock * clock, GstClockTime external)
1002 GstClockTime cinternal, cexternal, cnum, cdenom;
1003 GstClockPrivate *priv = clock->priv;
1005 /* get calibration values for readability */
1006 cinternal = priv->internal_calibration;
1007 cexternal = priv->external_calibration;
1008 cnum = priv->rate_numerator;
1009 cdenom = priv->rate_denominator;
1011 return gst_clock_unadjust_with_calibration (clock, external, cinternal,
1012 cexternal, cnum, cdenom);
1016 * gst_clock_get_internal_time:
1017 * @clock: a #GstClock to query
1019 * Gets the current internal time of the given clock. The time is returned
1020 * unadjusted for the offset and the rate.
1022 * Returns: the internal time of the clock. Or GST_CLOCK_TIME_NONE when
1023 * given invalid input.
1028 gst_clock_get_internal_time (GstClock * clock)
1031 GstClockClass *cclass;
1033 g_return_val_if_fail (GST_IS_CLOCK (clock), GST_CLOCK_TIME_NONE);
1035 if (G_UNLIKELY (GST_OBJECT_FLAG_IS_SET (clock,
1036 GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC) && !clock->priv->synced))
1037 GST_CAT_WARNING_OBJECT (GST_CAT_CLOCK, clock,
1038 "clock is not synchronized yet");
1040 cclass = GST_CLOCK_GET_CLASS (clock);
1042 if (G_UNLIKELY (cclass->get_internal_time == NULL))
1045 ret = cclass->get_internal_time (clock);
1047 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock, "internal time %" GST_TIME_FORMAT,
1048 GST_TIME_ARGS (ret));
1055 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
1056 "internal time not supported, return 0");
1057 return G_GINT64_CONSTANT (0);
1062 * gst_clock_get_time:
1063 * @clock: a #GstClock to query
1065 * Gets the current time of the given clock. The time is always
1066 * monotonically increasing and adjusted according to the current
1069 * Returns: the time of the clock. Or GST_CLOCK_TIME_NONE when
1070 * given invalid input.
1075 gst_clock_get_time (GstClock * clock)
1080 g_return_val_if_fail (GST_IS_CLOCK (clock), GST_CLOCK_TIME_NONE);
1083 /* reget the internal time when we retry to get the most current
1085 ret = gst_clock_get_internal_time (clock);
1087 seq = read_seqbegin (clock);
1088 /* this will scale for rate and offset */
1089 ret = gst_clock_adjust_unlocked (clock, ret);
1090 } while (read_seqretry (clock, seq));
1092 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock, "adjusted time %" GST_TIME_FORMAT,
1093 GST_TIME_ARGS (ret));
1099 * gst_clock_set_calibration:
1100 * @clock: a #GstClock to calibrate
1101 * @internal: a reference internal time
1102 * @external: a reference external time
1103 * @rate_num: the numerator of the rate of the clock relative to its
1105 * @rate_denom: the denominator of the rate of the clock
1107 * Adjusts the rate and time of @clock. A rate of 1/1 is the normal speed of
1108 * the clock. Values bigger than 1/1 make the clock go faster.
1110 * @internal and @external are calibration parameters that arrange that
1111 * gst_clock_get_time() should have been @external at internal time @internal.
1112 * This internal time should not be in the future; that is, it should be less
1113 * than the value of gst_clock_get_internal_time() when this function is called.
1115 * Subsequent calls to gst_clock_get_time() will return clock times computed as
1119 * time = (internal_time - internal) * rate_num / rate_denom + external
1122 * This formula is implemented in gst_clock_adjust_unlocked(). Of course, it
1123 * tries to do the integer arithmetic as precisely as possible.
1125 * Note that gst_clock_get_time() always returns increasing values so when you
1126 * move the clock backwards, gst_clock_get_time() will report the previous value
1127 * until the clock catches up.
1132 gst_clock_set_calibration (GstClock * clock, GstClockTime internal, GstClockTime
1133 external, GstClockTime rate_num, GstClockTime rate_denom)
1135 GstClockPrivate *priv;
1137 g_return_if_fail (GST_IS_CLOCK (clock));
1138 g_return_if_fail (rate_num != GST_CLOCK_TIME_NONE);
1139 g_return_if_fail (rate_denom > 0 && rate_denom != GST_CLOCK_TIME_NONE);
1143 write_seqlock (clock);
1144 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
1145 "internal %" GST_TIME_FORMAT " external %" GST_TIME_FORMAT " %"
1146 G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT " = %f", GST_TIME_ARGS (internal),
1147 GST_TIME_ARGS (external), rate_num, rate_denom,
1148 gst_guint64_to_gdouble (rate_num) / gst_guint64_to_gdouble (rate_denom));
1150 priv->internal_calibration = internal;
1151 priv->external_calibration = external;
1152 priv->rate_numerator = rate_num;
1153 priv->rate_denominator = rate_denom;
1154 write_sequnlock (clock);
1158 * gst_clock_get_calibration:
1159 * @clock: a #GstClock
1160 * @internal: (out) (allow-none): a location to store the internal time
1161 * @external: (out) (allow-none): a location to store the external time
1162 * @rate_num: (out) (allow-none): a location to store the rate numerator
1163 * @rate_denom: (out) (allow-none): a location to store the rate denominator
1165 * Gets the internal rate and reference time of @clock. See
1166 * gst_clock_set_calibration() for more information.
1168 * @internal, @external, @rate_num, and @rate_denom can be left %NULL if the
1169 * caller is not interested in the values.
1174 gst_clock_get_calibration (GstClock * clock, GstClockTime * internal,
1175 GstClockTime * external, GstClockTime * rate_num, GstClockTime * rate_denom)
1178 GstClockPrivate *priv;
1180 g_return_if_fail (GST_IS_CLOCK (clock));
1185 seq = read_seqbegin (clock);
1187 *rate_num = priv->rate_numerator;
1189 *rate_denom = priv->rate_denominator;
1191 *external = priv->external_calibration;
1193 *internal = priv->internal_calibration;
1194 } while (read_seqretry (clock, seq));
1197 /* will be called repeatedly to sample the master and slave clock
1198 * to recalibrate the clock */
1200 gst_clock_slave_callback (GstClock * master, GstClockTime time,
1201 GstClockID id, GstClock * clock)
1203 GstClockTime stime, mtime;
1206 if (!gst_clock_is_synced (clock)) {
1207 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
1208 "Slave clock is not synced yet");
1212 stime = gst_clock_get_internal_time (clock);
1213 mtime = gst_clock_get_time (master);
1215 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
1216 "master %" GST_TIME_FORMAT ", slave %" GST_TIME_FORMAT,
1217 GST_TIME_ARGS (mtime), GST_TIME_ARGS (stime));
1219 gst_clock_add_observation (clock, stime, mtime, &r_squared);
1221 /* FIXME, we can use the r_squared value to adjust the timeout
1222 * value of the clockid */
1228 * gst_clock_set_master:
1229 * @clock: a #GstClock
1230 * @master: (allow-none): a master #GstClock
1232 * Set @master as the master clock for @clock. @clock will be automatically
1233 * calibrated so that gst_clock_get_time() reports the same time as the
1236 * A clock provider that slaves its clock to a master can get the current
1237 * calibration values with gst_clock_get_calibration().
1239 * @master can be %NULL in which case @clock will not be slaved anymore. It will
1240 * however keep reporting its time adjusted with the last configured rate
1243 * Returns: %TRUE if the clock is capable of being slaved to a master clock.
1244 * Trying to set a master on a clock without the
1245 * #GST_CLOCK_FLAG_CAN_SET_MASTER flag will make this function return %FALSE.
1250 gst_clock_set_master (GstClock * clock, GstClock * master)
1252 GstClock **master_p;
1253 GstClockPrivate *priv;
1255 g_return_val_if_fail (GST_IS_CLOCK (clock), FALSE);
1256 g_return_val_if_fail (master != clock, FALSE);
1258 GST_OBJECT_LOCK (clock);
1259 /* we always allow setting the master to NULL */
1260 if (master && !GST_OBJECT_FLAG_IS_SET (clock, GST_CLOCK_FLAG_CAN_SET_MASTER))
1262 if (master && !gst_clock_is_synced (master))
1263 goto master_not_synced;
1265 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
1266 "slaving %p to master clock %p", clock, master);
1267 GST_OBJECT_UNLOCK (clock);
1271 GST_CLOCK_SLAVE_LOCK (clock);
1272 if (priv->clockid) {
1273 gst_clock_id_unschedule (priv->clockid);
1274 gst_clock_id_unref (priv->clockid);
1275 priv->clockid = NULL;
1278 priv->filling = TRUE;
1279 priv->time_index = 0;
1280 /* use the master periodic id to schedule sampling and
1281 * clock calibration. */
1282 priv->clockid = gst_clock_new_periodic_id (master,
1283 gst_clock_get_time (master), priv->timeout);
1284 gst_clock_id_wait_async (priv->clockid,
1285 (GstClockCallback) gst_clock_slave_callback,
1286 gst_object_ref (clock), (GDestroyNotify) gst_object_unref);
1288 GST_CLOCK_SLAVE_UNLOCK (clock);
1290 GST_OBJECT_LOCK (clock);
1291 master_p = &priv->master;
1292 gst_object_replace ((GstObject **) master_p, (GstObject *) master);
1293 GST_OBJECT_UNLOCK (clock);
1300 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
1301 "cannot be slaved to a master clock");
1302 GST_OBJECT_UNLOCK (clock);
1308 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, master,
1309 "master clock is not synced yet");
1310 GST_OBJECT_UNLOCK (clock);
1316 * gst_clock_get_master:
1317 * @clock: a #GstClock
1319 * Get the master clock that @clock is slaved to or %NULL when the clock is
1320 * not slaved to any master clock.
1322 * Returns: (transfer full) (nullable): a master #GstClock or %NULL
1323 * when this clock is not slaved to a master clock. Unref after
1329 gst_clock_get_master (GstClock * clock)
1331 GstClock *result = NULL;
1332 GstClockPrivate *priv;
1334 g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);
1338 GST_OBJECT_LOCK (clock);
1340 result = gst_object_ref (priv->master);
1341 GST_OBJECT_UNLOCK (clock);
1347 * gst_clock_add_observation:
1348 * @clock: a #GstClock
1349 * @slave: a time on the slave
1350 * @master: a time on the master
1351 * @r_squared: (out): a pointer to hold the result
1353 * The time @master of the master clock and the time @slave of the slave
1354 * clock are added to the list of observations. If enough observations
1355 * are available, a linear regression algorithm is run on the
1356 * observations and @clock is recalibrated.
1358 * If this functions returns %TRUE, @r_squared will contain the
1359 * correlation coefficient of the interpolation. A value of 1.0
1360 * means a perfect regression was performed. This value can
1361 * be used to control the sampling frequency of the master and slave
1364 * Returns: %TRUE if enough observations were added to run the
1365 * regression algorithm.
1370 gst_clock_add_observation (GstClock * clock, GstClockTime slave,
1371 GstClockTime master, gdouble * r_squared)
1373 GstClockTime m_num, m_denom, b, xbase;
1375 if (!gst_clock_add_observation_unapplied (clock, slave, master, r_squared,
1376 &xbase, &b, &m_num, &m_denom))
1379 /* if we have a valid regression, adjust the clock */
1380 gst_clock_set_calibration (clock, xbase, b, m_num, m_denom);
1386 * gst_clock_add_observation_unapplied:
1387 * @clock: a #GstClock
1388 * @slave: a time on the slave
1389 * @master: a time on the master
1390 * @r_squared: (out): a pointer to hold the result
1391 * @internal: (out) (allow-none): a location to store the internal time
1392 * @external: (out) (allow-none): a location to store the external time
1393 * @rate_num: (out) (allow-none): a location to store the rate numerator
1394 * @rate_denom: (out) (allow-none): a location to store the rate denominator
1396 * Add a clock observation to the internal slaving algorithm the same as
1397 * gst_clock_add_observation(), and return the result of the master clock
1398 * estimation, without updating the internal calibration.
1400 * The caller can then take the results and call gst_clock_set_calibration()
1401 * with the values, or some modified version of them.
1406 gst_clock_add_observation_unapplied (GstClock * clock, GstClockTime slave,
1407 GstClockTime master, gdouble * r_squared,
1408 GstClockTime * internal, GstClockTime * external,
1409 GstClockTime * rate_num, GstClockTime * rate_denom)
1411 GstClockTime m_num, m_denom, b, xbase;
1412 GstClockPrivate *priv;
1415 g_return_val_if_fail (GST_IS_CLOCK (clock), FALSE);
1416 g_return_val_if_fail (r_squared != NULL, FALSE);
1420 GST_CLOCK_SLAVE_LOCK (clock);
1422 GST_CAT_LOG_OBJECT (GST_CAT_CLOCK, clock,
1423 "adding observation slave %" GST_TIME_FORMAT ", master %" GST_TIME_FORMAT,
1424 GST_TIME_ARGS (slave), GST_TIME_ARGS (master));
1426 priv->times[(2 * priv->time_index)] = slave;
1427 priv->times[(2 * priv->time_index) + 1] = master;
1430 if (G_UNLIKELY (priv->time_index == priv->window_size)) {
1431 priv->filling = FALSE;
1432 priv->time_index = 0;
1435 if (G_UNLIKELY (priv->filling && priv->time_index < priv->window_threshold))
1438 n = priv->filling ? priv->time_index : priv->window_size;
1439 if (!gst_calculate_linear_regression (priv->times, priv->times_temp, n,
1440 &m_num, &m_denom, &b, &xbase, r_squared))
1443 GST_CLOCK_SLAVE_UNLOCK (clock);
1445 GST_CAT_LOG_OBJECT (GST_CAT_CLOCK, clock,
1446 "adjusting clock to m=%" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT ", b=%"
1447 G_GUINT64_FORMAT " (rsquared=%g)", m_num, m_denom, b, *r_squared);
1456 *rate_denom = m_denom;
1462 GST_CLOCK_SLAVE_UNLOCK (clock);
1467 /* no valid regression has been done, ignore the result then */
1468 GST_CLOCK_SLAVE_UNLOCK (clock);
1474 * gst_clock_set_timeout:
1475 * @clock: a #GstClock
1476 * @timeout: a timeout
1478 * Set the amount of time, in nanoseconds, to sample master and slave
1482 gst_clock_set_timeout (GstClock * clock, GstClockTime timeout)
1484 g_return_if_fail (GST_IS_CLOCK (clock));
1486 GST_CLOCK_SLAVE_LOCK (clock);
1487 clock->priv->timeout = timeout;
1488 GST_CLOCK_SLAVE_UNLOCK (clock);
1492 * gst_clock_get_timeout:
1493 * @clock: a #GstClock
1495 * Get the amount of time that master and slave clocks are sampled.
1497 * Returns: the interval between samples.
1500 gst_clock_get_timeout (GstClock * clock)
1502 GstClockTime result;
1504 g_return_val_if_fail (GST_IS_CLOCK (clock), GST_CLOCK_TIME_NONE);
1506 GST_CLOCK_SLAVE_LOCK (clock);
1507 result = clock->priv->timeout;
1508 GST_CLOCK_SLAVE_UNLOCK (clock);
1514 gst_clock_set_property (GObject * object, guint prop_id,
1515 const GValue * value, GParamSpec * pspec)
1518 GstClockPrivate *priv;
1520 clock = GST_CLOCK (object);
1524 case PROP_WINDOW_SIZE:
1525 GST_CLOCK_SLAVE_LOCK (clock);
1526 priv->window_size = g_value_get_int (value);
1527 priv->window_threshold = MIN (priv->window_threshold, priv->window_size);
1528 priv->times = g_renew (GstClockTime, priv->times, 4 * priv->window_size);
1529 priv->times_temp = priv->times + 2 * priv->window_size;
1530 /* restart calibration */
1531 priv->filling = TRUE;
1532 priv->time_index = 0;
1533 GST_CLOCK_SLAVE_UNLOCK (clock);
1535 case PROP_WINDOW_THRESHOLD:
1536 GST_CLOCK_SLAVE_LOCK (clock);
1537 priv->window_threshold = MIN (g_value_get_int (value), priv->window_size);
1538 GST_CLOCK_SLAVE_UNLOCK (clock);
1541 gst_clock_set_timeout (clock, g_value_get_uint64 (value));
1544 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1550 gst_clock_get_property (GObject * object, guint prop_id,
1551 GValue * value, GParamSpec * pspec)
1554 GstClockPrivate *priv;
1556 clock = GST_CLOCK (object);
1560 case PROP_WINDOW_SIZE:
1561 GST_CLOCK_SLAVE_LOCK (clock);
1562 g_value_set_int (value, priv->window_size);
1563 GST_CLOCK_SLAVE_UNLOCK (clock);
1565 case PROP_WINDOW_THRESHOLD:
1566 GST_CLOCK_SLAVE_LOCK (clock);
1567 g_value_set_int (value, priv->window_threshold);
1568 GST_CLOCK_SLAVE_UNLOCK (clock);
1571 g_value_set_uint64 (value, gst_clock_get_timeout (clock));
1574 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1581 * gst_clock_wait_for_sync:
1582 * @clock: a GstClock
1583 * @timeout: timeout for waiting or %GST_CLOCK_TIME_NONE
1585 * Waits until @clock is synced for reporting the current time. If @timeout
1586 * is %GST_CLOCK_TIME_NONE it will wait forever, otherwise it will time out
1587 * after @timeout nanoseconds.
1589 * For asynchronous waiting, the GstClock::synced signal can be used.
1592 * This returns immediately with TRUE if GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC
1593 * is not set on the clock, or if the clock is already synced.
1595 * Returns: %TRUE if waiting was successful, or %FALSE on timeout
1600 gst_clock_wait_for_sync (GstClock * clock, GstClockTime timeout)
1602 gboolean timed_out = FALSE;
1604 g_return_val_if_fail (GST_IS_CLOCK (clock), FALSE);
1606 GST_OBJECT_LOCK (clock);
1607 if (!GST_OBJECT_FLAG_IS_SET (clock, GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC)
1608 || clock->priv->synced) {
1609 GST_OBJECT_UNLOCK (clock);
1613 if (timeout != GST_CLOCK_TIME_NONE) {
1614 gint64 end_time = g_get_monotonic_time () + gst_util_uint64_scale (timeout,
1615 G_TIME_SPAN_SECOND, GST_SECOND);
1617 while (!clock->priv->synced && !timed_out) {
1619 !g_cond_wait_until (&clock->priv->sync_cond,
1620 GST_OBJECT_GET_LOCK (clock), end_time);
1624 while (!clock->priv->synced) {
1625 g_cond_wait (&clock->priv->sync_cond, GST_OBJECT_GET_LOCK (clock));
1628 GST_OBJECT_UNLOCK (clock);
1634 * gst_clock_is_synced:
1635 * @clock: a GstClock
1637 * Checks if the clock is currently synced.
1639 * This returns if GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC is not set on the clock.
1641 * Returns: %TRUE if the clock is currently synced
1646 gst_clock_is_synced (GstClock * clock)
1648 g_return_val_if_fail (GST_IS_CLOCK (clock), TRUE);
1650 return !GST_OBJECT_FLAG_IS_SET (clock, GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC)
1651 || clock->priv->synced;
1655 * gst_clock_set_synced:
1656 * @clock: a GstClock
1657 * @synced: if the clock is synced
1659 * Sets @clock to synced and emits the GstClock::synced signal, and wakes up any
1660 * thread waiting in gst_clock_wait_for_sync().
1662 * This function must only be called if GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC
1663 * is set on the clock, and is intended to be called by subclasses only.
1668 gst_clock_set_synced (GstClock * clock, gboolean synced)
1670 g_return_if_fail (GST_IS_CLOCK (clock));
1671 g_return_if_fail (GST_OBJECT_FLAG_IS_SET (clock,
1672 GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC));
1674 GST_OBJECT_LOCK (clock);
1675 if (clock->priv->synced != ! !synced) {
1676 clock->priv->synced = ! !synced;
1677 g_cond_signal (&clock->priv->sync_cond);
1678 GST_OBJECT_UNLOCK (clock);
1679 g_signal_emit (clock, gst_clock_signals[SIGNAL_SYNCED], 0, ! !synced);
1681 GST_OBJECT_UNLOCK (clock);