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 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (clock);
137 /* else we have to stop the thread */
139 sysclock->stopping = TRUE;
140 /* unschedule all entries */
141 for (entries = clock->entries; entries; entries = g_list_next (entries)) {
142 GstClockEntry *entry = (GstClockEntry *) entries->data;
144 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
145 entry->status = GST_CLOCK_UNSCHEDULED;
147 g_list_free (clock->entries);
148 clock->entries = NULL;
149 GST_CLOCK_BROADCAST (clock);
152 if (sysclock->thread)
153 g_thread_join (sysclock->thread);
154 sysclock->thread = NULL;
155 GST_CAT_DEBUG (GST_CAT_CLOCK, "joined thread");
157 G_OBJECT_CLASS (parent_class)->dispose (object);
159 if (_the_system_clock == clock) {
160 _the_system_clock = NULL;
161 GST_CAT_DEBUG (GST_CAT_CLOCK, "disposed system clock");
166 * gst_system_clock_obtain:
168 * Get a handle to the default system clock. The refcount of the
169 * clock will be increased so you need to unref the clock after
172 * Returns: the default clock.
177 gst_system_clock_obtain (void)
181 g_static_mutex_lock (&_gst_sysclock_mutex);
182 clock = _the_system_clock;
185 GST_CAT_DEBUG (GST_CAT_CLOCK, "creating new static system clock");
186 clock = g_object_new (GST_TYPE_SYSTEM_CLOCK,
187 "name", "GstSystemClock", NULL);
189 /* we created the global clock; take ownership so
190 * we can hand out instances later */
191 gst_object_ref (clock);
192 gst_object_sink (GST_OBJECT (clock));
194 _the_system_clock = clock;
195 g_static_mutex_unlock (&_gst_sysclock_mutex);
197 g_static_mutex_unlock (&_gst_sysclock_mutex);
198 GST_CAT_DEBUG (GST_CAT_CLOCK, "returning static system clock");
201 /* we ref it since we are a clock factory. */
202 gst_object_ref (clock);
206 /* this thread reads the sorted clock entries from the queue.
208 * It waits on each of them and fires the callback when the timeout occurs.
210 * When an entry in the queue was canceled, it is simply skipped.
212 * When waiting for an entry, it can become canceled, in that case we don't
213 * call the callback but move to the next item in the queue.
218 gst_system_clock_async_thread (GstClock * clock)
220 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (clock);
222 GST_CAT_DEBUG (GST_CAT_CLOCK, "enter system clock thread");
225 GST_CLOCK_BROADCAST (clock);
226 /* now enter our infinite loop */
227 while (!sysclock->stopping) {
228 GstClockEntry *entry;
231 /* check if something to be done */
232 while (clock->entries == NULL) {
233 GST_CAT_DEBUG (GST_CAT_CLOCK, "nothing to wait for");
234 /* wait for work to do */
235 GST_CLOCK_WAIT (clock);
236 GST_CAT_DEBUG (GST_CAT_CLOCK, "got signal");
237 /* clock was stopping, exit */
238 if (sysclock->stopping)
242 /* pick the next entry */
243 entry = clock->entries->data;
244 /* if it was unscheduled, just move on to the next entry */
245 if (entry->status == GST_CLOCK_UNSCHEDULED) {
246 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p was unscheduled", entry);
250 /* now wait for the entry, we already hold the lock */
251 res = gst_system_clock_id_wait_unlocked (clock, (GstClockID) entry);
254 case GST_CLOCK_UNSCHEDULED:
255 /* entry was unscheduled, move to the next */
256 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unscheduled", entry);
259 case GST_CLOCK_EARLY:
261 /* entry timed out normally, fire the callback and move to the next
263 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unlocked", entry);
265 entry->func (clock, entry->time, (GstClockID) entry,
268 if (entry->type == GST_CLOCK_ENTRY_PERIODIC) {
269 /* adjust time now */
270 entry->time += entry->interval;
271 /* and resort the list now */
273 g_list_sort (clock->entries, gst_clock_id_compare_func);
281 /* somebody unlocked the entry but is was not canceled, This means that
282 * either a new entry was added in front of the queue or some other entry
283 * was canceled. Whatever it is, pick the head entry of the list and
284 * continue waiting. */
285 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p needs restart", entry);
288 GST_CAT_DEBUG (GST_CAT_CLOCK,
289 "strange result %d waiting for %p, skipping", res, entry);
293 /* we remove the current entry and unref it */
294 clock->entries = g_list_remove (clock->entries, entry);
295 gst_clock_id_unref ((GstClockID) entry);
299 GST_CLOCK_BROADCAST (clock);
301 GST_CAT_DEBUG (GST_CAT_CLOCK, "exit system clock thread");
306 gst_system_clock_get_internal_time (GstClock * clock)
310 g_get_current_time (&timeval);
312 return GST_TIMEVAL_TO_TIME (timeval);
316 gst_system_clock_get_resolution (GstClock * clock)
318 return 1 * GST_USECOND;
321 /* synchronously wait on the given GstClockEntry.
323 * We do this by blocking on the global clock GCond variable with
324 * the requested time as a timeout. This allows us to unblock the
325 * entry by signaling the GCond variable.
327 * Note that signaling the global GCond unlocks all waiting entries. So
328 * we need to check if an unlocked entry has changed when it unlocks.
330 * Entries that arrive too late are simply not waited on and a
331 * GST_CLOCK_EARLY result is returned.
335 static GstClockReturn
336 gst_system_clock_id_wait_unlocked (GstClock * clock, GstClockEntry * entry)
338 GstClockTime entryt, real, now, target;
339 GstClockTimeDiff diff;
341 /* need to call the overridden method */
342 real = GST_CLOCK_GET_CLASS (clock)->get_internal_time (clock);
343 entryt = GST_CLOCK_ENTRY_TIME (entry);
345 now = gst_clock_adjust_unlocked (clock, real);
347 target = gst_system_clock_get_internal_time (clock) + diff;
349 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p"
350 " target %" GST_TIME_FORMAT
351 " entry %" GST_TIME_FORMAT
352 " now %" GST_TIME_FORMAT
353 " real %" GST_TIME_FORMAT
354 " diff %" G_GINT64_FORMAT,
356 GST_TIME_ARGS (target),
357 GST_TIME_ARGS (entryt), GST_TIME_ARGS (now), GST_TIME_ARGS (real), diff);
362 GST_TIME_TO_TIMEVAL (target, tv);
365 /* now wait on the entry, it either times out or the cond is signaled. */
366 if (!GST_CLOCK_TIMED_WAIT (clock, &tv)) {
367 /* timeout, this is fine, we can report success now */
368 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked after timeout", entry);
369 entry->status = GST_CLOCK_OK;
372 /* the waiting is interrupted because the GCond was signaled. This can
373 * be because this or some other entry was unscheduled. */
374 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked with signal", entry);
375 /* if the entry is unscheduled, we can stop waiting for it */
376 if (entry->status == GST_CLOCK_UNSCHEDULED)
381 entry->status = GST_CLOCK_EARLY;
383 return entry->status;
386 static GstClockReturn
387 gst_system_clock_id_wait (GstClock * clock, GstClockEntry * entry)
392 ret = gst_system_clock_id_wait_unlocked (clock, entry);
398 /* Add an entry to the list of pending async waits. The entry is inserted
399 * in sorted order. If we inserted the entry at the head of the list, we
400 * need to signal the thread as it might either be waiting on it or waiting
405 static GstClockReturn
406 gst_system_clock_id_wait_async (GstClock * clock, GstClockEntry * entry)
408 GST_CAT_DEBUG (GST_CAT_CLOCK, "adding entry %p", entry);
411 /* need to take a ref */
412 gst_clock_id_ref ((GstClockID) entry);
413 /* insert the entry in sorted order */
414 clock->entries = g_list_insert_sorted (clock->entries, entry,
415 gst_clock_id_compare_func);
417 /* only need to send the signal if the entry was added to the
418 * front, else the thread is just waiting for another entry and
419 * will get to this entry automatically. */
420 if (clock->entries->data == entry) {
421 GST_CAT_DEBUG (GST_CAT_CLOCK, "send signal");
422 GST_CLOCK_BROADCAST (clock);
429 /* unschedule an entry. This will set the state of the entry to GST_CLOCK_UNSCHEDULED
430 * and will signal any thread waiting for entries to recheck their entry.
431 * We cannot really decide if the signal is needed or not because the entry
432 * could be waited on in async or sync mode.
437 gst_system_clock_id_unschedule (GstClock * clock, GstClockEntry * entry)
439 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
442 entry->status = GST_CLOCK_UNSCHEDULED;
443 GST_CAT_DEBUG (GST_CAT_CLOCK, "send signal");
444 GST_CLOCK_BROADCAST (clock);