udpsrc: Port to GIO
[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, GError ** error)
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.host, &sink->uri.port) < 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     g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
146         "Could not parse UDP URI");
147     return FALSE;
148   }
149 }
150
151 static void
152 gst_udpsink_set_property (GObject * object, guint prop_id, const GValue * value,
153     GParamSpec * pspec)
154 {
155   GstUDPSink *udpsink;
156
157   udpsink = GST_UDPSINK (object);
158
159   /* remove old host */
160   gst_multiudpsink_remove (GST_MULTIUDPSINK (udpsink),
161       udpsink->uri.host, udpsink->uri.port);
162
163   switch (prop_id) {
164     case PROP_HOST:
165     {
166       const gchar *host;
167
168       host = g_value_get_string (value);
169
170       if (host)
171         gst_udp_uri_update (&udpsink->uri, host, -1);
172       else
173         gst_udp_uri_update (&udpsink->uri, UDP_DEFAULT_HOST, -1);
174       break;
175     }
176     case PROP_PORT:
177       gst_udp_uri_update (&udpsink->uri, NULL, g_value_get_int (value));
178       break;
179     default:
180       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181       break;
182   }
183   /* add new host */
184   gst_multiudpsink_add (GST_MULTIUDPSINK (udpsink),
185       udpsink->uri.host, udpsink->uri.port);
186 }
187
188 static void
189 gst_udpsink_get_property (GObject * object, guint prop_id, GValue * value,
190     GParamSpec * pspec)
191 {
192   GstUDPSink *udpsink;
193
194   udpsink = GST_UDPSINK (object);
195
196   switch (prop_id) {
197     case PROP_HOST:
198       g_value_set_string (value, udpsink->uri.host);
199       break;
200     case PROP_PORT:
201       g_value_set_int (value, udpsink->uri.port);
202       break;
203     default:
204       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205       break;
206   }
207 }
208
209 /*** GSTURIHANDLER INTERFACE *************************************************/
210
211 static GstURIType
212 gst_udpsink_uri_get_type (GType type)
213 {
214   return GST_URI_SINK;
215 }
216
217 static const gchar *const *
218 gst_udpsink_uri_get_protocols (GType type)
219 {
220   static const gchar *protocols[] = { "udp", NULL };
221
222   return protocols;
223 }
224
225 static gchar *
226 gst_udpsink_uri_get_uri (GstURIHandler * handler)
227 {
228   GstUDPSink *sink = GST_UDPSINK (handler);
229
230   g_free (sink->uristr);
231   sink->uristr = gst_udp_uri_string (&sink->uri);
232
233   return g_strdup (sink->uristr);
234 }
235
236 static gboolean
237 gst_udpsink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
238     GError ** error)
239 {
240   return gst_udpsink_set_uri (GST_UDPSINK (handler), uri, error);
241 }
242
243 static void
244 gst_udpsink_uri_handler_init (gpointer g_iface, gpointer iface_data)
245 {
246   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
247
248   iface->get_type = gst_udpsink_uri_get_type;
249   iface->get_protocols = gst_udpsink_uri_get_protocols;
250   iface->get_uri = gst_udpsink_uri_get_uri;
251   iface->set_uri = gst_udpsink_uri_set_uri;
252 }