2 * Copyright (C) <2005> Philippe Khalaf <burger@speedy.org>
3 * Copyright (C) <2005> Nokia Corporation <kai.vehmanen@nokia.com>
4 * Copyright (C) <2006> Joni Valtanen <joni.valtanen@movial.fi>
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.
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.
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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
25 #include "gstudp-marshal.h"
26 #include "gstdynudpsink.h"
35 #ifdef HAVE_SYS_TIME_H
38 #include <sys/types.h>
39 #include <gst/netbuffer/gstnetbuffer.h>
41 GST_DEBUG_CATEGORY_STATIC (dynudpsink_debug);
42 #define GST_CAT_DEFAULT (dynudpsink_debug)
44 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
49 /* DynUDPSink signals and args */
61 #define UDP_DEFAULT_SOCKFD -1
62 #define UDP_DEFAULT_CLOSEFD TRUE
71 #define CLOSE_IF_REQUESTED(udpctx) \
73 if ((!udpctx->externalfd) || (udpctx->externalfd && udpctx->closefd)) { \
74 CLOSE_SOCKET(udpctx->sock); \
75 if (udpctx->sock == udpctx->sockfd) \
76 udpctx->sockfd = UDP_DEFAULT_SOCKFD; \
81 static void gst_dynudpsink_base_init (gpointer g_class);
82 static void gst_dynudpsink_class_init (GstDynUDPSink * klass);
83 static void gst_dynudpsink_init (GstDynUDPSink * udpsink);
84 static void gst_dynudpsink_finalize (GObject * object);
86 static GstFlowReturn gst_dynudpsink_render (GstBaseSink * sink,
88 static void gst_dynudpsink_close (GstDynUDPSink * sink);
89 static GstStateChangeReturn gst_dynudpsink_change_state (GstElement * element,
90 GstStateChange transition);
92 static void gst_dynudpsink_set_property (GObject * object, guint prop_id,
93 const GValue * value, GParamSpec * pspec);
94 static void gst_dynudpsink_get_property (GObject * object, guint prop_id,
95 GValue * value, GParamSpec * pspec);
97 static GstElementClass *parent_class = NULL;
99 static guint gst_dynudpsink_signals[LAST_SIGNAL] = { 0 };
102 gst_dynudpsink_get_type (void)
104 static GType dynudpsink_type = 0;
106 if (!dynudpsink_type) {
107 static const GTypeInfo dynudpsink_info = {
108 sizeof (GstDynUDPSinkClass),
109 gst_dynudpsink_base_init,
111 (GClassInitFunc) gst_dynudpsink_class_init,
114 sizeof (GstDynUDPSink),
116 (GInstanceInitFunc) gst_dynudpsink_init,
121 g_type_register_static (GST_TYPE_BASE_SINK, "GstDynUDPSink",
122 &dynudpsink_info, 0);
124 return dynudpsink_type;
128 gst_dynudpsink_base_init (gpointer g_class)
130 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
132 gst_element_class_add_pad_template (element_class,
133 gst_static_pad_template_get (&sink_template));
135 gst_element_class_set_details_simple (element_class, "UDP packet sender",
137 "Send data over the network via UDP",
138 "Philippe Khalaf <burger@speedy.org>");
142 gst_dynudpsink_class_init (GstDynUDPSink * klass)
144 GObjectClass *gobject_class;
145 GstElementClass *gstelement_class;
146 GstBaseSinkClass *gstbasesink_class;
148 gobject_class = (GObjectClass *) klass;
149 gstelement_class = (GstElementClass *) klass;
150 gstbasesink_class = (GstBaseSinkClass *) klass;
152 parent_class = g_type_class_peek_parent (klass);
154 gobject_class->set_property = gst_dynudpsink_set_property;
155 gobject_class->get_property = gst_dynudpsink_get_property;
156 gobject_class->finalize = gst_dynudpsink_finalize;
158 gst_dynudpsink_signals[SIGNAL_GET_STATS] =
159 g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
160 G_STRUCT_OFFSET (GstDynUDPSinkClass, get_stats),
161 NULL, NULL, gst_udp_marshal_BOXED__STRING_INT, G_TYPE_VALUE_ARRAY, 2,
162 G_TYPE_STRING, G_TYPE_INT);
164 g_object_class_install_property (gobject_class, PROP_SOCKFD,
165 g_param_spec_int ("sockfd", "socket handle",
166 "Socket to use for UDP sending. (-1 == allocate)",
167 -1, G_MAXINT16, UDP_DEFAULT_SOCKFD,
168 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169 g_object_class_install_property (gobject_class, PROP_CLOSEFD,
170 g_param_spec_boolean ("closefd", "Close sockfd",
171 "Close sockfd if passed as property on state change",
172 UDP_DEFAULT_CLOSEFD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
174 gstelement_class->change_state = gst_dynudpsink_change_state;
176 gstbasesink_class->render = gst_dynudpsink_render;
178 GST_DEBUG_CATEGORY_INIT (dynudpsink_debug, "dynudpsink", 0, "UDP sink");
182 gst_dynudpsink_init (GstDynUDPSink * sink)
186 sink->sockfd = UDP_DEFAULT_SOCKFD;
187 sink->closefd = UDP_DEFAULT_CLOSEFD;
188 sink->externalfd = FALSE;
194 gst_dynudpsink_finalize (GObject * object)
196 GstDynUDPSink *udpsink;
198 udpsink = GST_DYNUDPSINK (object);
200 if (udpsink->sockfd >= 0 && udpsink->closefd)
201 CLOSE_SOCKET (udpsink->sockfd);
203 G_OBJECT_CLASS (parent_class)->finalize (object);
205 WSA_CLEANUP (object);
209 gst_dynudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
214 GstMetaNetAddress *meta;
215 struct sockaddr_in theiraddr;
219 memset (&theiraddr, 0, sizeof (theiraddr));
221 meta = gst_buffer_get_meta_net_address (buffer);
224 GST_DEBUG ("Received buffer is not a GstNetBuffer, skipping");
228 sink = GST_DYNUDPSINK (bsink);
230 size = GST_BUFFER_SIZE (buffer);
231 data = GST_BUFFER_DATA (buffer);
233 GST_DEBUG ("about to send %d bytes", size);
235 /* let's get the address from the metaata */
236 gst_netaddress_get_ip4_address (&meta->naddr, &destaddr, &destport);
238 GST_DEBUG ("sending %d bytes to client %d port %d", size, destaddr, destport);
240 theiraddr.sin_family = AF_INET;
241 theiraddr.sin_addr.s_addr = destaddr;
242 theiraddr.sin_port = destport;
244 ret = sendto (sink->sock, (char *) data, size, 0,
246 ret = sendto (sink->sock, data, size, 0,
248 (struct sockaddr *) &theiraddr, sizeof (theiraddr));
251 if (errno != EINTR && errno != EAGAIN) {
256 GST_DEBUG ("sent %d bytes", size);
262 GST_DEBUG ("got send error %s (%d)", g_strerror (errno), errno);
263 return GST_FLOW_ERROR;
268 gst_dynudpsink_set_property (GObject * object, guint prop_id,
269 const GValue * value, GParamSpec * pspec)
271 GstDynUDPSink *udpsink;
273 udpsink = GST_DYNUDPSINK (object);
277 if (udpsink->sockfd >= 0 && udpsink->sockfd != udpsink->sock &&
279 CLOSE_SOCKET (udpsink->sockfd);
280 udpsink->sockfd = g_value_get_int (value);
281 GST_DEBUG ("setting SOCKFD to %d", udpsink->sockfd);
284 udpsink->closefd = g_value_get_boolean (value);
288 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
294 gst_dynudpsink_get_property (GObject * object, guint prop_id, GValue * value,
297 GstDynUDPSink *udpsink;
299 udpsink = GST_DYNUDPSINK (object);
303 g_value_set_int (value, udpsink->sockfd);
306 g_value_set_boolean (value, udpsink->closefd);
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
315 /* create a socket for sending to remote machine */
317 gst_dynudpsink_init_send (GstDynUDPSink * sink)
321 if (sink->sockfd == -1) {
322 /* create sender socket if none available */
323 if ((sink->sock = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
327 if (setsockopt (sink->sock, SOL_SOCKET, SO_BROADCAST, &bc_val,
328 sizeof (bc_val)) < 0)
331 sink->externalfd = TRUE;
333 sink->sock = sink->sockfd;
334 sink->externalfd = TRUE;
346 perror ("setsockopt");
347 CLOSE_IF_REQUESTED (sink);
353 gst_dynudpsink_get_stats (GstDynUDPSink * sink, const gchar * host, gint port)
359 gst_dynudpsink_close (GstDynUDPSink * sink)
361 CLOSE_IF_REQUESTED (sink);
364 static GstStateChangeReturn
365 gst_dynudpsink_change_state (GstElement * element, GstStateChange transition)
367 GstStateChangeReturn ret;
370 sink = GST_DYNUDPSINK (element);
372 switch (transition) {
373 case GST_STATE_CHANGE_READY_TO_PAUSED:
374 if (!gst_dynudpsink_init_send (sink))
381 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
383 switch (transition) {
384 case GST_STATE_CHANGE_PAUSED_TO_READY:
385 gst_dynudpsink_close (sink);
395 return GST_STATE_CHANGE_FAILURE;