]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blobdiff - libs/gst/net/gstnetclientclock.c
DEBIAN: Debianization
[glsdk/gstreamer0-10.git] / libs / gst / net / gstnetclientclock.c
index 687dc48fb2ff6509a67a1eb698d8d8d17a94dc96..9a0e397db2ec5f6967e937d40eba8233d358e08e 100644 (file)
 GST_DEBUG_CATEGORY_STATIC (ncc_debug);
 #define GST_CAT_DEFAULT (ncc_debug)
 
-/* the select call is also performed on the control sockets, that way
- * we can send special commands to unblock or restart the select call */
-#define CONTROL_RESTART        'R'      /* restart the select call */
-#define CONTROL_STOP           'S'      /* stop the select call */
-#define CONTROL_SOCKETS(self)   self->control_sock
-#define WRITE_SOCKET(self)      self->control_sock[1]
-#define READ_SOCKET(self)       self->control_sock[0]
-
-#define SEND_COMMAND(self, command)                     \
-G_STMT_START {                                  \
-  unsigned char c; c = command;                 \
-  write (WRITE_SOCKET(self), &c, 1);            \
-} G_STMT_END
-
-#define READ_COMMAND(self, command, res)                \
-G_STMT_START {                                  \
-  res = read(READ_SOCKET(self), &command, 1);    \
-} G_STMT_END
-
 #define DEFAULT_ADDRESS         "127.0.0.1"
 #define DEFAULT_PORT            5637
 #define DEFAULT_TIMEOUT         GST_SECOND
@@ -98,6 +79,15 @@ enum
   PROP_PORT,
 };
 
+#define GST_NET_CLIENT_CLOCK_GET_PRIVATE(obj)  \
+  (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_NET_CLIENT_CLOCK, GstNetClientClockPrivate))
+
+struct _GstNetClientClockPrivate
+{
+  GstPollFD sock;
+  GstPoll *fdset;
+};
+
 #define _do_init(type) \
   GST_DEBUG_CATEGORY_INIT (ncc_debug, "netclock", 0, "Network client clock");
 
@@ -140,6 +130,8 @@ gst_net_client_clock_class_init (GstNetClientClockClass * klass)
 
   gobject_class = G_OBJECT_CLASS (klass);
 
+  g_type_class_add_private (klass, sizeof (GstNetClientClockPrivate));
+
   gobject_class->finalize = gst_net_client_clock_finalize;
   gobject_class->get_property = gst_net_client_clock_get_property;
   gobject_class->set_property = gst_net_client_clock_set_property;
@@ -147,11 +139,12 @@ gst_net_client_clock_class_init (GstNetClientClockClass * klass)
   g_object_class_install_property (gobject_class, PROP_ADDRESS,
       g_param_spec_string ("address", "address",
           "The address of the machine providing a time server, "
-          "as a dotted quad (x.x.x.x)", DEFAULT_ADDRESS, G_PARAM_READWRITE));
+          "as a dotted quad (x.x.x.x)", DEFAULT_ADDRESS,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_object_class_install_property (gobject_class, PROP_PORT,
       g_param_spec_int ("port", "port",
           "The port on which the remote server is listening", 0, G_MAXUINT16,
-          DEFAULT_PORT, G_PARAM_READWRITE));
+          DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 }
 
 static void
@@ -171,19 +164,17 @@ gst_net_client_clock_init (GstNetClientClock * self,
     WSACleanup ();
   }
 #endif
+  self->priv = GST_NET_CLIENT_CLOCK_GET_PRIVATE (self);
 
   self->port = DEFAULT_PORT;
   self->address = g_strdup (DEFAULT_ADDRESS);
 
   clock->timeout = DEFAULT_TIMEOUT;
 
-  self->sock = -1;
+  self->priv->sock.fd = -1;
   self->thread = NULL;
 
   self->servaddr = NULL;
-
-  READ_SOCKET (self) = -1;
-  WRITE_SOCKET (self) = -1;
 }
 
 static void
