gstclock.c 48.6 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
 *
 * 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
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
20 21
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
Wim Taymans's avatar
Wim Taymans committed
22
 */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
23

24 25
/**
 * SECTION:gstclock
26
 * @title: GstClock
27
 * @short_description: Abstract class for global clocks
Wim Taymans's avatar
Wim Taymans committed
28
 * @see_also: #GstSystemClock, #GstPipeline
29
 *
Wim Taymans's avatar
Wim Taymans committed
30
 * GStreamer uses a global clock to synchronize the plugins in a pipeline.
31
 * Different clock implementations are possible by implementing this abstract
32
 * base class or, more conveniently, by subclassing #GstSystemClock.
33
 *
Wim Taymans's avatar
Wim Taymans committed
34
 * The #GstClock returns a monotonically increasing time with the method
Wim Taymans's avatar
Wim Taymans committed
35 36
 * gst_clock_get_time(). Its accuracy and base time depend on the specific
 * clock implementation but time is always expressed in nanoseconds. Since the
Wim Taymans's avatar
Wim Taymans committed
37
 * baseline of the clock is undefined, the clock time returned is not
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
38
 * meaningful in itself, what matters are the deltas between two clock times.
Wim Taymans's avatar
Wim Taymans committed
39
 * The time returned by a clock is called the absolute time.
Wim Taymans's avatar
Wim Taymans committed
40
 *
41
 * The pipeline uses the clock to calculate the running time. Usually all
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
42
 * renderers synchronize to the global clock using the buffer timestamps, the
Wim Taymans's avatar
Wim Taymans committed
43
 * newsegment events and the element's base time, see #GstPipeline.
Wim Taymans's avatar
Wim Taymans committed
44
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
45 46
 * A clock implementation can support periodic and single shot clock
 * notifications both synchronous and asynchronous.
Wim Taymans's avatar
Wim Taymans committed
47 48
 *
 * One first needs to create a #GstClockID for the periodic or single shot
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
49 50
 * notification using gst_clock_new_single_shot_id() or
 * gst_clock_new_periodic_id().
Wim Taymans's avatar
Wim Taymans committed
51 52 53
 *
 * 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
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
54 55
 * 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
Stefan Kost's avatar
Stefan Kost committed
56
 * unscheduled a return value of #GST_CLOCK_UNSCHEDULED is returned.
Wim Taymans's avatar
Wim Taymans committed
57
 *
58
 * Periodic callbacks scheduled async will be repeatedly called automatically
Wim Taymans's avatar
Wim Taymans committed
59
 * until it is unscheduled. To schedule a sync periodic callback,
60
 * gst_clock_id_wait() should be called repeatedly.
61
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
62 63
 * 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.
Wim Taymans's avatar
Wim Taymans committed
64 65
 *
 * A #GstClockID that has been unscheduled cannot be used again for any wait
Wim Taymans's avatar
Wim Taymans committed
66
 * operation, a new #GstClockID should be created and the old unscheduled one
67
 * should be destroyed with gst_clock_id_unref().
Wim Taymans's avatar
Wim Taymans committed
68
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
69 70
 * It is possible to perform a blocking wait on the same #GstClockID from
 * multiple threads. However, registering the same #GstClockID for multiple
Wim Taymans's avatar
Wim Taymans committed
71 72
 * async notifications is not possible, the callback will only be called for
 * the thread registering the entry last.
Wim Taymans's avatar
Wim Taymans committed
73
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
74 75 76 77
 * 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
78
 * state changes and if the entry would be unreffed automatically, the handle
Wim Taymans's avatar
Wim Taymans committed
79
 * might become invalid without any notification.
Wim Taymans's avatar
Wim Taymans committed
80
 *
81
 * These clock operations do not operate on the running time, so the callbacks
Wim Taymans's avatar
Wim Taymans committed
82 83 84 85
 * 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.
 *
86
 * When a clock has the #GST_CLOCK_FLAG_CAN_SET_MASTER flag set, it can be
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
87
 * slaved to another #GstClock with the gst_clock_set_master(). The clock will
88
 * then automatically be synchronized to this master clock by repeatedly
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
89
 * sampling the master clock and the slave clock and recalibrating the slave
Kjartan Maraas's avatar
Kjartan Maraas committed
90
 * clock with gst_clock_set_calibration(). This feature is mostly useful for
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
91 92 93
 * plugins that have an internal clock but must operate with another clock
 * selected by the #GstPipeline.  They can track the offset and rate difference
 * of their internal clock relative to the master clock by using the
94
 * gst_clock_get_calibration() function.
Wim Taymans's avatar
Wim Taymans committed
95
 *
Stefan Kost's avatar
Stefan Kost committed
96 97 98 99 100 101
 * The master/slave synchronisation can be tuned with the #GstClock:timeout,
 * #GstClock:window-size and #GstClock:window-threshold properties.
 * The #GstClock:timeout property defines the interval to sample the master
 * clock and run the calibration functions. #GstClock:window-size defines the
 * number of samples to use when calibrating and #GstClock:window-threshold
 * defines the minimum number of samples before the calibration is performed.
102
 */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
