gstclock.c 4.25 KB
Newer Older
1 2 3 4 5
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
 *
 * gstclock.c: Clock subsystem for maintaining time sync
Wim Taymans's avatar
Wim Taymans committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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>
24 25 26 27

//#define GST_DEBUG_ENABLED
#include "gst_private.h"

28
#include "gstelement.h"
29
#include "gstclock.h"
Wim Taymans's avatar
Wim Taymans committed
30

31

Wim Taymans's avatar
Wim Taymans committed
32 33 34 35 36 37 38 39 40 41
static GstClock *the_system_clock = NULL;

/**
 * gst_clock_new:
 * @name: the name of the new clock
 *
 * create a new clock element
 *
 * Returns: the new clock element
 */
Wim Taymans's avatar
Wim Taymans committed
42 43 44
GstClock*
gst_clock_new (gchar *name) 
{
Wim Taymans's avatar
Wim Taymans committed
45
  GstClock *clock = (GstClock *) g_malloc(sizeof(GstClock));
Wim Taymans's avatar
Wim Taymans committed
46 47
  
  clock->name = g_strdup (name);
Wim Taymans's avatar
Wim Taymans committed
48
  clock->sinkobjects = NULL;
Wim Taymans's avatar
Wim Taymans committed
49 50 51 52
  clock->sinkmutex = g_mutex_new ();
  clock->lock = g_mutex_new ();
  g_mutex_lock (clock->sinkmutex);
  
53 54 55
  clock->num = 0;
  clock->num_locked = 0;
  clock->locking = FALSE;
Wim Taymans's avatar
Wim Taymans committed
56
  
Wim Taymans's avatar
Wim Taymans committed
57 58 59
  return clock;
}

Wim Taymans's avatar
Wim Taymans committed
60 61 62
GstClock*
gst_clock_get_system(void) 
{
Wim Taymans's avatar
Wim Taymans committed
63
  if (the_system_clock == NULL) {
Wim Taymans's avatar
Wim Taymans committed
64 65
    the_system_clock = gst_clock_new ("system_clock");
    gst_clock_reset (the_system_clock);
Wim Taymans's avatar
Wim Taymans committed
66 67 68 69
  }
  return the_system_clock;
}

Wim Taymans's avatar
Wim Taymans committed
70 71 72
void 
gst_clock_register (GstClock *clock, GstObject *obj) 
{
73
  if ((GST_ELEMENT(obj))->numsrcpads == 0) {
74
    GST_DEBUG (0,"gst_clock: setting registered sink object 0x%p\n", obj);
Wim Taymans's avatar
Wim Taymans committed
75
    clock->sinkobjects = g_list_append (clock->sinkobjects, obj);
76
    clock->num++;
Wim Taymans's avatar
Wim Taymans committed
77 78 79
  }
}

Wim Taymans's avatar
Wim Taymans committed
80 81 82
void 
gst_clock_set (GstClock *clock, GstClockTime time) 
{
Wim Taymans's avatar
Wim Taymans committed
83
  struct timeval tfnow;
84
  GstClockTime now;
Wim Taymans's avatar
Wim Taymans committed
85

Wim Taymans's avatar
Wim Taymans committed
86
  gettimeofday (&tfnow, (struct timezone *)NULL);
87
  now = tfnow.tv_sec*1000000LL+tfnow.tv_usec;
Wim Taymans's avatar
Wim Taymans committed
88
  g_mutex_lock (clock->lock);
89
  clock->start_time = now - time;
Wim Taymans's avatar
Wim Taymans committed
90
  g_mutex_unlock (clock->lock);
91
  GST_DEBUG (0,"gst_clock: setting clock to %llu %llu %llu\n", time, now, clock->start_time);
92 93
}

