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>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 * SECTION:element-udpsrc
25 * @see_also: udpsink, multifdsink
27 * udpsrc is a network source that reads UDP packets from the network.
28 * It can be combined with RTP depayloaders to implement RTP streaming.
30 * The udpsrc element supports automatic port allocation by setting the
31 * #GstUDPSrc:port property to 0. After setting the udpsrc to PAUSED, the
32 * allocated port can be obtained by reading the port property.
34 * udpsrc can read from multicast groups by setting the #GstUDPSrc:multicast-group
35 * property to the IP address of the multicast group.
37 * Alternatively one can provide a custom socket to udpsrc with the #GstUDPSrc:sockfd
38 * property, udpsrc will then not allocate a socket itself but use the provided
41 * The #GstUDPSrc:caps property is mainly used to give a type to the UDP packet
42 * so that they can be autoplugged in GStreamer pipelines. This is very usefull
43 * for RTP implementations where the contents of the UDP packets is transfered
44 * out-of-bounds using SDP or other means.
46 * The #GstUDPSrc:buffer-size property is used to change the default kernel
47 * buffersizes used for receiving packets. The buffer size may be increased for
48 * high-volume connections, or may be decreased to limit the possible backlog of
49 * incoming data. The system places an absolute limit on these values, on Linux,
50 * for example, the default buffer size is typically 50K and can be increased to
53 * The #GstUDPSrc:skip-first-bytes property is used to strip off an arbitrary
54 * number of bytes from the start of the raw udp packet and can be used to strip
55 * off proprietary header, for example.
57 * The udpsrc is always a live source. It does however not provide a #GstClock,
58 * this is left for upstream elements such as an RTP session manager or demuxer
59 * (such as an MPEG demuxer). As with all live sources, the captured buffers
60 * will have their timestamp set to the current running time of the pipeline.
62 * udpsrc implements a #GstURIHandler interface that handles udp://host:port
65 * If the #GstUDPSrc:timeout property is set to a value bigger than 0, udpsrc
66 * will generate an element message named
67 * <classname>"GstUDPSrcTimeout"</classname>
68 * if no data was recieved in the given timeout.
69 * The message's structure contains one field:
74 * <classname>"timeout"</classname>: the timeout in microseconds that
75 * expired when waiting for data.
79 * The message is typically used to detect that no UDP arrives in the receiver
80 * because it is blocked by a firewall.
82 * A custom file descriptor can be configured with the
83 * #GstUDPSrc:sockfd property. The socket will be closed when setting the
84 * element to READY by default. This behaviour can be
85 * overriden with the #GstUDPSrc:closefd property, in which case the application
86 * is responsible for closing the file descriptor.
89 * <title>Examples</title>
91 * gst-launch-1.0 -v udpsrc ! fakesink dump=1
92 * ]| A pipeline to read from the default port and dump the udp packets.
93 * To actually generate udp packets on the default port one can use the
94 * udpsink element. When running the following pipeline in another terminal, the
95 * above mentioned pipeline should dump data packets to the console.
97 * gst-launch-1.0 -v audiotestsrc ! udpsink
100 * gst-launch-1.0 -v udpsrc port=0 ! fakesink
101 * ]| read udp packets from a free port.
108 #include "gstudpsrc.h"
110 #include <gst/net/gstnetaddressmeta.h>
112 #if GLIB_CHECK_VERSION (2, 35, 7)
113 #include <gio/gnetworking.h>
116 /* nicked from gnetworking.h */
119 #define _WIN32_WINNT 0x0501
121 #include <winsock2.h>
123 #include <ws2tcpip.h> /* for socklen_t */
124 #endif /* G_OS_WIN32 */
126 #ifdef HAVE_SYS_SOCKET_H
127 #include <sys/socket.h>
131 /* not 100% correct, but a good upper bound for memory allocation purposes */
132 #define MAX_IPV4_UDP_PACKET_SIZE (65536 - 8)
134 GST_DEBUG_CATEGORY_STATIC (udpsrc_debug);
135 #define GST_CAT_DEFAULT (udpsrc_debug)
137 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
140 GST_STATIC_CAPS_ANY);
142 #define UDP_DEFAULT_PORT 5004
143 #define UDP_DEFAULT_MULTICAST_GROUP "0.0.0.0"
144 #define UDP_DEFAULT_MULTICAST_IFACE NULL
145 #define UDP_DEFAULT_URI "udp://"UDP_DEFAULT_MULTICAST_GROUP":"G_STRINGIFY(UDP_DEFAULT_PORT)
146 #define UDP_DEFAULT_CAPS NULL
147 #define UDP_DEFAULT_SOCKET NULL
148 #define UDP_DEFAULT_BUFFER_SIZE 0
149 #define UDP_DEFAULT_TIMEOUT 0
150 #define UDP_DEFAULT_SKIP_FIRST_BYTES 0
151 #define UDP_DEFAULT_CLOSE_SOCKET TRUE
152 #define UDP_DEFAULT_USED_SOCKET NULL
153 #define UDP_DEFAULT_AUTO_MULTICAST TRUE
154 #define UDP_DEFAULT_REUSE TRUE
161 PROP_MULTICAST_GROUP,
162 PROP_MULTICAST_IFACE,
168 PROP_SKIP_FIRST_BYTES,
178 static void gst_udpsrc_uri_handler_init (gpointer g_iface, gpointer iface_data);
180 static GstCaps *gst_udpsrc_getcaps (GstBaseSrc * src, GstCaps * filter);
181 static GstFlowReturn gst_udpsrc_create (GstPushSrc * psrc, GstBuffer ** buf);
182 static gboolean gst_udpsrc_close (GstUDPSrc * src);
183 static gboolean gst_udpsrc_unlock (GstBaseSrc * bsrc);
184 static gboolean gst_udpsrc_unlock_stop (GstBaseSrc * bsrc);
186 static void gst_udpsrc_finalize (GObject * object);
188 static void gst_udpsrc_set_property (GObject * object, guint prop_id,
189 const GValue * value, GParamSpec * pspec);
190 static void gst_udpsrc_get_property (GObject * object, guint prop_id,
191 GValue * value, GParamSpec * pspec);
193 static GstStateChangeReturn gst_udpsrc_change_state (GstElement * element,
194 GstStateChange transition);
196 #define gst_udpsrc_parent_class parent_class
197 G_DEFINE_TYPE_WITH_CODE (GstUDPSrc, gst_udpsrc, GST_TYPE_PUSH_SRC,
198 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_udpsrc_uri_handler_init));
201 gst_udpsrc_class_init (GstUDPSrcClass * klass)
203 GObjectClass *gobject_class;
204 GstElementClass *gstelement_class;
205 GstBaseSrcClass *gstbasesrc_class;
206 GstPushSrcClass *gstpushsrc_class;
208 gobject_class = (GObjectClass *) klass;
209 gstelement_class = (GstElementClass *) klass;
210 gstbasesrc_class = (GstBaseSrcClass *) klass;
211 gstpushsrc_class = (GstPushSrcClass *) klass;
213 GST_DEBUG_CATEGORY_INIT (udpsrc_debug, "udpsrc", 0, "UDP src");
215 gobject_class->set_property = gst_udpsrc_set_property;
216 gobject_class->get_property = gst_udpsrc_get_property;
217 gobject_class->finalize = gst_udpsrc_finalize;
219 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
220 g_param_spec_int ("port", "Port",
221 "The port to receive the packets from, 0=allocate", 0, G_MAXUINT16,
222 UDP_DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223 /* FIXME 2.0: Remove multicast-group property */
224 g_object_class_install_property (gobject_class, PROP_MULTICAST_GROUP,
225 g_param_spec_string ("multicast-group", "Multicast Group",
226 "The Address of multicast group to join. DEPRECATED: "
227 "Use address property instead", UDP_DEFAULT_MULTICAST_GROUP,
228 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
230 g_param_spec_string ("multicast-iface", "Multicast Interface",
231 "The network interface on which to join the multicast group",
232 UDP_DEFAULT_MULTICAST_IFACE,
233 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234 g_object_class_install_property (gobject_class, PROP_URI,
235 g_param_spec_string ("uri", "URI",
236 "URI in the form of udp://multicast_group:port", UDP_DEFAULT_URI,
237 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
238 g_object_class_install_property (gobject_class, PROP_CAPS,
239 g_param_spec_boxed ("caps", "Caps",
240 "The caps of the source pad", GST_TYPE_CAPS,
241 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
242 g_object_class_install_property (gobject_class, PROP_SOCKET,
243 g_param_spec_object ("socket", "Socket",
244 "Socket to use for UDP reception. (NULL == allocate)",
245 G_TYPE_SOCKET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
246 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_SIZE,
247 g_param_spec_int ("buffer-size", "Buffer Size",
248 "Size of the kernel receive buffer in bytes, 0=default", 0, G_MAXINT,
249 UDP_DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
250 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMEOUT,
251 g_param_spec_uint64 ("timeout", "Timeout",
252 "Post a message after timeout nanoseconds (0 = disabled)", 0,
253 G_MAXUINT64, UDP_DEFAULT_TIMEOUT,
254 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
255 g_object_class_install_property (G_OBJECT_CLASS (klass),
256 PROP_SKIP_FIRST_BYTES, g_param_spec_int ("skip-first-bytes",
257 "Skip first bytes", "number of bytes to skip for each udp packet", 0,
258 G_MAXINT, UDP_DEFAULT_SKIP_FIRST_BYTES,
259 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
260 g_object_class_install_property (gobject_class, PROP_CLOSE_SOCKET,
261 g_param_spec_boolean ("close-socket", "Close socket",
262 "Close socket if passed as property on state change",
263 UDP_DEFAULT_CLOSE_SOCKET,
264 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
265 g_object_class_install_property (gobject_class, PROP_USED_SOCKET,
266 g_param_spec_object ("used-socket", "Socket Handle",
267 "Socket currently in use for UDP reception. (NULL = no socket)",
268 G_TYPE_SOCKET, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
269 g_object_class_install_property (gobject_class, PROP_AUTO_MULTICAST,
270 g_param_spec_boolean ("auto-multicast", "Auto Multicast",
271 "Automatically join/leave multicast groups",
272 UDP_DEFAULT_AUTO_MULTICAST,
273 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
274 g_object_class_install_property (gobject_class, PROP_REUSE,
275 g_param_spec_boolean ("reuse", "Reuse", "Enable reuse of the port",
276 UDP_DEFAULT_REUSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
277 g_object_class_install_property (gobject_class, PROP_ADDRESS,
278 g_param_spec_string ("address", "Address",
279 "Address to receive packets for. This is equivalent to the "
280 "multicast-group property for now", UDP_DEFAULT_MULTICAST_GROUP,
281 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
283 gst_element_class_add_pad_template (gstelement_class,
284 gst_static_pad_template_get (&src_template));
286 gst_element_class_set_static_metadata (gstelement_class,
287 "UDP packet receiver", "Source/Network",
288 "Receive data over the network via UDP",
289 "Wim Taymans <wim@fluendo.com>, "
290 "Thijs Vermeir <thijs.vermeir@barco.com>");
292 gstelement_class->change_state = gst_udpsrc_change_state;
294 gstbasesrc_class->unlock = gst_udpsrc_unlock;
295 gstbasesrc_class->unlock_stop = gst_udpsrc_unlock_stop;
296 gstbasesrc_class->get_caps = gst_udpsrc_getcaps;
298 gstpushsrc_class->create = gst_udpsrc_create;
302 gst_udpsrc_init (GstUDPSrc * udpsrc)
305 g_strdup_printf ("udp://%s:%u", UDP_DEFAULT_MULTICAST_GROUP,
308 udpsrc->address = g_strdup (UDP_DEFAULT_MULTICAST_GROUP);
309 udpsrc->port = UDP_DEFAULT_PORT;
310 udpsrc->socket = UDP_DEFAULT_SOCKET;
311 udpsrc->multi_iface = g_strdup (UDP_DEFAULT_MULTICAST_IFACE);
312 udpsrc->buffer_size = UDP_DEFAULT_BUFFER_SIZE;
313 udpsrc->timeout = UDP_DEFAULT_TIMEOUT;
314 udpsrc->skip_first_bytes = UDP_DEFAULT_SKIP_FIRST_BYTES;
315 udpsrc->close_socket = UDP_DEFAULT_CLOSE_SOCKET;
316 udpsrc->external_socket = (udpsrc->socket != NULL);
317 udpsrc->auto_multicast = UDP_DEFAULT_AUTO_MULTICAST;
318 udpsrc->used_socket = UDP_DEFAULT_USED_SOCKET;
319 udpsrc->reuse = UDP_DEFAULT_REUSE;
321 udpsrc->cancellable = g_cancellable_new ();
323 /* configure basesrc to be a live source */
324 gst_base_src_set_live (GST_BASE_SRC (udpsrc), TRUE);
325 /* make basesrc output a segment in time */
326 gst_base_src_set_format (GST_BASE_SRC (udpsrc), GST_FORMAT_TIME);
327 /* make basesrc set timestamps on outgoing buffers based on the running_time
328 * when they were captured */
329 gst_base_src_set_do_timestamp (GST_BASE_SRC (udpsrc), TRUE);
333 gst_udpsrc_finalize (GObject * object)
337 udpsrc = GST_UDPSRC (object);
340 gst_caps_unref (udpsrc->caps);
343 g_free (udpsrc->multi_iface);
344 udpsrc->multi_iface = NULL;
346 g_free (udpsrc->uri);
349 g_free (udpsrc->address);
350 udpsrc->address = NULL;
353 g_object_unref (udpsrc->socket);
354 udpsrc->socket = NULL;
356 if (udpsrc->used_socket)
357 g_object_unref (udpsrc->used_socket);
358 udpsrc->used_socket = NULL;
360 if (udpsrc->cancellable)
361 g_object_unref (udpsrc->cancellable);
362 udpsrc->cancellable = NULL;
364 G_OBJECT_CLASS (parent_class)->finalize (object);
368 gst_udpsrc_getcaps (GstBaseSrc * src, GstCaps * filter)
371 GstCaps *caps, *result;
373 udpsrc = GST_UDPSRC (src);
375 GST_OBJECT_LOCK (src);
376 if ((caps = udpsrc->caps))
378 GST_OBJECT_UNLOCK (src);
382 result = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
383 gst_caps_unref (caps);
388 result = (filter) ? gst_caps_ref (filter) : gst_caps_new_any ();
394 gst_udpsrc_create (GstPushSrc * psrc, GstBuffer ** buf)
398 GstBuffer *outbuf = NULL;
400 GSocketAddress *saddr = NULL;
407 udpsrc = GST_UDPSRC_CAST (psrc);
410 /* quick check, avoid going in select when we already have data */
411 readsize = g_socket_get_available_bytes (udpsrc->used_socket);
421 timeout = udpsrc->timeout / 1000;
425 GST_LOG_OBJECT (udpsrc, "doing select, timeout %" G_GINT64_FORMAT, timeout);
427 if (!g_socket_condition_timed_wait (udpsrc->used_socket, G_IO_IN | G_IO_PRI,
428 timeout, udpsrc->cancellable, &err)) {
429 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_BUSY)
430 || g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
432 } else if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_TIMED_OUT)) {
433 g_clear_error (&err);
434 /* timeout, post element message */
435 gst_element_post_message (GST_ELEMENT_CAST (udpsrc),
436 gst_message_new_element (GST_OBJECT_CAST (udpsrc),
437 gst_structure_new ("GstUDPSrcTimeout",
438 "timeout", G_TYPE_UINT64, udpsrc->timeout, NULL)));
445 } while (G_UNLIKELY (try_again));
447 /* ask how much is available for reading on the socket, this should be exactly
448 * one UDP packet. We will check the return value, though, because in some
449 * case it can return 0 and we don't want a 0 sized buffer. */
450 readsize = g_socket_get_available_bytes (udpsrc->used_socket);
451 if (G_UNLIKELY (readsize < 0))
452 goto get_available_error;
454 /* If we get here and the readsize is zero, then either select was woken up
455 * by activity that is not a read, or a poll error occurred, or a UDP packet
456 * was received that has no data. Since we cannot identify which case it is,
457 * we handle all of them. This could possibly lead to a UDP packet getting
458 * lost, but since UDP is not reliable, we can accept this. */
459 if (G_UNLIKELY (!readsize)) {
460 /* try to read a packet (and it will be ignored),
461 * in case a packet with no data arrived */
463 g_socket_receive_from (udpsrc->used_socket, NULL, NULL,
464 0, udpsrc->cancellable, &err);
465 if (G_UNLIKELY (res < 0))
473 GST_LOG_OBJECT (udpsrc, "ioctl says %d bytes available", (int) readsize);
475 /* sanity check value from _get_available_bytes(), which might be as
476 * large as the kernel-side buffer on some operating systems */
477 if (g_socket_get_family (udpsrc->used_socket) == G_SOCKET_FAMILY_IPV4)
478 readsize = MIN (MAX_IPV4_UDP_PACKET_SIZE, readsize);
480 ret = GST_BASE_SRC_CLASS (parent_class)->alloc (GST_BASE_SRC_CAST (udpsrc),
481 -1, readsize, &outbuf);
482 if (ret != GST_FLOW_OK)
485 gst_buffer_map (outbuf, &info, GST_MAP_WRITE);
489 g_object_unref (saddr);
493 g_socket_receive_from (udpsrc->used_socket, &saddr, (gchar *) info.data,
494 info.size, udpsrc->cancellable, &err);
496 if (G_UNLIKELY (res < 0)) {
497 /* EHOSTUNREACH for a UDP socket means that a packet sent with udpsink
498 * generated a "port unreachable" ICMP response. We ignore that and try
500 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_HOST_UNREACHABLE)) {
501 gst_buffer_unmap (outbuf, &info);
502 gst_buffer_unref (outbuf);
504 g_clear_error (&err);
510 /* patch offset and size when stripping off the headers */
511 if (G_UNLIKELY (udpsrc->skip_first_bytes != 0)) {
512 if (G_UNLIKELY (readsize < udpsrc->skip_first_bytes))
515 offset += udpsrc->skip_first_bytes;
516 res -= udpsrc->skip_first_bytes;
519 gst_buffer_unmap (outbuf, &info);
520 gst_buffer_resize (outbuf, offset, res);
522 /* use buffer metadata so receivers can also track the address */
524 gst_buffer_add_net_address_meta (outbuf, saddr);
525 g_object_unref (saddr);
529 GST_LOG_OBJECT (udpsrc, "read %d bytes", (int) readsize);
531 *buf = GST_BUFFER_CAST (outbuf);
538 GST_ELEMENT_ERROR (udpsrc, RESOURCE, READ, (NULL),
539 ("select error: %s", err->message));
540 g_clear_error (&err);
541 return GST_FLOW_ERROR;
545 GST_DEBUG ("stop called");
546 g_clear_error (&err);
547 return GST_FLOW_FLUSHING;
551 GST_ELEMENT_ERROR (udpsrc, RESOURCE, READ, (NULL),
552 ("get available bytes failed"));
553 return GST_FLOW_ERROR;
557 GST_DEBUG ("Allocation failed");
562 if (outbuf != NULL) {
563 gst_buffer_unmap (outbuf, &info);
564 gst_buffer_unref (outbuf);
567 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_BUSY) ||
568 g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
569 g_clear_error (&err);
570 return GST_FLOW_FLUSHING;
572 GST_ELEMENT_ERROR (udpsrc, RESOURCE, READ, (NULL),
573 ("receive error %" G_GSSIZE_FORMAT ": %s", res, err->message));
574 g_clear_error (&err);
575 return GST_FLOW_ERROR;
580 gst_buffer_unmap (outbuf, &info);
581 gst_buffer_unref (outbuf);
583 GST_ELEMENT_ERROR (udpsrc, STREAM, DECODE, (NULL),
584 ("UDP buffer to small to skip header"));
585 return GST_FLOW_ERROR;
590 gst_udpsrc_set_uri (GstUDPSrc * src, const gchar * uri, GError ** error)
595 if (!gst_udp_parse_uri (uri, &address, &port))
598 if (port == (guint16) - 1)
599 port = UDP_DEFAULT_PORT;
601 g_free (src->address);
602 src->address = address;
606 src->uri = g_strdup (uri);
613 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
614 ("error parsing uri %s", uri));
615 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
616 "Could not parse UDP 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 udpsrc->port = g_value_get_int (value);
633 g_free (udpsrc->uri);
635 g_strdup_printf ("udp://%s:%u", udpsrc->address, udpsrc->port);
637 case PROP_MULTICAST_GROUP:
642 g_free (udpsrc->address);
643 if ((group = g_value_get_string (value)))
644 udpsrc->address = g_strdup (group);
646 udpsrc->address = g_strdup (UDP_DEFAULT_MULTICAST_GROUP);
648 g_free (udpsrc->uri);
650 g_strdup_printf ("udp://%s:%u", udpsrc->address, udpsrc->port);
653 case PROP_MULTICAST_IFACE:
654 g_free (udpsrc->multi_iface);
656 if (g_value_get_string (value) == NULL)
657 udpsrc->multi_iface = g_strdup (UDP_DEFAULT_MULTICAST_IFACE);
659 udpsrc->multi_iface = g_value_dup_string (value);
662 gst_udpsrc_set_uri (udpsrc, g_value_get_string (value), NULL);
666 const GstCaps *new_caps_val = gst_value_get_caps (value);
670 if (new_caps_val == NULL) {
671 new_caps = gst_caps_new_any ();
673 new_caps = gst_caps_copy (new_caps_val);
676 GST_OBJECT_LOCK (udpsrc);
677 old_caps = udpsrc->caps;
678 udpsrc->caps = new_caps;
679 GST_OBJECT_UNLOCK (udpsrc);
681 gst_caps_unref (old_caps);
683 gst_pad_mark_reconfigure (GST_BASE_SRC_PAD (udpsrc));
687 if (udpsrc->socket != NULL && udpsrc->socket != udpsrc->used_socket &&
688 udpsrc->close_socket) {
691 if (!g_socket_close (udpsrc->socket, &err)) {
692 GST_ERROR ("failed to close socket %p: %s", udpsrc->socket,
694 g_clear_error (&err);
698 g_object_unref (udpsrc->socket);
699 udpsrc->socket = g_value_dup_object (value);
700 GST_DEBUG ("setting socket to %p", udpsrc->socket);
703 udpsrc->timeout = g_value_get_uint64 (value);
705 case PROP_SKIP_FIRST_BYTES:
706 udpsrc->skip_first_bytes = g_value_get_int (value);
708 case PROP_CLOSE_SOCKET:
709 udpsrc->close_socket = g_value_get_boolean (value);
711 case PROP_AUTO_MULTICAST:
712 udpsrc->auto_multicast = g_value_get_boolean (value);
715 udpsrc->reuse = g_value_get_boolean (value);
723 gst_udpsrc_get_property (GObject * object, guint prop_id, GValue * value,
726 GstUDPSrc *udpsrc = GST_UDPSRC (object);
729 case PROP_BUFFER_SIZE:
730 g_value_set_int (value, udpsrc->buffer_size);
733 g_value_set_int (value, udpsrc->port);
735 case PROP_MULTICAST_GROUP:
737 g_value_set_string (value, udpsrc->address);
739 case PROP_MULTICAST_IFACE:
740 g_value_set_string (value, udpsrc->multi_iface);
743 g_value_set_string (value, udpsrc->uri);
746 GST_OBJECT_LOCK (udpsrc);
747 gst_value_set_caps (value, udpsrc->caps);
748 GST_OBJECT_UNLOCK (udpsrc);
751 g_value_set_object (value, udpsrc->socket);
754 g_value_set_uint64 (value, udpsrc->timeout);
756 case PROP_SKIP_FIRST_BYTES:
757 g_value_set_int (value, udpsrc->skip_first_bytes);
759 case PROP_CLOSE_SOCKET:
760 g_value_set_boolean (value, udpsrc->close_socket);
762 case PROP_USED_SOCKET:
763 g_value_set_object (value, udpsrc->used_socket);
765 case PROP_AUTO_MULTICAST:
766 g_value_set_boolean (value, udpsrc->auto_multicast);
769 g_value_set_boolean (value, udpsrc->reuse);
772 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
777 static GInetAddress *
778 gst_udpsrc_resolve (GstUDPSrc * src, const gchar * address)
784 addr = g_inet_address_new_from_string (address);
788 GST_DEBUG_OBJECT (src, "resolving IP address for host %s", address);
789 resolver = g_resolver_get_default ();
791 g_resolver_lookup_by_name (resolver, address, src->cancellable, &err);
794 addr = G_INET_ADDRESS (g_object_ref (results->data));
796 g_resolver_free_addresses (results);
797 g_object_unref (resolver);
799 #ifndef GST_DISABLE_GST_DEBUG
801 gchar *ip = g_inet_address_to_string (addr);
803 GST_DEBUG_OBJECT (src, "IP address for host %s is %s", address, ip);
812 GST_WARNING_OBJECT (src, "Failed to resolve %s: %s", address, err->message);
813 g_clear_error (&err);
814 g_object_unref (resolver);
819 /* create a socket for sending to remote machine */
821 gst_udpsrc_open (GstUDPSrc * src)
823 GInetAddress *addr, *bind_addr;
824 GSocketAddress *bind_saddr;
827 if (src->socket == NULL) {
828 /* need to allocate a socket */
829 GST_DEBUG_OBJECT (src, "allocating socket for %s:%d", src->address,
832 addr = gst_udpsrc_resolve (src, src->address);
836 if ((src->used_socket =
837 g_socket_new (g_inet_address_get_family (addr),
838 G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &err)) == NULL)
841 src->external_socket = FALSE;
843 GST_DEBUG_OBJECT (src, "got socket %p", src->used_socket);
846 g_object_unref (src->addr);
848 G_INET_SOCKET_ADDRESS (g_inet_socket_address_new (addr, src->port));
850 GST_DEBUG_OBJECT (src, "binding on port %d", src->port);
852 /* On Windows it's not possible to bind to a multicast address
853 * but the OS will make sure to filter out all packets that
854 * arrive not for the multicast address the socket joined.
856 * On Linux and others it is necessary to bind to a multicast
857 * address to let the OS filter out all packets that are received
858 * on the same port but for different addresses than the multicast
862 if (g_inet_address_get_is_multicast (addr))
863 bind_addr = g_inet_address_new_any (g_inet_address_get_family (addr));
866 bind_addr = G_INET_ADDRESS (g_object_ref (addr));
868 g_object_unref (addr);
870 bind_saddr = g_inet_socket_address_new (bind_addr, src->port);
871 g_object_unref (bind_addr);
872 if (!g_socket_bind (src->used_socket, bind_saddr, src->reuse, &err))
875 g_object_unref (bind_saddr);
877 GST_DEBUG_OBJECT (src, "using provided socket %p", src->socket);
878 /* we use the configured socket, try to get some info about it */
879 src->used_socket = G_SOCKET (g_object_ref (src->socket));
880 src->external_socket = TRUE;
883 g_object_unref (src->addr);
885 G_INET_SOCKET_ADDRESS (g_socket_get_local_address (src->used_socket,
888 goto getsockname_error;
891 #if GLIB_CHECK_VERSION (2, 35, 7)
895 if (src->buffer_size != 0) {
896 GError *opt_err = NULL;
898 GST_INFO_OBJECT (src, "setting udp buffer of %d bytes", src->buffer_size);
899 /* set buffer size, Note that on Linux this is typically limited to a
900 * maximum of around 100K. Also a minimum of 128 bytes is required on
902 if (!g_socket_set_option (src->used_socket, SOL_SOCKET, SO_RCVBUF,
903 src->buffer_size, &opt_err)) {
904 GST_ELEMENT_WARNING (src, RESOURCE, SETTINGS, (NULL),
905 ("Could not create a buffer of requested %d bytes: %s",
906 src->buffer_size, opt_err->message));
907 g_error_free (opt_err);
912 /* read the value of the receive buffer. Note that on linux this returns
913 * 2x the value we set because the kernel allocates extra memory for
914 * metadata. The default on Linux is about 100K (which is about 50K
915 * without metadata) */
916 if (g_socket_get_option (src->used_socket, SOL_SOCKET, SO_RCVBUF, &val,
918 GST_INFO_OBJECT (src, "have udp buffer of %d bytes", val);
920 GST_DEBUG_OBJECT (src, "could not get udp buffer size");
923 #elif defined (SO_RCVBUF)
928 len = sizeof (rcvsize);
929 if (src->buffer_size != 0) {
930 rcvsize = src->buffer_size;
932 GST_DEBUG_OBJECT (src, "setting udp buffer of %d bytes", rcvsize);
933 /* set buffer size, Note that on Linux this is typically limited to a
934 * maximum of around 100K. Also a minimum of 128 bytes is required on
937 setsockopt (g_socket_get_fd (src->used_socket), SOL_SOCKET, SO_RCVBUF,
938 (void *) &rcvsize, len);
940 GST_ELEMENT_WARNING (src, RESOURCE, SETTINGS, (NULL),
941 ("Could not create a buffer of requested %d bytes, %d: %s (%d)",
942 rcvsize, ret, g_strerror (errno), errno));
946 /* read the value of the receive buffer. Note that on linux this returns 2x the
947 * value we set because the kernel allocates extra memory for metadata.
948 * The default on Linux is about 100K (which is about 50K without metadata) */
950 getsockopt (g_socket_get_fd (src->used_socket), SOL_SOCKET, SO_RCVBUF,
951 (void *) &rcvsize, &len);
953 GST_DEBUG_OBJECT (src, "have udp buffer of %d bytes", rcvsize);
955 GST_DEBUG_OBJECT (src, "could not get udp buffer size");
958 if (src->buffer_size != 0) {
959 GST_WARNING_OBJECT (src, "don't know how to set udp buffer size on this "
960 "OS. Consider upgrading your GLib to >= 2.35.7 and re-compiling the "
961 "GStreamer udp plugin");
965 g_socket_set_broadcast (src->used_socket, TRUE);
967 if (src->auto_multicast
969 g_inet_address_get_is_multicast (g_inet_socket_address_get_address
971 GST_DEBUG_OBJECT (src, "joining multicast group %s", src->address);
972 if (!g_socket_join_multicast_group (src->used_socket,
973 g_inet_socket_address_get_address (src->addr),
974 FALSE, src->multi_iface, &err))
978 /* NOTE: sockaddr_in.sin_port works for ipv4 and ipv6 because sin_port
979 * follows ss_family on both */
981 GInetSocketAddress *addr;
985 G_INET_SOCKET_ADDRESS (g_socket_get_local_address (src->used_socket,
988 goto getsockname_error;
990 port = g_inet_socket_address_get_port (addr);
991 GST_DEBUG_OBJECT (src, "bound, on port %d", port);
992 if (port != src->port) {
994 GST_DEBUG_OBJECT (src, "notifying port %d", port);
995 g_object_notify (G_OBJECT (src), "port");
997 g_object_unref (addr);
1009 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
1010 ("no socket error: %s", err->message));
1011 g_clear_error (&err);
1012 g_object_unref (addr);
1017 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
1018 ("bind failed: %s", err->message));
1019 g_clear_error (&err);
1020 g_object_unref (bind_saddr);
1021 gst_udpsrc_close (src);
1026 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
1027 ("could add membership: %s", err->message));
1028 g_clear_error (&err);
1029 gst_udpsrc_close (src);
1034 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
1035 ("getsockname failed: %s", err->message));
1036 g_clear_error (&err);
1037 gst_udpsrc_close (src);
1043 gst_udpsrc_unlock (GstBaseSrc * bsrc)
1047 src = GST_UDPSRC (bsrc);
1049 GST_LOG_OBJECT (src, "Flushing");
1050 g_cancellable_cancel (src->cancellable);
1056 gst_udpsrc_unlock_stop (GstBaseSrc * bsrc)
1060 src = GST_UDPSRC (bsrc);
1062 GST_LOG_OBJECT (src, "No longer flushing");
1063 g_cancellable_reset (src->cancellable);
1069 gst_udpsrc_close (GstUDPSrc * src)
1071 GST_DEBUG ("closing sockets");
1073 if (src->used_socket) {
1074 if (src->auto_multicast
1076 g_inet_address_get_is_multicast (g_inet_socket_address_get_address
1080 GST_DEBUG_OBJECT (src, "leaving multicast group %s", src->address);
1082 if (!g_socket_leave_multicast_group (src->used_socket,
1083 g_inet_socket_address_get_address (src->addr), FALSE,
1084 src->multi_iface, &err)) {
1085 GST_ERROR_OBJECT (src, "Failed to leave multicast group: %s",
1087 g_clear_error (&err);
1091 if (src->close_socket || !src->external_socket) {
1093 if (!g_socket_close (src->used_socket, &err)) {
1094 GST_ERROR_OBJECT (src, "Failed to close socket: %s", err->message);
1095 g_clear_error (&err);
1099 g_object_unref (src->used_socket);
1100 src->used_socket = NULL;
1101 g_object_unref (src->addr);
1109 static GstStateChangeReturn
1110 gst_udpsrc_change_state (GstElement * element, GstStateChange transition)
1113 GstStateChangeReturn result;
1115 src = GST_UDPSRC (element);
1117 switch (transition) {
1118 case GST_STATE_CHANGE_NULL_TO_READY:
1119 if (!gst_udpsrc_open (src))
1126 GST_ELEMENT_CLASS (parent_class)->change_state (element,
1127 transition)) == GST_STATE_CHANGE_FAILURE)
1130 switch (transition) {
1131 case GST_STATE_CHANGE_READY_TO_NULL:
1132 gst_udpsrc_close (src);
1141 GST_DEBUG_OBJECT (src, "failed to open socket");
1142 return GST_STATE_CHANGE_FAILURE;
1146 GST_DEBUG_OBJECT (src, "parent failed state change");
1154 /*** GSTURIHANDLER INTERFACE *************************************************/
1157 gst_udpsrc_uri_get_type (GType type)
1162 static const gchar *const *
1163 gst_udpsrc_uri_get_protocols (GType type)
1165 static const gchar *protocols[] = { "udp", NULL };
1171 gst_udpsrc_uri_get_uri (GstURIHandler * handler)
1173 GstUDPSrc *src = GST_UDPSRC (handler);
1175 return g_strdup (src->uri);
1179 gst_udpsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
1182 return gst_udpsrc_set_uri (GST_UDPSRC (handler), uri, error);
1186 gst_udpsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
1188 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1190 iface->get_type = gst_udpsrc_uri_get_type;
1191 iface->get_protocols = gst_udpsrc_uri_get_protocols;
1192 iface->get_uri = gst_udpsrc_uri_get_uri;
1193 iface->set_uri = gst_udpsrc_uri_set_uri;