103

104
#include "gst_private.h"
105
#include <time.h>
106

107
#include "gstclock.h"
108
#include "gstinfo.h"
109
#include "gstutils.h"
110
#include "glib-compat-private.h"
Wim Taymans's avatar
Wim Taymans committed
111

112 113
/* #define DEBUGGING_ENABLED */

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
114 115 116
#define DEFAULT_WINDOW_SIZE             32
#define DEFAULT_WINDOW_THRESHOLD        4
#define DEFAULT_TIMEOUT                 GST_SECOND / 10
117

118 119
enum
{
120 121 122 123
  PROP_0,
  PROP_WINDOW_SIZE,
  PROP_WINDOW_THRESHOLD,
  PROP_TIMEOUT
124 125
};

126 127 128 129 130 131
enum
{
  SIGNAL_SYNCED,
  SIGNAL_LAST
};

Wim Taymans's avatar
Wim Taymans committed
132 133 134
#define GST_CLOCK_SLAVE_LOCK(clock)     g_mutex_lock (&GST_CLOCK_CAST (clock)->priv->slave_lock)
#define GST_CLOCK_SLAVE_UNLOCK(clock)   g_mutex_unlock (&GST_CLOCK_CAST (clock)->priv->slave_lock)

135 136
struct _GstClockPrivate
{
Wim Taymans's avatar
Wim Taymans committed
137 138
  GMutex slave_lock;            /* order: SLAVE_LOCK, OBJECT_LOCK */

139 140
  GCond sync_cond;

Wim Taymans's avatar
Wim Taymans committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  /* with LOCK */
  GstClockTime internal_calibration;
  GstClockTime external_calibration;
  GstClockTime rate_numerator;
  GstClockTime rate_denominator;
  GstClockTime last_time;

  /* with LOCK */
  GstClockTime resolution;

  /* for master/slave clocks */
  GstClock *master;

  /* with SLAVE_LOCK */
  gboolean filling;
  gint window_size;
  gint window_threshold;
  gint time_index;
  GstClockTime timeout;
  GstClockTime *times;
161
  GstClockTime *times_temp;
Wim Taymans's avatar
Wim Taymans committed
162 163
  GstClockID clockid;

164 165
  gint pre_count;
  gint post_count;
166 167

  gboolean synced;
168 169 170 171
};

/* seqlocks */
#define read_seqbegin(clock)                                   \
Wim Taymans's avatar
Wim Taymans committed
172
  g_atomic_int_get (&clock->priv->post_count);
173 174 175 176 177

static inline gboolean
read_seqretry (GstClock * clock, gint seq)
{
  /* no retry if the seqnum did not change */
Wim Taymans's avatar
Wim Taymans committed
178
  if (G_LIKELY (seq == g_atomic_int_get (&clock->priv->pre_count)))
179 180 181 182 183 184 185 186 187 188 189
    return FALSE;

  /* wait for the writer to finish and retry */
  GST_OBJECT_LOCK (clock);
  GST_OBJECT_UNLOCK (clock);
  return TRUE;
}

#define write_seqlock(clock)                      \
G_STMT_START {                                    \
  GST_OBJECT_LOCK (clock);                        \
Wim Taymans's avatar
Wim Taymans committed
190
  g_atomic_int_inc (&clock->priv->pre_count);     \
191 192 193 194
} G_STMT_END;

#define write_sequnlock(clock)                    \
G_STMT_START {                                    \
Wim Taymans's avatar
Wim Taymans committed
195
  g_atomic_int_inc (&clock->priv->post_count);    \
196 197 198
  GST_OBJECT_UNLOCK (clock);                      \
} G_STMT_END;

199
#ifndef GST_DISABLE_GST_DEBUG
200 201 202
static const gchar *
gst_clock_return_get_name (GstClockReturn ret)
{
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  switch (ret) {
    case GST_CLOCK_OK:
      return "ok";
    case GST_CLOCK_EARLY:
      return "early";
    case GST_CLOCK_UNSCHEDULED:
      return "unscheduled";
    case GST_CLOCK_BUSY:
      return "busy";
    case GST_CLOCK_BADTIME:
      return "bad-time";
    case GST_CLOCK_ERROR:
      return "error";
    case GST_CLOCK_UNSUPPORTED:
      return "unsupported";
    case GST_CLOCK_DONE:
      return "done";
    default:
      break;
222
  }
223

224 225
  return "unknown";
}
226
#endif /* GST_DISABLE_GST_DEBUG */
227

