gst_plugins_base/gst/tcp/gsttcpclientsrc.c
branchRCL_3
changeset 29 567bb019e3e3
parent 0 0e761a78d257
child 30 7e817e7e631c
equal deleted inserted replaced
6:9b2c3c7a1a9c 29:567bb019e3e3
    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  */
    21 
    35 
    22 #ifdef HAVE_CONFIG_H
    36 #ifdef HAVE_CONFIG_H
    23 #include "config.h"
    37 #include "config.h"
    24 #endif
    38 #endif
    25 
    39 
    26 #ifdef __SYMBIAN32__
       
    27 #include "gst/gst-i18n-plugin.h"
       
    28 #else
       
    29 #include <gst/gst-i18n-plugin.h>
    40 #include <gst/gst-i18n-plugin.h>
    30 #endif
       
    31 #include "gsttcp.h"
    41 #include "gsttcp.h"
    32 #include "gsttcpclientsrc.h"
    42 #include "gsttcpclientsrc.h"
    33 #include <string.h>             /* memset */
    43 #include <string.h>             /* memset */
    34 #include <unistd.h>
    44 #include <unistd.h>
    35 #include <arpa/inet.h>
    45 #include <arpa/inet.h>
   110   gobject_class->finalize = gst_tcp_client_src_finalize;
   120   gobject_class->finalize = gst_tcp_client_src_finalize;
   111 
   121 
   112   g_object_class_install_property (gobject_class, PROP_HOST,
   122   g_object_class_install_property (gobject_class, PROP_HOST,
   113       g_param_spec_string ("host", "Host",
   123       g_param_spec_string ("host", "Host",
   114           "The host IP address to receive packets from", TCP_DEFAULT_HOST,
   124           "The host IP address to receive packets from", TCP_DEFAULT_HOST,
   115           G_PARAM_READWRITE));
   125           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   116   g_object_class_install_property (gobject_class, PROP_PORT,
   126   g_object_class_install_property (gobject_class, PROP_PORT,
   117       g_param_spec_int ("port", "Port", "The port to receive packets from", 0,
   127       g_param_spec_int ("port", "Port", "The port to receive packets from", 0,
   118           TCP_HIGHEST_PORT, TCP_DEFAULT_PORT, G_PARAM_READWRITE));
   128           TCP_HIGHEST_PORT, TCP_DEFAULT_PORT,
       
   129           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   119   g_object_class_install_property (gobject_class, PROP_PROTOCOL,
   130   g_object_class_install_property (gobject_class, PROP_PROTOCOL,
   120       g_param_spec_enum ("protocol", "Protocol", "The protocol to wrap data in",
   131       g_param_spec_enum ("protocol", "Protocol", "The protocol to wrap data in",
   121           GST_TYPE_TCP_PROTOCOL, GST_TCP_PROTOCOL_NONE, G_PARAM_READWRITE));
   132           GST_TYPE_TCP_PROTOCOL, GST_TCP_PROTOCOL_NONE,
       
   133           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   122 
   134 
   123   gstbasesrc_class->get_caps = gst_tcp_client_src_getcaps;
   135   gstbasesrc_class->get_caps = gst_tcp_client_src_getcaps;
   124   gstbasesrc_class->start = gst_tcp_client_src_start;
   136   gstbasesrc_class->start = gst_tcp_client_src_start;
   125   gstbasesrc_class->stop = gst_tcp_client_src_stop;
   137   gstbasesrc_class->stop = gst_tcp_client_src_stop;
   126   gstbasesrc_class->unlock = gst_tcp_client_src_unlock;
   138   gstbasesrc_class->unlock = gst_tcp_client_src_unlock;
   137   this->port = TCP_DEFAULT_PORT;
   149   this->port = TCP_DEFAULT_PORT;
   138   this->host = g_strdup (TCP_DEFAULT_HOST);
   150   this->host = g_strdup (TCP_DEFAULT_HOST);
   139   this->sock_fd.fd = -1;
   151   this->sock_fd.fd = -1;
   140   this->protocol = GST_TCP_PROTOCOL_NONE;
   152   this->protocol = GST_TCP_PROTOCOL_NONE;
   141   this->caps = NULL;
   153   this->caps = NULL;
   142 
       
   143   gst_base_src_set_live (GST_BASE_SRC (this), TRUE);
       
   144 
   154 
   145   GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SRC_OPEN);
   155   GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SRC_OPEN);
   146 }
   156 }
   147 
   157 
   148 static void
   158 static void
   334   g_free (ip);
   344   g_free (ip);
   335 
   345 
   336   GST_DEBUG_OBJECT (src, "connecting to server");
   346   GST_DEBUG_OBJECT (src, "connecting to server");
   337   ret = connect (src->sock_fd.fd, (struct sockaddr *) &src->server_sin,
   347   ret = connect (src->sock_fd.fd, (struct sockaddr *) &src->server_sin,
   338       sizeof (src->server_sin));
   348       sizeof (src->server_sin));
   339 
   349   if (ret)
   340   if (ret) {
   350     goto connect_failed;
       
   351 
       
   352   /* add the socket to the poll */
       
   353   gst_poll_add_fd (src->fdset, &src->sock_fd);
       
   354   gst_poll_fd_ctl_read (src->fdset, &src->sock_fd, TRUE);
       
   355 
       
   356   return TRUE;
       
   357 
       
   358 socket_pair:
       
   359   {
       
   360     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
       
   361         GST_ERROR_SYSTEM);
       
   362     return FALSE;
       
   363   }
       
   364 no_socket:
       
   365   {
       
   366     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
       
   367     return FALSE;
       
   368   }
       
   369 name_resolv:
       
   370   {
       
   371     gst_tcp_client_src_stop (GST_BASE_SRC (src));
       
   372     return FALSE;
       
   373   }
       
   374 connect_failed:
       
   375   {
   341     gst_tcp_client_src_stop (GST_BASE_SRC (src));
   376     gst_tcp_client_src_stop (GST_BASE_SRC (src));
   342     switch (errno) {
   377     switch (errno) {
   343       case ECONNREFUSED:
   378       case ECONNREFUSED:
   344         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
   379         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
   345             (_("Connection to %s:%d refused."), src->host, src->port), (NULL));
   380             (_("Connection to %s:%d refused."), src->host, src->port), (NULL));
   346         return FALSE;
       
   347         break;
   381         break;
   348       default:
   382       default:
   349         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
   383         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
   350             ("connect to %s:%d failed: %s", src->host, src->port,
   384             ("connect to %s:%d failed: %s", src->host, src->port,
   351                 g_strerror (errno)));
   385                 g_strerror (errno)));
   352         return FALSE;
       
   353         break;
   386         break;
   354     }
   387     }
   355   }
       
   356 
       
   357   return TRUE;
       
   358 
       
   359 socket_pair:
       
   360   {
       
   361     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
       
   362         GST_ERROR_SYSTEM);
       
   363     return FALSE;
       
   364   }
       
   365 no_socket:
       
   366   {
       
   367     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
       
   368     return FALSE;
       
   369   }
       
   370 name_resolv:
       
   371   {
       
   372     gst_tcp_client_src_stop (GST_BASE_SRC (src));
       
   373     return FALSE;
   388     return FALSE;
   374   }
   389   }
   375 }
   390 }
   376 
   391 
   377 /* close the socket and associated resources
   392 /* close the socket and associated resources