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 #ifndef GST_DISABLE_TRACE
112 /* #define GST_WITH_ALLOC_TRACE */
113 #include "gsttrace.h"
114 static GstAllocTrace *_gst_clock_entry_trace;
117 /* #define DEBUGGING_ENABLED */
119 #define DEFAULT_WINDOW_SIZE 32
120 #define DEFAULT_WINDOW_THRESHOLD 4
121 #define DEFAULT_TIMEOUT GST_SECOND / 10
127 PROP_WINDOW_THRESHOLD,
137 #define GST_CLOCK_SLAVE_LOCK(clock) g_mutex_lock (&GST_CLOCK_CAST (clock)->priv->slave_lock)
138 #define GST_CLOCK_SLAVE_UNLOCK(clock) g_mutex_unlock (&GST_CLOCK_CAST (clock)->priv->slave_lock)
140 struct _GstClockPrivate
142 GMutex slave_lock; /* order: SLAVE_LOCK, OBJECT_LOCK */
147 GstClockTime internal_calibration;
148 GstClockTime external_calibration;
149 GstClockTime rate_numerator;
150 GstClockTime rate_denominator;
151 GstClockTime last_time;
154 GstClockTime resolution;
156 /* for master/slave clocks */
159 /* with SLAVE_LOCK */
162 gint window_threshold;
164 GstClockTime timeout;
175 #define read_seqbegin(clock) \
176 g_atomic_int_get (&clock->priv->post_count);
178 static inline gboolean
179 read_seqretry (GstClock * clock, gint seq)
181 /* no retry if the seqnum did not change */
182 if (G_LIKELY (seq == g_atomic_int_get (&clock->priv->pre_count)))
185 /* wait for the writer to finish and retry */
186 GST_OBJECT_LOCK (clock);
187 GST_OBJECT_UNLOCK (clock);
191 #define write_seqlock(clock) \
193 GST_OBJECT_LOCK (clock); \
194 g_atomic_int_inc (&clock->priv->pre_count); \
197 #define write_sequnlock(clock) \
199 g_atomic_int_inc (&clock->priv->post_count); \
200 GST_OBJECT_UNLOCK (clock); \
203 #ifndef GST_DISABLE_GST_DEBUG
205 gst_clock_return_get_name (GstClockReturn ret)
210 case GST_CLOCK_EARLY:
212 case GST_CLOCK_UNSCHEDULED:
213 return "unscheduled";
216 case GST_CLOCK_BADTIME:
218 case GST_CLOCK_ERROR:
220 case GST_CLOCK_UNSUPPORTED:
221 return "unsupported";
230 #endif /* GST_DISABLE_GST_DEBUG */
232 static void gst_clock_dispose (GObject * object);
233 static void gst_clock_finalize (GObject * object);
235 static void gst_clock_set_property (GObject * object, guint prop_id,
236 const GValue * value, GParamSpec * pspec);
237 static void gst_clock_get_property (GObject * object, guint prop_id,
238 GValue * value, GParamSpec * pspec);
240 static guint gst_clock_signals[SIGNAL_LAST] = { 0 };
243 gst_clock_entry_new (GstClock * clock, GstClockTime time,
244 GstClockTime interval, GstClockEntryType type)
246 GstClockEntry *entry;
248 entry = g_slice_new (GstClockEntry);
249 #ifndef GST_DISABLE_TRACE
250 _gst_alloc_trace_new (_gst_clock_entry_trace, entry);
252 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
253 "created entry %p, time %" GST_TIME_FORMAT, entry, GST_TIME_ARGS (time));
256 entry->clock = clock;
259 entry->interval = interval;
260 entry->status = GST_CLOCK_OK;
262 entry->user_data = NULL;
263 entry->destroy_data = NULL;
264 entry->unscheduled = FALSE;
265 entry->woken_up = FALSE;
267 return (GstClockID) entry;
270 /* WARNING : Does not modify the refcount
271 * WARNING : Do not use if a pending clock operation is happening on that entry */
273 gst_clock_entry_reinit (GstClock * clock, GstClockEntry * entry,
274 GstClockTime time, GstClockTime interval, GstClockEntryType type)
276 g_return_val_if_fail (entry->status != GST_CLOCK_BUSY, FALSE);
277 g_return_val_if_fail (entry->clock == clock, FALSE);
281 entry->interval = interval;
282 entry->status = GST_CLOCK_OK;
283 entry->unscheduled = FALSE;
284 entry->woken_up = FALSE;
290 * gst_clock_single_shot_id_reinit:
291 * @clock: a #GstClock
293 * @time: The requested time.
295 * Reinitializes the provided single shot @id to the provided time. Does not
296 * modify the reference count.
298 * Returns: %TRUE if the GstClockID could be reinitialized to the provided
299 * @time, else %FALSE.
302 gst_clock_single_shot_id_reinit (GstClock * clock, GstClockID id,
305 return gst_clock_entry_reinit (clock, (GstClockEntry *) id, time,
306 GST_CLOCK_TIME_NONE, GST_CLOCK_ENTRY_SINGLE);
310 * gst_clock_periodic_id_reinit:
311 * @clock: a #GstClock
313 * @start_time: the requested start time
314 * @interval: the requested interval
316 * Reinitializes the provided periodic @id to the provided start time and
317 * interval. Does not modify the reference count.
319 * Returns: %TRUE if the GstClockID could be reinitialized to the provided
320 * @time, else %FALSE.
323 gst_clock_periodic_id_reinit (GstClock * clock, GstClockID id,
324 GstClockTime start_time, GstClockTime interval)
326 return gst_clock_entry_reinit (clock, (GstClockEntry *) id, start_time,
327 interval, GST_CLOCK_ENTRY_PERIODIC);
332 * @id: The #GstClockID to ref
334 * Increase the refcount of given @id.
336 * Returns: (transfer full): The same #GstClockID with increased refcount.
341 gst_clock_id_ref (GstClockID id)
343 g_return_val_if_fail (id != NULL, NULL);
345 g_atomic_int_inc (&((GstClockEntry *) id)->refcount);
351 _gst_clock_id_free (GstClockID id)
353 GstClockEntry *entry;
354 g_return_if_fail (id != NULL);
356 GST_CAT_DEBUG (GST_CAT_CLOCK, "freed entry %p", id);
357 entry = (GstClockEntry *) id;
358 if (entry->destroy_data)
359 entry->destroy_data (entry->user_data);
361 #ifndef GST_DISABLE_TRACE
362 _gst_alloc_trace_free (_gst_clock_entry_trace, id);
364 g_slice_free (GstClockEntry, id);
368 * gst_clock_id_unref:
369 * @id: (transfer full): The #GstClockID to unref
371 * Unref given @id. When the refcount reaches 0 the
372 * #GstClockID will be freed.
377 gst_clock_id_unref (GstClockID id)
381 g_return_if_fail (id != NULL);
383 zero = g_atomic_int_dec_and_test (&((GstClockEntry *) id)->refcount);
384 /* if we ended up with the refcount at zero, free the id */
386 _gst_clock_id_free (id);
391 * gst_clock_new_single_shot_id:
392 * @clock: The #GstClockID to get a single shot notification from
393 * @time: the requested time
395 * Get a #GstClockID from @clock to trigger a single shot
396 * notification at the requested time. The single shot id should be
397 * unreffed after usage.
399 * Free-function: gst_clock_id_unref
401 * Returns: (transfer full): a #GstClockID that can be used to request the
407 gst_clock_new_single_shot_id (GstClock * clock, GstClockTime time)
409 g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);
411 return gst_clock_entry_new (clock,
412 time, GST_CLOCK_TIME_NONE, GST_CLOCK_ENTRY_SINGLE);
416 * gst_clock_new_periodic_id:
417 * @clock: The #GstClockID to get a periodic notification id from
418 * @start_time: the requested start time
419 * @interval: the requested interval
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
426 * Free-function: gst_clock_id_unref
428 * Returns: (transfer full): a #GstClockID that can be used to request the
434 gst_clock_new_periodic_id (GstClock * clock, GstClockTime start_time,
435 GstClockTime interval)
437 g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);
438 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (start_time), NULL);
439 g_return_val_if_fail (interval != 0, NULL);
440 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), NULL);
442 return gst_clock_entry_new (clock,
443 start_time, interval, GST_CLOCK_ENTRY_PERIODIC);
447 * gst_clock_id_compare_func:
448 * @id1: A #GstClockID
449 * @id2: A #GstClockID to compare with
451 * Compares the two #GstClockID instances. This function can be used
452 * as a GCompareFunc when sorting ids.
454 * Returns: negative value if a < b; zero if a = b; positive value if a > b
459 gst_clock_id_compare_func (gconstpointer id1, gconstpointer id2)
461 GstClockEntry *entry1, *entry2;
463 entry1 = (GstClockEntry *) id1;
464 entry2 = (GstClockEntry *) id2;
466 if (GST_CLOCK_ENTRY_TIME (entry1) > GST_CLOCK_ENTRY_TIME (entry2)) {
469 if (GST_CLOCK_ENTRY_TIME (entry1) < GST_CLOCK_ENTRY_TIME (entry2)) {
476 * gst_clock_id_get_time:
477 * @id: The #GstClockID to query
479 * Get the time of the clock ID
481 * Returns: the time of the given clock id.
486 gst_clock_id_get_time (GstClockID id)
488 g_return_val_if_fail (id != NULL, GST_CLOCK_TIME_NONE);
490 return GST_CLOCK_ENTRY_TIME ((GstClockEntry *) id);
495 * @id: The #GstClockID to wait on
496 * @jitter: (out) (allow-none): a pointer that will contain the jitter,
499 * Perform a blocking wait on @id.
500 * @id should have been created with gst_clock_new_single_shot_id()
501 * or gst_clock_new_periodic_id() and should not have been unscheduled
502 * with a call to gst_clock_id_unschedule().
504 * If the @jitter argument is not %NULL and this function returns #GST_CLOCK_OK
505 * or #GST_CLOCK_EARLY, it will contain the difference
506 * against the clock and the time of @id when this method was
508 * Positive values indicate how late @id was relative to the clock
509 * (in which case this function will return #GST_CLOCK_EARLY).
510 * Negative values indicate how much time was spent waiting on the clock
511 * before this function returned.
513 * Returns: the result of the blocking wait. #GST_CLOCK_EARLY will be returned
514 * if the current clock time is past the time of @id, #GST_CLOCK_OK if
515 * @id was scheduled in time. #GST_CLOCK_UNSCHEDULED if @id was
516 * unscheduled with gst_clock_id_unschedule().
521 gst_clock_id_wait (GstClockID id, GstClockTimeDiff * jitter)
523 GstClockEntry *entry;
526 GstClockTime requested;
527 GstClockClass *cclass;
529 g_return_val_if_fail (id != NULL, GST_CLOCK_ERROR);
531 entry = (GstClockEntry *) id;
532 requested = GST_CLOCK_ENTRY_TIME (entry);
534 clock = GST_CLOCK_ENTRY_CLOCK (entry);
536 /* can't sync on invalid times */
537 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (requested)))
540 cclass = GST_CLOCK_GET_CLASS (clock);
542 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock, "waiting on clock entry %p", id);
544 /* if we have a wait_jitter function, use that */
545 if (G_UNLIKELY (cclass->wait == NULL))
548 res = cclass->wait (clock, entry, jitter);
550 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
551 "done waiting entry %p, res: %d (%s)", id, res,
552 gst_clock_return_get_name (res));
554 if (entry->type == GST_CLOCK_ENTRY_PERIODIC)
555 entry->time = requested + entry->interval;
562 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
563 "invalid time requested, returning _BADTIME");
564 return GST_CLOCK_BADTIME;
568 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock, "clock wait is not supported");
569 return GST_CLOCK_UNSUPPORTED;
574 * gst_clock_id_wait_async:
575 * @id: a #GstClockID to wait on
576 * @func: The callback function
577 * @user_data: User data passed in the callback
578 * @destroy_data: #GDestroyNotify for user_data
580 * Register a callback on the given #GstClockID @id with the given
581 * function and user_data. When passing a #GstClockID with an invalid
582 * time to this function, the callback will be called immediately
583 * with a time set to GST_CLOCK_TIME_NONE. The callback will
584 * be called when the time of @id has been reached.
586 * The callback @func can be invoked from any thread, either provided by the
587 * core or from a streaming thread. The application should be prepared for this.
589 * Returns: the result of the non blocking wait.
594 gst_clock_id_wait_async (GstClockID id,
595 GstClockCallback func, gpointer user_data, GDestroyNotify destroy_data)
597 GstClockEntry *entry;
600 GstClockClass *cclass;
601 GstClockTime requested;
603 g_return_val_if_fail (id != NULL, GST_CLOCK_ERROR);
604 g_return_val_if_fail (func != NULL, GST_CLOCK_ERROR);
606 entry = (GstClockEntry *) id;
607 requested = GST_CLOCK_ENTRY_TIME (entry);
608 clock = GST_CLOCK_ENTRY_CLOCK (entry);
610 /* can't sync on invalid times */
611 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (requested)))
614 cclass = GST_CLOCK_GET_CLASS (clock);
616 if (G_UNLIKELY (cclass->wait_async == NULL))
620 entry->user_data = user_data;
621 entry->destroy_data = destroy_data;
623 res = cclass->wait_async (clock, entry);
630 (func) (clock, GST_CLOCK_TIME_NONE, id, user_data);
631 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
632 "invalid time requested, returning _BADTIME");
633 return GST_CLOCK_BADTIME;
637 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock, "clock wait is not supported");
638 return GST_CLOCK_UNSUPPORTED;
643 * gst_clock_id_unschedule:
644 * @id: The id to unschedule
646 * Cancel an outstanding request with @id. This can either
647 * be an outstanding async notification or a pending sync notification.
648 * After this call, @id cannot be used anymore to receive sync or
649 * async notifications, you need to create a new #GstClockID.
654 gst_clock_id_unschedule (GstClockID id)
656 GstClockEntry *entry;
658 GstClockClass *cclass;
660 g_return_if_fail (id != NULL);
662 entry = (GstClockEntry *) id;
663 clock = entry->clock;
665 cclass = GST_CLOCK_GET_CLASS (clock);
667 if (G_LIKELY (cclass->unschedule))
668 cclass->unschedule (clock, entry);
673 * GstClock abstract base class implementation
675 #define gst_clock_parent_class parent_class
676 G_DEFINE_ABSTRACT_TYPE (GstClock, gst_clock, GST_TYPE_OBJECT);
679 gst_clock_class_init (GstClockClass * klass)
681 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
683 #ifndef GST_DISABLE_TRACE
684 _gst_clock_entry_trace = _gst_alloc_trace_register ("GstClockEntry", -1);
687 gobject_class->dispose = gst_clock_dispose;
688 gobject_class->finalize = gst_clock_finalize;
689 gobject_class->set_property = gst_clock_set_property;
690 gobject_class->get_property = gst_clock_get_property;
692 g_object_class_install_property (gobject_class, PROP_WINDOW_SIZE,
693 g_param_spec_int ("window-size", "Window size",
694 "The size of the window used to calculate rate and offset", 2, 1024,
695 DEFAULT_WINDOW_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
696 g_object_class_install_property (gobject_class, PROP_WINDOW_THRESHOLD,
697 g_param_spec_int ("window-threshold", "Window threshold",
698 "The threshold to start calculating rate and offset", 2, 1024,
699 DEFAULT_WINDOW_THRESHOLD,
700 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
701 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
702 g_param_spec_uint64 ("timeout", "Timeout",
703 "The amount of time, in nanoseconds, to sample master and slave clocks",
704 0, G_MAXUINT64, DEFAULT_TIMEOUT,
705 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
710 * @synced: if the clock is synced now
712 * Signaled on clocks with GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC set once
713 * the clock is synchronized, or when it completely lost synchronization.
714 * This signal will not be emitted on clocks without the flag.
716 * This signal will be emitted from an arbitrary thread, most likely not
717 * the application's main thread.
721 gst_clock_signals[SIGNAL_SYNCED] =
722 g_signal_new ("synced", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
724 g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
726 g_type_class_add_private (klass, sizeof (GstClockPrivate));
730 gst_clock_init (GstClock * clock)
732 GstClockPrivate *priv;
735 G_TYPE_INSTANCE_GET_PRIVATE (clock, GST_TYPE_CLOCK, GstClockPrivate);
739 priv->internal_calibration = 0;
740 priv->external_calibration = 0;
741 priv->rate_numerator = 1;
742 priv->rate_denominator = 1;
744 g_mutex_init (&priv->slave_lock);
745 g_cond_init (&priv->sync_cond);
746 priv->window_size = DEFAULT_WINDOW_SIZE;
747 priv->window_threshold = DEFAULT_WINDOW_THRESHOLD;
748 priv->filling = TRUE;
749 priv->time_index = 0;
750 priv->timeout = DEFAULT_TIMEOUT;
751 priv->times = g_new0 (GstClockTime, 4 * priv->window_size);
753 /* clear floating flag */
754 gst_object_ref_sink (clock);
758 gst_clock_dispose (GObject * object)
760 GstClock *clock = GST_CLOCK (object);
763 GST_OBJECT_LOCK (clock);
764 master_p = &clock->priv->master;
765 gst_object_replace ((GstObject **) master_p, NULL);
766 GST_OBJECT_UNLOCK (clock);
768 G_OBJECT_CLASS (parent_class)->dispose (object);
772 gst_clock_finalize (GObject * object)
774 GstClock *clock = GST_CLOCK (object);
776 GST_CLOCK_SLAVE_LOCK (clock);
777 if (clock->priv->clockid) {
778 gst_clock_id_unschedule (clock->priv->clockid);
779 gst_clock_id_unref (clock->priv->clockid);
780 clock->priv->clockid = NULL;
782 g_free (clock->priv->times);
783 clock->priv->times = NULL;
784 GST_CLOCK_SLAVE_UNLOCK (clock);
786 g_mutex_clear (&clock->priv->slave_lock);
787 g_cond_clear (&clock->priv->sync_cond);
789 G_OBJECT_CLASS (parent_class)->finalize (object);
793 * gst_clock_set_resolution:
794 * @clock: a #GstClock
795 * @resolution: The resolution to set
797 * Set the accuracy of the clock. Some clocks have the possibility to operate
798 * with different accuracy at the expense of more resource usage. There is
799 * normally no need to change the default resolution of a clock. The resolution
800 * of a clock can only be changed if the clock has the
801 * #GST_CLOCK_FLAG_CAN_SET_RESOLUTION flag set.
803 * Returns: the new resolution of the clock.
806 gst_clock_set_resolution (GstClock * clock, GstClockTime resolution)
808 GstClockPrivate *priv;
809 GstClockClass *cclass;
811 g_return_val_if_fail (GST_IS_CLOCK (clock), 0);
812 g_return_val_if_fail (resolution != 0, 0);
814 cclass = GST_CLOCK_GET_CLASS (clock);
817 if (cclass->change_resolution)
819 cclass->change_resolution (clock, priv->resolution, resolution);
821 return priv->resolution;
825 * gst_clock_get_resolution:
826 * @clock: a #GstClock
828 * Get the accuracy of the clock. The accuracy of the clock is the granularity
829 * of the values returned by gst_clock_get_time().
831 * Returns: the resolution of the clock in units of #GstClockTime.
836 gst_clock_get_resolution (GstClock * clock)
838 GstClockClass *cclass;
840 g_return_val_if_fail (GST_IS_CLOCK (clock), 0);
842 cclass = GST_CLOCK_GET_CLASS (clock);
844 if (cclass->get_resolution)
845 return cclass->get_resolution (clock);
850 /* FIXME 2.0: Remove clock parameter below */
852 * gst_clock_adjust_with_calibration:
853 * @clock: (allow-none): a #GstClock to use
854 * @internal_target: a clock time
855 * @cinternal: a reference internal time
856 * @cexternal: a reference external time
857 * @cnum: the numerator of the rate of the clock relative to its
859 * @cdenom: the denominator of the rate of the clock
861 * Converts the given @internal_target clock time to the external time,
862 * using the passed calibration parameters. This function performs the
863 * same calculation as gst_clock_adjust_unlocked() when called using the
864 * current calibration parameters, but doesn't ensure a monotonically
865 * increasing result as gst_clock_adjust_unlocked() does.
867 * Note: The @clock parameter is unused and can be NULL
869 * Returns: the converted time of the clock.
874 gst_clock_adjust_with_calibration (GstClock * clock,
875 GstClockTime internal_target, GstClockTime cinternal,
876 GstClockTime cexternal, GstClockTime cnum, GstClockTime cdenom)
880 /* avoid divide by 0 */
881 if (G_UNLIKELY (cdenom == 0))
884 /* The formula is (internal - cinternal) * cnum / cdenom + cexternal
886 * Since we do math on unsigned 64-bit ints we have to special case for
887 * internal < cinternal to get the sign right. this case is not very common,
890 if (G_LIKELY (internal_target >= cinternal)) {
891 ret = internal_target - cinternal;
892 ret = gst_util_uint64_scale (ret, cnum, cdenom);
895 ret = cinternal - internal_target;
896 ret = gst_util_uint64_scale (ret, cnum, cdenom);
898 if (G_LIKELY (cexternal > ret))
899 ret = cexternal - ret;
908 * gst_clock_adjust_unlocked:
909 * @clock: a #GstClock to use
910 * @internal: a clock time
912 * Converts the given @internal clock time to the external time, adjusting for the
913 * rate and reference time set with gst_clock_set_calibration() and making sure
914 * that the returned time is increasing. This function should be called with the
915 * clock's OBJECT_LOCK held and is mainly used by clock subclasses.
917 * This function is the reverse of gst_clock_unadjust_unlocked().
919 * Returns: the converted time of the clock.
922 gst_clock_adjust_unlocked (GstClock * clock, GstClockTime internal)
924 GstClockTime ret, cinternal, cexternal, cnum, cdenom;
925 GstClockPrivate *priv = clock->priv;
927 /* get calibration values for readability */
928 cinternal = priv->internal_calibration;
929 cexternal = priv->external_calibration;
930 cnum = priv->rate_numerator;
931 cdenom = priv->rate_denominator;
934 gst_clock_adjust_with_calibration (clock, internal, cinternal, cexternal,
937 /* make sure the time is increasing */
938 priv->last_time = MAX (ret, priv->last_time);
940 return priv->last_time;
943 /* FIXME 2.0: Remove clock parameter below */
945 * gst_clock_unadjust_with_calibration:
946 * @clock: (allow-none): a #GstClock to use
947 * @external_target: a clock time
948 * @cinternal: a reference internal time
949 * @cexternal: a reference external time
950 * @cnum: the numerator of the rate of the clock relative to its
952 * @cdenom: the denominator of the rate of the clock
954 * Converts the given @external_target clock time to the internal time,
955 * using the passed calibration parameters. This function performs the
956 * same calculation as gst_clock_unadjust_unlocked() when called using the
957 * current calibration parameters.
959 * Note: The @clock parameter is unused and can be NULL
961 * Returns: the converted time of the clock.
966 gst_clock_unadjust_with_calibration (GstClock * clock,
967 GstClockTime external_target, GstClockTime cinternal,
968 GstClockTime cexternal, GstClockTime cnum, GstClockTime cdenom)
972 /* avoid divide by 0 */
973 if (G_UNLIKELY (cnum == 0))
976 /* The formula is (external - cexternal) * cdenom / cnum + cinternal */
977 if (G_LIKELY (external_target >= cexternal)) {
978 ret = external_target - cexternal;
979 ret = gst_util_uint64_scale (ret, cdenom, cnum);
982 ret = cexternal - external_target;
983 ret = gst_util_uint64_scale (ret, cdenom, cnum);
984 if (G_LIKELY (cinternal > ret))
985 ret = cinternal - ret;
994 * gst_clock_unadjust_unlocked:
995 * @clock: a #GstClock to use
996 * @external: an external clock time
998 * Converts the given @external clock time to the internal time of @clock,
999 * using the rate and reference time set with gst_clock_set_calibration().
1000 * This function should be called with the clock's OBJECT_LOCK held and
1001 * is mainly used by clock subclasses.
1003 * This function is the reverse of gst_clock_adjust_unlocked().
1005 * Returns: the internal time of the clock corresponding to @external.
1008 gst_clock_unadjust_unlocked (GstClock * clock, GstClockTime external)
1010 GstClockTime cinternal, cexternal, cnum, cdenom;
1011 GstClockPrivate *priv = clock->priv;
1013 /* get calibration values for readability */
1014 cinternal = priv->internal_calibration;
1015 cexternal = priv->external_calibration;
1016 cnum = priv->rate_numerator;
1017 cdenom = priv->rate_denominator;
1019 return gst_clock_unadjust_with_calibration (clock, external, cinternal,
1020 cexternal, cnum, cdenom);
1024 * gst_clock_get_internal_time:
1025 * @clock: a #GstClock to query
1027 * Gets the current internal time of the given clock. The time is returned
1028 * unadjusted for the offset and the rate.
1030 * Returns: the internal time of the clock. Or GST_CLOCK_TIME_NONE when
1031 * given invalid input.
1036 gst_clock_get_internal_time (GstClock * clock)
1039 GstClockClass *cclass;
1041 g_return_val_if_fail (GST_IS_CLOCK (clock), GST_CLOCK_TIME_NONE);
1043 if (G_UNLIKELY (GST_OBJECT_FLAG_IS_SET (clock,
1044 GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC) && !clock->priv->synced))
1045 GST_CAT_WARNING_OBJECT (GST_CAT_CLOCK, clock,
1046 "clock is not synchronized yet");
1048 cclass = GST_CLOCK_GET_CLASS (clock);
1050 if (G_UNLIKELY (cclass->get_internal_time == NULL))
1053 ret = cclass->get_internal_time (clock);
1055 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock, "internal time %" GST_TIME_FORMAT,
1056 GST_TIME_ARGS (ret));
1063 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
1064 "internal time not supported, return 0");
1065 return G_GINT64_CONSTANT (0);
1070 * gst_clock_get_time:
1071 * @clock: a #GstClock to query
1073 * Gets the current time of the given clock. The time is always
1074 * monotonically increasing and adjusted according to the current
1077 * Returns: the time of the clock. Or GST_CLOCK_TIME_NONE when
1078 * given invalid input.
1083 gst_clock_get_time (GstClock * clock)
1088 g_return_val_if_fail (GST_IS_CLOCK (clock), GST_CLOCK_TIME_NONE);
1091 /* reget the internal time when we retry to get the most current
1093 ret = gst_clock_get_internal_time (clock);
1095 seq = read_seqbegin (clock);
1096 /* this will scale for rate and offset */
1097 ret = gst_clock_adjust_unlocked (clock, ret);
1098 } while (read_seqretry (clock, seq));
1100 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock, "adjusted time %" GST_TIME_FORMAT,
1101 GST_TIME_ARGS (ret));
1107 * gst_clock_set_calibration:
1108 * @clock: a #GstClock to calibrate
1109 * @internal: a reference internal time
1110 * @external: a reference external time
1111 * @rate_num: the numerator of the rate of the clock relative to its
1113 * @rate_denom: the denominator of the rate of the clock
1115 * Adjusts the rate and time of @clock. A rate of 1/1 is the normal speed of
1116 * the clock. Values bigger than 1/1 make the clock go faster.
1118 * @internal and @external are calibration parameters that arrange that
1119 * gst_clock_get_time() should have been @external at internal time @internal.
1120 * This internal time should not be in the future; that is, it should be less
1121 * than the value of gst_clock_get_internal_time() when this function is called.
1123 * Subsequent calls to gst_clock_get_time() will return clock times computed as
1127 * time = (internal_time - internal) * rate_num / rate_denom + external
1130 * This formula is implemented in gst_clock_adjust_unlocked(). Of course, it
1131 * tries to do the integer arithmetic as precisely as possible.
1133 * Note that gst_clock_get_time() always returns increasing values so when you
1134 * move the clock backwards, gst_clock_get_time() will report the previous value
1135 * until the clock catches up.
1140 gst_clock_set_calibration (GstClock * clock, GstClockTime internal, GstClockTime
1141 external, GstClockTime rate_num, GstClockTime rate_denom)
1143 GstClockPrivate *priv;
1145 g_return_if_fail (GST_IS_CLOCK (clock));
1146 g_return_if_fail (rate_num != GST_CLOCK_TIME_NONE);
1147 g_return_if_fail (rate_denom > 0 && rate_denom != GST_CLOCK_TIME_NONE);
1151 write_seqlock (clock);
1152 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
1153 "internal %" GST_TIME_FORMAT " external %" GST_TIME_FORMAT " %"
1154 G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT " = %f", GST_TIME_ARGS (internal),
1155 GST_TIME_ARGS (external), rate_num, rate_denom,
1156 gst_guint64_to_gdouble (rate_num) / gst_guint64_to_gdouble (rate_denom));
1158 priv->internal_calibration = internal;
1159 priv->external_calibration = external;
1160 priv->rate_numerator = rate_num;
1161 priv->rate_denominator = rate_denom;
1162 write_sequnlock (clock);
1166 * gst_clock_get_calibration:
1167 * @clock: a #GstClock
1168 * @internal: (out) (allow-none): a location to store the internal time
1169 * @external: (out) (allow-none): a location to store the external time
1170 * @rate_num: (out) (allow-none): a location to store the rate numerator
1171 * @rate_denom: (out) (allow-none): a location to store the rate denominator
1173 * Gets the internal rate and reference time of @clock. See
1174 * gst_clock_set_calibration() for more information.
1176 * @internal, @external, @rate_num, and @rate_denom can be left %NULL if the
1177 * caller is not interested in the values.
1182 gst_clock_get_calibration (GstClock * clock, GstClockTime * internal,
1183 GstClockTime * external, GstClockTime * rate_num, GstClockTime * rate_denom)
1186 GstClockPrivate *priv;
1188 g_return_if_fail (GST_IS_CLOCK (clock));
1193 seq = read_seqbegin (clock);
1195 *rate_num = priv->rate_numerator;
1197 *rate_denom = priv->rate_denominator;
1199 *external = priv->external_calibration;
1201 *internal = priv->internal_calibration;
1202 } while (read_seqretry (clock, seq));
1205 /* will be called repeatedly to sample the master and slave clock
1206 * to recalibrate the clock */
1208 gst_clock_slave_callback (GstClock * master, GstClockTime time,
1209 GstClockID id, GstClock * clock)
1211 GstClockTime stime, mtime;
1214 if (!gst_clock_is_synced (clock)) {
1215 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
1216 "Slave clock is not synced yet");
1220 stime = gst_clock_get_internal_time (clock);
1221 mtime = gst_clock_get_time (master);
1223 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
1224 "master %" GST_TIME_FORMAT ", slave %" GST_TIME_FORMAT,
1225 GST_TIME_ARGS (mtime), GST_TIME_ARGS (stime));
1227 gst_clock_add_observation (clock, stime, mtime, &r_squared);
1229 /* FIXME, we can use the r_squared value to adjust the timeout
1230 * value of the clockid */
1236 * gst_clock_set_master:
1237 * @clock: a #GstClock
1238 * @master: (allow-none): a master #GstClock
1240 * Set @master as the master clock for @clock. @clock will be automatically
1241 * calibrated so that gst_clock_get_time() reports the same time as the
1244 * A clock provider that slaves its clock to a master can get the current
1245 * calibration values with gst_clock_get_calibration().
1247 * @master can be %NULL in which case @clock will not be slaved anymore. It will
1248 * however keep reporting its time adjusted with the last configured rate
1251 * Returns: %TRUE if the clock is capable of being slaved to a master clock.
1252 * Trying to set a master on a clock without the
1253 * #GST_CLOCK_FLAG_CAN_SET_MASTER flag will make this function return %FALSE.
1258 gst_clock_set_master (GstClock * clock, GstClock * master)
1260 GstClock **master_p;
1261 GstClockPrivate *priv;
1263 g_return_val_if_fail (GST_IS_CLOCK (clock), FALSE);
1264 g_return_val_if_fail (master != clock, FALSE);
1266 GST_OBJECT_LOCK (clock);
1267 /* we always allow setting the master to NULL */
1268 if (master && !GST_OBJECT_FLAG_IS_SET (clock, GST_CLOCK_FLAG_CAN_SET_MASTER))
1270 if (master && !gst_clock_is_synced (master))
1271 goto master_not_synced;
1273 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
1274 "slaving %p to master clock %p", clock, master);
1275 GST_OBJECT_UNLOCK (clock);
1279 GST_CLOCK_SLAVE_LOCK (clock);
1280 if (priv->clockid) {
1281 gst_clock_id_unschedule (priv->clockid);
1282 gst_clock_id_unref (priv->clockid);
1283 priv->clockid = NULL;
1286 priv->filling = TRUE;
1287 priv->time_index = 0;
1288 /* use the master periodic id to schedule sampling and
1289 * clock calibration. */
1290 priv->clockid = gst_clock_new_periodic_id (master,
1291 gst_clock_get_time (master), priv->timeout);
1292 gst_clock_id_wait_async (priv->clockid,
1293 (GstClockCallback) gst_clock_slave_callback,
1294 gst_object_ref (clock), (GDestroyNotify) gst_object_unref);
1296 GST_CLOCK_SLAVE_UNLOCK (clock);
1298 GST_OBJECT_LOCK (clock);
1299 master_p = &priv->master;
1300 gst_object_replace ((GstObject **) master_p, (GstObject *) master);
1301 GST_OBJECT_UNLOCK (clock);
1308 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
1309 "cannot be slaved to a master clock");
1310 GST_OBJECT_UNLOCK (clock);
1316 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, master,
1317 "master clock is not synced yet");
1318 GST_OBJECT_UNLOCK (clock);
1324 * gst_clock_get_master:
1325 * @clock: a #GstClock
1327 * Get the master clock that @clock is slaved to or %NULL when the clock is
1328 * not slaved to any master clock.
1330 * Returns: (transfer full) (nullable): a master #GstClock or %NULL
1331 * when this clock is not slaved to a master clock. Unref after
1337 gst_clock_get_master (GstClock * clock)
1339 GstClock *result = NULL;
1340 GstClockPrivate *priv;
1342 g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);
1346 GST_OBJECT_LOCK (clock);
1348 result = gst_object_ref (priv->master);
1349 GST_OBJECT_UNLOCK (clock);
1355 * gst_clock_add_observation:
1356 * @clock: a #GstClock
1357 * @slave: a time on the slave
1358 * @master: a time on the master
1359 * @r_squared: (out): a pointer to hold the result
1361 * The time @master of the master clock and the time @slave of the slave
1362 * clock are added to the list of observations. If enough observations
1363 * are available, a linear regression algorithm is run on the
1364 * observations and @clock is recalibrated.
1366 * If this functions returns %TRUE, @r_squared will contain the
1367 * correlation coefficient of the interpolation. A value of 1.0
1368 * means a perfect regression was performed. This value can
1369 * be used to control the sampling frequency of the master and slave
1372 * Returns: %TRUE if enough observations were added to run the
1373 * regression algorithm.
1378 gst_clock_add_observation (GstClock * clock, GstClockTime slave,
1379 GstClockTime master, gdouble * r_squared)
1381 GstClockTime m_num, m_denom, b, xbase;
1383 if (!gst_clock_add_observation_unapplied (clock, slave, master, r_squared,
1384 &xbase, &b, &m_num, &m_denom))
1387 /* if we have a valid regression, adjust the clock */
1388 gst_clock_set_calibration (clock, xbase, b, m_num, m_denom);
1394 * gst_clock_add_observation_unapplied:
1395 * @clock: a #GstClock
1396 * @slave: a time on the slave
1397 * @master: a time on the master
1398 * @r_squared: (out): a pointer to hold the result
1399 * @internal: (out) (allow-none): a location to store the internal time
1400 * @external: (out) (allow-none): a location to store the external time
1401 * @rate_num: (out) (allow-none): a location to store the rate numerator
1402 * @rate_denom: (out) (allow-none): a location to store the rate denominator
1404 * Add a clock observation to the internal slaving algorithm the same as
1405 * gst_clock_add_observation(), and return the result of the master clock
1406 * estimation, without updating the internal calibration.
1408 * The caller can then take the results and call gst_clock_set_calibration()
1409 * with the values, or some modified version of them.
1414 gst_clock_add_observation_unapplied (GstClock * clock, GstClockTime slave,
1415 GstClockTime master, gdouble * r_squared,
1416 GstClockTime * internal, GstClockTime * external,
1417 GstClockTime * rate_num, GstClockTime * rate_denom)
1419 GstClockTime m_num, m_denom, b, xbase;
1420 GstClockPrivate *priv;
1423 g_return_val_if_fail (GST_IS_CLOCK (clock), FALSE);
1424 g_return_val_if_fail (r_squared != NULL, FALSE);
1428 GST_CLOCK_SLAVE_LOCK (clock);
1430 GST_CAT_LOG_OBJECT (GST_CAT_CLOCK, clock,
1431 "adding observation slave %" GST_TIME_FORMAT ", master %" GST_TIME_FORMAT,
1432 GST_TIME_ARGS (slave), GST_TIME_ARGS (master));
1434 priv->times[(4 * priv->time_index)] = slave;
1435 priv->times[(4 * priv->time_index) + 2] = master;
1438 if (G_UNLIKELY (priv->time_index == priv->window_size)) {
1439 priv->filling = FALSE;
1440 priv->time_index = 0;
1443 if (G_UNLIKELY (priv->filling && priv->time_index < priv->window_threshold))
1446 n = priv->filling ? priv->time_index : priv->window_size;
1447 if (!_priv_gst_do_linear_regression (priv->times, n, &m_num, &m_denom, &b,
1451 GST_CLOCK_SLAVE_UNLOCK (clock);
1453 GST_CAT_LOG_OBJECT (GST_CAT_CLOCK, clock,
1454 "adjusting clock to m=%" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT ", b=%"
1455 G_GUINT64_FORMAT " (rsquared=%g)", m_num, m_denom, b, *r_squared);
1464 *rate_denom = m_denom;
1470 GST_CLOCK_SLAVE_UNLOCK (clock);
1475 /* no valid regression has been done, ignore the result then */
1476 GST_CLOCK_SLAVE_UNLOCK (clock);
1482 * gst_clock_set_timeout:
1483 * @clock: a #GstClock
1484 * @timeout: a timeout
1486 * Set the amount of time, in nanoseconds, to sample master and slave
1490 gst_clock_set_timeout (GstClock * clock, GstClockTime timeout)
1492 g_return_if_fail (GST_IS_CLOCK (clock));
1494 GST_CLOCK_SLAVE_LOCK (clock);
1495 clock->priv->timeout = timeout;
1496 GST_CLOCK_SLAVE_UNLOCK (clock);
1500 * gst_clock_get_timeout:
1501 * @clock: a #GstClock
1503 * Get the amount of time that master and slave clocks are sampled.
1505 * Returns: the interval between samples.
1508 gst_clock_get_timeout (GstClock * clock)
1510 GstClockTime result;
1512 g_return_val_if_fail (GST_IS_CLOCK (clock), GST_CLOCK_TIME_NONE);
1514 GST_CLOCK_SLAVE_LOCK (clock);
1515 result = clock->priv->timeout;
1516 GST_CLOCK_SLAVE_UNLOCK (clock);
1522 gst_clock_set_property (GObject * object, guint prop_id,
1523 const GValue * value, GParamSpec * pspec)
1526 GstClockPrivate *priv;
1528 clock = GST_CLOCK (object);
1532 case PROP_WINDOW_SIZE:
1533 GST_CLOCK_SLAVE_LOCK (clock);
1534 priv->window_size = g_value_get_int (value);
1535 priv->window_threshold = MIN (priv->window_threshold, priv->window_size);
1536 priv->times = g_renew (GstClockTime, priv->times, 4 * priv->window_size);
1537 /* restart calibration */
1538 priv->filling = TRUE;
1539 priv->time_index = 0;
1540 GST_CLOCK_SLAVE_UNLOCK (clock);
1542 case PROP_WINDOW_THRESHOLD:
1543 GST_CLOCK_SLAVE_LOCK (clock);
1544 priv->window_threshold = MIN (g_value_get_int (value), priv->window_size);
1545 GST_CLOCK_SLAVE_UNLOCK (clock);
1548 gst_clock_set_timeout (clock, g_value_get_uint64 (value));
1551 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1557 gst_clock_get_property (GObject * object, guint prop_id,
1558 GValue * value, GParamSpec * pspec)
1561 GstClockPrivate *priv;
1563 clock = GST_CLOCK (object);
1567 case PROP_WINDOW_SIZE:
1568 GST_CLOCK_SLAVE_LOCK (clock);
1569 g_value_set_int (value, priv->window_size);
1570 GST_CLOCK_SLAVE_UNLOCK (clock);
1572 case PROP_WINDOW_THRESHOLD:
1573 GST_CLOCK_SLAVE_LOCK (clock);
1574 g_value_set_int (value, priv->window_threshold);
1575 GST_CLOCK_SLAVE_UNLOCK (clock);
1578 g_value_set_uint64 (value, gst_clock_get_timeout (clock));
1581 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1588 * gst_clock_wait_for_sync:
1589 * @clock: a GstClock
1590 * @timeout: timeout for waiting or %GST_CLOCK_TIME_NONE
1592 * Waits until @clock is synced for reporting the current time. If @timeout
1593 * is %GST_CLOCK_TIME_NONE it will wait forever, otherwise it will time out
1594 * after @timeout nanoseconds.
1596 * For asynchronous waiting, the GstClock::synced signal can be used.
1599 * This returns immediately with TRUE if GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC
1600 * is not set on the clock, or if the clock is already synced.
1602 * Returns: %TRUE if waiting was successful, or %FALSE on timeout
1607 gst_clock_wait_for_sync (GstClock * clock, GstClockTime timeout)
1609 gboolean timed_out = FALSE;
1611 g_return_val_if_fail (GST_IS_CLOCK (clock), FALSE);
1613 GST_OBJECT_LOCK (clock);
1614 if (!GST_OBJECT_FLAG_IS_SET (clock, GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC)
1615 || clock->priv->synced) {
1616 GST_OBJECT_UNLOCK (clock);
1620 if (timeout != GST_CLOCK_TIME_NONE) {
1621 gint64 end_time = g_get_monotonic_time () + gst_util_uint64_scale (timeout,
1622 G_TIME_SPAN_SECOND, GST_SECOND);
1624 while (!clock->priv->synced && !timed_out) {
1626 !g_cond_wait_until (&clock->priv->sync_cond,
1627 GST_OBJECT_GET_LOCK (clock), end_time);
1631 while (!clock->priv->synced) {
1632 g_cond_wait (&clock->priv->sync_cond, GST_OBJECT_GET_LOCK (clock));
1635 GST_OBJECT_UNLOCK (clock);
1641 * gst_clock_is_synced:
1642 * @clock: a GstClock
1644 * Checks if the clock is currently synced.
1646 * This returns if GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC is not set on the clock.
1648 * Returns: %TRUE if the clock is currently synced
1653 gst_clock_is_synced (GstClock * clock)
1655 g_return_val_if_fail (GST_IS_CLOCK (clock), TRUE);
1657 return !GST_OBJECT_FLAG_IS_SET (clock, GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC)
1658 || clock->priv->synced;
1662 * gst_clock_set_synced:
1663 * @clock: a GstClock
1664 * @synced: if the clock is synced
1666 * Sets @clock to synced and emits the GstClock::synced signal, and wakes up any
1667 * thread waiting in gst_clock_wait_for_sync().
1669 * This function must only be called if GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC
1670 * is set on the clock, and is intended to be called by subclasses only.
1675 gst_clock_set_synced (GstClock * clock, gboolean synced)
1677 g_return_if_fail (GST_IS_CLOCK (clock));
1678 g_return_if_fail (GST_OBJECT_FLAG_IS_SET (clock,
1679 GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC));
1681 GST_OBJECT_LOCK (clock);
1682 if (clock->priv->synced != ! !synced) {
1683 clock->priv->synced = ! !synced;
1684 g_cond_signal (&clock->priv->sync_cond);
1685 GST_OBJECT_UNLOCK (clock);
1686 g_signal_emit (clock, gst_clock_signals[SIGNAL_SYNCED], 0, ! !synced);
1688 GST_OBJECT_UNLOCK (clock);