228
static void gst_clock_dispose (GObject * object);
Wim Taymans's avatar
Wim Taymans committed
229
static void gst_clock_finalize (GObject * object);
Wim Taymans's avatar
Wim Taymans committed
230

231 232 233 234
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);
235

236
static guint gst_clock_signals[SIGNAL_LAST] = { 0 };
237

Wim Taymans's avatar
Wim Taymans committed
238
static GstClockID
239 240
gst_clock_entry_new (GstClock * clock, GstClockTime time,
    GstClockTime interval, GstClockEntryType type)
241 242
{
  GstClockEntry *entry;
243

244
  entry = g_slice_new (GstClockEntry);
245 246 247

  /* FIXME: add tracer hook for struct allocations such as clock entries */

248 249
  GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
      "created entry %p, time %" GST_TIME_FORMAT, entry, GST_TIME_ARGS (time));
250

251
  entry->refcount = 1;
252
  entry->clock = clock;
253
  entry->type = type;
254
  entry->time = time;
255
  entry->interval = interval;
256
  entry->status = GST_CLOCK_OK;
257 258
  entry->func = NULL;
  entry->user_data = NULL;
259
  entry->destroy_data = NULL;
260 261
  entry->unscheduled = FALSE;
  entry->woken_up = FALSE;
262

263 264
  return (GstClockID) entry;
}
265

266
/* WARNING : Does not modify the refcount
267 268 269 270 271 272 273 274 275 276 277 278
 * WARNING : Do not use if a pending clock operation is happening on that entry */
static gboolean
gst_clock_entry_reinit (GstClock * clock, GstClockEntry * entry,
    GstClockTime time, GstClockTime interval, GstClockEntryType type)
{
  g_return_val_if_fail (entry->status != GST_CLOCK_BUSY, FALSE);
  g_return_val_if_fail (entry->clock == clock, FALSE);

  entry->type = type;
  entry->time = time;
  entry->interval = interval;
  entry->status = GST_CLOCK_OK;
279 280
  entry->unscheduled = FALSE;
  entry->woken_up = FALSE;
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

  return TRUE;
}

/**
 * gst_clock_single_shot_id_reinit:
 * @clock: a #GstClock
 * @id: a #GstClockID
 * @time: The requested time.
 *
 * Reinitializes the provided single shot @id to the provided time. Does not
 * modify the reference count.
 *
 * Returns: %TRUE if the GstClockID could be reinitialized to the provided
 * @time, else %FALSE.
 */
gboolean
gst_clock_single_shot_id_reinit (GstClock * clock, GstClockID id,
    GstClockTime time)
{
  return gst_clock_entry_reinit (clock, (GstClockEntry *) id, time,
      GST_CLOCK_TIME_NONE, GST_CLOCK_ENTRY_SINGLE);
}

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
/**
 * gst_clock_periodic_id_reinit:
 * @clock: a #GstClock
 * @id: a #GstClockID
 * @start_time: the requested start time
 * @interval: the requested interval
 *
 * Reinitializes the provided periodic @id to the provided start time and
 * interval. Does not modify the reference count.
 *
 * Returns: %TRUE if the GstClockID could be reinitialized to the provided
 * @time, else %FALSE.
 */
gboolean
gst_clock_periodic_id_reinit (GstClock * clock, GstClockID id,
    GstClockTime start_time, GstClockTime interval)
{
  return gst_clock_entry_reinit (clock, (GstClockEntry *) id, start_time,
      interval, GST_CLOCK_ENTRY_PERIODIC);
}

326 327
/**
 * gst_clock_id_ref:
Wim Taymans's avatar
Wim Taymans committed
328
 * @id: The #GstClockID to ref
329
 *
Wim Taymans's avatar
Wim Taymans committed
330
 * Increase the refcount of given @id.
331
 *
332
 * Returns: (transfer full): The same #GstClockID with increased refcount.
333 334 335 336 337 338 339 340
 *
 * MT safe.
 */
GstClockID
gst_clock_id_ref (GstClockID id)
{
  g_return_val_if_fail (id != NULL, NULL);

341
  g_atomic_int_inc (&((GstClockEntry *) id)->refcount);
342 343 344 345 346 347 348

  return id;
}

static void
_gst_clock_id_free (GstClockID id)
{
349
  GstClockEntry *entry;
350 351 352
  g_return_if_fail (id != NULL);

  GST_CAT_DEBUG (GST_CAT_CLOCK, "freed entry %p", id);
353 354 355
  entry = (GstClockEntry *) id;
  if (entry->destroy_data)
    entry->destroy_data (entry->user_data);
356

357 358
  /* FIXME: add tracer hook for struct allocations such as clock entries */

359
  g_slice_free (GstClockEntry, id);
360 361 362 363
}

