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 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 #define GST_CLOCK_SLAVE_LOCK(clock)     g_mutex_lock (&GST_CLOCK_CAST (clock)->slave_lock)
39 #define GST_CLOCK_SLAVE_UNLOCK(clock)   g_mutex_unlock (&GST_CLOCK_CAST (clock)->slave_lock)
40
41 /**
42  * GstClockTime:
43  *
44  * A datatype to hold a time, measured in nanoseconds.
45  */
46 typedef guint64 GstClockTime;
47
48 /**
49  * GST_TYPE_CLOCK_TIME:
50  *
51  * The #GType of a #GstClockTime.
52  */
53 #define GST_TYPE_CLOCK_TIME G_TYPE_UINT64
54
55 /**
56  * GstClockTimeDiff:
57  *
58  * A datatype to hold a time difference, measured in nanoseconds.
59  */
60 typedef gint64 GstClockTimeDiff;
61 /**
62  * GstClockID:
63  *
64  * A datatype to hold the handle to an outstanding sync or async clock callback.
65  */
66 typedef gpointer GstClockID;
67
68 /**
69  * GST_CLOCK_TIME_NONE:
70  *
71  * Constant to define an undefined clock time.
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  *
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  *
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  *
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  *
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   (tv).tv_sec  = ((GstClockTime) (t)) / GST_SECOND;             \
190   (tv).tv_usec = (((GstClockTime) (t)) -                        \
191                   ((GstClockTime) (tv).tv_sec) * GST_SECOND)    \
192                  / GST_USECOND;                                 \
193 } G_STMT_END
194
195 /**
196  * GST_TIMESPEC_TO_TIME:
197  * @ts: the timespec to convert
198  *
199  * Convert a struct timespec (see man pselect) to a #GstClockTime.
200  */
201 #define GST_TIMESPEC_TO_TIME(ts)        (GstClockTime)((ts).tv_sec * GST_SECOND + (ts).tv_nsec * GST_NSECOND)
202 /**
203  * GST_TIME_TO_TIMESPEC:
204  * @t: The #GstClockTime to convert
205  * @ts: The target timespec
206  *
207  * Convert a #GstClockTime to a struct timespec (see man pselect)
208  */
209 #define GST_TIME_TO_TIMESPEC(t,ts)                      \
210 G_STMT_START {                                          \
211   (ts).tv_sec  =  (t) / GST_SECOND;                     \
212   (ts).tv_nsec = ((t) - (ts).tv_sec * GST_SECOND) / GST_NSECOND;        \
213 } G_STMT_END
214
215 /* timestamp debugging macros */
216 /**
217  * GST_TIME_FORMAT:
218  *
219  * A string that can be used in printf-like format strings to display a
220  * #GstClockTime value in h:m:s format.  Use GST_TIME_ARGS() to construct
221  * the matching arguments.
222  *
223  * Example:
224  * |[
225  * printf("%" GST_TIME_FORMAT "\n", GST_TIME_ARGS(ts));
226  * ]|
227  */
228 #define GST_TIME_FORMAT "u:%02u:%02u.%09u"
229 /**
230  * GST_TIME_ARGS:
231  * @t: a #GstClockTime
232  *
233  * Format @t for the #GST_TIME_FORMAT format string. Note: @t will be
234  * evaluated more than once.
235  */
236 #define GST_TIME_ARGS(t) \
237         GST_CLOCK_TIME_IS_VALID (t) ? \
238         (guint) (((GstClockTime)(t)) / (GST_SECOND * 60 * 60)) : 99, \
239         GST_CLOCK_TIME_IS_VALID (t) ? \
240         (guint) ((((GstClockTime)(t)) / (GST_SECOND * 60)) % 60) : 99, \
241         GST_CLOCK_TIME_IS_VALID (t) ? \
242         (guint) ((((GstClockTime)(t)) / GST_SECOND) % 60) : 99, \
243         GST_CLOCK_TIME_IS_VALID (t) ? \
244         (guint) (((GstClockTime)(t)) % GST_SECOND) : 999999999
245
246 /**
247  * GST_CLOCK_ENTRY_TRACE_NAME:
248  *
249  * The name used for tracing clock entry allocations.
250  */
251 #define GST_CLOCK_ENTRY_TRACE_NAME "GstClockEntry"
252
253 typedef struct _GstClockEntry   GstClockEntry;
254 typedef struct _GstClock        GstClock;
255 typedef struct _GstClockClass   GstClockClass;
256 typedef struct _GstClockPrivate GstClockPrivate;
257
258 /* --- prototype for async callbacks --- */
259 /**
260  * GstClockCallback:
261  * @clock: The clock that triggered the callback
262  * @time: The time it was triggered
263  * @id: The #GstClockID that expired
264  * @user_data: user data passed in the gst_clock_id_wait_async() function
265  *
266  * The function prototype of the callback.
267  *
268  * Returns: %TRUE or %FALSE (currently unused)
269  */
270 typedef gboolean        (*GstClockCallback)     (GstClock *clock, GstClockTime time,
271                                                  GstClockID id, gpointer user_data);
272 /**
273  * GstClockReturn:
274  * @GST_CLOCK_OK: The operation succeeded.
275  * @GST_CLOCK_EARLY: The operation was scheduled too late.
276  * @GST_CLOCK_UNSCHEDULED: The clockID was unscheduled
277  * @GST_CLOCK_BUSY: The ClockID is busy
278  * @GST_CLOCK_BADTIME: A bad time was provided to a function.
279  * @GST_CLOCK_ERROR: An error occurred
280  * @GST_CLOCK_UNSUPPORTED: Operation is not supported
281  * @GST_CLOCK_DONE: The ClockID is done waiting (Since: 0.10.32)
282  *
283  * The return value of a clock operation.
284  */
285 typedef enum
286 {
287   GST_CLOCK_OK          =  0,
288   GST_CLOCK_EARLY       =  1,
289   GST_CLOCK_UNSCHEDULED =  2,
290   GST_CLOCK_BUSY        =  3,
291   GST_CLOCK_BADTIME     =  4,
292   GST_CLOCK_ERROR       =  5,
293   GST_CLOCK_UNSUPPORTED =  6,
294   GST_CLOCK_DONE        =  7
295 } GstClockReturn;
296
297 /**
298  * GstClockEntryType:
299  * @GST_CLOCK_ENTRY_SINGLE: a single shot timeout
300  * @GST_CLOCK_ENTRY_PERIODIC: a periodic timeout request
301  *
302  * The type of the clock entry
303  */
304 typedef enum {
305   GST_CLOCK_ENTRY_SINGLE,
306   GST_CLOCK_ENTRY_PERIODIC
307 } GstClockEntryType;
308
309 /**
310  * GST_CLOCK_ENTRY:
311  * @entry: the entry to cast
312  *
313  * Cast to a clock entry
314  */
315 #define GST_CLOCK_ENTRY(entry)          ((GstClockEntry *)(entry))
316 /**
317  * GST_CLOCK_ENTRY_CLOCK:
318  * @entry: the entry to query
319  *
320  * Get the owner clock of the entry
321  */
322 #define GST_CLOCK_ENTRY_CLOCK(entry)    ((entry)->clock)
323 /**
324  * GST_CLOCK_ENTRY_TYPE:
325  * @entry: the entry to query
326  *
327  * Get the type of the clock entry
328  */
329 #define GST_CLOCK_ENTRY_TYPE(entry)     ((entry)->type)
330 /**
331  * GST_CLOCK_ENTRY_TIME:
332  * @entry: the entry to query
333  *
334  * Get the requested time of this entry
335  */
336 #define GST_CLOCK_ENTRY_TIME(entry)     ((entry)->time)
337 /**
338  * GST_CLOCK_ENTRY_INTERVAL:
339  * @entry: the entry to query
340  *
341  * Get the interval of this periodic entry
342  */
343 #define GST_CLOCK_ENTRY_INTERVAL(entry) ((entry)->interval)
344 /**
345  * GST_CLOCK_ENTRY_STATUS:
346  * @entry: the entry to query
347  *
348  * The status of the entry
349  */
350 #define GST_CLOCK_ENTRY_STATUS(entry)   ((entry)->status)
351
352 /**
353  * GstClockEntry:
354  * @refcount: reference counter (read-only)
355  *
356  * All pending timeouts or periodic notifies are converted into
357  * an entry.
358  * Note that GstClockEntry should be treated as an opaque structure. It must
359  * not be extended or allocated using a custom allocator.
360  */
361 struct _GstClockEntry {
362   gint                  refcount;
363   /*< protected >*/
364   GstClock              *clock;
365   GstClockEntryType      type;
366   GstClockTime           time;
367   GstClockTime           interval;
368   GstClockReturn         status;
369   GstClockCallback       func;
370   gpointer               user_data;
371   GDestroyNotify         destroy_data;
372   gboolean               unscheduled;
373   gboolean               woken_up;
374
375   gpointer _gst_reserved[GST_PADDING];
376 };
377
378 #include <gst/gstobject.h>
379
380 /**
381  * GstClockFlags:
382  * @GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC: clock can do a single sync timeout request
383  * @GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC: clock can do a single async timeout request
384  * @GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC: clock can do sync periodic timeout requests
385  * @GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC: clock can do async periodic timeout callbacks
386  * @GST_CLOCK_FLAG_CAN_SET_RESOLUTION: clock's resolution can be changed
387  * @GST_CLOCK_FLAG_CAN_SET_MASTER: clock can be slaved to a master clock
388  * @GST_CLOCK_FLAG_LAST: subclasses can add additional flags starting from this flag
389  *
390  * The capabilities of this clock
391  */
392 typedef enum {
393   GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC     = (GST_OBJECT_FLAG_LAST << 0),
394   GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC    = (GST_OBJECT_FLAG_LAST << 1),
395   GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC   = (GST_OBJECT_FLAG_LAST << 2),
396   GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC  = (GST_OBJECT_FLAG_LAST << 3),
397   GST_CLOCK_FLAG_CAN_SET_RESOLUTION     = (GST_OBJECT_FLAG_LAST << 4),
398   GST_CLOCK_FLAG_CAN_SET_MASTER         = (GST_OBJECT_FLAG_LAST << 5),
399   /* padding */
400   GST_CLOCK_FLAG_LAST                   = (GST_OBJECT_FLAG_LAST << 8)
401 } GstClockFlags;
402
403 /**
404  * GST_CLOCK_FLAGS:
405  * @clock: the clock to query
406  *
407  * Gets the #GstClockFlags clock flags.
408  */
409 #define GST_CLOCK_FLAGS(clock)  GST_OBJECT_FLAGS(clock)
410
411 /**
412  * GST_CLOCK_GET_COND:
413  * @clock: the clock to query
414  *
415  * Gets the #GCond that gets signalled when the entries of the clock
416  * changed.
417  */
418 #define GST_CLOCK_GET_COND(clock)        (&GST_CLOCK_CAST(clock)->entries_changed)
419 /**
420  * GST_CLOCK_WAIT:
421  * @clock: the clock to wait on
422  *
423  * Wait on the clock until the entries changed.
424  */
425 #define GST_CLOCK_WAIT(clock)            g_cond_wait(GST_CLOCK_GET_COND(clock),GST_OBJECT_GET_LOCK(clock))
426 /**
427  * GST_CLOCK_TIMED_WAIT:
428  * @clock: the clock to wait on
429  * @tv: a #GTimeVal to wait.
430  *
431  * Wait on the clock until the entries changed or the specified timeout
432  * occurred.
433  */
434 #define GST_CLOCK_TIMED_WAIT(clock,tv)   g_cond_timed_wait(GST_CLOCK_GET_COND(clock),GST_OBJECT_GET_LOCK(clock),tv)
435 /**
436  * GST_CLOCK_BROADCAST:
437  * @clock: the clock to broadcast
438  *
439  * Signal that the entries in the clock have changed.
440  */
441 #define GST_CLOCK_BROADCAST(clock)       g_cond_broadcast(GST_CLOCK_GET_COND(clock))
442
443 /**
444  * GstClock:
445  *
446  * #GstClock base structure. The values of this structure are
447  * protected for subclasses, use the methods to use the #GstClock.
448  */
449 struct _GstClock {
450   GstObject      object;
451
452   GMutex         slave_lock; /* order: SLAVE_LOCK, OBJECT_LOCK */
453
454   /*< protected >*/ /* with LOCK */
455   GstClockTime   internal_calibration;
456   GstClockTime   external_calibration;
457   GstClockTime   rate_numerator;
458   GstClockTime   rate_denominator;
459   GstClockTime   last_time;
460   GList         *entries;
461   GCond          entries_changed;
462
463   /*< private >*/ /* with LOCK */
464   GstClockTime   resolution;
465   gboolean       stats;
466
467   /* for master/slave clocks */
468   GstClock      *master;
469
470   /* with SLAVE_LOCK */
471   gboolean       filling;
472   gint           window_size;
473   gint           window_threshold;
474   gint           time_index;
475   GstClockTime   timeout;
476   GstClockTime  *times;
477   GstClockID     clockid;
478
479   /*< private >*/
480   GstClockPrivate *priv;
481
482   gpointer _gst_reserved[GST_PADDING];
483 };
484
485 /**
486  * GstClockClass:
487  * @parent_class: the parent class structure
488  * @change_resolution: change the resolution of the clock. Not all values might
489  *                     be acceptable. The new resolution should be returned.
490  * @get_resolution: get the resolution of the clock.
491  * @get_internal_time: get the internal unadjusted time of the clock.
492  *        implement @wait_jitter instead.
493  * @wait: perform a blocking wait on the given #GstClockEntry and return
494  *               the jitter.
495  * @wait_async: perform an asynchronous wait for the given #GstClockEntry.
496  * @unschedule: unblock a blocking or async wait operation.
497  *
498  * GStreamer clock class. Override the vmethods to implement the clock
499  * functionality.
500  */
501 struct _GstClockClass {
502   GstObjectClass        parent_class;
503
504   /*< public >*/
505   /* vtable */
506   GstClockTime          (*change_resolution)    (GstClock *clock,
507                                                  GstClockTime old_resolution,
508                                                  GstClockTime new_resolution);
509   GstClockTime          (*get_resolution)       (GstClock *clock);
510
511   GstClockTime          (*get_internal_time)    (GstClock *clock);
512
513   /* waiting on an ID */
514   GstClockReturn        (*wait)                 (GstClock *clock, GstClockEntry *entry,
515                                                  GstClockTimeDiff *jitter);
516   GstClockReturn        (*wait_async)           (GstClock *clock, GstClockEntry *entry);
517   void                  (*unschedule)           (GstClock *clock, GstClockEntry *entry);
518
519   /*< private >*/
520   gpointer _gst_reserved[GST_PADDING];
521 };
522
523 GType                   gst_clock_get_type              (void);
524
525 GstClockTime            gst_clock_set_resolution        (GstClock *clock,
526                                                          GstClockTime resolution);
527 GstClockTime            gst_clock_get_resolution        (GstClock *clock);
528
529 GstClockTime            gst_clock_get_time              (GstClock *clock);
530 void                    gst_clock_set_calibration       (GstClock *clock, GstClockTime internal,
531                                                          GstClockTime external,
532                                                          GstClockTime rate_num,
533                                                          GstClockTime rate_denom);
534 void                    gst_clock_get_calibration       (GstClock *clock, GstClockTime *internal,
535                                                          GstClockTime *external,
536                                                          GstClockTime *rate_num,
537                                                          GstClockTime *rate_denom);
538
539 /* master/slave clocks */
540 gboolean                gst_clock_set_master            (GstClock *clock, GstClock *master);
541 GstClock*               gst_clock_get_master            (GstClock *clock);
542 gboolean                gst_clock_add_observation       (GstClock *clock, GstClockTime slave,
543                                                          GstClockTime master, gdouble *r_squared);
544
545
546 /* getting and adjusting internal/external time */
547 GstClockTime            gst_clock_get_internal_time     (GstClock *clock);
548 GstClockTime            gst_clock_adjust_unlocked       (GstClock *clock, GstClockTime internal);
549 GstClockTime            gst_clock_unadjust_unlocked     (GstClock * clock, GstClockTime external);
550
551
552 /* creating IDs that can be used to get notifications */
553 GstClockID              gst_clock_new_single_shot_id    (GstClock *clock,
554                                                          GstClockTime time);
555 GstClockID              gst_clock_new_periodic_id       (GstClock *clock,
556                                                          GstClockTime start_time,
557                                                          GstClockTime interval);
558
559 /* reference counting */
560 GstClockID              gst_clock_id_ref                (GstClockID id);
561 void                    gst_clock_id_unref              (GstClockID id);
562
563 /* operations on IDs */
564 gint                    gst_clock_id_compare_func       (gconstpointer id1, gconstpointer id2);
565
566 GstClockTime            gst_clock_id_get_time           (GstClockID id);
567 GstClockReturn          gst_clock_id_wait               (GstClockID id,
568                                                          GstClockTimeDiff *jitter);
569 GstClockReturn          gst_clock_id_wait_async         (GstClockID id,
570                                                          GstClockCallback func,
571                                                          gpointer user_data);
572 GstClockReturn          gst_clock_id_wait_async_full    (GstClockID id,
573                                                          GstClockCallback func,
574                                                          gpointer user_data,
575                                                          GDestroyNotify destroy_data);
576 void                    gst_clock_id_unschedule         (GstClockID id);
577
578 gboolean                gst_clock_single_shot_id_reinit (GstClock * clock,
579                                                          GstClockID id,
580                                                          GstClockTime time);
581 gboolean                gst_clock_periodic_id_reinit    (GstClock * clock,
582                                                          GstClockID id,
583                                                          GstClockTime start_time,
584                                                          GstClockTime interval);
585
586 G_END_DECLS
587
588 #endif /* __GST_CLOCK_H__ */