Add -Wwrite-strings to the configure flags
[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 /* UDPSink signals and args */
50 enum
51 {
52   /* FILL ME */
53   LAST_SIGNAL
54 };
55
56 enum
57 {
58   PROP_0,
59   PROP_HOST,
60   PROP_PORT,
61   PROP_URI,
62   /* FILL ME */
63 };
64
65 static void gst_udpsink_base_init (gpointer g_class);
66 static void gst_udpsink_class_init (GstUDPSink * klass);
67 static void gst_udpsink_init (GstUDPSink * udpsink);
68 static void gst_udpsink_finalize (GstUDPSink * udpsink);
69
70 static void gst_udpsink_uri_handler_init (gpointer g_iface,
71     gpointer iface_data);
72
73 static void gst_udpsink_set_property (GObject * object, guint prop_id,
74     const GValue * value, GParamSpec * pspec);
75 static void gst_udpsink_get_property (GObject * object, guint prop_id,
76     GValue * value, GParamSpec * pspec);
77
78 static GstElementClass *parent_class = NULL;
79
80 /*static guint gst_udpsink_signals[LAST_SIGNAL] = { 0 }; */
81
82 GType
83 gst_udpsink_get_type (void)
84 {
85   static GType udpsink_type = 0;
86
87   if (!udpsink_type) {
88     static const GTypeInfo udpsink_info = {
89       sizeof (GstUDPSinkClass),
90       gst_udpsink_base_init,
91       NULL,
92       (GClassInitFunc) gst_udpsink_class_init,
93       NULL,
94       NULL,
95       sizeof (GstUDPSink),
96       0,
97       (GInstanceInitFunc) gst_udpsink_init,
98       NULL
99     };
100     static const GInterfaceInfo urihandler_info = {
101       gst_udpsink_uri_handler_init,
102       NULL,
103       NULL
104     };
105
106     udpsink_type =
107         g_type_register_static (GST_TYPE_MULTIUDPSINK, "GstUDPSink",
108         &udpsink_info, 0);
109
110     g_type_add_interface_static (udpsink_type, GST_TYPE_URI_HANDLER,
111         &urihandler_info);
112
113   }
114   return udpsink_type;
115 }
116
117 static void
118 gst_udpsink_base_init (gpointer g_class)
119 {
120   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
121
122   gst_element_class_set_details_simple (element_class, "UDP packet sender",
123       "Sink/Network",
124       "Send data over the network via UDP", "Wim Taymans <wim@fluendo.com>");
125 }
126
127 static void
128 gst_udpsink_class_init (GstUDPSink * klass)
129 {
130   GObjectClass *gobject_class;
131
132   gobject_class = (GObjectClass *) klass;
133
134   parent_class = g_type_class_peek_parent (klass);
135
136   gobject_class->set_property = gst_udpsink_set_property;
137   gobject_class->get_property = gst_udpsink_get_property;
138
139   gobject_class->finalize = (GObjectFinalizeFunc) gst_udpsink_finalize;
140
141   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HOST,
142       g_param_spec_string ("host", "host",
143           "The host/IP/Multicast group to send the packets to",
144           UDP_DEFAULT_HOST, G_PARAM_READWRITE));
145   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
146       g_param_spec_int ("port", "port", "The port to send the packets to",
147           0, 65535, UDP_DEFAULT_PORT, G_PARAM_READWRITE));
148 }
149
150
151 static void
152 gst_udpsink_init (GstUDPSink * udpsink)
153 {
154   gst_udp_uri_init (&udpsink->uri, UDP_DEFAULT_HOST, UDP_DEFAULT_PORT);
155
156   gst_multiudpsink_add (GST_MULTIUDPSINK (udpsink), udpsink->uri.host,
157       udpsink->uri.port);
158 }
159
160 static void
161 gst_udpsink_finalize (GstUDPSink * udpsink)
162 {
163   gst_udp_uri_free (&udpsink->uri);
164   g_free (udpsink->uristr);
165
166   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) udpsink);
167 }
168
169 static gboolean
170 gst_udpsink_set_uri (GstUDPSink * sink, const gchar * uri)
171 {
172   gst_multiudpsink_remove (GST_MULTIUDPSINK (sink), sink->uri.host,
173       sink->uri.port);
174
175   if (gst_udp_parse_uri (uri, &sink->uri) < 0)
176     goto wrong_uri;
177
178   gst_multiudpsink_add (GST_MULTIUDPSINK (sink), sink->uri.host,
179       sink->uri.port);
180
181   return TRUE;
182
183   /* ERRORS */
184 wrong_uri:
185   {
186     GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
187         ("error parsing uri %s", uri));
188     return FALSE;
189   }
190 }
191
192 static void
193 gst_udpsink_set_property (GObject * object, guint prop_id, const GValue * value,
194     GParamSpec * pspec)
195 {
196   GstUDPSink *udpsink;
197
198   udpsink = GST_UDPSINK (object);
199
200   /* remove old host */
201   gst_multiudpsink_remove (GST_MULTIUDPSINK (udpsink),
202       udpsink->uri.host, udpsink->uri.port);
203
204   switch (prop_id) {
205     case PROP_HOST:
206     {
207       const gchar *host;
208
209       host = g_value_get_string (value);
210
211       if (host)
212         gst_udp_uri_update (&udpsink->uri, host, -1);
213       else
214         gst_udp_uri_update (&udpsink->uri, UDP_DEFAULT_HOST, -1);
215       break;
216     }
217     case PROP_PORT:
218       gst_udp_uri_update (&udpsink->uri, NULL, g_value_get_int (value));
219       break;
220     default:
221       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222       break;
223   }
224   /* add new host */
225   gst_multiudpsink_add (GST_MULTIUDPSINK (udpsink),
226       udpsink->uri.host, udpsink->uri.port);
227 }
228
229 static void
230 gst_udpsink_get_property (GObject * object, guint prop_id, GValue * value,
231     GParamSpec * pspec)
232 {
233   GstUDPSink *udpsink;
234
235   udpsink = GST_UDPSINK (object);
236
237   switch (prop_id) {
238     case PROP_HOST:
239       g_value_set_string (value, udpsink->uri.host);
240       break;
241     case PROP_PORT:
242       g_value_set_int (value, udpsink->uri.port);
243       break;
244     default:
245       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
246       break;
247   }
248 }
249
250 /*** GSTURIHANDLER INTERFACE *************************************************/
251
252 static GstURIType
253 gst_udpsink_uri_get_type (void)
254 {
255   return GST_URI_SINK;
256 }
257
258 static gchar **
259 gst_udpsink_uri_get_protocols (void)
260 {
261   static gchar *protocols[] = { (char *) "udp", NULL };
262
263   return protocols;
264 }
265
266 static const gchar *
267 gst_udpsink_uri_get_uri (GstURIHandler * handler)
268 {
269   GstUDPSink *sink = GST_UDPSINK (handler);
270
271   g_free (sink->uristr);
272   sink->uristr = gst_udp_uri_string (&sink->uri);
273
274   return sink->uristr;
275 }
276
277 static gboolean
278 gst_udpsink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
279 {
280   gboolean ret;
281   GstUDPSink *sink = GST_UDPSINK (handler);
282
283   ret = gst_udpsink_set_uri (sink, uri);
284
285   return ret;
286 }
287
288 static void
289 gst_udpsink_uri_handler_init (gpointer g_iface, gpointer iface_data)
290 {
291   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
292
293   iface->get_type = gst_udpsink_uri_get_type;
294   iface->get_protocols = gst_udpsink_uri_get_protocols;
295   iface->get_uri = gst_udpsink_uri_get_uri;
296   iface->set_uri = gst_udpsink_uri_set_uri;
297 }