Debian Codegraph @

libs/gst/net/gstnettimeprovider.c

raw
1 /* GStreamer
2 * Copyright (C) 2005 Andy Wingo <[email protected]>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19 /**
20 * SECTION:gstnettimeprovider
21 * @title: GstNetTimeProvider
22 * @short_description: Special object that exposed the time of a clock
23 * on the network.
24 * @see_also: #GstClock, #GstNetClientClock, #GstPipeline
25 *
26 * This object exposes the time of a #GstClock on the network.
27 *
28 * A #GstNetTimeProvider is created with gst_net_time_provider_new() which
29 * takes a #GstClock, an address and a port number as arguments.
30 *
31 * After creating the object, a client clock such as #GstNetClientClock can
32 * query the exposed clock over the network for its values.
33 *
34 * The #GstNetTimeProvider typically wraps the clock used by a #GstPipeline.
35 */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include "gstnettimeprovider.h"
42 #include "gstnettimepacket.h"
43 #include "gstnetutils.h"
44
45 GST_DEBUG_CATEGORY_STATIC (ntp_debug);
46 #define GST_CAT_DEFAULT (ntp_debug)
47
48 #define DEFAULT_ADDRESS "0.0.0.0"
49 #define DEFAULT_PORT 5637
50 #define DEFAULT_QOS_DSCP -1
51
52 #define IS_ACTIVE(self) (g_atomic_int_get (&((self)->priv->active)))
53
54 enum
55 {
56 PROP_0,
57 PROP_PORT,
58 PROP_ADDRESS,
59 PROP_CLOCK,
60 PROP_ACTIVE,
61 PROP_QOS_DSCP
62 };
63
64 struct _GstNetTimeProviderPrivate
65 {
66 gchar *address;
67 int port;
68 gint qos_dscp; /* ATOMIC */
69
70 GThread *thread;
71
72 GstClock *clock;
73
74 gboolean active; /* ATOMIC */
75
76 GSocket *socket;
77 GCancellable *cancel;
78 gboolean made_cancel_fd;
79 };
80
81 static void gst_net_time_provider_initable_iface_init (gpointer g_iface);
82
83 static gboolean gst_net_time_provider_start (GstNetTimeProvider * bself,
84 GError ** error);
85 static void gst_net_time_provider_stop (GstNetTimeProvider * bself);
86
87 static gpointer gst_net_time_provider_thread (gpointer data);
88
89 static void gst_net_time_provider_finalize (GObject * object);
90 static void gst_net_time_provider_set_property (GObject * object, guint prop_id,
91 const GValue * value, GParamSpec * pspec);
92 static void gst_net_time_provider_get_property (GObject * object, guint prop_id,
93 GValue * value, GParamSpec * pspec);
94
95 #define _do_init \
96 GST_DEBUG_CATEGORY_INIT (ntp_debug, "nettime", 0, "Network time provider"); \
97 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, gst_net_time_provider_initable_iface_init)
98
99 #define gst_net_time_provider_parent_class parent_class
100 G_DEFINE_TYPE_WITH_CODE (GstNetTimeProvider, gst_net_time_provider,
101 GST_TYPE_OBJECT, G_ADD_PRIVATE (GstNetTimeProvider) _do_init);
102
103 static void
104 gst_net_time_provider_class_init (GstNetTimeProviderClass * klass)
105 {
106 GObjectClass *gobject_class;
107
108 gobject_class = G_OBJECT_CLASS (klass);
109
110 g_assert (sizeof (GstClockTime) == 8);
111
112 gobject_class->finalize = gst_net_time_provider_finalize;
113 gobject_class->set_property = gst_net_time_provider_set_property;
114 gobject_class->get_property = gst_net_time_provider_get_property;
115
116 g_object_class_install_property (gobject_class, PROP_PORT,
117 g_param_spec_int ("port", "port",
118 "The port to receive the packets from, 0=allocate", 0, G_MAXUINT16,
119 DEFAULT_PORT,
120 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
121 g_object_class_install_property (gobject_class, PROP_ADDRESS,
122 g_param_spec_string ("address", "address",
123 "The address to bind on, as a dotted quad (x.x.x.x)", DEFAULT_ADDRESS,
124 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
125 g_object_class_install_property (gobject_class, PROP_CLOCK,
126 g_param_spec_object ("clock", "Clock",
127 "The clock to export over the network", GST_TYPE_CLOCK,
128 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
129 g_object_class_install_property (gobject_class, PROP_ACTIVE,
130 g_param_spec_boolean ("active", "Active",
131 "TRUE if the clock will respond to queries over the network", TRUE,
132 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
133 g_object_class_install_property (gobject_class, PROP_QOS_DSCP,
134 g_param_spec_int ("qos-dscp", "QoS diff srv code point",
135 "Quality of Service, differentiated services code point (-1 default)",
136 -1, 63, DEFAULT_QOS_DSCP,
137 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
138 }
139
140 static void
141 gst_net_time_provider_init (GstNetTimeProvider * self)
142 {
143 self->priv = gst_net_time_provider_get_instance_private (self);
144
145 self->priv->port = DEFAULT_PORT;
146 self->priv->address = g_strdup (DEFAULT_ADDRESS);
147 self->priv->qos_dscp = DEFAULT_QOS_DSCP;
148 self->priv->thread = NULL;
149 self->priv->active = TRUE;
150 }
151
152 static void
153 gst_net_time_provider_finalize (GObject * object)
154 {
155 GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object);
156
157 if (self->priv->thread) {
158 gst_net_time_provider_stop (self);
159 g_assert (self->priv->thread == NULL);
160 }
161
162 g_free (self->priv->address);
163 self->priv->address = NULL;
164
165 if (self->priv->clock)
166 gst_object_unref (self->priv->clock);
167 self->priv->clock = NULL;
168
169 G_OBJECT_CLASS (parent_class)->finalize (object);
170 }
171
172 static gpointer
173 gst_net_time_provider_thread (gpointer data)
174 {
175 GstNetTimeProvider *self = data;
176 GCancellable *cancel = self->priv->cancel;
177 GSocket *socket = self->priv->socket;
178 GstNetTimePacket *packet;
179 GError *err = NULL;
180 gint cur_qos_dscp = DEFAULT_QOS_DSCP;
181 gint new_qos_dscp;
182
183 GST_INFO_OBJECT (self, "time provider thread is running");
184
185 while (TRUE) {
186 GSocketAddress *sender_addr = NULL;
187
188
189 GST_LOG_OBJECT (self, "waiting on socket");
190 if (!g_socket_condition_wait (socket, G_IO_IN, cancel, &err)) {
191 GST_INFO_OBJECT (self, "socket error: %s", err->message);
192
193 if (err->code == G_IO_ERROR_CANCELLED)
194 break;
195
196 /* try again */
197 g_usleep (G_USEC_PER_SEC / 10);
198 g_error_free (err);
199 err = NULL;
200 continue;
201 }
202
203 /* got data in */
204 packet = gst_net_time_packet_receive (socket, &sender_addr, &err);
205
206 if (err != NULL) {
207 GST_DEBUG_OBJECT (self, "receive error: %s", err->message);
208 g_usleep (G_USEC_PER_SEC / 10);
209 g_error_free (err);
210 err = NULL;
211 continue;
212 }
213
214 /* before next sending check if need to change QoS */
215 new_qos_dscp = self->priv->qos_dscp;
216 if (cur_qos_dscp != new_qos_dscp &&
217 gst_net_utils_set_socket_tos (socket, new_qos_dscp)) {
218 GST_DEBUG_OBJECT (self, "changed QoS DSCP to: %d", new_qos_dscp);
219 cur_qos_dscp = new_qos_dscp;
220 }
221
222 if (IS_ACTIVE (self)) {
223 /* do what we were asked to and send the packet back */
224 packet->remote_time = gst_clock_get_time (self->priv->clock);
225
226 /* ignore errors */
227 gst_net_time_packet_send (packet, socket, sender_addr, NULL);
228 g_object_unref (sender_addr);
229 g_free (packet);
230 }
231 }
232
233 g_error_free (err);
234
235 GST_INFO_OBJECT (self, "time provider thread is stopping");
236 return NULL;
237 }
238
239 static void
240 gst_net_time_provider_set_property (GObject * object, guint prop_id,
241 const GValue * value, GParamSpec * pspec)
242 {
243 GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object);
244 GstClock **clock_p = &self->priv->clock;
245
246 switch (prop_id) {
247 case PROP_PORT:
248 self->priv->port = g_value_get_int (value);
249 break;
250 case PROP_ADDRESS:
251 g_free (self->priv->address);
252 if (g_value_get_string (value) == NULL)
253 self->priv->address = g_strdup (DEFAULT_ADDRESS);
254 else
255 self->priv->address = g_value_dup_string (value);
256 break;
257 case PROP_CLOCK:
258 gst_object_replace ((GstObject **) clock_p,
259 (GstObject *) g_value_get_object (value));
260 break;
261 case PROP_ACTIVE:
262 g_atomic_int_set (&self->priv->active, g_value_get_boolean (value));
263 break;
264 case PROP_QOS_DSCP:
265 g_atomic_int_set (&self->priv->qos_dscp, g_value_get_int (value));
266 break;
267 default:
268 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
269 break;
270 }
271 }
272
273 static void
274 gst_net_time_provider_get_property (GObject * object, guint prop_id,
275 GValue * value, GParamSpec * pspec)
276 {
277 GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object);
278
279 switch (prop_id) {
280 case PROP_PORT:
281 g_value_set_int (value, self->priv->port);
282 break;
283 case PROP_ADDRESS:
284 g_value_set_string (value, self->priv->address);
285 break;
286 case PROP_CLOCK:
287 g_value_set_object (value, self->priv->clock);
288 break;
289 case PROP_ACTIVE:
290 g_value_set_boolean (value, IS_ACTIVE (self));
291 break;
292 case PROP_QOS_DSCP:
293 g_value_set_int (value, self->priv->qos_dscp);
294 break;
295 default:
296 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
297 break;
298 }
299 }
300
301 static gboolean
302 gst_net_time_provider_start (GstNetTimeProvider * self, GError ** error)
303 {
304 GSocketAddress *socket_addr, *bound_addr;
305 GInetAddress *inet_addr;
306 GPollFD dummy_pollfd;
307 GSocket *socket;
308 int port;
309 gchar *address;
310 GError *err = NULL;
311
312 if (self->priv->address) {
313 inet_addr = g_inet_address_new_from_string (self->priv->address);
314 if (inet_addr == NULL) {
315 err =
316 g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
317 "Failed to parse address '%s'", self->priv->address);
318 goto invalid_address;
319 }
320 } else {
321 inet_addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
322 }
323
324 GST_LOG_OBJECT (self, "creating socket");
325 socket = g_socket_new (g_inet_address_get_family (inet_addr),
326 G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &err);
327
328 if (!socket)
329 goto no_socket;
330
331 GST_DEBUG_OBJECT (self, "binding on port %d", self->priv->port);
332 socket_addr = g_inet_socket_address_new (inet_addr, self->priv->port);
333 if (!g_socket_bind (socket, socket_addr, TRUE, &err)) {
334 g_object_unref (socket_addr);
335 g_object_unref (inet_addr);
336 goto bind_error;
337 }
338 g_object_unref (socket_addr);
339 g_object_unref (inet_addr);
340
341 bound_addr = g_socket_get_local_address (socket, NULL);
342 port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (bound_addr));
343 inet_addr =
344 g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (bound_addr));
345 address = g_inet_address_to_string (inet_addr);
346
347 if (g_strcmp0 (address, self->priv->address)) {
348 g_free (self->priv->address);
349 self->priv->address = address;
350 GST_DEBUG_OBJECT (self, "notifying address %s", address);
351 g_object_notify (G_OBJECT (self), "address");
352 } else {
353 g_free (address);
354 }
355 if (port != self->priv->port) {
356 self->priv->port = port;
357 GST_DEBUG_OBJECT (self, "notifying port %d", port);
358 g_object_notify (G_OBJECT (self), "port");
359 }
360 GST_DEBUG_OBJECT (self, "bound on UDP address %s, port %d",
361 self->priv->address, port);
362 g_object_unref (bound_addr);
363
364 self->priv->socket = socket;
365 self->priv->cancel = g_cancellable_new ();
366 self->priv->made_cancel_fd =
367 g_cancellable_make_pollfd (self->priv->cancel, &dummy_pollfd);
368
369 self->priv->thread = g_thread_try_new ("GstNetTimeProvider",
370 gst_net_time_provider_thread, self, &err);
371
372 if (!self->priv->thread)
373 goto no_thread;
374
375 return TRUE;
376
377 /* ERRORS */
378 invalid_address:
379 {
380 GST_ERROR_OBJECT (self, "invalid address: %s", self->priv->address);
381 g_propagate_error (error, err);
382 return FALSE;
383 }
384 no_socket:
385 {
386 GST_ERROR_OBJECT (self, "could not create socket: %s", err->message);
387 g_propagate_error (error, err);
388 g_object_unref (inet_addr);
389 return FALSE;
390 }
391 bind_error:
392 {
393 GST_ERROR_OBJECT (self, "bind failed: %s", err->message);
394 g_propagate_error (error, err);
395 g_object_unref (socket);
396 return FALSE;
397 }
398 no_thread:
399 {
400 GST_ERROR_OBJECT (self, "could not create thread: %s", err->message);
401 g_propagate_error (error, err);
402 g_object_unref (self->priv->socket);
403 self->priv->socket = NULL;
404 g_object_unref (self->priv->cancel);
405 self->priv->cancel = NULL;
406 return FALSE;
407 }
408 }
409
410 static void
411 gst_net_time_provider_stop (GstNetTimeProvider * self)
412 {
413 g_return_if_fail (self->priv->thread != NULL);
414
415 GST_INFO_OBJECT (self, "stopping..");
416 g_cancellable_cancel (self->priv->cancel);
417
418 g_thread_join (self->priv->thread);
419 self->priv->thread = NULL;
420
421 if (self->priv->made_cancel_fd)
422 g_cancellable_release_fd (self->priv->cancel);
423
424 g_object_unref (self->priv->cancel);
425 self->priv->cancel = NULL;
426
427 g_object_unref (self->priv->socket);
428 self->priv->socket = NULL;
429
430 GST_INFO_OBJECT (self, "stopped");
431 }
432
433 static gboolean
434 gst_net_time_provider_initable_init (GInitable * initable,
435 GCancellable * cancellable, GError ** error)
436 {
437 GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (initable);
438
439 return gst_net_time_provider_start (self, error);
440 }
441
442 static void
443 gst_net_time_provider_initable_iface_init (gpointer g_iface)
444 {
445 GInitableIface *iface = g_iface;
446
447 iface->init = gst_net_time_provider_initable_init;
448 }
449
450 /**
451 * gst_net_time_provider_new:
452 * @clock: a #GstClock to export over the network
453 * @address: (allow-none): an address to bind on as a dotted quad
454 * (xxx.xxx.xxx.xxx), IPv6 address, or NULL to bind to all addresses
455 * @port: a port to bind on, or 0 to let the kernel choose
456 *
457 * Allows network clients to get the current time of @clock.
458 *
459 * Returns: (transfer full) (nullable): the new #GstNetTimeProvider, or NULL on error
460 */
461 GstNetTimeProvider *
462 gst_net_time_provider_new (GstClock * clock, const gchar * address, gint port)
463 {
464 GstNetTimeProvider *ret;
465
466 g_return_val_if_fail (clock && GST_IS_CLOCK (clock), NULL);
467 g_return_val_if_fail (port >= 0 && port <= G_MAXUINT16, NULL);
468
469 ret =
470 g_initable_new (GST_TYPE_NET_TIME_PROVIDER, NULL, NULL, "clock", clock,
471 "address", address, "port", port, NULL);
472
473 /* Clear floating flag */
474 if (ret)
475 g_object_ref_sink (ret);
476
477 return ret;
478 }