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 */
23 /**
24 * SECTION:gstsystemclock
25 * @short_description: Default clock that uses the current system time
26 * @see_also: #GstClock
27 *
28 * The GStreamer core provides a GstSystemClock based on the system time.
29 * Asynchronous callbacks are scheduled from an internal thread.
30 *
31 * Clock implementors are encouraged to subclass this systemclock as it
32 * implements the async notification.
33 *
34 * Subclasses can however override all of the important methods for sync and
35 * async notifications to implement their own callback methods or blocking
36 * wait operations.
37 *
38 * Last reviewed on 2006-03-08 (0.10.4)
39 */
41 #include "gst_private.h"
42 #include "gstinfo.h"
44 #include "gstsystemclock.h"
46 /* Define this to get some extra debug about jitter from each clock_wait */
47 #undef WAIT_DEBUGGING
49 /* the one instance of the systemclock */
50 static GstClock *_the_system_clock = NULL;
52 static void gst_system_clock_class_init (GstSystemClockClass * klass);
53 static void gst_system_clock_init (GstSystemClock * clock);
54 static void gst_system_clock_dispose (GObject * object);
56 static GstClockTime gst_system_clock_get_internal_time (GstClock * clock);
57 static guint64 gst_system_clock_get_resolution (GstClock * clock);
58 static GstClockReturn gst_system_clock_id_wait_jitter (GstClock * clock,
59 GstClockEntry * entry, GstClockTimeDiff * jitter);
60 static GstClockReturn gst_system_clock_id_wait_jitter_unlocked
61 (GstClock * clock, GstClockEntry * entry, GstClockTimeDiff * jitter);
62 static GstClockReturn gst_system_clock_id_wait_async (GstClock * clock,
63 GstClockEntry * entry);
64 static void gst_system_clock_id_unschedule (GstClock * clock,
65 GstClockEntry * entry);
66 static void gst_system_clock_async_thread (GstClock * clock);
68 static GStaticMutex _gst_sysclock_mutex = G_STATIC_MUTEX_INIT;
70 static GstClockClass *parent_class = NULL;
72 /* static guint gst_system_clock_signals[LAST_SIGNAL] = { 0 }; */
74 GType
75 gst_system_clock_get_type (void)
76 {
77 static GType clock_type = 0;
79 if (G_UNLIKELY (clock_type == 0)) {
80 static const GTypeInfo clock_info = {
81 sizeof (GstSystemClockClass),
82 NULL,
83 NULL,
84 (GClassInitFunc) gst_system_clock_class_init,
85 NULL,
86 NULL,
87 sizeof (GstSystemClock),
88 0,
89 (GInstanceInitFunc) gst_system_clock_init,
90 NULL
91 };
93 clock_type = g_type_register_static (GST_TYPE_CLOCK, "GstSystemClock",
94 &clock_info, 0);
95 }
96 return clock_type;
97 }
99 static void
100 gst_system_clock_class_init (GstSystemClockClass * klass)
101 {
102 GObjectClass *gobject_class;
103 GstObjectClass *gstobject_class;
104 GstClockClass *gstclock_class;
106 gobject_class = (GObjectClass *) klass;
107 gstobject_class = (GstObjectClass *) klass;
108 gstclock_class = (GstClockClass *) klass;
110 parent_class = g_type_class_peek_parent (klass);
112 gobject_class->dispose = gst_system_clock_dispose;
114 gstclock_class->get_internal_time = gst_system_clock_get_internal_time;
115 gstclock_class->get_resolution = gst_system_clock_get_resolution;
116 gstclock_class->wait_jitter = gst_system_clock_id_wait_jitter;
117 gstclock_class->wait_async = gst_system_clock_id_wait_async;
118 gstclock_class->unschedule = gst_system_clock_id_unschedule;
119 }
121 static void
122 gst_system_clock_init (GstSystemClock * clock)
123 {
124 GError *error = NULL;
126 GST_OBJECT_FLAG_SET (clock,
127 GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC |
128 GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC |
129 GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC |
130 GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC);
132 GST_OBJECT_LOCK (clock);
133 clock->thread = g_thread_create ((GThreadFunc) gst_system_clock_async_thread,
134 clock, TRUE, &error);
135 if (error)
136 goto no_thread;
138 /* wait for it to spin up */
139 GST_CLOCK_WAIT (clock);
140 GST_OBJECT_UNLOCK (clock);
141 return;
143 /* ERRORS */
144 no_thread:
145 {
146 g_warning ("could not create async clock thread: %s", error->message);
147 GST_OBJECT_UNLOCK (clock);
148 }
149 }
151 static void
152 gst_system_clock_dispose (GObject * object)
153 {
154 GstClock *clock = (GstClock *) object;
156 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (clock);
157 GList *entries;
159 /* else we have to stop the thread */
160 GST_OBJECT_LOCK (clock);
161 sysclock->stopping = TRUE;
162 /* unschedule all entries */
163 for (entries = clock->entries; entries; entries = g_list_next (entries)) {
164 GstClockEntry *entry = (GstClockEntry *) entries->data;
166 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
167 entry->status = GST_CLOCK_UNSCHEDULED;
168 }
169 g_list_free (clock->entries);
170 clock->entries = NULL;
171 GST_CLOCK_BROADCAST (clock);
172 GST_OBJECT_UNLOCK (clock);
174 if (sysclock->thread)
175 g_thread_join (sysclock->thread);
176 sysclock->thread = NULL;
177 GST_CAT_DEBUG (GST_CAT_CLOCK, "joined thread");
179 G_OBJECT_CLASS (parent_class)->dispose (object);
181 if (_the_system_clock == clock) {
182 _the_system_clock = NULL;
183 GST_CAT_DEBUG (GST_CAT_CLOCK, "disposed system clock");
184 }
185 }
187 /**
188 * gst_system_clock_obtain:
189 *
190 * Get a handle to the default system clock. The refcount of the
191 * clock will be increased so you need to unref the clock after
192 * usage.
193 *
194 * Returns: the default clock.
195 *
196 * MT safe.
197 */
198 GstClock *
199 gst_system_clock_obtain (void)
200 {
201 GstClock *clock;
203 g_static_mutex_lock (&_gst_sysclock_mutex);
204 clock = _the_system_clock;
206 if (clock == NULL) {
207 GST_CAT_DEBUG (GST_CAT_CLOCK, "creating new static system clock");
208 clock = g_object_new (GST_TYPE_SYSTEM_CLOCK,
209 "name", "GstSystemClock", NULL);
211 /* we created the global clock; take ownership so
212 * we can hand out instances later */
213 gst_object_ref (clock);
214 gst_object_sink (GST_OBJECT (clock));
216 _the_system_clock = clock;
217 g_static_mutex_unlock (&_gst_sysclock_mutex);
218 } else {
219 g_static_mutex_unlock (&_gst_sysclock_mutex);
220 GST_CAT_DEBUG (GST_CAT_CLOCK, "returning static system clock");
221 }
223 /* we ref it since we are a clock factory. */
224 gst_object_ref (clock);
225 return clock;
226 }
228 /* this thread reads the sorted clock entries from the queue.
229 *
230 * It waits on each of them and fires the callback when the timeout occurs.
231 *
232 * When an entry in the queue was canceled, it is simply skipped.
233 *
234 * When waiting for an entry, it can become canceled, in that case we don't
235 * call the callback but move to the next item in the queue.
236 *
237 * MT safe.
238 */
239 static void
240 gst_system_clock_async_thread (GstClock * clock)
241 {
242 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (clock);
244 GST_CAT_DEBUG (GST_CAT_CLOCK, "enter system clock thread");
245 GST_OBJECT_LOCK (clock);
246 /* signal spinup */
247 GST_CLOCK_BROADCAST (clock);
248 /* now enter our (almost) infinite loop */
249 while (!sysclock->stopping) {
250 GstClockEntry *entry;
251 GstClockReturn res;
253 /* check if something to be done */
254 while (clock->entries == NULL) {
255 GST_CAT_DEBUG (GST_CAT_CLOCK, "no clock entries, waiting..");
256 /* wait for work to do */
257 GST_CLOCK_WAIT (clock);
258 GST_CAT_DEBUG (GST_CAT_CLOCK, "got signal");
259 /* clock was stopping, exit */
260 if (sysclock->stopping)
261 goto exit;
262 }
264 /* pick the next entry */
265 entry = clock->entries->data;
266 /* if it was unscheduled, just move on to the next entry */
267 if (entry->status == GST_CLOCK_UNSCHEDULED) {
268 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p was unscheduled", entry);
269 goto next_entry;
270 }
272 /* now wait for the entry, we already hold the lock */
273 res =
274 gst_system_clock_id_wait_jitter_unlocked (clock, (GstClockID) entry,
275 NULL);
277 switch (res) {
278 case GST_CLOCK_UNSCHEDULED:
279 /* entry was unscheduled, move to the next */
280 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unscheduled", entry);
281 goto next_entry;
282 case GST_CLOCK_OK:
283 case GST_CLOCK_EARLY:
284 {
285 /* entry timed out normally, fire the callback and move to the next
286 * entry */
287 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unlocked", entry);
288 if (entry->func) {
289 /* unlock before firing the callback */
290 GST_OBJECT_UNLOCK (clock);
291 entry->func (clock, entry->time, (GstClockID) entry,
292 entry->user_data);
293 GST_OBJECT_LOCK (clock);
294 }
295 if (entry->type == GST_CLOCK_ENTRY_PERIODIC) {
296 /* adjust time now */
297 entry->time += entry->interval;
298 /* and resort the list now */
299 clock->entries =
300 g_list_sort (clock->entries, gst_clock_id_compare_func);
301 /* and restart */
302 continue;
303 } else {
304 goto next_entry;
305 }
306 }
307 case GST_CLOCK_BUSY:
308 /* somebody unlocked the entry but is was not canceled, This means that
309 * either a new entry was added in front of the queue or some other entry
310 * was canceled. Whatever it is, pick the head entry of the list and
311 * continue waiting. */
312 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p needs restart", entry);
313 continue;
314 default:
315 GST_CAT_DEBUG (GST_CAT_CLOCK,
316 "strange result %d waiting for %p, skipping", res, entry);
317 g_warning ("%s: strange result %d waiting for %p, skipping",
318 GST_OBJECT_NAME (clock), res, entry);
319 goto next_entry;
320 }
321 next_entry:
322 /* we remove the current entry and unref it */
323 clock->entries = g_list_remove (clock->entries, entry);
324 gst_clock_id_unref ((GstClockID) entry);
325 }
326 exit:
327 /* signal exit */
328 GST_CLOCK_BROADCAST (clock);
329 GST_OBJECT_UNLOCK (clock);
330 GST_CAT_DEBUG (GST_CAT_CLOCK, "exit system clock thread");
331 }
333 /* MT safe */
334 static GstClockTime
335 gst_system_clock_get_internal_time (GstClock * clock)
336 {
337 GTimeVal timeval;
339 g_get_current_time (&timeval);
341 return GST_TIMEVAL_TO_TIME (timeval);
342 }
344 static guint64
345 gst_system_clock_get_resolution (GstClock * clock)
346 {
347 return 1 * GST_USECOND;
348 }
350 /* synchronously wait on the given GstClockEntry.
351 *
352 * We do this by blocking on the global clock GCond variable with
353 * the requested time as a timeout. This allows us to unblock the
354 * entry by signaling the GCond variable.
355 *
356 * Note that signaling the global GCond unlocks all waiting entries. So
357 * we need to check if an unlocked entry has changed when it unlocks.
358 *
359 * Entries that arrive too late are simply not waited on and a
360 * GST_CLOCK_EARLY result is returned.
361 *
362 * should be called with LOCK held.
363 *
364 * MT safe.
365 */
366 static GstClockReturn
367 gst_system_clock_id_wait_jitter_unlocked (GstClock * clock,
368 GstClockEntry * entry, GstClockTimeDiff * jitter)
369 {
370 GstClockTime entryt, real, now, target;
371 GstClockTimeDiff diff;
373 /* need to call the overridden method */
374 real = GST_CLOCK_GET_CLASS (clock)->get_internal_time (clock);
375 entryt = GST_CLOCK_ENTRY_TIME (entry);
377 now = gst_clock_adjust_unlocked (clock, real);
378 if (jitter) {
379 *jitter = GST_CLOCK_DIFF (entryt, now);
380 }
381 diff = entryt - now;
382 target = gst_system_clock_get_internal_time (clock) + diff;
384 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p"
385 " target %" GST_TIME_FORMAT
386 " entry %" GST_TIME_FORMAT
387 " now %" GST_TIME_FORMAT
388 " real %" GST_TIME_FORMAT
389 " diff %" G_GINT64_FORMAT,
390 entry,
391 GST_TIME_ARGS (target),
392 GST_TIME_ARGS (entryt), GST_TIME_ARGS (now), GST_TIME_ARGS (real), diff);
394 if (diff > 0) {
395 GTimeVal tv;
397 #ifdef WAIT_DEBUGGING
398 GstClockTime result, final;
399 #endif
401 GST_TIME_TO_TIMEVAL (target, tv);
403 while (TRUE) {
404 /* now wait on the entry, it either times out or the cond is signaled. */
405 if (!GST_CLOCK_TIMED_WAIT (clock, &tv)) {
406 /* timeout, this is fine, we can report success now */
407 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked after timeout", entry);
408 entry->status = GST_CLOCK_OK;
410 #ifdef WAIT_DEBUGGING
411 real = GST_CLOCK_GET_CLASS (clock)->get_internal_time (clock);
412 result = gst_clock_adjust_unlocked (clock, real);
413 final = gst_system_clock_get_internal_time (clock);
414 GST_CAT_DEBUG (GST_CAT_CLOCK, "Waited for %" G_GINT64_FORMAT
415 " got %" G_GINT64_FORMAT " diff %" G_GINT64_FORMAT
416 " %g target-offset %" G_GINT64_FORMAT " %g", entryt, result,
417 result - entryt,
418 (double) (GstClockTimeDiff) (result - entryt) / GST_SECOND,
419 (final - target),
420 ((double) (GstClockTimeDiff) (final - target)) / GST_SECOND);
421 #endif
422 break;
423 } else {
424 /* the waiting is interrupted because the GCond was signaled. This can
425 * be because this or some other entry was unscheduled. */
426 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked with signal", entry);
427 /* if the entry is unscheduled, we can stop waiting for it, else we
428 * continue our while loop. */
429 if (entry->status == GST_CLOCK_UNSCHEDULED)
430 break;
431 }
432 }
433 } else if (diff == 0) {
434 entry->status = GST_CLOCK_OK;
435 } else {
436 entry->status = GST_CLOCK_EARLY;
437 }
438 return entry->status;
439 }
441 static GstClockReturn
442 gst_system_clock_id_wait_jitter (GstClock * clock, GstClockEntry * entry,
443 GstClockTimeDiff * jitter)
444 {
445 GstClockReturn ret;
447 GST_OBJECT_LOCK (clock);
448 ret = gst_system_clock_id_wait_jitter_unlocked (clock, entry, jitter);
449 GST_OBJECT_UNLOCK (clock);
451 return ret;
452 }
454 /* Add an entry to the list of pending async waits. The entry is inserted
455 * in sorted order. If we inserted the entry at the head of the list, we
456 * need to signal the thread as it might either be waiting on it or waiting
457 * for a new entry.
458 *
459 * MT safe.
460 */
461 static GstClockReturn
462 gst_system_clock_id_wait_async (GstClock * clock, GstClockEntry * entry)
463 {
464 GST_CAT_DEBUG (GST_CAT_CLOCK, "adding entry %p", entry);
466 GST_OBJECT_LOCK (clock);
467 /* need to take a ref */
468 gst_clock_id_ref ((GstClockID) entry);
469 /* insert the entry in sorted order */
470 clock->entries = g_list_insert_sorted (clock->entries, entry,
471 gst_clock_id_compare_func);
473 /* only need to send the signal if the entry was added to the
474 * front, else the thread is just waiting for another entry and
475 * will get to this entry automatically. */
476 if (clock->entries->data == entry) {
477 GST_CAT_DEBUG (GST_CAT_CLOCK, "send signal");
478 GST_CLOCK_BROADCAST (clock);
479 }
480 GST_OBJECT_UNLOCK (clock);
482 return GST_CLOCK_OK;
483 }
485 /* unschedule an entry. This will set the state of the entry to GST_CLOCK_UNSCHEDULED
486 * and will signal any thread waiting for entries to recheck their entry.
487 * We cannot really decide if the signal is needed or not because the entry
488 * could be waited on in async or sync mode.
489 *
490 * MT safe.
491 */
492 static void
493 gst_system_clock_id_unschedule (GstClock * clock, GstClockEntry * entry)
494 {
495 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
497 GST_OBJECT_LOCK (clock);
498 entry->status = GST_CLOCK_UNSCHEDULED;
499 GST_CAT_DEBUG (GST_CAT_CLOCK, "send signal");
500 GST_CLOCK_BROADCAST (clock);
501 GST_OBJECT_UNLOCK (clock);
502 }