gstclock.c 40.9 KB
Newer Older
1 2 3
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
4
 *                    2004 Wim Taymans <wim@fluendo.com>
5 6
 *
 * gstclock.c: Clock subsystem for maintaining time sync
Wim Taymans's avatar
Wim Taymans committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
23

24 25 26
/**
 * SECTION:gstclock
 * @short_description: Abstract class for global clocks
Wim Taymans's avatar
Wim Taymans committed
27
 * @see_also: #GstSystemClock, #GstPipeline
28
 *
Wim Taymans's avatar
Wim Taymans committed
29
 * GStreamer uses a global clock to synchronize the plugins in a pipeline.
30 31 32
 * Different clock implementations are possible by implementing this abstract
 * base class.
 *
Wim Taymans's avatar
Wim Taymans committed
33
 * The #GstClock returns a monotonically increasing time with the method
Wim Taymans's avatar
Wim Taymans committed
34 35
 * gst_clock_get_time(). Its accuracy and base time depend on the specific
 * clock implementation but time is always expressed in nanoseconds. Since the
Wim Taymans's avatar
Wim Taymans committed
36
 * baseline of the clock is undefined, the clock time returned is not
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
37
 * meaningful in itself, what matters are the deltas between two clock times.
Wim Taymans's avatar
Wim Taymans committed
38
 * The time returned by a clock is called the absolute time.
Wim Taymans's avatar
Wim Taymans committed
39
 *
Wim Taymans's avatar
Wim Taymans committed
40
 * The pipeline uses the clock to calculate the stream time. Usually all
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
41
 * renderers synchronize to the global clock using the buffer timestamps, the
Wim Taymans's avatar
Wim Taymans committed
42
 * newsegment events and the element's base time, see #GstPipeline.
Wim Taymans's avatar
Wim Taymans committed
43
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
44 45
 * A clock implementation can support periodic and single shot clock
 * notifications both synchronous and asynchronous.
Wim Taymans's avatar
Wim Taymans committed
46 47
 *
 * One first needs to create a #GstClockID for the periodic or single shot
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
48 49
 * notification using gst_clock_new_single_shot_id() or
 * gst_clock_new_periodic_id().
Wim Taymans's avatar
Wim Taymans committed
50 51 52
 *
 * To perform a blocking wait for the specific time of the #GstClockID use the
 * gst_clock_id_wait(). To receive a callback when the specific time is reached
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
53 54 55
 * in the clock use gst_clock_id_wait_async(). Both these calls can be
 * interrupted with the gst_clock_id_unschedule() call. If the blocking wait is
 * unscheduled a return value of GST_CLOCK_UNSCHEDULED is returned.
Wim Taymans's avatar
Wim Taymans committed
56
 *
57
 * Periodic callbacks scheduled async will be repeatedly called automatically
Wim Taymans's avatar
Wim Taymans committed
58
 * until it is unscheduled. To schedule a sync periodic callback,
59
 * gst_clock_id_wait() should be called repeatedly.
60
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
61 62
 * The async callbacks can happen from any thread, either provided by the core
 * or from a streaming thread. The application should be prepared for this.
Wim Taymans's avatar
Wim Taymans committed
63 64
 *
 * A #GstClockID that has been unscheduled cannot be used again for any wait
Wim Taymans's avatar
Wim Taymans committed
65
 * operation, a new #GstClockID should be created and the old unscheduled one
66
 * should be destroyed with gst_clock_id_unref().
Wim Taymans's avatar
Wim Taymans committed
67
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
68 69
 * It is possible to perform a blocking wait on the same #GstClockID from
 * multiple threads. However, registering the same #GstClockID for multiple
Wim Taymans's avatar
Wim Taymans committed
70 71
 * async notifications is not possible, the callback will only be called for
 * the thread registering the entry last.
Wim Taymans's avatar
Wim Taymans committed
72
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
73 74 75 76
 * None of the wait operations unref the #GstClockID, the owner is responsible
 * for unreffing the ids itself. This holds for both periodic and single shot
 * notifications. The reason being that the owner of the #GstClockID has to
 * keep a handle to the #GstClockID to unblock the wait on FLUSHING events or
Wim Taymans's avatar
Wim Taymans committed
77 78
 * state changes and if the entry would be unreffed automatically, the handle 
 * might become invalid without any notification.
Wim Taymans's avatar
Wim Taymans committed
79 80 81 82 83 84
 *
 * These clock operations do not operate on the stream time, so the callbacks
 * will also occur when not in PLAYING state as if the clock just keeps on
 * running. Some clocks however do not progress when the element that provided
 * the clock is not PLAYING.
 *
85
 * When a clock has the #GST_CLOCK_FLAG_CAN_SET_MASTER flag set, it can be
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
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
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
88
 * sampling the master clock and the slave clock and recalibrating the slave
Kjartan Maraas's avatar
Kjartan Maraas committed
89
 * clock with gst_clock_set_calibration(). This feature is mostly useful for
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
90 91 92
 * plugins that have an internal clock but must operate with another clock
 * selected by the #GstPipeline.  They can track the offset and rate difference
 * of their internal clock relative to the master clock by using the
Wim Taymans's avatar
Wim Taymans committed
93 94 95 96 97 98 99 100
 * gst_clock_get_calibration() function. 
 *
 * The master/slave synchronisation can be tuned with the "timeout", "window-size"
 * and "window-threshold" properties. The "timeout" property defines the interval
 * to sample the master clock and run the calibration functions. 
 * "window-size" defines the number of samples to use when calibrating and
 * "window-threshold" defines the minimum number of samples before the 
 * calibration is performed.
Wim Taymans's avatar
Wim Taymans committed
101
 *
102
 * Last reviewed on 2006-08-11 (0.10.10)
103
 */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
