clock: remove unimplemented stats property while we can
[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 typedef struct _GstClockEntry   GstClockEntry;
247 typedef struct _GstClock        GstClock;
248 typedef struct _GstClockClass   GstClockClass;
249 typedef struct _GstClockPrivate GstClockPrivate;
250
251 /* --- prototype for async callbacks --- */
252 /**
253  * GstClockCallback:
254  * @clock: The clock that triggered the callback
255  * @time: The time it was triggered
256  * @id: The #GstClockID that expired
257  * @user_data: user data passed in the gst_clock_id_wait_async() function
258  *
259  * The function prototype of the callback.
260  *
261  * Returns: %TRUE or %FALSE (currently unused)
262  */
263 typedef gboolean        (*GstClockCallback)     (GstClock *clock, GstClockTime time,
264                                                  GstClockID id, gpointer user_data);
265 /**
266  * GstClockReturn:
267  * @GST_CLOCK_OK: The operation succeeded.
268  * @GST_CLOCK_EARLY: The operation was scheduled too late.
269  * @GST_CLOCK_UNSCHEDULED: The clockID was unscheduled
270  * @GST_CLOCK_BUSY: The ClockID is busy
271  * @GST_CLOCK_BADTIME: A bad time was provided to a function.
272  * @GST_CLOCK_ERROR: An error occurred
273  * @GST_CLOCK_UNSUPPORTED: Operation is not supported
274  * @GST_CLOCK_DONE: The ClockID is done waiting (Since: 0.10.32)
275  *
276  * The return value of a clock operation.
277  */
278 typedef enum
279 {
280   GST_CLOCK_OK          =  0,
281   GST_CLOCK_EARLY       =  1,
282   GST_CLOCK_UNSCHEDULED =  2,
283   GST_CLOCK_BUSY        =  3,
284   GST_CLOCK_BADTIME     =  4,
285   GST_CLOCK_ERROR       =  5,
286   GST_CLOCK_UNSUPPORTED =  6,
287   GST_CLOCK_DONE        =  7
288 } GstClockReturn;
289
290 /**
291  * GstClockEntryType:
292  * @GST_CLOCK_ENTRY_SINGLE: a single shot timeout
293  * @GST_CLOCK_ENTRY_PERIODIC: a periodic timeout request
294  *
295  * The type of the clock entry
296  */
297 typedef enum {
298   GST_CLOCK_ENTRY_SINGLE,
299   GST_CLOCK_ENTRY_PERIODIC
300 } GstClockEntryType;
301
302 /**
303  * GST_CLOCK_ENTRY:
304  * @entry: the entry to cast
305  *
306  * Cast to a clock entry
307  */
308 #define GST_CLOCK_ENTRY(entry)          ((GstClockEntry *)(entry))
309 /**
310  * GST_CLOCK_ENTRY_CLOCK:
311  * @entry: the entry to query
312  *
313  * Get the owner clock of the entry
314  */
315 #define GST_CLOCK_ENTRY_CLOCK(entry)    ((entry)->clock)
316 /**
317  * GST_CLOCK_ENTRY_TYPE:
318  * @entry: the entry to query
319  *
320  * Get the type of the clock entry
321  */
322 #define GST_CLOCK_ENTRY_TYPE(entry)     ((entry)->type)
323 /**
324  * GST_CLOCK_ENTRY_TIME:
325  * @entry: the entry to query
326  *
327  * Get the requested time of this entry
328  */
329 #define GST_CLOCK_ENTRY_TIME(entry)     ((entry)->time)
330 /**
331  * GST_CLOCK_ENTRY_INTERVAL:
332  * @entry: the entry to query
333  *
334  * Get the interval of this periodic entry
335  */
336 #define GST_CLOCK_ENTRY_INTERVAL(entry) ((entry)->interval)
337 /**
338  * GST_CLOCK_ENTRY_STATUS:
339  * @entry: the entry to query
340  *
341  * The status of the entry
342  */
343 #define GST_CLOCK_ENTRY_STATUS(entry)   ((entry)->status)
344
345 /**
346  * GstClockEntry:
347  * @refcount: reference counter (read-only)
348  *
349  * All pending timeouts or periodic notifies are converted into
350  * an entry.
351  * Note that GstClockEntry should be treated as an opaque structure. It must
352  * not be extended or allocated using a custom allocator.
353  */
354 struct _GstClockEntry {
355   gint                  refcount;
356   /*< protected >*/
357   GstClock              *clock;
358   GstClockEntryType      type;
359   GstClockTime           time;
360   GstClockTime           interval;
361   GstClockReturn         status;
362   GstClockCallback       func;
363   gpointer               user_data;
364   GDestroyNotify         destroy_data;
365   gboolean               unscheduled;
366   gboolean               woken_up;
367
368   gpointer _gst_reserved[GST_PADDING];
369 };
370
371 #include <gst/gstobject.h>
372
373 /**
374  * GstClockFlags:
375  * @GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC: clock can do a single sync timeout request
376  * @GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC: clock can do a single async timeout request
377  * @GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC: clock can do sync periodic timeout requests
378  * @GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC: clock can do async periodic timeout callbacks
379  * @GST_CLOCK_FLAG_CAN_SET_RESOLUTION: clock's resolution can be changed
380  * @GST_CLOCK_FLAG_CAN_SET_MASTER: clock can be slaved to a master clock
381  * @GST_CLOCK_FLAG_LAST: subclasses can add additional flags starting from this flag
382  *
383  * The capabilities of this clock
384  */
385 typedef enum {
386   GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC     = (GST_OBJECT_FLAG_LAST << 0),
387   GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC    = (GST_OBJECT_FLAG_LAST << 1),
388   GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC   = (GST_OBJECT_FLAG_LAST << 2),
389   GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC  = (GST_OBJECT_FLAG_LAST << 3),
390   GST_CLOCK_FLAG_CAN_SET_RESOLUTION     = (GST_OBJECT_FLAG_LAST << 4),
391   GST_CLOCK_FLAG_CAN_SET_MASTER         = (GST_OBJECT_FLAG_LAST << 5),
392   /* padding */
393   GST_CLOCK_FLAG_LAST                   = (GST_OBJECT_FLAG_LAST << 8)
394 } GstClockFlags;
395
396 /**
397  * GST_CLOCK_FLAGS:
398  * @clock: the clock to query
399  *
400  * Gets the #GstClockFlags clock flags.
401  */
402 #define GST_CLOCK_FLAGS(clock)  GST_OBJECT_FLAGS(clock)
403
404 /**
405  * GST_CLOCK_GET_COND:
406  * @clock: the clock to query
407  *
408  * Gets the #GCond that gets signalled when the entries of the clock
409  * changed.
410  */
411 #define GST_CLOCK_GET_COND(clock)        (&GST_CLOCK_CAST(clock)->entries_changed)
412 /**
413  * GST_CLOCK_WAIT:
414  * @clock: the clock to wait on
415  *
416  * Wait on the clock until the entries changed.
417  */
418 #define GST_CLOCK_WAIT(clock)            g_cond_wait(GST_CLOCK_GET_COND(clock),GST_OBJECT_GET_LOCK(clock))
419 /**
420  * GST_CLOCK_TIMED_WAIT:
421  * @clock: the clock to wait on
422  * @tv: a #GTimeVal to wait.
423  *
424  * Wait on the clock until the entries changed or the specified timeout
425  * occurred.
426  */
427 #define GST_CLOCK_TIMED_WAIT(clock,tv)   g_cond_timed_wait(GST_CLOCK_GET_COND(clock),GST_OBJECT_GET_LOCK(clock),tv)
428 /**
429  * GST_CLOCK_BROADCAST:
430  * @clock: the clock to broadcast
431  *
432  * Signal that the entries in the clock have changed.
433  */
434 #define GST_CLOCK_BROADCAST(clock)       g_cond_broadcast(GST_CLOCK_GET_COND(clock))
435
436 /**
437  * GstClock:
438  *
439  * #GstClock base structure. The values of this structure are
440  * protected for subclasses, use the methods to use the #GstClock.
441  */
442 struct _GstClock {
443   GstObject      object;
444
445   GMutex         slave_lock; /* order: SLAVE_LOCK, OBJECT_LOCK */
446
447   /*< protected >*/ /* with LOCK */
448   GstClockTime   internal_calibration;
449   GstClockTime   external_calibration;
450   GstClockTime   rate_numerator;
451   GstClockTime   rate_denominator;
452   GstClockTime   last_time;
453   GList         *entries;
454   GCond          entries_changed;
455
456   /*< private >*/ /* with LOCK */
457   GstClockTime   resolution;
458
459   /* for master/slave clocks */
460   GstClock      *master;
461
462   /* with SLAVE_LOCK */
463   gboolean       filling;
464   gint           window_size;
465   gint           window_threshold;
466   gint           time_index;
467   GstClockTime   timeout;
468   GstClockTime  *times;
469   GstClockID     clockid;
470
471   /*< private >*/
472   GstClockPrivate *priv;
473
474   gpointer _gst_reserved[GST_PADDING];
475 };
476
477 /**
478  * GstClockClass:
479  * @parent_class: the parent class structure
480  * @change_resolution: change the resolution of the clock. Not all values might
481  *                     be acceptable. The new resolution should be returned.
482  * @get_resolution: get the resolution of the clock.
483  * @get_internal_time: get the internal unadjusted time of the clock.
484  *        implement @wait_jitter instead.
485  * @wait: perform a blocking wait on the given #GstClockEntry and return
486  *               the jitter.
487  * @wait_async: perform an asynchronous wait for the given #GstClockEntry.
488  * @unschedule: unblock a blocking or async wait operation.
489  *
490  * GStreamer clock class. Override the vmethods to implement the clock
491  * functionality.
492  */
493 struct _GstClockClass {
494   GstObjectClass        parent_class;
495
496   /*< public >*/
497   /* vtable */
498   GstClockTime          (*change_resolution)    (GstClock *clock,
499                                                  GstClockTime old_resolution,
500                                                  GstClockTime new_resolution);
501   GstClockTime          (*get_resolution)       (GstClock *clock);
502
503   GstClockTime          (*get_internal_time)    (GstClock *clock);
504
505   /* waiting on an ID */
506   GstClockReturn        (*wait)                 (GstClock *clock, GstClockEntry *entry,
507                                                  GstClockTimeDiff *jitter);
508   GstClockReturn        (*wait_async)           (GstClock *clock, GstClockEntry *entry);
509   void                  (*unschedule)           (GstClock *clock, GstClockEntry *entry);
510
511   /*< private >*/
512   gpointer _gst_reserved[GST_PADDING];
513 };
514
515 GType                   gst_clock_get_type              (void);
516
517 GstClockTime            gst_clock_set_resolution        (GstClock *clock,
518                                                          GstClockTime resolution);
519 GstClockTime            gst_clock_get_resolution        (GstClock *clock);
520
521 GstClockTime            gst_clock_get_time              (GstClock *clock);
522 void                    gst_clock_set_calibration       (GstClock *clock, GstClockTime internal,
523                                                          GstClockTime external,
524                                                          GstClockTime rate_num,
525                                                          GstClockTime rate_denom);
526 void                    gst_clock_get_calibration       (GstClock *clock, GstClockTime *internal,
527                                                          GstClockTime *external,
528                                                          GstClockTime *rate_num,
529                                                          GstClockTime *rate_denom);
530
531 /* master/slave clocks */
532 gboolean                gst_clock_set_master            (GstClock *clock, GstClock *master);
533 GstClock*               gst_clock_get_master            (GstClock *clock);
534 gboolean                gst_clock_add_observation       (GstClock *clock, GstClockTime slave,
535                                                          GstClockTime master, gdouble *r_squared);
536
537
538 /* getting and adjusting internal/external time */
539 GstClockTime            gst_clock_get_internal_time     (GstClock *clock);
540 GstClockTime            gst_clock_adjust_unlocked       (GstClock *clock, GstClockTime internal);
541 GstClockTime            gst_clock_unadjust_unlocked     (GstClock * clock, GstClockTime external);
542
543
544 /* creating IDs that can be used to get notifications */
545 GstClockID              gst_clock_new_single_shot_id    (GstClock *clock,
546                                                          GstClockTime time);
547 GstClockID              gst_clock_new_periodic_id       (GstClock *clock,
548                                                          GstClockTime start_time,
549                                                          GstClockTime interval);
550
551 /* reference counting */
552 GstClockID              gst_clock_id_ref                (GstClockID id);
553 void                    gst_clock_id_unref              (GstClockID id);
554
555 /* operations on IDs */
556 gint                    gst_clock_id_compare_func       (gconstpointer id1, gconstpointer id2);
557
558 GstClockTime            gst_clock_id_get_time           (GstClockID id);
559 GstClockReturn          gst_clock_id_wait               (GstClockID id,
560                                                          GstClockTimeDiff *jitter);
561 GstClockReturn          gst_clock_id_wait_async         (GstClockID id,
562                                                          GstClockCallback func,
563                                                          gpointer user_data);
564 GstClockReturn          gst_clock_id_wait_async_full    (GstClockID id,
565                                                          GstClockCallback func,
566                                                          gpointer user_data,
567                                                          GDestroyNotify destroy_data);
568 void                    gst_clock_id_unschedule         (GstClockID id);
569
570 gboolean                gst_clock_single_shot_id_reinit (GstClock * clock,
571                                                          GstClockID id,
572                                                          GstClockTime time);
573 gboolean                gst_clock_periodic_id_reinit    (GstClock * clock,
574                                                          GstClockID id,
575                                                          GstClockTime start_time,
576                                                          GstClockTime interval);
577
578 G_END_DECLS
579
580 #endif /* __GST_CLOCK_H__ */