gstclock.c 18.5 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 27 28
/**
 * SECTION:gstclock
 * @short_description: Abstract class for global clocks
 * @see_also: #GstSystemClock
 *
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 34 35 36 37 38 39 40 41
 * The #GstClock returns a monotonically increasing time with the method
 * gst_clock_get_time(). Its accuracy and base time depends on the specific clock
 * implementation but time is always expessed in nanoseconds. Since the
 * baseline of the clock is undefined, the clock time returned is not
 * meaningfull in itself, what matters are the deltas between two clock
 * times.
 *
 * The pipeline uses the clock to calculate the stream time.
 * Usually all renderers synchronize to the global clock using the buffer timestamps,
42
 * the newsegment events and the element's base time.
Wim Taymans's avatar
Wim Taymans committed
43
 *
Stefan Kost's avatar
Stefan Kost committed
44
 * The time of the clock in itself is not very useful for an application.
Wim Taymans's avatar
Wim Taymans committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
 *
 * A clock implementation can support periodic and single shot clock notifications 
 * both synchronous and asynchronous.
 *
 * One first needs to create a #GstClockID for the periodic or single shot
 * notification using gst_clock_new_single_shot_id() or gst_clock_new_periodic_id().
 *
 * 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
 * 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.
 *
 * 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.
 *
 * A #GstClockID that has been unscheduled cannot be used again for any wait
 * operation.
 *
 * It is possible to perform a blocking wait on the same #GstClockID from multiple
 * threads. However, registering the same #GstClockID for multiple async notifications is
 * not possible, the callback will only be called once.
 *
 * 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 state changes and if we unref it automatically, the handle might be
 * invalid.
 *
 * 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.
 *
 * Last reviewed on 2005-10-28 (0.9.4)
81
 */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
82

83
#include <time.h>
84 85

#include "gst_private.h"
86

87
#include "gstclock.h"
88
#include "gstinfo.h"
89
#include "gstutils.h"
Wim Taymans's avatar
Wim Taymans committed
90

Wim Taymans's avatar
Wim Taymans committed
91
#ifndef GST_DISABLE_TRACE
Wim Taymans's avatar
Wim Taymans committed
92
/* #define GST_WITH_ALLOC_TRACE */
Wim Taymans's avatar
Wim Taymans committed
93 94 95
#include "gsttrace.h"
static GstAllocTrace *_gst_clock_entry_trace;
#endif
Wim Taymans's avatar
Wim Taymans committed
96

97
#define DEFAULT_EVENT_DIFF	(GST_SECOND)
98 99
#define DEFAULT_MAX_DIFF	(2 * GST_SECOND)

100 101
enum
{
102 103
  ARG_0,
  ARG_STATS,
104 105
  ARG_MAX_DIFF,
  ARG_EVENT_DIFF
106 107
};

108 109
static void gst_clock_class_init (GstClockClass * klass);
static void gst_clock_init (GstClock * clock);
Wim Taymans's avatar
Wim Taymans committed
110
static void gst_clock_finalize (GObject * object);
Wim Taymans's avatar
Wim Taymans committed
111

112 113 114 115 116
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);
117

118

119
static GstObjectClass *parent_class = NULL;
120

121 122
/* static guint gst_clock_signals[LAST_SIGNAL] = { 0 }; */

Wim Taymans's avatar
Wim Taymans committed
123
static GstClockID
124 125
gst_clock_entry_new (GstClock * clock, GstClockTime time,
    GstClockTime interval, GstClockEntryType type)
