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 gboolean restart);
63 static GstClockReturn gst_system_clock_id_wait_async (GstClock * clock,
64 GstClockEntry * entry);
65 static void gst_system_clock_id_unschedule (GstClock * clock,
66 GstClockEntry * entry);
67 static void gst_system_clock_async_thread (GstClock * clock);
69 static GStaticMutex _gst_sysclock_mutex = G_STATIC_MUTEX_INIT;
71 static GstClockClass *parent_class = NULL;
73 /* static guint gst_system_clock_signals[LAST_SIGNAL] = { 0 }; */
75 GType
76 gst_system_clock_get_type (void)
77 {
78 static GType clock_type = 0;
80 if (G_UNLIKELY (clock_type == 0)) {
81 static const GTypeInfo clock_info = {
82 sizeof (GstSystemClockClass),
83 NULL,
84 NULL,
85 (GClassInitFunc) gst_system_clock_class_init,
86 NULL,
87 NULL,
88 sizeof (GstSystemClock),
89 0,
90 (GInstanceInitFunc) gst_system_clock_init,
91 NULL
92 };
94 clock_type = g_type_register_static (GST_TYPE_CLOCK, "GstSystemClock",
95 &clock_info, 0);
96 }
97 return clock_type;
98 }
100 static void
101 gst_system_clock_class_init (GstSystemClockClass * klass)
102 {
103 GObjectClass *gobject_class;
104 GstObjectClass *gstobject_class;
105 GstClockClass *gstclock_class;
107 gobject_class = (GObjectClass *) klass;
108 gstobject_class = (GstObjectClass *) klass;
109 gstclock_class = (GstClockClass *) klass;
111 parent_class = g_type_class_peek_parent (klass);
113 gobject_class->dispose = gst_system_clock_dispose;
115 gstclock_class->get_internal_time = gst_system_clock_get_internal_time;
116 gstclock_class->get_resolution = gst_system_clock_get_resolution;
117 gstclock_class->wait_jitter = gst_system_clock_id_wait_jitter;
118 gstclock_class->wait_async = gst_system_clock_id_wait_async;
119 gstclock_class->unschedule = gst_system_clock_id_unschedule;
120 }
122 static void
123 gst_system_clock_init (GstSystemClock * clock)
124 {
125 GError *error = NULL;
127 GST_OBJECT_FLAG_SET (clock,
128 GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC |
129 GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC |
130 GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC |
131 GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC);
133 GST_OBJECT_LOCK (clock);
134 clock->thread = g_thread_create ((GThreadFunc) gst_system_clock_async_thread,
135 clock, TRUE, &error);
136 if (error)
137 goto no_thread;
139 /* wait for it to spin up */
140 GST_CLOCK_WAIT (clock);
141 GST_OBJECT_UNLOCK (clock);
142 return;
144 /* ERRORS */
145 no_thread:
146 {
147 g_warning ("could not create async clock thread: %s", error->message);
148 GST_OBJECT_UNLOCK (clock);
149 }
150 }
152 static void
153 gst_system_clock_dispose (GObject * object)
154 {
155 GstClock *clock = (GstClock *) object;
157 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (clock);
158 GList *entries;
160 /* else we have to stop the thread */
161 GST_OBJECT_LOCK (clock);
162 sysclock->stopping = TRUE;
163 /* unschedule all entries */
164 for (entries = clock->entries; entries; entries = g_list_next (entries)) {
165 GstClockEntry *entry = (GstClockEntry *) entries->data;
167 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
168 entry->status = GST_CLOCK_UNSCHEDULED;
169 }
170 g_list_free (clock->entries);
171 clock->entries = NULL;
172 GST_CLOCK_BROADCAST (clock);
173 GST_OBJECT_UNLOCK (clock);
175 if (sysclock->thread)
176 g_thread_join (sysclock->thread);
177 sysclock->thread = NULL;
178 GST_CAT_DEBUG (GST_CAT_CLOCK, "joined thread");
180 G_OBJECT_CLASS (parent_class)->dispose (object);
182 if (_the_system_clock == clock) {
183 _the_system_clock = NULL;
184 GST_CAT_DEBUG (GST_CAT_CLOCK, "disposed system clock");
185 }
186 }
188 /**
189 * gst_system_clock_obtain:
190 *
191 * Get a handle to the default system clock. The refcount of the
192 * clock will be increased so you need to unref the clock after
193 * usage.
194 *
195 * Returns: the default clock.
196 *
197 * MT safe.
198 */
199 GstClock *
200 gst_system_clock_obtain (void)
201 {
202 GstClock *clock;
204 g_static_mutex_lock (&_gst_sysclock_mutex);
205 clock = _the_system_clock;
207 if (clock == NULL) {
208 GST_CAT_DEBUG (GST_CAT_CLOCK, "creating new static system clock");
209 clock = g_object_new (GST_TYPE_SYSTEM_CLOCK,
210 "name", "GstSystemClock", NULL);
212 /* we created the global clock; take ownership so
213 * we can hand out instances later */
214 gst_object_ref (clock);
215 gst_object_sink (GST_OBJECT (clock));
217 _the_system_clock = clock;
218 g_static_mutex_unlock (&_gst_sysclock_mutex);
219 } else {
220 g_static_mutex_unlock (&_gst_sysclock_mutex);
221 GST_CAT_DEBUG (GST_CAT_CLOCK, "returning static system clock");
222 }
224 /* we ref it since we are a clock factory. */
225 gst_object_ref (clock);
226 return clock;
227 }
229 /* this thread reads the sorted clock entries from the queue.
230 *
231 * It waits on each of them and fires the callback when the timeout occurs.
232 *
233 * When an entry in the queue was canceled before we wait for it, it is
234 * simply skipped.
235 *
236 * When waiting for an entry, it can become canceled, in that case we don't
237 * call the callback but move to the next item in the queue.
238 *
239 * MT safe.
240 */
241 static void
242 gst_system_clock_async_thread (GstClock * clock)
243 {
244 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (clock);
246 GST_CAT_DEBUG (GST_CAT_CLOCK, "enter system clock thread");
247 GST_OBJECT_LOCK (clock);
248 /* signal spinup */
249 GST_CLOCK_BROADCAST (clock);
250 /* now enter our (almost) infinite loop */
251 while (!sysclock->stopping) {
252 GstClockEntry *entry;
253 GstClockTime requested;
254 GstClockReturn res;
256 /* check if something to be done */
257 while (clock->entries == NULL) {
258 GST_CAT_DEBUG (GST_CAT_CLOCK, "no clock entries, waiting..");
259 /* wait for work to do */
260 GST_CLOCK_WAIT (clock);
261 GST_CAT_DEBUG (GST_CAT_CLOCK, "got signal");
262 /* clock was stopping, exit */
263 if (sysclock->stopping)
264 goto exit;
265 }
267 /* pick the next entry */
268 entry = clock->entries->data;
269 /* if it was unscheduled, just move on to the next entry */
270 if (entry->status == GST_CLOCK_UNSCHEDULED) {
271 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p was unscheduled", entry);
272 goto next_entry;
273 }
275 requested = entry->time;
277 /* now wait for the entry, we already hold the lock */
278 res =
279 gst_system_clock_id_wait_jitter_unlocked (clock, (GstClockID) entry,
280 NULL, FALSE);
282 switch (res) {
283 case GST_CLOCK_UNSCHEDULED:
284 /* entry was unscheduled, move to the next */
285 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unscheduled", entry);
286 goto next_entry;
287 case GST_CLOCK_OK:
288 case GST_CLOCK_EARLY:
289 {
290 /* entry timed out normally, fire the callback and move to the next
291 * entry */
292 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unlocked", entry);
293 if (entry->func) {
294 /* unlock before firing the callback */
295 GST_OBJECT_UNLOCK (clock);
296 entry->func (clock, entry->time, (GstClockID) entry,
297 entry->user_data);
298 GST_OBJECT_LOCK (clock);
299 }
300 if (entry->type == GST_CLOCK_ENTRY_PERIODIC) {
301 /* adjust time now */
302 entry->time = requested + entry->interval;
303 /* and resort the list now */
304 clock->entries =
305 g_list_sort (clock->entries, gst_clock_id_compare_func);
306 /* and restart */
307 continue;
308 } else {
309 goto next_entry;
310 }
311 }
312 case GST_CLOCK_BUSY:
313 /* somebody unlocked the entry but is was not canceled, This means that
314 * either a new entry was added in front of the queue or some other entry
315 * was canceled. Whatever it is, pick the head entry of the list and
316 * continue waiting. */
317 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p needs restart", entry);
318 continue;
319 default:
320 GST_CAT_DEBUG (GST_CAT_CLOCK,
321 "strange result %d waiting for %p, skipping", res, entry);
322 g_warning ("%s: strange result %d waiting for %p, skipping",
323 GST_OBJECT_NAME (clock), res, entry);
324 goto next_entry;
325 }
326 next_entry:
327 /* we remove the current entry and unref it */
328 clock->entries = g_list_remove (clock->entries, entry);
329 gst_clock_id_unref ((GstClockID) entry);
330 }
331 exit:
332 /* signal exit */
333 GST_CLOCK_BROADCAST (clock);
334 GST_OBJECT_UNLOCK (clock);
335 GST_CAT_DEBUG (GST_CAT_CLOCK, "exit system clock thread");
336 }
338 /* MT safe */
339 static GstClockTime
340 gst_system_clock_get_internal_time (GstClock * clock)
341 {
342 GTimeVal timeval;
344 g_get_current_time (&timeval);
346 return GST_TIMEVAL_TO_TIME (timeval);
347 }
349 static guint64
350 gst_system_clock_get_resolution (GstClock * clock)
351 {
352 return 1 * GST_USECOND;
353 }
355 /* synchronously wait on the given GstClockEntry.
356 *
357 * We do this by blocking on the global clock GCond variable with
358 * the requested time as a timeout. This allows us to unblock the
359 * entry by signaling the GCond variable.
360 *
361 * Note that signaling the global GCond unlocks all waiting entries. So
362 * we need to check if an unlocked entry has changed when it unlocks.
363 *
364 * Entries that arrive too late are simply not waited on and a
365 * GST_CLOCK_EARLY result is returned.
366 *
367 * should be called with LOCK held.
368 *
369 * MT safe.
370 */
371 static GstClockReturn
372 gst_system_clock_id_wait_jitter_unlocked (GstClock * clock,
373 GstClockEntry * entry, GstClockTimeDiff * jitter, gboolean restart)
374 {
375 GstClockTime entryt, real, now, target;
376 GstClockTimeDiff diff;
378 /* need to call the overridden method */
379 real = GST_CLOCK_GET_CLASS (clock)->get_internal_time (clock);
380 entryt = GST_CLOCK_ENTRY_TIME (entry);
382 now = gst_clock_adjust_unlocked (clock, real);
383 if (jitter) {
384 *jitter = GST_CLOCK_DIFF (entryt, now);
385 }
386 diff = entryt - now;
387 target = gst_system_clock_get_internal_time (clock) + diff;
389 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p"
390 " target %" GST_TIME_FORMAT
391 " entry %" GST_TIME_FORMAT
392 " now %" GST_TIME_FORMAT
393 " real %" GST_TIME_FORMAT
394 " diff %" G_GINT64_FORMAT,
395 entry,
396 GST_TIME_ARGS (target),
397 GST_TIME_ARGS (entryt), GST_TIME_ARGS (now), GST_TIME_ARGS (real), diff);
399 if (diff > 0) {
400 GTimeVal tv;
402 #ifdef WAIT_DEBUGGING
403 GstClockTime result, final;
404 #endif
406 GST_TIME_TO_TIMEVAL (target, tv);
408 while (TRUE) {
409 /* now wait on the entry, it either times out or the cond is signaled. */
410 if (!GST_CLOCK_TIMED_WAIT (clock, &tv)) {
411 /* timeout, this is fine, we can report success now */
412 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked after timeout", entry);
413 entry->status = GST_CLOCK_OK;
415 #ifdef WAIT_DEBUGGING
416 real = GST_CLOCK_GET_CLASS (clock)->get_internal_time (clock);
417 result = gst_clock_adjust_unlocked (clock, real);
418 final = gst_system_clock_get_internal_time (clock);
419 GST_CAT_DEBUG (GST_CAT_CLOCK, "Waited for %" G_GINT64_FORMAT
420 " got %" G_GINT64_FORMAT " diff %" G_GINT64_FORMAT
421 " %g target-offset %" G_GINT64_FORMAT " %g", entryt, result,
422 result - entryt,
423 (double) (GstClockTimeDiff) (result - entryt) / GST_SECOND,
424 (final - target),
425 ((double) (GstClockTimeDiff) (final - target)) / GST_SECOND);
426 #endif
427 break;
428 } else {
429 /* the waiting is interrupted because the GCond was signaled. This can
430 * be because this or some other entry was unscheduled. */
431 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked with signal", entry);
432 /* if the entry is unscheduled, we can stop waiting for it, else we
433 * continue our while loop. */
434 if (entry->status == GST_CLOCK_UNSCHEDULED)
435 break;
436 /* else restart if we must */
437 if (!restart)
438 break;
439 }
440 }
441 } else if (diff == 0) {
442 entry->status = GST_CLOCK_OK;
443 } else {
444 entry->status = GST_CLOCK_EARLY;
445 }
446 return entry->status;
447 }
449 static GstClockReturn
450 gst_system_clock_id_wait_jitter (GstClock * clock, GstClockEntry * entry,
451 GstClockTimeDiff * jitter)
452 {
453 GstClockReturn ret;
455 GST_OBJECT_LOCK (clock);
456 ret = gst_system_clock_id_wait_jitter_unlocked (clock, entry, jitter, FALSE);
457 GST_OBJECT_UNLOCK (clock);
459 return ret;
460 }
462 /* Add an entry to the list of pending async waits. The entry is inserted
463 * in sorted order. If we inserted the entry at the head of the list, we
464 * need to signal the thread as it might either be waiting on it or waiting
465 * for a new entry.
466 *
467 * MT safe.
468 */
469 static GstClockReturn
470 gst_system_clock_id_wait_async (GstClock * clock, GstClockEntry * entry)
471 {
472 GST_CAT_DEBUG (GST_CAT_CLOCK, "adding entry %p", entry);
474 GST_OBJECT_LOCK (clock);
475 /* need to take a ref */
476 gst_clock_id_ref ((GstClockID) entry);
477 /* insert the entry in sorted order */
478 clock->entries = g_list_insert_sorted (clock->entries, entry,
479 gst_clock_id_compare_func);
481 /* only need to send the signal if the entry was added to the
482 * front, else the thread is just waiting for another entry and
483 * will get to this entry automatically. */
484 if (clock->entries->data == entry) {
485 GST_CAT_DEBUG (GST_CAT_CLOCK, "send signal");
486 GST_CLOCK_BROADCAST (clock);
487 }
488 GST_OBJECT_UNLOCK (clock);
490 return GST_CLOCK_OK;
491 }
493 /* unschedule an entry. This will set the state of the entry to GST_CLOCK_UNSCHEDULED
494 * and will signal any thread waiting for entries to recheck their entry.
495 * We cannot really decide if the signal is needed or not because the entry
496 * could be waited on in async or sync mode.
497 *
498 * MT safe.
499 */
500 static void
501 gst_system_clock_id_unschedule (GstClock * clock, GstClockEntry * entry)
502 {
503 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
505 GST_OBJECT_LOCK (clock);
506 entry->status = GST_CLOCK_UNSCHEDULED;
507 GST_CAT_DEBUG (GST_CAT_CLOCK, "send signal");
508 GST_CLOCK_BROADCAST (clock);
509 GST_OBJECT_UNLOCK (clock);
510 }