Merge branch 'master' into 0.11
[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_finalize (GstUDPSink * udpsink);
66
67 static void gst_udpsink_uri_handler_init (gpointer g_iface,
68     gpointer iface_data);
69
70 static void gst_udpsink_set_property (GObject * object, guint prop_id,
71     const GValue * value, GParamSpec * pspec);
72 static void gst_udpsink_get_property (GObject * object, guint prop_id,
73     GValue * value, GParamSpec * pspec);
74
75 /*static guint gst_udpsink_signals[LAST_SIGNAL] = { 0 }; */
76 #define gst_udpsink_parent_class parent_class
77 G_DEFINE_TYPE_WITH_CODE (GstUDPSink, gst_udpsink, GST_TYPE_MULTIUDPSINK,
78     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_udpsink_uri_handler_init));
79
80 static void
81 gst_udpsink_class_init (GstUDPSinkClass * klass)
82 {
83   GObjectClass *gobject_class;
84   GstElementClass *gstelement_class;
85
86   gobject_class = (GObjectClass *) klass;
87   gstelement_class = (GstElementClass *) klass;
88
89   gobject_class->set_property = gst_udpsink_set_property;
90   gobject_class->get_property = gst_udpsink_get_property;
91
92   gobject_class->finalize = (GObjectFinalizeFunc) gst_udpsink_finalize;
93
94   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HOST,
95       g_param_spec_string ("host", "host",
96           "The host/IP/Multicast group to send the packets to",
97           UDP_DEFAULT_HOST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
98   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
99       g_param_spec_int ("port", "port", "The port to send the packets to",
100           0, 65535, UDP_DEFAULT_PORT,
101           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
102
103   gst_element_class_set_details_simple (gstelement_class, "UDP packet sender",
104       "Sink/Network",
105       "Send data over the network via UDP", "Wim Taymans <wim@fluendo.com>");
106 }
107
108 static void
109 gst_udpsink_init (GstUDPSink * udpsink)
110 {
111   gst_udp_uri_init (&udpsink->uri, UDP_DEFAULT_HOST, UDP_DEFAULT_PORT);
112
113   gst_multiudpsink_add (GST_MULTIUDPSINK (udpsink), udpsink->uri.host,
114       udpsink->uri.port);
115 }
116
117 static void
118 gst_udpsink_finalize (GstUDPSink * udpsink)
119 {
120   gst_udp_uri_free (&udpsink->uri);
121   g_free (udpsink->uristr);
122
123   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) udpsink);
124 }
125
126 static gboolean
127 gst_udpsink_set_uri (GstUDPSink * sink, const gchar * uri)
128 {
129   gst_multiudpsink_remove (GST_MULTIUDPSINK (sink), sink->uri.host,
130       sink->uri.port);
131
132   if (gst_udp_parse_uri (uri, &sink->uri) < 0)
133     goto wrong_uri;
134
135   gst_multiudpsink_add (GST_MULTIUDPSINK (sink), sink->uri.host,
136       sink->uri.port);
137
138   return TRUE;
139
140   /* ERRORS */
141 wrong_uri:
142   {
143     GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
144         ("error parsing uri %s", uri));
145     return FALSE;
146   }
147 }
148
149 static void
150 gst_udpsink_set_property (GObject * object, guint prop_id, const GValue * value,
151     GParamSpec * pspec)
152 {
153   GstUDPSink *udpsink;
154
155   udpsink = GST_UDPSINK (object);
156
157   /* remove old host */
158   gst_multiudpsink_remove (GST_MULTIUDPSINK (udpsink),
159       udpsink->uri.host, udpsink->uri.port);
160
161   switch (prop_id) {
162     case PROP_HOST:
163     {
164       const gchar *host;
165
166       host = g_value_get_string (value);
167
168       if (host)
169         gst_udp_uri_update (&udpsink->uri, host, -1);
170       else
171         gst_udp_uri_update (&udpsink->uri, UDP_DEFAULT_HOST, -1);
172       break;
173     }
174     case PROP_PORT:
175       gst_udp_uri_update (&udpsink->uri, NULL, g_value_get_int (value));
176       break;
177     default:
178       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179       break;
180   }
181   /* add new host */
182   gst_multiudpsink_add (GST_MULTIUDPSINK (udpsink),
183       udpsink->uri.host, udpsink->uri.port);
184 }
185
186 static void
187 gst_udpsink_get_property (GObject * object, guint prop_id, GValue * value,
188     GParamSpec * pspec)
189 {
190   GstUDPSink *udpsink;
191
192   udpsink = GST_UDPSINK (object);
193
194   switch (prop_id) {
195     case PROP_HOST:
196       g_value_set_string (value, udpsink->uri.host);
197       break;
198     case PROP_PORT:
199       g_value_set_int (value, udpsink->uri.port);
200       break;
201     default:
202       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
203       break;
204   }
205 }
206
207 /*** GSTURIHANDLER INTERFACE *************************************************/
208
209 static GstURIType
210 gst_udpsink_uri_get_type (GType type)
211 {
212   return GST_URI_SINK;
213 }
214
215 static gchar **
216 gst_udpsink_uri_get_protocols (GType type)
217 {
218   static gchar *protocols[] = { (char *) "udp", NULL };
219
220   return protocols;
221 }
222
223 static const gchar *
224 gst_udpsink_uri_get_uri (GstURIHandler * handler)
225 {
226   GstUDPSink *sink = GST_UDPSINK (handler);
227
228   g_free (sink->uristr);
229   sink->uristr = gst_udp_uri_string (&sink->uri);
230
231   return sink->uristr;
232 }
233
234 static gboolean
235 gst_udpsink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
236 {
237   gboolean ret;
238   GstUDPSink *sink = GST_UDPSINK (handler);
239
240   ret = gst_udpsink_set_uri (sink, uri);
241
242   return ret;
243 }
244
245 static void
246 gst_udpsink_uri_handler_init (gpointer g_iface, gpointer iface_data)
247 {
248   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
249
250   iface->get_type = gst_udpsink_uri_get_type;
251   iface->get_protocols = gst_udpsink_uri_get_protocols;
252   iface->get_uri = gst_udpsink_uri_get_uri;
253   iface->set_uri = gst_udpsink_uri_set_uri;
254 }