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
|
2012-11-03 20:44:48 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2002-02-03 20:07:09 +00:00
|
|
|
*/
|
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.
|
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"
|
2009-05-12 19:56:06 +00:00
|
|
|
#include "gstenumtypes.h"
|
2009-02-03 17:04:46 +00:00
|
|
|
#include "gstpoll.h"
|
2009-08-29 00:44:51 +00:00
|
|
|
#include "gstutils.h"
|
2011-06-04 07:30:15 +00:00
|
|
|
#include "glib-compat-private.h"
|
2002-02-03 20:07:09 +00:00
|
|
|
|
2009-03-15 21:40:36 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
2009-08-28 15:02:30 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
# define WIN32_LEAN_AND_MEAN /* prevents from including too many things */
|
|
|
|
# include <windows.h> /* QueryPerformance* stuff */
|
|
|
|
# undef WIN32_LEAN_AND_MEAN
|
2012-05-05 19:17:43 +00:00
|
|
|
# ifndef EWOULDBLOCK
|
2011-05-25 09:40:30 +00:00
|
|
|
# define EWOULDBLOCK EAGAIN /* This is just to placate gcc */
|
2012-05-05 19:17:43 +00:00
|
|
|
# endif
|
2009-08-28 15:02:30 +00:00
|
|
|
#endif /* G_OS_WIN32 */
|
|
|
|
|
2011-08-25 19:49:29 +00:00
|
|
|
#define GET_ENTRY_STATUS(e) ((GstClockReturn) g_atomic_int_get(&GST_CLOCK_ENTRY_STATUS(e)))
|
2010-10-21 16:08:51 +00:00
|
|
|
#define SET_ENTRY_STATUS(e,val) (g_atomic_int_set(&GST_CLOCK_ENTRY_STATUS(e),(val)))
|
2012-01-22 01:25:22 +00:00
|
|
|
#define CAS_ENTRY_STATUS(e,old,val) (g_atomic_int_compare_and_exchange(\
|
2011-06-04 07:30:15 +00:00
|
|
|
(&GST_CLOCK_ENTRY_STATUS(e)), (old), (val)))
|
2010-10-21 16:08:51 +00:00
|
|
|
|
2006-05-30 12:04:29 +00:00
|
|
|
/* Define this to get some extra debug about jitter from each clock_wait */
|
|
|
|
#undef WAIT_DEBUGGING
|
|
|
|
|
2012-02-27 08:02:07 +00:00
|
|
|
#define GST_SYSTEM_CLOCK_GET_COND(clock) (&GST_SYSTEM_CLOCK_CAST(clock)->priv->entries_changed)
|
|
|
|
#define GST_SYSTEM_CLOCK_WAIT(clock) g_cond_wait(GST_SYSTEM_CLOCK_GET_COND(clock),GST_OBJECT_GET_LOCK(clock))
|
|
|
|
#define GST_SYSTEM_CLOCK_TIMED_WAIT(clock,tv) g_cond_timed_wait(GST_SYSTEM_CLOCK_GET_COND(clock),GST_OBJECT_GET_LOCK(clock),tv)
|
|
|
|
#define GST_SYSTEM_CLOCK_BROADCAST(clock) g_cond_broadcast(GST_SYSTEM_CLOCK_GET_COND(clock))
|
|
|
|
|
2009-02-03 17:04:46 +00:00
|
|
|
struct _GstSystemClockPrivate
|
|
|
|
{
|
2012-02-26 19:45:14 +00:00
|
|
|
GThread *thread; /* thread for async notify */
|
|
|
|
gboolean stopping;
|
|
|
|
|
2012-02-27 08:02:07 +00:00
|
|
|
GList *entries;
|
|
|
|
GCond entries_changed;
|
2012-02-26 19:45:14 +00:00
|
|
|
|
2009-02-03 17:04:46 +00:00
|
|
|
GstClockType clock_type;
|
|
|
|
GstPoll *timer;
|
2009-03-26 17:46:35 +00:00
|
|
|
gint wakeup_count; /* the number of entries with a pending wakeup */
|
|
|
|
gboolean async_wakeup; /* if the wakeup was because of a async list change */
|
2009-08-28 15:02:30 +00:00
|
|
|
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
LARGE_INTEGER start;
|
|
|
|
LARGE_INTEGER frequency;
|
|
|
|
#endif /* G_OS_WIN32 */
|
2009-02-03 17:04:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define GST_SYSTEM_CLOCK_GET_PRIVATE(obj) \
|
|
|
|
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_SYSTEM_CLOCK, \
|
|
|
|
GstSystemClockPrivate))
|
|
|
|
|
2009-05-25 14:21:55 +00:00
|
|
|
#ifdef HAVE_POSIX_TIMERS
|
|
|
|
# ifdef HAVE_MONOTONIC_CLOCK
|
|
|
|
# define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_MONOTONIC
|
|
|
|
# else
|
|
|
|
# define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
#define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
|
|
|
|
#endif
|
|
|
|
|
2009-02-03 17:04:46 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_CLOCK_TYPE,
|
|
|
|
/* FILL ME */
|
|
|
|
};
|
|
|
|
|
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;
|
2013-11-11 11:25:14 +00:00
|
|
|
static gboolean _external_default_clock = FALSE;
|
2002-02-03 20:07:09 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
static void gst_system_clock_dispose (GObject * object);
|
2009-02-03 17:04:46 +00:00
|
|
|
static void gst_system_clock_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_system_clock_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
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);
|
2006-08-11 15:24:03 +00:00
|
|
|
static GstClockReturn gst_system_clock_id_wait_jitter (GstClock * clock,
|
|
|
|
GstClockEntry * entry, GstClockTimeDiff * jitter);
|
|
|
|
static GstClockReturn gst_system_clock_id_wait_jitter_unlocked
|
2006-12-07 10:51:36 +00:00
|
|
|
(GstClock * clock, GstClockEntry * entry, GstClockTimeDiff * jitter,
|
|
|
|
gboolean restart);
|
2005-03-07 18:27:42 +00:00
|
|
|
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);
|
2007-03-12 15:27:05 +00:00
|
|
|
static gboolean gst_system_clock_start_async (GstSystemClock * clock);
|
2009-03-27 15:15:55 +00:00
|
|
|
static void gst_system_clock_add_wakeup (GstSystemClock * sysclock);
|
2002-02-03 20:07:09 +00:00
|
|
|
|
2012-01-22 22:44:59 +00:00
|
|
|
static GMutex _gst_sysclock_mutex;
|
2002-02-03 20:07:09 +00:00
|
|
|
|
|
|
|
/* static guint gst_system_clock_signals[LAST_SIGNAL] = { 0 }; */
|
|
|
|
|
2011-10-16 12:45:03 +00:00
|
|
|
#define gst_system_clock_parent_class parent_class
|
2009-04-04 08:20:36 +00:00
|
|
|
G_DEFINE_TYPE (GstSystemClock, gst_system_clock, GST_TYPE_CLOCK);
|
2002-02-03 20:07:09 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
GstClockClass *gstclock_class;
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gstclock_class = (GstClockClass *) klass;
|
2002-02-03 20:07:09 +00:00
|
|
|
|
2009-02-03 17:04:46 +00:00
|
|
|
g_type_class_add_private (klass, sizeof (GstSystemClockPrivate));
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
gobject_class->dispose = gst_system_clock_dispose;
|
2009-02-03 17:04:46 +00:00
|
|
|
gobject_class->set_property = gst_system_clock_set_property;
|
|
|
|
gobject_class->get_property = gst_system_clock_get_property;
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_CLOCK_TYPE,
|
|
|
|
g_param_spec_enum ("clock-type", "Clock type",
|
|
|
|
"The type of underlying clock implementation used",
|
2010-10-13 12:56:26 +00:00
|
|
|
GST_TYPE_CLOCK_TYPE, DEFAULT_CLOCK_TYPE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
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;
|
2011-02-22 15:04:12 +00:00
|
|
|
gstclock_class->wait = gst_system_clock_id_wait_jitter;
|
2005-03-07 18:27:42 +00:00
|
|
|
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
|
|
|
{
|
2012-02-27 08:02:07 +00:00
|
|
|
GstSystemClockPrivate *priv;
|
|
|
|
|
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
|
|
|
|
2012-02-27 08:02:07 +00:00
|
|
|
clock->priv = priv = GST_SYSTEM_CLOCK_GET_PRIVATE (clock);
|
|
|
|
|
|
|
|
priv->clock_type = DEFAULT_CLOCK_TYPE;
|
|
|
|
priv->timer = gst_poll_new_timer ();
|
2009-02-03 17:04:46 +00:00
|
|
|
|
2012-02-27 08:02:07 +00:00
|
|
|
priv->entries = NULL;
|
|
|
|
g_cond_init (&priv->entries_changed);
|
2009-02-03 17:04:46 +00:00
|
|
|
|
2009-08-28 15:02:30 +00:00
|
|
|
#ifdef G_OS_WIN32
|
2012-02-27 08:02:07 +00:00
|
|
|
QueryPerformanceFrequency (&priv->frequency);
|
2009-08-28 15:02:30 +00:00
|
|
|
/* can be 0 if the hardware does not have hardware support */
|
2012-02-27 08:02:07 +00:00
|
|
|
if (priv->frequency.QuadPart != 0)
|
2009-08-28 15:02:30 +00:00
|
|
|
/* we take a base time so that time starts from 0 to ease debugging */
|
2012-02-27 08:02:07 +00:00
|
|
|
QueryPerformanceCounter (&priv->start);
|
2009-08-28 15:02:30 +00:00
|
|
|
#endif /* G_OS_WIN32 */
|
|
|
|
|
2007-03-12 15:27:05 +00:00
|
|
|
#if 0
|
|
|
|
/* Uncomment this to start the async clock thread straight away */
|
2009-02-03 17:04:46 +00:00
|
|
|
GST_OBJECT_LOCK (clock);
|
2007-03-12 15:27:05 +00:00
|
|
|
gst_system_clock_start_async (clock);
|
2009-02-03 17:04:46 +00:00
|
|
|
GST_OBJECT_UNLOCK (clock);
|
2007-03-12 15:27:05 +00:00
|
|
|
#endif
|
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;
|
2009-02-03 17:04:46 +00:00
|
|
|
GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
|
2012-02-27 08:02:07 +00:00
|
|
|
GstSystemClockPrivate *priv = sysclock->priv;
|
2005-07-11 15:10:40 +00:00
|
|
|
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);
|
2012-02-27 08:02:07 +00:00
|
|
|
priv->stopping = TRUE;
|
2005-07-11 15:10:40 +00:00
|
|
|
/* unschedule all entries */
|
2012-02-27 08:02:07 +00:00
|
|
|
for (entries = priv->entries; entries; entries = g_list_next (entries)) {
|
2005-07-11 15:10:40 +00:00
|
|
|
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);
|
2010-10-21 16:08:51 +00:00
|
|
|
SET_ENTRY_STATUS (entry, GST_CLOCK_UNSCHEDULED);
|
2005-07-11 15:10:40 +00:00
|
|
|
}
|
2012-02-27 08:02:07 +00:00
|
|
|
GST_SYSTEM_CLOCK_BROADCAST (clock);
|
2009-03-27 15:15:55 +00:00
|
|
|
gst_system_clock_add_wakeup (sysclock);
|
2005-11-21 16:34:26 +00:00
|
|
|
GST_OBJECT_UNLOCK (clock);
|
2005-07-11 15:10:40 +00:00
|
|
|
|
2012-02-27 08:02:07 +00:00
|
|
|
if (priv->thread)
|
|
|
|
g_thread_join (priv->thread);
|
|
|
|
priv->thread = NULL;
|
2005-07-11 15:10:40 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "joined thread");
|
2005-06-28 19:45:26 +00:00
|
|
|
|
2012-02-27 08:02:07 +00:00
|
|
|
g_list_foreach (priv->entries, (GFunc) gst_clock_id_unref, NULL);
|
|
|
|
g_list_free (priv->entries);
|
|
|
|
priv->entries = NULL;
|
2010-01-12 16:10:59 +00:00
|
|
|
|
2012-02-27 08:02:07 +00:00
|
|
|
gst_poll_free (priv->timer);
|
|
|
|
g_cond_clear (&priv->entries_changed);
|
2009-02-03 17:04:46 +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
|
|
|
}
|
|
|
|
|
2009-02-03 17:04:46 +00:00
|
|
|
static void
|
|
|
|
gst_system_clock_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_CLOCK_TYPE:
|
2011-08-25 19:49:29 +00:00
|
|
|
sysclock->priv->clock_type = (GstClockType) g_value_get_enum (value);
|
2009-02-03 17:04:46 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "clock-type set to %d",
|
|
|
|
sysclock->priv->clock_type);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_system_clock_get_property (GObject * object, guint prop_id, GValue * value,
|
|
|
|
GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_CLOCK_TYPE:
|
|
|
|
g_value_set_enum (value, sysclock->priv->clock_type);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-11 11:25:14 +00:00
|
|
|
/**
|
|
|
|
* gst_system_clock_set_default:
|
|
|
|
* @new_clock: a #GstClock
|
|
|
|
*
|
|
|
|
* Sets the default system clock that can be obtained with
|
2014-07-01 06:39:18 +00:00
|
|
|
* gst_system_clock_obtain().
|
2013-11-11 11:25:14 +00:00
|
|
|
*
|
|
|
|
* This is mostly used for testing and debugging purposes when you
|
|
|
|
* want to have control over the time reported by the default system
|
|
|
|
* clock.
|
|
|
|
*
|
|
|
|
* MT safe.
|
2013-11-11 16:47:06 +00:00
|
|
|
*
|
|
|
|
* Since: 1.4
|
2013-11-11 11:25:14 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_system_clock_set_default (GstClock * new_clock)
|
|
|
|
{
|
|
|
|
GstClock *clock;
|
|
|
|
|
|
|
|
g_mutex_lock (&_gst_sysclock_mutex);
|
|
|
|
clock = _the_system_clock;
|
|
|
|
|
|
|
|
if (clock != NULL)
|
|
|
|
g_object_unref (clock);
|
|
|
|
|
|
|
|
if (new_clock == NULL) {
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "resetting default system clock");
|
|
|
|
_external_default_clock = FALSE;
|
|
|
|
} else {
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "setting new default system clock to %p",
|
|
|
|
new_clock);
|
|
|
|
_external_default_clock = TRUE;
|
|
|
|
g_object_ref (new_clock);
|
|
|
|
}
|
|
|
|
_the_system_clock = new_clock;
|
|
|
|
g_mutex_unlock (&_gst_sysclock_mutex);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2010-12-07 18:35:04 +00:00
|
|
|
* Returns: (transfer full): 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
|
|
|
|
2012-01-22 22:44:59 +00:00
|
|
|
g_mutex_lock (&_gst_sysclock_mutex);
|
2005-06-20 11:41:17 +00:00
|
|
|
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");
|
2013-11-11 11:25:14 +00:00
|
|
|
g_assert (_external_default_clock == FALSE);
|
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
|
|
|
|
2012-07-02 23:07:11 +00:00
|
|
|
g_assert (!g_object_is_floating (G_OBJECT (clock)));
|
2003-02-02 19:19:39 +00:00
|
|
|
|
|
|
|
_the_system_clock = clock;
|
2012-01-22 22:44:59 +00:00
|
|
|
g_mutex_unlock (&_gst_sysclock_mutex);
|
2004-07-06 11:21:34 +00:00
|
|
|
} else {
|
2012-01-22 22:44:59 +00:00
|
|
|
g_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
|
|
|
}
|
|
|
|
|
2009-02-03 17:04:46 +00:00
|
|
|
static void
|
2009-03-26 17:46:35 +00:00
|
|
|
gst_system_clock_remove_wakeup (GstSystemClock * sysclock)
|
2009-02-03 17:04:46 +00:00
|
|
|
{
|
2009-03-26 17:46:35 +00:00
|
|
|
g_return_if_fail (sysclock->priv->wakeup_count > 0);
|
|
|
|
|
|
|
|
sysclock->priv->wakeup_count--;
|
|
|
|
if (sysclock->priv->wakeup_count == 0) {
|
|
|
|
/* read the control socket byte when we removed the last wakeup count */
|
2009-02-03 17:04:46 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "reading control");
|
|
|
|
while (!gst_poll_read_control (sysclock->priv->timer)) {
|
|
|
|
g_warning ("gstsystemclock: read control failed, trying again\n");
|
|
|
|
}
|
2012-02-27 08:02:07 +00:00
|
|
|
GST_SYSTEM_CLOCK_BROADCAST (sysclock);
|
2009-02-03 17:04:46 +00:00
|
|
|
}
|
2009-03-26 17:46:35 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
|
|
|
|
sysclock->priv->wakeup_count);
|
2009-02-03 17:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-03-26 17:46:35 +00:00
|
|
|
gst_system_clock_add_wakeup (GstSystemClock * sysclock)
|
2009-02-03 17:04:46 +00:00
|
|
|
{
|
2009-03-26 17:46:35 +00:00
|
|
|
/* only write the control socket for the first wakeup */
|
|
|
|
if (sysclock->priv->wakeup_count == 0) {
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "writing control");
|
|
|
|
while (!gst_poll_write_control (sysclock->priv->timer)) {
|
2011-05-12 13:51:22 +00:00
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
|
|
|
|
g_warning
|
|
|
|
("gstsystemclock: write control failed in wakeup_async, trying again: %d:%s\n",
|
|
|
|
errno, g_strerror (errno));
|
|
|
|
} else {
|
|
|
|
g_critical
|
|
|
|
("gstsystemclock: write control failed in wakeup_async: %d:%s\n",
|
|
|
|
errno, g_strerror (errno));
|
|
|
|
return;
|
|
|
|
}
|
2009-03-26 17:46:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sysclock->priv->wakeup_count++;
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
|
|
|
|
sysclock->priv->wakeup_count);
|
|
|
|
}
|
2009-02-03 17:04:46 +00:00
|
|
|
|
2009-03-26 17:46:35 +00:00
|
|
|
static void
|
|
|
|
gst_system_clock_wait_wakeup (GstSystemClock * sysclock)
|
|
|
|
{
|
2010-12-26 21:20:31 +00:00
|
|
|
while (sysclock->priv->wakeup_count > 0) {
|
2012-02-27 08:02:07 +00:00
|
|
|
GST_SYSTEM_CLOCK_WAIT (sysclock);
|
2009-02-03 17:04:46 +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.
|
|
|
|
*
|
2006-10-11 16:30:14 +00:00
|
|
|
* When an entry in the queue was canceled before we wait for it, it is
|
|
|
|
* simply skipped.
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
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)
|
|
|
|
{
|
2009-02-03 17:04:46 +00:00
|
|
|
GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
|
2012-02-27 08:02:07 +00:00
|
|
|
GstSystemClockPrivate *priv = sysclock->priv;
|
2005-03-07 18:27:42 +00:00
|
|
|
|
|
|
|
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 */
|
2012-02-27 08:02:07 +00:00
|
|
|
GST_SYSTEM_CLOCK_BROADCAST (clock);
|
2005-10-08 08:00:37 +00:00
|
|
|
/* now enter our (almost) infinite loop */
|
2012-02-27 08:02:07 +00:00
|
|
|
while (!priv->stopping) {
|
2005-03-07 18:27:42 +00:00
|
|
|
GstClockEntry *entry;
|
2006-12-07 10:51:36 +00:00
|
|
|
GstClockTime requested;
|
2005-03-07 18:27:42 +00:00
|
|
|
GstClockReturn res;
|
|
|
|
|
|
|
|
/* check if something to be done */
|
2012-02-27 08:02:07 +00:00
|
|
|
while (priv->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 */
|
2012-02-27 08:02:07 +00:00
|
|
|
GST_SYSTEM_CLOCK_WAIT (clock);
|
2005-03-07 18:27:42 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "got signal");
|
|
|
|
/* clock was stopping, exit */
|
2012-02-27 08:02:07 +00:00
|
|
|
if (priv->stopping)
|
2005-03-07 18:27:42 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2009-03-26 20:40:20 +00:00
|
|
|
/* see if we have a pending wakeup because the order of the list
|
|
|
|
* changed. */
|
2012-02-27 08:02:07 +00:00
|
|
|
if (priv->async_wakeup) {
|
2009-03-26 20:40:20 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "clear async wakeup");
|
|
|
|
gst_system_clock_remove_wakeup (sysclock);
|
2012-02-27 08:02:07 +00:00
|
|
|
priv->async_wakeup = FALSE;
|
2009-03-26 20:40:20 +00:00
|
|
|
}
|
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
/* pick the next entry */
|
2012-02-27 08:02:07 +00:00
|
|
|
entry = priv->entries->data;
|
2010-10-21 16:08:51 +00:00
|
|
|
GST_OBJECT_UNLOCK (clock);
|
2005-03-07 18:27:42 +00:00
|
|
|
|
2006-12-07 10:51:36 +00:00
|
|
|
requested = entry->time;
|
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
/* now wait for the entry, we already hold the lock */
|
2006-08-11 15:24:03 +00:00
|
|
|
res =
|
|
|
|
gst_system_clock_id_wait_jitter_unlocked (clock, (GstClockID) entry,
|
2006-12-07 10:51:36 +00:00
|
|
|
NULL, FALSE);
|
2005-03-07 18:27:42 +00:00
|
|
|
|
2010-10-21 16:08:51 +00:00
|
|
|
GST_OBJECT_LOCK (clock);
|
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
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 */
|
2009-03-26 17:46:35 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p timed out", entry);
|
2005-03-07 18:27:42 +00:00
|
|
|
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) {
|
2009-03-26 14:55:02 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "updating periodic entry %p", entry);
|
2005-03-07 18:27:42 +00:00
|
|
|
/* adjust time now */
|
2006-12-07 10:51:36 +00:00
|
|
|
entry->time = requested + entry->interval;
|
2005-03-07 18:27:42 +00:00
|
|
|
/* and resort the list now */
|
2012-02-27 08:02:07 +00:00
|
|
|
priv->entries =
|
|
|
|
g_list_sort (priv->entries, gst_clock_id_compare_func);
|
2005-03-07 18:27:42 +00:00
|
|
|
/* and restart */
|
|
|
|
continue;
|
|
|
|
} else {
|
2009-03-26 17:46:35 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "moving to next entry");
|
2005-03-07 18:27:42 +00:00
|
|
|
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);
|
2009-03-26 14:55:02 +00:00
|
|
|
|
2009-03-26 17:46:35 +00:00
|
|
|
/* we set the entry back to the OK state. This is needed so that the
|
|
|
|
* _unschedule() code can see if an entry is currently being waited
|
|
|
|
* on (when its state is BUSY). */
|
2010-10-21 16:08:51 +00:00
|
|
|
SET_ENTRY_STATUS (entry, GST_CLOCK_OK);
|
2005-03-07 18:27:42 +00:00
|
|
|
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 */
|
2012-02-27 08:02:07 +00:00
|
|
|
priv->entries = g_list_remove (priv->entries, entry);
|
2005-03-07 18:27:42 +00:00
|
|
|
gst_clock_id_unref ((GstClockID) entry);
|
|
|
|
}
|
|
|
|
exit:
|
|
|
|
/* signal exit */
|
2012-02-27 08:02:07 +00:00
|
|
|
GST_SYSTEM_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");
|
|
|
|
}
|
|
|
|
|
2009-02-03 17:04:46 +00:00
|
|
|
#ifdef HAVE_POSIX_TIMERS
|
|
|
|
static inline clockid_t
|
|
|
|
clock_type_to_posix_id (GstClockType clock_type)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_MONOTONIC_CLOCK
|
|
|
|
if (clock_type == GST_CLOCK_TYPE_MONOTONIC)
|
|
|
|
return CLOCK_MONOTONIC;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
return CLOCK_REALTIME;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
/* 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
|
|
|
{
|
2009-08-28 15:02:30 +00:00
|
|
|
#ifdef G_OS_WIN32
|
2009-08-29 00:44:51 +00:00
|
|
|
GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
|
|
|
|
|
|
|
|
if (sysclock->priv->frequency.QuadPart != 0) {
|
2009-08-28 15:02:30 +00:00
|
|
|
LARGE_INTEGER now;
|
|
|
|
|
|
|
|
/* we prefer the highly accurate performance counters on windows */
|
|
|
|
QueryPerformanceCounter (&now);
|
2009-02-03 17:04:46 +00:00
|
|
|
|
2009-08-28 15:02:30 +00:00
|
|
|
return gst_util_uint64_scale (now.QuadPart - sysclock->priv->start.QuadPart,
|
|
|
|
GST_SECOND, sysclock->priv->frequency.QuadPart);
|
|
|
|
} else
|
|
|
|
#endif /* G_OS_WIN32 */
|
2013-04-08 11:30:07 +00:00
|
|
|
#if !defined HAVE_POSIX_TIMERS || !defined HAVE_CLOCK_GETTIME
|
2009-08-28 15:02:30 +00:00
|
|
|
{
|
|
|
|
GTimeVal timeval;
|
2009-02-03 17:04:46 +00:00
|
|
|
|
2009-08-28 15:02:30 +00:00
|
|
|
g_get_current_time (&timeval);
|
2009-02-03 17:04:46 +00:00
|
|
|
|
2009-08-28 15:02:30 +00:00
|
|
|
return GST_TIMEVAL_TO_TIME (timeval);
|
|
|
|
}
|
2009-02-03 17:04:46 +00:00
|
|
|
#else
|
2009-08-28 15:02:30 +00:00
|
|
|
{
|
|
|
|
GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
|
|
|
|
clockid_t ptype;
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
|
2002-05-26 21:54:27 +00:00
|
|
|
|
2009-08-28 15:02:30 +00:00
|
|
|
if (G_UNLIKELY (clock_gettime (ptype, &ts)))
|
|
|
|
return GST_CLOCK_TIME_NONE;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2009-08-28 15:02:30 +00:00
|
|
|
return GST_TIMESPEC_TO_TIME (ts);
|
|
|
|
}
|
2009-02-03 17:04:46 +00:00
|
|
|
#endif
|
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
|
|
|
{
|
2009-08-28 15:02:30 +00:00
|
|
|
#ifdef G_OS_WIN32
|
2009-08-29 00:44:51 +00:00
|
|
|
GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
|
|
|
|
|
|
|
|
if (sysclock->priv->frequency.QuadPart != 0) {
|
2009-08-28 15:02:30 +00:00
|
|
|
return GST_SECOND / sysclock->priv->frequency.QuadPart;
|
|
|
|
} else
|
|
|
|
#endif /* G_OS_WIN32 */
|
2013-04-08 11:30:07 +00:00
|
|
|
#if defined(HAVE_POSIX_TIMERS) && defined(HAVE_CLOCK_GETTIME)
|
2009-08-28 15:02:30 +00:00
|
|
|
{
|
|
|
|
GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
|
|
|
|
clockid_t ptype;
|
|
|
|
struct timespec ts;
|
2009-02-03 17:04:46 +00:00
|
|
|
|
2009-08-28 15:02:30 +00:00
|
|
|
ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
|
2009-02-03 17:04:46 +00:00
|
|
|
|
2009-08-28 15:02:30 +00:00
|
|
|
if (G_UNLIKELY (clock_getres (ptype, &ts)))
|
|
|
|
return GST_CLOCK_TIME_NONE;
|
2009-02-03 17:04:46 +00:00
|
|
|
|
2009-08-28 15:02:30 +00:00
|
|
|
return GST_TIMESPEC_TO_TIME (ts);
|
|
|
|
}
|
2009-02-03 17:04:46 +00:00
|
|
|
#else
|
2009-08-28 15:02:30 +00:00
|
|
|
{
|
|
|
|
return 1 * GST_USECOND;
|
|
|
|
}
|
2009-02-03 17:04:46 +00:00
|
|
|
#endif
|
2002-02-03 20:07:09 +00:00
|
|
|
}
|
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
/* synchronously wait on the given GstClockEntry.
|
|
|
|
*
|
2010-10-21 16:08:51 +00:00
|
|
|
* We do this by blocking on the global GstPoll timer with
|
|
|
|
* the requested timeout. This allows us to unblock the
|
|
|
|
* entry by writing on the control fd.
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
2010-10-21 16:08:51 +00:00
|
|
|
* Note that writing the global GstPoll unlocks all waiting entries. So
|
2005-03-07 18:27:42 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* MT safe.
|
|
|
|
*/
|
|
|
|
static GstClockReturn
|
2006-08-11 15:24:03 +00:00
|
|
|
gst_system_clock_id_wait_jitter_unlocked (GstClock * clock,
|
2006-12-07 10:51:36 +00:00
|
|
|
GstClockEntry * entry, GstClockTimeDiff * jitter, gboolean restart)
|
2002-11-02 13:54:34 +00:00
|
|
|
{
|
2009-02-03 17:04:46 +00:00
|
|
|
GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
|
2010-10-21 16:08:51 +00:00
|
|
|
GstClockTime entryt, now;
|
2005-03-07 18:27:42 +00:00
|
|
|
GstClockTimeDiff diff;
|
2010-10-21 16:08:51 +00:00
|
|
|
GstClockReturn status;
|
|
|
|
|
2014-10-06 11:38:21 +00:00
|
|
|
status = GET_ENTRY_STATUS (entry);
|
|
|
|
if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
|
2010-10-21 16:08:51 +00:00
|
|
|
return GST_CLOCK_UNSCHEDULED;
|
2002-12-27 04:11:59 +00:00
|
|
|
|
2007-12-24 14:35:24 +00:00
|
|
|
/* need to call the overridden method because we want to sync against the time
|
|
|
|
* of the clock, whatever the subclass uses as a clock. */
|
2010-10-21 16:08:51 +00:00
|
|
|
now = gst_clock_get_time (clock);
|
2009-02-03 17:04:46 +00:00
|
|
|
|
|
|
|
/* get the time of the entry */
|
2005-04-28 16:28:28 +00:00
|
|
|
entryt = GST_CLOCK_ENTRY_TIME (entry);
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2007-12-24 14:35:24 +00:00
|
|
|
/* the diff of the entry with the clock is the amount of time we have to
|
|
|
|
* wait */
|
2010-10-21 16:08:51 +00:00
|
|
|
diff = GST_CLOCK_DIFF (now, entryt);
|
|
|
|
if (G_LIKELY (jitter))
|
|
|
|
*jitter = -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"
|
2009-02-03 17:04:46 +00:00
|
|
|
" time %" GST_TIME_FORMAT
|
2005-03-07 18:27:42 +00:00
|
|
|
" now %" GST_TIME_FORMAT
|
2009-02-03 17:04:46 +00:00
|
|
|
" diff (time-now) %" G_GINT64_FORMAT,
|
2010-10-21 16:08:51 +00:00
|
|
|
entry, GST_TIME_ARGS (entryt), GST_TIME_ARGS (now), diff);
|
2002-12-11 21:33:07 +00:00
|
|
|
|
2010-10-21 16:08:51 +00:00
|
|
|
if (G_LIKELY (diff > 0)) {
|
2006-05-30 12:04:29 +00:00
|
|
|
#ifdef WAIT_DEBUGGING
|
2009-02-03 17:04:46 +00:00
|
|
|
GstClockTime final;
|
2006-05-30 12:04:29 +00:00
|
|
|
#endif
|
|
|
|
|
2010-10-21 16:08:51 +00:00
|
|
|
while (TRUE) {
|
2009-02-03 17:04:46 +00:00
|
|
|
gint pollret;
|
2005-03-07 18:27:42 +00:00
|
|
|
|
2010-10-21 16:08:51 +00:00
|
|
|
do {
|
|
|
|
status = GET_ENTRY_STATUS (entry);
|
|
|
|
|
|
|
|
/* stop when we are unscheduled */
|
|
|
|
if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
|
|
|
|
goto done;
|
2006-05-30 12:04:29 +00:00
|
|
|
|
2010-10-21 16:08:51 +00:00
|
|
|
/* mark the entry as busy but watch out for intermediate unscheduled
|
|
|
|
* statuses */
|
|
|
|
} while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_BUSY)));
|
|
|
|
|
|
|
|
/* now wait on the entry, it either times out or the fd is written. The
|
|
|
|
* status of the entry is only BUSY around the poll. */
|
2009-02-03 17:04:46 +00:00
|
|
|
pollret = gst_poll_wait (sysclock->priv->timer, diff);
|
|
|
|
|
2010-10-21 16:08:51 +00:00
|
|
|
/* get the new status, mark as DONE. We do this so that the unschedule
|
|
|
|
* function knows when we left the poll and doesn't need to wakeup the
|
|
|
|
* poll anymore. */
|
|
|
|
do {
|
|
|
|
status = GET_ENTRY_STATUS (entry);
|
|
|
|
/* we were unscheduled, exit immediately */
|
|
|
|
if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
|
|
|
|
break;
|
|
|
|
} while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_DONE)));
|
|
|
|
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked, status %d, ret %d",
|
|
|
|
entry, status, pollret);
|
|
|
|
|
|
|
|
if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
|
|
|
|
/* try to clean up The unschedule function managed to set the status to
|
|
|
|
* unscheduled. We now take the lock and mark the entry as unscheduled.
|
|
|
|
* This makes sure that the unschedule function doesn't perform a
|
|
|
|
* wakeup anymore. If the unschedule function has a change to perform
|
|
|
|
* the wakeup before us, we clean up here */
|
|
|
|
GST_OBJECT_LOCK (sysclock);
|
|
|
|
entry->unscheduled = TRUE;
|
|
|
|
if (entry->woken_up) {
|
|
|
|
gst_system_clock_remove_wakeup (sysclock);
|
|
|
|
}
|
|
|
|
GST_OBJECT_UNLOCK (sysclock);
|
|
|
|
goto done;
|
2009-02-03 17:04:46 +00:00
|
|
|
} else {
|
2010-10-21 16:08:51 +00:00
|
|
|
if (G_UNLIKELY (pollret != 0)) {
|
2009-02-03 17:04:46 +00:00
|
|
|
/* some other id got unlocked */
|
|
|
|
if (!restart) {
|
|
|
|
/* this can happen if the entry got unlocked because of an async
|
|
|
|
* entry was added to the head of the async queue. */
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup waiting for entry %p", entry);
|
2010-10-21 16:08:51 +00:00
|
|
|
goto done;
|
2009-02-03 17:04:46 +00:00
|
|
|
}
|
|
|
|
|
2009-03-26 17:46:35 +00:00
|
|
|
/* wait till all the entries got woken up */
|
2010-10-21 16:08:51 +00:00
|
|
|
GST_OBJECT_LOCK (sysclock);
|
2009-03-26 17:46:35 +00:00
|
|
|
gst_system_clock_wait_wakeup (sysclock);
|
2010-10-21 16:08:51 +00:00
|
|
|
GST_OBJECT_UNLOCK (sysclock);
|
2009-02-03 17:04:46 +00:00
|
|
|
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p needs to be restarted",
|
|
|
|
entry);
|
|
|
|
} else {
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked after timeout",
|
|
|
|
entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reschedule if gst_poll_wait returned early or we have to reschedule after
|
|
|
|
* an unlock*/
|
2010-10-21 16:08:51 +00:00
|
|
|
now = gst_clock_get_time (clock);
|
|
|
|
diff = GST_CLOCK_DIFF (now, entryt);
|
2009-02-03 17:04:46 +00:00
|
|
|
|
|
|
|
if (diff <= 0) {
|
|
|
|
/* timeout, this is fine, we can report success now */
|
2014-10-06 11:38:21 +00:00
|
|
|
if (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, GST_CLOCK_DONE, GST_CLOCK_OK))) {
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "unexpected status for entry %p", entry);
|
|
|
|
status = GET_ENTRY_STATUS (entry);
|
|
|
|
goto done;
|
|
|
|
} else {
|
|
|
|
status = GST_CLOCK_OK;
|
|
|
|
}
|
2009-02-03 17:04:46 +00:00
|
|
|
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK,
|
|
|
|
"entry %p finished, diff %" G_GINT64_FORMAT, entry, diff);
|
|
|
|
|
|
|
|
#ifdef WAIT_DEBUGGING
|
|
|
|
final = gst_system_clock_get_internal_time (clock);
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "Waited for %" G_GINT64_FORMAT
|
|
|
|
" got %" G_GINT64_FORMAT " diff %" G_GINT64_FORMAT
|
|
|
|
" %g target-offset %" G_GINT64_FORMAT " %g", entryt, now,
|
|
|
|
now - entryt,
|
|
|
|
(double) (GstClockTimeDiff) (now - entryt) / GST_SECOND,
|
|
|
|
(final - target),
|
|
|
|
((double) (GstClockTimeDiff) (final - target)) / GST_SECOND);
|
2006-05-30 12:04:29 +00:00
|
|
|
#endif
|
2010-10-21 16:08:51 +00:00
|
|
|
goto done;
|
2009-02-03 17:04:46 +00:00
|
|
|
} else {
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK,
|
|
|
|
"entry %p restart, diff %" G_GINT64_FORMAT, entry, diff);
|
|
|
|
}
|
2005-03-07 18:27:42 +00:00
|
|
|
}
|
|
|
|
}
|
2004-03-13 15:27:01 +00:00
|
|
|
} else {
|
2010-10-21 16:08:51 +00:00
|
|
|
/* we are right on time or too late */
|
2014-10-06 11:38:21 +00:00
|
|
|
if (G_UNLIKELY (diff == 0)) {
|
|
|
|
if (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_OK))) {
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "unexpected status for entry %p", entry);
|
|
|
|
status = GET_ENTRY_STATUS (entry);
|
|
|
|
} else {
|
|
|
|
status = GST_CLOCK_OK;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_EARLY))) {
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "unexpected status for entry %p", entry);
|
|
|
|
status = GET_ENTRY_STATUS (entry);
|
|
|
|
} else {
|
|
|
|
status = GST_CLOCK_EARLY;
|
|
|
|
}
|
|
|
|
}
|
2005-03-07 18:27:42 +00:00
|
|
|
}
|
2010-10-21 16:08:51 +00:00
|
|
|
done:
|
|
|
|
return status;
|
2005-03-07 18:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstClockReturn
|
2006-08-11 15:24:03 +00:00
|
|
|
gst_system_clock_id_wait_jitter (GstClock * clock, GstClockEntry * entry,
|
|
|
|
GstClockTimeDiff * jitter)
|
2005-03-07 18:27:42 +00:00
|
|
|
{
|
2010-10-21 16:08:51 +00:00
|
|
|
return gst_system_clock_id_wait_jitter_unlocked (clock, entry, jitter, TRUE);
|
2005-03-07 18:27:42 +00:00
|
|
|
}
|
|
|
|
|
2007-03-12 15:27:05 +00:00
|
|
|
/* Start the async clock thread. Must be called with the object lock
|
|
|
|
* held */
|
|
|
|
static gboolean
|
|
|
|
gst_system_clock_start_async (GstSystemClock * clock)
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
2012-02-27 08:02:07 +00:00
|
|
|
GstSystemClockPrivate *priv = clock->priv;
|
2007-03-12 15:27:05 +00:00
|
|
|
|
2012-02-27 08:02:07 +00:00
|
|
|
if (G_LIKELY (priv->thread != NULL))
|
2007-03-12 15:27:05 +00:00
|
|
|
return TRUE; /* Thread already running. Nothing to do */
|
|
|
|
|
2012-02-27 08:02:07 +00:00
|
|
|
priv->thread = g_thread_try_new ("GstSystemClock",
|
2011-12-03 13:58:51 +00:00
|
|
|
(GThreadFunc) gst_system_clock_async_thread, clock, &error);
|
|
|
|
|
2009-03-26 21:05:31 +00:00
|
|
|
if (G_UNLIKELY (error))
|
2007-03-12 15:27:05 +00:00
|
|
|
goto no_thread;
|
|
|
|
|
|
|
|
/* wait for it to spin up */
|
2012-02-27 08:02:07 +00:00
|
|
|
GST_SYSTEM_CLOCK_WAIT (clock);
|
2007-03-12 15:27:05 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
no_thread:
|
|
|
|
{
|
|
|
|
g_warning ("could not create async clock thread: %s", error->message);
|
2009-03-26 21:05:31 +00:00
|
|
|
g_error_free (error);
|
2007-03-12 15:27:05 +00:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
/* 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)
|
|
|
|
{
|
2009-02-03 17:04:46 +00:00
|
|
|
GstSystemClock *sysclock;
|
2012-02-27 08:02:07 +00:00
|
|
|
GstSystemClockPrivate *priv;
|
2009-03-26 17:46:35 +00:00
|
|
|
GstClockEntry *head;
|
2009-02-03 17:04:46 +00:00
|
|
|
|
|
|
|
sysclock = GST_SYSTEM_CLOCK_CAST (clock);
|
2012-02-27 08:02:07 +00:00
|
|
|
priv = sysclock->priv;
|
2009-02-03 17:04:46 +00:00
|
|
|
|
2007-03-22 11:58:08 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "adding async entry %p", entry);
|
2005-03-07 18:27:42 +00:00
|
|
|
|
2005-11-21 16:34:26 +00:00
|
|
|
GST_OBJECT_LOCK (clock);
|
2007-03-12 15:27:05 +00:00
|
|
|
/* Start the clock async thread if needed */
|
2009-03-26 21:05:31 +00:00
|
|
|
if (G_UNLIKELY (!gst_system_clock_start_async (sysclock)))
|
2007-03-12 15:27:05 +00:00
|
|
|
goto thread_error;
|
|
|
|
|
2010-10-21 16:08:51 +00:00
|
|
|
if (G_UNLIKELY (GET_ENTRY_STATUS (entry) == GST_CLOCK_UNSCHEDULED))
|
2009-03-26 21:05:31 +00:00
|
|
|
goto was_unscheduled;
|
|
|
|
|
2012-02-27 08:02:07 +00:00
|
|
|
if (priv->entries)
|
|
|
|
head = priv->entries->data;
|
2009-03-26 17:46:35 +00:00
|
|
|
else
|
|
|
|
head = NULL;
|
2009-02-03 17:04:46 +00:00
|
|
|
|
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 */
|
2012-02-27 08:02:07 +00:00
|
|
|
priv->entries = g_list_insert_sorted (priv->entries, entry,
|
2005-03-07 18:27:42 +00:00
|
|
|
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. */
|
2012-02-27 08:02:07 +00:00
|
|
|
if (priv->entries->data == entry) {
|
2010-10-21 16:08:51 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry added to head %p", head);
|
2009-03-26 17:46:35 +00:00
|
|
|
if (head == NULL) {
|
2009-02-03 17:04:46 +00:00
|
|
|
/* the list was empty before, signal the cond so that the async thread can
|
|
|
|
* start taking a look at the queue */
|
2009-03-26 17:46:35 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "first entry, sending signal");
|
2012-02-27 08:02:07 +00:00
|
|
|
GST_SYSTEM_CLOCK_BROADCAST (clock);
|
2009-02-03 17:04:46 +00:00
|
|
|
} else {
|
2010-10-21 16:08:51 +00:00
|
|
|
GstClockReturn status;
|
|
|
|
|
|
|
|
status = GET_ENTRY_STATUS (head);
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "head entry %p status %d", head, status);
|
|
|
|
|
|
|
|
if (status == GST_CLOCK_BUSY) {
|
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "head entry is busy");
|
2009-03-26 17:46:35 +00:00
|
|
|
/* the async thread was waiting for an entry, unlock the wait so that it
|
|
|
|
* looks at the new head entry instead, we only need to do this once */
|
2012-02-27 08:02:07 +00:00
|
|
|
if (!priv->async_wakeup) {
|
2009-03-26 17:46:35 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup async thread");
|
2012-02-27 08:02:07 +00:00
|
|
|
priv->async_wakeup = TRUE;
|
2009-03-26 17:46:35 +00:00
|
|
|
gst_system_clock_add_wakeup (sysclock);
|
|
|
|
}
|
|
|
|
}
|
2009-02-03 17:04:46 +00:00
|
|
|
}
|
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;
|
2007-03-12 15:27:05 +00:00
|
|
|
|
2009-03-26 21:05:31 +00:00
|
|
|
/* ERRORS */
|
2007-03-12 15:27:05 +00:00
|
|
|
thread_error:
|
2009-03-26 21:05:31 +00:00
|
|
|
{
|
|
|
|
/* Could not start the async clock thread */
|
|
|
|
GST_OBJECT_UNLOCK (clock);
|
|
|
|
return GST_CLOCK_ERROR;
|
|
|
|
}
|
|
|
|
was_unscheduled:
|
|
|
|
{
|
|
|
|
GST_OBJECT_UNLOCK (clock);
|
|
|
|
return GST_CLOCK_UNSCHEDULED;
|
|
|
|
}
|
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
|
|
|
{
|
2009-03-26 17:46:35 +00:00
|
|
|
GstSystemClock *sysclock;
|
2010-10-21 16:08:51 +00:00
|
|
|
GstClockReturn status;
|
2009-03-26 17:46:35 +00:00
|
|
|
|
|
|
|
sysclock = GST_SYSTEM_CLOCK_CAST (clock);
|
|
|
|
|
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);
|
2010-10-21 16:08:51 +00:00
|
|
|
/* change the entry status to unscheduled */
|
|
|
|
do {
|
|
|
|
status = GET_ENTRY_STATUS (entry);
|
|
|
|
} while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status,
|
|
|
|
GST_CLOCK_UNSCHEDULED)));
|
|
|
|
|
|
|
|
if (G_LIKELY (status == GST_CLOCK_BUSY)) {
|
2009-02-03 17:04:46 +00:00
|
|
|
/* the entry was being busy, wake up all entries so that they recheck their
|
|
|
|
* status. We cannot wake up just one entry because allocating such a
|
2009-03-26 17:46:35 +00:00
|
|
|
* datastructure for each entry would be too heavy and unlocking an entry
|
2009-02-03 17:04:46 +00:00
|
|
|
* is usually done when shutting down or some other exceptional case. */
|
2009-03-26 17:46:35 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_CLOCK, "entry was BUSY, doing wakeup");
|
2010-10-21 16:08:51 +00:00
|
|
|
if (!entry->unscheduled && !entry->woken_up) {
|
|
|
|
gst_system_clock_add_wakeup (sysclock);
|
|
|
|
entry->woken_up = TRUE;
|
|
|
|
}
|
2009-02-03 17:04:46 +00:00
|
|
|
}
|
2005-11-21 16:34:26 +00:00
|
|
|
GST_OBJECT_UNLOCK (clock);
|
2002-12-27 04:11:59 +00:00
|
|
|
}
|