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