104

105 106

#include "gst_private.h"
107
#include <time.h>
108

109
#include "gstclock.h"
110
#include "gstinfo.h"
111
#include "gstutils.h"
Wim Taymans's avatar
Wim Taymans committed
112

Wim Taymans's avatar
Wim Taymans committed
113
#ifndef GST_DISABLE_TRACE
Wim Taymans's avatar
Wim Taymans committed
114
/* #define GST_WITH_ALLOC_TRACE */
Wim Taymans's avatar
Wim Taymans committed
115 116 117
#include "gsttrace.h"
static GstAllocTrace *_gst_clock_entry_trace;
#endif
Wim Taymans's avatar
Wim Taymans committed
118

119 120
/* #define DEBUGGING_ENABLED */

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
121 122 123 124
#define DEFAULT_STATS                   FALSE
#define DEFAULT_WINDOW_SIZE             32
#define DEFAULT_WINDOW_THRESHOLD        4
#define DEFAULT_TIMEOUT                 GST_SECOND / 10
125

126 127
enum
{
128 129 130 131 132
  PROP_0,
  PROP_STATS,
  PROP_WINDOW_SIZE,
  PROP_WINDOW_THRESHOLD,
  PROP_TIMEOUT
133 134
};

135 136
static void gst_clock_class_init (GstClockClass * klass);
static void gst_clock_init (GstClock * clock);
137
static void gst_clock_dispose (GObject * object);
Wim Taymans's avatar
Wim Taymans committed
138
static void gst_clock_finalize (GObject * object);
Wim Taymans's avatar
Wim Taymans committed
139

140 141 142 143 144
static void gst_clock_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_clock_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
static void gst_clock_update_stats (GstClock * clock);
145

146

147
static GstObjectClass *parent_class = NULL;
148

149 150
/* static guint gst_clock_signals[LAST_SIGNAL] = { 0 }; */

Wim Taymans's avatar
Wim Taymans committed
151
static GstClockID
152 153
gst_clock_entry_new (GstClock * clock, GstClockTime time,
    GstClockTime interval, GstClockEntryType type)
154 155
{
  GstClockEntry *entry;
156

157
  entry = g_slice_new (GstClockEntry);
Wim Taymans's avatar
Wim Taymans committed
158
#ifndef GST_DISABLE_TRACE
Wim Taymans's avatar
Wim Taymans committed
159
  gst_alloc_trace_new (_gst_clock_entry_trace, entry);
Wim Taymans's avatar
Wim Taymans committed
160
#endif
161 162
  GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
      "created entry %p, time %" GST_TIME_FORMAT, entry, GST_TIME_ARGS (time));
163

164
  entry->refcount = 1;
165
  entry->clock = clock;
166
  entry->type = type;
167
  entry->time = time;
168
  entry->interval = interval;
169
  entry->status = GST_CLOCK_BUSY;
170 171
  entry->func = NULL;
  entry->user_data = NULL;
172

173 174
  return (GstClockID) entry;
}
175

176 177
/**
 * gst_clock_id_ref:
Wim Taymans's avatar
Wim Taymans committed
178
 * @id: The #GstClockID to ref
179
 *
Wim Taymans's avatar
Wim Taymans committed
180
 * Increase the refcount of given @id.
181 182 183 184 185 186 187 188 189 190
 *
 * Returns: The same #GstClockID with increased refcount.
 *
 * MT safe.
 */
GstClockID
gst_clock_id_ref (GstClockID id)
{
  g_return_val_if_fail (id != NULL, NULL);

191
  g_atomic_int_inc (&((GstClockEntry *) id)->refcount);
192 193 194 195 196 197 198 199 200 201 202 203 204 205

  return id;
}

