2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstclock.c: Clock subsystem for maintaining time sync
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.
25 /* #define GST_DEBUG_ENABLED */
26 #include "gst_private.h"
30 #define CLASS(clock) GST_CLOCK_CLASS (G_OBJECT_GET_CLASS (clock))
32 static GMemChunk *_gst_clock_entries_chunk;
33 static GMutex *_gst_clock_entries_chunk_lock;
34 static GList *_gst_clock_entries_pool;
36 static void gst_clock_class_init (GstClockClass *klass);
37 static void gst_clock_init (GstClock *clock);
40 static GstObjectClass *parent_class = NULL;
41 /* static guint gst_clock_signals[LAST_SIGNAL] = { 0 }; */
43 typedef struct _GstClockEntry GstClockEntry;
45 static void gst_clock_free_entry (GstClock *clock, GstClockEntry *entry);
52 struct _GstClockEntry {
54 GstEntryStatus status;
55 GstClockCallback func;
61 #define GST_CLOCK_ENTRY(entry) ((GstClockEntry *)(entry))
62 #define GST_CLOCK_ENTRY_TIME(entry) (((GstClockEntry *)(entry))->time)
63 #define GST_CLOCK_ENTRY_LOCK(entry) (g_mutex_lock ((entry)->lock))
64 #define GST_CLOCK_ENTRY_UNLOCK(entry) (g_mutex_unlock ((entry)->lock))
65 #define GST_CLOCK_ENTRY_SIGNAL(entry) (g_cond_signal ((entry)->cond))
66 #define GST_CLOCK_ENTRY_WAIT(entry) (g_cond_wait (entry->cond, entry->lock))
67 #define GST_CLOCK_ENTRY_TIMED_WAIT(entry, time) (g_cond_timed_wait (entry->cond, entry->lock, (time)))
70 gst_clock_entry_new (GstClockTime time,
71 GstClockCallback func, gpointer user_data)
75 g_mutex_lock (_gst_clock_entries_chunk_lock);
76 if (_gst_clock_entries_pool) {
77 entry = GST_CLOCK_ENTRY (_gst_clock_entries_pool->data);
79 _gst_clock_entries_pool = g_list_remove (_gst_clock_entries_pool, entry);
80 g_mutex_unlock (_gst_clock_entries_chunk_lock);
83 entry = g_mem_chunk_alloc (_gst_clock_entries_chunk);
84 g_mutex_unlock (_gst_clock_entries_chunk_lock);
86 entry->lock = g_mutex_new ();
87 entry->cond = g_cond_new ();
91 entry->user_data = user_data;
97 clock_compare_func (gconstpointer a,
100 GstClockEntry *entry1 = (GstClockEntry *)a;
101 GstClockEntry *entry2 = (GstClockEntry *)b;
103 return (entry1->time - entry2->time);
107 gst_clock_get_type (void)
109 static GType clock_type = 0;
112 static const GTypeInfo clock_info = {
113 sizeof (GstClockClass),
116 (GClassInitFunc) gst_clock_class_init,
121 (GInstanceInitFunc) gst_clock_init,
124 clock_type = g_type_register_static (GST_TYPE_OBJECT, "GstClock",
125 &clock_info, G_TYPE_FLAG_ABSTRACT);
131 gst_clock_class_init (GstClockClass *klass)
133 GObjectClass *gobject_class;
134 GstObjectClass *gstobject_class;
136 gobject_class = (GObjectClass*) klass;
137 gstobject_class = (GstObjectClass*) klass;
139 parent_class = g_type_class_ref (GST_TYPE_OBJECT);
141 if (!g_thread_supported ())
142 g_thread_init (NULL);
144 _gst_clock_entries_chunk = g_mem_chunk_new ("GstClockEntries",
145 sizeof (GstClockEntry), sizeof (GstClockEntry) * 32,
147 _gst_clock_entries_chunk_lock = g_mutex_new ();
148 _gst_clock_entries_pool = NULL;
152 gst_clock_init (GstClock *clock)
155 clock->active = FALSE;
156 clock->start_time = 0;
157 clock->last_time = 0;
158 clock->entries = NULL;
159 clock->async_supported = FALSE;
161 clock->active_mutex = g_mutex_new ();
162 clock->active_cond = g_cond_new ();
166 * gst_clock_async_supported
167 * @clock: a #GstClock to query
169 * Checks if this clock can support asynchronous notification.
171 * Returns: TRUE if async notification is supported.
174 gst_clock_async_supported (GstClock *clock)
176 g_return_val_if_fail (GST_IS_CLOCK (clock), FALSE);
178 return clock->async_supported;
182 * gst_clock_set_speed
183 * @clock: a #GstClock to modify
184 * @speed: the speed to set on the clock
186 * Sets the speed on the given clock. 1.0 is the default
190 gst_clock_set_speed (GstClock *clock, gdouble speed)
192 g_return_if_fail (GST_IS_CLOCK (clock));
194 clock->speed = speed;
198 * gst_clock_get_speed
199 * @clock: a #GstClock to query
201 * Gets the speed of the given clock.
203 * Returns: the speed of the clock.
206 gst_clock_get_speed (GstClock *clock)
208 g_return_val_if_fail (GST_IS_CLOCK (clock), 0.0);
216 * @clock: a #GstClock to reset
218 * Reset the clock to time 0.
221 gst_clock_reset (GstClock *clock)
223 GstClockTime time = 0LL;
225 g_return_if_fail (GST_IS_CLOCK (clock));
227 if (CLASS (clock)->get_internal_time) {
228 time = CLASS (clock)->get_internal_time (clock);
232 clock->active = FALSE;
233 clock->start_time = time;
234 clock->last_time = 0LL;
239 * gst_clock_set_active
240 * @clock: a #GstClock to set state of
241 * @active: flag indicating if the clock should be activated (TRUE) or deactivated
243 * Activates or deactivates the clock based on the active parameter.
244 * As soon as the clock is activated, the time will start ticking.
247 gst_clock_set_active (GstClock *clock, gboolean active)
249 GstClockTime time = 0LL;
251 g_return_if_fail (GST_IS_CLOCK (clock));
253 clock->active = active;
255 if (CLASS (clock)->get_internal_time) {
256 time = CLASS (clock)->get_internal_time (clock);
261 clock->start_time = time - clock->last_time;;
264 clock->last_time = time - clock->start_time;
268 g_mutex_lock (clock->active_mutex);
269 g_cond_broadcast (clock->active_cond);
270 g_mutex_unlock (clock->active_mutex);
274 * gst_clock_is_active
275 * @clock: a #GstClock to query
277 * Checks if the given clock is active.
279 * Returns: TRUE if the clock is active.
282 gst_clock_is_active (GstClock *clock)
284 g_return_val_if_fail (GST_IS_CLOCK (clock), FALSE);
286 return clock->active;
291 * @clock: a #GstClock to query
293 * Gets the current time of the given clock. The time is always
294 * monotonically increasing.
296 * Returns: the time of the clock.
299 gst_clock_get_time (GstClock *clock)
301 GstClockTime ret = 0LL;
303 g_return_val_if_fail (GST_IS_CLOCK (clock), 0LL);
305 if (!clock->active) {
306 /* clock is not activen return previous time */
307 ret = clock->last_time;
310 if (CLASS (clock)->get_internal_time) {
311 ret = CLASS (clock)->get_internal_time (clock) - clock->start_time;
313 /* make sure the time is increasing, else return last_time */
314 if (ret < clock->last_time) {
315 ret = clock->last_time;
318 clock->last_time = ret;
326 gst_clock_wait_async_func (GstClock *clock, GstClockTime time,
327 GstClockCallback func, gpointer user_data)
329 GstClockEntry *entry = NULL;
330 g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);
332 if (!clock->active) {
333 GST_DEBUG (GST_CAT_CLOCK, "blocking on clock\n");
334 g_mutex_lock (clock->active_mutex);
335 g_cond_wait (clock->active_cond, clock->active_mutex);
336 g_mutex_unlock (clock->active_mutex);
339 entry = gst_clock_entry_new (time, func, user_data);
342 clock->entries = g_list_insert_sorted (clock->entries, entry, clock_compare_func);
350 * @clock: a #GstClock to wait on
351 * @time: The #GstClockTime to wait for
353 * Wait and block till the clock reaches the specified time.
355 * Returns: the #GstClockReturn result of the operation.
358 gst_clock_wait (GstClock *clock, GstClockTime time)
363 g_return_val_if_fail (GST_IS_CLOCK (clock), GST_CLOCK_STOPPED);
365 id = gst_clock_wait_async_func (clock, time, NULL, NULL);
366 res = gst_clock_wait_id (clock, id);
372 * gst_clock_wait_async
373 * @clock: a #GstClock to wait on
374 * @time: The #GstClockTime to wait for
375 * @func: The callback function
376 * @user_data: User data passed in the calback
378 * Register a callback on the given clock that will be triggered
379 * when the clock has reached the given time. A ClockID is returned
380 * that can be used to cancel the request.
382 * Returns: the clock id or NULL when async notification is not supported.
385 gst_clock_wait_async (GstClock *clock, GstClockTime time,
386 GstClockCallback func, gpointer user_data)
388 g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);
390 if (clock->async_supported) {
391 return gst_clock_wait_async_func (clock, time, func, user_data);
397 * gst_clock_cancel_wait_async
398 * @clock: The clock to cancel the request on
399 * @id: The id to cancel
401 * Cancel an outstanding async notification request with the given ID.
404 gst_clock_cancel_wait_async (GstClock *clock, GstClockID id)
406 g_warning ("not supported");
410 * gst_clock_notify_async
411 * @clock: The clock to wait on
412 * @interval: The interval between notifications
413 * @func: The callback function
414 * @user_data: User data passed in the calback
416 * Register a callback on the given clock that will be periodically
417 * triggered with the specified interval. A ClockID is returned
418 * that can be used to cancel the request.
420 * Returns: the clock id or NULL when async notification is not supported.
423 gst_clock_notify_async (GstClock *clock, GstClockTime interval,
424 GstClockCallback func, gpointer user_data)
426 g_warning ("not supported");
431 * gst_clock_remove_notify_async
432 * @clock: The clock to cancel the request on
433 * @id: The id to cancel
435 * Cancel an outstanding async notification request with the given ID.
438 gst_clock_remove_notify_async (GstClock *clock, GstClockID id)
440 g_warning ("not supported");
444 gst_clock_unlock_func (GstClock *clock, GstClockTime time, GstClockID id, gpointer user_data)
446 GstClockEntry *entry = (GstClockEntry *) id;
448 GST_CLOCK_ENTRY_LOCK (entry);
449 GST_CLOCK_ENTRY_SIGNAL (entry);
450 GST_CLOCK_ENTRY_UNLOCK (entry);
455 * @clock: The clock to wait on
456 * @id: The clock id to wait on
458 * Wait and block on the clockid obtained with gst_clock_wait_async.
460 * Returns: result of the operation.
463 gst_clock_wait_id (GstClock *clock, GstClockID id)
465 GstClockReturn res = GST_CLOCK_TIMEOUT;
466 GstClockEntry *entry = (GstClockEntry *) id;
467 GstClockTime current_real, current, target;
470 g_return_val_if_fail (GST_IS_CLOCK (clock), GST_CLOCK_ERROR);
471 g_return_val_if_fail (entry, GST_CLOCK_ERROR);
473 current = gst_clock_get_time (clock);
475 g_get_current_time (&timeval);
476 current_real = GST_TIMEVAL_TO_TIME (timeval);
478 GST_CLOCK_ENTRY_LOCK (entry);
479 entry->func = gst_clock_unlock_func;
480 target = GST_CLOCK_ENTRY_TIME (entry) - current + current_real;
482 GST_DEBUG (GST_CAT_CLOCK, "%llu %llu %llu\n", target, current, current_real);
484 if (target > current_real) {
485 timeval.tv_usec = target % 1000000;
486 timeval.tv_sec = target / 1000000;
488 GST_CLOCK_ENTRY_TIMED_WAIT (entry, &timeval);
490 GST_CLOCK_ENTRY_UNLOCK (entry);
492 gst_clock_free_entry (clock, entry);
498 * gst_clock_get_next_id
499 * @clock: The clock to query
501 * Get the clockid of the next event.
503 * Returns: a clockid or NULL is no event is pending.
506 gst_clock_get_next_id (GstClock *clock)
508 GstClockEntry *entry = NULL;
512 entry = GST_CLOCK_ENTRY (clock->entries->data);
515 return (GstClockID *) entry;
519 * gst_clock_id_get_time
520 * @id: The clockid to query
522 * Get the time of the clock ID
524 * Returns: the time of the given clock id
527 gst_clock_id_get_time (GstClockID id)
529 return GST_CLOCK_ENTRY_TIME (id);
533 gst_clock_free_entry (GstClock *clock, GstClockEntry *entry)
536 clock->entries = g_list_remove (clock->entries, entry);
539 g_mutex_lock (_gst_clock_entries_chunk_lock);
540 _gst_clock_entries_pool = g_list_prepend (_gst_clock_entries_pool, entry);
541 g_mutex_unlock (_gst_clock_entries_chunk_lock);
545 * gst_clock_unlock_id
546 * @clock: The clock that own the id
547 * @id: The clockid to unlock
549 * Unlock the ClockID.
552 gst_clock_unlock_id (GstClock *clock, GstClockID id)
554 GstClockEntry *entry = (GstClockEntry *) id;
557 entry->func (clock, gst_clock_get_time (clock), id, entry->user_data);
559 gst_clock_free_entry (clock, entry);
563 * gst_clock_set_resolution
564 * @clock: The clock set the resolution on
565 * @resolution: The resolution to set
567 * Set the accuracy of the clock.
570 gst_clock_set_resolution (GstClock *clock, guint64 resolution)
572 g_return_if_fail (GST_IS_CLOCK (clock));
574 if (CLASS (clock)->set_resolution)
575 CLASS (clock)->set_resolution (clock, resolution);
579 * gst_clock_get_resolution
580 * @clock: The clock get the resolution of
582 * Get the accuracy of the clock.
584 * Returns: the resolution of the clock in microseconds.
587 gst_clock_get_resolution (GstClock *clock)
589 g_return_val_if_fail (GST_IS_CLOCK (clock), 0LL);
591 if (CLASS (clock)->get_resolution)
592 return CLASS (clock)->get_resolution (clock);