2002-02-03 20:07:09 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* 2000 Wim Taymans <wim.taymans@chello.be>
|
|
|
|
*
|
|
|
|
* gstclock.c: Clock subsystem for maintaining time sync
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
/* #define GST_DEBUG_ENABLED */
|
|
|
|
#include "gst_private.h"
|
|
|
|
|
|
|
|
#include "gstsystemclock.h"
|
|
|
|
|
|
|
|
#define CLASS(clock) GST_SYSTEM_CLOCK_CLASS (G_OBJECT_GET_CLASS (clock))
|
|
|
|
|
|
|
|
static GstClock *_the_system_clock = NULL;
|
|
|
|
|
2002-11-02 13:54:34 +00:00
|
|
|
static void gst_system_clock_class_init (GstSystemClockClass *klass);
|
|
|
|
static void gst_system_clock_init (GstSystemClock *clock);
|
2002-02-03 20:07:09 +00:00
|
|
|
|
2002-11-02 13:54:34 +00:00
|
|
|
static GstClockTime gst_system_clock_get_internal_time (GstClock *clock);
|
|
|
|
static guint64 gst_system_clock_get_resolution (GstClock *clock);
|
|
|
|
static GstClockEntryStatus gst_system_clock_wait (GstClock *clock, GstClockEntry *entry);
|
2002-02-03 20:07:09 +00:00
|
|
|
|
2002-11-02 13:54:34 +00:00
|
|
|
static GCond *_gst_sysclock_cond = NULL;
|
|
|
|
static GMutex *_gst_sysclock_mutex = NULL;
|
2002-02-03 20:07:09 +00:00
|
|
|
|
|
|
|
static GstClockClass *parent_class = NULL;
|
|
|
|
/* 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),
|
|
|
|
4,
|
|
|
|
(GInstanceInitFunc) gst_system_clock_init,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
clock_type = g_type_register_static (GST_TYPE_CLOCK, "GstSystemClock",
|
|
|
|
&clock_info, 0);
|
|
|
|
}
|
|
|
|
return clock_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_system_clock_class_init (GstSystemClockClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstObjectClass *gstobject_class;
|
|
|
|
GstClockClass *gstclock_class;
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass*) klass;
|
|
|
|
gstobject_class = (GstObjectClass*) klass;
|
|
|
|
gstclock_class = (GstClockClass*) klass;
|
|
|
|
|
|
|
|
parent_class = g_type_class_ref (GST_TYPE_CLOCK);
|
|
|
|
|
2002-11-02 13:54:34 +00:00
|
|
|
gstclock_class->get_internal_time = gst_system_clock_get_internal_time;
|
|
|
|
gstclock_class->get_resolution = gst_system_clock_get_resolution;
|
|
|
|
gstclock_class->wait = gst_system_clock_wait;
|
|
|
|
|
|
|
|
_gst_sysclock_cond = g_cond_new ();
|
|
|
|
_gst_sysclock_mutex = g_mutex_new ();
|
2002-02-03 20:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_system_clock_init (GstSystemClock *clock)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-03-31 14:04:50 +00:00
|
|
|
/**
|
|
|
|
* gst_system_clock_obtain
|
|
|
|
*
|
|
|
|
* Get a handle to the default system clock.
|
|
|
|
*
|
|
|
|
* Returns: the default clock.
|
|
|
|
*/
|
2002-02-03 20:07:09 +00:00
|
|
|
GstClock*
|
|
|
|
gst_system_clock_obtain (void)
|
|
|
|
{
|
|
|
|
if (_the_system_clock == NULL) {
|
|
|
|
_the_system_clock = GST_CLOCK (g_object_new (GST_TYPE_SYSTEM_CLOCK, NULL));
|
2002-11-02 13:54:34 +00:00
|
|
|
|
2002-02-03 20:07:09 +00:00
|
|
|
gst_object_set_name (GST_OBJECT (_the_system_clock), "GstSystemClock");
|
|
|
|
}
|
|
|
|
return _the_system_clock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstClockTime
|
2002-03-30 17:05:03 +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);
|
2002-05-26 21:54:27 +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
|
|
|
|
gst_system_clock_get_resolution (GstClock *clock)
|
|
|
|
{
|
2002-05-26 21:54:27 +00:00
|
|
|
return 1 * GST_USECOND;
|
2002-02-03 20:07:09 +00:00
|
|
|
}
|
|
|
|
|
2002-11-02 13:54:34 +00:00
|
|
|
static GstClockEntryStatus
|
|
|
|
gst_system_clock_wait (GstClock *clock, GstClockEntry *entry)
|
|
|
|
{
|
|
|
|
GstClockEntryStatus res = GST_CLOCK_ENTRY_OK;
|
|
|
|
GstClockTime current, target;
|
|
|
|
|
|
|
|
current = gst_clock_get_time (clock);
|
|
|
|
target = gst_system_clock_get_internal_time (clock) +
|
|
|
|
GST_CLOCK_ENTRY_TIME (entry) - current;
|
|
|
|
|
|
|
|
GST_DEBUG (GST_CAT_CLOCK, "real_target %llu, target %llu, now %llu",
|
|
|
|
target, GST_CLOCK_ENTRY_TIME (entry), current);
|
|
|
|
|
|
|
|
if (((gint64)target) > 0) {
|
|
|
|
GTimeVal tv;
|
|
|
|
|
|
|
|
GST_TIME_TO_TIMEVAL (target, tv);
|
|
|
|
g_mutex_lock (_gst_sysclock_mutex);
|
|
|
|
g_cond_timed_wait (_gst_sysclock_cond, _gst_sysclock_mutex, &tv);
|
|
|
|
g_mutex_unlock (_gst_sysclock_mutex);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res = GST_CLOCK_ENTRY_EARLY;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2002-02-03 20:07:09 +00:00
|
|
|
|