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))
43 * A datatype to hold a time, measured in nanoseconds.
45 typedef guint64 GstClockTime;
47 #define GST_TYPE_CLOCK_TIME G_TYPE_UINT64
52 * A datatype to hold a timedifference, measured in nanoseconds.
54 typedef gint64 GstClockTimeDiff;
58 * A detatype to hold the handle to an outstanding async clock callback
60 typedef gpointer GstClockID;
63 * GST_CLOCK_TIME_NONE:
65 * Constant to define an undefined clock time
67 #define GST_CLOCK_TIME_NONE ((GstClockTime) -1)
69 * GST_CLOCK_TIME_IS_VALID:
70 * @time: clock time to validate
72 * Tests if a given #GstClockTime represents a valid defined time.
74 #define GST_CLOCK_TIME_IS_VALID(time) ((time) != GST_CLOCK_TIME_NONE)
79 * Constant that defines one GStreamer second
81 #define GST_SECOND (G_USEC_PER_SEC * G_GINT64_CONSTANT (1000))
85 * Constant that defines one GStreamer millisecond
87 #define GST_MSECOND (GST_SECOND / G_GINT64_CONSTANT (1000))
91 * Constant that defines one GStreamer microsecond
93 #define GST_USECOND (GST_SECOND / G_GINT64_CONSTANT (1000000))
97 * Constant that defines one GStreamer nanosecond
99 #define GST_NSECOND (GST_SECOND / G_GINT64_CONSTANT (1000000000))
104 * @e: the second time
106 * Calculate a difference between two clock times as a #GstClockTimeDiff.
108 #define GST_CLOCK_DIFF(s, e) (GstClockTimeDiff)((e) - (s))
111 * GST_TIMEVAL_TO_TIME:
112 * @tv: the timeval to convert
114 * Convert a GTimeVal to a #GstClockTime.
116 #define GST_TIMEVAL_TO_TIME(tv) ((tv).tv_sec * GST_SECOND + (tv).tv_usec * GST_USECOND)
119 * GST_TIME_TO_TIMEVAL:
120 * @t: The GstClockTime to convert
121 * @tv: The target timeval
123 * Note: on 32-bit systems, a timeval has a range of only 2^32 - 1 seconds,
124 * which is about 68 years. Expect trouble if you want to schedule stuff
125 * in your pipeline for 2038.
127 * Convert a GstClockTime to a GTimeVal
129 #define GST_TIME_TO_TIMEVAL(t,tv) \
131 (tv).tv_sec = ((GstClockTime) (t)) / GST_SECOND; \
132 (tv).tv_usec = (((GstClockTime) (t)) - \
133 ((GstClockTime) (tv).tv_sec) * GST_SECOND) \
138 * GST_TIMESPEC_TO_TIME:
139 * @ts: the timespec to convert
141 * Convert a struct timespec (see man pselect) to a #GstClockTime.
143 #define GST_TIMESPEC_TO_TIME(ts) ((ts).tv_sec * GST_SECOND + (ts).tv_nsec * GST_NSECOND)
145 * GST_TIME_TO_TIMESPEC:
146 * @t: The GstClockTime to convert
147 * @ts: The target timespec
149 * Convert a #GstClockTime to a struct timespec (see man pselect)
151 #define GST_TIME_TO_TIMESPEC(t,ts) \
153 (ts).tv_sec = (t) / GST_SECOND; \
154 (ts).tv_nsec = ((t) - (ts).tv_sec * GST_SECOND) / GST_NSECOND; \
157 /* timestamp debugging macros */
158 #define GST_TIME_FORMAT "u:%02u:%02u.%09u"
159 #define GST_TIME_ARGS(t) \
160 (guint) (((GstClockTime)(t)) / (GST_SECOND * 60 * 60)), \
161 (guint) ((((GstClockTime)(t)) / (GST_SECOND * 60)) % 60), \
162 (guint) ((((GstClockTime)(t)) / GST_SECOND) % 60), \
163 (guint) (((GstClockTime)(t)) % GST_SECOND)
166 * GST_CLOCK_ENTRY_TRACE_NAME:
168 * The name used for tracing clock entry allocations.
170 #define GST_CLOCK_ENTRY_TRACE_NAME "GstClockEntry"
172 typedef struct _GstClockEntry GstClockEntry;
173 typedef struct _GstClock GstClock;
174 typedef struct _GstClockClass GstClockClass;
176 /* --- prototype for async callbacks --- */
179 * @clock: The clock that triggered the callback
180 * @time: The time it was triggered
181 * @id: The #GstClockID that expired
182 * @user_data: user data passed in the async_wait call
184 * The function prototype of the callback.
186 * Returns: %TRUE or %FALSE (currently unused)
188 typedef gboolean (*GstClockCallback) (GstClock *clock, GstClockTime time,
189 GstClockID id, gpointer user_data);
192 * @GST_CLOCK_OK: The operation succeded.
193 * @GST_CLOCK_EARLY: The operation was scheduled too late.
194 * @GST_CLOCK_UNSCHEDULED: The clockID was unscheduled
195 * @GST_CLOCK_BUSY: The ClockID is busy
196 * @GST_CLOCK_BADTIME: A bad time was provided to a function.
197 * @GST_CLOCK_ERROR: An error occured
198 * @GST_CLOCK_UNSUPPORTED: Operation is not supported
200 * The return value of a clock operation.
206 GST_CLOCK_UNSCHEDULED = 2,
208 GST_CLOCK_BADTIME = 4,
210 GST_CLOCK_UNSUPPORTED = 6,
215 * @GST_CLOCK_ENTRY_SINGLE: a single shot timeout
216 * @GST_CLOCK_ENTRY_PERIODIC: a periodic timeout request
218 * The type of the clock entry
221 GST_CLOCK_ENTRY_SINGLE,
222 GST_CLOCK_ENTRY_PERIODIC
227 * @entry: the entry to cast
229 * Cast to a clock entry
231 #define GST_CLOCK_ENTRY(entry) ((GstClockEntry *)(entry))
233 * GST_CLOCK_ENTRY_CLOCK:
234 * @entry: the entry to query
236 * Get the owner clock of the entry
238 #define GST_CLOCK_ENTRY_CLOCK(entry) ((entry)->clock)
240 * GST_CLOCK_ENTRY_TYPE:
241 * @entry: the entry to query
243 * Get the type of the clock entry
245 #define GST_CLOCK_ENTRY_TYPE(entry) ((entry)->type)
247 * GST_CLOCK_ENTRY_TIME:
248 * @entry: the entry to query
250 * Get the requested time of this entry
252 #define GST_CLOCK_ENTRY_TIME(entry) ((entry)->time)
254 * GST_CLOCK_ENTRY_INTERVAL:
255 * @entry: the entry to query
257 * Get the interval of this periodic entry
259 #define GST_CLOCK_ENTRY_INTERVAL(entry) ((entry)->interval)
261 * GST_CLOCK_ENTRY_STATUS:
262 * @entry: the entry to query
264 * The status of the entry
266 #define GST_CLOCK_ENTRY_STATUS(entry) ((entry)->status)
270 * @refcount: reference counter (read-only)
272 * All pending timeouts or periodic notifies are converted into
275 struct _GstClockEntry {
279 GstClockEntryType type;
281 GstClockTime interval;
282 GstClockReturn status;
283 GstClockCallback func;
289 * @GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC: clock can do a single sync timeout request
290 * @GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC: clock can do a single async timeout request
291 * @GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC: clock can do sync periodic timeout requests
292 * @GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC: clock can do async periodic timeout callbacks
293 * @GST_CLOCK_FLAG_CAN_SET_RESOLUTION: clock's resolution can be changed
295 * The capabilities of this clock
298 GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC = (1 << 1),
299 GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC = (1 << 2),
300 GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC = (1 << 3),
301 GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC = (1 << 4),
302 GST_CLOCK_FLAG_CAN_SET_RESOLUTION = (1 << 5),
307 * @clock: the clock to query
309 * Gets the #GstClockFlags clock flags.
311 #define GST_CLOCK_FLAGS(clock) (GST_CLOCK(clock)->flags)
313 #define GST_CLOCK_COND(clock) (GST_CLOCK_CAST(clock)->entries_changed)
314 #define GST_CLOCK_WAIT(clock) g_cond_wait(GST_CLOCK_COND(clock),GST_GET_LOCK(clock))
315 #define GST_CLOCK_TIMED_WAIT(clock,tv) g_cond_timed_wait(GST_CLOCK_COND(clock),GST_GET_LOCK(clock),tv)
316 #define GST_CLOCK_BROADCAST(clock) g_cond_broadcast(GST_CLOCK_COND(clock))
324 /*< protected >*/ /* with LOCK */
326 GstClockTime last_time;
328 GCond *entries_changed;
334 gpointer _gst_reserved[GST_PADDING];
337 struct _GstClockClass {
338 GstObjectClass parent_class;
342 guint64 (*change_resolution) (GstClock *clock, guint64 old_resolution,
343 guint64 new_resolution);
344 guint64 (*get_resolution) (GstClock *clock);
346 GstClockTime (*get_internal_time) (GstClock *clock);
348 /* waiting on an ID */
349 GstClockReturn (*wait) (GstClock *clock, GstClockEntry *entry);
350 GstClockReturn (*wait_async) (GstClock *clock, GstClockEntry *entry);
351 void (*unschedule) (GstClock *clock, GstClockEntry *entry);
354 gpointer _gst_reserved[GST_PADDING];
357 GType gst_clock_get_type (void);
359 guint64 gst_clock_set_resolution (GstClock *clock, guint64 resolution);
360 guint64 gst_clock_get_resolution (GstClock *clock);
362 GstClockTime gst_clock_get_time (GstClock *clock);
363 void gst_clock_set_time_adjust (GstClock *clock, GstClockTime adjust);
365 GstClockTime gst_clock_adjust_unlocked (GstClock *clock, GstClockTime internal);
368 /* creating IDs that can be used to get notifications */
369 GstClockID gst_clock_new_single_shot_id (GstClock *clock,
371 GstClockID gst_clock_new_periodic_id (GstClock *clock,
372 GstClockTime start_time,
373 GstClockTime interval);
375 /* reference counting */
376 GstClockID gst_clock_id_ref (GstClockID id);
377 void gst_clock_id_unref (GstClockID id);
379 /* operations on IDs */
380 gint gst_clock_id_compare_func (gconstpointer id1, gconstpointer id2);
382 GstClockTime gst_clock_id_get_time (GstClockID id);
383 GstClockReturn gst_clock_id_wait (GstClockID id,
384 GstClockTimeDiff *jitter);
385 GstClockReturn gst_clock_id_wait_async (GstClockID id,
386 GstClockCallback func,
388 void gst_clock_id_unschedule (GstClockID id);
392 #endif /* __GST_CLOCK_H__ */