gstclock.c 17.3 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 346 347 348
      4,
      (GInstanceInitFunc) gst_clock_init,
      NULL
    };
349 350
    clock_type = g_type_register_static (GST_TYPE_OBJECT, "GstClock",
	&clock_info, G_TYPE_FLAG_ABSTRACT);
351 352 353
  }
  return clock_type;
}
354

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

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

364
  parent_class = g_type_class_ref (GST_TYPE_OBJECT);
365

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

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

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

377
  gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_clock_dispose);
378 379 380 381
  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,
382 383
      g_param_spec_boolean ("stats", "Stats", "Enable clock stats",
	  FALSE, G_PARAM_READWRITE));
384
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MAX_DIFF,
385 386 387
      g_param_spec_int64 ("max-diff", "Max diff",
	  "The maximum amount of time to wait in nanoseconds", 0, G_MAXINT64,
	  DEFAULT_MAX_DIFF, G_PARAM_READWRITE));
388
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_EVENT_DIFF,
389 390 391 392
      g_param_spec_uint64 ("event-diff", "event diff",
	  "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
393 394
}

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

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

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

Wim Taymans's avatar
Wim Taymans committed
410
static void
411
gst_clock_dispose (GObject * object)
Wim Taymans's avatar
Wim Taymans committed
412 413 414 415 416 417 418 419 420
{
  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
421 422
/**
 * gst_clock_set_speed
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
423 424
 * @clock: a #GstClock to modify
 * @speed: the speed to set on the clock
Wim Taymans's avatar
Wim Taymans committed
425
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
426
 * Sets the speed on the given clock. 1.0 is the default 
Wim Taymans's avatar
Wim Taymans committed
427
 * speed.
428 429
 *
 * Returns: the new speed of the clock.
Wim Taymans's avatar
Wim Taymans committed
430
 */
431
gdouble
432
gst_clock_set_speed (GstClock * clock, gdouble speed)
Wim Taymans's avatar
Wim Taymans committed
433
{
434 435
  g_return_val_if_fail (GST_IS_CLOCK (clock), 0.0);

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

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

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

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

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

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

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

480 481
  return clock->resolution;
}
482

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

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

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

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

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

Wim Taymans's avatar
Wim Taymans committed
506
/**
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
507 508 509
 * 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
510
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
511
 * Activates or deactivates the clock based on the active parameter.
Wim Taymans's avatar
Wim Taymans committed
512 513
 * As soon as the clock is activated, the time will start ticking.
 */
514
void
515
gst_clock_set_active (GstClock * clock, gboolean active)
Wim Taymans's avatar
Wim Taymans committed
516
{
517
  g_return_if_fail (GST_IS_CLOCK (clock));
518

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

521
  return;
522 523
}

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

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

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

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

  g_return_if_fail (GST_IS_CLOCK (clock));

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

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

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

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

Wim Taymans's avatar
Wim Taymans committed
572 573 574 575 576 577
/**
 * gst_clock_handle_discont
 * @clock: a #GstClock to notify of the discontinuity
 * @time: The new time
 *
 * Notifies the clock of a discontinuity in time.
578
 *
Wim Taymans's avatar
Wim Taymans committed
579 580 581 582
 * 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.
 */
583
gboolean
584
gst_clock_handle_discont (GstClock * clock, guint64 time)
585
{
586
  GST_ERROR_OBJECT (clock, "called deprecated function.");
587

588
  return FALSE;
589 590
}

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

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

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

610 611 612 613 614 615
  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;
616
  } else {
617 618
    clock->last_time = ret;
  }
619

620
  return ret;
621 622
}

623 624 625 626 627 628 629 630 631 632 633 634
/**
 * 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
635
gst_clock_get_event_time (GstClock * clock)
636 637
{
  GstClockTime time;
638

639
  g_return_val_if_fail (GST_IS_CLOCK (clock), GST_CLOCK_TIME_NONE);
640

641 642 643
  time = gst_clock_get_time (clock);

  if (clock->last_event + clock->max_event_diff >= time) {
644
    GST_LOG_OBJECT (clock, "reporting last event time %" G_GUINT64_FORMAT,
645 646
	clock->last_event);
  } else {
647
    GST_LOG_OBJECT (clock, "reporting new event time %" G_GUINT64_FORMAT,
648 649 650
	clock->last_event);
    clock->last_event = time;
  }
651

652 653 654
  return clock->last_event;
}

Wim Taymans's avatar
Wim Taymans committed
655 656 657 658 659 660 661 662
/**
 * 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.
 */
663
GstClockID
664
gst_clock_get_next_id (GstClock * clock)
665 666 667
{
  GstClockEntry *entry = NULL;

668 669
  g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);

670 671 672 673 674 675 676 677 678
  GST_LOCK (clock);
  if (clock->entries)
    entry = GST_CLOCK_ENTRY (clock->entries->data);
  GST_UNLOCK (clock);

  return (GstClockID *) entry;
}

static void
679
gst_clock_update_stats (GstClock * clock)
680
{
681 682
}

683
static void
684 685
gst_clock_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
686
{
687
  GstClock *clock;
688

689 690 691 692 693
  clock = GST_CLOCK (object);

  switch (prop_id) {
    case ARG_STATS:
      clock->stats = g_value_get_boolean (value);
694
      g_object_notify (object, "stats");
695
      break;
696 697
    case ARG_MAX_DIFF:
      clock->max_diff = g_value_get_int64 (value);
698 699 700 701
      g_object_notify (object, "max-diff");
      break;
    case ARG_EVENT_DIFF:
      clock->max_event_diff = g_value_get_uint64 (value);
702
      g_object_notify (object, "event-diff");
703
      break;
704 705 706
    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
  clock = GST_CLOCK (object);

  switch (prop_id) {
    case ARG_STATS:
      g_value_set_boolean (value, clock->stats);
      break;
722 723 724
    case ARG_MAX_DIFF:
      g_value_set_int64 (value, clock->max_diff);
      break;
725 726 727
    case ARG_EVENT_DIFF:
      g_value_set_uint64 (value, clock->max_event_diff);
      break;
728 729 730
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
731
  }
732
}