/**
 * gst_clock_id_unref:
364
 * @id: (transfer full): The #GstClockID to unref
365
 *
Wim Taymans's avatar
Wim Taymans committed
366
 * Unref given @id. When the refcount reaches 0 the
367 368 369 370 371 372 373 374 375 376 377
 * #GstClockID will be freed.
 *
 * MT safe.
 */
void
gst_clock_id_unref (GstClockID id)
{
  gint zero;

  g_return_if_fail (id != NULL);

378
  zero = g_atomic_int_dec_and_test (&((GstClockEntry *) id)->refcount);
379 380 381 382 383 384
  /* if we ended up with the refcount at zero, free the id */
  if (zero) {
    _gst_clock_id_free (id);
  }
}

385
/**
386
 * gst_clock_new_single_shot_id:
Wim Taymans's avatar
Wim Taymans committed
387
 * @clock: The #GstClockID to get a single shot notification from
388 389
 * @time: the requested time
 *
Wim Taymans's avatar
Wim Taymans committed
390
 * Get a #GstClockID from @clock to trigger a single shot
391 392
 * notification at the requested time. The single shot id should be
 * unreffed after usage.
393
 *
394 395 396 397
 * Free-function: gst_clock_id_unref
 *
 * Returns: (transfer full): a #GstClockID that can be used to request the
 *     time notification.
398 399
 *
 * MT safe.
400 401
 */
GstClockID
402
gst_clock_new_single_shot_id (GstClock * clock, GstClockTime time)
403
{
404 405
  g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);

406 407
  return gst_clock_entry_new (clock,
      time, GST_CLOCK_TIME_NONE, GST_CLOCK_ENTRY_SINGLE);
408 409 410
}

/**
411
 * gst_clock_new_periodic_id:
Wim Taymans's avatar
Wim Taymans committed
412
 * @clock: The #GstClockID to get a periodic notification id from
413 414 415
 * @start_time: the requested start time
 * @interval: the requested interval
 *
Wim Taymans's avatar
Wim Taymans committed
416
 * Get an ID from @clock to trigger a periodic notification.
417 418
 * The periodic notifications will start at time @start_time and
 * will then be fired with the given @interval. @id should be unreffed
419
 * after usage.
420
 *
421 422 423 424
 * Free-function: gst_clock_id_unref
 *
 * Returns: (transfer full): a #GstClockID that can be used to request the
 *     time notification.
425 426
 *
 * MT safe.
427 428
 */
GstClockID
429 430
gst_clock_new_periodic_id (GstClock * clock, GstClockTime start_time,
    GstClockTime interval)
431
{
432
  g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);
433
  g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (start_time), NULL);
434
  g_return_val_if_fail (interval != 0, NULL);
435
  g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), NULL);
436

437 438
  return gst_clock_entry_new (clock,
      start_time, interval, GST_CLOCK_ENTRY_PERIODIC);
439 440
}

441
/**
442
 * gst_clock_id_compare_func:
Wim Taymans's avatar
Wim Taymans committed
443 444
 * @id1: A #GstClockID
 * @id2: A #GstClockID to compare with
445
 *
Wim Taymans's avatar
Wim Taymans committed
446
 * Compares the two #GstClockID instances. This function can be used
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
 * 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;
  }
Wim Taymans's avatar
Wim Taymans committed
467
  return 0;
468 469
}

470
/**
471
 * gst_clock_id_get_time:
Wim Taymans's avatar
Wim Taymans committed
472
 * @id: The #GstClockID to query
473 474 475
 *
 * Get the time of the clock ID
 *
476 477 478
 * Returns: the time of the given clock id.
 *
 * MT safe.
479 480 481 482 483 484
 */
GstClockTime
gst_clock_id_get_time (GstClockID id)
{
  g_return_val_if_fail (id != NULL, GST_CLOCK_TIME_NONE);

485
  return GST_CLOCK_ENTRY_TIME ((GstClockEntry *) id);
486 487 488
}

