1 /* GStreamer
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>
6 *
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.
11 *
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.
16 *
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.
21 */
22
23 /**
24 * SECTION:element-tcpclientsrc
25 * @title: tcpclientsrc
26 * @see_also: #tcpclientsink
27 *
28 * ## Example launch line (server):
29 * |[
30 * nc -l -p 3000
31 * ]|
32 * ## Example launch line (client):
33 * |[
34 * gst-launch-1.0 tcpclientsrc port=3000 ! fdsink fd=2
35 * ]|
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.
39 *
40 */
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include <gst/gst-i18n-plugin.h>
47 #include "gsttcpclientsrc.h"
48 #include "gsttcp.h"
49
50 GST_DEBUG_CATEGORY_STATIC (tcpclientsrc_debug);
51 #define GST_CAT_DEFAULT tcpclientsrc_debug
52
53 #define MAX_READ_SIZE 4 * 1024
54 #define TCP_DEFAULT_TIMEOUT 0
55
56
57 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
58 GST_PAD_SRC,
59 GST_PAD_ALWAYS,
60 GST_STATIC_CAPS_ANY);
61
62
63 enum
64 {
65 PROP_0,
66 PROP_HOST,
67 PROP_PORT,
68 PROP_TIMEOUT
69 };
70
71 #define gst_tcp_client_src_parent_class parent_class
72 G_DEFINE_TYPE (GstTCPClientSrc, gst_tcp_client_src, GST_TYPE_PUSH_SRC);
73
74
75 static void gst_tcp_client_src_finalize (GObject * gobject);
76
77 static GstCaps *gst_tcp_client_src_getcaps (GstBaseSrc * psrc,
78 GstCaps * filter);
79
80 static GstFlowReturn gst_tcp_client_src_create (GstPushSrc * psrc,
81 GstBuffer ** outbuf);
82 static gboolean gst_tcp_client_src_stop (GstBaseSrc * bsrc);
83 static gboolean gst_tcp_client_src_start (GstBaseSrc * bsrc);
84 static gboolean gst_tcp_client_src_unlock (GstBaseSrc * bsrc);
85 static gboolean gst_tcp_client_src_unlock_stop (GstBaseSrc * bsrc);
86
87 static void gst_tcp_client_src_set_property (GObject * object, guint prop_id,
88 const GValue * value, GParamSpec * pspec);
89 static void gst_tcp_client_src_get_property (GObject * object, guint prop_id,
90 GValue * value, GParamSpec * pspec);
91
92 static void
gst_tcp_client_src_class_init(GstTCPClientSrcClass * klass)93 gst_tcp_client_src_class_init (GstTCPClientSrcClass * klass)
94 {
95 GObjectClass *gobject_class;
96 GstElementClass *gstelement_class;
97 GstBaseSrcClass *gstbasesrc_class;
98 GstPushSrcClass *gstpush_src_class;
99
100 gobject_class = (GObjectClass *) klass;
101 gstelement_class = (GstElementClass *) klass;
102 gstbasesrc_class = (GstBaseSrcClass *) klass;
103 gstpush_src_class = (GstPushSrcClass *) klass;
104
105 gobject_class->set_property = gst_tcp_client_src_set_property;
106 gobject_class->get_property = gst_tcp_client_src_get_property;
107 gobject_class->finalize = gst_tcp_client_src_finalize;
108
109 g_object_class_install_property (gobject_class, PROP_HOST,
110 g_param_spec_string ("host", "Host",
111 "The host IP address to receive packets from", TCP_DEFAULT_HOST,
112 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
113 g_object_class_install_property (gobject_class, PROP_PORT,
114 g_param_spec_int ("port", "Port", "The port to receive packets from", 0,
115 TCP_HIGHEST_PORT, TCP_DEFAULT_PORT,
116 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
117
118 /**
119 * GstTCPClientSrc::timeout;
120 *
121 * Value in seconds to timeout a blocking I/O (0 = No timeout).
122 *
123 * Since: 1.12
124 */
125 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
126 g_param_spec_uint ("timeout", "timeout",
127 "Value in seconds to timeout a blocking I/O. 0 = No timeout. ", 0,
128 G_MAXUINT, TCP_DEFAULT_TIMEOUT,
129 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
130
131 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
132
133 gst_element_class_set_static_metadata (gstelement_class,
134 "TCP client source", "Source/Network",
135 "Receive data as a client over the network via TCP",
136 "Thomas Vander Stichele <thomas at apestaart dot org>");
137
138 gstbasesrc_class->get_caps = gst_tcp_client_src_getcaps;
139 gstbasesrc_class->start = gst_tcp_client_src_start;
140 gstbasesrc_class->stop = gst_tcp_client_src_stop;
141 gstbasesrc_class->unlock = gst_tcp_client_src_unlock;
142 gstbasesrc_class->unlock_stop = gst_tcp_client_src_unlock_stop;
143
144 gstpush_src_class->create = gst_tcp_client_src_create;
145
146 GST_DEBUG_CATEGORY_INIT (tcpclientsrc_debug, "tcpclientsrc", 0,
147 "TCP Client Source");
148 }
149
150 static void
gst_tcp_client_src_init(GstTCPClientSrc * this)151 gst_tcp_client_src_init (GstTCPClientSrc * this)
152 {
153 this->port = TCP_DEFAULT_PORT;
154 this->host = g_strdup (TCP_DEFAULT_HOST);
155 this->timeout = TCP_DEFAULT_TIMEOUT;
156 this->socket = NULL;
157 this->cancellable = g_cancellable_new ();
158
159 GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SRC_OPEN);
160 }
161
162 static void
gst_tcp_client_src_finalize(GObject * gobject)163 gst_tcp_client_src_finalize (GObject * gobject)
164 {
165 GstTCPClientSrc *this = GST_TCP_CLIENT_SRC (gobject);
166
167 if (this->cancellable)
168 g_object_unref (this->cancellable);
169 this->cancellable = NULL;
170 if (this->socket)
171 g_object_unref (this->socket);
172 this->socket = NULL;
173 g_free (this->host);
174 this->host = NULL;
175
176 G_OBJECT_CLASS (parent_class)->finalize (gobject);
177 }
178
179 static GstCaps *
gst_tcp_client_src_getcaps(GstBaseSrc * bsrc,GstCaps * filter)180 gst_tcp_client_src_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
181 {
182 GstTCPClientSrc *src;
183 GstCaps *caps = NULL;
184
185 src = GST_TCP_CLIENT_SRC (bsrc);
186
187 caps = (filter ? gst_caps_ref (filter) : gst_caps_new_any ());
188
189 GST_DEBUG_OBJECT (src, "returning caps %" GST_PTR_FORMAT, caps);
190 g_assert (GST_IS_CAPS (caps));
191 return caps;
192 }
193
194 static GstFlowReturn
gst_tcp_client_src_create(GstPushSrc * psrc,GstBuffer ** outbuf)195 gst_tcp_client_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
196 {
197 GstTCPClientSrc *src;
198 GstFlowReturn ret = GST_FLOW_OK;
199 gssize rret;
200 GError *err = NULL;
201 GstMapInfo map;
202 gssize avail, read;
203
204 src = GST_TCP_CLIENT_SRC (psrc);
205
206 if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_CLIENT_SRC_OPEN))
207 goto wrong_state;
208
209 GST_LOG_OBJECT (src, "asked for a buffer");
210
211 /* read the buffer header */
212 avail = g_socket_get_available_bytes (src->socket);
213 if (avail < 0) {
214 goto get_available_error;
215 } else if (avail == 0) {
216 GIOCondition condition;
217
218 if (!g_socket_condition_wait (src->socket,
219 G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, src->cancellable, &err))
220 goto select_error;
221
222 condition =
223 g_socket_condition_check (src->socket,
224 G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP);
225
226 if ((condition & G_IO_ERR)) {
227 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
228 ("Socket in error state"));
229 *outbuf = NULL;
230 ret = GST_FLOW_ERROR;
231 goto done;
232 } else if ((condition & G_IO_HUP)) {
233 GST_DEBUG_OBJECT (src, "Connection closed");
234 *outbuf = NULL;
235 ret = GST_FLOW_EOS;
236 goto done;
237 }
238 avail = g_socket_get_available_bytes (src->socket);
239 if (avail < 0)
240 goto get_available_error;
241 }
242
243 if (avail > 0) {
244 read = MIN (avail, MAX_READ_SIZE);
245 *outbuf = gst_buffer_new_and_alloc (read);
246 gst_buffer_map (*outbuf, &map, GST_MAP_READWRITE);
247 rret =
248 g_socket_receive (src->socket, (gchar *) map.data, read,
249 src->cancellable, &err);
250 } else {
251 /* Connection closed */
252 *outbuf = NULL;
253 read = 0;
254 rret = 0;
255 }
256
257 if (rret == 0) {
258 GST_DEBUG_OBJECT (src, "Connection closed");
259 ret = GST_FLOW_EOS;
260 if (*outbuf) {
261 gst_buffer_unmap (*outbuf, &map);
262 gst_buffer_unref (*outbuf);
263 }
264 *outbuf = NULL;
265 } else if (rret < 0) {
266 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
267 ret = GST_FLOW_FLUSHING;
268 GST_DEBUG_OBJECT (src, "Cancelled reading from socket");
269 } else {
270 ret = GST_FLOW_ERROR;
271 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
272 ("Failed to read from socket: %s", err->message));
273 }
274 gst_buffer_unmap (*outbuf, &map);
275 gst_buffer_unref (*outbuf);
276 *outbuf = NULL;
277 } else {
278 ret = GST_FLOW_OK;
279 gst_buffer_unmap (*outbuf, &map);
280 gst_buffer_resize (*outbuf, 0, rret);
281
282 GST_LOG_OBJECT (src,
283 "Returning buffer from _get of size %" G_GSIZE_FORMAT ", ts %"
284 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
285 ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
286 gst_buffer_get_size (*outbuf),
287 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*outbuf)),
288 GST_TIME_ARGS (GST_BUFFER_DURATION (*outbuf)),
289 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf));
290 }
291 g_clear_error (&err);
292
293 done:
294 return ret;
295
296 select_error:
297 {
298 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
299 GST_DEBUG_OBJECT (src, "Cancelled");
300 ret = GST_FLOW_FLUSHING;
301 } else {
302 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
303 ("Select failed: %s", err->message));
304 ret = GST_FLOW_ERROR;
305 }
306 g_clear_error (&err);
307 return ret;
308 }
309 get_available_error:
310 {
311 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
312 ("Failed to get available bytes from socket"));
313 return GST_FLOW_ERROR;
314 }
315 wrong_state:
316 {
317 GST_DEBUG_OBJECT (src, "connection to closed, cannot read data");
318 return GST_FLOW_FLUSHING;
319 }
320 }
321
322 static void
gst_tcp_client_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)323 gst_tcp_client_src_set_property (GObject * object, guint prop_id,
324 const GValue * value, GParamSpec * pspec)
325 {
326 GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
327
328 switch (prop_id) {
329 case PROP_HOST:
330 if (!g_value_get_string (value)) {
331 g_warning ("host property cannot be NULL");
332 break;
333 }
334 g_free (tcpclientsrc->host);
335 tcpclientsrc->host = g_strdup (g_value_get_string (value));
336 break;
337 case PROP_PORT:
338 tcpclientsrc->port = g_value_get_int (value);
339 break;
340 case PROP_TIMEOUT:
341 tcpclientsrc->timeout = g_value_get_uint (value);
342 break;
343
344 default:
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
346 break;
347 }
348 }
349
350 static void
gst_tcp_client_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)351 gst_tcp_client_src_get_property (GObject * object, guint prop_id,
352 GValue * value, GParamSpec * pspec)
353 {
354 GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
355
356 switch (prop_id) {
357 case PROP_HOST:
358 g_value_set_string (value, tcpclientsrc->host);
359 break;
360 case PROP_PORT:
361 g_value_set_int (value, tcpclientsrc->port);
362 break;
363 case PROP_TIMEOUT:
364 g_value_set_uint (value, tcpclientsrc->timeout);
365 break;
366 default:
367 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
368 break;
369 }
370 }
371
372 /* create a socket for connecting to remote server */
373 static gboolean
gst_tcp_client_src_start(GstBaseSrc * bsrc)374 gst_tcp_client_src_start (GstBaseSrc * bsrc)
375 {
376 GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
377 GError *err = NULL;
378 GInetAddress *addr;
379 GSocketAddress *saddr;
380 GResolver *resolver;
381
382 /* look up name if we need to */
383 addr = g_inet_address_new_from_string (src->host);
384 if (!addr) {
385 GList *results;
386
387 resolver = g_resolver_get_default ();
388
389 results =
390 g_resolver_lookup_by_name (resolver, src->host, src->cancellable, &err);
391 if (!results)
392 goto name_resolve;
393 addr = G_INET_ADDRESS (g_object_ref (results->data));
394
395 g_resolver_free_addresses (results);
396 g_object_unref (resolver);
397 }
398 #ifndef GST_DISABLE_GST_DEBUG
399 {
400 gchar *ip = g_inet_address_to_string (addr);
401
402 GST_DEBUG_OBJECT (src, "IP address for host %s is %s", src->host, ip);
403 g_free (ip);
404 }
405 #endif
406
407 saddr = g_inet_socket_address_new (addr, src->port);
408 g_object_unref (addr);
409
410 /* create receiving client socket */
411 GST_DEBUG_OBJECT (src, "opening receiving client socket to %s:%d",
412 src->host, src->port);
413
414 src->socket =
415 g_socket_new (g_socket_address_get_family (saddr), G_SOCKET_TYPE_STREAM,
416 G_SOCKET_PROTOCOL_TCP, &err);
417 if (!src->socket)
418 goto no_socket;
419
420 g_socket_set_timeout (src->socket, src->timeout);
421
422 GST_DEBUG_OBJECT (src, "opened receiving client socket");
423 GST_OBJECT_FLAG_SET (src, GST_TCP_CLIENT_SRC_OPEN);
424
425 /* connect to server */
426 if (!g_socket_connect (src->socket, saddr, src->cancellable, &err))
427 goto connect_failed;
428
429 g_object_unref (saddr);
430
431 return TRUE;
432
433 no_socket:
434 {
435 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
436 ("Failed to create socket: %s", err->message));
437 g_clear_error (&err);
438 g_object_unref (saddr);
439 return FALSE;
440 }
441 name_resolve:
442 {
443 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
444 GST_DEBUG_OBJECT (src, "Cancelled name resolval");
445 } else {
446 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
447 ("Failed to resolve host '%s': %s", src->host, err->message));
448 }
449 g_clear_error (&err);
450 g_object_unref (resolver);
451 return FALSE;
452 }
453 connect_failed:
454 {
455 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
456 GST_DEBUG_OBJECT (src, "Cancelled connecting");
457 } else {
458 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
459 ("Failed to connect to host '%s:%d': %s", src->host, src->port,
460 err->message));
461 }
462 g_clear_error (&err);
463 g_object_unref (saddr);
464 gst_tcp_client_src_stop (GST_BASE_SRC (src));
465 return FALSE;
466 }
467 }
468
469 /* close the socket and associated resources
470 * unset OPEN flag
471 * used both to recover from errors and go to NULL state */
472 static gboolean
gst_tcp_client_src_stop(GstBaseSrc * bsrc)473 gst_tcp_client_src_stop (GstBaseSrc * bsrc)
474 {
475 GstTCPClientSrc *src;
476 GError *err = NULL;
477
478 src = GST_TCP_CLIENT_SRC (bsrc);
479
480 if (src->socket) {
481 GST_DEBUG_OBJECT (src, "closing socket");
482
483 if (!g_socket_close (src->socket, &err)) {
484 GST_ERROR_OBJECT (src, "Failed to close socket: %s", err->message);
485 g_clear_error (&err);
486 }
487 g_object_unref (src->socket);
488 src->socket = NULL;
489 }
490
491 GST_OBJECT_FLAG_UNSET (src, GST_TCP_CLIENT_SRC_OPEN);
492
493 return TRUE;
494 }
495
496 /* will be called only between calls to start() and stop() */
497 static gboolean
gst_tcp_client_src_unlock(GstBaseSrc * bsrc)498 gst_tcp_client_src_unlock (GstBaseSrc * bsrc)
499 {
500 GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
501
502 GST_DEBUG_OBJECT (src, "set to flushing");
503 g_cancellable_cancel (src->cancellable);
504
505 return TRUE;
506 }
507
508 /* will be called only between calls to start() and stop() */
509 static gboolean
gst_tcp_client_src_unlock_stop(GstBaseSrc * bsrc)510 gst_tcp_client_src_unlock_stop (GstBaseSrc * bsrc)
511 {
512 GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
513
514 GST_DEBUG_OBJECT (src, "unset flushing");
515 g_object_unref (src->cancellable);
516 src->cancellable = g_cancellable_new ();
517
518 return TRUE;
519 }
520