Wim Taymans's avatar
Wim Taymans committed
94 95
GstClockTimeDiff 
gst_clock_current_diff (GstClock *clock, GstClockTime time)
96 97 98 99
{
  struct timeval tfnow;
  GstClockTime now;

Wim Taymans's avatar
Wim Taymans committed
100 101
  gettimeofday (&tfnow, (struct timezone *)NULL);
  g_mutex_lock (clock->lock);
102
  now = ((guint64)tfnow.tv_sec*1000000LL+tfnow.tv_usec) - (guint64)clock->start_time; 
Wim Taymans's avatar
Wim Taymans committed
103
  g_mutex_unlock (clock->lock);
104

Wim Taymans's avatar
Wim Taymans committed
105
  return GST_CLOCK_DIFF (time, now);
Wim Taymans's avatar
Wim Taymans committed
106 107
}

Wim Taymans's avatar
Wim Taymans committed
108 109 110
void 
gst_clock_reset (GstClock *clock) 
{
Wim Taymans's avatar
Wim Taymans committed
111 112
  struct timeval tfnow;

Wim Taymans's avatar
Wim Taymans committed
113 114
  gettimeofday (&tfnow, (struct timezone *)NULL);
  g_mutex_lock (clock->lock);
115
  clock->start_time = ((guint64)tfnow.tv_sec)*1000000LL+tfnow.tv_usec;
Wim Taymans's avatar
Wim Taymans committed
116 117
  clock->current_time = clock->start_time;
  clock->adjust = 0LL;
118
  GST_DEBUG (0,"gst_clock: setting start clock %llu\n", clock->start_time);
Wim Taymans's avatar
Wim Taymans committed
119
  g_mutex_unlock (clock->lock);
Wim Taymans's avatar
Wim Taymans committed
120 121
}

Wim Taymans's avatar
Wim Taymans committed
122 123 124
void 
gst_clock_wait (GstClock *clock, GstClockTime time, GstObject *obj) 
{
Wim Taymans's avatar
Wim Taymans committed
125
  struct timeval tfnow;
126
  GstClockTime now;
Wim Taymans's avatar
Wim Taymans committed
127 128 129
  GstClockTimeDiff diff;


Wim Taymans's avatar
Wim Taymans committed
130 131
  gettimeofday (&tfnow, (struct timezone *)NULL);
  g_mutex_lock (clock->lock);
132
  now = tfnow.tv_sec*1000000LL+tfnow.tv_usec - clock->start_time;
Wim Taymans's avatar
Wim Taymans committed
133

Wim Taymans's avatar
Wim Taymans committed
134
  diff = GST_CLOCK_DIFF (time, now);
Wim Taymans's avatar
Wim Taymans committed
135
  // if we are not behind wait a bit
136
  GST_DEBUG (0,"gst_clock: %s waiting for time %08llu %08llu %08lld\n", gst_element_get_name(GST_ELEMENT(obj)), time, now, diff);
Wim Taymans's avatar
Wim Taymans committed
137
   
Wim Taymans's avatar
Wim Taymans committed
138
  g_mutex_unlock (clock->lock);
139 140
  if (diff > 10000 ) {
    tfnow.tv_usec = (diff % 1000000);
Wim Taymans's avatar
Wim Taymans committed
141 142
    tfnow.tv_sec = diff / 1000000;
    // FIXME, this piece of code does not work with egcs optimisations on, had to use the following line
143 144 145
    if (!tfnow.tv_sec) {
      select(0, NULL, NULL, NULL, &tfnow);
    }
146
    else GST_DEBUG (0,"gst_clock: %s waiting %u %llu %llu %llu seconds\n", gst_element_get_name(GST_ELEMENT(obj)), 
147
		    (int)tfnow.tv_sec, now, diff, time);
Wim Taymans's avatar
Wim Taymans committed
148
  }
149
  GST_DEBUG (0,"gst_clock: %s waiting for time %08llu %08llu %08lld done \n", gst_element_get_name(GST_ELEMENT(obj)), time, now, diff);
Wim Taymans's avatar
Wim Taymans committed
150
}