2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3 * Copyright (C) <2009> Jarkko Palviainen <jarkko.palviainen@sesca.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-multiudpsink
23 * @see_also: udpsink, multifdsink
25 * multiudpsink is a network sink that sends UDP packets to multiple
27 * It can be combined with rtp payload encoders to implement RTP streaming.
33 #include "gstudp-marshal.h"
34 #include "gstmultiudpsink.h"
44 #include "gst/glib-compat-private.h"
46 GST_DEBUG_CATEGORY_STATIC (multiudpsink_debug);
47 #define GST_CAT_DEFAULT (multiudpsink_debug)
49 #define UDP_MAX_SIZE 65507
51 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
56 /* MultiUDPSink signals and args */
67 SIGNAL_CLIENT_REMOVED,
73 #define DEFAULT_SOCKFD -1
74 #define DEFAULT_CLOSEFD TRUE
75 #define DEFAULT_SOCK -1
76 #define DEFAULT_CLIENTS NULL
77 #define DEFAULT_FAMILY 0
78 /* FIXME, this should be disabled by default, we don't need to join a multicast
79 * group for sending, if this socket is also used for receiving, it should
80 * be configured in the element that does the receive. */
81 #define DEFAULT_AUTO_MULTICAST TRUE
82 #define DEFAULT_TTL 64
83 #define DEFAULT_TTL_MC 1
84 #define DEFAULT_LOOP TRUE
85 #define DEFAULT_QOS_DSCP -1
86 #define DEFAULT_SEND_DUPLICATES TRUE
87 #define DEFAULT_BUFFER_SIZE 0
103 PROP_SEND_DUPLICATES,
108 #define CLOSE_IF_REQUESTED(udpctx) \
110 if ((!udpctx->externalfd) || (udpctx->externalfd && udpctx->closefd)) { \
111 CLOSE_SOCKET(udpctx->sock); \
112 if (udpctx->sock == udpctx->sockfd) \
113 udpctx->sockfd = DEFAULT_SOCKFD; \
115 udpctx->sock = DEFAULT_SOCK; \
118 static void gst_multiudpsink_base_init (gpointer g_class);
119 static void gst_multiudpsink_class_init (GstMultiUDPSinkClass * klass);
120 static void gst_multiudpsink_init (GstMultiUDPSink * udpsink);
121 static void gst_multiudpsink_finalize (GObject * object);
123 static GstFlowReturn gst_multiudpsink_render (GstBaseSink * sink,
125 #ifndef G_OS_WIN32 /* sendmsg() is not available on Windows */
126 static GstFlowReturn gst_multiudpsink_render_list (GstBaseSink * bsink,
127 GstBufferList * list);
129 static GstStateChangeReturn gst_multiudpsink_change_state (GstElement *
130 element, GstStateChange transition);
132 static void gst_multiudpsink_set_property (GObject * object, guint prop_id,
133 const GValue * value, GParamSpec * pspec);
134 static void gst_multiudpsink_get_property (GObject * object, guint prop_id,
135 GValue * value, GParamSpec * pspec);
137 static void gst_multiudpsink_add_internal (GstMultiUDPSink * sink,
138 const gchar * host, gint port, gboolean lock);
139 static void gst_multiudpsink_clear_internal (GstMultiUDPSink * sink,
142 static GstElementClass *parent_class = NULL;
144 static guint gst_multiudpsink_signals[LAST_SIGNAL] = { 0 };
147 gst_multiudpsink_get_type (void)
149 static GType multiudpsink_type = 0;
151 if (!multiudpsink_type) {
152 static const GTypeInfo multiudpsink_info = {
153 sizeof (GstMultiUDPSinkClass),
154 gst_multiudpsink_base_init,
156 (GClassInitFunc) gst_multiudpsink_class_init,
159 sizeof (GstMultiUDPSink),
161 (GInstanceInitFunc) gst_multiudpsink_init,
166 g_type_register_static (GST_TYPE_BASE_SINK, "GstMultiUDPSink",
167 &multiudpsink_info, 0);
169 return multiudpsink_type;
173 gst_multiudpsink_base_init (gpointer g_class)
175 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
177 gst_element_class_add_static_pad_template (element_class, &sink_template);
179 gst_element_class_set_details_simple (element_class, "UDP packet sender",
181 "Send data over the network via UDP",
182 "Wim Taymans <wim.taymans@gmail.com>");
186 gst_multiudpsink_class_init (GstMultiUDPSinkClass * klass)
188 GObjectClass *gobject_class;
189 GstElementClass *gstelement_class;
190 GstBaseSinkClass *gstbasesink_class;
192 gobject_class = (GObjectClass *) klass;
193 gstelement_class = (GstElementClass *) klass;
194 gstbasesink_class = (GstBaseSinkClass *) klass;
196 parent_class = g_type_class_peek_parent (klass);
198 gobject_class->set_property = gst_multiudpsink_set_property;
199 gobject_class->get_property = gst_multiudpsink_get_property;
200 gobject_class->finalize = gst_multiudpsink_finalize;
203 * GstMultiUDPSink::add:
204 * @gstmultiudpsink: the sink on which the signal is emitted
205 * @host: the hostname/IP address of the client to add
206 * @port: the port of the client to add
208 * Add a client with destination @host and @port to the list of
209 * clients. When the same host/port pair is added multiple times, the
210 * send-duplicates property defines if the packets are sent multiple times to
211 * the same host/port pair or not.
213 * When a host/port pair is added multiple times, an equal amount of remove
214 * calls must be performed to actually remove the host/port pair from the list
217 gst_multiudpsink_signals[SIGNAL_ADD] =
218 g_signal_new ("add", G_TYPE_FROM_CLASS (klass),
219 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
220 G_STRUCT_OFFSET (GstMultiUDPSinkClass, add),
221 NULL, NULL, gst_udp_marshal_VOID__STRING_INT, G_TYPE_NONE, 2,
222 G_TYPE_STRING, G_TYPE_INT);
224 * GstMultiUDPSink::remove:
225 * @gstmultiudpsink: the sink on which the signal is emitted
226 * @host: the hostname/IP address of the client to remove
227 * @port: the port of the client to remove
229 * Remove the client with destination @host and @port from the list of
232 gst_multiudpsink_signals[SIGNAL_REMOVE] =
233 g_signal_new ("remove", G_TYPE_FROM_CLASS (klass),
234 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
235 G_STRUCT_OFFSET (GstMultiUDPSinkClass, remove),
236 NULL, NULL, gst_udp_marshal_VOID__STRING_INT, G_TYPE_NONE, 2,
237 G_TYPE_STRING, G_TYPE_INT);
239 * GstMultiUDPSink::clear:
240 * @gstmultiudpsink: the sink on which the signal is emitted
242 * Clear the list of clients.
244 gst_multiudpsink_signals[SIGNAL_CLEAR] =
245 g_signal_new ("clear", G_TYPE_FROM_CLASS (klass),
246 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
247 G_STRUCT_OFFSET (GstMultiUDPSinkClass, clear),
248 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
250 * GstMultiUDPSink::get-stats:
251 * @gstmultiudpsink: the sink on which the signal is emitted
252 * @host: the hostname/IP address of the client to get stats on
253 * @port: the port of the client to get stats on
255 * Get the statistics of the client with destination @host and @port.
257 * Returns: a GValueArray of uint64: bytes_sent, packets_sent,
258 * connect_time (in epoch seconds), disconnect_time (in epoch seconds)
260 gst_multiudpsink_signals[SIGNAL_GET_STATS] =
261 g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass),
262 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
263 G_STRUCT_OFFSET (GstMultiUDPSinkClass, get_stats),
264 NULL, NULL, gst_udp_marshal_BOXED__STRING_INT, G_TYPE_VALUE_ARRAY, 2,
265 G_TYPE_STRING, G_TYPE_INT);
267 * GstMultiUDPSink::client-added:
268 * @gstmultiudpsink: the sink emitting the signal
269 * @host: the hostname/IP address of the added client
270 * @port: the port of the added client
272 * Signal emited when a new client is added to the list of
275 gst_multiudpsink_signals[SIGNAL_CLIENT_ADDED] =
276 g_signal_new ("client-added", G_TYPE_FROM_CLASS (klass),
277 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiUDPSinkClass, client_added),
278 NULL, NULL, gst_udp_marshal_VOID__STRING_INT, G_TYPE_NONE, 2,
279 G_TYPE_STRING, G_TYPE_INT);
281 * GstMultiUDPSink::client-removed:
282 * @gstmultiudpsink: the sink emitting the signal
283 * @host: the hostname/IP address of the removed client
284 * @port: the port of the removed client
286 * Signal emited when a client is removed from the list of
289 gst_multiudpsink_signals[SIGNAL_CLIENT_REMOVED] =
290 g_signal_new ("client-removed", G_TYPE_FROM_CLASS (klass),
291 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiUDPSinkClass,
292 client_removed), NULL, NULL, gst_udp_marshal_VOID__STRING_INT,
293 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
295 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTES_TO_SERVE,
296 g_param_spec_uint64 ("bytes-to-serve", "Bytes to serve",
297 "Number of bytes received to serve to clients", 0, G_MAXUINT64, 0,
298 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
299 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTES_SERVED,
300 g_param_spec_uint64 ("bytes-served", "Bytes served",
301 "Total number of bytes sent to all clients", 0, G_MAXUINT64, 0,
302 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
303 g_object_class_install_property (gobject_class, PROP_SOCKFD,
304 g_param_spec_int ("sockfd", "Socket Handle",
305 "Socket to use for UDP sending. (-1 == allocate)",
306 -1, G_MAXINT, DEFAULT_SOCKFD,
307 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
308 g_object_class_install_property (gobject_class, PROP_CLOSEFD,
309 g_param_spec_boolean ("closefd", "Close sockfd",
310 "Close sockfd if passed as property on state change",
311 DEFAULT_CLOSEFD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
312 g_object_class_install_property (gobject_class, PROP_SOCK,
313 g_param_spec_int ("sock", "Socket Handle",
314 "Socket currently in use for UDP sending. (-1 == no socket)",
315 -1, G_MAXINT, DEFAULT_SOCK,
316 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
317 g_object_class_install_property (gobject_class, PROP_CLIENTS,
318 g_param_spec_string ("clients", "Clients",
319 "A comma separated list of host:port pairs with destinations",
320 DEFAULT_CLIENTS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
321 g_object_class_install_property (gobject_class, PROP_AUTO_MULTICAST,
322 g_param_spec_boolean ("auto-multicast",
323 "Automatically join/leave multicast groups",
324 "Automatically join/leave the multicast groups, FALSE means user"
325 " has to do it himself", DEFAULT_AUTO_MULTICAST,
326 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
327 g_object_class_install_property (gobject_class, PROP_TTL,
328 g_param_spec_int ("ttl", "Unicast TTL",
329 "Used for setting the unicast TTL parameter",
330 0, 255, DEFAULT_TTL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
331 g_object_class_install_property (gobject_class, PROP_TTL_MC,
332 g_param_spec_int ("ttl-mc", "Multicast TTL",
333 "Used for setting the multicast TTL parameter",
334 0, 255, DEFAULT_TTL_MC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
335 g_object_class_install_property (gobject_class, PROP_LOOP,
336 g_param_spec_boolean ("loop", "Multicast Loopback",
337 "Used for setting the multicast loop parameter. TRUE = enable,"
338 " FALSE = disable", DEFAULT_LOOP,
339 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
340 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QOS_DSCP,
341 g_param_spec_int ("qos-dscp", "QoS diff srv code point",
342 "Quality of Service, differentiated services code point (-1 default)",
343 -1, 63, DEFAULT_QOS_DSCP,
344 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
346 * GstMultiUDPSink::send-duplicates
348 * When a host/port pair is added mutliple times, send the packet to the host
349 * multiple times as well.
353 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEND_DUPLICATES,
354 g_param_spec_boolean ("send-duplicates", "Send Duplicates",
355 "When a distination/port pair is added multiple times, send packets "
356 "multiple times as well", DEFAULT_SEND_DUPLICATES,
357 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_SIZE,
360 g_param_spec_int ("buffer-size", "Buffer Size",
361 "Size of the kernel send buffer in bytes, 0=default", 0, G_MAXINT,
362 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
364 gstelement_class->change_state = gst_multiudpsink_change_state;
366 gstbasesink_class->render = gst_multiudpsink_render;
368 gstbasesink_class->render_list = gst_multiudpsink_render_list;
370 klass->add = gst_multiudpsink_add;
371 klass->remove = gst_multiudpsink_remove;
372 klass->clear = gst_multiudpsink_clear;
373 klass->get_stats = gst_multiudpsink_get_stats;
375 GST_DEBUG_CATEGORY_INIT (multiudpsink_debug, "multiudpsink", 0, "UDP sink");
380 gst_multiudpsink_init (GstMultiUDPSink * sink)
384 sink->client_lock = g_mutex_new ();
385 sink->sock = DEFAULT_SOCK;
386 sink->sockfd = DEFAULT_SOCKFD;
387 sink->closefd = DEFAULT_CLOSEFD;
388 sink->externalfd = (sink->sockfd != -1);
389 sink->auto_multicast = DEFAULT_AUTO_MULTICAST;
390 sink->ttl = DEFAULT_TTL;
391 sink->ttl_mc = DEFAULT_TTL_MC;
392 sink->loop = DEFAULT_LOOP;
393 sink->qos_dscp = DEFAULT_QOS_DSCP;
394 sink->ss_family = DEFAULT_FAMILY;
395 sink->send_duplicates = DEFAULT_SEND_DUPLICATES;
398 static GstUDPClient *
399 create_client (GstMultiUDPSink * sink, const gchar * host, gint port)
401 GstUDPClient *client;
403 client = g_slice_new0 (GstUDPClient);
404 client->refcount = 1;
405 client->host = g_strdup (host);
412 free_client (GstUDPClient * client)
414 g_free (client->host);
415 g_slice_free (GstUDPClient, client);
419 client_compare (GstUDPClient * a, GstUDPClient * b)
421 if ((a->port == b->port) && (strcmp (a->host, b->host) == 0))
428 gst_multiudpsink_finalize (GObject * object)
430 GstMultiUDPSink *sink;
432 sink = GST_MULTIUDPSINK (object);
434 g_list_foreach (sink->clients, (GFunc) free_client, NULL);
435 g_list_free (sink->clients);
437 if (sink->sockfd >= 0 && sink->closefd)
438 CLOSE_SOCKET (sink->sockfd);
440 g_mutex_free (sink->client_lock);
442 WSA_CLEANUP (object);
444 G_OBJECT_CLASS (parent_class)->finalize (object);
448 socket_error_is_ignorable (void)
451 /* Windows doesn't seem to have an EAGAIN for sockets */
452 return WSAGetLastError () == WSAEINTR;
454 return errno == EINTR || errno == EAGAIN;
459 socket_last_error_code (void)
462 return WSAGetLastError ();
469 socket_last_error_message (void)
472 int errorcode = WSAGetLastError ();
475 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
476 NULL, errorcode, 0, (LPSTR) buf, sizeof (buf) / sizeof (wchar_t), NULL);
477 if (FAILED (result)) {
478 return g_strdup ("failed to get error message from system");
481 g_convert ((gchar *) buf, -1, "UTF-16", "UTF-8", NULL, NULL, NULL);
482 /* g_convert() internally calls windows functions which reset the
483 windows error code, so fix it up again like this */
484 WSASetLastError (errorcode);
488 return g_strdup (g_strerror (errno));
493 gst_multiudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
495 GstMultiUDPSink *sink;
496 gint ret, size, num = 0, no_clients = 0;
501 sink = GST_MULTIUDPSINK (bsink);
503 size = GST_BUFFER_SIZE (buffer);
504 data = GST_BUFFER_DATA (buffer);
506 if (size > UDP_MAX_SIZE) {
507 GST_WARNING ("Attempting to send a UDP packet larger than maximum "
508 "size (%d > %d)", size, UDP_MAX_SIZE);
511 sink->bytes_to_serve += size;
513 /* grab lock while iterating and sending to clients, this should be
514 * fast as UDP never blocks */
515 g_mutex_lock (sink->client_lock);
516 GST_LOG_OBJECT (bsink, "about to send %d bytes", size);
518 for (clients = sink->clients; clients; clients = g_list_next (clients)) {
519 GstUDPClient *client;
522 client = (GstUDPClient *) clients->data;
524 GST_LOG_OBJECT (sink, "sending %d bytes to client %p", size, client);
526 count = sink->send_duplicates ? client->refcount : 1;
530 len = gst_udp_get_sockaddr_length (&client->theiraddr);
532 ret = sendto (*client->sock,
538 size, 0, (struct sockaddr *) &client->theiraddr, len);
541 /* some error, just warn, it's likely recoverable and we don't want to
542 * break streaming. We break so that we stop retrying for this client. */
543 if (!socket_error_is_ignorable ()) {
544 gchar *errormessage = socket_last_error_message ();
545 GST_WARNING_OBJECT (sink, "client %p gave error %d (%s)", client,
546 socket_last_error_code (), errormessage);
547 g_free (errormessage);
552 client->bytes_sent += ret;
553 client->packets_sent++;
554 sink->bytes_served += ret;
560 g_mutex_unlock (sink->client_lock);
562 GST_LOG_OBJECT (sink, "sent %d bytes to %d (of %d) clients", size, num,
570 gst_multiudpsink_render_list (GstBaseSink * bsink, GstBufferList * list)
572 GstMultiUDPSink *sink;
574 gint ret, size = 0, num = 0, no_clients = 0;
576 struct msghdr msg = { 0 };
578 GstBufferListIterator *it;
582 sink = GST_MULTIUDPSINK (bsink);
584 g_return_val_if_fail (list != NULL, GST_FLOW_ERROR);
586 it = gst_buffer_list_iterate (list);
587 g_return_val_if_fail (it != NULL, GST_FLOW_ERROR);
589 while (gst_buffer_list_iterator_next_group (it)) {
593 if ((gsize = gst_buffer_list_iterator_n_buffers (it)) == 0) {
597 iov = (struct iovec *) g_malloc (gsize * sizeof (struct iovec));
600 while ((buf = gst_buffer_list_iterator_next (it))) {
601 if (GST_BUFFER_SIZE (buf) > UDP_MAX_SIZE) {
602 GST_WARNING ("Attempting to send a UDP packet larger than maximum "
603 "size (%d > %d)", GST_BUFFER_SIZE (buf), UDP_MAX_SIZE);
606 msg.msg_iov[msg.msg_iovlen].iov_len = GST_BUFFER_SIZE (buf);
607 msg.msg_iov[msg.msg_iovlen].iov_base = GST_BUFFER_DATA (buf);
609 size += GST_BUFFER_SIZE (buf);
612 sink->bytes_to_serve += size;
614 /* grab lock while iterating and sending to clients, this should be
615 * fast as UDP never blocks */
616 g_mutex_lock (sink->client_lock);
617 GST_LOG_OBJECT (bsink, "about to send %d bytes", size);
619 for (clients = sink->clients; clients; clients = g_list_next (clients)) {
620 GstUDPClient *client;
623 client = (GstUDPClient *) clients->data;
625 GST_LOG_OBJECT (sink, "sending %d bytes to client %p", size, client);
627 count = sink->send_duplicates ? client->refcount : 1;
631 msg.msg_name = (void *) &client->theiraddr;
632 msg.msg_namelen = sizeof (client->theiraddr);
633 ret = sendmsg (*client->sock, &msg, 0);
636 if (!socket_error_is_ignorable ()) {
641 client->bytes_sent += ret;
642 client->packets_sent++;
643 sink->bytes_served += ret;
649 g_mutex_unlock (sink->client_lock);
654 GST_LOG_OBJECT (sink, "sent %d bytes to %d (of %d) clients", size, num,
658 gst_buffer_list_iterator_free (it);
663 gst_buffer_list_iterator_free (it);
664 return GST_FLOW_ERROR;
669 gst_multiudpsink_set_clients_string (GstMultiUDPSink * sink,
670 const gchar * string)
675 clients = g_strsplit (string, ",", 0);
677 g_mutex_lock (sink->client_lock);
678 /* clear all existing clients */
679 gst_multiudpsink_clear_internal (sink, FALSE);
680 for (i = 0; clients[i]; i++) {
685 p = strstr (clients[i], ":");
691 gst_multiudpsink_add_internal (sink, host, port, FALSE);
693 g_mutex_unlock (sink->client_lock);
695 g_strfreev (clients);
699 gst_multiudpsink_get_clients_string (GstMultiUDPSink * sink)
704 str = g_string_new ("");
706 g_mutex_lock (sink->client_lock);
707 clients = sink->clients;
709 GstUDPClient *client;
712 client = (GstUDPClient *) clients->data;
714 clients = g_list_next (clients);
716 count = client->refcount;
718 g_string_append_printf (str, "%s:%d%s", client->host, client->port,
719 (clients || count > 1 ? "," : ""));
722 g_mutex_unlock (sink->client_lock);
724 return g_string_free (str, FALSE);
728 gst_multiudpsink_setup_qos_dscp (GstMultiUDPSink * sink)
732 /* don't touch on -1 */
733 if (sink->qos_dscp < 0)
739 GST_DEBUG_OBJECT (sink, "setting TOS to %d", sink->qos_dscp);
741 /* Extract and shift 6 bits of DSFIELD */
742 tos = (sink->qos_dscp & 0x3f) << 2;
744 if (setsockopt (sink->sock, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0) {
745 gchar *errormessage = socket_last_error_message ();
746 GST_ERROR_OBJECT (sink, "could not set TOS: %s", errormessage);
747 g_free (errormessage);
750 if (setsockopt (sink->sock, IPPROTO_IPV6, IPV6_TCLASS, &tos,
752 gchar *errormessage = socket_last_error_message ();
753 GST_ERROR_OBJECT (sink, "could not set TCLASS: %s", errormessage);
754 g_free (errormessage);
760 gst_multiudpsink_set_property (GObject * object, guint prop_id,
761 const GValue * value, GParamSpec * pspec)
763 GstMultiUDPSink *udpsink;
765 udpsink = GST_MULTIUDPSINK (object);
769 if (udpsink->sockfd >= 0 && udpsink->sockfd != udpsink->sock &&
771 CLOSE_SOCKET (udpsink->sockfd);
772 udpsink->sockfd = g_value_get_int (value);
773 GST_DEBUG_OBJECT (udpsink, "setting SOCKFD to %d", udpsink->sockfd);
776 udpsink->closefd = g_value_get_boolean (value);
779 gst_multiudpsink_set_clients_string (udpsink, g_value_get_string (value));
781 case PROP_AUTO_MULTICAST:
782 udpsink->auto_multicast = g_value_get_boolean (value);
785 udpsink->ttl = g_value_get_int (value);
788 udpsink->ttl_mc = g_value_get_int (value);
791 udpsink->loop = g_value_get_boolean (value);
794 udpsink->qos_dscp = g_value_get_int (value);
795 gst_multiudpsink_setup_qos_dscp (udpsink);
797 case PROP_SEND_DUPLICATES:
798 udpsink->send_duplicates = g_value_get_boolean (value);
800 case PROP_BUFFER_SIZE:
801 udpsink->buffer_size = g_value_get_int (value);
804 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
810 gst_multiudpsink_get_property (GObject * object, guint prop_id, GValue * value,
813 GstMultiUDPSink *udpsink;
815 udpsink = GST_MULTIUDPSINK (object);
818 case PROP_BYTES_TO_SERVE:
819 g_value_set_uint64 (value, udpsink->bytes_to_serve);
821 case PROP_BYTES_SERVED:
822 g_value_set_uint64 (value, udpsink->bytes_served);
825 g_value_set_int (value, udpsink->sockfd);
828 g_value_set_boolean (value, udpsink->closefd);
831 g_value_set_int (value, udpsink->sock);
834 g_value_take_string (value,
835 gst_multiudpsink_get_clients_string (udpsink));
837 case PROP_AUTO_MULTICAST:
838 g_value_set_boolean (value, udpsink->auto_multicast);
841 g_value_set_int (value, udpsink->ttl);
844 g_value_set_int (value, udpsink->ttl_mc);
847 g_value_set_boolean (value, udpsink->loop);
850 g_value_set_int (value, udpsink->qos_dscp);
852 case PROP_SEND_DUPLICATES:
853 g_value_set_boolean (value, udpsink->send_duplicates);
855 case PROP_BUFFER_SIZE:
856 g_value_set_int (value, udpsink->buffer_size);
859 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
865 gst_multiudpsink_configure_client (GstMultiUDPSink * sink,
866 GstUDPClient * client)
868 GST_DEBUG_OBJECT (sink, "configuring client %p", client);
870 if (gst_udp_is_multicast (&client->theiraddr)) {
871 GST_DEBUG_OBJECT (sink, "we have a multicast client %p", client);
872 if (sink->auto_multicast) {
873 GST_DEBUG_OBJECT (sink, "autojoining group");
874 if (gst_udp_join_group (*(client->sock), &client->theiraddr, NULL)
876 goto join_group_failed;
878 GST_DEBUG_OBJECT (sink, "setting loop to %d", sink->loop);
879 if (gst_udp_set_loop (sink->sock, sink->ss_family, sink->loop) != 0)
881 GST_DEBUG_OBJECT (sink, "setting ttl to %d", sink->ttl_mc);
882 if (gst_udp_set_ttl (sink->sock, sink->ss_family, sink->ttl_mc, TRUE) != 0)
885 GST_DEBUG_OBJECT (sink, "setting unicast ttl to %d", sink->ttl);
886 if (gst_udp_set_ttl (sink->sock, sink->ss_family, sink->ttl, FALSE) != 0)
894 gchar *errormessage = socket_last_error_message ();
895 int errorcode = socket_last_error_code ();
896 CLOSE_IF_REQUESTED (sink);
897 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
898 ("Could not join multicast group (%d): %s", errorcode, errormessage));
899 g_free (errormessage);
904 gchar *errormessage = socket_last_error_message ();
905 int errorcode = socket_last_error_code ();
906 CLOSE_IF_REQUESTED (sink);
907 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
908 ("Could not set TTL socket option (%d): %s", errorcode, errormessage));
909 g_free (errormessage);
914 gchar *errormessage = socket_last_error_message ();
915 int errorcode = socket_last_error_code ();
916 CLOSE_IF_REQUESTED (sink);
917 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
918 ("Could not set loopback socket option (%d): %s",
919 errorcode, errormessage));
920 g_free (errormessage);
925 /* create a socket for sending to remote machine */
927 gst_multiudpsink_init_send (GstMultiUDPSink * sink)
931 GstUDPClient *client;
935 if (sink->sockfd == -1) {
936 GST_DEBUG_OBJECT (sink, "creating sockets");
937 /* create sender socket try IP6, fall back to IP4 */
938 sink->ss_family = AF_INET6;
939 if ((sink->sock = socket (AF_INET6, SOCK_DGRAM, 0)) == -1) {
940 sink->ss_family = AF_INET;
941 if ((sink->sock = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
945 GST_DEBUG_OBJECT (sink, "have socket");
946 sink->externalfd = FALSE;
948 struct sockaddr_storage myaddr;
950 GST_DEBUG_OBJECT (sink, "using configured socket");
951 /* we use the configured socket, try to get some info about it */
952 len = sizeof (myaddr);
953 if (getsockname (sink->sockfd, (struct sockaddr *) &myaddr, &len) < 0)
954 goto getsockname_error;
956 sink->ss_family = myaddr.ss_family;
957 /* we use the configured socket */
958 sink->sock = sink->sockfd;
959 sink->externalfd = TRUE;
962 len = sizeof (sndsize);
963 if (sink->buffer_size != 0) {
964 sndsize = sink->buffer_size;
966 GST_DEBUG_OBJECT (sink, "setting udp buffer of %d bytes", sndsize);
967 /* set buffer size, Note that on Linux this is typically limited to a
968 * maximum of around 100K. Also a minimum of 128 bytes is required on
971 setsockopt (sink->sockfd, SOL_SOCKET, SO_SNDBUF, (void *) &sndsize,
974 GST_ELEMENT_WARNING (sink, RESOURCE, SETTINGS, (NULL),
975 ("Could not create a buffer of requested %d bytes, %d: %s (%d)",
976 sndsize, ret, g_strerror (errno), errno));
980 /* read the value of the receive buffer. Note that on linux this returns 2x the
981 * value we set because the kernel allocates extra memory for metadata.
982 * The default on Linux is about 100K (which is about 50K without metadata) */
984 getsockopt (sink->sockfd, SOL_SOCKET, SO_SNDBUF, (void *) &sndsize, &len);
986 GST_DEBUG_OBJECT (sink, "have udp buffer of %d bytes", sndsize);
988 GST_DEBUG_OBJECT (sink, "could not get udp buffer size");
992 if (setsockopt (sink->sock, SOL_SOCKET, SO_BROADCAST, &bc_val,
993 sizeof (bc_val)) < 0)
996 sink->bytes_to_serve = 0;
997 sink->bytes_served = 0;
999 gst_multiudpsink_setup_qos_dscp (sink);
1001 /* look for multicast clients and join multicast groups appropriately
1002 set also ttl and multicast loopback delivery appropriately */
1003 for (clients = sink->clients; clients; clients = g_list_next (clients)) {
1004 client = (GstUDPClient *) clients->data;
1006 if (!gst_multiudpsink_configure_client (sink, client))
1014 gchar *errormessage = socket_last_error_message ();
1015 int errorcode = socket_last_error_code ();
1016 GST_ELEMENT_ERROR (sink, RESOURCE, FAILED, (NULL),
1017 ("Could not create socket (%d): %s", errorcode, errormessage));
1018 g_free (errormessage);
1023 gchar *errormessage = socket_last_error_message ();
1024 int errorcode = socket_last_error_code ();
1025 GST_ELEMENT_ERROR (sink, RESOURCE, FAILED, (NULL),
1026 ("Could not getsockname (%d): %s", errorcode, errormessage));
1027 g_free (errormessage);
1032 gchar *errormessage = socket_last_error_message ();
1033 int errorcode = socket_last_error_code ();
1034 CLOSE_IF_REQUESTED (sink);
1035 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
1036 ("Could not set broadcast socket option (%d): %s",
1037 errorcode, errormessage));
1038 g_free (errormessage);
1044 gst_multiudpsink_close (GstMultiUDPSink * sink)
1046 CLOSE_IF_REQUESTED (sink);
1050 gst_multiudpsink_add_internal (GstMultiUDPSink * sink, const gchar * host,
1051 gint port, gboolean lock)
1053 GstUDPClient *client;
1054 GstUDPClient udpclient;
1058 udpclient.host = (gchar *) host;
1059 udpclient.port = port;
1061 GST_DEBUG_OBJECT (sink, "adding client on host %s, port %d", host, port);
1064 g_mutex_lock (sink->client_lock);
1066 find = g_list_find_custom (sink->clients, &udpclient,
1067 (GCompareFunc) client_compare);
1069 client = (GstUDPClient *) find->data;
1071 GST_DEBUG_OBJECT (sink, "found %d existing clients with host %s, port %d",
1072 client->refcount, host, port);
1075 client = create_client (sink, host, port);
1077 client->sock = &sink->sock;
1079 if (gst_udp_get_addr (host, port, &client->theiraddr) < 0)
1080 goto getaddrinfo_error;
1082 g_get_current_time (&now);
1083 client->connect_time = GST_TIMEVAL_TO_TIME (now);
1085 if (*client->sock > 0) {
1086 gst_multiudpsink_configure_client (sink, client);
1089 GST_DEBUG_OBJECT (sink, "add client with host %s, port %d", host, port);
1090 sink->clients = g_list_prepend (sink->clients, client);
1094 g_mutex_unlock (sink->client_lock);
1096 g_signal_emit (G_OBJECT (sink),
1097 gst_multiudpsink_signals[SIGNAL_CLIENT_ADDED], 0, host, port);
1099 GST_DEBUG_OBJECT (sink, "added client on host %s, port %d", host, port);
1105 GST_DEBUG_OBJECT (sink, "did not add client on host %s, port %d", host,
1107 GST_WARNING_OBJECT (sink, "getaddrinfo lookup error?");
1108 free_client (client);
1110 g_mutex_unlock (sink->client_lock);
1116 gst_multiudpsink_add (GstMultiUDPSink * sink, const gchar * host, gint port)
1118 gst_multiudpsink_add_internal (sink, host, port, TRUE);
1122 gst_multiudpsink_remove (GstMultiUDPSink * sink, const gchar * host, gint port)
1125 GstUDPClient udpclient;
1126 GstUDPClient *client;
1129 udpclient.host = (gchar *) host;
1130 udpclient.port = port;
1132 g_mutex_lock (sink->client_lock);
1133 find = g_list_find_custom (sink->clients, &udpclient,
1134 (GCompareFunc) client_compare);
1138 client = (GstUDPClient *) find->data;
1140 GST_DEBUG_OBJECT (sink, "found %d clients with host %s, port %d",
1141 client->refcount, host, port);
1144 if (client->refcount == 0) {
1145 GST_DEBUG_OBJECT (sink, "remove client with host %s, port %d", host, port);
1147 g_get_current_time (&now);
1148 client->disconnect_time = GST_TIMEVAL_TO_TIME (now);
1150 if (*(client->sock) != -1 && sink->auto_multicast
1151 && gst_udp_is_multicast (&client->theiraddr))
1152 gst_udp_leave_group (*(client->sock), &client->theiraddr);
1154 /* Unlock to emit signal before we delete the actual client */
1155 g_mutex_unlock (sink->client_lock);
1156 g_signal_emit (G_OBJECT (sink),
1157 gst_multiudpsink_signals[SIGNAL_CLIENT_REMOVED], 0, host, port);
1158 g_mutex_lock (sink->client_lock);
1160 sink->clients = g_list_delete_link (sink->clients, find);
1162 free_client (client);
1164 g_mutex_unlock (sink->client_lock);
1171 g_mutex_unlock (sink->client_lock);
1172 GST_WARNING_OBJECT (sink, "client at host %s, port %d not found",
1179 gst_multiudpsink_clear_internal (GstMultiUDPSink * sink, gboolean lock)
1181 GST_DEBUG_OBJECT (sink, "clearing");
1182 /* we only need to remove the client structure, there is no additional
1183 * socket or anything to free for UDP */
1185 g_mutex_lock (sink->client_lock);
1186 g_list_foreach (sink->clients, (GFunc) free_client, sink);
1187 g_list_free (sink->clients);
1188 sink->clients = NULL;
1190 g_mutex_unlock (sink->client_lock);
1194 gst_multiudpsink_clear (GstMultiUDPSink * sink)
1196 gst_multiudpsink_clear_internal (sink, TRUE);
1200 gst_multiudpsink_get_stats (GstMultiUDPSink * sink, const gchar * host,
1203 GstUDPClient *client;
1204 GValueArray *result = NULL;
1205 GstUDPClient udpclient;
1207 GValue value = { 0 };
1209 udpclient.host = (gchar *) host;
1210 udpclient.port = port;
1212 g_mutex_lock (sink->client_lock);
1214 find = g_list_find_custom (sink->clients, &udpclient,
1215 (GCompareFunc) client_compare);
1219 GST_DEBUG_OBJECT (sink, "stats for client with host %s, port %d", host, port);
1221 client = (GstUDPClient *) find->data;
1223 /* Result is a value array of (bytes_sent, packets_sent,
1224 * connect_time, disconnect_time), all as uint64 */
1225 result = g_value_array_new (4);
1227 g_value_init (&value, G_TYPE_UINT64);
1228 g_value_set_uint64 (&value, client->bytes_sent);
1229 result = g_value_array_append (result, &value);
1230 g_value_unset (&value);
1232 g_value_init (&value, G_TYPE_UINT64);
1233 g_value_set_uint64 (&value, client->packets_sent);
1234 result = g_value_array_append (result, &value);
1235 g_value_unset (&value);
1237 g_value_init (&value, G_TYPE_UINT64);
1238 g_value_set_uint64 (&value, client->connect_time);
1239 result = g_value_array_append (result, &value);
1240 g_value_unset (&value);
1242 g_value_init (&value, G_TYPE_UINT64);
1243 g_value_set_uint64 (&value, client->disconnect_time);
1244 result = g_value_array_append (result, &value);
1245 g_value_unset (&value);
1247 g_mutex_unlock (sink->client_lock);
1254 g_mutex_unlock (sink->client_lock);
1255 GST_WARNING_OBJECT (sink, "client with host %s, port %d not found",
1257 /* Apparently (see comment in gstmultifdsink.c) returning NULL from here may
1258 * confuse/break python bindings */
1259 return g_value_array_new (0);
1263 static GstStateChangeReturn
1264 gst_multiudpsink_change_state (GstElement * element, GstStateChange transition)
1266 GstStateChangeReturn ret;
1267 GstMultiUDPSink *sink;
1269 sink = GST_MULTIUDPSINK (element);
1271 switch (transition) {
1272 case GST_STATE_CHANGE_READY_TO_PAUSED:
1273 if (!gst_multiudpsink_init_send (sink))
1280 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1282 switch (transition) {
1283 case GST_STATE_CHANGE_PAUSED_TO_READY:
1284 gst_multiudpsink_close (sink);
1294 /* _init_send() posted specific error already */
1295 return GST_STATE_CHANGE_FAILURE;