gstclock.c 14.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
#define CLASS(clock)  GST_CLOCK_CLASS (G_OBJECT_GET_CLASS (clock))
33

34
static GstMemChunk *_gst_clock_entries_chunk;
Wim Taymans's avatar
Wim Taymans committed
35

36 37 38
static void		gst_clock_class_init		(GstClockClass *klass);
static void		gst_clock_init			(GstClock *clock);

39

40 41 42
static GstObjectClass *parent_class = NULL;
/* static guint gst_clock_signals[LAST_SIGNAL] = { 0 }; */

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
typedef struct _GstClockEntry GstClockEntry;

static void 		gst_clock_free_entry 		(GstClock *clock, GstClockEntry *entry);

typedef enum {
  GST_ENTRY_OK,
  GST_ENTRY_RESTART,
} GstEntryStatus;

struct _GstClockEntry {
  GstClockTime 		 time;
  GstEntryStatus 	 status;
  GstClockCallback 	 func;
  gpointer		 user_data;
};

#define GST_CLOCK_ENTRY(entry)          ((GstClockEntry *)(entry))
#define GST_CLOCK_ENTRY_TIME(entry)     (((GstClockEntry *)(entry))->time)

static GstClockEntry*
gst_clock_entry_new (GstClockTime time,
		     GstClockCallback func, gpointer user_data)
{
  GstClockEntry *entry;

68
  entry = gst_mem_chunk_alloc (_gst_clock_entries_chunk);
69 70 71 72 73 74 75 76

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

  return entry;
}

77
/*
78 79 80 81 82 83 84 85 86
static gint
clock_compare_func (gconstpointer a,
                    gconstpointer b)
{
  GstClockEntry *entry1 = (GstClockEntry *)a;
  GstClockEntry *entry2 = (GstClockEntry *)b;

  return (entry1->time - entry2->time);
}
87
*/
88

89 90
GType
gst_clock_get_type (void)
Wim Taymans's avatar
Wim Taymans committed
91
{
92 93 94 95
  static GType clock_type = 0;

  if (!clock_type) {
    static const GTypeInfo clock_info = {
96
      sizeof (GstClockClass),
97 98 99 100 101
      NULL,
      NULL,
      (GClassInitFunc) gst_clock_class_init,
      NULL,
      NULL,
102
      sizeof (GstClock),
103 104 105 106 107
      4,
      (GInstanceInitFunc) gst_clock_init,
      NULL
    };
    clock_type = g_type_register_static (GST_TYPE_OBJECT, "GstClock", 
108
		    			 &clock_info,  G_TYPE_FLAG_ABSTRACT);
109 110 111
  }
  return clock_type;
}
112

113 114 115 116 117
static void
gst_clock_class_init (GstClockClass *klass)
{
  GObjectClass *gobject_class;
  GstObjectClass *gstobject_class;
118

119 120
  gobject_class = (GObjectClass*) klass;
  gstobject_class = (GstObjectClass*) klass;
121

122
  parent_class = g_type_class_ref (GST_TYPE_OBJECT);
123

Wim Taymans's avatar
Wim Taymans committed
124 125 126
  if (!g_thread_supported ())
    g_thread_init (NULL);

127
  _gst_clock_entries_chunk = gst_mem_chunk_new ("GstClockEntries",
128 129
                     sizeof (GstClockEntry), sizeof (GstClockEntry) * 32,
                     G_ALLOC_AND_FREE);
Wim Taymans's avatar
Wim Taymans committed
130 131
}