static void
_gst_clock_id_free (GstClockID id)
{
  g_return_if_fail (id != NULL);

  GST_CAT_DEBUG (GST_CAT_CLOCK, "freed entry %p", id);

#ifndef GST_DISABLE_TRACE
  gst_alloc_trace_free (_gst_clock_entry_trace, id);
#endif
206
  g_slice_free (GstClockEntry, id);
207 208 209 210
}

/**
 * gst_clock_id_unref:
Wim Taymans's avatar
Wim Taymans committed
211
 * @id: The #GstClockID to unref
212
 *
Wim Taymans's avatar
Wim Taymans committed
213
 * Unref given @id. When the refcount reaches 0 the
214 215 216 217 218 219 220 221 222 223 224
 * #GstClockID will be freed.
 *
 * MT safe.
 */
void
gst_clock_id_unref (GstClockID id)
{
  gint zero;

  g_return_if_fail (id != NULL);

225
  zero = g_atomic_int_dec_and_test (&((GstClockEntry *) id)->refcount);
226 227 228 229 230 231
  /* if we ended up with the refcount at zero, free the id */
  if (zero) {
    _gst_clock_id_free (id);
  }
}

232 233
/**
 * gst_clock_new_single_shot_id
Wim Taymans's avatar
Wim Taymans committed
234
 * @clock: The #GstClockID to get a single shot notification from
235 236
 * @time: the requested time
 *
Wim Taymans's avatar
Wim Taymans committed
237
 * Get a #GstClockID from @clock to trigger a single shot
238 239
 * notification at the requested time. The single shot id should be
 * unreffed after usage.
240
 *
Wim Taymans's avatar
Wim Taymans committed
241
 * Returns: A #GstClockID that can be used to request the time notification.
242 243
 *
 * MT safe.
244 245
 */
GstClockID
246
gst_clock_new_single_shot_id (GstClock * clock, GstClockTime time)
247
{
248 249
  g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);

250 251
  return gst_clock_entry_new (clock,
      time, GST_CLOCK_TIME_NONE, GST_CLOCK_ENTRY_SINGLE);
252 253 254
}

/**
Wim Taymans's avatar
Wim Taymans committed
255
 * gst_clock_new_periodic_id
Wim Taymans's avatar
Wim Taymans committed
256
 * @clock: The #GstClockID to get a periodic notification id from
257 258 259
 * @start_time: the requested start time
 * @interval: the requested interval
 *
Wim Taymans's avatar
Wim Taymans committed
260
 * Get an ID from @clock to trigger a periodic notification.
261 262
 * The periodic notifications will start at time @start_time and
 * will then be fired with the given @interval. @id should be unreffed
263
 * after usage.
264
 *
Wim Taymans's avatar
Wim Taymans committed
265
 * Returns: A #GstClockID that can be used to request the time notification.
266 267
 *
 * MT safe.
268 269
 */
GstClockID
270 271
gst_clock_new_periodic_id (GstClock * clock, GstClockTime start_time,
    GstClockTime interval)
272
{
273
  g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);
274
  g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (start_time), NULL);
275
  g_return_val_if_fail (interval != 0, NULL);
276
  g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), NULL);
277

278 279
  return gst_clock_entry_new (clock,
      start_time, interval, GST_CLOCK_ENTRY_PERIODIC);
280 281
}

282 283
/**
 * gst_clock_id_compare_func
Wim Taymans's avatar
Wim Taymans committed
284 285
 * @id1: A #GstClockID
 * @id2: A #GstClockID to compare with
286
 *
Wim Taymans's avatar
Wim Taymans committed
287
 * Compares the two #GstClockID instances. This function can be used
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
 * as a GCompareFunc when sorting ids.
 *
 * Returns: negative value if a < b; zero if a = b; positive value if a > b
 *
 * MT safe.
 */
gint
gst_clock_id_compare_func (gconstpointer id1, gconstpointer id2)
{
  GstClockEntry *entry1, *entry2;

  entry1 = (GstClockEntry *) id1;
  entry2 = (GstClockEntry *) id2;

  if (GST_CLOCK_ENTRY_TIME (entry1) > GST_CLOCK_ENTRY_TIME (entry2)) {
    return 1;
  }
  if (GST_CLOCK_ENTRY_TIME (entry1) < GST_CLOCK_ENTRY_TIME (entry2)) {
    return -1;
  }
Wim Taymans's avatar
Wim Taymans committed
308
  return 0;
309 310
}

311 312
/**
 * gst_clock_id_get_time
Wim Taymans's avatar
Wim Taymans committed
313
 * @id: The #GstClockID to query
314 315 316
 *
 * Get the time of the clock ID
 *
317 318 319
 * Returns: the time of the given clock id.
 *
 * MT safe.
320 321 322 323 324 325
 */