/**
489
 * gst_clock_id_wait:
Wim Taymans's avatar
Wim Taymans committed
490
 * @id: The #GstClockID to wait on
Wim Taymans's avatar
Wim Taymans committed
491
 * @jitter: (out) (allow-none): a pointer that will contain the jitter,
492
 *     can be %NULL.
Wim Taymans's avatar
Wim Taymans committed
493
 *
494
 * Perform a blocking wait on @id.
Wim Taymans's avatar
Wim Taymans committed
495 496
 * @id should have been created with gst_clock_new_single_shot_id()
 * or gst_clock_new_periodic_id() and should not have been unscheduled
497
 * with a call to gst_clock_id_unschedule().
Wim Taymans's avatar
Wim Taymans committed
498
 *
499
 * If the @jitter argument is not %NULL and this function returns #GST_CLOCK_OK
Wim Taymans's avatar
Wim Taymans committed
500 501
 * or #GST_CLOCK_EARLY, it will contain the difference
 * against the clock and the time of @id when this method was
502
 * called.
503
 * Positive values indicate how late @id was relative to the clock
504 505
 * (in which case this function will return #GST_CLOCK_EARLY).
 * Negative values indicate how much time was spent waiting on the clock
506
 * before this function returned.
Wim Taymans's avatar
Wim Taymans committed
507 508
 *
 * Returns: the result of the blocking wait. #GST_CLOCK_EARLY will be returned
509 510
 * if the current clock time is past the time of @id, #GST_CLOCK_OK if
 * @id was scheduled in time. #GST_CLOCK_UNSCHEDULED if @id was
Wim Taymans's avatar
Wim Taymans committed
511
 * unscheduled with gst_clock_id_unschedule().
512 513
 *
 * MT safe.
514 515
 */
GstClockReturn
516
gst_clock_id_wait (GstClockID id, GstClockTimeDiff * jitter)
517 518
{
  GstClockEntry *entry;
519
  GstClock *clock;
520
  GstClockReturn res;
521
  GstClockTime requested;
Wim Taymans's avatar
Wim Taymans committed
522
  GstClockClass *cclass;
523

524
  g_return_val_if_fail (id != NULL, GST_CLOCK_ERROR);
525

526 527
  entry = (GstClockEntry *) id;
  requested = GST_CLOCK_ENTRY_TIME (entry);
528

529 530
  clock = GST_CLOCK_ENTRY_CLOCK (entry);

Wim Taymans's avatar
Wim Taymans committed
531
  /* can't sync on invalid times */
532 533 534
  if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (requested)))
    goto invalid_time;

Wim Taymans's avatar
Wim Taymans committed
535
  cclass = GST_CLOCK_GET_CLASS (clock);
536

Wim Taymans's avatar
Wim Taymans committed
537 538
  GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock, "waiting on clock entry %p", id);

539
  /* if we have a wait_jitter function, use that */
Wim Taymans's avatar
Wim Taymans committed
540 541 542 543
  if (G_UNLIKELY (cclass->wait == NULL))
    goto not_supported;

  res = cclass->wait (clock, entry, jitter);
Wim Taymans's avatar
Wim Taymans committed
544 545

  GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
546 547
      "done waiting entry %p, res: %d (%s)", id, res,
      gst_clock_return_get_name (res));
Wim Taymans's avatar
Wim Taymans committed
548

549 550
  if (entry->type == GST_CLOCK_ENTRY_PERIODIC)
    entry->time = requested + entry->interval;
Wim Taymans's avatar
Wim Taymans committed
551

552
  return res;
553 554 555 556

  /* ERRORS */
invalid_time:
  {
557 558
    GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
        "invalid time requested, returning _BADTIME");
559 560
    return GST_CLOCK_BADTIME;
  }
Wim Taymans's avatar
Wim Taymans committed
561 562 563 564 565
not_supported:
  {
    GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock, "clock wait is not supported");
    return GST_CLOCK_UNSUPPORTED;
  }
566 567 568
}

/**
Wim Taymans's avatar
Wim Taymans committed
569
 * gst_clock_id_wait_async:
Wim Taymans's avatar
Wim Taymans committed
570
 * @id: a #GstClockID to wait on
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
571
 * @func: The callback function
572
 * @user_data: User data passed in the callback
573
 * @destroy_data: #GDestroyNotify for user_data
574
 *
Wim Taymans's avatar
Wim Taymans committed
575 576
 * Register a callback on the given #GstClockID @id with the given
 * function and user_data. When passing a #GstClockID with an invalid
577
 * time to this function, the callback will be called immediately
578
 * with  a time set to GST_CLOCK_TIME_NONE. The callback will
Wim Taymans's avatar
Wim Taymans committed
579
 * be called when the time of @id has been reached.
580
 *
581 582 583
 * The callback @func can be invoked from any thread, either provided by the
 * core or from a streaming thread. The application should be prepared for this.
 *
584
 * Returns: the result of the non blocking wait.
585 586
 *
 * MT safe.
587 588
 */
GstClockReturn
Wim Taymans's avatar
Wim Taymans committed
589
gst_clock_id_wait_async (GstClockID id,
590
    GstClockCallback func, gpointer user_data, GDestroyNotify destroy_data)
