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.
24 #include <gst/gst-i18n-plugin.h>
26 #include <sys/ioctl.h>
28 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
29 #include <sys/filio.h>
32 #include "gsttcpserversink.h"
33 #include "gsttcp-marshal.h"
35 #define TCP_DEFAULT_HOST "127.0.0.1"
36 #define TCP_DEFAULT_PORT 4953
39 /* elementfactory information */
40 static GstElementDetails gst_tcpserversink_details =
41 GST_ELEMENT_DETAILS ("TCP Server sink",
43 "Send data as a server over the network via TCP",
44 "Thomas Vander Stichele <thomas at apestaart dot org>");
46 GST_DEBUG_CATEGORY (tcpserversink_debug);
47 #define GST_CAT_DEFAULT (tcpserversink_debug)
56 static void gst_tcpserversink_base_init (gpointer g_class);
57 static void gst_tcpserversink_class_init (GstTCPServerSink * klass);
58 static void gst_tcpserversink_init (GstTCPServerSink * tcpserversink);
60 static gboolean gst_tcpserversink_handle_select (GstMultiFdSink * sink,
61 fd_set * readfds, fd_set * writefds);
62 static gboolean gst_tcpserversink_init_send (GstMultiFdSink * this);
63 static gboolean gst_tcpserversink_close (GstMultiFdSink * this);
65 static void gst_tcpserversink_set_property (GObject * object, guint prop_id,
66 const GValue * value, GParamSpec * pspec);
67 static void gst_tcpserversink_get_property (GObject * object, guint prop_id,
68 GValue * value, GParamSpec * pspec);
71 static GstMultiFdSinkClass *parent_class = NULL;
73 //static guint gst_tcpserversink_signals[LAST_SIGNAL] = { 0 };
76 gst_tcpserversink_get_type (void)
78 static GType tcpserversink_type = 0;
81 if (!tcpserversink_type) {
82 static const GTypeInfo tcpserversink_info = {
83 sizeof (GstTCPServerSinkClass),
84 gst_tcpserversink_base_init,
86 (GClassInitFunc) gst_tcpserversink_class_init,
89 sizeof (GstTCPServerSink),
91 (GInstanceInitFunc) gst_tcpserversink_init,
96 g_type_register_static (GST_TYPE_MULTIFDSINK, "GstTCPServerSink",
97 &tcpserversink_info, 0);
99 return tcpserversink_type;
103 gst_tcpserversink_base_init (gpointer g_class)
105 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
107 gst_element_class_set_details (element_class, &gst_tcpserversink_details);
111 gst_tcpserversink_class_init (GstTCPServerSink * klass)
113 GObjectClass *gobject_class;
114 GstElementClass *gstelement_class;
115 GstMultiFdSinkClass *gstmultifdsink_class;
117 gobject_class = (GObjectClass *) klass;
118 gstelement_class = (GstElementClass *) klass;
119 gstmultifdsink_class = (GstMultiFdSinkClass *) klass;
121 parent_class = g_type_class_ref (GST_TYPE_MULTIFDSINK);
123 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_HOST,
124 g_param_spec_string ("host", "host", "The host/IP to send the packets to",
125 TCP_DEFAULT_HOST, G_PARAM_READWRITE));
126 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PORT,
127 g_param_spec_int ("port", "port", "The port to send the packets to",
128 0, 32768, TCP_DEFAULT_PORT, G_PARAM_READWRITE));
130 gobject_class->set_property = gst_tcpserversink_set_property;
131 gobject_class->get_property = gst_tcpserversink_get_property;
133 gstmultifdsink_class->init = gst_tcpserversink_init_send;
134 gstmultifdsink_class->select = gst_tcpserversink_handle_select;
135 gstmultifdsink_class->close = gst_tcpserversink_close;
137 GST_DEBUG_CATEGORY_INIT (tcpserversink_debug, "tcpserversink", 0, "TCP sink");
141 gst_tcpserversink_init (GstTCPServerSink * this)
143 this->server_port = TCP_DEFAULT_PORT;
144 /* should support as minimum 576 for IPV4 and 1500 for IPV6 */
145 /* this->mtu = 1500; */
147 this->server_sock_fd = -1;
150 /* handle a read request on the server,
151 * which indicates a new client connection */
153 gst_tcpserversink_handle_server_read (GstTCPServerSink * sink)
157 struct sockaddr_in client_address;
158 int client_address_len;
159 GstTCPClient *client;
160 GstMultiFdSink *parent = GST_MULTIFDSINK (sink);
163 accept (sink->server_sock_fd, (struct sockaddr *) &client_address,
164 &client_address_len);
165 if (client_sock_fd == -1) {
166 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
167 ("Could not accept client on server socket: %s", g_strerror (errno)));
171 /* create client datastructure */
172 client = g_new0 (GstTCPClient, 1);
173 client->fd = client_sock_fd;
175 client->bufoffset = 0;
176 client->sending = NULL;
178 g_mutex_lock (parent->clientslock);
179 parent->clients = g_list_prepend (parent->clients, client);
180 g_mutex_unlock (parent->clientslock);
182 /* we always read from a client */
183 FD_SET (client_sock_fd, &parent->readfds);
185 /* set the socket to non blocking */
186 fcntl (client_sock_fd, F_SETFL, O_NONBLOCK);
188 GST_DEBUG_OBJECT (sink, "added new client ip %s with fd %d",
189 inet_ntoa (client_address.sin_addr), client_sock_fd);
195 gst_tcpserversink_handle_select (GstMultiFdSink * sink,
196 fd_set * readfds, fd_set * writefds)
198 GstTCPServerSink *this = GST_TCPSERVERSINK (sink);
200 if (FD_ISSET (this->server_sock_fd, readfds)) {
201 /* handle new client connection on server socket */
202 if (!gst_tcpserversink_handle_server_read (this)) {
203 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
204 ("client connection failed: %s", g_strerror (errno)));
212 gst_tcpserversink_set_property (GObject * object, guint prop_id,
213 const GValue * value, GParamSpec * pspec)
215 GstTCPServerSink *sink;
217 g_return_if_fail (GST_IS_TCPSERVERSINK (object));
218 sink = GST_TCPSERVERSINK (object);
223 sink->host = g_strdup (g_value_get_string (value));
226 sink->server_port = g_value_get_int (value);
230 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236 gst_tcpserversink_get_property (GObject * object, guint prop_id, GValue * value,
239 GstTCPServerSink *sink;
241 g_return_if_fail (GST_IS_TCPSERVERSINK (object));
242 sink = GST_TCPSERVERSINK (object);
246 g_value_set_string (value, sink->host);
249 g_value_set_int (value, sink->server_port);
253 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259 /* create a socket for sending to remote machine */
261 gst_tcpserversink_init_send (GstMultiFdSink * parent)
264 GstTCPServerSink *this = GST_TCPSERVERSINK (parent);
266 /* create sending server socket */
267 if ((this->server_sock_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1) {
268 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_WRITE, (NULL), GST_ERROR_SYSTEM);
271 GST_DEBUG_OBJECT (this, "opened sending server socket with fd %d",
272 this->server_sock_fd);
274 /* make address reusable */
275 if (setsockopt (this->server_sock_fd, SOL_SOCKET, SO_REUSEADDR, &ret,
277 GST_ELEMENT_ERROR (this, RESOURCE, SETTINGS, (NULL),
278 ("Could not setsockopt: %s", g_strerror (errno)));
281 /* keep connection alive; avoids SIGPIPE during write */
282 if (setsockopt (this->server_sock_fd, SOL_SOCKET, SO_KEEPALIVE, &ret,
284 GST_ELEMENT_ERROR (this, RESOURCE, SETTINGS, (NULL),
285 ("Could not setsockopt: %s", g_strerror (errno)));
289 /* name the socket */
290 memset (&this->server_sin, 0, sizeof (this->server_sin));
291 this->server_sin.sin_family = AF_INET; /* network socket */
292 this->server_sin.sin_port = htons (this->server_port); /* on port */
293 this->server_sin.sin_addr.s_addr = htonl (INADDR_ANY); /* for hosts */
296 GST_DEBUG_OBJECT (this, "binding server socket to address");
297 ret = bind (this->server_sock_fd, (struct sockaddr *) &this->server_sin,
298 sizeof (this->server_sin));
303 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
304 ("bind failed: %s", g_strerror (errno)));
310 /* set the server socket to nonblocking */
311 fcntl (this->server_sock_fd, F_SETFL, O_NONBLOCK);
313 GST_DEBUG_OBJECT (this, "listening on server socket %d with queue of %d",
314 this->server_sock_fd, TCP_BACKLOG);
315 if (listen (this->server_sock_fd, TCP_BACKLOG) == -1) {
316 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
317 ("Could not listen on server socket: %s", g_strerror (errno)));
320 GST_DEBUG_OBJECT (this,
321 "listened on server socket %d, returning from connection setup",
322 this->server_sock_fd);
324 FD_SET (this->server_sock_fd, &parent->readfds);
330 gst_tcpserversink_close (GstMultiFdSink * parent)
332 GstTCPServerSink *this = GST_TCPSERVERSINK (parent);
334 if (this->server_sock_fd != -1) {
335 close (this->server_sock_fd);
336 this->server_sock_fd = -1;