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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 * SECTION:element-tcpclientsrc
25 * @title: tcpclientsrc
26 * @see_also: #tcpclientsink
28 * ## Example launch line (server):
32 * ## Example launch line (client):
34 * gst-launch-1.0 tcpclientsrc port=3000 ! fdsink fd=2
36 * everything you type in the server is shown on the client.
37 * If you want to detect network failures and/or limit the time your tcp client
38 * keeps waiting for data from server setting a timeout value can be useful.
46 #include <gst/gst-i18n-plugin.h>
47 #include "gsttcpelements.h"
48 #include "gsttcpclientsrc.h"
49 #include "gsttcpsrcstats.h"
52 GST_DEBUG_CATEGORY_STATIC (tcpclientsrc_debug);
53 #define GST_CAT_DEFAULT tcpclientsrc_debug
55 #define MAX_READ_SIZE 4 * 1024
56 #define TCP_DEFAULT_TIMEOUT 0
59 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
74 #define gst_tcp_client_src_parent_class parent_class
75 G_DEFINE_TYPE (GstTCPClientSrc, gst_tcp_client_src, GST_TYPE_PUSH_SRC);
76 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (tcpclientsrc, "tcpclientsrc",
77 GST_RANK_NONE, GST_TYPE_TCP_CLIENT_SRC, tcp_element_init (plugin));
79 static void gst_tcp_client_src_finalize (GObject * gobject);
81 static GstCaps *gst_tcp_client_src_getcaps (GstBaseSrc * psrc,
84 static GstFlowReturn gst_tcp_client_src_create (GstPushSrc * psrc,
86 static gboolean gst_tcp_client_src_stop (GstBaseSrc * bsrc);
87 static gboolean gst_tcp_client_src_start (GstBaseSrc * bsrc);
88 static gboolean gst_tcp_client_src_unlock (GstBaseSrc * bsrc);
89 static gboolean gst_tcp_client_src_unlock_stop (GstBaseSrc * bsrc);
91 static void gst_tcp_client_src_set_property (GObject * object, guint prop_id,
92 const GValue * value, GParamSpec * pspec);
93 static void gst_tcp_client_src_get_property (GObject * object, guint prop_id,
94 GValue * value, GParamSpec * pspec);
95 static GstStructure *gst_tcp_client_src_get_stats (GstTCPClientSrc * src);
98 gst_tcp_client_src_class_init (GstTCPClientSrcClass * klass)
100 GObjectClass *gobject_class;
101 GstElementClass *gstelement_class;
102 GstBaseSrcClass *gstbasesrc_class;
103 GstPushSrcClass *gstpush_src_class;
105 gobject_class = (GObjectClass *) klass;
106 gstelement_class = (GstElementClass *) klass;
107 gstbasesrc_class = (GstBaseSrcClass *) klass;
108 gstpush_src_class = (GstPushSrcClass *) klass;
110 gobject_class->set_property = gst_tcp_client_src_set_property;
111 gobject_class->get_property = gst_tcp_client_src_get_property;
112 gobject_class->finalize = gst_tcp_client_src_finalize;
114 g_object_class_install_property (gobject_class, PROP_HOST,
115 g_param_spec_string ("host", "Host",
116 "The host IP address to receive packets from", TCP_DEFAULT_HOST,
117 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
118 g_object_class_install_property (gobject_class, PROP_PORT,
119 g_param_spec_int ("port", "Port", "The port to receive packets from", 0,
120 TCP_HIGHEST_PORT, TCP_DEFAULT_PORT,
121 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
124 * GstTCPClientSrc::timeout;
126 * Value in seconds to timeout a blocking I/O (0 = No timeout).
130 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
131 g_param_spec_uint ("timeout", "timeout",
132 "Value in seconds to timeout a blocking I/O. 0 = No timeout. ", 0,
133 G_MAXUINT, TCP_DEFAULT_TIMEOUT,
134 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
137 * GstTCPClientSrc::stats:
139 * Sends a GstStructure with statistics. We count bytes-received in a
140 * platform-independent way and the rest via the tcp_info struct, if it's
141 * available. The OS takes care of the TCP layer for us so we can't know it
146 * bytes-received (uint64): Total bytes received (platform-independent)
147 * reordering (uint): Amount of reordering (linux-specific)
148 * unacked (uint): Un-acked packets (linux-specific)
149 * sacked (uint): Selective acked packets (linux-specific)
150 * lost (uint): Lost packets (linux-specific)
151 * retrans (uint): Retransmits (linux-specific)
152 * fackets (uint): Forward acknowledgement (linux-specific)
156 g_object_class_install_property (gobject_class, PROP_STATS,
157 g_param_spec_boxed ("stats", "Stats", "Retrieve a statistics structure",
158 GST_TYPE_STRUCTURE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
160 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
162 gst_element_class_set_static_metadata (gstelement_class,
163 "TCP client source", "Source/Network",
164 "Receive data as a client over the network via TCP",
165 "Thomas Vander Stichele <thomas at apestaart dot org>");
167 gstbasesrc_class->get_caps = gst_tcp_client_src_getcaps;
168 gstbasesrc_class->start = gst_tcp_client_src_start;
169 gstbasesrc_class->stop = gst_tcp_client_src_stop;
170 gstbasesrc_class->unlock = gst_tcp_client_src_unlock;
171 gstbasesrc_class->unlock_stop = gst_tcp_client_src_unlock_stop;
173 gstpush_src_class->create = gst_tcp_client_src_create;
175 GST_DEBUG_CATEGORY_INIT (tcpclientsrc_debug, "tcpclientsrc", 0,
176 "TCP Client Source");
180 gst_tcp_client_src_init (GstTCPClientSrc * this)
182 this->port = TCP_DEFAULT_PORT;
183 this->host = g_strdup (TCP_DEFAULT_HOST);
184 this->timeout = TCP_DEFAULT_TIMEOUT;
186 this->cancellable = g_cancellable_new ();
188 GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SRC_OPEN);
192 gst_tcp_client_src_finalize (GObject * gobject)
194 GstTCPClientSrc *this = GST_TCP_CLIENT_SRC (gobject);
196 if (this->cancellable)
197 g_object_unref (this->cancellable);
198 this->cancellable = NULL;
200 g_object_unref (this->socket);
204 gst_clear_structure (&this->stats);
206 G_OBJECT_CLASS (parent_class)->finalize (gobject);
210 gst_tcp_client_src_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
212 GstTCPClientSrc *src;
213 GstCaps *caps = NULL;
215 src = GST_TCP_CLIENT_SRC (bsrc);
217 caps = (filter ? gst_caps_ref (filter) : gst_caps_new_any ());
219 GST_DEBUG_OBJECT (src, "returning caps %" GST_PTR_FORMAT, caps);
220 g_assert (GST_IS_CAPS (caps));
225 gst_tcp_client_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
227 GstTCPClientSrc *src;
228 GstFlowReturn ret = GST_FLOW_OK;
234 src = GST_TCP_CLIENT_SRC (psrc);
236 if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_CLIENT_SRC_OPEN))
239 GST_LOG_OBJECT (src, "asked for a buffer");
241 /* read the buffer header */
242 avail = g_socket_get_available_bytes (src->socket);
244 goto get_available_error;
245 } else if (avail == 0) {
246 GIOCondition condition;
248 if (!g_socket_condition_wait (src->socket,
249 G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, src->cancellable, &err))
253 g_socket_condition_check (src->socket,
254 G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP);
256 if ((condition & G_IO_ERR)) {
257 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
258 ("Socket in error state"));
260 ret = GST_FLOW_ERROR;
262 } else if ((condition & G_IO_HUP)) {
263 GST_DEBUG_OBJECT (src, "Connection closed");
268 avail = g_socket_get_available_bytes (src->socket);
270 goto get_available_error;
274 read = MIN (avail, MAX_READ_SIZE);
275 *outbuf = gst_buffer_new_and_alloc (read);
276 gst_buffer_map (*outbuf, &map, GST_MAP_READWRITE);
278 g_socket_receive (src->socket, (gchar *) map.data, read,
279 src->cancellable, &err);
281 /* Connection closed */
288 GST_DEBUG_OBJECT (src, "Connection closed");
291 gst_buffer_unmap (*outbuf, &map);
292 gst_buffer_unref (*outbuf);
295 } else if (rret < 0) {
296 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
297 ret = GST_FLOW_FLUSHING;
298 GST_DEBUG_OBJECT (src, "Cancelled reading from socket");
300 ret = GST_FLOW_ERROR;
301 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
302 ("Failed to read from socket: %s", err->message));
304 gst_buffer_unmap (*outbuf, &map);
305 gst_buffer_unref (*outbuf);
309 gst_buffer_unmap (*outbuf, &map);
310 gst_buffer_resize (*outbuf, 0, rret);
311 src->bytes_received += read;
314 "Returning buffer from _get of size %" G_GSIZE_FORMAT ", ts %"
315 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
316 ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
317 gst_buffer_get_size (*outbuf),
318 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*outbuf)),
319 GST_TIME_ARGS (GST_BUFFER_DURATION (*outbuf)),
320 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf));
322 g_clear_error (&err);
329 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
330 GST_DEBUG_OBJECT (src, "Cancelled");
331 ret = GST_FLOW_FLUSHING;
333 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
334 ("Select failed: %s", err->message));
335 ret = GST_FLOW_ERROR;
337 g_clear_error (&err);
342 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
343 ("Failed to get available bytes from socket"));
344 return GST_FLOW_ERROR;
348 GST_DEBUG_OBJECT (src, "connection to closed, cannot read data");
349 return GST_FLOW_FLUSHING;
354 gst_tcp_client_src_set_property (GObject * object, guint prop_id,
355 const GValue * value, GParamSpec * pspec)
357 GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
361 if (!g_value_get_string (value)) {
362 g_warning ("host property cannot be NULL");
365 g_free (tcpclientsrc->host);
366 tcpclientsrc->host = g_value_dup_string (value);
369 tcpclientsrc->port = g_value_get_int (value);
372 tcpclientsrc->timeout = g_value_get_uint (value);
376 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
382 gst_tcp_client_src_get_property (GObject * object, guint prop_id,
383 GValue * value, GParamSpec * pspec)
385 GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
389 g_value_set_string (value, tcpclientsrc->host);
392 g_value_set_int (value, tcpclientsrc->port);
395 g_value_set_uint (value, tcpclientsrc->timeout);
398 g_value_take_boxed (value, gst_tcp_client_src_get_stats (tcpclientsrc));
401 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
406 /* create a socket for connecting to remote server */
408 gst_tcp_client_src_start (GstBaseSrc * bsrc)
410 GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
413 GSocketAddress *saddr;
416 src->bytes_received = 0;
417 gst_clear_structure (&src->stats);
419 /* look up name if we need to */
420 addr = g_inet_address_new_from_string (src->host);
424 resolver = g_resolver_get_default ();
427 g_resolver_lookup_by_name (resolver, src->host, src->cancellable, &err);
430 addr = G_INET_ADDRESS (g_object_ref (results->data));
432 g_resolver_free_addresses (results);
433 g_object_unref (resolver);
435 #ifndef GST_DISABLE_GST_DEBUG
437 gchar *ip = g_inet_address_to_string (addr);
439 GST_DEBUG_OBJECT (src, "IP address for host %s is %s", src->host, ip);
444 saddr = g_inet_socket_address_new (addr, src->port);
445 g_object_unref (addr);
447 /* create receiving client socket */
448 GST_DEBUG_OBJECT (src, "opening receiving client socket to %s:%d",
449 src->host, src->port);
452 g_socket_new (g_socket_address_get_family (saddr), G_SOCKET_TYPE_STREAM,
453 G_SOCKET_PROTOCOL_TCP, &err);
457 g_socket_set_timeout (src->socket, src->timeout);
459 GST_DEBUG_OBJECT (src, "opened receiving client socket");
460 GST_OBJECT_FLAG_SET (src, GST_TCP_CLIENT_SRC_OPEN);
462 /* connect to server */
463 if (!g_socket_connect (src->socket, saddr, src->cancellable, &err))
466 g_object_unref (saddr);
472 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
473 ("Failed to create socket: %s", err->message));
474 g_clear_error (&err);
475 g_object_unref (saddr);
480 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
481 GST_DEBUG_OBJECT (src, "Cancelled name resolval");
483 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
484 ("Failed to resolve host '%s': %s", src->host, err->message));
486 g_clear_error (&err);
487 g_object_unref (resolver);
492 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
493 GST_DEBUG_OBJECT (src, "Cancelled connecting");
495 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
496 ("Failed to connect to host '%s:%d': %s", src->host, src->port,
499 g_clear_error (&err);
500 g_object_unref (saddr);
501 gst_tcp_client_src_stop (GST_BASE_SRC (src));
506 /* close the socket and associated resources
508 * used both to recover from errors and go to NULL state */
510 gst_tcp_client_src_stop (GstBaseSrc * bsrc)
512 GstTCPClientSrc *src;
515 src = GST_TCP_CLIENT_SRC (bsrc);
518 GST_DEBUG_OBJECT (src, "closing socket");
520 src->stats = gst_tcp_client_src_get_stats (src);
522 if (!g_socket_close (src->socket, &err)) {
523 GST_ERROR_OBJECT (src, "Failed to close socket: %s", err->message);
524 g_clear_error (&err);
526 g_object_unref (src->socket);
530 GST_OBJECT_FLAG_UNSET (src, GST_TCP_CLIENT_SRC_OPEN);
535 /* will be called only between calls to start() and stop() */
537 gst_tcp_client_src_unlock (GstBaseSrc * bsrc)
539 GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
541 GST_DEBUG_OBJECT (src, "set to flushing");
542 g_cancellable_cancel (src->cancellable);
547 /* will be called only between calls to start() and stop() */
549 gst_tcp_client_src_unlock_stop (GstBaseSrc * bsrc)
551 GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
553 GST_DEBUG_OBJECT (src, "unset flushing");
554 g_object_unref (src->cancellable);
555 src->cancellable = g_cancellable_new ();
560 static GstStructure *
561 gst_tcp_client_src_get_stats (GstTCPClientSrc * src)
565 /* we can't get the values post stop so just return the saved ones */
567 return gst_structure_copy (src->stats);
569 s = gst_structure_new ("GstTCPClientSrcStats",
570 "bytes-received", G_TYPE_UINT64, src->bytes_received, NULL);
572 gst_tcp_stats_from_socket (s, src->socket);