2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2004 Wim Taymans <wim@fluendo.com>
5 * gstsystemclock.c: Default clock, uses the system clock
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.
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.
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.
23 #include "gst_private.h"
26 #include "gstsystemclock.h"
28 /* the one instance of the systemclock */
29 static GstClock *_the_system_clock = NULL;
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);
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);
47 static GStaticMutex _gst_sysclock_mutex = G_STATIC_MUTEX_INIT;
49 static GstClockClass *parent_class = NULL;
51 /* static guint gst_system_clock_signals[LAST_SIGNAL] = { 0 }; */
54 gst_system_clock_get_type (void)
56 static GType clock_type = 0;
59 static const GTypeInfo clock_info = {
60 sizeof (GstSystemClockClass),
63 (GClassInitFunc) gst_system_clock_class_init,
66 sizeof (GstSystemClock),
68 (GInstanceInitFunc) gst_system_clock_init,
72 clock_type = g_type_register_static (GST_TYPE_CLOCK, "GstSystemClock",
79 gst_system_clock_class_init (GstSystemClockClass * klass)
81 GObjectClass *gobject_class;
82 GstObjectClass *gstobject_class;
83 GstClockClass *gstclock_class;
85 gobject_class = (GObjectClass *) klass;
86 gstobject_class = (GstObjectClass *) klass;
87 gstclock_class = (GstClockClass *) klass;
89 parent_class = g_type_class_ref (GST_TYPE_CLOCK);
91 gobject_class->dispose = gst_system_clock_dispose;
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;
101 gst_system_clock_init (GstSystemClock * clock)
103 GError *error = NULL;
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;
112 clock->thread = g_thread_create ((GThreadFunc) gst_system_clock_async_thread,
113 clock, TRUE, &error);
117 /* wait for it to spin up */
118 GST_CLOCK_WAIT (clock);
124 g_warning ("could not create async clock thread: %s", error->message);
130 gst_system_clock_dispose (GObject * object)
132 GstClock *clock = (GstClock *) object;
134 /* there are subclasses of GstSystemClock running around... */
135 if (_the_system_clock == clock) {
136 g_warning ("disposing systemclock!");
138 /* no parent dispose here, this is bad enough already */
140 G_OBJECT_CLASS (parent_class)->dispose (object);
145 * gst_system_clock_obtain:
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
151 * Returns: the default clock.
156 gst_system_clock_obtain (void)
160 g_static_mutex_lock (&_gst_sysclock_mutex);
161 clock = _the_system_clock;
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
168 clock = g_object_new (GST_TYPE_SYSTEM_CLOCK,
169 "name", "GstSystemClock", NULL);
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));
176 _the_system_clock = clock;
177 g_static_mutex_unlock (&_gst_sysclock_mutex);
179 g_static_mutex_unlock (&_gst_sysclock_mutex);
180 GST_CAT_DEBUG (GST_CAT_CLOCK, "returning static system clock");
183 /* we ref it since we are a clock factory. */
184 gst_object_ref (clock);
188 /* this thread reads the sorted clock entries from the queue.
190 * It waits on each of them and fires the callback when the timeout occurs.
192 * When an entry in the queue was canceled, it is simply skipped.
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.
200 gst_system_clock_async_thread (GstClock * clock)
202 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (clock);
204 GST_CAT_DEBUG (GST_CAT_CLOCK, "enter system clock thread");
207 GST_CLOCK_SIGNAL (clock);
208 /* now enter our infinite loop */
209 while (!sysclock->stopping) {
210 GstClockEntry *entry;
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)
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);
232 /* now wait for the entry, we already hold the lock */
233 res = gst_system_clock_id_wait_unlocked (clock, (GstClockID) entry);
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);
241 case GST_CLOCK_EARLY:
243 /* entry timed out normally, fire the callback and move to the next
245 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unlocked", entry);
247 entry->func (clock, entry->time, (GstClockID) entry,
250 if (entry->type == GST_CLOCK_ENTRY_PERIODIC) {
251 /* adjust time now */
252 entry->time += entry->interval;
253 /* and resort the list now */
255 g_list_sort (clock->entries, gst_clock_id_compare_func);
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);
270 GST_CAT_DEBUG (GST_CAT_CLOCK,
271 "strange result %d waiting for %p, skipping", res, 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);
281 GST_CLOCK_SIGNAL (clock);
283 GST_CAT_DEBUG (GST_CAT_CLOCK, "exit system clock thread");
288 gst_system_clock_get_internal_time (GstClock * clock)
292 g_get_current_time (&timeval);
294 return GST_TIMEVAL_TO_TIME (timeval);
298 gst_system_clock_get_resolution (GstClock * clock)
300 return 1 * GST_USECOND;
303 /* synchronously wait on the given GstClockEntry.
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.
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.
312 * Entries that arrive too late are simply not waited on and a
313 * GST_CLOCK_EARLY result is returned.
317 static GstClockReturn
318 gst_system_clock_id_wait_unlocked (GstClock * clock, GstClockEntry * entry)
320 GstClockTime entryt, real, now, target;
321 GstClockTimeDiff diff;
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);
327 now = gst_clock_adjust_unlocked (clock, real);
329 target = gst_system_clock_get_internal_time (clock) + diff;
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,
338 GST_TIME_ARGS (target),
339 GST_TIME_ARGS (entryt), GST_TIME_ARGS (now), GST_TIME_ARGS (real), diff);
344 GST_TIME_TO_TIMEVAL (target, tv);
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;
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)
363 entry->status = GST_CLOCK_EARLY;
365 return entry->status;
368 static GstClockReturn
369 gst_system_clock_id_wait (GstClock * clock, GstClockEntry * entry)
374 ret = gst_system_clock_id_wait_unlocked (clock, entry);
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
387 static GstClockReturn
388 gst_system_clock_id_wait_async (GstClock * clock, GstClockEntry * entry)
390 GST_CAT_DEBUG (GST_CAT_CLOCK, "adding entry %p", entry);
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);
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);
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.
419 gst_system_clock_id_unschedule (GstClock * clock, GstClockEntry * entry)
421 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
424 entry->status = GST_CLOCK_UNSCHEDULED;
425 GST_CAT_DEBUG (GST_CAT_CLOCK, "send signal");
426 GST_CLOCK_SIGNAL (clock);