4d09879c6622c0aff42ed71dadcb51e7ecf397b3
[platform/upstream/gst-plugins-good.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  * Copyright (C) <2012> Collabora Ltd.
5  *   Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 /**
24  * SECTION:element-multiudpsink
25  * @see_also: udpsink, multifdsink
26  *
27  * multiudpsink is a network sink that sends UDP packets to multiple
28  * clients.
29  * It can be combined with rtp payload encoders to implement RTP streaming.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 #include "gstmultiudpsink.h"
36
37 #include <string.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
40 #endif
41
42 #ifndef G_OS_WIN32
43 #include <netinet/in.h>
44 #endif
45
46 #include "gst/glib-compat-private.h"
47
48 GST_DEBUG_CATEGORY_STATIC (multiudpsink_debug);
49 #define GST_CAT_DEFAULT (multiudpsink_debug)
50
51 #define UDP_MAX_SIZE 65507
52
53 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
54     GST_PAD_SINK,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS_ANY);
57
58 /* MultiUDPSink signals and args */
59 enum
60 {
61   /* methods */
62   SIGNAL_ADD,
63   SIGNAL_REMOVE,
64   SIGNAL_CLEAR,
65   SIGNAL_GET_STATS,
66
67   /* signals */
68   SIGNAL_CLIENT_ADDED,
69   SIGNAL_CLIENT_REMOVED,
70
71   /* FILL ME */
72   LAST_SIGNAL
73 };
74
75 #define DEFAULT_SOCKET             NULL
76 #define DEFAULT_CLOSE_SOCKET       TRUE
77 #define DEFAULT_USED_SOCKET        NULL
78 #define DEFAULT_CLIENTS            NULL
79 /* FIXME, this should be disabled by default, we don't need to join a multicast
80  * group for sending, if this socket is also used for receiving, it should
81  * be configured in the element that does the receive. */
82 #define DEFAULT_AUTO_MULTICAST     TRUE
83 #define DEFAULT_MULTICAST_IFACE    NULL
84 #define DEFAULT_TTL                64
85 #define DEFAULT_TTL_MC             1
86 #define DEFAULT_LOOP               TRUE
87 #define DEFAULT_FORCE_IPV4         FALSE
88 #define DEFAULT_QOS_DSCP           -1
89 #define DEFAULT_SEND_DUPLICATES    TRUE
90 #define DEFAULT_BUFFER_SIZE        0
91 #define DEFAULT_BIND_ADDRESS       NULL
92 #define DEFAULT_BIND_PORT          0
93
94 enum
95 {
96   PROP_0,
97   PROP_BYTES_TO_SERVE,
98   PROP_BYTES_SERVED,
99   PROP_SOCKET,
100   PROP_SOCKET_V6,
101   PROP_CLOSE_SOCKET,
102   PROP_USED_SOCKET,
103   PROP_USED_SOCKET_V6,
104   PROP_CLIENTS,
105   PROP_AUTO_MULTICAST,
106   PROP_MULTICAST_IFACE,
107   PROP_TTL,
108   PROP_TTL_MC,
109   PROP_LOOP,
110   PROP_FORCE_IPV4,
111   PROP_QOS_DSCP,
112   PROP_SEND_DUPLICATES,
113   PROP_BUFFER_SIZE,
114   PROP_BIND_ADDRESS,
115   PROP_BIND_PORT
116 };
117
118 static void gst_multiudpsink_finalize (GObject * object);
119
120 static GstFlowReturn gst_multiudpsink_render (GstBaseSink * sink,
121     GstBuffer * buffer);
122 static GstFlowReturn gst_multiudpsink_render_list (GstBaseSink * bsink,
123     GstBufferList * buffer_list);
124
125 static gboolean gst_multiudpsink_start (GstBaseSink * bsink);
126 static gboolean gst_multiudpsink_stop (GstBaseSink * bsink);
127 static gboolean gst_multiudpsink_unlock (GstBaseSink * bsink);
128 static gboolean gst_multiudpsink_unlock_stop (GstBaseSink * bsink);
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 guint gst_multiudpsink_signals[LAST_SIGNAL] = { 0 };
141
142 #define gst_multiudpsink_parent_class parent_class
143 G_DEFINE_TYPE (GstMultiUDPSink, gst_multiudpsink, GST_TYPE_BASE_SINK);
144
145 static void
146 gst_multiudpsink_class_init (GstMultiUDPSinkClass * klass)
147 {
148   GObjectClass *gobject_class;
149   GstElementClass *gstelement_class;
150   GstBaseSinkClass *gstbasesink_class;
151
152   gobject_class = (GObjectClass *) klass;
153   gstelement_class = (GstElementClass *) klass;
154   gstbasesink_class = (GstBaseSinkClass *) klass;
155
156   gobject_class->set_property = gst_multiudpsink_set_property;
157   gobject_class->get_property = gst_multiudpsink_get_property;
158   gobject_class->finalize = gst_multiudpsink_finalize;
159
160   /**
161    * GstMultiUDPSink::add:
162    * @gstmultiudpsink: the sink on which the signal is emitted
163    * @host: the hostname/IP address of the client to add
164    * @port: the port of the client to add
165    *
166    * Add a client with destination @host and @port to the list of
167    * clients. When the same host/port pair is added multiple times, the
168    * send-duplicates property defines if the packets are sent multiple times to
169    * the same host/port pair or not.
170    *
171    * When a host/port pair is added multiple times, an equal amount of remove
172    * calls must be performed to actually remove the host/port pair from the list
173    * of destinations.
174    */
175   gst_multiudpsink_signals[SIGNAL_ADD] =
176       g_signal_new ("add", G_TYPE_FROM_CLASS (klass),
177       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
178       G_STRUCT_OFFSET (GstMultiUDPSinkClass, add),
179       NULL, NULL, g_cclosure_marshal_generic, 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),
192       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
193       G_STRUCT_OFFSET (GstMultiUDPSinkClass, remove),
194       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
195       G_TYPE_STRING, G_TYPE_INT);
196   /**
197    * GstMultiUDPSink::clear:
198    * @gstmultiudpsink: the sink on which the signal is emitted
199    *
200    * Clear the list of clients.
201    */
202   gst_multiudpsink_signals[SIGNAL_CLEAR] =
203       g_signal_new ("clear", G_TYPE_FROM_CLASS (klass),
204       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
205       G_STRUCT_OFFSET (GstMultiUDPSinkClass, clear),
206       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 0);
207   /**
208    * GstMultiUDPSink::get-stats:
209    * @gstmultiudpsink: the sink on which the signal is emitted
210    * @host: the hostname/IP address of the client to get stats on
211    * @port: the port of the client to get stats on
212    *
213    * Get the statistics of the client with destination @host and @port.
214    *
215    * Returns: a GstStructure: bytes_sent, packets_sent,
216    *           connect_time (in epoch seconds), disconnect_time (in epoch seconds)
217    */
218   gst_multiudpsink_signals[SIGNAL_GET_STATS] =
219       g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass),
220       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
221       G_STRUCT_OFFSET (GstMultiUDPSinkClass, get_stats),
222       NULL, NULL, g_cclosure_marshal_generic, GST_TYPE_STRUCTURE, 2,
223       G_TYPE_STRING, G_TYPE_INT);
224   /**
225    * GstMultiUDPSink::client-added:
226    * @gstmultiudpsink: the sink emitting the signal
227    * @host: the hostname/IP address of the added client
228    * @port: the port of the added client
229    *
230    * Signal emited when a new client is added to the list of
231    * clients.
232    */
233   gst_multiudpsink_signals[SIGNAL_CLIENT_ADDED] =
234       g_signal_new ("client-added", G_TYPE_FROM_CLASS (klass),
235       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiUDPSinkClass, client_added),
236       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
237       G_TYPE_STRING, G_TYPE_INT);
238   /**
239    * GstMultiUDPSink::client-removed:
240    * @gstmultiudpsink: the sink emitting the signal
241    * @host: the hostname/IP address of the removed client
242    * @port: the port of the removed client
243    *
244    * Signal emited when a client is removed from the list of
245    * clients.
246    */
247   gst_multiudpsink_signals[SIGNAL_CLIENT_REMOVED] =
248       g_signal_new ("client-removed", G_TYPE_FROM_CLASS (klass),
249       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiUDPSinkClass,
250           client_removed), NULL, NULL, g_cclosure_marshal_generic,
251       G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
252
253   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTES_TO_SERVE,
254       g_param_spec_uint64 ("bytes-to-serve", "Bytes to serve",
255           "Number of bytes received to serve to clients", 0, G_MAXUINT64, 0,
256           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
257   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTES_SERVED,
258       g_param_spec_uint64 ("bytes-served", "Bytes served",
259           "Total number of bytes sent to all clients", 0, G_MAXUINT64, 0,
260           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
261   g_object_class_install_property (gobject_class, PROP_SOCKET,
262       g_param_spec_object ("socket", "Socket Handle",
263           "Socket to use for UDP sending. (NULL == allocate)",
264           G_TYPE_SOCKET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
265   g_object_class_install_property (gobject_class, PROP_SOCKET_V6,
266       g_param_spec_object ("socket-v6", "Socket Handle IPv6",
267           "Socket to use for UDPv6 sending. (NULL == allocate)",
268           G_TYPE_SOCKET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
269   g_object_class_install_property (gobject_class, PROP_CLOSE_SOCKET,
270       g_param_spec_boolean ("close-socket", "Close socket",
271           "Close socket if passed as property on state change",
272           DEFAULT_CLOSE_SOCKET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
273   g_object_class_install_property (gobject_class, PROP_USED_SOCKET,
274       g_param_spec_object ("used-socket", "Used Socket Handle",
275           "Socket currently in use for UDP sending. (NULL == no socket)",
276           G_TYPE_SOCKET, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
277   g_object_class_install_property (gobject_class, PROP_USED_SOCKET_V6,
278       g_param_spec_object ("used-socket-v6", "Used Socket Handle IPv6",
279           "Socket currently in use for UDPv6 sending. (NULL == no socket)",
280           G_TYPE_SOCKET, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
281   g_object_class_install_property (gobject_class, PROP_CLIENTS,
282       g_param_spec_string ("clients", "Clients",
283           "A comma separated list of host:port pairs with destinations",
284           DEFAULT_CLIENTS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
285   g_object_class_install_property (gobject_class, PROP_AUTO_MULTICAST,
286       g_param_spec_boolean ("auto-multicast",
287           "Automatically join/leave multicast groups",
288           "Automatically join/leave the multicast groups, FALSE means user"
289           " has to do it himself", DEFAULT_AUTO_MULTICAST,
290           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
291   g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
292       g_param_spec_string ("multicast-iface", "Multicast Interface",
293           "The network interface on which to join the multicast group",
294           DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
295   g_object_class_install_property (gobject_class, PROP_TTL,
296       g_param_spec_int ("ttl", "Unicast TTL",
297           "Used for setting the unicast TTL parameter",
298           0, 255, DEFAULT_TTL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299   g_object_class_install_property (gobject_class, PROP_TTL_MC,
300       g_param_spec_int ("ttl-mc", "Multicast TTL",
301           "Used for setting the multicast TTL parameter",
302           0, 255, DEFAULT_TTL_MC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
303   g_object_class_install_property (gobject_class, PROP_LOOP,
304       g_param_spec_boolean ("loop", "Multicast Loopback",
305           "Used for setting the multicast loop parameter. TRUE = enable,"
306           " FALSE = disable", DEFAULT_LOOP,
307           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
308   /**
309    * GstMultiUDPSink::force-ipv4:
310    *
311    * Force the use of an IPv4 socket.
312    *
313    * Since: 1.0.2
314    */
315 #ifndef GST_REMOVE_DEPRECATED
316   g_object_class_install_property (gobject_class, PROP_FORCE_IPV4,
317       g_param_spec_boolean ("force-ipv4", "Force IPv4",
318           "Forcing the use of an IPv4 socket (DEPRECATED, has no effect anymore)",
319           DEFAULT_FORCE_IPV4,
320           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
321 #endif
322   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QOS_DSCP,
323       g_param_spec_int ("qos-dscp", "QoS diff srv code point",
324           "Quality of Service, differentiated services code point (-1 default)",
325           -1, 63, DEFAULT_QOS_DSCP,
326           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
327   /**
328    * GstMultiUDPSink::send-duplicates:
329    *
330    * When a host/port pair is added mutliple times, send the packet to the host
331    * multiple times as well.
332    */
333   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEND_DUPLICATES,
334       g_param_spec_boolean ("send-duplicates", "Send Duplicates",
335           "When a distination/port pair is added multiple times, send packets "
336           "multiple times as well", DEFAULT_SEND_DUPLICATES,
337           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
338
339   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_SIZE,
340       g_param_spec_int ("buffer-size", "Buffer Size",
341           "Size of the kernel send buffer in bytes, 0=default", 0, G_MAXINT,
342           DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
343
344   g_object_class_install_property (gobject_class, PROP_BIND_ADDRESS,
345       g_param_spec_string ("bind-address", "Bind Address",
346           "Address to bind the socket to", DEFAULT_BIND_ADDRESS,
347           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
348   g_object_class_install_property (gobject_class, PROP_BIND_PORT,
349       g_param_spec_int ("bind-port", "Bind Port",
350           "Port to bind the socket to", 0, G_MAXUINT16,
351           DEFAULT_BIND_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
352
353   gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
354
355   gst_element_class_set_static_metadata (gstelement_class, "UDP packet sender",
356       "Sink/Network",
357       "Send data over the network via UDP to one or multiple recipients "
358       "which can be added or removed at runtime using action signals",
359       "Wim Taymans <wim.taymans@gmail.com>");
360
361   gstbasesink_class->render = gst_multiudpsink_render;
362   gstbasesink_class->render_list = gst_multiudpsink_render_list;
363   gstbasesink_class->start = gst_multiudpsink_start;
364   gstbasesink_class->stop = gst_multiudpsink_stop;
365   gstbasesink_class->unlock = gst_multiudpsink_unlock;
366   gstbasesink_class->unlock_stop = gst_multiudpsink_unlock_stop;
367   klass->add = gst_multiudpsink_add;
368   klass->remove = gst_multiudpsink_remove;
369   klass->clear = gst_multiudpsink_clear;
370   klass->get_stats = gst_multiudpsink_get_stats;
371
372   GST_DEBUG_CATEGORY_INIT (multiudpsink_debug, "multiudpsink", 0, "UDP sink");
373 }
374
375 static void
376 gst_multiudpsink_create_cancellable (GstMultiUDPSink * sink)
377 {
378   GPollFD pollfd;
379
380   sink->cancellable = g_cancellable_new ();
381   sink->made_cancel_fd = g_cancellable_make_pollfd (sink->cancellable, &pollfd);
382 }
383
384 static void
385 gst_multiudpsink_free_cancellable (GstMultiUDPSink * sink)
386 {
387   if (sink->made_cancel_fd) {
388     g_cancellable_release_fd (sink->cancellable);
389     sink->made_cancel_fd = FALSE;
390   }
391   g_object_unref (sink->cancellable);
392   sink->cancellable = NULL;
393 }
394
395 static void
396 gst_multiudpsink_init (GstMultiUDPSink * sink)
397 {
398   guint max_mem;
399
400   g_mutex_init (&sink->client_lock);
401   sink->clients = NULL;
402   sink->num_v4_unique = 0;
403   sink->num_v4_all = 0;
404   sink->num_v6_unique = 0;
405   sink->num_v6_all = 0;
406
407   sink->socket = DEFAULT_SOCKET;
408   sink->socket_v6 = DEFAULT_SOCKET;
409   sink->used_socket = DEFAULT_USED_SOCKET;
410   sink->used_socket_v6 = DEFAULT_USED_SOCKET;
411   sink->close_socket = DEFAULT_CLOSE_SOCKET;
412   sink->external_socket = (sink->socket != NULL);
413   sink->auto_multicast = DEFAULT_AUTO_MULTICAST;
414   sink->ttl = DEFAULT_TTL;
415   sink->ttl_mc = DEFAULT_TTL_MC;
416   sink->loop = DEFAULT_LOOP;
417   sink->force_ipv4 = DEFAULT_FORCE_IPV4;
418   sink->qos_dscp = DEFAULT_QOS_DSCP;
419   sink->send_duplicates = DEFAULT_SEND_DUPLICATES;
420   sink->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
421
422   gst_multiudpsink_create_cancellable (sink);
423
424   /* pre-allocate OutputVector, MapInfo and OutputMessage arrays
425    * for use in the render and render_list functions */
426   max_mem = gst_buffer_get_max_memory ();
427
428   sink->n_vecs = max_mem;
429   sink->vecs = g_new (GOutputVector, sink->n_vecs);
430
431   sink->n_maps = max_mem;
432   sink->maps = g_new (GstMapInfo, sink->n_maps);
433
434   sink->n_messages = 1;
435   sink->messages = g_new (GstOutputMessage, sink->n_messages);
436
437   /* we assume that the number of memories per buffer can fit into a guint8 */
438   g_warn_if_fail (max_mem <= G_MAXUINT8);
439 }
440
441 static GstUDPClient *
442 gst_udp_client_new (GstMultiUDPSink * sink, const gchar * host, gint port)
443 {
444   GstUDPClient *client;
445   GInetAddress *addr;
446   GResolver *resolver;
447   GError *err = NULL;
448
449   addr = g_inet_address_new_from_string (host);
450   if (!addr) {
451     GList *results;
452
453     resolver = g_resolver_get_default ();
454     results =
455         g_resolver_lookup_by_name (resolver, host, sink->cancellable, &err);
456     if (!results)
457       goto name_resolve;
458     addr = G_INET_ADDRESS (g_object_ref (results->data));
459
460     g_resolver_free_addresses (results);
461     g_object_unref (resolver);
462   }
463 #ifndef GST_DISABLE_GST_DEBUG
464   {
465     gchar *ip = g_inet_address_to_string (addr);
466
467     GST_DEBUG_OBJECT (sink, "IP address for host %s is %s", host, ip);
468     g_free (ip);
469   }
470 #endif
471
472   client = g_slice_new0 (GstUDPClient);
473   client->ref_count = 1;
474   client->add_count = 0;
475   client->host = g_strdup (host);
476   client->port = port;
477   client->addr = g_inet_socket_address_new (addr, port);
478   g_object_unref (addr);
479
480   return client;
481
482 name_resolve:
483   {
484     g_clear_error (&err);
485     g_object_unref (resolver);
486
487     return NULL;
488   }
489 }
490
491 /* call with client lock held */
492 static void
493 gst_udp_client_unref (GstUDPClient * client)
494 {
495   if (--client->ref_count == 0) {
496     g_object_unref (client->addr);
497     g_free (client->host);
498     g_slice_free (GstUDPClient, client);
499   }
500 }
501
502 /* call with client lock held */
503 static inline GstUDPClient *
504 gst_udp_client_ref (GstUDPClient * client)
505 {
506   ++client->ref_count;
507   return client;
508 }
509
510 static gint
511 client_compare (GstUDPClient * a, GstUDPClient * b)
512 {
513   if ((a->port == b->port) && (strcmp (a->host, b->host) == 0))
514     return 0;
515
516   return 1;
517 }
518
519 static void
520 gst_multiudpsink_finalize (GObject * object)
521 {
522   GstMultiUDPSink *sink;
523
524   sink = GST_MULTIUDPSINK (object);
525
526   g_list_foreach (sink->clients, (GFunc) gst_udp_client_unref, NULL);
527   g_list_free (sink->clients);
528
529   if (sink->socket)
530     g_object_unref (sink->socket);
531   sink->socket = NULL;
532
533   if (sink->socket_v6)
534     g_object_unref (sink->socket_v6);
535   sink->socket_v6 = NULL;
536
537   if (sink->used_socket)
538     g_object_unref (sink->used_socket);
539   sink->used_socket = NULL;
540
541   if (sink->used_socket_v6)
542     g_object_unref (sink->used_socket_v6);
543   sink->used_socket_v6 = NULL;
544
545   gst_multiudpsink_free_cancellable (sink);
546
547   g_free (sink->multi_iface);
548   sink->multi_iface = NULL;
549
550   g_free (sink->vecs);
551   sink->vecs = NULL;
552   g_free (sink->maps);
553   sink->maps = NULL;
554   g_free (sink->messages);
555   sink->messages = NULL;
556
557   g_free (sink->bind_address);
558   sink->bind_address = NULL;
559
560   g_mutex_clear (&sink->client_lock);
561
562   G_OBJECT_CLASS (parent_class)->finalize (object);
563 }
564
565 /* replacement until we can depend unconditionally on the real one in GLib */
566 #ifndef HAVE_G_SOCKET_SEND_MESSAGES
567 #define g_socket_send_messages gst_socket_send_messages
568
569 static gint
570 gst_socket_send_messages (GSocket * socket, GstOutputMessage * messages,
571     guint num_messages, gint flags, GCancellable * cancellable, GError ** error)
572 {
573   gssize result;
574   gint i;
575
576   for (i = 0; i < num_messages; ++i) {
577     GstOutputMessage *msg = &messages[i];
578     GError *msg_error = NULL;
579
580     result = g_socket_send_message (socket, msg->address,
581         msg->vectors, msg->num_vectors,
582         msg->control_messages, msg->num_control_messages,
583         flags, cancellable, &msg_error);
584
585     if (result < 0) {
586       /* if we couldn't send all messages, just return how many we did
587        * manage to send, provided we managed to send at least one */
588       if (msg_error->code == G_IO_ERROR_WOULD_BLOCK && i > 0) {
589         g_error_free (msg_error);
590         return i;
591       } else {
592         g_propagate_error (error, msg_error);
593         return -1;
594       }
595     }
596
597     msg->bytes_sent = result;
598   }
599
600   return i;
601 }
602 #endif /* HAVE_G_SOCKET_SEND_MESSAGES */
603
604 static gsize
605 fill_vectors (GOutputVector * vecs, GstMapInfo * maps, guint n, GstBuffer * buf)
606 {
607   GstMemory *mem;
608   gsize size = 0;
609   guint i;
610
611   g_assert (gst_buffer_n_memory (buf) == n);
612
613   for (i = 0; i < n; ++i) {
614     mem = gst_buffer_peek_memory (buf, i);
615     if (gst_memory_map (mem, &maps[i], GST_MAP_READ)) {
616       vecs[i].buffer = maps[i].data;
617       vecs[i].size = maps[i].size;
618     } else {
619       GST_WARNING ("Failed to map memory %p for reading", mem);
620       vecs[i].buffer = "";
621       vecs[i].size = 0;
622     }
623     size += vecs[i].size;
624   }
625
626   return size;
627 }
628
629 static gsize
630 gst_udp_calc_message_size (GstOutputMessage * msg)
631 {
632   gsize size = 0;
633   guint i;
634
635   for (i = 0; i < msg->num_vectors; ++i)
636     size += msg->vectors[i].size;
637
638   return size;
639 }
640
641 static gint
642 gst_udp_messsages_find_first_not_sent (GstOutputMessage * messages,
643     guint num_messages)
644 {
645   guint i;
646
647   for (i = 0; i < num_messages; ++i) {
648     GstOutputMessage *msg = &messages[i];
649
650     if (msg->bytes_sent == 0 && gst_udp_calc_message_size (msg) > 0)
651       return i;
652   }
653
654   return -1;
655 }
656
657 static inline gchar *
658 gst_udp_address_get_string (GSocketAddress * addr, gchar * s, gsize size)
659 {
660   GInetSocketAddress *isa = G_INET_SOCKET_ADDRESS (addr);
661   GInetAddress *ia;
662   gchar *addr_str;
663
664   ia = g_inet_socket_address_get_address (isa);
665   addr_str = g_inet_address_to_string (ia);
666   g_snprintf (s, size, "%s:%u", addr_str, g_inet_socket_address_get_port (isa));
667   g_free (addr_str);
668
669   return s;
670 }
671
672 /* Wrapper around g_socket_send_messages() plus error handling (ignoring).
673  * Returns FALSE if we got cancelled, otherwise TRUE. */
674 static gboolean
675 gst_multiudpsink_send_messages (GstMultiUDPSink * sink, GSocket * socket,
676     GstOutputMessage * messages, guint num_messages)
677 {
678   gboolean sent_max_size_warning = FALSE;
679
680   while (num_messages > 0) {
681     gchar astr[64] G_GNUC_UNUSED;
682     GError *err = NULL;
683     guint msg_size, skip, i;
684     gint ret, err_idx;
685
686     ret = g_socket_send_messages (socket, messages, num_messages, 0,
687         sink->cancellable, &err);
688
689     if (G_UNLIKELY (ret < 0)) {
690       GstOutputMessage *msg;
691
692       if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
693         g_clear_error (&err);
694         return FALSE;
695       }
696
697       err_idx = gst_udp_messsages_find_first_not_sent (messages, num_messages);
698       if (err_idx < 0)
699         break;
700
701       msg = &messages[err_idx];
702       msg_size = gst_udp_calc_message_size (msg);
703
704       GST_LOG_OBJECT (sink, "error sending %u bytes to client %s: %s", msg_size,
705           gst_udp_address_get_string (msg->address, astr, sizeof (astr)),
706           err->message);
707
708       skip = 1;
709       if (msg_size > UDP_MAX_SIZE) {
710         if (!sent_max_size_warning) {
711           GST_ELEMENT_WARNING (sink, RESOURCE, WRITE,
712               ("Attempting to send a UDP packets larger than maximum size "
713                   "(%u > %d)", msg_size, UDP_MAX_SIZE),
714               ("Reason: %s", err ? err->message : "unknown reason"));
715           sent_max_size_warning = FALSE;
716         }
717       } else {
718         GST_ELEMENT_WARNING (sink, RESOURCE, WRITE,
719             ("Error sending UDP packets"), ("client %s, reason: %s",
720                 gst_udp_address_get_string (msg->address, astr, sizeof (astr)),
721                 (err != NULL) ? err->message : "unknown reason"));
722
723         for (i = err_idx + 1; i < num_messages; ++i, ++skip) {
724           if (messages[i].address != msg->address)
725             break;
726         }
727         GST_DEBUG_OBJECT (sink, "skipping %d message(s) to same client", skip);
728       }
729
730       /* ignore any errors and try sending the rest */
731       g_clear_error (&err);
732       ret = skip;
733     }
734
735     g_assert (ret <= num_messages);
736
737     messages += ret;
738     num_messages -= ret;
739   }
740
741   return TRUE;
742 }
743
744 static GstFlowReturn
745 gst_multiudpsink_render_buffers (GstMultiUDPSink * sink, GstBuffer ** buffers,
746     guint num_buffers, guint8 * mem_nums, guint total_mem_num)
747 {
748   GstOutputMessage *msgs;
749   gboolean send_duplicates;
750   GstUDPClient **clients;
751   GOutputVector *vecs;
752   GstMapInfo *map_infos;
753   GstFlowReturn flow_ret;
754   guint num_addr_v4, num_addr_v6;
755   guint num_addr, num_msgs;
756   GError *err = NULL;
757   guint i, j, mem;
758   gsize size = 0;
759   GList *l;
760
761   send_duplicates = sink->send_duplicates;
762
763   g_mutex_lock (&sink->client_lock);
764
765   if (send_duplicates) {
766     num_addr_v4 = sink->num_v4_all;
767     num_addr_v6 = sink->num_v6_all;
768   } else {
769     num_addr_v4 = sink->num_v4_unique;
770     num_addr_v6 = sink->num_v6_unique;
771   }
772   num_addr = num_addr_v4 + num_addr_v6;
773
774   if (num_addr == 0)
775     goto no_clients;
776
777   clients = g_newa (GstUDPClient *, num_addr);
778   for (l = sink->clients, i = 0; l != NULL; l = l->next) {
779     GstUDPClient *client = l->data;
780
781     clients[i++] = gst_udp_client_ref (client);
782     for (j = 1; send_duplicates && j < client->add_count; ++j)
783       clients[i++] = gst_udp_client_ref (client);
784   }
785   g_assert_cmpuint (i, ==, num_addr);
786
787   g_mutex_unlock (&sink->client_lock);
788
789   GST_LOG_OBJECT (sink, "%u buffers, %u memories -> to be sent to %u clients",
790       num_buffers, total_mem_num, num_addr);
791
792   /* ensure our pre-allocated scratch space arrays are large enough */
793   if (sink->n_vecs < total_mem_num) {
794     sink->n_vecs = GST_ROUND_UP_16 (total_mem_num);
795     g_free (sink->vecs);
796     sink->vecs = g_new (GOutputVector, sink->n_vecs);
797   }
798   vecs = sink->vecs;
799
800   if (sink->n_maps < total_mem_num) {
801     sink->n_maps = GST_ROUND_UP_16 (total_mem_num);
802     g_free (sink->maps);
803     sink->maps = g_new (GstMapInfo, sink->n_maps);
804   }
805   map_infos = sink->maps;
806
807   num_msgs = num_addr * num_buffers;
808   if (sink->n_messages < num_msgs) {
809     sink->n_messages = GST_ROUND_UP_16 (num_msgs);
810     g_free (sink->messages);
811     sink->messages = g_new (GstOutputMessage, sink->n_messages);
812   }
813   msgs = sink->messages;
814
815   /* populate first num_buffers messages with output vectors for the buffers */
816   for (i = 0, mem = 0; i < num_buffers; ++i) {
817     size += fill_vectors (&vecs[mem], &map_infos[mem], mem_nums[i], buffers[i]);
818     msgs[i].vectors = &vecs[mem];
819     msgs[i].num_vectors = mem_nums[i];
820     msgs[i].num_control_messages = 0;
821     msgs[i].bytes_sent = 0;
822     msgs[i].control_messages = NULL;
823     msgs[i].address = clients[0]->addr;
824     mem += mem_nums[i];
825   }
826
827   /* FIXME: how about some locking? (there wasn't any before either, but..) */
828   sink->bytes_to_serve += size;
829
830   /* now copy the pre-filled num_buffer messages over to the next num_buffer
831    * messages for the next client, where we also change the target adddress */
832   for (i = 1; i < num_addr; ++i) {
833     for (j = 0; j < num_buffers; ++j) {
834       msgs[i * num_buffers + j] = msgs[j];
835       msgs[i * num_buffers + j].address = clients[i]->addr;
836     }
837   }
838
839   /* now send it! */
840   {
841     gboolean ret;
842
843     /* no IPv4 socket? Send it all from the IPv6 socket then.. */
844     if (sink->used_socket == NULL) {
845       ret = gst_multiudpsink_send_messages (sink, sink->used_socket_v6,
846           msgs, num_msgs);
847     } else {
848       guint num_msgs_v4 = num_buffers * num_addr_v4;
849       guint num_msgs_v6 = num_buffers * num_addr_v6;
850
851       /* our client list is sorted with IPv4 clients first and IPv6 ones last */
852       ret = gst_multiudpsink_send_messages (sink, sink->used_socket,
853           msgs, num_msgs_v4);
854
855       if (!ret)
856         goto cancelled;
857
858       ret = gst_multiudpsink_send_messages (sink, sink->used_socket_v6,
859           msgs + num_msgs_v4, num_msgs_v6);
860     }
861
862     if (!ret)
863       goto cancelled;
864   }
865
866   flow_ret = GST_FLOW_OK;
867
868   /* now update stats */
869   g_mutex_lock (&sink->client_lock);
870
871   for (i = 0; i < num_addr; ++i) {
872     GstUDPClient *client = clients[i];
873
874     for (j = 0; j < num_buffers; ++j) {
875       gsize bytes_sent;
876
877       bytes_sent = msgs[i * num_buffers + j].bytes_sent;
878
879       client->bytes_sent += bytes_sent;
880       client->packets_sent++;
881       sink->bytes_served += bytes_sent;
882     }
883     gst_udp_client_unref (client);
884   }
885
886   g_mutex_unlock (&sink->client_lock);
887
888 out:
889
890   for (i = 0; i < mem; ++i)
891     gst_memory_unmap (map_infos[i].memory, &map_infos[i]);
892
893   return flow_ret;
894
895 no_clients:
896   {
897     g_mutex_unlock (&sink->client_lock);
898     GST_LOG_OBJECT (sink, "no clients");
899     return GST_FLOW_OK;
900   }
901 cancelled:
902   {
903     GST_INFO_OBJECT (sink, "cancelled");
904     g_clear_error (&err);
905     flow_ret = GST_FLOW_FLUSHING;
906
907     g_mutex_lock (&sink->client_lock);
908     for (i = 0; i < num_addr; ++i)
909       gst_udp_client_unref (clients[i]);
910     g_mutex_unlock (&sink->client_lock);
911     goto out;
912   }
913 }
914
915 static GstFlowReturn
916 gst_multiudpsink_render_list (GstBaseSink * bsink, GstBufferList * buffer_list)
917 {
918   GstMultiUDPSink *sink;
919   GstBuffer **buffers;
920   GstFlowReturn flow;
921   guint8 *mem_nums;
922   guint total_mems;
923   guint i, num_buffers;
924
925   sink = GST_MULTIUDPSINK_CAST (bsink);
926
927   num_buffers = gst_buffer_list_length (buffer_list);
928   if (num_buffers == 0)
929     goto no_data;
930
931   buffers = g_newa (GstBuffer *, num_buffers);
932   mem_nums = g_newa (guint8, num_buffers);
933   for (i = 0, total_mems = 0; i < num_buffers; ++i) {
934     buffers[i] = gst_buffer_list_get (buffer_list, i);
935     mem_nums[i] = gst_buffer_n_memory (buffers[i]);
936     total_mems += mem_nums[i];
937   }
938
939   flow = gst_multiudpsink_render_buffers (sink, buffers, num_buffers,
940       mem_nums, total_mems);
941
942   return flow;
943
944 no_data:
945   {
946     GST_LOG_OBJECT (sink, "empty buffer");
947     return GST_FLOW_OK;
948   }
949 }
950
951 static GstFlowReturn
952 gst_multiudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
953 {
954   GstMultiUDPSink *sink;
955   GstFlowReturn flow;
956   guint8 n_mem;
957
958   sink = GST_MULTIUDPSINK_CAST (bsink);
959
960   n_mem = gst_buffer_n_memory (buffer);
961
962   if (n_mem > 0)
963     flow = gst_multiudpsink_render_buffers (sink, &buffer, 1, &n_mem, n_mem);
964   else
965     flow = GST_FLOW_OK;
966
967   return flow;
968 }
969
970 static void
971 gst_multiudpsink_set_clients_string (GstMultiUDPSink * sink,
972     const gchar * string)
973 {
974   gchar **clients;
975   gint i;
976
977   clients = g_strsplit (string, ",", 0);
978
979   g_mutex_lock (&sink->client_lock);
980   /* clear all existing clients */
981   gst_multiudpsink_clear_internal (sink, FALSE);
982   for (i = 0; clients[i]; i++) {
983     gchar *host, *p;
984     gint64 port = 0;
985
986     host = clients[i];
987     p = strstr (clients[i], ":");
988     if (p != NULL) {
989       *p = '\0';
990       port = g_ascii_strtoll (p + 1, NULL, 10);
991     }
992     if (port != 0)
993       gst_multiudpsink_add_internal (sink, host, port, FALSE);
994   }
995   g_mutex_unlock (&sink->client_lock);
996
997   g_strfreev (clients);
998 }
999
1000 static gchar *
1001 gst_multiudpsink_get_clients_string (GstMultiUDPSink * sink)
1002 {
1003   GString *str;
1004   GList *clients;
1005
1006   str = g_string_new ("");
1007
1008   g_mutex_lock (&sink->client_lock);
1009   clients = sink->clients;
1010   while (clients) {
1011     GstUDPClient *client;
1012     gint count;
1013
1014     client = (GstUDPClient *) clients->data;
1015
1016     clients = g_list_next (clients);
1017
1018     count = client->add_count;
1019     while (count--) {
1020       g_string_append_printf (str, "%s:%d%s", client->host, client->port,
1021           (clients || count > 1 ? "," : ""));
1022     }
1023   }
1024   g_mutex_unlock (&sink->client_lock);
1025
1026   return g_string_free (str, FALSE);
1027 }
1028
1029 static void
1030 gst_multiudpsink_setup_qos_dscp (GstMultiUDPSink * sink, GSocket * socket)
1031 {
1032   /* don't touch on -1 */
1033   if (sink->qos_dscp < 0)
1034     return;
1035
1036   if (socket == NULL)
1037     return;
1038
1039 #ifdef IP_TOS
1040   {
1041     gint tos;
1042     gint fd;
1043
1044     fd = g_socket_get_fd (socket);
1045
1046     GST_DEBUG_OBJECT (sink, "setting TOS to %d", sink->qos_dscp);
1047
1048     /* Extract and shift 6 bits of DSFIELD */
1049     tos = (sink->qos_dscp & 0x3f) << 2;
1050
1051     if (setsockopt (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0) {
1052       GST_ERROR_OBJECT (sink, "could not set TOS: %s", g_strerror (errno));
1053     }
1054 #ifdef IPV6_TCLASS
1055     if (setsockopt (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0) {
1056       GST_ERROR_OBJECT (sink, "could not set TCLASS: %s", g_strerror (errno));
1057     }
1058 #endif
1059   }
1060 #endif
1061 }
1062
1063 static void
1064 gst_multiudpsink_set_property (GObject * object, guint prop_id,
1065     const GValue * value, GParamSpec * pspec)
1066 {
1067   GstMultiUDPSink *udpsink;
1068
1069   udpsink = GST_MULTIUDPSINK (object);
1070
1071   switch (prop_id) {
1072     case PROP_SOCKET:
1073       if (udpsink->socket != NULL && udpsink->socket != udpsink->used_socket &&
1074           udpsink->close_socket) {
1075         GError *err = NULL;
1076
1077         if (!g_socket_close (udpsink->socket, &err)) {
1078           GST_ERROR ("failed to close socket %p: %s", udpsink->socket,
1079               err->message);
1080           g_clear_error (&err);
1081         }
1082       }
1083       if (udpsink->socket)
1084         g_object_unref (udpsink->socket);
1085       udpsink->socket = g_value_dup_object (value);
1086       GST_DEBUG_OBJECT (udpsink, "setting socket to %p", udpsink->socket);
1087       break;
1088     case PROP_SOCKET_V6:
1089       if (udpsink->socket_v6 != NULL
1090           && udpsink->socket_v6 != udpsink->used_socket_v6
1091           && udpsink->close_socket) {
1092         GError *err = NULL;
1093
1094         if (!g_socket_close (udpsink->socket_v6, &err)) {
1095           GST_ERROR ("failed to close socket %p: %s", udpsink->socket_v6,
1096               err->message);
1097           g_clear_error (&err);
1098         }
1099       }
1100       if (udpsink->socket_v6)
1101         g_object_unref (udpsink->socket_v6);
1102       udpsink->socket_v6 = g_value_dup_object (value);
1103       GST_DEBUG_OBJECT (udpsink, "setting socket to %p", udpsink->socket_v6);
1104       break;
1105     case PROP_CLOSE_SOCKET:
1106       udpsink->close_socket = g_value_get_boolean (value);
1107       break;
1108     case PROP_CLIENTS:
1109       gst_multiudpsink_set_clients_string (udpsink, g_value_get_string (value));
1110       break;
1111     case PROP_AUTO_MULTICAST:
1112       udpsink->auto_multicast = g_value_get_boolean (value);
1113       break;
1114     case PROP_MULTICAST_IFACE:
1115       g_free (udpsink->multi_iface);
1116
1117       if (g_value_get_string (value) == NULL)
1118         udpsink->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1119       else
1120         udpsink->multi_iface = g_value_dup_string (value);
1121       break;
1122     case PROP_TTL:
1123       udpsink->ttl = g_value_get_int (value);
1124       break;
1125     case PROP_TTL_MC:
1126       udpsink->ttl_mc = g_value_get_int (value);
1127       break;
1128     case PROP_LOOP:
1129       udpsink->loop = g_value_get_boolean (value);
1130       break;
1131     case PROP_FORCE_IPV4:
1132       udpsink->force_ipv4 = g_value_get_boolean (value);
1133       break;
1134     case PROP_QOS_DSCP:
1135       udpsink->qos_dscp = g_value_get_int (value);
1136       gst_multiudpsink_setup_qos_dscp (udpsink, udpsink->used_socket);
1137       gst_multiudpsink_setup_qos_dscp (udpsink, udpsink->used_socket_v6);
1138       break;
1139     case PROP_SEND_DUPLICATES:
1140       udpsink->send_duplicates = g_value_get_boolean (value);
1141       break;
1142     case PROP_BUFFER_SIZE:
1143       udpsink->buffer_size = g_value_get_int (value);
1144       break;
1145     case PROP_BIND_ADDRESS:
1146       g_free (udpsink->bind_address);
1147       udpsink->bind_address = g_value_dup_string (value);
1148       break;
1149     case PROP_BIND_PORT:
1150       udpsink->bind_port = g_value_get_int (value);
1151       break;
1152     default:
1153       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1154       break;
1155   }
1156 }
1157
1158 static void
1159 gst_multiudpsink_get_property (GObject * object, guint prop_id, GValue * value,
1160     GParamSpec * pspec)
1161 {
1162   GstMultiUDPSink *udpsink;
1163
1164   udpsink = GST_MULTIUDPSINK (object);
1165
1166   switch (prop_id) {
1167     case PROP_BYTES_TO_SERVE:
1168       g_value_set_uint64 (value, udpsink->bytes_to_serve);
1169       break;
1170     case PROP_BYTES_SERVED:
1171       g_value_set_uint64 (value, udpsink->bytes_served);
1172       break;
1173     case PROP_SOCKET:
1174       g_value_set_object (value, udpsink->socket);
1175       break;
1176     case PROP_SOCKET_V6:
1177       g_value_set_object (value, udpsink->socket_v6);
1178       break;
1179     case PROP_CLOSE_SOCKET:
1180       g_value_set_boolean (value, udpsink->close_socket);
1181       break;
1182     case PROP_USED_SOCKET:
1183       g_value_set_object (value, udpsink->used_socket);
1184       break;
1185     case PROP_USED_SOCKET_V6:
1186       g_value_set_object (value, udpsink->used_socket_v6);
1187       break;
1188     case PROP_CLIENTS:
1189       g_value_take_string (value,
1190           gst_multiudpsink_get_clients_string (udpsink));
1191       break;
1192     case PROP_AUTO_MULTICAST:
1193       g_value_set_boolean (value, udpsink->auto_multicast);
1194       break;
1195     case PROP_MULTICAST_IFACE:
1196       g_value_set_string (value, udpsink->multi_iface);
1197       break;
1198     case PROP_TTL:
1199       g_value_set_int (value, udpsink->ttl);
1200       break;
1201     case PROP_TTL_MC:
1202       g_value_set_int (value, udpsink->ttl_mc);
1203       break;
1204     case PROP_LOOP:
1205       g_value_set_boolean (value, udpsink->loop);
1206       break;
1207     case PROP_FORCE_IPV4:
1208       g_value_set_boolean (value, udpsink->force_ipv4);
1209       break;
1210     case PROP_QOS_DSCP:
1211       g_value_set_int (value, udpsink->qos_dscp);
1212       break;
1213     case PROP_SEND_DUPLICATES:
1214       g_value_set_boolean (value, udpsink->send_duplicates);
1215       break;
1216     case PROP_BUFFER_SIZE:
1217       g_value_set_int (value, udpsink->buffer_size);
1218       break;
1219     case PROP_BIND_ADDRESS:
1220       g_value_set_string (value, udpsink->bind_address);
1221       break;
1222     case PROP_BIND_PORT:
1223       g_value_set_int (value, udpsink->bind_port);
1224       break;
1225     default:
1226       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1227       break;
1228   }
1229 }
1230
1231 static gboolean
1232 gst_multiudpsink_configure_client (GstMultiUDPSink * sink,
1233     GstUDPClient * client)
1234 {
1235   GInetSocketAddress *saddr = G_INET_SOCKET_ADDRESS (client->addr);
1236   GInetAddress *addr = g_inet_socket_address_get_address (saddr);
1237   GSocketFamily family = g_socket_address_get_family (G_SOCKET_ADDRESS (saddr));
1238   GSocket *socket;
1239   GError *err = NULL;
1240
1241   GST_DEBUG_OBJECT (sink, "configuring client %p", client);
1242
1243   if (family == G_SOCKET_FAMILY_IPV6 && !sink->used_socket_v6)
1244     goto invalid_family;
1245
1246   /* Select socket to send from for this address */
1247   if (family == G_SOCKET_FAMILY_IPV6 || !sink->used_socket)
1248     socket = sink->used_socket_v6;
1249   else
1250     socket = sink->used_socket;
1251
1252   if (g_inet_address_get_is_multicast (addr)) {
1253     GST_DEBUG_OBJECT (sink, "we have a multicast client %p", client);
1254     if (sink->auto_multicast) {
1255       GST_DEBUG_OBJECT (sink, "autojoining group");
1256       if (!g_socket_join_multicast_group (socket, addr, FALSE,
1257               sink->multi_iface, &err))
1258         goto join_group_failed;
1259     }
1260     GST_DEBUG_OBJECT (sink, "setting loop to %d", sink->loop);
1261     g_socket_set_multicast_loopback (socket, sink->loop);
1262     GST_DEBUG_OBJECT (sink, "setting ttl to %d", sink->ttl_mc);
1263     g_socket_set_multicast_ttl (socket, sink->ttl_mc);
1264   } else {
1265     GST_DEBUG_OBJECT (sink, "setting unicast ttl to %d", sink->ttl);
1266     g_socket_set_ttl (socket, sink->ttl);
1267   }
1268   return TRUE;
1269
1270   /* ERRORS */
1271 join_group_failed:
1272   {
1273     gst_multiudpsink_stop (GST_BASE_SINK (sink));
1274     GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
1275         ("Could not join multicast group: %s",
1276             err ? err->message : "unknown reason"));
1277     g_clear_error (&err);
1278     return FALSE;
1279   }
1280 invalid_family:
1281   {
1282     gst_multiudpsink_stop (GST_BASE_SINK (sink));
1283     GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
1284         ("Invalid address family (got %d)", family));
1285     return FALSE;
1286   }
1287 }
1288
1289 /* create a socket for sending to remote machine */
1290 static gboolean
1291 gst_multiudpsink_start (GstBaseSink * bsink)
1292 {
1293   GstMultiUDPSink *sink;
1294   GList *clients;
1295   GstUDPClient *client;
1296   GError *err = NULL;
1297
1298   sink = GST_MULTIUDPSINK (bsink);
1299
1300   sink->external_socket = FALSE;
1301
1302   if (sink->socket) {
1303     GST_DEBUG_OBJECT (sink, "using configured socket");
1304     if (g_socket_get_family (sink->socket) == G_SOCKET_FAMILY_IPV6) {
1305       sink->used_socket_v6 = G_SOCKET (g_object_ref (sink->socket));
1306       sink->external_socket = TRUE;
1307     } else {
1308       sink->used_socket = G_SOCKET (g_object_ref (sink->socket));
1309       sink->external_socket = TRUE;
1310     }
1311   }
1312
1313   if (sink->socket_v6) {
1314     GST_DEBUG_OBJECT (sink, "using configured IPv6 socket");
1315     g_return_val_if_fail (!sink->socket || g_socket_get_family (sink->socket) !=
1316         G_SOCKET_FAMILY_IPV6, FALSE);
1317
1318     if (sink->used_socket_v6 && sink->used_socket_v6 != sink->socket_v6) {
1319       GST_ERROR_OBJECT (sink,
1320           "Provided different IPv6 sockets in socket and socket-v6 properties");
1321       return FALSE;
1322     }
1323
1324     sink->used_socket_v6 = G_SOCKET (g_object_ref (sink->socket_v6));
1325     sink->external_socket = TRUE;
1326   }
1327
1328   if (!sink->used_socket && !sink->used_socket_v6) {
1329     GSocketAddress *bind_addr;
1330     GInetAddress *bind_iaddr;
1331
1332     if (sink->bind_address) {
1333       GSocketFamily family;
1334
1335       bind_iaddr = g_inet_address_new_from_string (sink->bind_address);
1336       if (!bind_iaddr) {
1337         GList *results;
1338         GResolver *resolver;
1339
1340         resolver = g_resolver_get_default ();
1341         results =
1342             g_resolver_lookup_by_name (resolver, sink->bind_address,
1343             sink->cancellable, &err);
1344         if (!results) {
1345           g_object_unref (resolver);
1346           goto name_resolve;
1347         }
1348         bind_iaddr = G_INET_ADDRESS (g_object_ref (results->data));
1349         g_resolver_free_addresses (results);
1350         g_object_unref (resolver);
1351       }
1352
1353       bind_addr = g_inet_socket_address_new (bind_iaddr, sink->bind_port);
1354       g_object_unref (bind_iaddr);
1355       family = g_socket_address_get_family (G_SOCKET_ADDRESS (bind_addr));
1356
1357       if ((sink->used_socket =
1358               g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1359                   G_SOCKET_PROTOCOL_UDP, &err)) == NULL) {
1360         g_object_unref (bind_addr);
1361         goto no_socket;
1362       }
1363
1364       g_socket_bind (sink->used_socket, bind_addr, TRUE, &err);
1365       g_object_unref (bind_addr);
1366       if (err != NULL)
1367         goto bind_error;
1368     } else {
1369       /* create sender sockets if none available */
1370       if ((sink->used_socket = g_socket_new (G_SOCKET_FAMILY_IPV4,
1371                   G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &err)) == NULL)
1372         goto no_socket;
1373
1374       bind_iaddr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
1375       bind_addr = g_inet_socket_address_new (bind_iaddr, sink->bind_port);
1376       g_socket_bind (sink->used_socket, bind_addr, TRUE, &err);
1377       g_object_unref (bind_addr);
1378       g_object_unref (bind_iaddr);
1379       if (err != NULL)
1380         goto bind_error;
1381
1382       if ((sink->used_socket_v6 = g_socket_new (G_SOCKET_FAMILY_IPV6,
1383                   G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP,
1384                   &err)) == NULL) {
1385         GST_INFO_OBJECT (sink, "Failed to create IPv6 socket: %s",
1386             err->message);
1387         g_clear_error (&err);
1388       } else {
1389         bind_iaddr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV6);
1390         bind_addr = g_inet_socket_address_new (bind_iaddr, sink->bind_port);
1391         g_socket_bind (sink->used_socket_v6, bind_addr, TRUE, &err);
1392         g_object_unref (bind_addr);
1393         g_object_unref (bind_iaddr);
1394         if (err != NULL)
1395           goto bind_error;
1396       }
1397     }
1398   }
1399 #ifdef SO_SNDBUF
1400   {
1401     socklen_t len;
1402     gint sndsize, ret;
1403
1404     len = sizeof (sndsize);
1405     if (sink->buffer_size != 0) {
1406       sndsize = sink->buffer_size;
1407
1408       GST_DEBUG_OBJECT (sink, "setting udp buffer of %d bytes", sndsize);
1409       /* set buffer size, Note that on Linux this is typically limited to a
1410        * maximum of around 100K. Also a minimum of 128 bytes is required on
1411        * Linux. */
1412
1413       if (sink->used_socket) {
1414         ret =
1415             setsockopt (g_socket_get_fd (sink->used_socket), SOL_SOCKET,
1416             SO_SNDBUF, (void *) &sndsize, len);
1417         if (ret != 0) {
1418           GST_ELEMENT_WARNING (sink, RESOURCE, SETTINGS, (NULL),
1419               ("Could not create a buffer of requested %d bytes, %d: %s",
1420                   sndsize, ret, g_strerror (errno)));
1421         }
1422       }
1423
1424       if (sink->used_socket_v6) {
1425         ret =
1426             setsockopt (g_socket_get_fd (sink->used_socket_v6), SOL_SOCKET,
1427             SO_SNDBUF, (void *) &sndsize, len);
1428         if (ret != 0) {
1429           GST_ELEMENT_WARNING (sink, RESOURCE, SETTINGS, (NULL),
1430               ("Could not create a buffer of requested %d bytes, %d: %s",
1431                   sndsize, ret, g_strerror (errno)));
1432         }
1433       }
1434     }
1435
1436     /* read the value of the receive buffer. Note that on linux this returns 2x the
1437      * value we set because the kernel allocates extra memory for metadata.
1438      * The default on Linux is about 100K (which is about 50K without metadata) */
1439     if (sink->used_socket) {
1440       ret =
1441           getsockopt (g_socket_get_fd (sink->used_socket), SOL_SOCKET,
1442           SO_SNDBUF, (void *) &sndsize, &len);
1443       if (ret == 0)
1444         GST_DEBUG_OBJECT (sink, "have UDP buffer of %d bytes", sndsize);
1445       else
1446         GST_DEBUG_OBJECT (sink, "could not get UDP buffer size");
1447     }
1448
1449     if (sink->used_socket_v6) {
1450       ret =
1451           getsockopt (g_socket_get_fd (sink->used_socket_v6), SOL_SOCKET,
1452           SO_SNDBUF, (void *) &sndsize, &len);
1453       if (ret == 0)
1454         GST_DEBUG_OBJECT (sink, "have UDPv6 buffer of %d bytes", sndsize);
1455       else
1456         GST_DEBUG_OBJECT (sink, "could not get UDPv6 buffer size");
1457     }
1458   }
1459 #endif
1460
1461 #ifdef SO_BINDTODEVICE
1462   if (sink->multi_iface) {
1463     if (sink->used_socket) {
1464       if (setsockopt (g_socket_get_fd (sink->used_socket), SOL_SOCKET,
1465               SO_BINDTODEVICE, sink->multi_iface,
1466               strlen (sink->multi_iface)) < 0)
1467         GST_WARNING_OBJECT (sink, "setsockopt SO_BINDTODEVICE failed: %s",
1468             strerror (errno));
1469     }
1470     if (sink->used_socket_v6) {
1471       if (setsockopt (g_socket_get_fd (sink->used_socket_v6), SOL_SOCKET,
1472               SO_BINDTODEVICE, sink->multi_iface,
1473               strlen (sink->multi_iface)) < 0)
1474         GST_WARNING_OBJECT (sink, "setsockopt SO_BINDTODEVICE failed (v6): %s",
1475             strerror (errno));
1476     }
1477   }
1478 #endif
1479
1480   if (sink->used_socket)
1481     g_socket_set_broadcast (sink->used_socket, TRUE);
1482   if (sink->used_socket_v6)
1483     g_socket_set_broadcast (sink->used_socket_v6, TRUE);
1484
1485   sink->bytes_to_serve = 0;
1486   sink->bytes_served = 0;
1487
1488   gst_multiudpsink_setup_qos_dscp (sink, sink->used_socket);
1489   gst_multiudpsink_setup_qos_dscp (sink, sink->used_socket_v6);
1490
1491   /* look for multicast clients and join multicast groups appropriately
1492      set also ttl and multicast loopback delivery appropriately  */
1493   for (clients = sink->clients; clients; clients = g_list_next (clients)) {
1494     client = (GstUDPClient *) clients->data;
1495
1496     if (!gst_multiudpsink_configure_client (sink, client))
1497       return FALSE;
1498   }
1499   return TRUE;
1500
1501   /* ERRORS */
1502 no_socket:
1503   {
1504     GST_ELEMENT_ERROR (sink, RESOURCE, FAILED, (NULL),
1505         ("Could not create socket: %s", err->message));
1506     g_clear_error (&err);
1507     return FALSE;
1508   }
1509 bind_error:
1510   {
1511     GST_ELEMENT_ERROR (sink, RESOURCE, FAILED, (NULL),
1512         ("Failed to bind socket: %s", err->message));
1513     g_clear_error (&err);
1514     return FALSE;
1515   }
1516 name_resolve:
1517   {
1518     GST_ELEMENT_ERROR (sink, RESOURCE, FAILED, (NULL),
1519         ("Failed to resolve bind address %s: %s", sink->bind_address,
1520             err->message));
1521     g_clear_error (&err);
1522     return FALSE;
1523   }
1524 }
1525
1526 static gboolean
1527 gst_multiudpsink_stop (GstBaseSink * bsink)
1528 {
1529   GstMultiUDPSink *udpsink;
1530
1531   udpsink = GST_MULTIUDPSINK (bsink);
1532
1533   if (udpsink->used_socket) {
1534     if (udpsink->close_socket || !udpsink->external_socket) {
1535       GError *err = NULL;
1536
1537       if (!g_socket_close (udpsink->used_socket, &err)) {
1538         GST_ERROR_OBJECT (udpsink, "Failed to close socket: %s", err->message);
1539         g_clear_error (&err);
1540       }
1541     }
1542
1543     g_object_unref (udpsink->used_socket);
1544     udpsink->used_socket = NULL;
1545   }
1546
1547   if (udpsink->used_socket_v6) {
1548     if (udpsink->close_socket || !udpsink->external_socket) {
1549       GError *err = NULL;
1550
1551       if (!g_socket_close (udpsink->used_socket_v6, &err)) {
1552         GST_ERROR_OBJECT (udpsink, "Failed to close socket: %s", err->message);
1553         g_clear_error (&err);
1554       }
1555     }
1556
1557     g_object_unref (udpsink->used_socket_v6);
1558     udpsink->used_socket_v6 = NULL;
1559   }
1560
1561   return TRUE;
1562 }
1563
1564 static gint
1565 gst_udp_client_compare_socket_family (GstUDPClient * a, GstUDPClient * b)
1566 {
1567   GSocketFamily fa = g_socket_address_get_family (a->addr);
1568   GSocketFamily fb = g_socket_address_get_family (b->addr);
1569
1570   if (fa == fb)
1571     return 0;
1572
1573   /* a should go before b */
1574   if (fa == G_SOCKET_FAMILY_IPV4 && fb == G_SOCKET_FAMILY_IPV6)
1575     return -1;
1576
1577   /* b should go before a */
1578   return 1;
1579 }
1580
1581 static void
1582 gst_multiudpsink_add_internal (GstMultiUDPSink * sink, const gchar * host,
1583     gint port, gboolean lock)
1584 {
1585   GSocketFamily family;
1586   GstUDPClient *client;
1587   GstUDPClient udpclient;
1588   GTimeVal now;
1589   GList *find;
1590
1591   udpclient.host = (gchar *) host;
1592   udpclient.port = port;
1593
1594   GST_DEBUG_OBJECT (sink, "adding client on host %s, port %d", host, port);
1595
1596   if (lock)
1597     g_mutex_lock (&sink->client_lock);
1598
1599   find = g_list_find_custom (sink->clients, &udpclient,
1600       (GCompareFunc) client_compare);
1601
1602   if (!find) {
1603     find = g_list_find_custom (sink->clients_to_be_removed, &udpclient,
1604         (GCompareFunc) client_compare);
1605     if (find)
1606       gst_udp_client_ref (find->data);
1607   }
1608
1609   if (find) {
1610     client = (GstUDPClient *) find->data;
1611
1612     family = g_socket_address_get_family (client->addr);
1613
1614     GST_DEBUG_OBJECT (sink, "found %d existing clients with host %s, port %d",
1615         client->add_count, host, port);
1616   } else {
1617     client = gst_udp_client_new (sink, host, port);
1618     if (!client)
1619       goto error;
1620
1621     family = g_socket_address_get_family (client->addr);
1622
1623     g_get_current_time (&now);
1624     client->connect_time = GST_TIMEVAL_TO_TIME (now);
1625
1626     if (sink->used_socket)
1627       gst_multiudpsink_configure_client (sink, client);
1628
1629     GST_DEBUG_OBJECT (sink, "add client with host %s, port %d", host, port);
1630
1631     /* keep IPv4 clients at the beginning, and IPv6 at the end, we can make
1632      * use of this in gst_multiudpsink_render_buffers() */
1633     sink->clients = g_list_insert_sorted (sink->clients, client,
1634         (GCompareFunc) gst_udp_client_compare_socket_family);
1635
1636     if (family == G_SOCKET_FAMILY_IPV4)
1637       ++sink->num_v4_unique;
1638     else
1639       ++sink->num_v6_unique;
1640   }
1641
1642   ++client->add_count;
1643
1644   if (family == G_SOCKET_FAMILY_IPV4)
1645     ++sink->num_v4_all;
1646   else
1647     ++sink->num_v6_all;
1648
1649   if (lock)
1650     g_mutex_unlock (&sink->client_lock);
1651
1652   g_signal_emit (G_OBJECT (sink),
1653       gst_multiudpsink_signals[SIGNAL_CLIENT_ADDED], 0, host, port);
1654
1655   GST_DEBUG_OBJECT (sink, "added client on host %s, port %d", host, port);
1656   return;
1657
1658   /* ERRORS */
1659 error:
1660   {
1661     GST_DEBUG_OBJECT (sink, "did not add client on host %s, port %d", host,
1662         port);
1663     if (lock)
1664       g_mutex_unlock (&sink->client_lock);
1665     return;
1666   }
1667 }
1668
1669 void
1670 gst_multiudpsink_add (GstMultiUDPSink * sink, const gchar * host, gint port)
1671 {
1672   gst_multiudpsink_add_internal (sink, host, port, TRUE);
1673 }
1674
1675 void
1676 gst_multiudpsink_remove (GstMultiUDPSink * sink, const gchar * host, gint port)
1677 {
1678   GSocketFamily family;
1679   GList *find;
1680   GstUDPClient udpclient;
1681   GstUDPClient *client;
1682   GTimeVal now;
1683
1684   udpclient.host = (gchar *) host;
1685   udpclient.port = port;
1686
1687   g_mutex_lock (&sink->client_lock);
1688   find = g_list_find_custom (sink->clients, &udpclient,
1689       (GCompareFunc) client_compare);
1690   if (!find)
1691     goto not_found;
1692
1693   client = (GstUDPClient *) find->data;
1694
1695   GST_DEBUG_OBJECT (sink, "found %d clients with host %s, port %d",
1696       client->add_count, host, port);
1697
1698   --client->add_count;
1699
1700   family = g_socket_address_get_family (client->addr);
1701   if (family == G_SOCKET_FAMILY_IPV4)
1702     --sink->num_v4_all;
1703   else
1704     --sink->num_v6_all;
1705
1706   if (client->add_count == 0) {
1707     GInetSocketAddress *saddr = G_INET_SOCKET_ADDRESS (client->addr);
1708     GInetAddress *addr = g_inet_socket_address_get_address (saddr);
1709     GSocket *socket;
1710
1711     /* Select socket to send from for this address */
1712     if (family == G_SOCKET_FAMILY_IPV6 || !sink->used_socket)
1713       socket = sink->used_socket_v6;
1714     else
1715       socket = sink->used_socket;
1716
1717     GST_DEBUG_OBJECT (sink, "remove client with host %s, port %d", host, port);
1718
1719     g_get_current_time (&now);
1720     client->disconnect_time = GST_TIMEVAL_TO_TIME (now);
1721
1722     if (socket && sink->auto_multicast
1723         && g_inet_address_get_is_multicast (addr)) {
1724       GError *err = NULL;
1725
1726       if (!g_socket_leave_multicast_group (socket, addr, FALSE,
1727               sink->multi_iface, &err)) {
1728         GST_DEBUG_OBJECT (sink, "Failed to leave multicast group: %s",
1729             err->message);
1730         g_clear_error (&err);
1731       }
1732     }
1733
1734     if (family == G_SOCKET_FAMILY_IPV4)
1735       --sink->num_v4_unique;
1736     else
1737       --sink->num_v6_unique;
1738
1739     /* Keep state consistent for streaming thread, so remove from client list,
1740      * but keep it around until after the signal has been emitted, in case a
1741      * callback wants to get stats for that client or so */
1742     sink->clients = g_list_delete_link (sink->clients, find);
1743
1744     sink->clients_to_be_removed =
1745         g_list_prepend (sink->clients_to_be_removed, client);
1746
1747     /* Unlock to emit signal before we delete the actual client */
1748     g_mutex_unlock (&sink->client_lock);
1749     g_signal_emit (G_OBJECT (sink),
1750         gst_multiudpsink_signals[SIGNAL_CLIENT_REMOVED], 0, host, port);
1751     g_mutex_lock (&sink->client_lock);
1752
1753     sink->clients_to_be_removed =
1754         g_list_remove (sink->clients_to_be_removed, client);
1755
1756     gst_udp_client_unref (client);
1757   }
1758   g_mutex_unlock (&sink->client_lock);
1759
1760   return;
1761
1762   /* ERRORS */
1763 not_found:
1764   {
1765     g_mutex_unlock (&sink->client_lock);
1766     GST_WARNING_OBJECT (sink, "client at host %s, port %d not found",
1767         host, port);
1768     return;
1769   }
1770 }
1771
1772 static void
1773 gst_multiudpsink_clear_internal (GstMultiUDPSink * sink, gboolean lock)
1774 {
1775   GST_DEBUG_OBJECT (sink, "clearing");
1776   /* we only need to remove the client structure, there is no additional
1777    * socket or anything to free for UDP */
1778   if (lock)
1779     g_mutex_lock (&sink->client_lock);
1780   g_list_foreach (sink->clients, (GFunc) gst_udp_client_unref, sink);
1781   g_list_free (sink->clients);
1782   sink->clients = NULL;
1783   sink->num_v4_unique = 0;
1784   sink->num_v4_all = 0;
1785   sink->num_v6_unique = 0;
1786   sink->num_v6_all = 0;
1787   if (lock)
1788     g_mutex_unlock (&sink->client_lock);
1789 }
1790
1791 void
1792 gst_multiudpsink_clear (GstMultiUDPSink * sink)
1793 {
1794   gst_multiudpsink_clear_internal (sink, TRUE);
1795 }
1796
1797 GstStructure *
1798 gst_multiudpsink_get_stats (GstMultiUDPSink * sink, const gchar * host,
1799     gint port)
1800 {
1801   GstUDPClient *client;
1802   GstStructure *result = NULL;
1803   GstUDPClient udpclient;
1804   GList *find;
1805
1806   udpclient.host = (gchar *) host;
1807   udpclient.port = port;
1808
1809   g_mutex_lock (&sink->client_lock);
1810
1811   find = g_list_find_custom (sink->clients, &udpclient,
1812       (GCompareFunc) client_compare);
1813
1814   if (!find)
1815     find = g_list_find_custom (sink->clients_to_be_removed, &udpclient,
1816         (GCompareFunc) client_compare);
1817
1818   if (!find)
1819     goto not_found;
1820
1821   GST_DEBUG_OBJECT (sink, "stats for client with host %s, port %d", host, port);
1822
1823   client = (GstUDPClient *) find->data;
1824
1825   result = gst_structure_new_empty ("multiudpsink-stats");
1826
1827   gst_structure_set (result,
1828       "bytes-sent", G_TYPE_UINT64, client->bytes_sent,
1829       "packets-sent", G_TYPE_UINT64, client->packets_sent,
1830       "connect-time", G_TYPE_UINT64, client->connect_time,
1831       "disconnect-time", G_TYPE_UINT64, client->disconnect_time, NULL);
1832
1833   g_mutex_unlock (&sink->client_lock);
1834
1835   return result;
1836
1837   /* ERRORS */
1838 not_found:
1839   {
1840     g_mutex_unlock (&sink->client_lock);
1841     GST_WARNING_OBJECT (sink, "client with host %s, port %d not found",
1842         host, port);
1843     /* Apparently (see comment in gstmultifdsink.c) returning NULL from here may
1844      * confuse/break python bindings */
1845     return gst_structure_new_empty ("multiudpsink-stats");
1846   }
1847 }
1848
1849 static gboolean
1850 gst_multiudpsink_unlock (GstBaseSink * bsink)
1851 {
1852   GstMultiUDPSink *sink;
1853
1854   sink = GST_MULTIUDPSINK (bsink);
1855
1856   g_cancellable_cancel (sink->cancellable);
1857
1858   return TRUE;
1859 }
1860
1861 static gboolean
1862 gst_multiudpsink_unlock_stop (GstBaseSink * bsink)
1863 {
1864   GstMultiUDPSink *sink;
1865
1866   sink = GST_MULTIUDPSINK (bsink);
1867
1868   gst_multiudpsink_free_cancellable (sink);
1869   gst_multiudpsink_create_cancellable (sink);
1870
1871   return TRUE;
1872 }