Fix FSF address
[platform/upstream/gstreamer.git] / gst / gstclock.h
1 /* GStreamer
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>
5  *
6  * gstclock.h: Header for clock subsystem
7  *
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.
12  *
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.
17  *
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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #ifndef __GST_CLOCK_H__
25 #define __GST_CLOCK_H__
26
27 G_BEGIN_DECLS
28
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))
37
38 /**
39  * GstClockTime:
40  *
41  * A datatype to hold a time, measured in nanoseconds.
42  */
43 typedef guint64 GstClockTime;
44
45 /**
46  * GST_TYPE_CLOCK_TIME:
47  *
48  * The #GType of a #GstClockTime.
49  */
50 #define GST_TYPE_CLOCK_TIME G_TYPE_UINT64
51
52 /**
53  * GstClockTimeDiff:
54  *
55  * A datatype to hold a time difference, measured in nanoseconds.
56  */
57 typedef gint64 GstClockTimeDiff;
58 /**
59  * GstClockID:
60  *
61  * A datatype to hold the handle to an outstanding sync or async clock callback.
62  */
63 typedef gpointer GstClockID;
64
65 /**
66  * GST_CLOCK_TIME_NONE:
67  *
68  * Constant to define an undefined clock time.
69  *
70  * Value: 18446744073709551615
71  * Type: GstClockTime
72  */
73 #define GST_CLOCK_TIME_NONE             ((GstClockTime) -1)
74 /**
75  * GST_CLOCK_TIME_IS_VALID:
76  * @time: clock time to validate
77  *
78  * Tests if a given #GstClockTime represents a valid defined time.
79  */
80 #define GST_CLOCK_TIME_IS_VALID(time)   (((GstClockTime)(time)) != GST_CLOCK_TIME_NONE)
81
82 /* FIXME: still need to explicitly force types on the defines below */
83 /**
84  * GST_SECOND:
85  *
86  * Constant that defines one GStreamer second.
87  *
88  * Value: 1000000000
89  * Type: GstClockTime
90  */
91 #define GST_SECOND  (G_USEC_PER_SEC * G_GINT64_CONSTANT (1000))
92 /**
93  * GST_MSECOND:
94  *
95  * Constant that defines one GStreamer millisecond.
96  *
97  * Value: 1000000
98  * Type: GstClockTime
99  */
100 #define GST_MSECOND (GST_SECOND / G_GINT64_CONSTANT (1000))
101 /**
102  * GST_USECOND:
103  *
104  * Constant that defines one GStreamer microsecond.
105  *
106  * Value: 1000
107  * Type: GstClockTime
108  */
109 #define GST_USECOND (GST_SECOND / G_GINT64_CONSTANT (1000000))
110 /**
111  * GST_NSECOND:
112  *
113  * Constant that defines one GStreamer nanosecond
114  *
115  * Value: 1
116  * Type: GstClockTime
117  */
118 #define GST_NSECOND (GST_SECOND / G_GINT64_CONSTANT (1000000000))
119
120
121 /**
122  * GST_TIME_AS_SECONDS:
123  * @time: the time
124  *
125  * Convert a #GstClockTime to seconds.
126  */
127 #define GST_TIME_AS_SECONDS(time)  ((time) / GST_SECOND)
128 /**
129  * GST_TIME_AS_MSECONDS:
130  * @time: the time
131  *
132  * Convert a #GstClockTime to milliseconds (1/1000 of a second).
133  */
134 #define GST_TIME_AS_MSECONDS(time) ((time) / G_GINT64_CONSTANT (1000000))
135 /**
136  * GST_TIME_AS_USECONDS:
137  * @time: the time
138  *
139  * Convert a #GstClockTime to microseconds (1/1000000 of a second).
140  */
141 #define GST_TIME_AS_USECONDS(time) ((time) / G_GINT64_CONSTANT (1000))
142 /**
143  * GST_TIME_AS_NSECONDS:
144  * @time: the time
145  *
146  * Convert a #GstClockTime to nanoseconds (1/1000000000 of a second).
147  */
148 #define GST_TIME_AS_NSECONDS(time) (time)
149
150 /**
151  * GST_CLOCK_DIFF:
152  * @s: the first time
153  * @e: the second time
154  *
155  * Calculate a difference between two clock times as a #GstClockTimeDiff.
156  * The difference is calculated as @e - @s.
157  */
158 #define GST_CLOCK_DIFF(s, e)            (GstClockTimeDiff)((e) - (s))
159
160 /**
161  * GST_TIMEVAL_TO_TIME:
162  * @tv: the timeval to convert
163  *
164  * Convert a #GTimeVal to a #GstClockTime.
165  */
166 #define GST_TIMEVAL_TO_TIME(tv)         (GstClockTime)((tv).tv_sec * GST_SECOND + (tv).tv_usec * GST_USECOND)
167
168 /**
169  * GST_TIME_TO_TIMEVAL:
170  * @t: The #GstClockTime to convert
171  * @tv: The target timeval
172  *
173  * Convert a #GstClockTime to a #GTimeVal
174  *
175  * <note>on 32-bit systems, a timeval has a range of only 2^32 - 1 seconds,
176  * which is about 68 years.  Expect trouble if you want to schedule stuff
177  * in your pipeline for 2038.</note>
178  */
179 #define GST_TIME_TO_TIMEVAL(t,tv)                               \
180 G_STMT_START {                                                  \
181   g_assert ("Value of time " #t " is out of timeval's range" && \
182       ((t) / GST_SECOND) < G_MAXLONG);                          \
183   (tv).tv_sec  = (glong) (((GstClockTime) (t)) / GST_SECOND);   \
184   (tv).tv_usec = (glong) ((((GstClockTime) (t)) -               \
185                   ((GstClockTime) (tv).tv_sec) * GST_SECOND)    \
186                  / GST_USECOND);                                \
187 } G_STMT_END
188
189 /**
190  * GST_TIMESPEC_TO_TIME:
191  * @ts: the timespec to convert
192  *
193  * Convert a struct timespec (see man pselect) to a #GstClockTime.
194  */
195 #define GST_TIMESPEC_TO_TIME(ts)        (GstClockTime)((ts).tv_sec * GST_SECOND + (ts).tv_nsec * GST_NSECOND)
196 /**
197  * GST_TIME_TO_TIMESPEC:
198  * @t: The #GstClockTime to convert
199  * @ts: The target timespec
200  *
201  * Convert a #GstClockTime to a struct timespec (see man pselect)
202  */
203 #define GST_TIME_TO_TIMESPEC(t,ts)                                \
204 G_STMT_START {                                                    \
205   g_assert ("Value of time " #t " is out of timespec's range" &&  \
206       ((t) / GST_SECOND) < G_MAXLONG);                            \
207   (ts).tv_sec  =  (glong) ((t) / GST_SECOND);                     \
208   (ts).tv_nsec = (glong) (((t) - (ts).tv_sec * GST_SECOND) / GST_NSECOND);        \
209 } G_STMT_END
210
211 /* timestamp debugging macros */
212 /**
213  * GST_TIME_FORMAT:
214  *
215  * A string that can be used in printf-like format strings to display a
216  * #GstClockTime value in h:m:s format.  Use GST_TIME_ARGS() to construct
217  * the matching arguments.
218  *
219  * Example:
220  * |[
221  * printf("%" GST_TIME_FORMAT "\n", GST_TIME_ARGS(ts));
222  * ]|
223  */
224 #define GST_TIME_FORMAT "u:%02u:%02u.%09u"
225 /**
226  * GST_TIME_ARGS:
227  * @t: a #GstClockTime
228  *
229  * Format @t for the #GST_TIME_FORMAT format string. Note: @t will be
230  * evaluated more than once.
231  */
232 #define GST_TIME_ARGS(t) \
233         GST_CLOCK_TIME_IS_VALID (t) ? \
234         (guint) (((GstClockTime)(t)) / (GST_SECOND * 60 * 60)) : 99, \
235         GST_CLOCK_TIME_IS_VALID (t) ? \
236         (guint) ((((GstClockTime)(t)) / (GST_SECOND * 60)) % 60) : 99, \
237         GST_CLOCK_TIME_IS_VALID (t) ? \
238         (guint) ((((GstClockTime)(t)) / GST_SECOND) % 60) : 99, \
239         GST_CLOCK_TIME_IS_VALID (t) ? \
240         (guint) (((GstClockTime)(t)) % GST_SECOND) : 999999999
241
242 typedef struct _GstClockEntry   GstClockEntry;
243 typedef struct _GstClock        GstClock;
244 typedef struct _GstClockClass   GstClockClass;
245 typedef struct _GstClockPrivate GstClockPrivate;
246
247 /* --- prototype for async callbacks --- */
248 /**
249  * GstClockCallback:
250  * @clock: The clock that triggered the callback
251  * @time: The time it was triggered
252  * @id: The #GstClockID that expired
253  * @user_data: user data passed in the gst_clock_id_wait_async() function
254  *
255  * The function prototype of the callback.
256  *
257  * Returns: %TRUE or %FALSE (currently unused)
258  */
259 typedef gboolean        (*GstClockCallback)     (GstClock *clock, GstClockTime time,
260                                                  GstClockID id, gpointer user_data);
261 /**
262  * GstClockReturn:
263  * @GST_CLOCK_OK: The operation succeeded.
264  * @GST_CLOCK_EARLY: The operation was scheduled too late.
265  * @GST_CLOCK_UNSCHEDULED: The clockID was unscheduled
266  * @GST_CLOCK_BUSY: The ClockID is busy
267  * @GST_CLOCK_BADTIME: A bad time was provided to a function.
268  * @GST_CLOCK_ERROR: An error occurred
269  * @GST_CLOCK_UNSUPPORTED: Operation is not supported
270  * @GST_CLOCK_DONE: The ClockID is done waiting
271  *
272  * The return value of a clock operation.
273  */
274 typedef enum
275 {
276   GST_CLOCK_OK          =  0,
277   GST_CLOCK_EARLY       =  1,
278   GST_CLOCK_UNSCHEDULED =  2,
279   GST_CLOCK_BUSY        =  3,
280   GST_CLOCK_BADTIME     =  4,
281   GST_CLOCK_ERROR       =  5,
282   GST_CLOCK_UNSUPPORTED =  6,
283   GST_CLOCK_DONE        =  7
284 } GstClockReturn;
285
286 /**
287  * GstClockEntryType:
288  * @GST_CLOCK_ENTRY_SINGLE: a single shot timeout
289  * @GST_CLOCK_ENTRY_PERIODIC: a periodic timeout request
290  *
291  * The type of the clock entry
292  */
293 typedef enum {
294   GST_CLOCK_ENTRY_SINGLE,
295   GST_CLOCK_ENTRY_PERIODIC
296 } GstClockEntryType;
297
298 /**
299  * GST_CLOCK_ENTRY:
300  * @entry: the entry to cast
301  *
302  * Cast to a clock entry
303  */
304 #define GST_CLOCK_ENTRY(entry)          ((GstClockEntry *)(entry))
305 /**
306  * GST_CLOCK_ENTRY_CLOCK:
307  * @entry: the entry to query
308  *
309  * Get the owner clock of the entry
310  */
311 #define GST_CLOCK_ENTRY_CLOCK(entry)    ((entry)->clock)
312 /**
313  * GST_CLOCK_ENTRY_TYPE:
314  * @entry: the entry to query
315  *
316  * Get the type of the clock entry
317  */
318 #define GST_CLOCK_ENTRY_TYPE(entry)     ((entry)->type)
319 /**
320  * GST_CLOCK_ENTRY_TIME:
321  * @entry: the entry to query
322  *
323  * Get the requested time of this entry
324  */
325 #define GST_CLOCK_ENTRY_TIME(entry)     ((entry)->time)
326 /**
327  * GST_CLOCK_ENTRY_INTERVAL:
328  * @entry: the entry to query
329  *
330  * Get the interval of this periodic entry
331  */
332 #define GST_CLOCK_ENTRY_INTERVAL(entry) ((entry)->interval)
333 /**
334  * GST_CLOCK_ENTRY_STATUS:
335  * @entry: the entry to query
336  *
337  * The status of the entry
338  */
339 #define GST_CLOCK_ENTRY_STATUS(entry)   ((entry)->status)
340
341 /**
342  * GstClockEntry:
343  * @refcount: reference counter (read-only)
344  *
345  * All pending timeouts or periodic notifies are converted into
346  * an entry.
347  * Note that GstClockEntry should be treated as an opaque structure. It must
348  * not be extended or allocated using a custom allocator.
349  */
350 struct _GstClockEntry {
351   gint                  refcount;
352   /*< protected >*/
353   GstClock              *clock;
354   GstClockEntryType      type;
355   GstClockTime           time;
356   GstClockTime           interval;
357   GstClockReturn         status;
358   GstClockCallback       func;
359   gpointer               user_data;
360   GDestroyNotify         destroy_data;
361   gboolean               unscheduled;
362   gboolean               woken_up;
363
364   /*< private >*/
365   gpointer _gst_reserved[GST_PADDING];
366 };
367
368 #include <gst/gstobject.h>
369
370 /**
371  * GstClockFlags:
372  * @GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC: clock can do a single sync timeout request
373  * @GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC: clock can do a single async timeout request
374  * @GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC: clock can do sync periodic timeout requests
375  * @GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC: clock can do async periodic timeout callbacks
376  * @GST_CLOCK_FLAG_CAN_SET_RESOLUTION: clock's resolution can be changed
377  * @GST_CLOCK_FLAG_CAN_SET_MASTER: clock can be slaved to a master clock
378  * @GST_CLOCK_FLAG_LAST: subclasses can add additional flags starting from this flag
379  *
380  * The capabilities of this clock
381  */
382 typedef enum {
383   GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC     = (GST_OBJECT_FLAG_LAST << 0),
384   GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC    = (GST_OBJECT_FLAG_LAST << 1),
385   GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC   = (GST_OBJECT_FLAG_LAST << 2),
386   GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC  = (GST_OBJECT_FLAG_LAST << 3),
387   GST_CLOCK_FLAG_CAN_SET_RESOLUTION     = (GST_OBJECT_FLAG_LAST << 4),
388   GST_CLOCK_FLAG_CAN_SET_MASTER         = (GST_OBJECT_FLAG_LAST << 5),
389   /* padding */
390   GST_CLOCK_FLAG_LAST                   = (GST_OBJECT_FLAG_LAST << 8)
391 } GstClockFlags;
392
393 /**
394  * GST_CLOCK_FLAGS:
395  * @clock: the clock to query
396  *
397  * Gets the #GstClockFlags clock flags.
398  */
399 #define GST_CLOCK_FLAGS(clock)  GST_OBJECT_FLAGS(clock)
400
401 /**
402  * GstClock:
403  *
404  * #GstClock base structure. The values of this structure are
405  * protected for subclasses, use the methods to use the #GstClock.
406  */
407 struct _GstClock {
408   GstObject      object;
409
410   /*< private >*/
411   GstClockPrivate *priv;
412
413   gpointer _gst_reserved[GST_PADDING];
414 };
415
416 /**
417  * GstClockClass:
418  * @parent_class: the parent class structure
419  * @change_resolution: change the resolution of the clock. Not all values might
420  *                     be acceptable. The new resolution should be returned.
421  * @get_resolution: get the resolution of the clock.
422  * @get_internal_time: get the internal unadjusted time of the clock.
423  *        implement @wait_jitter instead.
424  * @wait: perform a blocking wait on the given #GstClockEntry and return
425  *               the jitter.
426  * @wait_async: perform an asynchronous wait for the given #GstClockEntry.
427  * @unschedule: unblock a blocking or async wait operation.
428  *
429  * GStreamer clock class. Override the vmethods to implement the clock
430  * functionality.
431  */
432 struct _GstClockClass {
433   GstObjectClass        parent_class;
434
435   /*< public >*/
436   /* vtable */
437   GstClockTime          (*change_resolution)    (GstClock *clock,
438                                                  GstClockTime old_resolution,
439                                                  GstClockTime new_resolution);
440   GstClockTime          (*get_resolution)       (GstClock *clock);
441
442   GstClockTime          (*get_internal_time)    (GstClock *clock);
443
444   /* waiting on an ID */
445   GstClockReturn        (*wait)                 (GstClock *clock, GstClockEntry *entry,
446                                                  GstClockTimeDiff *jitter);
447   GstClockReturn        (*wait_async)           (GstClock *clock, GstClockEntry *entry);
448   void                  (*unschedule)           (GstClock *clock, GstClockEntry *entry);
449
450   /*< private >*/
451   gpointer _gst_reserved[GST_PADDING];
452 };
453
454 GType                   gst_clock_get_type              (void);
455
456 GstClockTime            gst_clock_set_resolution        (GstClock *clock,
457                                                          GstClockTime resolution);
458 GstClockTime            gst_clock_get_resolution        (GstClock *clock);
459
460 GstClockTime            gst_clock_get_time              (GstClock *clock);
461 void                    gst_clock_set_calibration       (GstClock *clock, GstClockTime internal,
462                                                          GstClockTime external,
463                                                          GstClockTime rate_num,
464                                                          GstClockTime rate_denom);
465 void                    gst_clock_get_calibration       (GstClock *clock, GstClockTime *internal,
466                                                          GstClockTime *external,
467                                                          GstClockTime *rate_num,
468                                                          GstClockTime *rate_denom);
469
470 /* master/slave clocks */
471 gboolean                gst_clock_set_master            (GstClock *clock, GstClock *master);
472 GstClock*               gst_clock_get_master            (GstClock *clock);
473
474 void                    gst_clock_set_timeout           (GstClock *clock,
475                                                          GstClockTime timeout);
476 GstClockTime            gst_clock_get_timeout           (GstClock *clock);
477
478 gboolean                gst_clock_add_observation       (GstClock *clock, GstClockTime slave,
479                                                          GstClockTime master, gdouble *r_squared);
480
481
482 /* getting and adjusting internal/external time */
483 GstClockTime            gst_clock_get_internal_time     (GstClock *clock);
484 GstClockTime            gst_clock_adjust_unlocked       (GstClock *clock, GstClockTime internal);
485 GstClockTime            gst_clock_unadjust_unlocked     (GstClock * clock, GstClockTime external);
486
487
488 /* creating IDs that can be used to get notifications */
489 GstClockID              gst_clock_new_single_shot_id    (GstClock *clock,
490                                                          GstClockTime time);
491 GstClockID              gst_clock_new_periodic_id       (GstClock *clock,
492                                                          GstClockTime start_time,
493                                                          GstClockTime interval);
494
495 /* reference counting */
496 GstClockID              gst_clock_id_ref                (GstClockID id);
497 void                    gst_clock_id_unref              (GstClockID id);
498
499 /* operations on IDs */
500 gint                    gst_clock_id_compare_func       (gconstpointer id1, gconstpointer id2);
501
502 GstClockTime            gst_clock_id_get_time           (GstClockID id);
503 GstClockReturn          gst_clock_id_wait               (GstClockID id,
504                                                          GstClockTimeDiff *jitter);
505 GstClockReturn          gst_clock_id_wait_async         (GstClockID id,
506                                                          GstClockCallback func,
507                                                          gpointer user_data,
508                                                          GDestroyNotify destroy_data);
509 void                    gst_clock_id_unschedule         (GstClockID id);
510
511 gboolean                gst_clock_single_shot_id_reinit (GstClock * clock,
512                                                          GstClockID id,
513                                                          GstClockTime time);
514 gboolean                gst_clock_periodic_id_reinit    (GstClock * clock,
515                                                          GstClockID id,
516                                                          GstClockTime start_time,
517                                                          GstClockTime interval);
518
519 G_END_DECLS
520
521 #endif /* __GST_CLOCK_H__ */