*.c: Don't cast to GST_OBJECT when reffing or unreffing. Large source-munging commit!!!
[platform/upstream/gstreamer.git] / gst / gstsystemclock.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2004 Wim Taymans <wim@fluendo.com>
4  *
5  * gstsystemclock.c: Default clock, uses the system clock
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 #include "gst_private.h"
24 #include "gstinfo.h"
25
26 #include "gstsystemclock.h"
27
28 /* the one instance of the systemclock */
29 static GstClock *_the_system_clock = NULL;
30
31 static void gst_system_clock_class_init (GstSystemClockClass * klass);
32 static void gst_system_clock_init (GstSystemClock * clock);
33 static void gst_system_clock_dispose (GObject * object);
34
35 static GstClockTime gst_system_clock_get_internal_time (GstClock * clock);
36 static guint64 gst_system_clock_get_resolution (GstClock * clock);
37 static GstClockReturn gst_system_clock_id_wait (GstClock * clock,
38     GstClockEntry * entry);
39 static GstClockReturn gst_system_clock_id_wait_unlocked
40     (GstClock * clock, GstClockEntry * entry);
41 static GstClockReturn gst_system_clock_id_wait_async (GstClock * clock,
42     GstClockEntry * entry);
43 static void gst_system_clock_id_unschedule (GstClock * clock,
44     GstClockEntry * entry);
45 static void gst_system_clock_async_thread (GstClock * clock);
46
47 static GStaticMutex _gst_sysclock_mutex = G_STATIC_MUTEX_INIT;
48
49 static GstClockClass *parent_class = NULL;
50
51 /* static guint gst_system_clock_signals[LAST_SIGNAL] = { 0 }; */
52
53 GType
54 gst_system_clock_get_type (void)
55 {
56   static GType clock_type = 0;
57
58   if (!clock_type) {
59     static const GTypeInfo clock_info = {
60       sizeof (GstSystemClockClass),
61       NULL,
62       NULL,
63       (GClassInitFunc) gst_system_clock_class_init,
64       NULL,
65       NULL,
66       sizeof (GstSystemClock),
67       0,
68       (GInstanceInitFunc) gst_system_clock_init,
69       NULL
70     };
71
72     clock_type = g_type_register_static (GST_TYPE_CLOCK, "GstSystemClock",
73         &clock_info, 0);
74   }
75   return clock_type;
76 }
77
78 static void
79 gst_system_clock_class_init (GstSystemClockClass * klass)
80 {
81   GObjectClass *gobject_class;
82   GstObjectClass *gstobject_class;
83   GstClockClass *gstclock_class;
84
85   gobject_class = (GObjectClass *) klass;
86   gstobject_class = (GstObjectClass *) klass;
87   gstclock_class = (GstClockClass *) klass;
88
89   parent_class = g_type_class_ref (GST_TYPE_CLOCK);
90
91   gobject_class->dispose = gst_system_clock_dispose;
92
93   gstclock_class->get_internal_time = gst_system_clock_get_internal_time;
94   gstclock_class->get_resolution = gst_system_clock_get_resolution;
95   gstclock_class->wait = gst_system_clock_id_wait;
96   gstclock_class->wait_async = gst_system_clock_id_wait_async;
97   gstclock_class->unschedule = gst_system_clock_id_unschedule;
98 }
99
100 static void
101 gst_system_clock_init (GstSystemClock * clock)
102 {
103   GError *error = NULL;
104
105   GST_CLOCK_FLAGS (clock) =
106       GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC |
107       GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC |
108       GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC |
109       GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC;
110
111   GST_LOCK (clock);
112   clock->thread = g_thread_create ((GThreadFunc) gst_system_clock_async_thread,
113       clock, TRUE, &error);
114   if (error)
115     goto no_thread;
116
117   /* wait for it to spin up */
118   GST_CLOCK_WAIT (clock);
119   GST_UNLOCK (clock);
120   return;
121
122 no_thread:
123   {
124     g_warning ("could not create async clock thread: %s", error->message);
125     GST_UNLOCK (clock);
126   }
127 }
128
129 static void
130 gst_system_clock_dispose (GObject * object)
131 {
132   GstClock *clock = (GstClock *) object;
133
134   /* there are subclasses of GstSystemClock running around... */
135   if (_the_system_clock == clock) {
136     g_warning ("disposing systemclock!");
137
138     /* no parent dispose here, this is bad enough already */
139   } else {
140     G_OBJECT_CLASS (parent_class)->dispose (object);
141   }
142 }
143
144 /**
145  * gst_system_clock_obtain:
146  *
147  * Get a handle to the default system clock. The refcount of the
148  * clock will be increased so you need to unref the clock after 
149  * usage.
150  *
151  * Returns: the default clock.
152  *
153  * MT safe.
154  */
155 GstClock *
156 gst_system_clock_obtain (void)
157 {
158   GstClock *clock;
159
160   g_static_mutex_lock (&_gst_sysclock_mutex);
161   clock = _the_system_clock;
162
163   if (clock == NULL) {
164     GST_CAT_DEBUG (GST_CAT_CLOCK, "creating new static system clock");
165     /* FIXME: the only way to clean this up is to have a gst_exit()
166      * function; until then, the program will always end with the sysclock
167      * at refcount 1 */
168     clock = g_object_new (GST_TYPE_SYSTEM_CLOCK,
169         "name", "GstSystemClock", NULL);
170
171     /* we created the global clock; take ownership so
172      * we can hand out instances later */
173     gst_object_ref (clock);
174     gst_object_sink (GST_OBJECT (clock));
175
176     _the_system_clock = clock;
177     g_static_mutex_unlock (&_gst_sysclock_mutex);
178   } else {
179     g_static_mutex_unlock (&_gst_sysclock_mutex);
180     GST_CAT_DEBUG (GST_CAT_CLOCK, "returning static system clock");
181   }
182
183   /* we ref it since we are a clock factory. */
184   gst_object_ref (clock);
185   return clock;
186 }
187
188 /* this thread reads the sorted clock entries from the queue. 
189  *
190  * It waits on each of them and fires the callback when the timeout occurs.
191  *
192  * When an entry in the queue was canceled, it is simply skipped.
193  *
194  * When waiting for an entry, it can become canceled, in that case we don't 
195  * call the callback but move to the next item in the queue.
196  *
197  * MT safe.
198  */
199 static void
200 gst_system_clock_async_thread (GstClock * clock)
201 {
202   GstSystemClock *sysclock = GST_SYSTEM_CLOCK (clock);
203
204   GST_CAT_DEBUG (GST_CAT_CLOCK, "enter system clock thread");
205   GST_LOCK (clock);
206   /* signal spinup */
207   GST_CLOCK_SIGNAL (clock);
208   /* now enter our infinite loop */
209   while (!sysclock->stopping) {
210     GstClockEntry *entry;
211     GstClockReturn res;
212
213     /* check if something to be done */
214     while (clock->entries == NULL) {
215       GST_CAT_DEBUG (GST_CAT_CLOCK, "nothing to wait for");
216       /* wait for work to do */
217       GST_CLOCK_WAIT (clock);
218       GST_CAT_DEBUG (GST_CAT_CLOCK, "got signal");
219       /* clock was stopping, exit */
220       if (sysclock->stopping)
221         goto exit;
222     }
223
224     /* pick the next entry */
225     entry = clock->entries->data;
226     /* if it was unscheduled, just move on to the next entry */
227     if (entry->status == GST_CLOCK_UNSCHEDULED) {
228       GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p was unscheduled", entry);
229       goto next_entry;
230     }
231
232     /* now wait for the entry, we already hold the lock */
233     res = gst_system_clock_id_wait_unlocked (clock, (GstClockID) entry);
234
235     switch (res) {
236       case GST_CLOCK_UNSCHEDULED:
237         /* entry was unscheduled, move to the next */
238         GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unscheduled", entry);
239         goto next_entry;
240       case GST_CLOCK_OK:
241       case GST_CLOCK_EARLY:
242       {
243         /* entry timed out normally, fire the callback and move to the next
244          * entry */
245         GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unlocked", entry);
246         if (entry->func) {
247           entry->func (clock, entry->time, (GstClockID) entry,
248               entry->user_data);
249         }
250         if (entry->type == GST_CLOCK_ENTRY_PERIODIC) {
251           /* adjust time now */
252           entry->time += entry->interval;
253           /* and resort the list now */
254           clock->entries =
255               g_list_sort (clock->entries, gst_clock_id_compare_func);
256           /* and restart */
257           continue;
258         } else {
259           goto next_entry;
260         }
261       }
262       case GST_CLOCK_BUSY:
263         /* somebody unlocked the entry but is was not canceled, This means that
264          * either a new entry was added in front of the queue or some other entry 
265          * was canceled. Whatever it is, pick the head entry of the list and
266          * continue waiting. */
267         GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p needs restart", entry);
268         continue;
269       default:
270         GST_CAT_DEBUG (GST_CAT_CLOCK,
271             "strange result %d waiting for %p, skipping", res, entry);
272         goto next_entry;
273     }
274   next_entry:
275     /* we remove the current entry and unref it */
276     clock->entries = g_list_remove (clock->entries, entry);
277     gst_clock_id_unref ((GstClockID) entry);
278   }
279 exit:
280   /* signal exit */
281   GST_CLOCK_SIGNAL (clock);
282   GST_UNLOCK (clock);
283   GST_CAT_DEBUG (GST_CAT_CLOCK, "exit system clock thread");
284 }
285
286 /* MT safe */
287 static GstClockTime
288 gst_system_clock_get_internal_time (GstClock * clock)
289 {
290   GTimeVal timeval;
291
292   g_get_current_time (&timeval);
293
294   return GST_TIMEVAL_TO_TIME (timeval);
295 }
296
297 static guint64
298 gst_system_clock_get_resolution (GstClock * clock)
299 {
300   return 1 * GST_USECOND;
301 }
302
303 /* synchronously wait on the given GstClockEntry.
304  *
305  * We do this by blocking on the global clock GCond variable with
306  * the requested time as a timeout. This allows us to unblock the
307  * entry by signaling the GCond variable.
308  *
309  * Note that signaling the global GCond unlocks all waiting entries. So
310  * we need to check if an unlocked entry has changed when it unlocks.
311  *
312  * Entries that arrive too late are simply not waited on and a
313  * GST_CLOCK_EARLY result is returned.
314  *
315  * MT safe.
316  */
317 static GstClockReturn
318 gst_system_clock_id_wait_unlocked (GstClock * clock, GstClockEntry * entry)
319 {
320   GstClockTime entryt, real, now, target;
321   GstClockTimeDiff diff;
322
323   /* need to call the overridden method */
324   real = GST_CLOCK_GET_CLASS (clock)->get_internal_time (clock);
325   entryt = GST_CLOCK_ENTRY_TIME (entry);
326
327   now = gst_clock_adjust_unlocked (clock, real);
328   diff = entryt - now;
329   target = gst_system_clock_get_internal_time (clock) + diff;
330
331   GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p"
332       " target %" GST_TIME_FORMAT
333       " entry %" GST_TIME_FORMAT
334       " now %" GST_TIME_FORMAT
335       " real %" GST_TIME_FORMAT
336       " diff %" G_GINT64_FORMAT,
337       entry,
338       GST_TIME_ARGS (target),
339       GST_TIME_ARGS (entryt), GST_TIME_ARGS (now), GST_TIME_ARGS (real), diff);
340
341   if (diff > 0) {
342     GTimeVal tv;
343
344     GST_TIME_TO_TIMEVAL (target, tv);
345
346     while (TRUE) {
347       /* now wait on the entry, it either times out or the cond is signaled. */
348       if (!GST_CLOCK_TIMED_WAIT (clock, &tv)) {
349         /* timeout, this is fine, we can report success now */
350         GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked after timeout", entry);
351         entry->status = GST_CLOCK_OK;
352         break;
353       } else {
354         /* the waiting is interrupted because the GCond was signaled. This can
355          * be because this or some other entry was unscheduled. */
356         GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked with signal", entry);
357         /* if the entry is unscheduled, we can stop waiting for it */
358         if (entry->status == GST_CLOCK_UNSCHEDULED)
359           break;
360       }
361     }
362   } else {
363     entry->status = GST_CLOCK_EARLY;
364   }
365   return entry->status;
366 }
367
368 static GstClockReturn
369 gst_system_clock_id_wait (GstClock * clock, GstClockEntry * entry)
370 {
371   GstClockReturn ret;
372
373   GST_LOCK (clock);
374   ret = gst_system_clock_id_wait_unlocked (clock, entry);
375   GST_UNLOCK (clock);
376
377   return ret;
378 }
379
380 /* Add an entry to the list of pending async waits. The entry is inserted
381  * in sorted order. If we inserted the entry at the head of the list, we
382  * need to signal the thread as it might either be waiting on it or waiting
383  * for a new entry. 
384  *
385  * MT safe.
386  */
387 static GstClockReturn
388 gst_system_clock_id_wait_async (GstClock * clock, GstClockEntry * entry)
389 {
390   GST_CAT_DEBUG (GST_CAT_CLOCK, "adding entry %p", entry);
391
392   GST_LOCK (clock);
393   /* need to take a ref */
394   gst_clock_id_ref ((GstClockID) entry);
395   /* insert the entry in sorted order */
396   clock->entries = g_list_insert_sorted (clock->entries, entry,
397       gst_clock_id_compare_func);
398
399   /* only need to send the signal if the entry was added to the
400    * front, else the thread is just waiting for another entry and
401    * will get to this entry automatically. */
402   if (clock->entries->data == entry) {
403     GST_CAT_DEBUG (GST_CAT_CLOCK, "send signal");
404     GST_CLOCK_SIGNAL (clock);
405   }
406   GST_UNLOCK (clock);
407
408   return GST_CLOCK_OK;
409 }
410
411 /* unschedule an entry. This will set the state of the entry to GST_CLOCK_UNSCHEDULED
412  * and will signal any thread waiting for entries to recheck their entry. 
413  * We cannot really decide if the signal is needed or not because the entry
414  * could be waited on in async or sync mode.
415  *
416  * MT safe.
417  */
418 static void
419 gst_system_clock_id_unschedule (GstClock * clock, GstClockEntry * entry)
420 {
421   GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
422
423   GST_LOCK (clock);
424   entry->status = GST_CLOCK_UNSCHEDULED;
425   GST_CAT_DEBUG (GST_CAT_CLOCK, "send signal");
426   GST_CLOCK_SIGNAL (clock);
427   GST_UNLOCK (clock);
428 }