2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-tcpclientsrc
23 * @see_also: #tcpclientsink
26 * <title>Example launch line</title>
31 * gst-launch tcpclientsrc protocol=none port=3000 ! fdsink fd=2
32 * ]| everything you type in the server is shown on the client
40 #include <gst/gst-i18n-plugin.h>
42 #include "gsttcpclientsrc.h"
43 #include <string.h> /* memset */
45 #include <arpa/inet.h>
49 GST_DEBUG_CATEGORY_STATIC (tcpclientsrc_debug);
50 #define GST_CAT_DEFAULT tcpclientsrc_debug
52 #define MAX_READ_SIZE 4 * 1024
55 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
68 #define gst_tcp_client_src_parent_class parent_class
69 G_DEFINE_TYPE (GstTCPClientSrc, gst_tcp_client_src, GST_TYPE_PUSH_SRC);
72 static void gst_tcp_client_src_finalize (GObject * gobject);
74 static GstCaps *gst_tcp_client_src_getcaps (GstBaseSrc * psrc,
77 static GstFlowReturn gst_tcp_client_src_create (GstPushSrc * psrc,
79 static gboolean gst_tcp_client_src_stop (GstBaseSrc * bsrc);
80 static gboolean gst_tcp_client_src_start (GstBaseSrc * bsrc);
81 static gboolean gst_tcp_client_src_unlock (GstBaseSrc * bsrc);
82 static gboolean gst_tcp_client_src_unlock_stop (GstBaseSrc * bsrc);
84 static void gst_tcp_client_src_set_property (GObject * object, guint prop_id,
85 const GValue * value, GParamSpec * pspec);
86 static void gst_tcp_client_src_get_property (GObject * object, guint prop_id,
87 GValue * value, GParamSpec * pspec);
90 gst_tcp_client_src_class_init (GstTCPClientSrcClass * klass)
92 GObjectClass *gobject_class;
93 GstElementClass *gstelement_class;
94 GstBaseSrcClass *gstbasesrc_class;
95 GstPushSrcClass *gstpush_src_class;
97 gobject_class = (GObjectClass *) klass;
98 gstelement_class = (GstElementClass *) klass;
99 gstbasesrc_class = (GstBaseSrcClass *) klass;
100 gstpush_src_class = (GstPushSrcClass *) klass;
102 gobject_class->set_property = gst_tcp_client_src_set_property;
103 gobject_class->get_property = gst_tcp_client_src_get_property;
104 gobject_class->finalize = gst_tcp_client_src_finalize;
106 g_object_class_install_property (gobject_class, PROP_HOST,
107 g_param_spec_string ("host", "Host",
108 "The host IP address to receive packets from", TCP_DEFAULT_HOST,
109 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
110 g_object_class_install_property (gobject_class, PROP_PORT,
111 g_param_spec_int ("port", "Port", "The port to receive packets from", 0,
112 TCP_HIGHEST_PORT, TCP_DEFAULT_PORT,
113 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
115 gst_element_class_add_pad_template (gstelement_class,
116 gst_static_pad_template_get (&srctemplate));
118 gst_element_class_set_details_simple (gstelement_class,
119 "TCP client source", "Source/Network",
120 "Receive data as a client over the network via TCP",
121 "Thomas Vander Stichele <thomas at apestaart dot org>");
123 gstbasesrc_class->get_caps = gst_tcp_client_src_getcaps;
124 gstbasesrc_class->start = gst_tcp_client_src_start;
125 gstbasesrc_class->stop = gst_tcp_client_src_stop;
126 gstbasesrc_class->unlock = gst_tcp_client_src_unlock;
127 gstbasesrc_class->unlock_stop = gst_tcp_client_src_unlock_stop;
129 gstpush_src_class->create = gst_tcp_client_src_create;
131 GST_DEBUG_CATEGORY_INIT (tcpclientsrc_debug, "tcpclientsrc", 0,
132 "TCP Client Source");
136 gst_tcp_client_src_init (GstTCPClientSrc * this)
138 this->port = TCP_DEFAULT_PORT;
139 this->host = g_strdup (TCP_DEFAULT_HOST);
140 this->sock_fd.fd = -1;
143 GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SRC_OPEN);
147 gst_tcp_client_src_finalize (GObject * gobject)
149 GstTCPClientSrc *this = GST_TCP_CLIENT_SRC (gobject);
153 G_OBJECT_CLASS (parent_class)->finalize (gobject);
157 gst_tcp_client_src_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
159 GstTCPClientSrc *src;
160 GstCaps *caps = NULL;
162 src = GST_TCP_CLIENT_SRC (bsrc);
164 if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_CLIENT_SRC_OPEN))
165 caps = (filter ? gst_caps_ref (filter) : gst_caps_new_any ());
166 else if (src->caps && filter)
168 gst_caps_intersect_full (filter, src->caps, GST_CAPS_INTERSECT_FIRST);
170 caps = gst_caps_copy (src->caps);
172 caps = (filter ? gst_caps_ref (filter) : gst_caps_new_any ());
174 GST_DEBUG_OBJECT (src, "returning caps %" GST_PTR_FORMAT, caps);
175 g_assert (GST_IS_CAPS (caps));
180 gst_tcp_client_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
182 GstTCPClientSrc *src;
183 GstFlowReturn ret = GST_FLOW_OK;
185 src = GST_TCP_CLIENT_SRC (psrc);
187 if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_CLIENT_SRC_OPEN))
190 GST_LOG_OBJECT (src, "asked for a buffer");
192 /* read the buffer header */
193 ret = gst_tcp_read_buffer (GST_ELEMENT (src), src->sock_fd.fd,
196 if (ret == GST_FLOW_OK) {
198 "Returning buffer from _get of size %" G_GSIZE_FORMAT ", ts %"
199 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
200 ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
201 gst_buffer_get_size (*outbuf),
202 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*outbuf)),
203 GST_TIME_ARGS (GST_BUFFER_DURATION (*outbuf)),
204 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf));
211 GST_DEBUG_OBJECT (src, "connection to closed, cannot read data");
212 return GST_FLOW_WRONG_STATE;
217 gst_tcp_client_src_set_property (GObject * object, guint prop_id,
218 const GValue * value, GParamSpec * pspec)
220 GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
224 if (!g_value_get_string (value)) {
225 g_warning ("host property cannot be NULL");
228 g_free (tcpclientsrc->host);
229 tcpclientsrc->host = g_strdup (g_value_get_string (value));
232 tcpclientsrc->port = g_value_get_int (value);
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242 gst_tcp_client_src_get_property (GObject * object, guint prop_id,
243 GValue * value, GParamSpec * pspec)
245 GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
249 g_value_set_string (value, tcpclientsrc->host);
252 g_value_set_int (value, tcpclientsrc->port);
256 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
261 /* create a socket for connecting to remote server */
263 gst_tcp_client_src_start (GstBaseSrc * bsrc)
267 GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
269 if ((src->fdset = gst_poll_new (TRUE)) == NULL)
272 /* create receiving client socket */
273 GST_DEBUG_OBJECT (src, "opening receiving client socket to %s:%d",
274 src->host, src->port);
276 if ((src->sock_fd.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
279 GST_DEBUG_OBJECT (src, "opened receiving client socket with fd %d",
281 GST_OBJECT_FLAG_SET (src, GST_TCP_CLIENT_SRC_OPEN);
283 /* look up name if we need to */
284 if (!(ip = gst_tcp_host_to_ip (GST_ELEMENT (src), src->host)))
287 GST_DEBUG_OBJECT (src, "IP address for host %s is %s", src->host, ip);
289 /* connect to server */
290 memset (&src->server_sin, 0, sizeof (src->server_sin));
291 src->server_sin.sin_family = AF_INET; /* network socket */
292 src->server_sin.sin_port = htons (src->port); /* on port */
293 src->server_sin.sin_addr.s_addr = inet_addr (ip); /* on host ip */
296 GST_DEBUG_OBJECT (src, "connecting to server");
297 ret = connect (src->sock_fd.fd, (struct sockaddr *) &src->server_sin,
298 sizeof (src->server_sin));
302 /* add the socket to the poll */
303 gst_poll_add_fd (src->fdset, &src->sock_fd);
304 gst_poll_fd_ctl_read (src->fdset, &src->sock_fd, TRUE);
310 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
316 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
321 gst_tcp_client_src_stop (GST_BASE_SRC (src));
326 gst_tcp_client_src_stop (GST_BASE_SRC (src));
329 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
330 (_("Connection to %s:%d refused."), src->host, src->port), (NULL));
333 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
334 ("connect to %s:%d failed: %s", src->host, src->port,
335 g_strerror (errno)));
342 /* close the socket and associated resources
344 * used both to recover from errors and go to NULL state */
346 gst_tcp_client_src_stop (GstBaseSrc * bsrc)
348 GstTCPClientSrc *src;
350 src = GST_TCP_CLIENT_SRC (bsrc);
352 GST_DEBUG_OBJECT (src, "closing socket");
354 if (src->fdset != NULL) {
355 gst_poll_free (src->fdset);
359 gst_tcp_socket_close (&src->sock_fd);
360 src->caps_received = FALSE;
362 gst_caps_unref (src->caps);
365 GST_OBJECT_FLAG_UNSET (src, GST_TCP_CLIENT_SRC_OPEN);
370 /* will be called only between calls to start() and stop() */
372 gst_tcp_client_src_unlock (GstBaseSrc * bsrc)
374 GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
376 GST_DEBUG_OBJECT (src, "set to flushing");
377 gst_poll_set_flushing (src->fdset, TRUE);
382 /* will be called only between calls to start() and stop() */
384 gst_tcp_client_src_unlock_stop (GstBaseSrc * bsrc)
386 GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
388 GST_DEBUG_OBJECT (src, "unset flushing");
389 gst_poll_set_flushing (src->fdset, FALSE);