132 133
static void
gst_clock_init (GstClock *clock)
Wim Taymans's avatar
Wim Taymans committed
134
{
135 136 137
  clock->speed = 1.0;
  clock->active = FALSE;
  clock->start_time = 0;
138 139 140
  clock->last_time = 0;
  clock->entries = NULL;
  clock->async_supported = FALSE;
141 142 143

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

Wim Taymans's avatar
Wim Taymans committed
146 147
/**
 * gst_clock_async_supported
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
148
 * @clock: a #GstClock to query
Wim Taymans's avatar
Wim Taymans committed
149
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
150
 * Checks if this clock can support asynchronous notification.
Wim Taymans's avatar
Wim Taymans committed
151 152 153
 *
 * Returns: TRUE if async notification is supported.
 */
154 155 156 157 158 159 160 161
gboolean
gst_clock_async_supported (GstClock *clock)
{
  g_return_val_if_fail (GST_IS_CLOCK (clock), FALSE);

  return clock->async_supported;
}

Wim Taymans's avatar
Wim Taymans committed
162 163
/**
 * gst_clock_set_speed
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
164 165
 * @clock: a #GstClock to modify
 * @speed: the speed to set on the clock
Wim Taymans's avatar
Wim Taymans committed
166
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
167
 * Sets the speed on the given clock. 1.0 is the default 
Wim Taymans's avatar
Wim Taymans committed
168 169 170 171 172 173 174 175 176 177 178 179
 * speed.
 */
void
gst_clock_set_speed (GstClock *clock, gdouble speed)
{
  g_return_if_fail (GST_IS_CLOCK (clock));

  clock->speed = speed;
}

/**
 * gst_clock_get_speed
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
180
 * @clock: a #GstClock to query
Wim Taymans's avatar
Wim Taymans committed
181
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
182
 * Gets the speed of the given clock.
Wim Taymans's avatar
Wim Taymans committed
183 184 185 186 187 188 189 190 191 192 193
 *
 * 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;
}

194

Wim Taymans's avatar
Wim Taymans committed
195 196
/**
 * gst_clock_reset
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
197
 * @clock: a #GstClock to reset
Wim Taymans's avatar
Wim Taymans committed
198 199 200
 *
 * Reset the clock to time 0.
 */
201
void
202
gst_clock_reset (GstClock *clock)
Wim Taymans's avatar
Wim Taymans committed
203
{
204 205
  GstClockTime time = 0LL;

206 207
  g_return_if_fail (GST_IS_CLOCK (clock));

208 209 210
  if (CLASS (clock)->get_internal_time) {
    time = CLASS (clock)->get_internal_time (clock);
  }
211

212 213 214 215 216
  GST_LOCK (clock);
  clock->active = FALSE;
  clock->start_time = time;
  clock->last_time = 0LL;
  GST_UNLOCK (clock);
Wim Taymans's avatar
Wim Taymans committed
217 218
}

Wim Taymans's avatar
Wim Taymans committed
219
/**
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
220 221 222
 * 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
223
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
224
 * Activates or deactivates the clock based on the active parameter.
Wim Taymans's avatar
Wim Taymans committed
225 226
 * As soon as the clock is activated, the time will start ticking.
 */
227
void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
228
gst_clock_set_active (GstClock *clock, gboolean active)
Wim Taymans's avatar
Wim Taymans committed
229
{
230 231
  GstClockTime time = 0LL;

232 233 234
  g_return_if_fail (GST_IS_CLOCK (clock));

  clock->active = active;
235 236 237 238
	        
  if (CLASS (clock)->get_internal_time) {
    time = CLASS (clock)->get_internal_time (clock);
  }
239

240 241
  GST_LOCK (clock);
  if (active) {
242 243
    clock->start_time = time - clock->last_time;
    clock->accept_discont = TRUE;
244 245 246
  }
  else {
    clock->last_time = time - clock->start_time;
247
    clock->accept_discont = FALSE;
248 249
  }
  GST_UNLOCK (clock);
250 251

  g_mutex_lock (clock->active_mutex);	
252
  g_cond_broadcast (clock->active_cond);	
253
  g_mutex_unlock (clock->active_mutex);	
254 255
}

Wim Taymans's avatar
Wim Taymans committed
256 257
/**
 * gst_clock_is_active
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
258
 * @clock: a #GstClock to query
Wim Taymans's avatar
Wim Taymans committed
259 260 261 262 263
 *
 * Checks if the given clock is active.
 * 
 * Returns: TRUE if the clock is active.
 */
264 265
gboolean
gst_clock_is_active (GstClock *clock)
266
{
267
  g_return_val_if_fail (GST_IS_CLOCK (clock), FALSE);
268

269
  return clock->active;
Wim Taymans's avatar
Wim Taymans committed
270 271
}

Wim Taymans's avatar
Wim Taymans committed
272 273 274 275 276 277 278 279 280 281 282
/**
 * gst_clock_handle_discont
 * @clock: a #GstClock to notify of the discontinuity
 * @time: The new time
 *
 * Notifies the clock of a discontinuity in time.
 * 
 * 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.
 */
283 284 285 286 287
gboolean
gst_clock_handle_discont (GstClock *clock, guint64 time)
{
  GstClockTime itime = 0LL;
  
288
  GST_DEBUG (GST_CAT_CLOCK, "clock discont %llu %llu %d", time, clock->start_time, clock->accept_discont);
289 290 291 292 293 294 295 296 297

  GST_LOCK (clock);
  if (clock->accept_discont) {
    if (CLASS (clock)->get_internal_time) {
      itime = CLASS (clock)->get_internal_time (clock);
    }
  }
  else {
    GST_UNLOCK (clock);
298
    GST_DEBUG (GST_CAT_CLOCK, "clock discont refused %llu %llu", time, clock->start_time);
299 300 301 302 303 304 305 306
    return FALSE;
  }

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

307
  GST_DEBUG (GST_CAT_CLOCK, "new time %llu", gst_clock_get_time (clock));
308 309 310 311 312 313 314 315

  g_mutex_lock (clock->active_mutex);	
  g_cond_broadcast (clock->active_cond);	
  g_mutex_unlock (clock->active_mutex);	

  return TRUE;
}

Wim Taymans's avatar
Wim Taymans committed
316 317
/**
 * gst_clock_get_time
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
318
 * @clock: a #GstClock to query
Wim Taymans's avatar
Wim Taymans committed
319
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
320
 * Gets the current time of the given clock. The time is always
Wim Taymans's avatar
Wim Taymans committed
321 322 323 324
 * monotonically increasing.
 * 
 * Returns: the time of the clock.
 */
325 326 327
GstClockTime
gst_clock_get_time (GstClock *clock)
{
328 329
  GstClockTime ret = 0LL;

330 331
  g_return_val_if_fail (GST_IS_CLOCK (clock), 0LL);

332 333 334 335 336 337 338 339 340
  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 */
341
    if ((gint64) ret < (gint64) clock->last_time) {
342 343 344 345 346 347
      ret = clock->last_time;
    }
    else {
      clock->last_time = ret;
    }
  }
348

349
  return ret;
350 351
}

