|
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 */ |
|
37 |
|
38 #ifdef HAVE_CONFIG_H |
|
39 #include "config.h" |
|
40 #endif |
|
41 |
|
42 #include "gstnettimeprovider.h" |
|
43 #include "gstnettimepacket.h" |
|
44 |
|
45 #include <glib.h> |
|
46 |
|
47 #ifdef HAVE_UNISTD_H |
|
48 #include <unistd.h> |
|
49 #endif |
|
50 |
|
51 #if defined (_MSC_VER) && _MSC_VER >= 1400 |
|
52 #include <io.h> |
|
53 #endif |
|
54 |
|
55 #ifndef G_OS_WIN32 |
|
56 #include <sys/ioctl.h> |
|
57 #endif |
|
58 |
|
59 #ifdef HAVE_FIONREAD_IN_SYS_FILIO |
|
60 #include <sys/filio.h> |
|
61 #endif |
|
62 |
|
63 GST_DEBUG_CATEGORY_STATIC (ntp_debug); |
|
64 #define GST_CAT_DEFAULT (ntp_debug) |
|
65 |
|
66 #ifdef G_OS_WIN32 |
|
67 #define close(sock) closesocket(sock) |
|
68 #endif |
|
69 |
|
70 #define DEFAULT_ADDRESS "0.0.0.0" |
|
71 #define DEFAULT_PORT 5637 |
|
72 |
|
73 #define IS_ACTIVE(self) (g_atomic_int_get (&((self)->active.active))) |
|
74 |
|
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 }; |
|
87 |
|
88 #define GST_NET_TIME_PROVIDER_GET_PRIVATE(obj) \ |
|
89 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_NET_TIME_PROVIDER, GstNetTimeProviderPrivate)) |
|
90 |
|
91 struct _GstNetTimeProviderPrivate |
|
92 { |
|
93 GstPollFD sock; |
|
94 GstPoll *fdset; |
|
95 }; |
|
96 |
|
97 static gboolean gst_net_time_provider_start (GstNetTimeProvider * bself); |
|
98 static void gst_net_time_provider_stop (GstNetTimeProvider * bself); |
|
99 |
|
100 static gpointer gst_net_time_provider_thread (gpointer data); |
|
101 |
|
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); |
|
107 |
|
108 #define _do_init(type) \ |
|
109 GST_DEBUG_CATEGORY_INIT (ntp_debug, "nettime", 0, "Network time provider"); |
|
110 |
|
111 GST_BOILERPLATE_FULL (GstNetTimeProvider, gst_net_time_provider, GstObject, |
|
112 GST_TYPE_OBJECT, _do_init); |
|
113 |
|
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; |
|
121 |
|
122 return 1; |
|
123 } |
|
124 #endif |
|
125 |
|
126 static void |
|
127 gst_net_time_provider_base_init (gpointer g_class) |
|
128 { |
|
129 /* Do nothing here */ |
|
130 } |
|
131 |
|
132 static void |
|
133 gst_net_time_provider_class_init (GstNetTimeProviderClass * klass) |
|
134 { |
|
135 GObjectClass *gobject_class; |
|
136 |
|
137 gobject_class = G_OBJECT_CLASS (klass); |
|
138 |
|
139 g_assert (sizeof (GstClockTime) == 8); |
|
140 |
|
141 g_type_class_add_private (klass, sizeof (GstNetTimeProviderPrivate)); |
|
142 |
|
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; |
|
146 |
|
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)); |
|
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)); |
|
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)); |
|
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)); |
|
163 } |
|
164 |
|
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); |
|
172 |
|
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); |
|
181 |
|
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 } |
|
188 |
|
189 static void |
|
190 gst_net_time_provider_finalize (GObject * object) |
|
191 { |
|
192 GstNetTimeProvider *self = GST_NET_TIME_PROVIDER (object); |
|
193 |
|
194 if (self->thread) { |
|
195 gst_net_time_provider_stop (self); |
|
196 g_assert (self->thread == NULL); |
|
197 } |
|
198 |
|
199 if (self->priv->fdset) { |
|
200 gst_poll_free (self->priv->fdset); |
|
201 self->priv->fdset = NULL; |
|
202 } |
|
203 |
|
204 g_free (self->address); |
|
205 self->address = NULL; |
|
206 |
|
207 if (self->clock) |
|
208 gst_object_unref (self->clock); |
|
209 self->clock = NULL; |
|
210 |
|
211 #ifdef G_OS_WIN32 |
|
212 WSACleanup (); |
|
213 #endif |
|
214 |
|
215 G_OBJECT_CLASS (parent_class)->finalize (object); |
|
216 } |
|
217 |
|
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; |
|
226 |
|
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); |
|
231 |
|
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); |
|
243 |
|
244 packet = gst_net_time_packet_receive (self->priv->sock.fd, |
|
245 (struct sockaddr *) &tmpaddr, &len); |
|
246 |
|
247 if (!packet) |
|
248 goto receive_error; |
|
249 |
|
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); |
|
253 |
|
254 /* ignore errors */ |
|
255 gst_net_time_packet_send (packet, self->priv->sock.fd, |
|
256 (struct sockaddr *) &tmpaddr, len); |
|
257 } |
|
258 |
|
259 g_free (packet); |
|
260 |
|
261 continue; |
|
262 } |
|
263 |
|
264 g_assert_not_reached (); |
|
265 |
|
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 } |
|
284 |
|
285 g_assert_not_reached (); |
|
286 |
|
287 } |
|
288 |
|
289 g_assert_not_reached (); |
|
290 |
|
291 return NULL; |
|
292 } |
|
293 |
|
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; |
|
300 |
|
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 gst_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 } |
|
324 |
|
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); |
|
330 |
|
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 } |
|
349 |
|
350 static gboolean |
|
351 gst_net_time_provider_start (GstNetTimeProvider * self) |
|
352 { |
|
353 gint ru; |
|
354 struct sockaddr_in my_addr; |
|
355 guint len; |
|
356 int port; |
|
357 gint ret; |
|
358 GError *error; |
|
359 |
|
360 if ((ret = socket (AF_INET, SOCK_DGRAM, 0)) < 0) |
|
361 goto no_socket; |
|
362 |
|
363 self->priv->sock.fd = ret; |
|
364 |
|
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; |
|
371 |
|
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 inet_aton (self->address, &my_addr.sin_addr); |
|
378 |
|
379 GST_DEBUG_OBJECT (self, "binding on port %d", self->port); |
|
380 ret = |
|
381 bind (self->priv->sock.fd, (struct sockaddr *) &my_addr, |
|
382 sizeof (my_addr)); |
|
383 if (ret < 0) |
|
384 goto bind_error; |
|
385 |
|
386 len = sizeof (my_addr); |
|
387 ret = getsockname (self->priv->sock.fd, (struct sockaddr *) &my_addr, &len); |
|
388 if (ret < 0) |
|
389 goto getsockname_error; |
|
390 |
|
391 port = ntohs (my_addr.sin_port); |
|
392 GST_DEBUG_OBJECT (self, "bound, on port %d", port); |
|
393 |
|
394 if (port != self->port) { |
|
395 self->port = port; |
|
396 GST_DEBUG_OBJECT (self, "notifying %d", port); |
|
397 g_object_notify (G_OBJECT (self), "port"); |
|
398 } |
|
399 |
|
400 gst_poll_add_fd (self->priv->fdset, &self->priv->sock); |
|
401 gst_poll_fd_ctl_read (self->priv->fdset, &self->priv->sock, TRUE); |
|
402 |
|
403 self->thread = g_thread_create (gst_net_time_provider_thread, self, TRUE, |
|
404 &error); |
|
405 if (!self->thread) |
|
406 goto no_thread; |
|
407 |
|
408 return TRUE; |
|
409 |
|
410 /* ERRORS */ |
|
411 no_socket: |
|
412 { |
|
413 GST_ERROR_OBJECT (self, "socket failed %d: %s (%d)", ret, |
|
414 g_strerror (errno), errno); |
|
415 return FALSE; |
|
416 } |
|
417 setsockopt_error: |
|
418 { |
|
419 close (self->priv->sock.fd); |
|
420 self->priv->sock.fd = -1; |
|
421 GST_ERROR_OBJECT (self, "setsockopt failed %d: %s (%d)", ret, |
|
422 g_strerror (errno), errno); |
|
423 return FALSE; |
|
424 } |
|
425 bind_error: |
|
426 { |
|
427 close (self->priv->sock.fd); |
|
428 self->priv->sock.fd = -1; |
|
429 GST_ERROR_OBJECT (self, "bind failed %d: %s (%d)", ret, |
|
430 g_strerror (errno), errno); |
|
431 return FALSE; |
|
432 } |
|
433 getsockname_error: |
|
434 { |
|
435 close (self->priv->sock.fd); |
|
436 self->priv->sock.fd = -1; |
|
437 GST_ERROR_OBJECT (self, "getsockname failed %d: %s (%d)", ret, |
|
438 g_strerror (errno), errno); |
|
439 return FALSE; |
|
440 } |
|
441 no_thread: |
|
442 { |
|
443 gst_poll_remove_fd (self->priv->fdset, &self->priv->sock); |
|
444 close (self->priv->sock.fd); |
|
445 self->priv->sock.fd = -1; |
|
446 GST_ERROR_OBJECT (self, "could not create thread: %s", error->message); |
|
447 g_error_free (error); |
|
448 return FALSE; |
|
449 } |
|
450 } |
|
451 |
|
452 static void |
|
453 gst_net_time_provider_stop (GstNetTimeProvider * self) |
|
454 { |
|
455 gst_poll_set_flushing (self->priv->fdset, TRUE); |
|
456 g_thread_join (self->thread); |
|
457 self->thread = NULL; |
|
458 |
|
459 if (self->priv->sock.fd != -1) { |
|
460 gst_poll_remove_fd (self->priv->fdset, &self->priv->sock); |
|
461 close (self->priv->sock.fd); |
|
462 self->priv->sock.fd = -1; |
|
463 } |
|
464 } |
|
465 |
|
466 /** |
|
467 * gst_net_time_provider_new: |
|
468 * @clock: a #GstClock to export over the network |
|
469 * @address: an address to bind on as a dotted quad (xxx.xxx.xxx.xxx), or NULL |
|
470 * to bind to all addresses |
|
471 * @port: a port to bind on, or 0 to let the kernel choose |
|
472 * |
|
473 * Allows network clients to get the current time of @clock. |
|
474 * |
|
475 * Returns: the new #GstNetTimeProvider, or NULL on error |
|
476 */ |
|
477 #ifdef __SYMBIAN32__ |
|
478 EXPORT_C |
|
479 #endif |
|
480 |
|
481 GstNetTimeProvider * |
|
482 gst_net_time_provider_new (GstClock * clock, const gchar * address, gint port) |
|
483 { |
|
484 GstNetTimeProvider *ret; |
|
485 |
|
486 g_return_val_if_fail (clock && GST_IS_CLOCK (clock), NULL); |
|
487 g_return_val_if_fail (port >= 0 && port <= G_MAXUINT16, NULL); |
|
488 |
|
489 ret = g_object_new (GST_TYPE_NET_TIME_PROVIDER, "clock", clock, "address", |
|
490 address, "port", port, NULL); |
|
491 |
|
492 if ((ret->priv->fdset = gst_poll_new (TRUE)) == NULL) |
|
493 goto no_fdset; |
|
494 |
|
495 if (!gst_net_time_provider_start (ret)) |
|
496 goto failed_start; |
|
497 |
|
498 /* all systems go, cap'n */ |
|
499 return ret; |
|
500 |
|
501 no_fdset: |
|
502 { |
|
503 GST_ERROR_OBJECT (ret, "could not create an fdset: %s (%d)", |
|
504 g_strerror (errno), errno); |
|
505 gst_object_unref (ret); |
|
506 return NULL; |
|
507 } |
|
508 failed_start: |
|
509 { |
|
510 /* already printed a nice error */ |
|
511 gst_object_unref (ret); |
|
512 return NULL; |
|
513 } |
|
514 } |