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>
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.
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.
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.
24 * SECTION:element-multiudpsink
25 * @see_also: udpsink, multifdsink
27 * multiudpsink is a network sink that sends UDP packets to multiple
29 * It can be combined with rtp payload encoders to implement RTP streaming.
35 #include "gstmultiudpsink.h"
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
43 #include <netinet/in.h>
46 #include "gst/glib-compat-private.h"
48 GST_DEBUG_CATEGORY_STATIC (multiudpsink_debug);
49 #define GST_CAT_DEFAULT (multiudpsink_debug)
51 #define UDP_MAX_SIZE 65507
53 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
58 /* MultiUDPSink signals and args */
69 SIGNAL_CLIENT_REMOVED,
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
106 PROP_MULTICAST_IFACE,
112 PROP_SEND_DUPLICATES,
119 static void gst_multiudpsink_finalize (GObject * object);
121 static GstFlowReturn gst_multiudpsink_render (GstBaseSink * sink,
123 static GstFlowReturn gst_multiudpsink_render_list (GstBaseSink * bsink,
124 GstBufferList * buffer_list);
126 static gboolean gst_multiudpsink_start (GstBaseSink * bsink);
127 static gboolean gst_multiudpsink_stop (GstBaseSink * bsink);
128 static gboolean gst_multiudpsink_unlock (GstBaseSink * bsink);
129 static gboolean gst_multiudpsink_unlock_stop (GstBaseSink * bsink);
131 static void gst_multiudpsink_set_property (GObject * object, guint prop_id,
132 const GValue * value, GParamSpec * pspec);
133 static void gst_multiudpsink_get_property (GObject * object, guint prop_id,
134 GValue * value, GParamSpec * pspec);
136 static void gst_multiudpsink_add_internal (GstMultiUDPSink * sink,
137 const gchar * host, gint port, gboolean lock);
138 static void gst_multiudpsink_clear_internal (GstMultiUDPSink * sink,
141 static guint gst_multiudpsink_signals[LAST_SIGNAL] = { 0 };
143 #define gst_multiudpsink_parent_class parent_class
144 G_DEFINE_TYPE (GstMultiUDPSink, gst_multiudpsink, GST_TYPE_BASE_SINK);
147 gst_multiudpsink_class_init (GstMultiUDPSinkClass * klass)
149 GObjectClass *gobject_class;
150 GstElementClass *gstelement_class;
151 GstBaseSinkClass *gstbasesink_class;
153 gobject_class = (GObjectClass *) klass;
154 gstelement_class = (GstElementClass *) klass;
155 gstbasesink_class = (GstBaseSinkClass *) klass;
157 gobject_class->set_property = gst_multiudpsink_set_property;
158 gobject_class->get_property = gst_multiudpsink_get_property;
159 gobject_class->finalize = gst_multiudpsink_finalize;
162 * GstMultiUDPSink::add:
163 * @gstmultiudpsink: the sink on which the signal is emitted
164 * @host: the hostname/IP address of the client to add
165 * @port: the port of the client to add
167 * Add a client with destination @host and @port to the list of
168 * clients. When the same host/port pair is added multiple times, the
169 * send-duplicates property defines if the packets are sent multiple times to
170 * the same host/port pair or not.
172 * When a host/port pair is added multiple times, an equal amount of remove
173 * calls must be performed to actually remove the host/port pair from the list
176 gst_multiudpsink_signals[SIGNAL_ADD] =
177 g_signal_new ("add", G_TYPE_FROM_CLASS (klass),
178 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
179 G_STRUCT_OFFSET (GstMultiUDPSinkClass, add),
180 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
181 G_TYPE_STRING, G_TYPE_INT);
183 * GstMultiUDPSink::remove:
184 * @gstmultiudpsink: the sink on which the signal is emitted
185 * @host: the hostname/IP address of the client to remove
186 * @port: the port of the client to remove
188 * Remove the client with destination @host and @port from the list of
191 gst_multiudpsink_signals[SIGNAL_REMOVE] =
192 g_signal_new ("remove", G_TYPE_FROM_CLASS (klass),
193 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
194 G_STRUCT_OFFSET (GstMultiUDPSinkClass, remove),
195 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
196 G_TYPE_STRING, G_TYPE_INT);
198 * GstMultiUDPSink::clear:
199 * @gstmultiudpsink: the sink on which the signal is emitted
201 * Clear the list of clients.
203 gst_multiudpsink_signals[SIGNAL_CLEAR] =
204 g_signal_new ("clear", G_TYPE_FROM_CLASS (klass),
205 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
206 G_STRUCT_OFFSET (GstMultiUDPSinkClass, clear),
207 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 0);
209 * GstMultiUDPSink::get-stats:
210 * @gstmultiudpsink: the sink on which the signal is emitted
211 * @host: the hostname/IP address of the client to get stats on
212 * @port: the port of the client to get stats on
214 * Get the statistics of the client with destination @host and @port.
216 * Returns: a GstStructure: bytes_sent, packets_sent,
217 * connect_time (in epoch seconds), disconnect_time (in epoch seconds)
219 gst_multiudpsink_signals[SIGNAL_GET_STATS] =
220 g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass),
221 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
222 G_STRUCT_OFFSET (GstMultiUDPSinkClass, get_stats),
223 NULL, NULL, g_cclosure_marshal_generic, GST_TYPE_STRUCTURE, 2,
224 G_TYPE_STRING, G_TYPE_INT);
226 * GstMultiUDPSink::client-added:
227 * @gstmultiudpsink: the sink emitting the signal
228 * @host: the hostname/IP address of the added client
229 * @port: the port of the added client
231 * Signal emited when a new client is added to the list of
234 gst_multiudpsink_signals[SIGNAL_CLIENT_ADDED] =
235 g_signal_new ("client-added", G_TYPE_FROM_CLASS (klass),
236 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiUDPSinkClass, client_added),
237 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
238 G_TYPE_STRING, G_TYPE_INT);
240 * GstMultiUDPSink::client-removed:
241 * @gstmultiudpsink: the sink emitting the signal
242 * @host: the hostname/IP address of the removed client
243 * @port: the port of the removed client
245 * Signal emited when a client is removed from the list of
248 gst_multiudpsink_signals[SIGNAL_CLIENT_REMOVED] =
249 g_signal_new ("client-removed", G_TYPE_FROM_CLASS (klass),
250 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiUDPSinkClass,
251 client_removed), NULL, NULL, g_cclosure_marshal_generic,
252 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
254 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTES_TO_SERVE,
255 g_param_spec_uint64 ("bytes-to-serve", "Bytes to serve",
256 "Number of bytes received to serve to clients", 0, G_MAXUINT64, 0,
257 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
258 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTES_SERVED,
259 g_param_spec_uint64 ("bytes-served", "Bytes served",
260 "Total number of bytes sent to all clients", 0, G_MAXUINT64, 0,
261 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
262 g_object_class_install_property (gobject_class, PROP_SOCKET,
263 g_param_spec_object ("socket", "Socket Handle",
264 "Socket to use for UDP sending. (NULL == allocate)",
265 G_TYPE_SOCKET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
266 g_object_class_install_property (gobject_class, PROP_SOCKET_V6,
267 g_param_spec_object ("socket-v6", "Socket Handle IPv6",
268 "Socket to use for UDPv6 sending. (NULL == allocate)",
269 G_TYPE_SOCKET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
270 g_object_class_install_property (gobject_class, PROP_CLOSE_SOCKET,
271 g_param_spec_boolean ("close-socket", "Close socket",
272 "Close socket if passed as property on state change",
273 DEFAULT_CLOSE_SOCKET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
274 g_object_class_install_property (gobject_class, PROP_USED_SOCKET,
275 g_param_spec_object ("used-socket", "Used Socket Handle",
276 "Socket currently in use for UDP sending. (NULL == no socket)",
277 G_TYPE_SOCKET, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
278 g_object_class_install_property (gobject_class, PROP_USED_SOCKET_V6,
279 g_param_spec_object ("used-socket-v6", "Used Socket Handle IPv6",
280 "Socket currently in use for UDPv6 sending. (NULL == no socket)",
281 G_TYPE_SOCKET, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
282 g_object_class_install_property (gobject_class, PROP_CLIENTS,
283 g_param_spec_string ("clients", "Clients",
284 "A comma separated list of host:port pairs with destinations",
285 DEFAULT_CLIENTS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
286 g_object_class_install_property (gobject_class, PROP_AUTO_MULTICAST,
287 g_param_spec_boolean ("auto-multicast",
288 "Automatically join/leave multicast groups",
289 "Automatically join/leave the multicast groups, FALSE means user"
290 " has to do it himself", DEFAULT_AUTO_MULTICAST,
291 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
292 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
293 g_param_spec_string ("multicast-iface", "Multicast Interface",
294 "The network interface on which to join the multicast group",
295 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
296 g_object_class_install_property (gobject_class, PROP_TTL,
297 g_param_spec_int ("ttl", "Unicast TTL",
298 "Used for setting the unicast TTL parameter",
299 0, 255, DEFAULT_TTL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
300 g_object_class_install_property (gobject_class, PROP_TTL_MC,
301 g_param_spec_int ("ttl-mc", "Multicast TTL",
302 "Used for setting the multicast TTL parameter",
303 0, 255, DEFAULT_TTL_MC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
304 g_object_class_install_property (gobject_class, PROP_LOOP,
305 g_param_spec_boolean ("loop", "Multicast Loopback",
306 "Used for setting the multicast loop parameter. TRUE = enable,"
307 " FALSE = disable", DEFAULT_LOOP,
308 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
310 * GstMultiUDPSink::force-ipv4:
312 * Force the use of an IPv4 socket.
316 #ifndef GST_REMOVE_DEPRECATED
317 g_object_class_install_property (gobject_class, PROP_FORCE_IPV4,
318 g_param_spec_boolean ("force-ipv4", "Force IPv4",
319 "Forcing the use of an IPv4 socket (DEPRECATED, has no effect anymore)",
321 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
323 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QOS_DSCP,
324 g_param_spec_int ("qos-dscp", "QoS diff srv code point",
325 "Quality of Service, differentiated services code point (-1 default)",
326 -1, 63, DEFAULT_QOS_DSCP,
327 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
329 * GstMultiUDPSink::send-duplicates:
331 * When a host/port pair is added mutliple times, send the packet to the host
332 * multiple times as well.
334 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEND_DUPLICATES,
335 g_param_spec_boolean ("send-duplicates", "Send Duplicates",
336 "When a distination/port pair is added multiple times, send packets "
337 "multiple times as well", DEFAULT_SEND_DUPLICATES,
338 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
340 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_SIZE,
341 g_param_spec_int ("buffer-size", "Buffer Size",
342 "Size of the kernel send buffer in bytes, 0=default", 0, G_MAXINT,
343 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
345 g_object_class_install_property (gobject_class, PROP_BIND_ADDRESS,
346 g_param_spec_string ("bind-address", "Bind Address",
347 "Address to bind the socket to", DEFAULT_BIND_ADDRESS,
348 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
349 g_object_class_install_property (gobject_class, PROP_BIND_PORT,
350 g_param_spec_int ("bind-port", "Bind Port",
351 "Port to bind the socket to", 0, G_MAXUINT16,
352 DEFAULT_BIND_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
354 gst_element_class_add_pad_template (gstelement_class,
355 gst_static_pad_template_get (&sink_template));
357 gst_element_class_set_static_metadata (gstelement_class, "UDP packet sender",
359 "Send data over the network via UDP to one or multiple recipients "
360 "which can be added or removed at runtime using action signals",
361 "Wim Taymans <wim.taymans@gmail.com>");
363 gstbasesink_class->render = gst_multiudpsink_render;
364 gstbasesink_class->render_list = gst_multiudpsink_render_list;
365 gstbasesink_class->start = gst_multiudpsink_start;
366 gstbasesink_class->stop = gst_multiudpsink_stop;
367 gstbasesink_class->unlock = gst_multiudpsink_unlock;
368 gstbasesink_class->unlock_stop = gst_multiudpsink_unlock_stop;
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;
374 GST_DEBUG_CATEGORY_INIT (multiudpsink_debug, "multiudpsink", 0, "UDP sink");
378 gst_multiudpsink_init (GstMultiUDPSink * sink)
382 g_mutex_init (&sink->client_lock);
383 sink->clients = NULL;
384 sink->num_v4_unique = 0;
385 sink->num_v4_all = 0;
386 sink->num_v6_unique = 0;
387 sink->num_v6_all = 0;
389 sink->socket = DEFAULT_SOCKET;
390 sink->socket_v6 = DEFAULT_SOCKET;
391 sink->used_socket = DEFAULT_USED_SOCKET;
392 sink->used_socket_v6 = DEFAULT_USED_SOCKET;
393 sink->close_socket = DEFAULT_CLOSE_SOCKET;
394 sink->external_socket = (sink->socket != NULL);
395 sink->auto_multicast = DEFAULT_AUTO_MULTICAST;
396 sink->ttl = DEFAULT_TTL;
397 sink->ttl_mc = DEFAULT_TTL_MC;
398 sink->loop = DEFAULT_LOOP;
399 sink->force_ipv4 = DEFAULT_FORCE_IPV4;
400 sink->qos_dscp = DEFAULT_QOS_DSCP;
401 sink->send_duplicates = DEFAULT_SEND_DUPLICATES;
402 sink->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
404 sink->cancellable = g_cancellable_new ();
406 /* pre-allocate OutputVector, MapInfo and OutputMessage arrays
407 * for use in the render and render_list functions */
408 max_mem = gst_buffer_get_max_memory ();
410 sink->n_vecs = max_mem;
411 sink->vecs = g_new (GOutputVector, sink->n_vecs);
413 sink->n_maps = max_mem;
414 sink->maps = g_new (GstMapInfo, sink->n_maps);
416 sink->n_messages = 1;
417 sink->messages = g_new (GstOutputMessage, sink->n_messages);
419 /* we assume that the number of memories per buffer can fit into a guint8 */
420 g_warn_if_fail (max_mem <= G_MAXUINT8);
423 static GstUDPClient *
424 gst_udp_client_new (GstMultiUDPSink * sink, const gchar * host, gint port)
426 GstUDPClient *client;
431 addr = g_inet_address_new_from_string (host);
435 resolver = g_resolver_get_default ();
437 g_resolver_lookup_by_name (resolver, host, sink->cancellable, &err);
440 addr = G_INET_ADDRESS (g_object_ref (results->data));
442 g_resolver_free_addresses (results);
443 g_object_unref (resolver);
445 #ifndef GST_DISABLE_GST_DEBUG
447 gchar *ip = g_inet_address_to_string (addr);
449 GST_DEBUG_OBJECT (sink, "IP address for host %s is %s", host, ip);
454 client = g_slice_new0 (GstUDPClient);
455 client->ref_count = 1;
456 client->add_count = 0;
457 client->host = g_strdup (host);
459 client->addr = g_inet_socket_address_new (addr, port);
460 g_object_unref (addr);
466 g_object_unref (resolver);
472 /* call with client lock held */
474 gst_udp_client_unref (GstUDPClient * client)
476 if (--client->ref_count == 0) {
477 g_object_unref (client->addr);
478 g_free (client->host);
479 g_slice_free (GstUDPClient, client);
483 /* call with client lock held */
484 static inline GstUDPClient *
485 gst_udp_client_ref (GstUDPClient * client)
492 client_compare (GstUDPClient * a, GstUDPClient * b)
494 if ((a->port == b->port) && (strcmp (a->host, b->host) == 0))
501 gst_multiudpsink_finalize (GObject * object)
503 GstMultiUDPSink *sink;
505 sink = GST_MULTIUDPSINK (object);
507 g_list_foreach (sink->clients, (GFunc) gst_udp_client_unref, NULL);
508 g_list_free (sink->clients);
511 g_object_unref (sink->socket);
515 g_object_unref (sink->socket_v6);
516 sink->socket_v6 = NULL;
518 if (sink->used_socket)
519 g_object_unref (sink->used_socket);
520 sink->used_socket = NULL;
522 if (sink->used_socket_v6)
523 g_object_unref (sink->used_socket_v6);
524 sink->used_socket_v6 = NULL;
526 if (sink->cancellable)
527 g_object_unref (sink->cancellable);
528 sink->cancellable = NULL;
530 g_free (sink->multi_iface);
531 sink->multi_iface = NULL;
537 g_free (sink->messages);
538 sink->messages = NULL;
540 g_free (sink->bind_address);
541 sink->bind_address = NULL;
543 g_mutex_clear (&sink->client_lock);
545 G_OBJECT_CLASS (parent_class)->finalize (object);
548 /* replacement until we can depend unconditionally on the real one in GLib */
549 #ifndef HAVE_G_SOCKET_SEND_MESSAGES
550 #define g_socket_send_messages gst_socket_send_messages
553 gst_socket_send_messages (GSocket * socket, GstOutputMessage * messages,
554 guint num_messages, gint flags, GCancellable * cancellable, GError ** error)
559 for (i = 0; i < num_messages; ++i) {
560 GstOutputMessage *msg = &messages[i];
561 GError *msg_error = NULL;
563 result = g_socket_send_message (socket, msg->address,
564 msg->vectors, msg->num_vectors,
565 msg->control_messages, msg->num_control_messages,
566 flags, cancellable, &msg_error);
569 /* if we couldn't send all messages, just return how many we did
570 * manage to send, provided we managed to send at least one */
571 if (msg_error->code == G_IO_ERROR_WOULD_BLOCK && i > 0) {
572 g_error_free (msg_error);
575 g_propagate_error (error, msg_error);
580 msg->bytes_sent = result;
585 #endif /* HAVE_G_SOCKET_SEND_MESSAGES */
588 fill_vectors (GOutputVector * vecs, GstMapInfo * maps, guint n, GstBuffer * buf)
594 g_assert (gst_buffer_n_memory (buf) == n);
596 for (i = 0; i < n; ++i) {
597 mem = gst_buffer_peek_memory (buf, i);
598 if (gst_memory_map (mem, &maps[i], GST_MAP_READ)) {
599 vecs[i].buffer = maps[i].data;
600 vecs[i].size = maps[i].size;
602 GST_WARNING ("Failed to map memory %p for reading", mem);
606 size += vecs[i].size;
613 gst_udp_calc_message_size (GstOutputMessage * msg)
618 for (i = 0; i < msg->num_vectors; ++i)
619 size += msg->vectors[i].size;
625 gst_udp_messsages_find_first_not_sent (GstOutputMessage * messages,
630 for (i = 0; i < num_messages; ++i) {
631 GstOutputMessage *msg = &messages[i];
633 if (msg->bytes_sent == 0 && gst_udp_calc_message_size (msg) > 0)
640 static inline gchar *
641 gst_udp_address_get_string (GSocketAddress * addr, gchar * s, gsize size)
643 GInetSocketAddress *isa = G_INET_SOCKET_ADDRESS (addr);
647 ia = g_inet_socket_address_get_address (isa);
648 addr_str = g_inet_address_to_string (ia);
649 g_snprintf (s, size, "%s:%u", addr_str, g_inet_socket_address_get_port (isa));
656 /* Wrapper around g_socket_send_messages() plus error handling (ignoring).
657 * Returns FALSE if we got cancelled, otherwise TRUE. */
659 gst_multiudpsink_send_messages (GstMultiUDPSink * sink, GSocket * socket,
660 GstOutputMessage * messages, guint num_messages)
662 gboolean sent_max_size_warning = FALSE;
664 while (num_messages > 0) {
665 gchar astr[64] G_GNUC_UNUSED;
667 guint msg_size, skip, i;
670 ret = g_socket_send_messages (socket, messages, num_messages, 0,
671 sink->cancellable, &err);
673 if (G_UNLIKELY (ret < 0)) {
674 GstOutputMessage *msg;
676 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
677 g_clear_error (&err);
681 err_idx = gst_udp_messsages_find_first_not_sent (messages, num_messages);
685 msg = &messages[err_idx];
686 msg_size = gst_udp_calc_message_size (msg);
688 GST_LOG_OBJECT (sink, "error sending %u bytes to client %s: %s", msg_size,
689 gst_udp_address_get_string (msg->address, astr, sizeof (astr)),
693 if (msg_size > UDP_MAX_SIZE) {
694 if (!sent_max_size_warning) {
695 GST_ELEMENT_WARNING (sink, RESOURCE, WRITE,
696 ("Attempting to send a UDP packets larger than maximum size "
697 "(%u > %d)", msg_size, UDP_MAX_SIZE),
698 ("Reason: %s", err ? err->message : "unknown reason"));
699 sent_max_size_warning = FALSE;
702 GST_ELEMENT_WARNING (sink, RESOURCE, WRITE,
703 ("Error sending UDP packets"), ("client %s, reason: %s",
704 gst_udp_address_get_string (msg->address, astr, sizeof (astr)),
705 (err != NULL) ? err->message : "unknown reason"));
707 for (i = err_idx + 1; i < num_messages; ++i, ++skip) {
708 if (messages[i].address != msg->address)
711 GST_DEBUG_OBJECT (sink, "skipping %d message(s) to same client", skip);
714 /* ignore any errors and try sending the rest */
715 g_clear_error (&err);
719 g_assert (ret <= num_messages);
729 gst_multiudpsink_render_buffers (GstMultiUDPSink * sink, GstBuffer ** buffers,
730 guint num_buffers, guint8 * mem_nums, guint total_mem_num)
732 GstOutputMessage *msgs;
733 gboolean send_duplicates;
734 GstUDPClient **clients;
736 GstMapInfo *map_infos;
737 GstFlowReturn flow_ret;
738 guint num_addr_v4, num_addr_v6;
739 guint num_addr, num_msgs;
745 send_duplicates = sink->send_duplicates;
747 g_mutex_lock (&sink->client_lock);
749 if (send_duplicates) {
750 num_addr_v4 = sink->num_v4_all;
751 num_addr_v6 = sink->num_v6_all;
753 num_addr_v4 = sink->num_v4_unique;
754 num_addr_v6 = sink->num_v6_unique;
756 num_addr = num_addr_v4 + num_addr_v6;
761 clients = g_newa (GstUDPClient *, num_addr);
762 for (l = sink->clients, i = 0; l != NULL; l = l->next) {
763 GstUDPClient *client = l->data;
765 clients[i++] = gst_udp_client_ref (client);
766 for (j = 1; send_duplicates && j < client->add_count; ++j)
767 clients[i++] = gst_udp_client_ref (client);
769 g_assert_cmpuint (i, ==, num_addr);
771 g_mutex_unlock (&sink->client_lock);
773 GST_LOG_OBJECT (sink, "%u buffers, %u memories -> to be sent to %u clients",
774 num_buffers, total_mem_num, num_addr);
776 /* ensure our pre-allocated scratch space arrays are large enough */
777 if (sink->n_vecs < total_mem_num) {
778 sink->n_vecs = GST_ROUND_UP_16 (total_mem_num);
780 sink->vecs = g_new (GOutputVector, sink->n_vecs);
784 if (sink->n_maps < total_mem_num) {
785 sink->n_maps = GST_ROUND_UP_16 (total_mem_num);
787 sink->maps = g_new (GstMapInfo, sink->n_maps);
789 map_infos = sink->maps;
791 num_msgs = num_addr * num_buffers;
792 if (sink->n_messages < num_msgs) {
793 sink->n_messages = GST_ROUND_UP_16 (num_msgs);
794 g_free (sink->messages);
795 sink->messages = g_new (GstOutputMessage, sink->n_messages);
797 msgs = sink->messages;
799 /* populate first num_buffers messages with output vectors for the buffers */
800 for (i = 0, mem = 0; i < num_buffers; ++i) {
801 size += fill_vectors (&vecs[mem], &map_infos[mem], mem_nums[i], buffers[i]);
802 msgs[i].vectors = &vecs[mem];
803 msgs[i].num_vectors = mem_nums[i];
804 msgs[i].num_control_messages = 0;
805 msgs[i].control_messages = NULL;
806 msgs[i].address = clients[0]->addr;
810 /* FIXME: how about some locking? (there wasn't any before either, but..) */
811 sink->bytes_to_serve += size;
813 /* now copy the pre-filled num_buffer messages over to the next num_buffer
814 * messages for the next client, where we also change the target adddress */
815 for (i = 1; i < num_addr; ++i) {
816 for (j = 0; j < num_buffers; ++j) {
817 msgs[i * num_buffers + j] = msgs[j];
818 msgs[i * num_buffers + j].address = clients[i]->addr;
826 /* no IPv4 socket? Send it all from the IPv6 socket then.. */
827 if (sink->used_socket == NULL) {
828 ret = gst_multiudpsink_send_messages (sink, sink->used_socket_v6,
831 guint num_msgs_v4 = num_buffers * num_addr_v4;
832 guint num_msgs_v6 = num_buffers * num_addr_v6;
834 /* our client list is sorted with IPv4 clients first and IPv6 ones last */
835 ret = gst_multiudpsink_send_messages (sink, sink->used_socket,
841 ret = gst_multiudpsink_send_messages (sink, sink->used_socket_v6,
842 msgs + num_msgs_v4, num_msgs_v6);
849 flow_ret = GST_FLOW_OK;
851 /* now update stats */
852 g_mutex_lock (&sink->client_lock);
854 for (i = 0; i < num_addr; ++i) {
855 GstUDPClient *client = clients[i];
857 for (j = 0; j < num_buffers; ++j) {
860 bytes_sent = msgs[i * num_buffers + j].bytes_sent;
862 client->bytes_sent += bytes_sent;
863 client->packets_sent++;
864 sink->bytes_served += bytes_sent;
866 gst_udp_client_unref (client);
869 g_mutex_unlock (&sink->client_lock);
873 for (i = 0; i < mem; ++i)
874 gst_memory_unmap (map_infos[i].memory, &map_infos[i]);
880 g_mutex_unlock (&sink->client_lock);
881 GST_LOG_OBJECT (sink, "no clients");
886 GST_INFO_OBJECT (sink, "cancelled");
887 g_clear_error (&err);
888 flow_ret = GST_FLOW_FLUSHING;
890 g_mutex_lock (&sink->client_lock);
891 for (i = 0; i < num_addr; ++i)
892 gst_udp_client_unref (clients[i]);
893 g_mutex_unlock (&sink->client_lock);
899 gst_multiudpsink_render_list (GstBaseSink * bsink, GstBufferList * buffer_list)
901 GstMultiUDPSink *sink;
906 guint i, num_buffers;
908 sink = GST_MULTIUDPSINK_CAST (bsink);
910 num_buffers = gst_buffer_list_length (buffer_list);
911 if (num_buffers == 0)
914 buffers = g_newa (GstBuffer *, num_buffers);
915 mem_nums = g_newa (guint8, num_buffers);
916 for (i = 0, total_mems = 0; i < num_buffers; ++i) {
917 buffers[i] = gst_buffer_list_get (buffer_list, i);
918 mem_nums[i] = gst_buffer_n_memory (buffers[i]);
919 total_mems += mem_nums[i];
922 flow = gst_multiudpsink_render_buffers (sink, buffers, num_buffers,
923 mem_nums, total_mems);
929 GST_LOG_OBJECT (sink, "empty buffer");
935 gst_multiudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
937 GstMultiUDPSink *sink;
941 sink = GST_MULTIUDPSINK_CAST (bsink);
943 n_mem = gst_buffer_n_memory (buffer);
946 flow = gst_multiudpsink_render_buffers (sink, &buffer, 1, &n_mem, n_mem);
954 gst_multiudpsink_set_clients_string (GstMultiUDPSink * sink,
955 const gchar * string)
960 clients = g_strsplit (string, ",", 0);
962 g_mutex_lock (&sink->client_lock);
963 /* clear all existing clients */
964 gst_multiudpsink_clear_internal (sink, FALSE);
965 for (i = 0; clients[i]; i++) {
970 p = strstr (clients[i], ":");
973 port = g_ascii_strtoll (p + 1, NULL, 10);
976 gst_multiudpsink_add_internal (sink, host, port, FALSE);
978 g_mutex_unlock (&sink->client_lock);
980 g_strfreev (clients);
984 gst_multiudpsink_get_clients_string (GstMultiUDPSink * sink)
989 str = g_string_new ("");
991 g_mutex_lock (&sink->client_lock);
992 clients = sink->clients;
994 GstUDPClient *client;
997 client = (GstUDPClient *) clients->data;
999 clients = g_list_next (clients);
1001 count = client->add_count;
1003 g_string_append_printf (str, "%s:%d%s", client->host, client->port,
1004 (clients || count > 1 ? "," : ""));
1007 g_mutex_unlock (&sink->client_lock);
1009 return g_string_free (str, FALSE);
1013 gst_multiudpsink_setup_qos_dscp (GstMultiUDPSink * sink, GSocket * socket)
1015 /* don't touch on -1 */
1016 if (sink->qos_dscp < 0)
1027 fd = g_socket_get_fd (socket);
1029 GST_DEBUG_OBJECT (sink, "setting TOS to %d", sink->qos_dscp);
1031 /* Extract and shift 6 bits of DSFIELD */
1032 tos = (sink->qos_dscp & 0x3f) << 2;
1034 if (setsockopt (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0) {
1035 GST_ERROR_OBJECT (sink, "could not set TOS: %s", g_strerror (errno));
1038 if (setsockopt (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0) {
1039 GST_ERROR_OBJECT (sink, "could not set TCLASS: %s", g_strerror (errno));
1047 gst_multiudpsink_set_property (GObject * object, guint prop_id,
1048 const GValue * value, GParamSpec * pspec)
1050 GstMultiUDPSink *udpsink;
1052 udpsink = GST_MULTIUDPSINK (object);
1056 if (udpsink->socket != NULL && udpsink->socket != udpsink->used_socket &&
1057 udpsink->close_socket) {
1060 if (!g_socket_close (udpsink->socket, &err)) {
1061 GST_ERROR ("failed to close socket %p: %s", udpsink->socket,
1063 g_clear_error (&err);
1066 if (udpsink->socket)
1067 g_object_unref (udpsink->socket);
1068 udpsink->socket = g_value_dup_object (value);
1069 GST_DEBUG_OBJECT (udpsink, "setting socket to %p", udpsink->socket);
1071 case PROP_SOCKET_V6:
1072 if (udpsink->socket_v6 != NULL
1073 && udpsink->socket_v6 != udpsink->used_socket_v6
1074 && udpsink->close_socket) {
1077 if (!g_socket_close (udpsink->socket_v6, &err)) {
1078 GST_ERROR ("failed to close socket %p: %s", udpsink->socket_v6,
1080 g_clear_error (&err);
1083 if (udpsink->socket_v6)
1084 g_object_unref (udpsink->socket_v6);
1085 udpsink->socket_v6 = g_value_dup_object (value);
1086 GST_DEBUG_OBJECT (udpsink, "setting socket to %p", udpsink->socket_v6);
1088 case PROP_CLOSE_SOCKET:
1089 udpsink->close_socket = g_value_get_boolean (value);
1092 gst_multiudpsink_set_clients_string (udpsink, g_value_get_string (value));
1094 case PROP_AUTO_MULTICAST:
1095 udpsink->auto_multicast = g_value_get_boolean (value);
1097 case PROP_MULTICAST_IFACE:
1098 g_free (udpsink->multi_iface);
1100 if (g_value_get_string (value) == NULL)
1101 udpsink->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1103 udpsink->multi_iface = g_value_dup_string (value);
1106 udpsink->ttl = g_value_get_int (value);
1109 udpsink->ttl_mc = g_value_get_int (value);
1112 udpsink->loop = g_value_get_boolean (value);
1114 case PROP_FORCE_IPV4:
1115 udpsink->force_ipv4 = g_value_get_boolean (value);
1118 udpsink->qos_dscp = g_value_get_int (value);
1119 gst_multiudpsink_setup_qos_dscp (udpsink, udpsink->used_socket);
1120 gst_multiudpsink_setup_qos_dscp (udpsink, udpsink->used_socket_v6);
1122 case PROP_SEND_DUPLICATES:
1123 udpsink->send_duplicates = g_value_get_boolean (value);
1125 case PROP_BUFFER_SIZE:
1126 udpsink->buffer_size = g_value_get_int (value);
1128 case PROP_BIND_ADDRESS:
1129 g_free (udpsink->bind_address);
1130 udpsink->bind_address = g_value_dup_string (value);
1132 case PROP_BIND_PORT:
1133 udpsink->bind_port = g_value_get_int (value);
1136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1142 gst_multiudpsink_get_property (GObject * object, guint prop_id, GValue * value,
1145 GstMultiUDPSink *udpsink;
1147 udpsink = GST_MULTIUDPSINK (object);
1150 case PROP_BYTES_TO_SERVE:
1151 g_value_set_uint64 (value, udpsink->bytes_to_serve);
1153 case PROP_BYTES_SERVED:
1154 g_value_set_uint64 (value, udpsink->bytes_served);
1157 g_value_set_object (value, udpsink->socket);
1159 case PROP_SOCKET_V6:
1160 g_value_set_object (value, udpsink->socket_v6);
1162 case PROP_CLOSE_SOCKET:
1163 g_value_set_boolean (value, udpsink->close_socket);
1165 case PROP_USED_SOCKET:
1166 g_value_set_object (value, udpsink->used_socket);
1168 case PROP_USED_SOCKET_V6:
1169 g_value_set_object (value, udpsink->used_socket_v6);
1172 g_value_take_string (value,
1173 gst_multiudpsink_get_clients_string (udpsink));
1175 case PROP_AUTO_MULTICAST:
1176 g_value_set_boolean (value, udpsink->auto_multicast);
1178 case PROP_MULTICAST_IFACE:
1179 g_value_set_string (value, udpsink->multi_iface);
1182 g_value_set_int (value, udpsink->ttl);
1185 g_value_set_int (value, udpsink->ttl_mc);
1188 g_value_set_boolean (value, udpsink->loop);
1190 case PROP_FORCE_IPV4:
1191 g_value_set_boolean (value, udpsink->force_ipv4);
1194 g_value_set_int (value, udpsink->qos_dscp);
1196 case PROP_SEND_DUPLICATES:
1197 g_value_set_boolean (value, udpsink->send_duplicates);
1199 case PROP_BUFFER_SIZE:
1200 g_value_set_int (value, udpsink->buffer_size);
1202 case PROP_BIND_ADDRESS:
1203 g_value_set_string (value, udpsink->bind_address);
1205 case PROP_BIND_PORT:
1206 g_value_set_int (value, udpsink->bind_port);
1209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1215 gst_multiudpsink_configure_client (GstMultiUDPSink * sink,
1216 GstUDPClient * client)
1218 GInetSocketAddress *saddr = G_INET_SOCKET_ADDRESS (client->addr);
1219 GInetAddress *addr = g_inet_socket_address_get_address (saddr);
1220 GSocketFamily family = g_socket_address_get_family (G_SOCKET_ADDRESS (saddr));
1224 GST_DEBUG_OBJECT (sink, "configuring client %p", client);
1226 if (family == G_SOCKET_FAMILY_IPV6 && !sink->used_socket_v6)
1227 goto invalid_family;
1229 /* Select socket to send from for this address */
1230 if (family == G_SOCKET_FAMILY_IPV6 || !sink->used_socket)
1231 socket = sink->used_socket_v6;
1233 socket = sink->used_socket;
1235 if (g_inet_address_get_is_multicast (addr)) {
1236 GST_DEBUG_OBJECT (sink, "we have a multicast client %p", client);
1237 if (sink->auto_multicast) {
1238 GST_DEBUG_OBJECT (sink, "autojoining group");
1239 if (!g_socket_join_multicast_group (socket, addr, FALSE,
1240 sink->multi_iface, &err))
1241 goto join_group_failed;
1243 GST_DEBUG_OBJECT (sink, "setting loop to %d", sink->loop);
1244 g_socket_set_multicast_loopback (socket, sink->loop);
1245 GST_DEBUG_OBJECT (sink, "setting ttl to %d", sink->ttl_mc);
1246 g_socket_set_multicast_ttl (socket, sink->ttl_mc);
1248 GST_DEBUG_OBJECT (sink, "setting unicast ttl to %d", sink->ttl);
1249 g_socket_set_ttl (socket, sink->ttl);
1256 gst_multiudpsink_stop (GST_BASE_SINK (sink));
1257 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
1258 ("Could not join multicast group: %s",
1259 err ? err->message : "unknown reason"));
1260 g_clear_error (&err);
1265 gst_multiudpsink_stop (GST_BASE_SINK (sink));
1266 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
1267 ("Invalid address family (got %d)", family));
1272 /* create a socket for sending to remote machine */
1274 gst_multiudpsink_start (GstBaseSink * bsink)
1276 GstMultiUDPSink *sink;
1278 GstUDPClient *client;
1281 sink = GST_MULTIUDPSINK (bsink);
1283 sink->external_socket = FALSE;
1286 GST_DEBUG_OBJECT (sink, "using configured socket");
1287 if (g_socket_get_family (sink->socket) == G_SOCKET_FAMILY_IPV6) {
1288 sink->used_socket_v6 = G_SOCKET (g_object_ref (sink->socket));
1289 sink->external_socket = TRUE;
1291 sink->used_socket = G_SOCKET (g_object_ref (sink->socket));
1292 sink->external_socket = TRUE;
1296 if (sink->socket_v6) {
1297 GST_DEBUG_OBJECT (sink, "using configured IPv6 socket");
1298 g_return_val_if_fail (g_socket_get_family (sink->socket) !=
1299 G_SOCKET_FAMILY_IPV6, FALSE);
1301 if (sink->used_socket_v6 && sink->used_socket_v6 != sink->socket_v6) {
1302 GST_ERROR_OBJECT (sink,
1303 "Provided different IPv6 sockets in socket and socket-v6 properties");
1307 sink->used_socket_v6 = G_SOCKET (g_object_ref (sink->socket_v6));
1308 sink->external_socket = TRUE;
1311 if (!sink->used_socket && !sink->used_socket_v6) {
1312 GSocketAddress *bind_addr;
1313 GInetAddress *bind_iaddr;
1315 if (sink->bind_address) {
1316 GSocketFamily family;
1318 bind_iaddr = g_inet_address_new_from_string (sink->bind_address);
1321 GResolver *resolver;
1323 resolver = g_resolver_get_default ();
1325 g_resolver_lookup_by_name (resolver, sink->bind_address,
1326 sink->cancellable, &err);
1328 g_object_unref (resolver);
1331 bind_iaddr = G_INET_ADDRESS (g_object_ref (results->data));
1332 g_resolver_free_addresses (results);
1333 g_object_unref (resolver);
1336 bind_addr = g_inet_socket_address_new (bind_iaddr, sink->bind_port);
1337 g_object_unref (bind_iaddr);
1338 family = g_socket_address_get_family (G_SOCKET_ADDRESS (bind_addr));
1340 if ((sink->used_socket =
1341 g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1342 G_SOCKET_PROTOCOL_UDP, &err)) == NULL) {
1343 g_object_unref (bind_addr);
1347 g_socket_bind (sink->used_socket, bind_addr, TRUE, &err);
1351 /* create sender sockets if none available */
1352 if ((sink->used_socket = g_socket_new (G_SOCKET_FAMILY_IPV4,
1353 G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &err)) == NULL)
1356 bind_iaddr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
1357 bind_addr = g_inet_socket_address_new (bind_iaddr, sink->bind_port);
1358 g_socket_bind (sink->used_socket, bind_addr, TRUE, &err);
1359 g_object_unref (bind_addr);
1360 g_object_unref (bind_iaddr);
1364 if ((sink->used_socket_v6 = g_socket_new (G_SOCKET_FAMILY_IPV6,
1365 G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP,
1367 GST_INFO_OBJECT (sink, "Failed to create IPv6 socket: %s",
1369 g_clear_error (&err);
1371 bind_iaddr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV6);
1372 bind_addr = g_inet_socket_address_new (bind_iaddr, sink->bind_port);
1373 g_socket_bind (sink->used_socket_v6, bind_addr, TRUE, &err);
1374 g_object_unref (bind_addr);
1375 g_object_unref (bind_iaddr);
1386 len = sizeof (sndsize);
1387 if (sink->buffer_size != 0) {
1388 sndsize = sink->buffer_size;
1390 GST_DEBUG_OBJECT (sink, "setting udp buffer of %d bytes", sndsize);
1391 /* set buffer size, Note that on Linux this is typically limited to a
1392 * maximum of around 100K. Also a minimum of 128 bytes is required on
1395 if (sink->used_socket) {
1397 setsockopt (g_socket_get_fd (sink->used_socket), SOL_SOCKET,
1398 SO_SNDBUF, (void *) &sndsize, len);
1400 GST_ELEMENT_WARNING (sink, RESOURCE, SETTINGS, (NULL),
1401 ("Could not create a buffer of requested %d bytes, %d: %s",
1402 sndsize, ret, g_strerror (errno)));
1406 if (sink->used_socket_v6) {
1408 setsockopt (g_socket_get_fd (sink->used_socket_v6), SOL_SOCKET,
1409 SO_SNDBUF, (void *) &sndsize, len);
1411 GST_ELEMENT_WARNING (sink, RESOURCE, SETTINGS, (NULL),
1412 ("Could not create a buffer of requested %d bytes, %d: %s",
1413 sndsize, ret, g_strerror (errno)));
1418 /* read the value of the receive buffer. Note that on linux this returns 2x the
1419 * value we set because the kernel allocates extra memory for metadata.
1420 * The default on Linux is about 100K (which is about 50K without metadata) */
1421 if (sink->used_socket) {
1423 getsockopt (g_socket_get_fd (sink->used_socket), SOL_SOCKET,
1424 SO_SNDBUF, (void *) &sndsize, &len);
1426 GST_DEBUG_OBJECT (sink, "have UDP buffer of %d bytes", sndsize);
1428 GST_DEBUG_OBJECT (sink, "could not get UDP buffer size");
1431 if (sink->used_socket_v6) {
1433 getsockopt (g_socket_get_fd (sink->used_socket_v6), SOL_SOCKET,
1434 SO_SNDBUF, (void *) &sndsize, &len);
1436 GST_DEBUG_OBJECT (sink, "have UDPv6 buffer of %d bytes", sndsize);
1438 GST_DEBUG_OBJECT (sink, "could not get UDPv6 buffer size");
1443 #ifdef SO_BINDTODEVICE
1444 if (sink->multi_iface) {
1445 if (sink->used_socket) {
1446 if (setsockopt (g_socket_get_fd (sink->used_socket), SOL_SOCKET,
1447 SO_BINDTODEVICE, sink->multi_iface,
1448 strlen (sink->multi_iface)) < 0)
1449 GST_WARNING_OBJECT (sink, "setsockopt SO_BINDTODEVICE failed: %s",
1452 if (sink->used_socket_v6) {
1453 if (setsockopt (g_socket_get_fd (sink->used_socket_v6), SOL_SOCKET,
1454 SO_BINDTODEVICE, sink->multi_iface,
1455 strlen (sink->multi_iface)) < 0)
1456 GST_WARNING_OBJECT (sink, "setsockopt SO_BINDTODEVICE failed (v6): %s",
1462 if (sink->used_socket)
1463 g_socket_set_broadcast (sink->used_socket, TRUE);
1464 if (sink->used_socket_v6)
1465 g_socket_set_broadcast (sink->used_socket_v6, TRUE);
1467 sink->bytes_to_serve = 0;
1468 sink->bytes_served = 0;
1470 gst_multiudpsink_setup_qos_dscp (sink, sink->used_socket);
1471 gst_multiudpsink_setup_qos_dscp (sink, sink->used_socket_v6);
1473 /* look for multicast clients and join multicast groups appropriately
1474 set also ttl and multicast loopback delivery appropriately */
1475 for (clients = sink->clients; clients; clients = g_list_next (clients)) {
1476 client = (GstUDPClient *) clients->data;
1478 if (!gst_multiudpsink_configure_client (sink, client))
1486 GST_ELEMENT_ERROR (sink, RESOURCE, FAILED, (NULL),
1487 ("Could not create socket: %s", err->message));
1488 g_clear_error (&err);
1493 GST_ELEMENT_ERROR (sink, RESOURCE, FAILED, (NULL),
1494 ("Failed to bind socket: %s", err->message));
1495 g_clear_error (&err);
1500 GST_ELEMENT_ERROR (sink, RESOURCE, FAILED, (NULL),
1501 ("Failed to resolve bind address %s: %s", sink->bind_address,
1503 g_clear_error (&err);
1509 gst_multiudpsink_stop (GstBaseSink * bsink)
1511 GstMultiUDPSink *udpsink;
1513 udpsink = GST_MULTIUDPSINK (bsink);
1515 if (udpsink->used_socket) {
1516 if (udpsink->close_socket || !udpsink->external_socket) {
1519 if (!g_socket_close (udpsink->used_socket, &err)) {
1520 GST_ERROR_OBJECT (udpsink, "Failed to close socket: %s", err->message);
1521 g_clear_error (&err);
1525 g_object_unref (udpsink->used_socket);
1526 udpsink->used_socket = NULL;
1529 if (udpsink->used_socket_v6) {
1530 if (udpsink->close_socket || !udpsink->external_socket) {
1533 if (!g_socket_close (udpsink->used_socket_v6, &err)) {
1534 GST_ERROR_OBJECT (udpsink, "Failed to close socket: %s", err->message);
1535 g_clear_error (&err);
1539 g_object_unref (udpsink->used_socket_v6);
1540 udpsink->used_socket_v6 = NULL;
1547 gst_udp_client_compare_socket_family (GstUDPClient * a, GstUDPClient * b)
1549 GSocketFamily fa = g_socket_address_get_family (a->addr);
1550 GSocketFamily fb = g_socket_address_get_family (b->addr);
1555 /* a should go before b */
1556 if (fa == G_SOCKET_FAMILY_IPV4 && fb == G_SOCKET_FAMILY_IPV6)
1559 /* b should go before a */
1564 gst_multiudpsink_add_internal (GstMultiUDPSink * sink, const gchar * host,
1565 gint port, gboolean lock)
1567 GSocketFamily family;
1568 GstUDPClient *client;
1569 GstUDPClient udpclient;
1573 udpclient.host = (gchar *) host;
1574 udpclient.port = port;
1576 GST_DEBUG_OBJECT (sink, "adding client on host %s, port %d", host, port);
1579 g_mutex_lock (&sink->client_lock);
1581 find = g_list_find_custom (sink->clients, &udpclient,
1582 (GCompareFunc) client_compare);
1585 find = g_list_find_custom (sink->clients_to_be_removed, &udpclient,
1586 (GCompareFunc) client_compare);
1588 gst_udp_client_ref (find->data);
1592 client = (GstUDPClient *) find->data;
1594 family = g_socket_address_get_family (client->addr);
1596 GST_DEBUG_OBJECT (sink, "found %d existing clients with host %s, port %d",
1597 client->add_count, host, port);
1599 client = gst_udp_client_new (sink, host, port);
1603 family = g_socket_address_get_family (client->addr);
1605 g_get_current_time (&now);
1606 client->connect_time = GST_TIMEVAL_TO_TIME (now);
1608 if (sink->used_socket)
1609 gst_multiudpsink_configure_client (sink, client);
1611 GST_DEBUG_OBJECT (sink, "add client with host %s, port %d", host, port);
1613 /* keep IPv4 clients at the beginning, and IPv6 at the end, we can make
1614 * use of this in gst_multiudpsink_render_buffers() */
1615 sink->clients = g_list_insert_sorted (sink->clients, client,
1616 (GCompareFunc) gst_udp_client_compare_socket_family);
1618 if (family == G_SOCKET_FAMILY_IPV4)
1619 ++sink->num_v4_unique;
1621 ++sink->num_v6_unique;
1624 ++client->add_count;
1626 if (family == G_SOCKET_FAMILY_IPV4)
1632 g_mutex_unlock (&sink->client_lock);
1634 g_signal_emit (G_OBJECT (sink),
1635 gst_multiudpsink_signals[SIGNAL_CLIENT_ADDED], 0, host, port);
1637 GST_DEBUG_OBJECT (sink, "added client on host %s, port %d", host, port);
1643 GST_DEBUG_OBJECT (sink, "did not add client on host %s, port %d", host,
1646 g_mutex_unlock (&sink->client_lock);
1652 gst_multiudpsink_add (GstMultiUDPSink * sink, const gchar * host, gint port)
1654 gst_multiudpsink_add_internal (sink, host, port, TRUE);
1658 gst_multiudpsink_remove (GstMultiUDPSink * sink, const gchar * host, gint port)
1660 GSocketFamily family;
1662 GstUDPClient udpclient;
1663 GstUDPClient *client;
1666 udpclient.host = (gchar *) host;
1667 udpclient.port = port;
1669 g_mutex_lock (&sink->client_lock);
1670 find = g_list_find_custom (sink->clients, &udpclient,
1671 (GCompareFunc) client_compare);
1675 client = (GstUDPClient *) find->data;
1677 GST_DEBUG_OBJECT (sink, "found %d clients with host %s, port %d",
1678 client->add_count, host, port);
1680 --client->add_count;
1682 family = g_socket_address_get_family (client->addr);
1683 if (family == G_SOCKET_FAMILY_IPV4)
1688 if (client->add_count == 0) {
1689 GInetSocketAddress *saddr = G_INET_SOCKET_ADDRESS (client->addr);
1690 GInetAddress *addr = g_inet_socket_address_get_address (saddr);
1693 /* Select socket to send from for this address */
1694 if (family == G_SOCKET_FAMILY_IPV6 || !sink->used_socket)
1695 socket = sink->used_socket_v6;
1697 socket = sink->used_socket;
1699 GST_DEBUG_OBJECT (sink, "remove client with host %s, port %d", host, port);
1701 g_get_current_time (&now);
1702 client->disconnect_time = GST_TIMEVAL_TO_TIME (now);
1704 if (socket && sink->auto_multicast
1705 && g_inet_address_get_is_multicast (addr)) {
1708 if (!g_socket_leave_multicast_group (socket, addr, FALSE,
1709 sink->multi_iface, &err)) {
1710 GST_DEBUG_OBJECT (sink, "Failed to leave multicast group: %s",
1712 g_clear_error (&err);
1716 if (family == G_SOCKET_FAMILY_IPV4)
1717 --sink->num_v4_unique;
1719 --sink->num_v6_unique;
1721 /* Keep state consistent for streaming thread, so remove from client list,
1722 * but keep it around until after the signal has been emitted, in case a
1723 * callback wants to get stats for that client or so */
1724 sink->clients = g_list_delete_link (sink->clients, find);
1726 sink->clients_to_be_removed =
1727 g_list_prepend (sink->clients_to_be_removed, client);
1729 /* Unlock to emit signal before we delete the actual client */
1730 g_mutex_unlock (&sink->client_lock);
1731 g_signal_emit (G_OBJECT (sink),
1732 gst_multiudpsink_signals[SIGNAL_CLIENT_REMOVED], 0, host, port);
1733 g_mutex_lock (&sink->client_lock);
1735 sink->clients_to_be_removed =
1736 g_list_remove (sink->clients_to_be_removed, client);
1738 gst_udp_client_unref (client);
1740 g_mutex_unlock (&sink->client_lock);
1747 g_mutex_unlock (&sink->client_lock);
1748 GST_WARNING_OBJECT (sink, "client at host %s, port %d not found",
1755 gst_multiudpsink_clear_internal (GstMultiUDPSink * sink, gboolean lock)
1757 GST_DEBUG_OBJECT (sink, "clearing");
1758 /* we only need to remove the client structure, there is no additional
1759 * socket or anything to free for UDP */
1761 g_mutex_lock (&sink->client_lock);
1762 g_list_foreach (sink->clients, (GFunc) gst_udp_client_unref, sink);
1763 g_list_free (sink->clients);
1764 sink->clients = NULL;
1765 sink->num_v4_unique = 0;
1766 sink->num_v4_all = 0;
1767 sink->num_v6_unique = 0;
1768 sink->num_v6_all = 0;
1770 g_mutex_unlock (&sink->client_lock);
1774 gst_multiudpsink_clear (GstMultiUDPSink * sink)
1776 gst_multiudpsink_clear_internal (sink, TRUE);
1780 gst_multiudpsink_get_stats (GstMultiUDPSink * sink, const gchar * host,
1783 GstUDPClient *client;
1784 GstStructure *result = NULL;
1785 GstUDPClient udpclient;
1788 udpclient.host = (gchar *) host;
1789 udpclient.port = port;
1791 g_mutex_lock (&sink->client_lock);
1793 find = g_list_find_custom (sink->clients, &udpclient,
1794 (GCompareFunc) client_compare);
1797 find = g_list_find_custom (sink->clients_to_be_removed, &udpclient,
1798 (GCompareFunc) client_compare);
1803 GST_DEBUG_OBJECT (sink, "stats for client with host %s, port %d", host, port);
1805 client = (GstUDPClient *) find->data;
1807 result = gst_structure_new_empty ("multiudpsink-stats");
1809 gst_structure_set (result,
1810 "bytes-sent", G_TYPE_UINT64, client->bytes_sent,
1811 "packets-sent", G_TYPE_UINT64, client->packets_sent,
1812 "connect-time", G_TYPE_UINT64, client->connect_time,
1813 "disconnect-time", G_TYPE_UINT64, client->disconnect_time, NULL);
1815 g_mutex_unlock (&sink->client_lock);
1822 g_mutex_unlock (&sink->client_lock);
1823 GST_WARNING_OBJECT (sink, "client with host %s, port %d not found",
1825 /* Apparently (see comment in gstmultifdsink.c) returning NULL from here may
1826 * confuse/break python bindings */
1827 return gst_structure_new_empty ("multiudpsink-stats");
1832 gst_multiudpsink_unlock (GstBaseSink * bsink)
1834 GstMultiUDPSink *sink;
1836 sink = GST_MULTIUDPSINK (bsink);
1838 g_cancellable_cancel (sink->cancellable);
1844 gst_multiudpsink_unlock_stop (GstBaseSink * bsink)
1846 GstMultiUDPSink *sink;
1848 sink = GST_MULTIUDPSINK (bsink);
1850 g_cancellable_reset (sink->cancellable);