gstclock.c 15.4 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
/* #define GST_DEBUG_ENABLED */
26 27
#include "gst_private.h"

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

32 33 34 35 36
enum {
  ARG_0,
  ARG_STATS,
};

37
#define CLASS(clock)  GST_CLOCK_CLASS (G_OBJECT_GET_CLASS (clock))
38

39
static GstMemChunk *_gst_clock_entries_chunk;
Wim Taymans's avatar
Wim Taymans committed
40

41 42
static void		gst_clock_class_init		(GstClockClass *klass);
static void		gst_clock_init			(GstClock *clock);
43 44 45 46 47
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);
48

49

50 51 52
static GstObjectClass *parent_class = NULL;
/* static guint gst_clock_signals[LAST_SIGNAL] = { 0 }; */

53 54
static GMutex *_gst_clock_mutex;
static GCond  *_gst_clock_cond;
55

56 57 58 59 60
static inline GstClockID
gst_clock_entry_new (GstClock *clock, GstClockTime time, 
		     GstClockTime interval, GstClockEntryType type)
{
  GstClockEntry *entry;
61

62
  entry = gst_mem_chunk_alloc (_gst_clock_entries_chunk);
63

64 65 66 67 68
  entry->clock = clock;
  entry->time = time;
  entry->interval = time;
  entry->type = type;
  entry->status = GST_CLOCK_ENTRY_OK;
69

70 71
  return (GstClockID) entry;
}
72

73 74 75 76 77 78 79 80 81 82 83 84 85
/**
 * 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
gst_clock_new_single_shot_id (GstClock *clock, GstClockTime time)
{
86 87
  g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  return gst_clock_entry_new (clock, 
		  	      time, 
			      GST_CLOCK_TIME_NONE, 
			      GST_CLOCK_ENTRY_SINGLE);
}

/**
 * gst_clock_new_periodic__id
 * @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
gst_clock_new_periodic_id (GstClock *clock, GstClockTime start_time,
                           GstClockTime interval)
{
110 111 112 113
  g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);
  g_return_val_if_fail (start_time != GST_CLOCK_TIME_NONE, NULL);
  g_return_val_if_fail (interval != 0, NULL);

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  return gst_clock_entry_new (clock, 
		  	      start_time, 
			      interval, 
			      GST_CLOCK_ENTRY_PERIODIC);
}

/**
 * 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);

  return GST_CLOCK_ENTRY_TIME ((GstClockEntry *)id);
}


/**
 * 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
gst_clock_id_wait (GstClockID id, GstClockTimeDiff *jitter)
149 150
{
  GstClockEntry *entry;
151 152 153 154 155
  GstClock *clock;
  GstClockReturn res = GST_CLOCK_UNSUPPORTED;
  GstClockTime requested;
  
  g_return_val_if_fail (id != NULL, GST_CLOCK_ERROR);
156

157 158
  entry = (GstClockEntry *) id;
  requested = GST_CLOCK_ENTRY_TIME (entry);
159 160 161 162 163 164 165

  if (requested == GST_CLOCK_TIME_NONE) {
    res = GST_CLOCK_TIMEOUT;
    goto done;
  }
  
  clock = GST_CLOCK_ENTRY_CLOCK (entry);
166 167 168
  
  if (CLASS (clock)->wait) {
    GstClockTime now;
169

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    do {
      res = CLASS (clock)->wait (clock, entry);
    }
    while (res == GST_CLOCK_ENTRY_RESTART);

    if (jitter) {
      now = gst_clock_get_time (clock);
      *jitter = now - requested;
    }

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

185
done:
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  if (entry->type == GST_CLOCK_ENTRY_SINGLE) {
    gst_clock_id_free (id);
  }

  return res;
}

/**
 * gst_clock_wait_async
 * @clock: a #GstClock to wait on
 * @time: The #GstClockTime to wait for
 * @func: The callback function 
 * @user_data: User data passed in the calback
 *
 * Register a callback on the given clock that will be triggered 
 * when the clock has reached the given time. A ClockID is returned
 * that can be used to cancel the request.
 *
204
 * Returns: the result of the non blocking wait.
205 206 207 208 209 210 211 212 213 214
 */
GstClockReturn
gst_clock_id_wait_async (GstClockID id,
		         GstClockCallback func, gpointer user_data)
{
  GstClockEntry *entry;
  GstClock *clock;
  GstClockReturn res = GST_CLOCK_UNSUPPORTED;
  
  g_return_val_if_fail (id != NULL, GST_CLOCK_ERROR);
215
  g_return_val_if_fail (func != NULL, GST_CLOCK_ERROR);
216 217 218 219

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

220 221 222 223 224
  if (GST_CLOCK_ENTRY_TIME (entry) == GST_CLOCK_TIME_NONE) {
    (func) (clock, GST_CLOCK_TIME_NONE, id, user_data);
    return GST_CLOCK_TIMEOUT;
  }

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
  if (CLASS (clock)->wait_async) {
    res = CLASS (clock)->wait_async (clock, entry, func, user_data);
  }

  return res;
}

