Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / gst / udp / gstmultiudpsink.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  * Copyright (C) <2009> Jarkko Palviainen <jarkko.palviainen@sesca.com>
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-multiudpsink
23  * @see_also: udpsink, multifdsink
24  *
25  * multiudpsink is a network sink that sends UDP packets to multiple
26  * clients.
27  * It can be combined with rtp payload encoders to implement RTP streaming.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 #include "gstudp-marshal.h"
34 #include "gstmultiudpsink.h"
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41 #include <errno.h>
42 #include <string.h>
43
44 GST_DEBUG_CATEGORY_STATIC (multiudpsink_debug);
45 #define GST_CAT_DEFAULT (multiudpsink_debug)
46
47 #define UDP_MAX_SIZE 65507
48
49 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
50     GST_PAD_SINK,
51     GST_PAD_ALWAYS,
52     GST_STATIC_CAPS_ANY);
53
54 /* MultiUDPSink signals and args */
55 enum
56 {
57   /* methods */
58   SIGNAL_ADD,
59   SIGNAL_REMOVE,
60   SIGNAL_CLEAR,
61   SIGNAL_GET_STATS,
62
63   /* signals */
64   SIGNAL_CLIENT_ADDED,
65   SIGNAL_CLIENT_REMOVED,
66
67   /* FILL ME */
68   LAST_SIGNAL
69 };
70
71 #define DEFAULT_SOCKFD             -1
72 #define DEFAULT_CLOSEFD            TRUE
73 #define DEFAULT_SOCK               -1
74 #define DEFAULT_CLIENTS            NULL
75 #define DEFAULT_FAMILY             0
76 /* FIXME, this should be disabled by default, we don't need to join a multicast
77  * group for sending, if this socket is also used for receiving, it should
78  * be configured in the element that does the receive. */
79 #define DEFAULT_AUTO_MULTICAST     TRUE
80 #define DEFAULT_TTL                64
81 #define DEFAULT_TTL_MC             1
82 #define DEFAULT_LOOP               TRUE
83 #define DEFAULT_QOS_DSCP           -1
84 #define DEFAULT_SEND_DUPLICATES    TRUE
85 #define DEFAULT_BUFFER_SIZE        0
86
87 enum
88 {
89   PROP_0,
90   PROP_BYTES_TO_SERVE,
91   PROP_BYTES_SERVED,
92   PROP_SOCKFD,
93   PROP_CLOSEFD,
94   PROP_SOCK,
95   PROP_CLIENTS,
96   PROP_AUTO_MULTICAST,
97   PROP_TTL,
98   PROP_TTL_MC,
99   PROP_LOOP,
100   PROP_QOS_DSCP,
101   PROP_SEND_DUPLICATES,
102   PROP_BUFFER_SIZE,
103   PROP_LAST
104 };
105
106 #define CLOSE_IF_REQUESTED(udpctx)                                        \
107 G_STMT_START {                                                            \
108   if ((!udpctx->externalfd) || (udpctx->externalfd && udpctx->closefd)) { \
109     CLOSE_SOCKET(udpctx->sock);                                           \
110     if (udpctx->sock == udpctx->sockfd)                                   \
111       udpctx->sockfd = DEFAULT_SOCKFD;                                    \
112   }                                                                       \
113   udpctx->sock = DEFAULT_SOCK;                                            \
114 } G_STMT_END
115
116 static void gst_multiudpsink_finalize (GObject * object);
117
118 static GstFlowReturn gst_multiudpsink_render (GstBaseSink * sink,
119     GstBuffer * buffer);
120 #if 0
121 #ifndef G_OS_WIN32              /* sendmsg() is not available on Windows */
122 static GstFlowReturn gst_multiudpsink_render_list (GstBaseSink * bsink,
123     GstBufferList * list);
124 #endif
125 #endif
126 static GstStateChangeReturn gst_multiudpsink_change_state (GstElement *
127     element, GstStateChange transition);
128
129 static void gst_multiudpsink_set_property (GObject * object, guint prop_id,
130     const GValue * value, GParamSpec * pspec);
131 static void gst_multiudpsink_get_property (GObject * object, guint prop_id,
132     GValue * value, GParamSpec * pspec);
133
134 static void gst_multiudpsink_add_internal (GstMultiUDPSink * sink,
135     const gchar * host, gint port, gboolean lock);
136 static void gst_multiudpsink_clear_internal (GstMultiUDPSink * sink,
137     gboolean lock);
138
139 static guint gst_multiudpsink_signals[LAST_SIGNAL] = { 0 };
140
141 #define gst_multiudpsink_parent_class parent_class
142 G_DEFINE_TYPE (GstMultiUDPSink, gst_multiudpsink, GST_TYPE_BASE_SINK);
143
144 static void
145 gst_multiudpsink_class_init (GstMultiUDPSinkClass * klass)
146 {
147   GObjectClass *gobject_class;
148   GstElementClass *gstelement_class;
149   GstBaseSinkClass *gstbasesink_class;
150
151   gobject_class = (GObjectClass *) klass;
152   gstelement_class = (GstElementClass *) klass;
153   gstbasesink_class = (GstBaseSinkClass *) klass;
154
155   parent_class = g_type_class_peek_parent (klass);
156
157   gobject_class->set_property = gst_multiudpsink_set_property;
158   gobject_class->get_property = gst_multiudpsink_get_property;
159   gobject_class->finalize = gst_multiudpsink_finalize;
160
161   /**
162    * GstMultiUDPSink::add:
163    * @gstmultiudpsink: the sink on which the signal is emitted
164    * @host: the hostname/IP address of the client to add
165    * @port: the port of the client to add
166    *
167    * Add a client with destination @host and @port to the list of
168    * clients. When the same host/port pair is added multiple times, the
169    * send-duplicates property defines if the packets are sent multiple times to
170    * the same host/port pair or not.
171    *
172    * When a host/port pair is added multiple times, an equal amount of remove
173    * calls must be performed to actually remove the host/port pair from the list
174    * of destinations.
175    */
176   gst_multiudpsink_signals[SIGNAL_ADD] =
177       g_signal_new ("add", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
178       G_STRUCT_OFFSET (GstMultiUDPSinkClass, add),
179       NULL, NULL, gst_udp_marshal_VOID__STRING_INT, G_TYPE_NONE, 2,
180       G_TYPE_STRING, G_TYPE_INT);
181   /**
182    * GstMultiUDPSink::remove:
183    * @gstmultiudpsink: the sink on which the signal is emitted
184    * @host: the hostname/IP address of the client to remove
185    * @port: the port of the client to remove
186    *
187    * Remove the client with destination @host and @port from the list of
188    * clients.
189    */
190   gst_multiudpsink_signals[SIGNAL_REMOVE] =
191       g_signal_new ("remove", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
192       G_STRUCT_OFFSET (GstMultiUDPSinkClass, remove),
193       NULL, NULL, gst_udp_marshal_VOID__STRING_INT, G_TYPE_NONE, 2,
194       G_TYPE_STRING, G_TYPE_INT);
195   /**
196    * GstMultiUDPSink::clear:
197    * @gstmultiudpsink: the sink on which the signal is emitted
198    *
199    * Clear the list of clients.
200    */
201   gst_multiudpsink_signals[SIGNAL_CLEAR] =
202       g_signal_new ("clear", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
203       G_STRUCT_OFFSET (GstMultiUDPSinkClass, clear),
204       NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
205   /**
206    * GstMultiUDPSink::get-stats:
207    * @gstmultiudpsink: the sink on which the signal is emitted
208    * @host: the hostname/IP address of the client to get stats on
209    * @port: the port of the client to get stats on
210    *
211    * Get the statistics of the client with destination @host and @port.
212    *
213    * Returns: a GValueArray of uint64: bytes_sent, packets_sent,
214    *           connect_time (in epoch seconds), disconnect_time (in epoch seconds)
215    */
216   gst_multiudpsink_signals[SIGNAL_GET_STATS] =
217       g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
218       G_STRUCT_OFFSET (GstMultiUDPSinkClass, get_stats),
219       NULL, NULL, gst_udp_marshal_BOXED__STRING_INT, G_TYPE_VALUE_ARRAY, 2,
220       G_TYPE_STRING, G_TYPE_INT);
221   /**
222    * GstMultiUDPSink::client-added:
223    * @gstmultiudpsink: the sink emitting the signal
224    * @host: the hostname/IP address of the added client
225    * @port: the port of the added client
226    *
227    * Signal emited when a new client is added to the list of
228    * clients.
229    */
230   gst_multiudpsink_signals[SIGNAL_CLIENT_ADDED] =
231       g_signal_new ("client-added", G_TYPE_FROM_CLASS (klass),
232       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiUDPSinkClass, client_added),
233       NULL, NULL, gst_udp_marshal_VOID__STRING_INT, G_TYPE_NONE, 2,
234       G_TYPE_STRING, G_TYPE_INT);
235   /**
236    * GstMultiUDPSink::client-removed:
237    * @gstmultiudpsink: the sink emitting the signal
238    * @host: the hostname/IP address of the removed client
239    * @port: the port of the removed client
240    *
241    * Signal emited when a client is removed from the list of
242    * clients.
243    */
244   gst_multiudpsink_signals[SIGNAL_CLIENT_REMOVED] =
245       g_signal_new ("client-removed", G_TYPE_FROM_CLASS (klass),
246       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiUDPSinkClass,
247           client_removed), NULL, NULL, gst_udp_marshal_VOID__STRING_INT,
248       G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
249
250   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTES_TO_SERVE,
251       g_param_spec_uint64 ("bytes-to-serve", "Bytes to serve",
252           "Number of bytes received to serve to clients", 0, G_MAXUINT64, 0,
253           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
254   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTES_SERVED,
255       g_param_spec_uint64 ("bytes-served", "Bytes served",
256           "Total number of bytes send to all clients", 0, G_MAXUINT64, 0,
257           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
258   g_object_class_install_property (gobject_class, PROP_SOCKFD,
259       g_param_spec_int ("sockfd", "Socket Handle",
260           "Socket to use for UDP sending. (-1 == allocate)",
261           -1, G_MAXINT, DEFAULT_SOCKFD,
262           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
263   g_object_class_install_property (gobject_class, PROP_CLOSEFD,
264       g_param_spec_boolean ("closefd", "Close sockfd",
265           "Close sockfd if passed as property on state change",
266           DEFAULT_CLOSEFD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
267   g_object_class_install_property (gobject_class, PROP_SOCK,
268       g_param_spec_int ("sock", "Socket Handle",
269           "Socket currently in use for UDP sending. (-1 == no socket)",
270           -1, G_MAXINT, DEFAULT_SOCK,
271           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
272   g_object_class_install_property (gobject_class, PROP_CLIENTS,
273       g_param_spec_string ("clients", "Clients",
274           "A comma separated list of host:port pairs with destinations",
275           DEFAULT_CLIENTS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
276   g_object_class_install_property (gobject_class, PROP_AUTO_MULTICAST,
277       g_param_spec_boolean ("auto-multicast",
278           "Automatically join/leave multicast groups",
279           "Automatically join/leave the multicast groups, FALSE means user"
280           " has to do it himself", DEFAULT_AUTO_MULTICAST,
281           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
282   g_object_class_install_property (gobject_class, PROP_TTL,
283       g_param_spec_int ("ttl", "Unicast TTL",
284           "Used for setting the unicast TTL parameter",
285           0, 255, DEFAULT_TTL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
286   g_object_class_install_property (gobject_class, PROP_TTL_MC,
287       g_param_spec_int ("ttl-mc", "Multicast TTL",
288           "Used for setting the multicast TTL parameter",
289           0, 255, DEFAULT_TTL_MC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
290   g_object_class_install_property (gobject_class, PROP_LOOP,
291       g_param_spec_boolean ("loop", "Multicast Loopback",
292           "Used for setting the multicast loop parameter. TRUE = enable,"
293           " FALSE = disable", DEFAULT_LOOP,
294           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
295   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QOS_DSCP,
296       g_param_spec_int ("qos-dscp", "QoS diff srv code point",
297           "Quality of Service, differentiated services code point (-1 default)",
298           -1, 63, DEFAULT_QOS_DSCP,
299           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
300   /**
301    * GstMultiUDPSink::send-duplicates
302    *
303    * When a host/port pair is added mutliple times, send the packet to the host
304    * multiple times as well.
305    *
306    * Since: 0.10.26
307    */
308   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEND_DUPLICATES,
309       g_param_spec_boolean ("send-duplicates", "Send Duplicates",
310           "When a distination/port pair is added multiple times, send packets "
311           "multiple times as well", DEFAULT_SEND_DUPLICATES,
312           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
313
314   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_SIZE,
315       g_param_spec_int ("buffer-size", "Buffer Size",
316           "Size of the kernel send buffer in bytes, 0=default", 0, G_MAXINT,
317           DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
318
319   gstelement_class->change_state = gst_multiudpsink_change_state;
320
321   gst_element_class_add_pad_template (gstelement_class,
322       gst_static_pad_template_get (&sink_template));
323
324   gst_element_class_set_details_simple (gstelement_class, "UDP packet sender",
325       "Sink/Network",
326       "Send data over the network via UDP",
327       "Wim Taymans <wim.taymans@gmail.com>");
328
329   gstbasesink_class->render = gst_multiudpsink_render;
330 #if 0
331 #ifndef G_OS_WIN32
332   gstbasesink_class->render_list = gst_multiudpsink_render_list;
333 #endif
334 #endif
335   klass->add = gst_multiudpsink_add;
336   klass->remove = gst_multiudpsink_remove;
337   klass->clear = gst_multiudpsink_clear;
338   klass->get_stats = gst_multiudpsink_get_stats;
339
340   GST_DEBUG_CATEGORY_INIT (multiudpsink_debug, "multiudpsink", 0, "UDP sink");
341 }
342
343
344 static void
345 gst_multiudpsink_init (GstMultiUDPSink * sink)
346 {
347   WSA_STARTUP (sink);
348
349   sink->client_lock = g_mutex_new ();
350   sink->sock = DEFAULT_SOCK;
351   sink->sockfd = DEFAULT_SOCKFD;
352   sink->closefd = DEFAULT_CLOSEFD;
353   sink->externalfd = (sink->sockfd != -1);
354   sink->auto_multicast = DEFAULT_AUTO_MULTICAST;
355   sink->ttl = DEFAULT_TTL;
356   sink->ttl_mc = DEFAULT_TTL_MC;
357   sink->loop = DEFAULT_LOOP;
358   sink->qos_dscp = DEFAULT_QOS_DSCP;
359   sink->ss_family = DEFAULT_FAMILY;
360   sink->send_duplicates = DEFAULT_SEND_DUPLICATES;
361 }
362
363 static GstUDPClient *
364 create_client (GstMultiUDPSink * sink, const gchar * host, gint port)
365 {
366   GstUDPClient *client;
367
368   client = g_slice_new0 (GstUDPClient);
369   client->refcount = 1;
370   client->host = g_strdup (host);
371   client->port = port;
372
373   return client;
374 }
375
376 static void
377 free_client (GstUDPClient * client)
378 {
379   g_free (client->host);
380   g_slice_free (GstUDPClient, client);
381 }
382
383 static gint
384 client_compare (GstUDPClient * a, GstUDPClient * b)
385 {
386   if ((a->port == b->port) && (strcmp (a->host, b->host) == 0))
387     return 0;
388
389   return 1;
390 }
391
392 static void
393 gst_multiudpsink_finalize (GObject * object)
394 {
395   GstMultiUDPSink *sink;
396
397   sink = GST_MULTIUDPSINK (object);
398
399   g_list_foreach (sink->clients, (GFunc) free_client, NULL);
400   g_list_free (sink->clients);
401
402   if (sink->sockfd >= 0 && sink->closefd)
403     CLOSE_SOCKET (sink->sockfd);
404
405   g_mutex_free (sink->client_lock);
406
407   WSA_CLEANUP (object);
408
409   G_OBJECT_CLASS (parent_class)->finalize (object);
410 }
411
412 static gboolean
413 socket_error_is_ignorable (void)
414 {
415 #ifdef G_OS_WIN32
416   /* Windows doesn't seem to have an EAGAIN for sockets */
417   return WSAGetLastError () == WSAEINTR;
418 #else
419   return errno == EINTR || errno == EAGAIN;
420 #endif
421 }
422
423 static int
424 socket_last_error_code (void)
425 {
426 #ifdef G_OS_WIN32
427   return WSAGetLastError ();
428 #else
429   return errno;
430 #endif
431 }
432
433 static gchar *
434 socket_last_error_message (void)
435 {
436 #ifdef G_OS_WIN32
437   int errorcode = WSAGetLastError ();
438   wchar_t buf[1024];
439   DWORD result =
440       FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
441       NULL, errorcode, 0, (LPSTR) buf, sizeof (buf) / sizeof (wchar_t), NULL);
442   if (FAILED (result)) {
443     return g_strdup ("failed to get error message from system");
444   } else {
445     gchar *res =
446         g_convert ((gchar *) buf, -1, "UTF-16", "UTF-8", NULL, NULL, NULL);
447     /* g_convert() internally calls windows functions which reset the
448        windows error code, so fix it up again like this */
449     WSASetLastError (errorcode);
450     return res;
451   }
452 #else
453   return g_strdup (g_strerror (errno));
454 #endif
455 }
456
457 #ifdef G_OS_WIN32
458 /* version without sendmsg */
459 static GstFlowReturn
460 gst_multiudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
461 {
462   GstMultiUDPSink *sink;
463   gint ret, num = 0, no_clients = 0;
464   gsize size;
465   guint8 *data;
466   GList *clients;
467   gint len;
468
469   sink = GST_MULTIUDPSINK (bsink);
470
471   data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
472
473   if (size > UDP_MAX_SIZE) {
474     GST_WARNING ("Attempting to send a UDP packet larger than maximum "
475         "size (%d > %d)", size, UDP_MAX_SIZE);
476   }
477
478   sink->bytes_to_serve += size;
479
480   /* grab lock while iterating and sending to clients, this should be
481    * fast as UDP never blocks */
482   g_mutex_lock (sink->client_lock);
483   GST_LOG_OBJECT (bsink, "about to send %d bytes", size);
484
485   for (clients = sink->clients; clients; clients = g_list_next (clients)) {
486     GstUDPClient *client;
487     gint count;
488
489     client = (GstUDPClient *) clients->data;
490     no_clients++;
491     GST_LOG_OBJECT (sink, "sending %d bytes to client %p", size, client);
492
493     count = sink->send_duplicates ? client->refcount : 1;
494
495     while (count--) {
496       while (TRUE) {
497         len = gst_udp_get_sockaddr_length (&client->theiraddr);
498
499         ret = sendto (*client->sock,
500 #ifdef G_OS_WIN32
501             (char *) data,
502 #else
503             data,
504 #endif
505             size, 0, (struct sockaddr *) &client->theiraddr, len);
506
507         if (ret < 0) {
508           /* some error, just warn, it's likely recoverable and we don't want to
509            * break streaming. We break so that we stop retrying for this client. */
510           if (!socket_error_is_ignorable ()) {
511             gchar *errormessage = socket_last_error_message ();
512             GST_WARNING_OBJECT (sink, "client %p gave error %d (%s)", client,
513                 socket_last_error_code (), errormessage);
514             g_free (errormessage);
515             break;
516           }
517         } else {
518           num++;
519           client->bytes_sent += ret;
520           client->packets_sent++;
521           sink->bytes_served += ret;
522           break;
523         }
524       }
525     }
526   }
527   g_mutex_unlock (sink->client_lock);
528
529   gst_buffer_unmap (buffer, data, size);
530
531   GST_LOG_OBJECT (sink, "sent %d bytes to %d (of %d) clients", size, num,
532       no_clients);
533
534   return GST_FLOW_OK;
535 }
536 #else /* !G_OS_WIN32 */
537 /* version with sendmsg */
538 static GstFlowReturn
539 gst_multiudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
540 {
541   GstMultiUDPSink *sink;
542   GList *clients;
543   gint ret, size = 0, num = 0, no_clients = 0;
544   struct iovec *iov;
545   struct msghdr msg = { 0 };
546   guint n_mem, i;
547   gpointer bdata;
548   gsize bsize;
549   GstMemory *mem;
550
551   sink = GST_MULTIUDPSINK (bsink);
552
553   msg.msg_iovlen = 0;
554   size = 0;
555
556   n_mem = gst_buffer_n_memory (buffer);
557   if (n_mem == 0)
558     goto no_data;
559
560   iov = (struct iovec *) g_malloc (n_mem * sizeof (struct iovec));
561   msg.msg_iov = iov;
562
563   for (i = 0; i < n_mem; i++) {
564     mem = gst_buffer_peek_memory (buffer, i, GST_MAP_READ);
565     bdata = gst_memory_map (mem, &bsize, NULL, GST_MAP_READ);
566
567     if (bsize > UDP_MAX_SIZE) {
568       GST_WARNING ("Attempting to send a UDP packet larger than maximum "
569           "size (%d > %d)", bsize, UDP_MAX_SIZE);
570     }
571
572     msg.msg_iov[msg.msg_iovlen].iov_len = bsize;
573     msg.msg_iov[msg.msg_iovlen].iov_base = bdata;
574     msg.msg_iovlen++;
575
576     size += bsize;
577   }
578
579   sink->bytes_to_serve += size;
580
581   /* grab lock while iterating and sending to clients, this should be
582    * fast as UDP never blocks */
583   g_mutex_lock (sink->client_lock);
584   GST_LOG_OBJECT (bsink, "about to send %d bytes", size);
585
586   for (clients = sink->clients; clients; clients = g_list_next (clients)) {
587     GstUDPClient *client;
588     gint count;
589
590     client = (GstUDPClient *) clients->data;
591     no_clients++;
592     GST_LOG_OBJECT (sink, "sending %d bytes to client %p", size, client);
593
594     count = sink->send_duplicates ? client->refcount : 1;
595
596     while (count--) {
597       while (TRUE) {
598         msg.msg_name = (void *) &client->theiraddr;
599         msg.msg_namelen = sizeof (client->theiraddr);
600         ret = sendmsg (*client->sock, &msg, 0);
601
602         if (ret < 0) {
603           if (!socket_error_is_ignorable ()) {
604             gchar *errormessage = socket_last_error_message ();
605             GST_WARNING_OBJECT (sink, "client %p gave error %d (%s)", client,
606                 socket_last_error_code (), errormessage);
607             g_free (errormessage);
608             break;
609             break;
610           }
611         } else {
612           num++;
613           client->bytes_sent += ret;
614           client->packets_sent++;
615           sink->bytes_served += ret;
616           break;
617         }
618       }
619     }
620   }
621   g_mutex_unlock (sink->client_lock);
622
623   /* unmap all memory again */
624   for (i = 0; i < n_mem; i++) {
625     mem = gst_buffer_peek_memory (buffer, i, GST_MAP_READ);
626
627     bsize = msg.msg_iov[i].iov_len;
628     bdata = msg.msg_iov[i].iov_base;
629
630     gst_memory_unmap (mem, bdata, bsize);
631   }
632   g_free (iov);
633
634   GST_LOG_OBJECT (sink, "sent %d bytes to %d (of %d) clients", size, num,
635       no_clients);
636
637   return GST_FLOW_OK;
638
639 no_data:
640   {
641     return GST_FLOW_OK;
642   }
643 }
644 #endif
645
646 #if 0
647 /* DISABLED, core sends buffers to our render one by one, we can't really do
648  * much better */
649 static GstFlowReturn
650 gst_multiudpsink_render_list (GstBaseSink * bsink, GstBufferList * list)
651 {
652 }
653 #endif
654
655 static void
656 gst_multiudpsink_set_clients_string (GstMultiUDPSink * sink,
657     const gchar * string)
658 {
659   gchar **clients;
660   gint i;
661
662   clients = g_strsplit (string, ",", 0);
663
664   g_mutex_lock (sink->client_lock);
665   /* clear all existing clients */
666   gst_multiudpsink_clear_internal (sink, FALSE);
667   for (i = 0; clients[i]; i++) {
668     gchar *host, *p;
669     gint port = 0;
670
671     host = clients[i];
672     p = strstr (clients[i], ":");
673     if (p != NULL) {
674       *p = '\0';
675       port = atoi (p + 1);
676     }
677     if (port != 0)
678       gst_multiudpsink_add_internal (sink, host, port, FALSE);
679   }
680   g_mutex_unlock (sink->client_lock);
681
682   g_strfreev (clients);
683 }
684
685 static gchar *
686 gst_multiudpsink_get_clients_string (GstMultiUDPSink * sink)
687 {
688   GString *str;
689   GList *clients;
690
691   str = g_string_new ("");
692
693   g_mutex_lock (sink->client_lock);
694   clients = sink->clients;
695   while (clients) {
696     GstUDPClient *client;
697     gint count;
698
699     client = (GstUDPClient *) clients->data;
700
701     clients = g_list_next (clients);
702
703     count = client->refcount;
704     while (count--) {
705       g_string_append_printf (str, "%s:%d%s", client->host, client->port,
706           (clients || count > 1 ? "," : ""));
707     }
708   }
709   g_mutex_unlock (sink->client_lock);
710
711   return g_string_free (str, FALSE);
712 }
713
714 static void
715 gst_multiudpsink_setup_qos_dscp (GstMultiUDPSink * sink)
716 {
717   gint tos;
718
719   /* don't touch on -1 */
720   if (sink->qos_dscp < 0)
721     return;
722
723   if (sink->sock < 0)
724     return;
725
726   GST_DEBUG_OBJECT (sink, "setting TOS to %d", sink->qos_dscp);
727
728   /* Extract and shift 6 bits of DSFIELD */
729   tos = (sink->qos_dscp & 0x3f) << 2;
730
731   if (setsockopt (sink->sock, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0) {
732     gchar *errormessage = socket_last_error_message ();
733     GST_ERROR_OBJECT (sink, "could not set TOS: %s", errormessage);
734     g_free (errormessage);
735   }
736 #ifdef IPV6_TCLASS
737   if (setsockopt (sink->sock, IPPROTO_IPV6, IPV6_TCLASS, &tos,
738           sizeof (tos)) < 0) {
739     gchar *errormessage = socket_last_error_message ();
740     GST_ERROR_OBJECT (sink, "could not set TCLASS: %s", errormessage);
741     g_free (errormessage);
742   }
743 #endif
744 }
745
746 static void
747 gst_multiudpsink_set_property (GObject * object, guint prop_id,
748     const GValue * value, GParamSpec * pspec)
749 {
750   GstMultiUDPSink *udpsink;
751
752   udpsink = GST_MULTIUDPSINK (object);
753
754   switch (prop_id) {
755     case PROP_SOCKFD:
756       if (udpsink->sockfd >= 0 && udpsink->sockfd != udpsink->sock &&
757           udpsink->closefd)
758         CLOSE_SOCKET (udpsink->sockfd);
759       udpsink->sockfd = g_value_get_int (value);
760       GST_DEBUG_OBJECT (udpsink, "setting SOCKFD to %d", udpsink->sockfd);
761       break;
762     case PROP_CLOSEFD:
763       udpsink->closefd = g_value_get_boolean (value);
764       break;
765     case PROP_CLIENTS:
766       gst_multiudpsink_set_clients_string (udpsink, g_value_get_string (value));
767       break;
768     case PROP_AUTO_MULTICAST:
769       udpsink->auto_multicast = g_value_get_boolean (value);
770       break;
771     case PROP_TTL:
772       udpsink->ttl = g_value_get_int (value);
773       break;
774     case PROP_TTL_MC:
775       udpsink->ttl_mc = g_value_get_int (value);
776       break;
777     case PROP_LOOP:
778       udpsink->loop = g_value_get_boolean (value);
779       break;
780     case PROP_QOS_DSCP:
781       udpsink->qos_dscp = g_value_get_int (value);
782       gst_multiudpsink_setup_qos_dscp (udpsink);
783       break;
784     case PROP_SEND_DUPLICATES:
785       udpsink->send_duplicates = g_value_get_boolean (value);
786       break;
787     case PROP_BUFFER_SIZE:
788       udpsink->buffer_size = g_value_get_int (value);
789       break;
790     default:
791       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
792       break;
793   }
794 }
795
796 static void
797 gst_multiudpsink_get_property (GObject * object, guint prop_id, GValue * value,
798     GParamSpec * pspec)
799 {
800   GstMultiUDPSink *udpsink;
801
802   udpsink = GST_MULTIUDPSINK (object);
803
804   switch (prop_id) {
805     case PROP_BYTES_TO_SERVE:
806       g_value_set_uint64 (value, udpsink->bytes_to_serve);
807       break;
808     case PROP_BYTES_SERVED:
809       g_value_set_uint64 (value, udpsink->bytes_served);
810       break;
811     case PROP_SOCKFD:
812       g_value_set_int (value, udpsink->sockfd);
813       break;
814     case PROP_CLOSEFD:
815       g_value_set_boolean (value, udpsink->closefd);
816       break;
817     case PROP_SOCK:
818       g_value_set_int (value, udpsink->sock);
819       break;
820     case PROP_CLIENTS:
821       g_value_take_string (value,
822           gst_multiudpsink_get_clients_string (udpsink));
823       break;
824     case PROP_AUTO_MULTICAST:
825       g_value_set_boolean (value, udpsink->auto_multicast);
826       break;
827     case PROP_TTL:
828       g_value_set_int (value, udpsink->ttl);
829       break;
830     case PROP_TTL_MC:
831       g_value_set_int (value, udpsink->ttl_mc);
832       break;
833     case PROP_LOOP:
834       g_value_set_boolean (value, udpsink->loop);
835       break;
836     case PROP_QOS_DSCP:
837       g_value_set_int (value, udpsink->qos_dscp);
838       break;
839     case PROP_SEND_DUPLICATES:
840       g_value_set_boolean (value, udpsink->send_duplicates);
841       break;
842     case PROP_BUFFER_SIZE:
843       g_value_set_int (value, udpsink->buffer_size);
844       break;
845     default:
846       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
847       break;
848   }
849 }
850
851 static gboolean
852 gst_multiudpsink_configure_client (GstMultiUDPSink * sink,
853     GstUDPClient * client)
854 {
855   GST_DEBUG_OBJECT (sink, "configuring client %p", client);
856
857   if (gst_udp_is_multicast (&client->theiraddr)) {
858     GST_DEBUG_OBJECT (sink, "we have a multicast client %p", client);
859     if (sink->auto_multicast) {
860       GST_DEBUG_OBJECT (sink, "autojoining group");
861       if (gst_udp_join_group (*(client->sock), &client->theiraddr, NULL)
862           != 0)
863         goto join_group_failed;
864     }
865     GST_DEBUG_OBJECT (sink, "setting loop to %d", sink->loop);
866     if (gst_udp_set_loop (sink->sock, sink->ss_family, sink->loop) != 0)
867       goto loop_failed;
868     GST_DEBUG_OBJECT (sink, "setting ttl to %d", sink->ttl_mc);
869     if (gst_udp_set_ttl (sink->sock, sink->ss_family, sink->ttl_mc, TRUE) != 0)
870       goto ttl_failed;
871   } else {
872     GST_DEBUG_OBJECT (sink, "setting unicast ttl to %d", sink->ttl);
873     if (gst_udp_set_ttl (sink->sock, sink->ss_family, sink->ttl, FALSE) != 0)
874       goto ttl_failed;
875   }
876   return TRUE;
877
878   /* ERRORS */
879 join_group_failed:
880   {
881     gchar *errormessage = socket_last_error_message ();
882     int errorcode = socket_last_error_code ();
883     CLOSE_IF_REQUESTED (sink);
884     GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
885         ("Could not join multicast group (%d): %s", errorcode, errormessage));
886     g_free (errormessage);
887     return FALSE;
888   }
889 ttl_failed:
890   {
891     gchar *errormessage = socket_last_error_message ();
892     int errorcode = socket_last_error_code ();
893     CLOSE_IF_REQUESTED (sink);
894     GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
895         ("Could not set TTL socket option (%d): %s", errorcode, errormessage));
896     g_free (errormessage);
897     return FALSE;
898   }
899 loop_failed:
900   {
901     gchar *errormessage = socket_last_error_message ();
902     int errorcode = socket_last_error_code ();
903     CLOSE_IF_REQUESTED (sink);
904     GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
905         ("Could not set loopback socket option (%d): %s",
906             errorcode, errormessage));
907     g_free (errormessage);
908     return FALSE;
909   }
910 }
911
912 /* create a socket for sending to remote machine */
913 static gboolean
914 gst_multiudpsink_init_send (GstMultiUDPSink * sink)
915 {
916   guint bc_val;
917   GList *clients;
918   GstUDPClient *client;
919   int sndsize, ret;
920   socklen_t len;
921
922   if (sink->sockfd == -1) {
923     GST_DEBUG_OBJECT (sink, "creating sockets");
924     /* create sender socket try IP6, fall back to IP4 */
925     sink->ss_family = AF_INET6;
926     if ((sink->sock = socket (AF_INET6, SOCK_DGRAM, 0)) == -1) {
927       sink->ss_family = AF_INET;
928       if ((sink->sock = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
929         goto no_socket;
930     }
931
932     GST_DEBUG_OBJECT (sink, "have socket");
933     sink->externalfd = FALSE;
934   } else {
935     struct sockaddr_storage myaddr;
936
937     GST_DEBUG_OBJECT (sink, "using configured socket");
938     /* we use the configured socket, try to get some info about it */
939     len = sizeof (myaddr);
940     if (getsockname (sink->sockfd, (struct sockaddr *) &myaddr, &len) < 0)
941       goto getsockname_error;
942
943     sink->ss_family = myaddr.ss_family;
944     /* we use the configured socket */
945     sink->sock = sink->sockfd;
946     sink->externalfd = TRUE;
947   }
948
949   len = sizeof (sndsize);
950   if (sink->buffer_size != 0) {
951     sndsize = sink->buffer_size;
952
953     GST_DEBUG_OBJECT (sink, "setting udp buffer of %d bytes", sndsize);
954     /* set buffer size, Note that on Linux this is typically limited to a
955      * maximum of around 100K. Also a minimum of 128 bytes is required on
956      * Linux. */
957     ret =
958         setsockopt (sink->sockfd, SOL_SOCKET, SO_SNDBUF, (void *) &sndsize,
959         len);
960     if (ret != 0) {
961       GST_ELEMENT_WARNING (sink, RESOURCE, SETTINGS, (NULL),
962           ("Could not create a buffer of requested %d bytes, %d: %s (%d)",
963               sndsize, ret, g_strerror (errno), errno));
964     }
965   }
966
967   /* read the value of the receive buffer. Note that on linux this returns 2x the
968    * value we set because the kernel allocates extra memory for metadata.
969    * The default on Linux is about 100K (which is about 50K without metadata) */
970   ret =
971       getsockopt (sink->sockfd, SOL_SOCKET, SO_SNDBUF, (void *) &sndsize, &len);
972   if (ret == 0)
973     GST_DEBUG_OBJECT (sink, "have udp buffer of %d bytes", sndsize);
974   else
975     GST_DEBUG_OBJECT (sink, "could not get udp buffer size");
976
977
978   bc_val = 1;
979   if (setsockopt (sink->sock, SOL_SOCKET, SO_BROADCAST, &bc_val,
980           sizeof (bc_val)) < 0)
981     goto no_broadcast;
982
983   sink->bytes_to_serve = 0;
984   sink->bytes_served = 0;
985
986   gst_multiudpsink_setup_qos_dscp (sink);
987
988   /* look for multicast clients and join multicast groups appropriately
989      set also ttl and multicast loopback delivery appropriately  */
990   for (clients = sink->clients; clients; clients = g_list_next (clients)) {
991     client = (GstUDPClient *) clients->data;
992
993     if (!gst_multiudpsink_configure_client (sink, client))
994       return FALSE;
995   }
996   return TRUE;
997
998   /* ERRORS */
999 no_socket:
1000   {
1001     gchar *errormessage = socket_last_error_message ();
1002     int errorcode = socket_last_error_code ();
1003     GST_ELEMENT_ERROR (sink, RESOURCE, FAILED, (NULL),
1004         ("Could not create socket (%d): %s", errorcode, errormessage));
1005     g_free (errormessage);
1006     return FALSE;
1007   }
1008 getsockname_error:
1009   {
1010     gchar *errormessage = socket_last_error_message ();
1011     int errorcode = socket_last_error_code ();
1012     GST_ELEMENT_ERROR (sink, RESOURCE, FAILED, (NULL),
1013         ("Could not getsockname (%d): %s", errorcode, errormessage));
1014     g_free (errormessage);
1015     return FALSE;
1016   }
1017 no_broadcast:
1018   {
1019     gchar *errormessage = socket_last_error_message ();
1020     int errorcode = socket_last_error_code ();
1021     CLOSE_IF_REQUESTED (sink);
1022     GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
1023         ("Could not set broadcast socket option (%d): %s",
1024             errorcode, errormessage));
1025     g_free (errormessage);
1026     return FALSE;
1027   }
1028 }
1029
1030 static void
1031 gst_multiudpsink_close (GstMultiUDPSink * sink)
1032 {
1033   CLOSE_IF_REQUESTED (sink);
1034 }
1035
1036 static void
1037 gst_multiudpsink_add_internal (GstMultiUDPSink * sink, const gchar * host,
1038     gint port, gboolean lock)
1039 {
1040   GstUDPClient *client;
1041   GstUDPClient udpclient;
1042   GTimeVal now;
1043   GList *find;
1044
1045   udpclient.host = (gchar *) host;
1046   udpclient.port = port;
1047
1048   GST_DEBUG_OBJECT (sink, "adding client on host %s, port %d", host, port);
1049
1050   if (lock)
1051     g_mutex_lock (sink->client_lock);
1052
1053   find = g_list_find_custom (sink->clients, &udpclient,
1054       (GCompareFunc) client_compare);
1055   if (find) {
1056     client = (GstUDPClient *) find->data;
1057
1058     GST_DEBUG_OBJECT (sink, "found %d existing clients with host %s, port %d",
1059         client->refcount, host, port);
1060     client->refcount++;
1061   } else {
1062     client = create_client (sink, host, port);
1063
1064     client->sock = &sink->sock;
1065
1066     if (gst_udp_get_addr (host, port, &client->theiraddr) < 0)
1067       goto getaddrinfo_error;
1068
1069     g_get_current_time (&now);
1070     client->connect_time = GST_TIMEVAL_TO_TIME (now);
1071
1072     if (*client->sock > 0) {
1073       gst_multiudpsink_configure_client (sink, client);
1074     }
1075
1076     GST_DEBUG_OBJECT (sink, "add client with host %s, port %d", host, port);
1077     sink->clients = g_list_prepend (sink->clients, client);
1078   }
1079
1080   if (lock)
1081     g_mutex_unlock (sink->client_lock);
1082
1083   g_signal_emit (G_OBJECT (sink),
1084       gst_multiudpsink_signals[SIGNAL_CLIENT_ADDED], 0, host, port);
1085
1086   GST_DEBUG_OBJECT (sink, "added client on host %s, port %d", host, port);
1087   return;
1088
1089   /* ERRORS */
1090 getaddrinfo_error:
1091   {
1092     GST_DEBUG_OBJECT (sink, "did not add client on host %s, port %d", host,
1093         port);
1094     GST_WARNING_OBJECT (sink, "getaddrinfo lookup error?");
1095     free_client (client);
1096     if (lock)
1097       g_mutex_unlock (sink->client_lock);
1098     return;
1099   }
1100 }
1101
1102 void
1103 gst_multiudpsink_add (GstMultiUDPSink * sink, const gchar * host, gint port)
1104 {
1105   gst_multiudpsink_add_internal (sink, host, port, TRUE);
1106 }
1107
1108 void
1109 gst_multiudpsink_remove (GstMultiUDPSink * sink, const gchar * host, gint port)
1110 {
1111   GList *find;
1112   GstUDPClient udpclient;
1113   GstUDPClient *client;
1114   GTimeVal now;
1115
1116   udpclient.host = (gchar *) host;
1117   udpclient.port = port;
1118
1119   g_mutex_lock (sink->client_lock);
1120   find = g_list_find_custom (sink->clients, &udpclient,
1121       (GCompareFunc) client_compare);
1122   if (!find)
1123     goto not_found;
1124
1125   client = (GstUDPClient *) find->data;
1126
1127   GST_DEBUG_OBJECT (sink, "found %d clients with host %s, port %d",
1128       client->refcount, host, port);
1129
1130   client->refcount--;
1131   if (client->refcount == 0) {
1132     GST_DEBUG_OBJECT (sink, "remove client with host %s, port %d", host, port);
1133
1134     g_get_current_time (&now);
1135     client->disconnect_time = GST_TIMEVAL_TO_TIME (now);
1136
1137     if (*(client->sock) != -1 && sink->auto_multicast
1138         && gst_udp_is_multicast (&client->theiraddr))
1139       gst_udp_leave_group (*(client->sock), &client->theiraddr);
1140
1141     /* Unlock to emit signal before we delete the actual client */
1142     g_mutex_unlock (sink->client_lock);
1143     g_signal_emit (G_OBJECT (sink),
1144         gst_multiudpsink_signals[SIGNAL_CLIENT_REMOVED], 0, host, port);
1145     g_mutex_lock (sink->client_lock);
1146
1147     sink->clients = g_list_delete_link (sink->clients, find);
1148
1149     free_client (client);
1150   }
1151   g_mutex_unlock (sink->client_lock);
1152
1153   return;
1154
1155   /* ERRORS */
1156 not_found:
1157   {
1158     g_mutex_unlock (sink->client_lock);
1159     GST_WARNING_OBJECT (sink, "client at host %s, port %d not found",
1160         host, port);
1161     return;
1162   }
1163 }
1164
1165 static void
1166 gst_multiudpsink_clear_internal (GstMultiUDPSink * sink, gboolean lock)
1167 {
1168   GST_DEBUG_OBJECT (sink, "clearing");
1169   /* we only need to remove the client structure, there is no additional
1170    * socket or anything to free for UDP */
1171   if (lock)
1172     g_mutex_lock (sink->client_lock);
1173   g_list_foreach (sink->clients, (GFunc) free_client, sink);
1174   g_list_free (sink->clients);
1175   sink->clients = NULL;
1176   if (lock)
1177     g_mutex_unlock (sink->client_lock);
1178 }
1179
1180 void
1181 gst_multiudpsink_clear (GstMultiUDPSink * sink)
1182 {
1183   gst_multiudpsink_clear_internal (sink, TRUE);
1184 }
1185
1186 GValueArray *
1187 gst_multiudpsink_get_stats (GstMultiUDPSink * sink, const gchar * host,
1188     gint port)
1189 {
1190   GstUDPClient *client;
1191   GValueArray *result = NULL;
1192   GstUDPClient udpclient;
1193   GList *find;
1194   GValue value = { 0 };
1195
1196   udpclient.host = (gchar *) host;
1197   udpclient.port = port;
1198
1199   g_mutex_lock (sink->client_lock);
1200
1201   find = g_list_find_custom (sink->clients, &udpclient,
1202       (GCompareFunc) client_compare);
1203   if (!find)
1204     goto not_found;
1205
1206   GST_DEBUG_OBJECT (sink, "stats for client with host %s, port %d", host, port);
1207
1208   client = (GstUDPClient *) find->data;
1209
1210   /* Result is a value array of (bytes_sent, packets_sent,
1211    * connect_time, disconnect_time), all as uint64 */
1212   result = g_value_array_new (4);
1213
1214   g_value_init (&value, G_TYPE_UINT64);
1215   g_value_set_uint64 (&value, client->bytes_sent);
1216   result = g_value_array_append (result, &value);
1217   g_value_unset (&value);
1218
1219   g_value_init (&value, G_TYPE_UINT64);
1220   g_value_set_uint64 (&value, client->packets_sent);
1221   result = g_value_array_append (result, &value);
1222   g_value_unset (&value);
1223
1224   g_value_init (&value, G_TYPE_UINT64);
1225   g_value_set_uint64 (&value, client->connect_time);
1226   result = g_value_array_append (result, &value);
1227   g_value_unset (&value);
1228
1229   g_value_init (&value, G_TYPE_UINT64);
1230   g_value_set_uint64 (&value, client->disconnect_time);
1231   result = g_value_array_append (result, &value);
1232   g_value_unset (&value);
1233
1234   g_mutex_unlock (sink->client_lock);
1235
1236   return result;
1237
1238   /* ERRORS */
1239 not_found:
1240   {
1241     g_mutex_unlock (sink->client_lock);
1242     GST_WARNING_OBJECT (sink, "client with host %s, port %d not found",
1243         host, port);
1244     /* Apparently (see comment in gstmultifdsink.c) returning NULL from here may
1245      * confuse/break python bindings */
1246     return g_value_array_new (0);
1247   }
1248 }
1249
1250 static GstStateChangeReturn
1251 gst_multiudpsink_change_state (GstElement * element, GstStateChange transition)
1252 {
1253   GstStateChangeReturn ret;
1254   GstMultiUDPSink *sink;
1255
1256   sink = GST_MULTIUDPSINK (element);
1257
1258   switch (transition) {
1259     case GST_STATE_CHANGE_READY_TO_PAUSED:
1260       if (!gst_multiudpsink_init_send (sink))
1261         goto no_init;
1262       break;
1263     default:
1264       break;
1265   }
1266
1267   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1268
1269   switch (transition) {
1270     case GST_STATE_CHANGE_PAUSED_TO_READY:
1271       gst_multiudpsink_close (sink);
1272       break;
1273     default:
1274       break;
1275   }
1276   return ret;
1277
1278   /* ERRORS */
1279 no_init:
1280   {
1281     /* _init_send() posted specific error already */
1282     return GST_STATE_CHANGE_FAILURE;
1283   }
1284 }