2 * Copyright (C) <2005> Wim Taymans <wim@fluendo.com>
3 * Copyright (C) <2005> Nokia Corporation <kai.vehmanen@nokia.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-udpsrc
23 * @see_also: udpsink, multifdsink
25 * udpsrc is a network source that reads UDP packets from the network.
26 * It can be combined with RTP depayloaders to implement RTP streaming.
28 * The udpsrc element supports automatic port allocation by setting the
29 * #GstUDPSrc:port property to 0. After setting the udpsrc to PAUSED, the
30 * allocated port can be obtained by reading the port property.
32 * udpsrc can read from multicast groups by setting the #GstUDPSrc:multicast-group
33 * property to the IP address of the multicast group.
35 * Alternatively one can provide a custom socket to udpsrc with the #GstUDPSrc:sockfd
36 * property, udpsrc will then not allocate a socket itself but use the provided
39 * The #GstUDPSrc:caps property is mainly used to give a type to the UDP packet
40 * so that they can be autoplugged in GStreamer pipelines. This is very usefull
41 * for RTP implementations where the contents of the UDP packets is transfered
42 * out-of-bounds using SDP or other means.
44 * The #GstUDPSrc:buffer-size property is used to change the default kernel
45 * buffersizes used for receiving packets. The buffer size may be increased for
46 * high-volume connections, or may be decreased to limit the possible backlog of
47 * incoming data. The system places an absolute limit on these values, on Linux,
48 * for example, the default buffer size is typically 50K and can be increased to
51 * The #GstUDPSrc:skip-first-bytes property is used to strip off an arbitrary
52 * number of bytes from the start of the raw udp packet and can be used to strip
53 * off proprietary header, for example.
55 * The udpsrc is always a live source. It does however not provide a #GstClock,
56 * this is left for upstream elements such as an RTP session manager or demuxer
57 * (such as an MPEG demuxer). As with all live sources, the captured buffers
58 * will have their timestamp set to the current running time of the pipeline.
60 * udpsrc implements a #GstURIHandler interface that handles udp://host:port
63 * If the #GstUDPSrc:timeout property is set to a value bigger than 0, udpsrc
64 * will generate an element message named
65 * <classname>"GstUDPSrcTimeout"</classname>
66 * if no data was recieved in the given timeout.
67 * The message's structure contains one field:
72 * <classname>"timeout"</classname>: the timeout in microseconds that
73 * expired when waiting for data.
77 * The message is typically used to detect that no UDP arrives in the receiver
78 * because it is blocked by a firewall.
81 * A custom file descriptor can be configured with the
82 * #GstUDPSrc:sockfd property. The socket will be closed when setting the
83 * element to READY by default. This behaviour can be
84 * overriden with the #GstUDPSrc:closefd property, in which case the application
85 * is responsible for closing the file descriptor.
88 * <title>Examples</title>
90 * gst-launch -v udpsrc ! fakesink dump=1
91 * ]| A pipeline to read from the default port and dump the udp packets.
92 * To actually generate udp packets on the default port one can use the
93 * udpsink element. When running the following pipeline in another terminal, the
94 * above mentioned pipeline should dump data packets to the console.
96 * gst-launch -v audiotestsrc ! udpsink
99 * gst-launch -v udpsrc port=0 ! fakesink
100 * ]| read udp packets from a free port.
103 * Last reviewed on 2007-09-20 (0.10.7)
109 #include "gstudpsrc.h"
115 #if defined _MSC_VER && (_MSC_VER >= 1400)
119 #include <gst/netbuffer/gstnetbuffer.h>
121 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
122 #include <sys/filio.h>
125 GST_DEBUG_CATEGORY_STATIC (udpsrc_debug);
126 #define GST_CAT_DEFAULT (udpsrc_debug)
128 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
131 GST_STATIC_CAPS_ANY);
133 #define UDP_DEFAULT_PORT 4951
134 #define UDP_DEFAULT_MULTICAST_GROUP "0.0.0.0"
135 #define UDP_DEFAULT_MULTICAST_IFACE NULL
136 #define UDP_DEFAULT_URI "udp://"UDP_DEFAULT_MULTICAST_GROUP":"G_STRINGIFY(UDP_DEFAULT_PORT)
137 #define UDP_DEFAULT_CAPS NULL
138 #define UDP_DEFAULT_SOCKFD -1
139 #define UDP_DEFAULT_BUFFER_SIZE 0
140 #define UDP_DEFAULT_TIMEOUT 0
141 #define UDP_DEFAULT_SKIP_FIRST_BYTES 0
142 #define UDP_DEFAULT_CLOSEFD TRUE
143 #define UDP_DEFAULT_SOCK -1
144 #define UDP_DEFAULT_AUTO_MULTICAST TRUE
145 #define UDP_DEFAULT_REUSE TRUE
152 PROP_MULTICAST_GROUP,
153 PROP_MULTICAST_IFACE,
159 PROP_SKIP_FIRST_BYTES,
168 #define CLOSE_IF_REQUESTED(udpctx) \
170 if ((!udpctx->externalfd) || (udpctx->externalfd && udpctx->closefd)) { \
171 CLOSE_SOCKET(udpctx->sock.fd); \
172 if (udpctx->sock.fd == udpctx->sockfd) \
173 udpctx->sockfd = UDP_DEFAULT_SOCKFD; \
175 udpctx->sock.fd = UDP_DEFAULT_SOCK; \
178 static void gst_udpsrc_uri_handler_init (gpointer g_iface, gpointer iface_data);
180 static GstCaps *gst_udpsrc_getcaps (GstBaseSrc * src);
182 static GstFlowReturn gst_udpsrc_create (GstPushSrc * psrc, GstBuffer ** buf);
184 static gboolean gst_udpsrc_start (GstBaseSrc * bsrc);
186 static gboolean gst_udpsrc_stop (GstBaseSrc * bsrc);
188 static gboolean gst_udpsrc_unlock (GstBaseSrc * bsrc);
190 static gboolean gst_udpsrc_unlock_stop (GstBaseSrc * bsrc);
192 static void gst_udpsrc_finalize (GObject * object);
194 static void gst_udpsrc_set_property (GObject * object, guint prop_id,
195 const GValue * value, GParamSpec * pspec);
196 static void gst_udpsrc_get_property (GObject * object, guint prop_id,
197 GValue * value, GParamSpec * pspec);
199 #define gst_udpsrc_parent_class parent_class
200 G_DEFINE_TYPE_WITH_CODE (GstUDPSrc, gst_udpsrc, GST_TYPE_PUSH_SRC,
201 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_udpsrc_uri_handler_init));
204 gst_udpsrc_class_init (GstUDPSrcClass * klass)
206 GObjectClass *gobject_class;
207 GstElementClass *gstelement_class;
208 GstBaseSrcClass *gstbasesrc_class;
209 GstPushSrcClass *gstpushsrc_class;
211 gobject_class = (GObjectClass *) klass;
212 gstelement_class = (GstElementClass *) klass;
213 gstbasesrc_class = (GstBaseSrcClass *) klass;
214 gstpushsrc_class = (GstPushSrcClass *) klass;
216 GST_DEBUG_CATEGORY_INIT (udpsrc_debug, "udpsrc", 0, "UDP src");
218 gobject_class->set_property = gst_udpsrc_set_property;
219 gobject_class->get_property = gst_udpsrc_get_property;
220 gobject_class->finalize = gst_udpsrc_finalize;
222 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
223 g_param_spec_int ("port", "Port",
224 "The port to receive the packets from, 0=allocate", 0, G_MAXUINT16,
225 UDP_DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226 g_object_class_install_property (gobject_class, PROP_MULTICAST_GROUP,
227 g_param_spec_string ("multicast-group", "Multicast Group",
228 "The Address of multicast group to join", UDP_DEFAULT_MULTICAST_GROUP,
229 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
231 g_param_spec_string ("multicast-iface", "Multicast Interface",
232 "The network interface on which to join the multicast group",
233 UDP_DEFAULT_MULTICAST_IFACE,
234 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
235 g_object_class_install_property (gobject_class, PROP_URI,
236 g_param_spec_string ("uri", "URI",
237 "URI in the form of udp://multicast_group:port", UDP_DEFAULT_URI,
238 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
239 g_object_class_install_property (gobject_class, PROP_CAPS,
240 g_param_spec_boxed ("caps", "Caps",
241 "The caps of the source pad", GST_TYPE_CAPS,
242 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243 g_object_class_install_property (gobject_class, PROP_SOCKFD,
244 g_param_spec_int ("sockfd", "Socket Handle",
245 "Socket to use for UDP reception. (-1 == allocate)",
246 -1, G_MAXINT, UDP_DEFAULT_SOCKFD,
247 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_SIZE,
249 g_param_spec_int ("buffer-size", "Buffer Size",
250 "Size of the kernel receive buffer in bytes, 0=default", 0, G_MAXINT,
251 UDP_DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMEOUT,
253 g_param_spec_uint64 ("timeout", "Timeout",
254 "Post a message after timeout microseconds (0 = disabled)", 0,
255 G_MAXUINT64, UDP_DEFAULT_TIMEOUT,
256 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
257 g_object_class_install_property (G_OBJECT_CLASS (klass),
258 PROP_SKIP_FIRST_BYTES, g_param_spec_int ("skip-first-bytes",
259 "Skip first bytes", "number of bytes to skip for each udp packet", 0,
260 G_MAXINT, UDP_DEFAULT_SKIP_FIRST_BYTES,
261 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
262 g_object_class_install_property (gobject_class, PROP_CLOSEFD,
263 g_param_spec_boolean ("closefd", "Close sockfd",
264 "Close sockfd if passed as property on state change",
265 UDP_DEFAULT_CLOSEFD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
266 g_object_class_install_property (gobject_class, PROP_SOCK,
267 g_param_spec_int ("sock", "Socket Handle",
268 "Socket currently in use for UDP reception. (-1 = no socket)",
269 -1, G_MAXINT, UDP_DEFAULT_SOCK,
270 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
271 g_object_class_install_property (gobject_class, PROP_AUTO_MULTICAST,
272 g_param_spec_boolean ("auto-multicast", "Auto Multicast",
273 "Automatically join/leave multicast groups",
274 UDP_DEFAULT_AUTO_MULTICAST,
275 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
276 g_object_class_install_property (gobject_class, PROP_REUSE,
277 g_param_spec_boolean ("reuse", "Reuse", "Enable reuse of the port",
278 UDP_DEFAULT_REUSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
280 gst_element_class_add_pad_template (gstelement_class,
281 gst_static_pad_template_get (&src_template));
283 gst_element_class_set_details_simple (gstelement_class, "UDP packet receiver",
285 "Receive data over the network via UDP",
286 "Wim Taymans <wim@fluendo.com>, "
287 "Thijs Vermeir <thijs.vermeir@barco.com>");
289 gstbasesrc_class->start = gst_udpsrc_start;
290 gstbasesrc_class->stop = gst_udpsrc_stop;
291 gstbasesrc_class->unlock = gst_udpsrc_unlock;
292 gstbasesrc_class->unlock_stop = gst_udpsrc_unlock_stop;
293 gstbasesrc_class->get_caps = gst_udpsrc_getcaps;
295 gstpushsrc_class->create = gst_udpsrc_create;
299 gst_udpsrc_init (GstUDPSrc * udpsrc)
301 WSA_STARTUP (udpsrc);
303 gst_udp_uri_init (&udpsrc->uri, UDP_DEFAULT_MULTICAST_GROUP,
306 udpsrc->sockfd = UDP_DEFAULT_SOCKFD;
307 udpsrc->multi_iface = g_strdup (UDP_DEFAULT_MULTICAST_IFACE);
308 udpsrc->buffer_size = UDP_DEFAULT_BUFFER_SIZE;
309 udpsrc->timeout = UDP_DEFAULT_TIMEOUT;
310 udpsrc->skip_first_bytes = UDP_DEFAULT_SKIP_FIRST_BYTES;
311 udpsrc->closefd = UDP_DEFAULT_CLOSEFD;
312 udpsrc->externalfd = (udpsrc->sockfd != -1);
313 udpsrc->auto_multicast = UDP_DEFAULT_AUTO_MULTICAST;
314 udpsrc->sock.fd = UDP_DEFAULT_SOCK;
315 udpsrc->reuse = UDP_DEFAULT_REUSE;
317 /* configure basesrc to be a live source */
318 gst_base_src_set_live (GST_BASE_SRC (udpsrc), TRUE);
319 /* make basesrc output a segment in time */
320 gst_base_src_set_format (GST_BASE_SRC (udpsrc), GST_FORMAT_TIME);
321 /* make basesrc set timestamps on outgoing buffers based on the running_time
322 * when they were captured */
323 gst_base_src_set_do_timestamp (GST_BASE_SRC (udpsrc), TRUE);
327 gst_udpsrc_finalize (GObject * object)
331 udpsrc = GST_UDPSRC (object);
334 gst_caps_unref (udpsrc->caps);
336 g_free (udpsrc->multi_iface);
338 gst_udp_uri_free (&udpsrc->uri);
339 g_free (udpsrc->uristr);
341 if (udpsrc->sockfd >= 0 && udpsrc->closefd)
342 CLOSE_SOCKET (udpsrc->sockfd);
344 WSA_CLEANUP (object);
346 G_OBJECT_CLASS (parent_class)->finalize (object);
350 gst_udpsrc_getcaps (GstBaseSrc * src)
354 udpsrc = GST_UDPSRC (src);
357 return gst_caps_ref (udpsrc->caps);
359 return gst_caps_new_any ();
362 /* read a message from the error queue */
364 clear_error (GstUDPSrc * udpsrc)
366 #if defined (MSG_ERRQUEUE)
369 char msgbuf[CMSG_SPACE (128)];
372 /* Flush ERRORS from fd so next poll will not return at once */
373 /* No need for address : We look for local error */
374 cmsg.msg_name = NULL;
375 cmsg.msg_namelen = 0;
378 memset (&cbuf, 0, sizeof (cbuf));
380 iov.iov_len = sizeof (cbuf);
385 memset (&msgbuf, 0, sizeof (msgbuf));
386 cmsg.msg_control = &msgbuf;
387 cmsg.msg_controllen = sizeof (msgbuf);
389 recvmsg (udpsrc->sock.fd, &cmsg, MSG_ERRQUEUE);
394 gst_udpsrc_create (GstPushSrc * psrc, GstBuffer ** buf)
397 GstMetaNetAddress *meta;
402 struct sockaddr_in sa_in;
403 struct sockaddr_in6 sa_in6;
404 struct sockaddr_storage sa_stor;
412 #elif defined G_OS_WIN32
415 GstClockTime timeout;
419 udpsrc = GST_UDPSRC_CAST (psrc);
422 /* quick check, avoid going in select when we already have data */
424 if (G_UNLIKELY ((ret =
425 IOCTL_SOCKET (udpsrc->sock.fd, FIONREAD, &readsize)) < 0))
431 if (udpsrc->timeout > 0) {
432 timeout = udpsrc->timeout * GST_USECOND;
434 timeout = GST_CLOCK_TIME_NONE;
440 GST_LOG_OBJECT (udpsrc, "doing select, timeout %" G_GUINT64_FORMAT,
443 ret = gst_poll_wait (udpsrc->fdset, timeout);
444 GST_LOG_OBJECT (udpsrc, "select returned %d", ret);
445 if (G_UNLIKELY (ret < 0)) {
449 if (WSAGetLastError () != WSAEINTR)
452 if (errno != EAGAIN && errno != EINTR)
456 } else if (G_UNLIKELY (ret == 0)) {
457 /* timeout, post element message */
458 gst_element_post_message (GST_ELEMENT_CAST (udpsrc),
459 gst_message_new_element (GST_OBJECT_CAST (udpsrc),
460 gst_structure_new ("GstUDPSrcTimeout",
461 "timeout", G_TYPE_UINT64, udpsrc->timeout, NULL)));
464 } while (G_UNLIKELY (try_again));
466 /* ask how much is available for reading on the socket, this should be exactly
467 * one UDP packet. We will check the return value, though, because in some
468 * case it can return 0 and we don't want a 0 sized buffer. */
470 if (G_UNLIKELY ((ret =
471 IOCTL_SOCKET (udpsrc->sock.fd, FIONREAD, &readsize)) < 0))
474 /* if we get here and there is nothing to read from the socket, the select got
475 * woken up by activity on the socket but it was not a read. We know someone
476 * will also do something with the socket so that we don't go into an infinite
477 * loop in the select(). */
478 if (G_UNLIKELY (!readsize)) {
479 clear_error (udpsrc);
484 GST_LOG_OBJECT (udpsrc, "ioctl says %d bytes available", (int) readsize);
486 pktdata = g_malloc (readsize);
493 ret = recvfrom (udpsrc->sock.fd, (char *) pktdata, pktsize, 0, &sa.sa,
496 ret = recvfrom (udpsrc->sock.fd, pktdata, pktsize, 0, &sa.sa, &slen);
498 if (G_UNLIKELY (ret < 0)) {
500 /* WSAECONNRESET for a UDP socket means that a packet sent with udpsink
501 * generated a "port unreachable" ICMP response. We ignore that and try
503 if (WSAGetLastError () == WSAECONNRESET) {
508 if (WSAGetLastError () != WSAEINTR)
511 if (errno != EAGAIN && errno != EINTR)
518 /* patch pktdata and len when stripping off the headers */
519 if (G_UNLIKELY (udpsrc->skip_first_bytes != 0)) {
520 if (G_UNLIKELY (readsize <= udpsrc->skip_first_bytes))
523 offset += udpsrc->skip_first_bytes;
524 ret -= udpsrc->skip_first_bytes;
527 outbuf = gst_buffer_new ();
528 gst_buffer_take_memory (outbuf,
529 gst_memory_new_wrapped (0, pktdata, g_free, pktsize, offset, ret));
531 /* use buffer metadata so receivers can also track the address */
532 meta = gst_buffer_add_meta_net_address (outbuf);
534 switch (sa.sa.sa_family) {
537 gst_netaddress_set_ip4_address (&meta->naddr, sa.sa_in.sin_addr.s_addr,
545 memcpy (ip6, &sa.sa_in6.sin6_addr, sizeof (ip6));
546 gst_netaddress_set_ip6_address (&meta->naddr, ip6, sa.sa_in6.sin6_port);
551 WSASetLastError (WSAEAFNOSUPPORT);
553 errno = EAFNOSUPPORT;
557 GST_LOG_OBJECT (udpsrc, "read %d bytes", (int) readsize);
559 *buf = GST_BUFFER_CAST (outbuf);
566 GST_ELEMENT_ERROR (udpsrc, RESOURCE, READ, (NULL),
567 ("select error %d: %s (%d)", ret, g_strerror (errno), errno));
568 return GST_FLOW_ERROR;
572 GST_DEBUG ("stop called");
573 return GST_FLOW_WRONG_STATE;
577 GST_ELEMENT_ERROR (udpsrc, RESOURCE, READ, (NULL),
578 ("ioctl failed %d: %s (%d)", ret, g_strerror (errno), errno));
579 return GST_FLOW_ERROR;
585 GST_ELEMENT_ERROR (udpsrc, RESOURCE, READ, (NULL),
586 ("receive error %d (WSA error: %d)", ret, WSAGetLastError ()));
588 GST_ELEMENT_ERROR (udpsrc, RESOURCE, READ, (NULL),
589 ("receive error %d: %s (%d)", ret, g_strerror (errno), errno));
591 return GST_FLOW_ERROR;
595 GST_ELEMENT_ERROR (udpsrc, STREAM, DECODE, (NULL),
596 ("UDP buffer to small to skip header"));
597 return GST_FLOW_ERROR;
602 gst_udpsrc_set_uri (GstUDPSrc * src, const gchar * uri)
604 if (gst_udp_parse_uri (uri, &src->uri) < 0)
607 if (src->uri.port == -1)
608 src->uri.port = UDP_DEFAULT_PORT;
615 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
616 ("error parsing uri %s", uri));
622 gst_udpsrc_set_property (GObject * object, guint prop_id, const GValue * value,
625 GstUDPSrc *udpsrc = GST_UDPSRC (object);
628 case PROP_BUFFER_SIZE:
629 udpsrc->buffer_size = g_value_get_int (value);
632 gst_udp_uri_update (&udpsrc->uri, NULL, g_value_get_int (value));
634 case PROP_MULTICAST_GROUP:
638 if ((group = g_value_get_string (value)))
639 gst_udp_uri_update (&udpsrc->uri, group, -1);
641 gst_udp_uri_update (&udpsrc->uri, UDP_DEFAULT_MULTICAST_GROUP, -1);
644 case PROP_MULTICAST_IFACE:
645 g_free (udpsrc->multi_iface);
647 if (g_value_get_string (value) == NULL)
648 udpsrc->multi_iface = g_strdup (UDP_DEFAULT_MULTICAST_IFACE);
650 udpsrc->multi_iface = g_value_dup_string (value);
653 gst_udpsrc_set_uri (udpsrc, g_value_get_string (value));
657 const GstCaps *new_caps_val = gst_value_get_caps (value);
663 if (new_caps_val == NULL) {
664 new_caps = gst_caps_new_any ();
666 new_caps = gst_caps_copy (new_caps_val);
669 old_caps = udpsrc->caps;
670 udpsrc->caps = new_caps;
672 gst_caps_unref (old_caps);
673 gst_pad_set_caps (GST_BASE_SRC (udpsrc)->srcpad, new_caps);
677 if (udpsrc->sockfd >= 0 && udpsrc->sockfd != udpsrc->sock.fd &&
679 CLOSE_SOCKET (udpsrc->sockfd);
680 udpsrc->sockfd = g_value_get_int (value);
681 GST_DEBUG ("setting SOCKFD to %d", udpsrc->sockfd);
684 udpsrc->timeout = g_value_get_uint64 (value);
686 case PROP_SKIP_FIRST_BYTES:
687 udpsrc->skip_first_bytes = g_value_get_int (value);
690 udpsrc->closefd = g_value_get_boolean (value);
692 case PROP_AUTO_MULTICAST:
693 udpsrc->auto_multicast = g_value_get_boolean (value);
696 udpsrc->reuse = g_value_get_boolean (value);
704 gst_udpsrc_get_property (GObject * object, guint prop_id, GValue * value,
707 GstUDPSrc *udpsrc = GST_UDPSRC (object);
710 case PROP_BUFFER_SIZE:
711 g_value_set_int (value, udpsrc->buffer_size);
714 g_value_set_int (value, udpsrc->uri.port);
716 case PROP_MULTICAST_GROUP:
717 g_value_set_string (value, udpsrc->uri.host);
719 case PROP_MULTICAST_IFACE:
720 g_value_set_string (value, udpsrc->multi_iface);
723 g_value_take_string (value, gst_udp_uri_string (&udpsrc->uri));
726 gst_value_set_caps (value, udpsrc->caps);
729 g_value_set_int (value, udpsrc->sockfd);
732 g_value_set_uint64 (value, udpsrc->timeout);
734 case PROP_SKIP_FIRST_BYTES:
735 g_value_set_int (value, udpsrc->skip_first_bytes);
738 g_value_set_boolean (value, udpsrc->closefd);
741 g_value_set_int (value, udpsrc->sock.fd);
743 case PROP_AUTO_MULTICAST:
744 g_value_set_boolean (value, udpsrc->auto_multicast);
747 g_value_set_boolean (value, udpsrc->reuse);
750 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
755 /* create a socket for sending to remote machine */
757 gst_udpsrc_start (GstBaseSrc * bsrc)
766 struct sockaddr_storage bind_address;
768 src = GST_UDPSRC (bsrc);
770 if (src->sockfd == -1) {
771 /* need to allocate a socket */
772 GST_DEBUG_OBJECT (src, "allocating socket for %s:%d", src->uri.host,
775 gst_udp_get_addr (src->uri.host, src->uri.port, &src->myaddr)) < 0)
776 goto getaddrinfo_error;
778 if ((ret = socket (src->myaddr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
782 src->externalfd = FALSE;
784 GST_DEBUG_OBJECT (src, "got socket %d", src->sock.fd);
786 GST_DEBUG_OBJECT (src, "setting reuse %d", src->reuse);
787 reuse = src->reuse ? 1 : 0;
789 setsockopt (src->sock.fd, SOL_SOCKET, SO_REUSEADDR, &reuse,
790 sizeof (reuse))) < 0)
791 goto setsockopt_error;
793 GST_DEBUG_OBJECT (src, "binding on port %d", src->uri.port);
795 /* Take a temporary copy of the address in case we need to fix it for bind */
796 memcpy (&bind_address, &src->myaddr, sizeof (struct sockaddr_storage));
799 /* Windows does not allow binding to a multicast group so fix source address */
800 if (gst_udp_is_multicast (&src->myaddr)) {
801 switch (((struct sockaddr *) &bind_address)->sa_family) {
803 ((struct sockaddr_in *) &bind_address)->sin_addr.s_addr =
807 ((struct sockaddr_in6 *) &bind_address)->sin6_addr = in6addr_any;
815 len = gst_udp_get_sockaddr_length (&bind_address);
816 if ((ret = bind (src->sock.fd, (struct sockaddr *) &bind_address, len)) < 0)
819 if (!gst_udp_is_multicast (&src->myaddr)) {
820 len = sizeof (src->myaddr);
821 if ((ret = getsockname (src->sock.fd, (struct sockaddr *) &src->myaddr,
823 goto getsockname_error;
826 GST_DEBUG_OBJECT (src, "using provided socket %d", src->sockfd);
827 /* we use the configured socket, try to get some info about it */
828 len = sizeof (src->myaddr);
830 getsockname (src->sockfd, (struct sockaddr *) &src->myaddr,
832 goto getsockname_error;
834 src->sock.fd = src->sockfd;
835 src->externalfd = TRUE;
838 len = sizeof (rcvsize);
839 if (src->buffer_size != 0) {
840 rcvsize = src->buffer_size;
842 GST_DEBUG_OBJECT (src, "setting udp buffer of %d bytes", rcvsize);
843 /* set buffer size, Note that on Linux this is typically limited to a
844 * maximum of around 100K. Also a minimum of 128 bytes is required on
847 setsockopt (src->sock.fd, SOL_SOCKET, SO_RCVBUF, (void *) &rcvsize,
850 GST_ELEMENT_WARNING (src, RESOURCE, SETTINGS, (NULL),
851 ("Could not create a buffer of requested %d bytes, %d: %s (%d)",
852 rcvsize, ret, g_strerror (errno), errno));
856 /* read the value of the receive buffer. Note that on linux this returns 2x the
857 * value we set because the kernel allocates extra memory for metadata.
858 * The default on Linux is about 100K (which is about 50K without metadata) */
860 getsockopt (src->sock.fd, SOL_SOCKET, SO_RCVBUF, (void *) &rcvsize, &len);
862 GST_DEBUG_OBJECT (src, "have udp buffer of %d bytes", rcvsize);
864 GST_DEBUG_OBJECT (src, "could not get udp buffer size");
867 if ((ret = setsockopt (src->sock.fd, SOL_SOCKET, SO_BROADCAST, &bc_val,
868 sizeof (bc_val))) < 0) {
869 GST_ELEMENT_WARNING (src, RESOURCE, SETTINGS, (NULL),
870 ("could not configure socket for broadcast %d: %s (%d)", ret,
871 g_strerror (errno), errno));
874 /* Accept ERRQUEUE to get and flush icmp errors */
876 #if defined (IP_RECVERR)
877 if ((ret = setsockopt (src->sock.fd, IPPROTO_IP, IP_RECVERR, &err_val,
878 sizeof (err_val))) < 0) {
879 GST_ELEMENT_WARNING (src, RESOURCE, SETTINGS, (NULL),
880 ("could not configure socket for IP_RECVERR %d: %s (%d)", ret,
881 g_strerror (errno), errno));
885 if (src->auto_multicast && gst_udp_is_multicast (&src->myaddr)) {
886 GST_DEBUG_OBJECT (src, "joining multicast group %s", src->uri.host);
887 ret = gst_udp_join_group (src->sock.fd, &src->myaddr, src->multi_iface);
892 /* NOTE: sockaddr_in.sin_port works for ipv4 and ipv6 because sin_port
893 * follows ss_family on both */
894 port = g_ntohs (((struct sockaddr_in *) &src->myaddr)->sin_port);
895 GST_DEBUG_OBJECT (src, "bound, on port %d", port);
896 if (port != src->uri.port) {
897 src->uri.port = port;
898 GST_DEBUG_OBJECT (src, "notifying port %d", port);
899 g_object_notify (G_OBJECT (src), "port");
902 if ((src->fdset = gst_poll_new (TRUE)) == NULL)
905 gst_poll_add_fd (src->fdset, &src->sock);
906 gst_poll_fd_ctl_read (src->fdset, &src->sock, TRUE);
913 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
914 ("getaddrinfo failed: %s (%d)", gai_strerror (ret), ret));
919 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
920 ("no socket error %d: %s (%d)", ret, g_strerror (errno), errno));
925 CLOSE_IF_REQUESTED (src);
926 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
927 ("setsockopt failed %d: %s (%d)", ret, g_strerror (errno), errno));
932 CLOSE_IF_REQUESTED (src);
933 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
934 ("bind failed %d: %s (%d)", ret, g_strerror (errno), errno));
939 CLOSE_IF_REQUESTED (src);
940 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
941 ("could add membership %d: %s (%d)", ret, g_strerror (errno), errno));
946 CLOSE_IF_REQUESTED (src);
947 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
948 ("getsockname failed %d: %s (%d)", ret, g_strerror (errno), errno));
953 CLOSE_IF_REQUESTED (src);
954 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
955 ("could not create an fdset %d: %s (%d)", ret, g_strerror (errno),
962 gst_udpsrc_unlock (GstBaseSrc * bsrc)
966 src = GST_UDPSRC (bsrc);
968 GST_LOG_OBJECT (src, "Flushing");
969 gst_poll_set_flushing (src->fdset, TRUE);
975 gst_udpsrc_unlock_stop (GstBaseSrc * bsrc)
979 src = GST_UDPSRC (bsrc);
981 GST_LOG_OBJECT (src, "No longer flushing");
982 gst_poll_set_flushing (src->fdset, FALSE);
988 gst_udpsrc_stop (GstBaseSrc * bsrc)
992 src = GST_UDPSRC (bsrc);
994 GST_DEBUG ("stopping, closing sockets");
996 if (src->sock.fd >= 0) {
997 if (src->auto_multicast && gst_udp_is_multicast (&src->myaddr)) {
998 GST_DEBUG_OBJECT (src, "leaving multicast group %s", src->uri.host);
999 gst_udp_leave_group (src->sock.fd, &src->myaddr);
1001 CLOSE_IF_REQUESTED (src);
1005 gst_poll_free (src->fdset);
1012 /*** GSTURIHANDLER INTERFACE *************************************************/
1015 gst_udpsrc_uri_get_type (void)
1021 gst_udpsrc_uri_get_protocols (void)
1023 static gchar *protocols[] = { (char *) "udp", NULL };
1028 static const gchar *
1029 gst_udpsrc_uri_get_uri (GstURIHandler * handler)
1031 GstUDPSrc *src = GST_UDPSRC (handler);
1033 g_free (src->uristr);
1034 src->uristr = gst_udp_uri_string (&src->uri);
1040 gst_udpsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1044 GstUDPSrc *src = GST_UDPSRC (handler);
1046 ret = gst_udpsrc_set_uri (src, uri);
1052 gst_udpsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
1054 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1056 iface->get_type = gst_udpsrc_uri_get_type;
1057 iface->get_protocols = gst_udpsrc_uri_get_protocols;
1058 iface->get_uri = gst_udpsrc_uri_get_uri;
1059 iface->set_uri = gst_udpsrc_uri_set_uri;