2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 //#define DEBUG_ENABLED
26 static GstClock *the_system_clock = NULL;
30 * @name: the name of the new clock
32 * create a new clock element
34 * Returns: the new clock element
37 gst_clock_new (gchar *name)
39 GstClock *clock = (GstClock *) g_malloc(sizeof(GstClock));
41 clock->name = g_strdup (name);
42 clock->sinkobjects = NULL;
43 clock->sinkmutex = g_mutex_new ();
44 clock->lock = g_mutex_new ();
45 g_mutex_lock (clock->sinkmutex);
48 clock->num_locked = 0;
49 clock->locking = FALSE;
55 gst_clock_get_system(void)
57 if (the_system_clock == NULL) {
58 the_system_clock = gst_clock_new ("system_clock");
59 gst_clock_reset (the_system_clock);
61 return the_system_clock;
65 gst_clock_register (GstClock *clock, GstObject *obj)
67 if (GST_IS_SINK (obj)) {
68 DEBUG("gst_clock: setting registered sink object 0x%p\n", obj);
69 clock->sinkobjects = g_list_append (clock->sinkobjects, obj);
75 gst_clock_set (GstClock *clock, GstClockTime time)
80 gettimeofday (&tfnow, (struct timezone *)NULL);
81 now = tfnow.tv_sec*1000000LL+tfnow.tv_usec;
82 g_mutex_lock (clock->lock);
83 clock->start_time = now - time;
84 g_mutex_unlock (clock->lock);
85 DEBUG("gst_clock: setting clock to %llu %llu %llu\n", time, now, clock->start_time);
89 gst_clock_current_diff (GstClock *clock, GstClockTime time)
94 gettimeofday (&tfnow, (struct timezone *)NULL);
95 g_mutex_lock (clock->lock);
96 now = ((guint64)tfnow.tv_sec*1000000LL+tfnow.tv_usec) - (guint64)clock->start_time;
97 g_mutex_unlock (clock->lock);
99 return GST_CLOCK_DIFF (time, now);
103 gst_clock_reset (GstClock *clock)
105 struct timeval tfnow;
107 gettimeofday (&tfnow, (struct timezone *)NULL);
108 g_mutex_lock (clock->lock);
109 clock->start_time = ((guint64)tfnow.tv_sec)*1000000LL+tfnow.tv_usec;
110 clock->current_time = clock->start_time;
112 DEBUG("gst_clock: setting start clock %llu\n", clock->start_time);
113 g_mutex_unlock (clock->lock);
117 gst_clock_wait (GstClock *clock, GstClockTime time, GstObject *obj)
119 struct timeval tfnow;
121 GstClockTimeDiff diff;
124 gettimeofday (&tfnow, (struct timezone *)NULL);
125 g_mutex_lock (clock->lock);
126 now = tfnow.tv_sec*1000000LL+tfnow.tv_usec - clock->start_time;
128 diff = GST_CLOCK_DIFF (time, now);
129 // if we are not behind wait a bit
130 DEBUG("gst_clock: %s waiting for time %08llu %08llu %08lld\n", gst_element_get_name(GST_ELEMENT(obj)), time, now, diff);
132 g_mutex_unlock (clock->lock);
134 tfnow.tv_usec = (diff % 1000000);
135 tfnow.tv_sec = diff / 1000000;
136 // FIXME, this piece of code does not work with egcs optimisations on, had to use the following line
138 select(0, NULL, NULL, NULL, &tfnow);
140 else DEBUG("gst_clock: %s waiting %u %llu %llu %llu seconds\n", gst_element_get_name(GST_ELEMENT(obj)),
141 (int)tfnow.tv_sec, now, diff, time);
143 DEBUG("gst_clock: %s waiting for time %08llu %08llu %08lld done \n", gst_element_get_name(GST_ELEMENT(obj)), time, now, diff);