GstClockTime
gst_clock_id_get_time (GstClockID id)
{
  g_return_val_if_fail (id != NULL, GST_CLOCK_TIME_NONE);

326
  return GST_CLOCK_ENTRY_TIME ((GstClockEntry *) id);
327 328 329 330
}

/**
 * gst_clock_id_wait
Wim Taymans's avatar
Wim Taymans committed
331
 * @id: The #GstClockID to wait on
332
 * @jitter: A pointer that will contain the jitter, can be %NULL.
Wim Taymans's avatar
Wim Taymans committed
333 334 335 336 337 338
 *
 * Perform a blocking wait on @id. 
 * @id should have been created with gst_clock_new_single_shot_id()
 * or gst_clock_new_periodic_id() and should not have been unscheduled
 * with a call to gst_clock_id_unschedule(). 
 *
339
 * If the @jitter argument is not %NULL and this function returns #GST_CLOCK_OK
Wim Taymans's avatar
Wim Taymans committed
340 341
 * or #GST_CLOCK_EARLY, it will contain the difference
 * against the clock and the time of @id when this method was
342 343
 * called. 
 * Positive values indicate how late @id was relative to the clock
344
 * (in which case this function will return #GST_CLOCK_EARLY). 
345
 * Negative values indicate how much time was spent waiting on the clock 
346
 * before this function returned.
Wim Taymans's avatar
Wim Taymans committed
347 348 349 350 351
 *
 * Returns: the result of the blocking wait. #GST_CLOCK_EARLY will be returned
 * if the current clock time is past the time of @id, #GST_CLOCK_OK if 
 * @id was scheduled in time. #GST_CLOCK_UNSCHEDULED if @id was 
 * unscheduled with gst_clock_id_unschedule().
352 353
 *
 * MT safe.
354 355
 */
GstClockReturn
356
gst_clock_id_wait (GstClockID id, GstClockTimeDiff * jitter)
357 358
{
  GstClockEntry *entry;
359
  GstClock *clock;
360
  GstClockReturn res;
361
  GstClockTime requested;
Wim Taymans's avatar
Wim Taymans committed
362
  GstClockClass *cclass;
363

364
  g_return_val_if_fail (id != NULL, GST_CLOCK_ERROR);
365

366 367
  entry = (GstClockEntry *) id;
  requested = GST_CLOCK_ENTRY_TIME (entry);
368

369 370
  clock = GST_CLOCK_ENTRY_CLOCK (entry);

Wim Taymans's avatar
Wim Taymans committed
371
  /* can't sync on invalid times */
372 373 374
  if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (requested)))
    goto invalid_time;

Wim Taymans's avatar
Wim Taymans committed
375
  /* a previously unscheduled entry cannot be scheduled again */
376 377
  if (G_UNLIKELY (entry->status == GST_CLOCK_UNSCHEDULED))
    goto unscheduled;
378

Wim Taymans's avatar
Wim Taymans committed
379
  cclass = GST_CLOCK_GET_CLASS (clock);
380

Wim Taymans's avatar
Wim Taymans committed
381 382
  GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock, "waiting on clock entry %p", id);

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
  /* if we have a wait_jitter function, use that */
  if (G_LIKELY (cclass->wait_jitter)) {
    res = cclass->wait_jitter (clock, entry, jitter);
  } else {
    /* check if we have a simple _wait function otherwise. The function without
     * the jitter arg is less optimal as we need to do an additional _get_time()
     * which is not atomic with the _wait() and a typical _wait() function does
     * yet another _get_time() anyway. */
    if (G_UNLIKELY (cclass->wait == NULL))
      goto not_supported;

    if (jitter) {
      GstClockTime now = gst_clock_get_time (clock);

      /* jitter is the diff against the clock when this entry is scheduled. Negative
       * values mean that the entry was in time, a positive value means that the
       * entry was too late. */
      *jitter = GST_CLOCK_DIFF (requested, now);
    }
    res = cclass->wait (clock, entry);
Wim Taymans's avatar
Wim Taymans committed
403 404 405 406 407
  }

  GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
      "done waiting entry %p, res: %d", id, res);

408 409
  if (entry->type == GST_CLOCK_ENTRY_PERIODIC)
    entry->time = requested + entry->interval;
Wim Taymans's avatar
Wim Taymans committed
410 411 412 413

  if (clock->stats)
    gst_clock_update_stats (clock);

414
  return res;
415 416 417 418

  /* ERRORS */
invalid_time:
  {
419 420
    GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
        "invalid time requested, returning _BADTIME");
421 422 423 424
    return GST_CLOCK_BADTIME;
  }
unscheduled:
  {
425 426
    GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
        "entry was unscheduled return _UNSCHEDULED");
