Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 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 |
Sebastian Dröge | e6513c1 | 2013-07-14 12:12:42 +0200 | [diff] [blame] | 16 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 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_pad_template (element_class, |
| 340 | gst_static_pad_template_get (&sinktemplate)); |
| 341 | |
Sebastian Dröge | 6a5ec01 | 2012-10-25 14:10:09 +0200 | [diff] [blame] | 342 | gst_element_class_set_static_metadata (element_class, "DCCP server sink", |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 343 | "Sink/Network", |
| 344 | "Send data as a server over the network via DCCP", |
| 345 | "E-Phone Team at Federal University of Campina Grande <leandroal@gmail.com>"); |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Set the value of a property for the server sink. |
| 350 | */ |
| 351 | static void |
| 352 | gst_dccp_server_sink_set_property (GObject * object, guint prop_id, |
| 353 | const GValue * value, GParamSpec * pspec) |
| 354 | { |
| 355 | GstDCCPServerSink *sink = GST_DCCP_SERVER_SINK (object); |
| 356 | |
| 357 | switch (prop_id) { |
| 358 | case PROP_PORT: |
| 359 | sink->port = g_value_get_int (value); |
| 360 | break; |
| 361 | case PROP_CLIENT_SOCK_FD: |
| 362 | sink->client_sock_fd = g_value_get_int (value); |
| 363 | break; |
| 364 | case PROP_CLOSED: |
| 365 | sink->closed = g_value_get_boolean (value); |
| 366 | break; |
| 367 | case PROP_WAIT_CONNECTIONS: |
| 368 | sink->wait_connections = g_value_get_boolean (value); |
| 369 | break; |
| 370 | case PROP_CCID: |
| 371 | sink->ccid = g_value_get_int (value); |
| 372 | break; |
| 373 | default: |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | static void |
| 379 | gst_dccp_server_sink_get_property (GObject * object, guint prop_id, |
| 380 | GValue * value, GParamSpec * pspec) |
| 381 | { |
| 382 | GstDCCPServerSink *sink = GST_DCCP_SERVER_SINK (object); |
| 383 | |
| 384 | switch (prop_id) { |
| 385 | case PROP_PORT: |
| 386 | g_value_set_int (value, sink->port); |
| 387 | break; |
| 388 | case PROP_CLIENT_SOCK_FD: |
| 389 | g_value_set_int (value, sink->client_sock_fd); |
| 390 | break; |
| 391 | case PROP_CLOSED: |
| 392 | g_value_set_boolean (value, sink->closed); |
| 393 | break; |
| 394 | case PROP_WAIT_CONNECTIONS: |
| 395 | g_value_set_boolean (value, sink->wait_connections); |
| 396 | break; |
| 397 | case PROP_CCID: |
| 398 | g_value_set_int (value, sink->ccid); |
| 399 | break; |
| 400 | default: |
| 401 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 402 | break; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | |
| 407 | static void |
| 408 | gst_dccp_server_sink_class_init (GstDCCPServerSinkClass * klass) |
| 409 | { |
| 410 | GObjectClass *gobject_class; |
| 411 | GstBaseSinkClass *gstbasesink_class; |
| 412 | |
| 413 | gobject_class = (GObjectClass *) klass; |
| 414 | gstbasesink_class = (GstBaseSinkClass *) klass; |
| 415 | |
| 416 | gobject_class->set_property = gst_dccp_server_sink_set_property; |
| 417 | gobject_class->get_property = gst_dccp_server_sink_get_property; |
| 418 | |
| 419 | g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT, |
| 420 | g_param_spec_int ("port", "Port", |
| 421 | "The port to listen to", 0, G_MAXUINT16, |
| 422 | DCCP_DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 423 | |
| 424 | g_object_class_install_property (gobject_class, PROP_CLIENT_SOCK_FD, |
| 425 | g_param_spec_int ("sockfd", "Socket fd", |
| 426 | "The client socket file descriptor", -1, G_MAXINT, |
| 427 | DCCP_DEFAULT_CLIENT_SOCK_FD, |
| 428 | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 429 | |
| 430 | g_object_class_install_property (gobject_class, PROP_CLOSED, |
| 431 | g_param_spec_boolean ("close-socket", "Close", |
| 432 | "Close the client sockets at end of stream", |
| 433 | DCCP_DEFAULT_CLOSED, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 434 | |
| 435 | g_object_class_install_property (gobject_class, PROP_CCID, |
| 436 | g_param_spec_int ("ccid", "CCID", |
| 437 | "The Congestion Control IDentified to be used", 2, G_MAXINT, |
| 438 | DCCP_DEFAULT_CCID, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 439 | |
| 440 | g_object_class_install_property (gobject_class, PROP_WAIT_CONNECTIONS, |
| 441 | g_param_spec_boolean ("wait-connections", "Wait connections", |
| 442 | "Wait for many client connections", |
| 443 | DCCP_DEFAULT_WAIT_CONNECTIONS, |
| 444 | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 445 | |
| 446 | |
| 447 | /* signals */ |
| 448 | /** |
| 449 | * GstDccpServerSink::connected: |
| 450 | * @sink: the gstdccpserversink element that emitted this signal |
| 451 | * @fd: the connected socket file descriptor |
| 452 | * |
| 453 | * Reports that the element has connected, giving the fd of the socket |
| 454 | */ |
| 455 | gst_dccp_server_sink_signals[SIGNAL_CONNECTED] = |
| 456 | g_signal_new ("connected", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST, |
| 457 | G_STRUCT_OFFSET (GstDCCPServerSinkClass, connected), NULL, NULL, |
| 458 | gst_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT); |
| 459 | |
| 460 | gstbasesink_class->start = gst_dccp_server_sink_start; |
| 461 | gstbasesink_class->stop = gst_dccp_server_sink_stop; |
| 462 | gstbasesink_class->render = gst_dccp_server_sink_render; |
| 463 | |
| 464 | GST_DEBUG_CATEGORY_INIT (dccpserversink_debug, "dccpserversink", 0, |
| 465 | "DCCP Server Sink"); |
| 466 | } |