2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wim.taymans@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"
29 #include "gstsystemclock.h"
31 static GstClock *_the_system_clock = NULL;
33 static void gst_system_clock_class_init (GstSystemClockClass *klass);
34 static void gst_system_clock_init (GstSystemClock *clock);
36 static GstClockTime gst_system_clock_get_internal_time (GstClock *clock);
37 static guint64 gst_system_clock_get_resolution (GstClock *clock);
38 static GstClockEntryStatus gst_system_clock_wait (GstClock *clock, GstClockEntry *entry);
39 static void gst_system_clock_unlock (GstClock *clock, GstClockEntry *entry);
41 static GCond *_gst_sysclock_cond = NULL;
42 static GMutex *_gst_sysclock_mutex = NULL;
44 static GstClockClass *parent_class = NULL;
45 /* static guint gst_system_clock_signals[LAST_SIGNAL] = { 0 }; */
48 gst_system_clock_get_type (void)
50 static GType clock_type = 0;
53 static const GTypeInfo clock_info = {
54 sizeof (GstSystemClockClass),
57 (GClassInitFunc) gst_system_clock_class_init,
60 sizeof (GstSystemClock),
62 (GInstanceInitFunc) gst_system_clock_init,
65 clock_type = g_type_register_static (GST_TYPE_CLOCK, "GstSystemClock",
72 gst_system_clock_class_init (GstSystemClockClass *klass)
74 GObjectClass *gobject_class;
75 GstObjectClass *gstobject_class;
76 GstClockClass *gstclock_class;
78 gobject_class = (GObjectClass*) klass;
79 gstobject_class = (GstObjectClass*) klass;
80 gstclock_class = (GstClockClass*) klass;
82 parent_class = g_type_class_ref (GST_TYPE_CLOCK);
84 gstclock_class->get_internal_time = gst_system_clock_get_internal_time;
85 gstclock_class->get_resolution = gst_system_clock_get_resolution;
86 gstclock_class->wait = gst_system_clock_wait;
87 gstclock_class->unlock = gst_system_clock_unlock;
89 _gst_sysclock_cond = g_cond_new ();
90 _gst_sysclock_mutex = g_mutex_new ();
94 gst_system_clock_init (GstSystemClock *clock)
99 * gst_system_clock_obtain
101 * Get a handle to the default system clock.
103 * Returns: the default clock.
106 gst_system_clock_obtain (void)
108 if (_the_system_clock == NULL) {
109 _the_system_clock = GST_CLOCK (g_object_new (GST_TYPE_SYSTEM_CLOCK, NULL));
111 gst_object_set_name (GST_OBJECT (_the_system_clock), "GstSystemClock");
113 gst_object_ref (GST_OBJECT (_the_system_clock));
114 gst_object_sink (GST_OBJECT (_the_system_clock));
116 return _the_system_clock;
120 gst_system_clock_get_internal_time (GstClock *clock)
124 g_get_current_time (&timeval);
126 return GST_TIMEVAL_TO_TIME (timeval);
130 gst_system_clock_get_resolution (GstClock *clock)
132 return 1 * GST_USECOND;
135 static GstClockEntryStatus
136 gst_system_clock_wait (GstClock *clock, GstClockEntry *entry)
138 GstClockEntryStatus res;
139 GstClockTime current, target;
142 current = gst_clock_get_time (clock);
143 diff = GST_CLOCK_ENTRY_TIME (entry) - current;
145 if (ABS (diff) > clock->max_diff) {
146 g_warning ("abnormal clock request diff: ABS(%" G_GINT64_FORMAT
147 ") > %" G_GINT64_FORMAT, diff, clock->max_diff);
148 return GST_CLOCK_ENTRY_EARLY;
151 target = gst_system_clock_get_internal_time (clock) + diff;
153 GST_DEBUG (GST_CAT_CLOCK, "real_target %" G_GUINT64_FORMAT
154 " target %" G_GUINT64_FORMAT
155 " now %" G_GUINT64_FORMAT,
156 target, GST_CLOCK_ENTRY_TIME (entry), current);
158 if (((gint64)target) > 0) {
161 GST_TIME_TO_TIMEVAL (target, tv);
162 g_mutex_lock (_gst_sysclock_mutex);
163 g_cond_timed_wait (_gst_sysclock_cond, _gst_sysclock_mutex, &tv);
164 g_mutex_unlock (_gst_sysclock_mutex);
168 res = GST_CLOCK_ENTRY_EARLY;
174 gst_system_clock_unlock (GstClock *clock, GstClockEntry *entry)
176 g_mutex_lock (_gst_sysclock_mutex);
177 g_cond_broadcast (_gst_sysclock_cond);
178 g_mutex_unlock (_gst_sysclock_mutex);