rtsp: Added gst_rtsp_connection_create_from_fd().
authorPeter Kjellerstedt <pkj@axis.com>
Tue, 9 Jun 2009 14:22:17 +0000 (16:22 +0200)
committerPeter Kjellerstedt <pkj@axis.com>
Mon, 24 Aug 2009 11:19:44 +0000 (13:19 +0200)
API: gst_rtsp_connection_create_from_fd()

gst-libs/gst/rtsp/gstrtspconnection.c
gst-libs/gst/rtsp/gstrtspconnection.h

index 4acf48a..d807ec2 100644 (file)
@@ -318,44 +318,35 @@ no_fdset:
 }
 
 /**
- * gst_rtsp_connection_accept:
- * @sock: a socket
+ * gst_rtsp_connection_create_from_fd:
+ * @fd: a file descriptor
+ * @ip: the IP address of the other end
+ * @port: the port used by the other end
+ * @initial_buffer: data already read from @fd
  * @conn: storage for a #GstRTSPConnection
  *
- * Accept a new connection on @sock and create a new #GstRTSPConnection for
- * handling communication on new socket.
+ * Create a new #GstRTSPConnection for handling communication on the existing
+ * file descriptor @fd. The @initial_buffer contains any data already read from
+ * @fd which should be used before starting to read new data.
  *
  * Returns: #GST_RTSP_OK when @conn contains a valid connection.
  *
- * Since: 0.10.23
+ * Since: 0.10.25
  */
 GstRTSPResult
-gst_rtsp_connection_accept (gint sock, GstRTSPConnection ** conn)
+gst_rtsp_connection_create_from_fd (gint fd, const gchar * ip, guint16 port,
+    const gchar * initial_buffer, GstRTSPConnection ** conn)
 {
-  int fd;
   GstRTSPConnection *newconn = NULL;
-  union gst_sockaddr sa;
-  socklen_t slen = sizeof (sa);
-  gchar ip[INET6_ADDRSTRLEN];
   GstRTSPUrl *url;
 #ifdef G_OS_WIN32
   gulong flags = 1;
 #endif
+  GstRTSPResult res;
 
-  memset (&sa, 0, slen);
-
-#ifndef G_OS_WIN32
-  fd = accept (sock, &sa.sa, &slen);
-#else
-  fd = accept (sock, &sa.sa, (gint *) & slen);
-#endif /* G_OS_WIN32 */
-  if (fd == -1)
-    goto accept_failed;
-
-  if (getnameinfo (&sa.sa, slen, ip, sizeof (ip), NULL, 0, NI_NUMERICHOST) != 0)
-    goto getnameinfo_failed;
-  if (sa.sa.sa_family != AF_INET && sa.sa.sa_family != AF_INET6)
-    goto wrong_family;
+  g_return_val_if_fail (fd >= 0, GST_RTSP_EINVAL);
+  g_return_val_if_fail (ip != NULL, GST_RTSP_EINVAL);
+  g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
 
   /* set to non-blocking mode so that we can cancel the communication */
 #ifndef G_OS_WIN32
@@ -367,13 +358,10 @@ gst_rtsp_connection_accept (gint sock, GstRTSPConnection ** conn)
   /* create a url for the client address */
   url = g_new0 (GstRTSPUrl, 1);
   url->host = g_strdup (ip);
-  if (sa.sa.sa_family == AF_INET)
-    url->port = sa.sa_in.sin_port;
-  else
-    url->port = sa.sa_in6.sin6_port;
+  url->port = port;
 
   /* now create the connection object */
-  gst_rtsp_connection_create (url, &newconn);
+  GST_RTSP_CHECK (gst_rtsp_connection_create (url, &newconn), newconn_failed);
   gst_rtsp_url_free (url);
 
   ADD_POLLFD (newconn->fdset, &newconn->fd0, fd);
@@ -382,11 +370,69 @@ gst_rtsp_connection_accept (gint sock, GstRTSPConnection ** conn)
   newconn->readfd = &newconn->fd0;
   newconn->writefd = &newconn->fd0;
 
+  newconn->ip = g_strdup (ip);
+
+  newconn->initial_buffer = g_strdup (initial_buffer);
+
   *conn = newconn;
 
   return GST_RTSP_OK;
 
   /* ERRORS */
+newconn_failed:
+  {
+    gst_rtsp_url_free (url);
+    return res;
+  }
+}
+
+/**
+ * gst_rtsp_connection_accept:
+ * @sock: a socket
+ * @conn: storage for a #GstRTSPConnection
+ *
+ * Accept a new connection on @sock and create a new #GstRTSPConnection for
+ * handling communication on new socket.
+ *
+ * Returns: #GST_RTSP_OK when @conn contains a valid connection.
+ *
+ * Since: 0.10.23
+ */
+GstRTSPResult
+gst_rtsp_connection_accept (gint sock, GstRTSPConnection ** conn)
+{
+  int fd;
+  union gst_sockaddr sa;
+  socklen_t slen = sizeof (sa);
+  gchar ip[INET6_ADDRSTRLEN];
+  guint16 port;
+
+  g_return_val_if_fail (sock >= 0, GST_RTSP_EINVAL);
+  g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
+
+  memset (&sa, 0, slen);
+
+#ifndef G_OS_WIN32
+  fd = accept (sock, &sa.sa, &slen);
+#else
+  fd = accept (sock, &sa.sa, (gint *) & slen);
+#endif /* G_OS_WIN32 */
+  if (fd == -1)
+    goto accept_failed;
+
+  if (getnameinfo (&sa.sa, slen, ip, sizeof (ip), NULL, 0, NI_NUMERICHOST) != 0)
+    goto getnameinfo_failed;
+
+  if (sa.sa.sa_family == AF_INET)
+    port = sa.sa_in.sin_port;
+  else if (sa.sa.sa_family == AF_INET6)
+    port = sa.sa_in6.sin6_port;
+  else
+    goto wrong_family;
+
+  return gst_rtsp_connection_create_from_fd (fd, ip, port, NULL, conn);
+
+  /* ERRORS */
 accept_failed:
   {
     return GST_RTSP_ESYS;
index f2c052b..4f95447 100644 (file)
@@ -61,6 +61,11 @@ typedef struct _GstRTSPConnection GstRTSPConnection;
 
 /* opening/closing a connection */
 GstRTSPResult      gst_rtsp_connection_create        (const GstRTSPUrl *url, GstRTSPConnection **conn);
+GstRTSPResult      gst_rtsp_connection_create_from_fd (gint fd,
+                                                       const gchar * ip,
+                                                       guint16 port,
+                                                       const gchar * initial_buffer,
+                                                       GstRTSPConnection ** conn);
 GstRTSPResult      gst_rtsp_connection_accept        (gint sock, GstRTSPConnection **conn);
 GstRTSPResult      gst_rtsp_connection_connect       (GstRTSPConnection *conn, GTimeVal *timeout);
 GstRTSPResult      gst_rtsp_connection_close         (GstRTSPConnection *conn);