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_finalize (GObject * object);
118 static GstFlowReturn gst_multiudpsink_render (GstBaseSink * sink,
121 #ifndef G_OS_WIN32 /* sendmsg() is not available on Windows */
122 static GstFlowReturn gst_multiudpsink_render_list (GstBaseSink * bsink,
123 GstBufferList * list);
126 static GstStateChangeReturn gst_multiudpsink_change_state (GstElement *
127 element, GstStateChange transition);
129 static void gst_multiudpsink_set_property (GObject * object, guint prop_id,
130 const GValue * value, GParamSpec * pspec);
131 static void gst_multiudpsink_get_property (GObject * object, guint prop_id,
132 GValue * value, GParamSpec * pspec);
134 static void gst_multiudpsink_add_internal (GstMultiUDPSink * sink,
135 const gchar * host, gint port, gboolean lock);
136 static void gst_multiudpsink_clear_internal (GstMultiUDPSink * sink,
139 static guint gst_multiudpsink_signals[LAST_SIGNAL] = { 0 };
141 #define gst_multiudpsink_parent_class parent_class
142 G_DEFINE_TYPE (GstMultiUDPSink, gst_multiudpsink, GST_TYPE_BASE_SINK);
145 gst_multiudpsink_class_init (GstMultiUDPSinkClass * klass)
147 GObjectClass *gobject_class;
148 GstElementClass *gstelement_class;
149 GstBaseSinkClass *gstbasesink_class;
151 gobject_class = (GObjectClass *) klass;
152 gstelement_class = (GstElementClass *) klass;
153 gstbasesink_class = (GstBaseSinkClass *) klass;
155 parent_class = g_type_class_peek_parent (klass);
157 gobject_class->set_property = gst_multiudpsink_set_property;
158 gobject_class->get_property = gst_multiudpsink_get_property;
159 gobject_class->finalize = gst_multiudpsink_finalize;
162 * GstMultiUDPSink::add:
163 * @gstmultiudpsink: the sink on which the signal is emitted
164 * @host: the hostname/IP address of the client to add
165 * @port: the port of the client to add
167 * Add a client with destination @host and @port to the list of
168 * clients. When the same host/port pair is added multiple times, the
169 * send-duplicates property defines if the packets are sent multiple times to
170 * the same host/port pair or not.
172 * When a host/port pair is added multiple times, an equal amount of remove
173 * calls must be performed to actually remove the host/port pair from the list
176 gst_multiudpsink_signals[SIGNAL_ADD] =
177 g_signal_new ("add", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
178 G_STRUCT_OFFSET (GstMultiUDPSinkClass, add),
179 NULL, NULL, gst_udp_marshal_VOID__STRING_INT, G_TYPE_NONE, 2,
180 G_TYPE_STRING, G_TYPE_INT);
182 * GstMultiUDPSink::remove:
183 * @gstmultiudpsink: the sink on which the signal is emitted
184 * @host: the hostname/IP address of the client to remove
185 * @port: the port of the client to remove
187 * Remove the client with destination @host and @port from the list of
190 gst_multiudpsink_signals[SIGNAL_REMOVE] =
191 g_signal_new ("remove", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
192 G_STRUCT_OFFSET (GstMultiUDPSinkClass, remove),
193 NULL, NULL, gst_udp_marshal_VOID__STRING_INT, G_TYPE_NONE, 2,
194 G_TYPE_STRING, G_TYPE_INT);
196 * GstMultiUDPSink::clear:
197 * @gstmultiudpsink: the sink on which the signal is emitted
199 * Clear the list of clients.
201 gst_multiudpsink_signals[SIGNAL_CLEAR] =
202 g_signal_new ("clear", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
203 G_STRUCT_OFFSET (GstMultiUDPSinkClass, clear),
204 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
206 * GstMultiUDPSink::get-stats:
207 * @gstmultiudpsink: the sink on which the signal is emitted
208 * @host: the hostname/IP address of the client to get stats on
209 * @port: the port of the client to get stats on
211 * Get the statistics of the client with destination @host and @port.
213 * Returns: a GValueArray of uint64: bytes_sent, packets_sent,
214 * connect_time (in epoch seconds), disconnect_time (in epoch seconds)
216 gst_multiudpsink_signals[SIGNAL_GET_STATS] =
217 g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
218 G_STRUCT_OFFSET (GstMultiUDPSinkClass, get_stats),
219 NULL, NULL, gst_udp_marshal_BOXED__STRING_INT, G_TYPE_VALUE_ARRAY, 2,
220 G_TYPE_STRING, G_TYPE_INT);
222 * GstMultiUDPSink::client-added:
223 * @gstmultiudpsink: the sink emitting the signal
224 * @host: the hostname/IP address of the added client
225 * @port: the port of the added client
227 * Signal emited when a new client is added to the list of
230 gst_multiudpsink_signals[SIGNAL_CLIENT_ADDED] =
231 g_signal_new ("client-added", G_TYPE_FROM_CLASS (klass),
232 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiUDPSinkClass, client_added),
233 NULL, NULL, gst_udp_marshal_VOID__STRING_INT, G_TYPE_NONE, 2,
234 G_TYPE_STRING, G_TYPE_INT);
236 * GstMultiUDPSink::client-removed:
237 * @gstmultiudpsink: the sink emitting the signal
238 * @host: the hostname/IP address of the removed client
239 * @port: the port of the removed client
241 * Signal emited when a client is removed from the list of
244 gst_multiudpsink_signals[SIGNAL_CLIENT_REMOVED] =
245 g_signal_new ("client-removed", G_TYPE_FROM_CLASS (klass),
246 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiUDPSinkClass,
247 client_removed), NULL, NULL, gst_udp_marshal_VOID__STRING_INT,
248 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
250 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTES_TO_SERVE,
251 g_param_spec_uint64 ("bytes-to-serve", "Bytes to serve",
252 "Number of bytes received to serve to clients", 0, G_MAXUINT64, 0,
253 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
254 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTES_SERVED,
255 g_param_spec_uint64 ("bytes-served", "Bytes served",
256 "Total number of bytes send to all clients", 0, G_MAXUINT64, 0,
257 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
258 g_object_class_install_property (gobject_class, PROP_SOCKFD,
259 g_param_spec_int ("sockfd", "Socket Handle",
260 "Socket to use for UDP sending. (-1 == allocate)",
261 -1, G_MAXINT, DEFAULT_SOCKFD,
262 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
263 g_object_class_install_property (gobject_class, PROP_CLOSEFD,
264 g_param_spec_boolean ("closefd", "Close sockfd",
265 "Close sockfd if passed as property on state change",
266 DEFAULT_CLOSEFD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
267 g_object_class_install_property (gobject_class, PROP_SOCK,
268 g_param_spec_int ("sock", "Socket Handle",
269 "Socket currently in use for UDP sending. (-1 == no socket)",
270 -1, G_MAXINT, DEFAULT_SOCK,
271 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
272 g_object_class_install_property (gobject_class, PROP_CLIENTS,
273 g_param_spec_string ("clients", "Clients",
274 "A comma separated list of host:port pairs with destinations",
275 DEFAULT_CLIENTS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
276 g_object_class_install_property (gobject_class, PROP_AUTO_MULTICAST,
277 g_param_spec_boolean ("auto-multicast",
278 "Automatically join/leave multicast groups",
279 "Automatically join/leave the multicast groups, FALSE means user"
280 " has to do it himself", DEFAULT_AUTO_MULTICAST,
281 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
282 g_object_class_install_property (gobject_class, PROP_TTL,
283 g_param_spec_int ("ttl", "Unicast TTL",
284 "Used for setting the unicast TTL parameter",
285 0, 255, DEFAULT_TTL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
286 g_object_class_install_property (gobject_class, PROP_TTL_MC,
287 g_param_spec_int ("ttl-mc", "Multicast TTL",
288 "Used for setting the multicast TTL parameter",
289 0, 255, DEFAULT_TTL_MC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
290 g_object_class_install_property (gobject_class, PROP_LOOP,
291 g_param_spec_boolean ("loop", "Multicast Loopback",
292 "Used for setting the multicast loop parameter. TRUE = enable,"
293 " FALSE = disable", DEFAULT_LOOP,
294 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
295 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QOS_DSCP,
296 g_param_spec_int ("qos-dscp", "QoS diff srv code point",
297 "Quality of Service, differentiated services code point (-1 default)",
298 -1, 63, DEFAULT_QOS_DSCP,
299 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
301 * GstMultiUDPSink::send-duplicates
303 * When a host/port pair is added mutliple times, send the packet to the host
304 * multiple times as well.
308 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEND_DUPLICATES,
309 g_param_spec_boolean ("send-duplicates", "Send Duplicates",
310 "When a distination/port pair is added multiple times, send packets "
311 "multiple times as well", DEFAULT_SEND_DUPLICATES,
312 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
314 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_SIZE,
315 g_param_spec_int ("buffer-size", "Buffer Size",
316 "Size of the kernel send buffer in bytes, 0=default", 0, G_MAXINT,
317 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
319 gstelement_class->change_state = gst_multiudpsink_change_state;
321 gst_element_class_add_pad_template (gstelement_class,
322 gst_static_pad_template_get (&sink_template));
324 gst_element_class_set_details_simple (gstelement_class, "UDP packet sender",
326 "Send data over the network via UDP",
327 "Wim Taymans <wim.taymans@gmail.com>");
329 gstbasesink_class->render = gst_multiudpsink_render;
332 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;
359 sink->ss_family = DEFAULT_FAMILY;
360 sink->send_duplicates = DEFAULT_SEND_DUPLICATES;
363 static GstUDPClient *
364 create_client (GstMultiUDPSink * sink, const gchar * host, gint port)
366 GstUDPClient *client;
368 client = g_slice_new0 (GstUDPClient);
369 client->refcount = 1;
370 client->host = g_strdup (host);
377 free_client (GstUDPClient * client)
379 g_free (client->host);
380 g_slice_free (GstUDPClient, client);
384 client_compare (GstUDPClient * a, GstUDPClient * b)
386 if ((a->port == b->port) && (strcmp (a->host, b->host) == 0))
393 gst_multiudpsink_finalize (GObject * object)
395 GstMultiUDPSink *sink;
397 sink = GST_MULTIUDPSINK (object);
399 g_list_foreach (sink->clients, (GFunc) free_client, NULL);
400 g_list_free (sink->clients);
402 if (sink->sockfd >= 0 && sink->closefd)
403 CLOSE_SOCKET (sink->sockfd);
405 g_mutex_free (sink->client_lock);
407 WSA_CLEANUP (object);
409 G_OBJECT_CLASS (parent_class)->finalize (object);
413 socket_error_is_ignorable (void)
416 /* Windows doesn't seem to have an EAGAIN for sockets */
417 return WSAGetLastError () == WSAEINTR;
419 return errno == EINTR || errno == EAGAIN;
424 socket_last_error_code (void)
427 return WSAGetLastError ();
434 socket_last_error_message (void)
437 int errorcode = WSAGetLastError ();
440 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
441 NULL, errorcode, 0, (LPSTR) buf, sizeof (buf) / sizeof (wchar_t), NULL);
442 if (FAILED (result)) {
443 return g_strdup ("failed to get error message from system");
446 g_convert ((gchar *) buf, -1, "UTF-16", "UTF-8", NULL, NULL, NULL);
447 /* g_convert() internally calls windows functions which reset the
448 windows error code, so fix it up again like this */
449 WSASetLastError (errorcode);
453 return g_strdup (g_strerror (errno));
458 /* version without sendmsg */
460 gst_multiudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
462 GstMultiUDPSink *sink;
463 gint ret, num = 0, no_clients = 0;
469 sink = GST_MULTIUDPSINK (bsink);
471 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
473 if (size > UDP_MAX_SIZE) {
474 GST_WARNING ("Attempting to send a UDP packet larger than maximum "
475 "size (%d > %d)", size, UDP_MAX_SIZE);
478 sink->bytes_to_serve += size;
480 /* grab lock while iterating and sending to clients, this should be
481 * fast as UDP never blocks */
482 g_mutex_lock (sink->client_lock);
483 GST_LOG_OBJECT (bsink, "about to send %d bytes", size);
485 for (clients = sink->clients; clients; clients = g_list_next (clients)) {
486 GstUDPClient *client;
489 client = (GstUDPClient *) clients->data;
491 GST_LOG_OBJECT (sink, "sending %d bytes to client %p", size, client);
493 count = sink->send_duplicates ? client->refcount : 1;
497 len = gst_udp_get_sockaddr_length (&client->theiraddr);
499 ret = sendto (*client->sock,
505 size, 0, (struct sockaddr *) &client->theiraddr, len);
508 /* some error, just warn, it's likely recoverable and we don't want to
509 * break streaming. We break so that we stop retrying for this client. */
510 if (!socket_error_is_ignorable ()) {
511 gchar *errormessage = socket_last_error_message ();
512 GST_WARNING_OBJECT (sink, "client %p gave error %d (%s)", client,
513 socket_last_error_code (), errormessage);
514 g_free (errormessage);
519 client->bytes_sent += ret;
520 client->packets_sent++;
521 sink->bytes_served += ret;
527 g_mutex_unlock (sink->client_lock);
529 gst_buffer_unmap (buffer, data, size);
531 GST_LOG_OBJECT (sink, "sent %d bytes to %d (of %d) clients", size, num,
536 #else /* !G_OS_WIN32 */
537 /* version with sendmsg */
539 gst_multiudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
541 GstMultiUDPSink *sink;
543 gint ret, size = 0, num = 0, no_clients = 0;
545 struct msghdr msg = { 0 };
551 sink = GST_MULTIUDPSINK (bsink);
556 n_mem = gst_buffer_n_memory (buffer);
560 iov = (struct iovec *) g_malloc (n_mem * sizeof (struct iovec));
563 for (i = 0; i < n_mem; i++) {
564 mem = gst_buffer_peek_memory (buffer, i, GST_MAP_READ);
565 bdata = gst_memory_map (mem, &bsize, NULL, GST_MAP_READ);
567 if (bsize > UDP_MAX_SIZE) {
568 GST_WARNING ("Attempting to send a UDP packet larger than maximum "
569 "size (%d > %d)", bsize, UDP_MAX_SIZE);
572 msg.msg_iov[msg.msg_iovlen].iov_len = bsize;
573 msg.msg_iov[msg.msg_iovlen].iov_base = bdata;
579 sink->bytes_to_serve += size;
581 /* grab lock while iterating and sending to clients, this should be
582 * fast as UDP never blocks */
583 g_mutex_lock (sink->client_lock);
584 GST_LOG_OBJECT (bsink, "about to send %d bytes", size);
586 for (clients = sink->clients; clients; clients = g_list_next (clients)) {
587 GstUDPClient *client;
590 client = (GstUDPClient *) clients->data;
592 GST_LOG_OBJECT (sink, "sending %d bytes to client %p", size, client);
594 count = sink->send_duplicates ? client->refcount : 1;
598 msg.msg_name = (void *) &client->theiraddr;
599 msg.msg_namelen = sizeof (client->theiraddr);
600 ret = sendmsg (*client->sock, &msg, 0);
603 if (!socket_error_is_ignorable ()) {
604 gchar *errormessage = socket_last_error_message ();
605 GST_WARNING_OBJECT (sink, "client %p gave error %d (%s)", client,
606 socket_last_error_code (), errormessage);
607 g_free (errormessage);
613 client->bytes_sent += ret;
614 client->packets_sent++;
615 sink->bytes_served += ret;
621 g_mutex_unlock (sink->client_lock);
623 /* unmap all memory again */
624 for (i = 0; i < n_mem; i++) {
625 mem = gst_buffer_peek_memory (buffer, i, GST_MAP_READ);
627 bsize = msg.msg_iov[i].iov_len;
628 bdata = msg.msg_iov[i].iov_base;
630 gst_memory_unmap (mem, bdata, bsize);
634 GST_LOG_OBJECT (sink, "sent %d bytes to %d (of %d) clients", size, num,
647 /* DISABLED, core sends buffers to our render one by one, we can't really do
650 gst_multiudpsink_render_list (GstBaseSink * bsink, GstBufferList * list)
656 gst_multiudpsink_set_clients_string (GstMultiUDPSink * sink,
657 const gchar * string)
662 clients = g_strsplit (string, ",", 0);
664 g_mutex_lock (sink->client_lock);
665 /* clear all existing clients */
666 gst_multiudpsink_clear_internal (sink, FALSE);
667 for (i = 0; clients[i]; i++) {
672 p = strstr (clients[i], ":");
678 gst_multiudpsink_add_internal (sink, host, port, FALSE);
680 g_mutex_unlock (sink->client_lock);
682 g_strfreev (clients);
686 gst_multiudpsink_get_clients_string (GstMultiUDPSink * sink)
691 str = g_string_new ("");
693 g_mutex_lock (sink->client_lock);
694 clients = sink->clients;
696 GstUDPClient *client;
699 client = (GstUDPClient *) clients->data;
701 clients = g_list_next (clients);
703 count = client->refcount;
705 g_string_append_printf (str, "%s:%d%s", client->host, client->port,
706 (clients || count > 1 ? "," : ""));
709 g_mutex_unlock (sink->client_lock);
711 return g_string_free (str, FALSE);
715 gst_multiudpsink_setup_qos_dscp (GstMultiUDPSink * sink)
719 /* don't touch on -1 */
720 if (sink->qos_dscp < 0)
726 GST_DEBUG_OBJECT (sink, "setting TOS to %d", sink->qos_dscp);
728 /* Extract and shift 6 bits of DSFIELD */
729 tos = (sink->qos_dscp & 0x3f) << 2;
731 if (setsockopt (sink->sock, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0) {
732 gchar *errormessage = socket_last_error_message ();
733 GST_ERROR_OBJECT (sink, "could not set TOS: %s", errormessage);
734 g_free (errormessage);
737 if (setsockopt (sink->sock, IPPROTO_IPV6, IPV6_TCLASS, &tos,
739 gchar *errormessage = socket_last_error_message ();
740 GST_ERROR_OBJECT (sink, "could not set TCLASS: %s", errormessage);
741 g_free (errormessage);
747 gst_multiudpsink_set_property (GObject * object, guint prop_id,
748 const GValue * value, GParamSpec * pspec)
750 GstMultiUDPSink *udpsink;
752 udpsink = GST_MULTIUDPSINK (object);
756 if (udpsink->sockfd >= 0 && udpsink->sockfd != udpsink->sock &&
758 CLOSE_SOCKET (udpsink->sockfd);
759 udpsink->sockfd = g_value_get_int (value);
760 GST_DEBUG_OBJECT (udpsink, "setting SOCKFD to %d", udpsink->sockfd);
763 udpsink->closefd = g_value_get_boolean (value);
766 gst_multiudpsink_set_clients_string (udpsink, g_value_get_string (value));
768 case PROP_AUTO_MULTICAST:
769 udpsink->auto_multicast = g_value_get_boolean (value);
772 udpsink->ttl = g_value_get_int (value);
775 udpsink->ttl_mc = g_value_get_int (value);
778 udpsink->loop = g_value_get_boolean (value);
781 udpsink->qos_dscp = g_value_get_int (value);
782 gst_multiudpsink_setup_qos_dscp (udpsink);
784 case PROP_SEND_DUPLICATES:
785 udpsink->send_duplicates = g_value_get_boolean (value);
787 case PROP_BUFFER_SIZE:
788 udpsink->buffer_size = g_value_get_int (value);
791 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
797 gst_multiudpsink_get_property (GObject * object, guint prop_id, GValue * value,
800 GstMultiUDPSink *udpsink;
802 udpsink = GST_MULTIUDPSINK (object);
805 case PROP_BYTES_TO_SERVE:
806 g_value_set_uint64 (value, udpsink->bytes_to_serve);
808 case PROP_BYTES_SERVED:
809 g_value_set_uint64 (value, udpsink->bytes_served);
812 g_value_set_int (value, udpsink->sockfd);
815 g_value_set_boolean (value, udpsink->closefd);
818 g_value_set_int (value, udpsink->sock);
821 g_value_take_string (value,
822 gst_multiudpsink_get_clients_string (udpsink));
824 case PROP_AUTO_MULTICAST:
825 g_value_set_boolean (value, udpsink->auto_multicast);
828 g_value_set_int (value, udpsink->ttl);
831 g_value_set_int (value, udpsink->ttl_mc);
834 g_value_set_boolean (value, udpsink->loop);
837 g_value_set_int (value, udpsink->qos_dscp);
839 case PROP_SEND_DUPLICATES:
840 g_value_set_boolean (value, udpsink->send_duplicates);
842 case PROP_BUFFER_SIZE:
843 g_value_set_int (value, udpsink->buffer_size);
846 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
852 gst_multiudpsink_configure_client (GstMultiUDPSink * sink,
853 GstUDPClient * client)
855 GST_DEBUG_OBJECT (sink, "configuring client %p", client);
857 if (gst_udp_is_multicast (&client->theiraddr)) {
858 GST_DEBUG_OBJECT (sink, "we have a multicast client %p", client);
859 if (sink->auto_multicast) {
860 GST_DEBUG_OBJECT (sink, "autojoining group");
861 if (gst_udp_join_group (*(client->sock), &client->theiraddr, NULL)
863 goto join_group_failed;
865 GST_DEBUG_OBJECT (sink, "setting loop to %d", sink->loop);
866 if (gst_udp_set_loop (sink->sock, sink->ss_family, sink->loop) != 0)
868 GST_DEBUG_OBJECT (sink, "setting ttl to %d", sink->ttl_mc);
869 if (gst_udp_set_ttl (sink->sock, sink->ss_family, sink->ttl_mc, TRUE) != 0)
872 GST_DEBUG_OBJECT (sink, "setting unicast ttl to %d", sink->ttl);
873 if (gst_udp_set_ttl (sink->sock, sink->ss_family, sink->ttl, FALSE) != 0)
881 gchar *errormessage = socket_last_error_message ();
882 int errorcode = socket_last_error_code ();
883 CLOSE_IF_REQUESTED (sink);
884 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
885 ("Could not join multicast group (%d): %s", errorcode, errormessage));
886 g_free (errormessage);
891 gchar *errormessage = socket_last_error_message ();
892 int errorcode = socket_last_error_code ();
893 CLOSE_IF_REQUESTED (sink);
894 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
895 ("Could not set TTL socket option (%d): %s", errorcode, errormessage));
896 g_free (errormessage);
901 gchar *errormessage = socket_last_error_message ();
902 int errorcode = socket_last_error_code ();
903 CLOSE_IF_REQUESTED (sink);
904 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
905 ("Could not set loopback socket option (%d): %s",
906 errorcode, errormessage));
907 g_free (errormessage);
912 /* create a socket for sending to remote machine */
914 gst_multiudpsink_init_send (GstMultiUDPSink * sink)
918 GstUDPClient *client;
922 if (sink->sockfd == -1) {
923 GST_DEBUG_OBJECT (sink, "creating sockets");
924 /* create sender socket try IP6, fall back to IP4 */
925 sink->ss_family = AF_INET6;
926 if ((sink->sock = socket (AF_INET6, SOCK_DGRAM, 0)) == -1) {
927 sink->ss_family = AF_INET;
928 if ((sink->sock = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
932 GST_DEBUG_OBJECT (sink, "have socket");
933 sink->externalfd = FALSE;
935 struct sockaddr_storage myaddr;
937 GST_DEBUG_OBJECT (sink, "using configured socket");
938 /* we use the configured socket, try to get some info about it */
939 len = sizeof (myaddr);
940 if (getsockname (sink->sockfd, (struct sockaddr *) &myaddr, &len) < 0)
941 goto getsockname_error;
943 sink->ss_family = myaddr.ss_family;
944 /* we use the configured socket */
945 sink->sock = sink->sockfd;
946 sink->externalfd = TRUE;
949 len = sizeof (sndsize);
950 if (sink->buffer_size != 0) {
951 sndsize = sink->buffer_size;
953 GST_DEBUG_OBJECT (sink, "setting udp buffer of %d bytes", sndsize);
954 /* set buffer size, Note that on Linux this is typically limited to a
955 * maximum of around 100K. Also a minimum of 128 bytes is required on
958 setsockopt (sink->sockfd, SOL_SOCKET, SO_SNDBUF, (void *) &sndsize,
961 GST_ELEMENT_WARNING (sink, RESOURCE, SETTINGS, (NULL),
962 ("Could not create a buffer of requested %d bytes, %d: %s (%d)",
963 sndsize, ret, g_strerror (errno), errno));
967 /* read the value of the receive buffer. Note that on linux this returns 2x the
968 * value we set because the kernel allocates extra memory for metadata.
969 * The default on Linux is about 100K (which is about 50K without metadata) */
971 getsockopt (sink->sockfd, SOL_SOCKET, SO_SNDBUF, (void *) &sndsize, &len);
973 GST_DEBUG_OBJECT (sink, "have udp buffer of %d bytes", sndsize);
975 GST_DEBUG_OBJECT (sink, "could not get udp buffer size");
979 if (setsockopt (sink->sock, SOL_SOCKET, SO_BROADCAST, &bc_val,
980 sizeof (bc_val)) < 0)
983 sink->bytes_to_serve = 0;
984 sink->bytes_served = 0;
986 gst_multiudpsink_setup_qos_dscp (sink);
988 /* look for multicast clients and join multicast groups appropriately
989 set also ttl and multicast loopback delivery appropriately */
990 for (clients = sink->clients; clients; clients = g_list_next (clients)) {
991 client = (GstUDPClient *) clients->data;
993 if (!gst_multiudpsink_configure_client (sink, client))
1001 gchar *errormessage = socket_last_error_message ();
1002 int errorcode = socket_last_error_code ();
1003 GST_ELEMENT_ERROR (sink, RESOURCE, FAILED, (NULL),
1004 ("Could not create socket (%d): %s", errorcode, errormessage));
1005 g_free (errormessage);
1010 gchar *errormessage = socket_last_error_message ();
1011 int errorcode = socket_last_error_code ();
1012 GST_ELEMENT_ERROR (sink, RESOURCE, FAILED, (NULL),
1013 ("Could not getsockname (%d): %s", errorcode, errormessage));
1014 g_free (errormessage);
1019 gchar *errormessage = socket_last_error_message ();
1020 int errorcode = socket_last_error_code ();
1021 CLOSE_IF_REQUESTED (sink);
1022 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
1023 ("Could not set broadcast socket option (%d): %s",
1024 errorcode, errormessage));
1025 g_free (errormessage);
1031 gst_multiudpsink_close (GstMultiUDPSink * sink)
1033 CLOSE_IF_REQUESTED (sink);
1037 gst_multiudpsink_add_internal (GstMultiUDPSink * sink, const gchar * host,
1038 gint port, gboolean lock)
1040 GstUDPClient *client;
1041 GstUDPClient udpclient;
1045 udpclient.host = (gchar *) host;
1046 udpclient.port = port;
1048 GST_DEBUG_OBJECT (sink, "adding client on host %s, port %d", host, port);
1051 g_mutex_lock (sink->client_lock);
1053 find = g_list_find_custom (sink->clients, &udpclient,
1054 (GCompareFunc) client_compare);
1056 client = (GstUDPClient *) find->data;
1058 GST_DEBUG_OBJECT (sink, "found %d existing clients with host %s, port %d",
1059 client->refcount, host, port);
1062 client = create_client (sink, host, port);
1064 client->sock = &sink->sock;
1066 if (gst_udp_get_addr (host, port, &client->theiraddr) < 0)
1067 goto getaddrinfo_error;
1069 g_get_current_time (&now);
1070 client->connect_time = GST_TIMEVAL_TO_TIME (now);
1072 if (*client->sock > 0) {
1073 gst_multiudpsink_configure_client (sink, client);
1076 GST_DEBUG_OBJECT (sink, "add client with host %s, port %d", host, port);
1077 sink->clients = g_list_prepend (sink->clients, client);
1081 g_mutex_unlock (sink->client_lock);
1083 g_signal_emit (G_OBJECT (sink),
1084 gst_multiudpsink_signals[SIGNAL_CLIENT_ADDED], 0, host, port);
1086 GST_DEBUG_OBJECT (sink, "added client on host %s, port %d", host, port);
1092 GST_DEBUG_OBJECT (sink, "did not add client on host %s, port %d", host,
1094 GST_WARNING_OBJECT (sink, "getaddrinfo lookup error?");
1095 free_client (client);
1097 g_mutex_unlock (sink->client_lock);
1103 gst_multiudpsink_add (GstMultiUDPSink * sink, const gchar * host, gint port)
1105 gst_multiudpsink_add_internal (sink, host, port, TRUE);
1109 gst_multiudpsink_remove (GstMultiUDPSink * sink, const gchar * host, gint port)
1112 GstUDPClient udpclient;
1113 GstUDPClient *client;
1116 udpclient.host = (gchar *) host;
1117 udpclient.port = port;
1119 g_mutex_lock (sink->client_lock);
1120 find = g_list_find_custom (sink->clients, &udpclient,
1121 (GCompareFunc) client_compare);
1125 client = (GstUDPClient *) find->data;
1127 GST_DEBUG_OBJECT (sink, "found %d clients with host %s, port %d",
1128 client->refcount, host, port);
1131 if (client->refcount == 0) {
1132 GST_DEBUG_OBJECT (sink, "remove client with host %s, port %d", host, port);
1134 g_get_current_time (&now);
1135 client->disconnect_time = GST_TIMEVAL_TO_TIME (now);
1137 if (*(client->sock) != -1 && sink->auto_multicast
1138 && gst_udp_is_multicast (&client->theiraddr))
1139 gst_udp_leave_group (*(client->sock), &client->theiraddr);
1141 /* Unlock to emit signal before we delete the actual client */
1142 g_mutex_unlock (sink->client_lock);
1143 g_signal_emit (G_OBJECT (sink),
1144 gst_multiudpsink_signals[SIGNAL_CLIENT_REMOVED], 0, host, port);
1145 g_mutex_lock (sink->client_lock);
1147 sink->clients = g_list_delete_link (sink->clients, find);
1149 free_client (client);
1151 g_mutex_unlock (sink->client_lock);
1158 g_mutex_unlock (sink->client_lock);
1159 GST_WARNING_OBJECT (sink, "client at host %s, port %d not found",
1166 gst_multiudpsink_clear_internal (GstMultiUDPSink * sink, gboolean lock)
1168 GST_DEBUG_OBJECT (sink, "clearing");
1169 /* we only need to remove the client structure, there is no additional
1170 * socket or anything to free for UDP */
1172 g_mutex_lock (sink->client_lock);
1173 g_list_foreach (sink->clients, (GFunc) free_client, sink);
1174 g_list_free (sink->clients);
1175 sink->clients = NULL;
1177 g_mutex_unlock (sink->client_lock);
1181 gst_multiudpsink_clear (GstMultiUDPSink * sink)
1183 gst_multiudpsink_clear_internal (sink, TRUE);
1187 gst_multiudpsink_get_stats (GstMultiUDPSink * sink, const gchar * host,
1190 GstUDPClient *client;
1191 GValueArray *result = NULL;
1192 GstUDPClient udpclient;
1194 GValue value = { 0 };
1196 udpclient.host = (gchar *) host;
1197 udpclient.port = port;
1199 g_mutex_lock (sink->client_lock);
1201 find = g_list_find_custom (sink->clients, &udpclient,
1202 (GCompareFunc) client_compare);
1206 GST_DEBUG_OBJECT (sink, "stats for client with host %s, port %d", host, port);
1208 client = (GstUDPClient *) find->data;
1210 /* Result is a value array of (bytes_sent, packets_sent,
1211 * connect_time, disconnect_time), all as uint64 */
1212 result = g_value_array_new (4);
1214 g_value_init (&value, G_TYPE_UINT64);
1215 g_value_set_uint64 (&value, client->bytes_sent);
1216 result = g_value_array_append (result, &value);
1217 g_value_unset (&value);
1219 g_value_init (&value, G_TYPE_UINT64);
1220 g_value_set_uint64 (&value, client->packets_sent);
1221 result = g_value_array_append (result, &value);
1222 g_value_unset (&value);
1224 g_value_init (&value, G_TYPE_UINT64);
1225 g_value_set_uint64 (&value, client->connect_time);
1226 result = g_value_array_append (result, &value);
1227 g_value_unset (&value);
1229 g_value_init (&value, G_TYPE_UINT64);
1230 g_value_set_uint64 (&value, client->disconnect_time);
1231 result = g_value_array_append (result, &value);
1232 g_value_unset (&value);
1234 g_mutex_unlock (sink->client_lock);
1241 g_mutex_unlock (sink->client_lock);
1242 GST_WARNING_OBJECT (sink, "client with host %s, port %d not found",
1244 /* Apparently (see comment in gstmultifdsink.c) returning NULL from here may
1245 * confuse/break python bindings */
1246 return g_value_array_new (0);
1250 static GstStateChangeReturn
1251 gst_multiudpsink_change_state (GstElement * element, GstStateChange transition)
1253 GstStateChangeReturn ret;
1254 GstMultiUDPSink *sink;
1256 sink = GST_MULTIUDPSINK (element);
1258 switch (transition) {
1259 case GST_STATE_CHANGE_READY_TO_PAUSED:
1260 if (!gst_multiudpsink_init_send (sink))
1267 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1269 switch (transition) {
1270 case GST_STATE_CHANGE_PAUSED_TO_READY:
1271 gst_multiudpsink_close (sink);
1281 /* _init_send() posted specific error already */
1282 return GST_STATE_CHANGE_FAILURE;