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-tcpserversrc
25 * @see_also: #tcpserversink
28 * <title>Example launch line</title>
31 * gst-launch tcpserversrc port=3000 ! fdsink fd=2
33 * gst-launch fdsrc fd=1 ! tcpclientsink port=3000
42 #include <gst/gst-i18n-plugin.h>
44 #include "gsttcpserversrc.h"
46 GST_DEBUG_CATEGORY_STATIC (tcpserversrc_debug);
47 #define GST_CAT_DEFAULT tcpserversrc_debug
49 #define TCP_DEFAULT_LISTEN_HOST NULL /* listen on all interfaces */
50 #define TCP_BACKLOG 1 /* client connection queue */
52 #define MAX_READ_SIZE 4 * 1024
54 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
67 #define gst_tcp_server_src_parent_class parent_class
68 G_DEFINE_TYPE (GstTCPServerSrc, gst_tcp_server_src, GST_TYPE_PUSH_SRC);
70 static void gst_tcp_server_src_finalize (GObject * gobject);
72 static gboolean gst_tcp_server_src_start (GstBaseSrc * bsrc);
73 static gboolean gst_tcp_server_src_stop (GstBaseSrc * bsrc);
74 static gboolean gst_tcp_server_src_unlock (GstBaseSrc * bsrc);
75 static gboolean gst_tcp_server_src_unlock_stop (GstBaseSrc * bsrc);
76 static GstFlowReturn gst_tcp_server_src_create (GstPushSrc * psrc,
79 static void gst_tcp_server_src_set_property (GObject * object, guint prop_id,
80 const GValue * value, GParamSpec * pspec);
81 static void gst_tcp_server_src_get_property (GObject * object, guint prop_id,
82 GValue * value, GParamSpec * pspec);
85 gst_tcp_server_src_class_init (GstTCPServerSrcClass * klass)
87 GObjectClass *gobject_class;
88 GstElementClass *gstelement_class;
89 GstBaseSrcClass *gstbasesrc_class;
90 GstPushSrcClass *gstpush_src_class;
92 gobject_class = (GObjectClass *) klass;
93 gstelement_class = (GstElementClass *) klass;
94 gstbasesrc_class = (GstBaseSrcClass *) klass;
95 gstpush_src_class = (GstPushSrcClass *) klass;
97 gobject_class->set_property = gst_tcp_server_src_set_property;
98 gobject_class->get_property = gst_tcp_server_src_get_property;
99 gobject_class->finalize = gst_tcp_server_src_finalize;
101 g_object_class_install_property (gobject_class, PROP_HOST,
102 g_param_spec_string ("host", "Host", "The hostname to listen as",
103 TCP_DEFAULT_LISTEN_HOST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
104 g_object_class_install_property (gobject_class, PROP_PORT,
105 g_param_spec_int ("port", "Port",
106 "The port to listen to (0=random available port)",
107 0, TCP_HIGHEST_PORT, TCP_DEFAULT_PORT,
108 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
110 * GstTCPServerSrc:current-port:
112 * The port number the socket is currently bound to. Applications can use
113 * this property to retrieve the port number actually bound to in case
114 * the port requested was 0 (=allocate a random available port).
118 g_object_class_install_property (gobject_class, PROP_CURRENT_PORT,
119 g_param_spec_int ("current-port", "current-port",
120 "The port number the socket is currently bound to", 0,
121 TCP_HIGHEST_PORT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
123 gst_element_class_add_pad_template (gstelement_class,
124 gst_static_pad_template_get (&srctemplate));
126 gst_element_class_set_static_metadata (gstelement_class,
127 "TCP server source", "Source/Network",
128 "Receive data as a server over the network via TCP",
129 "Thomas Vander Stichele <thomas at apestaart dot org>");
131 gstbasesrc_class->start = gst_tcp_server_src_start;
132 gstbasesrc_class->stop = gst_tcp_server_src_stop;
133 gstbasesrc_class->unlock = gst_tcp_server_src_unlock;
134 gstbasesrc_class->unlock_stop = gst_tcp_server_src_unlock_stop;
136 gstpush_src_class->create = gst_tcp_server_src_create;
138 GST_DEBUG_CATEGORY_INIT (tcpserversrc_debug, "tcpserversrc", 0,
139 "TCP Server Source");
143 gst_tcp_server_src_init (GstTCPServerSrc * src)
145 src->server_port = TCP_DEFAULT_PORT;
146 src->host = g_strdup (TCP_DEFAULT_HOST);
147 src->server_socket = NULL;
148 src->client_socket = NULL;
149 src->cancellable = g_cancellable_new ();
151 GST_OBJECT_FLAG_UNSET (src, GST_TCP_SERVER_SRC_OPEN);
155 gst_tcp_server_src_finalize (GObject * gobject)
157 GstTCPServerSrc *src = GST_TCP_SERVER_SRC (gobject);
159 if (src->cancellable)
160 g_object_unref (src->cancellable);
161 src->cancellable = NULL;
162 if (src->server_socket)
163 g_object_unref (src->server_socket);
164 src->server_socket = NULL;
165 if (src->client_socket)
166 g_object_unref (src->client_socket);
167 src->client_socket = NULL;
172 G_OBJECT_CLASS (parent_class)->finalize (gobject);
176 gst_tcp_server_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
178 GstTCPServerSrc *src;
179 GstFlowReturn ret = GST_FLOW_OK;
185 src = GST_TCP_SERVER_SRC (psrc);
187 if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_SERVER_SRC_OPEN))
190 if (!src->client_socket) {
191 /* wait on server socket for connections */
193 g_socket_accept (src->server_socket, src->cancellable, &err);
194 if (!src->client_socket)
196 /* now read from the socket. */
199 /* if we have a client, wait for read */
200 GST_LOG_OBJECT (src, "asked for a buffer");
202 /* read the buffer header */
203 avail = g_socket_get_available_bytes (src->client_socket);
205 goto get_available_error;
206 } else if (avail == 0) {
207 GIOCondition condition;
209 if (!g_socket_condition_wait (src->client_socket,
210 G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, src->cancellable, &err))
214 g_socket_condition_check (src->client_socket,
215 G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP);
217 if ((condition & G_IO_ERR)) {
218 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
219 ("Socket in error state"));
221 ret = GST_FLOW_ERROR;
223 } else if ((condition & G_IO_HUP)) {
224 GST_DEBUG_OBJECT (src, "Connection closed");
229 avail = g_socket_get_available_bytes (src->client_socket);
231 goto get_available_error;
235 read = MIN (avail, MAX_READ_SIZE);
236 *outbuf = gst_buffer_new_and_alloc (read);
237 gst_buffer_map (*outbuf, &map, GST_MAP_READWRITE);
239 g_socket_receive (src->client_socket, (gchar *) map.data, read,
240 src->cancellable, &err);
242 /* Connection closed */
249 GST_DEBUG_OBJECT (src, "Connection closed");
252 gst_buffer_unmap (*outbuf, &map);
253 gst_buffer_unref (*outbuf);
256 } else if (rret < 0) {
257 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
258 ret = GST_FLOW_FLUSHING;
259 GST_DEBUG_OBJECT (src, "Cancelled reading from socket");
261 ret = GST_FLOW_ERROR;
262 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
263 ("Failed to read from socket: %s", err->message));
265 gst_buffer_unmap (*outbuf, &map);
266 gst_buffer_unref (*outbuf);
270 gst_buffer_unmap (*outbuf, &map);
271 gst_buffer_resize (*outbuf, 0, rret);
274 "Returning buffer from _get of size %" G_GSIZE_FORMAT ", ts %"
275 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
276 ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
277 gst_buffer_get_size (*outbuf),
278 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*outbuf)),
279 GST_TIME_ARGS (GST_BUFFER_DURATION (*outbuf)),
280 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf));
282 g_clear_error (&err);
289 GST_DEBUG_OBJECT (src, "connection to closed, cannot read data");
290 return GST_FLOW_FLUSHING;
294 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
295 GST_DEBUG_OBJECT (src, "Cancelled accepting of client");
297 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
298 ("Failed to accept client: %s", err->message));
300 g_clear_error (&err);
301 return GST_FLOW_ERROR;
305 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
306 ("Select failed: %s", err->message));
307 g_clear_error (&err);
308 return GST_FLOW_ERROR;
312 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
313 ("Failed to get available bytes from socket"));
314 return GST_FLOW_ERROR;
319 gst_tcp_server_src_set_property (GObject * object, guint prop_id,
320 const GValue * value, GParamSpec * pspec)
322 GstTCPServerSrc *tcpserversrc = GST_TCP_SERVER_SRC (object);
326 if (!g_value_get_string (value)) {
327 g_warning ("host property cannot be NULL");
330 g_free (tcpserversrc->host);
331 tcpserversrc->host = g_strdup (g_value_get_string (value));
334 tcpserversrc->server_port = g_value_get_int (value);
338 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
344 gst_tcp_server_src_get_property (GObject * object, guint prop_id,
345 GValue * value, GParamSpec * pspec)
347 GstTCPServerSrc *tcpserversrc = GST_TCP_SERVER_SRC (object);
351 g_value_set_string (value, tcpserversrc->host);
354 g_value_set_int (value, tcpserversrc->server_port);
356 case PROP_CURRENT_PORT:
357 g_value_set_int (value, g_atomic_int_get (&tcpserversrc->current_port));
360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
367 gst_tcp_server_src_start (GstBaseSrc * bsrc)
369 GstTCPServerSrc *src = GST_TCP_SERVER_SRC (bsrc);
372 GSocketAddress *saddr;
376 /* look up name if we need to */
377 addr = g_inet_address_new_from_string (src->host);
381 resolver = g_resolver_get_default ();
384 g_resolver_lookup_by_name (resolver, src->host, src->cancellable, &err);
387 addr = G_INET_ADDRESS (g_object_ref (results->data));
389 g_resolver_free_addresses (results);
390 g_object_unref (resolver);
392 #ifndef GST_DISABLE_GST_DEBUG
394 gchar *ip = g_inet_address_to_string (addr);
396 GST_DEBUG_OBJECT (src, "IP address for host %s is %s", src->host, ip);
401 saddr = g_inet_socket_address_new (addr, src->server_port);
402 g_object_unref (addr);
404 /* create the server listener socket */
406 g_socket_new (g_socket_address_get_family (saddr), G_SOCKET_TYPE_STREAM,
407 G_SOCKET_PROTOCOL_TCP, &err);
408 if (!src->server_socket)
411 GST_DEBUG_OBJECT (src, "opened receiving server socket");
414 GST_DEBUG_OBJECT (src, "binding server socket to address");
415 if (!g_socket_bind (src->server_socket, saddr, TRUE, &err))
418 g_object_unref (saddr);
420 GST_DEBUG_OBJECT (src, "listening on server socket");
422 g_socket_set_listen_backlog (src->server_socket, TCP_BACKLOG);
424 if (!g_socket_listen (src->server_socket, &err))
427 GST_OBJECT_FLAG_SET (src, GST_TCP_SERVER_SRC_OPEN);
429 if (src->server_port == 0) {
430 saddr = g_socket_get_local_address (src->server_socket, NULL);
431 bound_port = g_inet_socket_address_get_port ((GInetSocketAddress *) saddr);
432 g_object_unref (saddr);
434 bound_port = src->server_port;
437 GST_DEBUG_OBJECT (src, "listening on port %d", bound_port);
439 g_atomic_int_set (&src->current_port, bound_port);
440 g_object_notify (G_OBJECT (src), "current-port");
447 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
448 ("Failed to create socket: %s", err->message));
449 g_clear_error (&err);
450 g_object_unref (saddr);
455 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
456 GST_DEBUG_OBJECT (src, "Cancelled name resolval");
458 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
459 ("Failed to resolve host '%s': %s", src->host, err->message));
461 g_clear_error (&err);
462 g_object_unref (resolver);
467 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
468 GST_DEBUG_OBJECT (src, "Cancelled binding");
470 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
471 ("Failed to bind on host '%s:%d': %s", src->host, src->server_port,
474 g_clear_error (&err);
475 g_object_unref (saddr);
476 gst_tcp_server_src_stop (GST_BASE_SRC (src));
481 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
482 GST_DEBUG_OBJECT (src, "Cancelled listening");
484 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
485 ("Failed to listen on host '%s:%d': %s", src->host, src->server_port,
488 g_clear_error (&err);
489 gst_tcp_server_src_stop (GST_BASE_SRC (src));
495 gst_tcp_server_src_stop (GstBaseSrc * bsrc)
497 GstTCPServerSrc *src = GST_TCP_SERVER_SRC (bsrc);
500 if (src->client_socket) {
501 GST_DEBUG_OBJECT (src, "closing socket");
503 if (!g_socket_close (src->client_socket, &err)) {
504 GST_ERROR_OBJECT (src, "Failed to close socket: %s", err->message);
505 g_clear_error (&err);
507 g_object_unref (src->client_socket);
508 src->client_socket = NULL;
511 if (src->server_socket) {
512 GST_DEBUG_OBJECT (src, "closing socket");
514 if (!g_socket_close (src->server_socket, &err)) {
515 GST_ERROR_OBJECT (src, "Failed to close socket: %s", err->message);
516 g_clear_error (&err);
518 g_object_unref (src->server_socket);
519 src->server_socket = NULL;
521 g_atomic_int_set (&src->current_port, 0);
522 g_object_notify (G_OBJECT (src), "current-port");
525 GST_OBJECT_FLAG_UNSET (src, GST_TCP_SERVER_SRC_OPEN);
530 /* will be called only between calls to start() and stop() */
532 gst_tcp_server_src_unlock (GstBaseSrc * bsrc)
534 GstTCPServerSrc *src = GST_TCP_SERVER_SRC (bsrc);
536 g_cancellable_cancel (src->cancellable);
542 gst_tcp_server_src_unlock_stop (GstBaseSrc * bsrc)
544 GstTCPServerSrc *src = GST_TCP_SERVER_SRC (bsrc);
546 g_cancellable_reset (src->cancellable);