gstclock.c 9.86 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
/* #define GST_DEBUG_ENABLED */
26 27
#include "gst_private.h"

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

30
#define CLASS(clock)  GST_CLOCK_CLASS (G_OBJECT_GET_CLASS (clock))
31

32 33 34
static GMemChunk *_gst_clock_entries_chunk;
static GMutex *_gst_clock_entries_chunk_lock;
static GList *_gst_clock_entries_pool;
Wim Taymans's avatar
Wim Taymans committed
35

36 37 38
static void		gst_clock_class_init		(GstClockClass *klass);
static void		gst_clock_init			(GstClock *clock);

39

40 41 42
static GstObjectClass *parent_class = NULL;
/* static guint gst_clock_signals[LAST_SIGNAL] = { 0 }; */

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
typedef struct _GstClockEntry GstClockEntry;

static void 		gst_clock_free_entry 		(GstClock *clock, GstClockEntry *entry);

typedef enum {
  GST_ENTRY_OK,
  GST_ENTRY_RESTART,
} GstEntryStatus;

struct _GstClockEntry {
  GstClockTime 		 time;
  GstEntryStatus 	 status;
  GstClockCallback 	 func;
  gpointer		 user_data;
  GMutex 		*lock;
  GCond 		*cond;
};

#define GST_CLOCK_ENTRY(entry)          ((GstClockEntry *)(entry))
#define GST_CLOCK_ENTRY_TIME(entry)     (((GstClockEntry *)(entry))->time)
#define GST_CLOCK_ENTRY_LOCK(entry)     (g_mutex_lock ((entry)->lock))
#define GST_CLOCK_ENTRY_UNLOCK(entry)   (g_mutex_unlock ((entry)->lock))
#define GST_CLOCK_ENTRY_SIGNAL(entry)   (g_cond_signal ((entry)->cond))
#define GST_CLOCK_ENTRY_WAIT(entry)     (g_cond_wait (entry->cond, entry->lock))
#define GST_CLOCK_ENTRY_TIMED_WAIT(entry, time)         (g_cond_timed_wait (entry->cond, entry->lock, (time)))

static GstClockEntry*
gst_clock_entry_new (GstClockTime time,
		     GstClockCallback func, gpointer user_data)
{
  GstClockEntry *entry;

  g_mutex_lock (_gst_clock_entries_chunk_lock);
  if (_gst_clock_entries_pool) {
    entry = GST_CLOCK_ENTRY (_gst_clock_entries_pool->data);

    _gst_clock_entries_pool = g_list_remove (_gst_clock_entries_pool, entry);
    g_mutex_unlock (_gst_clock_entries_chunk_lock);
  }
  else {
    entry = g_mem_chunk_alloc (_gst_clock_entries_chunk);
    g_mutex_unlock (_gst_clock_entries_chunk_lock);

    entry->lock = g_mutex_new ();
    entry->cond = g_cond_new ();
  }
  entry->time = time;
  entry->func = func;
  entry->user_data = user_data;

  return entry;
}

static gint
clock_compare_func (gconstpointer a,
                    gconstpointer b)
{
  GstClockEntry *entry1 = (GstClockEntry *)a;
  GstClockEntry *entry2 = (GstClockEntry *)b;

  return (entry1->time - entry2->time);
}

106 107
GType
gst_clock_get_type (void)
Wim Taymans's avatar
Wim Taymans committed
108
{
109 110 111 112
  static GType clock_type = 0;

  if (!clock_type) {
    static const GTypeInfo clock_info = {
113
      sizeof (GstClockClass),
114 115 116 117 118
      NULL,
      NULL,
      (GClassInitFunc) gst_clock_class_init,
      NULL,
      NULL,
119
      sizeof (GstClock),
120 121 122 123 124
      4,
      (GInstanceInitFunc) gst_clock_init,
      NULL
    };
    clock_type = g_type_register_static (GST_TYPE_OBJECT, "GstClock", 
125
		    			 &clock_info,  G_TYPE_FLAG_ABSTRACT);
126 127 128
  }
  return clock_type;
}
129

