• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
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>
5  *
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.
10  *
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.
15  *
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.
20  */
21 /**
22  * SECTION:element-udpsink
23  * @title: udpsink
24  * @see_also: udpsrc, multifdsink
25  *
26  * udpsink is a network sink that sends UDP packets to the network.
27  * It can be combined with RTP payloaders to implement RTP streaming.
28  *
29  * ## Examples
30  * |[
31  * gst-launch-1.0 -v audiotestsrc ! udpsink
32  * ]|
33  *
34  */
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38 #include "gstudpelements.h"
39 #include "gstudpsink.h"
40 
41 #define UDP_DEFAULT_HOST        "localhost"
42 #define UDP_DEFAULT_PORT        5004
43 
44 /* UDPSink signals and args */
45 enum
46 {
47   /* FILL ME */
48   LAST_SIGNAL
49 };
50 
51 enum
52 {
53   PROP_0,
54   PROP_HOST,
55   PROP_PORT,
56   PROP_URI,
57   /* FILL ME */
58 };
59 
60 static void gst_udpsink_finalize (GstUDPSink * udpsink);
61 
62 static void gst_udpsink_uri_handler_init (gpointer g_iface,
63     gpointer iface_data);
64 
65 static void gst_udpsink_set_property (GObject * object, guint prop_id,
66     const GValue * value, GParamSpec * pspec);
67 static void gst_udpsink_get_property (GObject * object, guint prop_id,
68     GValue * value, GParamSpec * pspec);
69 
70 /*static guint gst_udpsink_signals[LAST_SIGNAL] = { 0 }; */
71 #define gst_udpsink_parent_class parent_class
72 G_DEFINE_TYPE_WITH_CODE (GstUDPSink, gst_udpsink, GST_TYPE_MULTIUDPSINK,
73     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_udpsink_uri_handler_init));
74 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (udpsink, "udpsink", GST_RANK_NONE,
75     GST_TYPE_UDPSINK, udp_element_init (plugin));
76 
77 static void
gst_udpsink_class_init(GstUDPSinkClass * klass)78 gst_udpsink_class_init (GstUDPSinkClass * klass)
79 {
80   GObjectClass *gobject_class;
81   GstElementClass *gstelement_class;
82 
83   gobject_class = (GObjectClass *) klass;
84   gstelement_class = (GstElementClass *) klass;
85 
86   gobject_class->set_property = gst_udpsink_set_property;
87   gobject_class->get_property = gst_udpsink_get_property;
88 
89   gobject_class->finalize = (GObjectFinalizeFunc) gst_udpsink_finalize;
90 
91   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HOST,
92       g_param_spec_string ("host", "host",
93           "The host/IP/Multicast group to send the packets to",
94           UDP_DEFAULT_HOST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
95   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
96       g_param_spec_int ("port", "port", "The port to send the packets to",
97           0, 65535, UDP_DEFAULT_PORT,
98           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
99 
100   gst_element_class_set_static_metadata (gstelement_class, "UDP packet sender",
101       "Sink/Network",
102       "Send data over the network via UDP", "Wim Taymans <wim@fluendo.com>");
103 }
104 
105 static void
gst_udpsink_init(GstUDPSink * udpsink)106 gst_udpsink_init (GstUDPSink * udpsink)
107 {
108   udpsink->host = g_strdup (UDP_DEFAULT_HOST);
109   udpsink->port = UDP_DEFAULT_PORT;
110   udpsink->uri = g_strdup_printf ("udp://%s:%d", udpsink->host, udpsink->port);
111 
112   gst_multiudpsink_add (GST_MULTIUDPSINK (udpsink), udpsink->host,
113       udpsink->port);
114 }
115 
116 static void
gst_udpsink_finalize(GstUDPSink * udpsink)117 gst_udpsink_finalize (GstUDPSink * udpsink)
118 {
119   g_free (udpsink->host);
120   udpsink->host = NULL;
121 
122   g_free (udpsink->uri);
123   udpsink->uri = NULL;
124 
125   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) udpsink);
126 }
127 
128 static gboolean
gst_udpsink_set_uri(GstUDPSink * sink,const gchar * uri,GError ** error)129 gst_udpsink_set_uri (GstUDPSink * sink, const gchar * uri, GError ** error)
130 {
131   gchar *host;
132   guint16 port;
133 
134   gst_multiudpsink_remove (GST_MULTIUDPSINK (sink), sink->host, sink->port);
135 
136   if (!gst_udp_parse_uri (uri, &host, &port))
137     goto wrong_uri;
138 
139   g_free (sink->host);
140   sink->host = host;
141   sink->port = port;
142 
143   g_free (sink->uri);
144   sink->uri = g_strdup (uri);
145 
146   gst_multiudpsink_add (GST_MULTIUDPSINK (sink), sink->host, sink->port);
147 
148   return TRUE;
149 
150   /* ERRORS */
151 wrong_uri:
152   {
153     GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
154         ("error parsing uri %s", uri));
155     g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
156         "Could not parse UDP URI");
157     return FALSE;
158   }
159 }
160 
161 static void
gst_udpsink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)162 gst_udpsink_set_property (GObject * object, guint prop_id, const GValue * value,
163     GParamSpec * pspec)
164 {
165   GstUDPSink *udpsink;
166 
167   udpsink = GST_UDPSINK (object);
168 
169   /* remove old host */
170   gst_multiudpsink_remove (GST_MULTIUDPSINK (udpsink),
171       udpsink->host, udpsink->port);
172 
173   switch (prop_id) {
174     case PROP_HOST:
175     {
176       const gchar *host;
177 
178       host = g_value_get_string (value);
179       g_free (udpsink->host);
180       udpsink->host = g_strdup (host);
181       g_free (udpsink->uri);
182       udpsink->uri =
183           g_strdup_printf ("udp://%s:%d", udpsink->host, udpsink->port);
184       break;
185     }
186     case PROP_PORT:
187       udpsink->port = g_value_get_int (value);
188       g_free (udpsink->uri);
189       udpsink->uri =
190           g_strdup_printf ("udp://%s:%d", udpsink->host, udpsink->port);
191       break;
192     default:
193       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194       break;
195   }
196   /* add new host */
197   gst_multiudpsink_add (GST_MULTIUDPSINK (udpsink),
198       udpsink->host, udpsink->port);
199 }
200 
201 static void
gst_udpsink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)202 gst_udpsink_get_property (GObject * object, guint prop_id, GValue * value,
203     GParamSpec * pspec)
204 {
205   GstUDPSink *udpsink;
206 
207   udpsink = GST_UDPSINK (object);
208 
209   switch (prop_id) {
210     case PROP_HOST:
211       g_value_set_string (value, udpsink->host);
212       break;
213     case PROP_PORT:
214       g_value_set_int (value, udpsink->port);
215       break;
216     default:
217       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218       break;
219   }
220 }
221 
222 /*** GSTURIHANDLER INTERFACE *************************************************/
223 
224 static GstURIType
gst_udpsink_uri_get_type(GType type)225 gst_udpsink_uri_get_type (GType type)
226 {
227   return GST_URI_SINK;
228 }
229 
230 static const gchar *const *
gst_udpsink_uri_get_protocols(GType type)231 gst_udpsink_uri_get_protocols (GType type)
232 {
233   static const gchar *protocols[] = { "udp", NULL };
234 
235   return protocols;
236 }
237 
238 static gchar *
gst_udpsink_uri_get_uri(GstURIHandler * handler)239 gst_udpsink_uri_get_uri (GstURIHandler * handler)
240 {
241   GstUDPSink *sink = GST_UDPSINK (handler);
242 
243   return g_strdup (sink->uri);
244 }
245 
246 static gboolean
gst_udpsink_uri_set_uri(GstURIHandler * handler,const gchar * uri,GError ** error)247 gst_udpsink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
248     GError ** error)
249 {
250   return gst_udpsink_set_uri (GST_UDPSINK (handler), uri, error);
251 }
252 
253 static void
gst_udpsink_uri_handler_init(gpointer g_iface,gpointer iface_data)254 gst_udpsink_uri_handler_init (gpointer g_iface, gpointer iface_data)
255 {
256   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
257 
258   iface->get_type = gst_udpsink_uri_get_type;
259   iface->get_protocols = gst_udpsink_uri_get_protocols;
260   iface->get_uri = gst_udpsink_uri_get_uri;
261   iface->set_uri = gst_udpsink_uri_set_uri;
262 }
263