427 428
    return GST_CLOCK_UNSCHEDULED;
  }
Wim Taymans's avatar
Wim Taymans committed
429 430 431 432 433
not_supported:
  {
    GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock, "clock wait is not supported");
    return GST_CLOCK_UNSUPPORTED;
  }
434 435 436
}

/**
Wim Taymans's avatar
Wim Taymans committed
437 438
 * gst_clock_id_wait_async:
 * @id: a #GstClockID to wait on
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
439
 * @func: The callback function
440
 * @user_data: User data passed in the callback
441
 *
Wim Taymans's avatar
Wim Taymans committed
442 443
 * Register a callback on the given #GstClockID @id with the given
 * function and user_data. When passing a #GstClockID with an invalid
444
 * time to this function, the callback will be called immediately
445
 * with  a time set to GST_CLOCK_TIME_NONE. The callback will
Wim Taymans's avatar
Wim Taymans committed
446
 * be called when the time of @id has been reached.
447
 *
448
 * Returns: the result of the non blocking wait.
449 450
 *
 * MT safe.
451 452 453
 */
GstClockReturn
gst_clock_id_wait_async (GstClockID id,
454
    GstClockCallback func, gpointer user_data)
455 456 457
{
  GstClockEntry *entry;
  GstClock *clock;
458
  GstClockReturn res;
Wim Taymans's avatar
Wim Taymans committed
459
  GstClockClass *cclass;
460
  GstClockTime requested;
461

462
  g_return_val_if_fail (id != NULL, GST_CLOCK_ERROR);
463
  g_return_val_if_fail (func != NULL, GST_CLOCK_ERROR);
464 465

  entry = (GstClockEntry *) id;
466 467
  requested = GST_CLOCK_ENTRY_TIME (entry);
  clock = GST_CLOCK_ENTRY_CLOCK (entry);
468

Wim Taymans's avatar
Wim Taymans committed
469
  /* can't sync on invalid times */
470 471 472
  if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (requested)))
    goto invalid_time;

Wim Taymans's avatar
Wim Taymans committed
473
  /* a previously unscheduled entry cannot be scheduled again */
474 475
  if (G_UNLIKELY (entry->status == GST_CLOCK_UNSCHEDULED))
    goto unscheduled;
476

Wim Taymans's avatar
Wim Taymans committed
477 478
  cclass = GST_CLOCK_GET_CLASS (clock);

Wim Taymans's avatar
Wim Taymans committed
479 480 481 482 483 484 485
  if (G_UNLIKELY (cclass->wait_async == NULL))
    goto not_supported;

  entry->func = func;
  entry->user_data = user_data;

  res = cclass->wait_async (clock, entry);
Wim Taymans's avatar
Wim Taymans committed
486

487 488
  return res;

489 490 491 492
  /* ERRORS */
invalid_time:
  {
    (func) (clock, GST_CLOCK_TIME_NONE, id, user_data);
493 494
    GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
        "invalid time requested, returning _BADTIME");
495 496 497 498
    return GST_CLOCK_BADTIME;
  }
unscheduled:
  {
499 500
    GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
        "entry was unscheduled return _UNSCHEDULED");
501 502
    return GST_CLOCK_UNSCHEDULED;
  }
Wim Taymans's avatar
Wim Taymans committed
503 504 505 506 507
not_supported:
  {
    GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock, "clock wait is not supported");
    return GST_CLOCK_UNSUPPORTED;
  }
508 509
}

510
/**
Wim Taymans's avatar
Wim Taymans committed
511 512
 * gst_clock_id_unschedule:
 * @id: The id to unschedule
513
 *
Wim Taymans's avatar
Wim Taymans committed
514
 * Cancel an outstanding request with @id. This can either
515
 * be an outstanding async notification or a pending sync notification.
Wim Taymans's avatar
Wim Taymans committed
516 517
 * After this call, @id cannot be used anymore to receive sync or
 * async notifications, you need to create a new #GstClockID.
518 519
 *
 * MT safe.
520 521 522 523 524 525
 */
void
gst_clock_id_unschedule (GstClockID id)
{
  GstClockEntry *entry;
  GstClock *clock;
Wim Taymans's avatar
Wim Taymans committed
526
  GstClockClass *cclass;
527

528 529 530 531
  g_return_if_fail (id != NULL);

  entry = (GstClockEntry *) id;
  clock = entry->clock;
532

Wim Taymans's avatar
Wim Taymans committed
533 534
  cclass = GST_CLOCK_GET_CLASS (clock);

Wim Taymans's avatar
Wim Taymans committed
535
  if (G_LIKELY (cclass->unschedule))
Wim Taymans's avatar
Wim Taymans committed
536
    cclass->unschedule (clock, entry);
537 538
}

539 540 541 542

