2 * Copyright (C) <2005> Wim Taymans <wim@fluendo.com>
3 * Copyright (C) <2012> Collabora Ltd.
4 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 * SECTION:element-udpsink
23 * @see_also: udpsrc, multifdsink
25 * udpsink is a network sink that sends UDP packets to the network.
26 * It can be combined with RTP payloaders to implement RTP streaming.
29 * <title>Examples</title>
31 * gst-launch-1.0 -v audiotestsrc ! udpsink
38 #include "gstudpsink.h"
40 #define UDP_DEFAULT_HOST "localhost"
41 #define UDP_DEFAULT_PORT 5004
43 /* UDPSink signals and args */
59 static void gst_udpsink_finalize (GstUDPSink * udpsink);
61 static void gst_udpsink_uri_handler_init (gpointer g_iface,
64 static void gst_udpsink_set_property (GObject * object, guint prop_id,
65 const GValue * value, GParamSpec * pspec);
66 static void gst_udpsink_get_property (GObject * object, guint prop_id,
67 GValue * value, GParamSpec * pspec);
69 /*static guint gst_udpsink_signals[LAST_SIGNAL] = { 0 }; */
70 #define gst_udpsink_parent_class parent_class
71 G_DEFINE_TYPE_WITH_CODE (GstUDPSink, gst_udpsink, GST_TYPE_MULTIUDPSINK,
72 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_udpsink_uri_handler_init));
75 gst_udpsink_class_init (GstUDPSinkClass * klass)
77 GObjectClass *gobject_class;
78 GstElementClass *gstelement_class;
80 gobject_class = (GObjectClass *) klass;
81 gstelement_class = (GstElementClass *) klass;
83 gobject_class->set_property = gst_udpsink_set_property;
84 gobject_class->get_property = gst_udpsink_get_property;
86 gobject_class->finalize = (GObjectFinalizeFunc) gst_udpsink_finalize;
88 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HOST,
89 g_param_spec_string ("host", "host",
90 "The host/IP/Multicast group to send the packets to",
91 UDP_DEFAULT_HOST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
92 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
93 g_param_spec_int ("port", "port", "The port to send the packets to",
94 0, 65535, UDP_DEFAULT_PORT,
95 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
97 gst_element_class_set_static_metadata (gstelement_class, "UDP packet sender",
99 "Send data over the network via UDP", "Wim Taymans <wim@fluendo.com>");
103 gst_udpsink_init (GstUDPSink * udpsink)
105 udpsink->host = g_strdup (UDP_DEFAULT_HOST);
106 udpsink->port = UDP_DEFAULT_PORT;
107 udpsink->uri = g_strdup_printf ("udp://%s:%d", udpsink->host, udpsink->port);
109 gst_multiudpsink_add (GST_MULTIUDPSINK (udpsink), udpsink->host,
114 gst_udpsink_finalize (GstUDPSink * udpsink)
116 g_free (udpsink->host);
117 udpsink->host = NULL;
119 g_free (udpsink->uri);
122 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) udpsink);
126 gst_udpsink_set_uri (GstUDPSink * sink, const gchar * uri, GError ** error)
131 gst_multiudpsink_remove (GST_MULTIUDPSINK (sink), sink->host, sink->port);
133 if (!gst_udp_parse_uri (uri, &host, &port))
141 sink->uri = g_strdup (uri);
143 gst_multiudpsink_add (GST_MULTIUDPSINK (sink), sink->host, sink->port);
150 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
151 ("error parsing uri %s", uri));
152 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
153 "Could not parse UDP URI");
159 gst_udpsink_set_property (GObject * object, guint prop_id, const GValue * value,
164 udpsink = GST_UDPSINK (object);
166 /* remove old host */
167 gst_multiudpsink_remove (GST_MULTIUDPSINK (udpsink),
168 udpsink->host, udpsink->port);
175 host = g_value_get_string (value);
176 g_free (udpsink->host);
177 udpsink->host = g_strdup (host);
178 g_free (udpsink->uri);
180 g_strdup_printf ("udp://%s:%d", udpsink->host, udpsink->port);
184 udpsink->port = g_value_get_int (value);
185 g_free (udpsink->uri);
187 g_strdup_printf ("udp://%s:%d", udpsink->host, udpsink->port);
190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194 gst_multiudpsink_add (GST_MULTIUDPSINK (udpsink),
195 udpsink->host, udpsink->port);
199 gst_udpsink_get_property (GObject * object, guint prop_id, GValue * value,
204 udpsink = GST_UDPSINK (object);
208 g_value_set_string (value, udpsink->host);
211 g_value_set_int (value, udpsink->port);
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219 /*** GSTURIHANDLER INTERFACE *************************************************/
222 gst_udpsink_uri_get_type (GType type)
227 static const gchar *const *
228 gst_udpsink_uri_get_protocols (GType type)
230 static const gchar *protocols[] = { "udp", NULL };
236 gst_udpsink_uri_get_uri (GstURIHandler * handler)
238 GstUDPSink *sink = GST_UDPSINK (handler);
240 return g_strdup (sink->uri);
244 gst_udpsink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
247 return gst_udpsink_set_uri (GST_UDPSINK (handler), uri, error);
251 gst_udpsink_uri_handler_init (gpointer g_iface, gpointer iface_data)
253 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
255 iface->get_type = gst_udpsink_uri_get_type;
256 iface->get_protocols = gst_udpsink_uri_get_protocols;
257 iface->get_uri = gst_udpsink_uri_get_uri;
258 iface->set_uri = gst_udpsink_uri_set_uri;