gstclock.c 18.2 KB
Newer Older
1 2 3 4 5
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
 *
 * gstclock.c: Clock subsystem for maintaining time sync
Wim Taymans's avatar
Wim Taymans committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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.
 */

#include <sys/time.h>
24 25

#include "gst_private.h"
26

27
#include "gstclock.h"
28
#include "gstinfo.h"
29
#include "gstmemchunk.h"
Wim Taymans's avatar
Wim Taymans committed
30

Wim Taymans's avatar
Wim Taymans committed
31
#ifndef GST_DISABLE_TRACE
Wim Taymans's avatar
Wim Taymans committed
32
/* #define GST_WITH_ALLOC_TRACE */
Wim Taymans's avatar
Wim Taymans committed
33 34 35
#include "gsttrace.h"
static GstAllocTrace *_gst_clock_entry_trace;
#endif
Wim Taymans's avatar
Wim Taymans committed
36

37
#define DEFAULT_EVENT_DIFF	(GST_SECOND)
38 39
#define DEFAULT_MAX_DIFF	(2 * GST_SECOND)

40 41
enum
{
42 43
  ARG_0,
  ARG_STATS,
44 45
  ARG_MAX_DIFF,
  ARG_EVENT_DIFF
46 47
};

48 49 50
static GstMemChunk *_gst_clock_entries_chunk;

void gst_clock_id_unlock (GstClockID id);
Wim Taymans's avatar
Wim Taymans committed
51

52

53 54 55
static void gst_clock_class_init (GstClockClass * klass);
static void gst_clock_init (GstClock * clock);
static void gst_clock_dispose (GObject * object);
Wim Taymans's avatar
Wim Taymans committed
56

57 58 59 60 61
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);
62

63

64
static GstObjectClass *parent_class = NULL;
65

66 67
/* static guint gst_clock_signals[LAST_SIGNAL] = { 0 }; */

Wim Taymans's avatar
Wim Taymans committed
68
static GstClockID
69 70
gst_clock_entry_new (GstClock * clock, GstClockTime time,
    GstClockTime interval, GstClockEntryType type)
71 72
{
  GstClockEntry *entry;
73

74
  entry = gst_mem_chunk_alloc (_gst_clock_entries_chunk);
Wim Taymans's avatar
Wim Taymans committed
75
#ifndef GST_DISABLE_TRACE
Wim Taymans's avatar
Wim Taymans committed
76
  gst_alloc_trace_new (_gst_clock_entry_trace, entry);
Wim Taymans's avatar
Wim Taymans committed
77
#endif
78

79 80
  entry->clock = clock;
  entry->time = time;
81
  entry->interval = interval;
82 83
  entry->type = type;
  entry->status = GST_CLOCK_ENTRY_OK;
84

85 86
  return (GstClockID) entry;
}
87

88 89 90 91 92 93 94 95 96 97 98
/**
 * gst_clock_new_single_shot_id
 * @clock: The clockid to get a single shot notification from
 * @time: the requested time
 *
 * Get an ID from the given clock to trigger a single shot 
 * notification at the requested time.
 *
 * Returns: An id that can be used to request the time notification.
 */
GstClockID
99
gst_clock_new_single_shot_id (GstClock * clock, GstClockTime time)
100
{
101 102
  g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);

103 104
  return gst_clock_entry_new (clock,
      time, GST_CLOCK_TIME_NONE, GST_CLOCK_ENTRY_SINGLE);
105 106 107
}

/**
Wim Taymans's avatar
Wim Taymans committed
108
 * gst_clock_new_periodic_id
109 110 111 112 113 114 115 116 117 118 119
 * @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
 * will then be fired with the given interval.
 *
 * Returns: An id that can be used to request the time notification.
 */
GstClockID
120 121
gst_clock_new_periodic_id (GstClock * clock, GstClockTime start_time,
    GstClockTime interval)
122
{
123
  g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);
