2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
4 * Copyright (C) <2011> Collabora Ltd.
5 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * SECTION:element-tcpclientsrc
25 * @see_also: #tcpclientsink
28 * <title>Example launch line</title>
33 * gst-launch tcpclientsrc port=3000 ! fdsink fd=2
34 * ]| everything you type in the server is shown on the client
42 #include <gst/gst-i18n-plugin.h>
43 #include "gsttcpclientsrc.h"
46 GST_DEBUG_CATEGORY_STATIC (tcpclientsrc_debug);
47 #define GST_CAT_DEFAULT tcpclientsrc_debug
49 #define MAX_READ_SIZE 4 * 1024
52 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
65 #define gst_tcp_client_src_parent_class parent_class
66 G_DEFINE_TYPE (GstTCPClientSrc, gst_tcp_client_src, GST_TYPE_PUSH_SRC);
69 static void gst_tcp_client_src_finalize (GObject * gobject);
71 static GstCaps *gst_tcp_client_src_getcaps (GstBaseSrc * psrc,
74 static GstFlowReturn gst_tcp_client_src_create (GstPushSrc * psrc,
76 static gboolean gst_tcp_client_src_stop (GstBaseSrc * bsrc);
77 static gboolean gst_tcp_client_src_start (GstBaseSrc * bsrc);
78 static gboolean gst_tcp_client_src_unlock (GstBaseSrc * bsrc);
79 static gboolean gst_tcp_client_src_unlock_stop (GstBaseSrc * bsrc);
81 static void gst_tcp_client_src_set_property (GObject * object, guint prop_id,
82 const GValue * value, GParamSpec * pspec);
83 static void gst_tcp_client_src_get_property (GObject * object, guint prop_id,
84 GValue * value, GParamSpec * pspec);
87 gst_tcp_client_src_class_init (GstTCPClientSrcClass * klass)
89 GObjectClass *gobject_class;
90 GstElementClass *gstelement_class;
91 GstBaseSrcClass *gstbasesrc_class;
92 GstPushSrcClass *gstpush_src_class;
94 gobject_class = (GObjectClass *) klass;
95 gstelement_class = (GstElementClass *) klass;
96 gstbasesrc_class = (GstBaseSrcClass *) klass;
97 gstpush_src_class = (GstPushSrcClass *) klass;
99 gobject_class->set_property = gst_tcp_client_src_set_property;
100 gobject_class->get_property = gst_tcp_client_src_get_property;
101 gobject_class->finalize = gst_tcp_client_src_finalize;
103 g_object_class_install_property (gobject_class, PROP_HOST,
104 g_param_spec_string ("host", "Host",
105 "The host IP address to receive packets from", TCP_DEFAULT_HOST,
106 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
107 g_object_class_install_property (gobject_class, PROP_PORT,
108 g_param_spec_int ("port", "Port", "The port to receive packets from", 0,
109 TCP_HIGHEST_PORT, TCP_DEFAULT_PORT,
110 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
112 gst_element_class_add_pad_template (gstelement_class,
113 gst_static_pad_template_get (&srctemplate));
115 gst_element_class_set_static_metadata (gstelement_class,
116 "TCP client source", "Source/Network",
117 "Receive data as a client over the network via TCP",
118 "Thomas Vander Stichele <thomas at apestaart dot org>");
120 gstbasesrc_class->get_caps = gst_tcp_client_src_getcaps;
121 gstbasesrc_class->start = gst_tcp_client_src_start;
122 gstbasesrc_class->stop = gst_tcp_client_src_stop;
123 gstbasesrc_class->unlock = gst_tcp_client_src_unlock;
124 gstbasesrc_class->unlock_stop = gst_tcp_client_src_unlock_stop;
126 gstpush_src_class->create = gst_tcp_client_src_create;
128 GST_DEBUG_CATEGORY_INIT (tcpclientsrc_debug, "tcpclientsrc", 0,
129 "TCP Client Source");
133 gst_tcp_client_src_init (GstTCPClientSrc * this)
135 this->port = TCP_DEFAULT_PORT;
136 this->host = g_strdup (TCP_DEFAULT_HOST);
138 this->cancellable = g_cancellable_new ();
140 GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SRC_OPEN);
144 gst_tcp_client_src_finalize (GObject * gobject)
146 GstTCPClientSrc *this = GST_TCP_CLIENT_SRC (gobject);
148 if (this->cancellable)
149 g_object_unref (this->cancellable);
150 this->cancellable = NULL;
152 g_object_unref (this->socket);
157 G_OBJECT_CLASS (parent_class)->finalize (gobject);
161 gst_tcp_client_src_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
163 GstTCPClientSrc *src;
164 GstCaps *caps = NULL;
166 src = GST_TCP_CLIENT_SRC (bsrc);
168 caps = (filter ? gst_caps_ref (filter) : gst_caps_new_any ());
170 GST_DEBUG_OBJECT (src, "returning caps %" GST_PTR_FORMAT, caps);
171 g_assert (GST_IS_CAPS (caps));
176 gst_tcp_client_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
178 GstTCPClientSrc *src;
179 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 avail = g_socket_get_available_bytes (src->socket);
195 goto get_available_error;
196 } else if (avail == 0) {
197 GIOCondition condition;
199 if (!g_socket_condition_wait (src->socket,
200 G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, src->cancellable, &err))
204 g_socket_condition_check (src->socket,
205 G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP);
207 if ((condition & G_IO_ERR)) {
208 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
209 ("Socket in error state"));
211 ret = GST_FLOW_ERROR;
213 } else if ((condition & G_IO_HUP)) {
214 GST_DEBUG_OBJECT (src, "Connection closed");
219 avail = g_socket_get_available_bytes (src->socket);
221 goto get_available_error;
225 read = MIN (avail, MAX_READ_SIZE);
226 *outbuf = gst_buffer_new_and_alloc (read);
227 gst_buffer_map (*outbuf, &map, GST_MAP_READWRITE);
229 g_socket_receive (src->socket, (gchar *) map.data, read,
230 src->cancellable, &err);
232 /* Connection closed */
239 GST_DEBUG_OBJECT (src, "Connection closed");
242 gst_buffer_unmap (*outbuf, &map);
243 gst_buffer_unref (*outbuf);
246 } else if (rret < 0) {
247 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
248 ret = GST_FLOW_FLUSHING;
249 GST_DEBUG_OBJECT (src, "Cancelled reading from socket");
251 ret = GST_FLOW_ERROR;
252 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
253 ("Failed to read from socket: %s", err->message));
255 gst_buffer_unmap (*outbuf, &map);
256 gst_buffer_unref (*outbuf);
260 gst_buffer_unmap (*outbuf, &map);
261 gst_buffer_resize (*outbuf, 0, rret);
264 "Returning buffer from _get of size %" G_GSIZE_FORMAT ", ts %"
265 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
266 ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
267 gst_buffer_get_size (*outbuf),
268 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*outbuf)),
269 GST_TIME_ARGS (GST_BUFFER_DURATION (*outbuf)),
270 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf));
272 g_clear_error (&err);
279 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
280 ("Select failed: %s", err->message));
281 g_clear_error (&err);
282 return GST_FLOW_ERROR;
286 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
287 ("Failed to get available bytes from socket"));
288 return GST_FLOW_ERROR;
292 GST_DEBUG_OBJECT (src, "connection to closed, cannot read data");
293 return GST_FLOW_FLUSHING;
298 gst_tcp_client_src_set_property (GObject * object, guint prop_id,
299 const GValue * value, GParamSpec * pspec)
301 GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
305 if (!g_value_get_string (value)) {
306 g_warning ("host property cannot be NULL");
309 g_free (tcpclientsrc->host);
310 tcpclientsrc->host = g_strdup (g_value_get_string (value));
313 tcpclientsrc->port = g_value_get_int (value);
317 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
323 gst_tcp_client_src_get_property (GObject * object, guint prop_id,
324 GValue * value, GParamSpec * pspec)
326 GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
330 g_value_set_string (value, tcpclientsrc->host);
333 g_value_set_int (value, tcpclientsrc->port);
336 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
341 /* create a socket for connecting to remote server */
343 gst_tcp_client_src_start (GstBaseSrc * bsrc)
345 GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
348 GSocketAddress *saddr;
351 /* look up name if we need to */
352 addr = g_inet_address_new_from_string (src->host);
356 resolver = g_resolver_get_default ();
359 g_resolver_lookup_by_name (resolver, src->host, src->cancellable, &err);
362 addr = G_INET_ADDRESS (g_object_ref (results->data));
364 g_resolver_free_addresses (results);
365 g_object_unref (resolver);
367 #ifndef GST_DISABLE_GST_DEBUG
369 gchar *ip = g_inet_address_to_string (addr);
371 GST_DEBUG_OBJECT (src, "IP address for host %s is %s", src->host, ip);
376 saddr = g_inet_socket_address_new (addr, src->port);
377 g_object_unref (addr);
379 /* create receiving client socket */
380 GST_DEBUG_OBJECT (src, "opening receiving client socket to %s:%d",
381 src->host, src->port);
384 g_socket_new (g_socket_address_get_family (saddr), G_SOCKET_TYPE_STREAM,
385 G_SOCKET_PROTOCOL_TCP, &err);
389 GST_DEBUG_OBJECT (src, "opened receiving client socket");
390 GST_OBJECT_FLAG_SET (src, GST_TCP_CLIENT_SRC_OPEN);
392 /* connect to server */
393 if (!g_socket_connect (src->socket, saddr, src->cancellable, &err))
396 g_object_unref (saddr);
402 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
403 ("Failed to create socket: %s", err->message));
404 g_clear_error (&err);
405 g_object_unref (saddr);
410 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
411 GST_DEBUG_OBJECT (src, "Cancelled name resolval");
413 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
414 ("Failed to resolve host '%s': %s", src->host, err->message));
416 g_clear_error (&err);
417 g_object_unref (resolver);
422 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
423 GST_DEBUG_OBJECT (src, "Cancelled connecting");
425 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
426 ("Failed to connect to host '%s:%d': %s", src->host, src->port,
429 g_clear_error (&err);
430 g_object_unref (saddr);
431 gst_tcp_client_src_stop (GST_BASE_SRC (src));
436 /* close the socket and associated resources
438 * used both to recover from errors and go to NULL state */
440 gst_tcp_client_src_stop (GstBaseSrc * bsrc)
442 GstTCPClientSrc *src;
445 src = GST_TCP_CLIENT_SRC (bsrc);
448 GST_DEBUG_OBJECT (src, "closing socket");
450 if (!g_socket_close (src->socket, &err)) {
451 GST_ERROR_OBJECT (src, "Failed to close socket: %s", err->message);
452 g_clear_error (&err);
454 g_object_unref (src->socket);
458 GST_OBJECT_FLAG_UNSET (src, GST_TCP_CLIENT_SRC_OPEN);
463 /* will be called only between calls to start() and stop() */
465 gst_tcp_client_src_unlock (GstBaseSrc * bsrc)
467 GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
469 GST_DEBUG_OBJECT (src, "set to flushing");
470 g_cancellable_cancel (src->cancellable);
475 /* will be called only between calls to start() and stop() */
477 gst_tcp_client_src_unlock_stop (GstBaseSrc * bsrc)
479 GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
481 GST_DEBUG_OBJECT (src, "unset flushing");
482 g_cancellable_reset (src->cancellable);