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_pad_template (element_class,
176 gst_static_pad_template_get (&sink_template));
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), G_SIGNAL_RUN_LAST,
218 G_STRUCT_OFFSET (GstMultiUDPSinkClass, add),
219 NULL, NULL, gst_udp_marshal_VOID__STRING_INT, G_TYPE_NONE, 2,
220 G_TYPE_STRING, G_TYPE_INT);
222 * GstMultiUDPSink::remove:
223 * @gstmultiudpsink: the sink on which the signal is emitted
224 * @host: the hostname/IP address of the client to remove
225 * @port: the port of the client to remove
227 * Remove the client with destination @host and @port from the list of
230 gst_multiudpsink_signals[SIGNAL_REMOVE] =
231 g_signal_new ("remove", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
232 G_STRUCT_OFFSET (GstMultiUDPSinkClass, remove),
233 NULL, NULL, gst_udp_marshal_VOID__STRING_INT, G_TYPE_NONE, 2,
234 G_TYPE_STRING, G_TYPE_INT);
236 * GstMultiUDPSink::clear:
237 * @gstmultiudpsink: the sink on which the signal is emitted
239 * Clear the list of clients.
241 gst_multiudpsink_signals[SIGNAL_CLEAR] =
242 g_signal_new ("clear", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
243 G_STRUCT_OFFSET (GstMultiUDPSinkClass, clear),
244 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
246 * GstMultiUDPSink::get-stats:
247 * @gstmultiudpsink: the sink on which the signal is emitted
248 * @host: the hostname/IP address of the client to get stats on
249 * @port: the port of the client to get stats on
251 * Get the statistics of the client with destination @host and @port.
253 * Returns: a GValueArray of uint64: bytes_sent, packets_sent,
254 * connect_time (in epoch seconds), disconnect_time (in epoch seconds)
256 gst_multiudpsink_signals[SIGNAL_GET_STATS] =
257 g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
258 G_STRUCT_OFFSET (GstMultiUDPSinkClass, get_stats),
259 NULL, NULL, gst_udp_marshal_BOXED__STRING_INT, G_TYPE_VALUE_ARRAY, 2,
260 G_TYPE_STRING, G_TYPE_INT);
262 * GstMultiUDPSink::client-added:
263 * @gstmultiudpsink: the sink emitting the signal
264 * @host: the hostname/IP address of the added client
265 * @port: the port of the added client
267 * Signal emited when a new client is added to the list of
270 gst_multiudpsink_signals[SIGNAL_CLIENT_ADDED] =
271 g_signal_new ("client-added", G_TYPE_FROM_CLASS (klass),
272 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiUDPSinkClass, client_added),
273 NULL, NULL, gst_udp_marshal_VOID__STRING_INT, G_TYPE_NONE, 2,
274 G_TYPE_STRING, G_TYPE_INT);
276 * GstMultiUDPSink::client-removed:
277 * @gstmultiudpsink: the sink emitting the signal
278 * @host: the hostname/IP address of the removed client
279 * @port: the port of the removed client
281 * Signal emited when a client is removed from the list of
284 gst_multiudpsink_signals[SIGNAL_CLIENT_REMOVED] =
285 g_signal_new ("client-removed", G_TYPE_FROM_CLASS (klass),
286 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiUDPSinkClass,
287 client_removed), NULL, NULL, gst_udp_marshal_VOID__STRING_INT,
288 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
290 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTES_TO_SERVE,
291 g_param_spec_uint64 ("bytes-to-serve", "Bytes to serve",
292 "Number of bytes received to serve to clients", 0, G_MAXUINT64, 0,
293 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
294 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTES_SERVED,
295 g_param_spec_uint64 ("bytes-served", "Bytes served",
296 "Total number of bytes send to all clients", 0, G_MAXUINT64, 0,
297 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
298 g_object_class_install_property (gobject_class, PROP_SOCKFD,
299 g_param_spec_int ("sockfd", "Socket Handle",
300 "Socket to use for UDP sending. (-1 == allocate)",
301 -1, G_MAXINT, DEFAULT_SOCKFD,
302 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
303 g_object_class_install_property (gobject_class, PROP_CLOSEFD,
304 g_param_spec_boolean ("closefd", "Close sockfd",
305 "Close sockfd if passed as property on state change",
306 DEFAULT_CLOSEFD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
307 g_object_class_install_property (gobject_class, PROP_SOCK,
308 g_param_spec_int ("sock", "Socket Handle",
309 "Socket currently in use for UDP sending. (-1 == no socket)",
310 -1, G_MAXINT, DEFAULT_SOCK,
311 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
312 g_object_class_install_property (gobject_class, PROP_CLIENTS,
313 g_param_spec_string ("clients", "Clients",
314 "A comma separated list of host:port pairs with destinations",
315 DEFAULT_CLIENTS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
316 g_object_class_install_property (gobject_class, PROP_AUTO_MULTICAST,
317 g_param_spec_boolean ("auto-multicast",
318 "Automatically join/leave multicast groups",
319 "Automatically join/leave the multicast groups, FALSE means user"
320 " has to do it himself", DEFAULT_AUTO_MULTICAST,
321 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
322 g_object_class_install_property (gobject_class, PROP_TTL,
323 g_param_spec_int ("ttl", "Unicast TTL",
324 "Used for setting the unicast TTL parameter",
325 0, 255, DEFAULT_TTL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326 g_object_class_install_property (gobject_class, PROP_TTL_MC,
327 g_param_spec_int ("ttl-mc", "Multicast TTL",
328 "Used for setting the multicast TTL parameter",
329 0, 255, DEFAULT_TTL_MC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
330 g_object_class_install_property (gobject_class, PROP_LOOP,
331 g_param_spec_boolean ("loop", "Multicast Loopback",
332 "Used for setting the multicast loop parameter. TRUE = enable,"
333 " FALSE = disable", DEFAULT_LOOP,
334 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
335 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QOS_DSCP,
336 g_param_spec_int ("qos-dscp", "QoS diff srv code point",
337 "Quality of Service, differentiated services code point (-1 default)",
338 -1, 63, DEFAULT_QOS_DSCP,
339 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
341 * GstMultiUDPSink::send-duplicates
343 * When a host/port pair is added mutliple times, send the packet to the host
344 * multiple times as well.
348 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEND_DUPLICATES,
349 g_param_spec_boolean ("send-duplicates", "Send Duplicates",
350 "When a distination/port pair is added multiple times, send packets "
351 "multiple times as well", DEFAULT_SEND_DUPLICATES,
352 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
354 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_SIZE,
355 g_param_spec_int ("buffer-size", "Buffer Size",
356 "Size of the kernel send buffer in bytes, 0=default", 0, G_MAXINT,
357 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359 gstelement_class->change_state = gst_multiudpsink_change_state;
361 gstbasesink_class->render = gst_multiudpsink_render;
363 gstbasesink_class->render_list = gst_multiudpsink_render_list;
365 klass->add = gst_multiudpsink_add;
366 klass->remove = gst_multiudpsink_remove;
367 klass->clear = gst_multiudpsink_clear;
368 klass->get_stats = gst_multiudpsink_get_stats;
370 GST_DEBUG_CATEGORY_INIT (multiudpsink_debug, "multiudpsink", 0, "UDP sink");
375 gst_multiudpsink_init (GstMultiUDPSink * sink)
379 sink->client_lock = g_mutex_new ();
380 sink->sock = DEFAULT_SOCK;
381 sink->sockfd = DEFAULT_SOCKFD;
382 sink->closefd = DEFAULT_CLOSEFD;
383 sink->externalfd = (sink->sockfd != -1);
384 sink->auto_multicast = DEFAULT_AUTO_MULTICAST;
385 sink->ttl = DEFAULT_TTL;
386 sink->ttl_mc = DEFAULT_TTL_MC;
387 sink->loop = DEFAULT_LOOP;
388 sink->qos_dscp = DEFAULT_QOS_DSCP;
389 sink->ss_family = DEFAULT_FAMILY;
390 sink->send_duplicates = DEFAULT_SEND_DUPLICATES;
393 static GstUDPClient *
394 create_client (GstMultiUDPSink * sink, const gchar * host, gint port)
396 GstUDPClient *client;
398 client = g_slice_new0 (GstUDPClient);
399 client->refcount = 1;
400 client->host = g_strdup (host);
407 free_client (GstUDPClient * client)
409 g_free (client->host);
410 g_slice_free (GstUDPClient, client);
414 client_compare (GstUDPClient * a, GstUDPClient * b)
416 if ((a->port == b->port) && (strcmp (a->host, b->host) == 0))
423 gst_multiudpsink_finalize (GObject * object)
425 GstMultiUDPSink *sink;
427 sink = GST_MULTIUDPSINK (object);
429 g_list_foreach (sink->clients, (GFunc) free_client, NULL);
430 g_list_free (sink->clients);
432 if (sink->sockfd >= 0 && sink->closefd)
433 CLOSE_SOCKET (sink->sockfd);
435 g_mutex_free (sink->client_lock);
437 WSA_CLEANUP (object);
439 G_OBJECT_CLASS (parent_class)->finalize (object);
443 socket_error_is_ignorable (void)
446 /* Windows doesn't seem to have an EAGAIN for sockets */
447 return WSAGetLastError () == WSAEINTR;
449 return errno == EINTR || errno == EAGAIN;
454 socket_last_error_code (void)
457 return WSAGetLastError ();
464 socket_last_error_message (void)
467 int errorcode = WSAGetLastError ();
470 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
471 NULL, errorcode, 0, (LPSTR) buf, sizeof (buf) / sizeof (wchar_t), NULL);
472 if (FAILED (result)) {
473 return g_strdup ("failed to get error message from system");
476 g_convert ((gchar *) buf, -1, "UTF-16", "UTF-8", NULL, NULL, NULL);
477 /* g_convert() internally calls windows functions which reset the
478 windows error code, so fix it up again like this */
479 WSASetLastError (errorcode);
483 return g_strdup (g_strerror (errno));
488 gst_multiudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
490 GstMultiUDPSink *sink;
491 gint ret, size, num = 0, no_clients = 0;
496 sink = GST_MULTIUDPSINK (bsink);
498 size = GST_BUFFER_SIZE (buffer);
499 data = GST_BUFFER_DATA (buffer);
501 if (size > UDP_MAX_SIZE) {
502 GST_WARNING ("Attempting to send a UDP packet larger than maximum "
503 "size (%d > %d)", size, UDP_MAX_SIZE);
506 sink->bytes_to_serve += size;
508 /* grab lock while iterating and sending to clients, this should be
509 * fast as UDP never blocks */
510 g_mutex_lock (sink->client_lock);
511 GST_LOG_OBJECT (bsink, "about to send %d bytes", size);
513 for (clients = sink->clients; clients; clients = g_list_next (clients)) {
514 GstUDPClient *client;
517 client = (GstUDPClient *) clients->data;
519 GST_LOG_OBJECT (sink, "sending %d bytes to client %p", size, client);
521 count = sink->send_duplicates ? client->refcount : 1;
525 len = gst_udp_get_sockaddr_length (&client->theiraddr);
527 ret = sendto (*client->sock,
533 size, 0, (struct sockaddr *) &client->theiraddr, len);
536 /* some error, just warn, it's likely recoverable and we don't want to
537 * break streaming. We break so that we stop retrying for this client. */
538 if (!socket_error_is_ignorable ()) {
539 gchar *errormessage = socket_last_error_message ();
540 GST_WARNING_OBJECT (sink, "client %p gave error %d (%s)", client,
541 socket_last_error_code (), errormessage);
542 g_free (errormessage);
547 client->bytes_sent += ret;
548 client->packets_sent++;
549 sink->bytes_served += ret;
555 g_mutex_unlock (sink->client_lock);
557 GST_LOG_OBJECT (sink, "sent %d bytes to %d (of %d) clients", size, num,
565 gst_multiudpsink_render_list (GstBaseSink * bsink, GstBufferList * list)
567 GstMultiUDPSink *sink;
569 gint ret, size = 0, num = 0, no_clients = 0;
571 struct msghdr msg = { 0 };
573 GstBufferListIterator *it;
577 sink = GST_MULTIUDPSINK (bsink);
579 g_return_val_if_fail (list != NULL, GST_FLOW_ERROR);
581 it = gst_buffer_list_iterate (list);
582 g_return_val_if_fail (it != NULL, GST_FLOW_ERROR);
584 while (gst_buffer_list_iterator_next_group (it)) {
588 if ((gsize = gst_buffer_list_iterator_n_buffers (it)) == 0) {
592 iov = (struct iovec *) g_malloc (gsize * sizeof (struct iovec));
595 while ((buf = gst_buffer_list_iterator_next (it))) {
596 if (GST_BUFFER_SIZE (buf) > UDP_MAX_SIZE) {
597 GST_WARNING ("Attempting to send a UDP packet larger than maximum "
598 "size (%d > %d)", GST_BUFFER_SIZE (buf), UDP_MAX_SIZE);
601 msg.msg_iov[msg.msg_iovlen].iov_len = GST_BUFFER_SIZE (buf);
602 msg.msg_iov[msg.msg_iovlen].iov_base = GST_BUFFER_DATA (buf);
604 size += GST_BUFFER_SIZE (buf);
607 sink->bytes_to_serve += size;
609 /* grab lock while iterating and sending to clients, this should be
610 * fast as UDP never blocks */
611 g_mutex_lock (sink->client_lock);
612 GST_LOG_OBJECT (bsink, "about to send %d bytes", size);
614 for (clients = sink->clients; clients; clients = g_list_next (clients)) {
615 GstUDPClient *client;
618 client = (GstUDPClient *) clients->data;
620 GST_LOG_OBJECT (sink, "sending %d bytes to client %p", size, client);
622 count = sink->send_duplicates ? client->refcount : 1;
626 msg.msg_name = (void *) &client->theiraddr;
627 msg.msg_namelen = sizeof (client->theiraddr);
628 ret = sendmsg (*client->sock, &msg, 0);
631 if (!socket_error_is_ignorable ()) {
636 client->bytes_sent += ret;
637 client->packets_sent++;
638 sink->bytes_served += ret;
644 g_mutex_unlock (sink->client_lock);
649 GST_LOG_OBJECT (sink, "sent %d bytes to %d (of %d) clients", size, num,
653 gst_buffer_list_iterator_free (it);
658 gst_buffer_list_iterator_free (it);
659 return GST_FLOW_ERROR;
664 gst_multiudpsink_set_clients_string (GstMultiUDPSink * sink,
665 const gchar * string)
670 clients = g_strsplit (string, ",", 0);
672 g_mutex_lock (sink->client_lock);
673 /* clear all existing clients */
674 gst_multiudpsink_clear_internal (sink, FALSE);
675 for (i = 0; clients[i]; i++) {
680 p = strstr (clients[i], ":");
686 gst_multiudpsink_add_internal (sink, host, port, FALSE);
688 g_mutex_unlock (sink->client_lock);
690 g_strfreev (clients);
694 gst_multiudpsink_get_clients_string (GstMultiUDPSink * sink)
699 str = g_string_new ("");
701 g_mutex_lock (sink->client_lock);
702 clients = sink->clients;
704 GstUDPClient *client;
707 client = (GstUDPClient *) clients->data;
709 clients = g_list_next (clients);
711 count = client->refcount;
713 g_string_append_printf (str, "%s:%d%s", client->host, client->port,
714 (clients || count > 1 ? "," : ""));
717 g_mutex_unlock (sink->client_lock);
719 return g_string_free (str, FALSE);
723 gst_multiudpsink_setup_qos_dscp (GstMultiUDPSink * sink)
727 /* don't touch on -1 */
728 if (sink->qos_dscp < 0)
734 GST_DEBUG_OBJECT (sink, "setting TOS to %d", sink->qos_dscp);
736 /* Extract and shift 6 bits of DSFIELD */
737 tos = (sink->qos_dscp & 0x3f) << 2;
739 if (setsockopt (sink->sock, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0) {
740 gchar *errormessage = socket_last_error_message ();
741 GST_ERROR_OBJECT (sink, "could not set TOS: %s", errormessage);
742 g_free (errormessage);
745 if (setsockopt (sink->sock, IPPROTO_IPV6, IPV6_TCLASS, &tos,
747 gchar *errormessage = socket_last_error_message ();
748 GST_ERROR_OBJECT (sink, "could not set TCLASS: %s", errormessage);
749 g_free (errormessage);
755 gst_multiudpsink_set_property (GObject * object, guint prop_id,
756 const GValue * value, GParamSpec * pspec)
758 GstMultiUDPSink *udpsink;
760 udpsink = GST_MULTIUDPSINK (object);
764 if (udpsink->sockfd >= 0 && udpsink->sockfd != udpsink->sock &&
766 CLOSE_SOCKET (udpsink->sockfd);
767 udpsink->sockfd = g_value_get_int (value);
768 GST_DEBUG_OBJECT (udpsink, "setting SOCKFD to %d", udpsink->sockfd);
771 udpsink->closefd = g_value_get_boolean (value);
774 gst_multiudpsink_set_clients_string (udpsink, g_value_get_string (value));
776 case PROP_AUTO_MULTICAST:
777 udpsink->auto_multicast = g_value_get_boolean (value);
780 udpsink->ttl = g_value_get_int (value);
783 udpsink->ttl_mc = g_value_get_int (value);
786 udpsink->loop = g_value_get_boolean (value);
789 udpsink->qos_dscp = g_value_get_int (value);
790 gst_multiudpsink_setup_qos_dscp (udpsink);
792 case PROP_SEND_DUPLICATES:
793 udpsink->send_duplicates = g_value_get_boolean (value);
795 case PROP_BUFFER_SIZE:
796 udpsink->buffer_size = g_value_get_int (value);
799 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
805 gst_multiudpsink_get_property (GObject * object, guint prop_id, GValue * value,
808 GstMultiUDPSink *udpsink;
810 udpsink = GST_MULTIUDPSINK (object);
813 case PROP_BYTES_TO_SERVE:
814 g_value_set_uint64 (value, udpsink->bytes_to_serve);
816 case PROP_BYTES_SERVED:
817 g_value_set_uint64 (value, udpsink->bytes_served);
820 g_value_set_int (value, udpsink->sockfd);
823 g_value_set_boolean (value, udpsink->closefd);
826 g_value_set_int (value, udpsink->sock);
829 g_value_take_string (value,
830 gst_multiudpsink_get_clients_string (udpsink));
832 case PROP_AUTO_MULTICAST:
833 g_value_set_boolean (value, udpsink->auto_multicast);
836 g_value_set_int (value, udpsink->ttl);
839 g_value_set_int (value, udpsink->ttl_mc);
842 g_value_set_boolean (value, udpsink->loop);
845 g_value_set_int (value, udpsink->qos_dscp);
847 case PROP_SEND_DUPLICATES:
848 g_value_set_boolean (value, udpsink->send_duplicates);
850 case PROP_BUFFER_SIZE:
851 g_value_set_int (value, udpsink->buffer_size);
854 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
860 gst_multiudpsink_configure_client (GstMultiUDPSink * sink,
861 GstUDPClient * client)
863 GST_DEBUG_OBJECT (sink, "configuring client %p", client);
865 if (gst_udp_is_multicast (&client->theiraddr)) {
866 GST_DEBUG_OBJECT (sink, "we have a multicast client %p", client);
867 if (sink->auto_multicast) {
868 GST_DEBUG_OBJECT (sink, "autojoining group");
869 if (gst_udp_join_group (*(client->sock), &client->theiraddr, NULL)
871 goto join_group_failed;
873 GST_DEBUG_OBJECT (sink, "setting loop to %d", sink->loop);
874 if (gst_udp_set_loop (sink->sock, sink->ss_family, sink->loop) != 0)
876 GST_DEBUG_OBJECT (sink, "setting ttl to %d", sink->ttl_mc);
877 if (gst_udp_set_ttl (sink->sock, sink->ss_family, sink->ttl_mc, TRUE) != 0)
880 GST_DEBUG_OBJECT (sink, "setting unicast ttl to %d", sink->ttl);
881 if (gst_udp_set_ttl (sink->sock, sink->ss_family, sink->ttl, FALSE) != 0)
889 gchar *errormessage = socket_last_error_message ();
890 int errorcode = socket_last_error_code ();
891 CLOSE_IF_REQUESTED (sink);
892 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
893 ("Could not join multicast group (%d): %s", errorcode, errormessage));
894 g_free (errormessage);
899 gchar *errormessage = socket_last_error_message ();
900 int errorcode = socket_last_error_code ();
901 CLOSE_IF_REQUESTED (sink);
902 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
903 ("Could not set TTL socket option (%d): %s", errorcode, errormessage));
904 g_free (errormessage);
909 gchar *errormessage = socket_last_error_message ();
910 int errorcode = socket_last_error_code ();
911 CLOSE_IF_REQUESTED (sink);
912 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
913 ("Could not set loopback socket option (%d): %s",
914 errorcode, errormessage));
915 g_free (errormessage);
920 /* create a socket for sending to remote machine */
922 gst_multiudpsink_init_send (GstMultiUDPSink * sink)
926 GstUDPClient *client;
930 if (sink->sockfd == -1) {
931 GST_DEBUG_OBJECT (sink, "creating sockets");
932 /* create sender socket try IP6, fall back to IP4 */
933 sink->ss_family = AF_INET6;
934 if ((sink->sock = socket (AF_INET6, SOCK_DGRAM, 0)) == -1) {
935 sink->ss_family = AF_INET;
936 if ((sink->sock = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
940 GST_DEBUG_OBJECT (sink, "have socket");
941 sink->externalfd = FALSE;
943 struct sockaddr_storage myaddr;
945 GST_DEBUG_OBJECT (sink, "using configured socket");
946 /* we use the configured socket, try to get some info about it */
947 len = sizeof (myaddr);
948 if (getsockname (sink->sockfd, (struct sockaddr *) &myaddr, &len) < 0)
949 goto getsockname_error;
951 sink->ss_family = myaddr.ss_family;
952 /* we use the configured socket */
953 sink->sock = sink->sockfd;
954 sink->externalfd = TRUE;
957 len = sizeof (sndsize);
958 if (sink->buffer_size != 0) {
959 sndsize = sink->buffer_size;
961 GST_DEBUG_OBJECT (sink, "setting udp buffer of %d bytes", sndsize);
962 /* set buffer size, Note that on Linux this is typically limited to a
963 * maximum of around 100K. Also a minimum of 128 bytes is required on
966 setsockopt (sink->sockfd, SOL_SOCKET, SO_SNDBUF, (void *) &sndsize,
969 GST_ELEMENT_WARNING (sink, RESOURCE, SETTINGS, (NULL),
970 ("Could not create a buffer of requested %d bytes, %d: %s (%d)",
971 sndsize, ret, g_strerror (errno), errno));
975 /* read the value of the receive buffer. Note that on linux this returns 2x the
976 * value we set because the kernel allocates extra memory for metadata.
977 * The default on Linux is about 100K (which is about 50K without metadata) */
979 getsockopt (sink->sockfd, SOL_SOCKET, SO_SNDBUF, (void *) &sndsize, &len);
981 GST_DEBUG_OBJECT (sink, "have udp buffer of %d bytes", sndsize);
983 GST_DEBUG_OBJECT (sink, "could not get udp buffer size");
987 if (setsockopt (sink->sock, SOL_SOCKET, SO_BROADCAST, &bc_val,
988 sizeof (bc_val)) < 0)
991 sink->bytes_to_serve = 0;
992 sink->bytes_served = 0;
994 gst_multiudpsink_setup_qos_dscp (sink);
996 /* look for multicast clients and join multicast groups appropriately
997 set also ttl and multicast loopback delivery appropriately */
998 for (clients = sink->clients; clients; clients = g_list_next (clients)) {
999 client = (GstUDPClient *) clients->data;
1001 if (!gst_multiudpsink_configure_client (sink, client))
1009 gchar *errormessage = socket_last_error_message ();
1010 int errorcode = socket_last_error_code ();
1011 GST_ELEMENT_ERROR (sink, RESOURCE, FAILED, (NULL),
1012 ("Could not create socket (%d): %s", errorcode, errormessage));
1013 g_free (errormessage);
1018 gchar *errormessage = socket_last_error_message ();
1019 int errorcode = socket_last_error_code ();
1020 GST_ELEMENT_ERROR (sink, RESOURCE, FAILED, (NULL),
1021 ("Could not getsockname (%d): %s", errorcode, errormessage));
1022 g_free (errormessage);
1027 gchar *errormessage = socket_last_error_message ();
1028 int errorcode = socket_last_error_code ();
1029 CLOSE_IF_REQUESTED (sink);
1030 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
1031 ("Could not set broadcast socket option (%d): %s",
1032 errorcode, errormessage));
1033 g_free (errormessage);
1039 gst_multiudpsink_close (GstMultiUDPSink * sink)
1041 CLOSE_IF_REQUESTED (sink);
1045 gst_multiudpsink_add_internal (GstMultiUDPSink * sink, const gchar * host,
1046 gint port, gboolean lock)
1048 GstUDPClient *client;
1049 GstUDPClient udpclient;
1053 udpclient.host = (gchar *) host;
1054 udpclient.port = port;
1056 GST_DEBUG_OBJECT (sink, "adding client on host %s, port %d", host, port);
1059 g_mutex_lock (sink->client_lock);
1061 find = g_list_find_custom (sink->clients, &udpclient,
1062 (GCompareFunc) client_compare);
1064 client = (GstUDPClient *) find->data;
1066 GST_DEBUG_OBJECT (sink, "found %d existing clients with host %s, port %d",
1067 client->refcount, host, port);
1070 client = create_client (sink, host, port);
1072 client->sock = &sink->sock;
1074 if (gst_udp_get_addr (host, port, &client->theiraddr) < 0)
1075 goto getaddrinfo_error;
1077 g_get_current_time (&now);
1078 client->connect_time = GST_TIMEVAL_TO_TIME (now);
1080 if (*client->sock > 0) {
1081 gst_multiudpsink_configure_client (sink, client);
1084 GST_DEBUG_OBJECT (sink, "add client with host %s, port %d", host, port);
1085 sink->clients = g_list_prepend (sink->clients, client);
1089 g_mutex_unlock (sink->client_lock);
1091 g_signal_emit (G_OBJECT (sink),
1092 gst_multiudpsink_signals[SIGNAL_CLIENT_ADDED], 0, host, port);
1094 GST_DEBUG_OBJECT (sink, "added client on host %s, port %d", host, port);
1100 GST_DEBUG_OBJECT (sink, "did not add client on host %s, port %d", host,
1102 GST_WARNING_OBJECT (sink, "getaddrinfo lookup error?");
1103 free_client (client);
1105 g_mutex_unlock (sink->client_lock);
1111 gst_multiudpsink_add (GstMultiUDPSink * sink, const gchar * host, gint port)
1113 gst_multiudpsink_add_internal (sink, host, port, TRUE);
1117 gst_multiudpsink_remove (GstMultiUDPSink * sink, const gchar * host, gint port)
1120 GstUDPClient udpclient;
1121 GstUDPClient *client;
1124 udpclient.host = (gchar *) host;
1125 udpclient.port = port;
1127 g_mutex_lock (sink->client_lock);
1128 find = g_list_find_custom (sink->clients, &udpclient,
1129 (GCompareFunc) client_compare);
1133 client = (GstUDPClient *) find->data;
1135 GST_DEBUG_OBJECT (sink, "found %d clients with host %s, port %d",
1136 client->refcount, host, port);
1139 if (client->refcount == 0) {
1140 GST_DEBUG_OBJECT (sink, "remove client with host %s, port %d", host, port);
1142 g_get_current_time (&now);
1143 client->disconnect_time = GST_TIMEVAL_TO_TIME (now);
1145 if (*(client->sock) != -1 && sink->auto_multicast
1146 && gst_udp_is_multicast (&client->theiraddr))
1147 gst_udp_leave_group (*(client->sock), &client->theiraddr);
1149 /* Unlock to emit signal before we delete the actual client */
1150 g_mutex_unlock (sink->client_lock);
1151 g_signal_emit (G_OBJECT (sink),
1152 gst_multiudpsink_signals[SIGNAL_CLIENT_REMOVED], 0, host, port);
1153 g_mutex_lock (sink->client_lock);
1155 sink->clients = g_list_delete_link (sink->clients, find);
1157 free_client (client);
1159 g_mutex_unlock (sink->client_lock);
1166 g_mutex_unlock (sink->client_lock);
1167 GST_WARNING_OBJECT (sink, "client at host %s, port %d not found",
1174 gst_multiudpsink_clear_internal (GstMultiUDPSink * sink, gboolean lock)
1176 GST_DEBUG_OBJECT (sink, "clearing");
1177 /* we only need to remove the client structure, there is no additional
1178 * socket or anything to free for UDP */
1180 g_mutex_lock (sink->client_lock);
1181 g_list_foreach (sink->clients, (GFunc) free_client, sink);
1182 g_list_free (sink->clients);
1183 sink->clients = NULL;
1185 g_mutex_unlock (sink->client_lock);
1189 gst_multiudpsink_clear (GstMultiUDPSink * sink)
1191 gst_multiudpsink_clear_internal (sink, TRUE);
1195 gst_multiudpsink_get_stats (GstMultiUDPSink * sink, const gchar * host,
1198 GstUDPClient *client;
1199 GValueArray *result = NULL;
1200 GstUDPClient udpclient;
1202 GValue value = { 0 };
1204 udpclient.host = (gchar *) host;
1205 udpclient.port = port;
1207 g_mutex_lock (sink->client_lock);
1209 find = g_list_find_custom (sink->clients, &udpclient,
1210 (GCompareFunc) client_compare);
1214 GST_DEBUG_OBJECT (sink, "stats for client with host %s, port %d", host, port);
1216 client = (GstUDPClient *) find->data;
1218 /* Result is a value array of (bytes_sent, packets_sent,
1219 * connect_time, disconnect_time), all as uint64 */
1220 result = g_value_array_new (4);
1222 g_value_init (&value, G_TYPE_UINT64);
1223 g_value_set_uint64 (&value, client->bytes_sent);
1224 result = g_value_array_append (result, &value);
1225 g_value_unset (&value);
1227 g_value_init (&value, G_TYPE_UINT64);
1228 g_value_set_uint64 (&value, client->packets_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->connect_time);
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->disconnect_time);
1239 result = g_value_array_append (result, &value);
1240 g_value_unset (&value);
1242 g_mutex_unlock (sink->client_lock);
1249 g_mutex_unlock (sink->client_lock);
1250 GST_WARNING_OBJECT (sink, "client with host %s, port %d not found",
1252 /* Apparently (see comment in gstmultifdsink.c) returning NULL from here may
1253 * confuse/break python bindings */
1254 return g_value_array_new (0);
1258 static GstStateChangeReturn
1259 gst_multiudpsink_change_state (GstElement * element, GstStateChange transition)
1261 GstStateChangeReturn ret;
1262 GstMultiUDPSink *sink;
1264 sink = GST_MULTIUDPSINK (element);
1266 switch (transition) {
1267 case GST_STATE_CHANGE_READY_TO_PAUSED:
1268 if (!gst_multiudpsink_init_send (sink))
1275 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1277 switch (transition) {
1278 case GST_STATE_CHANGE_PAUSED_TO_READY:
1279 gst_multiudpsink_close (sink);
1289 /* _init_send() posted specific error already */
1290 return GST_STATE_CHANGE_FAILURE;