Merge branch 'master' into 0.11
[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 #include <gst/gstobject.h>
28
29 G_BEGIN_DECLS
30
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))
39
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)
42
43 /**
44  * GstClockTime:
45  *
46  * A datatype to hold a time, measured in nanoseconds.
47  */
48 typedef guint64 GstClockTime;
49
50 /**
51  * GST_TYPE_CLOCK_TIME:
52  *
53  * The #GType of a #GstClockTime.
54  */
55 #define GST_TYPE_CLOCK_TIME G_TYPE_UINT64
56
57 /**
58  * GstClockTimeDiff:
59  *
60  * A datatype to hold a time difference, measured in nanoseconds.
61  */
62 typedef gint64 GstClockTimeDiff;
63 /**
64  * GstClockID:
65  *
66  * A datatype to hold the handle to an outstanding sync or async clock callback.
67  */
68 typedef gpointer GstClockID;
69
70 /**
71  * GST_CLOCK_TIME_NONE:
72  *
73  * Constant to define an undefined clock time.
74  */
75 #define GST_CLOCK_TIME_NONE             ((GstClockTime) -1)
76 /**
77  * GST_CLOCK_TIME_IS_VALID:
78  * @time: clock time to validate
79  *
80  * Tests if a given #GstClockTime represents a valid defined time.
81  */
82 #define GST_CLOCK_TIME_IS_VALID(time)   (((GstClockTime)(time)) != GST_CLOCK_TIME_NONE)
83
84 /**
85  * GST_SECOND:
86  *
87  * Constant that defines one GStreamer second.
88  */
89 #define GST_SECOND  (G_USEC_PER_SEC * G_GINT64_CONSTANT (1000))
90 /**
91  * GST_MSECOND:
92  *
93  * Constant that defines one GStreamer millisecond.
94  */
95 #define GST_MSECOND (GST_SECOND / G_GINT64_CONSTANT (1000))
96 /**
97  * GST_USECOND:
98  *
99  * Constant that defines one GStreamer microsecond.
100  */
101 #define GST_USECOND (GST_SECOND / G_GINT64_CONSTANT (1000000))
102 /**
103  * GST_NSECOND:
104  *
105  * Constant that defines one GStreamer nanosecond
106  */
107 #define GST_NSECOND (GST_SECOND / G_GINT64_CONSTANT (1000000000))
108
109
110 /**
111  * GST_TIME_AS_SECONDS:
112  * @time: the time
113  *
114  * Convert a #GstClockTime to seconds.
115  *
116  * Since: 0.10.16
117  */
118 #define GST_TIME_AS_SECONDS(time)  ((time) / GST_SECOND)
119 /**
120  * GST_TIME_AS_MSECONDS:
121  * @time: the time
122  *
123  * Convert a #GstClockTime to milliseconds (1/1000 of a second).
124  *
125  * Since: 0.10.16
126  */
127 #define GST_TIME_AS_MSECONDS(time) ((time) / G_GINT64_CONSTANT (1000000))
128 /**
129  * GST_TIME_AS_USECONDS:
130  * @time: the time
131  *
132  * Convert a #GstClockTime to microseconds (1/1000000 of a second).
133  *
134  * Since: 0.10.16
135  */
136 #define GST_TIME_AS_USECONDS(time) ((time) / G_GINT64_CONSTANT (1000))
137 /**
138  * GST_TIME_AS_NSECONDS:
139  * @time: the time
140  *
141  * Convert a #GstClockTime to nanoseconds (1/1000000000 of a second).
142  *
143  * Since: 0.10.16
144  */
145 #define GST_TIME_AS_NSECONDS(time) (time)
146
147 /**
148  * GST_CLOCK_DIFF:
149  * @s: the first time
150  * @e: the second time
151  *
152  * Calculate a difference between two clock times as a #GstClockTimeDiff.
153  * The difference is calculated as @e - @s.
154  */
155 #define GST_CLOCK_DIFF(s, e)            (GstClockTimeDiff)((e) - (s))
156
157 /**
158  * GST_TIMEVAL_TO_TIME:
159  * @tv: the timeval to convert
160  *
161  * Convert a #GTimeVal to a #GstClockTime.
162  */
163 #define GST_TIMEVAL_TO_TIME(tv)         (GstClockTime)((tv).tv_sec * GST_SECOND + (tv).tv_usec * GST_USECOND)
164
165 /**
166  * GST_TIME_TO_TIMEVAL:
167  * @t: The #GstClockTime to convert
168  * @tv: The target timeval
169  *
170  * Convert a #GstClockTime to a #GTimeVal
171  *
172  * <note>on 32-bit systems, a timeval has a range of only 2^32 - 1 seconds,
173  * which is about 68 years.  Expect trouble if you want to schedule stuff
174  * in your pipeline for 2038.</note>
175  */
176 #define GST_TIME_TO_TIMEVAL(t,tv)                               \
177 G_STMT_START {                                                  \
178   (tv).tv_sec  = ((GstClockTime) (t)) / GST_SECOND;             \
179   (tv).tv_usec = (((GstClockTime) (t)) -                        \
180                   ((GstClockTime) (tv).tv_sec) * GST_SECOND)    \
181                  / GST_USECOND;                                 \
182 } G_STMT_END
183
184 /**
185  * GST_TIMESPEC_TO_TIME:
186  * @ts: the timespec to convert
187  *
188  * Convert a struct timespec (see man pselect) to a #GstClockTime.
189  */
190 #define GST_TIMESPEC_TO_TIME(ts)        (GstClockTime)((ts).tv_sec * GST_SECOND + (ts).tv_nsec * GST_NSECOND)
191 /**
192  * GST_TIME_TO_TIMESPEC:
193  * @t: The #GstClockTime to convert
194  * @ts: The target timespec
195  *
196  * Convert a #GstClockTime to a struct timespec (see man pselect)
197  */
198 #define GST_TIME_TO_TIMESPEC(t,ts)                      \
199 G_STMT_START {                                          \
200   (ts).tv_sec  =  (t) / GST_SECOND;                     \
201   (ts).tv_nsec = ((t) - (ts).tv_sec * GST_SECOND) / GST_NSECOND;        \
202 } G_STMT_END
203
204 /* timestamp debugging macros */
205 /**
206  * GST_TIME_FORMAT:
207  *
208  * A string that can be used in printf-like format strings to display a
209  * #GstClockTime value in h:m:s format.  Use GST_TIME_ARGS() to construct
210  * the matching arguments.
211  *
212  * Example:
213  * |[
214  * printf("%" GST_TIME_FORMAT "\n", GST_TIME_ARGS(ts));
215  * ]|
216  */
217 #define GST_TIME_FORMAT "u:%02u:%02u.%09u"
218 /**
219  * GST_TIME_ARGS:
220  * @t: a #GstClockTime
221  *
222  * Format @t for the #GST_TIME_FORMAT format string. Note: @t will be
223  * evaluated more than once.
224  */
225 #define GST_TIME_ARGS(t) \
226         GST_CLOCK_TIME_IS_VALID (t) ? \
227         (guint) (((GstClockTime)(t)) / (GST_SECOND * 60 * 60)) : 99, \
228         GST_CLOCK_TIME_IS_VALID (t) ? \
229         (guint) ((((GstClockTime)(t)) / (GST_SECOND * 60)) % 60) : 99, \
230         GST_CLOCK_TIME_IS_VALID (t) ? \
231         (guint) ((((GstClockTime)(t)) / GST_SECOND) % 60) : 99, \
232         GST_CLOCK_TIME_IS_VALID (t) ? \
233         (guint) (((GstClockTime)(t)) % GST_SECOND) : 999999999
234
235 /**
236  * GST_CLOCK_ENTRY_TRACE_NAME:
237  *
238  * The name used for tracing clock entry allocations.
239  */
240 #define GST_CLOCK_ENTRY_TRACE_NAME "GstClockEntry"
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 (Since: 0.10.32)
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
365 /**
366  * GstClockFlags:
367  * @GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC: clock can do a single sync timeout request
368  * @GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC: clock can do a single async timeout request
369  * @GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC: clock can do sync periodic timeout requests
370  * @GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC: clock can do async periodic timeout callbacks
371  * @GST_CLOCK_FLAG_CAN_SET_RESOLUTION: clock's resolution can be changed
372  * @GST_CLOCK_FLAG_CAN_SET_MASTER: clock can be slaved to a master clock
373  * @GST_CLOCK_FLAG_LAST: subclasses can add additional flags starting from this flag
374  *
375  * The capabilities of this clock
376  */
377 typedef enum {
378   GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC     = (GST_OBJECT_FLAG_LAST << 0),
379   GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC    = (GST_OBJECT_FLAG_LAST << 1),
380   GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC   = (GST_OBJECT_FLAG_LAST << 2),
381   GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC  = (GST_OBJECT_FLAG_LAST << 3),
382   GST_CLOCK_FLAG_CAN_SET_RESOLUTION     = (GST_OBJECT_FLAG_LAST << 4),
383   GST_CLOCK_FLAG_CAN_SET_MASTER         = (GST_OBJECT_FLAG_LAST << 5),
384   /* padding */
385   GST_CLOCK_FLAG_LAST                   = (GST_OBJECT_FLAG_LAST << 8)
386 } GstClockFlags;
387
388 /**
389  * GST_CLOCK_FLAGS:
390  * @clock: the clock to query
391  *
392  * Gets the #GstClockFlags clock flags.
393  */
394 #define GST_CLOCK_FLAGS(clock)  GST_OBJECT_FLAGS(clock)
395
396 /**
397  * GST_CLOCK_COND:
398  * @clock: the clock to query
399  *
400  * Gets the #GCond that gets signalled when the entries of the clock
401  * changed.
402  */
403 #define GST_CLOCK_COND(clock)            (GST_CLOCK_CAST(clock)->entries_changed)
404 /**
405  * GST_CLOCK_WAIT:
406  * @clock: the clock to wait on
407  *
408  * Wait on the clock until the entries changed.
409  */
410 #define GST_CLOCK_WAIT(clock)            g_cond_wait(GST_CLOCK_COND(clock),GST_OBJECT_GET_LOCK(clock))
411 /**
412  * GST_CLOCK_TIMED_WAIT:
413  * @clock: the clock to wait on
414  * @tv: a #GTimeVal to wait.
415  *
416  * Wait on the clock until the entries changed or the specified timeout
417  * occurred.
418  */
419 #define GST_CLOCK_TIMED_WAIT(clock,tv)   g_cond_timed_wait(GST_CLOCK_COND(clock),GST_OBJECT_GET_LOCK(clock),tv)
420 /**
421  * GST_CLOCK_BROADCAST:
422  * @clock: the clock to broadcast
423  *
424  * Signal that the entries in the clock have changed.
425  */
426 #define GST_CLOCK_BROADCAST(clock)       g_cond_broadcast(GST_CLOCK_COND(clock))
427
428 /**
429  * GstClock:
430  *
431  * #GstClock base structure. The values of this structure are
432  * protected for subclasses, use the methods to use the #GstClock.
433  */
434 struct _GstClock {
435   GstObject      object;
436
437   GMutex        *slave_lock; /* order: SLAVE_LOCK, OBJECT_LOCK */
438
439   /*< protected >*/ /* with LOCK */
440   GstClockTime   internal_calibration;
441   GstClockTime   external_calibration;
442   GstClockTime   rate_numerator;
443   GstClockTime   rate_denominator;
444   GstClockTime   last_time;
445   GList         *entries;
446   GCond         *entries_changed;
447
448   /*< private >*/ /* with LOCK */
449   GstClockTime   resolution;
450   gboolean       stats;
451
452   /* for master/slave clocks */
453   GstClock      *master;
454
455   /* with SLAVE_LOCK */
456   gboolean       filling;
457   gint           window_size;
458   gint           window_threshold;
459   gint           time_index;
460   GstClockTime   timeout;
461   GstClockTime  *times;
462   GstClockID     clockid;
463
464   /*< private >*/
465   union {
466     GstClockPrivate *priv;
467     GstClockTime     _gst_reserved[GST_PADDING];
468   } ABI;
469 };
470
471 /**
472  * GstClockClass:
473  * @parent_class: the parent class structure
474  * @change_resolution: change the resolution of the clock. Not all values might
475  *                     be acceptable. The new resolution should be returned.
476  * @get_resolution: get the resolution of the clock.
477  * @get_internal_time: get the internal unadjusted time of the clock.
478  *        implement @wait_jitter instead.
479  * @wait: perform a blocking wait on the given #GstClockEntry and return
480  *               the jitter.
481  * @wait_async: perform an asynchronous wait for the given #GstClockEntry.
482  * @unschedule: unblock a blocking or async wait operation.
483  *
484  * GStreamer clock class. Override the vmethods to implement the clock
485  * functionality.
486  */
487 struct _GstClockClass {
488   GstObjectClass        parent_class;
489
490   /*< public >*/
491   /* vtable */
492   GstClockTime          (*change_resolution)    (GstClock *clock,
493                                                  GstClockTime old_resolution,
494                                                  GstClockTime new_resolution);
495   GstClockTime          (*get_resolution)       (GstClock *clock);
496
497   GstClockTime          (*get_internal_time)    (GstClock *clock);
498
499   /* waiting on an ID */
500   GstClockReturn        (*wait)                 (GstClock *clock, GstClockEntry *entry,
501                                                  GstClockTimeDiff *jitter);
502   GstClockReturn        (*wait_async)           (GstClock *clock, GstClockEntry *entry);
503   void                  (*unschedule)           (GstClock *clock, GstClockEntry *entry);
504
505   /*< private >*/
506   gpointer _gst_reserved[GST_PADDING];
507 };
508
509 GType                   gst_clock_get_type              (void);
510
511 GstClockTime            gst_clock_set_resolution        (GstClock *clock,
512                                                          GstClockTime resolution);
513 GstClockTime            gst_clock_get_resolution        (GstClock *clock);
514
515 GstClockTime            gst_clock_get_time              (GstClock *clock);
516 void                    gst_clock_set_calibration       (GstClock *clock, GstClockTime internal,
517                                                          GstClockTime external,
518                                                          GstClockTime rate_num,
519                                                          GstClockTime rate_denom);
520 void                    gst_clock_get_calibration       (GstClock *clock, GstClockTime *internal,
521                                                          GstClockTime *external,
522                                                          GstClockTime *rate_num,
523                                                          GstClockTime *rate_denom);
524
525 /* master/slave clocks */
526 gboolean                gst_clock_set_master            (GstClock *clock, GstClock *master);
527 GstClock*               gst_clock_get_master            (GstClock *clock);
528 gboolean                gst_clock_add_observation       (GstClock *clock, GstClockTime slave,
529                                                          GstClockTime master, gdouble *r_squared);
530
531
532 /* getting and adjusting internal/external time */
533 GstClockTime            gst_clock_get_internal_time     (GstClock *clock);
534 GstClockTime            gst_clock_adjust_unlocked       (GstClock *clock, GstClockTime internal);
535 GstClockTime            gst_clock_unadjust_unlocked     (GstClock * clock, GstClockTime external);
536
537
538 /* creating IDs that can be used to get notifications */
539 GstClockID              gst_clock_new_single_shot_id    (GstClock *clock,
540                                                          GstClockTime time);
541 GstClockID              gst_clock_new_periodic_id       (GstClock *clock,
542                                                          GstClockTime start_time,
543                                                          GstClockTime interval);
544
545 /* reference counting */
546 GstClockID              gst_clock_id_ref                (GstClockID id);
547 void                    gst_clock_id_unref              (GstClockID id);
548
549 /* operations on IDs */
550 gint                    gst_clock_id_compare_func       (gconstpointer id1, gconstpointer id2);
551
552 GstClockTime            gst_clock_id_get_time           (GstClockID id);
553 GstClockReturn          gst_clock_id_wait               (GstClockID id,
554                                                          GstClockTimeDiff *jitter);
555 GstClockReturn          gst_clock_id_wait_async         (GstClockID id,
556                                                          GstClockCallback func,
557                                                          gpointer user_data);
558 GstClockReturn          gst_clock_id_wait_async_full    (GstClockID id,
559                                                          GstClockCallback func,
560                                                          gpointer user_data,
561                                                          GDestroyNotify destroy_data);
562 void                    gst_clock_id_unschedule         (GstClockID id);
563
564 gboolean                gst_clock_single_shot_id_reinit (GstClock * clock,
565                                                          GstClockID id,
566                                                          GstClockTime time);
567 gboolean                gst_clock_periodic_id_reinit    (GstClock * clock,
568                                                          GstClockID id,
569                                                          GstClockTime start_time,
570                                                          GstClockTime interval);
571
572 G_END_DECLS
573
574 #endif /* __GST_CLOCK_H__ */