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 usefull
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 upstream 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 recieved 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.
111 #include "gstudpsrc.h"
113 #include <gst/net/gstnetaddressmeta.h>
115 #if GLIB_CHECK_VERSION (2, 35, 7)
116 #include <gio/gnetworking.h>
119 /* nicked from gnetworking.h */
122 #define _WIN32_WINNT 0x0501
124 #include <winsock2.h>
126 #include <ws2tcpip.h> /* for socklen_t */
127 #endif /* G_OS_WIN32 */
129 #ifdef HAVE_SYS_SOCKET_H
130 #include <sys/socket.h>
134 /* not 100% correct, but a good upper bound for memory allocation purposes */
135 #define MAX_IPV4_UDP_PACKET_SIZE (65536 - 8)
137 GST_DEBUG_CATEGORY_STATIC (udpsrc_debug);
138 #define GST_CAT_DEFAULT (udpsrc_debug)
140 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
143 GST_STATIC_CAPS_ANY);
145 #define UDP_DEFAULT_PORT 5004
146 #define UDP_DEFAULT_MULTICAST_GROUP "0.0.0.0"
147 #define UDP_DEFAULT_MULTICAST_IFACE NULL
148 #define UDP_DEFAULT_URI "udp://"UDP_DEFAULT_MULTICAST_GROUP":"G_STRINGIFY(UDP_DEFAULT_PORT)
149 #define UDP_DEFAULT_CAPS NULL
150 #define UDP_DEFAULT_SOCKET NULL
151 #define UDP_DEFAULT_BUFFER_SIZE 0
152 #define UDP_DEFAULT_TIMEOUT 0
153 #define UDP_DEFAULT_SKIP_FIRST_BYTES 0
154 #define UDP_DEFAULT_CLOSE_SOCKET TRUE
155 #define UDP_DEFAULT_USED_SOCKET NULL
156 #define UDP_DEFAULT_AUTO_MULTICAST TRUE
157 #define UDP_DEFAULT_REUSE TRUE
164 PROP_MULTICAST_GROUP,
165 PROP_MULTICAST_IFACE,
171 PROP_SKIP_FIRST_BYTES,
181 static void gst_udpsrc_uri_handler_init (gpointer g_iface, gpointer iface_data);
183 static GstCaps *gst_udpsrc_getcaps (GstBaseSrc * src, GstCaps * filter);
184 static GstFlowReturn gst_udpsrc_create (GstPushSrc * psrc, GstBuffer ** buf);
185 static gboolean gst_udpsrc_close (GstUDPSrc * src);
186 static gboolean gst_udpsrc_unlock (GstBaseSrc * bsrc);
187 static gboolean gst_udpsrc_unlock_stop (GstBaseSrc * bsrc);
188 static gboolean gst_udpsrc_negotiate (GstBaseSrc * basesrc);
190 static void gst_udpsrc_finalize (GObject * object);
192 static void gst_udpsrc_set_property (GObject * object, guint prop_id,
193 const GValue * value, GParamSpec * pspec);
194 static void gst_udpsrc_get_property (GObject * object, guint prop_id,
195 GValue * value, GParamSpec * pspec);
197 static GstStateChangeReturn gst_udpsrc_change_state (GstElement * element,
198 GstStateChange transition);
200 #define gst_udpsrc_parent_class parent_class
201 G_DEFINE_TYPE_WITH_CODE (GstUDPSrc, gst_udpsrc, GST_TYPE_PUSH_SRC,
202 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_udpsrc_uri_handler_init));
205 gst_udpsrc_class_init (GstUDPSrcClass * klass)
207 GObjectClass *gobject_class;
208 GstElementClass *gstelement_class;
209 GstBaseSrcClass *gstbasesrc_class;
210 GstPushSrcClass *gstpushsrc_class;
212 gobject_class = (GObjectClass *) klass;
213 gstelement_class = (GstElementClass *) klass;
214 gstbasesrc_class = (GstBaseSrcClass *) klass;
215 gstpushsrc_class = (GstPushSrcClass *) klass;
217 GST_DEBUG_CATEGORY_INIT (udpsrc_debug, "udpsrc", 0, "UDP src");
219 gobject_class->set_property = gst_udpsrc_set_property;
220 gobject_class->get_property = gst_udpsrc_get_property;
221 gobject_class->finalize = gst_udpsrc_finalize;
223 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
224 g_param_spec_int ("port", "Port",
225 "The port to receive the packets from, 0=allocate", 0, G_MAXUINT16,
226 UDP_DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 /* FIXME 2.0: Remove multicast-group property */
228 #ifndef GST_REMOVE_DEPRECATED
229 g_object_class_install_property (gobject_class, PROP_MULTICAST_GROUP,
230 g_param_spec_string ("multicast-group", "Multicast Group",
231 "The Address of multicast group to join. (DEPRECATED: "
232 "Use address property instead)", UDP_DEFAULT_MULTICAST_GROUP,
233 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
235 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
236 g_param_spec_string ("multicast-iface", "Multicast Interface",
237 "The network interface on which to join the multicast group",
238 UDP_DEFAULT_MULTICAST_IFACE,
239 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240 g_object_class_install_property (gobject_class, PROP_URI,
241 g_param_spec_string ("uri", "URI",
242 "URI in the form of udp://multicast_group:port", UDP_DEFAULT_URI,
243 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
244 g_object_class_install_property (gobject_class, PROP_CAPS,
245 g_param_spec_boxed ("caps", "Caps",
246 "The caps of the source pad", GST_TYPE_CAPS,
247 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248 g_object_class_install_property (gobject_class, PROP_SOCKET,
249 g_param_spec_object ("socket", "Socket",
250 "Socket to use for UDP reception. (NULL == allocate)",
251 G_TYPE_SOCKET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_SIZE,
253 g_param_spec_int ("buffer-size", "Buffer Size",
254 "Size of the kernel receive buffer in bytes, 0=default", 0, G_MAXINT,
255 UDP_DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
256 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMEOUT,
257 g_param_spec_uint64 ("timeout", "Timeout",
258 "Post a message after timeout nanoseconds (0 = disabled)", 0,
259 G_MAXUINT64, UDP_DEFAULT_TIMEOUT,
260 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
261 g_object_class_install_property (G_OBJECT_CLASS (klass),
262 PROP_SKIP_FIRST_BYTES, g_param_spec_int ("skip-first-bytes",
263 "Skip first bytes", "number of bytes to skip for each udp packet", 0,
264 G_MAXINT, UDP_DEFAULT_SKIP_FIRST_BYTES,
265 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
266 g_object_class_install_property (gobject_class, PROP_CLOSE_SOCKET,
267 g_param_spec_boolean ("close-socket", "Close socket",
268 "Close socket if passed as property on state change",
269 UDP_DEFAULT_CLOSE_SOCKET,
270 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
271 g_object_class_install_property (gobject_class, PROP_USED_SOCKET,
272 g_param_spec_object ("used-socket", "Socket Handle",
273 "Socket currently in use for UDP reception. (NULL = no socket)",
274 G_TYPE_SOCKET, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
275 g_object_class_install_property (gobject_class, PROP_AUTO_MULTICAST,
276 g_param_spec_boolean ("auto-multicast", "Auto Multicast",
277 "Automatically join/leave multicast groups",
278 UDP_DEFAULT_AUTO_MULTICAST,
279 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
280 g_object_class_install_property (gobject_class, PROP_REUSE,
281 g_param_spec_boolean ("reuse", "Reuse", "Enable reuse of the port",
282 UDP_DEFAULT_REUSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
283 g_object_class_install_property (gobject_class, PROP_ADDRESS,
284 g_param_spec_string ("address", "Address",
285 "Address to receive packets for. This is equivalent to the "
286 "multicast-group property for now", UDP_DEFAULT_MULTICAST_GROUP,
287 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289 gst_element_class_add_pad_template (gstelement_class,
290 gst_static_pad_template_get (&src_template));
292 gst_element_class_set_static_metadata (gstelement_class,
293 "UDP packet receiver", "Source/Network",
294 "Receive data over the network via UDP",
295 "Wim Taymans <wim@fluendo.com>, "
296 "Thijs Vermeir <thijs.vermeir@barco.com>");
298 gstelement_class->change_state = gst_udpsrc_change_state;
300 gstbasesrc_class->unlock = gst_udpsrc_unlock;
301 gstbasesrc_class->unlock_stop = gst_udpsrc_unlock_stop;
302 gstbasesrc_class->get_caps = gst_udpsrc_getcaps;
303 gstbasesrc_class->negotiate = gst_udpsrc_negotiate;
305 gstpushsrc_class->create = gst_udpsrc_create;
309 gst_udpsrc_init (GstUDPSrc * udpsrc)
312 g_strdup_printf ("udp://%s:%u", UDP_DEFAULT_MULTICAST_GROUP,
315 udpsrc->address = g_strdup (UDP_DEFAULT_MULTICAST_GROUP);
316 udpsrc->port = UDP_DEFAULT_PORT;
317 udpsrc->socket = UDP_DEFAULT_SOCKET;
318 udpsrc->multi_iface = g_strdup (UDP_DEFAULT_MULTICAST_IFACE);
319 udpsrc->buffer_size = UDP_DEFAULT_BUFFER_SIZE;
320 udpsrc->timeout = UDP_DEFAULT_TIMEOUT;
321 udpsrc->skip_first_bytes = UDP_DEFAULT_SKIP_FIRST_BYTES;
322 udpsrc->close_socket = UDP_DEFAULT_CLOSE_SOCKET;
323 udpsrc->external_socket = (udpsrc->socket != NULL);
324 udpsrc->auto_multicast = UDP_DEFAULT_AUTO_MULTICAST;
325 udpsrc->used_socket = UDP_DEFAULT_USED_SOCKET;
326 udpsrc->reuse = UDP_DEFAULT_REUSE;
328 udpsrc->cancellable = g_cancellable_new ();
330 /* configure basesrc to be a live source */
331 gst_base_src_set_live (GST_BASE_SRC (udpsrc), TRUE);
332 /* make basesrc output a segment in time */
333 gst_base_src_set_format (GST_BASE_SRC (udpsrc), GST_FORMAT_TIME);
334 /* make basesrc set timestamps on outgoing buffers based on the running_time
335 * when they were captured */
336 gst_base_src_set_do_timestamp (GST_BASE_SRC (udpsrc), TRUE);
340 gst_udpsrc_finalize (GObject * object)
344 udpsrc = GST_UDPSRC (object);
347 gst_caps_unref (udpsrc->caps);
350 g_free (udpsrc->multi_iface);
351 udpsrc->multi_iface = NULL;
353 g_free (udpsrc->uri);
356 g_free (udpsrc->address);
357 udpsrc->address = NULL;
360 g_object_unref (udpsrc->socket);
361 udpsrc->socket = NULL;
363 if (udpsrc->used_socket)
364 g_object_unref (udpsrc->used_socket);
365 udpsrc->used_socket = NULL;
367 if (udpsrc->cancellable)
368 g_object_unref (udpsrc->cancellable);
369 udpsrc->cancellable = NULL;
371 G_OBJECT_CLASS (parent_class)->finalize (object);
375 gst_udpsrc_getcaps (GstBaseSrc * src, GstCaps * filter)
378 GstCaps *caps, *result;
380 udpsrc = GST_UDPSRC (src);
382 GST_OBJECT_LOCK (src);
383 if ((caps = udpsrc->caps))
385 GST_OBJECT_UNLOCK (src);
389 result = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
390 gst_caps_unref (caps);
395 result = (filter) ? gst_caps_ref (filter) : gst_caps_new_any ();
401 gst_udpsrc_reset_memory_allocator (GstUDPSrc * src)
403 if (src->mem != NULL) {
404 gst_memory_unmap (src->mem, &src->map);
405 gst_memory_unref (src->mem);
408 if (src->mem_max != NULL) {
409 gst_memory_unmap (src->mem_max, &src->map_max);
410 gst_memory_unref (src->mem_max);
414 src->vec[0].buffer = NULL;
415 src->vec[0].size = 0;
416 src->vec[1].buffer = NULL;
417 src->vec[1].size = 0;
419 if (src->allocator != NULL) {
420 gst_object_unref (src->allocator);
421 src->allocator = NULL;
426 gst_udpsrc_negotiate (GstBaseSrc * basesrc)
428 GstUDPSrc *src = GST_UDPSRC_CAST (basesrc);
431 /* just chain up to the default implementation, we just want to
432 * retrieve the allocator at the end of it (if there is one) */
433 ret = GST_BASE_SRC_CLASS (parent_class)->negotiate (basesrc);
436 GstAllocationParams new_params;
437 GstAllocator *new_allocator = NULL;
439 /* retrieve new allocator */
440 gst_base_src_get_allocator (basesrc, &new_allocator, &new_params);
442 if (src->allocator != new_allocator ||
443 memcmp (&src->params, &new_params, sizeof (GstAllocationParams)) != 0) {
444 /* drop old allocator and throw away any memory allocated with it */
445 gst_udpsrc_reset_memory_allocator (src);
447 /* and save the new allocator and/or new allocation parameters */
448 src->allocator = new_allocator;
449 src->params = new_params;
451 GST_INFO_OBJECT (src, "new allocator: %" GST_PTR_FORMAT, new_allocator);
459 gst_udpsrc_alloc_mem (GstUDPSrc * src, GstMemory ** p_mem, GstMapInfo * map,
464 mem = gst_allocator_alloc (src->allocator, size, &src->params);
466 if (!gst_memory_map (mem, map, GST_MAP_WRITE)) {
467 gst_memory_unref (mem);
468 memset (map, 0, sizeof (GstMapInfo));
476 gst_udpsrc_ensure_mem (GstUDPSrc * src)
478 if (src->mem == NULL) {
479 gsize mem_size = 1500; /* typical max. MTU */
481 /* if packets are likely to be smaller, just use that size, otherwise
482 * default to assuming incoming packets are around MTU size */
483 if (src->max_size > 0 && src->max_size < mem_size)
484 mem_size = src->max_size;
486 if (!gst_udpsrc_alloc_mem (src, &src->mem, &src->map, mem_size))
489 src->vec[0].buffer = src->map.data;
490 src->vec[0].size = src->map.size;
493 if (src->mem_max == NULL) {
494 gsize max_size = MAX_IPV4_UDP_PACKET_SIZE;
496 if (!gst_udpsrc_alloc_mem (src, &src->mem_max, &src->map_max, max_size))
499 src->vec[1].buffer = src->map_max.data;
500 src->vec[1].size = src->map_max.size;
507 gst_udpsrc_create (GstPushSrc * psrc, GstBuffer ** buf)
510 GstBuffer *outbuf = NULL;
511 GSocketAddress *saddr = NULL;
512 gint flags = G_SOCKET_MSG_NONE;
518 udpsrc = GST_UDPSRC_CAST (psrc);
520 if (!gst_udpsrc_ensure_mem (udpsrc))
521 goto memory_alloc_error;
531 timeout = udpsrc->timeout / 1000;
535 GST_LOG_OBJECT (udpsrc, "doing select, timeout %" G_GINT64_FORMAT, timeout);
537 if (!g_socket_condition_timed_wait (udpsrc->used_socket, G_IO_IN | G_IO_PRI,
538 timeout, udpsrc->cancellable, &err)) {
539 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_BUSY)
540 || g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
542 } else if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_TIMED_OUT)) {
543 g_clear_error (&err);
544 /* timeout, post element message */
545 gst_element_post_message (GST_ELEMENT_CAST (udpsrc),
546 gst_message_new_element (GST_OBJECT_CAST (udpsrc),
547 gst_structure_new ("GstUDPSrcTimeout",
548 "timeout", G_TYPE_UINT64, udpsrc->timeout, NULL)));
555 } while (G_UNLIKELY (try_again));
558 g_object_unref (saddr);
563 g_socket_receive_message (udpsrc->used_socket, &saddr, udpsrc->vec, 2,
564 NULL, NULL, &flags, udpsrc->cancellable, &err);
566 if (G_UNLIKELY (res < 0)) {
567 /* EHOSTUNREACH for a UDP socket means that a packet sent with udpsink
568 * generated a "port unreachable" ICMP response. We ignore that and try
570 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_HOST_UNREACHABLE)) {
571 g_clear_error (&err);
577 /* remember maximum packet size */
578 if (res > udpsrc->max_size)
579 udpsrc->max_size = res;
581 outbuf = gst_buffer_new ();
583 /* append first memory chunk to buffer */
584 gst_buffer_append_memory (outbuf, udpsrc->mem);
586 /* if the packet didn't fit into the first chunk, add second one as well */
587 if (res > udpsrc->map.size) {
588 gst_buffer_append_memory (outbuf, udpsrc->mem_max);
589 gst_memory_unmap (udpsrc->mem_max, &udpsrc->map_max);
590 udpsrc->vec[1].buffer = NULL;
591 udpsrc->vec[1].size = 0;
592 udpsrc->mem_max = NULL;
595 /* make sure we allocate a new chunk next time (we do this only here because
596 * we look at map.size to see if the second memory chunk is needed above) */
597 gst_memory_unmap (udpsrc->mem, &udpsrc->map);
598 udpsrc->vec[0].buffer = NULL;
599 udpsrc->vec[0].size = 0;
602 offset = udpsrc->skip_first_bytes;
604 if (G_UNLIKELY (offset > 0 && res < offset))
607 gst_buffer_resize (outbuf, offset, res - offset);
609 /* use buffer metadata so receivers can also track the address */
611 gst_buffer_add_net_address_meta (outbuf, saddr);
612 g_object_unref (saddr);
616 GST_LOG_OBJECT (udpsrc, "read packet of %d bytes", (int) res);
618 *buf = GST_BUFFER_CAST (outbuf);
625 GST_ELEMENT_ERROR (udpsrc, RESOURCE, READ, (NULL),
626 ("Failed to allocate or map memory"));
627 return GST_FLOW_ERROR;
631 GST_ELEMENT_ERROR (udpsrc, RESOURCE, READ, (NULL),
632 ("select error: %s", err->message));
633 g_clear_error (&err);
634 return GST_FLOW_ERROR;
638 GST_DEBUG ("stop called");
639 g_clear_error (&err);
640 return GST_FLOW_FLUSHING;
644 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_BUSY) ||
645 g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
646 g_clear_error (&err);
647 return GST_FLOW_FLUSHING;
649 GST_ELEMENT_ERROR (udpsrc, RESOURCE, READ, (NULL),
650 ("receive error %" G_GSSIZE_FORMAT ": %s", res, err->message));
651 g_clear_error (&err);
652 return GST_FLOW_ERROR;
657 gst_buffer_unref (outbuf);
659 GST_ELEMENT_ERROR (udpsrc, STREAM, DECODE, (NULL),
660 ("UDP buffer to small to skip header"));
661 return GST_FLOW_ERROR;
666 gst_udpsrc_set_uri (GstUDPSrc * src, const gchar * uri, GError ** error)
671 if (!gst_udp_parse_uri (uri, &address, &port))
674 if (port == (guint16) - 1)
675 port = UDP_DEFAULT_PORT;
677 g_free (src->address);
678 src->address = address;
682 src->uri = g_strdup (uri);
689 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
690 ("error parsing uri %s", uri));
691 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
692 "Could not parse UDP URI");
698 gst_udpsrc_set_property (GObject * object, guint prop_id, const GValue * value,
701 GstUDPSrc *udpsrc = GST_UDPSRC (object);
704 case PROP_BUFFER_SIZE:
705 udpsrc->buffer_size = g_value_get_int (value);
708 udpsrc->port = g_value_get_int (value);
709 g_free (udpsrc->uri);
711 g_strdup_printf ("udp://%s:%u", udpsrc->address, udpsrc->port);
713 case PROP_MULTICAST_GROUP:
718 g_free (udpsrc->address);
719 if ((group = g_value_get_string (value)))
720 udpsrc->address = g_strdup (group);
722 udpsrc->address = g_strdup (UDP_DEFAULT_MULTICAST_GROUP);
724 g_free (udpsrc->uri);
726 g_strdup_printf ("udp://%s:%u", udpsrc->address, udpsrc->port);
729 case PROP_MULTICAST_IFACE:
730 g_free (udpsrc->multi_iface);
732 if (g_value_get_string (value) == NULL)
733 udpsrc->multi_iface = g_strdup (UDP_DEFAULT_MULTICAST_IFACE);
735 udpsrc->multi_iface = g_value_dup_string (value);
738 gst_udpsrc_set_uri (udpsrc, g_value_get_string (value), NULL);
742 const GstCaps *new_caps_val = gst_value_get_caps (value);
746 if (new_caps_val == NULL) {
747 new_caps = gst_caps_new_any ();
749 new_caps = gst_caps_copy (new_caps_val);
752 GST_OBJECT_LOCK (udpsrc);
753 old_caps = udpsrc->caps;
754 udpsrc->caps = new_caps;
755 GST_OBJECT_UNLOCK (udpsrc);
757 gst_caps_unref (old_caps);
759 gst_pad_mark_reconfigure (GST_BASE_SRC_PAD (udpsrc));
763 if (udpsrc->socket != NULL && udpsrc->socket != udpsrc->used_socket &&
764 udpsrc->close_socket) {
767 if (!g_socket_close (udpsrc->socket, &err)) {
768 GST_ERROR ("failed to close socket %p: %s", udpsrc->socket,
770 g_clear_error (&err);
774 g_object_unref (udpsrc->socket);
775 udpsrc->socket = g_value_dup_object (value);
776 GST_DEBUG ("setting socket to %p", udpsrc->socket);
779 udpsrc->timeout = g_value_get_uint64 (value);
781 case PROP_SKIP_FIRST_BYTES:
782 udpsrc->skip_first_bytes = g_value_get_int (value);
784 case PROP_CLOSE_SOCKET:
785 udpsrc->close_socket = g_value_get_boolean (value);
787 case PROP_AUTO_MULTICAST:
788 udpsrc->auto_multicast = g_value_get_boolean (value);
791 udpsrc->reuse = g_value_get_boolean (value);
799 gst_udpsrc_get_property (GObject * object, guint prop_id, GValue * value,
802 GstUDPSrc *udpsrc = GST_UDPSRC (object);
805 case PROP_BUFFER_SIZE:
806 g_value_set_int (value, udpsrc->buffer_size);
809 g_value_set_int (value, udpsrc->port);
811 case PROP_MULTICAST_GROUP:
813 g_value_set_string (value, udpsrc->address);
815 case PROP_MULTICAST_IFACE:
816 g_value_set_string (value, udpsrc->multi_iface);
819 g_value_set_string (value, udpsrc->uri);
822 GST_OBJECT_LOCK (udpsrc);
823 gst_value_set_caps (value, udpsrc->caps);
824 GST_OBJECT_UNLOCK (udpsrc);
827 g_value_set_object (value, udpsrc->socket);
830 g_value_set_uint64 (value, udpsrc->timeout);
832 case PROP_SKIP_FIRST_BYTES:
833 g_value_set_int (value, udpsrc->skip_first_bytes);
835 case PROP_CLOSE_SOCKET:
836 g_value_set_boolean (value, udpsrc->close_socket);
838 case PROP_USED_SOCKET:
839 g_value_set_object (value, udpsrc->used_socket);
841 case PROP_AUTO_MULTICAST:
842 g_value_set_boolean (value, udpsrc->auto_multicast);
845 g_value_set_boolean (value, udpsrc->reuse);
848 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
853 static GInetAddress *
854 gst_udpsrc_resolve (GstUDPSrc * src, const gchar * address)
860 addr = g_inet_address_new_from_string (address);
864 GST_DEBUG_OBJECT (src, "resolving IP address for host %s", address);
865 resolver = g_resolver_get_default ();
867 g_resolver_lookup_by_name (resolver, address, src->cancellable, &err);
870 addr = G_INET_ADDRESS (g_object_ref (results->data));
872 g_resolver_free_addresses (results);
873 g_object_unref (resolver);
875 #ifndef GST_DISABLE_GST_DEBUG
877 gchar *ip = g_inet_address_to_string (addr);
879 GST_DEBUG_OBJECT (src, "IP address for host %s is %s", address, ip);
888 GST_WARNING_OBJECT (src, "Failed to resolve %s: %s", address, err->message);
889 g_clear_error (&err);
890 g_object_unref (resolver);
895 /* create a socket for sending to remote machine */
897 gst_udpsrc_open (GstUDPSrc * src)
899 GInetAddress *addr, *bind_addr;
900 GSocketAddress *bind_saddr;
903 if (src->socket == NULL) {
904 /* need to allocate a socket */
905 GST_DEBUG_OBJECT (src, "allocating socket for %s:%d", src->address,
908 addr = gst_udpsrc_resolve (src, src->address);
912 if ((src->used_socket =
913 g_socket_new (g_inet_address_get_family (addr),
914 G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &err)) == NULL)
917 src->external_socket = FALSE;
919 GST_DEBUG_OBJECT (src, "got socket %p", src->used_socket);
922 g_object_unref (src->addr);
924 G_INET_SOCKET_ADDRESS (g_inet_socket_address_new (addr, src->port));
926 GST_DEBUG_OBJECT (src, "binding on port %d", src->port);
928 /* On Windows it's not possible to bind to a multicast address
929 * but the OS will make sure to filter out all packets that
930 * arrive not for the multicast address the socket joined.
932 * On Linux and others it is necessary to bind to a multicast
933 * address to let the OS filter out all packets that are received
934 * on the same port but for different addresses than the multicast
938 if (g_inet_address_get_is_multicast (addr))
939 bind_addr = g_inet_address_new_any (g_inet_address_get_family (addr));
942 bind_addr = G_INET_ADDRESS (g_object_ref (addr));
944 g_object_unref (addr);
946 bind_saddr = g_inet_socket_address_new (bind_addr, src->port);
947 g_object_unref (bind_addr);
948 if (!g_socket_bind (src->used_socket, bind_saddr, src->reuse, &err))
951 g_object_unref (bind_saddr);
953 GST_DEBUG_OBJECT (src, "using provided socket %p", src->socket);
954 /* we use the configured socket, try to get some info about it */
955 src->used_socket = G_SOCKET (g_object_ref (src->socket));
956 src->external_socket = TRUE;
959 g_object_unref (src->addr);
961 G_INET_SOCKET_ADDRESS (g_socket_get_local_address (src->used_socket,
964 goto getsockname_error;
967 #if GLIB_CHECK_VERSION (2, 35, 7)
971 if (src->buffer_size != 0) {
972 GError *opt_err = NULL;
974 GST_INFO_OBJECT (src, "setting udp buffer of %d bytes", src->buffer_size);
975 /* set buffer size, Note that on Linux this is typically limited to a
976 * maximum of around 100K. Also a minimum of 128 bytes is required on
978 if (!g_socket_set_option (src->used_socket, SOL_SOCKET, SO_RCVBUF,
979 src->buffer_size, &opt_err)) {
980 GST_ELEMENT_WARNING (src, RESOURCE, SETTINGS, (NULL),
981 ("Could not create a buffer of requested %d bytes: %s",
982 src->buffer_size, opt_err->message));
983 g_error_free (opt_err);
988 /* read the value of the receive buffer. Note that on linux this returns
989 * 2x the value we set because the kernel allocates extra memory for
990 * metadata. The default on Linux is about 100K (which is about 50K
991 * without metadata) */
992 if (g_socket_get_option (src->used_socket, SOL_SOCKET, SO_RCVBUF, &val,
994 GST_INFO_OBJECT (src, "have udp buffer of %d bytes", val);
996 GST_DEBUG_OBJECT (src, "could not get udp buffer size");
999 #elif defined (SO_RCVBUF)
1004 len = sizeof (rcvsize);
1005 if (src->buffer_size != 0) {
1006 rcvsize = src->buffer_size;
1008 GST_DEBUG_OBJECT (src, "setting udp buffer of %d bytes", rcvsize);
1009 /* set buffer size, Note that on Linux this is typically limited to a
1010 * maximum of around 100K. Also a minimum of 128 bytes is required on
1013 setsockopt (g_socket_get_fd (src->used_socket), SOL_SOCKET, SO_RCVBUF,
1014 (void *) &rcvsize, len);
1016 GST_ELEMENT_WARNING (src, RESOURCE, SETTINGS, (NULL),
1017 ("Could not create a buffer of requested %d bytes, %d: %s (%d)",
1018 rcvsize, ret, g_strerror (errno), errno));
1022 /* read the value of the receive buffer. Note that on linux this returns 2x the
1023 * value we set because the kernel allocates extra memory for metadata.
1024 * The default on Linux is about 100K (which is about 50K without metadata) */
1026 getsockopt (g_socket_get_fd (src->used_socket), SOL_SOCKET, SO_RCVBUF,
1027 (void *) &rcvsize, &len);
1029 GST_DEBUG_OBJECT (src, "have udp buffer of %d bytes", rcvsize);
1031 GST_DEBUG_OBJECT (src, "could not get udp buffer size");
1034 if (src->buffer_size != 0) {
1035 GST_WARNING_OBJECT (src, "don't know how to set udp buffer size on this "
1036 "OS. Consider upgrading your GLib to >= 2.35.7 and re-compiling the "
1037 "GStreamer udp plugin");
1041 g_socket_set_broadcast (src->used_socket, TRUE);
1043 if (src->auto_multicast
1045 g_inet_address_get_is_multicast (g_inet_socket_address_get_address
1047 GST_DEBUG_OBJECT (src, "joining multicast group %s", src->address);
1048 if (!g_socket_join_multicast_group (src->used_socket,
1049 g_inet_socket_address_get_address (src->addr),
1050 FALSE, src->multi_iface, &err))
1054 /* NOTE: sockaddr_in.sin_port works for ipv4 and ipv6 because sin_port
1055 * follows ss_family on both */
1057 GInetSocketAddress *addr;
1061 G_INET_SOCKET_ADDRESS (g_socket_get_local_address (src->used_socket,
1064 goto getsockname_error;
1066 port = g_inet_socket_address_get_port (addr);
1067 GST_DEBUG_OBJECT (src, "bound, on port %d", port);
1068 if (port != src->port) {
1070 GST_DEBUG_OBJECT (src, "notifying port %d", port);
1071 g_object_notify (G_OBJECT (src), "port");
1073 g_object_unref (addr);
1076 src->allocator = NULL;
1077 gst_allocation_params_init (&src->params);
1090 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
1091 ("no socket error: %s", err->message));
1092 g_clear_error (&err);
1093 g_object_unref (addr);
1098 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
1099 ("bind failed: %s", err->message));
1100 g_clear_error (&err);
1101 g_object_unref (bind_saddr);
1102 gst_udpsrc_close (src);
1107 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
1108 ("could add membership: %s", err->message));
1109 g_clear_error (&err);
1110 gst_udpsrc_close (src);
1115 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
1116 ("getsockname failed: %s", err->message));
1117 g_clear_error (&err);
1118 gst_udpsrc_close (src);
1124 gst_udpsrc_unlock (GstBaseSrc * bsrc)
1128 src = GST_UDPSRC (bsrc);
1130 GST_LOG_OBJECT (src, "Flushing");
1131 g_cancellable_cancel (src->cancellable);
1137 gst_udpsrc_unlock_stop (GstBaseSrc * bsrc)
1141 src = GST_UDPSRC (bsrc);
1143 GST_LOG_OBJECT (src, "No longer flushing");
1144 g_cancellable_reset (src->cancellable);
1150 gst_udpsrc_close (GstUDPSrc * src)
1152 GST_DEBUG ("closing sockets");
1154 if (src->used_socket) {
1155 if (src->auto_multicast
1157 g_inet_address_get_is_multicast (g_inet_socket_address_get_address
1161 GST_DEBUG_OBJECT (src, "leaving multicast group %s", src->address);
1163 if (!g_socket_leave_multicast_group (src->used_socket,
1164 g_inet_socket_address_get_address (src->addr), FALSE,
1165 src->multi_iface, &err)) {
1166 GST_ERROR_OBJECT (src, "Failed to leave multicast group: %s",
1168 g_clear_error (&err);
1172 if (src->close_socket || !src->external_socket) {
1174 if (!g_socket_close (src->used_socket, &err)) {
1175 GST_ERROR_OBJECT (src, "Failed to close socket: %s", err->message);
1176 g_clear_error (&err);
1180 g_object_unref (src->used_socket);
1181 src->used_socket = NULL;
1182 g_object_unref (src->addr);
1186 gst_udpsrc_reset_memory_allocator (src);
1192 static GstStateChangeReturn
1193 gst_udpsrc_change_state (GstElement * element, GstStateChange transition)
1196 GstStateChangeReturn result;
1198 src = GST_UDPSRC (element);
1200 switch (transition) {
1201 case GST_STATE_CHANGE_NULL_TO_READY:
1202 if (!gst_udpsrc_open (src))
1209 GST_ELEMENT_CLASS (parent_class)->change_state (element,
1210 transition)) == GST_STATE_CHANGE_FAILURE)
1213 switch (transition) {
1214 case GST_STATE_CHANGE_READY_TO_NULL:
1215 gst_udpsrc_close (src);
1224 GST_DEBUG_OBJECT (src, "failed to open socket");
1225 return GST_STATE_CHANGE_FAILURE;
1229 GST_DEBUG_OBJECT (src, "parent failed state change");
1237 /*** GSTURIHANDLER INTERFACE *************************************************/
1240 gst_udpsrc_uri_get_type (GType type)
1245 static const gchar *const *
1246 gst_udpsrc_uri_get_protocols (GType type)
1248 static const gchar *protocols[] = { "udp", NULL };
1254 gst_udpsrc_uri_get_uri (GstURIHandler * handler)
1256 GstUDPSrc *src = GST_UDPSRC (handler);
1258 return g_strdup (src->uri);
1262 gst_udpsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
1265 return gst_udpsrc_set_uri (GST_UDPSRC (handler), uri, error);
1269 gst_udpsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
1271 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1273 iface->get_type = gst_udpsrc_uri_get_type;
1274 iface->get_protocols = gst_udpsrc_uri_get_protocols;
1275 iface->get_uri = gst_udpsrc_uri_get_uri;
1276 iface->set_uri = gst_udpsrc_uri_set_uri;