Initialize Tizen 2.3
[framework/multimedia/gstreamer0.10.git] / wearable / libs / gst / net / gstnetclientclock.c
1 /* GStreamer
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>
5  *
6  * gstnetclientclock.h: clock that synchronizes itself to a time provider over
7  * the network
8  *
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.
13  *
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.
18  *
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.
23  */
24 /**
25  * SECTION:gstnetclientclock
26  * @short_description: Special clock that synchronizes to a remote time
27  *                     provider.
28  * @see_also: #GstClock, #GstNetTimeProvider, #GstPipeline
29  *
30  * This object implements a custom #GstClock that synchronizes its time
31  * to a remote time provider such as #GstNetTimeProvider.
32  *
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
35  * an initial time.
36  *
37  * This clock will poll the time provider and will update its calibration
38  * parameters based on the local and remote observations.
39  *
40  * Various parameters of the clock can be configured with the parent #GstClock
41  * "timeout", "window-size" and "window-threshold" object properties.
42  *
43  * A #GstNetClientClock is typically set on a #GstPipeline with 
44  * gst_pipeline_use_clock().
45  *
46  * Last reviewed on 2005-11-23 (0.9.5)
47  */
48
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52
53 #include "gstnettimepacket.h"
54 #include "gstnetclientclock.h"
55
56 #ifdef HAVE_UNISTD_H
57 #include <unistd.h>
58 #endif
59
60 #if defined (_MSC_VER) && _MSC_VER >= 1400
61 #include <io.h>
62 #endif
63
64 GST_DEBUG_CATEGORY_STATIC (ncc_debug);
65 #define GST_CAT_DEFAULT (ncc_debug)
66
67 #define DEFAULT_ADDRESS         "127.0.0.1"
68 #define DEFAULT_PORT            5637
69 #define DEFAULT_TIMEOUT         GST_SECOND
70
71 #ifdef G_OS_WIN32
72 #define getsockname(sock,addr,len) getsockname(sock,addr,(int *)len)
73 #endif
74
75 enum
76 {
77   PROP_0,
78   PROP_ADDRESS,
79   PROP_PORT,
80 };
81
82 #define GST_NET_CLIENT_CLOCK_GET_PRIVATE(obj)  \
83   (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_NET_CLIENT_CLOCK, GstNetClientClockPrivate))
84
85 struct _GstNetClientClockPrivate
86 {
87   GstPollFD sock;
88   GstPoll *fdset;
89 };
90
91 #define _do_init(type) \
92   GST_DEBUG_CATEGORY_INIT (ncc_debug, "netclock", 0, "Network client clock");
93
94 GST_BOILERPLATE_FULL (GstNetClientClock, gst_net_client_clock,
95     GstSystemClock, GST_TYPE_SYSTEM_CLOCK, _do_init);
96
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);
102
103 static void gst_net_client_clock_stop (GstNetClientClock * self);
104
105 #ifdef G_OS_WIN32
106 static int
107 inet_aton (const char *c, struct in_addr *paddr)
108 {
109   /* note that inet_addr is deprecated on unix because
110    * inet_addr returns -1 (INADDR_NONE) for the valid 255.255.255.255
111    * address. */
112   paddr->s_addr = inet_addr (c);
113   if (paddr->s_addr == INADDR_NONE)
114     return 0;
115
116   return 1;
117 }
118 #endif
119
120 static void
121 gst_net_client_clock_base_init (gpointer g_class)
122 {
123   /* nop */
124 }
125
126 static void
127 gst_net_client_clock_class_init (GstNetClientClockClass * klass)
128 {
129   GObjectClass *gobject_class;
130
131   gobject_class = G_OBJECT_CLASS (klass);
132
133   g_type_class_add_private (klass, sizeof (GstNetClientClockPrivate));
134
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;
138
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));
148 }
149
150 static void
151 gst_net_client_clock_init (GstNetClientClock * self,
152     GstNetClientClockClass * g_class)
153 {
154   GstClock *clock = GST_CLOCK_CAST (self);
155
156 #ifdef G_OS_WIN32
157   WSADATA w;
158   int error = WSAStartup (0x0202, &w);
159
160   if (error) {
161     GST_DEBUG_OBJECT (self, "Error on WSAStartup");
162   }
163   if (w.wVersion != 0x0202) {
164     WSACleanup ();
165   }
166 #endif
167   self->priv = GST_NET_CLIENT_CLOCK_GET_PRIVATE (self);
168
169   self->port = DEFAULT_PORT;
170   self->address = g_strdup (DEFAULT_ADDRESS);
171
172   clock->timeout = DEFAULT_TIMEOUT;
173
174   self->priv->sock.fd = -1;
175   self->thread = NULL;
176
177   self->servaddr = NULL;
178 }
179
180 static void
181 gst_net_client_clock_finalize (GObject * object)
182 {
183   GstNetClientClock *self = GST_NET_CLIENT_CLOCK (object);
184
185   if (self->thread) {
186     gst_net_client_clock_stop (self);
187     g_assert (self->thread == NULL);
188   }
189
190   if (self->priv->fdset) {
191     gst_poll_free (self->priv->fdset);
192     self->priv->fdset = NULL;
193   }
194
195   g_free (self->address);
196   self->address = NULL;
197
198   g_free (self->servaddr);
199   self->servaddr = NULL;
200
201 #ifdef G_OS_WIN32
202   WSACleanup ();
203 #endif
204
205   G_OBJECT_CLASS (parent_class)->finalize (object);
206 }
207
208 static void
209 gst_net_client_clock_set_property (GObject * object, guint prop_id,
210     const GValue * value, GParamSpec * pspec)
211 {
212   GstNetClientClock *self = GST_NET_CLIENT_CLOCK (object);
213
214   switch (prop_id) {
215     case PROP_ADDRESS:
216       g_free (self->address);
217       if (g_value_get_string (value) == NULL)
218         self->address = g_strdup (DEFAULT_ADDRESS);
219       else
220         self->address = g_strdup (g_value_get_string (value));
221       break;
222     case PROP_PORT:
223       self->port = g_value_get_int (value);
224       break;
225     default:
226       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227       break;
228   }
229 }
230
231 static void
232 gst_net_client_clock_get_property (GObject * object, guint prop_id,
233     GValue * value, GParamSpec * pspec)
234 {
235   GstNetClientClock *self = GST_NET_CLIENT_CLOCK (object);
236
237   switch (prop_id) {
238     case PROP_ADDRESS:
239       g_value_set_string (value, self->address);
240       break;
241     case PROP_PORT:
242       g_value_set_int (value, self->port);
243       break;
244     default:
245       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
246       break;
247   }
248 }
249
250 static void
251 gst_net_client_clock_observe_times (GstNetClientClock * self,
252     GstClockTime local_1, GstClockTime remote, GstClockTime local_2)
253 {
254   GstClockTime local_avg;
255   gdouble r_squared;
256   GstClock *clock;
257
258   if (local_2 < local_1)
259     goto bogus_observation;
260
261   local_avg = (local_2 + local_1) / 2;
262
263   clock = GST_CLOCK_CAST (self);
264
265   gst_clock_add_observation (GST_CLOCK (self), local_avg, remote, &r_squared);
266
267   GST_CLOCK_SLAVE_LOCK (self);
268   if (clock->filling) {
269     self->current_timeout = 0;
270   } else {
271     /* geto formula */
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);
275   }
276   GST_CLOCK_SLAVE_UNLOCK (clock);
277
278   return;
279
280 bogus_observation:
281   {
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));
285     return;
286   }
287 }
288
289 static gint
290 gst_net_client_clock_do_select (GstNetClientClock * self)
291 {
292   while (TRUE) {
293     GstClockTime diff;
294     gint ret;
295
296     GST_LOG_OBJECT (self, "doing select");
297
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;
301
302     if (diff > self->current_timeout)
303       self->current_timeout = 0;
304     else
305       self->current_timeout -= diff;
306
307     GST_LOG_OBJECT (self, "select returned %d", ret);
308
309     if (ret < 0 && errno != EBUSY) {
310       if (errno != EAGAIN && errno != EINTR)
311         goto select_error;
312       else
313         continue;
314     } else {
315       return ret;
316     }
317
318     g_assert_not_reached ();
319
320     /* log errors and keep going */
321   select_error:
322     {
323       GST_WARNING_OBJECT (self, "select error %d: %s (%d)", ret,
324           g_strerror (errno), errno);
325       continue;
326     }
327   }
328
329   g_assert_not_reached ();
330   return -1;
331 }
332
333 static gpointer
334 gst_net_client_clock_thread (gpointer data)
335 {
336   GstNetClientClock *self = data;
337   struct sockaddr_in tmpaddr;
338   socklen_t len;
339   GstNetTimePacket *packet;
340   gint ret;
341   GstClock *clock = data;
342
343   while (TRUE) {
344     ret = gst_net_client_clock_do_select (self);
345
346     if (ret < 0 && errno == EBUSY) {
347       GST_LOG_OBJECT (self, "stop");
348       goto stopped;
349     } else if (ret == 0) {
350       /* timed out, let's send another packet */
351       GST_DEBUG_OBJECT (self, "timed out");
352
353       packet = gst_net_time_packet_new (NULL);
354
355       packet->local_time = gst_clock_get_internal_time (GST_CLOCK (self));
356
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));
361
362       g_free (packet);
363
364       /* reset timeout */
365       self->current_timeout = clock->timeout;
366       continue;
367     } else if (gst_poll_fd_can_read (self->priv->fdset, &self->priv->sock)) {
368       /* got data in */
369       GstClockTime new_local = gst_clock_get_internal_time (GST_CLOCK (self));
370
371       len = sizeof (struct sockaddr);
372       packet = gst_net_time_packet_receive (self->priv->sock.fd,
373           (struct sockaddr *) &tmpaddr, &len);
374
375       if (!packet)
376         goto receive_error;
377
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));
385
386       /* observe_times will reset the timeout */
387       gst_net_client_clock_observe_times (self, packet->local_time,
388           packet->remote_time, new_local);
389
390       g_free (packet);
391       continue;
392     } else {
393       GST_WARNING_OBJECT (self, "unhandled select return state?");
394       continue;
395     }
396
397     g_assert_not_reached ();
398
399   stopped:
400     {
401       GST_DEBUG_OBJECT (self, "shutting down");
402       /* socket gets closed in _stop() */
403       return NULL;
404     }
405   receive_error:
406     {
407       GST_WARNING_OBJECT (self, "receive error");
408       continue;
409     }
410
411     g_assert_not_reached ();
412
413   }
414
415   g_assert_not_reached ();
416
417   return NULL;
418 }
419
420 static gboolean
421 gst_net_client_clock_start (GstNetClientClock * self)
422 {
423   struct sockaddr_in servaddr, myaddr;
424   socklen_t len;
425   gint ret;
426   GError *error = NULL;
427
428   g_return_val_if_fail (self->address != NULL, FALSE);
429   g_return_val_if_fail (self->servaddr == NULL, FALSE);
430
431   if ((ret = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
432     goto no_socket;
433
434   self->priv->sock.fd = ret;
435
436   len = sizeof (myaddr);
437   ret = getsockname (self->priv->sock.fd, (struct sockaddr *) &myaddr, &len);
438   if (ret < 0)
439     goto getsockname_error;
440
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 */
444
445   GST_DEBUG_OBJECT (self, "socket opened on UDP port %hd",
446       ntohs (servaddr.sin_port));
447
448   if (!inet_aton (self->address, &servaddr.sin_addr))
449     goto bad_address;
450
451   self->servaddr = g_malloc (sizeof (struct sockaddr_in));
452   memcpy (self->servaddr, &servaddr, sizeof (servaddr));
453
454   GST_DEBUG_OBJECT (self, "will communicate with %s:%d", self->address,
455       self->port);
456
457   gst_poll_add_fd (self->priv->fdset, &self->priv->sock);
458   gst_poll_fd_ctl_read (self->priv->fdset, &self->priv->sock, TRUE);
459
460 #if !GLIB_CHECK_VERSION (2, 31, 0)
461   self->thread = g_thread_create (gst_net_client_clock_thread, self, TRUE,
462       &error);
463 #else
464   self->thread = g_thread_try_new ("GstNetClientClock",
465       gst_net_client_clock_thread, self, &error);
466 #endif
467
468   if (error != NULL)
469     goto no_thread;
470
471   return TRUE;
472
473   /* ERRORS */
474 no_socket:
475   {
476     GST_ERROR_OBJECT (self, "socket failed %d: %s (%d)", ret,
477         g_strerror (errno), errno);
478     return FALSE;
479   }
480 getsockname_error:
481   {
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;
486     return FALSE;
487   }
488 bad_address:
489   {
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;
494     return FALSE;
495   }
496 no_thread:
497   {
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);
505     return FALSE;
506   }
507 }
508
509 static void
510 gst_net_client_clock_stop (GstNetClientClock * self)
511 {
512   gst_poll_set_flushing (self->priv->fdset, TRUE);
513   g_thread_join (self->thread);
514   self->thread = NULL;
515
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;
520   }
521 }
522
523 /**
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
529  *
530  * Create a new #GstNetClientClock that will report the time
531  * provided by the #GstNetTimeProvider on @remote_address and 
532  * @remote_port.
533  *
534  * Returns: a new #GstClock that receives a time from the remote
535  * clock.
536  */
537 GstClock *
538 gst_net_client_clock_new (gchar * name, const gchar * remote_address,
539     gint remote_port, GstClockTime base_time)
540 {
541   GstNetClientClock *ret;
542   GstClockTime internal;
543
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);
548
549   ret = g_object_new (GST_TYPE_NET_CLIENT_CLOCK, "address", remote_address,
550       "port", remote_port, NULL);
551
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
555    * internal time.
556    */
557
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);
562
563   {
564     GstClockTime now = gst_clock_get_time (GST_CLOCK (ret));
565
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!");
569     }
570   }
571
572   if ((ret->priv->fdset = gst_poll_new (TRUE)) == NULL)
573     goto no_fdset;
574
575   if (!gst_net_client_clock_start (ret))
576     goto failed_start;
577
578   /* all systems go, cap'n */
579   return (GstClock *) ret;
580
581 no_fdset:
582   {
583     GST_ERROR_OBJECT (ret, "could not create an fdset: %s (%d)",
584         g_strerror (errno), errno);
585     gst_object_unref (ret);
586     return NULL;
587   }
588 failed_start:
589   {
590     /* already printed a nice error */
591     gst_object_unref (ret);
592     return NULL;
593   }
594 }