/**
 * GstClock abstract base class implementation
 */
543 544
GType
gst_clock_get_type (void)
Wim Taymans's avatar
Wim Taymans committed
545
{
546 547
  static GType clock_type = 0;

548
  if (G_UNLIKELY (clock_type == 0)) {
549
    static const GTypeInfo clock_info = {
550
      sizeof (GstClockClass),
551 552 553 554 555
      NULL,
      NULL,
      (GClassInitFunc) gst_clock_class_init,
      NULL,
      NULL,
556
      sizeof (GstClock),
557
      0,
558 559 560
      (GInstanceInitFunc) gst_clock_init,
      NULL
    };
561

562
    clock_type = g_type_register_static (GST_TYPE_OBJECT, "GstClock",
563
        &clock_info, G_TYPE_FLAG_ABSTRACT);
564 565 566
  }
  return clock_type;
}
567

568
static void
569
gst_clock_class_init (GstClockClass * klass)
570 571 572
{
  GObjectClass *gobject_class;
  GstObjectClass *gstobject_class;
573

574 575
  gobject_class = G_OBJECT_CLASS (klass);
  gstobject_class = GST_OBJECT_CLASS (klass);
576

577
  parent_class = g_type_class_peek_parent (klass);
578

Wim Taymans's avatar
Wim Taymans committed
579 580 581
  if (!g_thread_supported ())
    g_thread_init (NULL);

Wim Taymans's avatar
Wim Taymans committed
582
#ifndef GST_DISABLE_TRACE
583 584
  _gst_clock_entry_trace =
      gst_alloc_trace_register (GST_CLOCK_ENTRY_TRACE_NAME);
Wim Taymans's avatar
Wim Taymans committed
585
#endif
586

587
  gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_clock_dispose);
Wim Taymans's avatar
Wim Taymans committed
588
  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_clock_finalize);
589 590 591
  gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_clock_set_property);
  gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_clock_get_property);

