2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2005 Wim Taymans <wim@fluendo.com>
6 * gstclock.h: Header for clock subsystem
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 #ifndef __GST_CLOCK_H__
25 #define __GST_CLOCK_H__
29 /* --- standard type macros --- */
30 #define GST_TYPE_CLOCK (gst_clock_get_type ())
31 #define GST_CLOCK(clock) (G_TYPE_CHECK_INSTANCE_CAST ((clock), GST_TYPE_CLOCK, GstClock))
32 #define GST_IS_CLOCK(clock) (G_TYPE_CHECK_INSTANCE_TYPE ((clock), GST_TYPE_CLOCK))
33 #define GST_CLOCK_CLASS(cclass) (G_TYPE_CHECK_CLASS_CAST ((cclass), GST_TYPE_CLOCK, GstClockClass))
34 #define GST_IS_CLOCK_CLASS(cclass) (G_TYPE_CHECK_CLASS_TYPE ((cclass), GST_TYPE_CLOCK))
35 #define GST_CLOCK_GET_CLASS(clock) (G_TYPE_INSTANCE_GET_CLASS ((clock), GST_TYPE_CLOCK, GstClockClass))
36 #define GST_CLOCK_CAST(clock) ((GstClock*)(clock))
38 #define GST_CLOCK_SLAVE_LOCK(clock) g_mutex_lock (&GST_CLOCK_CAST (clock)->slave_lock)
39 #define GST_CLOCK_SLAVE_UNLOCK(clock) g_mutex_unlock (&GST_CLOCK_CAST (clock)->slave_lock)
44 * A datatype to hold a time, measured in nanoseconds.
46 typedef guint64 GstClockTime;
49 * GST_TYPE_CLOCK_TIME:
51 * The #GType of a #GstClockTime.
53 #define GST_TYPE_CLOCK_TIME G_TYPE_UINT64
58 * A datatype to hold a time difference, measured in nanoseconds.
60 typedef gint64 GstClockTimeDiff;
64 * A datatype to hold the handle to an outstanding sync or async clock callback.
66 typedef gpointer GstClockID;
69 * GST_CLOCK_TIME_NONE:
71 * Constant to define an undefined clock time.
73 #define GST_CLOCK_TIME_NONE ((GstClockTime) -1)
75 * GST_CLOCK_TIME_IS_VALID:
76 * @time: clock time to validate
78 * Tests if a given #GstClockTime represents a valid defined time.
80 #define GST_CLOCK_TIME_IS_VALID(time) (((GstClockTime)(time)) != GST_CLOCK_TIME_NONE)
82 /* FIXME: still need to explicitly force types on the defines below */
86 * Constant that defines one GStreamer second.
91 #define GST_SECOND (G_USEC_PER_SEC * G_GINT64_CONSTANT (1000))
95 * Constant that defines one GStreamer millisecond.
100 #define GST_MSECOND (GST_SECOND / G_GINT64_CONSTANT (1000))
104 * Constant that defines one GStreamer microsecond.
109 #define GST_USECOND (GST_SECOND / G_GINT64_CONSTANT (1000000))
113 * Constant that defines one GStreamer nanosecond
118 #define GST_NSECOND (GST_SECOND / G_GINT64_CONSTANT (1000000000))
122 * GST_TIME_AS_SECONDS:
125 * Convert a #GstClockTime to seconds.
129 #define GST_TIME_AS_SECONDS(time) ((time) / GST_SECOND)
131 * GST_TIME_AS_MSECONDS:
134 * Convert a #GstClockTime to milliseconds (1/1000 of a second).
138 #define GST_TIME_AS_MSECONDS(time) ((time) / G_GINT64_CONSTANT (1000000))
140 * GST_TIME_AS_USECONDS:
143 * Convert a #GstClockTime to microseconds (1/1000000 of a second).
147 #define GST_TIME_AS_USECONDS(time) ((time) / G_GINT64_CONSTANT (1000))
149 * GST_TIME_AS_NSECONDS:
152 * Convert a #GstClockTime to nanoseconds (1/1000000000 of a second).
156 #define GST_TIME_AS_NSECONDS(time) (time)
161 * @e: the second time
163 * Calculate a difference between two clock times as a #GstClockTimeDiff.
164 * The difference is calculated as @e - @s.
166 #define GST_CLOCK_DIFF(s, e) (GstClockTimeDiff)((e) - (s))
169 * GST_TIMEVAL_TO_TIME:
170 * @tv: the timeval to convert
172 * Convert a #GTimeVal to a #GstClockTime.
174 #define GST_TIMEVAL_TO_TIME(tv) (GstClockTime)((tv).tv_sec * GST_SECOND + (tv).tv_usec * GST_USECOND)
177 * GST_TIME_TO_TIMEVAL:
178 * @t: The #GstClockTime to convert
179 * @tv: The target timeval
181 * Convert a #GstClockTime to a #GTimeVal
183 * <note>on 32-bit systems, a timeval has a range of only 2^32 - 1 seconds,
184 * which is about 68 years. Expect trouble if you want to schedule stuff
185 * in your pipeline for 2038.</note>
187 #define GST_TIME_TO_TIMEVAL(t,tv) \
189 (tv).tv_sec = ((GstClockTime) (t)) / GST_SECOND; \
190 (tv).tv_usec = (((GstClockTime) (t)) - \
191 ((GstClockTime) (tv).tv_sec) * GST_SECOND) \
196 * GST_TIMESPEC_TO_TIME:
197 * @ts: the timespec to convert
199 * Convert a struct timespec (see man pselect) to a #GstClockTime.
201 #define GST_TIMESPEC_TO_TIME(ts) (GstClockTime)((ts).tv_sec * GST_SECOND + (ts).tv_nsec * GST_NSECOND)
203 * GST_TIME_TO_TIMESPEC:
204 * @t: The #GstClockTime to convert
205 * @ts: The target timespec
207 * Convert a #GstClockTime to a struct timespec (see man pselect)
209 #define GST_TIME_TO_TIMESPEC(t,ts) \
211 (ts).tv_sec = (t) / GST_SECOND; \
212 (ts).tv_nsec = ((t) - (ts).tv_sec * GST_SECOND) / GST_NSECOND; \
215 /* timestamp debugging macros */
219 * A string that can be used in printf-like format strings to display a
220 * #GstClockTime value in h:m:s format. Use GST_TIME_ARGS() to construct
221 * the matching arguments.
225 * printf("%" GST_TIME_FORMAT "\n", GST_TIME_ARGS(ts));
228 #define GST_TIME_FORMAT "u:%02u:%02u.%09u"
231 * @t: a #GstClockTime
233 * Format @t for the #GST_TIME_FORMAT format string. Note: @t will be
234 * evaluated more than once.
236 #define GST_TIME_ARGS(t) \
237 GST_CLOCK_TIME_IS_VALID (t) ? \
238 (guint) (((GstClockTime)(t)) / (GST_SECOND * 60 * 60)) : 99, \
239 GST_CLOCK_TIME_IS_VALID (t) ? \
240 (guint) ((((GstClockTime)(t)) / (GST_SECOND * 60)) % 60) : 99, \
241 GST_CLOCK_TIME_IS_VALID (t) ? \
242 (guint) ((((GstClockTime)(t)) / GST_SECOND) % 60) : 99, \
243 GST_CLOCK_TIME_IS_VALID (t) ? \
244 (guint) (((GstClockTime)(t)) % GST_SECOND) : 999999999
246 typedef struct _GstClockEntry GstClockEntry;
247 typedef struct _GstClock GstClock;
248 typedef struct _GstClockClass GstClockClass;
249 typedef struct _GstClockPrivate GstClockPrivate;
251 /* --- prototype for async callbacks --- */
254 * @clock: The clock that triggered the callback
255 * @time: The time it was triggered
256 * @id: The #GstClockID that expired
257 * @user_data: user data passed in the gst_clock_id_wait_async() function
259 * The function prototype of the callback.
261 * Returns: %TRUE or %FALSE (currently unused)
263 typedef gboolean (*GstClockCallback) (GstClock *clock, GstClockTime time,
264 GstClockID id, gpointer user_data);
267 * @GST_CLOCK_OK: The operation succeeded.
268 * @GST_CLOCK_EARLY: The operation was scheduled too late.
269 * @GST_CLOCK_UNSCHEDULED: The clockID was unscheduled
270 * @GST_CLOCK_BUSY: The ClockID is busy
271 * @GST_CLOCK_BADTIME: A bad time was provided to a function.
272 * @GST_CLOCK_ERROR: An error occurred
273 * @GST_CLOCK_UNSUPPORTED: Operation is not supported
274 * @GST_CLOCK_DONE: The ClockID is done waiting (Since: 0.10.32)
276 * The return value of a clock operation.
282 GST_CLOCK_UNSCHEDULED = 2,
284 GST_CLOCK_BADTIME = 4,
286 GST_CLOCK_UNSUPPORTED = 6,
292 * @GST_CLOCK_ENTRY_SINGLE: a single shot timeout
293 * @GST_CLOCK_ENTRY_PERIODIC: a periodic timeout request
295 * The type of the clock entry
298 GST_CLOCK_ENTRY_SINGLE,
299 GST_CLOCK_ENTRY_PERIODIC
304 * @entry: the entry to cast
306 * Cast to a clock entry
308 #define GST_CLOCK_ENTRY(entry) ((GstClockEntry *)(entry))
310 * GST_CLOCK_ENTRY_CLOCK:
311 * @entry: the entry to query
313 * Get the owner clock of the entry
315 #define GST_CLOCK_ENTRY_CLOCK(entry) ((entry)->clock)
317 * GST_CLOCK_ENTRY_TYPE:
318 * @entry: the entry to query
320 * Get the type of the clock entry
322 #define GST_CLOCK_ENTRY_TYPE(entry) ((entry)->type)
324 * GST_CLOCK_ENTRY_TIME:
325 * @entry: the entry to query
327 * Get the requested time of this entry
329 #define GST_CLOCK_ENTRY_TIME(entry) ((entry)->time)
331 * GST_CLOCK_ENTRY_INTERVAL:
332 * @entry: the entry to query
334 * Get the interval of this periodic entry
336 #define GST_CLOCK_ENTRY_INTERVAL(entry) ((entry)->interval)
338 * GST_CLOCK_ENTRY_STATUS:
339 * @entry: the entry to query
341 * The status of the entry
343 #define GST_CLOCK_ENTRY_STATUS(entry) ((entry)->status)
347 * @refcount: reference counter (read-only)
349 * All pending timeouts or periodic notifies are converted into
351 * Note that GstClockEntry should be treated as an opaque structure. It must
352 * not be extended or allocated using a custom allocator.
354 struct _GstClockEntry {
358 GstClockEntryType type;
360 GstClockTime interval;
361 GstClockReturn status;
362 GstClockCallback func;
364 GDestroyNotify destroy_data;
365 gboolean unscheduled;
368 gpointer _gst_reserved[GST_PADDING];
371 #include <gst/gstobject.h>
375 * @GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC: clock can do a single sync timeout request
376 * @GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC: clock can do a single async timeout request
377 * @GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC: clock can do sync periodic timeout requests
378 * @GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC: clock can do async periodic timeout callbacks
379 * @GST_CLOCK_FLAG_CAN_SET_RESOLUTION: clock's resolution can be changed
380 * @GST_CLOCK_FLAG_CAN_SET_MASTER: clock can be slaved to a master clock
381 * @GST_CLOCK_FLAG_LAST: subclasses can add additional flags starting from this flag
383 * The capabilities of this clock
386 GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC = (GST_OBJECT_FLAG_LAST << 0),
387 GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC = (GST_OBJECT_FLAG_LAST << 1),
388 GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC = (GST_OBJECT_FLAG_LAST << 2),
389 GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC = (GST_OBJECT_FLAG_LAST << 3),
390 GST_CLOCK_FLAG_CAN_SET_RESOLUTION = (GST_OBJECT_FLAG_LAST << 4),
391 GST_CLOCK_FLAG_CAN_SET_MASTER = (GST_OBJECT_FLAG_LAST << 5),
393 GST_CLOCK_FLAG_LAST = (GST_OBJECT_FLAG_LAST << 8)
398 * @clock: the clock to query
400 * Gets the #GstClockFlags clock flags.
402 #define GST_CLOCK_FLAGS(clock) GST_OBJECT_FLAGS(clock)
405 * GST_CLOCK_GET_COND:
406 * @clock: the clock to query
408 * Gets the #GCond that gets signalled when the entries of the clock
411 #define GST_CLOCK_GET_COND(clock) (&GST_CLOCK_CAST(clock)->entries_changed)
414 * @clock: the clock to wait on
416 * Wait on the clock until the entries changed.
418 #define GST_CLOCK_WAIT(clock) g_cond_wait(GST_CLOCK_GET_COND(clock),GST_OBJECT_GET_LOCK(clock))
420 * GST_CLOCK_TIMED_WAIT:
421 * @clock: the clock to wait on
422 * @tv: a #GTimeVal to wait.
424 * Wait on the clock until the entries changed or the specified timeout
427 #define GST_CLOCK_TIMED_WAIT(clock,tv) g_cond_timed_wait(GST_CLOCK_GET_COND(clock),GST_OBJECT_GET_LOCK(clock),tv)
429 * GST_CLOCK_BROADCAST:
430 * @clock: the clock to broadcast
432 * Signal that the entries in the clock have changed.
434 #define GST_CLOCK_BROADCAST(clock) g_cond_broadcast(GST_CLOCK_GET_COND(clock))
439 * #GstClock base structure. The values of this structure are
440 * protected for subclasses, use the methods to use the #GstClock.
445 GMutex slave_lock; /* order: SLAVE_LOCK, OBJECT_LOCK */
447 /*< protected >*/ /* with LOCK */
448 GstClockTime internal_calibration;
449 GstClockTime external_calibration;
450 GstClockTime rate_numerator;
451 GstClockTime rate_denominator;
452 GstClockTime last_time;
454 GCond entries_changed;
456 /*< private >*/ /* with LOCK */
457 GstClockTime resolution;
459 /* for master/slave clocks */
462 /* with SLAVE_LOCK */
465 gint window_threshold;
467 GstClockTime timeout;
472 GstClockPrivate *priv;
474 gpointer _gst_reserved[GST_PADDING];
479 * @parent_class: the parent class structure
480 * @change_resolution: change the resolution of the clock. Not all values might
481 * be acceptable. The new resolution should be returned.
482 * @get_resolution: get the resolution of the clock.
483 * @get_internal_time: get the internal unadjusted time of the clock.
484 * implement @wait_jitter instead.
485 * @wait: perform a blocking wait on the given #GstClockEntry and return
487 * @wait_async: perform an asynchronous wait for the given #GstClockEntry.
488 * @unschedule: unblock a blocking or async wait operation.
490 * GStreamer clock class. Override the vmethods to implement the clock
493 struct _GstClockClass {
494 GstObjectClass parent_class;
498 GstClockTime (*change_resolution) (GstClock *clock,
499 GstClockTime old_resolution,
500 GstClockTime new_resolution);
501 GstClockTime (*get_resolution) (GstClock *clock);
503 GstClockTime (*get_internal_time) (GstClock *clock);
505 /* waiting on an ID */
506 GstClockReturn (*wait) (GstClock *clock, GstClockEntry *entry,
507 GstClockTimeDiff *jitter);
508 GstClockReturn (*wait_async) (GstClock *clock, GstClockEntry *entry);
509 void (*unschedule) (GstClock *clock, GstClockEntry *entry);
512 gpointer _gst_reserved[GST_PADDING];
515 GType gst_clock_get_type (void);
517 GstClockTime gst_clock_set_resolution (GstClock *clock,
518 GstClockTime resolution);
519 GstClockTime gst_clock_get_resolution (GstClock *clock);
521 GstClockTime gst_clock_get_time (GstClock *clock);
522 void gst_clock_set_calibration (GstClock *clock, GstClockTime internal,
523 GstClockTime external,
524 GstClockTime rate_num,
525 GstClockTime rate_denom);
526 void gst_clock_get_calibration (GstClock *clock, GstClockTime *internal,
527 GstClockTime *external,
528 GstClockTime *rate_num,
529 GstClockTime *rate_denom);
531 /* master/slave clocks */
532 gboolean gst_clock_set_master (GstClock *clock, GstClock *master);
533 GstClock* gst_clock_get_master (GstClock *clock);
534 gboolean gst_clock_add_observation (GstClock *clock, GstClockTime slave,
535 GstClockTime master, gdouble *r_squared);
538 /* getting and adjusting internal/external time */
539 GstClockTime gst_clock_get_internal_time (GstClock *clock);
540 GstClockTime gst_clock_adjust_unlocked (GstClock *clock, GstClockTime internal);
541 GstClockTime gst_clock_unadjust_unlocked (GstClock * clock, GstClockTime external);
544 /* creating IDs that can be used to get notifications */
545 GstClockID gst_clock_new_single_shot_id (GstClock *clock,
547 GstClockID gst_clock_new_periodic_id (GstClock *clock,
548 GstClockTime start_time,
549 GstClockTime interval);
551 /* reference counting */
552 GstClockID gst_clock_id_ref (GstClockID id);
553 void gst_clock_id_unref (GstClockID id);
555 /* operations on IDs */
556 gint gst_clock_id_compare_func (gconstpointer id1, gconstpointer id2);
558 GstClockTime gst_clock_id_get_time (GstClockID id);
559 GstClockReturn gst_clock_id_wait (GstClockID id,
560 GstClockTimeDiff *jitter);
561 GstClockReturn gst_clock_id_wait_async (GstClockID id,
562 GstClockCallback func,
564 GstClockReturn gst_clock_id_wait_async_full (GstClockID id,
565 GstClockCallback func,
567 GDestroyNotify destroy_data);
568 void gst_clock_id_unschedule (GstClockID id);
570 gboolean gst_clock_single_shot_id_reinit (GstClock * clock,
573 gboolean gst_clock_periodic_id_reinit (GstClock * clock,
575 GstClockTime start_time,
576 GstClockTime interval);
580 #endif /* __GST_CLOCK_H__ */