Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / gst / dccp / gstdccpserversink.c
1 /* GStreamer
2  * Copyright (C) <2007> Leandro Melo de Sales <leandroal@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:element-dccpserversink
22  * @see_also: dccpclientsink, dccpclientsrc, dccpserversrc
23  *
24  * This element wait for connections from clients and send data to them.
25  * <ulink url="http://www.linuxfoundation.org/en/Net:DCCP">DCCP</ulink> (Datagram
26  * Congestion Control Protocol) is a Transport Layer protocol like
27  * TCP and UDP.
28  *
29  * <refsect2>
30  * <title>Example pipeline</title>
31  * <para>
32  * |[
33  * gst-launch -v dccpclientsrc host=localhost port=9011 ccid=2 ! decodebin ! alsasink
34  * ]| Client
35  * |[
36  * gst-launch -v filesrc location=music.mp3 ! mp3parse ! dccpserversink port=9011 ccid=2
37  * ]| Server
38  *
39  * This example pipeline will send a MP3 stream to the client using DCCP.
40  * The client will decode the MP3 and play it. Run the server pipeline
41  * first than the client pipeline. If you want, you can run more than one dccpclientsrc
42  * to connect to the same server (see wait-connections property at dccpserversink).
43  * </para>
44  * </refsect2>
45  */
46
47
48 #ifdef HAVE_CONFIG_H
49 #include "config.h"
50 #endif
51
52 #include "gstdccpserversink.h"
53 #include "gstdccp.h"
54 #include <fcntl.h>
55
56 /* signals */
57 enum
58 {
59   SIGNAL_CONNECTED,
60   LAST_SIGNAL
61 };
62
63 /* properties */
64 enum
65 {
66   PROP_0,
67   PROP_PORT,
68   PROP_CLIENT_SOCK_FD,
69   PROP_CCID,
70   PROP_CLOSED,
71   PROP_WAIT_CONNECTIONS
72 };
73
74 static pthread_t accept_thread_id;
75
76 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
77 static gboolean gst_dccp_server_sink_stop (GstBaseSink * bsink);
78
79 GST_DEBUG_CATEGORY_STATIC (dccpserversink_debug);
80
81 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
82     GST_PAD_SINK,
83     GST_PAD_ALWAYS,
84     GST_STATIC_CAPS_ANY);
85
86 GST_BOILERPLATE (GstDCCPServerSink, gst_dccp_server_sink, GstBaseSink,
87     GST_TYPE_BASE_SINK);
88
89 static guint gst_dccp_server_sink_signals[LAST_SIGNAL] = { 0 };
90
91 /*
92  * Create a new client with the socket and the MTU
93  *
94  * @param element - the gstdccpserversink instance
95  * @param socket - the client socket
96  * @return the client
97  */
98 static Client *
99 gst_dccp_server_create_client (GstElement * element, int socket)
100 {
101   Client *client = (Client *) g_malloc (sizeof (Client));
102   client->socket = socket;
103   client->pksize = gst_dccp_get_max_packet_size (element, client->socket);
104   client->flow_status = GST_FLOW_OK;
105
106   GST_DEBUG_OBJECT (element, "Creating a new client with fd %d and MTU %d.",
107       client->socket, client->pksize);
108
109   /* the socket is connected */
110   g_signal_emit (element, gst_dccp_server_sink_signals[SIGNAL_CONNECTED], 0,
111       socket);
112
113   return client;
114 }
115
116 /*
117  * Wait connections of new clients
118  *
119  * @param arg - the gstdccpserversink instance
120  */
121 static void *
122 gst_dccp_server_accept_new_clients (void *arg)
123 {
124   GstDCCPServerSink *sink = (GstDCCPServerSink *) arg;
125   int newsockfd;
126   Client *client;
127
128   while (1) {
129     newsockfd =
130         gst_dccp_server_wait_connections (GST_ELEMENT (sink), sink->sock_fd);
131
132     client = gst_dccp_server_create_client (GST_ELEMENT (sink), newsockfd);
133
134     pthread_mutex_lock (&lock);
135     sink->clients = g_list_append (sink->clients, client);
136     pthread_mutex_unlock (&lock);
137   }
138
139   return NULL;
140 }
141
142 /*
143  * Send the buffer to a client
144  *
145  * @param arg - the client
146  */
147 static void *
148 gst_dccp_server_send_buffer (void *arg)
149 {
150   Client *client = (Client *) arg;
151   GstDCCPServerSink *sink = client->server;
152   GstBuffer *buf = client->buf;
153   int client_sock_fd = client->socket;
154   int pksize = client->pksize;
155
156   if (gst_dccp_send_buffer (GST_ELEMENT (sink), buf, client_sock_fd,
157           pksize) == GST_FLOW_ERROR) {
158     client->flow_status = GST_FLOW_ERROR;
159   }
160   return NULL;
161 }
162
163 /* Remove clients with problems to send.
164  *
165  * @param arg - the gstdccpserversink instance
166  */
167 static void *
168 gst_dccp_server_delete_dead_clients (void *arg)
169 {
170   GstDCCPServerSink *sink = (GstDCCPServerSink *) arg;
171   GList *tmp = NULL;
172   GList *l;
173
174   pthread_mutex_lock (&lock);
175   for (l = sink->clients; l != NULL; l = l->next) {
176     Client *client = (Client *) l->data;
177
178     if (client->flow_status == GST_FLOW_OK) {
179       tmp = g_list_append (tmp, client);
180     } else {
181       close (client->socket);
182       g_free (client);
183     }
184   }
185   g_list_free (sink->clients);
186   sink->clients = tmp;
187   pthread_mutex_unlock (&lock);
188   return 0;
189 }
190
191 static void
192 gst_dccp_server_sink_init (GstDCCPServerSink * this,
193     GstDCCPServerSinkClass * g_class)
194 {
195   this->port = DCCP_DEFAULT_PORT;
196   this->sock_fd = DCCP_DEFAULT_SOCK_FD;
197   this->client_sock_fd = DCCP_DEFAULT_CLIENT_SOCK_FD;
198   this->closed = DCCP_DEFAULT_CLOSED;
199   this->ccid = DCCP_DEFAULT_CCID;
200   this->wait_connections = DCCP_DEFAULT_WAIT_CONNECTIONS;
201   this->clients = NULL;
202 }
203
204 /*
205  * Starts the element. If the sockfd property was not the default, this method
206  * will wait for a client connection.  If wait-connections property is true, it
207  * creates a thread to wait for new client connections.
208  *
209  * @param bsink - the element
210  * @return TRUE if the send operation was successful, FALSE otherwise.
211  */
212 static gboolean
213 gst_dccp_server_sink_start (GstBaseSink * bsink)
214 {
215   GstDCCPServerSink *sink = GST_DCCP_SERVER_SINK (bsink);
216   Client *client;
217
218   if ((sink->sock_fd = gst_dccp_create_new_socket (GST_ELEMENT (sink))) < 0) {
219     return FALSE;
220   }
221
222   if (!gst_dccp_make_address_reusable (GST_ELEMENT (sink), sink->sock_fd)) {
223     return FALSE;
224   }
225
226   /* name the server socket */
227   memset (&sink->server_sin, 0, sizeof (sink->server_sin));
228   sink->server_sin.sin_family = AF_INET;        /* network socket */
229   sink->server_sin.sin_port = htons (sink->port);       /* on port */
230   sink->server_sin.sin_addr.s_addr = htonl (INADDR_ANY);        /* for hosts */
231
232   if (!gst_dccp_bind_server_socket (GST_ELEMENT (sink), sink->sock_fd,
233           sink->server_sin)) {
234     return FALSE;
235   }
236
237   if (!gst_dccp_set_ccid (GST_ELEMENT (sink), sink->sock_fd, sink->ccid)) {
238     return FALSE;
239   }
240
241   if (!gst_dccp_listen_server_socket (GST_ELEMENT (sink), sink->sock_fd)) {
242     return FALSE;
243   }
244
245
246   if (sink->client_sock_fd == DCCP_DEFAULT_CLIENT_SOCK_FD) {
247     sink->client_sock_fd =
248         gst_dccp_server_wait_connections (GST_ELEMENT (sink), sink->sock_fd);
249   }
250
251   if (sink->client_sock_fd == -1) {
252     return FALSE;
253   }
254
255   client =
256       gst_dccp_server_create_client (GST_ELEMENT (sink), sink->client_sock_fd);
257   sink->clients = g_list_append (sink->clients, client);
258
259   pthread_mutex_init (&lock, NULL);
260
261   if (sink->wait_connections == TRUE) {
262     pthread_create (&accept_thread_id, NULL, gst_dccp_server_accept_new_clients,
263         sink);
264     pthread_detach (accept_thread_id);
265   }
266
267   return TRUE;
268 }
269
270 static GstFlowReturn
271 gst_dccp_server_sink_render (GstBaseSink * bsink, GstBuffer * buf)
272 {
273   GstDCCPServerSink *sink = GST_DCCP_SERVER_SINK (bsink);
274
275   pthread_t thread_id;
276   GList *l;
277
278   pthread_mutex_lock (&lock);
279
280   for (l = sink->clients; l != NULL; l = l->next) {
281     Client *client = (Client *) l->data;
282
283     client->buf = buf;
284     client->server = sink;
285
286     /* FIXME: are we really creating a new thread here for every single buffer
287      * and every single client? */
288     if (client->flow_status == GST_FLOW_OK) {
289       pthread_create (&thread_id, NULL, gst_dccp_server_send_buffer,
290           (void *) client);
291       pthread_detach (thread_id);
292     } else {
293       /* FIXME: what's the point of doing this in a separate thread if it
294        * keeps he global lock anyway while going through all the clients and
295        * waiting for close() to finish? */
296       pthread_create (&thread_id, NULL, gst_dccp_server_delete_dead_clients,
297           (void *) sink);
298       pthread_detach (thread_id);
299     }
300   }
301
302   pthread_mutex_unlock (&lock);
303   return GST_FLOW_OK;
304 }
305
306 static gboolean
307 gst_dccp_server_sink_stop (GstBaseSink * bsink)
308 {
309   GstDCCPServerSink *sink;
310   GList *l;
311
312   sink = GST_DCCP_SERVER_SINK (bsink);
313
314   if (sink->wait_connections == TRUE) {
315     pthread_cancel (accept_thread_id);
316   }
317
318   gst_dccp_socket_close (GST_ELEMENT (sink), &(sink->sock_fd));
319
320   pthread_mutex_lock (&lock);
321   for (l = sink->clients; l != NULL; l = l->next) {
322     Client *client = (Client *) l->data;
323
324     if (client->socket != DCCP_DEFAULT_CLIENT_SOCK_FD && sink->closed == TRUE) {
325       gst_dccp_socket_close (GST_ELEMENT (sink), &(client->socket));
326     }
327     g_free (client);
328   }
329   pthread_mutex_unlock (&lock);
330
331   return TRUE;
332 }
333
334 static void
335 gst_dccp_server_sink_base_init (gpointer g_class)
336 {
337   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
338
339   gst_element_class_add_static_pad_template (element_class, &sinktemplate);
340
341   gst_element_class_set_details_simple (element_class, "DCCP server sink",
342       "Sink/Network",
343       "Send data as a server over the network via DCCP",
344       "E-Phone Team at Federal University of Campina Grande <leandroal@gmail.com>");
345 }
346
347 /*
348  * Set the value of a property for the server sink.
349  */
350 static void
351 gst_dccp_server_sink_set_property (GObject * object, guint prop_id,
352     const GValue * value, GParamSpec * pspec)
353 {
354   GstDCCPServerSink *sink = GST_DCCP_SERVER_SINK (object);
355
356   switch (prop_id) {
357     case PROP_PORT:
358       sink->port = g_value_get_int (value);
359       break;
360     case PROP_CLIENT_SOCK_FD:
361       sink->client_sock_fd = g_value_get_int (value);
362       break;
363     case PROP_CLOSED:
364       sink->closed = g_value_get_boolean (value);
365       break;
366     case PROP_WAIT_CONNECTIONS:
367       sink->wait_connections = g_value_get_boolean (value);
368       break;
369     case PROP_CCID:
370       sink->ccid = g_value_get_int (value);
371       break;
372     default:
373       break;
374   }
375 }
376
377 static void
378 gst_dccp_server_sink_get_property (GObject * object, guint prop_id,
379     GValue * value, GParamSpec * pspec)
380 {
381   GstDCCPServerSink *sink = GST_DCCP_SERVER_SINK (object);
382
383   switch (prop_id) {
384     case PROP_PORT:
385       g_value_set_int (value, sink->port);
386       break;
387     case PROP_CLIENT_SOCK_FD:
388       g_value_set_int (value, sink->client_sock_fd);
389       break;
390     case PROP_CLOSED:
391       g_value_set_boolean (value, sink->closed);
392       break;
393     case PROP_WAIT_CONNECTIONS:
394       g_value_set_boolean (value, sink->wait_connections);
395       break;
396     case PROP_CCID:
397       g_value_set_int (value, sink->ccid);
398       break;
399     default:
400       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
401       break;
402   }
403 }
404
405
406 static void
407 gst_dccp_server_sink_class_init (GstDCCPServerSinkClass * klass)
408 {
409   GObjectClass *gobject_class;
410   GstBaseSinkClass *gstbasesink_class;
411
412   gobject_class = (GObjectClass *) klass;
413   gstbasesink_class = (GstBaseSinkClass *) klass;
414
415   gobject_class->set_property = gst_dccp_server_sink_set_property;
416   gobject_class->get_property = gst_dccp_server_sink_get_property;
417
418   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
419       g_param_spec_int ("port", "Port",
420           "The port to listen to", 0, G_MAXUINT16,
421           DCCP_DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
422
423   g_object_class_install_property (gobject_class, PROP_CLIENT_SOCK_FD,
424       g_param_spec_int ("sockfd", "Socket fd",
425           "The client socket file descriptor", -1, G_MAXINT,
426           DCCP_DEFAULT_CLIENT_SOCK_FD,
427           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
428
429   g_object_class_install_property (gobject_class, PROP_CLOSED,
430       g_param_spec_boolean ("close-socket", "Close",
431           "Close the client sockets at end of stream",
432           DCCP_DEFAULT_CLOSED, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
433
434   g_object_class_install_property (gobject_class, PROP_CCID,
435       g_param_spec_int ("ccid", "CCID",
436           "The Congestion Control IDentified to be used", 2, G_MAXINT,
437           DCCP_DEFAULT_CCID, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
438
439   g_object_class_install_property (gobject_class, PROP_WAIT_CONNECTIONS,
440       g_param_spec_boolean ("wait-connections", "Wait connections",
441           "Wait for many client connections",
442           DCCP_DEFAULT_WAIT_CONNECTIONS,
443           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
444
445
446   /* signals */
447   /**
448    * GstDccpServerSink::connected:
449    * @sink: the gstdccpserversink element that emitted this signal
450    * @fd: the connected socket file descriptor
451    *
452    * Reports that the element has connected, giving the fd of the socket
453    */
454   gst_dccp_server_sink_signals[SIGNAL_CONNECTED] =
455       g_signal_new ("connected", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
456       G_STRUCT_OFFSET (GstDCCPServerSinkClass, connected), NULL, NULL,
457       gst_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
458
459   gstbasesink_class->start = gst_dccp_server_sink_start;
460   gstbasesink_class->stop = gst_dccp_server_sink_stop;
461   gstbasesink_class->render = gst_dccp_server_sink_render;
462
463   GST_DEBUG_CATEGORY_INIT (dccpserversink_debug, "dccpserversink", 0,
464       "DCCP Server Sink");
465 }