- Reworked the clock to prepare for async notifications
[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  *
5  * gstclock.h: Header for clock subsystem
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifndef __GST_CLOCK_H__
24 #define __GST_CLOCK_H__
25
26 #include <gst/gstobject.h>
27
28 G_BEGIN_DECLS
29
30 #define GST_TYPE_CLOCK \
31   (gst_clock_get_type())
32 #define GST_CLOCK(obj) \
33   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_CLOCK,GstClock))
34 #define GST_CLOCK_CLASS(klass) \
35   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_CLOCK,GstClockClass))
36 #define GST_IS_CLOCK(obj) \
37   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_CLOCK))
38 #define GST_IS_CLOCK_CLASS(obj) \
39   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_CLOCK))
40         
41 typedef guint64         GstClockTime;
42 typedef gint64          GstClockTimeDiff;
43 typedef gpointer        GstClockID;
44
45 #define GST_CLOCK_TIME_NONE  ((guint64)-1)
46
47 #define GST_SECOND  ((guint64) G_USEC_PER_SEC * 1000LL)
48 #define GST_MSECOND ((guint64) GST_SECOND / 1000LL)
49 #define GST_USECOND ((guint64) GST_SECOND / 1000000LL)
50 #define GST_NSECOND ((guint64) GST_SECOND / 1000000000LL)
51
52 #define GST_CLOCK_DIFF(s, e)            (GstClockTimeDiff)((s) - (e))
53 #define GST_TIMEVAL_TO_TIME(tv)         ((tv).tv_sec * GST_SECOND + (tv).tv_usec * GST_USECOND)
54 #define GST_TIME_TO_TIMEVAL(t,tv)                       \
55 G_STMT_START {                                          \
56   (tv).tv_sec  =  (t) / GST_SECOND;                     \
57   (tv).tv_usec = ((t) / GST_USECOND) % GST_MSECOND;     \
58 } G_STMT_END
59
60 typedef struct _GstClockEntry   GstClockEntry;
61 typedef struct _GstClock        GstClock;
62 typedef struct _GstClockClass   GstClockClass;
63
64 typedef gboolean (*GstClockCallback) (GstClock *clock, GstClockTime time, GstClockID id, gpointer user_data);
65
66 typedef enum {
67   /*< protected >*/
68   GST_CLOCK_ENTRY_OK,
69   GST_CLOCK_ENTRY_EARLY,
70   GST_CLOCK_ENTRY_RESTART,
71 } GstClockEntryStatus;
72
73 typedef enum {
74   /*< protected >*/
75   GST_CLOCK_ENTRY_SINGLE,
76   GST_CLOCK_ENTRY_PERIODIC,
77 } GstClockEntryType;
78
79 #define GST_CLOCK_ENTRY(entry)          ((GstClockEntry *)(entry))
80 #define GST_CLOCK_ENTRY_CLOCK(entry)    ((entry)->clock)
81 #define GST_CLOCK_ENTRY_TYPE(entry)     ((entry)->type)
82 #define GST_CLOCK_ENTRY_TIME(entry)     ((entry)->time)
83 #define GST_CLOCK_ENTRY_INTERVAL(entry) ((entry)->interval)
84 #define GST_CLOCK_ENTRY_STATUS(entry)   ((entry)->status)
85
86 struct _GstClockEntry {
87   /*< protected >*/
88   GstClock              *clock;
89   GstClockEntryType      type;
90   GstClockTime           time;
91   GstClockTime           interval;
92   GstClockEntryStatus    status;
93   GstClockCallback       func;
94   gpointer               user_data;
95 };
96
97 typedef enum
98 {
99   GST_CLOCK_STOPPED     = 0,
100   GST_CLOCK_TIMEOUT     = 1,
101   GST_CLOCK_EARLY       = 2,
102   GST_CLOCK_ERROR       = 3,
103   GST_CLOCK_UNSUPPORTED = 4
104 } GstClockReturn;
105
106 typedef enum
107 {
108   GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC     = (1 << 1),
109   GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC    = (1 << 2),
110   GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC   = (1 << 3),
111   GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC  = (1 << 4),
112   GST_CLOCK_FLAG_CAN_SET_RESOLUTION     = (1 << 5),
113   GST_CLOCK_FLAG_CAN_SET_SPEED          = (1 << 6),
114 } GstClockFlags;
115
116 #define GST_CLOCK_FLAGS(clock)  (GST_CLOCK(clock)->flags)
117
118 struct _GstClock {
119   GstObject      object;
120
121   GstClockFlags  flags;
122
123   /*< protected >*/
124   GstClockTime   start_time;
125   GstClockTime   last_time;
126
127   /*< private >*/
128   gboolean       accept_discont;
129   gdouble        speed;
130   guint64        resolution;
131   gboolean       active;
132   GList         *entries;
133   GMutex        *active_mutex;
134   GCond         *active_cond;
135   gboolean       stats;
136 };
137
138 struct _GstClockClass {
139   GstObjectClass        parent_class;
140
141   /* vtable */
142   gdouble               (*change_speed)         (GstClock *clock,
143                                                  gdouble oldspeed, gdouble newspeed);
144   gdouble               (*get_speed)            (GstClock *clock);
145   guint64               (*change_resolution)    (GstClock *clock, guint64 old_resolution,
146                                                  guint64 new_resolution);
147   guint64               (*get_resolution)       (GstClock *clock);
148
149   GstClockTime          (*get_internal_time)    (GstClock *clock);
150
151   /* waiting on an ID */
152   GstClockEntryStatus   (*wait)                 (GstClock *clock, GstClockEntry *entry);
153   GstClockEntryStatus   (*wait_async)           (GstClock *clock, GstClockEntry *entry,
154                                                  GstClockCallback func, gpointer user_data);
155   void                  (*unschedule)           (GstClock *clock, GstClockEntry *entry);
156   void                  (*unlock)               (GstClock *clock, GstClockEntry *entry);
157
158   /* signals */
159   void                  (*object_sync)          (GstClock *clock, GstObject *object, 
160                                                  GstClockID id);
161 };
162
163 GType                   gst_clock_get_type              (void);
164
165 gdouble                 gst_clock_set_speed             (GstClock *clock, gdouble speed);
166 gdouble                 gst_clock_get_speed             (GstClock *clock);
167
168 guint64                 gst_clock_set_resolution        (GstClock *clock, guint64 resolution);
169 guint64                 gst_clock_get_resolution        (GstClock *clock);
170
171 void                    gst_clock_set_active            (GstClock *clock, gboolean active);
172 gboolean                gst_clock_is_active             (GstClock *clock);
173 void                    gst_clock_reset                 (GstClock *clock);
174 gboolean                gst_clock_handle_discont        (GstClock *clock, guint64 time);
175
176 GstClockTime            gst_clock_get_time              (GstClock *clock);
177
178 GstClockID              gst_clock_get_next_id           (GstClock *clock);
179
180 /* creating IDs that can be used to get notifications */
181 GstClockID              gst_clock_new_single_shot_id    (GstClock *clock, 
182                                                          GstClockTime time); 
183 GstClockID              gst_clock_new_periodic_id       (GstClock *clock, 
184                                                          GstClockTime start_time,
185                                                          GstClockTime interval); 
186
187 /* operations on IDs */
188 GstClockTime            gst_clock_id_get_time           (GstClockID id);
189 GstClockReturn          gst_clock_id_wait               (GstClockID id, 
190                                                          GstClockTimeDiff *jitter);
191 GstClockReturn          gst_clock_id_wait_async         (GstClockID id, 
192                                                          GstClockCallback func, 
193                                                          gpointer user_data);
194 void                    gst_clock_id_unschedule         (GstClockID id);
195 void                    gst_clock_id_unlock             (GstClockID id);
196 void                    gst_clock_id_free               (GstClockID id);
197
198 G_END_DECLS
199
200 #endif /* __GST_CLOCK_H__ */