aggregator: remove DEBUG_FUNCPTR
[platform/upstream/gstreamer.git] / libs / gst / net / gstnettimeprovider.c
1 /* GStreamer
2  * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
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 #define GST_NET_TIME_PROVIDER_GET_PRIVATE(obj)  \
65    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_NET_TIME_PROVIDER, GstNetTimeProviderPrivate))
66
67 struct _GstNetTimeProviderPrivate
68 {
69   gchar *address;
70   int port;
71   gint qos_dscp;                /* ATOMIC */
72
73   GThread *thread;
74
75   GstClock *clock;
76
77   gboolean active;              /* ATOMIC */
78
79   GSocket *socket;
80   GCancellable *cancel;
81   gboolean made_cancel_fd;
82 };
83
84 static void gst_net_time_provider_initable_iface_init (gpointer g_iface);
85
86 static gboolean gst_net_time_provider_start (GstNetTimeProvider * bself,
87     GError ** error);
88 static void gst_net_time_provider_stop (GstNetTimeProvider * bself);
89
90 static gpointer gst_net_time_provider_thread (gpointer data);
91
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);
97
98 #define _do_init \
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)
101
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);
105
106 static void
107 gst_net_time_provider_class_init (GstNetTimeProviderClass * klass)
108 {
109   GObjectClass *gobject_class;
110
111   gobject_class = G_OBJECT_CLASS (klass);
112
113   g_assert (sizeof (GstClockTime) == 8);
114
115   g_type_class_add_private (klass, sizeof (GstNetTimeProviderPrivate));
116
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;
120
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,
124           DEFAULT_PORT,
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));
143 }
144
145 static void
146 gst_net_time_provider_init (GstNetTimeProvider * self)
147 {
148   self->priv = GST_NET_TIME_PROVIDER_GET_PRIVATE (self);
149
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;
155 }
156
157 static void
158 gst_net_time_provider_finalize (GObject * object)
159 {
160   GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object);
161
162   if (self->priv->thread) {
163     gst_net_time_provider_stop (self);
164     g_assert (self->priv->thread == NULL);
165   }
166
167   g_free (self->priv->address);
168   self->priv->address = NULL;
169
170   if (self->priv->clock)
171     gst_object_unref (self->priv->clock);
172   self->priv->clock = NULL;
173
174   G_OBJECT_CLASS (parent_class)->finalize (object);
175 }
176
177 static gpointer
178 gst_net_time_provider_thread (gpointer data)
179 {
180   GstNetTimeProvider *self = data;
181   GCancellable *cancel = self->priv->cancel;
182   GSocket *socket = self->priv->socket;
183   GstNetTimePacket *packet;
184   GError *err = NULL;
185   gint cur_qos_dscp = DEFAULT_QOS_DSCP;
186   gint new_qos_dscp;
187
188   GST_INFO_OBJECT (self, "time provider thread is running");
189
190   while (TRUE) {
191     GSocketAddress *sender_addr = NULL;
192
193
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);
197
198       if (err->code == G_IO_ERROR_CANCELLED)
199         break;
200
201       /* try again */
202       g_usleep (G_USEC_PER_SEC / 10);
203       g_error_free (err);
204       err = NULL;
205       continue;
206     }
207
208     /* got data in */
209     packet = gst_net_time_packet_receive (socket, &sender_addr, &err);
210
211     if (err != NULL) {
212       GST_DEBUG_OBJECT (self, "receive error: %s", err->message);
213       g_usleep (G_USEC_PER_SEC / 10);
214       g_error_free (err);
215       err = NULL;
216       continue;
217     }
218
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;
225     }
226
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);
230
231       /* ignore errors */
232       gst_net_time_packet_send (packet, socket, sender_addr, NULL);
233       g_object_unref (sender_addr);
234       g_free (packet);
235     }
236   }
237
238   g_error_free (err);
239
240   GST_INFO_OBJECT (self, "time provider thread is stopping");
241   return NULL;
242 }
243
244 static void
245 gst_net_time_provider_set_property (GObject * object, guint prop_id,
246     const GValue * value, GParamSpec * pspec)
247 {
248   GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object);
249   GstClock **clock_p = &self->priv->clock;
250
251   switch (prop_id) {
252     case PROP_PORT:
253       self->priv->port = g_value_get_int (value);
254       break;
255     case PROP_ADDRESS:
256       g_free (self->priv->address);
257       if (g_value_get_string (value) == NULL)
258         self->priv->address = g_strdup (DEFAULT_ADDRESS);
259       else
260         self->priv->address = g_strdup (g_value_get_string (value));
261       break;
262     case PROP_CLOCK:
263       gst_object_replace ((GstObject **) clock_p,
264           (GstObject *) g_value_get_object (value));
265       break;
266     case PROP_ACTIVE:
267       g_atomic_int_set (&self->priv->active, g_value_get_boolean (value));
268       break;
269     case PROP_QOS_DSCP:
270       g_atomic_int_set (&self->priv->qos_dscp, g_value_get_int (value));
271       break;
272     default:
273       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
274       break;
275   }
276 }
277
278 static void
279 gst_net_time_provider_get_property (GObject * object, guint prop_id,
280     GValue * value, GParamSpec * pspec)
281 {
282   GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object);
283
284   switch (prop_id) {
285     case PROP_PORT:
286       g_value_set_int (value, self->priv->port);
287       break;
288     case PROP_ADDRESS:
289       g_value_set_string (value, self->priv->address);
290       break;
291     case PROP_CLOCK:
292       g_value_set_object (value, self->priv->clock);
293       break;
294     case PROP_ACTIVE:
295       g_value_set_boolean (value, IS_ACTIVE (self));
296       break;
297     case PROP_QOS_DSCP:
298       g_value_set_int (value, self->priv->qos_dscp);
299       break;
300     default:
301       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
302       break;
303   }
304 }
305
306 static gboolean
307 gst_net_time_provider_start (GstNetTimeProvider * self, GError ** error)
308 {
309   GSocketAddress *socket_addr, *bound_addr;
310   GInetAddress *inet_addr;
311   GPollFD dummy_pollfd;
312   GSocket *socket;
313   int port;
314   gchar *address;
315   GError *err = NULL;
316
317   if (self->priv->address) {
318     inet_addr = g_inet_address_new_from_string (self->priv->address);
319     if (inet_addr == NULL) {
320       err =
321           g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
322           "Failed to parse address '%s'", self->priv->address);
323       goto invalid_address;
324     }
325   } else {
326     inet_addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
327   }
328
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);
332
333   if (!socket)
334     goto no_socket;
335
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);
341     goto bind_error;
342   }
343   g_object_unref (socket_addr);
344   g_object_unref (inet_addr);
345
346   bound_addr = g_socket_get_local_address (socket, NULL);
347   port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (bound_addr));
348   inet_addr =
349       g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (bound_addr));
350   address = g_inet_address_to_string (inet_addr);
351
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");
357   } else {
358     g_free (address);
359   }
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");
364   }
365   GST_DEBUG_OBJECT (self, "bound on UDP address %s, port %d",
366       self->priv->address, port);
367   g_object_unref (bound_addr);
368
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);
373
374   self->priv->thread = g_thread_try_new ("GstNetTimeProvider",
375       gst_net_time_provider_thread, self, &err);
376
377   if (!self->priv->thread)
378     goto no_thread;
379
380   return TRUE;
381
382   /* ERRORS */
383 invalid_address:
384   {
385     GST_ERROR_OBJECT (self, "invalid address: %s", self->priv->address);
386     g_propagate_error (error, err);
387     return FALSE;
388   }
389 no_socket:
390   {
391     GST_ERROR_OBJECT (self, "could not create socket: %s", err->message);
392     g_propagate_error (error, err);
393     g_object_unref (inet_addr);
394     return FALSE;
395   }
396 bind_error:
397   {
398     GST_ERROR_OBJECT (self, "bind failed: %s", err->message);
399     g_propagate_error (error, err);
400     g_object_unref (socket);
401     return FALSE;
402   }
403 no_thread:
404   {
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;
411     return FALSE;
412   }
413 }
414
415 static void
416 gst_net_time_provider_stop (GstNetTimeProvider * self)
417 {
418   g_return_if_fail (self->priv->thread != NULL);
419
420   GST_INFO_OBJECT (self, "stopping..");
421   g_cancellable_cancel (self->priv->cancel);
422
423   g_thread_join (self->priv->thread);
424   self->priv->thread = NULL;
425
426   if (self->priv->made_cancel_fd)
427     g_cancellable_release_fd (self->priv->cancel);
428
429   g_object_unref (self->priv->cancel);
430   self->priv->cancel = NULL;
431
432   g_object_unref (self->priv->socket);
433   self->priv->socket = NULL;
434
435   GST_INFO_OBJECT (self, "stopped");
436 }
437
438 static gboolean
439 gst_net_time_provider_initable_init (GInitable * initable,
440     GCancellable * cancellable, GError ** error)
441 {
442   GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (initable);
443
444   return gst_net_time_provider_start (self, error);
445 }
446
447 static void
448 gst_net_time_provider_initable_iface_init (gpointer g_iface)
449 {
450   GInitableIface *iface = g_iface;
451
452   iface->init = gst_net_time_provider_initable_init;
453 }
454
455 /**
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
461  *
462  * Allows network clients to get the current time of @clock.
463  *
464  * Returns: (transfer full): the new #GstNetTimeProvider, or NULL on error
465  */
466 GstNetTimeProvider *
467 gst_net_time_provider_new (GstClock * clock, const gchar * address, gint port)
468 {
469   GstNetTimeProvider *ret;
470
471   g_return_val_if_fail (clock && GST_IS_CLOCK (clock), NULL);
472   g_return_val_if_fail (port >= 0 && port <= G_MAXUINT16, NULL);
473
474   ret =
475       g_initable_new (GST_TYPE_NET_TIME_PROVIDER, NULL, NULL, "clock", clock,
476       "address", address, "port", port, NULL);
477
478   /* Clear floating flag */
479   g_object_ref_sink (ret);
480
481   return ret;
482 }