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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19 /**
20 * SECTION:gstnettimeprovider
21 * @short_description: Special object that exposed the time of a clock
22 * on the network.
23 * @see_also: #GstClock, #GstNetClientClock, #GstPipeline
24 *
25 * This object exposes the time of a #GstClock on the network.
26 *
27 * A #GstNetTimeProvider is created with gst_net_time_provider_new() which
28 * takes a #GstClock, an address and a port number as arguments.
29 *
30 * After creating the object, a client clock such as #GstNetClientClock can
31 * query the exposed clock over the network for its values.
32 *
33 * The #GstNetTimeProvider typically wraps the clock used by a #GstPipeline.
34 *
35 * Last reviewed on 2005-11-23 (0.9.5)
36 */
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
42 #include "gstnettimeprovider.h"
43 #include "gstnettimepacket.h"
45 #include <glib.h>
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
51 #if defined (_MSC_VER) && _MSC_VER >= 1400
52 #include <io.h>
53 #endif
55 #ifndef G_OS_WIN32
56 #include <sys/ioctl.h>
57 #endif
59 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
60 #include <sys/filio.h>
61 #endif
63 GST_DEBUG_CATEGORY_STATIC (ntp_debug);
64 #define GST_CAT_DEFAULT (ntp_debug)
66 #ifdef G_OS_WIN32
67 #define close(sock) closesocket(sock)
68 #endif
70 #define DEFAULT_ADDRESS "0.0.0.0"
71 #define DEFAULT_PORT 5637
73 #define IS_ACTIVE(self) (g_atomic_int_get (&((self)->active.active)))
75 #ifdef G_OS_WIN32
76 #define setsockopt(sock, sol_flags, reuse_flags, ru, sizeofru) setsockopt (sock, sol_flags, reuse_flags, (char *)ru, sizeofru)
77 #endif
78 enum
79 {
80 PROP_0,
81 PROP_PORT,
82 PROP_ADDRESS,
83 PROP_CLOCK,
84 PROP_ACTIVE
85 /* FILL ME */
86 };
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
92 {
93 GstPollFD sock;
94 GstPoll *fdset;
95 };
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);
108 #define _do_init(type) \
109 GST_DEBUG_CATEGORY_INIT (ntp_debug, "nettime", 0, "Network time provider");
111 GST_BOILERPLATE_FULL (GstNetTimeProvider, gst_net_time_provider, GstObject,
112 GST_TYPE_OBJECT, _do_init);
114 #ifdef G_OS_WIN32
115 static int
116 inet_aton (const char *c, struct in_addr *paddr)
117 {
118 paddr->s_addr = inet_addr (c);
119 if (paddr->s_addr == INADDR_NONE)
120 return 0;
122 return 1;
123 }
124 #endif
126 static void
127 gst_net_time_provider_base_init (gpointer g_class)
128 {
129 /* Do nothing here */
130 }
132 static void
133 gst_net_time_provider_class_init (GstNetTimeProviderClass * klass)
134 {
135 GObjectClass *gobject_class;
137 gobject_class = G_OBJECT_CLASS (klass);
139 g_assert (sizeof (GstClockTime) == 8);
141 g_type_class_add_private (klass, sizeof (GstNetTimeProviderPrivate));
143 gobject_class->finalize = gst_net_time_provider_finalize;
144 gobject_class->set_property = gst_net_time_provider_set_property;
145 gobject_class->get_property = gst_net_time_provider_get_property;
147 g_object_class_install_property (gobject_class, PROP_PORT,
148 g_param_spec_int ("port", "port",
149 "The port to receive the packets from, 0=allocate", 0, G_MAXUINT16,
150 DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
151 g_object_class_install_property (gobject_class, PROP_ADDRESS,
152 g_param_spec_string ("address", "address",
153 "The address to bind on, as a dotted quad (x.x.x.x)",
154 DEFAULT_ADDRESS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155 g_object_class_install_property (gobject_class, PROP_CLOCK,
156 g_param_spec_object ("clock", "Clock",
157 "The clock to export over the network", GST_TYPE_CLOCK,
158 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
159 g_object_class_install_property (gobject_class, PROP_ACTIVE,
160 g_param_spec_boolean ("active", "Active",
161 "TRUE if the clock will respond to queries over the network", TRUE,
162 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
163 }
165 static void
166 gst_net_time_provider_init (GstNetTimeProvider * self,
167 GstNetTimeProviderClass * g_class)
168 {
169 #ifdef G_OS_WIN32
170 WSADATA w;
171 int error = WSAStartup (0x0202, &w);
173 if (error) {
174 GST_DEBUG_OBJECT (self, "Error on WSAStartup");
175 }
176 if (w.wVersion != 0x0202) {
177 WSACleanup ();
178 }
179 #endif
180 self->priv = GST_NET_TIME_PROVIDER_GET_PRIVATE (self);
182 self->port = DEFAULT_PORT;
183 self->priv->sock.fd = -1;
184 self->address = g_strdup (DEFAULT_ADDRESS);
185 self->thread = NULL;
186 self->active.active = TRUE;
187 }
189 static void
190 gst_net_time_provider_finalize (GObject * object)
191 {
192 GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object);
194 if (self->thread) {
195 gst_net_time_provider_stop (self);
196 g_assert (self->thread == NULL);
197 }
199 if (self->priv->fdset) {
200 gst_poll_free (self->priv->fdset);
201 self->priv->fdset = NULL;
202 }
204 g_free (self->address);
205 self->address = NULL;
207 if (self->clock)
208 gst_object_unref (self->clock);
209 self->clock = NULL;
211 #ifdef G_OS_WIN32
212 WSACleanup ();
213 #endif
215 G_OBJECT_CLASS (parent_class)->finalize (object);
216 }
218 static gpointer
219 gst_net_time_provider_thread (gpointer data)
220 {
221 GstNetTimeProvider *self = data;
222 struct sockaddr_in tmpaddr;
223 socklen_t len;
224 GstNetTimePacket *packet;
225 gint ret;
227 while (TRUE) {
228 GST_LOG_OBJECT (self, "doing select");
229 ret = gst_poll_wait (self->priv->fdset, GST_CLOCK_TIME_NONE);
230 GST_LOG_OBJECT (self, "select returned %d", ret);
232 if (ret <= 0) {
233 if (errno == EBUSY) {
234 GST_LOG_OBJECT (self, "stop");
235 goto stopped;
236 } else if (errno != EAGAIN && errno != EINTR)
237 goto select_error;
238 else
239 continue;
240 } else {
241 /* got data in */
242 len = sizeof (struct sockaddr);
244 packet = gst_net_time_packet_receive (self->priv->sock.fd,
245 (struct sockaddr *) &tmpaddr, &len);
247 if (!packet)
248 goto receive_error;
250 if (IS_ACTIVE (self)) {
251 /* do what we were asked to and send the packet back */
252 packet->remote_time = gst_clock_get_time (self->clock);
254 /* ignore errors */
255 gst_net_time_packet_send (packet, self->priv->sock.fd,
256 (struct sockaddr *) &tmpaddr, len);
257 }
259 g_free (packet);
261 continue;
262 }
264 g_assert_not_reached ();
266 /* log errors and keep going */
267 select_error:
268 {
269 GST_DEBUG_OBJECT (self, "select error %d: %s (%d)", ret,
270 g_strerror (errno), errno);
271 continue;
272 }
273 stopped:
274 {
275 GST_DEBUG_OBJECT (self, "shutting down");
276 /* close socket */
277 return NULL;
278 }
279 receive_error:
280 {
281 GST_DEBUG_OBJECT (self, "receive error");
282 continue;
283 }
285 g_assert_not_reached ();
287 }
289 g_assert_not_reached ();
291 return NULL;
292 }
294 static void
295 gst_net_time_provider_set_property (GObject * object, guint prop_id,
296 const GValue * value, GParamSpec * pspec)
297 {
298 GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object);
299 GstClock **clock_p = &self->clock;
301 switch (prop_id) {
302 case PROP_PORT:
303 self->port = g_value_get_int (value);
304 break;
305 case PROP_ADDRESS:
306 g_free (self->address);
307 if (g_value_get_string (value) == NULL)
308 self->address = g_strdup (DEFAULT_ADDRESS);
309 else
310 self->address = g_strdup (g_value_get_string (value));
311 break;
312 case PROP_CLOCK:
313 gst_object_replace ((GstObject **) clock_p,
314 (GstObject *) g_value_get_object (value));
315 break;
316 case PROP_ACTIVE:
317 g_atomic_int_set (&self->active.active, g_value_get_boolean (value));
318 break;
319 default:
320 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
321 break;
322 }
323 }
325 static void
326 gst_net_time_provider_get_property (GObject * object, guint prop_id,
327 GValue * value, GParamSpec * pspec)
328 {
329 GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object);
331 switch (prop_id) {
332 case PROP_PORT:
333 g_value_set_int (value, self->port);
334 break;
335 case PROP_ADDRESS:
336 g_value_set_string (value, self->address);
337 break;
338 case PROP_CLOCK:
339 g_value_set_object (value, self->clock);
340 break;
341 case PROP_ACTIVE:
342 g_value_set_boolean (value, IS_ACTIVE (self));
343 break;
344 default:
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
346 break;
347 }
348 }
350 static gboolean
351 gst_net_time_provider_start (GstNetTimeProvider * self)
352 {
353 gint ru;
354 struct sockaddr_in my_addr;
355 socklen_t len;
356 int port;
357 gint ret;
358 GError *error;
360 if ((ret = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
361 goto no_socket;
363 self->priv->sock.fd = ret;
365 ru = 1;
366 ret =
367 setsockopt (self->priv->sock.fd, SOL_SOCKET, SO_REUSEADDR, &ru,
368 sizeof (ru));
369 if (ret < 0)
370 goto setsockopt_error;
372 memset (&my_addr, 0, sizeof (my_addr));
373 my_addr.sin_family = AF_INET; /* host byte order */
374 my_addr.sin_port = htons ((gint16) self->port); /* short, network byte order */
375 my_addr.sin_addr.s_addr = INADDR_ANY;
376 if (self->address) {
377 ret = inet_aton (self->address, &my_addr.sin_addr);
378 if (ret == 0)
379 goto invalid_address_error;
380 }
382 GST_DEBUG_OBJECT (self, "binding on port %d", self->port);
383 ret =
384 bind (self->priv->sock.fd, (struct sockaddr *) &my_addr,
385 sizeof (my_addr));
386 if (ret < 0)
387 goto bind_error;
389 len = sizeof (my_addr);
390 ret = getsockname (self->priv->sock.fd, (struct sockaddr *) &my_addr, &len);
391 if (ret < 0)
392 goto getsockname_error;
394 port = ntohs (my_addr.sin_port);
395 GST_DEBUG_OBJECT (self, "bound, on port %d", port);
397 if (port != self->port) {
398 self->port = port;
399 GST_DEBUG_OBJECT (self, "notifying %d", port);
400 g_object_notify (G_OBJECT (self), "port");
401 }
403 gst_poll_add_fd (self->priv->fdset, &self->priv->sock);
404 gst_poll_fd_ctl_read (self->priv->fdset, &self->priv->sock, TRUE);
406 #if !GLIB_CHECK_VERSION (2, 31, 0)
407 self->thread = g_thread_create (gst_net_time_provider_thread, self, TRUE,
408 &error);
409 #else
410 self->thread = g_thread_try_new ("GstNetTimeProvider",
411 gst_net_time_provider_thread, self, &error);
412 #endif
414 if (error != NULL)
415 goto no_thread;
417 return TRUE;
419 /* ERRORS */
420 no_socket:
421 {
422 GST_ERROR_OBJECT (self, "socket failed %d: %s (%d)", ret,
423 g_strerror (errno), errno);
424 return FALSE;
425 }
426 setsockopt_error:
427 {
428 close (self->priv->sock.fd);
429 self->priv->sock.fd = -1;
430 GST_ERROR_OBJECT (self, "setsockopt failed %d: %s (%d)", ret,
431 g_strerror (errno), errno);
432 return FALSE;
433 }
434 invalid_address_error:
435 {
436 close (self->priv->sock.fd);
437 self->priv->sock.fd = -1;
438 GST_ERROR_OBJECT (self, "invalid network address %s: %s (%d)",
439 self->address, g_strerror (errno), errno);
440 return FALSE;
441 }
442 bind_error:
443 {
444 close (self->priv->sock.fd);
445 self->priv->sock.fd = -1;
446 GST_ERROR_OBJECT (self, "bind failed %d: %s (%d)", ret,
447 g_strerror (errno), errno);
448 return FALSE;
449 }
450 getsockname_error:
451 {
452 close (self->priv->sock.fd);
453 self->priv->sock.fd = -1;
454 GST_ERROR_OBJECT (self, "getsockname failed %d: %s (%d)", ret,
455 g_strerror (errno), errno);
456 return FALSE;
457 }
458 no_thread:
459 {
460 gst_poll_remove_fd (self->priv->fdset, &self->priv->sock);
461 close (self->priv->sock.fd);
462 self->priv->sock.fd = -1;
463 GST_ERROR_OBJECT (self, "could not create thread: %s", error->message);
464 g_error_free (error);
465 return FALSE;
466 }
467 }
469 static void
470 gst_net_time_provider_stop (GstNetTimeProvider * self)
471 {
472 gst_poll_set_flushing (self->priv->fdset, TRUE);
473 g_thread_join (self->thread);
474 self->thread = NULL;
476 if (self->priv->sock.fd != -1) {
477 gst_poll_remove_fd (self->priv->fdset, &self->priv->sock);
478 close (self->priv->sock.fd);
479 self->priv->sock.fd = -1;
480 }
481 }
483 /**
484 * gst_net_time_provider_new:
485 * @clock: a #GstClock to export over the network
486 * @address: an address to bind on as a dotted quad (xxx.xxx.xxx.xxx), or NULL
487 * to bind to all addresses
488 * @port: a port to bind on, or 0 to let the kernel choose
489 *
490 * Allows network clients to get the current time of @clock.
491 *
492 * Returns: the new #GstNetTimeProvider, or NULL on error
493 */
494 GstNetTimeProvider *
495 gst_net_time_provider_new (GstClock * clock, const gchar * address, gint port)
496 {
497 GstNetTimeProvider *ret;
499 g_return_val_if_fail (clock && GST_IS_CLOCK (clock), NULL);
500 g_return_val_if_fail (port >= 0 && port <= G_MAXUINT16, NULL);
502 ret = g_object_new (GST_TYPE_NET_TIME_PROVIDER, "clock", clock, "address",
503 address, "port", port, NULL);
505 if ((ret->priv->fdset = gst_poll_new (TRUE)) == NULL)
506 goto no_fdset;
508 if (!gst_net_time_provider_start (ret))
509 goto failed_start;
511 /* all systems go, cap'n */
512 return ret;
514 no_fdset:
515 {
516 GST_ERROR_OBJECT (ret, "could not create an fdset: %s (%d)",
517 g_strerror (errno), errno);
518 gst_object_unref (ret);
519 return NULL;
520 }
521 failed_start:
522 {
523 /* already printed a nice error */
524 gst_object_unref (ret);
525 return NULL;
526 }
527 }