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 /* the select call is also performed on the control sockets, that way
67 * we can send special commands to unblock or restart the select call */
68 #define CONTROL_RESTART 'R' /* restart the select call */
69 #define CONTROL_STOP 'S' /* stop the select call */
70 #define CONTROL_SOCKETS(self) self->control_sock
71 #define WRITE_SOCKET(self) self->control_sock[1]
72 #define READ_SOCKET(self) self->control_sock[0]
74 #ifdef G_OS_WIN32
75 #define close(sock) closesocket(sock)
76 #endif
78 #define SEND_COMMAND(self, command) \
79 G_STMT_START { \
80 unsigned char c; c = command; \
81 write (WRITE_SOCKET(self), &c, 1); \
82 } G_STMT_END
84 #define READ_COMMAND(self, command, res) \
85 G_STMT_START { \
86 res = read(READ_SOCKET(self), &command, 1); \
87 } G_STMT_END
89 #define DEFAULT_ADDRESS "0.0.0.0"
90 #define DEFAULT_PORT 5637
92 #define IS_ACTIVE(self) (g_atomic_int_get (&((self)->active.active)))
94 #ifdef G_OS_WIN32
95 #define setsockopt(sock, sol_flags, reuse_flags, ru, sizeofru) setsockopt (sock, sol_flags, reuse_flags, (char *)ru, sizeofru)
96 #endif
97 enum
98 {
99 PROP_0,
100 PROP_PORT,
101 PROP_ADDRESS,
102 PROP_CLOCK,
103 PROP_ACTIVE
104 /* FILL ME */
105 };
107 static gboolean gst_net_time_provider_start (GstNetTimeProvider * bself);
108 static void gst_net_time_provider_stop (GstNetTimeProvider * bself);
110 static gpointer gst_net_time_provider_thread (gpointer data);
112 static void gst_net_time_provider_finalize (GObject * object);
113 static void gst_net_time_provider_set_property (GObject * object, guint prop_id,
114 const GValue * value, GParamSpec * pspec);
115 static void gst_net_time_provider_get_property (GObject * object, guint prop_id,
116 GValue * value, GParamSpec * pspec);
118 #define _do_init(type) \
119 GST_DEBUG_CATEGORY_INIT (ntp_debug, "nettime", 0, "Network time provider");
121 GST_BOILERPLATE_FULL (GstNetTimeProvider, gst_net_time_provider, GstObject,
122 GST_TYPE_OBJECT, _do_init);
124 #ifdef G_OS_WIN32
125 static int
126 inet_aton (const char *c, struct in_addr *paddr)
127 {
128 paddr->s_addr = inet_addr (c);
129 if (paddr->s_addr == INADDR_NONE)
130 return 0;
132 return 1;
133 }
134 #endif
136 static void
137 gst_net_time_provider_base_init (gpointer g_class)
138 {
139 /* Do nothing here */
140 }
142 static void
143 gst_net_time_provider_class_init (GstNetTimeProviderClass * klass)
144 {
145 GObjectClass *gobject_class;
147 gobject_class = G_OBJECT_CLASS (klass);
149 g_assert (sizeof (GstClockTime) == 8);
151 gobject_class->finalize = gst_net_time_provider_finalize;
152 gobject_class->set_property = gst_net_time_provider_set_property;
153 gobject_class->get_property = gst_net_time_provider_get_property;
155 g_object_class_install_property (gobject_class, PROP_PORT,
156 g_param_spec_int ("port", "port",
157 "The port to receive the packets from, 0=allocate", 0, G_MAXUINT16,
158 DEFAULT_PORT, G_PARAM_READWRITE));
159 g_object_class_install_property (gobject_class, PROP_ADDRESS,
160 g_param_spec_string ("address", "address",
161 "The address to bind on, as a dotted quad (x.x.x.x)",
162 DEFAULT_ADDRESS, G_PARAM_READWRITE));
163 g_object_class_install_property (gobject_class, PROP_CLOCK,
164 g_param_spec_object ("clock", "Clock",
165 "The clock to export over the network", GST_TYPE_CLOCK,
166 G_PARAM_READWRITE));
167 g_object_class_install_property (gobject_class, PROP_ACTIVE,
168 g_param_spec_boolean ("active", "Active",
169 "TRUE if the clock will respond to queries over the network", TRUE,
170 G_PARAM_READWRITE));
171 }
173 static void
174 gst_net_time_provider_init (GstNetTimeProvider * self,
175 GstNetTimeProviderClass * g_class)
176 {
177 #ifdef G_OS_WIN32
178 WSADATA w;
179 int error = WSAStartup (0x0202, &w);
181 if (error) {
182 GST_DEBUG_OBJECT (self, "Error on WSAStartup");
183 }
184 if (w.wVersion != 0x0202) {
185 WSACleanup ();
186 }
187 #endif
189 self->port = DEFAULT_PORT;
190 self->sock = -1;
191 self->address = g_strdup (DEFAULT_ADDRESS);
192 self->thread = NULL;
193 self->active.active = TRUE;
195 READ_SOCKET (self) = -1;
196 WRITE_SOCKET (self) = -1;
197 }
199 static void
200 gst_net_time_provider_finalize (GObject * object)
201 {
202 GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object);
204 if (self->thread) {
205 gst_net_time_provider_stop (self);
206 g_assert (self->thread == NULL);
207 }
209 if (READ_SOCKET (self) != -1) {
210 close (READ_SOCKET (self));
211 close (WRITE_SOCKET (self));
212 READ_SOCKET (self) = -1;
213 WRITE_SOCKET (self) = -1;
214 }
216 g_free (self->address);
217 self->address = NULL;
219 if (self->clock)
220 gst_object_unref (self->clock);
221 self->clock = NULL;
223 #ifdef G_OS_WIN32
224 WSACleanup ();
225 #endif
227 G_OBJECT_CLASS (parent_class)->finalize (object);
228 }
230 static gpointer
231 gst_net_time_provider_thread (gpointer data)
232 {
233 GstNetTimeProvider *self = data;
234 struct sockaddr_in tmpaddr;
235 socklen_t len;
236 fd_set read_fds;
237 guint max_sock;
238 GstNetTimePacket *packet;
239 gint ret;
241 while (TRUE) {
242 FD_ZERO (&read_fds);
243 FD_SET (self->sock, &read_fds);
244 FD_SET (READ_SOCKET (self), &read_fds);
245 max_sock = MAX (self->sock, READ_SOCKET (self));
247 GST_LOG_OBJECT (self, "doing select");
248 #ifdef G_OS_WIN32
249 if (((max_sock + 1) != READ_SOCKET (self)) ||
250 ((max_sock + 1) != WRITE_SOCKET (self))) {
251 ret = select (max_sock + 1, &read_fds, NULL, NULL, NULL);
252 } else {
253 ret = 1;
254 }
255 #else
256 ret = select (max_sock + 1, &read_fds, NULL, NULL, NULL);
257 #endif
258 GST_LOG_OBJECT (self, "select returned %d", ret);
260 if (ret <= 0) {
261 if (errno != EAGAIN && errno != EINTR)
262 goto select_error;
263 else
264 continue;
265 } else if (FD_ISSET (READ_SOCKET (self), &read_fds)) {
266 /* got control message */
267 while (TRUE) {
268 gchar command;
269 int res;
271 READ_COMMAND (self, command, res);
272 if (res <= 0) {
273 GST_LOG_OBJECT (self, "no more commands");
274 break;
275 }
277 switch (command) {
278 case CONTROL_STOP:
279 /* break out of the select loop */
280 GST_LOG_OBJECT (self, "stop");
281 goto stopped;
282 default:
283 GST_WARNING_OBJECT (self, "unkown");
284 g_warning ("nettimeprovider: unknown control message received");
285 continue;
286 }
288 g_assert_not_reached ();
289 }
291 continue;
292 } else {
293 /* got data in */
294 len = sizeof (struct sockaddr);
296 packet = gst_net_time_packet_receive (self->sock,
297 (struct sockaddr *) &tmpaddr, &len);
299 if (!packet)
300 goto receive_error;
302 if (IS_ACTIVE (self)) {
303 /* do what we were asked to and send the packet back */
304 packet->remote_time = gst_clock_get_time (self->clock);
306 /* ignore errors */
307 gst_net_time_packet_send (packet, self->sock,
308 (struct sockaddr *) &tmpaddr, len);
309 }
311 g_free (packet);
313 continue;
314 }
316 g_assert_not_reached ();
318 /* log errors and keep going */
319 select_error:
320 {
321 GST_DEBUG_OBJECT (self, "select error %d: %s (%d)", ret,
322 g_strerror (errno), errno);
323 continue;
324 }
325 stopped:
326 {
327 GST_DEBUG_OBJECT (self, "shutting down");
328 /* close socket */
329 return NULL;
330 }
331 receive_error:
332 {
333 GST_DEBUG_OBJECT (self, "receive error");
334 continue;
335 }
337 g_assert_not_reached ();
339 }
341 g_assert_not_reached ();
343 return NULL;
344 }
346 static void
347 gst_net_time_provider_set_property (GObject * object, guint prop_id,
348 const GValue * value, GParamSpec * pspec)
349 {
350 GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object);
351 GstClock **clock_p = &self->clock;
353 switch (prop_id) {
354 case PROP_PORT:
355 self->port = g_value_get_int (value);
356 break;
357 case PROP_ADDRESS:
358 g_free (self->address);
359 if (g_value_get_string (value) == NULL)
360 self->address = g_strdup (DEFAULT_ADDRESS);
361 else
362 self->address = g_strdup (g_value_get_string (value));
363 break;
364 case PROP_CLOCK:
365 gst_object_replace ((GstObject **) clock_p,
366 (GstObject *) g_value_get_object (value));
367 break;
368 case PROP_ACTIVE:
369 gst_atomic_int_set (&self->active.active, g_value_get_boolean (value));
370 break;
371 default:
372 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
373 break;
374 }
375 }
377 static void
378 gst_net_time_provider_get_property (GObject * object, guint prop_id,
379 GValue * value, GParamSpec * pspec)
380 {
381 GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object);
383 switch (prop_id) {
384 case PROP_PORT:
385 g_value_set_int (value, self->port);
386 break;
387 case PROP_ADDRESS:
388 g_value_set_string (value, self->address);
389 break;
390 case PROP_CLOCK:
391 g_value_set_object (value, self->clock);
392 break;
393 case PROP_ACTIVE:
394 g_value_set_boolean (value, IS_ACTIVE (self));
395 break;
396 default:
397 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
398 break;
399 }
400 }
402 static gboolean
403 gst_net_time_provider_start (GstNetTimeProvider * self)
404 {
405 gint ru;
406 struct sockaddr_in my_addr;
407 guint len;
408 int port;
409 gint ret;
410 GError *error;
412 if ((ret = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
413 goto no_socket;
415 self->sock = ret;
417 ru = 1;
418 ret = setsockopt (self->sock, SOL_SOCKET, SO_REUSEADDR, &ru, sizeof (ru));
419 if (ret < 0)
420 goto setsockopt_error;
422 memset (&my_addr, 0, sizeof (my_addr));
423 my_addr.sin_family = AF_INET; /* host byte order */
424 my_addr.sin_port = htons ((gint16) self->port); /* short, network byte order */
425 my_addr.sin_addr.s_addr = INADDR_ANY;
426 if (self->address)
427 inet_aton (self->address, &my_addr.sin_addr);
429 GST_DEBUG_OBJECT (self, "binding on port %d", self->port);
430 ret = bind (self->sock, (struct sockaddr *) &my_addr, sizeof (my_addr));
431 if (ret < 0)
432 goto bind_error;
434 len = sizeof (my_addr);
435 ret = getsockname (self->sock, (struct sockaddr *) &my_addr, &len);
436 if (ret < 0)
437 goto getsockname_error;
439 port = ntohs (my_addr.sin_port);
440 GST_DEBUG_OBJECT (self, "bound, on port %d", port);
442 if (port != self->port) {
443 self->port = port;
444 GST_DEBUG_OBJECT (self, "notifying %d", port);
445 g_object_notify (G_OBJECT (self), "port");
446 }
448 self->thread = g_thread_create (gst_net_time_provider_thread, self, TRUE,
449 &error);
450 if (!self->thread)
451 goto no_thread;
453 return TRUE;
455 /* ERRORS */
456 no_socket:
457 {
458 GST_ERROR_OBJECT (self, "socket failed %d: %s (%d)", ret,
459 g_strerror (errno), errno);
460 return FALSE;
461 }
462 setsockopt_error:
463 {
464 close (self->sock);
465 self->sock = -1;
466 GST_ERROR_OBJECT (self, "setsockopt failed %d: %s (%d)", ret,
467 g_strerror (errno), errno);
468 return FALSE;
469 }
470 bind_error:
471 {
472 close (self->sock);
473 self->sock = -1;
474 GST_ERROR_OBJECT (self, "bind failed %d: %s (%d)", ret,
475 g_strerror (errno), errno);
476 return FALSE;
477 }
478 getsockname_error:
479 {
480 close (self->sock);
481 self->sock = -1;
482 GST_ERROR_OBJECT (self, "getsockname failed %d: %s (%d)", ret,
483 g_strerror (errno), errno);
484 return FALSE;
485 }
486 no_thread:
487 {
488 close (self->sock);
489 self->sock = -1;
490 GST_ERROR_OBJECT (self, "could not create thread: %s", error->message);
491 g_error_free (error);
492 return FALSE;
493 }
494 }
496 static void
497 gst_net_time_provider_stop (GstNetTimeProvider * self)
498 {
499 SEND_COMMAND (self, CONTROL_STOP);
500 g_thread_join (self->thread);
501 self->thread = NULL;
503 if (self->sock != -1) {
504 close (self->sock);
505 self->sock = -1;
506 }
507 }
509 /**
510 * gst_net_time_provider_new:
511 * @clock: a #GstClock to export over the network
512 * @address: an address to bind on as a dotted quad (xxx.xxx.xxx.xxx), or NULL
513 * to bind to all addresses
514 * @port: a port to bind on, or 0 to let the kernel choose
515 *
516 * Allows network clients to get the current time of @clock.
517 *
518 * Returns: the new #GstNetTimeProvider, or NULL on error
519 */
520 GstNetTimeProvider *
521 gst_net_time_provider_new (GstClock * clock, const gchar * address, gint port)
522 {
523 GstNetTimeProvider *ret;
524 gint iret;
526 g_return_val_if_fail (clock && GST_IS_CLOCK (clock), NULL);
527 g_return_val_if_fail (port >= 0 && port <= G_MAXUINT16, NULL);
529 ret = g_object_new (GST_TYPE_NET_TIME_PROVIDER, "clock", clock, "address",
530 address, "port", port, NULL);
532 #ifdef G_OS_WIN32
533 GST_DEBUG_OBJECT (ret, "creating pipe");
534 if ((iret = _pipe (CONTROL_SOCKETS (ret), 4096, _O_BINARY)) < 0)
535 goto no_socket_pair;
536 #else
537 GST_DEBUG_OBJECT (ret, "creating socket pair");
538 if ((iret = socketpair (PF_UNIX, SOCK_STREAM, 0, CONTROL_SOCKETS (ret))) < 0)
539 goto no_socket_pair;
541 fcntl (READ_SOCKET (ret), F_SETFL, O_NONBLOCK);
542 fcntl (WRITE_SOCKET (ret), F_SETFL, O_NONBLOCK);
543 #endif
545 if (!gst_net_time_provider_start (ret))
546 goto failed_start;
548 /* all systems go, cap'n */
549 return ret;
551 no_socket_pair:
552 {
553 GST_ERROR_OBJECT (ret, "no socket pair %d: %s (%d)", iret,
554 g_strerror (errno), errno);
555 gst_object_unref (ret);
556 return NULL;
557 }
558 failed_start:
559 {
560 /* already printed a nice error */
561 gst_object_unref (ret);
562 return NULL;
563 }
565 }