gst/tcp/: Added multifdsink, made tcpserversink a subclass of fdsink, removed one...
[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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 #include <gst/gst-i18n-plugin.h>
25
26 #include <sys/ioctl.h>
27
28 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
29 #include <sys/filio.h>
30 #endif
31
32 #include "gsttcpserversink.h"
33 #include "gsttcp-marshal.h"
34
35 #define TCP_DEFAULT_HOST        "127.0.0.1"
36 #define TCP_DEFAULT_PORT        4953
37 #define TCP_BACKLOG             5
38
39 /* elementfactory information */
40 static GstElementDetails gst_tcpserversink_details =
41 GST_ELEMENT_DETAILS ("TCP Server sink",
42     "Sink/Network",
43     "Send data as a server over the network via TCP",
44     "Thomas Vander Stichele <thomas at apestaart dot org>");
45
46 GST_DEBUG_CATEGORY (tcpserversink_debug);
47 #define GST_CAT_DEFAULT (tcpserversink_debug)
48
49 enum
50 {
51   ARG_0,
52   ARG_HOST,
53   ARG_PORT,
54 };
55
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);
59
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);
64
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);
69
70
71 static GstMultiFdSinkClass *parent_class = NULL;
72
73 //static guint gst_tcpserversink_signals[LAST_SIGNAL] = { 0 };
74
75 GType
76 gst_tcpserversink_get_type (void)
77 {
78   static GType tcpserversink_type = 0;
79
80
81   if (!tcpserversink_type) {
82     static const GTypeInfo tcpserversink_info = {
83       sizeof (GstTCPServerSinkClass),
84       gst_tcpserversink_base_init,
85       NULL,
86       (GClassInitFunc) gst_tcpserversink_class_init,
87       NULL,
88       NULL,
89       sizeof (GstTCPServerSink),
90       0,
91       (GInstanceInitFunc) gst_tcpserversink_init,
92       NULL
93     };
94
95     tcpserversink_type =
96         g_type_register_static (GST_TYPE_MULTIFDSINK, "GstTCPServerSink",
97         &tcpserversink_info, 0);
98   }
99   return tcpserversink_type;
100 }
101
102 static void
103 gst_tcpserversink_base_init (gpointer g_class)
104 {
105   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
106
107   gst_element_class_set_details (element_class, &gst_tcpserversink_details);
108 }
109
110 static void
111 gst_tcpserversink_class_init (GstTCPServerSink * klass)
112 {
113   GObjectClass *gobject_class;
114   GstElementClass *gstelement_class;
115   GstMultiFdSinkClass *gstmultifdsink_class;
116
117   gobject_class = (GObjectClass *) klass;
118   gstelement_class = (GstElementClass *) klass;
119   gstmultifdsink_class = (GstMultiFdSinkClass *) klass;
120
121   parent_class = g_type_class_ref (GST_TYPE_MULTIFDSINK);
122
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));
129
130   gobject_class->set_property = gst_tcpserversink_set_property;
131   gobject_class->get_property = gst_tcpserversink_get_property;
132
133   gstmultifdsink_class->init = gst_tcpserversink_init_send;
134   gstmultifdsink_class->select = gst_tcpserversink_handle_select;
135   gstmultifdsink_class->close = gst_tcpserversink_close;
136
137   GST_DEBUG_CATEGORY_INIT (tcpserversink_debug, "tcpserversink", 0, "TCP sink");
138 }
139
140 static void
141 gst_tcpserversink_init (GstTCPServerSink * this)
142 {
143   this->server_port = TCP_DEFAULT_PORT;
144   /* should support as minimum 576 for IPV4 and 1500 for IPV6 */
145   /* this->mtu = 1500; */
146
147   this->server_sock_fd = -1;
148 }
149
150 /* handle a read request on the server,
151  * which indicates a new client connection */
152 static gboolean
153 gst_tcpserversink_handle_server_read (GstTCPServerSink * sink)
154 {
155   /* new client */
156   int client_sock_fd;
157   struct sockaddr_in client_address;
158   int client_address_len;
159   GstTCPClient *client;
160   GstMultiFdSink *parent = GST_MULTIFDSINK (sink);
161
162   client_sock_fd =
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)));
168     return FALSE;
169   }
170
171   /* create client datastructure */
172   client = g_new0 (GstTCPClient, 1);
173   client->fd = client_sock_fd;
174   client->bufpos = -1;
175   client->bufoffset = 0;
176   client->sending = NULL;
177
178   g_mutex_lock (parent->clientslock);
179   parent->clients = g_list_prepend (parent->clients, client);
180   g_mutex_unlock (parent->clientslock);
181
182   /* we always read from a client */
183   FD_SET (client_sock_fd, &parent->readfds);
184
185   /* set the socket to non blocking */
186   fcntl (client_sock_fd, F_SETFL, O_NONBLOCK);
187
188   GST_DEBUG_OBJECT (sink, "added new client ip %s with fd %d",
189       inet_ntoa (client_address.sin_addr), client_sock_fd);
190
191   return TRUE;
192 }
193
194 static gboolean
195 gst_tcpserversink_handle_select (GstMultiFdSink * sink,
196     fd_set * readfds, fd_set * writefds)
197 {
198   GstTCPServerSink *this = GST_TCPSERVERSINK (sink);
199
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)));
205       return FALSE;
206     }
207   }
208   return TRUE;
209 }
210
211 static void
212 gst_tcpserversink_set_property (GObject * object, guint prop_id,
213     const GValue * value, GParamSpec * pspec)
214 {
215   GstTCPServerSink *sink;
216
217   g_return_if_fail (GST_IS_TCPSERVERSINK (object));
218   sink = GST_TCPSERVERSINK (object);
219
220   switch (prop_id) {
221     case ARG_HOST:
222       g_free (sink->host);
223       sink->host = g_strdup (g_value_get_string (value));
224       break;
225     case ARG_PORT:
226       sink->server_port = g_value_get_int (value);
227       break;
228
229     default:
230       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231       break;
232   }
233 }
234
235 static void
236 gst_tcpserversink_get_property (GObject * object, guint prop_id, GValue * value,
237     GParamSpec * pspec)
238 {
239   GstTCPServerSink *sink;
240
241   g_return_if_fail (GST_IS_TCPSERVERSINK (object));
242   sink = GST_TCPSERVERSINK (object);
243
244   switch (prop_id) {
245     case ARG_HOST:
246       g_value_set_string (value, sink->host);
247       break;
248     case ARG_PORT:
249       g_value_set_int (value, sink->server_port);
250       break;
251
252     default:
253       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254       break;
255   }
256 }
257
258
259 /* create a socket for sending to remote machine */
260 static gboolean
261 gst_tcpserversink_init_send (GstMultiFdSink * parent)
262 {
263   int ret;
264   GstTCPServerSink *this = GST_TCPSERVERSINK (parent);
265
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);
269     return FALSE;
270   }
271   GST_DEBUG_OBJECT (this, "opened sending server socket with fd %d",
272       this->server_sock_fd);
273
274   /* make address reusable */
275   if (setsockopt (this->server_sock_fd, SOL_SOCKET, SO_REUSEADDR, &ret,
276           sizeof (int)) < 0) {
277     GST_ELEMENT_ERROR (this, RESOURCE, SETTINGS, (NULL),
278         ("Could not setsockopt: %s", g_strerror (errno)));
279     return FALSE;
280   }
281   /* keep connection alive; avoids SIGPIPE during write */
282   if (setsockopt (this->server_sock_fd, SOL_SOCKET, SO_KEEPALIVE, &ret,
283           sizeof (int)) < 0) {
284     GST_ELEMENT_ERROR (this, RESOURCE, SETTINGS, (NULL),
285         ("Could not setsockopt: %s", g_strerror (errno)));
286     return FALSE;
287   }
288
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 */
294
295   /* bind it */
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));
299
300   if (ret) {
301     switch (errno) {
302       default:
303         GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
304             ("bind failed: %s", g_strerror (errno)));
305         return FALSE;
306         break;
307     }
308   }
309
310   /* set the server socket to nonblocking */
311   fcntl (this->server_sock_fd, F_SETFL, O_NONBLOCK);
312
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)));
318     return FALSE;
319   }
320   GST_DEBUG_OBJECT (this,
321       "listened on server socket %d, returning from connection setup",
322       this->server_sock_fd);
323
324   FD_SET (this->server_sock_fd, &parent->readfds);
325
326   return TRUE;
327 }
328
329 static gboolean
330 gst_tcpserversink_close (GstMultiFdSink * parent)
331 {
332   GstTCPServerSink *this = GST_TCPSERVERSINK (parent);
333
334   if (this->server_sock_fd != -1) {
335     close (this->server_sock_fd);
336     this->server_sock_fd = -1;
337   }
338   return TRUE;
339 }