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