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-multiupdsink
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 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
52 /* elementfactory information */
53 static const GstElementDetails gst_multiudpsink_details =
54 GST_ELEMENT_DETAILS ("UDP packet sender",
56 "Send data over the network via UDP",
57 "Wim Taymans <wim.taymans@gmail.com>");
59 /* MultiUDPSink signals and args */
70 SIGNAL_CLIENT_REMOVED,
76 #define DEFAULT_SOCKFD -1
77 #define DEFAULT_CLOSEFD TRUE
78 #define DEFAULT_SOCK -1
79 #define DEFAULT_CLIENTS NULL
80 /* FIXME, this should be disabled by default, we don't need to join a multicast
81 * group for sending, if this socket is also used for receiving, it should
82 * be configured in the element that does the receive. */
83 #define DEFAULT_AUTO_MULTICAST TRUE
84 #define DEFAULT_TTL 64
85 #define DEFAULT_TTL_MC 1
86 #define DEFAULT_LOOP TRUE
87 #define DEFAULT_QOS_DSCP -1
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 void free_client (GstUDPClient * client);
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_pad_template (element_class,
178 gst_static_pad_template_get (&sink_template));
180 gst_element_class_set_details (element_class, &gst_multiudpsink_details);
184 gst_multiudpsink_class_init (GstMultiUDPSinkClass * klass)
186 GObjectClass *gobject_class;
187 GstElementClass *gstelement_class;
188 GstBaseSinkClass *gstbasesink_class;
190 gobject_class = (GObjectClass *) klass;
191 gstelement_class = (GstElementClass *) klass;
192 gstbasesink_class = (GstBaseSinkClass *) klass;
194 parent_class = g_type_class_peek_parent (klass);
196 gobject_class->set_property = gst_multiudpsink_set_property;
197 gobject_class->get_property = gst_multiudpsink_get_property;
198 gobject_class->finalize = gst_multiudpsink_finalize;
201 * GstMultiUDPSink::add:
202 * @gstmultiudpsink: the sink on which the signal is emitted
203 * @host: the hostname/IP address of the client to add
204 * @port: the port of the client to add
206 * Add a client with destination @host and @port to the list of
209 gst_multiudpsink_signals[SIGNAL_ADD] =
210 g_signal_new ("add", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
211 G_STRUCT_OFFSET (GstMultiUDPSinkClass, add),
212 NULL, NULL, gst_udp_marshal_VOID__STRING_INT, G_TYPE_NONE, 2,
213 G_TYPE_STRING, G_TYPE_INT);
215 * GstMultiUDPSink::remove:
216 * @gstmultiudpsink: the sink on which the signal is emitted
217 * @host: the hostname/IP address of the client to remove
218 * @port: the port of the client to remove
220 * Remove the client with destination @host and @port from the list of
223 gst_multiudpsink_signals[SIGNAL_REMOVE] =
224 g_signal_new ("remove", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
225 G_STRUCT_OFFSET (GstMultiUDPSinkClass, remove),
226 NULL, NULL, gst_udp_marshal_VOID__STRING_INT, G_TYPE_NONE, 2,
227 G_TYPE_STRING, G_TYPE_INT);
229 * GstMultiUDPSink::clear:
230 * @gstmultiudpsink: the sink on which the signal is emitted
232 * Clear the list of clients.
234 gst_multiudpsink_signals[SIGNAL_CLEAR] =
235 g_signal_new ("clear", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
236 G_STRUCT_OFFSET (GstMultiUDPSinkClass, clear),
237 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
239 * GstMultiUDPSink::get-stats:
240 * @gstmultiudpsink: the sink on which the signal is emitted
241 * @host: the hostname/IP address of the client to get stats on
242 * @port: the port of the client to get stats on
244 * Get the statistics of the client with destination @host and @port.
246 * Returns: a GValueArray of uint64: bytes_sent, packets_sent,
247 * connect_time (in epoch seconds), disconnect_time (in epoch seconds)
249 gst_multiudpsink_signals[SIGNAL_GET_STATS] =
250 g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
251 G_STRUCT_OFFSET (GstMultiUDPSinkClass, get_stats),
252 NULL, NULL, gst_udp_marshal_BOXED__STRING_INT, G_TYPE_VALUE_ARRAY, 2,
253 G_TYPE_STRING, G_TYPE_INT);
255 * GstMultiUDPSink::client-added:
256 * @gstmultiudpsink: the sink emitting the signal
257 * @host: the hostname/IP address of the added client
258 * @port: the port of the added client
260 * Signal emited when a new client is added to the list of
263 gst_multiudpsink_signals[SIGNAL_CLIENT_ADDED] =
264 g_signal_new ("client-added", G_TYPE_FROM_CLASS (klass),
265 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiUDPSinkClass, client_added),
266 NULL, NULL, gst_udp_marshal_VOID__STRING_INT, G_TYPE_NONE, 2,
267 G_TYPE_STRING, G_TYPE_INT);
269 * GstMultiUDPSink::client-removed:
270 * @gstmultiudpsink: the sink emitting the signal
271 * @host: the hostname/IP address of the removed client
272 * @port: the port of the removed client
274 * Signal emited when a client is removed from the list of
277 gst_multiudpsink_signals[SIGNAL_CLIENT_REMOVED] =
278 g_signal_new ("client-removed", G_TYPE_FROM_CLASS (klass),
279 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiUDPSinkClass,
280 client_removed), NULL, NULL, gst_udp_marshal_VOID__STRING_INT,
281 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
283 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTES_TO_SERVE,
284 g_param_spec_uint64 ("bytes-to-serve", "Bytes to serve",
285 "Number of bytes received to serve to clients", 0, G_MAXUINT64, 0,
287 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTES_SERVED,
288 g_param_spec_uint64 ("bytes-served", "Bytes served",
289 "Total number of bytes send to all clients", 0, G_MAXUINT64, 0,
291 g_object_class_install_property (gobject_class, PROP_SOCKFD,
292 g_param_spec_int ("sockfd", "Socket Handle",
293 "Socket to use for UDP sending. (-1 == allocate)",
294 -1, G_MAXINT, DEFAULT_SOCKFD, G_PARAM_READWRITE));
295 g_object_class_install_property (gobject_class, PROP_CLOSEFD,
296 g_param_spec_boolean ("closefd", "Close sockfd",
297 "Close sockfd if passed as property on state change",
298 DEFAULT_CLOSEFD, G_PARAM_READWRITE));
299 g_object_class_install_property (gobject_class, PROP_SOCK,
300 g_param_spec_int ("sock", "Socket Handle",
301 "Socket currently in use for UDP sending. (-1 == no socket)",
302 -1, G_MAXINT, DEFAULT_SOCK, G_PARAM_READABLE));
303 g_object_class_install_property (gobject_class, PROP_CLIENTS,
304 g_param_spec_string ("clients", "Clients",
305 "A comma separated list of host:port pairs with destinations",
306 DEFAULT_CLIENTS, G_PARAM_READWRITE));
307 g_object_class_install_property (gobject_class, PROP_AUTO_MULTICAST,
308 g_param_spec_boolean ("auto-multicast",
309 "Automatically join/leave multicast groups",
310 "Automatically join/leave the multicast groups, FALSE means user"
311 " has to do it himself", DEFAULT_AUTO_MULTICAST, G_PARAM_READWRITE));
312 g_object_class_install_property (gobject_class, PROP_TTL,
313 g_param_spec_int ("ttl", "Unicast TTL",
314 "Used for setting the unicast TTL parameter",
315 0, 255, DEFAULT_TTL, G_PARAM_READWRITE));
316 g_object_class_install_property (gobject_class, PROP_TTL_MC,
317 g_param_spec_int ("ttl-mc", "Multicast TTL",
318 "Used for setting the multicast TTL parameter",
319 0, 255, DEFAULT_TTL_MC, G_PARAM_READWRITE));
320 g_object_class_install_property (gobject_class, PROP_LOOP,
321 g_param_spec_boolean ("loop", "Multicast Loopback",
322 "Used for setting the multicast loop parameter. TRUE = enable,"
323 " FALSE = disable", DEFAULT_LOOP, G_PARAM_READWRITE));
324 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QOS_DSCP,
325 g_param_spec_int ("qos-dscp", "QoS diff srv code point",
326 "Quality of Service, differentiated services code point (-1 default)",
327 -1, 63, DEFAULT_QOS_DSCP, G_PARAM_READWRITE));
329 gstelement_class->change_state = gst_multiudpsink_change_state;
331 gstbasesink_class->render = gst_multiudpsink_render;
333 gstbasesink_class->render_list = gst_multiudpsink_render_list;
335 klass->add = gst_multiudpsink_add;
336 klass->remove = gst_multiudpsink_remove;
337 klass->clear = gst_multiudpsink_clear;
338 klass->get_stats = gst_multiudpsink_get_stats;
340 GST_DEBUG_CATEGORY_INIT (multiudpsink_debug, "multiudpsink", 0, "UDP sink");
345 gst_multiudpsink_init (GstMultiUDPSink * sink)
349 sink->client_lock = g_mutex_new ();
350 sink->sock = DEFAULT_SOCK;
351 sink->sockfd = DEFAULT_SOCKFD;
352 sink->closefd = DEFAULT_CLOSEFD;
353 sink->externalfd = (sink->sockfd != -1);
354 sink->auto_multicast = DEFAULT_AUTO_MULTICAST;
355 sink->ttl = DEFAULT_TTL;
356 sink->ttl_mc = DEFAULT_TTL_MC;
357 sink->loop = DEFAULT_LOOP;
358 sink->qos_dscp = DEFAULT_QOS_DSCP;
362 gst_multiudpsink_finalize (GObject * object)
364 GstMultiUDPSink *sink;
366 sink = GST_MULTIUDPSINK (object);
368 g_list_foreach (sink->clients, (GFunc) free_client, NULL);
369 g_list_free (sink->clients);
371 if (sink->sockfd >= 0 && sink->closefd)
372 CLOSE_SOCKET (sink->sockfd);
374 g_mutex_free (sink->client_lock);
376 WSA_CLEANUP (object);
378 G_OBJECT_CLASS (parent_class)->finalize (object);
382 socket_error_is_ignorable ()
385 /* Windows doesn't seem to have an EAGAIN for sockets */
386 return WSAGetLastError () == WSAEINTR;
388 return errno == EINTR || errno == EAGAIN;
393 socket_last_error_code ()
396 return WSAGetLastError ();
403 socket_last_error_message ()
406 int errorcode = WSAGetLastError ();
409 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
410 NULL, errorcode, 0, (LPSTR) buf, sizeof (buf) / sizeof (wchar_t), NULL);
411 if (FAILED (result)) {
412 return g_strdup ("failed to get error message from system");
415 g_convert ((gchar *) buf, -1, "UTF-16", "UTF-8", NULL, NULL, NULL);
416 /* g_convert() internally calls windows functions which reset the
417 windows error code, so fix it up again like this */
418 WSASetLastError (errorcode);
422 return g_strdup (g_strerror (errno));
427 gst_multiudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
429 GstMultiUDPSink *sink;
430 gint ret, size, num = 0, no_clients = 0;
435 sink = GST_MULTIUDPSINK (bsink);
437 size = GST_BUFFER_SIZE (buffer);
438 data = GST_BUFFER_DATA (buffer);
440 sink->bytes_to_serve += size;
442 /* grab lock while iterating and sending to clients, this should be
443 * fast as UDP never blocks */
444 g_mutex_lock (sink->client_lock);
445 GST_LOG_OBJECT (bsink, "about to send %d bytes", size);
447 for (clients = sink->clients; clients; clients = g_list_next (clients)) {
448 GstUDPClient *client;
450 client = (GstUDPClient *) clients->data;
452 GST_LOG_OBJECT (sink, "sending %d bytes to client %p", size, client);
455 len = gst_udp_get_sockaddr_length (&client->theiraddr);
457 ret = sendto (*client->sock,
463 size, 0, (struct sockaddr *) &client->theiraddr, len);
466 /* some error, just warn, it's likely recoverable and we don't want to
467 * break streaming. We break so that we stop retrying for this client. */
468 if (!socket_error_is_ignorable ()) {
469 gchar *errormessage = socket_last_error_message ();
470 GST_WARNING_OBJECT (sink, "client %p gave error %d (%s)", client,
471 socket_last_error_code (), errormessage);
472 g_free (errormessage);
477 client->bytes_sent += ret;
478 client->packets_sent++;
479 sink->bytes_served += ret;
484 g_mutex_unlock (sink->client_lock);
486 GST_LOG_OBJECT (sink, "sent %d bytes to %d (of %d) clients", size, num,
494 gst_multiudpsink_render_list (GstBaseSink * bsink, GstBufferList * list)
496 GstMultiUDPSink *sink;
498 gint ret, size = 0, num = 0, no_clients = 0;
500 struct msghdr msg = { 0 };
502 GstBufferListIterator *it;
506 sink = GST_MULTIUDPSINK (bsink);
508 g_return_val_if_fail (list != NULL, GST_FLOW_ERROR);
510 it = gst_buffer_list_iterate (list);
511 g_return_val_if_fail (it != NULL, GST_FLOW_ERROR);
513 while (gst_buffer_list_iterator_next_group (it)) {
517 if ((gsize = gst_buffer_list_iterator_n_buffers (it)) == 0) {
521 iov = (struct iovec *) g_malloc (gsize * sizeof (struct iovec));
524 while ((buf = gst_buffer_list_iterator_next (it))) {
525 msg.msg_iov[msg.msg_iovlen].iov_len = GST_BUFFER_SIZE (buf);
526 msg.msg_iov[msg.msg_iovlen].iov_base = GST_BUFFER_DATA (buf);
528 size += GST_BUFFER_SIZE (buf);
531 sink->bytes_to_serve += size;
533 /* grab lock while iterating and sending to clients, this should be
534 * fast as UDP never blocks */
535 g_mutex_lock (sink->client_lock);
536 GST_LOG_OBJECT (bsink, "about to send %d bytes", size);
538 for (clients = sink->clients; clients; clients = g_list_next (clients)) {
539 GstUDPClient *client;
541 client = (GstUDPClient *) clients->data;
543 GST_LOG_OBJECT (sink, "sending %d bytes to client %p", size, client);
546 msg.msg_name = (void *) &client->theiraddr;
547 msg.msg_namelen = sizeof (client->theiraddr);
548 ret = sendmsg (*client->sock, &msg, 0);
551 if (!socket_error_is_ignorable ()) {
556 client->bytes_sent += ret;
557 client->packets_sent++;
558 sink->bytes_served += ret;
563 g_mutex_unlock (sink->client_lock);
568 GST_LOG_OBJECT (sink, "sent %d bytes to %d (of %d) clients", size, num,
572 gst_buffer_list_iterator_free (it);
577 gst_buffer_list_iterator_free (it);
578 return GST_FLOW_ERROR;
583 gst_multiudpsink_set_clients_string (GstMultiUDPSink * sink,
584 const gchar * string)
589 clients = g_strsplit (string, ",", 0);
591 g_mutex_lock (sink->client_lock);
592 /* clear all existing clients */
593 gst_multiudpsink_clear_internal (sink, FALSE);
594 for (i = 0; clients[i]; i++) {
599 p = strstr (clients[i], ":");
605 gst_multiudpsink_add_internal (sink, host, port, FALSE);
607 g_mutex_unlock (sink->client_lock);
609 g_strfreev (clients);
613 gst_multiudpsink_get_clients_string (GstMultiUDPSink * sink)
618 str = g_string_new ("");
620 g_mutex_lock (sink->client_lock);
621 clients = sink->clients;
623 GstUDPClient *client;
625 client = (GstUDPClient *) clients->data;
627 clients = g_list_next (clients);
629 g_string_append_printf (str, "%s:%d%s", client->host, client->port,
630 (clients ? "," : ""));
632 g_mutex_unlock (sink->client_lock);
634 return g_string_free (str, FALSE);
638 gst_multiudpsink_setup_qos_dscp (GstMultiUDPSink * sink)
642 /* don't touch on -1 */
643 if (sink->qos_dscp < 0)
649 GST_DEBUG_OBJECT (sink, "setting TOS to %d", sink->qos_dscp);
651 /* Extract and shift 6 bits of DSFIELD */
652 tos = (sink->qos_dscp & 0x3f) << 2;
654 if (setsockopt (sink->sock, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0) {
655 gchar *errormessage = socket_last_error_message ();
656 GST_ERROR_OBJECT (sink, "could not set TOS: %s", errormessage);
657 g_free (errormessage);
660 if (setsockopt (sink->sock, IPPROTO_IPV6, IPV6_TCLASS, &tos,
662 gchar *errormessage = socket_last_error_message ();
663 GST_ERROR_OBJECT (sink, "could not set TCLASS: %s", errormessage);
664 g_free (errormessage);
670 gst_multiudpsink_set_property (GObject * object, guint prop_id,
671 const GValue * value, GParamSpec * pspec)
673 GstMultiUDPSink *udpsink;
675 udpsink = GST_MULTIUDPSINK (object);
679 if (udpsink->sockfd >= 0 && udpsink->sockfd != udpsink->sock &&
681 CLOSE_SOCKET (udpsink->sockfd);
682 udpsink->sockfd = g_value_get_int (value);
683 GST_DEBUG_OBJECT (udpsink, "setting SOCKFD to %d", udpsink->sockfd);
686 udpsink->closefd = g_value_get_boolean (value);
689 gst_multiudpsink_set_clients_string (udpsink, g_value_get_string (value));
691 case PROP_AUTO_MULTICAST:
692 udpsink->auto_multicast = g_value_get_boolean (value);
695 udpsink->ttl = g_value_get_int (value);
698 udpsink->ttl_mc = g_value_get_int (value);
701 udpsink->loop = g_value_get_boolean (value);
704 udpsink->qos_dscp = g_value_get_int (value);
705 gst_multiudpsink_setup_qos_dscp (udpsink);
708 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
714 gst_multiudpsink_get_property (GObject * object, guint prop_id, GValue * value,
717 GstMultiUDPSink *udpsink;
719 udpsink = GST_MULTIUDPSINK (object);
722 case PROP_BYTES_TO_SERVE:
723 g_value_set_uint64 (value, udpsink->bytes_to_serve);
725 case PROP_BYTES_SERVED:
726 g_value_set_uint64 (value, udpsink->bytes_served);
729 g_value_set_int (value, udpsink->sockfd);
732 g_value_set_boolean (value, udpsink->closefd);
735 g_value_set_int (value, udpsink->sock);
738 g_value_take_string (value,
739 gst_multiudpsink_get_clients_string (udpsink));
741 case PROP_AUTO_MULTICAST:
742 g_value_set_boolean (value, udpsink->auto_multicast);
745 g_value_set_int (value, udpsink->ttl);
748 g_value_set_int (value, udpsink->ttl_mc);
751 g_value_set_boolean (value, udpsink->loop);
754 g_value_set_int (value, udpsink->qos_dscp);
757 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
763 gst_multiudpsink_configure_client (GstMultiUDPSink * sink,
764 GstUDPClient * client)
766 GST_DEBUG_OBJECT (sink, "configuring client %p", client);
768 if (gst_udp_is_multicast (&client->theiraddr)) {
769 GST_DEBUG_OBJECT (sink, "we have a multicast client %p", client);
770 if (sink->auto_multicast) {
771 GST_DEBUG_OBJECT (sink, "autojoining group");
772 if (gst_udp_join_group (*(client->sock), &client->theiraddr, NULL)
774 goto join_group_failed;
776 GST_DEBUG_OBJECT (sink, "setting loop to %d", sink->loop);
777 if (gst_udp_set_loop (sink->sock, sink->loop) != 0)
779 GST_DEBUG_OBJECT (sink, "setting ttl to %d", sink->ttl_mc);
780 if (gst_udp_set_ttl (sink->sock, sink->ttl_mc, TRUE) != 0)
783 GST_DEBUG_OBJECT (sink, "setting unicast ttl to %d", sink->ttl);
784 if (gst_udp_set_ttl (sink->sock, sink->ttl, FALSE) != 0)
792 gchar *errormessage = socket_last_error_message ();
793 int errorcode = socket_last_error_code ();
794 CLOSE_IF_REQUESTED (sink);
795 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
796 ("Could not join multicast group (%d): %s", errorcode, errormessage));
797 g_free (errormessage);
802 gchar *errormessage = socket_last_error_message ();
803 int errorcode = socket_last_error_code ();
804 CLOSE_IF_REQUESTED (sink);
805 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
806 ("Could not set TTL socket option (%d): %s", errorcode, errormessage));
807 g_free (errormessage);
812 gchar *errormessage = socket_last_error_message ();
813 int errorcode = socket_last_error_code ();
814 CLOSE_IF_REQUESTED (sink);
815 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
816 ("Could not set loopback socket option (%d): %s",
817 errorcode, errormessage));
818 g_free (errormessage);
823 /* create a socket for sending to remote machine */
825 gst_multiudpsink_init_send (GstMultiUDPSink * sink)
829 GstUDPClient *client;
831 if (sink->sockfd == -1) {
832 GST_DEBUG_OBJECT (sink, "creating sockets");
833 /* create sender socket try IP6, fall back to IP4 */
834 if ((sink->sock = socket (AF_INET6, SOCK_DGRAM, 0)) == -1)
835 if ((sink->sock = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
838 GST_DEBUG_OBJECT (sink, "have socket");
839 sink->externalfd = FALSE;
841 GST_DEBUG_OBJECT (sink, "using configured socket");
842 /* we use the configured socket */
843 sink->sock = sink->sockfd;
844 sink->externalfd = TRUE;
848 if (setsockopt (sink->sock, SOL_SOCKET, SO_BROADCAST, &bc_val,
849 sizeof (bc_val)) < 0)
852 sink->bytes_to_serve = 0;
853 sink->bytes_served = 0;
855 gst_multiudpsink_setup_qos_dscp (sink);
857 /* look for multicast clients and join multicast groups appropriately
858 set also ttl and multicast loopback delivery appropriately */
859 for (clients = sink->clients; clients; clients = g_list_next (clients)) {
860 client = (GstUDPClient *) clients->data;
862 if (!gst_multiudpsink_configure_client (sink, client))
870 gchar *errormessage = socket_last_error_message ();
871 int errorcode = socket_last_error_code ();
872 GST_ELEMENT_ERROR (sink, RESOURCE, FAILED, (NULL),
873 ("Could not create socket (%d): %s", errorcode, errormessage));
874 g_free (errormessage);
879 gchar *errormessage = socket_last_error_message ();
880 int errorcode = socket_last_error_code ();
881 CLOSE_IF_REQUESTED (sink);
882 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
883 ("Could not set broadcast socket option (%d): %s",
884 errorcode, errormessage));
885 g_free (errormessage);
891 gst_multiudpsink_close (GstMultiUDPSink * sink)
893 CLOSE_IF_REQUESTED (sink);
897 gst_multiudpsink_add_internal (GstMultiUDPSink * sink, const gchar * host,
898 gint port, gboolean lock)
900 GstUDPClient *client;
903 GST_DEBUG_OBJECT (sink, "adding client on host %s, port %d", host, port);
904 client = g_new0 (GstUDPClient, 1);
905 client->host = g_strdup (host);
907 client->sock = &sink->sock;
909 if (gst_udp_get_addr (host, port, &client->theiraddr) < 0)
910 goto getaddrinfo_error;
912 g_get_current_time (&now);
913 client->connect_time = GST_TIMEVAL_TO_TIME (now);
915 if (*client->sock > 0) {
916 gst_multiudpsink_configure_client (sink, client);
920 g_mutex_lock (sink->client_lock);
921 sink->clients = g_list_prepend (sink->clients, client);
923 g_mutex_unlock (sink->client_lock);
925 g_signal_emit (G_OBJECT (sink),
926 gst_multiudpsink_signals[SIGNAL_CLIENT_ADDED], 0, host, port);
928 GST_DEBUG_OBJECT (sink, "added client on host %s, port %d", host, port);
934 GST_DEBUG_OBJECT (sink, "did not add client on host %s, port %d", host,
936 GST_WARNING_OBJECT (sink, "getaddrinfo lookup error?");
937 g_free (client->host);
944 gst_multiudpsink_add (GstMultiUDPSink * sink, const gchar * host, gint port)
946 gst_multiudpsink_add_internal (sink, host, port, TRUE);
950 client_compare (GstUDPClient * a, GstUDPClient * b)
952 if ((a->port == b->port) && (strcmp (a->host, b->host) == 0))
959 free_client (GstUDPClient * client)
961 g_free (client->host);
966 gst_multiudpsink_remove (GstMultiUDPSink * sink, const gchar * host, gint port)
969 GstUDPClient udpclient;
970 GstUDPClient *client;
973 udpclient.host = (gchar *) host;
974 udpclient.port = port;
976 g_mutex_lock (sink->client_lock);
977 find = g_list_find_custom (sink->clients, &udpclient,
978 (GCompareFunc) client_compare);
982 GST_DEBUG_OBJECT (sink, "remove client with host %s, port %d", host, port);
984 client = (GstUDPClient *) find->data;
986 g_get_current_time (&now);
987 client->disconnect_time = GST_TIMEVAL_TO_TIME (now);
989 if (*(client->sock) != -1 && sink->auto_multicast
990 && gst_udp_is_multicast (&client->theiraddr))
991 gst_udp_leave_group (*(client->sock), &client->theiraddr);
993 /* Unlock to emit signal before we delete the actual client */
994 g_mutex_unlock (sink->client_lock);
995 g_signal_emit (G_OBJECT (sink),
996 gst_multiudpsink_signals[SIGNAL_CLIENT_REMOVED], 0, host, port);
997 g_mutex_lock (sink->client_lock);
999 sink->clients = g_list_delete_link (sink->clients, find);
1001 free_client (client);
1002 g_mutex_unlock (sink->client_lock);
1009 g_mutex_unlock (sink->client_lock);
1010 GST_WARNING_OBJECT (sink, "client at host %s, port %d not found",
1017 gst_multiudpsink_clear_internal (GstMultiUDPSink * sink, gboolean lock)
1019 GST_DEBUG_OBJECT (sink, "clearing");
1020 /* we only need to remove the client structure, there is no additional
1021 * socket or anything to free for UDP */
1023 g_mutex_lock (sink->client_lock);
1024 g_list_foreach (sink->clients, (GFunc) free_client, sink);
1025 g_list_free (sink->clients);
1026 sink->clients = NULL;
1028 g_mutex_unlock (sink->client_lock);
1032 gst_multiudpsink_clear (GstMultiUDPSink * sink)
1034 gst_multiudpsink_clear_internal (sink, TRUE);
1038 gst_multiudpsink_get_stats (GstMultiUDPSink * sink, const gchar * host,
1041 GstUDPClient *client;
1042 GValueArray *result = NULL;
1043 GstUDPClient udpclient;
1045 GValue value = { 0 };
1047 udpclient.host = (gchar *) host;
1048 udpclient.port = port;
1050 g_mutex_lock (sink->client_lock);
1052 find = g_list_find_custom (sink->clients, &udpclient,
1053 (GCompareFunc) client_compare);
1057 GST_DEBUG_OBJECT (sink, "stats for client with host %s, port %d", host, port);
1059 client = (GstUDPClient *) find->data;
1061 /* Result is a value array of (bytes_sent, packets_sent,
1062 * connect_time, disconnect_time), all as uint64 */
1063 result = g_value_array_new (4);
1065 g_value_init (&value, G_TYPE_UINT64);
1066 g_value_set_uint64 (&value, client->bytes_sent);
1067 result = g_value_array_append (result, &value);
1068 g_value_unset (&value);
1070 g_value_init (&value, G_TYPE_UINT64);
1071 g_value_set_uint64 (&value, client->packets_sent);
1072 result = g_value_array_append (result, &value);
1073 g_value_unset (&value);
1075 g_value_init (&value, G_TYPE_UINT64);
1076 g_value_set_uint64 (&value, client->connect_time);
1077 result = g_value_array_append (result, &value);
1078 g_value_unset (&value);
1080 g_value_init (&value, G_TYPE_UINT64);
1081 g_value_set_uint64 (&value, client->disconnect_time);
1082 result = g_value_array_append (result, &value);
1083 g_value_unset (&value);
1085 g_mutex_unlock (sink->client_lock);
1092 g_mutex_unlock (sink->client_lock);
1093 GST_WARNING_OBJECT (sink, "client with host %s, port %d not found",
1095 /* Apparently (see comment in gstmultifdsink.c) returning NULL from here may
1096 * confuse/break python bindings */
1097 return g_value_array_new (0);
1101 static GstStateChangeReturn
1102 gst_multiudpsink_change_state (GstElement * element, GstStateChange transition)
1104 GstStateChangeReturn ret;
1105 GstMultiUDPSink *sink;
1107 sink = GST_MULTIUDPSINK (element);
1109 switch (transition) {
1110 case GST_STATE_CHANGE_READY_TO_PAUSED:
1111 if (!gst_multiudpsink_init_send (sink))
1118 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1120 switch (transition) {
1121 case GST_STATE_CHANGE_PAUSED_TO_READY:
1122 gst_multiudpsink_close (sink);
1132 /* _init_send() posted specific error already */
1133 return GST_STATE_CHANGE_FAILURE;