gst_plugins_base/gst/tcp/gsttcpclientsrc.c
branchRCL_3
changeset 30 7e817e7e631c
parent 29 567bb019e3e3
equal deleted inserted replaced
29:567bb019e3e3 30:7e817e7e631c
    16  * License along with this library; if not, write to the
    16  * License along with this library; if not, write to the
    17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    18  * Boston, MA 02111-1307, USA.
    18  * Boston, MA 02111-1307, USA.
    19  */
    19  */
    20 
    20 
    21 /**
       
    22  * SECTION:element-tcpclientsrc
       
    23  * @see_also: #tcpclientsink
       
    24  *
       
    25  * <refsect2>
       
    26  * <title>Example launch line</title>
       
    27  * |[
       
    28  * # server:
       
    29  * nc -l -p 3000
       
    30  * # client:
       
    31  * gst-launch tcpclientsrc protocol=none port=3000 ! fdsink fd=2
       
    32  * ]| everything you type in the server is shown on the client
       
    33  * </refsect2>
       
    34  */
       
    35 
    21 
    36 #ifdef HAVE_CONFIG_H
    22 #ifdef HAVE_CONFIG_H
    37 #include "config.h"
    23 #include "config.h"
    38 #endif
    24 #endif
    39 
    25 
       
    26 #ifdef __SYMBIAN32__
       
    27 #include "gst/gst-i18n-plugin.h"
       
    28 #else
    40 #include <gst/gst-i18n-plugin.h>
    29 #include <gst/gst-i18n-plugin.h>
       
    30 #endif
    41 #include "gsttcp.h"
    31 #include "gsttcp.h"
    42 #include "gsttcpclientsrc.h"
    32 #include "gsttcpclientsrc.h"
    43 #include <string.h>             /* memset */
    33 #include <string.h>             /* memset */
    44 #include <unistd.h>
    34 #include <unistd.h>
    45 #include <arpa/inet.h>
    35 #include <arpa/inet.h>
   120   gobject_class->finalize = gst_tcp_client_src_finalize;
   110   gobject_class->finalize = gst_tcp_client_src_finalize;
   121 
   111 
   122   g_object_class_install_property (gobject_class, PROP_HOST,
   112   g_object_class_install_property (gobject_class, PROP_HOST,
   123       g_param_spec_string ("host", "Host",
   113       g_param_spec_string ("host", "Host",
   124           "The host IP address to receive packets from", TCP_DEFAULT_HOST,
   114           "The host IP address to receive packets from", TCP_DEFAULT_HOST,
   125           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   115           G_PARAM_READWRITE));
   126   g_object_class_install_property (gobject_class, PROP_PORT,
   116   g_object_class_install_property (gobject_class, PROP_PORT,
   127       g_param_spec_int ("port", "Port", "The port to receive packets from", 0,
   117       g_param_spec_int ("port", "Port", "The port to receive packets from", 0,
   128           TCP_HIGHEST_PORT, TCP_DEFAULT_PORT,
   118           TCP_HIGHEST_PORT, TCP_DEFAULT_PORT, G_PARAM_READWRITE));
   129           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
       
   130   g_object_class_install_property (gobject_class, PROP_PROTOCOL,
   119   g_object_class_install_property (gobject_class, PROP_PROTOCOL,
   131       g_param_spec_enum ("protocol", "Protocol", "The protocol to wrap data in",
   120       g_param_spec_enum ("protocol", "Protocol", "The protocol to wrap data in",
   132           GST_TYPE_TCP_PROTOCOL, GST_TCP_PROTOCOL_NONE,
   121           GST_TYPE_TCP_PROTOCOL, GST_TCP_PROTOCOL_NONE, G_PARAM_READWRITE));
   133           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
       
   134 
   122 
   135   gstbasesrc_class->get_caps = gst_tcp_client_src_getcaps;
   123   gstbasesrc_class->get_caps = gst_tcp_client_src_getcaps;
   136   gstbasesrc_class->start = gst_tcp_client_src_start;
   124   gstbasesrc_class->start = gst_tcp_client_src_start;
   137   gstbasesrc_class->stop = gst_tcp_client_src_stop;
   125   gstbasesrc_class->stop = gst_tcp_client_src_stop;
   138   gstbasesrc_class->unlock = gst_tcp_client_src_unlock;
   126   gstbasesrc_class->unlock = gst_tcp_client_src_unlock;
   149   this->port = TCP_DEFAULT_PORT;
   137   this->port = TCP_DEFAULT_PORT;
   150   this->host = g_strdup (TCP_DEFAULT_HOST);
   138   this->host = g_strdup (TCP_DEFAULT_HOST);
   151   this->sock_fd.fd = -1;
   139   this->sock_fd.fd = -1;
   152   this->protocol = GST_TCP_PROTOCOL_NONE;
   140   this->protocol = GST_TCP_PROTOCOL_NONE;
   153   this->caps = NULL;
   141   this->caps = NULL;
       
   142 
       
   143   gst_base_src_set_live (GST_BASE_SRC (this), TRUE);
   154 
   144 
   155   GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SRC_OPEN);
   145   GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SRC_OPEN);
   156 }
   146 }
   157 
   147 
   158 static void
   148 static void
   344   g_free (ip);
   334   g_free (ip);
   345 
   335 
   346   GST_DEBUG_OBJECT (src, "connecting to server");
   336   GST_DEBUG_OBJECT (src, "connecting to server");
   347   ret = connect (src->sock_fd.fd, (struct sockaddr *) &src->server_sin,
   337   ret = connect (src->sock_fd.fd, (struct sockaddr *) &src->server_sin,
   348       sizeof (src->server_sin));
   338       sizeof (src->server_sin));
   349   if (ret)
   339 
   350     goto connect_failed;
   340   if (ret) {
   351 
   341     gst_tcp_client_src_stop (GST_BASE_SRC (src));
   352   /* add the socket to the poll */
   342     switch (errno) {
   353   gst_poll_add_fd (src->fdset, &src->sock_fd);
   343       case ECONNREFUSED:
   354   gst_poll_fd_ctl_read (src->fdset, &src->sock_fd, TRUE);
   344         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
       
   345             (_("Connection to %s:%d refused."), src->host, src->port), (NULL));
       
   346         return FALSE;
       
   347         break;
       
   348       default:
       
   349         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
       
   350             ("connect to %s:%d failed: %s", src->host, src->port,
       
   351                 g_strerror (errno)));
       
   352         return FALSE;
       
   353         break;
       
   354     }
       
   355   }
   355 
   356 
   356   return TRUE;
   357   return TRUE;
   357 
   358 
   358 socket_pair:
   359 socket_pair:
   359   {
   360   {
   369 name_resolv:
   370 name_resolv:
   370   {
   371   {
   371     gst_tcp_client_src_stop (GST_BASE_SRC (src));
   372     gst_tcp_client_src_stop (GST_BASE_SRC (src));
   372     return FALSE;
   373     return FALSE;
   373   }
   374   }
   374 connect_failed:
       
   375   {
       
   376     gst_tcp_client_src_stop (GST_BASE_SRC (src));
       
   377     switch (errno) {
       
   378       case ECONNREFUSED:
       
   379         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
       
   380             (_("Connection to %s:%d refused."), src->host, src->port), (NULL));
       
   381         break;
       
   382       default:
       
   383         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
       
   384             ("connect to %s:%d failed: %s", src->host, src->port,
       
   385                 g_strerror (errno)));
       
   386         break;
       
   387     }
       
   388     return FALSE;
       
   389   }
       
   390 }
   375 }
   391 
   376 
   392 /* close the socket and associated resources
   377 /* close the socket and associated resources
   393  * unset OPEN flag
   378  * unset OPEN flag
   394  * used both to recover from errors and go to NULL state */
   379  * used both to recover from errors and go to NULL state */