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