tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.git] / ext / raw1394 / gst1394clock.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  * Copyright (C) 2009      David Schleef <ds@schleef.org>
5  *
6  * gst1394clock.c: Clock for use by IEEE 1394 plugins
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include "gst1394clock.h"
29
30 GST_DEBUG_CATEGORY_STATIC (gst_1394_clock_debug);
31 #define GST_CAT_DEFAULT gst_1394_clock_debug
32
33 static void gst_1394_clock_class_init (Gst1394ClockClass * klass);
34 static void gst_1394_clock_init (Gst1394Clock * clock);
35
36 static GstClockTime gst_1394_clock_get_internal_time (GstClock * clock);
37
38 static GstSystemClockClass *parent_class = NULL;
39
40 /* static guint gst_1394_clock_signals[LAST_SIGNAL] = { 0 }; */
41
42 GType
43 gst_1394_clock_get_type (void)
44 {
45   static GType clock_type = 0;
46
47   if (!clock_type) {
48     static const GTypeInfo clock_info = {
49       sizeof (Gst1394ClockClass),
50       NULL,
51       NULL,
52       (GClassInitFunc) gst_1394_clock_class_init,
53       NULL,
54       NULL,
55       sizeof (Gst1394Clock),
56       4,
57       (GInstanceInitFunc) gst_1394_clock_init,
58       NULL
59     };
60
61     clock_type = g_type_register_static (GST_TYPE_SYSTEM_CLOCK, "Gst1394Clock",
62         &clock_info, 0);
63   }
64   return clock_type;
65 }
66
67
68 static void
69 gst_1394_clock_class_init (Gst1394ClockClass * klass)
70 {
71   GstClockClass *gstclock_class;
72
73   gstclock_class = (GstClockClass *) klass;
74
75   parent_class = g_type_class_peek_parent (klass);
76
77   gstclock_class->get_internal_time = gst_1394_clock_get_internal_time;
78
79   GST_DEBUG_CATEGORY_INIT (gst_1394_clock_debug, "1394clock", 0, "1394clock");
80 }
81
82 static void
83 gst_1394_clock_init (Gst1394Clock * clock)
84 {
85   GST_OBJECT_FLAG_SET (clock, GST_CLOCK_FLAG_CAN_SET_MASTER);
86 }
87
88 /**
89  * gst_1394_clock_new:
90  * @name: the name of the clock
91  *
92  * Create a new #Gst1394Clock instance.
93  *
94  * Returns: a new #Gst1394Clock
95  */
96 Gst1394Clock *
97 gst_1394_clock_new (const gchar * name)
98 {
99   Gst1394Clock *_1394clock =
100       GST_1394_CLOCK (g_object_new (GST_TYPE_1394_CLOCK, "name", name, NULL));
101
102   return _1394clock;
103 }
104
105 static GstClockTime
106 gst_1394_clock_get_internal_time (GstClock * clock)
107 {
108   Gst1394Clock *_1394clock;
109   GstClockTime result;
110   guint32 cycle_timer;
111   guint64 local_time;
112
113   _1394clock = GST_1394_CLOCK_CAST (clock);
114
115   if (_1394clock->handle != NULL) {
116     GST_OBJECT_LOCK (clock);
117     raw1394_read_cycle_timer (_1394clock->handle, &cycle_timer, &local_time);
118
119     if (cycle_timer < _1394clock->cycle_timer_lo) {
120       GST_LOG_OBJECT (clock, "overflow %u to %u",
121           _1394clock->cycle_timer_lo, cycle_timer);
122
123       _1394clock->cycle_timer_hi++;
124     }
125     _1394clock->cycle_timer_lo = cycle_timer;
126
127     /* get the seconds from the cycleSeconds counter */
128     result = (((((guint64) _1394clock->cycle_timer_hi) << 32) |
129             cycle_timer) >> 25) * GST_SECOND;
130     /* add the microseconds from the cycleCount counter */
131     result += (((cycle_timer >> 12) & 0x1fff) * 125) * GST_USECOND;
132
133     GST_LOG_OBJECT (clock, "result %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
134     GST_OBJECT_UNLOCK (clock);
135   } else {
136     result = GST_CLOCK_TIME_NONE;
137   }
138
139   return result;
140 }
141
142 void
143 gst_1394_clock_set_handle (Gst1394Clock * clock, raw1394handle_t handle)
144 {
145   clock->handle = handle;
146   clock->cycle_timer_lo = 0;
147   clock->cycle_timer_hi = 0;
148 }
149
150 void
151 gst_1394_clock_unset_handle (Gst1394Clock * clock)
152 {
153   clock->handle = NULL;
154 }