126 127
{
  GstClockEntry *entry;
128

129
  entry = g_malloc0 (sizeof (GstClockEntry));
Wim Taymans's avatar
Wim Taymans committed
130
#ifndef GST_DISABLE_TRACE
Wim Taymans's avatar
Wim Taymans committed
131
  gst_alloc_trace_new (_gst_clock_entry_trace, entry);
Wim Taymans's avatar
Wim Taymans committed
132
#endif
133 134
  GST_CAT_DEBUG (GST_CAT_CLOCK, "created entry %p, time %" GST_TIME_FORMAT,
      entry, GST_TIME_ARGS (time));
135

136
  gst_atomic_int_set (&entry->refcount, 1);
137 138
  entry->clock = clock;
  entry->time = time;
139
  entry->interval = interval;
140
  entry->type = type;
141
  entry->status = GST_CLOCK_BUSY;
142

143 144
  return (GstClockID) entry;
}
145

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
/**
 * gst_clock_id_ref:
 * @id: The clockid to ref
 *
 * Increase the refcount of the given clockid.
 *
 * Returns: The same #GstClockID with increased refcount.
 *
 * MT safe.
 */
GstClockID
gst_clock_id_ref (GstClockID id)
{
  g_return_val_if_fail (id != NULL, NULL);

161
  g_atomic_int_inc (&((GstClockEntry *) id)->refcount);
162 163 164 165 166 167 168 169 170 171 172 173 174 175

  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
176
  g_free (id);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
}

/**
 * gst_clock_id_unref:
 * @id: The clockid to unref
 *
 * Unref the given clockid. When the refcount reaches 0 the
 * #GstClockID will be freed.
 *
 * MT safe.
 */
void
gst_clock_id_unref (GstClockID id)
{
  gint zero;

  g_return_if_fail (id != NULL);

195
  zero = g_atomic_int_dec_and_test (&((GstClockEntry *) id)->refcount);
196 197 198 199 200 201
  /* if we ended up with the refcount at zero, free the id */
  if (zero) {
    _gst_clock_id_free (id);
  }
}

202 203 204 205 206
/**
 * gst_clock_new_single_shot_id
 * @clock: The clockid to get a single shot notification from
 * @time: the requested time
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
207
 * Get an ID from the given clock to trigger a single shot
208 209
 * notification at the requested time. The single shot id should be
 * unreffed after usage.
210 211
 *
 * Returns: An id that can be used to request the time notification.
212 213
 *
 * MT safe.
214 215
 */
GstClockID
216
gst_clock_new_single_shot_id (GstClock * clock, GstClockTime time)
217
{
218 219
  g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);

220 221
  return gst_clock_entry_new (clock,
      time, GST_CLOCK_TIME_NONE, GST_CLOCK_ENTRY_SINGLE);
222 223 224
}

/**
Wim Taymans's avatar
Wim Taymans committed
225
 * gst_clock_new_periodic_id
226 227 228 229 230 231
 * @clock: The clockid to get a periodic notification id from
 * @start_time: the requested start time
 * @interval: the requested interval
 *
 * Get an ID from the given clock to trigger a periodic notification.
 * The periodeic notifications will be start at time start_time and
232 233
 * will then be fired with the given interval. The id should be unreffed
 * after usage.
234 235
 *
 * Returns: An id that can be used to request the time notification.
236 237
 *
 * MT safe.
238 239
 */
GstClockID
240 241
gst_clock_new_periodic_id (GstClock * clock, GstClockTime start_time,
    GstClockTime interval)
242
{
243
  g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);
244
  g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (start_time), NULL);
245 246
  g_return_val_if_fail (interval != 0, NULL);

247 248
  return gst_clock_entry_new (clock,
      start_time, interval, GST_CLOCK_ENTRY_PERIODIC);
249 250
}

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
/**
 * gst_clock_id_compare_func
 * @id1: A clockid
 * @id2: A clockid to compare with
 *
 * Compares the two GstClockID instances. This function can be used
 * 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;
  }

  return entry1 - entry2;
}

281 282 283 284 285 286
/**
 * gst_clock_id_get_time
 * @id: The clockid to query
 *
 * Get the time of the clock ID
 *
287 288 289
 * Returns: the time of the given clock id.
 *
 * MT safe.
290 291 292 293 294 295
 */
GstClockTime
gst_clock_id_get_time (GstClockID id)
{
  g_return_val_if_fail (id != NULL, GST_CLOCK_TIME_NONE);

296
  return GST_CLOCK_ENTRY_TIME ((GstClockEntry *) id);
297 298 299 300 301 302 303 304 305
}