124
  g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (start_time), NULL);
125 126
  g_return_val_if_fail (interval != 0, NULL);

127 128
  return gst_clock_entry_new (clock,
      start_time, interval, GST_CLOCK_ENTRY_PERIODIC);
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
}

/**
 * gst_clock_id_get_time
 * @id: The clockid to query
 *
 * Get the time of the clock ID
 *
 * Returns: the time of the given clock id
 */
GstClockTime
gst_clock_id_get_time (GstClockID id)
{
  g_return_val_if_fail (id != NULL, GST_CLOCK_TIME_NONE);

144
  return GST_CLOCK_ENTRY_TIME ((GstClockEntry *) id);
145 146 147 148 149 150 151 152 153 154 155 156 157 158
}


/**
 * 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
 * NULL
 *
 * Returns: the result of the blocking wait.
 */
GstClockReturn
159
gst_clock_id_wait (GstClockID id, GstClockTimeDiff * jitter)
160 161
{
  GstClockEntry *entry;
162 163 164
  GstClock *clock;
  GstClockReturn res = GST_CLOCK_UNSUPPORTED;
  GstClockTime requested;
Wim Taymans's avatar
Wim Taymans committed
165
  GstClockClass *cclass;
166

167
  g_return_val_if_fail (id != NULL, GST_CLOCK_ERROR);
168

169 170
  entry = (GstClockEntry *) id;
  requested = GST_CLOCK_ENTRY_TIME (entry);
171

172
  if (!GST_CLOCK_TIME_IS_VALID (requested)) {
173
    GST_CAT_DEBUG (GST_CAT_CLOCK, "invalid time requested, returning _TIMEOUT");
174
    return GST_CLOCK_TIMEOUT;
175
  }
176

177
  clock = GST_CLOCK_ENTRY_CLOCK (entry);
Wim Taymans's avatar
Wim Taymans committed
178
  cclass = GST_CLOCK_GET_CLASS (clock);
179

Wim Taymans's avatar
Wim Taymans committed
180
  if (cclass->wait) {
181
    GstClockTime now;
182

183 184 185
    GST_LOCK (clock);
    clock->entries = g_list_prepend (clock->entries, entry);
    GST_UNLOCK (clock);
186

187
    GST_CAT_DEBUG (GST_CAT_CLOCK, "waiting on clock");
188
    do {
Wim Taymans's avatar
Wim Taymans committed
189
      res = cclass->wait (clock, entry);
190 191
    }
    while (res == GST_CLOCK_ENTRY_RESTART);
192
    GST_CAT_DEBUG (GST_CAT_CLOCK, "done waiting");
193

194 195 196 197
    GST_LOCK (clock);
    clock->entries = g_list_remove (clock->entries, entry);
    GST_UNLOCK (clock);

198 199 200 201 202 203 204 205 206 207 208 209 210 211
    if (jitter) {
      now = gst_clock_get_time (clock);
      *jitter = now - requested;
    }

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

  return res;
}

/**
Wim Taymans's avatar
Wim Taymans committed
212 213
 * gst_clock_id_wait_async:
 * @id: a #GstClockID to wait on
214 215 216
 * @func: The callback function 
 * @user_data: User data passed in the calback
 *
Wim Taymans's avatar
Wim Taymans committed
217 218
 * Register a callback on the given clockid with the given
 * function and user_data.
219
 *
220
 * Returns: the result of the non blocking wait.
221 222 223
 */
GstClockReturn
gst_clock_id_wait_async (GstClockID id,
224
    GstClockCallback func, gpointer user_data)
225 226 227 228
{
  GstClockEntry *entry;
  GstClock *clock;
  GstClockReturn res = GST_CLOCK_UNSUPPORTED;
Wim Taymans's avatar
Wim Taymans committed
229
  GstClockClass *cclass;
230

231
  g_return_val_if_fail (id != NULL, GST_CLOCK_ERROR);
232
  g_return_val_if_fail (func != NULL, GST_CLOCK_ERROR);
233 234 235 236

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

237
  if (!GST_CLOCK_TIME_IS_VALID (GST_CLOCK_ENTRY_TIME (entry))) {
238 239 240 241
    (func) (clock, GST_CLOCK_TIME_NONE, id, user_data);
    return GST_CLOCK_TIMEOUT;
  }

Wim Taymans's avatar
Wim Taymans committed
242 243 244 245 246 247 248
  cclass = GST_CLOCK_GET_CLASS (clock);

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

    res = cclass->wait_async (clock, entry);
249 250 251 252 253
  }

  return res;
}

