2 * Copyright (C) <2005> Wim Taymans <wim@fluendo.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 * SECTION:element-udpsink
21 * @see_also: udpsrc, multifdsink
23 * udpsink is a network sink that sends UDP packets to the network.
24 * It can be combined with RTP payloaders to implement RTP streaming.
27 * <title>Examples</title>
29 * gst-launch -v audiotestsrc ! udpsink
36 #include "gstudpsink.h"
46 #define UDP_DEFAULT_HOST "localhost"
47 #define UDP_DEFAULT_PORT 4951
49 /* elementfactory information */
50 static const GstElementDetails gst_udpsink_details =
51 GST_ELEMENT_DETAILS ("UDP packet sender",
53 "Send data over the network via UDP",
54 "Wim Taymans <wim@fluendo.com>");
56 /* UDPSink signals and args */
72 static void gst_udpsink_base_init (gpointer g_class);
73 static void gst_udpsink_class_init (GstUDPSink * klass);
74 static void gst_udpsink_init (GstUDPSink * udpsink);
75 static void gst_udpsink_finalize (GstUDPSink * udpsink);
77 static void gst_udpsink_uri_handler_init (gpointer g_iface,
80 static void gst_udpsink_set_property (GObject * object, guint prop_id,
81 const GValue * value, GParamSpec * pspec);
82 static void gst_udpsink_get_property (GObject * object, guint prop_id,
83 GValue * value, GParamSpec * pspec);
85 static GstElementClass *parent_class = NULL;
87 /*static guint gst_udpsink_signals[LAST_SIGNAL] = { 0 }; */
90 gst_udpsink_get_type (void)
92 static GType udpsink_type = 0;
95 static const GTypeInfo udpsink_info = {
96 sizeof (GstUDPSinkClass),
97 gst_udpsink_base_init,
99 (GClassInitFunc) gst_udpsink_class_init,
104 (GInstanceInitFunc) gst_udpsink_init,
107 static const GInterfaceInfo urihandler_info = {
108 gst_udpsink_uri_handler_init,
114 g_type_register_static (GST_TYPE_MULTIUDPSINK, "GstUDPSink",
117 g_type_add_interface_static (udpsink_type, GST_TYPE_URI_HANDLER,
125 gst_udpsink_base_init (gpointer g_class)
127 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
129 gst_element_class_set_details (element_class, &gst_udpsink_details);
133 gst_udpsink_class_init (GstUDPSink * klass)
135 GObjectClass *gobject_class;
137 gobject_class = (GObjectClass *) klass;
139 parent_class = g_type_class_peek_parent (klass);
141 gobject_class->set_property = gst_udpsink_set_property;
142 gobject_class->get_property = gst_udpsink_get_property;
144 gobject_class->finalize = (GObjectFinalizeFunc) gst_udpsink_finalize;
146 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HOST,
147 g_param_spec_string ("host", "host",
148 "The host/IP/Multicast group to send the packets to",
149 UDP_DEFAULT_HOST, G_PARAM_READWRITE));
150 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
151 g_param_spec_int ("port", "port", "The port to send the packets to",
152 0, 65535, UDP_DEFAULT_PORT, G_PARAM_READWRITE));
157 gst_udpsink_init (GstUDPSink * udpsink)
159 udpsink->host = g_strdup (UDP_DEFAULT_HOST);
160 udpsink->port = UDP_DEFAULT_PORT;
161 gst_multiudpsink_add (GST_MULTIUDPSINK (udpsink), udpsink->host,
166 gst_udpsink_finalize (GstUDPSink * udpsink)
168 g_free (udpsink->host);
169 g_free (udpsink->uri);
171 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) udpsink);
175 gst_udpsink_update_uri (GstUDPSink * sink)
178 sink->uri = g_strdup_printf ("udp://%s:%d", sink->host, sink->port);
180 GST_DEBUG_OBJECT (sink, "updated uri to %s", sink->uri);
184 gst_udpsink_set_uri (GstUDPSink * sink, const gchar * uri)
190 protocol = gst_uri_get_protocol (uri);
191 if (strcmp (protocol, "udp") != 0)
195 location = gst_uri_get_location (uri);
198 colptr = strstr (location, ":");
200 gst_multiudpsink_remove (GST_MULTIUDPSINK (sink), sink->host, sink->port);
202 if (colptr != NULL) {
204 sink->host = g_strndup (location, colptr - location);
205 sink->port = atoi (colptr + 1);
208 sink->host = g_strdup (location);
209 sink->port = UDP_DEFAULT_PORT;
213 gst_multiudpsink_add (GST_MULTIUDPSINK (sink), sink->host, sink->port);
215 gst_udpsink_update_uri (sink);
222 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
223 ("error parsing uri %s: wrong protocol (%s != udp)", uri, protocol));
230 gst_udpsink_set_property (GObject * object, guint prop_id, const GValue * value,
235 udpsink = GST_UDPSINK (object);
237 /* remove old host */
238 gst_multiudpsink_remove (GST_MULTIUDPSINK (udpsink),
239 udpsink->host, udpsink->port);
243 g_free (udpsink->host);
244 udpsink->host = g_value_dup_string (value);
247 udpsink->port = g_value_get_int (value);
250 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254 gst_multiudpsink_add (GST_MULTIUDPSINK (udpsink),
255 udpsink->host, udpsink->port);
259 gst_udpsink_get_property (GObject * object, guint prop_id, GValue * value,
264 udpsink = GST_UDPSINK (object);
268 g_value_set_string (value, udpsink->host);
271 g_value_set_int (value, udpsink->port);
274 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
279 /*** GSTURIHANDLER INTERFACE *************************************************/
282 gst_udpsink_uri_get_type (void)
288 gst_udpsink_uri_get_protocols (void)
290 static gchar *protocols[] = { "udp", NULL };
296 gst_udpsink_uri_get_uri (GstURIHandler * handler)
298 GstUDPSink *sink = GST_UDPSINK (handler);
300 return g_strdup (sink->uri);
304 gst_udpsink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
307 GstUDPSink *sink = GST_UDPSINK (handler);
309 ret = gst_udpsink_set_uri (sink, uri);
315 gst_udpsink_uri_handler_init (gpointer g_iface, gpointer iface_data)
317 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
319 iface->get_type = gst_udpsink_uri_get_type;
320 iface->get_protocols = gst_udpsink_uri_get_protocols;
321 iface->get_uri = gst_udpsink_uri_get_uri;
322 iface->set_uri = gst_udpsink_uri_set_uri;