gstreamer/gst/udp/gstudpsrc.c

1020 lines
31 KiB
C
Raw Normal View History

/* GStreamer
2005-05-12 15:32:51 +00:00
* Copyright (C) <2005> Wim Taymans <wim@fluendo.com>
* Copyright (C) <2005> Nokia Corporation <kai.vehmanen@nokia.com>
2012-01-17 08:03:38 +00:00
* Copyright (C) <2012> Collabora Ltd.
* Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/**
* SECTION:element-udpsrc
* @see_also: udpsink, multifdsink
*
* udpsrc is a network source that reads UDP packets from the network.
* It can be combined with RTP depayloaders to implement RTP streaming.
*
* The udpsrc element supports automatic port allocation by setting the
* #GstUDPSrc:port property to 0. After setting the udpsrc to PAUSED, the
* allocated port can be obtained by reading the port property.
*
* udpsrc can read from multicast groups by setting the #GstUDPSrc:multicast-group
* property to the IP address of the multicast group.
*
* Alternatively one can provide a custom socket to udpsrc with the #GstUDPSrc:sockfd
* property, udpsrc will then not allocate a socket itself but use the provided
* one.
*
* The #GstUDPSrc:caps property is mainly used to give a type to the UDP packet
* so that they can be autoplugged in GStreamer pipelines. This is very usefull
* for RTP implementations where the contents of the UDP packets is transfered
* out-of-bounds using SDP or other means.
*
* The #GstUDPSrc:buffer-size property is used to change the default kernel
* buffersizes used for receiving packets. The buffer size may be increased for
* high-volume connections, or may be decreased to limit the possible backlog of
* incoming data. The system places an absolute limit on these values, on Linux,
* for example, the default buffer size is typically 50K and can be increased to
* maximally 100K.
*
* The #GstUDPSrc:skip-first-bytes property is used to strip off an arbitrary
* number of bytes from the start of the raw udp packet and can be used to strip
* off proprietary header, for example.
*
* The udpsrc is always a live source. It does however not provide a #GstClock,
* this is left for upstream elements such as an RTP session manager or demuxer
* (such as an MPEG demuxer). As with all live sources, the captured buffers
* will have their timestamp set to the current running time of the pipeline.
*
* udpsrc implements a #GstURIHandler interface that handles udp://host:port
* type URIs.
*
* If the #GstUDPSrc:timeout property is set to a value bigger than 0, udpsrc
* will generate an element message named
* <classname>&quot;GstUDPSrcTimeout&quot;</classname>
* if no data was recieved in the given timeout.
* The message's structure contains one field:
* <itemizedlist>
* <listitem>
* <para>
* #guint64
* <classname>&quot;timeout&quot;</classname>: the timeout in microseconds that
* expired when waiting for data.
* </para>
* </listitem>
* </itemizedlist>
* The message is typically used to detect that no UDP arrives in the receiver
* because it is blocked by a firewall.
* </para>
* <para>
* A custom file descriptor can be configured with the
* #GstUDPSrc:sockfd property. The socket will be closed when setting the
* element to READY by default. This behaviour can be
* overriden with the #GstUDPSrc:closefd property, in which case the application
* is responsible for closing the file descriptor.
*
* <refsect2>
* <title>Examples</title>
* |[
* gst-launch -v udpsrc ! fakesink dump=1
* ]| A pipeline to read from the default port and dump the udp packets.
* To actually generate udp packets on the default port one can use the
* udpsink element. When running the following pipeline in another terminal, the
* above mentioned pipeline should dump data packets to the console.
* |[
* gst-launch -v audiotestsrc ! udpsink
* ]|
* |[
* gst-launch -v udpsrc port=0 ! fakesink
* ]| read udp packets from a free port.
* </refsect2>
*
* Last reviewed on 2007-09-20 (0.10.7)
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstudpsrc.h"
2011-11-03 15:43:00 +00:00
#include <gst/net/gstnetaddressmeta.h>
2005-05-12 15:32:51 +00:00
#ifdef HAVE_SYS_SOCKET_H
2012-03-13 19:36:56 +00:00
#include <sys/socket.h>
#endif
2012-03-13 19:36:56 +00:00
GST_DEBUG_CATEGORY_STATIC (udpsrc_debug);
2005-05-12 15:32:51 +00:00
#define GST_CAT_DEFAULT (udpsrc_debug)
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS_ANY);
#define UDP_DEFAULT_PORT 5004
#define UDP_DEFAULT_MULTICAST_GROUP "0.0.0.0"
#define UDP_DEFAULT_MULTICAST_IFACE NULL
#define UDP_DEFAULT_URI "udp://"UDP_DEFAULT_MULTICAST_GROUP":"G_STRINGIFY(UDP_DEFAULT_PORT)
#define UDP_DEFAULT_CAPS NULL
2012-01-17 08:03:38 +00:00
#define UDP_DEFAULT_SOCKET NULL
#define UDP_DEFAULT_BUFFER_SIZE 0
#define UDP_DEFAULT_TIMEOUT 0
#define UDP_DEFAULT_SKIP_FIRST_BYTES 0
2012-01-17 08:03:38 +00:00
#define UDP_DEFAULT_CLOSE_SOCKET TRUE
#define UDP_DEFAULT_USED_SOCKET NULL
#define UDP_DEFAULT_AUTO_MULTICAST TRUE
#define UDP_DEFAULT_REUSE TRUE
enum
{
PROP_0,
PROP_PORT,
PROP_MULTICAST_GROUP,
PROP_MULTICAST_IFACE,
PROP_URI,
PROP_CAPS,
2012-01-17 08:03:38 +00:00
PROP_SOCKET,
PROP_BUFFER_SIZE,
PROP_TIMEOUT,
PROP_SKIP_FIRST_BYTES,
2012-01-17 08:03:38 +00:00
PROP_CLOSE_SOCKET,
PROP_USED_SOCKET,
PROP_AUTO_MULTICAST,
PROP_REUSE,
PROP_LAST
};
static void gst_udpsrc_uri_handler_init (gpointer g_iface, gpointer iface_data);
2011-06-09 15:50:08 +00:00
static GstCaps *gst_udpsrc_getcaps (GstBaseSrc * src, GstCaps * filter);
static GstFlowReturn gst_udpsrc_create (GstPushSrc * psrc, GstBuffer ** buf);
static gboolean gst_udpsrc_start (GstBaseSrc * bsrc);
static gboolean gst_udpsrc_stop (GstBaseSrc * bsrc);
2005-05-12 15:32:51 +00:00
static gboolean gst_udpsrc_unlock (GstBaseSrc * bsrc);
static gboolean gst_udpsrc_unlock_stop (GstBaseSrc * bsrc);
static void gst_udpsrc_finalize (GObject * object);
static void gst_udpsrc_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_udpsrc_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
2011-04-19 15:35:47 +00:00
#define gst_udpsrc_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstUDPSrc, gst_udpsrc, GST_TYPE_PUSH_SRC,
G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_udpsrc_uri_handler_init));
static void
gst_udpsrc_class_init (GstUDPSrcClass * klass)
{
GObjectClass *gobject_class;
2011-04-19 15:35:47 +00:00
GstElementClass *gstelement_class;
GstBaseSrcClass *gstbasesrc_class;
GstPushSrcClass *gstpushsrc_class;
gobject_class = (GObjectClass *) klass;
2011-04-19 15:35:47 +00:00
gstelement_class = (GstElementClass *) klass;
gstbasesrc_class = (GstBaseSrcClass *) klass;
gstpushsrc_class = (GstPushSrcClass *) klass;
2011-04-19 15:35:47 +00:00
GST_DEBUG_CATEGORY_INIT (udpsrc_debug, "udpsrc", 0, "UDP src");
gobject_class->set_property = gst_udpsrc_set_property;
gobject_class->get_property = gst_udpsrc_get_property;
gobject_class->finalize = gst_udpsrc_finalize;
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
g_param_spec_int ("port", "Port",
"The port to receive the packets from, 0=allocate", 0, G_MAXUINT16,
UDP_DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_MULTICAST_GROUP,
g_param_spec_string ("multicast-group", "Multicast Group",
"The Address of multicast group to join", UDP_DEFAULT_MULTICAST_GROUP,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
g_param_spec_string ("multicast-iface", "Multicast Interface",
"The network interface on which to join the multicast group",
UDP_DEFAULT_MULTICAST_IFACE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_URI,
g_param_spec_string ("uri", "URI",
"URI in the form of udp://multicast_group:port", UDP_DEFAULT_URI,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_CAPS,
g_param_spec_boxed ("caps", "Caps",
"The caps of the source pad", GST_TYPE_CAPS,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2012-01-17 08:03:38 +00:00
g_object_class_install_property (gobject_class, PROP_SOCKET,
g_param_spec_object ("socket", "Socket",
"Socket to use for UDP reception. (NULL == allocate)",
G_TYPE_SOCKET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_SIZE,
g_param_spec_int ("buffer-size", "Buffer Size",
"Size of the kernel receive buffer in bytes, 0=default", 0, G_MAXINT,
UDP_DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMEOUT,
g_param_spec_uint64 ("timeout", "Timeout",
2012-01-17 08:03:38 +00:00
"Post a message after timeout nanoseconds (0 = disabled)", 0,
G_MAXUINT64, UDP_DEFAULT_TIMEOUT,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (G_OBJECT_CLASS (klass),
PROP_SKIP_FIRST_BYTES, g_param_spec_int ("skip-first-bytes",
"Skip first bytes", "number of bytes to skip for each udp packet", 0,
G_MAXINT, UDP_DEFAULT_SKIP_FIRST_BYTES,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2012-01-17 08:03:38 +00:00
g_object_class_install_property (gobject_class, PROP_CLOSE_SOCKET,
g_param_spec_boolean ("close-socket", "Close socket",
"Close socket if passed as property on state change",
UDP_DEFAULT_CLOSE_SOCKET,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_USED_SOCKET,
g_param_spec_object ("used-socket", "Socket Handle",
"Socket currently in use for UDP reception. (NULL = no socket)",
G_TYPE_SOCKET, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_AUTO_MULTICAST,
g_param_spec_boolean ("auto-multicast", "Auto Multicast",
"Automatically join/leave multicast groups",
UDP_DEFAULT_AUTO_MULTICAST,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_REUSE,
g_param_spec_boolean ("reuse", "Reuse", "Enable reuse of the port",
UDP_DEFAULT_REUSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2011-04-19 15:35:47 +00:00
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&src_template));
gst_element_class_set_details_simple (gstelement_class, "UDP packet receiver",
"Source/Network",
"Receive data over the network via UDP",
"Wim Taymans <wim@fluendo.com>, "
"Thijs Vermeir <thijs.vermeir@barco.com>");
gstbasesrc_class->start = gst_udpsrc_start;
gstbasesrc_class->stop = gst_udpsrc_stop;
2005-05-12 15:32:51 +00:00
gstbasesrc_class->unlock = gst_udpsrc_unlock;
gstbasesrc_class->unlock_stop = gst_udpsrc_unlock_stop;
gstbasesrc_class->get_caps = gst_udpsrc_getcaps;
2005-05-12 15:32:51 +00:00
gstpushsrc_class->create = gst_udpsrc_create;
}
static void
2011-04-19 15:35:47 +00:00
gst_udpsrc_init (GstUDPSrc * udpsrc)
{
2012-01-17 08:03:38 +00:00
udpsrc->uri =
g_strdup_printf ("udp://%s:%u", UDP_DEFAULT_MULTICAST_GROUP,
UDP_DEFAULT_PORT);
2012-01-17 08:03:38 +00:00
udpsrc->host = g_strdup (UDP_DEFAULT_MULTICAST_GROUP);
udpsrc->port = UDP_DEFAULT_PORT;
udpsrc->socket = UDP_DEFAULT_SOCKET;
udpsrc->multi_iface = g_strdup (UDP_DEFAULT_MULTICAST_IFACE);
udpsrc->buffer_size = UDP_DEFAULT_BUFFER_SIZE;
udpsrc->timeout = UDP_DEFAULT_TIMEOUT;
udpsrc->skip_first_bytes = UDP_DEFAULT_SKIP_FIRST_BYTES;
2012-01-17 08:03:38 +00:00
udpsrc->close_socket = UDP_DEFAULT_CLOSE_SOCKET;
udpsrc->external_socket = (udpsrc->socket != NULL);
udpsrc->auto_multicast = UDP_DEFAULT_AUTO_MULTICAST;
2012-01-17 08:03:38 +00:00
udpsrc->used_socket = UDP_DEFAULT_USED_SOCKET;
udpsrc->reuse = UDP_DEFAULT_REUSE;
2012-01-17 08:03:38 +00:00
udpsrc->cancellable = g_cancellable_new ();
/* configure basesrc to be a live source */
gst_base_src_set_live (GST_BASE_SRC (udpsrc), TRUE);
/* make basesrc output a segment in time */
gst_base_src_set_format (GST_BASE_SRC (udpsrc), GST_FORMAT_TIME);
/* make basesrc set timestamps on outgoing buffers based on the running_time
* when they were captured */
gst_base_src_set_do_timestamp (GST_BASE_SRC (udpsrc), TRUE);
}
static void
gst_udpsrc_finalize (GObject * object)
{
GstUDPSrc *udpsrc;
udpsrc = GST_UDPSRC (object);
if (udpsrc->caps)
gst_caps_unref (udpsrc->caps);
2012-01-17 08:03:38 +00:00
udpsrc->caps = NULL;
g_free (udpsrc->multi_iface);
2012-01-17 08:03:38 +00:00
udpsrc->multi_iface = NULL;
2012-01-17 08:03:38 +00:00
g_free (udpsrc->uri);
udpsrc->uri = NULL;
2012-01-24 14:53:38 +00:00
g_free (udpsrc->host);
udpsrc->host = NULL;
2012-01-17 08:03:38 +00:00
if (udpsrc->socket)
g_object_unref (udpsrc->socket);
udpsrc->socket = NULL;
2012-01-17 08:03:38 +00:00
if (udpsrc->used_socket)
g_object_unref (udpsrc->used_socket);
udpsrc->used_socket = NULL;
if (udpsrc->cancellable)
g_object_unref (udpsrc->cancellable);
udpsrc->cancellable = NULL;
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static GstCaps *
2011-06-09 15:50:08 +00:00
gst_udpsrc_getcaps (GstBaseSrc * src, GstCaps * filter)
{
GstUDPSrc *udpsrc;
udpsrc = GST_UDPSRC (src);
2012-01-17 08:03:38 +00:00
if (udpsrc->caps) {
return (filter) ? gst_caps_intersect_full (filter, udpsrc->caps,
GST_CAPS_INTERSECT_FIRST) : gst_caps_ref (udpsrc->caps);
} else {
return (filter) ? gst_caps_ref (filter) : gst_caps_new_any ();
}
}
static GstFlowReturn
gst_udpsrc_create (GstPushSrc * psrc, GstBuffer ** buf)
{
GstUDPSrc *udpsrc;
GstBuffer *outbuf;
2012-01-17 08:03:38 +00:00
GSocketAddress *saddr = NULL;
guint8 *pktdata;
gint pktsize;
2011-04-05 15:06:41 +00:00
gsize offset;
2012-01-17 08:03:38 +00:00
gssize readsize;
gssize ret;
2005-05-12 15:32:51 +00:00
gboolean try_again;
2012-01-17 08:03:38 +00:00
GError *err = NULL;
udpsrc = GST_UDPSRC_CAST (psrc);
retry:
/* quick check, avoid going in select when we already have data */
2012-01-17 08:03:38 +00:00
readsize = g_socket_get_available_bytes (udpsrc->used_socket);
if (readsize > 0)
goto no_select;
do {
2005-05-12 15:32:51 +00:00
try_again = FALSE;
GST_LOG_OBJECT (udpsrc, "doing select, timeout %" G_GUINT64_FORMAT,
udpsrc->timeout);
2012-01-17 08:03:38 +00:00
if (!g_socket_condition_wait (udpsrc->used_socket, G_IO_IN | G_IO_PRI,
udpsrc->cancellable, &err)) {
if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_BUSY)
|| g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
goto stopped;
2012-01-17 08:03:38 +00:00
} else if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_TIMED_OUT)) {
/* timeout, post element message */
gst_element_post_message (GST_ELEMENT_CAST (udpsrc),
gst_message_new_element (GST_OBJECT_CAST (udpsrc),
gst_structure_new ("GstUDPSrcTimeout",
"timeout", G_TYPE_UINT64, udpsrc->timeout, NULL)));
} else {
2005-05-12 15:32:51 +00:00
goto select_error;
2012-01-17 08:03:38 +00:00
}
try_again = TRUE;
2005-05-12 15:32:51 +00:00
}
} while (G_UNLIKELY (try_again));
/* ask how much is available for reading on the socket, this should be exactly
* one UDP packet. We will check the return value, though, because in some
* case it can return 0 and we don't want a 0 sized buffer. */
2012-01-17 08:03:38 +00:00
readsize = g_socket_get_available_bytes (udpsrc->used_socket);
if (G_UNLIKELY (readsize < 0))
goto get_available_error;
/* If we get here and the readsize is zero, then either select was woken up
* by activity that is not a read, or a poll error occurred, or a UDP packet
* was received that has no data. Since we cannot identify which case it is,
* we handle all of them. This could possibly lead to a UDP packet getting
* lost, but since UDP is not reliable, we can accept this. */
if (G_UNLIKELY (!readsize)) {
/* try to read a packet (and it will be ignored),
* in case a packet with no data arrived */
2012-01-17 08:03:38 +00:00
pktdata = NULL;
pktsize = 0;
ret =
g_socket_receive_from (udpsrc->used_socket, NULL, (gchar *) pktdata,
pktsize, udpsrc->cancellable, &err);
if (G_UNLIKELY (ret < 0))
goto receive_error;
/* poll again */
goto retry;
}
no_select:
GST_LOG_OBJECT (udpsrc, "ioctl says %d bytes available", (int) readsize);
2005-05-12 15:32:51 +00:00
pktdata = g_malloc (readsize);
pktsize = readsize;
2011-04-05 15:06:41 +00:00
offset = 0;
2012-01-17 08:03:38 +00:00
if (saddr)
g_object_unref (saddr);
saddr = NULL;
ret =
g_socket_receive_from (udpsrc->used_socket, &saddr, (gchar *) pktdata,
pktsize, udpsrc->cancellable, &err);
if (G_UNLIKELY (ret < 0))
goto receive_error;
/* patch pktdata and len when stripping off the headers */
if (G_UNLIKELY (udpsrc->skip_first_bytes != 0)) {
if (G_UNLIKELY (readsize < udpsrc->skip_first_bytes))
goto skip_error;
2011-04-05 15:06:41 +00:00
offset += udpsrc->skip_first_bytes;
ret -= udpsrc->skip_first_bytes;
}
2011-04-05 15:06:41 +00:00
outbuf = gst_buffer_new ();
2012-03-30 16:13:08 +00:00
gst_buffer_append_memory (outbuf,
2012-02-22 01:06:17 +00:00
gst_memory_new_wrapped (0, pktdata, pktsize, offset, ret, pktdata,
g_free));
/* use buffer metadata so receivers can also track the address */
2012-01-17 08:03:38 +00:00
if (saddr) {
gst_buffer_add_net_address_meta (outbuf, saddr);
g_object_unref (saddr);
}
2012-01-17 08:03:38 +00:00
saddr = NULL;
GST_LOG_OBJECT (udpsrc, "read %d bytes", (int) readsize);
*buf = GST_BUFFER_CAST (outbuf);
return GST_FLOW_OK;
/* ERRORS */
select_error:
{
2005-05-12 15:32:51 +00:00
GST_ELEMENT_ERROR (udpsrc, RESOURCE, READ, (NULL),
2012-01-17 08:03:38 +00:00
("select error: %s", err->message));
g_clear_error (&err);
2005-05-12 15:32:51 +00:00
return GST_FLOW_ERROR;
}
stopped:
{
GST_DEBUG ("stop called");
return GST_FLOW_FLUSHING;
2005-05-12 15:32:51 +00:00
}
2012-01-17 08:03:38 +00:00
get_available_error:
2005-05-12 15:32:51 +00:00
{
GST_ELEMENT_ERROR (udpsrc, RESOURCE, READ, (NULL),
2012-01-17 08:03:38 +00:00
("get available bytes failed"));
return GST_FLOW_ERROR;
}
receive_error:
{
2005-05-12 15:32:51 +00:00
g_free (pktdata);
2012-01-17 08:03:38 +00:00
if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_BUSY) ||
g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
g_clear_error (&err);
return GST_FLOW_FLUSHING;
2012-01-17 08:03:38 +00:00
} else {
GST_ELEMENT_ERROR (udpsrc, RESOURCE, READ, (NULL),
2012-01-26 08:29:28 +00:00
("receive error %" G_GSSIZE_FORMAT ": %s", ret, err->message));
2012-01-17 08:03:38 +00:00
g_clear_error (&err);
return GST_FLOW_ERROR;
}
}
skip_error:
{
GST_ELEMENT_ERROR (udpsrc, STREAM, DECODE, (NULL),
("UDP buffer to small to skip header"));
return GST_FLOW_ERROR;
}
}
static gboolean
gst_udpsrc_set_uri (GstUDPSrc * src, const gchar * uri, GError ** error)
{
2012-01-17 08:03:38 +00:00
gchar *host;
guint16 port;
if (!gst_udp_parse_uri (uri, &host, &port))
goto wrong_uri;
2012-02-22 01:06:17 +00:00
if (port == (guint16) - 1)
2012-01-17 08:03:38 +00:00
port = UDP_DEFAULT_PORT;
g_free (src->host);
src->host = host;
src->port = port;
g_free (src->uri);
src->uri = g_strdup (uri);
return TRUE;
/* ERRORS */
wrong_uri:
{
2005-05-12 15:32:51 +00:00
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
("error parsing uri %s", uri));
g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
"Could not parse UDP URI");
return FALSE;
}
}
static void
gst_udpsrc_set_property (GObject * object, guint prop_id, const GValue * value,
GParamSpec * pspec)
{
GstUDPSrc *udpsrc = GST_UDPSRC (object);
switch (prop_id) {
case PROP_BUFFER_SIZE:
udpsrc->buffer_size = g_value_get_int (value);
break;
case PROP_PORT:
2012-01-17 08:03:38 +00:00
udpsrc->port = g_value_get_int (value);
g_free (udpsrc->uri);
udpsrc->uri = g_strdup_printf ("udp://%s:%u", udpsrc->host, udpsrc->port);
break;
case PROP_MULTICAST_GROUP:
{
const gchar *group;
2012-01-17 08:03:38 +00:00
g_free (udpsrc->host);
if ((group = g_value_get_string (value)))
2012-01-17 08:03:38 +00:00
udpsrc->host = g_strdup (group);
else
2012-01-17 08:03:38 +00:00
udpsrc->host = g_strdup (UDP_DEFAULT_MULTICAST_GROUP);
g_free (udpsrc->uri);
udpsrc->uri = g_strdup_printf ("udp://%s:%u", udpsrc->host, udpsrc->port);
break;
}
case PROP_MULTICAST_IFACE:
g_free (udpsrc->multi_iface);
if (g_value_get_string (value) == NULL)
udpsrc->multi_iface = g_strdup (UDP_DEFAULT_MULTICAST_IFACE);
else
udpsrc->multi_iface = g_value_dup_string (value);
break;
case PROP_URI:
gst_udpsrc_set_uri (udpsrc, g_value_get_string (value), NULL);
break;
case PROP_CAPS:
{
const GstCaps *new_caps_val = gst_value_get_caps (value);
GstCaps *new_caps;
GstCaps *old_caps;
if (new_caps_val == NULL) {
new_caps = gst_caps_new_any ();
} else {
new_caps = gst_caps_copy (new_caps_val);
}
old_caps = udpsrc->caps;
udpsrc->caps = new_caps;
if (old_caps)
gst_caps_unref (old_caps);
gst_pad_set_caps (GST_BASE_SRC (udpsrc)->srcpad, new_caps);
break;
}
2012-01-17 08:03:38 +00:00
case PROP_SOCKET:
if (udpsrc->socket != NULL && udpsrc->socket != udpsrc->used_socket &&
udpsrc->close_socket) {
GError *err = NULL;
if (!g_socket_close (udpsrc->socket, &err)) {
GST_ERROR ("failed to close socket %p: %s", udpsrc->socket,
err->message);
g_clear_error (&err);
}
}
if (udpsrc->socket)
g_object_unref (udpsrc->socket);
udpsrc->socket = g_value_dup_object (value);
GST_DEBUG ("setting socket to %p", udpsrc->socket);
break;
case PROP_TIMEOUT:
udpsrc->timeout = g_value_get_uint64 (value);
break;
case PROP_SKIP_FIRST_BYTES:
udpsrc->skip_first_bytes = g_value_get_int (value);
break;
2012-01-17 08:03:38 +00:00
case PROP_CLOSE_SOCKET:
udpsrc->close_socket = g_value_get_boolean (value);
break;
case PROP_AUTO_MULTICAST:
udpsrc->auto_multicast = g_value_get_boolean (value);
break;
case PROP_REUSE:
udpsrc->reuse = g_value_get_boolean (value);
break;
default:
break;
}
}
static void
gst_udpsrc_get_property (GObject * object, guint prop_id, GValue * value,
GParamSpec * pspec)
{
GstUDPSrc *udpsrc = GST_UDPSRC (object);
switch (prop_id) {
case PROP_BUFFER_SIZE:
g_value_set_int (value, udpsrc->buffer_size);
break;
case PROP_PORT:
2012-01-17 08:03:38 +00:00
g_value_set_int (value, udpsrc->port);
break;
case PROP_MULTICAST_GROUP:
2012-01-17 08:03:38 +00:00
g_value_set_string (value, udpsrc->host);
break;
case PROP_MULTICAST_IFACE:
g_value_set_string (value, udpsrc->multi_iface);
break;
case PROP_URI:
2012-01-24 14:52:09 +00:00
g_value_set_string (value, udpsrc->uri);
break;
case PROP_CAPS:
gst_value_set_caps (value, udpsrc->caps);
break;
2012-01-17 08:03:38 +00:00
case PROP_SOCKET:
g_value_set_object (value, udpsrc->socket);
break;
case PROP_TIMEOUT:
g_value_set_uint64 (value, udpsrc->timeout);
break;
case PROP_SKIP_FIRST_BYTES:
g_value_set_int (value, udpsrc->skip_first_bytes);
break;
2012-01-17 08:03:38 +00:00
case PROP_CLOSE_SOCKET:
g_value_set_boolean (value, udpsrc->close_socket);
break;
2012-01-17 08:03:38 +00:00
case PROP_USED_SOCKET:
g_value_set_object (value, udpsrc->used_socket);
break;
case PROP_AUTO_MULTICAST:
g_value_set_boolean (value, udpsrc->auto_multicast);
break;
case PROP_REUSE:
g_value_set_boolean (value, udpsrc->reuse);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/* create a socket for sending to remote machine */
static gboolean
gst_udpsrc_start (GstBaseSrc * bsrc)
{
GstUDPSrc *src;
2012-01-17 08:03:38 +00:00
GInetAddress *addr, *bind_addr;
GSocketAddress *bind_saddr;
GResolver *resolver;
GError *err = NULL;
src = GST_UDPSRC (bsrc);
2012-01-17 08:03:38 +00:00
if (src->socket == NULL) {
/* need to allocate a socket */
2012-01-17 08:03:38 +00:00
GST_DEBUG_OBJECT (src, "allocating socket for %s:%d", src->host, src->port);
addr = g_inet_address_new_from_string (src->host);
if (!addr) {
GList *results;
resolver = g_resolver_get_default ();
results =
g_resolver_lookup_by_name (resolver, src->host, src->cancellable,
&err);
if (!results)
goto name_resolve;
addr = G_INET_ADDRESS (g_object_ref (results->data));
g_resolver_free_addresses (results);
g_object_unref (resolver);
}
#ifndef GST_DISABLE_GST_DEBUG
{
gchar *ip = g_inet_address_to_string (addr);
2012-01-17 08:03:38 +00:00
GST_DEBUG_OBJECT (src, "IP address for host %s is %s", src->host, ip);
g_free (ip);
}
#endif
2012-01-17 08:03:38 +00:00
if ((src->used_socket =
g_socket_new (g_inet_address_get_family (addr),
G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &err)) == NULL)
goto no_socket;
src->external_socket = FALSE;
GST_DEBUG_OBJECT (src, "got socket %p", src->used_socket);
if (src->addr)
g_object_unref (src->addr);
src->addr =
G_INET_SOCKET_ADDRESS (g_inet_socket_address_new (addr, src->port));
GST_DEBUG_OBJECT (src, "binding on port %d", src->port);
if (g_inet_address_get_is_multicast (addr))
bind_addr = g_inet_address_new_any (g_inet_address_get_family (addr));
else
bind_addr = G_INET_ADDRESS (g_object_ref (addr));
g_object_unref (addr);
bind_saddr = g_inet_socket_address_new (bind_addr, src->port);
g_object_unref (bind_addr);
if (!g_socket_bind (src->used_socket, bind_saddr, src->reuse, &err))
goto bind_error;
2012-01-17 08:03:38 +00:00
g_object_unref (bind_saddr);
} else {
2012-01-26 15:05:34 +00:00
GST_DEBUG_OBJECT (src, "using provided socket %p", src->socket);
/* we use the configured socket, try to get some info about it */
2012-01-17 08:03:38 +00:00
src->used_socket = G_SOCKET (g_object_ref (src->socket));
src->external_socket = TRUE;
if (src->addr)
g_object_unref (src->addr);
src->addr =
G_INET_SOCKET_ADDRESS (g_socket_get_local_address (src->used_socket,
&err));
if (!src->addr)
goto getsockname_error;
}
2012-01-17 08:03:38 +00:00
if (src->timeout)
g_socket_set_timeout (src->used_socket, src->timeout / GST_SECOND);
2012-03-13 19:36:56 +00:00
#ifdef SO_RCVBUF
2012-01-17 08:03:38 +00:00
{
gint rcvsize, ret;
2012-03-13 19:36:56 +00:00
socklen_t len;
2012-01-17 08:03:38 +00:00
len = sizeof (rcvsize);
if (src->buffer_size != 0) {
rcvsize = src->buffer_size;
GST_DEBUG_OBJECT (src, "setting udp buffer of %d bytes", rcvsize);
/* set buffer size, Note that on Linux this is typically limited to a
* maximum of around 100K. Also a minimum of 128 bytes is required on
* Linux. */
ret =
setsockopt (g_socket_get_fd (src->used_socket), SOL_SOCKET, SO_RCVBUF,
(void *) &rcvsize, len);
if (ret != 0) {
GST_ELEMENT_WARNING (src, RESOURCE, SETTINGS, (NULL),
("Could not create a buffer of requested %d bytes, %d: %s (%d)",
rcvsize, ret, g_strerror (errno), errno));
}
}
2012-01-17 08:03:38 +00:00
/* read the value of the receive buffer. Note that on linux this returns 2x the
* value we set because the kernel allocates extra memory for metadata.
* The default on Linux is about 100K (which is about 50K without metadata) */
ret =
getsockopt (g_socket_get_fd (src->used_socket), SOL_SOCKET, SO_RCVBUF,
(void *) &rcvsize, &len);
if (ret == 0)
GST_DEBUG_OBJECT (src, "have udp buffer of %d bytes", rcvsize);
else
GST_DEBUG_OBJECT (src, "could not get udp buffer size");
}
#endif
2012-01-17 08:03:38 +00:00
g_socket_set_broadcast (src->used_socket, TRUE);
if (src->auto_multicast
&&
g_inet_address_get_is_multicast (g_inet_socket_address_get_address
(src->addr))) {
GST_DEBUG_OBJECT (src, "joining multicast group %s", src->host);
if (!g_socket_join_multicast_group (src->used_socket,
g_inet_socket_address_get_address (src->addr),
FALSE, src->multi_iface, &err))
goto membership;
}
2005-05-12 15:32:51 +00:00
/* NOTE: sockaddr_in.sin_port works for ipv4 and ipv6 because sin_port
* follows ss_family on both */
2012-01-17 08:03:38 +00:00
{
GInetSocketAddress *addr;
guint16 port;
2012-01-17 08:03:38 +00:00
addr =
G_INET_SOCKET_ADDRESS (g_socket_get_local_address (src->used_socket,
&err));
if (!addr)
goto getsockname_error;
2012-01-17 08:03:38 +00:00
port = g_inet_socket_address_get_port (addr);
GST_DEBUG_OBJECT (src, "bound, on port %d", port);
if (port != src->port) {
src->port = port;
GST_DEBUG_OBJECT (src, "notifying port %d", port);
g_object_notify (G_OBJECT (src), "port");
}
g_object_unref (addr);
}
return TRUE;
/* ERRORS */
2012-01-17 08:03:38 +00:00
name_resolve:
{
GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
2012-01-17 08:03:38 +00:00
("Name resolval failed: %s", err->message));
g_clear_error (&err);
g_object_unref (resolver);
return FALSE;
}
no_socket:
{
2005-05-12 15:32:51 +00:00
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
2012-01-17 08:03:38 +00:00
("no socket error: %s", err->message));
g_clear_error (&err);
g_object_unref (addr);
return FALSE;
}
bind_error:
{
2012-01-17 08:03:38 +00:00
gst_udpsrc_stop (GST_BASE_SRC (src));
2005-05-12 15:32:51 +00:00
GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
2012-01-17 08:03:38 +00:00
("bind failed: %s", err->message));
g_clear_error (&err);
g_object_unref (bind_saddr);
2005-05-12 15:32:51 +00:00
return FALSE;
}
membership:
{
2012-01-17 08:03:38 +00:00
gst_udpsrc_stop (GST_BASE_SRC (src));
2005-05-12 15:32:51 +00:00
GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
2012-01-17 08:03:38 +00:00
("could add membership: %s", err->message));
g_clear_error (&err);
return FALSE;
}
getsockname_error:
{
2012-01-17 08:03:38 +00:00
gst_udpsrc_stop (GST_BASE_SRC (src));
2005-05-12 15:32:51 +00:00
GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
2012-01-17 08:03:38 +00:00
("getsockname failed: %s", err->message));
g_clear_error (&err);
return FALSE;
}
}
2005-05-12 15:32:51 +00:00
static gboolean
gst_udpsrc_unlock (GstBaseSrc * bsrc)
{
GstUDPSrc *src;
src = GST_UDPSRC (bsrc);
GST_LOG_OBJECT (src, "Flushing");
2012-01-17 08:03:38 +00:00
g_cancellable_cancel (src->cancellable);
return TRUE;
}
static gboolean
gst_udpsrc_unlock_stop (GstBaseSrc * bsrc)
{
GstUDPSrc *src;
src = GST_UDPSRC (bsrc);
GST_LOG_OBJECT (src, "No longer flushing");
2012-01-17 08:03:38 +00:00
g_cancellable_reset (src->cancellable);
2005-05-12 15:32:51 +00:00
return TRUE;
}
static gboolean
gst_udpsrc_stop (GstBaseSrc * bsrc)
{
GstUDPSrc *src;
src = GST_UDPSRC (bsrc);
GST_DEBUG ("stopping, closing sockets");
2012-01-17 08:03:38 +00:00
if (src->used_socket) {
if (src->auto_multicast
&&
g_inet_address_get_is_multicast (g_inet_socket_address_get_address
(src->addr))) {
GError *err = NULL;
GST_DEBUG_OBJECT (src, "leaving multicast group %s", src->host);
if (!g_socket_leave_multicast_group (src->used_socket,
g_inet_socket_address_get_address (src->addr), FALSE,
src->multi_iface, NULL)) {
GST_ERROR_OBJECT (src, "Failed to leave multicast group: %s",
err->message);
g_clear_error (&err);
}
}
if (src->close_socket || !src->external_socket) {
GError *err = NULL;
if (!g_socket_close (src->used_socket, &err)) {
GST_ERROR_OBJECT (src, "Failed to close socket: %s", err->message);
g_clear_error (&err);
}
}
2012-01-17 08:03:38 +00:00
g_object_unref (src->used_socket);
src->used_socket = NULL;
g_object_unref (src->addr);
src->addr = NULL;
}
return TRUE;
}
/*** GSTURIHANDLER INTERFACE *************************************************/
static GstURIType
2011-07-04 16:15:42 +00:00
gst_udpsrc_uri_get_type (GType type)
{
return GST_URI_SRC;
}
static const gchar *const *
2011-07-04 16:15:42 +00:00
gst_udpsrc_uri_get_protocols (GType type)
{
static const gchar *protocols[] = { "udp", NULL };
return protocols;
}
static gchar *
gst_udpsrc_uri_get_uri (GstURIHandler * handler)
{
GstUDPSrc *src = GST_UDPSRC (handler);
2012-01-17 08:03:38 +00:00
return g_strdup (src->uri);
}
static gboolean
gst_udpsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
GError ** error)
{
return gst_udpsrc_set_uri (GST_UDPSRC (handler), uri, error);
}
static void
gst_udpsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
{
GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
iface->get_type = gst_udpsrc_uri_get_type;
iface->get_protocols = gst_udpsrc_uri_get_protocols;
iface->get_uri = gst_udpsrc_uri_get_uri;
iface->set_uri = gst_udpsrc_uri_set_uri;
}