/**
 * gst_clock_id_wait
 * @id: The clockid to wait on
 * @jitter: A pointer that will contain the jitter
 *
 * Perform a blocking wait on the given ID. The jitter arg can be
306
 * NULL.
307 308
 *
 * Returns: the result of the blocking wait.
309 310
 *
 * MT safe.
311 312
 */
GstClockReturn
313
gst_clock_id_wait (GstClockID id, GstClockTimeDiff * jitter)
314 315
{
  GstClockEntry *entry;
316
  GstClock *clock;
317
  GstClockReturn res;
318
  GstClockTime requested;
Wim Taymans's avatar
Wim Taymans committed
319
  GstClockClass *cclass;
320

321
  g_return_val_if_fail (id != NULL, GST_CLOCK_ERROR);
322

323 324
  entry = (GstClockEntry *) id;
  requested = GST_CLOCK_ENTRY_TIME (entry);
325

326 327 328 329 330
  if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (requested)))
    goto invalid_time;

  if (G_UNLIKELY (entry->status == GST_CLOCK_UNSCHEDULED))
    goto unscheduled;
331

332
  clock = GST_CLOCK_ENTRY_CLOCK (entry);
Wim Taymans's avatar
Wim Taymans committed
333
  cclass = GST_CLOCK_GET_CLASS (clock);
334

335
  if (G_LIKELY (cclass->wait)) {
336

337 338 339
    GST_CAT_DEBUG (GST_CAT_CLOCK, "waiting on clock entry %p", id);
    res = cclass->wait (clock, entry);
    GST_CAT_DEBUG (GST_CAT_CLOCK, "done waiting entry %p", id);
340

341
    if (jitter) {
342 343
      GstClockTime now = gst_clock_get_time (clock);

344 345
      *jitter = now - requested;
    }
346 347 348
    if (entry->type == GST_CLOCK_ENTRY_PERIODIC) {
      entry->time += entry->interval;
    }
349 350 351 352

    if (clock->stats) {
      gst_clock_update_stats (clock);
    }
353 354
  } else {
    res = GST_CLOCK_UNSUPPORTED;
355 356
  }
  return res;
357 358 359 360 361 362 363 364 365 366 367 368

  /* ERRORS */
invalid_time:
  {
    GST_CAT_DEBUG (GST_CAT_CLOCK, "invalid time requested, returning _BADTIME");
    return GST_CLOCK_BADTIME;
  }
unscheduled:
  {
    GST_CAT_DEBUG (GST_CAT_CLOCK, "entry was unscheduled return _UNSCHEDULED");
    return GST_CLOCK_UNSCHEDULED;
  }
369 370 371
}

/**
Wim Taymans's avatar
Wim Taymans committed
372 373
 * gst_clock_id_wait_async:
 * @id: a #GstClockID to wait on
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
374
 * @func: The callback function
375 376
 * @user_data: User data passed in the calback
 *
Wim Taymans's avatar
Wim Taymans committed
377
 * Register a callback on the given clockid with the given
378 379 380 381
 * function and user_data. When passing an id with an invalid
 * time to this function, the callback will be called immediatly
 * with  a time set to GST_CLOCK_TIME_NONE. The callback will
 * be called when the time of the id has been reached.
382
 *
383
 * Returns: the result of the non blocking wait.
384 385
 *
 * MT safe.
386 387 388
 */
GstClockReturn
gst_clock_id_wait_async (GstClockID id,
389
    GstClockCallback func, gpointer user_data)
390 391 392
{
  GstClockEntry *entry;
  GstClock *clock;
393
  GstClockReturn res;
Wim Taymans's avatar
Wim Taymans committed
394
  GstClockClass *cclass;
395
  GstClockTime requested;
396

397
  g_return_val_if_fail (id != NULL, GST_CLOCK_ERROR);
398
  g_return_val_if_fail (func != NULL, GST_CLOCK_ERROR);
399 400

  entry = (GstClockEntry *) id;
401 402
  requested = GST_CLOCK_ENTRY_TIME (entry);
  clock = GST_CLOCK_ENTRY_CLOCK (entry);
403

404 405 406 407 408
  if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (requested)))
    goto invalid_time;

  if (G_UNLIKELY (entry->status == GST_CLOCK_UNSCHEDULED))
    goto unscheduled;
