2 * Copyright (C) <2007> Leandro Melo de Sales <leandroal@gmail.com>
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.
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.
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.
21 * SECTION:element-dccpserversink
22 * @see_also: dccpclientsink, dccpclientsrc, dccpserversrc
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
30 * <title>Example pipeline</title>
33 * gst-launch -v dccpclientsrc host=localhost port=9011 ccid=2 ! decodebin ! alsasink
36 * gst-launch -v filesrc location=music.mp3 ! mp3parse ! dccpserversink port=9011 ccid=2
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).
52 #include "gstdccpserversink.h"
74 static pthread_t accept_thread_id;
76 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
77 static gboolean gst_dccp_server_sink_stop (GstBaseSink * bsink);
79 GST_DEBUG_CATEGORY_STATIC (dccpserversink_debug);
81 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
86 GST_BOILERPLATE (GstDCCPServerSink, gst_dccp_server_sink, GstBaseSink,
89 static guint gst_dccp_server_sink_signals[LAST_SIGNAL] = { 0 };
92 * Create a new client with the socket and the MTU
94 * @param element - the gstdccpserversink instance
95 * @param socket - the client socket
99 gst_dccp_server_create_client (GstElement * element, int socket)
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;
106 GST_DEBUG_OBJECT (element, "Creating a new client with fd %d and MTU %d.",
107 client->socket, client->pksize);
109 /* the socket is connected */
110 g_signal_emit (element, gst_dccp_server_sink_signals[SIGNAL_CONNECTED], 0,
117 * Wait connections of new clients
119 * @param arg - the gstdccpserversink instance
122 gst_dccp_server_accept_new_clients (void *arg)
124 GstDCCPServerSink *sink = (GstDCCPServerSink *) arg;
130 gst_dccp_server_wait_connections (GST_ELEMENT (sink), sink->sock_fd);
132 client = gst_dccp_server_create_client (GST_ELEMENT (sink), newsockfd);
134 pthread_mutex_lock (&lock);
135 sink->clients = g_list_append (sink->clients, client);
136 pthread_mutex_unlock (&lock);
143 * Send the buffer to a client
145 * @param arg - the client
148 gst_dccp_server_send_buffer (void *arg)
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;
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;
163 /* Remove clients with problems to send.
165 * @param arg - the gstdccpserversink instance
168 gst_dccp_server_delete_dead_clients (void *arg)
170 GstDCCPServerSink *sink = (GstDCCPServerSink *) arg;
174 pthread_mutex_lock (&lock);
175 for (l = sink->clients; l != NULL; l = l->next) {
176 Client *client = (Client *) l->data;
178 if (client->flow_status == GST_FLOW_OK) {
179 tmp = g_list_append (tmp, client);
181 close (client->socket);
185 g_list_free (sink->clients);
187 pthread_mutex_unlock (&lock);
192 gst_dccp_server_sink_init (GstDCCPServerSink * this,
193 GstDCCPServerSinkClass * g_class)
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;
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.
209 * @param bsink - the element
210 * @return TRUE if the send operation was successful, FALSE otherwise.
213 gst_dccp_server_sink_start (GstBaseSink * bsink)
215 GstDCCPServerSink *sink = GST_DCCP_SERVER_SINK (bsink);
218 if ((sink->sock_fd = gst_dccp_create_new_socket (GST_ELEMENT (sink))) < 0) {
222 if (!gst_dccp_make_address_reusable (GST_ELEMENT (sink), sink->sock_fd)) {
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 */
232 if (!gst_dccp_bind_server_socket (GST_ELEMENT (sink), sink->sock_fd,
237 if (!gst_dccp_set_ccid (GST_ELEMENT (sink), sink->sock_fd, sink->ccid)) {
241 if (!gst_dccp_listen_server_socket (GST_ELEMENT (sink), sink->sock_fd)) {
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);
251 if (sink->client_sock_fd == -1) {
256 gst_dccp_server_create_client (GST_ELEMENT (sink), sink->client_sock_fd);
257 sink->clients = g_list_append (sink->clients, client);
259 pthread_mutex_init (&lock, NULL);
261 if (sink->wait_connections == TRUE) {
262 pthread_create (&accept_thread_id, NULL, gst_dccp_server_accept_new_clients,
264 pthread_detach (accept_thread_id);
271 gst_dccp_server_sink_render (GstBaseSink * bsink, GstBuffer * buf)
273 GstDCCPServerSink *sink = GST_DCCP_SERVER_SINK (bsink);
278 pthread_mutex_lock (&lock);
280 for (l = sink->clients; l != NULL; l = l->next) {
281 Client *client = (Client *) l->data;
284 client->server = sink;
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,
291 pthread_detach (thread_id);
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,
298 pthread_detach (thread_id);
302 pthread_mutex_unlock (&lock);
307 gst_dccp_server_sink_stop (GstBaseSink * bsink)
309 GstDCCPServerSink *sink;
312 sink = GST_DCCP_SERVER_SINK (bsink);
314 if (sink->wait_connections == TRUE) {
315 pthread_cancel (accept_thread_id);
318 gst_dccp_socket_close (GST_ELEMENT (sink), &(sink->sock_fd));
320 pthread_mutex_lock (&lock);
321 for (l = sink->clients; l != NULL; l = l->next) {
322 Client *client = (Client *) l->data;
324 if (client->socket != DCCP_DEFAULT_CLIENT_SOCK_FD && sink->closed == TRUE) {
325 gst_dccp_socket_close (GST_ELEMENT (sink), &(client->socket));
329 pthread_mutex_unlock (&lock);
335 gst_dccp_server_sink_base_init (gpointer g_class)
337 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
339 gst_element_class_add_static_pad_template (element_class, &sinktemplate);
341 gst_element_class_set_details_simple (element_class, "DCCP server sink",
343 "Send data as a server over the network via DCCP",
344 "E-Phone Team at Federal University of Campina Grande <leandroal@gmail.com>");
348 * Set the value of a property for the server sink.
351 gst_dccp_server_sink_set_property (GObject * object, guint prop_id,
352 const GValue * value, GParamSpec * pspec)
354 GstDCCPServerSink *sink = GST_DCCP_SERVER_SINK (object);
358 sink->port = g_value_get_int (value);
360 case PROP_CLIENT_SOCK_FD:
361 sink->client_sock_fd = g_value_get_int (value);
364 sink->closed = g_value_get_boolean (value);
366 case PROP_WAIT_CONNECTIONS:
367 sink->wait_connections = g_value_get_boolean (value);
370 sink->ccid = g_value_get_int (value);
378 gst_dccp_server_sink_get_property (GObject * object, guint prop_id,
379 GValue * value, GParamSpec * pspec)
381 GstDCCPServerSink *sink = GST_DCCP_SERVER_SINK (object);
385 g_value_set_int (value, sink->port);
387 case PROP_CLIENT_SOCK_FD:
388 g_value_set_int (value, sink->client_sock_fd);
391 g_value_set_boolean (value, sink->closed);
393 case PROP_WAIT_CONNECTIONS:
394 g_value_set_boolean (value, sink->wait_connections);
397 g_value_set_int (value, sink->ccid);
400 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
407 gst_dccp_server_sink_class_init (GstDCCPServerSinkClass * klass)
409 GObjectClass *gobject_class;
410 GstBaseSinkClass *gstbasesink_class;
412 gobject_class = (GObjectClass *) klass;
413 gstbasesink_class = (GstBaseSinkClass *) klass;
415 gobject_class->set_property = gst_dccp_server_sink_set_property;
416 gobject_class->get_property = gst_dccp_server_sink_get_property;
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));
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));
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));
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));
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));
448 * GstDccpServerSink::connected:
449 * @sink: the gstdccpserversink element that emitted this signal
450 * @fd: the connected socket file descriptor
452 * Reports that the element has connected, giving the fd of the socket
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);
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;
463 GST_DEBUG_CATEGORY_INIT (dccpserversink_debug, "dccpserversink", 0,