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.
32 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
33 * with newer GLib versions (>= 2.31.0) */
34 #define GLIB_DISABLE_DEPRECATION_WARNINGS
39 #include "gstmultiudpsink.h"
42 #ifdef HAVE_SYS_SOCKET_H
43 #include <sys/socket.h>
47 #include <netinet/in.h>
50 #include "gst/glib-compat-private.h"
52 GST_DEBUG_CATEGORY_STATIC (multiudpsink_debug);
53 #define GST_CAT_DEFAULT (multiudpsink_debug)
55 #define UDP_MAX_SIZE 65507
57 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
62 /* MultiUDPSink signals and args */
73 SIGNAL_CLIENT_REMOVED,
79 #define DEFAULT_SOCKET NULL
80 #define DEFAULT_CLOSE_SOCKET TRUE
81 #define DEFAULT_USED_SOCKET NULL
82 #define DEFAULT_CLIENTS NULL
83 /* FIXME, this should be disabled by default, we don't need to join a multicast
84 * group for sending, if this socket is also used for receiving, it should
85 * be configured in the element that does the receive. */
86 #define DEFAULT_AUTO_MULTICAST TRUE
87 #define DEFAULT_MULTICAST_IFACE NULL
88 #define DEFAULT_TTL 64
89 #define DEFAULT_TTL_MC 1
90 #define DEFAULT_LOOP TRUE
91 #define DEFAULT_FORCE_IPV4 FALSE
92 #define DEFAULT_QOS_DSCP -1
93 #define DEFAULT_SEND_DUPLICATES TRUE
94 #define DEFAULT_BUFFER_SIZE 0
106 PROP_MULTICAST_IFACE,
112 PROP_SEND_DUPLICATES,
117 static void gst_multiudpsink_finalize (GObject * object);
119 static GstFlowReturn gst_multiudpsink_render (GstBaseSink * sink,
122 static gboolean gst_multiudpsink_start (GstBaseSink * bsink);
123 static gboolean gst_multiudpsink_stop (GstBaseSink * bsink);
124 static gboolean gst_multiudpsink_unlock (GstBaseSink * bsink);
125 static gboolean gst_multiudpsink_unlock_stop (GstBaseSink * bsink);
127 static void gst_multiudpsink_set_property (GObject * object, guint prop_id,
128 const GValue * value, GParamSpec * pspec);
129 static void gst_multiudpsink_get_property (GObject * object, guint prop_id,
130 GValue * value, GParamSpec * pspec);
132 static void gst_multiudpsink_add_internal (GstMultiUDPSink * sink,
133 const gchar * host, gint port, gboolean lock);
134 static void gst_multiudpsink_clear_internal (GstMultiUDPSink * sink,
137 static guint gst_multiudpsink_signals[LAST_SIGNAL] = { 0 };
139 #define gst_multiudpsink_parent_class parent_class
140 G_DEFINE_TYPE (GstMultiUDPSink, gst_multiudpsink, GST_TYPE_BASE_SINK);
143 gst_multiudpsink_class_init (GstMultiUDPSinkClass * klass)
145 GObjectClass *gobject_class;
146 GstElementClass *gstelement_class;
147 GstBaseSinkClass *gstbasesink_class;
149 gobject_class = (GObjectClass *) klass;
150 gstelement_class = (GstElementClass *) klass;
151 gstbasesink_class = (GstBaseSinkClass *) klass;
153 gobject_class->set_property = gst_multiudpsink_set_property;
154 gobject_class->get_property = gst_multiudpsink_get_property;
155 gobject_class->finalize = gst_multiudpsink_finalize;
158 * GstMultiUDPSink::add:
159 * @gstmultiudpsink: the sink on which the signal is emitted
160 * @host: the hostname/IP address of the client to add
161 * @port: the port of the client to add
163 * Add a client with destination @host and @port to the list of
164 * clients. When the same host/port pair is added multiple times, the
165 * send-duplicates property defines if the packets are sent multiple times to
166 * the same host/port pair or not.
168 * When a host/port pair is added multiple times, an equal amount of remove
169 * calls must be performed to actually remove the host/port pair from the list
172 gst_multiudpsink_signals[SIGNAL_ADD] =
173 g_signal_new ("add", G_TYPE_FROM_CLASS (klass),
174 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
175 G_STRUCT_OFFSET (GstMultiUDPSinkClass, add),
176 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
177 G_TYPE_STRING, G_TYPE_INT);
179 * GstMultiUDPSink::remove:
180 * @gstmultiudpsink: the sink on which the signal is emitted
181 * @host: the hostname/IP address of the client to remove
182 * @port: the port of the client to remove
184 * Remove the client with destination @host and @port from the list of
187 gst_multiudpsink_signals[SIGNAL_REMOVE] =
188 g_signal_new ("remove", G_TYPE_FROM_CLASS (klass),
189 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
190 G_STRUCT_OFFSET (GstMultiUDPSinkClass, remove),
191 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
192 G_TYPE_STRING, G_TYPE_INT);
194 * GstMultiUDPSink::clear:
195 * @gstmultiudpsink: the sink on which the signal is emitted
197 * Clear the list of clients.
199 gst_multiudpsink_signals[SIGNAL_CLEAR] =
200 g_signal_new ("clear", G_TYPE_FROM_CLASS (klass),
201 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
202 G_STRUCT_OFFSET (GstMultiUDPSinkClass, clear),
203 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 0);
205 * GstMultiUDPSink::get-stats:
206 * @gstmultiudpsink: the sink on which the signal is emitted
207 * @host: the hostname/IP address of the client to get stats on
208 * @port: the port of the client to get stats on
210 * Get the statistics of the client with destination @host and @port.
212 * Returns: a GstStructure: bytes_sent, packets_sent,
213 * connect_time (in epoch seconds), disconnect_time (in epoch seconds)
215 gst_multiudpsink_signals[SIGNAL_GET_STATS] =
216 g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass),
217 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
218 G_STRUCT_OFFSET (GstMultiUDPSinkClass, get_stats),
219 NULL, NULL, g_cclosure_marshal_generic, GST_TYPE_STRUCTURE, 2,
220 G_TYPE_STRING, G_TYPE_INT);
222 * GstMultiUDPSink::client-added:
223 * @gstmultiudpsink: the sink emitting the signal
224 * @host: the hostname/IP address of the added client
225 * @port: the port of the added client
227 * Signal emited when a new client is added to the list of
230 gst_multiudpsink_signals[SIGNAL_CLIENT_ADDED] =
231 g_signal_new ("client-added", G_TYPE_FROM_CLASS (klass),
232 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiUDPSinkClass, client_added),
233 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
234 G_TYPE_STRING, G_TYPE_INT);
236 * GstMultiUDPSink::client-removed:
237 * @gstmultiudpsink: the sink emitting the signal
238 * @host: the hostname/IP address of the removed client
239 * @port: the port of the removed client
241 * Signal emited when a client is removed from the list of
244 gst_multiudpsink_signals[SIGNAL_CLIENT_REMOVED] =
245 g_signal_new ("client-removed", G_TYPE_FROM_CLASS (klass),
246 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiUDPSinkClass,
247 client_removed), NULL, NULL, g_cclosure_marshal_generic,
248 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
250 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTES_TO_SERVE,
251 g_param_spec_uint64 ("bytes-to-serve", "Bytes to serve",
252 "Number of bytes received to serve to clients", 0, G_MAXUINT64, 0,
253 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
254 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTES_SERVED,
255 g_param_spec_uint64 ("bytes-served", "Bytes served",
256 "Total number of bytes sent to all clients", 0, G_MAXUINT64, 0,
257 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
258 g_object_class_install_property (gobject_class, PROP_SOCKET,
259 g_param_spec_object ("socket", "Socket Handle",
260 "Socket to use for UDP sending. (NULL == allocate)",
261 G_TYPE_SOCKET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
262 g_object_class_install_property (gobject_class, PROP_CLOSE_SOCKET,
263 g_param_spec_boolean ("close-socket", "Close socket",
264 "Close socket if passed as property on state change",
265 DEFAULT_CLOSE_SOCKET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
266 g_object_class_install_property (gobject_class, PROP_USED_SOCKET,
267 g_param_spec_object ("used-socket", "Used Socket Handle",
268 "Socket currently in use for UDP sending. (NULL == no socket)",
269 G_TYPE_SOCKET, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
270 g_object_class_install_property (gobject_class, PROP_CLIENTS,
271 g_param_spec_string ("clients", "Clients",
272 "A comma separated list of host:port pairs with destinations",
273 DEFAULT_CLIENTS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
274 g_object_class_install_property (gobject_class, PROP_AUTO_MULTICAST,
275 g_param_spec_boolean ("auto-multicast",
276 "Automatically join/leave multicast groups",
277 "Automatically join/leave the multicast groups, FALSE means user"
278 " has to do it himself", DEFAULT_AUTO_MULTICAST,
279 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
280 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
281 g_param_spec_string ("multicast-iface", "Multicast Interface",
282 "The network interface on which to join the multicast group",
283 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
284 g_object_class_install_property (gobject_class, PROP_TTL,
285 g_param_spec_int ("ttl", "Unicast TTL",
286 "Used for setting the unicast TTL parameter",
287 0, 255, DEFAULT_TTL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
288 g_object_class_install_property (gobject_class, PROP_TTL_MC,
289 g_param_spec_int ("ttl-mc", "Multicast TTL",
290 "Used for setting the multicast TTL parameter",
291 0, 255, DEFAULT_TTL_MC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
292 g_object_class_install_property (gobject_class, PROP_LOOP,
293 g_param_spec_boolean ("loop", "Multicast Loopback",
294 "Used for setting the multicast loop parameter. TRUE = enable,"
295 " FALSE = disable", DEFAULT_LOOP,
296 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
298 * GstMultiUDPSink::force-ipv4
300 * Force the use of an IPv4 socket.
304 g_object_class_install_property (gobject_class, PROP_FORCE_IPV4,
305 g_param_spec_boolean ("force-ipv4", "Force IPv4",
306 "Forcing the use of an IPv4 socket", DEFAULT_FORCE_IPV4,
307 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
309 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QOS_DSCP,
310 g_param_spec_int ("qos-dscp", "QoS diff srv code point",
311 "Quality of Service, differentiated services code point (-1 default)",
312 -1, 63, DEFAULT_QOS_DSCP,
313 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
315 * GstMultiUDPSink::send-duplicates
317 * When a host/port pair is added mutliple times, send the packet to the host
318 * multiple times as well.
322 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEND_DUPLICATES,
323 g_param_spec_boolean ("send-duplicates", "Send Duplicates",
324 "When a distination/port pair is added multiple times, send packets "
325 "multiple times as well", DEFAULT_SEND_DUPLICATES,
326 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
328 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_SIZE,
329 g_param_spec_int ("buffer-size", "Buffer Size",
330 "Size of the kernel send buffer in bytes, 0=default", 0, G_MAXINT,
331 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
333 gst_element_class_add_pad_template (gstelement_class,
334 gst_static_pad_template_get (&sink_template));
336 gst_element_class_set_static_metadata (gstelement_class, "UDP packet sender",
338 "Send data over the network via UDP",
339 "Wim Taymans <wim.taymans@gmail.com>");
341 gstbasesink_class->render = gst_multiudpsink_render;
342 gstbasesink_class->start = gst_multiudpsink_start;
343 gstbasesink_class->stop = gst_multiudpsink_stop;
344 gstbasesink_class->unlock = gst_multiudpsink_unlock;
345 gstbasesink_class->unlock_stop = gst_multiudpsink_unlock_stop;
346 klass->add = gst_multiudpsink_add;
347 klass->remove = gst_multiudpsink_remove;
348 klass->clear = gst_multiudpsink_clear;
349 klass->get_stats = gst_multiudpsink_get_stats;
351 GST_DEBUG_CATEGORY_INIT (multiudpsink_debug, "multiudpsink", 0, "UDP sink");
356 gst_multiudpsink_init (GstMultiUDPSink * sink)
360 g_mutex_init (&sink->client_lock);
361 sink->socket = DEFAULT_SOCKET;
362 sink->used_socket = DEFAULT_USED_SOCKET;
363 sink->close_socket = DEFAULT_CLOSE_SOCKET;
364 sink->external_socket = (sink->socket != NULL);
365 sink->auto_multicast = DEFAULT_AUTO_MULTICAST;
366 sink->ttl = DEFAULT_TTL;
367 sink->ttl_mc = DEFAULT_TTL_MC;
368 sink->loop = DEFAULT_LOOP;
369 sink->force_ipv4 = DEFAULT_FORCE_IPV4;
370 sink->qos_dscp = DEFAULT_QOS_DSCP;
371 sink->send_duplicates = DEFAULT_SEND_DUPLICATES;
372 sink->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
374 sink->cancellable = g_cancellable_new ();
376 /* allocate OutputVector and MapInfo for use in the render function, buffers can
377 * hold up to a maximum amount of memory so we can create a maximally sized
379 max_mem = gst_buffer_get_max_memory ();
381 sink->vec = g_new (GOutputVector, max_mem);
382 sink->map = g_new (GstMapInfo, max_mem);
385 static GstUDPClient *
386 create_client (GstMultiUDPSink * sink, const gchar * host, gint port)
388 GstUDPClient *client;
393 addr = g_inet_address_new_from_string (host);
397 resolver = g_resolver_get_default ();
399 g_resolver_lookup_by_name (resolver, host, sink->cancellable, &err);
402 addr = G_INET_ADDRESS (g_object_ref (results->data));
404 g_resolver_free_addresses (results);
405 g_object_unref (resolver);
407 #ifndef GST_DISABLE_GST_DEBUG
409 gchar *ip = g_inet_address_to_string (addr);
411 GST_DEBUG_OBJECT (sink, "IP address for host %s is %s", host, ip);
416 client = g_slice_new0 (GstUDPClient);
417 client->refcount = 1;
418 client->host = g_strdup (host);
420 client->addr = g_inet_socket_address_new (addr, port);
421 g_object_unref (addr);
427 g_object_unref (resolver);
434 free_client (GstUDPClient * client)
436 g_object_unref (client->addr);
437 g_free (client->host);
438 g_slice_free (GstUDPClient, client);
442 client_compare (GstUDPClient * a, GstUDPClient * b)
444 if ((a->port == b->port) && (strcmp (a->host, b->host) == 0))
451 gst_multiudpsink_finalize (GObject * object)
453 GstMultiUDPSink *sink;
455 sink = GST_MULTIUDPSINK (object);
457 g_list_foreach (sink->clients, (GFunc) free_client, NULL);
458 g_list_free (sink->clients);
461 g_object_unref (sink->socket);
464 if (sink->used_socket)
465 g_object_unref (sink->used_socket);
466 sink->used_socket = NULL;
468 if (sink->cancellable)
469 g_object_unref (sink->cancellable);
470 sink->cancellable = NULL;
472 g_free (sink->multi_iface);
473 sink->multi_iface = NULL;
478 g_mutex_clear (&sink->client_lock);
480 G_OBJECT_CLASS (parent_class)->finalize (object);
484 gst_multiudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
486 GstMultiUDPSink *sink;
493 gint num, no_clients;
496 sink = GST_MULTIUDPSINK (bsink);
498 n_mem = gst_buffer_n_memory (buffer);
502 /* allocated on the stack, the max number of memory blocks is limited so this
503 * should not cause stack overflows */
508 for (i = 0; i < n_mem; i++) {
509 mem = gst_buffer_get_memory (buffer, i);
510 gst_memory_map (mem, &map[i], GST_MAP_READ);
512 vec[i].buffer = map[i].data;
513 vec[i].size = map[i].size;
518 sink->bytes_to_serve += size;
520 /* grab lock while iterating and sending to clients, this should be
521 * fast as UDP never blocks */
522 g_mutex_lock (&sink->client_lock);
523 GST_LOG_OBJECT (bsink, "about to send %" G_GSIZE_FORMAT " bytes in %u blocks",
528 for (clients = sink->clients; clients; clients = g_list_next (clients)) {
529 GstUDPClient *client;
532 client = (GstUDPClient *) clients->data;
534 GST_LOG_OBJECT (sink, "sending %" G_GSIZE_FORMAT " bytes to client %p",
537 count = sink->send_duplicates ? client->refcount : 1;
543 g_socket_send_message (sink->used_socket, client->addr, vec, n_mem,
544 NULL, 0, 0, sink->cancellable, &err);
546 if (G_UNLIKELY (ret < 0)) {
547 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED))
550 /* we continue after posting a warning, next packets might be ok
552 if (size > UDP_MAX_SIZE) {
553 GST_ELEMENT_WARNING (sink, RESOURCE, WRITE,
554 ("Attempting to send a UDP packet larger than maximum size "
555 "(%" G_GSIZE_FORMAT " > %d)", size, UDP_MAX_SIZE),
556 ("Reason: %s", err ? err->message : "unknown reason"));
558 GST_ELEMENT_WARNING (sink, RESOURCE, WRITE,
559 ("Error sending UDP packet"), ("Reason: %s",
560 err ? err->message : "unknown reason"));
562 g_clear_error (&err);
565 client->bytes_sent += ret;
566 client->packets_sent++;
567 sink->bytes_served += ret;
571 g_mutex_unlock (&sink->client_lock);
573 /* unmap all memory again */
574 for (i = 0; i < n_mem; i++) {
575 gst_memory_unmap (map[i].memory, &map[i]);
576 gst_memory_unref (map[i].memory);
579 GST_LOG_OBJECT (sink, "sent %" G_GSIZE_FORMAT " bytes to %d (of %d) clients",
580 size, num, no_clients);
590 GST_DEBUG ("we are flushing");
591 g_mutex_unlock (&sink->client_lock);
592 g_clear_error (&err);
594 return GST_FLOW_FLUSHING;
599 gst_multiudpsink_set_clients_string (GstMultiUDPSink * sink,
600 const gchar * string)
605 clients = g_strsplit (string, ",", 0);
607 g_mutex_lock (&sink->client_lock);
608 /* clear all existing clients */
609 gst_multiudpsink_clear_internal (sink, FALSE);
610 for (i = 0; clients[i]; i++) {
615 p = strstr (clients[i], ":");
618 port = g_ascii_strtoll (p + 1, NULL, 10);
621 gst_multiudpsink_add_internal (sink, host, port, FALSE);
623 g_mutex_unlock (&sink->client_lock);
625 g_strfreev (clients);
629 gst_multiudpsink_get_clients_string (GstMultiUDPSink * sink)
634 str = g_string_new ("");
636 g_mutex_lock (&sink->client_lock);
637 clients = sink->clients;
639 GstUDPClient *client;
642 client = (GstUDPClient *) clients->data;
644 clients = g_list_next (clients);
646 count = client->refcount;
648 g_string_append_printf (str, "%s:%d%s", client->host, client->port,
649 (clients || count > 1 ? "," : ""));
652 g_mutex_unlock (&sink->client_lock);
654 return g_string_free (str, FALSE);
658 gst_multiudpsink_setup_qos_dscp (GstMultiUDPSink * sink)
660 /* don't touch on -1 */
661 if (sink->qos_dscp < 0)
664 if (sink->used_socket == NULL)
672 fd = g_socket_get_fd (sink->used_socket);
674 GST_DEBUG_OBJECT (sink, "setting TOS to %d", sink->qos_dscp);
676 /* Extract and shift 6 bits of DSFIELD */
677 tos = (sink->qos_dscp & 0x3f) << 2;
679 if (setsockopt (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0) {
680 GST_ERROR_OBJECT (sink, "could not set TOS: %s", g_strerror (errno));
683 if (setsockopt (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0) {
684 GST_ERROR_OBJECT (sink, "could not set TCLASS: %s", g_strerror (errno));
692 gst_multiudpsink_set_property (GObject * object, guint prop_id,
693 const GValue * value, GParamSpec * pspec)
695 GstMultiUDPSink *udpsink;
697 udpsink = GST_MULTIUDPSINK (object);
701 if (udpsink->socket != NULL && udpsink->socket != udpsink->used_socket &&
702 udpsink->close_socket) {
705 if (!g_socket_close (udpsink->socket, &err)) {
706 GST_ERROR ("failed to close socket %p: %s", udpsink->socket,
708 g_clear_error (&err);
712 g_object_unref (udpsink->socket);
713 udpsink->socket = g_value_dup_object (value);
714 GST_DEBUG_OBJECT (udpsink, "setting socket to %p", udpsink->socket);
716 case PROP_CLOSE_SOCKET:
717 udpsink->close_socket = g_value_get_boolean (value);
720 gst_multiudpsink_set_clients_string (udpsink, g_value_get_string (value));
722 case PROP_AUTO_MULTICAST:
723 udpsink->auto_multicast = g_value_get_boolean (value);
725 case PROP_MULTICAST_IFACE:
726 g_free (udpsink->multi_iface);
728 if (g_value_get_string (value) == NULL)
729 udpsink->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
731 udpsink->multi_iface = g_value_dup_string (value);
734 udpsink->ttl = g_value_get_int (value);
737 udpsink->ttl_mc = g_value_get_int (value);
740 udpsink->loop = g_value_get_boolean (value);
742 case PROP_FORCE_IPV4:
743 udpsink->force_ipv4 = g_value_get_boolean (value);
746 udpsink->qos_dscp = g_value_get_int (value);
747 gst_multiudpsink_setup_qos_dscp (udpsink);
749 case PROP_SEND_DUPLICATES:
750 udpsink->send_duplicates = g_value_get_boolean (value);
752 case PROP_BUFFER_SIZE:
753 udpsink->buffer_size = g_value_get_int (value);
756 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
762 gst_multiudpsink_get_property (GObject * object, guint prop_id, GValue * value,
765 GstMultiUDPSink *udpsink;
767 udpsink = GST_MULTIUDPSINK (object);
770 case PROP_BYTES_TO_SERVE:
771 g_value_set_uint64 (value, udpsink->bytes_to_serve);
773 case PROP_BYTES_SERVED:
774 g_value_set_uint64 (value, udpsink->bytes_served);
777 g_value_set_object (value, udpsink->socket);
779 case PROP_CLOSE_SOCKET:
780 g_value_set_boolean (value, udpsink->close_socket);
782 case PROP_USED_SOCKET:
783 g_value_set_object (value, udpsink->used_socket);
786 g_value_take_string (value,
787 gst_multiudpsink_get_clients_string (udpsink));
789 case PROP_AUTO_MULTICAST:
790 g_value_set_boolean (value, udpsink->auto_multicast);
792 case PROP_MULTICAST_IFACE:
793 g_value_set_string (value, udpsink->multi_iface);
796 g_value_set_int (value, udpsink->ttl);
799 g_value_set_int (value, udpsink->ttl_mc);
802 g_value_set_boolean (value, udpsink->loop);
804 case PROP_FORCE_IPV4:
805 g_value_set_boolean (value, udpsink->force_ipv4);
808 g_value_set_int (value, udpsink->qos_dscp);
810 case PROP_SEND_DUPLICATES:
811 g_value_set_boolean (value, udpsink->send_duplicates);
813 case PROP_BUFFER_SIZE:
814 g_value_set_int (value, udpsink->buffer_size);
817 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
823 gst_multiudpsink_configure_client (GstMultiUDPSink * sink,
824 GstUDPClient * client)
826 GInetSocketAddress *saddr = G_INET_SOCKET_ADDRESS (client->addr);
827 GInetAddress *addr = g_inet_socket_address_get_address (saddr);
830 GST_DEBUG_OBJECT (sink, "configuring client %p", client);
832 if (g_inet_address_get_is_multicast (addr)) {
833 GST_DEBUG_OBJECT (sink, "we have a multicast client %p", client);
834 if (sink->auto_multicast) {
835 GST_DEBUG_OBJECT (sink, "autojoining group");
836 if (!g_socket_join_multicast_group (sink->used_socket, addr, FALSE,
837 sink->multi_iface, &err))
838 goto join_group_failed;
840 GST_DEBUG_OBJECT (sink, "setting loop to %d", sink->loop);
841 g_socket_set_multicast_loopback (sink->used_socket, sink->loop);
842 GST_DEBUG_OBJECT (sink, "setting ttl to %d", sink->ttl_mc);
843 g_socket_set_multicast_ttl (sink->used_socket, sink->ttl_mc);
845 GST_DEBUG_OBJECT (sink, "setting unicast ttl to %d", sink->ttl);
846 g_socket_set_ttl (sink->used_socket, sink->ttl);
853 gst_multiudpsink_stop (GST_BASE_SINK (sink));
854 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
855 ("Could not join multicast group: %s",
856 err ? err->message : "unknown reason"));
857 g_clear_error (&err);
862 /* create a socket for sending to remote machine */
864 gst_multiudpsink_start (GstBaseSink * bsink)
866 GstMultiUDPSink *sink;
868 GstUDPClient *client;
871 sink = GST_MULTIUDPSINK (bsink);
873 if (sink->socket == NULL) {
874 GST_DEBUG_OBJECT (sink, "creating sockets");
875 /* create sender socket try IP6, fall back to IP4 */
876 if (sink->force_ipv4 || (sink->used_socket =
877 g_socket_new (G_SOCKET_FAMILY_IPV6,
878 G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &err)) == NULL) {
879 if ((sink->used_socket = g_socket_new (G_SOCKET_FAMILY_IPV4,
880 G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &err)) == NULL)
884 GST_DEBUG_OBJECT (sink, "have socket");
885 sink->external_socket = FALSE;
887 GST_DEBUG_OBJECT (sink, "using configured socket");
888 /* we use the configured socket */
889 sink->used_socket = G_SOCKET (g_object_ref (sink->socket));
890 sink->external_socket = TRUE;
898 len = sizeof (sndsize);
899 if (sink->buffer_size != 0) {
900 sndsize = sink->buffer_size;
902 GST_DEBUG_OBJECT (sink, "setting udp buffer of %d bytes", sndsize);
903 /* set buffer size, Note that on Linux this is typically limited to a
904 * maximum of around 100K. Also a minimum of 128 bytes is required on
907 setsockopt (g_socket_get_fd (sink->used_socket), SOL_SOCKET,
908 SO_SNDBUF, (void *) &sndsize, len);
910 GST_ELEMENT_WARNING (sink, RESOURCE, SETTINGS, (NULL),
911 ("Could not create a buffer of requested %d bytes, %d: %s",
912 sndsize, ret, g_strerror (errno)));
916 /* read the value of the receive buffer. Note that on linux this returns 2x the
917 * value we set because the kernel allocates extra memory for metadata.
918 * The default on Linux is about 100K (which is about 50K without metadata) */
920 getsockopt (g_socket_get_fd (sink->used_socket), SOL_SOCKET, SO_SNDBUF,
921 (void *) &sndsize, &len);
923 GST_DEBUG_OBJECT (sink, "have udp buffer of %d bytes", sndsize);
925 GST_DEBUG_OBJECT (sink, "could not get udp buffer size");
929 g_socket_set_broadcast (sink->used_socket, TRUE);
931 sink->bytes_to_serve = 0;
932 sink->bytes_served = 0;
934 gst_multiudpsink_setup_qos_dscp (sink);
936 /* look for multicast clients and join multicast groups appropriately
937 set also ttl and multicast loopback delivery appropriately */
938 for (clients = sink->clients; clients; clients = g_list_next (clients)) {
939 client = (GstUDPClient *) clients->data;
941 if (!gst_multiudpsink_configure_client (sink, client))
949 GST_ELEMENT_ERROR (sink, RESOURCE, FAILED, (NULL),
950 ("Could not create socket: %s", err->message));
951 g_clear_error (&err);
957 gst_multiudpsink_stop (GstBaseSink * bsink)
959 GstMultiUDPSink *udpsink;
961 udpsink = GST_MULTIUDPSINK (bsink);
963 if (udpsink->used_socket) {
964 if (udpsink->close_socket || !udpsink->external_socket) {
967 if (!g_socket_close (udpsink->used_socket, &err)) {
968 GST_ERROR_OBJECT (udpsink, "Failed to close socket: %s", err->message);
969 g_clear_error (&err);
973 g_object_unref (udpsink->used_socket);
974 udpsink->used_socket = NULL;
981 gst_multiudpsink_add_internal (GstMultiUDPSink * sink, const gchar * host,
982 gint port, gboolean lock)
984 GstUDPClient *client;
985 GstUDPClient udpclient;
989 udpclient.host = (gchar *) host;
990 udpclient.port = port;
992 GST_DEBUG_OBJECT (sink, "adding client on host %s, port %d", host, port);
995 g_mutex_lock (&sink->client_lock);
997 find = g_list_find_custom (sink->clients, &udpclient,
998 (GCompareFunc) client_compare);
1000 client = (GstUDPClient *) find->data;
1002 GST_DEBUG_OBJECT (sink, "found %d existing clients with host %s, port %d",
1003 client->refcount, host, port);
1006 client = create_client (sink, host, port);
1010 g_get_current_time (&now);
1011 client->connect_time = GST_TIMEVAL_TO_TIME (now);
1013 if (sink->used_socket)
1014 gst_multiudpsink_configure_client (sink, client);
1016 GST_DEBUG_OBJECT (sink, "add client with host %s, port %d", host, port);
1017 sink->clients = g_list_prepend (sink->clients, client);
1021 g_mutex_unlock (&sink->client_lock);
1023 g_signal_emit (G_OBJECT (sink),
1024 gst_multiudpsink_signals[SIGNAL_CLIENT_ADDED], 0, host, port);
1026 GST_DEBUG_OBJECT (sink, "added client on host %s, port %d", host, port);
1032 GST_DEBUG_OBJECT (sink, "did not add client on host %s, port %d", host,
1035 g_mutex_unlock (&sink->client_lock);
1041 gst_multiudpsink_add (GstMultiUDPSink * sink, const gchar * host, gint port)
1043 gst_multiudpsink_add_internal (sink, host, port, TRUE);
1047 gst_multiudpsink_remove (GstMultiUDPSink * sink, const gchar * host, gint port)
1050 GstUDPClient udpclient;
1051 GstUDPClient *client;
1054 udpclient.host = (gchar *) host;
1055 udpclient.port = port;
1057 g_mutex_lock (&sink->client_lock);
1058 find = g_list_find_custom (sink->clients, &udpclient,
1059 (GCompareFunc) client_compare);
1063 client = (GstUDPClient *) find->data;
1065 GST_DEBUG_OBJECT (sink, "found %d clients with host %s, port %d",
1066 client->refcount, host, port);
1069 if (client->refcount == 0) {
1070 GInetSocketAddress *saddr = G_INET_SOCKET_ADDRESS (client->addr);
1071 GInetAddress *addr = g_inet_socket_address_get_address (saddr);
1073 GST_DEBUG_OBJECT (sink, "remove client with host %s, port %d", host, port);
1075 g_get_current_time (&now);
1076 client->disconnect_time = GST_TIMEVAL_TO_TIME (now);
1078 if (sink->used_socket && sink->auto_multicast
1079 && g_inet_address_get_is_multicast (addr)) {
1082 if (!g_socket_leave_multicast_group (sink->used_socket, addr, FALSE,
1083 sink->multi_iface, &err)) {
1084 GST_DEBUG_OBJECT (sink, "Failed to leave multicast group: %s",
1086 g_clear_error (&err);
1090 /* Unlock to emit signal before we delete the actual client */
1091 g_mutex_unlock (&sink->client_lock);
1092 g_signal_emit (G_OBJECT (sink),
1093 gst_multiudpsink_signals[SIGNAL_CLIENT_REMOVED], 0, host, port);
1094 g_mutex_lock (&sink->client_lock);
1096 sink->clients = g_list_delete_link (sink->clients, find);
1098 free_client (client);
1100 g_mutex_unlock (&sink->client_lock);
1107 g_mutex_unlock (&sink->client_lock);
1108 GST_WARNING_OBJECT (sink, "client at host %s, port %d not found",
1115 gst_multiudpsink_clear_internal (GstMultiUDPSink * sink, gboolean lock)
1117 GST_DEBUG_OBJECT (sink, "clearing");
1118 /* we only need to remove the client structure, there is no additional
1119 * socket or anything to free for UDP */
1121 g_mutex_lock (&sink->client_lock);
1122 g_list_foreach (sink->clients, (GFunc) free_client, sink);
1123 g_list_free (sink->clients);
1124 sink->clients = NULL;
1126 g_mutex_unlock (&sink->client_lock);
1130 gst_multiudpsink_clear (GstMultiUDPSink * sink)
1132 gst_multiudpsink_clear_internal (sink, TRUE);
1136 gst_multiudpsink_get_stats (GstMultiUDPSink * sink, const gchar * host,
1139 GstUDPClient *client;
1140 GstStructure *result = NULL;
1141 GstUDPClient udpclient;
1144 udpclient.host = (gchar *) host;
1145 udpclient.port = port;
1147 g_mutex_lock (&sink->client_lock);
1149 find = g_list_find_custom (sink->clients, &udpclient,
1150 (GCompareFunc) client_compare);
1154 GST_DEBUG_OBJECT (sink, "stats for client with host %s, port %d", host, port);
1156 client = (GstUDPClient *) find->data;
1158 result = gst_structure_new_empty ("multiudpsink-stats");
1160 gst_structure_set (result,
1161 "bytes-sent", G_TYPE_UINT64, client->bytes_sent,
1162 "packets-sent", G_TYPE_UINT64, client->packets_sent,
1163 "connect-time", G_TYPE_UINT64, client->connect_time,
1164 "disconnect-time", G_TYPE_UINT64, client->disconnect_time, NULL);
1166 g_mutex_unlock (&sink->client_lock);
1173 g_mutex_unlock (&sink->client_lock);
1174 GST_WARNING_OBJECT (sink, "client with host %s, port %d not found",
1176 /* Apparently (see comment in gstmultifdsink.c) returning NULL from here may
1177 * confuse/break python bindings */
1178 return gst_structure_new_empty ("multiudpsink-stats");
1183 gst_multiudpsink_unlock (GstBaseSink * bsink)
1185 GstMultiUDPSink *sink;
1187 sink = GST_MULTIUDPSINK (bsink);
1189 g_cancellable_cancel (sink->cancellable);
1195 gst_multiudpsink_unlock_stop (GstBaseSink * bsink)
1197 GstMultiUDPSink *sink;
1199 sink = GST_MULTIUDPSINK (bsink);
1201 g_cancellable_reset (sink->cancellable);