2 * Copyright (C) <2005> Wim Taymans <wim@fluendo.com>
3 * Copyright (C) <2005> Nokia Corporation <kai.vehmanen@nokia.com>
4 * Copyright (C) <2012> Collabora Ltd.
5 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
6 * Copyright (C) 2014 Tim-Philipp Müller <tim@centricular.com>
7 * Copyright (C) 2014 Centricular Ltd
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
26 * SECTION:element-udpsrc
27 * @see_also: udpsink, multifdsink
29 * udpsrc is a network source that reads UDP packets from the network.
30 * It can be combined with RTP depayloaders to implement RTP streaming.
32 * The udpsrc element supports automatic port allocation by setting the
33 * #GstUDPSrc:port property to 0. After setting the udpsrc to PAUSED, the
34 * allocated port can be obtained by reading the port property.
36 * udpsrc can read from multicast groups by setting the #GstUDPSrc:multicast-group
37 * property to the IP address of the multicast group.
39 * Alternatively one can provide a custom socket to udpsrc with the #GstUDPSrc:socket
40 * property, udpsrc will then not allocate a socket itself but use the provided
43 * The #GstUDPSrc:caps property is mainly used to give a type to the UDP packet
44 * so that they can be autoplugged in GStreamer pipelines. This is very useful
45 * for RTP implementations where the contents of the UDP packets is transfered
46 * out-of-bounds using SDP or other means.
48 * The #GstUDPSrc:buffer-size property is used to change the default kernel
49 * buffersizes used for receiving packets. The buffer size may be increased for
50 * high-volume connections, or may be decreased to limit the possible backlog of
51 * incoming data. The system places an absolute limit on these values, on Linux,
52 * for example, the default buffer size is typically 50K and can be increased to
55 * The #GstUDPSrc:skip-first-bytes property is used to strip off an arbitrary
56 * number of bytes from the start of the raw udp packet and can be used to strip
57 * off proprietary header, for example.
59 * The udpsrc is always a live source. It does however not provide a #GstClock,
60 * this is left for downstream elements such as an RTP session manager or demuxer
61 * (such as an MPEG demuxer). As with all live sources, the captured buffers
62 * will have their timestamp set to the current running time of the pipeline.
64 * udpsrc implements a #GstURIHandler interface that handles udp://host:port
67 * If the #GstUDPSrc:timeout property is set to a value bigger than 0, udpsrc
68 * will generate an element message named
69 * <classname>"GstUDPSrcTimeout"</classname>
70 * if no data was received in the given timeout.
71 * The message's structure contains one field:
76 * <classname>"timeout"</classname>: the timeout in microseconds that
77 * expired when waiting for data.
81 * The message is typically used to detect that no UDP arrives in the receiver
82 * because it is blocked by a firewall.
84 * A custom file descriptor can be configured with the
85 * #GstUDPSrc:socket property. The socket will be closed when setting
86 * the element to READY by default. This behaviour can be overriden
87 * with the #GstUDPSrc:close-socket property, in which case the
88 * application is responsible for closing the file descriptor.
91 * <title>Examples</title>
93 * gst-launch-1.0 -v udpsrc ! fakesink dump=1
94 * ]| A pipeline to read from the default port and dump the udp packets.
95 * To actually generate udp packets on the default port one can use the
96 * udpsink element. When running the following pipeline in another terminal, the
97 * above mentioned pipeline should dump data packets to the console.
99 * gst-launch-1.0 -v audiotestsrc ! udpsink
102 * gst-launch-1.0 -v udpsrc port=0 ! fakesink
103 * ]| read udp packets from a free port.
110 /* Needed to get struct in6_pktinfo.
111 * Also all these have to be before glib.h is included as
112 * otherwise struct in6_pktinfo is not defined completely
113 * due to broken glibc headers */
115 /* Needed for OSX/iOS to define the IPv6 variants */
116 #define __APPLE_USE_RFC_3542
117 #include <sys/types.h>
118 #ifdef HAVE_SYS_SOCKET_H
119 #include <sys/socket.h>
123 #include "gstudpsrc.h"
125 #include <gst/net/gstnetaddressmeta.h>
127 #include <gio/gnetworking.h>
129 /* Required for other parts of in_pktinfo / in6_pktinfo but only
130 * on non-Windows and can be included after glib.h */
131 #ifndef G_PLATFORM_WIN32
132 #include <netinet/ip.h>
135 /* Control messages for getting the destination address */
137 GType gst_ip_pktinfo_message_get_type (void);
139 #define GST_TYPE_IP_PKTINFO_MESSAGE (gst_ip_pktinfo_message_get_type ())
140 #define GST_IP_PKTINFO_MESSAGE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GST_TYPE_IP_PKTINFO_MESSAGE, GstIPPktinfoMessage))
141 #define GST_IP_PKTINFO_MESSAGE_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), GST_TYPE_IP_PKTINFO_MESSAGE, GstIPPktinfoMessageClass))
142 #define GST_IS_IP_PKTINFO_MESSAGE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GST_TYPE_IP_PKTINFO_MESSAGE))
143 #define GST_IS_IP_PKTINFO_MESSAGE_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), GST_TYPE_IP_PKTINFO_MESSAGE))
144 #define GST_IP_PKTINFO_MESSAGE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GST_TYPE_IP_PKTINFO_MESSAGE, GstIPPktinfoMessageClass))
146 typedef struct _GstIPPktinfoMessage GstIPPktinfoMessage;
147 typedef struct _GstIPPktinfoMessageClass GstIPPktinfoMessageClass;
149 struct _GstIPPktinfoMessageClass
151 GSocketControlMessageClass parent_class;
155 struct _GstIPPktinfoMessage
157 GSocketControlMessage parent;
160 #ifndef G_PLATFORM_WIN32
162 struct in_addr spec_dst;
168 G_DEFINE_TYPE (GstIPPktinfoMessage, gst_ip_pktinfo_message,
169 G_TYPE_SOCKET_CONTROL_MESSAGE);
172 gst_ip_pktinfo_message_get_size (GSocketControlMessage * message)
174 return sizeof (struct in_pktinfo);
178 gst_ip_pktinfo_message_get_level (GSocketControlMessage * message)
184 gst_ip_pktinfo_message_get_msg_type (GSocketControlMessage * message)
189 static GSocketControlMessage *
190 gst_ip_pktinfo_message_deserialize (gint level,
191 gint type, gsize size, gpointer data)
193 struct in_pktinfo *pktinfo;
194 GstIPPktinfoMessage *message;
196 if (level != IPPROTO_IP || type != IP_PKTINFO)
199 if (size < sizeof (struct in_pktinfo))
204 message = g_object_new (GST_TYPE_IP_PKTINFO_MESSAGE, NULL);
205 message->ifindex = pktinfo->ipi_ifindex;
206 #ifndef G_PLATFORM_WIN32
208 message->spec_dst = pktinfo->ipi_spec_dst;
211 message->addr = pktinfo->ipi_addr;
213 return G_SOCKET_CONTROL_MESSAGE (message);
217 gst_ip_pktinfo_message_init (GstIPPktinfoMessage * message)
222 gst_ip_pktinfo_message_class_init (GstIPPktinfoMessageClass * class)
224 GSocketControlMessageClass *scm_class;
226 scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
227 scm_class->get_size = gst_ip_pktinfo_message_get_size;
228 scm_class->get_level = gst_ip_pktinfo_message_get_level;
229 scm_class->get_type = gst_ip_pktinfo_message_get_msg_type;
230 scm_class->deserialize = gst_ip_pktinfo_message_deserialize;
235 GType gst_ipv6_pktinfo_message_get_type (void);
237 #define GST_TYPE_IPV6_PKTINFO_MESSAGE (gst_ipv6_pktinfo_message_get_type ())
238 #define GST_IPV6_PKTINFO_MESSAGE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GST_TYPE_IPV6_PKTINFO_MESSAGE, GstIPV6PktinfoMessage))
239 #define GST_IPV6_PKTINFO_MESSAGE_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), GST_TYPE_IPV6_PKTINFO_MESSAGE, GstIPV6PktinfoMessageClass))
240 #define GST_IS_IPV6_PKTINFO_MESSAGE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GST_TYPE_IPV6_PKTINFO_MESSAGE))
241 #define GST_IS_IPV6_PKTINFO_MESSAGE_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), GST_TYPE_IPV6_PKTINFO_MESSAGE))
242 #define GST_IPV6_PKTINFO_MESSAGE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GST_TYPE_IPV6_PKTINFO_MESSAGE, GstIPV6PktinfoMessageClass))
244 typedef struct _GstIPV6PktinfoMessage GstIPV6PktinfoMessage;
245 typedef struct _GstIPV6PktinfoMessageClass GstIPV6PktinfoMessageClass;
247 struct _GstIPV6PktinfoMessageClass
249 GSocketControlMessageClass parent_class;
253 struct _GstIPV6PktinfoMessage
255 GSocketControlMessage parent;
258 struct in6_addr addr;
261 G_DEFINE_TYPE (GstIPV6PktinfoMessage, gst_ipv6_pktinfo_message,
262 G_TYPE_SOCKET_CONTROL_MESSAGE);
265 gst_ipv6_pktinfo_message_get_size (GSocketControlMessage * message)
267 return sizeof (struct in6_pktinfo);
271 gst_ipv6_pktinfo_message_get_level (GSocketControlMessage * message)
277 gst_ipv6_pktinfo_message_get_msg_type (GSocketControlMessage * message)
282 static GSocketControlMessage *
283 gst_ipv6_pktinfo_message_deserialize (gint level,
284 gint type, gsize size, gpointer data)
286 struct in6_pktinfo *pktinfo;
287 GstIPV6PktinfoMessage *message;
289 if (level != IPPROTO_IPV6 || type != IPV6_PKTINFO)
292 if (size < sizeof (struct in6_pktinfo))
297 message = g_object_new (GST_TYPE_IPV6_PKTINFO_MESSAGE, NULL);
298 message->ifindex = pktinfo->ipi6_ifindex;
299 message->addr = pktinfo->ipi6_addr;
301 return G_SOCKET_CONTROL_MESSAGE (message);
305 gst_ipv6_pktinfo_message_init (GstIPV6PktinfoMessage * message)
310 gst_ipv6_pktinfo_message_class_init (GstIPV6PktinfoMessageClass * class)
312 GSocketControlMessageClass *scm_class;
314 scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
315 scm_class->get_size = gst_ipv6_pktinfo_message_get_size;
316 scm_class->get_level = gst_ipv6_pktinfo_message_get_level;
317 scm_class->get_type = gst_ipv6_pktinfo_message_get_msg_type;
318 scm_class->deserialize = gst_ipv6_pktinfo_message_deserialize;
323 #ifdef IP_RECVDSTADDR
324 GType gst_ip_recvdstaddr_message_get_type (void);
326 #define GST_TYPE_IP_RECVDSTADDR_MESSAGE (gst_ip_recvdstaddr_message_get_type ())
327 #define GST_IP_RECVDSTADDR_MESSAGE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GST_TYPE_IP_RECVDSTADDR_MESSAGE, GstIPRecvdstaddrMessage))
328 #define GST_IP_RECVDSTADDR_MESSAGE_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), GST_TYPE_IP_RECVDSTADDR_MESSAGE, GstIPRecvdstaddrMessageClass))
329 #define GST_IS_IP_RECVDSTADDR_MESSAGE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GST_TYPE_IP_RECVDSTADDR_MESSAGE))
330 #define GST_IS_IP_RECVDSTADDR_MESSAGE_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), GST_TYPE_IP_RECVDSTADDR_MESSAGE))
331 #define GST_IP_RECVDSTADDR_MESSAGE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GST_TYPE_IP_RECVDSTADDR_MESSAGE, GstIPRecvdstaddrMessageClass))
333 typedef struct _GstIPRecvdstaddrMessage GstIPRecvdstaddrMessage;
334 typedef struct _GstIPRecvdstaddrMessageClass GstIPRecvdstaddrMessageClass;
336 struct _GstIPRecvdstaddrMessageClass
338 GSocketControlMessageClass parent_class;
342 struct _GstIPRecvdstaddrMessage
344 GSocketControlMessage parent;
350 G_DEFINE_TYPE (GstIPRecvdstaddrMessage, gst_ip_recvdstaddr_message,
351 G_TYPE_SOCKET_CONTROL_MESSAGE);
354 gst_ip_recvdstaddr_message_get_size (GSocketControlMessage * message)
356 return sizeof (struct in_addr);
360 gst_ip_recvdstaddr_message_get_level (GSocketControlMessage * message)
366 gst_ip_recvdstaddr_message_get_msg_type (GSocketControlMessage * message)
368 return IP_RECVDSTADDR;
371 static GSocketControlMessage *
372 gst_ip_recvdstaddr_message_deserialize (gint level,
373 gint type, gsize size, gpointer data)
375 struct in_addr *addr;
376 GstIPRecvdstaddrMessage *message;
378 if (level != IPPROTO_IP || type != IP_RECVDSTADDR)
381 if (size < sizeof (struct in_addr))
386 message = g_object_new (GST_TYPE_IP_RECVDSTADDR_MESSAGE, NULL);
387 message->addr = *addr;
389 return G_SOCKET_CONTROL_MESSAGE (message);
393 gst_ip_recvdstaddr_message_init (GstIPRecvdstaddrMessage * message)
398 gst_ip_recvdstaddr_message_class_init (GstIPRecvdstaddrMessageClass * class)
400 GSocketControlMessageClass *scm_class;
402 scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
403 scm_class->get_size = gst_ip_recvdstaddr_message_get_size;
404 scm_class->get_level = gst_ip_recvdstaddr_message_get_level;
405 scm_class->get_type = gst_ip_recvdstaddr_message_get_msg_type;
406 scm_class->deserialize = gst_ip_recvdstaddr_message_deserialize;
411 gst_udpsrc_decide_allocation (GstBaseSrc * bsrc, GstQuery * query)
416 GstStructure *config;
417 GstCaps *caps = NULL;
419 udpsrc = GST_UDPSRC (bsrc);
421 if (gst_query_get_n_allocation_pools (query) > 0) {
427 pool = gst_buffer_pool_new ();
429 config = gst_buffer_pool_get_config (pool);
431 gst_query_parse_allocation (query, &caps, NULL);
433 gst_buffer_pool_config_set_params (config, caps, udpsrc->mtu, 0, 0);
435 gst_buffer_pool_set_config (pool, config);
438 gst_query_set_nth_allocation_pool (query, 0, pool, udpsrc->mtu, 0, 0);
440 gst_query_add_allocation_pool (query, pool, udpsrc->mtu, 0, 0);
442 gst_object_unref (pool);
447 /* not 100% correct, but a good upper bound for memory allocation purposes */
448 #define MAX_IPV4_UDP_PACKET_SIZE (65536 - 8)
450 GST_DEBUG_CATEGORY_STATIC (udpsrc_debug);
451 #define GST_CAT_DEFAULT (udpsrc_debug)
453 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
456 GST_STATIC_CAPS_ANY);
458 #define UDP_DEFAULT_PORT 5004
459 #define UDP_DEFAULT_MULTICAST_GROUP "0.0.0.0"
460 #define UDP_DEFAULT_MULTICAST_IFACE NULL
461 #define UDP_DEFAULT_URI "udp://"UDP_DEFAULT_MULTICAST_GROUP":"G_STRINGIFY(UDP_DEFAULT_PORT)
462 #define UDP_DEFAULT_CAPS NULL
463 #define UDP_DEFAULT_SOCKET NULL
464 #define UDP_DEFAULT_BUFFER_SIZE 0
465 #define UDP_DEFAULT_TIMEOUT 0
466 #define UDP_DEFAULT_SKIP_FIRST_BYTES 0
467 #define UDP_DEFAULT_CLOSE_SOCKET TRUE
468 #define UDP_DEFAULT_USED_SOCKET NULL
469 #define UDP_DEFAULT_AUTO_MULTICAST TRUE
470 #define UDP_DEFAULT_REUSE TRUE
471 #define UDP_DEFAULT_LOOP TRUE
472 #define UDP_DEFAULT_RETRIEVE_SENDER_ADDRESS TRUE
473 #define UDP_DEFAULT_MTU (1492)
480 PROP_MULTICAST_GROUP,
481 PROP_MULTICAST_IFACE,
487 PROP_SKIP_FIRST_BYTES,
494 PROP_RETRIEVE_SENDER_ADDRESS,
498 static void gst_udpsrc_uri_handler_init (gpointer g_iface, gpointer iface_data);
500 static GstCaps *gst_udpsrc_getcaps (GstBaseSrc * src, GstCaps * filter);
501 static gboolean gst_udpsrc_close (GstUDPSrc * src);
502 static gboolean gst_udpsrc_unlock (GstBaseSrc * bsrc);
503 static gboolean gst_udpsrc_unlock_stop (GstBaseSrc * bsrc);
504 static GstFlowReturn gst_udpsrc_fill (GstPushSrc * psrc, GstBuffer * outbuf);
506 static void gst_udpsrc_finalize (GObject * object);
508 static void gst_udpsrc_set_property (GObject * object, guint prop_id,
509 const GValue * value, GParamSpec * pspec);
510 static void gst_udpsrc_get_property (GObject * object, guint prop_id,
511 GValue * value, GParamSpec * pspec);
513 static GstStateChangeReturn gst_udpsrc_change_state (GstElement * element,
514 GstStateChange transition);
516 #define gst_udpsrc_parent_class parent_class
517 G_DEFINE_TYPE_WITH_CODE (GstUDPSrc, gst_udpsrc, GST_TYPE_PUSH_SRC,
518 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_udpsrc_uri_handler_init));
521 gst_udpsrc_class_init (GstUDPSrcClass * klass)
523 GObjectClass *gobject_class;
524 GstElementClass *gstelement_class;
525 GstBaseSrcClass *gstbasesrc_class;
526 GstPushSrcClass *gstpushsrc_class;
528 gobject_class = (GObjectClass *) klass;
529 gstelement_class = (GstElementClass *) klass;
530 gstbasesrc_class = (GstBaseSrcClass *) klass;
531 gstpushsrc_class = (GstPushSrcClass *) klass;
533 GST_DEBUG_CATEGORY_INIT (udpsrc_debug, "udpsrc", 0, "UDP src");
536 GST_TYPE_IP_PKTINFO_MESSAGE;
539 GST_TYPE_IPV6_PKTINFO_MESSAGE;
541 #ifdef IP_RECVDSTADDR
542 GST_TYPE_IP_RECVDSTADDR_MESSAGE;
545 gobject_class->set_property = gst_udpsrc_set_property;
546 gobject_class->get_property = gst_udpsrc_get_property;
547 gobject_class->finalize = gst_udpsrc_finalize;
549 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
550 g_param_spec_int ("port", "Port",
551 "The port to receive the packets from, 0=allocate", 0, G_MAXUINT16,
552 UDP_DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
553 /* FIXME 2.0: Remove multicast-group property */
554 #ifndef GST_REMOVE_DEPRECATED
555 g_object_class_install_property (gobject_class, PROP_MULTICAST_GROUP,
556 g_param_spec_string ("multicast-group", "Multicast Group",
557 "The Address of multicast group to join. (DEPRECATED: "
558 "Use address property instead)", UDP_DEFAULT_MULTICAST_GROUP,
559 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
561 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
562 g_param_spec_string ("multicast-iface", "Multicast Interface",
563 "The network interface on which to join the multicast group."
564 "This allows multiple interfaces seperated by comma. (\"eth0,eth1\")",
565 UDP_DEFAULT_MULTICAST_IFACE,
566 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
567 g_object_class_install_property (gobject_class, PROP_URI,
568 g_param_spec_string ("uri", "URI",
569 "URI in the form of udp://multicast_group:port", UDP_DEFAULT_URI,
570 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
571 g_object_class_install_property (gobject_class, PROP_CAPS,
572 g_param_spec_boxed ("caps", "Caps",
573 "The caps of the source pad", GST_TYPE_CAPS,
574 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
575 g_object_class_install_property (gobject_class, PROP_SOCKET,
576 g_param_spec_object ("socket", "Socket",
577 "Socket to use for UDP reception. (NULL == allocate)",
578 G_TYPE_SOCKET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
579 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_SIZE,
580 g_param_spec_int ("buffer-size", "Buffer Size",
581 "Size of the kernel receive buffer in bytes, 0=default", 0, G_MAXINT,
582 UDP_DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
583 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMEOUT,
584 g_param_spec_uint64 ("timeout", "Timeout",
585 "Post a message after timeout nanoseconds (0 = disabled)", 0,
586 G_MAXUINT64, UDP_DEFAULT_TIMEOUT,
587 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
588 g_object_class_install_property (G_OBJECT_CLASS (klass),
589 PROP_SKIP_FIRST_BYTES, g_param_spec_int ("skip-first-bytes",
590 "Skip first bytes", "number of bytes to skip for each udp packet", 0,
591 G_MAXINT, UDP_DEFAULT_SKIP_FIRST_BYTES,
592 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
593 g_object_class_install_property (gobject_class, PROP_CLOSE_SOCKET,
594 g_param_spec_boolean ("close-socket", "Close socket",
595 "Close socket if passed as property on state change",
596 UDP_DEFAULT_CLOSE_SOCKET,
597 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
598 g_object_class_install_property (gobject_class, PROP_USED_SOCKET,
599 g_param_spec_object ("used-socket", "Socket Handle",
600 "Socket currently in use for UDP reception. (NULL = no socket)",
601 G_TYPE_SOCKET, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
602 g_object_class_install_property (gobject_class, PROP_AUTO_MULTICAST,
603 g_param_spec_boolean ("auto-multicast", "Auto Multicast",
604 "Automatically join/leave multicast groups",
605 UDP_DEFAULT_AUTO_MULTICAST,
606 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
607 g_object_class_install_property (gobject_class, PROP_REUSE,
608 g_param_spec_boolean ("reuse", "Reuse", "Enable reuse of the port",
609 UDP_DEFAULT_REUSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
610 g_object_class_install_property (gobject_class, PROP_ADDRESS,
611 g_param_spec_string ("address", "Address",
612 "Address to receive packets for. This is equivalent to the "
613 "multicast-group property for now", UDP_DEFAULT_MULTICAST_GROUP,
614 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
618 * Can be used to disable multicast loopback.
622 g_object_class_install_property (gobject_class, PROP_LOOP,
623 g_param_spec_boolean ("loop", "Multicast Loopback",
624 "Used for setting the multicast loop parameter. TRUE = enable,"
625 " FALSE = disable", UDP_DEFAULT_LOOP,
626 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
628 * GstUDPSrc::retrieve-sender-address:
630 * Whether to retrieve the sender address and add it to the buffers as
631 * meta. Disabling this might result in minor performance improvements
632 * in certain scenarios.
636 g_object_class_install_property (gobject_class, PROP_RETRIEVE_SENDER_ADDRESS,
637 g_param_spec_boolean ("retrieve-sender-address",
638 "Retrieve Sender Address",
639 "Whether to retrieve the sender address and add it to buffers as "
640 "meta. Disabling this might result in minor performance improvements "
641 "in certain scenarios", UDP_DEFAULT_RETRIEVE_SENDER_ADDRESS,
642 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
646 * Maximum expected packet size. This directly defines the allocation
647 * size of the receive buffer pool.
649 * In case more data is received, a new #GstMemory is appended to the
650 * output buffer, ensuring no data is lost, this however leads to that
651 * buffer being freed and reallocated.
655 g_object_class_install_property (gobject_class, PROP_MTU,
656 g_param_spec_uint ("mtu", "Expected Maximum Transmission Unit",
657 "Maximum expected packet size. This directly defines the allocation"
658 "size of the receive buffer pool.",
659 0, G_MAXINT, UDP_DEFAULT_MTU,
660 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
662 gst_element_class_add_static_pad_template (gstelement_class, &src_template);
664 gst_element_class_set_static_metadata (gstelement_class,
665 "UDP packet receiver", "Source/Network",
666 "Receive data over the network via UDP",
667 "Wim Taymans <wim@fluendo.com>, "
668 "Thijs Vermeir <thijs.vermeir@barco.com>");
670 gstelement_class->change_state = gst_udpsrc_change_state;
672 gstbasesrc_class->unlock = gst_udpsrc_unlock;
673 gstbasesrc_class->unlock_stop = gst_udpsrc_unlock_stop;
674 gstbasesrc_class->get_caps = gst_udpsrc_getcaps;
675 gstbasesrc_class->decide_allocation = gst_udpsrc_decide_allocation;
677 gstpushsrc_class->fill = gst_udpsrc_fill;
681 gst_udpsrc_init (GstUDPSrc * udpsrc)
684 g_strdup_printf ("udp://%s:%u", UDP_DEFAULT_MULTICAST_GROUP,
687 udpsrc->address = g_strdup (UDP_DEFAULT_MULTICAST_GROUP);
688 udpsrc->port = UDP_DEFAULT_PORT;
689 udpsrc->socket = UDP_DEFAULT_SOCKET;
690 udpsrc->multi_iface = g_strdup (UDP_DEFAULT_MULTICAST_IFACE);
691 udpsrc->buffer_size = UDP_DEFAULT_BUFFER_SIZE;
692 udpsrc->timeout = UDP_DEFAULT_TIMEOUT;
693 udpsrc->skip_first_bytes = UDP_DEFAULT_SKIP_FIRST_BYTES;
694 udpsrc->close_socket = UDP_DEFAULT_CLOSE_SOCKET;
695 udpsrc->external_socket = (udpsrc->socket != NULL);
696 udpsrc->auto_multicast = UDP_DEFAULT_AUTO_MULTICAST;
697 udpsrc->used_socket = UDP_DEFAULT_USED_SOCKET;
698 udpsrc->reuse = UDP_DEFAULT_REUSE;
699 udpsrc->loop = UDP_DEFAULT_LOOP;
700 udpsrc->retrieve_sender_address = UDP_DEFAULT_RETRIEVE_SENDER_ADDRESS;
701 udpsrc->mtu = UDP_DEFAULT_MTU;
703 /* configure basesrc to be a live source */
704 gst_base_src_set_live (GST_BASE_SRC (udpsrc), TRUE);
705 /* make basesrc output a segment in time */
706 gst_base_src_set_format (GST_BASE_SRC (udpsrc), GST_FORMAT_TIME);
707 /* make basesrc set timestamps on outgoing buffers based on the running_time
708 * when they were captured */
709 gst_base_src_set_do_timestamp (GST_BASE_SRC (udpsrc), TRUE);
713 gst_udpsrc_finalize (GObject * object)
717 udpsrc = GST_UDPSRC (object);
720 gst_caps_unref (udpsrc->caps);
723 g_free (udpsrc->multi_iface);
724 udpsrc->multi_iface = NULL;
726 g_free (udpsrc->uri);
729 g_free (udpsrc->address);
730 udpsrc->address = NULL;
733 g_object_unref (udpsrc->socket);
734 udpsrc->socket = NULL;
736 if (udpsrc->used_socket)
737 g_object_unref (udpsrc->used_socket);
738 udpsrc->used_socket = NULL;
740 if (udpsrc->extra_mem)
741 gst_memory_unref (udpsrc->extra_mem);
742 udpsrc->extra_mem = NULL;
744 G_OBJECT_CLASS (parent_class)->finalize (object);
748 gst_udpsrc_getcaps (GstBaseSrc * src, GstCaps * filter)
751 GstCaps *caps, *result;
753 udpsrc = GST_UDPSRC (src);
755 GST_OBJECT_LOCK (src);
756 if ((caps = udpsrc->caps))
758 GST_OBJECT_UNLOCK (src);
762 result = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
763 gst_caps_unref (caps);
768 result = (filter) ? gst_caps_ref (filter) : gst_caps_new_any ();
774 gst_udpsrc_create_cancellable (GstUDPSrc * src)
778 src->cancellable = g_cancellable_new ();
779 src->made_cancel_fd = g_cancellable_make_pollfd (src->cancellable, &pollfd);
783 gst_udpsrc_free_cancellable (GstUDPSrc * src)
785 if (src->made_cancel_fd) {
786 g_cancellable_release_fd (src->cancellable);
787 src->made_cancel_fd = FALSE;
789 g_object_unref (src->cancellable);
790 src->cancellable = NULL;
794 gst_udpsrc_fill (GstPushSrc * psrc, GstBuffer * outbuf)
797 GSocketAddress *saddr = NULL;
798 GSocketAddress **p_saddr;
799 gint flags = G_SOCKET_MSG_NONE;
804 GSocketControlMessage **msgs = NULL;
805 GSocketControlMessage ***p_msgs;
808 GstMapInfo extra_info;
809 GInputVector ivec[2];
811 udpsrc = GST_UDPSRC_CAST (psrc);
813 /* optimization: use messages only in multicast mode and
814 * if we can't let the kernel do the filtering for us */
816 (g_inet_address_get_is_multicast (g_inet_socket_address_get_address
817 (udpsrc->addr))) ? &msgs : NULL;
818 #ifdef IP_MULTICAST_ALL
819 if (g_inet_address_get_family (g_inet_socket_address_get_address
820 (udpsrc->addr)) == G_SOCKET_FAMILY_IPV4)
824 /* Retrieve sender address unless we've been configured not to do so */
825 p_saddr = (udpsrc->retrieve_sender_address) ? &saddr : NULL;
827 if (!gst_buffer_map (outbuf, &info, GST_MAP_READWRITE))
828 goto buffer_map_error;
830 ivec[0].buffer = info.data;
831 ivec[0].size = info.size;
833 /* Prepare memory in case the data size exceeds mtu */
834 if (udpsrc->extra_mem == NULL) {
836 GstStructure *config;
837 GstAllocator *allocator = NULL;
838 GstAllocationParams params;
840 pool = gst_base_src_get_buffer_pool (GST_BASE_SRC_CAST (psrc));
841 config = gst_buffer_pool_get_config (pool);
842 gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms);
845 gst_allocator_alloc (allocator, MAX_IPV4_UDP_PACKET_SIZE, ¶ms);
847 gst_object_unref (pool);
848 gst_structure_free (config);
850 gst_object_unref (allocator);
853 if (!gst_memory_map (udpsrc->extra_mem, &extra_info, GST_MAP_READWRITE))
854 goto memory_map_error;
856 ivec[1].buffer = extra_info.data;
857 ivec[1].size = extra_info.size;
861 g_object_unref (saddr);
871 timeout = udpsrc->timeout / 1000;
875 GST_LOG_OBJECT (udpsrc, "doing select, timeout %" G_GINT64_FORMAT, timeout);
877 if (!g_socket_condition_timed_wait (udpsrc->used_socket, G_IO_IN | G_IO_PRI,
878 timeout, udpsrc->cancellable, &err)) {
879 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_BUSY)
880 || g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
882 } else if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_TIMED_OUT)) {
883 g_clear_error (&err);
884 /* timeout, post element message */
885 gst_element_post_message (GST_ELEMENT_CAST (udpsrc),
886 gst_message_new_element (GST_OBJECT_CAST (udpsrc),
887 gst_structure_new ("GstUDPSrcTimeout",
888 "timeout", G_TYPE_UINT64, udpsrc->timeout, NULL)));
895 } while (G_UNLIKELY (try_again));
898 g_socket_receive_message (udpsrc->used_socket, p_saddr, ivec, 2,
899 p_msgs, &n_msgs, &flags, udpsrc->cancellable, &err);
901 if (G_UNLIKELY (res < 0)) {
902 /* G_IO_ERROR_HOST_UNREACHABLE for a UDP socket means that a packet sent
903 * with udpsink generated a "port unreachable" ICMP response. We ignore
904 * that and try again.
905 * On Windows we get G_IO_ERROR_CONNECTION_CLOSED instead */
906 #if GLIB_CHECK_VERSION(2,44,0)
907 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_HOST_UNREACHABLE) ||
908 g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED)) {
910 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_HOST_UNREACHABLE)) {
912 g_clear_error (&err);
918 /* Retry if multicast and the destination address is not ours. We don't want
919 * to receive arbitrary packets */
921 GInetAddress *iaddr = g_inet_socket_address_get_address (udpsrc->addr);
922 gboolean skip_packet = FALSE;
923 gsize iaddr_size = g_inet_address_get_native_size (iaddr);
924 const guint8 *iaddr_bytes = g_inet_address_to_bytes (iaddr);
926 for (i = 0; i < n_msgs && !skip_packet; i++) {
928 if (GST_IS_IP_PKTINFO_MESSAGE (msgs[i])) {
929 GstIPPktinfoMessage *msg = GST_IP_PKTINFO_MESSAGE (msgs[i]);
931 if (sizeof (msg->addr) == iaddr_size
932 && memcmp (iaddr_bytes, &msg->addr, sizeof (msg->addr)))
937 if (GST_IS_IPV6_PKTINFO_MESSAGE (msgs[i])) {
938 GstIPV6PktinfoMessage *msg = GST_IPV6_PKTINFO_MESSAGE (msgs[i]);
940 if (sizeof (msg->addr) == iaddr_size
941 && memcmp (iaddr_bytes, &msg->addr, sizeof (msg->addr)))
945 #ifdef IP_RECVDSTADDR
946 if (GST_IS_IP_RECVDSTADDR_MESSAGE (msgs[i])) {
947 GstIPRecvdstaddrMessage *msg = GST_IP_RECVDSTADDR_MESSAGE (msgs[i]);
949 if (sizeof (msg->addr) == iaddr_size
950 && memcmp (iaddr_bytes, &msg->addr, sizeof (msg->addr)))
956 for (i = 0; i < n_msgs; i++) {
957 g_object_unref (msgs[i]);
962 GST_DEBUG_OBJECT (udpsrc,
963 "Dropping packet for a different multicast address");
968 gst_buffer_unmap (outbuf, &info);
969 gst_memory_unmap (udpsrc->extra_mem, &extra_info);
971 /* If this is the case, the buffer will be freed once unreffed,
972 * and the buffer pool will have to reallocate a new one.
974 if (res > udpsrc->mtu) {
975 gst_buffer_append_memory (outbuf, udpsrc->extra_mem);
976 udpsrc->extra_mem = NULL;
979 offset = udpsrc->skip_first_bytes;
981 if (G_UNLIKELY (offset > 0 && res < offset))
984 gst_buffer_resize (outbuf, offset, res - offset);
986 /* use buffer metadata so receivers can also track the address */
988 gst_buffer_add_net_address_meta (outbuf, saddr);
989 g_object_unref (saddr);
993 GST_LOG_OBJECT (udpsrc, "read packet of %d bytes", (int) res);
1000 GST_ELEMENT_ERROR (udpsrc, RESOURCE, READ, (NULL),
1001 ("Failed to map memory"));
1002 return GST_FLOW_ERROR;
1006 gst_buffer_unmap (outbuf, &info);
1007 GST_ELEMENT_ERROR (udpsrc, RESOURCE, READ, (NULL),
1008 ("Failed to map memory"));
1009 return GST_FLOW_ERROR;
1013 gst_buffer_unmap (outbuf, &info);
1014 gst_memory_unmap (udpsrc->extra_mem, &extra_info);
1015 GST_ELEMENT_ERROR (udpsrc, RESOURCE, READ, (NULL),
1016 ("select error: %s", err->message));
1017 g_clear_error (&err);
1018 return GST_FLOW_ERROR;
1022 gst_buffer_unmap (outbuf, &info);
1023 gst_memory_unmap (udpsrc->extra_mem, &extra_info);
1024 GST_DEBUG ("stop called");
1025 g_clear_error (&err);
1026 return GST_FLOW_FLUSHING;
1030 gst_buffer_unmap (outbuf, &info);
1031 gst_memory_unmap (udpsrc->extra_mem, &extra_info);
1032 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_BUSY) ||
1033 g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
1034 g_clear_error (&err);
1035 return GST_FLOW_FLUSHING;
1037 GST_ELEMENT_ERROR (udpsrc, RESOURCE, READ, (NULL),
1038 ("receive error %" G_GSSIZE_FORMAT ": %s", res, err->message));
1039 g_clear_error (&err);
1040 return GST_FLOW_ERROR;
1045 GST_ELEMENT_ERROR (udpsrc, STREAM, DECODE, (NULL),
1046 ("UDP buffer to small to skip header"));
1047 return GST_FLOW_ERROR;
1052 gst_udpsrc_set_uri (GstUDPSrc * src, const gchar * uri, GError ** error)
1057 if (!gst_udp_parse_uri (uri, &address, &port))
1060 if (port == (guint16) - 1)
1061 port = UDP_DEFAULT_PORT;
1063 g_free (src->address);
1064 src->address = address;
1068 src->uri = g_strdup (uri);
1075 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
1076 ("error parsing uri %s", uri));
1077 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
1078 "Could not parse UDP URI");
1084 gst_udpsrc_set_property (GObject * object, guint prop_id, const GValue * value,
1087 GstUDPSrc *udpsrc = GST_UDPSRC (object);
1090 case PROP_BUFFER_SIZE:
1091 udpsrc->buffer_size = g_value_get_int (value);
1094 udpsrc->port = g_value_get_int (value);
1095 g_free (udpsrc->uri);
1097 g_strdup_printf ("udp://%s:%u", udpsrc->address, udpsrc->port);
1099 case PROP_MULTICAST_GROUP:
1104 g_free (udpsrc->address);
1105 if ((group = g_value_get_string (value)))
1106 udpsrc->address = g_strdup (group);
1108 udpsrc->address = g_strdup (UDP_DEFAULT_MULTICAST_GROUP);
1110 g_free (udpsrc->uri);
1112 g_strdup_printf ("udp://%s:%u", udpsrc->address, udpsrc->port);
1115 case PROP_MULTICAST_IFACE:
1116 g_free (udpsrc->multi_iface);
1118 if (g_value_get_string (value) == NULL)
1119 udpsrc->multi_iface = g_strdup (UDP_DEFAULT_MULTICAST_IFACE);
1121 udpsrc->multi_iface = g_value_dup_string (value);
1124 gst_udpsrc_set_uri (udpsrc, g_value_get_string (value), NULL);
1128 const GstCaps *new_caps_val = gst_value_get_caps (value);
1132 if (new_caps_val == NULL) {
1133 new_caps = gst_caps_new_any ();
1135 new_caps = gst_caps_copy (new_caps_val);
1138 GST_OBJECT_LOCK (udpsrc);
1139 old_caps = udpsrc->caps;
1140 udpsrc->caps = new_caps;
1141 GST_OBJECT_UNLOCK (udpsrc);
1143 gst_caps_unref (old_caps);
1145 gst_pad_mark_reconfigure (GST_BASE_SRC_PAD (udpsrc));
1149 if (udpsrc->socket != NULL && udpsrc->socket != udpsrc->used_socket &&
1150 udpsrc->close_socket) {
1153 if (!g_socket_close (udpsrc->socket, &err)) {
1154 GST_ERROR ("failed to close socket %p: %s", udpsrc->socket,
1156 g_clear_error (&err);
1160 g_object_unref (udpsrc->socket);
1161 udpsrc->socket = g_value_dup_object (value);
1162 GST_DEBUG ("setting socket to %p", udpsrc->socket);
1165 udpsrc->timeout = g_value_get_uint64 (value);
1167 case PROP_SKIP_FIRST_BYTES:
1168 udpsrc->skip_first_bytes = g_value_get_int (value);
1170 case PROP_CLOSE_SOCKET:
1171 udpsrc->close_socket = g_value_get_boolean (value);
1173 case PROP_AUTO_MULTICAST:
1174 udpsrc->auto_multicast = g_value_get_boolean (value);
1177 udpsrc->reuse = g_value_get_boolean (value);
1180 udpsrc->loop = g_value_get_boolean (value);
1182 case PROP_RETRIEVE_SENDER_ADDRESS:
1183 udpsrc->retrieve_sender_address = g_value_get_boolean (value);
1186 udpsrc->mtu = g_value_get_uint (value);
1194 gst_udpsrc_get_property (GObject * object, guint prop_id, GValue * value,
1197 GstUDPSrc *udpsrc = GST_UDPSRC (object);
1200 case PROP_BUFFER_SIZE:
1201 g_value_set_int (value, udpsrc->buffer_size);
1204 g_value_set_int (value, udpsrc->port);
1206 case PROP_MULTICAST_GROUP:
1208 g_value_set_string (value, udpsrc->address);
1210 case PROP_MULTICAST_IFACE:
1211 g_value_set_string (value, udpsrc->multi_iface);
1214 g_value_set_string (value, udpsrc->uri);
1217 GST_OBJECT_LOCK (udpsrc);
1218 gst_value_set_caps (value, udpsrc->caps);
1219 GST_OBJECT_UNLOCK (udpsrc);
1222 g_value_set_object (value, udpsrc->socket);
1225 g_value_set_uint64 (value, udpsrc->timeout);
1227 case PROP_SKIP_FIRST_BYTES:
1228 g_value_set_int (value, udpsrc->skip_first_bytes);
1230 case PROP_CLOSE_SOCKET:
1231 g_value_set_boolean (value, udpsrc->close_socket);
1233 case PROP_USED_SOCKET:
1234 g_value_set_object (value, udpsrc->used_socket);
1236 case PROP_AUTO_MULTICAST:
1237 g_value_set_boolean (value, udpsrc->auto_multicast);
1240 g_value_set_boolean (value, udpsrc->reuse);
1243 g_value_set_boolean (value, udpsrc->loop);
1245 case PROP_RETRIEVE_SENDER_ADDRESS:
1246 g_value_set_boolean (value, udpsrc->retrieve_sender_address);
1249 g_value_set_uint (value, udpsrc->mtu);
1252 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1257 static GInetAddress *
1258 gst_udpsrc_resolve (GstUDPSrc * src, const gchar * address)
1262 GResolver *resolver;
1264 addr = g_inet_address_new_from_string (address);
1268 GST_DEBUG_OBJECT (src, "resolving IP address for host %s", address);
1269 resolver = g_resolver_get_default ();
1271 g_resolver_lookup_by_name (resolver, address, src->cancellable, &err);
1274 addr = G_INET_ADDRESS (g_object_ref (results->data));
1276 g_resolver_free_addresses (results);
1277 g_object_unref (resolver);
1279 #ifndef GST_DISABLE_GST_DEBUG
1281 gchar *ip = g_inet_address_to_string (addr);
1283 GST_DEBUG_OBJECT (src, "IP address for host %s is %s", address, ip);
1292 GST_WARNING_OBJECT (src, "Failed to resolve %s: %s", address, err->message);
1293 g_clear_error (&err);
1294 g_object_unref (resolver);
1300 gst_udpsrc_get_rcvbuf (GstUDPSrc * src)
1304 /* read the value of the receive buffer. Note that on linux this returns
1305 * 2x the value we set because the kernel allocates extra memory for
1306 * metadata. The default on Linux is about 100K (which is about 50K
1307 * without metadata) */
1308 if (!g_socket_get_option (src->used_socket, SOL_SOCKET, SO_RCVBUF, &val,
1310 GST_DEBUG_OBJECT (src, "could not get udp buffer size");
1314 /* Devise by 2 so that the numbers matches when we do get/set */
1321 /* create a socket for sending to remote machine */
1323 gst_udpsrc_open (GstUDPSrc * src)
1325 GInetAddress *addr, *bind_addr;
1326 GSocketAddress *bind_saddr;
1329 gst_udpsrc_create_cancellable (src);
1331 if (src->socket == NULL) {
1332 /* need to allocate a socket */
1333 GST_DEBUG_OBJECT (src, "allocating socket for %s:%d", src->address,
1336 addr = gst_udpsrc_resolve (src, src->address);
1340 if ((src->used_socket =
1341 g_socket_new (g_inet_address_get_family (addr),
1342 G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &err)) == NULL)
1345 src->external_socket = FALSE;
1347 GST_DEBUG_OBJECT (src, "got socket %p", src->used_socket);
1350 g_object_unref (src->addr);
1352 G_INET_SOCKET_ADDRESS (g_inet_socket_address_new (addr, src->port));
1354 GST_DEBUG_OBJECT (src, "binding on port %d", src->port);
1356 /* For multicast, bind to ANY and join the multicast group later */
1357 if (g_inet_address_get_is_multicast (addr))
1358 bind_addr = g_inet_address_new_any (g_inet_address_get_family (addr));
1360 bind_addr = G_INET_ADDRESS (g_object_ref (addr));
1362 g_object_unref (addr);
1364 bind_saddr = g_inet_socket_address_new (bind_addr, src->port);
1365 g_object_unref (bind_addr);
1366 if (!g_socket_bind (src->used_socket, bind_saddr, src->reuse, &err)) {
1367 GST_ERROR_OBJECT (src, "%s: error binding to %s:%d", err->message,
1368 src->address, src->port);
1372 g_object_unref (bind_saddr);
1373 g_socket_set_multicast_loopback (src->used_socket, src->loop);
1375 GInetSocketAddress *local_addr;
1377 GST_DEBUG_OBJECT (src, "using provided socket %p", src->socket);
1378 /* we use the configured socket, try to get some info about it */
1379 src->used_socket = G_SOCKET (g_object_ref (src->socket));
1380 src->external_socket = TRUE;
1383 G_INET_SOCKET_ADDRESS (g_socket_get_local_address (src->used_socket,
1386 goto getsockname_error;
1388 addr = gst_udpsrc_resolve (src, src->address);
1392 /* If bound to ANY and address points to a multicast address, make
1393 * sure that address is not overridden with ANY but we have the
1394 * opportunity later to join the multicast address. This ensures that we
1395 * have the same behaviour as for sockets created by udpsrc */
1396 if (!src->auto_multicast ||
1397 !g_inet_address_get_is_any (g_inet_socket_address_get_address
1399 || !g_inet_address_get_is_multicast (addr)) {
1400 g_object_unref (addr);
1402 g_object_unref (src->addr);
1403 src->addr = local_addr;
1405 g_object_unref (local_addr);
1407 g_object_unref (src->addr);
1409 G_INET_SOCKET_ADDRESS (g_inet_socket_address_new (addr, src->port));
1410 g_object_unref (addr);
1416 GError *opt_err = NULL;
1417 gboolean force_rcvbuf G_GNUC_UNUSED = FALSE;
1419 if (src->buffer_size != 0) {
1420 GST_INFO_OBJECT (src, "setting udp buffer of %d bytes", src->buffer_size);
1421 /* set buffer size, Note that on Linux this is typically limited to a
1422 * maximum of around 100K. Also a minimum of 128 bytes is required on
1424 if (!g_socket_set_option (src->used_socket, SOL_SOCKET, SO_RCVBUF,
1425 src->buffer_size, &opt_err)) {
1426 GST_INFO_OBJECT (src,
1427 "Could not create a buffer of requested %d bytes (%s) try forcing",
1428 src->buffer_size, opt_err->message);
1429 g_clear_error (&opt_err);
1430 force_rcvbuf = TRUE;
1433 #if defined(SO_RCVBUFFORCE)
1434 val = gst_udpsrc_get_rcvbuf (src);
1435 if (val < src->buffer_size)
1436 force_rcvbuf = TRUE;
1439 GST_INFO_OBJECT (src,
1440 "forcibly setting udp buffer of %d bytes", src->buffer_size);
1442 /* Will only work with CAP_NET_ADMIN privilege */
1443 if (!g_socket_set_option (src->used_socket, SOL_SOCKET, SO_RCVBUFFORCE,
1444 src->buffer_size, &opt_err)) {
1445 GST_ELEMENT_WARNING (src, RESOURCE, SETTINGS, (NULL),
1446 ("Could not create a buffer of requested %d bytes (%s). Need net.admin privilege?",
1447 src->buffer_size, opt_err->message));
1448 g_clear_error (&opt_err);
1453 val = gst_udpsrc_get_rcvbuf (src);
1454 if (val < src->buffer_size)
1455 GST_WARNING_OBJECT (src,
1456 "have udp buffer of %d bytes while %d were requested",
1457 val, src->buffer_size);
1459 GST_INFO_OBJECT (src, "have udp buffer of %d bytes", val);
1462 g_socket_set_broadcast (src->used_socket, TRUE);
1464 if (src->auto_multicast
1466 g_inet_address_get_is_multicast (g_inet_socket_address_get_address
1469 if (src->multi_iface) {
1470 GStrv multi_ifaces = g_strsplit (src->multi_iface, ",", -1);
1471 gchar **ifaces = multi_ifaces;
1473 g_strstrip (*ifaces);
1474 GST_DEBUG_OBJECT (src, "joining multicast group %s interface %s",
1475 src->address, *ifaces);
1476 if (!g_socket_join_multicast_group (src->used_socket,
1477 g_inet_socket_address_get_address (src->addr),
1478 FALSE, *ifaces, &err)) {
1479 g_strfreev (multi_ifaces);
1485 g_strfreev (multi_ifaces);
1487 GST_DEBUG_OBJECT (src, "joining multicast group %s", src->address);
1488 if (!g_socket_join_multicast_group (src->used_socket,
1489 g_inet_socket_address_get_address (src->addr), FALSE, NULL, &err))
1493 if (g_inet_address_get_family (g_inet_socket_address_get_address
1494 (src->addr)) == G_SOCKET_FAMILY_IPV4) {
1495 #if defined(IP_MULTICAST_ALL)
1496 if (!g_socket_set_option (src->used_socket, IPPROTO_IP, IP_MULTICAST_ALL,
1498 GST_WARNING_OBJECT (src, "Failed to disable IP_MULTICAST_ALL: %s",
1500 g_clear_error (&err);
1502 #elif defined(IP_PKTINFO)
1503 if (!g_socket_set_option (src->used_socket, IPPROTO_IP, IP_PKTINFO, TRUE,
1505 GST_WARNING_OBJECT (src, "Failed to enable IP_PKTINFO: %s",
1507 g_clear_error (&err);
1509 #elif defined(IP_RECVDSTADDR)
1510 if (!g_socket_set_option (src->used_socket, IPPROTO_IP, IP_RECVDSTADDR,
1512 GST_WARNING_OBJECT (src, "Failed to enable IP_RECVDSTADDR: %s",
1514 g_clear_error (&err);
1517 #pragma message("No API available for getting IPv4 destination address")
1518 GST_WARNING_OBJECT (src, "No API available for getting IPv4 destination "
1519 "address, will receive packets for every destination to our port");
1522 if (g_inet_address_get_family (g_inet_socket_address_get_address
1523 (src->addr)) == G_SOCKET_FAMILY_IPV6) {
1525 #ifdef IPV6_RECVPKTINFO
1526 if (!g_socket_set_option (src->used_socket, IPPROTO_IPV6,
1527 IPV6_RECVPKTINFO, TRUE, &err)) {
1529 if (!g_socket_set_option (src->used_socket, IPPROTO_IPV6, IPV6_PKTINFO,
1532 GST_WARNING_OBJECT (src, "Failed to enable IPV6_PKTINFO: %s",
1534 g_clear_error (&err);
1537 #pragma message("No API available for getting IPv6 destination address")
1538 GST_WARNING_OBJECT (src, "No API available for getting IPv6 destination "
1539 "address, will receive packets for every destination to our port");
1544 /* NOTE: sockaddr_in.sin_port works for ipv4 and ipv6 because sin_port
1545 * follows ss_family on both */
1547 GInetSocketAddress *addr;
1551 G_INET_SOCKET_ADDRESS (g_socket_get_local_address (src->used_socket,
1554 goto getsockname_error;
1556 port = g_inet_socket_address_get_port (addr);
1557 GST_DEBUG_OBJECT (src, "bound, on port %d", port);
1558 if (port != src->port) {
1560 GST_DEBUG_OBJECT (src, "notifying port %d", port);
1561 g_object_notify (G_OBJECT (src), "port");
1563 g_object_unref (addr);
1575 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
1576 ("no socket error: %s", err->message));
1577 g_clear_error (&err);
1578 g_object_unref (addr);
1583 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
1584 ("bind failed: %s", err->message));
1585 g_clear_error (&err);
1586 g_object_unref (bind_saddr);
1587 gst_udpsrc_close (src);
1592 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
1593 ("could not add membership: %s", err->message));
1594 g_clear_error (&err);
1595 gst_udpsrc_close (src);
1600 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
1601 ("getsockname failed: %s", err->message));
1602 g_clear_error (&err);
1603 gst_udpsrc_close (src);
1609 gst_udpsrc_unlock (GstBaseSrc * bsrc)
1613 src = GST_UDPSRC (bsrc);
1615 GST_LOG_OBJECT (src, "Flushing");
1616 g_cancellable_cancel (src->cancellable);
1622 gst_udpsrc_unlock_stop (GstBaseSrc * bsrc)
1626 src = GST_UDPSRC (bsrc);
1628 GST_LOG_OBJECT (src, "No longer flushing");
1630 gst_udpsrc_free_cancellable (src);
1631 gst_udpsrc_create_cancellable (src);
1637 gst_udpsrc_close (GstUDPSrc * src)
1639 GST_DEBUG ("closing sockets");
1641 if (src->used_socket) {
1642 if (src->auto_multicast
1644 g_inet_address_get_is_multicast (g_inet_socket_address_get_address
1648 if (src->multi_iface) {
1649 GStrv multi_ifaces = g_strsplit (src->multi_iface, ",", -1);
1650 gchar **ifaces = multi_ifaces;
1652 g_strstrip (*ifaces);
1653 GST_DEBUG_OBJECT (src, "leaving multicast group %s interface %s",
1654 src->address, *ifaces);
1655 if (!g_socket_leave_multicast_group (src->used_socket,
1656 g_inet_socket_address_get_address (src->addr),
1657 FALSE, *ifaces, &err)) {
1658 GST_ERROR_OBJECT (src, "Failed to leave multicast group: %s",
1660 g_clear_error (&err);
1664 g_strfreev (multi_ifaces);
1667 GST_DEBUG_OBJECT (src, "leaving multicast group %s", src->address);
1668 if (!g_socket_leave_multicast_group (src->used_socket,
1669 g_inet_socket_address_get_address (src->addr), FALSE,
1671 GST_ERROR_OBJECT (src, "Failed to leave multicast group: %s",
1673 g_clear_error (&err);
1678 if (src->close_socket || !src->external_socket) {
1680 if (!g_socket_close (src->used_socket, &err)) {
1681 GST_ERROR_OBJECT (src, "Failed to close socket: %s", err->message);
1682 g_clear_error (&err);
1686 g_object_unref (src->used_socket);
1687 src->used_socket = NULL;
1688 g_object_unref (src->addr);
1692 gst_udpsrc_free_cancellable (src);
1698 static GstStateChangeReturn
1699 gst_udpsrc_change_state (GstElement * element, GstStateChange transition)
1702 GstStateChangeReturn result;
1704 src = GST_UDPSRC (element);
1706 switch (transition) {
1707 case GST_STATE_CHANGE_NULL_TO_READY:
1708 if (!gst_udpsrc_open (src))
1715 GST_ELEMENT_CLASS (parent_class)->change_state (element,
1716 transition)) == GST_STATE_CHANGE_FAILURE)
1719 switch (transition) {
1720 case GST_STATE_CHANGE_READY_TO_NULL:
1721 gst_udpsrc_close (src);
1730 GST_DEBUG_OBJECT (src, "failed to open socket");
1731 return GST_STATE_CHANGE_FAILURE;
1735 GST_DEBUG_OBJECT (src, "parent failed state change");
1743 /*** GSTURIHANDLER INTERFACE *************************************************/
1746 gst_udpsrc_uri_get_type (GType type)
1751 static const gchar *const *
1752 gst_udpsrc_uri_get_protocols (GType type)
1754 static const gchar *protocols[] = { "udp", NULL };
1760 gst_udpsrc_uri_get_uri (GstURIHandler * handler)
1762 GstUDPSrc *src = GST_UDPSRC (handler);
1764 return g_strdup (src->uri);
1768 gst_udpsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
1771 return gst_udpsrc_set_uri (GST_UDPSRC (handler), uri, error);
1775 gst_udpsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
1777 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1779 iface->get_type = gst_udpsrc_uri_get_type;
1780 iface->get_protocols = gst_udpsrc_uri_get_protocols;
1781 iface->get_uri = gst_udpsrc_uri_get_uri;
1782 iface->set_uri = gst_udpsrc_uri_set_uri;