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_static_pad_template (gstelement_class, &sink_template);
135 gst_element_class_set_static_metadata (gstelement_class, "UDP packet sender",
137 "Send data over the network via UDP with packet destinations picked up "
138 "dynamically from meta on the buffers passed",
139 "Philippe Khalaf <burger@speedy.org>");
141 gstbasesink_class->render = gst_dynudpsink_render;
142 gstbasesink_class->start = gst_dynudpsink_start;
143 gstbasesink_class->stop = gst_dynudpsink_stop;
144 gstbasesink_class->unlock = gst_dynudpsink_unlock;
145 gstbasesink_class->unlock_stop = gst_dynudpsink_unlock_stop;
147 klass->get_stats = gst_dynudpsink_get_stats;
149 GST_DEBUG_CATEGORY_INIT (dynudpsink_debug, "dynudpsink", 0, "UDP sink");
153 gst_dynudpsink_init (GstDynUDPSink * sink)
155 sink->socket = UDP_DEFAULT_SOCKET;
156 sink->socket_v6 = UDP_DEFAULT_SOCKET;
157 sink->close_socket = UDP_DEFAULT_CLOSE_SOCKET;
158 sink->external_socket = FALSE;
159 sink->bind_address = UDP_DEFAULT_BIND_ADDRESS;
160 sink->bind_port = UDP_DEFAULT_BIND_PORT;
162 sink->used_socket = NULL;
163 sink->used_socket_v6 = NULL;
167 gst_dynudpsink_finalize (GObject * object)
171 sink = GST_DYNUDPSINK (object);
174 g_object_unref (sink->socket);
178 g_object_unref (sink->socket_v6);
179 sink->socket_v6 = NULL;
181 if (sink->used_socket)
182 g_object_unref (sink->used_socket);
183 sink->used_socket = NULL;
185 if (sink->used_socket_v6)
186 g_object_unref (sink->used_socket_v6);
187 sink->used_socket_v6 = NULL;
189 g_free (sink->bind_address);
190 sink->bind_address = NULL;
192 G_OBJECT_CLASS (parent_class)->finalize (object);
196 gst_dynudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
201 GstNetAddressMeta *meta;
202 GSocketAddress *addr;
204 GSocketFamily family;
207 meta = gst_buffer_get_net_address_meta (buffer);
210 GST_DEBUG ("Received buffer without GstNetAddressMeta, skipping");
214 sink = GST_DYNUDPSINK (bsink);
216 /* let's get the address from the metadata */
219 family = g_socket_address_get_family (addr);
220 if (family == G_SOCKET_FAMILY_IPV6 && !sink->used_socket_v6)
223 gst_buffer_map (buffer, &map, GST_MAP_READ);
225 GST_DEBUG ("about to send %" G_GSIZE_FORMAT " bytes", map.size);
227 #ifndef GST_DISABLE_GST_DEBUG
232 g_inet_address_to_string (g_inet_socket_address_get_address
233 (G_INET_SOCKET_ADDRESS (addr)));
234 GST_DEBUG ("sending %" G_GSIZE_FORMAT " bytes to client %s port %d",
236 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
241 /* Select socket to send from for this address */
242 if (family == G_SOCKET_FAMILY_IPV6 || !sink->used_socket)
243 socket = sink->used_socket_v6;
245 socket = sink->used_socket;
248 g_socket_send_to (socket, addr, (gchar *) map.data, map.size,
249 sink->cancellable, &err);
250 gst_buffer_unmap (buffer, &map);
255 GST_DEBUG ("sent %" G_GSSIZE_FORMAT " bytes", ret);
261 GstFlowReturn flow_ret;
263 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
264 GST_DEBUG_OBJECT (sink, "send cancelled");
265 flow_ret = GST_FLOW_FLUSHING;
267 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
268 ("send error: %s", err->message));
269 flow_ret = GST_FLOW_ERROR;
271 g_clear_error (&err);
276 GST_DEBUG ("invalid address family (got %d)", family);
277 return GST_FLOW_ERROR;
282 gst_dynudpsink_set_property (GObject * object, guint prop_id,
283 const GValue * value, GParamSpec * pspec)
285 GstDynUDPSink *udpsink;
287 udpsink = GST_DYNUDPSINK (object);
291 if (udpsink->socket != NULL && udpsink->socket != udpsink->used_socket &&
292 udpsink->close_socket) {
295 if (!g_socket_close (udpsink->socket, &err)) {
296 GST_ERROR ("failed to close socket %p: %s", udpsink->socket,
298 g_clear_error (&err);
302 g_object_unref (udpsink->socket);
303 udpsink->socket = g_value_dup_object (value);
304 GST_DEBUG ("setting socket to %p", udpsink->socket);
307 if (udpsink->socket_v6 != NULL
308 && udpsink->socket_v6 != udpsink->used_socket_v6
309 && udpsink->close_socket) {
312 if (!g_socket_close (udpsink->socket_v6, &err)) {
313 GST_ERROR ("failed to close socket %p: %s", udpsink->socket_v6,
315 g_clear_error (&err);
318 if (udpsink->socket_v6)
319 g_object_unref (udpsink->socket_v6);
320 udpsink->socket_v6 = g_value_dup_object (value);
321 GST_DEBUG ("setting socket v6 to %p", udpsink->socket_v6);
323 case PROP_CLOSE_SOCKET:
324 udpsink->close_socket = g_value_get_boolean (value);
326 case PROP_BIND_ADDRESS:
327 g_free (udpsink->bind_address);
328 udpsink->bind_address = g_value_dup_string (value);
331 udpsink->bind_port = g_value_get_int (value);
334 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
340 gst_dynudpsink_get_property (GObject * object, guint prop_id, GValue * value,
343 GstDynUDPSink *udpsink;
345 udpsink = GST_DYNUDPSINK (object);
349 g_value_set_object (value, udpsink->socket);
352 g_value_set_object (value, udpsink->socket_v6);
354 case PROP_CLOSE_SOCKET:
355 g_value_set_boolean (value, udpsink->close_socket);
357 case PROP_BIND_ADDRESS:
358 g_value_set_string (value, udpsink->bind_address);
361 g_value_set_int (value, udpsink->bind_port);
364 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
370 gst_dynudpsink_create_cancellable (GstDynUDPSink * sink)
374 sink->cancellable = g_cancellable_new ();
375 sink->made_cancel_fd = g_cancellable_make_pollfd (sink->cancellable, &pollfd);
379 gst_dynudpsink_free_cancellable (GstDynUDPSink * sink)
381 if (sink->made_cancel_fd) {
382 g_cancellable_release_fd (sink->cancellable);
383 sink->made_cancel_fd = FALSE;
385 g_object_unref (sink->cancellable);
386 sink->cancellable = NULL;
389 /* create a socket for sending to remote machine */
391 gst_dynudpsink_start (GstBaseSink * bsink)
393 GstDynUDPSink *udpsink;
396 udpsink = GST_DYNUDPSINK (bsink);
398 gst_dynudpsink_create_cancellable (udpsink);
400 udpsink->external_socket = FALSE;
402 if (udpsink->socket) {
403 if (g_socket_get_family (udpsink->socket) == G_SOCKET_FAMILY_IPV6) {
404 udpsink->used_socket_v6 = G_SOCKET (g_object_ref (udpsink->socket));
405 udpsink->external_socket = TRUE;
407 udpsink->used_socket = G_SOCKET (g_object_ref (udpsink->socket));
408 udpsink->external_socket = TRUE;
412 if (udpsink->socket_v6) {
413 g_return_val_if_fail (g_socket_get_family (udpsink->socket) !=
414 G_SOCKET_FAMILY_IPV6, FALSE);
416 if (udpsink->used_socket_v6
417 && udpsink->used_socket_v6 != udpsink->socket_v6) {
418 GST_ERROR_OBJECT (udpsink,
419 "Provided different IPv6 sockets in socket and socket-v6 properties");
423 udpsink->used_socket_v6 = G_SOCKET (g_object_ref (udpsink->socket_v6));
424 udpsink->external_socket = TRUE;
427 if (!udpsink->used_socket && !udpsink->used_socket_v6) {
428 GSocketAddress *bind_addr;
429 GInetAddress *bind_iaddr;
431 if (udpsink->bind_address) {
432 GSocketFamily family;
434 bind_iaddr = g_inet_address_new_from_string (udpsink->bind_address);
439 resolver = g_resolver_get_default ();
441 g_resolver_lookup_by_name (resolver, udpsink->bind_address,
442 udpsink->cancellable, &err);
444 g_object_unref (resolver);
447 bind_iaddr = G_INET_ADDRESS (g_object_ref (results->data));
448 g_resolver_free_addresses (results);
449 g_object_unref (resolver);
452 bind_addr = g_inet_socket_address_new (bind_iaddr, udpsink->bind_port);
453 g_object_unref (bind_iaddr);
454 family = g_socket_address_get_family (G_SOCKET_ADDRESS (bind_addr));
456 if ((udpsink->used_socket =
457 g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
458 G_SOCKET_PROTOCOL_UDP, &err)) == NULL) {
459 g_object_unref (bind_addr);
463 g_socket_bind (udpsink->used_socket, bind_addr, TRUE, &err);
467 /* create sender sockets if none available */
468 if ((udpsink->used_socket = g_socket_new (G_SOCKET_FAMILY_IPV4,
469 G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &err)) == NULL)
472 bind_iaddr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
473 bind_addr = g_inet_socket_address_new (bind_iaddr, 0);
474 g_socket_bind (udpsink->used_socket, bind_addr, TRUE, &err);
475 g_object_unref (bind_addr);
476 g_object_unref (bind_iaddr);
480 if ((udpsink->used_socket_v6 = g_socket_new (G_SOCKET_FAMILY_IPV6,
481 G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP,
483 GST_INFO_OBJECT (udpsink, "Failed to create IPv6 socket: %s",
485 g_clear_error (&err);
487 bind_iaddr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV6);
488 bind_addr = g_inet_socket_address_new (bind_iaddr, 0);
489 g_socket_bind (udpsink->used_socket_v6, bind_addr, TRUE, &err);
490 g_object_unref (bind_addr);
491 g_object_unref (bind_iaddr);
498 if (udpsink->used_socket)
499 g_socket_set_broadcast (udpsink->used_socket, TRUE);
500 if (udpsink->used_socket_v6)
501 g_socket_set_broadcast (udpsink->used_socket_v6, TRUE);
508 GST_ERROR_OBJECT (udpsink, "Failed to create IPv4 socket: %s",
510 g_clear_error (&err);
515 GST_ELEMENT_ERROR (udpsink, RESOURCE, FAILED, (NULL),
516 ("Failed to bind socket: %s", err->message));
517 g_clear_error (&err);
522 GST_ELEMENT_ERROR (udpsink, RESOURCE, FAILED, (NULL),
523 ("Failed to resolve bind address %s: %s", udpsink->bind_address,
525 g_clear_error (&err);
530 static GstStructure *
531 gst_dynudpsink_get_stats (GstDynUDPSink * sink, const gchar * host, gint port)
537 gst_dynudpsink_stop (GstBaseSink * bsink)
539 GstDynUDPSink *udpsink;
541 udpsink = GST_DYNUDPSINK (bsink);
543 if (udpsink->used_socket) {
544 if (udpsink->close_socket || !udpsink->external_socket) {
547 if (!g_socket_close (udpsink->used_socket, &err)) {
548 GST_ERROR_OBJECT (udpsink, "Failed to close socket: %s", err->message);
549 g_clear_error (&err);
553 g_object_unref (udpsink->used_socket);
554 udpsink->used_socket = NULL;
557 if (udpsink->used_socket_v6) {
558 if (udpsink->close_socket || !udpsink->external_socket) {
561 if (!g_socket_close (udpsink->used_socket_v6, &err)) {
562 GST_ERROR_OBJECT (udpsink, "Failed to close socket: %s", err->message);
563 g_clear_error (&err);
567 g_object_unref (udpsink->used_socket_v6);
568 udpsink->used_socket_v6 = NULL;
571 gst_dynudpsink_free_cancellable (udpsink);
577 gst_dynudpsink_unlock (GstBaseSink * bsink)
579 GstDynUDPSink *udpsink;
581 udpsink = GST_DYNUDPSINK (bsink);
583 g_cancellable_cancel (udpsink->cancellable);
589 gst_dynudpsink_unlock_stop (GstBaseSink * bsink)
591 GstDynUDPSink *udpsink;
593 udpsink = GST_DYNUDPSINK (bsink);
595 gst_dynudpsink_free_cancellable (udpsink);
596 gst_dynudpsink_create_cancellable (udpsink);