gstclock.c 4.07 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

Wim Taymans's avatar
Wim Taymans committed
32

33 34 35 36 37 38 39 40
static void		gst_clock_class_init		(GstClockClass *klass);
static void		gst_clock_init			(GstClock *clock);

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

GType
gst_clock_get_type (void)
Wim Taymans's avatar
Wim Taymans committed
41
{
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  static GType clock_type = 0;

  if (!clock_type) {
    static const GTypeInfo clock_info = {
      sizeof (GstObjectClass),
      NULL,
      NULL,
      (GClassInitFunc) gst_clock_class_init,
      NULL,
      NULL,
      sizeof (GstObject),
      4,
      (GInstanceInitFunc) gst_clock_init,
      NULL
    };
    clock_type = g_type_register_static (GST_TYPE_OBJECT, "GstClock", 
		    			 &clock_info, G_TYPE_FLAG_ABSTRACT);
  }
  return clock_type;
}
62

63 64 65 66 67
static void
gst_clock_class_init (GstClockClass *klass)
{
  GObjectClass *gobject_class;
  GstObjectClass *gstobject_class;
68

69 70
  gobject_class = (GObjectClass*) klass;
  gstobject_class = (GstObjectClass*) klass;
71

72
  parent_class = g_type_class_ref (GST_TYPE_OBJECT);
Wim Taymans's avatar
Wim Taymans committed
73 74
}

75 76
static void
gst_clock_init (GstClock *clock)
Wim Taymans's avatar
Wim Taymans committed
77
{
78 79 80 81 82 83
  clock->speed = 1.0;
  clock->active = FALSE;
  clock->start_time = 0;

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

86
void
87
gst_clock_reset (GstClock *clock)
Wim Taymans's avatar
Wim Taymans committed
88
{
89 90 91 92 93 94 95
  g_return_if_fail (GST_IS_CLOCK (clock));

  clock->start_time = 0;
  clock->active = FALSE;

  if (CLASS (clock)->reset)
    CLASS (clock)->reset (clock);
Wim Taymans's avatar
Wim Taymans committed
96 97
}

98
void
99
gst_clock_activate (GstClock *clock, gboolean active)
Wim Taymans's avatar
Wim Taymans committed
100
{
101 102 103 104 105 106 107 108 109 110 111
  g_return_if_fail (GST_IS_CLOCK (clock));

  clock->active = active;

  if (CLASS (clock)->activate)
    CLASS (clock)->activate (clock, active);

  g_mutex_lock (clock->active_mutex);	
  g_cond_signal (clock->active_cond);	
  g_mutex_unlock (clock->active_mutex);	

112 113
}

114 115
gboolean
gst_clock_is_active (GstClock *clock)
116
{
117
  g_return_val_if_fail (GST_IS_CLOCK (clock), FALSE);
118

119
  return clock->active;
Wim Taymans's avatar
Wim Taymans committed
120 121
}

122
void
123
gst_clock_set_time (GstClock *clock, GstClockTime time)
Wim Taymans's avatar
Wim Taymans committed
124
{
125 126 127 128
  g_return_if_fail (GST_IS_CLOCK (clock));

  if (CLASS (clock)->set_time)
    CLASS (clock)->set_time (clock, time);
Wim Taymans's avatar
Wim Taymans committed
129 130
}

131 132 133 134 135 136 137 138 139 140 141 142 143
GstClockTime
gst_clock_get_time (GstClock *clock)
{
  g_return_val_if_fail (GST_IS_CLOCK (clock), 0LL);

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

  return 0LL;
}

GstClockReturn
gst_clock_wait (GstClock *clock, GstClockTime time)
Wim Taymans's avatar
Wim Taymans committed
144
{
145 146 147 148 149 150
  g_return_val_if_fail (GST_IS_CLOCK (clock), GST_CLOCK_STOPPED);

  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
151
  }
152 153 154 155 156 157 158 159 160 161 162 163 164
  if (CLASS (clock)->wait)
    return CLASS (clock)->wait (clock, time);

  return GST_CLOCK_TIMEOUT;
}

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
165
}
166 167 168 169 170 171 172 173 174 175 176 177

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);

  return 0LL;
}