/* GStreamer
- * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
+ * Copyright (C) <2005> Wim Taymans <wim@fluendo.com>
+ * 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
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
-
-
+/**
+ * SECTION:element-udpsink
+ * @see_also: udpsrc, multifdsink
+ *
+ * udpsink is a network sink that sends UDP packets to the network.
+ * It can be combined with RTP payloaders to implement RTP streaming.
+ *
+ * <refsect2>
+ * <title>Examples</title>
+ * |[
+ * gst-launch -v audiotestsrc ! udpsink
+ * ]|
+ * </refsect2>
+ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstudpsink.h"
-#define UDP_DEFAULT_HOST "localhost"
-#define UDP_DEFAULT_PORT 4951
-#define UDP_DEFAULT_CONTROL 1
-
-/* elementfactory information */
-GstElementDetails gst_udpsink_details = GST_ELEMENT_DETAILS (
- "UDP packet sender",
- "Sink/Network",
- "Send data over the network via UDP",
- "Wim Taymans <wim.taymans@chello.be>"
-);
+#define UDP_DEFAULT_HOST "localhost"
+#define UDP_DEFAULT_PORT 4951
/* UDPSink signals and args */
-enum {
- FRAME_ENCODED,
+enum
+{
/* FILL ME */
LAST_SIGNAL
};
-enum {
- ARG_0,
- ARG_HOST,
- ARG_PORT,
- ARG_CONTROL,
- ARG_MTU
+enum
+{
+ PROP_0,
+ PROP_HOST,
+ PROP_PORT,
+ PROP_URI,
/* FILL ME */
};
-#define GST_TYPE_UDPSINK_CONTROL (gst_udpsink_control_get_type())
-static GType
-gst_udpsink_control_get_type(void) {
- static GType udpsink_control_type = 0;
- static GEnumValue udpsink_control[] = {
- {CONTROL_NONE, "1", "none"},
- {CONTROL_UDP, "2", "udp"},
- {CONTROL_TCP, "3", "tcp"},
- {CONTROL_ZERO, NULL, NULL},
- };
- if (!udpsink_control_type) {
- udpsink_control_type = g_enum_register_static("GstUDPSinkControl", udpsink_control);
- }
- return udpsink_control_type;
-}
+static void gst_udpsink_finalize (GstUDPSink * udpsink);
-static void gst_udpsink_base_init (gpointer g_class);
-static void gst_udpsink_class_init (GstUDPSink *klass);
-static void gst_udpsink_init (GstUDPSink *udpsink);
+static void gst_udpsink_uri_handler_init (gpointer g_iface,
+ gpointer iface_data);
-static void gst_udpsink_set_clock (GstElement *element, GstClock *clock);
+static void gst_udpsink_set_property (GObject * object, guint prop_id,
+ const GValue * value, GParamSpec * pspec);
+static void gst_udpsink_get_property (GObject * object, guint prop_id,
+ GValue * value, GParamSpec * pspec);
-static void gst_udpsink_chain (GstPad *pad,GstData *_data);
-static GstElementStateReturn gst_udpsink_change_state (GstElement *element);
-
-static void gst_udpsink_set_property (GObject *object, guint prop_id,
- const GValue *value, GParamSpec *pspec);
-static void gst_udpsink_get_property (GObject *object, guint prop_id,
- GValue *value, GParamSpec *pspec);
-
-
-static GstElementClass *parent_class = NULL;
/*static guint gst_udpsink_signals[LAST_SIGNAL] = { 0 }; */
-
-GType
-gst_udpsink_get_type (void)
-{
- static GType udpsink_type = 0;
-
- if (!udpsink_type) {
- static const GTypeInfo udpsink_info = {
- sizeof(GstUDPSinkClass),
- gst_udpsink_base_init,
- NULL,
- (GClassInitFunc)gst_udpsink_class_init,
- NULL,
- NULL,
- sizeof(GstUDPSink),
- 0,
- (GInstanceInitFunc)gst_udpsink_init,
- NULL
- };
- udpsink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstUDPSink", &udpsink_info, 0);
- }
- return udpsink_type;
-}
+#define gst_udpsink_parent_class parent_class
+G_DEFINE_TYPE_WITH_CODE (GstUDPSink, gst_udpsink, GST_TYPE_MULTIUDPSINK,
+ G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_udpsink_uri_handler_init));
static void
-gst_udpsink_base_init (gpointer g_class)
-{
- GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
-
- gst_element_class_set_details (element_class, &gst_udpsink_details);
-}
-
-static void
-gst_udpsink_class_init (GstUDPSink *klass)
+gst_udpsink_class_init (GstUDPSinkClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
- gobject_class = (GObjectClass*) klass;
- gstelement_class = (GstElementClass*) klass;
-
- parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
-
- g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_HOST,
- g_param_spec_string ("host", "host",
- "The host/IP/Multicast group to send the packets to",
- UDP_DEFAULT_HOST, G_PARAM_READWRITE));
- g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PORT,
- g_param_spec_int ("port", "port", "The port to send the packets to",
- 0, 32768, UDP_DEFAULT_PORT, G_PARAM_READWRITE));
- g_object_class_install_property (gobject_class, ARG_CONTROL,
- g_param_spec_enum ("control", "control", "The type of control",
- GST_TYPE_UDPSINK_CONTROL, CONTROL_UDP, G_PARAM_READWRITE));
- g_object_class_install_property (gobject_class, ARG_MTU,
- g_param_spec_int ("mtu", "mtu", "maximun transmit unit", G_MININT, G_MAXINT,
- 0, G_PARAM_READWRITE)); /* CHECKME */
+ gobject_class = (GObjectClass *) klass;
+ gstelement_class = (GstElementClass *) klass;
gobject_class->set_property = gst_udpsink_set_property;
gobject_class->get_property = gst_udpsink_get_property;
- gstelement_class->change_state = gst_udpsink_change_state;
- gstelement_class->set_clock = gst_udpsink_set_clock;
-}
+ gobject_class->finalize = (GObjectFinalizeFunc) gst_udpsink_finalize;
+ g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HOST,
+ g_param_spec_string ("host", "host",
+ "The host/IP/Multicast group to send the packets to",
+ UDP_DEFAULT_HOST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
+ g_param_spec_int ("port", "port", "The port to send the packets to",
+ 0, 65535, UDP_DEFAULT_PORT,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
-static GstPadLinkReturn
-gst_udpsink_sinkconnect (GstPad *pad, GstCaps *caps)
-{
- GstUDPSink *udpsink;
- struct sockaddr_in serv_addr;
- struct hostent *serverhost;
- int fd;
- FILE *f;
- guint bc_val;
-#ifndef GST_DISABLE_LOADSAVE
- xmlDocPtr doc;
- xmlChar *buf;
- int buf_size;
-
- udpsink = GST_UDPSINK (gst_pad_get_parent (pad));
-
- memset(&serv_addr, 0, sizeof(serv_addr));
-
- /* its a name rather than an ipnum */
- serverhost = gethostbyname(udpsink->host);
- if (serverhost == (struct hostent *)0) {
- perror("gethostbyname");
- return GST_PAD_LINK_REFUSED;
- }
-
- memmove(&serv_addr.sin_addr,serverhost->h_addr, serverhost->h_length);
-
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_port = htons(udpsink->port+1);
-
- doc = xmlNewDoc ("1.0");
- doc->xmlRootNode = xmlNewDocNode (doc, NULL, "NewCaps", NULL);
-
- gst_caps_save_thyself (caps, doc->xmlRootNode);
-
- switch (udpsink->control) {
- case CONTROL_UDP:
- if ((fd = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
- perror("socket");
- return GST_PAD_LINK_REFUSED;
- }
-
- /* We can only do broadcast in udp */
- bc_val = 1;
- setsockopt (fd,SOL_SOCKET, SO_BROADCAST, &bc_val, sizeof (bc_val));
-
- xmlDocDumpMemory(doc, &buf, &buf_size);
-
- if (sendto (fd, buf, buf_size, 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1)
- {
- perror("sending");
- return GST_PAD_LINK_REFUSED;
- }
- close (fd);
- break;
- case CONTROL_TCP:
- if ((fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
- perror("socket");
- return GST_PAD_LINK_REFUSED;
- }
-
- if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != 0) {
- g_printerr ("udpsink: connect to %s port %d failed: %s\n",
- udpsink->host, udpsink->port, g_strerror(errno));
- return GST_PAD_LINK_REFUSED;
- }
-
- f = fdopen (dup (fd), "wb");
-
- xmlDocDump(f, doc);
- fclose (f);
- close (fd);
- break;
- case CONTROL_NONE:
- return GST_PAD_LINK_OK;
- break;
- default:
- return GST_PAD_LINK_REFUSED;
- break;
- }
-#endif
-
- return GST_PAD_LINK_OK;
+ gst_element_class_set_details_simple (gstelement_class, "UDP packet sender",
+ "Sink/Network",
+ "Send data over the network via UDP", "Wim Taymans <wim@fluendo.com>");
}
static void
-gst_udpsink_set_clock (GstElement *element, GstClock *clock)
+gst_udpsink_init (GstUDPSink * udpsink)
{
- GstUDPSink *udpsink;
-
- udpsink = GST_UDPSINK (element);
+ udpsink->host = g_strdup (UDP_DEFAULT_HOST);
+ udpsink->port = UDP_DEFAULT_PORT;
+ udpsink->uri = g_strdup_printf ("udp://%s:%d", udpsink->host, udpsink->port);
- udpsink->clock = clock;
+ gst_multiudpsink_add (GST_MULTIUDPSINK (udpsink), udpsink->host,
+ udpsink->port);
}
static void
-gst_udpsink_init (GstUDPSink *udpsink)
+gst_udpsink_finalize (GstUDPSink * udpsink)
{
- /* create the sink and src pads */
- udpsink->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
- gst_element_add_pad (GST_ELEMENT (udpsink), udpsink->sinkpad);
- gst_pad_set_chain_function (udpsink->sinkpad, gst_udpsink_chain);
- gst_pad_set_link_function (udpsink->sinkpad, gst_udpsink_sinkconnect);
+ g_free (udpsink->host);
+ udpsink->host = NULL;
- udpsink->host = g_strdup (UDP_DEFAULT_HOST);
- udpsink->port = UDP_DEFAULT_PORT;
- udpsink->control = CONTROL_UDP;
- udpsink->mtu = 1024;
-
- udpsink->clock = NULL;
+ g_free (udpsink->uri);
+ udpsink->uri = NULL;
+
+ G_OBJECT_CLASS (parent_class)->finalize ((GObject *) udpsink);
}
-static void
-gst_udpsink_chain (GstPad *pad, GstData *_data)
+static gboolean
+gst_udpsink_set_uri (GstUDPSink * sink, const gchar * uri, GError ** error)
{
- GstBuffer *buf = GST_BUFFER (_data);
- GstUDPSink *udpsink;
- guint tolen, i;
+ gst_multiudpsink_remove (GST_MULTIUDPSINK (sink), sink->host, sink->port);
- g_return_if_fail (pad != NULL);
- g_return_if_fail (GST_IS_PAD (pad));
- g_return_if_fail (buf != NULL);
+ if (!gst_udp_parse_uri (uri, &sink->host, &sink->port))
+ goto wrong_uri;
- udpsink = GST_UDPSINK (GST_OBJECT_PARENT (pad));
-
- if (udpsink->clock) {
- GstClockID id = gst_clock_new_single_shot_id (udpsink->clock, GST_BUFFER_TIMESTAMP (buf));
+ g_free (sink->uri);
+ sink->uri = g_strdup (uri);
- GST_DEBUG ("udpsink: clock wait: %" G_GUINT64_FORMAT "\n", GST_BUFFER_TIMESTAMP (buf));
- gst_element_clock_wait (GST_ELEMENT (udpsink), id, NULL);
- gst_clock_id_free (id);
- }
-
- tolen = sizeof(udpsink->theiraddr);
-
- /*
- if (sendto (udpsink->sock, GST_BUFFER_DATA (buf),
- GST_BUFFER_SIZE (buf), 0, (struct sockaddr *) &udpsink->theiraddr,
- tolen) == -1) {
- perror("sending");
- }
-*/
- /* MTU */
- for (i = 0; i < GST_BUFFER_SIZE (buf); i += udpsink->mtu) {
- if (GST_BUFFER_SIZE (buf) - i > udpsink->mtu) {
- if (sendto (udpsink->sock, GST_BUFFER_DATA (buf) + i,
- udpsink->mtu, 0, (struct sockaddr *) &udpsink->theiraddr,
- tolen) == -1) {
- perror("sending");
- }
- }
- else {
- if (sendto (udpsink->sock, GST_BUFFER_DATA (buf) + i,
- GST_BUFFER_SIZE (buf) -i, 0,
- (struct sockaddr *) &udpsink->theiraddr, tolen) == -1) {
- perror("sending");
- }
- }
- }
+ gst_multiudpsink_add (GST_MULTIUDPSINK (sink), sink->host, sink->port);
- gst_buffer_unref(buf);
+ return TRUE;
+
+ /* ERRORS */
+wrong_uri:
+ {
+ GST_ELEMENT_ERROR (sink, 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_udpsink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
+gst_udpsink_set_property (GObject * object, guint prop_id, const GValue * value,
+ GParamSpec * pspec)
{
GstUDPSink *udpsink;
- /* it's not null if we got it, but it might not be ours */
- g_return_if_fail(GST_IS_UDPSINK(object));
- udpsink = GST_UDPSINK(object);
+ udpsink = GST_UDPSINK (object);
+
+ /* remove old host */
+ gst_multiudpsink_remove (GST_MULTIUDPSINK (udpsink),
+ udpsink->host, udpsink->port);
switch (prop_id) {
- case ARG_HOST:
- if (udpsink->host != NULL) g_free(udpsink->host);
- if (g_value_get_string (value) == NULL)
- udpsink->host = NULL;
- else
- udpsink->host = g_strdup (g_value_get_string (value));
+ case PROP_HOST:
+ {
+ const gchar *host;
+
+ host = g_value_get_string (value);
+ g_free (udpsink->host);
+ udpsink->host = g_strdup (host);
+ g_free (udpsink->uri);
+ udpsink->uri =
+ g_strdup_printf ("udp://%s:%d", udpsink->host, udpsink->port);
break;
- case ARG_PORT:
- udpsink->port = g_value_get_int (value);
- break;
- case ARG_CONTROL:
- udpsink->control = g_value_get_enum (value);
- break;
- case ARG_MTU:
- udpsink->mtu = g_value_get_int (value);
+ }
+ case PROP_PORT:
+ udpsink->port = g_value_get_int (value);
+ g_free (udpsink->uri);
+ udpsink->uri =
+ g_strdup_printf ("udp://%s:%d", udpsink->host, udpsink->port);
break;
default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
+ /* add new host */
+ gst_multiudpsink_add (GST_MULTIUDPSINK (udpsink),
+ udpsink->host, udpsink->port);
}
static void
-gst_udpsink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
+gst_udpsink_get_property (GObject * object, guint prop_id, GValue * value,
+ GParamSpec * pspec)
{
GstUDPSink *udpsink;
- /* it's not null if we got it, but it might not be ours */
- g_return_if_fail(GST_IS_UDPSINK(object));
- udpsink = GST_UDPSINK(object);
+ udpsink = GST_UDPSINK (object);
switch (prop_id) {
- case ARG_HOST:
+ case PROP_HOST:
g_value_set_string (value, udpsink->host);
break;
- case ARG_PORT:
+ case PROP_PORT:
g_value_set_int (value, udpsink->port);
break;
- case ARG_CONTROL:
- g_value_set_enum (value, udpsink->control);
- break;
- case ARG_MTU:
- g_value_set_int (value, udpsink->mtu);
- break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
+/*** GSTURIHANDLER INTERFACE *************************************************/
-/* create a socket for sending to remote machine */
-static gboolean
-gst_udpsink_init_send (GstUDPSink *sink)
+static GstURIType
+gst_udpsink_uri_get_type (GType type)
{
- struct hostent *he;
- struct in_addr addr;
- guint bc_val;
-
- bzero (&sink->theiraddr, sizeof (sink->theiraddr));
- sink->theiraddr.sin_family = AF_INET; /* host byte order */
- sink->theiraddr.sin_port = htons (sink->port); /* short, network byte order */
-
- /* if its an IP address */
- if (inet_aton (sink->host, &addr)) {
- /* check if its a multicast address */
- if ((ntohl (addr.s_addr) & 0xe0000000) == 0xe0000000) {
- sink->multi_addr.imr_multiaddr.s_addr = addr.s_addr;
- sink->multi_addr.imr_interface.s_addr = INADDR_ANY;
-
- sink->theiraddr.sin_addr = sink->multi_addr.imr_multiaddr;
-
- /* Joining the multicast group */
- setsockopt (sink->sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &sink->multi_addr, sizeof(sink->multi_addr));
- }
-
- else {
- sink->theiraddr.sin_addr =
- *((struct in_addr *) &addr);
- }
- }
-
- /* we dont need to lookup for localhost */
- else if (strcmp (sink->host, UDP_DEFAULT_HOST) == 0 &&
- inet_aton ("127.0.0.1", &addr)) {
- sink->theiraddr.sin_addr =
- *((struct in_addr *) &addr);
- }
-
- /* if its a hostname */
- else if ((he = gethostbyname (sink->host))) {
- sink->theiraddr.sin_addr = *((struct in_addr *) he->h_addr);
- }
-
- else {
- perror("hostname lookup error?");
- return FALSE;
- }
-
- if ((sink->sock = socket (AF_INET, SOCK_DGRAM, 0)) == -1) {
- perror("socket");
- return FALSE;
- }
+ return GST_URI_SINK;
+}
- bc_val = 1;
- setsockopt (sink->sock, SOL_SOCKET, SO_BROADCAST, &bc_val, sizeof (bc_val));
-
- GST_FLAG_SET (sink, GST_UDPSINK_OPEN);
+static const gchar *const *
+gst_udpsink_uri_get_protocols (GType type)
+{
+ static const gchar *protocols[] = { "udp", NULL };
- return TRUE;
+ return protocols;
}
-static void
-gst_udpsink_close (GstUDPSink *sink)
+static gchar *
+gst_udpsink_uri_get_uri (GstURIHandler * handler)
{
- close (sink->sock);
+ GstUDPSink *sink = GST_UDPSINK (handler);
- GST_FLAG_UNSET (sink, GST_UDPSINK_OPEN);
+ return g_strdup (sink->uri);
}
-static GstElementStateReturn
-gst_udpsink_change_state (GstElement *element)
+static gboolean
+gst_udpsink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
+ GError ** error)
{
- g_return_val_if_fail (GST_IS_UDPSINK (element), GST_STATE_FAILURE);
-
- if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
- if (GST_FLAG_IS_SET (element, GST_UDPSINK_OPEN))
- gst_udpsink_close (GST_UDPSINK (element));
- } else {
- if (!GST_FLAG_IS_SET (element, GST_UDPSINK_OPEN)) {
- if (!gst_udpsink_init_send (GST_UDPSINK (element)))
- return GST_STATE_FAILURE;
- }
- }
+ return gst_udpsink_set_uri (GST_UDPSINK (handler), uri, error);
+}
- if (GST_ELEMENT_CLASS (parent_class)->change_state)
- return GST_ELEMENT_CLASS (parent_class)->change_state (element);
+static void
+gst_udpsink_uri_handler_init (gpointer g_iface, gpointer iface_data)
+{
+ GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
- return GST_STATE_SUCCESS;
+ iface->get_type = gst_udpsink_uri_get_type;
+ iface->get_protocols = gst_udpsink_uri_get_protocols;
+ iface->get_uri = gst_udpsink_uri_get_uri;
+ iface->set_uri = gst_udpsink_uri_set_uri;
}
-