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