409

Wim Taymans's avatar
Wim Taymans committed
410 411 412 413 414 415 416
  cclass = GST_CLOCK_GET_CLASS (clock);

  if (cclass->wait_async) {
    entry->func = func;
    entry->user_data = user_data;

    res = cclass->wait_async (clock, entry);
417 418
  } else {
    res = GST_CLOCK_UNSUPPORTED;
419 420 421
  }
  return res;

422 423 424 425 426 427 428 429 430 431 432 433
  /* ERRORS */
invalid_time:
  {
    (func) (clock, GST_CLOCK_TIME_NONE, id, user_data);
    GST_CAT_DEBUG (GST_CAT_CLOCK, "invalid time requested, returning _BADTIME");
    return GST_CLOCK_BADTIME;
  }
unscheduled:
  {
    GST_CAT_DEBUG (GST_CAT_CLOCK, "entry was unscheduled return _UNSCHEDULED");
    return GST_CLOCK_UNSCHEDULED;
  }
434 435
}

436
/**
Wim Taymans's avatar
Wim Taymans committed
437 438
 * gst_clock_id_unschedule:
 * @id: The id to unschedule
439
 *
440 441 442 443 444 445
 * Cancel an outstanding request with the given ID. This can either
 * be an outstanding async notification or a pending sync notification.
 * After this call, the @id cannot be used anymore to receive sync or
 * async notifications, you need to create a new GstClockID.
 *
 * MT safe.
446 447 448 449 450 451
 */
void
gst_clock_id_unschedule (GstClockID id)
{
  GstClockEntry *entry;
  GstClock *clock;
Wim Taymans's avatar
Wim Taymans committed
452
  GstClockClass *cclass;
453

454 455 456 457
  g_return_if_fail (id != NULL);

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

Wim Taymans's avatar
Wim Taymans committed
459 460 461 462
  cclass = GST_CLOCK_GET_CLASS (clock);

  if (cclass->unschedule)
    cclass->unschedule (clock, entry);
463 464
}

465 466 467 468

/**
 * GstClock abstract base class implementation
 */
469 470
GType
gst_clock_get_type (void)
Wim Taymans's avatar
Wim Taymans committed
471
{
472 473 474 475
  static GType clock_type = 0;

  if (!clock_type) {
    static const GTypeInfo clock_info = {
476
      sizeof (GstClockClass),
477 478 479 480 481
      NULL,
      NULL,
      (GClassInitFunc) gst_clock_class_init,
      NULL,
      NULL,
482
      sizeof (GstClock),
483
      0,
484 485 486
      (GInstanceInitFunc) gst_clock_init,
      NULL
    };
487

488
    clock_type = g_type_register_static (GST_TYPE_OBJECT, "GstClock",
489
        &clock_info, G_TYPE_FLAG_ABSTRACT);
490 491 492
  }
  return clock_type;
}
493

494
static void
495
gst_clock_class_init (GstClockClass * klass)
496 497 498
{
  GObjectClass *gobject_class;
  GstObjectClass *gstobject_class;
499

500 501
  gobject_class = (GObjectClass *) klass;
  gstobject_class = (GstObjectClass *) klass;
502

503
  parent_class = g_type_class_ref (GST_TYPE_OBJECT);
504

Wim Taymans's avatar
Wim Taymans committed
505 506 507
  if (!g_thread_supported ())
    g_thread_init (NULL);

Wim Taymans's avatar
Wim Taymans committed
508
#ifndef GST_DISABLE_TRACE
509 510
  _gst_clock_entry_trace =
      gst_alloc_trace_register (GST_CLOCK_ENTRY_TRACE_NAME);
Wim Taymans's avatar
Wim Taymans committed
511
#endif
512

Wim Taymans's avatar
Wim Taymans committed
513
  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_clock_finalize);
514 515 516 517
  gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_clock_set_property);
  gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_clock_get_property);

  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_STATS,