254
static void
255
gst_clock_reschedule_func (GstClockEntry * entry)
256 257
{
  entry->status = GST_CLOCK_ENTRY_OK;
258 259

  gst_clock_id_unlock ((GstClockID) entry);
260 261
}

262
/**
Wim Taymans's avatar
Wim Taymans committed
263 264
 * gst_clock_id_unschedule:
 * @id: The id to unschedule
265 266 267 268 269 270 271 272
 *
 * Cancel an outstanding async notification request with the given ID.
 */
void
gst_clock_id_unschedule (GstClockID id)
{
  GstClockEntry *entry;
  GstClock *clock;
Wim Taymans's avatar
Wim Taymans committed
273
  GstClockClass *cclass;
274

275 276 277 278
  g_return_if_fail (id != NULL);

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

Wim Taymans's avatar
Wim Taymans committed
280 281 282 283
  cclass = GST_CLOCK_GET_CLASS (clock);

  if (cclass->unschedule)
    cclass->unschedule (clock, entry);
284 285
}

286
/**
Wim Taymans's avatar
Wim Taymans committed
287
 * gst_clock_id_free:
288 289 290 291 292 293
 * @id: The clockid to free
 *
 * Free the resources held by the given id
 */
void
gst_clock_id_free (GstClockID id)
294
{
295 296
  g_return_if_fail (id != NULL);

Wim Taymans's avatar
Wim Taymans committed
297
#ifndef GST_DISABLE_TRACE
Wim Taymans's avatar
Wim Taymans committed
298
  gst_alloc_trace_free (_gst_clock_entry_trace, id);
Wim Taymans's avatar
Wim Taymans committed
299
#endif
300 301
  gst_mem_chunk_free (_gst_clock_entries_chunk, id);
}
302

303
/**
Wim Taymans's avatar
Wim Taymans committed
304
 * gst_clock_id_unlock:
305 306 307 308 309 310 311 312 313
 * @id: The clockid to unlock
 *
 * Unlock the givan ClockID.
 */
void
gst_clock_id_unlock (GstClockID id)
{
  GstClockEntry *entry;
  GstClock *clock;
Wim Taymans's avatar
Wim Taymans committed
314
  GstClockClass *cclass;
315

316 317 318 319 320
  g_return_if_fail (id != NULL);

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

Wim Taymans's avatar
Wim Taymans committed
321 322 323 324
  cclass = GST_CLOCK_GET_CLASS (clock);

  if (cclass->unlock)
    cclass->unlock (clock, entry);
325 326
}

327 328 329 330

/**
 * GstClock abstract base class implementation
 */
331 332
GType
gst_clock_get_type (void)
Wim Taymans's avatar
Wim Taymans committed
333
{
334 335 336 337
  static GType clock_type = 0;

  if (!clock_type) {
    static const GTypeInfo clock_info = {
338
      sizeof (GstClockClass),
339 340 341 342 343
      NULL,
      NULL,
      (GClassInitFunc) gst_clock_class_init,
      NULL,
      NULL,
344
      sizeof (GstClock),
345
      0,
346 347 348
      (GInstanceInitFunc) gst_clock_init,
      NULL
    };
349

350
    clock_type = g_type_register_static (GST_TYPE_OBJECT, "GstClock",
351
        &clock_info, G_TYPE_FLAG_ABSTRACT);
352 353 354
  }
  return clock_type;
}
355

