Remove unused variables in _class_init
[platform/upstream/gstreamer.git] / gst / udp / gstudpsink.c
1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim@fluendo.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19 /**
20  * SECTION:element-udpsink
21  * @see_also: udpsrc, multifdsink
22  *
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.
25  *
26  * <refsect2>
27  * <title>Examples</title>
28  * |[
29  * gst-launch -v audiotestsrc ! udpsink
30  * ]|
31  * </refsect2>
32  */
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 #include "gstudpsink.h"
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43 #include <errno.h>
44 #include <string.h>
45
46 #define UDP_DEFAULT_HOST        "localhost"
47 #define UDP_DEFAULT_PORT        4951
48
49 /* elementfactory information */
50 static const GstElementDetails gst_udpsink_details =
51 GST_ELEMENT_DETAILS ("UDP packet sender",
52     "Sink/Network",
53     "Send data over the network via UDP",
54     "Wim Taymans <wim@fluendo.com>");
55
56 /* UDPSink signals and args */
57 enum
58 {
59   /* FILL ME */
60   LAST_SIGNAL
61 };
62
63 enum
64 {
65   PROP_0,
66   PROP_HOST,
67   PROP_PORT,
68   PROP_URI,
69   /* FILL ME */
70 };
71
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);
76
77 static void gst_udpsink_uri_handler_init (gpointer g_iface,
78     gpointer iface_data);
79
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);
84
85 static GstElementClass *parent_class = NULL;
86
87 /*static guint gst_udpsink_signals[LAST_SIGNAL] = { 0 }; */
88
89 GType
90 gst_udpsink_get_type (void)
91 {
92   static GType udpsink_type = 0;
93
94   if (!udpsink_type) {
95     static const GTypeInfo udpsink_info = {
96       sizeof (GstUDPSinkClass),
97       gst_udpsink_base_init,
98       NULL,
99       (GClassInitFunc) gst_udpsink_class_init,
100       NULL,
101       NULL,
102       sizeof (GstUDPSink),
103       0,
104       (GInstanceInitFunc) gst_udpsink_init,
105       NULL
106     };
107     static const GInterfaceInfo urihandler_info = {
108       gst_udpsink_uri_handler_init,
109       NULL,
110       NULL
111     };
112
113     udpsink_type =
114         g_type_register_static (GST_TYPE_MULTIUDPSINK, "GstUDPSink",
115         &udpsink_info, 0);
116
117     g_type_add_interface_static (udpsink_type, GST_TYPE_URI_HANDLER,
118         &urihandler_info);
119
120   }
121   return udpsink_type;
122 }
123
124 static void
125 gst_udpsink_base_init (gpointer g_class)
126 {
127   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
128
129   gst_element_class_set_details (element_class, &gst_udpsink_details);
130 }
131
132 static void
133 gst_udpsink_class_init (GstUDPSink * klass)
134 {
135   GObjectClass *gobject_class;
136
137   gobject_class = (GObjectClass *) klass;
138
139   parent_class = g_type_class_peek_parent (klass);
140
141   gobject_class->set_property = gst_udpsink_set_property;
142   gobject_class->get_property = gst_udpsink_get_property;
143
144   gobject_class->finalize = (GObjectFinalizeFunc) gst_udpsink_finalize;
145
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));
153 }
154
155
156 static void
157 gst_udpsink_init (GstUDPSink * udpsink)
158 {
159   udpsink->host = g_strdup (UDP_DEFAULT_HOST);
160   udpsink->port = UDP_DEFAULT_PORT;
161   gst_multiudpsink_add (GST_MULTIUDPSINK (udpsink), udpsink->host,
162       udpsink->port);
163 }
164
165 static void
166 gst_udpsink_finalize (GstUDPSink * udpsink)
167 {
168   g_free (udpsink->host);
169   g_free (udpsink->uri);
170
171   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) udpsink);
172 }
173
174 static void
175 gst_udpsink_update_uri (GstUDPSink * sink)
176 {
177   g_free (sink->uri);
178   sink->uri = g_strdup_printf ("udp://%s:%d", sink->host, sink->port);
179
180   GST_DEBUG_OBJECT (sink, "updated uri to %s", sink->uri);
181 }
182
183 static gboolean
184 gst_udpsink_set_uri (GstUDPSink * sink, const gchar * uri)
185 {
186   gchar *protocol;
187   gchar *location;
188   gchar *colptr;
189
190   protocol = gst_uri_get_protocol (uri);
191   if (strcmp (protocol, "udp") != 0)
192     goto wrong_protocol;
193   g_free (protocol);
194
195   location = gst_uri_get_location (uri);
196   if (!location)
197     return FALSE;
198   colptr = strstr (location, ":");
199
200   gst_multiudpsink_remove (GST_MULTIUDPSINK (sink), sink->host, sink->port);
201
202   if (colptr != NULL) {
203     g_free (sink->host);
204     sink->host = g_strndup (location, colptr - location);
205     sink->port = atoi (colptr + 1);
206   } else {
207     g_free (sink->host);
208     sink->host = g_strdup (location);
209     sink->port = UDP_DEFAULT_PORT;
210   }
211   g_free (location);
212
213   gst_multiudpsink_add (GST_MULTIUDPSINK (sink), sink->host, sink->port);
214
215   gst_udpsink_update_uri (sink);
216
217   return TRUE;
218
219   /* ERRORS */
220 wrong_protocol:
221   {
222     GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
223         ("error parsing uri %s: wrong protocol (%s != udp)", uri, protocol));
224     g_free (protocol);
225     return FALSE;
226   }
227 }
228
229 static void
230 gst_udpsink_set_property (GObject * object, guint prop_id, const GValue * value,
231     GParamSpec * pspec)
232 {
233   GstUDPSink *udpsink;
234
235   udpsink = GST_UDPSINK (object);
236
237   /* remove old host */
238   gst_multiudpsink_remove (GST_MULTIUDPSINK (udpsink),
239       udpsink->host, udpsink->port);
240
241   switch (prop_id) {
242     case PROP_HOST:
243       g_free (udpsink->host);
244       udpsink->host = g_value_dup_string (value);
245       break;
246     case PROP_PORT:
247       udpsink->port = g_value_get_int (value);
248       break;
249     default:
250       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
251       break;
252   }
253   /* add new host */
254   gst_multiudpsink_add (GST_MULTIUDPSINK (udpsink),
255       udpsink->host, udpsink->port);
256 }
257
258 static void
259 gst_udpsink_get_property (GObject * object, guint prop_id, GValue * value,
260     GParamSpec * pspec)
261 {
262   GstUDPSink *udpsink;
263
264   udpsink = GST_UDPSINK (object);
265
266   switch (prop_id) {
267     case PROP_HOST:
268       g_value_set_string (value, udpsink->host);
269       break;
270     case PROP_PORT:
271       g_value_set_int (value, udpsink->port);
272       break;
273     default:
274       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
275       break;
276   }
277 }
278
279 /*** GSTURIHANDLER INTERFACE *************************************************/
280
281 static GstURIType
282 gst_udpsink_uri_get_type (void)
283 {
284   return GST_URI_SINK;
285 }
286
287 static gchar **
288 gst_udpsink_uri_get_protocols (void)
289 {
290   static gchar *protocols[] = { "udp", NULL };
291
292   return protocols;
293 }
294
295 static const gchar *
296 gst_udpsink_uri_get_uri (GstURIHandler * handler)
297 {
298   GstUDPSink *sink = GST_UDPSINK (handler);
299
300   return g_strdup (sink->uri);
301 }
302
303 static gboolean
304 gst_udpsink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
305 {
306   gboolean ret;
307   GstUDPSink *sink = GST_UDPSINK (handler);
308
309   ret = gst_udpsink_set_uri (sink, uri);
310
311   return ret;
312 }
313
314 static void
315 gst_udpsink_uri_handler_init (gpointer g_iface, gpointer iface_data)
316 {
317   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
318
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;
323 }