518
      g_param_spec_boolean ("stats", "Stats", "Enable clock stats",
519
          FALSE, G_PARAM_READWRITE));
Wim Taymans's avatar
Wim Taymans committed
520 521
}

522
static void
523
gst_clock_init (GstClock * clock)
Wim Taymans's avatar
Wim Taymans committed
524
{
525
  clock->adjust = 0;
526 527
  clock->last_time = 0;
  clock->entries = NULL;
528
  clock->entries_changed = g_cond_new ();
529 530
  clock->flags = 0;
  clock->stats = FALSE;
Wim Taymans's avatar
Wim Taymans committed
531 532
}

Wim Taymans's avatar
Wim Taymans committed
533
static void
Wim Taymans's avatar
Wim Taymans committed
534
gst_clock_finalize (GObject * object)
Wim Taymans's avatar
Wim Taymans committed
535 536 537
{
  GstClock *clock = GST_CLOCK (object);

538
  g_cond_free (clock->entries_changed);
Wim Taymans's avatar
Wim Taymans committed
539

Wim Taymans's avatar
Wim Taymans committed
540
  G_OBJECT_CLASS (parent_class)->finalize (object);
Wim Taymans's avatar
Wim Taymans committed
541 542
}

Wim Taymans's avatar
Wim Taymans committed
543
/**
544 545 546
 * gst_clock_set_resolution
 * @clock: The clock set the resolution on
 * @resolution: The resolution to set
Wim Taymans's avatar
Wim Taymans committed
547
 *
548 549 550
 * Set the accuracy of the clock.
 *
 * Returns: the new resolution of the clock.
Wim Taymans's avatar
Wim Taymans committed
551
 */
552
guint64
553
gst_clock_set_resolution (GstClock * clock, guint64 resolution)
Wim Taymans's avatar
Wim Taymans committed
554
{
Wim Taymans's avatar
Wim Taymans committed
555 556
  GstClockClass *cclass;

Wim Taymans's avatar
Wim Taymans committed
557 558
  g_return_val_if_fail (GST_IS_CLOCK (clock), G_GINT64_CONSTANT (0));
  g_return_val_if_fail (resolution != 0, G_GINT64_CONSTANT (0));
559

Wim Taymans's avatar
Wim Taymans committed
560 561 562
  cclass = GST_CLOCK_GET_CLASS (clock);

  if (cclass->change_resolution)
563
    clock->resolution =
564
        cclass->change_resolution (clock, clock->resolution, resolution);
565

566 567
  return clock->resolution;
}
568

569 570 571 572 573 574 575
/**
 * gst_clock_get_resolution
 * @clock: The clock get the resolution of
 *
 * Get the accuracy of the clock.
 *
 * Returns: the resolution of the clock in microseconds.
576 577
 *
 * MT safe.
578 579
 */
guint64
580
gst_clock_get_resolution (GstClock * clock)
581
{
Wim Taymans's avatar
Wim Taymans committed
582 583
  GstClockClass *cclass;

Wim Taymans's avatar
Wim Taymans committed
584
  g_return_val_if_fail (GST_IS_CLOCK (clock), G_GINT64_CONSTANT (0));
585

Wim Taymans's avatar
Wim Taymans committed
586 587 588 589
  cclass = GST_CLOCK_GET_CLASS (clock);

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

Wim Taymans's avatar
Wim Taymans committed
591
  return G_GINT64_CONSTANT (1);
Wim Taymans's avatar
Wim Taymans committed
592 593
}

Wim Taymans's avatar
Wim Taymans committed
594
/**
595 596 597
 * gst_clock_adjust_unlocked
 * @clock: a #GstClock to use
 * @internal: a clock time
Wim Taymans's avatar
Wim Taymans committed
598
 *
599 600
 * Converts the given @internal clock time to the real time, adjusting
 * and making sure that the returned time is increasing.
601 602
 * This function should be called with the clock LOCK held and is
 * mainly used by clock subclasses.
Wim Taymans's avatar
Wim Taymans committed
603
 *
604
 * Returns: the converted time of the clock.
605
 *
606
 * MT safe.
607
 */