356
static void
357
gst_clock_class_init (GstClockClass * klass)
358 359 360
{
  GObjectClass *gobject_class;
  GstObjectClass *gstobject_class;
361

362 363
  gobject_class = (GObjectClass *) klass;
  gstobject_class = (GstObjectClass *) klass;
364

365
  parent_class = g_type_class_ref (GST_TYPE_OBJECT);
366

Wim Taymans's avatar
Wim Taymans committed
367 368 369
  if (!g_thread_supported ())
    g_thread_init (NULL);

370
  _gst_clock_entries_chunk = gst_mem_chunk_new ("GstClockEntries",
371
      sizeof (GstClockEntry), sizeof (GstClockEntry) * 32, G_ALLOC_AND_FREE);
372

Wim Taymans's avatar
Wim Taymans committed
373
#ifndef GST_DISABLE_TRACE
374 375
  _gst_clock_entry_trace =
      gst_alloc_trace_register (GST_CLOCK_ENTRY_TRACE_NAME);
Wim Taymans's avatar
Wim Taymans committed
376
#endif
377

378
  gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_clock_dispose);
379 380 381 382
  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,
383
      g_param_spec_boolean ("stats", "Stats", "Enable clock stats",
384
          FALSE, G_PARAM_READWRITE));
385
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MAX_DIFF,
386
      g_param_spec_int64 ("max-diff", "Max diff",
387 388
          "The maximum amount of time to wait in nanoseconds", 0, G_MAXINT64,
          DEFAULT_MAX_DIFF, G_PARAM_READWRITE));
389
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_EVENT_DIFF,
390
      g_param_spec_uint64 ("event-diff", "event diff",
391 392 393
          "The amount of time that may elapse until 2 events are treated as happening at different times",
          0, G_MAXUINT64, DEFAULT_EVENT_DIFF,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
Wim Taymans's avatar
Wim Taymans committed
394 395
}

396
static void
397
gst_clock_init (GstClock * clock)
Wim Taymans's avatar
Wim Taymans committed
398
{
399 400
  clock->max_diff = DEFAULT_MAX_DIFF;

401
  clock->start_time = 0;
402 403
  clock->last_time = 0;
  clock->entries = NULL;
404 405
  clock->flags = 0;
  clock->stats = FALSE;
406 407 408

  clock->active_mutex = g_mutex_new ();
  clock->active_cond = g_cond_new ();
Wim Taymans's avatar
Wim Taymans committed
409 410
}