591 592 593
{
  GstClockEntry *entry;
  GstClock *clock;
594
  GstClockReturn res;
Wim Taymans's avatar
Wim Taymans committed
595
  GstClockClass *cclass;
596
  GstClockTime requested;
597

598
  g_return_val_if_fail (id != NULL, GST_CLOCK_ERROR);
599
  g_return_val_if_fail (func != NULL, GST_CLOCK_ERROR);
600 601

  entry = (GstClockEntry *) id;
602 603
  requested = GST_CLOCK_ENTRY_TIME (entry);
  clock = GST_CLOCK_ENTRY_CLOCK (entry);
604

Wim Taymans's avatar
Wim Taymans committed
605
  /* can't sync on invalid times */
606 607 608
  if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (requested)))
    goto invalid_time;

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

Wim Taymans's avatar
Wim Taymans committed
611 612 613 614 615
  if (G_UNLIKELY (cclass->wait_async == NULL))
    goto not_supported;

  entry->func = func;
  entry->user_data = user_data;
616
  entry->destroy_data = destroy_data;
Wim Taymans's avatar
Wim Taymans committed
617 618

  res = cclass->wait_async (clock, entry);
Wim Taymans's avatar
Wim Taymans committed
619

620 621
  return res;

622 623 624 625
  /* ERRORS */
invalid_time:
  {
    (func) (clock, GST_CLOCK_TIME_NONE, id, user_data);
626 627
    GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock,
        "invalid time requested, returning _BADTIME");
628 629
    return GST_CLOCK_BADTIME;
  }
Wim Taymans's avatar
Wim Taymans committed
630 631 632 633 634
not_supported:
  {
    GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, clock, "clock wait is not supported");
    return GST_CLOCK_UNSUPPORTED;
  }
635 636
}

637
/**
Wim Taymans's avatar
Wim Taymans committed
638 639
 * gst_clock_id_unschedule:
 * @id: The id to unschedule
640
 *
Wim Taymans's avatar
Wim Taymans committed
641
 * Cancel an outstanding request with @id. This can either
642
 * be an outstanding async notification or a pending sync notification.
Wim Taymans's avatar
Wim Taymans committed
643 644
 * After this call, @id cannot be used anymore to receive sync or
 * async notifications, you need to create a new #GstClockID.
645 646
 *
 * MT safe.
647 648 649 650 651 652
 */
void
gst_clock_id_unschedule (GstClockID id)
{
  GstClockEntry *entry;
  GstClock *clock;
Wim Taymans's avatar
Wim Taymans committed
653
  GstClockClass *cclass;
654

655 656 657 658
  g_return_if_fail (id != NULL);

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

Wim Taymans's avatar
Wim Taymans committed
660 661
  cclass = GST_CLOCK_GET_CLASS (clock);

Wim Taymans's avatar
Wim Taymans committed
662
  if (G_LIKELY (cclass->unschedule))
Wim Taymans's avatar
Wim Taymans committed
663
    cclass->unschedule (clock, entry);
664 665
}

666

Stefan Kost's avatar
Stefan Kost committed
667
/*
668 669
 * GstClock abstract base class implementation
 */
Wim Taymans's avatar
Wim Taymans committed
670
#define gst_clock_parent_class parent_class
Wim Taymans's avatar
Wim Taymans committed
671
G_DEFINE_ABSTRACT_TYPE (GstClock, gst_clock, GST_TYPE_OBJECT);
672

