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 GST_DEBUG_CATEGORY_STATIC (multiudpsink_debug);
45 #define GST_CAT_DEFAULT (multiudpsink_debug)
47 #define UDP_MAX_SIZE 65507
49 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
54 /* MultiUDPSink signals and args */
65 SIGNAL_CLIENT_REMOVED,
71 #define DEFAULT_SOCKFD -1
72 #define DEFAULT_CLOSEFD TRUE
73 #define DEFAULT_SOCK -1
74 #define DEFAULT_CLIENTS NULL
75 #define DEFAULT_FAMILY 0
76 /* FIXME, this should be disabled by default, we don't need to join a multicast
77 * group for sending, if this socket is also used for receiving, it should
78 * be configured in the element that does the receive. */
79 #define DEFAULT_AUTO_MULTICAST TRUE
80 #define DEFAULT_TTL 64
81 #define DEFAULT_TTL_MC 1
82 #define DEFAULT_LOOP TRUE
83 #define DEFAULT_QOS_DSCP -1
84 #define DEFAULT_SEND_DUPLICATES TRUE
85 #define DEFAULT_BUFFER_SIZE 0
101 PROP_SEND_DUPLICATES,
106 #define CLOSE_IF_REQUESTED(udpctx) \
108 if ((!udpctx->externalfd) || (udpctx->externalfd && udpctx->closefd)) { \
109 CLOSE_SOCKET(udpctx->sock); \
110 if (udpctx->sock == udpctx->sockfd) \
111 udpctx->sockfd = DEFAULT_SOCKFD; \
113 udpctx->sock = DEFAULT_SOCK; \
116 static void gst_multiudpsink_base_init (gpointer g_class);
117 static void gst_multiudpsink_class_init (GstMultiUDPSinkClass * klass);
118 static void gst_multiudpsink_init (GstMultiUDPSink * udpsink);
119 static void gst_multiudpsink_finalize (GObject * object);
121 static GstFlowReturn gst_multiudpsink_render (GstBaseSink * sink,
123 #ifndef G_OS_WIN32 /* sendmsg() is not available on Windows */
124 static GstFlowReturn gst_multiudpsink_render_list (GstBaseSink * bsink,
125 GstBufferList * list);
127 static GstStateChangeReturn gst_multiudpsink_change_state (GstElement *
128 element, GstStateChange transition);
130 static void gst_multiudpsink_set_property (GObject * object, guint prop_id,
131 const GValue * value, GParamSpec * pspec);
132 static void gst_multiudpsink_get_property (GObject * object, guint prop_id,
133 GValue * value, GParamSpec * pspec);
135 static void gst_multiudpsink_add_internal (GstMultiUDPSink * sink,
136 const gchar * host, gint port, gboolean lock);
137 static void gst_multiudpsink_clear_internal (GstMultiUDPSink * sink,
140 static GstElementClass *parent_class = NULL;
142 static guint gst_multiudpsink_signals[LAST_SIGNAL] = { 0 };
145 gst_multiudpsink_get_type (void)
147 static GType multiudpsink_type = 0;
149 if (!multiudpsink_type) {
150 static const GTypeInfo multiudpsink_info = {
151 sizeof (GstMultiUDPSinkClass),
152 gst_multiudpsink_base_init,
154 (GClassInitFunc) gst_multiudpsink_class_init,
157 sizeof (GstMultiUDPSink),
159 (GInstanceInitFunc) gst_multiudpsink_init,
164 g_type_register_static (GST_TYPE_BASE_SINK, "GstMultiUDPSink",
165 &multiudpsink_info, 0);
167 return multiudpsink_type;
171 gst_multiudpsink_base_init (gpointer g_class)
173 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
175 gst_element_class_add_static_pad_template (element_class,
178 gst_element_class_set_details_simple (element_class, "UDP packet sender",
180 "Send data over the network via UDP",
181 "Wim Taymans <wim.taymans@gmail.com>");
185 gst_multiudpsink_class_init (GstMultiUDPSinkClass * klass)
187 GObjectClass *gobject_class;
188 GstElementClass *gstelement_class;
189 GstBaseSinkClass *gstbasesink_class;
191 gobject_class = (GObjectClass *) klass;
192 gstelement_class = (GstElementClass *) klass;
193 gstbasesink_class = (GstBaseSinkClass *) klass;
195 parent_class = g_type_class_peek_parent (klass);
197 gobject_class->set_property = gst_multiudpsink_set_property;
198 gobject_class->get_property = gst_multiudpsink_get_property;
199 gobject_class->finalize = gst_multiudpsink_finalize;
202 * GstMultiUDPSink::add:
203 * @gstmultiudpsink: the sink on which the signal is emitted
204 * @host: the hostname/IP address of the client to add
205 * @port: the port of the client to add
207 * Add a client with destination @host and @port to the list of
208 * clients. When the same host/port pair is added multiple times, the
209 * send-duplicates property defines if the packets are sent multiple times to
210 * the same host/port pair or not.
212 * When a host/port pair is added multiple times, an equal amount of remove
213 * calls must be performed to actually remove the host/port pair from the list
216 gst_multiudpsink_signals[SIGNAL_ADD] =
217 g_signal_new ("add", G_TYPE_FROM_CLASS (klass),
218 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
219 G_STRUCT_OFFSET (GstMultiUDPSinkClass, add),
220 NULL, NULL, gst_udp_marshal_VOID__STRING_INT, G_TYPE_NONE, 2,
221 G_TYPE_STRING, G_TYPE_INT);
223 * GstMultiUDPSink::remove:
224 * @gstmultiudpsink: the sink on which the signal is emitted
225 * @host: the hostname/IP address of the client to remove
226 * @port: the port of the client to remove
228 * Remove the client with destination @host and @port from the list of
231 gst_multiudpsink_signals[SIGNAL_REMOVE] =
232 g_signal_new ("remove", G_TYPE_FROM_CLASS (klass),
233 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
234 G_STRUCT_OFFSET (GstMultiUDPSinkClass, remove),
235 NULL, NULL, gst_udp_marshal_VOID__STRING_INT, G_TYPE_NONE, 2,
236 G_TYPE_STRING, G_TYPE_INT);
238 * GstMultiUDPSink::clear:
239 * @gstmultiudpsink: the sink on which the signal is emitted
241 * Clear the list of clients.
243 gst_multiudpsink_signals[SIGNAL_CLEAR] =
244 g_signal_new ("clear", G_TYPE_FROM_CLASS (klass),
245 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
246 G_STRUCT_OFFSET (GstMultiUDPSinkClass, clear),
247 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
249 * GstMultiUDPSink::get-stats:
250 * @gstmultiudpsink: the sink on which the signal is emitted
251 * @host: the hostname/IP address of the client to get stats on
252 * @port: the port of the client to get stats on
254 * Get the statistics of the client with destination @host and @port.
256 * Returns: a GValueArray of uint64: bytes_sent, packets_sent,
257 * connect_time (in epoch seconds), disconnect_time (in epoch seconds)
259 gst_multiudpsink_signals[SIGNAL_GET_STATS] =
260 g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass),
261 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
262 G_STRUCT_OFFSET (GstMultiUDPSinkClass, get_stats),
263 NULL, NULL, gst_udp_marshal_BOXED__STRING_INT, G_TYPE_VALUE_ARRAY, 2,
264 G_TYPE_STRING, G_TYPE_INT);
266 * GstMultiUDPSink::client-added:
267 * @gstmultiudpsink: the sink emitting the signal
268 * @host: the hostname/IP address of the added client
269 * @port: the port of the added client
271 * Signal emited when a new client is added to the list of
274 gst_multiudpsink_signals[SIGNAL_CLIENT_ADDED] =
275 g_signal_new ("client-added", G_TYPE_FROM_CLASS (klass),
276 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiUDPSinkClass, client_added),
277 NULL, NULL, gst_udp_marshal_VOID__STRING_INT, G_TYPE_NONE, 2,
278 G_TYPE_STRING, G_TYPE_INT);
280 * GstMultiUDPSink::client-removed:
281 * @gstmultiudpsink: the sink emitting the signal
282 * @host: the hostname/IP address of the removed client
283 * @port: the port of the removed client
285 * Signal emited when a client is removed from the list of
288 gst_multiudpsink_signals[SIGNAL_CLIENT_REMOVED] =
289 g_signal_new ("client-removed", G_TYPE_FROM_CLASS (klass),
290 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiUDPSinkClass,
291 client_removed), NULL, NULL, gst_udp_marshal_VOID__STRING_INT,
292 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
294 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTES_TO_SERVE,
295 g_param_spec_uint64 ("bytes-to-serve", "Bytes to serve",
296 "Number of bytes received to serve to clients", 0, G_MAXUINT64, 0,
297 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
298 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTES_SERVED,
299 g_param_spec_uint64 ("bytes-served", "Bytes served",
300 "Total number of bytes send to all clients", 0, G_MAXUINT64, 0,
301 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
302 g_object_class_install_property (gobject_class, PROP_SOCKFD,
303 g_param_spec_int ("sockfd", "Socket Handle",
304 "Socket to use for UDP sending. (-1 == allocate)",
305 -1, G_MAXINT, DEFAULT_SOCKFD,
306 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
307 g_object_class_install_property (gobject_class, PROP_CLOSEFD,
308 g_param_spec_boolean ("closefd", "Close sockfd",
309 "Close sockfd if passed as property on state change",
310 DEFAULT_CLOSEFD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
311 g_object_class_install_property (gobject_class, PROP_SOCK,
312 g_param_spec_int ("sock", "Socket Handle",
313 "Socket currently in use for UDP sending. (-1 == no socket)",
314 -1, G_MAXINT, DEFAULT_SOCK,
315 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
316 g_object_class_install_property (gobject_class, PROP_CLIENTS,
317 g_param_spec_string ("clients", "Clients",
318 "A comma separated list of host:port pairs with destinations",
319 DEFAULT_CLIENTS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
320 g_object_class_install_property (gobject_class, PROP_AUTO_MULTICAST,
321 g_param_spec_boolean ("auto-multicast",
322 "Automatically join/leave multicast groups",
323 "Automatically join/leave the multicast groups, FALSE means user"
324 " has to do it himself", DEFAULT_AUTO_MULTICAST,
325 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326 g_object_class_install_property (gobject_class, PROP_TTL,
327 g_param_spec_int ("ttl", "Unicast TTL",
328 "Used for setting the unicast TTL parameter",
329 0, 255, DEFAULT_TTL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
330 g_object_class_install_property (gobject_class, PROP_TTL_MC,
331 g_param_spec_int ("ttl-mc", "Multicast TTL",
332 "Used for setting the multicast TTL parameter",
333 0, 255, DEFAULT_TTL_MC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
334 g_object_class_install_property (gobject_class, PROP_LOOP,
335 g_param_spec_boolean ("loop", "Multicast Loopback",
336 "Used for setting the multicast loop parameter. TRUE = enable,"
337 " FALSE = disable", DEFAULT_LOOP,
338 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
339 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QOS_DSCP,
340 g_param_spec_int ("qos-dscp", "QoS diff srv code point",
341 "Quality of Service, differentiated services code point (-1 default)",
342 -1, 63, DEFAULT_QOS_DSCP,
343 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
345 * GstMultiUDPSink::send-duplicates
347 * When a host/port pair is added mutliple times, send the packet to the host
348 * multiple times as well.
352 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEND_DUPLICATES,
353 g_param_spec_boolean ("send-duplicates", "Send Duplicates",
354 "When a distination/port pair is added multiple times, send packets "
355 "multiple times as well", DEFAULT_SEND_DUPLICATES,
356 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
358 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_SIZE,
359 g_param_spec_int ("buffer-size", "Buffer Size",
360 "Size of the kernel send buffer in bytes, 0=default", 0, G_MAXINT,
361 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
363 gstelement_class->change_state = gst_multiudpsink_change_state;
365 gstbasesink_class->render = gst_multiudpsink_render;
367 gstbasesink_class->render_list = gst_multiudpsink_render_list;
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");
379 gst_multiudpsink_init (GstMultiUDPSink * sink)
383 sink->client_lock = g_mutex_new ();
384 sink->sock = DEFAULT_SOCK;
385 sink->sockfd = DEFAULT_SOCKFD;
386 sink->closefd = DEFAULT_CLOSEFD;
387 sink->externalfd = (sink->sockfd != -1);
388 sink->auto_multicast = DEFAULT_AUTO_MULTICAST;
389 sink->ttl = DEFAULT_TTL;
390 sink->ttl_mc = DEFAULT_TTL_MC;
391 sink->loop = DEFAULT_LOOP;
392 sink->qos_dscp = DEFAULT_QOS_DSCP;
393 sink->ss_family = DEFAULT_FAMILY;
394 sink->send_duplicates = DEFAULT_SEND_DUPLICATES;
397 static GstUDPClient *
398 create_client (GstMultiUDPSink * sink, const gchar * host, gint port)
400 GstUDPClient *client;
402 client = g_slice_new0 (GstUDPClient);
403 client->refcount = 1;
404 client->host = g_strdup (host);
411 free_client (GstUDPClient * client)
413 g_free (client->host);
414 g_slice_free (GstUDPClient, client);
418 client_compare (GstUDPClient * a, GstUDPClient * b)
420 if ((a->port == b->port) && (strcmp (a->host, b->host) == 0))
427 gst_multiudpsink_finalize (GObject * object)
429 GstMultiUDPSink *sink;
431 sink = GST_MULTIUDPSINK (object);
433 g_list_foreach (sink->clients, (GFunc) free_client, NULL);
434 g_list_free (sink->clients);
436 if (sink->sockfd >= 0 && sink->closefd)
437 CLOSE_SOCKET (sink->sockfd);
439 g_mutex_free (sink->client_lock);
441 WSA_CLEANUP (object);
443 G_OBJECT_CLASS (parent_class)->finalize (object);
447 socket_error_is_ignorable (void)
450 /* Windows doesn't seem to have an EAGAIN for sockets */
451 return WSAGetLastError () == WSAEINTR;
453 return errno == EINTR || errno == EAGAIN;
458 socket_last_error_code (void)
461 return WSAGetLastError ();
468 socket_last_error_message (void)
471 int errorcode = WSAGetLastError ();
474 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
475 NULL, errorcode, 0, (LPSTR) buf, sizeof (buf) / sizeof (wchar_t), NULL);
476 if (FAILED (result)) {
477 return g_strdup ("failed to get error message from system");
480 g_convert ((gchar *) buf, -1, "UTF-16", "UTF-8", NULL, NULL, NULL);
481 /* g_convert() internally calls windows functions which reset the
482 windows error code, so fix it up again like this */
483 WSASetLastError (errorcode);
487 return g_strdup (g_strerror (errno));
492 gst_multiudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
494 GstMultiUDPSink *sink;
495 gint ret, size, num = 0, no_clients = 0;
500 sink = GST_MULTIUDPSINK (bsink);
502 size = GST_BUFFER_SIZE (buffer);
503 data = GST_BUFFER_DATA (buffer);
505 if (size > UDP_MAX_SIZE) {
506 GST_WARNING ("Attempting to send a UDP packet larger than maximum "
507 "size (%d > %d)", size, UDP_MAX_SIZE);
510 sink->bytes_to_serve += size;
512 /* grab lock while iterating and sending to clients, this should be
513 * fast as UDP never blocks */
514 g_mutex_lock (sink->client_lock);
515 GST_LOG_OBJECT (bsink, "about to send %d bytes", size);
517 for (clients = sink->clients; clients; clients = g_list_next (clients)) {
518 GstUDPClient *client;
521 client = (GstUDPClient *) clients->data;
523 GST_LOG_OBJECT (sink, "sending %d bytes to client %p", size, client);
525 count = sink->send_duplicates ? client->refcount : 1;
529 len = gst_udp_get_sockaddr_length (&client->theiraddr);
531 ret = sendto (*client->sock,
537 size, 0, (struct sockaddr *) &client->theiraddr, len);
540 /* some error, just warn, it's likely recoverable and we don't want to
541 * break streaming. We break so that we stop retrying for this client. */
542 if (!socket_error_is_ignorable ()) {
543 gchar *errormessage = socket_last_error_message ();
544 GST_WARNING_OBJECT (sink, "client %p gave error %d (%s)", client,
545 socket_last_error_code (), errormessage);
546 g_free (errormessage);
551 client->bytes_sent += ret;
552 client->packets_sent++;
553 sink->bytes_served += ret;
559 g_mutex_unlock (sink->client_lock);
561 GST_LOG_OBJECT (sink, "sent %d bytes to %d (of %d) clients", size, num,
569 gst_multiudpsink_render_list (GstBaseSink * bsink, GstBufferList * list)
571 GstMultiUDPSink *sink;
573 gint ret, size = 0, num = 0, no_clients = 0;
575 struct msghdr msg = { 0 };
577 GstBufferListIterator *it;
581 sink = GST_MULTIUDPSINK (bsink);
583 g_return_val_if_fail (list != NULL, GST_FLOW_ERROR);
585 it = gst_buffer_list_iterate (list);
586 g_return_val_if_fail (it != NULL, GST_FLOW_ERROR);
588 while (gst_buffer_list_iterator_next_group (it)) {
592 if ((gsize = gst_buffer_list_iterator_n_buffers (it)) == 0) {
596 iov = (struct iovec *) g_malloc (gsize * sizeof (struct iovec));
599 while ((buf = gst_buffer_list_iterator_next (it))) {
600 if (GST_BUFFER_SIZE (buf) > UDP_MAX_SIZE) {
601 GST_WARNING ("Attempting to send a UDP packet larger than maximum "
602 "size (%d > %d)", GST_BUFFER_SIZE (buf), UDP_MAX_SIZE);
605 msg.msg_iov[msg.msg_iovlen].iov_len = GST_BUFFER_SIZE (buf);
606 msg.msg_iov[msg.msg_iovlen].iov_base = GST_BUFFER_DATA (buf);
608 size += GST_BUFFER_SIZE (buf);
611 sink->bytes_to_serve += size;
613 /* grab lock while iterating and sending to clients, this should be
614 * fast as UDP never blocks */
615 g_mutex_lock (sink->client_lock);
616 GST_LOG_OBJECT (bsink, "about to send %d bytes", size);
618 for (clients = sink->clients; clients; clients = g_list_next (clients)) {
619 GstUDPClient *client;
622 client = (GstUDPClient *) clients->data;
624 GST_LOG_OBJECT (sink, "sending %d bytes to client %p", size, client);
626 count = sink->send_duplicates ? client->refcount : 1;
630 msg.msg_name = (void *) &client->theiraddr;
631 msg.msg_namelen = sizeof (client->theiraddr);
632 ret = sendmsg (*client->sock, &msg, 0);
635 if (!socket_error_is_ignorable ()) {
640 client->bytes_sent += ret;
641 client->packets_sent++;
642 sink->bytes_served += ret;
648 g_mutex_unlock (sink->client_lock);
653 GST_LOG_OBJECT (sink, "sent %d bytes to %d (of %d) clients", size, num,
657 gst_buffer_list_iterator_free (it);
662 gst_buffer_list_iterator_free (it);
663 return GST_FLOW_ERROR;
668 gst_multiudpsink_set_clients_string (GstMultiUDPSink * sink,
669 const gchar * string)
674 clients = g_strsplit (string, ",", 0);
676 g_mutex_lock (sink->client_lock);
677 /* clear all existing clients */
678 gst_multiudpsink_clear_internal (sink, FALSE);
679 for (i = 0; clients[i]; i++) {
684 p = strstr (clients[i], ":");
690 gst_multiudpsink_add_internal (sink, host, port, FALSE);
692 g_mutex_unlock (sink->client_lock);
694 g_strfreev (clients);
698 gst_multiudpsink_get_clients_string (GstMultiUDPSink * sink)
703 str = g_string_new ("");
705 g_mutex_lock (sink->client_lock);
706 clients = sink->clients;
708 GstUDPClient *client;
711 client = (GstUDPClient *) clients->data;
713 clients = g_list_next (clients);
715 count = client->refcount;
717 g_string_append_printf (str, "%s:%d%s", client->host, client->port,
718 (clients || count > 1 ? "," : ""));
721 g_mutex_unlock (sink->client_lock);
723 return g_string_free (str, FALSE);
727 gst_multiudpsink_setup_qos_dscp (GstMultiUDPSink * sink)
731 /* don't touch on -1 */
732 if (sink->qos_dscp < 0)
738 GST_DEBUG_OBJECT (sink, "setting TOS to %d", sink->qos_dscp);
740 /* Extract and shift 6 bits of DSFIELD */
741 tos = (sink->qos_dscp & 0x3f) << 2;
743 if (setsockopt (sink->sock, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0) {
744 gchar *errormessage = socket_last_error_message ();
745 GST_ERROR_OBJECT (sink, "could not set TOS: %s", errormessage);
746 g_free (errormessage);
749 if (setsockopt (sink->sock, IPPROTO_IPV6, IPV6_TCLASS, &tos,
751 gchar *errormessage = socket_last_error_message ();
752 GST_ERROR_OBJECT (sink, "could not set TCLASS: %s", errormessage);
753 g_free (errormessage);
759 gst_multiudpsink_set_property (GObject * object, guint prop_id,
760 const GValue * value, GParamSpec * pspec)
762 GstMultiUDPSink *udpsink;
764 udpsink = GST_MULTIUDPSINK (object);
768 if (udpsink->sockfd >= 0 && udpsink->sockfd != udpsink->sock &&
770 CLOSE_SOCKET (udpsink->sockfd);
771 udpsink->sockfd = g_value_get_int (value);
772 GST_DEBUG_OBJECT (udpsink, "setting SOCKFD to %d", udpsink->sockfd);
775 udpsink->closefd = g_value_get_boolean (value);
778 gst_multiudpsink_set_clients_string (udpsink, g_value_get_string (value));
780 case PROP_AUTO_MULTICAST:
781 udpsink->auto_multicast = g_value_get_boolean (value);
784 udpsink->ttl = g_value_get_int (value);
787 udpsink->ttl_mc = g_value_get_int (value);
790 udpsink->loop = g_value_get_boolean (value);
793 udpsink->qos_dscp = g_value_get_int (value);
794 gst_multiudpsink_setup_qos_dscp (udpsink);
796 case PROP_SEND_DUPLICATES:
797 udpsink->send_duplicates = g_value_get_boolean (value);
799 case PROP_BUFFER_SIZE:
800 udpsink->buffer_size = g_value_get_int (value);
803 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
809 gst_multiudpsink_get_property (GObject * object, guint prop_id, GValue * value,
812 GstMultiUDPSink *udpsink;
814 udpsink = GST_MULTIUDPSINK (object);
817 case PROP_BYTES_TO_SERVE:
818 g_value_set_uint64 (value, udpsink->bytes_to_serve);
820 case PROP_BYTES_SERVED:
821 g_value_set_uint64 (value, udpsink->bytes_served);
824 g_value_set_int (value, udpsink->sockfd);
827 g_value_set_boolean (value, udpsink->closefd);
830 g_value_set_int (value, udpsink->sock);
833 g_value_take_string (value,
834 gst_multiudpsink_get_clients_string (udpsink));
836 case PROP_AUTO_MULTICAST:
837 g_value_set_boolean (value, udpsink->auto_multicast);
840 g_value_set_int (value, udpsink->ttl);
843 g_value_set_int (value, udpsink->ttl_mc);
846 g_value_set_boolean (value, udpsink->loop);
849 g_value_set_int (value, udpsink->qos_dscp);
851 case PROP_SEND_DUPLICATES:
852 g_value_set_boolean (value, udpsink->send_duplicates);
854 case PROP_BUFFER_SIZE:
855 g_value_set_int (value, udpsink->buffer_size);
858 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
864 gst_multiudpsink_configure_client (GstMultiUDPSink * sink,
865 GstUDPClient * client)
867 GST_DEBUG_OBJECT (sink, "configuring client %p", client);
869 if (gst_udp_is_multicast (&client->theiraddr)) {
870 GST_DEBUG_OBJECT (sink, "we have a multicast client %p", client);
871 if (sink->auto_multicast) {
872 GST_DEBUG_OBJECT (sink, "autojoining group");
873 if (gst_udp_join_group (*(client->sock), &client->theiraddr, NULL)
875 goto join_group_failed;
877 GST_DEBUG_OBJECT (sink, "setting loop to %d", sink->loop);
878 if (gst_udp_set_loop (sink->sock, sink->ss_family, sink->loop) != 0)
880 GST_DEBUG_OBJECT (sink, "setting ttl to %d", sink->ttl_mc);
881 if (gst_udp_set_ttl (sink->sock, sink->ss_family, sink->ttl_mc, TRUE) != 0)
884 GST_DEBUG_OBJECT (sink, "setting unicast ttl to %d", sink->ttl);
885 if (gst_udp_set_ttl (sink->sock, sink->ss_family, sink->ttl, FALSE) != 0)
893 gchar *errormessage = socket_last_error_message ();
894 int errorcode = socket_last_error_code ();
895 CLOSE_IF_REQUESTED (sink);
896 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
897 ("Could not join multicast group (%d): %s", errorcode, errormessage));
898 g_free (errormessage);
903 gchar *errormessage = socket_last_error_message ();
904 int errorcode = socket_last_error_code ();
905 CLOSE_IF_REQUESTED (sink);
906 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
907 ("Could not set TTL socket option (%d): %s", errorcode, errormessage));
908 g_free (errormessage);
913 gchar *errormessage = socket_last_error_message ();
914 int errorcode = socket_last_error_code ();
915 CLOSE_IF_REQUESTED (sink);
916 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
917 ("Could not set loopback socket option (%d): %s",
918 errorcode, errormessage));
919 g_free (errormessage);
924 /* create a socket for sending to remote machine */
926 gst_multiudpsink_init_send (GstMultiUDPSink * sink)
930 GstUDPClient *client;
934 if (sink->sockfd == -1) {
935 GST_DEBUG_OBJECT (sink, "creating sockets");
936 /* create sender socket try IP6, fall back to IP4 */
937 sink->ss_family = AF_INET6;
938 if ((sink->sock = socket (AF_INET6, SOCK_DGRAM, 0)) == -1) {
939 sink->ss_family = AF_INET;
940 if ((sink->sock = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
944 GST_DEBUG_OBJECT (sink, "have socket");
945 sink->externalfd = FALSE;
947 struct sockaddr_storage myaddr;
949 GST_DEBUG_OBJECT (sink, "using configured socket");
950 /* we use the configured socket, try to get some info about it */
951 len = sizeof (myaddr);
952 if (getsockname (sink->sockfd, (struct sockaddr *) &myaddr, &len) < 0)
953 goto getsockname_error;
955 sink->ss_family = myaddr.ss_family;
956 /* we use the configured socket */
957 sink->sock = sink->sockfd;
958 sink->externalfd = TRUE;
961 len = sizeof (sndsize);
962 if (sink->buffer_size != 0) {
963 sndsize = sink->buffer_size;
965 GST_DEBUG_OBJECT (sink, "setting udp buffer of %d bytes", sndsize);
966 /* set buffer size, Note that on Linux this is typically limited to a
967 * maximum of around 100K. Also a minimum of 128 bytes is required on
970 setsockopt (sink->sockfd, SOL_SOCKET, SO_SNDBUF, (void *) &sndsize,
973 GST_ELEMENT_WARNING (sink, RESOURCE, SETTINGS, (NULL),
974 ("Could not create a buffer of requested %d bytes, %d: %s (%d)",
975 sndsize, ret, g_strerror (errno), errno));
979 /* read the value of the receive buffer. Note that on linux this returns 2x the
980 * value we set because the kernel allocates extra memory for metadata.
981 * The default on Linux is about 100K (which is about 50K without metadata) */
983 getsockopt (sink->sockfd, SOL_SOCKET, SO_SNDBUF, (void *) &sndsize, &len);
985 GST_DEBUG_OBJECT (sink, "have udp buffer of %d bytes", sndsize);
987 GST_DEBUG_OBJECT (sink, "could not get udp buffer size");
991 if (setsockopt (sink->sock, SOL_SOCKET, SO_BROADCAST, &bc_val,
992 sizeof (bc_val)) < 0)
995 sink->bytes_to_serve = 0;
996 sink->bytes_served = 0;
998 gst_multiudpsink_setup_qos_dscp (sink);
1000 /* look for multicast clients and join multicast groups appropriately
1001 set also ttl and multicast loopback delivery appropriately */
1002 for (clients = sink->clients; clients; clients = g_list_next (clients)) {
1003 client = (GstUDPClient *) clients->data;
1005 if (!gst_multiudpsink_configure_client (sink, client))
1013 gchar *errormessage = socket_last_error_message ();
1014 int errorcode = socket_last_error_code ();
1015 GST_ELEMENT_ERROR (sink, RESOURCE, FAILED, (NULL),
1016 ("Could not create socket (%d): %s", errorcode, errormessage));
1017 g_free (errormessage);
1022 gchar *errormessage = socket_last_error_message ();
1023 int errorcode = socket_last_error_code ();
1024 GST_ELEMENT_ERROR (sink, RESOURCE, FAILED, (NULL),
1025 ("Could not getsockname (%d): %s", errorcode, errormessage));
1026 g_free (errormessage);
1031 gchar *errormessage = socket_last_error_message ();
1032 int errorcode = socket_last_error_code ();
1033 CLOSE_IF_REQUESTED (sink);
1034 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
1035 ("Could not set broadcast socket option (%d): %s",
1036 errorcode, errormessage));
1037 g_free (errormessage);
1043 gst_multiudpsink_close (GstMultiUDPSink * sink)
1045 CLOSE_IF_REQUESTED (sink);
1049 gst_multiudpsink_add_internal (GstMultiUDPSink * sink, const gchar * host,
1050 gint port, gboolean lock)
1052 GstUDPClient *client;
1053 GstUDPClient udpclient;
1057 udpclient.host = (gchar *) host;
1058 udpclient.port = port;
1060 GST_DEBUG_OBJECT (sink, "adding client on host %s, port %d", host, port);
1063 g_mutex_lock (sink->client_lock);
1065 find = g_list_find_custom (sink->clients, &udpclient,
1066 (GCompareFunc) client_compare);
1068 client = (GstUDPClient *) find->data;
1070 GST_DEBUG_OBJECT (sink, "found %d existing clients with host %s, port %d",
1071 client->refcount, host, port);
1074 client = create_client (sink, host, port);
1076 client->sock = &sink->sock;
1078 if (gst_udp_get_addr (host, port, &client->theiraddr) < 0)
1079 goto getaddrinfo_error;
1081 g_get_current_time (&now);
1082 client->connect_time = GST_TIMEVAL_TO_TIME (now);
1084 if (*client->sock > 0) {
1085 gst_multiudpsink_configure_client (sink, client);
1088 GST_DEBUG_OBJECT (sink, "add client with host %s, port %d", host, port);
1089 sink->clients = g_list_prepend (sink->clients, client);
1093 g_mutex_unlock (sink->client_lock);
1095 g_signal_emit (G_OBJECT (sink),
1096 gst_multiudpsink_signals[SIGNAL_CLIENT_ADDED], 0, host, port);
1098 GST_DEBUG_OBJECT (sink, "added client on host %s, port %d", host, port);
1104 GST_DEBUG_OBJECT (sink, "did not add client on host %s, port %d", host,
1106 GST_WARNING_OBJECT (sink, "getaddrinfo lookup error?");
1107 free_client (client);
1109 g_mutex_unlock (sink->client_lock);
1115 gst_multiudpsink_add (GstMultiUDPSink * sink, const gchar * host, gint port)
1117 gst_multiudpsink_add_internal (sink, host, port, TRUE);
1121 gst_multiudpsink_remove (GstMultiUDPSink * sink, const gchar * host, gint port)
1124 GstUDPClient udpclient;
1125 GstUDPClient *client;
1128 udpclient.host = (gchar *) host;
1129 udpclient.port = port;
1131 g_mutex_lock (sink->client_lock);
1132 find = g_list_find_custom (sink->clients, &udpclient,
1133 (GCompareFunc) client_compare);
1137 client = (GstUDPClient *) find->data;
1139 GST_DEBUG_OBJECT (sink, "found %d clients with host %s, port %d",
1140 client->refcount, host, port);
1143 if (client->refcount == 0) {
1144 GST_DEBUG_OBJECT (sink, "remove client with host %s, port %d", host, port);
1146 g_get_current_time (&now);
1147 client->disconnect_time = GST_TIMEVAL_TO_TIME (now);
1149 if (*(client->sock) != -1 && sink->auto_multicast
1150 && gst_udp_is_multicast (&client->theiraddr))
1151 gst_udp_leave_group (*(client->sock), &client->theiraddr);
1153 /* Unlock to emit signal before we delete the actual client */
1154 g_mutex_unlock (sink->client_lock);
1155 g_signal_emit (G_OBJECT (sink),
1156 gst_multiudpsink_signals[SIGNAL_CLIENT_REMOVED], 0, host, port);
1157 g_mutex_lock (sink->client_lock);
1159 sink->clients = g_list_delete_link (sink->clients, find);
1161 free_client (client);
1163 g_mutex_unlock (sink->client_lock);
1170 g_mutex_unlock (sink->client_lock);
1171 GST_WARNING_OBJECT (sink, "client at host %s, port %d not found",
1178 gst_multiudpsink_clear_internal (GstMultiUDPSink * sink, gboolean lock)
1180 GST_DEBUG_OBJECT (sink, "clearing");
1181 /* we only need to remove the client structure, there is no additional
1182 * socket or anything to free for UDP */
1184 g_mutex_lock (sink->client_lock);
1185 g_list_foreach (sink->clients, (GFunc) free_client, sink);
1186 g_list_free (sink->clients);
1187 sink->clients = NULL;
1189 g_mutex_unlock (sink->client_lock);
1193 gst_multiudpsink_clear (GstMultiUDPSink * sink)
1195 gst_multiudpsink_clear_internal (sink, TRUE);
1199 gst_multiudpsink_get_stats (GstMultiUDPSink * sink, const gchar * host,
1202 GstUDPClient *client;
1203 GValueArray *result = NULL;
1204 GstUDPClient udpclient;
1206 GValue value = { 0 };
1208 udpclient.host = (gchar *) host;
1209 udpclient.port = port;
1211 g_mutex_lock (sink->client_lock);
1213 find = g_list_find_custom (sink->clients, &udpclient,
1214 (GCompareFunc) client_compare);
1218 GST_DEBUG_OBJECT (sink, "stats for client with host %s, port %d", host, port);
1220 client = (GstUDPClient *) find->data;
1222 /* Result is a value array of (bytes_sent, packets_sent,
1223 * connect_time, disconnect_time), all as uint64 */
1224 result = g_value_array_new (4);
1226 g_value_init (&value, G_TYPE_UINT64);
1227 g_value_set_uint64 (&value, client->bytes_sent);
1228 result = g_value_array_append (result, &value);
1229 g_value_unset (&value);
1231 g_value_init (&value, G_TYPE_UINT64);
1232 g_value_set_uint64 (&value, client->packets_sent);
1233 result = g_value_array_append (result, &value);
1234 g_value_unset (&value);
1236 g_value_init (&value, G_TYPE_UINT64);
1237 g_value_set_uint64 (&value, client->connect_time);
1238 result = g_value_array_append (result, &value);
1239 g_value_unset (&value);
1241 g_value_init (&value, G_TYPE_UINT64);
1242 g_value_set_uint64 (&value, client->disconnect_time);
1243 result = g_value_array_append (result, &value);
1244 g_value_unset (&value);
1246 g_mutex_unlock (sink->client_lock);
1253 g_mutex_unlock (sink->client_lock);
1254 GST_WARNING_OBJECT (sink, "client with host %s, port %d not found",
1256 /* Apparently (see comment in gstmultifdsink.c) returning NULL from here may
1257 * confuse/break python bindings */
1258 return g_value_array_new (0);
1262 static GstStateChangeReturn
1263 gst_multiudpsink_change_state (GstElement * element, GstStateChange transition)
1265 GstStateChangeReturn ret;
1266 GstMultiUDPSink *sink;
1268 sink = GST_MULTIUDPSINK (element);
1270 switch (transition) {
1271 case GST_STATE_CHANGE_READY_TO_PAUSED:
1272 if (!gst_multiudpsink_init_send (sink))
1279 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1281 switch (transition) {
1282 case GST_STATE_CHANGE_PAUSED_TO_READY:
1283 gst_multiudpsink_close (sink);
1293 /* _init_send() posted specific error already */
1294 return GST_STATE_CHANGE_FAILURE;