@@ -196,11 +187,9 @@ gst_net_client_clock_finalize (GObject * object)
     g_assert (self->thread == NULL);
   }
 
-  if (READ_SOCKET (self) != -1) {
-    close (READ_SOCKET (self));
-    close (WRITE_SOCKET (self));
-    READ_SOCKET (self) = -1;
-    WRITE_SOCKET (self) = -1;
+  if (self->priv->fdset) {
+    gst_poll_free (self->priv->fdset);
+    self->priv->fdset = NULL;
   }
 
   g_free (self->address);
@@ -298,46 +287,26 @@ bogus_observation:
 }
 
 static gint
-gst_net_client_clock_do_select (GstNetClientClock * self, fd_set * readfds)
+gst_net_client_clock_do_select (GstNetClientClock * self)
 {
-  gint max_sock;
-  gint ret;
-
   while (TRUE) {
-    FD_ZERO (readfds);
-    FD_SET (self->sock, readfds);
-    FD_SET (READ_SOCKET (self), readfds);
-    max_sock = MAX (self->sock, READ_SOCKET (self));
+    GstClockTime diff;
+    gint ret;
 
     GST_LOG_OBJECT (self, "doing select");
-    {
-      GstClockTime diff;
-      GTimeVal tv, *ptv = &tv;
 
-      diff = gst_clock_get_internal_time (GST_CLOCK (self));
-      GST_TIME_TO_TIMEVAL (self->current_timeout, tv);
+    diff = gst_clock_get_internal_time (GST_CLOCK (self));
+    ret = gst_poll_wait (self->priv->fdset, self->current_timeout);
+    diff = gst_clock_get_internal_time (GST_CLOCK (self)) - diff;
 
-#ifdef G_OS_WIN32
-      if (((max_sock + 1) != READ_SOCKET (self)) ||
-          ((max_sock + 1) != WRITE_SOCKET (self))) {
-        ret =
-            select (max_sock + 1, readfds, NULL, NULL, (struct timeval *) ptv);
-      } else {
-        ret = 1;
-      }
-#else
-      ret = select (max_sock + 1, readfds, NULL, NULL, (struct timeval *) ptv);
-#endif
-      diff = gst_clock_get_internal_time (GST_CLOCK (self)) - diff;
+    if (diff > self->current_timeout)
+      self->current_timeout = 0;
+    else
+      self->current_timeout -= diff;
 
-      if (diff > self->current_timeout)
-        self->current_timeout = 0;
-      else
-        self->current_timeout -= diff;
-    }
     GST_LOG_OBJECT (self, "select returned %d", ret);
 
-    if (ret < 0) {
+    if (ret < 0 && errno != EBUSY) {
       if (errno != EAGAIN && errno != EINTR)
         goto select_error;
       else
@@ -367,42 +336,16 @@ gst_net_client_clock_thread (gpointer data)
   GstNetClientClock *self = data;
   struct sockaddr_in tmpaddr;
   socklen_t len;
-  fd_set read_fds;
   GstNetTimePacket *packet;
   gint ret;
   GstClock *clock = data;
 
   while (TRUE) {
-    ret = gst_net_client_clock_do_select (self, &read_fds);
-
-    if (FD_ISSET (READ_SOCKET (self), &read_fds)) {
-      /* got control message */
-      while (TRUE) {
-        gchar command;
-        int res;
-
-        READ_COMMAND (self, command, res);
-        if (res <= 0) {
-          GST_LOG_OBJECT (self, "no more commands");
-          break;
-        }
-
-        GST_LOG_OBJECT (self, "control message: '%c'", command);
-        switch (command) {
-          case CONTROL_STOP:
-            /* break out of the select loop */
-            GST_LOG_OBJECT (self, "stop");
-            goto stopped;
-          default:
-            GST_WARNING_OBJECT (self, "unknown message: '%c'", command);
-            g_warning ("netclientclock: unknown control message received");
-            continue;
-        }
-
-        g_assert_not_reached ();
-      }
+    ret = gst_net_client_clock_do_select (self);
 
-      continue;
+    if (ret < 0 && errno == EBUSY) {
+      GST_LOG_OBJECT (self, "stop");
+      goto stopped;
     } else if (ret == 0) {
       /* timed out, let's send another packet */
       GST_DEBUG_OBJECT (self, "timed out");
@@ -413,7 +356,7 @@ gst_net_client_clock_thread (gpointer data)
 
       GST_DEBUG_OBJECT (self, "sending packet, local time = %" GST_TIME_FORMAT,
           GST_TIME_ARGS (packet->local_time));
-      gst_net_time_packet_send (packet, self->sock,
+      gst_net_time_packet_send (packet, self->priv->sock.fd,
           (struct sockaddr *) self->servaddr, sizeof (struct sockaddr_in));
 
       g_free (packet);
@@ -421,12 +364,12 @@ gst_net_client_clock_thread (gpointer data)
       /* reset timeout */
       self->current_timeout = clock->timeout;
       continue;
-    } else if (FD_ISSET (self->sock, &read_fds)) {
+    } else if (gst_poll_fd_can_read (self->priv->fdset, &self->priv->sock)) {
       /* got data in */
       GstClockTime new_local = gst_clock_get_internal_time (GST_CLOCK (self));
 
       len = sizeof (struct sockaddr);
-      packet = gst_net_time_packet_receive (self->sock,
+      packet = gst_net_time_packet_receive (self->priv->sock.fd,
           (struct sockaddr *) &tmpaddr, &len);
 
       if (!packet)
@@ -480,7 +423,7 @@ gst_net_client_clock_start (GstNetClientClock * self)
   struct sockaddr_in servaddr, myaddr;
   socklen_t len;
   gint ret;
-  GError *error;
+  GError *error = NULL;
 
   g_return_val_if_fail (self->address != NULL, FALSE);
   g_return_val_if_fail (self->servaddr == NULL, FALSE);
@@ -488,10 +431,10 @@ gst_net_client_clock_start (GstNetClientClock * self)
   if ((ret = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
     goto no_socket;
 
-  self->sock = ret;
+  self->priv->sock.fd = ret;
 
   len = sizeof (myaddr);
-  ret = getsockname (self->sock, (struct sockaddr *) &myaddr, &len);
+  ret = getsockname (self->priv->sock.fd, (struct sockaddr *) &myaddr, &len);
   if (ret < 0)
     goto getsockname_error;
 
@@ -511,9 +454,18 @@ gst_net_client_clock_start (GstNetClientClock * self)
   GST_DEBUG_OBJECT (self, "will communicate with %s:%d", self->address,
       self->port);
 
+  gst_poll_add_fd (self->priv->fdset, &self->priv->sock);
+  gst_poll_fd_ctl_read (self->priv->fdset, &self->priv->sock, TRUE);
+
+#if !GLIB_CHECK_VERSION (2, 31, 0)
   self->thread = g_thread_create (gst_net_client_clock_thread, self, TRUE,
       &error);
-  if (!self->thread)
+#else
+  self->thread = g_thread_try_new ("GstNetClientClock",
+      gst_net_client_clock_thread, self, &error);
+#endif
+
+  if (error != NULL)
     goto no_thread;
 
   return TRUE;
@@ -529,23 +481,24 @@ getsockname_error:
   {
     GST_ERROR_OBJECT (self, "getsockname failed %d: %s (%d)", ret,
         g_strerror (errno), errno);
-    close (self->sock);
-    self->sock = -1;
+    close (self->priv->sock.fd);
+    self->priv->sock.fd = -1;
     return FALSE;
   }
 bad_address:
   {
     GST_ERROR_OBJECT (self, "inet_aton failed %d: %s (%d)", ret,
         g_strerror (errno), errno);
-    close (self->sock);
-    self->sock = -1;
+    close (self->priv->sock.fd);
+    self->priv->sock.fd = -1;
     return FALSE;
   }
 no_thread:
   {
     GST_ERROR_OBJECT (self, "could not create thread: %s", error->message);
-    close (self->sock);
-    self->sock = -1;
+    gst_poll_remove_fd (self->priv->fdset, &self->priv->sock);
+    close (self->priv->sock.fd);
+    self->priv->sock.fd = -1;
     g_free (self->servaddr);
     self->servaddr = NULL;
     g_error_free (error);
@@ -556,13 +509,14 @@ no_thread:
 static void
 gst_net_client_clock_stop (GstNetClientClock * self)
 {
-  SEND_COMMAND (self, CONTROL_STOP);
+  gst_poll_set_flushing (self->priv->fdset, TRUE);
   g_thread_join (self->thread);
   self->thread = NULL;
 
-  if (self->sock != -1) {
-    close (self->sock);
-    self->sock = -1;
+  if (self->priv->sock.fd != -1) {
+    gst_poll_remove_fd (self->priv->fdset, &self->priv->sock);
+    close (self->priv->sock.fd);
+    self->priv->sock.fd = -1;
   }
 }
 
@@ -574,7 +528,7 @@ gst_net_client_clock_stop (GstNetClientClock * self)
  * @base_time: initial time of the clock
  *
  * Create a new #GstNetClientClock that will report the time
- * provided by the #GstNetClockProvider on @remote_address and 
+ * provided by the #GstNetTimeProvider on @remote_address and 
  * @remote_port.
  *
  * Returns: a new #GstClock that receives a time from the remote
@@ -586,7 +540,6 @@ gst_net_client_clock_new (gchar * name, const gchar * remote_address,
 {
   GstNetClientClock *ret;
   GstClockTime internal;
-  gint iret;
 
   g_return_val_if_fail (remote_address != NULL, NULL);
   g_return_val_if_fail (remote_port > 0, NULL);
@@ -610,22 +563,14 @@ gst_net_client_clock_new (gchar * name, const gchar * remote_address,
   {
     GstClockTime now = gst_clock_get_time (GST_CLOCK (ret));
 
-    if (now < base_time || now > base_time + GST_SECOND)
+    if (GST_CLOCK_DIFF (now, base_time) > 0 ||
+        GST_CLOCK_DIFF (now, base_time + GST_SECOND) < 0) {
       g_warning ("unable to set the base time, expect sync problems!");
+    }
   }
 
-#ifdef G_OS_WIN32
-  GST_DEBUG_OBJECT (ret, "creating pipe");
-  if ((iret = _pipe (CONTROL_SOCKETS (ret), 4096, _O_BINARY)) < 0)
-    goto no_socket_pair;
-#else
-  GST_DEBUG_OBJECT (ret, "creating socket pair");
-  if ((iret = socketpair (PF_UNIX, SOCK_STREAM, 0, CONTROL_SOCKETS (ret))) < 0)
-    goto no_socket_pair;
-
-  fcntl (READ_SOCKET (ret), F_SETFL, O_NONBLOCK);
-  fcntl (WRITE_SOCKET (ret), F_SETFL, O_NONBLOCK);
-#endif
+  if ((ret->priv->fdset = gst_poll_new (TRUE)) == NULL)
+    goto no_fdset;
 
   if (!gst_net_client_clock_start (ret))
     goto failed_start;
@@ -633,9 +578,9 @@ gst_net_client_clock_new (gchar * name, const gchar * remote_address,
   /* all systems go, cap'n */
   return (GstClock *) ret;
 
-no_socket_pair:
+no_fdset:
   {
-    GST_ERROR_OBJECT (ret, "no socket pair %d: %s (%d)", iret,
+    GST_ERROR_OBJECT (ret, "could not create an fdset: %s (%d)",
         g_strerror (errno), errno);
     gst_object_unref (ret);
     return NULL;