expand tabs
[platform/upstream/gstreamer.git] / gst / tcp / gsttcpserversink.c
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  *
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.
9  *
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.
14  *
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.
19  */
20
21 /**
22  * SECTION:tcpserversink
23  * @short_description: a sink that acts as a TCP server and sends data to
24  *  multiple clients
25  * @see_also: #multifdsink
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 #include <gst/gst-i18n-plugin.h>
32
33 #include <sys/ioctl.h>
34
35 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
36 #include <sys/filio.h>
37 #endif
38
39 #include "gsttcp.h"
40 #include "gsttcpserversink.h"
41 #include "gsttcp-marshal.h"
42
43 #define TCP_BACKLOG             5
44
45 /* elementfactory information */
46 static GstElementDetails gst_tcp_server_sink_details =
47 GST_ELEMENT_DETAILS ("TCP Server sink",
48     "Sink/Network",
49     "Send data as a server over the network via TCP",
50     "Thomas Vander Stichele <thomas at apestaart dot org>");
51
52 GST_DEBUG_CATEGORY (tcpserversink_debug);
53 #define GST_CAT_DEFAULT (tcpserversink_debug)
54
55 enum
56 {
57   ARG_0,
58   ARG_HOST,
59   ARG_PORT,
60 };
61
62 static void gst_tcp_server_sink_finalize (GObject * gobject);
63
64 static gboolean gst_tcp_server_sink_handle_wait (GstMultiFdSink * sink,
65     GstFDSet * set);
66 static gboolean gst_tcp_server_sink_init_send (GstMultiFdSink * this);
67 static gboolean gst_tcp_server_sink_close (GstMultiFdSink * this);
68 static void gst_tcp_server_sink_removed (GstMultiFdSink * sink, int fd);
69
70 static void gst_tcp_server_sink_set_property (GObject * object, guint prop_id,
71     const GValue * value, GParamSpec * pspec);
72 static void gst_tcp_server_sink_get_property (GObject * object, guint prop_id,
73     GValue * value, GParamSpec * pspec);
74
75
76 GST_BOILERPLATE (GstTCPServerSink, gst_tcp_server_sink, GstMultiFdSink,
77     GST_TYPE_MULTI_FD_SINK);
78
79
80 static void
81 gst_tcp_server_sink_base_init (gpointer g_class)
82 {
83   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
84
85   gst_element_class_set_details (element_class, &gst_tcp_server_sink_details);
86 }
87
88 static void
89 gst_tcp_server_sink_class_init (GstTCPServerSinkClass * klass)
90 {
91   GObjectClass *gobject_class;
92   GstMultiFdSinkClass *gstmultifdsink_class;
93
94   gobject_class = (GObjectClass *) klass;
95   gstmultifdsink_class = (GstMultiFdSinkClass *) klass;
96
97   gobject_class->set_property = gst_tcp_server_sink_set_property;
98   gobject_class->get_property = gst_tcp_server_sink_get_property;
99   gobject_class->finalize = gst_tcp_server_sink_finalize;
100
101   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_HOST,
102       g_param_spec_string ("host", "host", "The host/IP to send the packets to",
103           TCP_DEFAULT_HOST, G_PARAM_READWRITE));
104   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PORT,
105       g_param_spec_int ("port", "port", "The port to send the packets to",
106           0, TCP_HIGHEST_PORT, TCP_DEFAULT_PORT, G_PARAM_READWRITE));
107
108   gstmultifdsink_class->init = gst_tcp_server_sink_init_send;
109   gstmultifdsink_class->wait = gst_tcp_server_sink_handle_wait;
110   gstmultifdsink_class->close = gst_tcp_server_sink_close;
111   gstmultifdsink_class->removed = gst_tcp_server_sink_removed;
112
113   GST_DEBUG_CATEGORY_INIT (tcpserversink_debug, "tcpserversink", 0, "TCP sink");
114 }
115
116 static void
117 gst_tcp_server_sink_init (GstTCPServerSink * this,
118     GstTCPServerSinkClass * klass)
119 {
120   this->server_port = TCP_DEFAULT_PORT;
121   /* should support as minimum 576 for IPV4 and 1500 for IPV6 */
122   /* this->mtu = 1500; */
123   this->host = g_strdup (TCP_DEFAULT_HOST);
124
125   this->server_sock.fd = -1;
126 }
127
128 static void
129 gst_tcp_server_sink_finalize (GObject * gobject)
130 {
131   GstTCPServerSink *this = GST_TCP_SERVER_SINK (gobject);
132
133   g_free (this->host);
134 }
135
136 /* handle a read request on the server,
137  * which indicates a new client connection */
138 static gboolean
139 gst_tcp_server_sink_handle_server_read (GstTCPServerSink * sink)
140 {
141   /* new client */
142   int client_sock_fd;
143   struct sockaddr_in client_address;
144   unsigned int client_address_len;
145
146   /* For some stupid reason, client_address and client_address_len has to be
147    * zeroed */
148   memset (&client_address, 0, sizeof (client_address));
149   client_address_len = 0;
150
151   client_sock_fd =
152       accept (sink->server_sock.fd, (struct sockaddr *) &client_address,
153       &client_address_len);
154   if (client_sock_fd == -1) {
155     GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
156         ("Could not accept client on server socket %d: %s (%d)",
157             sink->server_sock.fd, g_strerror (errno), errno));
158     return FALSE;
159   }
160
161   gst_multi_fd_sink_add (GST_MULTI_FD_SINK (sink), client_sock_fd);
162
163   GST_DEBUG_OBJECT (sink, "added new client ip %s with fd %d",
164       inet_ntoa (client_address.sin_addr), client_sock_fd);
165
166   return TRUE;
167 }
168
169 static void
170 gst_tcp_server_sink_removed (GstMultiFdSink * sink, int fd)
171 {
172   GstTCPServerSink *this = GST_TCP_SERVER_SINK (sink);
173
174   GST_LOG_OBJECT (this, "closing fd %d", fd);
175   if (close (fd) < 0) {
176     GST_WARNING_OBJECT (this, "error closing fd %d: %s", fd,
177         g_strerror (errno));
178   }
179 }
180
181 static gboolean
182 gst_tcp_server_sink_handle_wait (GstMultiFdSink * sink, GstFDSet * set)
183 {
184   GstTCPServerSink *this = GST_TCP_SERVER_SINK (sink);
185
186   if (gst_fdset_fd_can_read (set, &this->server_sock)) {
187     /* handle new client connection on server socket */
188     if (!gst_tcp_server_sink_handle_server_read (this)) {
189       GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
190           ("client connection failed: %s", g_strerror (errno)));
191       return FALSE;
192     }
193   }
194   return TRUE;
195 }
196
197 static void
198 gst_tcp_server_sink_set_property (GObject * object, guint prop_id,
199     const GValue * value, GParamSpec * pspec)
200 {
201   GstTCPServerSink *sink;
202
203   g_return_if_fail (GST_IS_TCP_SERVER_SINK (object));
204   sink = GST_TCP_SERVER_SINK (object);
205
206   switch (prop_id) {
207     case ARG_HOST:
208       if (!g_value_get_string (value)) {
209         g_warning ("host property cannot be NULL");
210         break;
211       }
212       g_free (sink->host);
213       sink->host = g_strdup (g_value_get_string (value));
214       break;
215     case ARG_PORT:
216       sink->server_port = g_value_get_int (value);
217       break;
218
219     default:
220       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221       break;
222   }
223 }
224
225 static void
226 gst_tcp_server_sink_get_property (GObject * object, guint prop_id,
227     GValue * value, GParamSpec * pspec)
228 {
229   GstTCPServerSink *sink;
230
231   g_return_if_fail (GST_IS_TCP_SERVER_SINK (object));
232   sink = GST_TCP_SERVER_SINK (object);
233
234   switch (prop_id) {
235     case ARG_HOST:
236       g_value_set_string (value, sink->host);
237       break;
238     case ARG_PORT:
239       g_value_set_int (value, sink->server_port);
240       break;
241
242     default:
243       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
244       break;
245   }
246 }
247
248
249 /* create a socket for sending to remote machine */
250 static gboolean
251 gst_tcp_server_sink_init_send (GstMultiFdSink * parent)
252 {
253   int ret;
254   GstTCPServerSink *this = GST_TCP_SERVER_SINK (parent);
255
256   /* create sending server socket */
257   if ((this->server_sock.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1) {
258     GST_ELEMENT_ERROR (this, RESOURCE, OPEN_WRITE, (NULL), GST_ERROR_SYSTEM);
259     return FALSE;
260   }
261   GST_DEBUG_OBJECT (this, "opened sending server socket with fd %d",
262       this->server_sock.fd);
263
264   /* make address reusable */
265   ret = 1;
266   if (setsockopt (this->server_sock.fd, SOL_SOCKET, SO_REUSEADDR,
267           (void *) &ret, sizeof (ret)) < 0) {
268     gst_tcp_socket_close (&this->server_sock.fd);
269     GST_ELEMENT_ERROR (this, RESOURCE, SETTINGS, (NULL),
270         ("Could not setsockopt: %s", g_strerror (errno)));
271     return FALSE;
272   }
273   /* keep connection alive; avoids SIGPIPE during write */
274   ret = 1;
275   if (setsockopt (this->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE,
276           (void *) &ret, sizeof (ret)) < 0) {
277     gst_tcp_socket_close (&this->server_sock.fd);
278     GST_ELEMENT_ERROR (this, RESOURCE, SETTINGS, (NULL),
279         ("Could not setsockopt: %s", g_strerror (errno)));
280     return FALSE;
281   }
282
283   /* name the socket */
284   memset (&this->server_sin, 0, sizeof (this->server_sin));
285   this->server_sin.sin_family = AF_INET;        /* network socket */
286   this->server_sin.sin_port = htons (this->server_port);        /* on port */
287   this->server_sin.sin_addr.s_addr = htonl (INADDR_ANY);        /* for hosts */
288
289   /* bind it */
290   GST_DEBUG_OBJECT (this, "binding server socket to address");
291   ret = bind (this->server_sock.fd, (struct sockaddr *) &this->server_sin,
292       sizeof (this->server_sin));
293
294   if (ret) {
295     gst_tcp_socket_close (&this->server_sock.fd);
296     switch (errno) {
297       default:
298         GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
299             ("bind on port %d failed: %s", this->server_port,
300                 g_strerror (errno)));
301         return FALSE;
302         break;
303     }
304   }
305
306   /* set the server socket to nonblocking */
307   fcntl (this->server_sock.fd, F_SETFL, O_NONBLOCK);
308
309   GST_DEBUG_OBJECT (this, "listening on server socket %d with queue of %d",
310       this->server_sock.fd, TCP_BACKLOG);
311   if (listen (this->server_sock.fd, TCP_BACKLOG) == -1) {
312     gst_tcp_socket_close (&this->server_sock.fd);
313     GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
314         ("Could not listen on server socket: %s", g_strerror (errno)));
315     return FALSE;
316   }
317   GST_DEBUG_OBJECT (this,
318       "listened on server socket %d, returning from connection setup",
319       this->server_sock.fd);
320
321   gst_fdset_add_fd (parent->fdset, &this->server_sock);
322   gst_fdset_fd_ctl_read (parent->fdset, &this->server_sock, TRUE);
323
324   return TRUE;
325 }
326
327 static gboolean
328 gst_tcp_server_sink_close (GstMultiFdSink * parent)
329 {
330   GstTCPServerSink *this = GST_TCP_SERVER_SINK (parent);
331
332   if (this->server_sock.fd != -1) {
333     gst_fdset_remove_fd (parent->fdset, &this->server_sock);
334
335     close (this->server_sock.fd);
336     this->server_sock.fd = -1;
337   }
338   return TRUE;
339 }