/**
 * gst_clock_remove_id
 * @clock: The clock to cancel the request on
 * @id: The id to cancel
 *
 * Cancel an outstanding async notification request with the given ID.
 * This can be an ID generated with gst_clock_wait_async() or 
 * gst_clock_notify_async().
 */
void
gst_clock_id_unschedule (GstClockID id)
{
  GstClockEntry *entry;
  GstClock *clock;
  
  g_return_if_fail (id != NULL);

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

252 253
  if (CLASS (clock)->unschedule)
    CLASS (clock)->unschedule (clock, entry);
254 255
}

256 257 258 259 260 261 262 263
/**
 * gst_clock_id_free
 * @id: The clockid to free
 *
 * Free the resources held by the given id
 */
void
gst_clock_id_free (GstClockID id)
264
{
265 266
  g_return_if_fail (id != NULL);

267 268
  gst_mem_chunk_free (_gst_clock_entries_chunk, id);
}
269

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
/**
 * gst_clock_unlock_id
 * @id: The clockid to unlock
 *
 * Unlock the givan ClockID.
 */
void
gst_clock_id_unlock (GstClockID id)
{
  GstClockEntry *entry;
  GstClock *clock;
  
  g_return_if_fail (id != NULL);

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

  if (CLASS (clock)->unlock)
    CLASS (clock)->unlock (clock, entry);
289 290
}

291 292 293 294

/**
 * GstClock abstract base class implementation
 */
295 296
GType
gst_clock_get_type (void)
Wim Taymans's avatar
Wim Taymans committed
297
{
298 299 300 301
  static GType clock_type = 0;

  if (!clock_type) {
    static const GTypeInfo clock_info = {
302
      sizeof (GstClockClass),
303 304 305 306 307
      NULL,
      NULL,
      (GClassInitFunc) gst_clock_class_init,
      NULL,
      NULL,
308
      sizeof (GstClock),
309 310 311 312 313
      4,
      (GInstanceInitFunc) gst_clock_init,
      NULL
    };
    clock_type = g_type_register_static (GST_TYPE_OBJECT, "GstClock", 
314
		    			 &clock_info,  G_TYPE_FLAG_ABSTRACT);
315 316 317
  }
  return clock_type;
}
318

319 320 321 322 323
static void
gst_clock_class_init (GstClockClass *klass)
{
  GObjectClass *gobject_class;
  GstObjectClass *gstobject_class;
324

325 326
  gobject_class = (GObjectClass*) klass;
  gstobject_class = (GstObjectClass*) klass;
327

328
  parent_class = g_type_class_ref (GST_TYPE_OBJECT);
329

Wim Taymans's avatar
Wim Taymans committed
330 331 332
  if (!g_thread_supported ())
    g_thread_init (NULL);

333
  _gst_clock_entries_chunk = gst_mem_chunk_new ("GstClockEntries",
334 335
                     sizeof (GstClockEntry), sizeof (GstClockEntry) * 32,
                     G_ALLOC_AND_FREE);
336 337 338 339 340 341 342 343 344 345

  _gst_clock_mutex = g_mutex_new ();
  _gst_clock_cond  = g_cond_new ();

  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,
    g_param_spec_boolean ("stats", "Stats", "Enable clock stats",
                          FALSE, G_PARAM_READWRITE));
Wim Taymans's avatar
Wim Taymans committed
346 347
}