673
static void
674
gst_clock_class_init (GstClockClass * klass)
675
{
676
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
677

678 679 680 681
  gobject_class->dispose = gst_clock_dispose;
  gobject_class->finalize = gst_clock_finalize;
  gobject_class->set_property = gst_clock_set_property;
  gobject_class->get_property = gst_clock_get_property;
682

683
  g_object_class_install_property (gobject_class, PROP_WINDOW_SIZE,
684 685
      g_param_spec_int ("window-size", "Window size",
          "The size of the window used to calculate rate and offset", 2, 1024,
686
          DEFAULT_WINDOW_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
687 688
  g_object_class_install_property (gobject_class, PROP_WINDOW_THRESHOLD,
      g_param_spec_int ("window-threshold", "Window threshold",
689
          "The threshold to start calculating rate and offset", 2, 1024,
690 691
          DEFAULT_WINDOW_THRESHOLD,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
692
  g_object_class_install_property (gobject_class, PROP_TIMEOUT,
693 694
      g_param_spec_uint64 ("timeout", "Timeout",
          "The amount of time, in nanoseconds, to sample master and slave clocks",
695 696
          0, G_MAXUINT64, DEFAULT_TIMEOUT,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
697

698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
  /**
   * GstClock::synced:
   * @clock: the clock
   * @synced: if the clock is synced now
   *
   * Signaled on clocks with GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC set once
   * the clock is synchronized, or when it completely lost synchronization.
   * This signal will not be emitted on clocks without the flag.
   *
   * This signal will be emitted from an arbitrary thread, most likely not
   * the application's main thread.
   *
   * Since: 1.6
   */
  gst_clock_signals[SIGNAL_SYNCED] =
      g_signal_new ("synced", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
      0, NULL, NULL,
      g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_BOOLEAN);

717
  g_type_class_add_private (klass, sizeof (GstClockPrivate));
Wim Taymans's avatar
Wim Taymans committed
718 719
}

720
static void
721
gst_clock_init (GstClock * clock)
Wim Taymans's avatar
Wim Taymans committed
722
{
Wim Taymans's avatar
Wim Taymans committed
723
  GstClockPrivate *priv;
724

Wim Taymans's avatar
Wim Taymans committed
725
  clock->priv = priv =
726 727
      G_TYPE_INSTANCE_GET_PRIVATE (clock, GST_TYPE_CLOCK, GstClockPrivate);

Wim Taymans's avatar
Wim Taymans committed
728 729 730 731 732 733 734 735
  priv->last_time = 0;

  priv->internal_calibration = 0;
  priv->external_calibration = 0;
  priv->rate_numerator = 1;
  priv->rate_denominator = 1;

  g_mutex_init (&priv->slave_lock);
736
  g_cond_init (&priv->sync_cond);
Wim Taymans's avatar
Wim Taymans committed
737 738 739 740 741 742
  priv->window_size = DEFAULT_WINDOW_SIZE;
  priv->window_threshold = DEFAULT_WINDOW_THRESHOLD;
  priv->filling = TRUE;
  priv->time_index = 0;
  priv->timeout = DEFAULT_TIMEOUT;
  priv->times = g_new0 (GstClockTime, 4 * priv->window_size);
743
  priv->times_temp = priv->times + 2 * priv->window_size;
744 745 746

  /* clear floating flag */
  gst_object_ref_sink (clock);
Wim Taymans's avatar
Wim Taymans committed
747 748
}

749 750 751 752
static void
gst_clock_dispose (GObject * object)
{
  GstClock *clock = GST_CLOCK (object);
753
  GstClock **master_p;
754 755

  GST_OBJECT_LOCK (clock);
Wim Taymans's avatar
Wim Taymans committed
756
  master_p = &clock->priv->master;
757
  gst_object_replace ((GstObject **) master_p, NULL);
758 759 760 761 762
  GST_OBJECT_UNLOCK (clock);

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

Wim Taymans's avatar
Wim Taymans committed
763
static void
Wim Taymans's avatar
Wim Taymans committed
764
gst_clock_finalize (GObject * object)
Wim Taymans's avatar
Wim Taymans committed
765 766 767
{
  GstClock *clock = GST_CLOCK (object);

768
  GST_CLOCK_SLAVE_LOCK (clock);
Wim Taymans's avatar
Wim Taymans committed
769 770 771 772
  if (clock->priv->clockid) {
    gst_clock_id_unschedule (clock->priv->clockid);
    gst_clock_id_unref (clock->priv->clockid);
    clock->priv->clockid = NULL;
773
  }
Wim Taymans's avatar
Wim Taymans committed
774 775
  g_free (clock->priv->times);
  clock->priv->times = NULL;
776
  clock->priv->times_temp = NULL;
777
  GST_CLOCK_SLAVE_UNLOCK (clock);
778

Wim Taymans's avatar
Wim Taymans committed
779
  g_mutex_clear (&clock->priv->slave_lock);
780
  g_cond_clear (&clock->priv->sync_cond);
781

Wim Taymans's avatar
Wim Taymans committed
782
  G_OBJECT_CLASS (parent_class)->finalize (object);
Wim Taymans's avatar
Wim Taymans committed
783 784
}

Wim Taymans's avatar
Wim Taymans committed
785
/**
786
 * gst_clock_set_resolution:
787
 * @clock: a #GstClock
788
 * @resolution: The resolution to set
Wim Taymans's avatar
Wim Taymans committed
789
 *
790 791 792 793 794
 * Set the accuracy of the clock. Some clocks have the possibility to operate
 * with different accuracy at the expense of more resource usage. There is
 * normally no need to change the default resolution of a clock. The resolution
 * of a clock can only be changed if the clock has the
 * #GST_CLOCK_FLAG_CAN_SET_RESOLUTION flag set.
795 796
 *
 * Returns: the new resolution of the clock.
Wim Taymans's avatar
Wim Taymans committed
797
 */
798 799
GstClockTime
gst_clock_set_resolution (GstClock * clock, GstClockTime resolution)
Wim Taymans's avatar
Wim Taymans committed
800
{
Wim Taymans's avatar
Wim Taymans committed
801
  GstClockPrivate *priv;
Wim Taymans's avatar
Wim Taymans committed
802 803
  GstClockClass *cclass;

804 805
  g_return_val_if_fail (GST_IS_CLOCK (clock), 0);
  g_return_val_if_fail (resolution != 0, 0);
806

Wim Taymans's avatar
Wim Taymans committed
807
  cclass = GST_CLOCK_GET_CLASS (clock);
Wim Taymans's avatar
Wim Taymans committed
808
  priv = clock->priv;
Wim Taymans's avatar
Wim Taymans committed
809 810

  if (cclass->change_resolution)
Wim Taymans's avatar
Wim Taymans committed
811 812
    priv->resolution =
        cclass->change_resolution (clock, priv->resolution, resolution);
813

Wim Taymans's avatar
Wim Taymans committed
814
  return priv->resolution;
815
}
816

817
/**
818
 * gst_clock_get_resolution:
819
 * @clock: a #GstClock
820
 *
821 822
 * Get the accuracy of the clock. The accuracy of the clock is the granularity
 * of the values returned by gst_clock_get_time().
823
 *
824
 * Returns: the resolution of the clock in units of #GstClockTime.
825 826
 *
 * MT safe.
827
 */
828
GstClockTime
829
gst_clock_get_resolution (GstClock * clock)
830
{
Wim Taymans's avatar
Wim Taymans committed
831 832
  GstClockClass *cclass;

833
  g_return_val_if_fail (GST_IS_CLOCK (clock), 0);
834

Wim Taymans's avatar
Wim Taymans committed
835 836 837 838
  cclass = GST_CLOCK_GET_CLASS (clock);

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

840
  return 1;
Wim Taymans's avatar
Wim Taymans committed
841 842
}

843
/* FIXME 2.0: Remove clock parameter below */
Wim Taymans's avatar
Wim Taymans committed
844
/**
845
 * gst_clock_adjust_with_calibration:
846
 * @clock: (allow-none): a #GstClock to use
847 848 849 850 851 852 853 854 855 856 857 858
 * @internal_target: a clock time
 * @cinternal: a reference internal time
 * @cexternal: a reference external time
 * @cnum: the numerator of the rate of the clock relative to its
 *        internal time
 * @cdenom: the denominator of the rate of the clock
 *
 * Converts the given @internal_target clock time to the external time,
 * using the passed calibration parameters. This function performs the
 * same calculation as gst_clock_adjust_unlocked() when called using the
 * current calibration parameters, but doesn't ensure a monotonically
 * increasing result as gst_clock_adjust_unlocked() does.
859
 *
860 861
 * Note: The @clock parameter is unused and can be NULL
 *
862
 * Returns: the converted time of the clock.
863 864
 *
 * Since: 1.6
865
 */
866
GstClockTime
867 868 869
gst_clock_adjust_with_calibration (GstClock * clock,
    GstClockTime internal_target, GstClockTime cinternal,
    GstClockTime cexternal, GstClockTime cnum, GstClockTime cdenom)
870
{
871
  GstClockTime ret;
872 873

  /* avoid divide by 0 */
874
  if (G_UNLIKELY (cdenom == 0))
875 876 877 878 879
    cnum = cdenom = 1;

  /* The formula is (internal - cinternal) * cnum / cdenom + cexternal
   *
   * Since we do math on unsigned 64-bit ints we have to special case for
880
   * internal < cinternal to get the sign right. this case is not very common,
881 882
   * though.
   */
883 884
  if (G_LIKELY (internal_target >= cinternal)) {
    ret = internal_target - cinternal;
885
    ret = gst_util_uint64_scale (ret, cnum, cdenom);
886 887
    ret += cexternal;
  } else {
888
    ret = cinternal - internal_target;
889
    ret = gst_util_uint64_scale (ret, cnum, cdenom);
890
    /* clamp to 0 */
891
    if (G_LIKELY (cexternal > ret))
892 893 894 895
      ret = cexternal - ret;
    else
      ret = 0;
  }
896

897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
  return ret;
}

/**
 * gst_clock_adjust_unlocked:
 * @clock: a #GstClock to use
 * @internal: a clock time
 *
 * Converts the given @internal clock time to the external time, adjusting for the
 * rate and reference time set with gst_clock_set_calibration() and making sure
 * that the returned time is increasing. This function should be called with the
 * clock's OBJECT_LOCK held and is mainly used by clock subclasses.
 *
 * This function is the reverse of gst_clock_unadjust_unlocked().
 *
 * Returns: the converted time of the clock.
 */
GstClockTime
gst_clock_adjust_unlocked (GstClock * clock, GstClockTime internal)
{
  GstClockTime ret, cinternal, cexternal, cnum, cdenom;
  GstClockPrivate *priv = clock->priv;

  /* get calibration values for readability */
  cinternal = priv->internal_calibration;
  cexternal = priv->external_calibration;
  cnum = priv->rate_numerator;
  cdenom = priv->rate_denominator;

  ret =
      gst_clock_adjust_with_calibration (clock, internal, cinternal, cexternal,
      cnum, cdenom);