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__
27 #include <gst/gstobject.h>
31 /* --- standard type macros --- */
32 #define GST_TYPE_CLOCK (gst_clock_get_type ())
33 #define GST_CLOCK(clock) (G_TYPE_CHECK_INSTANCE_CAST ((clock), GST_TYPE_CLOCK, GstClock))
34 #define GST_IS_CLOCK(clock) (G_TYPE_CHECK_INSTANCE_TYPE ((clock), GST_TYPE_CLOCK))
35 #define GST_CLOCK_CLASS(cclass) (G_TYPE_CHECK_CLASS_CAST ((cclass), GST_TYPE_CLOCK, GstClockClass))
36 #define GST_IS_CLOCK_CLASS(cclass) (G_TYPE_CHECK_CLASS_TYPE ((cclass), GST_TYPE_CLOCK))
37 #define GST_CLOCK_GET_CLASS(clock) (G_TYPE_INSTANCE_GET_CLASS ((clock), GST_TYPE_CLOCK, GstClockClass))
38 #define GST_CLOCK_CAST(clock) ((GstClock*)(clock))
40 #define GST_CLOCK_SLAVE_LOCK(clock) g_mutex_lock (GST_CLOCK_CAST (clock)->slave_lock)
41 #define GST_CLOCK_SLAVE_UNLOCK(clock) g_mutex_unlock (GST_CLOCK_CAST (clock)->slave_lock)
46 * A datatype to hold a time, measured in nanoseconds.
48 typedef guint64 GstClockTime;
51 * GST_TYPE_CLOCK_TIME:
53 * The GType of a GstClockTime.
55 #define GST_TYPE_CLOCK_TIME G_TYPE_UINT64
60 * A datatype to hold a timedifference, measured in nanoseconds.
62 typedef gint64 GstClockTimeDiff;
66 * A datatype to hold the handle to an outstanding sync or async clock callback.
68 typedef gpointer GstClockID;
71 * GST_CLOCK_TIME_NONE:
73 * Constant to define an undefined clock time.
75 #define GST_CLOCK_TIME_NONE ((GstClockTime) -1)
77 * GST_CLOCK_TIME_IS_VALID:
78 * @time: clock time to validate
80 * Tests if a given #GstClockTime represents a valid defined time.
82 #define GST_CLOCK_TIME_IS_VALID(time) (((GstClockTime)(time)) != GST_CLOCK_TIME_NONE)
87 * Constant that defines one GStreamer second.
89 #define GST_SECOND (G_USEC_PER_SEC * G_GINT64_CONSTANT (1000))
93 * Constant that defines one GStreamer millisecond.
95 #define GST_MSECOND (GST_SECOND / G_GINT64_CONSTANT (1000))
99 * Constant that defines one GStreamer microsecond.
101 #define GST_USECOND (GST_SECOND / G_GINT64_CONSTANT (1000000))
105 * Constant that defines one GStreamer nanosecond
107 #define GST_NSECOND (GST_SECOND / G_GINT64_CONSTANT (1000000000))
112 * @e: the second time
114 * Calculate a difference between two clock times as a #GstClockTimeDiff.
115 * The difference is calculated as @e - @s.
117 #define GST_CLOCK_DIFF(s, e) (GstClockTimeDiff)((e) - (s))
120 * GST_TIMEVAL_TO_TIME:
121 * @tv: the timeval to convert
123 * Convert a GTimeVal to a #GstClockTime.
125 #define GST_TIMEVAL_TO_TIME(tv) ((tv).tv_sec * GST_SECOND + (tv).tv_usec * GST_USECOND)
128 * GST_TIME_TO_TIMEVAL:
129 * @t: The GstClockTime to convert
130 * @tv: The target timeval
132 * Note: on 32-bit systems, a timeval has a range of only 2^32 - 1 seconds,
133 * which is about 68 years. Expect trouble if you want to schedule stuff
134 * in your pipeline for 2038.
136 * Convert a GstClockTime to a GTimeVal
138 #define GST_TIME_TO_TIMEVAL(t,tv) \
140 (tv).tv_sec = ((GstClockTime) (t)) / GST_SECOND; \
141 (tv).tv_usec = (((GstClockTime) (t)) - \
142 ((GstClockTime) (tv).tv_sec) * GST_SECOND) \
147 * GST_TIMESPEC_TO_TIME:
148 * @ts: the timespec to convert
150 * Convert a struct timespec (see man pselect) to a #GstClockTime.
152 #define GST_TIMESPEC_TO_TIME(ts) ((ts).tv_sec * GST_SECOND + (ts).tv_nsec * GST_NSECOND)
154 * GST_TIME_TO_TIMESPEC:
155 * @t: The GstClockTime to convert
156 * @ts: The target timespec
158 * Convert a #GstClockTime to a struct timespec (see man pselect)
160 #define GST_TIME_TO_TIMESPEC(t,ts) \
162 (ts).tv_sec = (t) / GST_SECOND; \
163 (ts).tv_nsec = ((t) - (ts).tv_sec * GST_SECOND) / GST_NSECOND; \
166 /* timestamp debugging macros */
170 * A format that can be used in printf like format strings to format
171 * a GstClockTime value.
173 #define GST_TIME_FORMAT "u:%02u:%02u.%09u"
176 * @t: a #GstClockTime
178 * Format @t for the GST_TIME_FORMAT format string.
180 #define GST_TIME_ARGS(t) \
181 GST_CLOCK_TIME_IS_VALID (t) ? \
182 (guint) (((GstClockTime)(t)) / (GST_SECOND * 60 * 60)) : 99, \
183 GST_CLOCK_TIME_IS_VALID (t) ? \
184 (guint) ((((GstClockTime)(t)) / (GST_SECOND * 60)) % 60) : 99, \
185 GST_CLOCK_TIME_IS_VALID (t) ? \
186 (guint) ((((GstClockTime)(t)) / GST_SECOND) % 60) : 99, \
187 GST_CLOCK_TIME_IS_VALID (t) ? \
188 (guint) (((GstClockTime)(t)) % GST_SECOND) : 999999999
191 * GST_CLOCK_ENTRY_TRACE_NAME:
193 * The name used for tracing clock entry allocations.
195 #define GST_CLOCK_ENTRY_TRACE_NAME "GstClockEntry"
197 typedef struct _GstClockEntry GstClockEntry;
198 typedef struct _GstClock GstClock;
199 typedef struct _GstClockClass GstClockClass;
201 /* --- prototype for async callbacks --- */
204 * @clock: The clock that triggered the callback
205 * @time: The time it was triggered
206 * @id: The #GstClockID that expired
207 * @user_data: user data passed in the async_wait call
209 * The function prototype of the callback.
211 * Returns: %TRUE or %FALSE (currently unused)
213 typedef gboolean (*GstClockCallback) (GstClock *clock, GstClockTime time,
214 GstClockID id, gpointer user_data);
217 * @GST_CLOCK_OK: The operation succeded.
218 * @GST_CLOCK_EARLY: The operation was scheduled too late.
219 * @GST_CLOCK_UNSCHEDULED: The clockID was unscheduled
220 * @GST_CLOCK_BUSY: The ClockID is busy
221 * @GST_CLOCK_BADTIME: A bad time was provided to a function.
222 * @GST_CLOCK_ERROR: An error occured
223 * @GST_CLOCK_UNSUPPORTED: Operation is not supported
225 * The return value of a clock operation.
231 GST_CLOCK_UNSCHEDULED = 2,
233 GST_CLOCK_BADTIME = 4,
235 GST_CLOCK_UNSUPPORTED = 6,
240 * @GST_CLOCK_ENTRY_SINGLE: a single shot timeout
241 * @GST_CLOCK_ENTRY_PERIODIC: a periodic timeout request
243 * The type of the clock entry
246 GST_CLOCK_ENTRY_SINGLE,
247 GST_CLOCK_ENTRY_PERIODIC
252 * @entry: the entry to cast
254 * Cast to a clock entry
256 #define GST_CLOCK_ENTRY(entry) ((GstClockEntry *)(entry))
258 * GST_CLOCK_ENTRY_CLOCK:
259 * @entry: the entry to query
261 * Get the owner clock of the entry
263 #define GST_CLOCK_ENTRY_CLOCK(entry) ((entry)->clock)
265 * GST_CLOCK_ENTRY_TYPE:
266 * @entry: the entry to query
268 * Get the type of the clock entry
270 #define GST_CLOCK_ENTRY_TYPE(entry) ((entry)->type)
272 * GST_CLOCK_ENTRY_TIME:
273 * @entry: the entry to query
275 * Get the requested time of this entry
277 #define GST_CLOCK_ENTRY_TIME(entry) ((entry)->time)
279 * GST_CLOCK_ENTRY_INTERVAL:
280 * @entry: the entry to query
282 * Get the interval of this periodic entry
284 #define GST_CLOCK_ENTRY_INTERVAL(entry) ((entry)->interval)
286 * GST_CLOCK_ENTRY_STATUS:
287 * @entry: the entry to query
289 * The status of the entry
291 #define GST_CLOCK_ENTRY_STATUS(entry) ((entry)->status)
295 * @refcount: reference counter (read-only)
297 * All pending timeouts or periodic notifies are converted into
300 struct _GstClockEntry {
304 GstClockEntryType type;
306 GstClockTime interval;
307 GstClockReturn status;
308 GstClockCallback func;
314 * @GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC: clock can do a single sync timeout request
315 * @GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC: clock can do a single async timeout request
316 * @GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC: clock can do sync periodic timeout requests
317 * @GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC: clock can do async periodic timeout callbacks
318 * @GST_CLOCK_FLAG_CAN_SET_RESOLUTION: clock's resolution can be changed
319 * @GST_CLOCK_FLAG_CAN_SET_MASTER: clock can be slaved to a master clock
320 * @GST_CLOCK_FLAG_LAST: subclasses can add additional flags starting from this flag
322 * The capabilities of this clock
325 GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC = (GST_OBJECT_FLAG_LAST << 0),
326 GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC = (GST_OBJECT_FLAG_LAST << 1),
327 GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC = (GST_OBJECT_FLAG_LAST << 2),
328 GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC = (GST_OBJECT_FLAG_LAST << 3),
329 GST_CLOCK_FLAG_CAN_SET_RESOLUTION = (GST_OBJECT_FLAG_LAST << 4),
330 GST_CLOCK_FLAG_CAN_SET_MASTER = (GST_OBJECT_FLAG_LAST << 5),
332 GST_CLOCK_FLAG_LAST = (GST_OBJECT_FLAG_LAST << 8),
337 * @clock: the clock to query
339 * Gets the #GstClockFlags clock flags.
341 #define GST_CLOCK_FLAGS(clock) (GST_CLOCK(clock)->flags)
345 * @clock: the clock to query
347 * Gets the #GCond that gets signaled when the entries of the clock
350 #define GST_CLOCK_COND(clock) (GST_CLOCK_CAST(clock)->entries_changed)
353 * @clock: the clock to wait on
355 * Wait on the clock until the entries changed.
357 #define GST_CLOCK_WAIT(clock) g_cond_wait(GST_CLOCK_COND(clock),GST_OBJECT_GET_LOCK(clock))
359 * GST_CLOCK_TIMED_WAIT:
360 * @clock: the clock to wait on
361 * @tv: a GTimeVal to wait.
363 * Wait on the clock until the entries changed or the specified timeout
366 #define GST_CLOCK_TIMED_WAIT(clock,tv) g_cond_timed_wait(GST_CLOCK_COND(clock),GST_OBJECT_GET_LOCK(clock),tv)
368 * GST_CLOCK_BROADCAST:
369 * @clock: the clock to broadcast
371 * Signal that the entries in the clock have changed.
373 #define GST_CLOCK_BROADCAST(clock) g_cond_broadcast(GST_CLOCK_COND(clock))
377 * @flags: The flags specifying the capabilities of the clock.
379 * GstClock base structure. The values of this structure are
380 * protected for subclasses, use the methods to use the #GstClock.
385 GMutex *slave_lock; /* order: SLAVE_LOCK, OBJECT_LOCK */
387 /*< protected >*/ /* with LOCK */
388 GstClockTime internal_calibration;
389 GstClockTime external_calibration;
390 GstClockTime rate_numerator;
391 GstClockTime rate_denominator;
392 GstClockTime last_time;
394 GCond *entries_changed;
396 /*< private >*/ /* with LOCK */
397 GstClockTime resolution;
400 /* for master/slave clocks */
403 /* with SLAVE_LOCK */
406 gint window_threshold;
408 GstClockTime timeout;
413 GstClockTime _gst_reserved[GST_PADDING];
416 struct _GstClockClass {
417 GstObjectClass parent_class;
421 GstClockTime (*change_resolution) (GstClock *clock,
422 GstClockTime old_resolution,
423 GstClockTime new_resolution);
424 GstClockTime (*get_resolution) (GstClock *clock);
426 GstClockTime (*get_internal_time) (GstClock *clock);
428 /* waiting on an ID */
429 GstClockReturn (*wait) (GstClock *clock, GstClockEntry *entry);
430 GstClockReturn (*wait_async) (GstClock *clock, GstClockEntry *entry);
431 void (*unschedule) (GstClock *clock, GstClockEntry *entry);
434 gpointer _gst_reserved[GST_PADDING];
437 GType gst_clock_get_type (void);
439 GstClockTime gst_clock_set_resolution (GstClock *clock,
440 GstClockTime resolution);
441 GstClockTime gst_clock_get_resolution (GstClock *clock);
443 GstClockTime gst_clock_get_time (GstClock *clock);
444 void gst_clock_set_calibration (GstClock *clock, GstClockTime internal,
445 GstClockTime external,
446 GstClockTime rate_num,
447 GstClockTime rate_denom);
448 void gst_clock_get_calibration (GstClock *clock, GstClockTime *internal,
449 GstClockTime *external,
450 GstClockTime *rate_num,
451 GstClockTime *rate_denom);
453 /* master/slave clocks */
454 gboolean gst_clock_set_master (GstClock *clock, GstClock *master);
455 GstClock* gst_clock_get_master (GstClock *clock);
456 gboolean gst_clock_add_observation (GstClock *clock, GstClockTime slave,
457 GstClockTime master, gdouble *r_squared);
460 /* getting and adjusting internal time */
461 GstClockTime gst_clock_get_internal_time (GstClock *clock);
462 GstClockTime gst_clock_adjust_unlocked (GstClock *clock, GstClockTime internal);
465 /* creating IDs that can be used to get notifications */
466 GstClockID gst_clock_new_single_shot_id (GstClock *clock,
468 GstClockID gst_clock_new_periodic_id (GstClock *clock,
469 GstClockTime start_time,
470 GstClockTime interval);
472 /* reference counting */
473 GstClockID gst_clock_id_ref (GstClockID id);
474 void gst_clock_id_unref (GstClockID id);
476 /* operations on IDs */
477 gint gst_clock_id_compare_func (gconstpointer id1, gconstpointer id2);
479 GstClockTime gst_clock_id_get_time (GstClockID id);
480 GstClockReturn gst_clock_id_wait (GstClockID id,
481 GstClockTimeDiff *jitter);
482 GstClockReturn gst_clock_id_wait_async (GstClockID id,
483 GstClockCallback func,
485 void gst_clock_id_unschedule (GstClockID id);
490 #endif /* __GST_CLOCK_H__ */