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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, 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
gst_1394_clock_get_type(void)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
gst_1394_clock_class_init(Gst1394ClockClass * klass)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
gst_1394_clock_init(Gst1394Clock * clock)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 *
gst_1394_clock_new(const gchar * name)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,
101           "clock-type", GST_CLOCK_TYPE_OTHER, NULL));
102 
103   /* Clear floating flag */
104   gst_object_ref_sink (_1394clock);
105 
106   return _1394clock;
107 }
108 
109 static GstClockTime
gst_1394_clock_get_internal_time(GstClock * clock)110 gst_1394_clock_get_internal_time (GstClock * clock)
111 {
112   Gst1394Clock *_1394clock;
113   GstClockTime result;
114   guint32 cycle_timer;
115   guint64 local_time;
116 
117   _1394clock = GST_1394_CLOCK_CAST (clock);
118 
119   if (_1394clock->handle != NULL) {
120     GST_OBJECT_LOCK (clock);
121     raw1394_read_cycle_timer (_1394clock->handle, &cycle_timer, &local_time);
122 
123     if (cycle_timer < _1394clock->cycle_timer_lo) {
124       GST_LOG_OBJECT (clock, "overflow %u to %u",
125           _1394clock->cycle_timer_lo, cycle_timer);
126 
127       _1394clock->cycle_timer_hi++;
128     }
129     _1394clock->cycle_timer_lo = cycle_timer;
130 
131     /* get the seconds from the cycleSeconds counter */
132     result = (((((guint64) _1394clock->cycle_timer_hi) << 32) |
133             cycle_timer) >> 25) * GST_SECOND;
134     /* add the microseconds from the cycleCount counter */
135     result += (((cycle_timer >> 12) & 0x1fff) * 125) * GST_USECOND;
136 
137     GST_LOG_OBJECT (clock, "result %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
138     GST_OBJECT_UNLOCK (clock);
139   } else {
140     result = GST_CLOCK_TIME_NONE;
141   }
142 
143   return result;
144 }
145 
146 void
gst_1394_clock_set_handle(Gst1394Clock * clock,raw1394handle_t handle)147 gst_1394_clock_set_handle (Gst1394Clock * clock, raw1394handle_t handle)
148 {
149   clock->handle = handle;
150   clock->cycle_timer_lo = 0;
151   clock->cycle_timer_hi = 0;
152 }
153 
154 void
gst_1394_clock_unset_handle(Gst1394Clock * clock)155 gst_1394_clock_unset_handle (Gst1394Clock * clock)
156 {
157   clock->handle = NULL;
158 }
159