Wim Taymans's avatar
Wim Taymans committed
411
static void
412
gst_clock_dispose (GObject * object)
Wim Taymans's avatar
Wim Taymans committed
413 414 415 416 417 418 419 420 421
{
  GstClock *clock = GST_CLOCK (object);

  g_mutex_free (clock->active_mutex);
  g_cond_free (clock->active_cond);

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

Wim Taymans's avatar
Wim Taymans committed
422 423
/**
 * gst_clock_set_speed
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
424 425
 * @clock: a #GstClock to modify
 * @speed: the speed to set on the clock
Wim Taymans's avatar
Wim Taymans committed
426
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
427
 * Sets the speed on the given clock. 1.0 is the default 
Wim Taymans's avatar
Wim Taymans committed
428
 * speed.
429 430
 *
 * Returns: the new speed of the clock.
Wim Taymans's avatar
Wim Taymans committed
431
 */
432
gdouble
433
gst_clock_set_speed (GstClock * clock, gdouble speed)
Wim Taymans's avatar
Wim Taymans committed
434
{
435 436
  g_return_val_if_fail (GST_IS_CLOCK (clock), 0.0);

437
  GST_WARNING_OBJECT (clock, "called deprecated function");
438
  return 1.0;
Wim Taymans's avatar
Wim Taymans committed
439 440 441 442
}

/**
 * gst_clock_get_speed
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
443
 * @clock: a #GstClock to query
Wim Taymans's avatar
Wim Taymans committed
444
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
445
 * Gets the speed of the given clock.
Wim Taymans's avatar
Wim Taymans committed
446 447 448 449
 *
 * Returns: the speed of the clock.
 */
gdouble
450
gst_clock_get_speed (GstClock * clock)
Wim Taymans's avatar
Wim Taymans committed
451 452 453
{
  g_return_val_if_fail (GST_IS_CLOCK (clock), 0.0);

454
  GST_WARNING_OBJECT (clock, "called deprecated function");
455
  return 1.0;
Wim Taymans's avatar
Wim Taymans committed
456 457 458
}

/**
459 460 461
 * gst_clock_set_resolution
 * @clock: The clock set the resolution on
 * @resolution: The resolution to set
Wim Taymans's avatar
Wim Taymans committed
462
 *
463 464 465
 * Set the accuracy of the clock.
 *
 * Returns: the new resolution of the clock.
Wim Taymans's avatar
Wim Taymans committed
466
 */
467
guint64
468
gst_clock_set_resolution (GstClock * clock, guint64 resolution)
Wim Taymans's avatar
Wim Taymans committed
469
{
Wim Taymans's avatar
Wim Taymans committed
470 471
  GstClockClass *cclass;

Wim Taymans's avatar
Wim Taymans committed
472 473
  g_return_val_if_fail (GST_IS_CLOCK (clock), G_GINT64_CONSTANT (0));
  g_return_val_if_fail (resolution != 0, G_GINT64_CONSTANT (0));
474

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

  if (cclass->change_resolution)
478
    clock->resolution =
479
        cclass->change_resolution (clock, clock->resolution, resolution);
480

481 482
  return clock->resolution;
}
483

484 485 486 487 488 489 490 491 492
/**
 * 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.
 */
guint64
493
gst_clock_get_resolution (GstClock * clock)
494
{
Wim Taymans's avatar
Wim Taymans committed
495 496
  GstClockClass *cclass;

Wim Taymans's avatar
Wim Taymans committed
497
  g_return_val_if_fail (GST_IS_CLOCK (clock), G_GINT64_CONSTANT (0));
498

Wim Taymans's avatar
Wim Taymans committed
499 500 501 502
  cclass = GST_CLOCK_GET_CLASS (clock);

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

Wim Taymans's avatar
Wim Taymans committed
504
  return G_GINT64_CONSTANT (1);
Wim Taymans's avatar
Wim Taymans committed
505 506
}

Wim Taymans's avatar
Wim Taymans committed
507
/**
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
508 509 510
 * gst_clock_set_active
 * @clock: a #GstClock to set state of
 * @active: flag indicating if the clock should be activated (TRUE) or deactivated
Wim Taymans's avatar
Wim Taymans committed
511
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
512
 * Activates or deactivates the clock based on the active parameter.
Wim Taymans's avatar
Wim Taymans committed
513 514
 * As soon as the clock is activated, the time will start ticking.
 */
515
void
516
gst_clock_set_active (GstClock * clock, gboolean active)
Wim Taymans's avatar
Wim Taymans committed
517
{
518
  g_return_if_fail (GST_IS_CLOCK (clock));
519

520
  GST_ERROR_OBJECT (clock, "called deprecated function that does nothing now.");
521

522
  return;
523 524
}

Wim Taymans's avatar
Wim Taymans committed
525 526
/**
 * gst_clock_is_active
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
527
 * @clock: a #GstClock to query
Wim Taymans's avatar
Wim Taymans committed
528 529 530 531 532
 *
 * Checks if the given clock is active.
 * 
 * Returns: TRUE if the clock is active.
 */
533
gboolean
534
gst_clock_is_active (GstClock * clock)
535
{
536
  g_return_val_if_fail (GST_IS_CLOCK (clock), FALSE);
537

538 539 540
  GST_WARNING_OBJECT (clock, "called deprecated function.");

  return TRUE;
Wim Taymans's avatar
Wim Taymans committed
541 542
}

543 544 545 546 547 548 549
/**
 * gst_clock_reset
 * @clock: a #GstClock to reset
 *
 * Reset the clock to time 0.
 */
void
550
gst_clock_reset (GstClock * clock)
551
{
Wim Taymans's avatar
Wim Taymans committed
552
  GstClockTime time = G_GINT64_CONSTANT (0);
Wim Taymans's avatar
Wim Taymans committed
553
  GstClockClass *cclass;
554 555 556

  g_return_if_fail (GST_IS_CLOCK (clock));

557 558
  GST_ERROR_OBJECT (clock, "called deprecated function.");

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

Wim Taymans's avatar
Wim Taymans committed
561 562
  if (cclass->get_internal_time) {
    time = cclass->get_internal_time (clock);
563 564 565
  }

  GST_LOCK (clock);
566
  //clock->active = FALSE;
567
  clock->start_time = time;
Wim Taymans's avatar
Wim Taymans committed
568
  clock->last_time = G_GINT64_CONSTANT (0);
569
  g_list_foreach (clock->entries, (GFunc) gst_clock_reschedule_func, NULL);
570 571 572
  GST_UNLOCK (clock);
}

Wim Taymans's avatar
Wim Taymans committed
573 574 575 576 577 578
/**
 * gst_clock_handle_discont
 * @clock: a #GstClock to notify of the discontinuity
 * @time: The new time
 *
 * Notifies the clock of a discontinuity in time.
579
 *
Wim Taymans's avatar
Wim Taymans committed
580 581 582 583
 * Returns: TRUE if the clock was updated. It is possible that
 * the clock was not updated by this call because only the first
 * discontinuitity in the pipeline is honoured.
 */
584
gboolean
585
gst_clock_handle_discont (GstClock * clock, guint64 time)
586
{
587
  GST_ERROR_OBJECT (clock, "called deprecated function.");
588

589
  return FALSE;
590 591
}

Wim Taymans's avatar
Wim Taymans committed
592 593
/**
 * gst_clock_get_time
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
594
 * @clock: a #GstClock to query
Wim Taymans's avatar
Wim Taymans committed
595
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
596
 * Gets the current time of the given clock. The time is always
Wim Taymans's avatar
Wim Taymans committed
597
 * monotonically increasing.
598
 *
Wim Taymans's avatar
Wim Taymans committed
599 600
 * Returns: the time of the clock.
 */
601
GstClockTime
602
gst_clock_get_time (GstClock * clock)
603
{
Wim Taymans's avatar
Wim Taymans committed
604
  GstClockTime ret = G_GINT64_CONSTANT (0);
605
  GstClockClass *cclass;
606

Wim Taymans's avatar
Wim Taymans committed
607
  g_return_val_if_fail (GST_IS_CLOCK (clock), G_GINT64_CONSTANT (0));
608

609
  cclass = GST_CLOCK_GET_CLASS (clock);
Wim Taymans's avatar
Wim Taymans committed
610

611 612 613 614 615 616
  if (cclass->get_internal_time) {
    ret = cclass->get_internal_time (clock) - clock->start_time;
  }
  /* make sure the time is increasing, else return last_time */
  if ((gint64) ret < (gint64) clock->last_time) {
    ret = clock->last_time;
617
  } else {
618 619
    clock->last_time = ret;
  }
620

621
  return ret;
622 623
}

624 625 626 627 628 629 630 631 632 633 634 635
/**
 * gst_clock_get_event_time:
 * @clock: clock to query
 * 
 * Gets the "event time" of a given clock. An event on the clock happens
 * whenever this function is called. This ensures that multiple events that
 * happen shortly after each other are treated as if they happened at the same
 * time. GStreamer uses to keep state changes of multiple elements in sync.
 *
 * Returns: the time of the event
 */
GstClockTime
636
gst_clock_get_event_time (GstClock * clock)
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
{
  return gst_clock_get_event_time_delay (clock, 0);
}

/**
 * gst_clock_get_event_time_delay:
 * @clock: clock to query
 * @delay: time before the event actually occurs
 *
 * Gets the "event time" of a given clock. An event on the clock happens
 * whenever this function is called. This ensures that multiple events that
 * happen shortly after each other are treated as if they happened at the same
 * time. GStreamer uses to keep state changes of multiple elements in sync.
 *
 * When calling this function, the specified delay will be added to the current
 * time to produce the event time. This can be used for events that are
 * scheduled to happen at some point in the future.
 *
 * Returns: the time of the event
 */
GstClockTime
gst_clock_get_event_time_delay (GstClock * clock, GstClockTime delay)
659 660
{
  GstClockTime time;
661

662
  g_return_val_if_fail (GST_IS_CLOCK (clock), GST_CLOCK_TIME_NONE);
663

664 665
  time = gst_clock_get_time (clock);

666 667
  if (ABS (GST_CLOCK_DIFF (clock->last_event, time + delay)) <
      clock->max_event_diff) {
668
    GST_LOG_OBJECT (clock, "reporting last event time %" G_GUINT64_FORMAT,
669
        clock->last_event);
670
  } else {
671
    clock->last_event = time + delay;
672
    GST_LOG_OBJECT (clock, "reporting new event time %" G_GUINT64_FORMAT,
673
        clock->last_event);
674
  }
675

676 677 678
  return clock->last_event;
}

Wim Taymans's avatar
Wim Taymans committed
679 680 681 682 683 684 685 686
/**
 * gst_clock_get_next_id
 * @clock: The clock to query
 *
 * Get the clockid of the next event.
 *
 * Returns: a clockid or NULL is no event is pending.
 */
687
GstClockID
688
gst_clock_get_next_id (GstClock * clock)
689 690 691
{
  GstClockEntry *entry = NULL;

692 693
  g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);

694 695 696 697 698 699 700 701 702
  GST_LOCK (clock);
  if (clock->entries)
    entry = GST_CLOCK_ENTRY (clock->entries->data);
  GST_UNLOCK (clock);

  return (GstClockID *) entry;
}

