1 /* GStreamer
2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wim.taymans@chello.be>
4 *
5 * gstclock.c: Clock subsystem for maintaining time sync
6 *
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.
11 *
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.
16 *
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.
21 */
23 #include <sys/time.h>
25 /* #define GST_DEBUG_ENABLED */
26 #include "gst_private.h"
28 #include "gstsystemclock.h"
30 #define CLASS(clock) GST_SYSTEM_CLOCK_CLASS (G_OBJECT_GET_CLASS (clock))
32 static GstClock *_the_system_clock = NULL;
34 static void gst_system_clock_class_init (GstSystemClockClass *klass);
35 static void gst_system_clock_init (GstSystemClock *clock);
37 static GstClockTime gst_system_clock_get_internal_time (GstClock *clock);
38 static guint64 gst_system_clock_get_resolution (GstClock *clock);
39 static GstClockEntryStatus gst_system_clock_wait (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 }; */
47 GType
48 gst_system_clock_get_type (void)
49 {
50 static GType clock_type = 0;
52 if (!clock_type) {
53 static const GTypeInfo clock_info = {
54 sizeof (GstSystemClockClass),
55 NULL,
56 NULL,
57 (GClassInitFunc) gst_system_clock_class_init,
58 NULL,
59 NULL,
60 sizeof (GstSystemClock),
61 4,
62 (GInstanceInitFunc) gst_system_clock_init,
63 NULL
64 };
65 clock_type = g_type_register_static (GST_TYPE_CLOCK, "GstSystemClock",
66 &clock_info, 0);
67 }
68 return clock_type;
69 }
71 static void
72 gst_system_clock_class_init (GstSystemClockClass *klass)
73 {
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;
88 _gst_sysclock_cond = g_cond_new ();
89 _gst_sysclock_mutex = g_mutex_new ();
90 }
92 static void
93 gst_system_clock_init (GstSystemClock *clock)
94 {
95 }
97 /**
98 * gst_system_clock_obtain
99 *
100 * Get a handle to the default system clock.
101 *
102 * Returns: the default clock.
103 */
104 GstClock*
105 gst_system_clock_obtain (void)
106 {
107 if (_the_system_clock == NULL) {
108 _the_system_clock = GST_CLOCK (g_object_new (GST_TYPE_SYSTEM_CLOCK, NULL));
110 gst_object_set_name (GST_OBJECT (_the_system_clock), "GstSystemClock");
111 }
112 return _the_system_clock;
113 }
115 static GstClockTime
116 gst_system_clock_get_internal_time (GstClock *clock)
117 {
118 GTimeVal timeval;
120 g_get_current_time (&timeval);
122 return GST_TIMEVAL_TO_TIME (timeval);
123 }
125 static guint64
126 gst_system_clock_get_resolution (GstClock *clock)
127 {
128 return 1 * GST_USECOND;
129 }
131 static GstClockEntryStatus
132 gst_system_clock_wait (GstClock *clock, GstClockEntry *entry)
133 {
134 GstClockEntryStatus res = GST_CLOCK_ENTRY_OK;
135 GstClockTime current, target;
137 current = gst_clock_get_time (clock);
138 target = gst_system_clock_get_internal_time (clock) +
139 GST_CLOCK_ENTRY_TIME (entry) - current;
141 GST_DEBUG (GST_CAT_CLOCK, "real_target %llu, target %llu, now %llu",
142 target, GST_CLOCK_ENTRY_TIME (entry), current);
144 if (((gint64)target) > 0) {
145 GTimeVal tv;
147 GST_TIME_TO_TIMEVAL (target, tv);
148 g_mutex_lock (_gst_sysclock_mutex);
149 g_cond_timed_wait (_gst_sysclock_cond, _gst_sysclock_mutex, &tv);
150 g_mutex_unlock (_gst_sysclock_mutex);
151 }
152 else {
153 res = GST_CLOCK_ENTRY_EARLY;
154 }
155 return res;
156 }