592
  g_object_class_install_property (gobject_class, PROP_STATS,
593 594
      g_param_spec_boolean ("stats", "Stats",
          "Enable clock stats (unimplemented)", DEFAULT_STATS,
595
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
596
  g_object_class_install_property (gobject_class, PROP_WINDOW_SIZE,
597 598
      g_param_spec_int ("window-size", "Window size",
          "The size of the window used to calculate rate and offset", 2, 1024,
599
          DEFAULT_WINDOW_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
600 601
  g_object_class_install_property (gobject_class, PROP_WINDOW_THRESHOLD,
      g_param_spec_int ("window-threshold", "Window threshold",
602
          "The threshold to start calculating rate and offset", 2, 1024,
603 604
          DEFAULT_WINDOW_THRESHOLD,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
605
  g_object_class_install_property (gobject_class, PROP_TIMEOUT,
606 607
      g_param_spec_uint64 ("timeout", "Timeout",
          "The amount of time, in nanoseconds, to sample master and slave clocks",
608 609
          0, G_MAXUINT64, DEFAULT_TIMEOUT,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
610 611
}

612
static void
613
gst_clock_init (GstClock * clock)
Wim Taymans's avatar
Wim Taymans committed
614
{
615 616
  clock->last_time = 0;
  clock->entries = NULL;
617
  clock->entries_changed = g_cond_new ();
618
  clock->stats = FALSE;
619

620 621
  clock->internal_calibration = 0;
  clock->external_calibration = 0;
622 623 624 625
  clock->rate_numerator = 1;
  clock->rate_denominator = 1;

  clock->slave_lock = g_mutex_new ();
626 627
  clock->window_size = DEFAULT_WINDOW_SIZE;
  clock->window_threshold = DEFAULT_WINDOW_THRESHOLD;
Wim Taymans's avatar
Wim Taymans committed
628
  clock->filling = TRUE;
629 630 631
  clock->time_index = 0;
  clock->timeout = DEFAULT_TIMEOUT;
  clock->times = g_new0 (GstClockTime, 4 * clock->window_size);
Wim Taymans's avatar
Wim Taymans committed
632 633
}

634 635 636 637
static void
gst_clock_dispose (GObject * object)
{
  GstClock *clock = GST_CLOCK (object);
638
  GstClock **master_p;
639 640

  GST_OBJECT_LOCK (clock);
641 642
  master_p = &clock->master;
  gst_object_replace ((GstObject **) master_p, NULL);
643 644 645 646 647
  GST_OBJECT_UNLOCK (clock);

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

Wim Taymans's avatar
Wim Taymans committed
648
static void
Wim Taymans's avatar
Wim Taymans committed
649
gst_clock_finalize (GObject * object)
Wim Taymans's avatar
Wim Taymans committed
650 651 652
{
  GstClock *clock = GST_CLOCK (object);

653
  GST_CLOCK_SLAVE_LOCK (clock);
654 655 656 657 658 659 660
  if (clock->clockid) {
    gst_clock_id_unschedule (clock->clockid);
    gst_clock_id_unref (clock->clockid);
    clock->clockid = NULL;
  }
  g_free (clock->times);
  clock->times = NULL;
661
  GST_CLOCK_SLAVE_UNLOCK (clock);
662

663
  g_cond_free (clock->entries_changed);
664 665
  g_mutex_free (clock->slave_lock);

Wim Taymans's avatar
Wim Taymans committed
666
  G_OBJECT_CLASS (parent_class)->finalize (object);
Wim Taymans's avatar
Wim Taymans committed
667 668
}

Wim Taymans's avatar
Wim Taymans committed
669
/**
670
 * gst_clock_set_resolution
671
 * @clock: a #GstClock
672
 * @resolution: The resolution to set
Wim Taymans's avatar
Wim Taymans committed
673
 *
674 675 676 677 678
 * Set the accuracy of the clock. Some clocks have the possibility to operate
 * with different accuracy at the expense of more resource usage. There is
 * normally no need to change the default resolution of a clock. The resolution
 * of a clock can only be changed if the clock has the
 * #GST_CLOCK_FLAG_CAN_SET_RESOLUTION flag set.
679 680
 *
 * Returns: the new resolution of the clock.
Wim Taymans's avatar
Wim Taymans committed
681
 */
682 683
GstClockTime
gst_clock_set_resolution (GstClock * clock, GstClockTime resolution)
Wim Taymans's avatar
Wim Taymans committed
684
{
Wim Taymans's avatar
Wim Taymans committed
685 686
  GstClockClass *cclass;

687 688
  g_return_val_if_fail (GST_IS_CLOCK (clock), 0);
  g_return_val_if_fail (resolution != 0, 0);
689

Wim Taymans's avatar
Wim Taymans committed
690 691 692
  cclass = GST_CLOCK_GET_CLASS (clock);

  if (cclass->change_resolution)
693
    clock->resolution =
694
        cclass->change_resolution (clock, clock->resolution, resolution);
695

696 697
  return clock->resolution;
}
698

699 700
/**
 * gst_clock_get_resolution
701
 * @clock: a #GstClock
702
 *
703 704
 * Get the accuracy of the clock. The accuracy of the clock is the granularity
 * of the values returned by gst_clock_get_time().
705
 *
706
 * Returns: the resolution of the clock in units of #GstClockTime.
707 708
 *
 * MT safe.
709
 */
710
GstClockTime
711
gst_clock_get_resolution (GstClock * clock)
712
{
Wim Taymans's avatar
Wim Taymans committed
713 714
  GstClockClass *cclass;

715
  g_return_val_if_fail (GST_IS_CLOCK (clock), 0);
716

Wim Taymans's avatar
Wim Taymans committed
717 718 719 720
  cclass = GST_CLOCK_GET_CLASS (clock);

  if (cclass->get_resolution)
    return cclass->get_resolution (clock);
721

722
  return 1;
Wim Taymans's avatar
Wim Taymans committed
723 724
}

Wim Taymans's avatar
Wim Taymans committed
725
/**
726 727 728
 * gst_clock_adjust_unlocked
 * @clock: a #GstClock to use
 * @internal: a clock time
Wim Taymans's avatar
Wim Taymans committed
729
 *
730
 * Converts the given @internal clock time to the external time, adjusting for the
731 732
 * rate and reference time set with gst_clock_set_calibration() and making sure
 * that the returned time is increasing. This function should be called with the
733
 * clock's OBJECT_LOCK held and is mainly used by clock subclasses.
Wim Taymans's avatar
Wim Taymans committed
734
 *
735
 * This function is the reverse of gst_clock_unadjust_unlocked().
736
 *
737
 * Returns: the converted time of the clock.
738
 */
739 740
GstClockTime
gst_clock_adjust_unlocked (GstClock * clock, GstClockTime internal)
741
{
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
  GstClockTime ret, cinternal, cexternal, cnum, cdenom;

  /* get calibration values for readability */
  cinternal = clock->internal_calibration;
  cexternal = clock->external_calibration;
  cnum = clock->rate_numerator;
  cdenom = clock->rate_denominator;

  /* avoid divide by 0 */
  if (cdenom == 0)
    cnum = cdenom = 1;

  /* The formula is (internal - cinternal) * cnum / cdenom + cexternal
   *
   * Since we do math on unsigned 64-bit ints we have to special case for
757
   * internal < cinternal to get the sign right. this case is not very common,
758 759 760 761 762 763 764 765 766 767 768 769 770
   * though.
   */
  if (G_LIKELY (internal >= cinternal)) {
    ret = gst_util_uint64_scale (internal - cinternal, cnum, cdenom);
    ret += cexternal;
  } else {
    ret = gst_util_uint64_scale (cinternal - internal, cnum, cdenom);
    /* clamp to 0 */
    if (cexternal > ret)
      ret = cexternal - ret;
    else
      ret = 0;
  }
771 772 773 774 775

  /* make sure the time is increasing */
  clock->last_time = MAX (ret, clock->last_time);

  return clock->last_time;
776 777
}

778 779 780 781 782 783 784 785 786 787
/**
 * gst_clock_unadjust_unlocked
 * @clock: a #GstClock to use
 * @external: an external clock time
 *
 * Converts the given @external clock time to the internal time of @clock,
 * using the rate and reference time set with gst_clock_set_calibration().
 * This function should be called with the clock's OBJECT_LOCK held and
 * is mainly used by clock subclasses.
 *
788
 * This function is the reverse of gst_clock_adjust_unlocked().
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
 *
 * Returns: the internal time of the clock corresponding to @external.
 *
 * Since: 0.10.13
 */
GstClockTime
gst_clock_unadjust_unlocked (GstClock * clock, GstClockTime external)
{
  GstClockTime ret, cinternal, cexternal, cnum, cdenom;

  /* get calibration values for readability */
  cinternal = clock->internal_calibration;
  cexternal = clock->external_calibration;
  cnum = clock->rate_numerator;
  cdenom = clock->rate_denominator;

  /* avoid divide by 0 */
  if (cnum == 0)
    cnum = cdenom = 1;

  /* The formula is (external - cexternal) * cdenom / cnum + cinternal */
  if (external >= cexternal) {
    ret = gst_util_uint64_scale (external - cexternal, cdenom, cnum);
    ret += cinternal;
  } else {
    ret = gst_util_uint64_scale (cexternal - external, cdenom, cnum);
    if (cinternal > ret)
      ret = cinternal - ret;
    else
      ret = 0;
  }
  return ret;
}

Wim Taymans's avatar
Wim Taymans committed
823
/**
824
 * gst_clock_get_internal_time
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
825
 * @clock: a #GstClock to query
Wim Taymans's avatar
Wim Taymans committed
826
 *
827 828
 * Gets the current internal time of the given clock. The time is returned
 * unadjusted for the offset and the rate.
829
 *
830
 * Returns: the internal time of the clock. Or GST_CLOCK_TIME_NONE when
831
 * given invalid input.
832 833
 *
 * MT safe.
Wim Taymans's avatar
Wim Taymans committed
834
 */
835
GstClockTime
836
gst_clock_get_internal_time (GstClock * clock)
837
{
838
  GstClockTime ret;
839
  GstClockClass *cclass;
840

841
  g_return_val_if_fail (GST_IS_CLOCK (clock), GST_CLOCK_TIME_NONE);
842

843
  cclass = GST_CLOCK_GET_CLASS (clock);
Wim Taymans's avatar
Wim Taymans committed
844

Wim Taymans's avatar
Wim Taymans committed
845 846 847 848 849
  if (G_UNLIKELY (cclass->get_internal_time == NULL))
    goto not_supported;

  ret = cclass->get_internal_time (clock);

850
  GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock, "internal time %" GST_TIME_FORMAT,
851
      GST_TIME_ARGS (ret));
852

853
  return ret;
Wim Taymans's avatar
Wim Taymans committed
854 855 856 857 858 859 860 861

  /* ERRORS */
not_supported:
  {
    GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
        "internal time not supported, return 0");
    return G_GINT64_CONSTANT (0);
  }
862 863 864 865 866 867 868 869 870 871 872
}

/**
 * gst_clock_get_time
 * @clock: a #GstClock to query
 *
 * Gets the current time of the given clock. The time is always
 * monotonically increasing and adjusted according to the current
 * offset and rate.
 *
 * Returns: the time of the clock. Or GST_CLOCK_TIME_NONE when
873
 * given invalid input.
874 875 876 877 878 879 880 881 882 883 884 885
 *
 * MT safe.
 */
GstClockTime
gst_clock_get_time (GstClock * clock)
{
  GstClockTime ret;

  g_return_val_if_fail (GST_IS_CLOCK (clock), GST_CLOCK_TIME_NONE);

  ret = gst_clock_get_internal_time (clock);

886
  GST_OBJECT_LOCK (clock);
887
  /* this will scale for rate and offset */
888
  ret = gst_clock_adjust_unlocked (clock, ret);
889
  GST_OBJECT_UNLOCK (clock);
890

891
  GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock, "adjusted time %" GST_TIME_FORMAT,
892
      GST_TIME_ARGS (ret));
893

Wim Taymans's avatar