2 * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
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.
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.
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.
20 * SECTION:gstnettimeprovider
21 * @title: GstNetTimeProvider
22 * @short_description: Special object that exposed the time of a clock
24 * @see_also: #GstClock, #GstNetClientClock, #GstPipeline
26 * This object exposes the time of a #GstClock on the network.
28 * A #GstNetTimeProvider is created with gst_net_time_provider_new() which
29 * takes a #GstClock, an address and a port number as arguments.
31 * After creating the object, a client clock such as #GstNetClientClock can
32 * query the exposed clock over the network for its values.
34 * The #GstNetTimeProvider typically wraps the clock used by a #GstPipeline.
41 #include "gstnettimeprovider.h"
42 #include "gstnettimepacket.h"
43 #include "gstnetutils.h"
45 GST_DEBUG_CATEGORY_STATIC (ntp_debug);
46 #define GST_CAT_DEFAULT (ntp_debug)
48 #define DEFAULT_ADDRESS "0.0.0.0"
49 #define DEFAULT_PORT 5637
50 #define DEFAULT_QOS_DSCP -1
52 #define IS_ACTIVE(self) (g_atomic_int_get (&((self)->priv->active)))
64 #define GST_NET_TIME_PROVIDER_GET_PRIVATE(obj) \
65 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_NET_TIME_PROVIDER, GstNetTimeProviderPrivate))
67 struct _GstNetTimeProviderPrivate
71 gint qos_dscp; /* ATOMIC */
77 gboolean active; /* ATOMIC */
81 gboolean made_cancel_fd;
84 static void gst_net_time_provider_initable_iface_init (gpointer g_iface);
86 static gboolean gst_net_time_provider_start (GstNetTimeProvider * bself,
88 static void gst_net_time_provider_stop (GstNetTimeProvider * bself);
90 static gpointer gst_net_time_provider_thread (gpointer data);
92 static void gst_net_time_provider_finalize (GObject * object);
93 static void gst_net_time_provider_set_property (GObject * object, guint prop_id,
94 const GValue * value, GParamSpec * pspec);
95 static void gst_net_time_provider_get_property (GObject * object, guint prop_id,
96 GValue * value, GParamSpec * pspec);
99 GST_DEBUG_CATEGORY_INIT (ntp_debug, "nettime", 0, "Network time provider"); \
100 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, gst_net_time_provider_initable_iface_init)
102 #define gst_net_time_provider_parent_class parent_class
103 G_DEFINE_TYPE_WITH_CODE (GstNetTimeProvider, gst_net_time_provider,
104 GST_TYPE_OBJECT, _do_init);
107 gst_net_time_provider_class_init (GstNetTimeProviderClass * klass)
109 GObjectClass *gobject_class;
111 gobject_class = G_OBJECT_CLASS (klass);
113 g_assert (sizeof (GstClockTime) == 8);
115 g_type_class_add_private (klass, sizeof (GstNetTimeProviderPrivate));
117 gobject_class->finalize = gst_net_time_provider_finalize;
118 gobject_class->set_property = gst_net_time_provider_set_property;
119 gobject_class->get_property = gst_net_time_provider_get_property;
121 g_object_class_install_property (gobject_class, PROP_PORT,
122 g_param_spec_int ("port", "port",
123 "The port to receive the packets from, 0=allocate", 0, G_MAXUINT16,
125 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
126 g_object_class_install_property (gobject_class, PROP_ADDRESS,
127 g_param_spec_string ("address", "address",
128 "The address to bind on, as a dotted quad (x.x.x.x)", DEFAULT_ADDRESS,
129 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
130 g_object_class_install_property (gobject_class, PROP_CLOCK,
131 g_param_spec_object ("clock", "Clock",
132 "The clock to export over the network", GST_TYPE_CLOCK,
133 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
134 g_object_class_install_property (gobject_class, PROP_ACTIVE,
135 g_param_spec_boolean ("active", "Active",
136 "TRUE if the clock will respond to queries over the network", TRUE,
137 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
138 g_object_class_install_property (gobject_class, PROP_QOS_DSCP,
139 g_param_spec_int ("qos-dscp", "QoS diff srv code point",
140 "Quality of Service, differentiated services code point (-1 default)",
141 -1, 63, DEFAULT_QOS_DSCP,
142 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
146 gst_net_time_provider_init (GstNetTimeProvider * self)
148 self->priv = GST_NET_TIME_PROVIDER_GET_PRIVATE (self);
150 self->priv->port = DEFAULT_PORT;
151 self->priv->address = g_strdup (DEFAULT_ADDRESS);
152 self->priv->qos_dscp = DEFAULT_QOS_DSCP;
153 self->priv->thread = NULL;
154 self->priv->active = TRUE;
158 gst_net_time_provider_finalize (GObject * object)
160 GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object);
162 if (self->priv->thread) {
163 gst_net_time_provider_stop (self);
164 g_assert (self->priv->thread == NULL);
167 g_free (self->priv->address);
168 self->priv->address = NULL;
170 if (self->priv->clock)
171 gst_object_unref (self->priv->clock);
172 self->priv->clock = NULL;
174 G_OBJECT_CLASS (parent_class)->finalize (object);
178 gst_net_time_provider_thread (gpointer data)
180 GstNetTimeProvider *self = data;
181 GCancellable *cancel = self->priv->cancel;
182 GSocket *socket = self->priv->socket;
183 GstNetTimePacket *packet;
185 gint cur_qos_dscp = DEFAULT_QOS_DSCP;
188 GST_INFO_OBJECT (self, "time provider thread is running");
191 GSocketAddress *sender_addr = NULL;
194 GST_LOG_OBJECT (self, "waiting on socket");
195 if (!g_socket_condition_wait (socket, G_IO_IN, cancel, &err)) {
196 GST_INFO_OBJECT (self, "socket error: %s", err->message);
198 if (err->code == G_IO_ERROR_CANCELLED)
202 g_usleep (G_USEC_PER_SEC / 10);
209 packet = gst_net_time_packet_receive (socket, &sender_addr, &err);
212 GST_DEBUG_OBJECT (self, "receive error: %s", err->message);
213 g_usleep (G_USEC_PER_SEC / 10);
219 /* before next sending check if need to change QoS */
220 new_qos_dscp = self->priv->qos_dscp;
221 if (cur_qos_dscp != new_qos_dscp &&
222 gst_net_utils_set_socket_dscp (socket, new_qos_dscp)) {
223 GST_DEBUG_OBJECT (self, "changed QoS DSCP to: %d", new_qos_dscp);
224 cur_qos_dscp = new_qos_dscp;
227 if (IS_ACTIVE (self)) {
228 /* do what we were asked to and send the packet back */
229 packet->remote_time = gst_clock_get_time (self->priv->clock);
232 gst_net_time_packet_send (packet, socket, sender_addr, NULL);
233 g_object_unref (sender_addr);
240 GST_INFO_OBJECT (self, "time provider thread is stopping");
245 gst_net_time_provider_set_property (GObject * object, guint prop_id,
246 const GValue * value, GParamSpec * pspec)
248 GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object);
249 GstClock **clock_p = &self->priv->clock;
253 self->priv->port = g_value_get_int (value);
256 g_free (self->priv->address);
257 if (g_value_get_string (value) == NULL)
258 self->priv->address = g_strdup (DEFAULT_ADDRESS);
260 self->priv->address = g_strdup (g_value_get_string (value));
263 gst_object_replace ((GstObject **) clock_p,
264 (GstObject *) g_value_get_object (value));
267 g_atomic_int_set (&self->priv->active, g_value_get_boolean (value));
270 g_atomic_int_set (&self->priv->qos_dscp, g_value_get_int (value));
273 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
279 gst_net_time_provider_get_property (GObject * object, guint prop_id,
280 GValue * value, GParamSpec * pspec)
282 GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object);
286 g_value_set_int (value, self->priv->port);
289 g_value_set_string (value, self->priv->address);
292 g_value_set_object (value, self->priv->clock);
295 g_value_set_boolean (value, IS_ACTIVE (self));
298 g_value_set_int (value, self->priv->qos_dscp);
301 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
307 gst_net_time_provider_start (GstNetTimeProvider * self, GError ** error)
309 GSocketAddress *socket_addr, *bound_addr;
310 GInetAddress *inet_addr;
311 GPollFD dummy_pollfd;
317 if (self->priv->address) {
318 inet_addr = g_inet_address_new_from_string (self->priv->address);
319 if (inet_addr == NULL) {
321 g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
322 "Failed to parse address '%s'", self->priv->address);
323 goto invalid_address;
326 inet_addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
329 GST_LOG_OBJECT (self, "creating socket");
330 socket = g_socket_new (g_inet_address_get_family (inet_addr),
331 G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &err);
336 GST_DEBUG_OBJECT (self, "binding on port %d", self->priv->port);
337 socket_addr = g_inet_socket_address_new (inet_addr, self->priv->port);
338 if (!g_socket_bind (socket, socket_addr, TRUE, &err)) {
339 g_object_unref (socket_addr);
340 g_object_unref (inet_addr);
343 g_object_unref (socket_addr);
344 g_object_unref (inet_addr);
346 bound_addr = g_socket_get_local_address (socket, NULL);
347 port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (bound_addr));
349 g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (bound_addr));
350 address = g_inet_address_to_string (inet_addr);
352 if (g_strcmp0 (address, self->priv->address)) {
353 g_free (self->priv->address);
354 self->priv->address = address;
355 GST_DEBUG_OBJECT (self, "notifying address %s", address);
356 g_object_notify (G_OBJECT (self), "address");
360 if (port != self->priv->port) {
361 self->priv->port = port;
362 GST_DEBUG_OBJECT (self, "notifying port %d", port);
363 g_object_notify (G_OBJECT (self), "port");
365 GST_DEBUG_OBJECT (self, "bound on UDP address %s, port %d",
366 self->priv->address, port);
367 g_object_unref (bound_addr);
369 self->priv->socket = socket;
370 self->priv->cancel = g_cancellable_new ();
371 self->priv->made_cancel_fd =
372 g_cancellable_make_pollfd (self->priv->cancel, &dummy_pollfd);
374 self->priv->thread = g_thread_try_new ("GstNetTimeProvider",
375 gst_net_time_provider_thread, self, &err);
377 if (!self->priv->thread)
385 GST_ERROR_OBJECT (self, "invalid address: %s", self->priv->address);
386 g_propagate_error (error, err);
391 GST_ERROR_OBJECT (self, "could not create socket: %s", err->message);
392 g_propagate_error (error, err);
393 g_object_unref (inet_addr);
398 GST_ERROR_OBJECT (self, "bind failed: %s", err->message);
399 g_propagate_error (error, err);
400 g_object_unref (socket);
405 GST_ERROR_OBJECT (self, "could not create thread: %s", err->message);
406 g_propagate_error (error, err);
407 g_object_unref (self->priv->socket);
408 self->priv->socket = NULL;
409 g_object_unref (self->priv->cancel);
410 self->priv->cancel = NULL;
416 gst_net_time_provider_stop (GstNetTimeProvider * self)
418 g_return_if_fail (self->priv->thread != NULL);
420 GST_INFO_OBJECT (self, "stopping..");
421 g_cancellable_cancel (self->priv->cancel);
423 g_thread_join (self->priv->thread);
424 self->priv->thread = NULL;
426 if (self->priv->made_cancel_fd)
427 g_cancellable_release_fd (self->priv->cancel);
429 g_object_unref (self->priv->cancel);
430 self->priv->cancel = NULL;
432 g_object_unref (self->priv->socket);
433 self->priv->socket = NULL;
435 GST_INFO_OBJECT (self, "stopped");
439 gst_net_time_provider_initable_init (GInitable * initable,
440 GCancellable * cancellable, GError ** error)
442 GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (initable);
444 return gst_net_time_provider_start (self, error);
448 gst_net_time_provider_initable_iface_init (gpointer g_iface)
450 GInitableIface *iface = g_iface;
452 iface->init = gst_net_time_provider_initable_init;
456 * gst_net_time_provider_new:
457 * @clock: a #GstClock to export over the network
458 * @address: (allow-none): an address to bind on as a dotted quad
459 * (xxx.xxx.xxx.xxx), IPv6 address, or NULL to bind to all addresses
460 * @port: a port to bind on, or 0 to let the kernel choose
462 * Allows network clients to get the current time of @clock.
464 * Returns: (transfer full): the new #GstNetTimeProvider, or NULL on error
467 gst_net_time_provider_new (GstClock * clock, const gchar * address, gint port)
469 GstNetTimeProvider *ret;
471 g_return_val_if_fail (clock && GST_IS_CLOCK (clock), NULL);
472 g_return_val_if_fail (port >= 0 && port <= G_MAXUINT16, NULL);
475 g_initable_new (GST_TYPE_NET_TIME_PROVIDER, NULL, NULL, "clock", clock,
476 "address", address, "port", port, NULL);
478 /* Clear floating flag */
479 g_object_ref_sink (ret);