352 353 354
static GstClockID
gst_clock_wait_async_func (GstClock *clock, GstClockTime time,
		           GstClockCallback func, gpointer user_data)
Wim Taymans's avatar
Wim Taymans committed
355
{
356 357
  GstClockEntry *entry = NULL;
  g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);
358 359

  if (!clock->active) {
360
    GST_DEBUG (GST_CAT_CLOCK, "blocking on clock");
361 362 363
    g_mutex_lock (clock->active_mutex);	
    g_cond_wait (clock->active_cond, clock->active_mutex);	
    g_mutex_unlock (clock->active_mutex);	
Wim Taymans's avatar
Wim Taymans committed
364
  }
365

366 367 368 369 370
  entry = gst_clock_entry_new (time, func, user_data);

  return entry;
}

Wim Taymans's avatar
Wim Taymans committed
371 372
/**
 * gst_clock_wait
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
373 374
 * @clock: a #GstClock to wait on
 * @time: The #GstClockTime to wait for
Wim Taymans's avatar
Wim Taymans committed
375
 * @jitter: The jitter 
Wim Taymans's avatar
Wim Taymans committed
376 377
 *
 * Wait and block till the clock reaches the specified time.
Wim Taymans's avatar
Wim Taymans committed
378 379 380
 * The jitter value contains the difference between the requested time and
 * the actual time, negative values indicate that the requested time
 * was allready passed when this call was made.
Wim Taymans's avatar
Wim Taymans committed
381
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
382
 * Returns: the #GstClockReturn result of the operation.
Wim Taymans's avatar
Wim Taymans committed
383
 */