130 131 132 133 134
static void
gst_clock_class_init (GstClockClass *klass)
{
  GObjectClass *gobject_class;
  GstObjectClass *gstobject_class;
135

136 137
  gobject_class = (GObjectClass*) klass;
  gstobject_class = (GstObjectClass*) klass;
138

139
  parent_class = g_type_class_ref (GST_TYPE_OBJECT);
140 141 142 143 144 145

  _gst_clock_entries_chunk = g_mem_chunk_new ("GstClockEntries",
                     sizeof (GstClockEntry), sizeof (GstClockEntry) * 32,
                     G_ALLOC_AND_FREE);
  _gst_clock_entries_chunk_lock = g_mutex_new ();
  _gst_clock_entries_pool = NULL;
Wim Taymans's avatar
Wim Taymans committed
146 147
}

148 149
static void
gst_clock_init (GstClock *clock)
Wim Taymans's avatar
Wim Taymans committed
150
{
151 152 153
  clock->speed = 1.0;
  clock->active = FALSE;
  clock->start_time = 0;
154 155 156
  clock->last_time = 0;
  clock->entries = NULL;
  clock->async_supported = FALSE;
157 158 159

  clock->active_mutex = g_mutex_new ();
  clock->active_cond = g_cond_new ();
Wim Taymans's avatar
Wim Taymans committed
160 161
}

162 163 164 165 166 167 168 169 170
gboolean
gst_clock_async_supported (GstClock *clock)
{
  g_return_val_if_fail (GST_IS_CLOCK (clock), FALSE);

  return clock->async_supported;
}


171
void
172
gst_clock_reset (GstClock *clock)
Wim Taymans's avatar
Wim Taymans committed
173
{
174 175
  GstClockTime time = 0LL;

176 177
  g_return_if_fail (GST_IS_CLOCK (clock));

178 179 180
  if (CLASS (clock)->get_internal_time) {
    time = CLASS (clock)->get_internal_time (clock);
  }
181

182 183 184 185 186
  GST_LOCK (clock);
  clock->active = FALSE;
  clock->start_time = time;
  clock->last_time = 0LL;
  GST_UNLOCK (clock);
Wim Taymans's avatar
Wim Taymans committed
187 188
}

189
void
190
gst_clock_activate (GstClock *clock, gboolean active)
Wim Taymans's avatar
Wim Taymans committed
191
{
192 193
  GstClockTime time = 0LL;

194 195 196
  g_return_if_fail (GST_IS_CLOCK (clock));

  clock->active = active;
197 198 199 200
	        
  if (CLASS (clock)->get_internal_time) {
    time = CLASS (clock)->get_internal_time (clock);
  }
201

202 203 204 205 206 207 208 209
  GST_LOCK (clock);
  if (active) {
    clock->start_time = time - clock->last_time;;
  }
  else {
    clock->last_time = time - clock->start_time;
  }
  GST_UNLOCK (clock);
210 211

  g_mutex_lock (clock->active_mutex);	
212
  g_cond_broadcast (clock->active_cond);	
213
  g_mutex_unlock (clock->active_mutex);	
214 215
}

216 217
gboolean
gst_clock_is_active (GstClock *clock)
218
{
219
  g_return_val_if_fail (GST_IS_CLOCK (clock), FALSE);
220

221
  return clock->active;
Wim Taymans's avatar
Wim Taymans committed
222 223
}

224 225 226
GstClockTime
gst_clock_get_time (GstClock *clock)
{
227 228
  GstClockTime ret = 0LL;

229 230
  g_return_val_if_fail (GST_IS_CLOCK (clock), 0LL);

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
  if (!clock->active) {
    /* clock is not activen return previous time */
    ret = clock->last_time;
  }
  else {
    if (CLASS (clock)->get_internal_time) {
      ret = CLASS (clock)->get_internal_time (clock) - clock->start_time;
    }
    /* make sure the time is increasing, else return last_time */
    if (ret < clock->last_time) {
      ret = clock->last_time;
    }
    else {
      clock->last_time = ret;
    }
  }
247

248
  return ret;
249 250
}

251 252 253
static GstClockID
gst_clock_wait_async_func (GstClock *clock, GstClockTime time,
		           GstClockCallback func, gpointer user_data)
Wim Taymans's avatar
Wim Taymans committed
254
{
255 256
  GstClockEntry *entry = NULL;
  g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);
257 258 259 260 261

  if (!clock->active) {
    g_mutex_lock (clock->active_mutex);	
    g_cond_wait (clock->active_cond, clock->active_mutex);	
    g_mutex_unlock (clock->active_mutex);	
Wim Taymans's avatar
Wim Taymans committed
262
  }
263

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
  entry = gst_clock_entry_new (time, func, user_data);

  GST_LOCK (clock);
  clock->entries = g_list_insert_sorted (clock->entries, entry, clock_compare_func);
  GST_UNLOCK (clock);

  return entry;
}

