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-tcpserversrc
23 * @see_also: #tcpserversink
26 * <title>Example launch line</title>
29 * gst-launch tcpserversrc protocol=none port=3000 ! fdsink fd=2
31 * gst-launch fdsrc fd=1 ! tcpclientsink protocol=none port=3000
40 #include <gst/gst-i18n-plugin.h>
42 #include "gsttcpserversrc.h"
43 #include <string.h> /* memset */
45 #include <sys/ioctl.h>
49 GST_DEBUG_CATEGORY_STATIC (tcpserversrc_debug);
50 #define GST_CAT_DEFAULT tcpserversrc_debug
52 #define TCP_DEFAULT_LISTEN_HOST NULL /* listen on all interfaces */
53 #define TCP_BACKLOG 1 /* client connection queue */
56 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
70 GST_BOILERPLATE (GstTCPServerSrc, gst_tcp_server_src, GstPushSrc,
74 static void gst_tcp_server_src_finalize (GObject * gobject);
76 static gboolean gst_tcp_server_src_start (GstBaseSrc * bsrc);
77 static gboolean gst_tcp_server_src_stop (GstBaseSrc * bsrc);
78 static gboolean gst_tcp_server_src_unlock (GstBaseSrc * bsrc);
79 static GstFlowReturn gst_tcp_server_src_create (GstPushSrc * psrc,
82 static void gst_tcp_server_src_set_property (GObject * object, guint prop_id,
83 const GValue * value, GParamSpec * pspec);
84 static void gst_tcp_server_src_get_property (GObject * object, guint prop_id,
85 GValue * value, GParamSpec * pspec);
89 gst_tcp_server_src_base_init (gpointer g_class)
91 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
93 gst_element_class_add_pad_template (element_class,
94 gst_static_pad_template_get (&srctemplate));
96 gst_element_class_set_details_simple (element_class,
97 "TCP server source", "Source/Network",
98 "Receive data as a server over the network via TCP",
99 "Thomas Vander Stichele <thomas at apestaart dot org>");
103 gst_tcp_server_src_class_init (GstTCPServerSrcClass * klass)
105 GObjectClass *gobject_class;
106 GstBaseSrcClass *gstbasesrc_class;
107 GstPushSrcClass *gstpush_src_class;
109 gobject_class = (GObjectClass *) klass;
110 gstbasesrc_class = (GstBaseSrcClass *) klass;
111 gstpush_src_class = (GstPushSrcClass *) klass;
113 gobject_class->set_property = gst_tcp_server_src_set_property;
114 gobject_class->get_property = gst_tcp_server_src_get_property;
115 gobject_class->finalize = gst_tcp_server_src_finalize;
117 g_object_class_install_property (gobject_class, PROP_HOST,
118 g_param_spec_string ("host", "Host", "The hostname to listen as",
119 TCP_DEFAULT_LISTEN_HOST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
120 g_object_class_install_property (gobject_class, PROP_PORT,
121 g_param_spec_int ("port", "Port", "The port to listen to",
122 0, TCP_HIGHEST_PORT, TCP_DEFAULT_PORT,
123 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
125 gstbasesrc_class->start = gst_tcp_server_src_start;
126 gstbasesrc_class->stop = gst_tcp_server_src_stop;
127 gstbasesrc_class->unlock = gst_tcp_server_src_unlock;
129 gstpush_src_class->create = gst_tcp_server_src_create;
131 GST_DEBUG_CATEGORY_INIT (tcpserversrc_debug, "tcpserversrc", 0,
132 "TCP Server Source");
136 gst_tcp_server_src_init (GstTCPServerSrc * src, GstTCPServerSrcClass * g_class)
138 src->server_port = TCP_DEFAULT_PORT;
139 src->host = g_strdup (TCP_DEFAULT_HOST);
140 src->server_sock_fd.fd = -1;
141 src->client_sock_fd.fd = -1;
143 GST_OBJECT_FLAG_UNSET (src, GST_TCP_SERVER_SRC_OPEN);
147 gst_tcp_server_src_finalize (GObject * gobject)
149 GstTCPServerSrc *src = GST_TCP_SERVER_SRC (gobject);
153 G_OBJECT_CLASS (parent_class)->finalize (gobject);
157 gst_tcp_server_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
159 GstTCPServerSrc *src;
160 GstFlowReturn ret = GST_FLOW_OK;
162 src = GST_TCP_SERVER_SRC (psrc);
164 if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_SERVER_SRC_OPEN))
168 if (src->client_sock_fd.fd >= 0) {
169 /* if we have a client, wait for read */
170 gst_poll_fd_ctl_read (src->fdset, &src->server_sock_fd, FALSE);
171 gst_poll_fd_ctl_read (src->fdset, &src->client_sock_fd, TRUE);
173 /* else wait on server socket for connections */
174 gst_poll_fd_ctl_read (src->fdset, &src->server_sock_fd, TRUE);
177 /* no action (0) is an error too in our case */
178 if ((ret = gst_poll_wait (src->fdset, GST_CLOCK_TIME_NONE)) <= 0) {
179 if (ret == -1 && errno == EBUSY)
180 goto select_cancelled;
185 /* if we have no client socket we can accept one now */
186 if (src->client_sock_fd.fd < 0) {
187 if (gst_poll_fd_can_read (src->fdset, &src->server_sock_fd)) {
188 if ((src->client_sock_fd.fd =
189 accept (src->server_sock_fd.fd,
190 (struct sockaddr *) &src->client_sin,
191 &src->client_sin_len)) == -1)
194 gst_poll_add_fd (src->fdset, &src->client_sock_fd);
196 /* and restart now to poll the socket. */
200 GST_LOG_OBJECT (src, "asked for a buffer");
202 ret = gst_tcp_read_buffer (GST_ELEMENT (src), src->client_sock_fd.fd,
205 if (ret == GST_FLOW_OK) {
207 "Returning buffer from _get of size %d, ts %"
208 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
209 ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
210 GST_BUFFER_SIZE (*outbuf),
211 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*outbuf)),
212 GST_TIME_ARGS (GST_BUFFER_DURATION (*outbuf)),
213 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf));
220 GST_DEBUG_OBJECT (src, "connection to closed, cannot read data");
221 return GST_FLOW_WRONG_STATE;
225 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
226 ("Select error: %s", g_strerror (errno)));
227 return GST_FLOW_ERROR;
231 GST_DEBUG_OBJECT (src, "select canceled");
232 return GST_FLOW_WRONG_STATE;
236 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
237 ("Could not accept client on server socket: %s", g_strerror (errno)));
238 return GST_FLOW_ERROR;
243 gst_tcp_server_src_set_property (GObject * object, guint prop_id,
244 const GValue * value, GParamSpec * pspec)
246 GstTCPServerSrc *tcpserversrc = GST_TCP_SERVER_SRC (object);
250 if (!g_value_get_string (value)) {
251 g_warning ("host property cannot be NULL");
254 g_free (tcpserversrc->host);
255 tcpserversrc->host = g_strdup (g_value_get_string (value));
258 tcpserversrc->server_port = g_value_get_int (value);
262 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
268 gst_tcp_server_src_get_property (GObject * object, guint prop_id,
269 GValue * value, GParamSpec * pspec)
271 GstTCPServerSrc *tcpserversrc = GST_TCP_SERVER_SRC (object);
275 g_value_set_string (value, tcpserversrc->host);
278 g_value_set_int (value, tcpserversrc->server_port);
282 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289 gst_tcp_server_src_start (GstBaseSrc * bsrc)
292 GstTCPServerSrc *src = GST_TCP_SERVER_SRC (bsrc);
294 /* reset caps_received flag */
295 src->caps_received = FALSE;
297 /* create the server listener socket */
298 if ((src->server_sock_fd.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
301 GST_DEBUG_OBJECT (src, "opened receiving server socket with fd %d",
302 src->server_sock_fd.fd);
304 /* make address reusable */
306 if (setsockopt (src->server_sock_fd.fd, SOL_SOCKET, SO_REUSEADDR, &ret,
310 /* name the socket */
311 memset (&src->server_sin, 0, sizeof (src->server_sin));
312 src->server_sin.sin_family = AF_INET; /* network socket */
313 src->server_sin.sin_port = htons (src->server_port); /* on port */
317 if (!(host = gst_tcp_host_to_ip (GST_ELEMENT (src), src->host)))
319 src->server_sin.sin_addr.s_addr = inet_addr (host);
322 src->server_sin.sin_addr.s_addr = htonl (INADDR_ANY);
325 GST_DEBUG_OBJECT (src, "binding server socket to address");
326 if ((ret = bind (src->server_sock_fd.fd, (struct sockaddr *) &src->server_sin,
327 sizeof (src->server_sin))) < 0)
330 GST_DEBUG_OBJECT (src, "listening on server socket %d with queue of %d",
331 src->server_sock_fd.fd, TCP_BACKLOG);
333 if (listen (src->server_sock_fd.fd, TCP_BACKLOG) == -1)
336 /* create an fdset to keep track of our file descriptors */
337 if ((src->fdset = gst_poll_new (TRUE)) == NULL)
340 gst_poll_add_fd (src->fdset, &src->server_sock_fd);
342 GST_DEBUG_OBJECT (src, "received client");
344 GST_OBJECT_FLAG_SET (src, GST_TCP_SERVER_SRC_OPEN);
351 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
356 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
357 ("Could not setsockopt: %s", g_strerror (errno)));
358 gst_tcp_socket_close (&src->server_sock_fd);
363 gst_tcp_socket_close (&src->server_sock_fd);
368 gst_tcp_socket_close (&src->server_sock_fd);
371 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
372 ("bind failed: %s", g_strerror (errno)));
379 gst_tcp_socket_close (&src->server_sock_fd);
380 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
381 ("Could not listen on server socket: %s", g_strerror (errno)));
386 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
388 gst_tcp_socket_close (&src->server_sock_fd);
394 gst_tcp_server_src_stop (GstBaseSrc * bsrc)
396 GstTCPServerSrc *src = GST_TCP_SERVER_SRC (bsrc);
398 gst_poll_free (src->fdset);
401 gst_tcp_socket_close (&src->server_sock_fd);
402 gst_tcp_socket_close (&src->client_sock_fd);
404 GST_OBJECT_FLAG_UNSET (src, GST_TCP_SERVER_SRC_OPEN);
409 /* will be called only between calls to start() and stop() */
411 gst_tcp_server_src_unlock (GstBaseSrc * bsrc)
413 GstTCPServerSrc *src = GST_TCP_SERVER_SRC (bsrc);
415 gst_poll_set_flushing (src->fdset, TRUE);