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