2002-02-03 20:07:09 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
2005-03-07 18:27:42 +00:00
|
|
|
* 2004 Wim Taymans <wim@fluendo.com>
|
2002-02-03 20:07:09 +00:00
|
|
|
*
|
2003-07-16 15:49:40 +00:00
|
|
|
* gstsystemclock.c: Default clock, uses the system clock
|
2002-02-03 20:07:09 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
2005-10-15 16:01:57 +00:00
|
|
|
|
2005-09-02 16:17:23 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstsystemclock
|
|
|
|
* @short_description: Default clock that uses the current system time
|
|
|
|
* @see_also: #GstClock
|
|
|
|
*
|
2005-10-28 18:14:24 +00:00
|
|
|
* The GStreamer core provides a GstSystemClock based on the system time.
|
|
|
|
* Asynchronous callbacks are scheduled from an internal thread.
|
|
|
|
*
|
|
|
|
* Clock implementors are encouraged to subclass this systemclock as it
|
|
|
|
* implements the async notification.
|
|
|
|
*
|
|
|
|
* Subclasses can however override all of the important methods for sync and
|
|
|
|
* async notifications to implement their own callback methods or blocking
|
|
|
|
* wait operations.
|
|
|
|
*
|
|
|
|
* Last reviewed on 2005-10-28 (0.9.4)
|
2005-09-02 16:17:23 +00:00
|
|
|
*/
|
2002-02-03 20:07:09 +00:00
|
|
|
|
|
|
|
#include "gst_private.h"
|
2003-06-29 14:05:49 +00:00
|
|
|
#include "gstinfo.h"
|
2002-02-03 20:07:09 +00:00
|
|
|
|
|
|
|
#include "gstsystemclock.h"
|
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
/* the one instance of the systemclock */
|
2002-02-03 20:07:09 +00:00
|
|
|
static GstClock *_the_system_clock = NULL;
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
static void gst_system_clock_class_init (GstSystemClockClass * klass);
|
|
|
|
static void gst_system_clock_init (GstSystemClock * clock);
|
|
|
|
static void gst_system_clock_dispose (GObject * object);
|
2002-02-03 20:07:09 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
static GstClockTime gst_system_clock_get_internal_time (GstClock * clock);
|
|
|
|
static guint64 gst_system_clock_get_resolution (GstClock * clock);
|
2005-03-07 18:27:42 +00:00
|
|
|
static GstClockReturn gst_system_clock_id_wait (GstClock * clock,
|
|
|
|
GstClockEntry * entry);
|
|
|
|
static GstClockReturn gst_system_clock_id_wait_unlocked
|
|
|
|
(GstClock * clock, GstClockEntry * entry);
|
|
|
|
static GstClockReturn gst_system_clock_id_wait_async (GstClock * clock,
|
|
|
|
GstClockEntry * entry);
|
|
|
|
static void gst_system_clock_id_unschedule (GstClock * clock,
|
2004-03-13 15:27:01 +00:00
|
|
|
GstClockEntry * entry);
|
2005-03-07 18:27:42 +00:00
|
|
|
static void gst_system_clock_async_thread (GstClock * clock);
|
2002-02-03 20:07:09 +00:00
|
|
|
|
2003-05-10 14:38:48 +00:00
|
|
|
static GStaticMutex _gst_sysclock_mutex = G_STATIC_MUTEX_INIT;
|
2002-02-03 20:07:09 +00:00
|
|
|
|
|
|
|
static GstClockClass *parent_class = NULL;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2002-02-03 20:07:09 +00:00
|
|
|
/* static guint gst_system_clock_signals[LAST_SIGNAL] = { 0 }; */
|
|
|
|
|
|
|
|
GType
|
|
|
|
gst_system_clock_get_type (void)
|
|
|
|
{
|
|
|
|
static GType clock_type = 0;
|
|
|
|
|
|
|
|
if (!clock_type) {
|
|
|
|
static const GTypeInfo clock_info = {
|
|
|
|
sizeof (GstSystemClockClass),
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
(GClassInitFunc) gst_system_clock_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof (GstSystemClock),
|
2004-04-09 19:05:03 +00:00
|
|
|
0,
|
2002-02-03 20:07:09 +00:00
|
|
|
(GInstanceInitFunc) gst_system_clock_init,
|
|
|
|
NULL
|
|
|
|
};
|
2004-03-15 19:27:17 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
clock_type = g_type_register_static (GST_TYPE_CLOCK, "GstSystemClock",
|
2004-03-15 19:27:17 +00:00
|
|
|
&clock_info, 0);
|
2002-02-03 20:07:09 +00:00
|
|
|
}
|
|
|
|
return clock_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_system_clock_class_init (GstSystemClockClass * klass)
|
2002-02-03 20:07:09 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstObjectClass *gstobject_class;
|
|
|
|
GstClockClass *gstclock_class;
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gstobject_class = (GstObjectClass *) klass;
|
|
|
|
gstclock_class = (GstClockClass *) klass;
|
2002-02-03 20:07:09 +00:00
|
|
|
|
|
|
|
parent_class = g_type_class_ref (GST_TYPE_CLOCK);
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
gobject_class->dispose = gst_system_clock_dispose;
|
2003-02-02 19:19:39 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
gstclock_class->get_internal_time = gst_system_clock_get_internal_time;
|
|
|
|
gstclock_class->get_resolution = gst_system_clock_get_resolution;
|
2005-03-07 18:27:42 +00:00
|
|
|
gstclock_class->wait = gst_system_clock_id_wait;
|
|
|
|
gstclock_class->wait_async = gst_system_clock_id_wait_async;
|
|
|
|
gstclock_class->unschedule = gst_system_clock_id_unschedule;
|
2002-02-03 20:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_system_clock_init (GstSystemClock * clock)
|
2002-02-03 20:07:09 +00:00
|
|
|
{
|
2005-03-07 18:27:42 +00:00
|
|
|
GError *error = NULL;
|
|
|
|
|
2005-11-21 17:12:50 +00:00
|
|
|
GST_OBJECT_FLAG_SET (clock,
|
2005-03-07 18:27:42 +00:00
|
|
|
GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC |
|
|
|
|
GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC |
|
|
|
|
GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC |
|
2005-11-21 17:12:50 +00:00
|
|
|
GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC);
|
2005-03-07 18:27:42 +00:00
|
|
|
|
2005-11-21 16:34:26 +00:00
|
|
|
GST_OBJECT_LOCK (clock);
|
2005-03-07 18:27:42 +00:00
|
|
|
clock->thread = g_thread_create ((GThreadFunc) gst_system_clock_async_thread,
|
|
|
|
clock, TRUE, &error);
|
|
|
|
if (error)
|
|
|
|
goto no_thread;
|
|
|
|
|
|
|
|
/* wait for it to spin up */
|
|
|
|
GST_CLOCK_WAIT (clock);
|
2005-11-21 16:34:26 +00:00
|
|
|
GST_OBJECT_UNLOCK (clock);
|
2005-03-07 18:27:42 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
no_thread:
|
|
|
|
{
|
|
|
|
g_warning ("could not create async clock thread: %s", error->message);
|
2005-11-21 16:34:26 +00:00
|
|
|
GST_OBJECT_UNLOCK (clock);
|
2005-03-07 18:27:42 +00:00
|
|
|
}
|
2002-02-03 20:07:09 +00:00
|
|
|
}
|
|
|
|
|
2003-02-02 19:19:39 +00:00
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_system_clock_dispose (GObject * object)
|
2003-02-02 19:19:39 +00:00
|
|
|
{
|
2003-05-10 11:11:29 +00:00
|
|
|
GstClock *clock = (GstClock *) object;
|
2003-02-02 19:19:39 +00:00
|
|
|
|
2005-07-11 15:10:40 +00:00
|
|
|
GstSystemClock *sysclock = GST_SYSTEM_CLOCK (clock);
|
|
|
|
GList *entries;
|
2003-05-10 11:11:29 +00:00
|
|
|
|
2005-07-11 15:10:40 +00:00
|
|
|
/* else we have to stop the thread */
|
2005-11-21 16:34:26 +00:00
|
|
|
GST_OBJECT_LOCK (clock);
|
2005-07-11 15:10:40 +00:00
|
|
|
sysclock->stopping = TRUE;
|
|
|
|
/* unschedule all entries */
|
|
|
|
for (entries = clock->entries; entries; entries = g_list_next (entries)) {
|
|
|
|
GstClockEntry *entry = (GstClockEntry *) entries->data;
|
2005-06-28 19:45:26 +00:00
|
|
|
|
2005-07-11 15:10:40 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
|
|
|
|
entry->status = GST_CLOCK_UNSCHEDULED;
|
|
|
|
}
|
|
|
|
g_list_free (clock->entries);
|
|
|
|
clock->entries = NULL;
|
|
|
|
GST_CLOCK_BROADCAST (clock);
|
2005-11-21 16:34:26 +00:00
|
|
|
GST_OBJECT_UNLOCK (clock);
|
2005-07-11 15:10:40 +00:00
|
|
|
|
|
|
|
if (sysclock->thread)
|
|
|
|
g_thread_join (sysclock->thread);
|
|
|
|
sysclock->thread = NULL;
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "joined thread");
|
2005-06-28 19:45:26 +00:00
|
|
|
|
2005-07-11 15:10:40 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->dispose (object);
|
|
|
|
|
|
|
|
if (_the_system_clock == clock) {
|
|
|
|
_the_system_clock = NULL;
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "disposed system clock");
|
2003-05-10 11:11:29 +00:00
|
|
|
}
|
2003-02-02 19:19:39 +00:00
|
|
|
}
|
|
|
|
|
2002-03-31 14:04:50 +00:00
|
|
|
/**
|
2005-06-20 11:41:17 +00:00
|
|
|
* gst_system_clock_obtain:
|
2002-03-31 14:04:50 +00:00
|
|
|
*
|
2005-03-07 18:27:42 +00:00
|
|
|
* Get a handle to the default system clock. The refcount of the
|
2005-07-11 15:10:40 +00:00
|
|
|
* clock will be increased so you need to unref the clock after
|
2005-03-07 18:27:42 +00:00
|
|
|
* usage.
|
2002-03-31 14:04:50 +00:00
|
|
|
*
|
|
|
|
* Returns: the default clock.
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
|
|
|
* MT safe.
|
2002-03-31 14:04:50 +00:00
|
|
|
*/
|
2004-03-13 15:27:01 +00:00
|
|
|
GstClock *
|
2002-02-03 20:07:09 +00:00
|
|
|
gst_system_clock_obtain (void)
|
|
|
|
{
|
2005-06-20 11:41:17 +00:00
|
|
|
GstClock *clock;
|
2003-02-02 19:19:39 +00:00
|
|
|
|
2005-06-20 11:41:17 +00:00
|
|
|
g_static_mutex_lock (&_gst_sysclock_mutex);
|
|
|
|
clock = _the_system_clock;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2005-06-20 11:41:17 +00:00
|
|
|
if (clock == NULL) {
|
2004-07-06 11:21:34 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "creating new static system clock");
|
2005-06-20 11:41:17 +00:00
|
|
|
clock = g_object_new (GST_TYPE_SYSTEM_CLOCK,
|
|
|
|
"name", "GstSystemClock", NULL);
|
2003-01-17 18:48:17 +00:00
|
|
|
|
2004-07-06 11:21:34 +00:00
|
|
|
/* we created the global clock; take ownership so
|
|
|
|
* we can hand out instances later */
|
2005-06-28 09:59:01 +00:00
|
|
|
gst_object_ref (clock);
|
2003-02-02 19:19:39 +00:00
|
|
|
gst_object_sink (GST_OBJECT (clock));
|
|
|
|
|
|
|
|
_the_system_clock = clock;
|
2003-05-10 14:38:48 +00:00
|
|
|
g_static_mutex_unlock (&_gst_sysclock_mutex);
|
2004-07-06 11:21:34 +00:00
|
|
|
} else {
|
2005-06-20 11:41:17 +00:00
|
|
|
g_static_mutex_unlock (&_gst_sysclock_mutex);
|
2004-07-06 11:21:34 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "returning static system clock");
|
2002-02-03 20:07:09 +00:00
|
|
|
}
|
2003-02-02 19:19:39 +00:00
|
|
|
|
2004-07-06 11:21:34 +00:00
|
|
|
/* we ref it since we are a clock factory. */
|
2005-06-28 09:59:01 +00:00
|
|
|
gst_object_ref (clock);
|
2003-02-02 19:19:39 +00:00
|
|
|
return clock;
|
2002-02-03 20:07:09 +00:00
|
|
|
}
|
|
|
|
|
2005-10-15 15:30:24 +00:00
|
|
|
/* this thread reads the sorted clock entries from the queue.
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
|
|
|
* It waits on each of them and fires the callback when the timeout occurs.
|
|
|
|
*
|
|
|
|
* When an entry in the queue was canceled, it is simply skipped.
|
|
|
|
*
|
2005-10-15 15:30:24 +00:00
|
|
|
* When waiting for an entry, it can become canceled, in that case we don't
|
2005-03-07 18:27:42 +00:00
|
|
|
* call the callback but move to the next item in the queue.
|
|
|
|
*
|
|
|
|
* MT safe.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
gst_system_clock_async_thread (GstClock * clock)
|
|
|
|
{
|
|
|
|
GstSystemClock *sysclock = GST_SYSTEM_CLOCK (clock);
|
|
|
|
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "enter system clock thread");
|
2005-11-21 16:34:26 +00:00
|
|
|
GST_OBJECT_LOCK (clock);
|
2005-03-07 18:27:42 +00:00
|
|
|
/* signal spinup */
|
2005-06-29 11:10:44 +00:00
|
|
|
GST_CLOCK_BROADCAST (clock);
|
2005-10-08 08:00:37 +00:00
|
|
|
/* now enter our (almost) infinite loop */
|
2005-03-07 18:27:42 +00:00
|
|
|
while (!sysclock->stopping) {
|
|
|
|
GstClockEntry *entry;
|
|
|
|
GstClockReturn res;
|
|
|
|
|
|
|
|
/* check if something to be done */
|
|
|
|
while (clock->entries == NULL) {
|
2005-10-08 08:00:37 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "no clock entries, waiting..");
|
2005-03-07 18:27:42 +00:00
|
|
|
/* wait for work to do */
|
|
|
|
GST_CLOCK_WAIT (clock);
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "got signal");
|
|
|
|
/* clock was stopping, exit */
|
|
|
|
if (sysclock->stopping)
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pick the next entry */
|
|
|
|
entry = clock->entries->data;
|
|
|
|
/* if it was unscheduled, just move on to the next entry */
|
|
|
|
if (entry->status == GST_CLOCK_UNSCHEDULED) {
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p was unscheduled", entry);
|
|
|
|
goto next_entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now wait for the entry, we already hold the lock */
|
|
|
|
res = gst_system_clock_id_wait_unlocked (clock, (GstClockID) entry);
|
|
|
|
|
|
|
|
switch (res) {
|
|
|
|
case GST_CLOCK_UNSCHEDULED:
|
|
|
|
/* entry was unscheduled, move to the next */
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unscheduled", entry);
|
|
|
|
goto next_entry;
|
|
|
|
case GST_CLOCK_OK:
|
|
|
|
case GST_CLOCK_EARLY:
|
|
|
|
{
|
|
|
|
/* entry timed out normally, fire the callback and move to the next
|
|
|
|
* entry */
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unlocked", entry);
|
|
|
|
if (entry->func) {
|
2005-11-18 13:18:44 +00:00
|
|
|
/* unlock before firing the callback */
|
2005-11-21 16:34:26 +00:00
|
|
|
GST_OBJECT_UNLOCK (clock);
|
2005-03-07 18:27:42 +00:00
|
|
|
entry->func (clock, entry->time, (GstClockID) entry,
|
|
|
|
entry->user_data);
|
2005-11-21 16:34:26 +00:00
|
|
|
GST_OBJECT_LOCK (clock);
|
2005-03-07 18:27:42 +00:00
|
|
|
}
|
|
|
|
if (entry->type == GST_CLOCK_ENTRY_PERIODIC) {
|
|
|
|
/* adjust time now */
|
|
|
|
entry->time += entry->interval;
|
|
|
|
/* and resort the list now */
|
|
|
|
clock->entries =
|
|
|
|
g_list_sort (clock->entries, gst_clock_id_compare_func);
|
|
|
|
/* and restart */
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
goto next_entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case GST_CLOCK_BUSY:
|
|
|
|
/* somebody unlocked the entry but is was not canceled, This means that
|
2005-10-15 15:30:24 +00:00
|
|
|
* either a new entry was added in front of the queue or some other entry
|
2005-03-07 18:27:42 +00:00
|
|
|
* was canceled. Whatever it is, pick the head entry of the list and
|
|
|
|
* continue waiting. */
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p needs restart", entry);
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK,
|
|
|
|
"strange result %d waiting for %p, skipping", res, entry);
|
2005-10-08 08:00:37 +00:00
|
|
|
g_warning ("%s: strange result %d waiting for %p, skipping",
|
|
|
|
GST_OBJECT_NAME (clock), res, entry);
|
2005-03-07 18:27:42 +00:00
|
|
|
goto next_entry;
|
|
|
|
}
|
|
|
|
next_entry:
|
|
|
|
/* we remove the current entry and unref it */
|
|
|
|
clock->entries = g_list_remove (clock->entries, entry);
|
|
|
|
gst_clock_id_unref ((GstClockID) entry);
|
|
|
|
}
|
|
|
|
exit:
|
|
|
|
/* signal exit */
|
2005-06-29 11:10:44 +00:00
|
|
|
GST_CLOCK_BROADCAST (clock);
|
2005-11-21 16:34:26 +00:00
|
|
|
GST_OBJECT_UNLOCK (clock);
|
2005-03-07 18:27:42 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "exit system clock thread");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* MT safe */
|
2002-02-03 20:07:09 +00:00
|
|
|
static GstClockTime
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_system_clock_get_internal_time (GstClock * clock)
|
2002-02-03 20:07:09 +00:00
|
|
|
{
|
|
|
|
GTimeVal timeval;
|
2002-05-26 21:54:27 +00:00
|
|
|
|
2002-03-30 17:05:03 +00:00
|
|
|
g_get_current_time (&timeval);
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2002-03-30 17:05:03 +00:00
|
|
|
return GST_TIMEVAL_TO_TIME (timeval);
|
2002-02-03 20:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static guint64
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_system_clock_get_resolution (GstClock * clock)
|
2002-02-03 20:07:09 +00:00
|
|
|
{
|
2002-05-26 21:54:27 +00:00
|
|
|
return 1 * GST_USECOND;
|
2002-02-03 20:07:09 +00:00
|
|
|
}
|
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
/* synchronously wait on the given GstClockEntry.
|
|
|
|
*
|
|
|
|
* We do this by blocking on the global clock GCond variable with
|
|
|
|
* the requested time as a timeout. This allows us to unblock the
|
|
|
|
* entry by signaling the GCond variable.
|
|
|
|
*
|
|
|
|
* Note that signaling the global GCond unlocks all waiting entries. So
|
|
|
|
* we need to check if an unlocked entry has changed when it unlocks.
|
|
|
|
*
|
|
|
|
* Entries that arrive too late are simply not waited on and a
|
|
|
|
* GST_CLOCK_EARLY result is returned.
|
|
|
|
*
|
2005-10-08 08:00:37 +00:00
|
|
|
* should be called with LOCK held.
|
|
|
|
*
|
2005-03-07 18:27:42 +00:00
|
|
|
* MT safe.
|
|
|
|
*/
|
|
|
|
static GstClockReturn
|
|
|
|
gst_system_clock_id_wait_unlocked (GstClock * clock, GstClockEntry * entry)
|
2002-11-02 13:54:34 +00:00
|
|
|
{
|
2005-04-28 16:28:28 +00:00
|
|
|
GstClockTime entryt, real, now, target;
|
2005-03-07 18:27:42 +00:00
|
|
|
GstClockTimeDiff diff;
|
2002-12-27 04:11:59 +00:00
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
/* need to call the overridden method */
|
|
|
|
real = GST_CLOCK_GET_CLASS (clock)->get_internal_time (clock);
|
2005-04-28 16:28:28 +00:00
|
|
|
entryt = GST_CLOCK_ENTRY_TIME (entry);
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2005-04-28 16:28:28 +00:00
|
|
|
now = gst_clock_adjust_unlocked (clock, real);
|
|
|
|
diff = entryt - now;
|
2002-12-27 04:11:59 +00:00
|
|
|
target = gst_system_clock_get_internal_time (clock) + diff;
|
2002-12-11 21:33:07 +00:00
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p"
|
|
|
|
" target %" GST_TIME_FORMAT
|
2005-04-28 16:28:28 +00:00
|
|
|
" entry %" GST_TIME_FORMAT
|
2005-03-07 18:27:42 +00:00
|
|
|
" now %" GST_TIME_FORMAT
|
|
|
|
" real %" GST_TIME_FORMAT
|
|
|
|
" diff %" G_GINT64_FORMAT,
|
|
|
|
entry,
|
|
|
|
GST_TIME_ARGS (target),
|
2005-04-28 16:28:28 +00:00
|
|
|
GST_TIME_ARGS (entryt), GST_TIME_ARGS (now), GST_TIME_ARGS (real), diff);
|
2002-12-11 21:33:07 +00:00
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
if (diff > 0) {
|
2002-11-02 13:54:34 +00:00
|
|
|
GTimeVal tv;
|
|
|
|
|
|
|
|
GST_TIME_TO_TIMEVAL (target, tv);
|
2005-03-07 18:27:42 +00:00
|
|
|
|
|
|
|
while (TRUE) {
|
|
|
|
/* now wait on the entry, it either times out or the cond is signaled. */
|
|
|
|
if (!GST_CLOCK_TIMED_WAIT (clock, &tv)) {
|
|
|
|
/* timeout, this is fine, we can report success now */
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked after timeout", entry);
|
|
|
|
entry->status = GST_CLOCK_OK;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
/* the waiting is interrupted because the GCond was signaled. This can
|
|
|
|
* be because this or some other entry was unscheduled. */
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked with signal", entry);
|
|
|
|
/* if the entry is unscheduled, we can stop waiting for it */
|
|
|
|
if (entry->status == GST_CLOCK_UNSCHEDULED)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-03-13 15:27:01 +00:00
|
|
|
} else {
|
2005-03-07 18:27:42 +00:00
|
|
|
entry->status = GST_CLOCK_EARLY;
|
|
|
|
}
|
|
|
|
return entry->status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstClockReturn
|
|
|
|
gst_system_clock_id_wait (GstClock * clock, GstClockEntry * entry)
|
|
|
|
{
|
|
|
|
GstClockReturn ret;
|
|
|
|
|
2005-11-21 16:34:26 +00:00
|
|
|
GST_OBJECT_LOCK (clock);
|
2005-03-07 18:27:42 +00:00
|
|
|
ret = gst_system_clock_id_wait_unlocked (clock, entry);
|
2005-11-21 16:34:26 +00:00
|
|
|
GST_OBJECT_UNLOCK (clock);
|
2005-03-07 18:27:42 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add an entry to the list of pending async waits. The entry is inserted
|
|
|
|
* in sorted order. If we inserted the entry at the head of the list, we
|
|
|
|
* need to signal the thread as it might either be waiting on it or waiting
|
2005-10-15 15:30:24 +00:00
|
|
|
* for a new entry.
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
|
|
|
* MT safe.
|
|
|
|
*/
|
|
|
|
static GstClockReturn
|
|
|
|
gst_system_clock_id_wait_async (GstClock * clock, GstClockEntry * entry)
|
|
|
|
{
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "adding entry %p", entry);
|
|
|
|
|
2005-11-21 16:34:26 +00:00
|
|
|
GST_OBJECT_LOCK (clock);
|
2005-03-07 18:27:42 +00:00
|
|
|
/* need to take a ref */
|
|
|
|
gst_clock_id_ref ((GstClockID) entry);
|
|
|
|
/* insert the entry in sorted order */
|
|
|
|
clock->entries = g_list_insert_sorted (clock->entries, entry,
|
|
|
|
gst_clock_id_compare_func);
|
|
|
|
|
|
|
|
/* only need to send the signal if the entry was added to the
|
|
|
|
* front, else the thread is just waiting for another entry and
|
|
|
|
* will get to this entry automatically. */
|
|
|
|
if (clock->entries->data == entry) {
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "send signal");
|
2005-06-29 11:10:44 +00:00
|
|
|
GST_CLOCK_BROADCAST (clock);
|
2002-11-02 13:54:34 +00:00
|
|
|
}
|
2005-11-21 16:34:26 +00:00
|
|
|
GST_OBJECT_UNLOCK (clock);
|
2005-03-07 18:27:42 +00:00
|
|
|
|
|
|
|
return GST_CLOCK_OK;
|
2002-11-02 13:54:34 +00:00
|
|
|
}
|
2002-02-03 20:07:09 +00:00
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
/* unschedule an entry. This will set the state of the entry to GST_CLOCK_UNSCHEDULED
|
2005-10-15 15:30:24 +00:00
|
|
|
* and will signal any thread waiting for entries to recheck their entry.
|
2005-03-07 18:27:42 +00:00
|
|
|
* We cannot really decide if the signal is needed or not because the entry
|
|
|
|
* could be waited on in async or sync mode.
|
|
|
|
*
|
|
|
|
* MT safe.
|
|
|
|
*/
|
2002-12-27 04:11:59 +00:00
|
|
|
static void
|
2005-03-07 18:27:42 +00:00
|
|
|
gst_system_clock_id_unschedule (GstClock * clock, GstClockEntry * entry)
|
2002-12-27 04:11:59 +00:00
|
|
|
{
|
2005-03-07 18:27:42 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
|
2003-05-10 14:38:48 +00:00
|
|
|
|
2005-11-21 16:34:26 +00:00
|
|
|
GST_OBJECT_LOCK (clock);
|
2005-03-07 18:27:42 +00:00
|
|
|
entry->status = GST_CLOCK_UNSCHEDULED;
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "send signal");
|
2005-06-29 11:10:44 +00:00
|
|
|
GST_CLOCK_BROADCAST (clock);
|
2005-11-21 16:34:26 +00:00
|
|
|
GST_OBJECT_UNLOCK (clock);
|
2002-12-27 04:11:59 +00:00
|
|
|
}
|