2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2005 Wim Taymans <wim@fluendo.com>
4 * 2005 Andy Wingo <wingo@pobox.com>
6 * gstnetclientclock.h: clock that synchronizes itself to a time provider over
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
25 * SECTION:gstnetclientclock
26 * @short_description: Special clock that synchronizes to a remote time
28 * @see_also: #GstClock, #GstNetTimeProvider, #GstPipeline
30 * This object implements a custom #GstClock that synchronizes its time
31 * to a remote time provider such as #GstNetTimeProvider.
33 * A new clock is created with gst_net_client_clock_new() which takes the
34 * address and port of the remote time provider along with a name and
37 * This clock will poll the time provider and will update its calibration
38 * parameters based on the local and remote observations.
40 * Various parameters of the clock can be configured with the parent #GstClock
41 * "timeout", "window-size" and "window-threshold" object properties.
43 * A #GstNetClientClock is typically set on a #GstPipeline with
44 * gst_pipeline_use_clock().
46 * Last reviewed on 2005-11-23 (0.9.5)
53 #include "gstnettimepacket.h"
54 #include "gstnetclientclock.h"
60 #if defined (_MSC_VER) && _MSC_VER >= 1400
64 GST_DEBUG_CATEGORY_STATIC (ncc_debug);
65 #define GST_CAT_DEFAULT (ncc_debug)
67 #define DEFAULT_ADDRESS "127.0.0.1"
68 #define DEFAULT_PORT 5637
69 #define DEFAULT_TIMEOUT GST_SECOND
72 #define getsockname(sock,addr,len) getsockname(sock,addr,(int *)len)
82 #define GST_NET_CLIENT_CLOCK_GET_PRIVATE(obj) \
83 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_NET_CLIENT_CLOCK, GstNetClientClockPrivate))
85 struct _GstNetClientClockPrivate
91 #define _do_init(type) \
92 GST_DEBUG_CATEGORY_INIT (ncc_debug, "netclock", 0, "Network client clock");
94 GST_BOILERPLATE_FULL (GstNetClientClock, gst_net_client_clock,
95 GstSystemClock, GST_TYPE_SYSTEM_CLOCK, _do_init);
97 static void gst_net_client_clock_finalize (GObject * object);
98 static void gst_net_client_clock_set_property (GObject * object, guint prop_id,
99 const GValue * value, GParamSpec * pspec);
100 static void gst_net_client_clock_get_property (GObject * object, guint prop_id,
101 GValue * value, GParamSpec * pspec);
103 static void gst_net_client_clock_stop (GstNetClientClock * self);
107 inet_aton (const char *c, struct in_addr *paddr)
109 /* note that inet_addr is deprecated on unix because
110 * inet_addr returns -1 (INADDR_NONE) for the valid 255.255.255.255
112 paddr->s_addr = inet_addr (c);
113 if (paddr->s_addr == INADDR_NONE)
121 gst_net_client_clock_base_init (gpointer g_class)
127 gst_net_client_clock_class_init (GstNetClientClockClass * klass)
129 GObjectClass *gobject_class;
131 gobject_class = G_OBJECT_CLASS (klass);
133 g_type_class_add_private (klass, sizeof (GstNetClientClockPrivate));
135 gobject_class->finalize = gst_net_client_clock_finalize;
136 gobject_class->get_property = gst_net_client_clock_get_property;
137 gobject_class->set_property = gst_net_client_clock_set_property;
139 g_object_class_install_property (gobject_class, PROP_ADDRESS,
140 g_param_spec_string ("address", "address",
141 "The address of the machine providing a time server, "
142 "as a dotted quad (x.x.x.x)", DEFAULT_ADDRESS,
143 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
144 g_object_class_install_property (gobject_class, PROP_PORT,
145 g_param_spec_int ("port", "port",
146 "The port on which the remote server is listening", 0, G_MAXUINT16,
147 DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
151 gst_net_client_clock_init (GstNetClientClock * self,
152 GstNetClientClockClass * g_class)
154 GstClock *clock = GST_CLOCK_CAST (self);
158 int error = WSAStartup (0x0202, &w);
161 GST_DEBUG_OBJECT (self, "Error on WSAStartup");
163 if (w.wVersion != 0x0202) {
167 self->priv = GST_NET_CLIENT_CLOCK_GET_PRIVATE (self);
169 self->port = DEFAULT_PORT;
170 self->address = g_strdup (DEFAULT_ADDRESS);
172 clock->timeout = DEFAULT_TIMEOUT;
174 self->priv->sock.fd = -1;
177 self->servaddr = NULL;
181 gst_net_client_clock_finalize (GObject * object)
183 GstNetClientClock *self = GST_NET_CLIENT_CLOCK (object);
186 gst_net_client_clock_stop (self);
187 g_assert (self->thread == NULL);
190 if (self->priv->fdset) {
191 gst_poll_free (self->priv->fdset);
192 self->priv->fdset = NULL;
195 g_free (self->address);
196 self->address = NULL;
198 g_free (self->servaddr);
199 self->servaddr = NULL;
205 G_OBJECT_CLASS (parent_class)->finalize (object);
209 gst_net_client_clock_set_property (GObject * object, guint prop_id,
210 const GValue * value, GParamSpec * pspec)
212 GstNetClientClock *self = GST_NET_CLIENT_CLOCK (object);
216 g_free (self->address);
217 if (g_value_get_string (value) == NULL)
218 self->address = g_strdup (DEFAULT_ADDRESS);
220 self->address = g_strdup (g_value_get_string (value));
223 self->port = g_value_get_int (value);
226 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
232 gst_net_client_clock_get_property (GObject * object, guint prop_id,
233 GValue * value, GParamSpec * pspec)
235 GstNetClientClock *self = GST_NET_CLIENT_CLOCK (object);
239 g_value_set_string (value, self->address);
242 g_value_set_int (value, self->port);
245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
251 gst_net_client_clock_observe_times (GstNetClientClock * self,
252 GstClockTime local_1, GstClockTime remote, GstClockTime local_2)
254 GstClockTime local_avg;
258 if (local_2 < local_1)
259 goto bogus_observation;
261 local_avg = (local_2 + local_1) / 2;
263 clock = GST_CLOCK_CAST (self);
265 gst_clock_add_observation (GST_CLOCK (self), local_avg, remote, &r_squared);
267 GST_CLOCK_SLAVE_LOCK (self);
268 if (clock->filling) {
269 self->current_timeout = 0;
272 self->current_timeout =
273 (1e-3 / (1 - MIN (r_squared, 0.99999))) * GST_SECOND;
274 self->current_timeout = MIN (self->current_timeout, clock->timeout);
276 GST_CLOCK_SLAVE_UNLOCK (clock);
282 GST_WARNING_OBJECT (self, "time packet receive time < send time (%"
283 GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")", GST_TIME_ARGS (local_1),
284 GST_TIME_ARGS (local_2));
290 gst_net_client_clock_do_select (GstNetClientClock * self)
296 GST_LOG_OBJECT (self, "doing select");
298 diff = gst_clock_get_internal_time (GST_CLOCK (self));
299 ret = gst_poll_wait (self->priv->fdset, self->current_timeout);
300 diff = gst_clock_get_internal_time (GST_CLOCK (self)) - diff;
302 if (diff > self->current_timeout)
303 self->current_timeout = 0;
305 self->current_timeout -= diff;
307 GST_LOG_OBJECT (self, "select returned %d", ret);
309 if (ret < 0 && errno != EBUSY) {
310 if (errno != EAGAIN && errno != EINTR)
318 g_assert_not_reached ();
320 /* log errors and keep going */
323 GST_WARNING_OBJECT (self, "select error %d: %s (%d)", ret,
324 g_strerror (errno), errno);
329 g_assert_not_reached ();
334 gst_net_client_clock_thread (gpointer data)
336 GstNetClientClock *self = data;
337 struct sockaddr_in tmpaddr;
339 GstNetTimePacket *packet;
341 GstClock *clock = data;
344 ret = gst_net_client_clock_do_select (self);
346 if (ret < 0 && errno == EBUSY) {
347 GST_LOG_OBJECT (self, "stop");
349 } else if (ret == 0) {
350 /* timed out, let's send another packet */
351 GST_DEBUG_OBJECT (self, "timed out");
353 packet = gst_net_time_packet_new (NULL);
355 packet->local_time = gst_clock_get_internal_time (GST_CLOCK (self));
357 GST_DEBUG_OBJECT (self, "sending packet, local time = %" GST_TIME_FORMAT,
358 GST_TIME_ARGS (packet->local_time));
359 gst_net_time_packet_send (packet, self->priv->sock.fd,
360 (struct sockaddr *) self->servaddr, sizeof (struct sockaddr_in));
365 self->current_timeout = clock->timeout;
367 } else if (gst_poll_fd_can_read (self->priv->fdset, &self->priv->sock)) {
369 GstClockTime new_local = gst_clock_get_internal_time (GST_CLOCK (self));
371 len = sizeof (struct sockaddr);
372 packet = gst_net_time_packet_receive (self->priv->sock.fd,
373 (struct sockaddr *) &tmpaddr, &len);
378 GST_LOG_OBJECT (self, "got packet back");
379 GST_LOG_OBJECT (self, "local_1 = %" GST_TIME_FORMAT,
380 GST_TIME_ARGS (packet->local_time));
381 GST_LOG_OBJECT (self, "remote = %" GST_TIME_FORMAT,
382 GST_TIME_ARGS (packet->remote_time));
383 GST_LOG_OBJECT (self, "local_2 = %" GST_TIME_FORMAT,
384 GST_TIME_ARGS (new_local));
386 /* observe_times will reset the timeout */
387 gst_net_client_clock_observe_times (self, packet->local_time,
388 packet->remote_time, new_local);
393 GST_WARNING_OBJECT (self, "unhandled select return state?");
397 g_assert_not_reached ();
401 GST_DEBUG_OBJECT (self, "shutting down");
402 /* socket gets closed in _stop() */
407 GST_WARNING_OBJECT (self, "receive error");
411 g_assert_not_reached ();
415 g_assert_not_reached ();
421 gst_net_client_clock_start (GstNetClientClock * self)
423 struct sockaddr_in servaddr, myaddr;
426 GError *error = NULL;
428 g_return_val_if_fail (self->address != NULL, FALSE);
429 g_return_val_if_fail (self->servaddr == NULL, FALSE);
431 if ((ret = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
434 self->priv->sock.fd = ret;
436 len = sizeof (myaddr);
437 ret = getsockname (self->priv->sock.fd, (struct sockaddr *) &myaddr, &len);
439 goto getsockname_error;
441 memset (&servaddr, 0, sizeof (servaddr));
442 servaddr.sin_family = AF_INET; /* host byte order */
443 servaddr.sin_port = htons (self->port); /* short, network byte order */
445 GST_DEBUG_OBJECT (self, "socket opened on UDP port %hd",
446 ntohs (servaddr.sin_port));
448 if (!inet_aton (self->address, &servaddr.sin_addr))
451 self->servaddr = g_malloc (sizeof (struct sockaddr_in));
452 memcpy (self->servaddr, &servaddr, sizeof (servaddr));
454 GST_DEBUG_OBJECT (self, "will communicate with %s:%d", self->address,
457 gst_poll_add_fd (self->priv->fdset, &self->priv->sock);
458 gst_poll_fd_ctl_read (self->priv->fdset, &self->priv->sock, TRUE);
460 #if !GLIB_CHECK_VERSION (2, 31, 0)
461 self->thread = g_thread_create (gst_net_client_clock_thread, self, TRUE,
464 self->thread = g_thread_try_new ("GstNetClientClock",
465 gst_net_client_clock_thread, self, &error);
476 GST_ERROR_OBJECT (self, "socket failed %d: %s (%d)", ret,
477 g_strerror (errno), errno);
482 GST_ERROR_OBJECT (self, "getsockname failed %d: %s (%d)", ret,
483 g_strerror (errno), errno);
484 close (self->priv->sock.fd);
485 self->priv->sock.fd = -1;
490 GST_ERROR_OBJECT (self, "inet_aton failed %d: %s (%d)", ret,
491 g_strerror (errno), errno);
492 close (self->priv->sock.fd);
493 self->priv->sock.fd = -1;
498 GST_ERROR_OBJECT (self, "could not create thread: %s", error->message);
499 gst_poll_remove_fd (self->priv->fdset, &self->priv->sock);
500 close (self->priv->sock.fd);
501 self->priv->sock.fd = -1;
502 g_free (self->servaddr);
503 self->servaddr = NULL;
504 g_error_free (error);
510 gst_net_client_clock_stop (GstNetClientClock * self)
512 gst_poll_set_flushing (self->priv->fdset, TRUE);
513 g_thread_join (self->thread);
516 if (self->priv->sock.fd != -1) {
517 gst_poll_remove_fd (self->priv->fdset, &self->priv->sock);
518 close (self->priv->sock.fd);
519 self->priv->sock.fd = -1;
524 * gst_net_client_clock_new:
525 * @name: a name for the clock
526 * @remote_address: the address of the remote clock provider
527 * @remote_port: the port of the remote clock provider
528 * @base_time: initial time of the clock
530 * Create a new #GstNetClientClock that will report the time
531 * provided by the #GstNetTimeProvider on @remote_address and
534 * Returns: a new #GstClock that receives a time from the remote
538 gst_net_client_clock_new (gchar * name, const gchar * remote_address,
539 gint remote_port, GstClockTime base_time)
541 GstNetClientClock *ret;
542 GstClockTime internal;
544 g_return_val_if_fail (remote_address != NULL, NULL);
545 g_return_val_if_fail (remote_port > 0, NULL);
546 g_return_val_if_fail (remote_port <= G_MAXUINT16, NULL);
547 g_return_val_if_fail (base_time != GST_CLOCK_TIME_NONE, NULL);
549 ret = g_object_new (GST_TYPE_NET_CLIENT_CLOCK, "address", remote_address,
550 "port", remote_port, NULL);
552 /* gst_clock_get_time() values are guaranteed to be increasing. because no one
553 * has called get_time on this clock yet we are free to adjust to any value
554 * without worrying about worrying about MAX() issues with the clock's
558 /* update our internal time so get_time() give something around base_time.
559 assume that the rate is 1 in the beginning. */
560 internal = gst_clock_get_internal_time (GST_CLOCK (ret));
561 gst_clock_set_calibration (GST_CLOCK (ret), internal, base_time, 1, 1);
564 GstClockTime now = gst_clock_get_time (GST_CLOCK (ret));
566 if (GST_CLOCK_DIFF (now, base_time) > 0 ||
567 GST_CLOCK_DIFF (now, base_time + GST_SECOND) < 0) {
568 g_warning ("unable to set the base time, expect sync problems!");
572 if ((ret->priv->fdset = gst_poll_new (TRUE)) == NULL)
575 if (!gst_net_client_clock_start (ret))
578 /* all systems go, cap'n */
579 return (GstClock *) ret;
583 GST_ERROR_OBJECT (ret, "could not create an fdset: %s (%d)",
584 g_strerror (errno), errno);
585 gst_object_unref (ret);
590 /* already printed a nice error */
591 gst_object_unref (ret);