gstclock.c 4.06 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* Gnome-Streamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *
 * 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>
21
//#define DEBUG_ENABLED
22 23 24
#include "gstclock.h"
#include "gstdebug.h"
#include "gstsink.h"
Wim Taymans's avatar
Wim Taymans committed
25 26 27 28 29 30 31 32 33 34 35

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
36 37 38
GstClock*
gst_clock_new (gchar *name) 
{
Wim Taymans's avatar
Wim Taymans committed
39
  GstClock *clock = (GstClock *) g_malloc(sizeof(GstClock));
Wim Taymans's avatar
Wim Taymans committed
40 41
  
  clock->name = g_strdup (name);
Wim Taymans's avatar
Wim Taymans committed
42
  clock->sinkobjects = NULL;
Wim Taymans's avatar
Wim Taymans committed
43 44 45 46
  clock->sinkmutex = g_mutex_new ();
  clock->lock = g_mutex_new ();
  g_mutex_lock (clock->sinkmutex);
  
47 48 49
  clock->num = 0;
  clock->num_locked = 0;
  clock->locking = FALSE;
Wim Taymans's avatar
Wim Taymans committed
50
  
Wim Taymans's avatar
Wim Taymans committed
51 52 53
  return clock;
}

Wim Taymans's avatar
Wim Taymans committed
54 55 56
GstClock*
gst_clock_get_system(void) 
{
Wim Taymans's avatar
Wim Taymans committed
57
  if (the_system_clock == NULL) {
Wim Taymans's avatar
Wim Taymans committed
58 59
    the_system_clock = gst_clock_new ("system_clock");
    gst_clock_reset (the_system_clock);
Wim Taymans's avatar
Wim Taymans committed
60 61 62 63
  }
  return the_system_clock;
}

Wim Taymans's avatar
Wim Taymans committed
64 65 66 67
void 
gst_clock_register (GstClock *clock, GstObject *obj) 
{
  if (GST_IS_SINK (obj)) {
68
    DEBUG("gst_clock: setting registered sink object 0x%p\n", obj);
Wim Taymans's avatar
Wim Taymans committed
69
    clock->sinkobjects = g_list_append (clock->sinkobjects, obj);
70
    clock->num++;
Wim Taymans's avatar
Wim Taymans committed
71 72 73
  }
}

Wim Taymans's avatar
Wim Taymans committed
74 75 76
void 
gst_clock_set (GstClock *clock, GstClockTime time) 
{
Wim Taymans's avatar
Wim Taymans committed
77
  struct timeval tfnow;
78
  GstClockTime now;
Wim Taymans's avatar
Wim Taymans committed
79

Wim Taymans's avatar
Wim Taymans committed
80
  gettimeofday (&tfnow, (struct timezone *)NULL);
81
  now = tfnow.tv_sec*1000000LL+tfnow.tv_usec;
Wim Taymans's avatar
Wim Taymans committed
82
  g_mutex_lock (clock->lock);
83
  clock->start_time = now - time;
Wim Taymans's avatar
Wim Taymans committed
84
  g_mutex_unlock (clock->lock);
85 86 87
  DEBUG("gst_clock: setting clock to %llu %llu %llu\n", time, now, clock->start_time);
}

Wim Taymans's avatar
Wim Taymans committed
88 89
GstClockTimeDiff 
gst_clock_current_diff (GstClock *clock, GstClockTime time)
90 91 92 93
{
  struct timeval tfnow;
  GstClockTime now;

Wim Taymans's avatar
Wim Taymans committed
94 95
  gettimeofday (&tfnow, (struct timezone *)NULL);
  g_mutex_lock (clock->lock);
96
  now = ((guint64)tfnow.tv_sec*1000000LL+tfnow.tv_usec) - (guint64)clock->start_time; 
Wim Taymans's avatar
Wim Taymans committed
97
  g_mutex_unlock (clock->lock);
98

Wim Taymans's avatar
Wim Taymans committed
99
  return GST_CLOCK_DIFF (time, now);
Wim Taymans's avatar
Wim Taymans committed
100 101
}

Wim Taymans's avatar
Wim Taymans committed
102 103 104
void 
gst_clock_reset (GstClock *clock) 
{
Wim Taymans's avatar
Wim Taymans committed
105 106
  struct timeval tfnow;

Wim Taymans's avatar
Wim Taymans committed
107 108
  gettimeofday (&tfnow, (struct timezone *)NULL);
  g_mutex_lock (clock->lock);
109
  clock->start_time = ((guint64)tfnow.tv_sec)*1000000LL+tfnow.tv_usec;
Wim Taymans's avatar
Wim Taymans committed
110 111 112
  clock->current_time = clock->start_time;
  clock->adjust = 0LL;
  DEBUG("gst_clock: setting start clock %llu\n", clock->start_time);
Wim Taymans's avatar
Wim Taymans committed
113
  g_mutex_unlock (clock->lock);
Wim Taymans's avatar
Wim Taymans committed
114 115
}

Wim Taymans's avatar
Wim Taymans committed
116 117 118
void 
gst_clock_wait (GstClock *clock, GstClockTime time, GstObject *obj) 
{
Wim Taymans's avatar
Wim Taymans committed
119
  struct timeval tfnow;
120
  GstClockTime now;
Wim Taymans's avatar
Wim Taymans committed
121 122 123
  GstClockTimeDiff diff;


Wim Taymans's avatar
Wim Taymans committed
124 125
  gettimeofday (&tfnow, (struct timezone *)NULL);
  g_mutex_lock (clock->lock);
126
  now = tfnow.tv_sec*1000000LL+tfnow.tv_usec - clock->start_time;
Wim Taymans's avatar
Wim Taymans committed
127

Wim Taymans's avatar
Wim Taymans committed
128
  diff = GST_CLOCK_DIFF (time, now);
Wim Taymans's avatar
Wim Taymans committed
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);
Wim Taymans's avatar
Wim Taymans committed
131
   
Wim Taymans's avatar
Wim Taymans committed
132
  g_mutex_unlock (clock->lock);
133 134
  if (diff > 10000 ) {
    tfnow.tv_usec = (diff % 1000000);
Wim Taymans's avatar
Wim Taymans committed
135 136
    tfnow.tv_sec = diff / 1000000;
    // FIXME, this piece of code does not work with egcs optimisations on, had to use the following line
137 138 139
    if (!tfnow.tv_sec) {
      select(0, NULL, NULL, NULL, &tfnow);
    }
Wim Taymans's avatar
Wim Taymans committed
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);
Wim Taymans's avatar
Wim Taymans committed
142
  }
143
  DEBUG("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
144
}