1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
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
|
/*
* Copyright (C) 2008 Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "gstksclock.h"
#include "kshelpers.h"
GST_DEBUG_CATEGORY_EXTERN (gst_ks_debug);
#define GST_CAT_DEFAULT gst_ks_debug
struct _GstKsClockPrivate
{
GMutex *mutex;
GCond *client_cond;
GCond *worker_cond;
HANDLE clock_handle;
gboolean open;
gboolean closing;
KSSTATE state;
GThread *worker_thread;
gboolean worker_running;
gboolean worker_initialized;
GstClock *master_clock;
};
#define GST_KS_CLOCK_GET_PRIVATE(o) ((o)->priv)
#define GST_KS_CLOCK_LOCK() g_mutex_lock (priv->mutex)
#define GST_KS_CLOCK_UNLOCK() g_mutex_unlock (priv->mutex)
static void gst_ks_clock_dispose (GObject * object);
static void gst_ks_clock_finalize (GObject * object);
GST_BOILERPLATE (GstKsClock, gst_ks_clock, GObject, G_TYPE_OBJECT);
static void
gst_ks_clock_base_init (gpointer gclass)
{
}
static void
gst_ks_clock_class_init (GstKsClockClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (GstKsClockPrivate));
gobject_class->dispose = gst_ks_clock_dispose;
gobject_class->finalize = gst_ks_clock_finalize;
}
static void
gst_ks_clock_init (GstKsClock * self, GstKsClockClass * gclass)
{
GstKsClockPrivate *priv;
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_KS_CLOCK,
GstKsClockPrivate);
priv = GST_KS_CLOCK_GET_PRIVATE (self);
priv->mutex = g_mutex_new ();
priv->client_cond = g_cond_new ();
priv->worker_cond = g_cond_new ();
priv->clock_handle = INVALID_HANDLE_VALUE;
priv->open = FALSE;
priv->closing = FALSE;
priv->state = KSSTATE_STOP;
priv->worker_thread = NULL;
priv->worker_running = FALSE;
priv->worker_initialized = FALSE;
priv->master_clock = NULL;
}
static void
gst_ks_clock_dispose (GObject * object)
{
g_assert (!GST_KS_CLOCK_GET_PRIVATE (GST_KS_CLOCK (object))->open);
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static void
gst_ks_clock_finalize (GObject * object)
{
GstKsClock *self = GST_KS_CLOCK (object);
GstKsClockPrivate *priv = GST_KS_CLOCK_GET_PRIVATE (self);
g_cond_free (priv->worker_cond);
g_cond_free (priv->client_cond);
g_mutex_free (priv->mutex);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void gst_ks_clock_close_unlocked (GstKsClock * self);
gboolean
gst_ks_clock_open (GstKsClock * self)
{
GstKsClockPrivate *priv = GST_KS_CLOCK_GET_PRIVATE (self);
GList *devices;
KsDeviceEntry *device;
KSSTATE state;
GST_KS_CLOCK_LOCK ();
g_assert (!priv->open);
priv->state = KSSTATE_STOP;
devices = ks_enumerate_devices (&KSCATEGORY_CLOCK);
if (devices == NULL)
goto error;
device = devices->data;
priv->clock_handle = CreateFile (device->path, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL);
if (!ks_is_valid_handle (priv->clock_handle))
goto error;
state = KSSTATE_STOP;
if (!ks_object_set_property (priv->clock_handle, KSPROPSETID_Clock,
KSPROPERTY_CLOCK_STATE, &state, sizeof (state), NULL))
goto error;
ks_device_list_free (devices);
priv->open = TRUE;
GST_KS_CLOCK_UNLOCK ();
return TRUE;
error:
ks_device_list_free (devices);
gst_ks_clock_close_unlocked (self);
GST_KS_CLOCK_UNLOCK ();
return FALSE;
}
static gboolean
gst_ks_clock_set_state_unlocked (GstKsClock * self, KSSTATE state)
{
GstKsClockPrivate *priv = GST_KS_CLOCK_GET_PRIVATE (self);
KSSTATE initial_state;
gint addend;
g_assert (priv->open);
if (state == priv->state)
return TRUE;
initial_state = priv->state;
addend = (state > priv->state) ? 1 : -1;
GST_DEBUG ("Initiating clock state change from %s to %s",
ks_state_to_string (priv->state), ks_state_to_string (state));
while (priv->state != state) {
KSSTATE next_state = priv->state + addend;
GST_DEBUG ("Changing clock state from %s to %s",
ks_state_to_string (priv->state), ks_state_to_string (next_state));
if (ks_object_set_property (priv->clock_handle, KSPROPSETID_Clock,
KSPROPERTY_CLOCK_STATE, &next_state, sizeof (next_state), NULL)) {
priv->state = next_state;
GST_DEBUG ("Changed clock state to %s", ks_state_to_string (priv->state));
} else {
GST_WARNING ("Failed to change clock state to %s",
ks_state_to_string (next_state));
return FALSE;
}
}
GST_DEBUG ("Finished clock state change from %s to %s",
ks_state_to_string (initial_state), ks_state_to_string (state));
return TRUE;
}
static void
gst_ks_clock_close_unlocked (GstKsClock * self)
{
GstKsClockPrivate *priv = GST_KS_CLOCK_GET_PRIVATE (self);
if (priv->closing)
return;
priv->closing = TRUE;
if (priv->worker_thread != NULL) {
priv->worker_running = FALSE;
g_cond_signal (priv->worker_cond);
GST_KS_CLOCK_UNLOCK ();
g_thread_join (priv->worker_thread);
priv->worker_thread = NULL;
GST_KS_CLOCK_LOCK ();
}
if (priv->open)
gst_ks_clock_set_state_unlocked (self, KSSTATE_STOP);
if (ks_is_valid_handle (priv->clock_handle)) {
CloseHandle (priv->clock_handle);
priv->clock_handle = INVALID_HANDLE_VALUE;
}
if (priv->master_clock != NULL) {
gst_object_unref (priv->master_clock);
priv->master_clock = NULL;
}
priv->open = FALSE;
priv->closing = FALSE;
}
void
gst_ks_clock_close (GstKsClock * self)
{
GstKsClockPrivate *priv = GST_KS_CLOCK_GET_PRIVATE (self);
GST_KS_CLOCK_LOCK ();
gst_ks_clock_close_unlocked (self);
GST_KS_CLOCK_UNLOCK ();
}
HANDLE
gst_ks_clock_get_handle (GstKsClock * self)
{
GstKsClockPrivate *priv = GST_KS_CLOCK_GET_PRIVATE (self);
HANDLE handle;
GST_KS_CLOCK_LOCK ();
g_assert (priv->open);
handle = priv->clock_handle;
GST_KS_CLOCK_UNLOCK ();
return handle;
}
void
gst_ks_clock_prepare (GstKsClock * self)
{
GstKsClockPrivate *priv = GST_KS_CLOCK_GET_PRIVATE (self);
GST_KS_CLOCK_LOCK ();
if (priv->state < KSSTATE_PAUSE)
gst_ks_clock_set_state_unlocked (self, KSSTATE_PAUSE);
GST_KS_CLOCK_UNLOCK ();
}
static gpointer
gst_ks_clock_worker_thread_func (gpointer data)
{
GstKsClock *self = GST_KS_CLOCK (data);
GstKsClockPrivate *priv = GST_KS_CLOCK_GET_PRIVATE (self);
GST_KS_CLOCK_LOCK ();
gst_ks_clock_set_state_unlocked (self, KSSTATE_RUN);
while (priv->worker_running) {
if (priv->master_clock != NULL) {
GstClockTime now = gst_clock_get_time (priv->master_clock);
now /= 100;
if (ks_object_set_property (priv->clock_handle, KSPROPSETID_Clock,
KSPROPERTY_CLOCK_TIME, &now, sizeof (now), NULL)) {
GST_DEBUG ("clock synchronized");
gst_object_unref (priv->master_clock);
priv->master_clock = NULL;
} else {
GST_WARNING ("failed to synchronize clock");
}
}
if (!priv->worker_initialized) {
priv->worker_initialized = TRUE;
g_cond_signal (priv->client_cond);
}
g_cond_wait (priv->worker_cond, priv->mutex);
}
priv->worker_initialized = FALSE;
GST_KS_CLOCK_UNLOCK ();
return NULL;
}
void
gst_ks_clock_start (GstKsClock * self)
{
GstKsClockPrivate *priv = GST_KS_CLOCK_GET_PRIVATE (self);
GST_KS_CLOCK_LOCK ();
if (priv->worker_thread == NULL) {
priv->worker_running = TRUE;
priv->worker_initialized = FALSE;
priv->worker_thread =
g_thread_create (gst_ks_clock_worker_thread_func, self, TRUE, NULL);
}
while (!priv->worker_initialized)
g_cond_wait (priv->client_cond, priv->mutex);
GST_KS_CLOCK_UNLOCK ();
}
void
gst_ks_clock_provide_master_clock (GstKsClock * self, GstClock * master_clock)
{
GstKsClockPrivate *priv = GST_KS_CLOCK_GET_PRIVATE (self);
GST_KS_CLOCK_LOCK ();
gst_object_ref (master_clock);
if (priv->master_clock != NULL)
gst_object_unref (priv->master_clock);
priv->master_clock = master_clock;
g_cond_signal (priv->worker_cond);
GST_KS_CLOCK_UNLOCK ();
}
|