608 609
GstClockTime
gst_clock_adjust_unlocked (GstClock * clock, GstClockTime internal)
610
{
611
  GstClockTime ret;
612

613 614 615 616 617 618
  ret = internal + clock->adjust;
  /* make sure the time is increasing, else return last_time */
  if ((gint64) ret < (gint64) clock->last_time) {
    ret = clock->last_time;
  } else {
    clock->last_time = ret;
619
  }
620
  return ret;
621 622
}

Wim Taymans's avatar
Wim Taymans committed
623 624
/**
 * gst_clock_get_time
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
625
 * @clock: a #GstClock to query
Wim Taymans's avatar
Wim Taymans committed
626
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
627
 * Gets the current time of the given clock. The time is always
Wim Taymans's avatar
Wim Taymans committed
628
 * monotonically increasing.
629
 *
630 631 632 633
 * Returns: the time of the clock. Or GST_CLOCK_TIME_NONE when
 * giving wrong input.
 *
 * MT safe.
Wim Taymans's avatar
Wim Taymans committed
634
 */
635
GstClockTime
636
gst_clock_get_time (GstClock * clock)
637
{
638
  GstClockTime ret;
639
  GstClockClass *cclass;
640

641
  g_return_val_if_fail (GST_IS_CLOCK (clock), GST_CLOCK_TIME_NONE);
642

643
  cclass = GST_CLOCK_GET_CLASS (clock);
Wim Taymans's avatar
Wim Taymans committed
644

645
  if (cclass->get_internal_time) {
646
    ret = cclass->get_internal_time (clock);
647
  } else {
648
    ret = G_GINT64_CONSTANT (0);
649
  }
650 651
  GST_CAT_DEBUG (GST_CAT_CLOCK, "internal time %" GST_TIME_FORMAT,
      GST_TIME_ARGS (ret));
652

653 654 655
  GST_LOCK (clock);
  ret = gst_clock_adjust_unlocked (clock, ret);
  GST_UNLOCK (clock);
656

657 658
  GST_CAT_DEBUG (GST_CAT_CLOCK, "adjusted time %" GST_TIME_FORMAT,
      GST_TIME_ARGS (ret));
659

660
  return ret;
661 662
}

Wim Taymans's avatar
Wim Taymans committed
663
/**
664 665 666
 * gst_clock_set_time_adjust
 * @clock: a #GstClock to adjust
 * @adjust: the adjust value
Wim Taymans's avatar
Wim Taymans committed
667
 *
668 669
 * Adjusts the current time of the clock with the adjust value.
 * A positive value moves the clock forwards and a backwards value
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
670
 * moves it backwards. Note that _get_time() always returns
671 672
 * increasing values so when you move the clock backwards, _get_time()
 * will report the previous value until the clock catches up.
Wim Taymans's avatar
Wim Taymans committed
673
 *
674
 * MT safe.
Wim Taymans's avatar
Wim Taymans committed
675
 */
676 677
void
gst_clock_set_time_adjust (GstClock * clock, GstClockTime adjust)
678
{
679
  g_return_if_fail (GST_IS_CLOCK (clock));
680

681
  GST_LOCK (clock);
682
  clock->adjust = adjust;
683 684 685 686
  GST_UNLOCK (clock);
}

static void
687
gst_clock_update_stats (GstClock * clock)
688
{
689 690
}

691
static void
692 693
gst_clock_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
694
{
695
  GstClock *clock;
696

697 698 699 700 701
  clock = GST_CLOCK (object);

  switch (prop_id) {
    case ARG_STATS:
      clock->stats = g_value_get_boolean (value);
702
      g_object_notify (object, "stats");
703 704 705 706
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
707
  }
Wim Taymans's avatar
Wim Taymans committed
708
}
709

710
static void
711 712
gst_clock_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
713
{
714
  GstClock *clock;
715

716 717 718 719 720 721 722 723 724
  clock = GST_CLOCK (object);

  switch (prop_id) {
    case ARG_STATS:
      g_value_set_boolean (value, clock->stats);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
725
  }
726
}