gstreamer/gst/udp/gstdynudpsink.c

395 lines
11 KiB
C
Raw Normal View History

/* GStreamer
* Copyright (C) <2005> Philippe Khalaf <burger@speedy.org>
* Copyright (C) <2005> Nokia Corporation <kai.vehmanen@nokia.com>
* Copyright (C) <2006> Joni Valtanen <joni.valtanen@movial.fi>
2012-01-17 08:32:27 +00:00
* Copyright (C) <2012> Collabora Ltd.
* Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
* with newer GLib versions (>= 2.31.0) */
#define GLIB_DISABLE_DEPRECATION_WARNINGS
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstudp-marshal.h"
#include "gstdynudpsink.h"
2011-11-03 15:43:00 +00:00
#include <gst/net/gstnetaddressmeta.h>
GST_DEBUG_CATEGORY_STATIC (dynudpsink_debug);
#define GST_CAT_DEFAULT (dynudpsink_debug)
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS_ANY);
/* DynUDPSink signals and args */
enum
{
/* methods */
SIGNAL_GET_STATS,
/* signals */
/* FILL ME */
LAST_SIGNAL
};
2012-01-17 08:32:27 +00:00
#define UDP_DEFAULT_SOCKET NULL
#define UDP_DEFAULT_CLOSE_SOCKET TRUE
enum
{
PROP_0,
2012-01-17 08:32:27 +00:00
PROP_SOCKET,
2012-01-17 08:38:33 +00:00
PROP_CLOSE_SOCKET
};
static void gst_dynudpsink_finalize (GObject * object);
static GstFlowReturn gst_dynudpsink_render (GstBaseSink * sink,
GstBuffer * buffer);
2012-01-17 08:32:27 +00:00
static gboolean gst_dynudpsink_stop (GstBaseSink * bsink);
static gboolean gst_dynudpsink_start (GstBaseSink * bsink);
static gboolean gst_dynudpsink_unlock (GstBaseSink * bsink);
static gboolean gst_dynudpsink_unlock_stop (GstBaseSink * bsink);
static void gst_dynudpsink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_dynudpsink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static GstStructure *gst_dynudpsink_get_stats (GstDynUDPSink * sink,
const gchar * host, gint port);
static guint gst_dynudpsink_signals[LAST_SIGNAL] = { 0 };
2011-04-19 15:35:47 +00:00
#define gst_dynudpsink_parent_class parent_class
G_DEFINE_TYPE (GstDynUDPSink, gst_dynudpsink, GST_TYPE_BASE_SINK);
static void
2011-04-19 15:35:47 +00:00
gst_dynudpsink_class_init (GstDynUDPSinkClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
GstBaseSinkClass *gstbasesink_class;
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
gstbasesink_class = (GstBaseSinkClass *) klass;
Fix #337365 (g_type_class_ref <-> g_type_class_peek_parent) Original commit message from CVS: * ext/aalib/gstaasink.c: (gst_aasink_class_init): * ext/esd/esdsink.c: (gst_esdsink_class_init): * ext/flac/gstflactag.c: (gst_flac_tag_class_init): * ext/gdk_pixbuf/gstgdkpixbuf.c: (gst_gdk_pixbuf_class_init): * ext/jpeg/gstjpegenc.c: (gst_jpegenc_class_init): * ext/jpeg/gstsmokedec.c: (gst_smokedec_class_init): * ext/jpeg/gstsmokeenc.c: (gst_smokeenc_class_init): * ext/libcaca/gstcacasink.c: (gst_cacasink_class_init): * ext/libmng/gstmngdec.c: (gst_mngdec_class_init): * ext/libmng/gstmngenc.c: (gst_mngenc_class_init): * ext/libpng/gstpngdec.c: (gst_pngdec_class_init): * ext/libpng/gstpngenc.c: (gst_pngenc_class_init): * ext/mikmod/gstmikmod.c: (gst_mikmod_class_init): * ext/shout2/gstshout2.c: (gst_shout2send_class_init): * ext/speex/gstspeexenc.c: (gst_speexenc_class_init): * gst/alpha/gstalpha.c: (gst_alpha_class_init): * gst/avi/gstavimux.c: (gst_avimux_class_init): * gst/debug/efence.c: (gst_efence_class_init): * gst/debug/negotiation.c: (gst_negotiation_class_init): * gst/flx/gstflxdec.c: (gst_flxdec_class_init): * gst/goom/gstgoom.c: (gst_goom_class_init): * gst/id3demux/gstid3demux.c: (gst_id3demux_class_init): * gst/interleave/deinterleave.c: (deinterleave_class_init): * gst/interleave/interleave.c: (interleave_class_init): * gst/law/alaw-decode.c: (gst_alawdec_class_init): * gst/law/alaw-encode.c: (gst_alawenc_class_init): * gst/law/mulaw-encode.c: (gst_mulawenc_class_init): * gst/median/gstmedian.c: (gst_median_class_init): * gst/monoscope/gstmonoscope.c: (gst_monoscope_class_init): * gst/multipart/multipartmux.c: (gst_multipart_mux_class_init): * gst/rtp/gstasteriskh263.c: (gst_asteriskh263_class_init): * gst/rtp/gstrtpL16depay.c: (gst_rtp_L16depay_class_init): * gst/rtp/gstrtpL16pay.c: (gst_rtpL16pay_class_init): * gst/rtp/gstrtpamrdepay.c: (gst_rtp_amr_depay_class_init): * gst/rtp/gstrtpamrpay.c: (gst_rtp_amr_pay_class_init): * gst/rtp/gstrtpdepay.c: (gst_rtp_depay_class_init): * gst/rtp/gstrtpgsmdepay.c: (gst_rtp_gsm_depay_class_init): * gst/rtp/gstrtpgsmpay.c: (gst_rtp_gsm_pay_class_init): * gst/rtp/gstrtph263pay.c: (gst_rtp_h263_pay_class_init): * gst/rtp/gstrtph263pdepay.c: (gst_rtp_h263p_depay_class_init): * gst/rtp/gstrtph263ppay.c: (gst_rtp_h263p_pay_class_init): * gst/rtp/gstrtpmp4gpay.c: (gst_rtp_mp4g_pay_class_init): * gst/rtp/gstrtpmp4vdepay.c: (gst_rtp_mp4v_depay_class_init): * gst/rtp/gstrtpmp4vpay.c: (gst_rtp_mp4v_pay_class_init): * gst/rtp/gstrtpmpadepay.c: (gst_rtp_mpa_depay_class_init): * gst/rtp/gstrtpmpapay.c: (gst_rtp_mpa_pay_class_init): * gst/rtp/gstrtppcmadepay.c: (gst_rtp_pcma_depay_class_init): * gst/rtp/gstrtppcmapay.c: (gst_rtp_pcma_pay_class_init): * gst/rtp/gstrtppcmudepay.c: (gst_rtp_pcmu_depay_class_init): * gst/rtp/gstrtppcmupay.c: (gst_rtp_pcmu_pay_class_init): * gst/rtp/gstrtpspeexdepay.c: (gst_rtp_speex_depay_class_init): * gst/rtp/gstrtpspeexpay.c: (gst_rtp_speex_pay_class_init): * gst/rtsp/gstrtpdec.c: (gst_rtpdec_class_init): * gst/rtsp/gstrtspsrc.c: (gst_rtspsrc_class_init): * gst/smpte/gstsmpte.c: (gst_smpte_class_init): * gst/udp/gstdynudpsink.c: (gst_dynudpsink_class_init): * gst/udp/gstmultiudpsink.c: (gst_multiudpsink_class_init): * gst/udp/gstudpsink.c: (gst_udpsink_class_init): * gst/videomixer/videomixer.c: (gst_videomixer_class_init): * gst/wavenc/gstwavenc.c: (gst_wavenc_class_init): * sys/oss/gstossdmabuffer.c: (gst_ossdmabuffer_class_init): * sys/oss/gstosssink.c: (gst_oss_sink_class_init): * sys/osxaudio/gstosxaudioelement.c: (gst_osxaudioelement_class_init): * sys/osxaudio/gstosxaudiosink.c: (gst_osxaudiosink_class_init): * sys/osxaudio/gstosxaudiosrc.c: (gst_osxaudiosrc_class_init): * sys/sunaudio/gstsunaudiosink.c: (gst_sunaudiosink_class_init): Fix #337365 (g_type_class_ref <-> g_type_class_peek_parent)
2006-04-08 21:21:45 +00:00
parent_class = g_type_class_peek_parent (klass);
gobject_class->set_property = gst_dynudpsink_set_property;
gobject_class->get_property = gst_dynudpsink_get_property;
gobject_class->finalize = gst_dynudpsink_finalize;
gst_dynudpsink_signals[SIGNAL_GET_STATS] =
2012-01-26 15:37:23 +00:00
g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GstDynUDPSinkClass, get_stats),
NULL, NULL, gst_udp_marshal_BOXED__STRING_INT, GST_TYPE_STRUCTURE, 2,
G_TYPE_STRING, G_TYPE_INT);
2012-01-17 08:32:27 +00:00
g_object_class_install_property (gobject_class, PROP_SOCKET,
g_param_spec_object ("socket", "Socket",
"Socket to use for UDP sending. (NULL == allocate)",
G_TYPE_SOCKET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_CLOSE_SOCKET,
g_param_spec_boolean ("close-socket", "Close socket",
"Close socket if passed as property on state change",
UDP_DEFAULT_CLOSE_SOCKET,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2011-04-19 15:35:47 +00:00
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&sink_template));
gst_element_class_set_static_metadata (gstelement_class, "UDP packet sender",
2011-04-19 15:35:47 +00:00
"Sink/Network",
"Send data over the network via UDP",
"Philippe Khalaf <burger@speedy.org>");
gstbasesink_class->render = gst_dynudpsink_render;
2012-01-17 08:32:27 +00:00
gstbasesink_class->start = gst_dynudpsink_start;
gstbasesink_class->stop = gst_dynudpsink_stop;
gstbasesink_class->unlock = gst_dynudpsink_unlock;
gstbasesink_class->unlock_stop = gst_dynudpsink_unlock_stop;
klass->get_stats = gst_dynudpsink_get_stats;
GST_DEBUG_CATEGORY_INIT (dynudpsink_debug, "dynudpsink", 0, "UDP sink");
}
static void
gst_dynudpsink_init (GstDynUDPSink * sink)
{
2012-01-17 08:32:27 +00:00
sink->socket = UDP_DEFAULT_SOCKET;
sink->close_socket = UDP_DEFAULT_CLOSE_SOCKET;
sink->external_socket = FALSE;
2012-01-17 08:32:27 +00:00
sink->used_socket = NULL;
sink->cancellable = g_cancellable_new ();
2012-01-17 08:38:33 +00:00
sink->family = G_SOCKET_FAMILY_IPV6;
}
static void
gst_dynudpsink_finalize (GObject * object)
{
2012-01-17 08:32:27 +00:00
GstDynUDPSink *sink;
2012-01-17 08:32:27 +00:00
sink = GST_DYNUDPSINK (object);
2012-01-17 08:32:27 +00:00
if (sink->cancellable)
g_object_unref (sink->cancellable);
sink->cancellable = NULL;
2012-01-17 08:32:27 +00:00
if (sink->socket)
g_object_unref (sink->socket);
sink->socket = NULL;
2012-01-17 08:32:27 +00:00
if (sink->used_socket)
g_object_unref (sink->used_socket);
sink->used_socket = NULL;
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static GstFlowReturn
gst_dynudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
{
GstDynUDPSink *sink;
2012-01-17 08:32:27 +00:00
gssize ret;
2012-01-24 13:38:58 +00:00
GstMapInfo map;
2011-10-31 01:35:51 +00:00
GstNetAddressMeta *meta;
2012-01-17 08:32:27 +00:00
GSocketAddress *addr;
GError *err = NULL;
2012-01-17 08:38:33 +00:00
GSocketFamily family;
2011-10-31 01:35:51 +00:00
meta = gst_buffer_get_net_address_meta (buffer);
if (meta == NULL) {
GST_DEBUG ("Received buffer is not a GstNetBuffer, skipping");
return GST_FLOW_OK;
}
sink = GST_DYNUDPSINK (bsink);
2012-01-17 08:32:27 +00:00
/* let's get the address from the metadata */
addr = meta->addr;
2012-01-17 08:38:33 +00:00
family = g_socket_address_get_family (addr);
if (sink->family != family && family != G_SOCKET_FAMILY_IPV4)
2012-01-17 08:32:27 +00:00
goto invalid_family;
2012-01-24 13:38:58 +00:00
gst_buffer_map (buffer, &map, GST_MAP_READ);
2012-01-24 13:38:58 +00:00
GST_DEBUG ("about to send %" G_GSIZE_FORMAT " bytes", map.size);
2012-01-17 08:32:27 +00:00
#ifndef GST_DISABLE_GST_DEBUG
{
gchar *host;
host =
g_inet_address_to_string (g_inet_socket_address_get_address
(G_INET_SOCKET_ADDRESS (addr)));
2012-01-24 13:38:58 +00:00
GST_DEBUG ("sending %" G_GSIZE_FORMAT " bytes to client %s port %d",
map.size, host,
g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
2012-01-17 08:32:27 +00:00
g_free (host);
}
#endif
2012-01-17 08:32:27 +00:00
ret =
2012-01-24 13:38:58 +00:00
g_socket_send_to (sink->used_socket, addr, (gchar *) map.data, map.size,
2012-01-17 08:32:27 +00:00
sink->cancellable, &err);
2012-01-24 13:38:58 +00:00
gst_buffer_unmap (buffer, &map);
2011-04-05 15:06:41 +00:00
2012-01-17 08:32:27 +00:00
if (ret < 0)
goto send_error;
2012-01-17 08:32:27 +00:00
GST_DEBUG ("sent %" G_GSSIZE_FORMAT " bytes", ret);
return GST_FLOW_OK;
send_error:
{
2012-01-17 08:32:27 +00:00
GST_DEBUG ("got send error %s", err->message);
g_clear_error (&err);
return GST_FLOW_ERROR;
}
invalid_family:
{
GST_DEBUG ("invalid family (got %d, expected %d)",
g_socket_address_get_family (addr), sink->family);
return GST_FLOW_ERROR;
}
}
static void
gst_dynudpsink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstDynUDPSink *udpsink;
udpsink = GST_DYNUDPSINK (object);
switch (prop_id) {
2012-01-17 08:32:27 +00:00
case PROP_SOCKET:
if (udpsink->socket != NULL && udpsink->socket != udpsink->used_socket &&
udpsink->close_socket) {
GError *err = NULL;
if (!g_socket_close (udpsink->socket, &err)) {
GST_ERROR ("failed to close socket %p: %s", udpsink->socket,
err->message);
g_clear_error (&err);
}
}
if (udpsink->socket)
g_object_unref (udpsink->socket);
udpsink->socket = g_value_dup_object (value);
GST_DEBUG ("setting socket to %p", udpsink->socket);
break;
2012-01-17 08:32:27 +00:00
case PROP_CLOSE_SOCKET:
udpsink->close_socket = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_dynudpsink_get_property (GObject * object, guint prop_id, GValue * value,
GParamSpec * pspec)
{
GstDynUDPSink *udpsink;
udpsink = GST_DYNUDPSINK (object);
switch (prop_id) {
2012-01-17 08:32:27 +00:00
case PROP_SOCKET:
g_value_set_object (value, udpsink->socket);
break;
2012-01-17 08:32:27 +00:00
case PROP_CLOSE_SOCKET:
g_value_set_boolean (value, udpsink->close_socket);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/* create a socket for sending to remote machine */
static gboolean
2012-01-17 08:32:27 +00:00
gst_dynudpsink_start (GstBaseSink * bsink)
{
2012-01-17 08:32:27 +00:00
GstDynUDPSink *udpsink;
GError *err = NULL;
udpsink = GST_DYNUDPSINK (bsink);
2012-01-17 08:32:27 +00:00
if (udpsink->socket == NULL) {
2012-01-17 08:38:33 +00:00
/* create sender socket if none available, first try IPv6, then
* fall-back to IPv4 */
udpsink->family = G_SOCKET_FAMILY_IPV6;
2012-01-17 08:32:27 +00:00
if ((udpsink->used_socket =
2012-01-17 08:38:33 +00:00
g_socket_new (G_SOCKET_FAMILY_IPV6,
G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &err)) == NULL) {
udpsink->family = G_SOCKET_FAMILY_IPV4;
if ((udpsink->used_socket = g_socket_new (G_SOCKET_FAMILY_IPV4,
G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &err)) == NULL)
goto no_socket;
}
2012-01-17 08:32:27 +00:00
udpsink->external_socket = FALSE;
} else {
2012-01-17 08:32:27 +00:00
udpsink->used_socket = G_SOCKET (g_object_ref (udpsink->socket));
udpsink->external_socket = TRUE;
2012-01-17 08:38:33 +00:00
udpsink->family = g_socket_get_family (udpsink->used_socket);
}
2012-01-17 08:32:27 +00:00
2012-01-17 08:38:33 +00:00
g_socket_set_broadcast (udpsink->used_socket, TRUE);
return TRUE;
/* ERRORS */
no_socket:
{
2012-01-17 08:32:27 +00:00
GST_ERROR_OBJECT (udpsink, "Failed to create socket: %s", err->message);
g_clear_error (&err);
return FALSE;
}
}
static GstStructure *
gst_dynudpsink_get_stats (GstDynUDPSink * sink, const gchar * host, gint port)
{
return NULL;
}
2012-01-17 08:32:27 +00:00
static gboolean
gst_dynudpsink_stop (GstBaseSink * bsink)
{
2012-01-17 08:32:27 +00:00
GstDynUDPSink *udpsink;
udpsink = GST_DYNUDPSINK (bsink);
if (udpsink->used_socket) {
if (udpsink->close_socket || !udpsink->external_socket) {
GError *err = NULL;
if (!g_socket_close (udpsink->used_socket, &err)) {
GST_ERROR_OBJECT (udpsink, "Failed to close socket: %s", err->message);
g_clear_error (&err);
}
}
g_object_unref (udpsink->used_socket);
udpsink->used_socket = NULL;
}
return TRUE;
}
2012-01-17 08:32:27 +00:00
static gboolean
gst_dynudpsink_unlock (GstBaseSink * bsink)
{
2012-01-17 08:32:27 +00:00
GstDynUDPSink *udpsink;
2012-01-17 08:32:27 +00:00
udpsink = GST_DYNUDPSINK (bsink);
2012-01-17 08:32:27 +00:00
g_cancellable_cancel (udpsink->cancellable);
2012-01-17 08:32:27 +00:00
return TRUE;
}
2012-01-17 08:32:27 +00:00
static gboolean
gst_dynudpsink_unlock_stop (GstBaseSink * bsink)
{
GstDynUDPSink *udpsink;
2012-01-17 08:32:27 +00:00
udpsink = GST_DYNUDPSINK (bsink);
g_cancellable_reset (udpsink->cancellable);
return TRUE;
}