384
GstClockReturn
385
gst_clock_wait (GstClock *clock, GstClockTime time, GstClockTimeDiff *jitter)
386 387 388 389 390 391 392
{
  GstClockID id;
  GstClockReturn res;
  
  g_return_val_if_fail (GST_IS_CLOCK (clock), GST_CLOCK_STOPPED);

  id = gst_clock_wait_async_func (clock, time, NULL, NULL);
393
  res = gst_clock_wait_id (clock, id, jitter);
394 395 396 397

  return res;
}

Wim Taymans's avatar
Wim Taymans committed
398 399
/**
 * gst_clock_wait_async
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
400 401
 * @clock: a #GstClock to wait on
 * @time: The #GstClockTime to wait for
Wim Taymans's avatar
Wim Taymans committed
402 403 404 405 406 407 408 409 410
 * @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.
 *
 * Returns: the clock id or NULL when async notification is not supported.
 */
411 412 413 414 415 416 417 418 419 420 421 422
GstClockID
gst_clock_wait_async (GstClock *clock, GstClockTime time,
		      GstClockCallback func, gpointer user_data)
{
  g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);

  if (clock->async_supported) {
    return gst_clock_wait_async_func (clock, time, func, user_data);
  }
  return NULL;
}

Wim Taymans's avatar
Wim Taymans committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
/**
 * gst_clock_cancel_wait_async
 * @clock: The clock to cancel the request on
 * @id: The id to cancel
 *
 * Cancel an outstanding async notification request with the given ID.
 */
void
gst_clock_cancel_wait_async (GstClock *clock, GstClockID id)
{
  g_warning ("not supported");
}

/**
 * gst_clock_notify_async
 * @clock: The clock to wait on
 * @interval: The interval between notifications
 * @func: The callback function 
 * @user_data: User data passed in the calback
 *
 * Register a callback on the given clock that will be periodically
 * triggered with the specified interval. A ClockID is returned
 * that can be used to cancel the request.
 *
 * Returns: the clock id or NULL when async notification is not supported.
 */
GstClockID
gst_clock_notify_async (GstClock *clock, GstClockTime interval,
		        GstClockCallback func, gpointer user_data) 
{
  g_warning ("not supported");
Benjamin Otte's avatar
Benjamin Otte committed
454
  return NULL;
Wim Taymans's avatar
Wim Taymans committed
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
}

/**
 * gst_clock_remove_notify_async
 * @clock: The clock to cancel the request on
 * @id: The id to cancel
 *
 * Cancel an outstanding async notification request with the given ID.
 */
void
gst_clock_remove_notify_async (GstClock *clock, GstClockID id)
{
  g_warning ("not supported");
}

470 471 472 473 474
static void
gst_clock_unlock_func (GstClock *clock, GstClockTime time, GstClockID id, gpointer user_data)
{
}

Wim Taymans's avatar
Wim Taymans committed
475 476 477 478
/**
 * gst_clock_wait_id
 * @clock: The clock to wait on
 * @id: The clock id to wait on
Wim Taymans's avatar
Wim Taymans committed
479
 * @jitter: The jitter 
Wim Taymans's avatar
Wim Taymans committed
480 481
 *
 * Wait and block on the clockid obtained with gst_clock_wait_async.
Wim Taymans's avatar
Wim Taymans committed
482
 * The jitter value is described in gst_clock_wait().
Wim Taymans's avatar
Wim Taymans committed
483 484 485
 *
 * Returns: result of the operation.
 */
