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>
5 * Copyright (C) <2012> Collabora Ltd.
6 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
27 #include "gstdynudpsink.h"
29 #include <gst/net/gstnetaddressmeta.h>
31 GST_DEBUG_CATEGORY_STATIC (dynudpsink_debug);
32 #define GST_CAT_DEFAULT (dynudpsink_debug)
34 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
39 /* DynUDPSink signals and args */
51 #define UDP_DEFAULT_SOCKET NULL
52 #define UDP_DEFAULT_CLOSE_SOCKET TRUE
53 #define UDP_DEFAULT_BIND_ADDRESS NULL
54 #define UDP_DEFAULT_BIND_PORT 0
66 static void gst_dynudpsink_finalize (GObject * object);
68 static GstFlowReturn gst_dynudpsink_render (GstBaseSink * sink,
70 static gboolean gst_dynudpsink_stop (GstBaseSink * bsink);
71 static gboolean gst_dynudpsink_start (GstBaseSink * bsink);
72 static gboolean gst_dynudpsink_unlock (GstBaseSink * bsink);
73 static gboolean gst_dynudpsink_unlock_stop (GstBaseSink * bsink);
75 static void gst_dynudpsink_set_property (GObject * object, guint prop_id,
76 const GValue * value, GParamSpec * pspec);
77 static void gst_dynudpsink_get_property (GObject * object, guint prop_id,
78 GValue * value, GParamSpec * pspec);
79 static GstStructure *gst_dynudpsink_get_stats (GstDynUDPSink * sink,
80 const gchar * host, gint port);
82 static guint gst_dynudpsink_signals[LAST_SIGNAL] = { 0 };
84 #define gst_dynudpsink_parent_class parent_class
85 G_DEFINE_TYPE (GstDynUDPSink, gst_dynudpsink, GST_TYPE_BASE_SINK);
88 gst_dynudpsink_class_init (GstDynUDPSinkClass * klass)
90 GObjectClass *gobject_class;
91 GstElementClass *gstelement_class;
92 GstBaseSinkClass *gstbasesink_class;
94 gobject_class = (GObjectClass *) klass;
95 gstelement_class = (GstElementClass *) klass;
96 gstbasesink_class = (GstBaseSinkClass *) klass;
98 parent_class = g_type_class_peek_parent (klass);
100 gobject_class->set_property = gst_dynudpsink_set_property;
101 gobject_class->get_property = gst_dynudpsink_get_property;
102 gobject_class->finalize = gst_dynudpsink_finalize;
104 gst_dynudpsink_signals[SIGNAL_GET_STATS] =
105 g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass),
106 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
107 G_STRUCT_OFFSET (GstDynUDPSinkClass, get_stats),
108 NULL, NULL, g_cclosure_marshal_generic, GST_TYPE_STRUCTURE, 2,
109 G_TYPE_STRING, G_TYPE_INT);
111 g_object_class_install_property (gobject_class, PROP_SOCKET,
112 g_param_spec_object ("socket", "Socket",
113 "Socket to use for UDP sending. (NULL == allocate)",
114 G_TYPE_SOCKET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
115 g_object_class_install_property (gobject_class, PROP_SOCKET_V6,
116 g_param_spec_object ("socket-v6", "Socket IPv6",
117 "Socket to use for UDPv6 sending. (NULL == allocate)",
118 G_TYPE_SOCKET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
119 g_object_class_install_property (gobject_class, PROP_CLOSE_SOCKET,
120 g_param_spec_boolean ("close-socket", "Close socket",
121 "Close socket if passed as property on state change",
122 UDP_DEFAULT_CLOSE_SOCKET,
123 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
124 g_object_class_install_property (gobject_class, PROP_BIND_ADDRESS,
125 g_param_spec_string ("bind-address", "Bind Address",
126 "Address to bind the socket to", UDP_DEFAULT_BIND_ADDRESS,
127 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
128 g_object_class_install_property (gobject_class, PROP_BIND_PORT,
129 g_param_spec_int ("bind-port", "Bind Port",
130 "Port to bind the socket to", 0, G_MAXUINT16,
131 UDP_DEFAULT_BIND_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
133 gst_element_class_add_pad_template (gstelement_class,
134 gst_static_pad_template_get (&sink_template));
136 gst_element_class_set_static_metadata (gstelement_class, "UDP packet sender",
138 "Send data over the network via UDP with packet destinations picked up "
139 "dynamically from meta on the buffers passed",
140 "Philippe Khalaf <burger@speedy.org>");
142 gstbasesink_class->render = gst_dynudpsink_render;
143 gstbasesink_class->start = gst_dynudpsink_start;
144 gstbasesink_class->stop = gst_dynudpsink_stop;
145 gstbasesink_class->unlock = gst_dynudpsink_unlock;
146 gstbasesink_class->unlock_stop = gst_dynudpsink_unlock_stop;
148 klass->get_stats = gst_dynudpsink_get_stats;
150 GST_DEBUG_CATEGORY_INIT (dynudpsink_debug, "dynudpsink", 0, "UDP sink");
154 gst_dynudpsink_init (GstDynUDPSink * sink)
156 sink->socket = UDP_DEFAULT_SOCKET;
157 sink->socket_v6 = UDP_DEFAULT_SOCKET;
158 sink->close_socket = UDP_DEFAULT_CLOSE_SOCKET;
159 sink->external_socket = FALSE;
160 sink->bind_address = UDP_DEFAULT_BIND_ADDRESS;
161 sink->bind_port = UDP_DEFAULT_BIND_PORT;
163 sink->used_socket = NULL;
164 sink->used_socket_v6 = NULL;
168 gst_dynudpsink_finalize (GObject * object)
172 sink = GST_DYNUDPSINK (object);
175 g_object_unref (sink->socket);
179 g_object_unref (sink->socket_v6);
180 sink->socket_v6 = NULL;
182 if (sink->used_socket)
183 g_object_unref (sink->used_socket);
184 sink->used_socket = NULL;
186 if (sink->used_socket_v6)
187 g_object_unref (sink->used_socket_v6);
188 sink->used_socket_v6 = NULL;
190 g_free (sink->bind_address);
191 sink->bind_address = NULL;
193 G_OBJECT_CLASS (parent_class)->finalize (object);
197 gst_dynudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
202 GstNetAddressMeta *meta;
203 GSocketAddress *addr;
205 GSocketFamily family;
208 meta = gst_buffer_get_net_address_meta (buffer);
211 GST_DEBUG ("Received buffer without GstNetAddressMeta, skipping");
215 sink = GST_DYNUDPSINK (bsink);
217 /* let's get the address from the metadata */
220 family = g_socket_address_get_family (addr);
221 if (family == G_SOCKET_FAMILY_IPV6 && !sink->used_socket_v6)
224 gst_buffer_map (buffer, &map, GST_MAP_READ);
226 GST_DEBUG ("about to send %" G_GSIZE_FORMAT " bytes", map.size);
228 #ifndef GST_DISABLE_GST_DEBUG
233 g_inet_address_to_string (g_inet_socket_address_get_address
234 (G_INET_SOCKET_ADDRESS (addr)));
235 GST_DEBUG ("sending %" G_GSIZE_FORMAT " bytes to client %s port %d",
237 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
242 /* Select socket to send from for this address */
243 if (family == G_SOCKET_FAMILY_IPV6 || !sink->used_socket)
244 socket = sink->used_socket_v6;
246 socket = sink->used_socket;
249 g_socket_send_to (socket, addr, (gchar *) map.data, map.size,
250 sink->cancellable, &err);
251 gst_buffer_unmap (buffer, &map);
256 GST_DEBUG ("sent %" G_GSSIZE_FORMAT " bytes", ret);
262 GstFlowReturn flow_ret;
264 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
265 GST_DEBUG_OBJECT (sink, "send cancelled");
266 flow_ret = GST_FLOW_FLUSHING;
268 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
269 ("send error: %s", err->message));
270 flow_ret = GST_FLOW_ERROR;
272 g_clear_error (&err);
277 GST_DEBUG ("invalid address family (got %d)", family);
278 return GST_FLOW_ERROR;
283 gst_dynudpsink_set_property (GObject * object, guint prop_id,
284 const GValue * value, GParamSpec * pspec)
286 GstDynUDPSink *udpsink;
288 udpsink = GST_DYNUDPSINK (object);
292 if (udpsink->socket != NULL && udpsink->socket != udpsink->used_socket &&
293 udpsink->close_socket) {
296 if (!g_socket_close (udpsink->socket, &err)) {
297 GST_ERROR ("failed to close socket %p: %s", udpsink->socket,
299 g_clear_error (&err);
303 g_object_unref (udpsink->socket);
304 udpsink->socket = g_value_dup_object (value);
305 GST_DEBUG ("setting socket to %p", udpsink->socket);
308 if (udpsink->socket_v6 != NULL
309 && udpsink->socket_v6 != udpsink->used_socket_v6
310 && udpsink->close_socket) {
313 if (!g_socket_close (udpsink->socket_v6, &err)) {
314 GST_ERROR ("failed to close socket %p: %s", udpsink->socket_v6,
316 g_clear_error (&err);
319 if (udpsink->socket_v6)
320 g_object_unref (udpsink->socket_v6);
321 udpsink->socket_v6 = g_value_dup_object (value);
322 GST_DEBUG ("setting socket v6 to %p", udpsink->socket_v6);
324 case PROP_CLOSE_SOCKET:
325 udpsink->close_socket = g_value_get_boolean (value);
327 case PROP_BIND_ADDRESS:
328 g_free (udpsink->bind_address);
329 udpsink->bind_address = g_value_dup_string (value);
332 udpsink->bind_port = g_value_get_int (value);
335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
341 gst_dynudpsink_get_property (GObject * object, guint prop_id, GValue * value,
344 GstDynUDPSink *udpsink;
346 udpsink = GST_DYNUDPSINK (object);
350 g_value_set_object (value, udpsink->socket);
353 g_value_set_object (value, udpsink->socket_v6);
355 case PROP_CLOSE_SOCKET:
356 g_value_set_boolean (value, udpsink->close_socket);
358 case PROP_BIND_ADDRESS:
359 g_value_set_string (value, udpsink->bind_address);
362 g_value_set_int (value, udpsink->bind_port);
365 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
371 gst_dynudpsink_create_cancellable (GstDynUDPSink * sink)
375 sink->cancellable = g_cancellable_new ();
376 sink->made_cancel_fd = g_cancellable_make_pollfd (sink->cancellable, &pollfd);
380 gst_dynudpsink_free_cancellable (GstDynUDPSink * sink)
382 if (sink->made_cancel_fd) {
383 g_cancellable_release_fd (sink->cancellable);
384 sink->made_cancel_fd = FALSE;
386 g_object_unref (sink->cancellable);
387 sink->cancellable = NULL;
390 /* create a socket for sending to remote machine */
392 gst_dynudpsink_start (GstBaseSink * bsink)
394 GstDynUDPSink *udpsink;
397 udpsink = GST_DYNUDPSINK (bsink);
399 gst_dynudpsink_create_cancellable (udpsink);
401 udpsink->external_socket = FALSE;
403 if (udpsink->socket) {
404 if (g_socket_get_family (udpsink->socket) == G_SOCKET_FAMILY_IPV6) {
405 udpsink->used_socket_v6 = G_SOCKET (g_object_ref (udpsink->socket));
406 udpsink->external_socket = TRUE;
408 udpsink->used_socket = G_SOCKET (g_object_ref (udpsink->socket));
409 udpsink->external_socket = TRUE;
413 if (udpsink->socket_v6) {
414 g_return_val_if_fail (g_socket_get_family (udpsink->socket) !=
415 G_SOCKET_FAMILY_IPV6, FALSE);
417 if (udpsink->used_socket_v6
418 && udpsink->used_socket_v6 != udpsink->socket_v6) {
419 GST_ERROR_OBJECT (udpsink,
420 "Provided different IPv6 sockets in socket and socket-v6 properties");
424 udpsink->used_socket_v6 = G_SOCKET (g_object_ref (udpsink->socket_v6));
425 udpsink->external_socket = TRUE;
428 if (!udpsink->used_socket && !udpsink->used_socket_v6) {
429 GSocketAddress *bind_addr;
430 GInetAddress *bind_iaddr;
432 if (udpsink->bind_address) {
433 GSocketFamily family;
435 bind_iaddr = g_inet_address_new_from_string (udpsink->bind_address);
440 resolver = g_resolver_get_default ();
442 g_resolver_lookup_by_name (resolver, udpsink->bind_address,
443 udpsink->cancellable, &err);
445 g_object_unref (resolver);
448 bind_iaddr = G_INET_ADDRESS (g_object_ref (results->data));
449 g_resolver_free_addresses (results);
450 g_object_unref (resolver);
453 bind_addr = g_inet_socket_address_new (bind_iaddr, udpsink->bind_port);
454 g_object_unref (bind_iaddr);
455 family = g_socket_address_get_family (G_SOCKET_ADDRESS (bind_addr));
457 if ((udpsink->used_socket =
458 g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
459 G_SOCKET_PROTOCOL_UDP, &err)) == NULL) {
460 g_object_unref (bind_addr);
464 g_socket_bind (udpsink->used_socket, bind_addr, TRUE, &err);
468 /* create sender sockets if none available */
469 if ((udpsink->used_socket = g_socket_new (G_SOCKET_FAMILY_IPV4,
470 G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &err)) == NULL)
473 bind_iaddr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
474 bind_addr = g_inet_socket_address_new (bind_iaddr, 0);
475 g_socket_bind (udpsink->used_socket, bind_addr, TRUE, &err);
476 g_object_unref (bind_addr);
477 g_object_unref (bind_iaddr);
481 if ((udpsink->used_socket_v6 = g_socket_new (G_SOCKET_FAMILY_IPV6,
482 G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP,
484 GST_INFO_OBJECT (udpsink, "Failed to create IPv6 socket: %s",
486 g_clear_error (&err);
488 bind_iaddr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV6);
489 bind_addr = g_inet_socket_address_new (bind_iaddr, 0);
490 g_socket_bind (udpsink->used_socket_v6, bind_addr, TRUE, &err);
491 g_object_unref (bind_addr);
492 g_object_unref (bind_iaddr);
499 if (udpsink->used_socket)
500 g_socket_set_broadcast (udpsink->used_socket, TRUE);
501 if (udpsink->used_socket_v6)
502 g_socket_set_broadcast (udpsink->used_socket_v6, TRUE);
509 GST_ERROR_OBJECT (udpsink, "Failed to create IPv4 socket: %s",
511 g_clear_error (&err);
516 GST_ELEMENT_ERROR (udpsink, RESOURCE, FAILED, (NULL),
517 ("Failed to bind socket: %s", err->message));
518 g_clear_error (&err);
523 GST_ELEMENT_ERROR (udpsink, RESOURCE, FAILED, (NULL),
524 ("Failed to resolve bind address %s: %s", udpsink->bind_address,
526 g_clear_error (&err);
531 static GstStructure *
532 gst_dynudpsink_get_stats (GstDynUDPSink * sink, const gchar * host, gint port)
538 gst_dynudpsink_stop (GstBaseSink * bsink)
540 GstDynUDPSink *udpsink;
542 udpsink = GST_DYNUDPSINK (bsink);
544 if (udpsink->used_socket) {
545 if (udpsink->close_socket || !udpsink->external_socket) {
548 if (!g_socket_close (udpsink->used_socket, &err)) {
549 GST_ERROR_OBJECT (udpsink, "Failed to close socket: %s", err->message);
550 g_clear_error (&err);
554 g_object_unref (udpsink->used_socket);
555 udpsink->used_socket = NULL;
558 if (udpsink->used_socket_v6) {
559 if (udpsink->close_socket || !udpsink->external_socket) {
562 if (!g_socket_close (udpsink->used_socket_v6, &err)) {
563 GST_ERROR_OBJECT (udpsink, "Failed to close socket: %s", err->message);
564 g_clear_error (&err);
568 g_object_unref (udpsink->used_socket_v6);
569 udpsink->used_socket_v6 = NULL;
572 gst_dynudpsink_free_cancellable (udpsink);
578 gst_dynudpsink_unlock (GstBaseSink * bsink)
580 GstDynUDPSink *udpsink;
582 udpsink = GST_DYNUDPSINK (bsink);
584 g_cancellable_cancel (udpsink->cancellable);
590 gst_dynudpsink_unlock_stop (GstBaseSink * bsink)
592 GstDynUDPSink *udpsink;
594 udpsink = GST_DYNUDPSINK (bsink);
596 gst_dynudpsink_free_cancellable (udpsink);
597 gst_dynudpsink_create_cancellable (udpsink);