static void
703
gst_clock_update_stats (GstClock * clock)
704
{
705 706
}

707
static void
708 709
gst_clock_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
710
{
711
  GstClock *clock;
712

713 714 715 716 717
  clock = GST_CLOCK (object);

  switch (prop_id) {
    case ARG_STATS:
      clock->stats = g_value_get_boolean (value);
718
      g_object_notify (object, "stats");
719
      break;
720 721
    case ARG_MAX_DIFF:
      clock->max_diff = g_value_get_int64 (value);
722 723 724 725
      g_object_notify (object, "max-diff");
      break;
    case ARG_EVENT_DIFF:
      clock->max_event_diff = g_value_get_uint64 (value);
726
      g_object_notify (object, "event-diff");
727
      break;
728 729 730
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
731
  }
Wim Taymans's avatar
Wim Taymans committed
732
}
733

734
static void
735 736
gst_clock_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
737
{
738
  GstClock *clock;
739

740 741 742 743 744 745
  clock = GST_CLOCK (object);

  switch (prop_id) {
    case ARG_STATS:
      g_value_set_boolean (value, clock->stats);
      break;
746 747 748
    case ARG_MAX_DIFF:
      g_value_set_int64 (value, clock->max_diff);
      break;
749 750 751
    case ARG_EVENT_DIFF:
      g_value_set_uint64 (value, clock->max_event_diff);
      break;
752 753 754
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
755
  }
756
}