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;
165 sink->cancellable = g_cancellable_new ();
169 gst_dynudpsink_finalize (GObject * object)
173 sink = GST_DYNUDPSINK (object);
175 if (sink->cancellable)
176 g_object_unref (sink->cancellable);
177 sink->cancellable = NULL;
180 g_object_unref (sink->socket);
184 g_object_unref (sink->socket_v6);
185 sink->socket_v6 = NULL;
187 if (sink->used_socket)
188 g_object_unref (sink->used_socket);
189 sink->used_socket = NULL;
191 if (sink->used_socket_v6)
192 g_object_unref (sink->used_socket_v6);
193 sink->used_socket_v6 = NULL;
195 g_free (sink->bind_address);
196 sink->bind_address = NULL;
198 G_OBJECT_CLASS (parent_class)->finalize (object);
202 gst_dynudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
207 GstNetAddressMeta *meta;
208 GSocketAddress *addr;
210 GSocketFamily family;
213 meta = gst_buffer_get_net_address_meta (buffer);
216 GST_DEBUG ("Received buffer without GstNetAddressMeta, skipping");
220 sink = GST_DYNUDPSINK (bsink);
222 /* let's get the address from the metadata */
225 family = g_socket_address_get_family (addr);
226 if (family == G_SOCKET_FAMILY_IPV6 && !sink->used_socket_v6)
229 gst_buffer_map (buffer, &map, GST_MAP_READ);
231 GST_DEBUG ("about to send %" G_GSIZE_FORMAT " bytes", map.size);
233 #ifndef GST_DISABLE_GST_DEBUG
238 g_inet_address_to_string (g_inet_socket_address_get_address
239 (G_INET_SOCKET_ADDRESS (addr)));
240 GST_DEBUG ("sending %" G_GSIZE_FORMAT " bytes to client %s port %d",
242 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
247 /* Select socket to send from for this address */
248 if (family == G_SOCKET_FAMILY_IPV6 || !sink->used_socket)
249 socket = sink->used_socket_v6;
251 socket = sink->used_socket;
254 g_socket_send_to (socket, addr, (gchar *) map.data, map.size,
255 sink->cancellable, &err);
256 gst_buffer_unmap (buffer, &map);
261 GST_DEBUG ("sent %" G_GSSIZE_FORMAT " bytes", ret);
267 GstFlowReturn flow_ret;
269 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
270 GST_DEBUG_OBJECT (sink, "send cancelled");
271 flow_ret = GST_FLOW_FLUSHING;
273 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
274 ("send error: %s", err->message));
275 flow_ret = GST_FLOW_ERROR;
277 g_clear_error (&err);
282 GST_DEBUG ("invalid address family (got %d)", family);
283 return GST_FLOW_ERROR;
288 gst_dynudpsink_set_property (GObject * object, guint prop_id,
289 const GValue * value, GParamSpec * pspec)
291 GstDynUDPSink *udpsink;
293 udpsink = GST_DYNUDPSINK (object);
297 if (udpsink->socket != NULL && udpsink->socket != udpsink->used_socket &&
298 udpsink->close_socket) {
301 if (!g_socket_close (udpsink->socket, &err)) {
302 GST_ERROR ("failed to close socket %p: %s", udpsink->socket,
304 g_clear_error (&err);
308 g_object_unref (udpsink->socket);
309 udpsink->socket = g_value_dup_object (value);
310 GST_DEBUG ("setting socket to %p", udpsink->socket);
313 if (udpsink->socket_v6 != NULL
314 && udpsink->socket_v6 != udpsink->used_socket_v6
315 && udpsink->close_socket) {
318 if (!g_socket_close (udpsink->socket_v6, &err)) {
319 GST_ERROR ("failed to close socket %p: %s", udpsink->socket_v6,
321 g_clear_error (&err);
324 if (udpsink->socket_v6)
325 g_object_unref (udpsink->socket_v6);
326 udpsink->socket_v6 = g_value_dup_object (value);
327 GST_DEBUG ("setting socket v6 to %p", udpsink->socket_v6);
329 case PROP_CLOSE_SOCKET:
330 udpsink->close_socket = g_value_get_boolean (value);
332 case PROP_BIND_ADDRESS:
333 g_free (udpsink->bind_address);
334 udpsink->bind_address = g_value_dup_string (value);
337 udpsink->bind_port = g_value_get_int (value);
340 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
346 gst_dynudpsink_get_property (GObject * object, guint prop_id, GValue * value,
349 GstDynUDPSink *udpsink;
351 udpsink = GST_DYNUDPSINK (object);
355 g_value_set_object (value, udpsink->socket);
358 g_value_set_object (value, udpsink->socket_v6);
360 case PROP_CLOSE_SOCKET:
361 g_value_set_boolean (value, udpsink->close_socket);
363 case PROP_BIND_ADDRESS:
364 g_value_set_string (value, udpsink->bind_address);
367 g_value_set_int (value, udpsink->bind_port);
370 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
375 /* create a socket for sending to remote machine */
377 gst_dynudpsink_start (GstBaseSink * bsink)
379 GstDynUDPSink *udpsink;
382 udpsink = GST_DYNUDPSINK (bsink);
384 udpsink->external_socket = FALSE;
386 if (udpsink->socket) {
387 if (g_socket_get_family (udpsink->socket) == G_SOCKET_FAMILY_IPV6) {
388 udpsink->used_socket_v6 = G_SOCKET (g_object_ref (udpsink->socket));
389 udpsink->external_socket = TRUE;
391 udpsink->used_socket = G_SOCKET (g_object_ref (udpsink->socket));
392 udpsink->external_socket = TRUE;
396 if (udpsink->socket_v6) {
397 g_return_val_if_fail (g_socket_get_family (udpsink->socket) !=
398 G_SOCKET_FAMILY_IPV6, FALSE);
400 if (udpsink->used_socket_v6
401 && udpsink->used_socket_v6 != udpsink->socket_v6) {
402 GST_ERROR_OBJECT (udpsink,
403 "Provided different IPv6 sockets in socket and socket-v6 properties");
407 udpsink->used_socket_v6 = G_SOCKET (g_object_ref (udpsink->socket_v6));
408 udpsink->external_socket = TRUE;
411 if (!udpsink->used_socket && !udpsink->used_socket_v6) {
412 GSocketAddress *bind_addr;
413 GInetAddress *bind_iaddr;
415 if (udpsink->bind_address) {
416 GSocketFamily family;
418 bind_iaddr = g_inet_address_new_from_string (udpsink->bind_address);
423 resolver = g_resolver_get_default ();
425 g_resolver_lookup_by_name (resolver, udpsink->bind_address,
426 udpsink->cancellable, &err);
428 g_object_unref (resolver);
431 bind_iaddr = G_INET_ADDRESS (g_object_ref (results->data));
432 g_resolver_free_addresses (results);
433 g_object_unref (resolver);
436 bind_addr = g_inet_socket_address_new (bind_iaddr, udpsink->bind_port);
437 g_object_unref (bind_iaddr);
438 family = g_socket_address_get_family (G_SOCKET_ADDRESS (bind_addr));
440 if ((udpsink->used_socket =
441 g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
442 G_SOCKET_PROTOCOL_UDP, &err)) == NULL) {
443 g_object_unref (bind_addr);
447 g_socket_bind (udpsink->used_socket, bind_addr, TRUE, &err);
451 /* create sender sockets if none available */
452 if ((udpsink->used_socket = g_socket_new (G_SOCKET_FAMILY_IPV4,
453 G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &err)) == NULL)
456 bind_iaddr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
457 bind_addr = g_inet_socket_address_new (bind_iaddr, 0);
458 g_socket_bind (udpsink->used_socket, bind_addr, TRUE, &err);
459 g_object_unref (bind_addr);
460 g_object_unref (bind_iaddr);
464 if ((udpsink->used_socket_v6 = g_socket_new (G_SOCKET_FAMILY_IPV6,
465 G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP,
467 GST_INFO_OBJECT (udpsink, "Failed to create IPv6 socket: %s",
469 g_clear_error (&err);
471 bind_iaddr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV6);
472 bind_addr = g_inet_socket_address_new (bind_iaddr, 0);
473 g_socket_bind (udpsink->used_socket_v6, bind_addr, TRUE, &err);
474 g_object_unref (bind_addr);
475 g_object_unref (bind_iaddr);
482 if (udpsink->used_socket)
483 g_socket_set_broadcast (udpsink->used_socket, TRUE);
484 if (udpsink->used_socket_v6)
485 g_socket_set_broadcast (udpsink->used_socket_v6, TRUE);
492 GST_ERROR_OBJECT (udpsink, "Failed to create IPv4 socket: %s",
494 g_clear_error (&err);
499 GST_ELEMENT_ERROR (udpsink, RESOURCE, FAILED, (NULL),
500 ("Failed to bind socket: %s", err->message));
501 g_clear_error (&err);
506 GST_ELEMENT_ERROR (udpsink, RESOURCE, FAILED, (NULL),
507 ("Failed to resolve bind address %s: %s", udpsink->bind_address,
509 g_clear_error (&err);
514 static GstStructure *
515 gst_dynudpsink_get_stats (GstDynUDPSink * sink, const gchar * host, gint port)
521 gst_dynudpsink_stop (GstBaseSink * bsink)
523 GstDynUDPSink *udpsink;
525 udpsink = GST_DYNUDPSINK (bsink);
527 if (udpsink->used_socket) {
528 if (udpsink->close_socket || !udpsink->external_socket) {
531 if (!g_socket_close (udpsink->used_socket, &err)) {
532 GST_ERROR_OBJECT (udpsink, "Failed to close socket: %s", err->message);
533 g_clear_error (&err);
537 g_object_unref (udpsink->used_socket);
538 udpsink->used_socket = NULL;
541 if (udpsink->used_socket_v6) {
542 if (udpsink->close_socket || !udpsink->external_socket) {
545 if (!g_socket_close (udpsink->used_socket_v6, &err)) {
546 GST_ERROR_OBJECT (udpsink, "Failed to close socket: %s", err->message);
547 g_clear_error (&err);
551 g_object_unref (udpsink->used_socket_v6);
552 udpsink->used_socket_v6 = NULL;
559 gst_dynudpsink_unlock (GstBaseSink * bsink)
561 GstDynUDPSink *udpsink;
563 udpsink = GST_DYNUDPSINK (bsink);
565 g_cancellable_cancel (udpsink->cancellable);
571 gst_dynudpsink_unlock_stop (GstBaseSink * bsink)
573 GstDynUDPSink *udpsink;
575 udpsink = GST_DYNUDPSINK (bsink);
577 g_cancellable_reset (udpsink->cancellable);