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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 * SECTION:gstnettimeprovider
21 * @short_description: Special object that exposed the time of a clock
23 * @see_also: #GstClock, #GstNetClientClock, #GstPipeline
25 * This object exposes the time of a #GstClock on the network.
27 * A #GstNetTimeProvider is created with gst_net_time_provider_new() which
28 * takes a #GstClock, an address and a port number as arguments.
30 * After creating the object, a client clock such as #GstNetClientClock can
31 * query the exposed clock over the network for its values.
33 * The #GstNetTimeProvider typically wraps the clock used by a #GstPipeline.
35 * Last reviewed on 2005-11-23 (0.9.5)
42 #include "gstnettimeprovider.h"
43 #include "gstnettimepacket.h"
51 #if defined (_MSC_VER) && _MSC_VER >= 1400
56 #include <sys/ioctl.h>
59 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
60 #include <sys/filio.h>
63 GST_DEBUG_CATEGORY_STATIC (ntp_debug);
64 #define GST_CAT_DEFAULT (ntp_debug)
67 #define close(sock) closesocket(sock)
70 #define DEFAULT_ADDRESS "0.0.0.0"
71 #define DEFAULT_PORT 5637
73 #define IS_ACTIVE(self) (g_atomic_int_get (&((self)->active)))
76 #define setsockopt(sock, sol_flags, reuse_flags, ru, sizeofru) setsockopt (sock, sol_flags, reuse_flags, (char *)ru, sizeofru)
88 #define GST_NET_TIME_PROVIDER_GET_PRIVATE(obj) \
89 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_NET_TIME_PROVIDER, GstNetTimeProviderPrivate))
91 struct _GstNetTimeProviderPrivate
97 static gboolean gst_net_time_provider_start (GstNetTimeProvider * bself);
98 static void gst_net_time_provider_stop (GstNetTimeProvider * bself);
100 static gpointer gst_net_time_provider_thread (gpointer data);
102 static void gst_net_time_provider_finalize (GObject * object);
103 static void gst_net_time_provider_set_property (GObject * object, guint prop_id,
104 const GValue * value, GParamSpec * pspec);
105 static void gst_net_time_provider_get_property (GObject * object, guint prop_id,
106 GValue * value, GParamSpec * pspec);
109 GST_DEBUG_CATEGORY_INIT (ntp_debug, "nettime", 0, "Network time provider");
111 #define gst_net_time_provider_parent_class parent_class
112 G_DEFINE_TYPE_WITH_CODE (GstNetTimeProvider, gst_net_time_provider,
113 GST_TYPE_OBJECT, _do_init);
117 inet_aton (const char *c, struct in_addr *paddr)
119 paddr->s_addr = inet_addr (c);
120 if (paddr->s_addr == INADDR_NONE)
128 gst_net_time_provider_class_init (GstNetTimeProviderClass * klass)
130 GObjectClass *gobject_class;
132 gobject_class = G_OBJECT_CLASS (klass);
134 g_assert (sizeof (GstClockTime) == 8);
136 g_type_class_add_private (klass, sizeof (GstNetTimeProviderPrivate));
138 gobject_class->finalize = gst_net_time_provider_finalize;
139 gobject_class->set_property = gst_net_time_provider_set_property;
140 gobject_class->get_property = gst_net_time_provider_get_property;
142 g_object_class_install_property (gobject_class, PROP_PORT,
143 g_param_spec_int ("port", "port",
144 "The port to receive the packets from, 0=allocate", 0, G_MAXUINT16,
145 DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
146 g_object_class_install_property (gobject_class, PROP_ADDRESS,
147 g_param_spec_string ("address", "address",
148 "The address to bind on, as a dotted quad (x.x.x.x)",
149 DEFAULT_ADDRESS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
150 g_object_class_install_property (gobject_class, PROP_CLOCK,
151 g_param_spec_object ("clock", "Clock",
152 "The clock to export over the network", GST_TYPE_CLOCK,
153 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
154 g_object_class_install_property (gobject_class, PROP_ACTIVE,
155 g_param_spec_boolean ("active", "Active",
156 "TRUE if the clock will respond to queries over the network", TRUE,
157 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
161 gst_net_time_provider_init (GstNetTimeProvider * self)
165 int error = WSAStartup (0x0202, &w);
168 GST_DEBUG_OBJECT (self, "Error on WSAStartup");
170 if (w.wVersion != 0x0202) {
174 self->priv = GST_NET_TIME_PROVIDER_GET_PRIVATE (self);
176 self->port = DEFAULT_PORT;
177 self->priv->sock.fd = -1;
178 self->address = g_strdup (DEFAULT_ADDRESS);
184 gst_net_time_provider_finalize (GObject * object)
186 GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object);
189 gst_net_time_provider_stop (self);
190 g_assert (self->thread == NULL);
193 if (self->priv->fdset) {
194 gst_poll_free (self->priv->fdset);
195 self->priv->fdset = NULL;
198 g_free (self->address);
199 self->address = NULL;
202 gst_object_unref (self->clock);
209 G_OBJECT_CLASS (parent_class)->finalize (object);
213 gst_net_time_provider_thread (gpointer data)
215 GstNetTimeProvider *self = data;
216 struct sockaddr_in tmpaddr;
218 GstNetTimePacket *packet;
222 GST_LOG_OBJECT (self, "doing select");
223 ret = gst_poll_wait (self->priv->fdset, GST_CLOCK_TIME_NONE);
224 GST_LOG_OBJECT (self, "select returned %d", ret);
227 if (errno == EBUSY) {
228 GST_LOG_OBJECT (self, "stop");
230 } else if (errno != EAGAIN && errno != EINTR)
236 len = sizeof (struct sockaddr);
238 packet = gst_net_time_packet_receive (self->priv->sock.fd,
239 (struct sockaddr *) &tmpaddr, &len);
244 if (IS_ACTIVE (self)) {
245 /* do what we were asked to and send the packet back */
246 packet->remote_time = gst_clock_get_time (self->clock);
249 gst_net_time_packet_send (packet, self->priv->sock.fd,
250 (struct sockaddr *) &tmpaddr, len);
258 g_assert_not_reached ();
260 /* log errors and keep going */
263 GST_DEBUG_OBJECT (self, "select error %d: %s (%d)", ret,
264 g_strerror (errno), errno);
269 GST_DEBUG_OBJECT (self, "shutting down");
275 GST_DEBUG_OBJECT (self, "receive error");
279 g_assert_not_reached ();
283 g_assert_not_reached ();
289 gst_net_time_provider_set_property (GObject * object, guint prop_id,
290 const GValue * value, GParamSpec * pspec)
292 GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object);
293 GstClock **clock_p = &self->clock;
297 self->port = g_value_get_int (value);
300 g_free (self->address);
301 if (g_value_get_string (value) == NULL)
302 self->address = g_strdup (DEFAULT_ADDRESS);
304 self->address = g_strdup (g_value_get_string (value));
307 gst_object_replace ((GstObject **) clock_p,
308 (GstObject *) g_value_get_object (value));
311 g_atomic_int_set (&self->active, g_value_get_boolean (value));
314 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
320 gst_net_time_provider_get_property (GObject * object, guint prop_id,
321 GValue * value, GParamSpec * pspec)
323 GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object);
327 g_value_set_int (value, self->port);
330 g_value_set_string (value, self->address);
333 g_value_set_object (value, self->clock);
336 g_value_set_boolean (value, IS_ACTIVE (self));
339 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
345 gst_net_time_provider_start (GstNetTimeProvider * self)
348 struct sockaddr_in my_addr;
354 if ((ret = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
357 self->priv->sock.fd = ret;
361 setsockopt (self->priv->sock.fd, SOL_SOCKET, SO_REUSEADDR, &ru,
364 goto setsockopt_error;
366 memset (&my_addr, 0, sizeof (my_addr));
367 my_addr.sin_family = AF_INET; /* host byte order */
368 my_addr.sin_port = htons ((gint16) self->port); /* short, network byte order */
369 my_addr.sin_addr.s_addr = INADDR_ANY;
371 ret = inet_aton (self->address, &my_addr.sin_addr);
373 goto invalid_address_error;
376 GST_DEBUG_OBJECT (self, "binding on port %d", self->port);
378 bind (self->priv->sock.fd, (struct sockaddr *) &my_addr,
383 len = sizeof (my_addr);
384 ret = getsockname (self->priv->sock.fd, (struct sockaddr *) &my_addr, &len);
386 goto getsockname_error;
388 port = ntohs (my_addr.sin_port);
389 GST_DEBUG_OBJECT (self, "bound, on port %d", port);
391 if (port != self->port) {
393 GST_DEBUG_OBJECT (self, "notifying %d", port);
394 g_object_notify (G_OBJECT (self), "port");
397 gst_poll_add_fd (self->priv->fdset, &self->priv->sock);
398 gst_poll_fd_ctl_read (self->priv->fdset, &self->priv->sock, TRUE);
400 self->thread = g_thread_create (gst_net_time_provider_thread, self, TRUE,
410 GST_ERROR_OBJECT (self, "socket failed %d: %s (%d)", ret,
411 g_strerror (errno), errno);
416 close (self->priv->sock.fd);
417 self->priv->sock.fd = -1;
418 GST_ERROR_OBJECT (self, "setsockopt failed %d: %s (%d)", ret,
419 g_strerror (errno), errno);
422 invalid_address_error:
424 close (self->priv->sock.fd);
425 self->priv->sock.fd = -1;
426 GST_ERROR_OBJECT (self, "invalid network address %s: %s (%d)",
427 self->address, g_strerror (errno), errno);
432 close (self->priv->sock.fd);
433 self->priv->sock.fd = -1;
434 GST_ERROR_OBJECT (self, "bind failed %d: %s (%d)", ret,
435 g_strerror (errno), errno);
440 close (self->priv->sock.fd);
441 self->priv->sock.fd = -1;
442 GST_ERROR_OBJECT (self, "getsockname failed %d: %s (%d)", ret,
443 g_strerror (errno), errno);
448 gst_poll_remove_fd (self->priv->fdset, &self->priv->sock);
449 close (self->priv->sock.fd);
450 self->priv->sock.fd = -1;
451 GST_ERROR_OBJECT (self, "could not create thread: %s", error->message);
452 g_error_free (error);
458 gst_net_time_provider_stop (GstNetTimeProvider * self)
460 gst_poll_set_flushing (self->priv->fdset, TRUE);
461 g_thread_join (self->thread);
464 if (self->priv->sock.fd != -1) {
465 gst_poll_remove_fd (self->priv->fdset, &self->priv->sock);
466 close (self->priv->sock.fd);
467 self->priv->sock.fd = -1;
472 * gst_net_time_provider_new:
473 * @clock: a #GstClock to export over the network
474 * @address: an address to bind on as a dotted quad (xxx.xxx.xxx.xxx), or NULL
475 * to bind to all addresses
476 * @port: a port to bind on, or 0 to let the kernel choose
478 * Allows network clients to get the current time of @clock.
480 * Returns: the new #GstNetTimeProvider, or NULL on error
483 gst_net_time_provider_new (GstClock * clock, const gchar * address, gint port)
485 GstNetTimeProvider *ret;
487 g_return_val_if_fail (clock && GST_IS_CLOCK (clock), NULL);
488 g_return_val_if_fail (port >= 0 && port <= G_MAXUINT16, NULL);
490 ret = g_object_new (GST_TYPE_NET_TIME_PROVIDER, "clock", clock, "address",
491 address, "port", port, NULL);
493 if ((ret->priv->fdset = gst_poll_new (TRUE)) == NULL)
496 if (!gst_net_time_provider_start (ret))
499 /* all systems go, cap'n */
504 GST_ERROR_OBJECT (ret, "could not create an fdset: %s (%d)",
505 g_strerror (errno), errno);
506 gst_object_unref (ret);
511 /* already printed a nice error */
512 gst_object_unref (ret);