486
GstClockReturn
487
gst_clock_wait_id (GstClock *clock, GstClockID id, GstClockTimeDiff *jitter)
488 489 490
{
  GstClockReturn res = GST_CLOCK_TIMEOUT;
  GstClockEntry *entry = (GstClockEntry *) id;
491
  GstClockTime current, target;
492
  GstClockTimeDiff this_jitter;
493 494 495 496 497 498 499
  
  g_return_val_if_fail (GST_IS_CLOCK (clock), GST_CLOCK_ERROR);
  g_return_val_if_fail (entry, GST_CLOCK_ERROR);

  current = gst_clock_get_time (clock);

  entry->func = gst_clock_unlock_func;
500
  target = GST_CLOCK_ENTRY_TIME (entry) - current;
501

502 503
  GST_DEBUG (GST_CAT_CLOCK, "real_target %llu,  target %llu, now %llu", 
		  target, GST_CLOCK_ENTRY_TIME (entry), current); 
504
  
505 506 507 508 509 510
  if (((gint64)target) > 0) {
    struct timeval tv;

    GST_TIME_TO_TIMEVAL (target, tv);
    select (0, NULL, NULL, NULL, &tv);

511 512 513 514 515
    current = gst_clock_get_time (clock);
    this_jitter = current - GST_CLOCK_ENTRY_TIME (entry);
  }
  else {
    res = GST_CLOCK_EARLY;
516
    this_jitter = target;
517 518
  }

519 520 521
  if (jitter)
    *jitter = this_jitter;

522 523 524 525 526
  gst_clock_free_entry (clock, entry);

  return res;
}

Wim Taymans's avatar
Wim Taymans committed
527 528 529 530 531 532 533 534
/**
 * 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.
 */
535 536 537 538 539 540 541 542 543 544 545 546 547
GstClockID
gst_clock_get_next_id (GstClock *clock)
{
  GstClockEntry *entry = NULL;

  GST_LOCK (clock);
  if (clock->entries)
    entry = GST_CLOCK_ENTRY (clock->entries->data);
  GST_UNLOCK (clock);

  return (GstClockID *) entry;
}

Wim Taymans's avatar
Wim Taymans committed
548 549 550 551 552 553 554 555
/**
 * 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
 */
556 557 558 559 560 561 562 563 564
GstClockTime
gst_clock_id_get_time (GstClockID id)
{
  return GST_CLOCK_ENTRY_TIME (id);
}

static void
gst_clock_free_entry (GstClock *clock, GstClockEntry *entry)
{
565
  gst_mem_chunk_free (_gst_clock_entries_chunk, entry);
566 567
}

Wim Taymans's avatar
Wim Taymans committed
568 569 570 571 572 573 574
/**
 * gst_clock_unlock_id
 * @clock: The clock that own the id
 * @id: The clockid to unlock
 *
 * Unlock the ClockID.
 */
575 576 577 578 579 580 581 582 583
void
gst_clock_unlock_id (GstClock *clock, GstClockID id)
{
  GstClockEntry *entry = (GstClockEntry *) id;

  if (entry->func)
    entry->func (clock, gst_clock_get_time (clock), id, entry->user_data);

  gst_clock_free_entry (clock, entry);
584 585
}

Wim Taymans's avatar
Wim Taymans committed
586 587 588 589 590 591 592
/**
 * gst_clock_set_resolution
 * @clock: The clock set the resolution on
 * @resolution: The resolution to set
 *
 * Set the accuracy of the clock.
 */
593 594 595 596 597 598 599
void
gst_clock_set_resolution (GstClock *clock, guint64 resolution)
{
  g_return_if_fail (GST_IS_CLOCK (clock));

  if (CLASS (clock)->set_resolution)
    CLASS (clock)->set_resolution (clock, resolution);
Wim Taymans's avatar
Wim Taymans committed
600
}
601

Wim Taymans's avatar
Wim Taymans committed
602 603 604 605 606 607 608 609
/**
 * 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.
 */
610 611 612 613 614 615 616 617
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);

618
  return 1LL;
619 620
}