GstClockReturn
gst_clock_wait (GstClock *clock, GstClockTime time)
{
  GstClockID id;
  GstClockReturn res;
  
  g_return_val_if_fail (GST_IS_CLOCK (clock), GST_CLOCK_STOPPED);

  id = gst_clock_wait_async_func (clock, time, NULL, NULL);
  res = gst_clock_wait_id (clock, id);

  return res;
}

GstClockID
gst_clock_wait_async (GstClock *clock, GstClockTime time,
		      GstClockCallback func, gpointer user_data)
{
  g_return_val_if_fail (GST_IS_CLOCK (clock), NULL);

  if (clock->async_supported) {
    return gst_clock_wait_async_func (clock, time, func, user_data);
  }
  return NULL;
}

static void
gst_clock_unlock_func (GstClock *clock, GstClockTime time, GstClockID id, gpointer user_data)
{
  GstClockEntry *entry = (GstClockEntry *) id;

  GST_CLOCK_ENTRY_LOCK (entry);
  GST_CLOCK_ENTRY_SIGNAL (entry);
  GST_CLOCK_ENTRY_UNLOCK (entry);
}

GstClockReturn
gst_clock_wait_id (GstClock *clock, GstClockID id)
{
  GstClockReturn res = GST_CLOCK_TIMEOUT;
  GstClockEntry *entry = (GstClockEntry *) id;
  GstClockTime current_real, current, target;
  GTimeVal timeval;
  
  g_return_val_if_fail (GST_IS_CLOCK (clock), GST_CLOCK_ERROR);
  g_return_val_if_fail (entry, GST_CLOCK_ERROR);

  current = gst_clock_get_time (clock);

  g_get_current_time (&timeval);
  current_real = GST_TIMEVAL_TO_TIME (timeval);

  GST_CLOCK_ENTRY_LOCK (entry);
  entry->func = gst_clock_unlock_func;
  target = GST_CLOCK_ENTRY_TIME (entry) - current + current_real;

  //g_print ("%lld %lld %lld\n", target, current, current_real);
  
  if (target > current_real) {
    timeval.tv_usec = target % 1000000;
    timeval.tv_sec = target / 1000000;

    GST_CLOCK_ENTRY_TIMED_WAIT (entry, &timeval);
  }
  GST_CLOCK_ENTRY_UNLOCK (entry);

  gst_clock_free_entry (clock, entry);

  return res;
}

GstClockID
gst_clock_get_next_id (GstClock *clock)
{
  GstClockEntry *entry = NULL;

  GST_LOCK (clock);
  if (clock->entries)
    entry = GST_CLOCK_ENTRY (clock->entries->data);
  GST_UNLOCK (clock);

  return (GstClockID *) entry;
}

GstClockTime
gst_clock_id_get_time (GstClockID id)
{
  return GST_CLOCK_ENTRY_TIME (id);
}

static void
gst_clock_free_entry (GstClock *clock, GstClockEntry *entry)
{
  GST_LOCK (clock);
  clock->entries = g_list_remove (clock->entries, entry);
  GST_UNLOCK (clock);

  g_mutex_lock (_gst_clock_entries_chunk_lock);
  _gst_clock_entries_pool = g_list_prepend (_gst_clock_entries_pool, entry);
  g_mutex_unlock (_gst_clock_entries_chunk_lock);
}

void
gst_clock_unlock_id (GstClock *clock, GstClockID id)
{
  GstClockEntry *entry = (GstClockEntry *) id;

  if (entry->func)
    entry->func (clock, gst_clock_get_time (clock), id, entry->user_data);

  gst_clock_free_entry (clock, entry);
384 385 386 387 388 389 390 391 392
}

void
gst_clock_set_resolution (GstClock *clock, guint64 resolution)
{
  g_return_if_fail (GST_IS_CLOCK (clock));

  if (CLASS (clock)->set_resolution)
    CLASS (clock)->set_resolution (clock, resolution);
Wim Taymans's avatar
Wim Taymans committed
393
}
394 395 396 397 398 399 400 401 402

guint64
gst_clock_get_resolution (GstClock *clock)
{
  g_return_val_if_fail (GST_IS_CLOCK (clock), 0LL);

  if (CLASS (clock)->get_resolution)
    return CLASS (clock)->get_resolution (clock);

403
  return 1LL;
404 405
}