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.
24 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
25 * with newer GLib versions (>= 2.31.0) */
26 #define GLIB_DISABLE_DEPRECATION_WARNINGS
31 #include "gstdynudpsink.h"
33 #include <gst/net/gstnetaddressmeta.h>
35 GST_DEBUG_CATEGORY_STATIC (dynudpsink_debug);
36 #define GST_CAT_DEFAULT (dynudpsink_debug)
38 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
43 /* DynUDPSink signals and args */
55 #define UDP_DEFAULT_SOCKET NULL
56 #define UDP_DEFAULT_CLOSE_SOCKET TRUE
57 #define UDP_DEFAULT_BIND_ADDRESS NULL
58 #define UDP_DEFAULT_BIND_PORT 0
70 static void gst_dynudpsink_finalize (GObject * object);
72 static GstFlowReturn gst_dynudpsink_render (GstBaseSink * sink,
74 static gboolean gst_dynudpsink_stop (GstBaseSink * bsink);
75 static gboolean gst_dynudpsink_start (GstBaseSink * bsink);
76 static gboolean gst_dynudpsink_unlock (GstBaseSink * bsink);
77 static gboolean gst_dynudpsink_unlock_stop (GstBaseSink * bsink);
79 static void gst_dynudpsink_set_property (GObject * object, guint prop_id,
80 const GValue * value, GParamSpec * pspec);
81 static void gst_dynudpsink_get_property (GObject * object, guint prop_id,
82 GValue * value, GParamSpec * pspec);
83 static GstStructure *gst_dynudpsink_get_stats (GstDynUDPSink * sink,
84 const gchar * host, gint port);
86 static guint gst_dynudpsink_signals[LAST_SIGNAL] = { 0 };
88 #define gst_dynudpsink_parent_class parent_class
89 G_DEFINE_TYPE (GstDynUDPSink, gst_dynudpsink, GST_TYPE_BASE_SINK);
92 gst_dynudpsink_class_init (GstDynUDPSinkClass * klass)
94 GObjectClass *gobject_class;
95 GstElementClass *gstelement_class;
96 GstBaseSinkClass *gstbasesink_class;
98 gobject_class = (GObjectClass *) klass;
99 gstelement_class = (GstElementClass *) klass;
100 gstbasesink_class = (GstBaseSinkClass *) klass;
102 parent_class = g_type_class_peek_parent (klass);
104 gobject_class->set_property = gst_dynudpsink_set_property;
105 gobject_class->get_property = gst_dynudpsink_get_property;
106 gobject_class->finalize = gst_dynudpsink_finalize;
108 gst_dynudpsink_signals[SIGNAL_GET_STATS] =
109 g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass),
110 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
111 G_STRUCT_OFFSET (GstDynUDPSinkClass, get_stats),
112 NULL, NULL, g_cclosure_marshal_generic, GST_TYPE_STRUCTURE, 2,
113 G_TYPE_STRING, G_TYPE_INT);
115 g_object_class_install_property (gobject_class, PROP_SOCKET,
116 g_param_spec_object ("socket", "Socket",
117 "Socket to use for UDP sending. (NULL == allocate)",
118 G_TYPE_SOCKET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
119 g_object_class_install_property (gobject_class, PROP_SOCKET_V6,
120 g_param_spec_object ("socket-v6", "Socket IPv6",
121 "Socket to use for UDPv6 sending. (NULL == allocate)",
122 G_TYPE_SOCKET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
123 g_object_class_install_property (gobject_class, PROP_CLOSE_SOCKET,
124 g_param_spec_boolean ("close-socket", "Close socket",
125 "Close socket if passed as property on state change",
126 UDP_DEFAULT_CLOSE_SOCKET,
127 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
128 g_object_class_install_property (gobject_class, PROP_BIND_ADDRESS,
129 g_param_spec_string ("bind-address", "Bind Address",
130 "Address to bind the socket to", UDP_DEFAULT_BIND_ADDRESS,
131 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
132 g_object_class_install_property (gobject_class, PROP_BIND_PORT,
133 g_param_spec_int ("bind-port", "Bind Port",
134 "Port to bind the socket to", 0, G_MAXUINT16,
135 UDP_DEFAULT_BIND_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
137 gst_element_class_add_pad_template (gstelement_class,
138 gst_static_pad_template_get (&sink_template));
140 gst_element_class_set_static_metadata (gstelement_class, "UDP packet sender",
142 "Send data over the network via UDP",
143 "Philippe Khalaf <burger@speedy.org>");
145 gstbasesink_class->render = gst_dynudpsink_render;
146 gstbasesink_class->start = gst_dynudpsink_start;
147 gstbasesink_class->stop = gst_dynudpsink_stop;
148 gstbasesink_class->unlock = gst_dynudpsink_unlock;
149 gstbasesink_class->unlock_stop = gst_dynudpsink_unlock_stop;
151 klass->get_stats = gst_dynudpsink_get_stats;
153 GST_DEBUG_CATEGORY_INIT (dynudpsink_debug, "dynudpsink", 0, "UDP sink");
157 gst_dynudpsink_init (GstDynUDPSink * sink)
159 sink->socket = UDP_DEFAULT_SOCKET;
160 sink->socket_v6 = UDP_DEFAULT_SOCKET;
161 sink->close_socket = UDP_DEFAULT_CLOSE_SOCKET;
162 sink->external_socket = FALSE;
163 sink->bind_address = UDP_DEFAULT_BIND_ADDRESS;
164 sink->bind_port = UDP_DEFAULT_BIND_PORT;
166 sink->used_socket = NULL;
167 sink->used_socket_v6 = NULL;
168 sink->cancellable = g_cancellable_new ();
172 gst_dynudpsink_finalize (GObject * object)
176 sink = GST_DYNUDPSINK (object);
178 if (sink->cancellable)
179 g_object_unref (sink->cancellable);
180 sink->cancellable = NULL;
183 g_object_unref (sink->socket);
187 g_object_unref (sink->socket_v6);
188 sink->socket_v6 = NULL;
190 if (sink->used_socket)
191 g_object_unref (sink->used_socket);
192 sink->used_socket = NULL;
194 if (sink->used_socket_v6)
195 g_object_unref (sink->used_socket_v6);
196 sink->used_socket_v6 = NULL;
198 g_free (sink->bind_address);
199 sink->bind_address = NULL;
201 G_OBJECT_CLASS (parent_class)->finalize (object);
205 gst_dynudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
210 GstNetAddressMeta *meta;
211 GSocketAddress *addr;
213 GSocketFamily family;
216 meta = gst_buffer_get_net_address_meta (buffer);
219 GST_DEBUG ("Received buffer without GstNetAddressMeta, skipping");
223 sink = GST_DYNUDPSINK (bsink);
225 /* let's get the address from the metadata */
228 family = g_socket_address_get_family (addr);
229 if (family == G_SOCKET_FAMILY_IPV6 && !sink->used_socket_v6)
232 gst_buffer_map (buffer, &map, GST_MAP_READ);
234 GST_DEBUG ("about to send %" G_GSIZE_FORMAT " bytes", map.size);
236 #ifndef GST_DISABLE_GST_DEBUG
241 g_inet_address_to_string (g_inet_socket_address_get_address
242 (G_INET_SOCKET_ADDRESS (addr)));
243 GST_DEBUG ("sending %" G_GSIZE_FORMAT " bytes to client %s port %d",
245 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
250 /* Select socket to send from for this address */
251 if (family == G_SOCKET_FAMILY_IPV6 || !sink->used_socket)
252 socket = sink->used_socket_v6;
254 socket = sink->used_socket;
257 g_socket_send_to (socket, addr, (gchar *) map.data, map.size,
258 sink->cancellable, &err);
259 gst_buffer_unmap (buffer, &map);
264 GST_DEBUG ("sent %" G_GSSIZE_FORMAT " bytes", ret);
270 GST_DEBUG ("got send error %s", err->message);
271 g_clear_error (&err);
272 return GST_FLOW_ERROR;
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);
369 /* create a socket for sending to remote machine */
371 gst_dynudpsink_start (GstBaseSink * bsink)
373 GstDynUDPSink *udpsink;
376 udpsink = GST_DYNUDPSINK (bsink);
378 udpsink->external_socket = FALSE;
380 if (udpsink->socket) {
381 if (g_socket_get_family (udpsink->socket) == G_SOCKET_FAMILY_IPV6) {
382 udpsink->used_socket_v6 = G_SOCKET (g_object_ref (udpsink->socket));
383 udpsink->external_socket = TRUE;
385 udpsink->used_socket = G_SOCKET (g_object_ref (udpsink->socket));
386 udpsink->external_socket = TRUE;
390 if (udpsink->socket_v6) {
391 g_return_val_if_fail (g_socket_get_family (udpsink->socket) !=
392 G_SOCKET_FAMILY_IPV6, FALSE);
394 if (udpsink->used_socket_v6
395 && udpsink->used_socket_v6 != udpsink->socket_v6) {
396 GST_ERROR_OBJECT (udpsink,
397 "Provided different IPv6 sockets in socket and socket-v6 properties");
401 udpsink->used_socket_v6 = G_SOCKET (g_object_ref (udpsink->socket_v6));
402 udpsink->external_socket = TRUE;
405 if (!udpsink->used_socket && !udpsink->used_socket_v6) {
406 GSocketAddress *bind_addr;
407 GInetAddress *bind_iaddr;
409 if (udpsink->bind_address) {
410 GSocketFamily family;
412 bind_iaddr = g_inet_address_new_from_string (udpsink->bind_address);
417 resolver = g_resolver_get_default ();
419 g_resolver_lookup_by_name (resolver, udpsink->bind_address,
420 udpsink->cancellable, &err);
422 g_object_unref (resolver);
425 bind_iaddr = G_INET_ADDRESS (g_object_ref (results->data));
426 g_resolver_free_addresses (results);
427 g_object_unref (resolver);
430 bind_addr = g_inet_socket_address_new (bind_iaddr, udpsink->bind_port);
431 g_object_unref (bind_iaddr);
432 family = g_socket_address_get_family (G_SOCKET_ADDRESS (bind_addr));
434 if ((udpsink->used_socket =
435 g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
436 G_SOCKET_PROTOCOL_UDP, &err)) == NULL) {
437 g_object_unref (bind_addr);
441 g_socket_bind (udpsink->used_socket, bind_addr, TRUE, &err);
445 /* create sender sockets if none available */
446 if ((udpsink->used_socket = g_socket_new (G_SOCKET_FAMILY_IPV4,
447 G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &err)) == NULL)
450 bind_iaddr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
451 bind_addr = g_inet_socket_address_new (bind_iaddr, 0);
452 g_socket_bind (udpsink->used_socket, bind_addr, TRUE, &err);
453 g_object_unref (bind_addr);
454 g_object_unref (bind_iaddr);
458 if ((udpsink->used_socket_v6 = g_socket_new (G_SOCKET_FAMILY_IPV6,
459 G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP,
461 GST_INFO_OBJECT (udpsink, "Failed to create IPv6 socket: %s",
463 g_clear_error (&err);
465 bind_iaddr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV6);
466 bind_addr = g_inet_socket_address_new (bind_iaddr, 0);
467 g_socket_bind (udpsink->used_socket_v6, bind_addr, TRUE, &err);
468 g_object_unref (bind_addr);
469 g_object_unref (bind_iaddr);
476 if (udpsink->used_socket)
477 g_socket_set_broadcast (udpsink->used_socket, TRUE);
478 if (udpsink->used_socket_v6)
479 g_socket_set_broadcast (udpsink->used_socket_v6, TRUE);
486 GST_ERROR_OBJECT (udpsink, "Failed to create IPv4 socket: %s",
488 g_clear_error (&err);
493 GST_ELEMENT_ERROR (udpsink, RESOURCE, FAILED, (NULL),
494 ("Failed to bind socket: %s", err->message));
495 g_clear_error (&err);
500 GST_ELEMENT_ERROR (udpsink, RESOURCE, FAILED, (NULL),
501 ("Failed to resolve bind address %s: %s", udpsink->bind_address,
503 g_clear_error (&err);
508 static GstStructure *
509 gst_dynudpsink_get_stats (GstDynUDPSink * sink, const gchar * host, gint port)
515 gst_dynudpsink_stop (GstBaseSink * bsink)
517 GstDynUDPSink *udpsink;
519 udpsink = GST_DYNUDPSINK (bsink);
521 if (udpsink->used_socket) {
522 if (udpsink->close_socket || !udpsink->external_socket) {
525 if (!g_socket_close (udpsink->used_socket, &err)) {
526 GST_ERROR_OBJECT (udpsink, "Failed to close socket: %s", err->message);
527 g_clear_error (&err);
531 g_object_unref (udpsink->used_socket);
532 udpsink->used_socket = NULL;
535 if (udpsink->used_socket_v6) {
536 if (udpsink->close_socket || !udpsink->external_socket) {
539 if (!g_socket_close (udpsink->used_socket_v6, &err)) {
540 GST_ERROR_OBJECT (udpsink, "Failed to close socket: %s", err->message);
541 g_clear_error (&err);
545 g_object_unref (udpsink->used_socket_v6);
546 udpsink->used_socket_v6 = NULL;
553 gst_dynudpsink_unlock (GstBaseSink * bsink)
555 GstDynUDPSink *udpsink;
557 udpsink = GST_DYNUDPSINK (bsink);
559 g_cancellable_cancel (udpsink->cancellable);
565 gst_dynudpsink_unlock_stop (GstBaseSink * bsink)
567 GstDynUDPSink *udpsink;
569 udpsink = GST_DYNUDPSINK (bsink);
571 g_cancellable_reset (udpsink->cancellable);