348 349
static void
gst_clock_init (GstClock *clock)
Wim Taymans's avatar
Wim Taymans committed
350
{
351 352 353
  clock->speed = 1.0;
  clock->active = FALSE;
  clock->start_time = 0;
354 355
  clock->last_time = 0;
  clock->entries = NULL;
356 357
  clock->flags = 0;
  clock->stats = FALSE;
358 359 360

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

Wim Taymans's avatar
Wim Taymans committed
363 364
/**
 * gst_clock_set_speed
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
365 366
 * @clock: a #GstClock to modify
 * @speed: the speed to set on the clock
Wim Taymans's avatar
Wim Taymans committed
367
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
368
 * Sets the speed on the given clock. 1.0 is the default 
Wim Taymans's avatar
Wim Taymans committed
369
 * speed.
370 371
 *
 * Returns: the new speed of the clock.
Wim Taymans's avatar
Wim Taymans committed
372
 */
373
gdouble
Wim Taymans's avatar
Wim Taymans committed
374 375
gst_clock_set_speed (GstClock *clock, gdouble speed)
{
376 377 378 379
  g_return_val_if_fail (GST_IS_CLOCK (clock), 0.0);

  if (CLASS (clock)->change_speed)
    clock->speed = CLASS (clock)->change_speed (clock, clock->speed, speed);
Wim Taymans's avatar
Wim Taymans committed
380

381
  return clock->speed;
Wim Taymans's avatar
Wim Taymans committed
382 383 384 385
}

/**
 * gst_clock_get_speed
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
386
 * @clock: a #GstClock to query
Wim Taymans's avatar
Wim Taymans committed
387
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
388
 * Gets the speed of the given clock.
Wim Taymans's avatar
Wim Taymans committed
389 390 391 392 393 394 395 396 397 398 399 400
 *
 * Returns: the speed of the clock.
 */
gdouble
gst_clock_get_speed (GstClock *clock)
{
  g_return_val_if_fail (GST_IS_CLOCK (clock), 0.0);

  return clock->speed;
}

/**
401 402 403
 * gst_clock_set_resolution
 * @clock: The clock set the resolution on
 * @resolution: The resolution to set
Wim Taymans's avatar
Wim Taymans committed
404
 *
405 406 407
 * Set the accuracy of the clock.
 *
 * Returns: the new resolution of the clock.
Wim Taymans's avatar
Wim Taymans committed
408
 */
409 410
guint64
gst_clock_set_resolution (GstClock *clock, guint64 resolution)
Wim Taymans's avatar
Wim Taymans committed
411
{
412
  g_return_val_if_fail (GST_IS_CLOCK (clock), 0LL);
413
  g_return_val_if_fail (resolution != 0, 0LL);
414

415 416
  if (CLASS (clock)->change_resolution)
    clock->resolution = CLASS (clock)->change_resolution (clock, clock->resolution, resolution);
417

418 419
  return clock->resolution;
}
420

421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
/**
 * 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
gst_clock_get_resolution (GstClock *clock)
{
  g_return_val_if_fail (GST_IS_CLOCK (clock), 0LL);

  if (CLASS (clock)->get_resolution)
    return CLASS (clock)->get_resolution (clock);

  return 1LL;
Wim Taymans's avatar
Wim Taymans committed
438 439
}

Wim Taymans's avatar
Wim Taymans committed
440
/**
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
441 442 443
 * 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
444
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
445
 * Activates or deactivates the clock based on the active parameter.
Wim Taymans's avatar
Wim Taymans committed
446 447
 * As soon as the clock is activated, the time will start ticking.
 */
448
void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
449
gst_clock_set_active (GstClock *clock, gboolean active)
Wim Taymans's avatar
Wim Taymans committed
450
{
451 452
  GstClockTime time = 0LL;

453 454 455
  g_return_if_fail (GST_IS_CLOCK (clock));

  clock->active = active;
456 457 458 459
	        
  if (CLASS (clock)->get_internal_time) {
    time = CLASS (clock)->get_internal_time (clock);
  }
460

461 462
  GST_LOCK (clock);
  if (active) {
463 464
    clock->start_time = time - clock->last_time;
    clock->accept_discont = TRUE;
465 466 467
  }
  else {
    clock->last_time = time - clock->start_time;
468
    clock->accept_discont = FALSE;
469 470
  }
  GST_UNLOCK (clock);
471 472

  g_mutex_lock (clock->active_mutex);	
473
  g_cond_broadcast (clock->active_cond);	
474
  g_mutex_unlock (clock->active_mutex);	
475 476
}

Wim Taymans's avatar
Wim Taymans committed
477 478
/**
 * gst_clock_is_active
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
479
 * @clock: a #GstClock to query
Wim Taymans's avatar
Wim Taymans committed
480 481 482 483 484
 *
 * Checks if the given clock is active.
 * 
 * Returns: TRUE if the clock is active.
 */
485 486
gboolean
gst_clock_is_active (GstClock *clock)
487
{
488
  g_return_val_if_fail (GST_IS_CLOCK (clock), FALSE);
489

490
  return clock->active;
Wim Taymans's avatar
Wim Taymans committed
491 492
}

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
/**
 * gst_clock_reset
 * @clock: a #GstClock to reset
 *
 * Reset the clock to time 0.
 */
void
gst_clock_reset (GstClock *clock)
{
  GstClockTime time = 0LL;

  g_return_if_fail (GST_IS_CLOCK (clock));

  if (CLASS (clock)->get_internal_time) {
    time = CLASS (clock)->get_internal_time (clock);
  }

  GST_LOCK (clock);
  clock->active = FALSE;
  clock->start_time = time;
  clock->last_time = 0LL;
  GST_UNLOCK (clock);
}

Wim Taymans's avatar
Wim Taymans committed
517 518 519 520 521 522
/**
 * gst_clock_handle_discont
 * @clock: a #GstClock to notify of the discontinuity
 * @time: The new time
 *
 * Notifies the clock of a discontinuity in time.
523
 *
Wim Taymans's avatar
Wim Taymans committed
524 525 526 527
 * 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.
 */
528 529 530 531
gboolean
gst_clock_handle_discont (GstClock *clock, guint64 time)
{
  GstClockTime itime = 0LL;
532 533 534 535

  GST_DEBUG (GST_CAT_CLOCK, "clock discont %" G_GUINT64_FORMAT
		            " %" G_GUINT64_FORMAT " %d",
			    time, clock->start_time, clock->accept_discont);
536

537 538 539
  if (time == GST_CLOCK_TIME_NONE)
    return TRUE;

540 541 542 543 544 545 546 547
  GST_LOCK (clock);
  if (clock->accept_discont) {
    if (CLASS (clock)->get_internal_time) {
      itime = CLASS (clock)->get_internal_time (clock);
    }
  }
  else {
    GST_UNLOCK (clock);
548 549 550
    GST_DEBUG (GST_CAT_CLOCK, "clock discont refused %" G_GUINT64_FORMAT
		              " %" G_GUINT64_FORMAT,
			      time, clock->start_time);
551 552 553 554 555 556 557 558
    return FALSE;
  }

  clock->start_time = itime - time;
  clock->last_time = time;
  clock->accept_discont = FALSE;
  GST_UNLOCK (clock);

559 560
  GST_DEBUG (GST_CAT_CLOCK, "new time %" G_GUINT64_FORMAT,
	     gst_clock_get_time (clock));
561

562 563 564
  g_mutex_lock (clock->active_mutex);
  g_cond_broadcast (clock->active_cond);
  g_mutex_unlock (clock->active_mutex);
565 566 567 568

  return TRUE;
}

Wim Taymans's avatar
Wim Taymans committed
569 570
/**
 * gst_clock_get_time
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
571
 * @clock: a #GstClock to query
Wim Taymans's avatar
Wim Taymans committed
572
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
573
 * Gets the current time of the given clock. The time is always
Wim Taymans's avatar
Wim Taymans committed
574
 * monotonically increasing.
575
 *
Wim Taymans's avatar
Wim Taymans committed
576 577
 * Returns: the time of the clock.
 */
578 579 580
GstClockTime
gst_clock_get_time (GstClock *clock)
{
581 582
  GstClockTime ret = 0LL;

583 584
  g_return_val_if_fail (GST_IS_CLOCK (clock), 0LL);

585 586 587 588 589 590 591 592 593
  if (!clock->active) {
    /* clock is not activen return previous time */
    ret = clock->last_time;
  }
  else {
    if (CLASS (clock)->get_internal_time) {
      ret = CLASS (clock)->get_internal_time (clock) - clock->start_time;
    }
    /* make sure the time is increasing, else return last_time */
594
    if ((gint64) ret < (gint64) clock->last_time) {
595 596 597 598 599 600
      ret = clock->last_time;
    }
    else {
      clock->last_time = ret;
    }
  }
601

602
  return ret;
603 604
}

Wim Taymans's avatar
Wim Taymans committed
605 606 607 608 609 610 611 612
/**
 * 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.
 */
613 614 615 616 617
GstClockID
gst_clock_get_next_id (GstClock *clock)
{
  GstClockEntry *entry = NULL;

618 619
  g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);

620 621 622 623 624 625 626 627 628
  GST_LOCK (clock);
  if (clock->entries)
    entry = GST_CLOCK_ENTRY (clock->entries->data);
  GST_UNLOCK (clock);

  return (GstClockID *) entry;
}

static void
629
gst_clock_update_stats (GstClock *clock)
630
{
631 632
}

633 634 635
static void
gst_clock_set_property (GObject *object, guint prop_id,
     		        const GValue *value, GParamSpec *pspec)
636
{
637 638 639 640 641 642 643 644 645 646 647 648
  GstClock *clock;
	     
  clock = GST_CLOCK (object);

  switch (prop_id) {
    case ARG_STATS:
      clock->stats = g_value_get_boolean (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
   }
Wim Taymans's avatar
Wim Taymans committed
649
}
650

651 652 653
static void
gst_clock_get_property (GObject *object, guint prop_id, 
			GValue *value, GParamSpec * pspec)
654
{
655 656 657 658 659 660 661 662 663 664 665 666
  GstClock *clock;
	     
  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;
   }
667
}