mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-11-18 15:51:11 +00:00
d143d256a6
Original commit message from CVS: more autistic cleanliness in functions/names/defines
510 lines
14 KiB
C
510 lines
14 KiB
C
/* GStreamer
|
|
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
|
* Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
|
|
*
|
|
* 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.
|
|
*/
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
#include "config.h"
|
|
#endif
|
|
|
|
#include <gst/gst-i18n-plugin.h>
|
|
#include "gsttcp.h"
|
|
#include "gsttcpclientsrc.h"
|
|
#include <string.h> /* memset */
|
|
#include <unistd.h>
|
|
#include <arpa/inet.h>
|
|
#include <sys/ioctl.h>
|
|
|
|
#ifdef HAVE_FIONREAD_IN_SYS_FILIO
|
|
#include <sys/filio.h>
|
|
#endif
|
|
|
|
GST_DEBUG_CATEGORY (tcpclientsrc_debug);
|
|
#define GST_CAT_DEFAULT tcpclientsrc_debug
|
|
|
|
#define MAX_READ_SIZE 4 * 1024
|
|
|
|
/* elementfactory information */
|
|
static GstElementDetails gst_tcpclientsrc_details =
|
|
GST_ELEMENT_DETAILS ("TCP Client source",
|
|
"Source/Network",
|
|
"Receive data as a client over the network via TCP",
|
|
"Thomas Vander Stichele <thomas at apestaart dot org>");
|
|
|
|
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
|
|
GST_PAD_SRC,
|
|
GST_PAD_ALWAYS,
|
|
GST_STATIC_CAPS_ANY);
|
|
|
|
/* TCPClientSrc signals and args */
|
|
enum
|
|
{
|
|
LAST_SIGNAL
|
|
};
|
|
|
|
enum
|
|
{
|
|
ARG_0,
|
|
ARG_HOST,
|
|
ARG_PORT,
|
|
ARG_PROTOCOL
|
|
};
|
|
|
|
static void gst_tcpclientsrc_base_init (gpointer g_class);
|
|
static void gst_tcpclientsrc_class_init (GstTCPClientSrc * klass);
|
|
static void gst_tcpclientsrc_init (GstTCPClientSrc * tcpclientsrc);
|
|
static void gst_tcpclientsrc_finalize (GObject * gobject);
|
|
|
|
static GstCaps *gst_tcpclientsrc_getcaps (GstBaseSrc * psrc);
|
|
|
|
static GstFlowReturn gst_tcpclientsrc_create (GstPushSrc * psrc,
|
|
GstBuffer ** outbuf);
|
|
static gboolean gst_tcpclientsrc_stop (GstBaseSrc * bsrc);
|
|
static gboolean gst_tcpclientsrc_start (GstBaseSrc * bsrc);
|
|
static gboolean gst_tcpclientsrc_unlock (GstBaseSrc * bsrc);
|
|
|
|
static void gst_tcpclientsrc_set_property (GObject * object, guint prop_id,
|
|
const GValue * value, GParamSpec * pspec);
|
|
static void gst_tcpclientsrc_get_property (GObject * object, guint prop_id,
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
static GstElementClass *parent_class = NULL;
|
|
|
|
/*static guint gst_tcpclientsrc_signals[LAST_SIGNAL] = { 0 }; */
|
|
|
|
GType
|
|
gst_tcpclientsrc_get_type (void)
|
|
{
|
|
static GType tcpclientsrc_type = 0;
|
|
|
|
if (!tcpclientsrc_type) {
|
|
static const GTypeInfo tcpclientsrc_info = {
|
|
sizeof (GstTCPClientSrcClass),
|
|
gst_tcpclientsrc_base_init,
|
|
NULL,
|
|
(GClassInitFunc) gst_tcpclientsrc_class_init,
|
|
NULL,
|
|
NULL,
|
|
sizeof (GstTCPClientSrc),
|
|
0,
|
|
(GInstanceInitFunc) gst_tcpclientsrc_init,
|
|
NULL
|
|
};
|
|
|
|
tcpclientsrc_type =
|
|
g_type_register_static (GST_TYPE_PUSH_SRC, "GstTCPClientSrc",
|
|
&tcpclientsrc_info, 0);
|
|
}
|
|
return tcpclientsrc_type;
|
|
}
|
|
|
|
static void
|
|
gst_tcpclientsrc_base_init (gpointer g_class)
|
|
{
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
gst_static_pad_template_get (&srctemplate));
|
|
|
|
gst_element_class_set_details (element_class, &gst_tcpclientsrc_details);
|
|
}
|
|
|
|
static void
|
|
gst_tcpclientsrc_class_init (GstTCPClientSrc * klass)
|
|
{
|
|
GObjectClass *gobject_class;
|
|
GstElementClass *gstelement_class;
|
|
GstBaseSrcClass *gstbasesrc_class;
|
|
GstPushSrcClass *gstpush_src_class;
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
gstelement_class = (GstElementClass *) klass;
|
|
gstbasesrc_class = (GstBaseSrcClass *) klass;
|
|
gstpush_src_class = (GstPushSrcClass *) klass;
|
|
|
|
parent_class = g_type_class_ref (GST_TYPE_PUSH_SRC);
|
|
|
|
gobject_class->set_property = gst_tcpclientsrc_set_property;
|
|
gobject_class->get_property = gst_tcpclientsrc_get_property;
|
|
gobject_class->finalize = gst_tcpclientsrc_finalize;
|
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_HOST,
|
|
g_param_spec_string ("host", "Host",
|
|
"The host IP address to receive packets from", TCP_DEFAULT_HOST,
|
|
G_PARAM_READWRITE));
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PORT,
|
|
g_param_spec_int ("port", "Port", "The port to receive packets from", 0,
|
|
TCP_HIGHEST_PORT, TCP_DEFAULT_PORT, G_PARAM_READWRITE));
|
|
g_object_class_install_property (gobject_class, ARG_PROTOCOL,
|
|
g_param_spec_enum ("protocol", "Protocol", "The protocol to wrap data in",
|
|
GST_TYPE_TCP_PROTOCOL_TYPE, GST_TCP_PROTOCOL_TYPE_NONE,
|
|
G_PARAM_READWRITE));
|
|
|
|
gstbasesrc_class->get_caps = gst_tcpclientsrc_getcaps;
|
|
gstbasesrc_class->start = gst_tcpclientsrc_start;
|
|
gstbasesrc_class->stop = gst_tcpclientsrc_stop;
|
|
gstbasesrc_class->unlock = gst_tcpclientsrc_unlock;
|
|
|
|
gstpush_src_class->create = gst_tcpclientsrc_create;
|
|
|
|
GST_DEBUG_CATEGORY_INIT (tcpclientsrc_debug, "tcpclientsrc", 0,
|
|
"TCP Client Source");
|
|
}
|
|
|
|
static void
|
|
gst_tcpclientsrc_init (GstTCPClientSrc * this)
|
|
{
|
|
this->port = TCP_DEFAULT_PORT;
|
|
this->host = g_strdup (TCP_DEFAULT_HOST);
|
|
this->sock_fd = -1;
|
|
this->protocol = GST_TCP_PROTOCOL_TYPE_NONE;
|
|
this->caps = NULL;
|
|
this->curoffset = 0;
|
|
|
|
gst_base_src_set_live (GST_BASE_SRC (this), TRUE);
|
|
|
|
GST_FLAG_UNSET (this, GST_TCPCLIENTSRC_OPEN);
|
|
}
|
|
|
|
static void
|
|
gst_tcpclientsrc_finalize (GObject * gobject)
|
|
{
|
|
GstTCPClientSrc *this = GST_TCPCLIENTSRC (gobject);
|
|
|
|
g_free (this->host);
|
|
}
|
|
|
|
static GstCaps *
|
|
gst_tcpclientsrc_getcaps (GstBaseSrc * bsrc)
|
|
{
|
|
GstTCPClientSrc *src;
|
|
GstCaps *caps = NULL;
|
|
|
|
src = GST_TCPCLIENTSRC (bsrc);
|
|
|
|
if (!GST_FLAG_IS_SET (src, GST_TCPCLIENTSRC_OPEN))
|
|
caps = gst_caps_new_any ();
|
|
else if (src->caps)
|
|
caps = gst_caps_copy (src->caps);
|
|
else
|
|
caps = gst_caps_new_any ();
|
|
GST_DEBUG_OBJECT (src, "returning caps %" GST_PTR_FORMAT, caps);
|
|
g_assert (GST_IS_CAPS (caps));
|
|
return caps;
|
|
}
|
|
|
|
static GstFlowReturn
|
|
gst_tcpclientsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf)
|
|
{
|
|
GstTCPClientSrc *src;
|
|
size_t readsize;
|
|
int ret;
|
|
GstBuffer *buf = NULL;
|
|
|
|
src = GST_TCPCLIENTSRC (psrc);
|
|
|
|
if (!GST_FLAG_IS_SET (src, GST_TCPCLIENTSRC_OPEN))
|
|
goto wrong_state;
|
|
|
|
GST_LOG_OBJECT (src, "asked for a buffer");
|
|
|
|
/* read the buffer header if we're using a protocol */
|
|
switch (src->protocol) {
|
|
fd_set testfds;
|
|
|
|
case GST_TCP_PROTOCOL_TYPE_NONE:
|
|
/* do a blocking select on the socket */
|
|
FD_ZERO (&testfds);
|
|
FD_SET (src->sock_fd, &testfds);
|
|
|
|
/* no action (0) is an error too in our case */
|
|
if ((ret = select (src->sock_fd + 1, &testfds, NULL, NULL, 0)) <= 0)
|
|
goto select_error;
|
|
|
|
/* ask how much is available for reading on the socket */
|
|
if ((ret = ioctl (src->sock_fd, FIONREAD, &readsize)) < 0)
|
|
goto ioctl_error;
|
|
|
|
GST_LOG_OBJECT (src, "ioctl says %d bytes available", readsize);
|
|
|
|
buf = gst_buffer_new_and_alloc (readsize);
|
|
break;
|
|
|
|
case GST_TCP_PROTOCOL_TYPE_GDP:
|
|
if (!(buf = gst_tcp_gdp_read_buffer (GST_ELEMENT (src), src->sock_fd)))
|
|
goto hit_eos;
|
|
|
|
GST_LOG_OBJECT (src, "Going to read data from socket into buffer %p",
|
|
buf);
|
|
|
|
/* use this new buffer to read data into */
|
|
readsize = GST_BUFFER_SIZE (buf);
|
|
break;
|
|
default:
|
|
/* need to assert as buf == NULL */
|
|
g_assert ("Unhandled protocol type");
|
|
break;
|
|
}
|
|
|
|
GST_LOG_OBJECT (src, "Reading %d bytes into buffer", readsize);
|
|
if ((ret =
|
|
gst_tcp_socket_read (src->sock_fd, GST_BUFFER_DATA (buf),
|
|
readsize)) < 0)
|
|
goto read_error;
|
|
|
|
/* if we read 0 bytes, and we're blocking, we hit eos */
|
|
if (ret == 0)
|
|
goto zero_read;
|
|
|
|
readsize = ret;
|
|
GST_BUFFER_SIZE (buf) = readsize;
|
|
|
|
src->curoffset += readsize;
|
|
|
|
GST_LOG_OBJECT (src,
|
|
"Returning buffer from _get of size %d, ts %"
|
|
GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
|
|
", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
|
|
GST_BUFFER_SIZE (buf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
|
|
GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
|
|
GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf));
|
|
|
|
gst_buffer_set_caps (buf, src->caps);
|
|
|
|
*outbuf = buf;
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
/* ERRORS */
|
|
wrong_state:
|
|
{
|
|
GST_DEBUG_OBJECT (src, "connection to server closed, cannot give data");
|
|
return GST_FLOW_WRONG_STATE;
|
|
}
|
|
select_error:
|
|
{
|
|
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
|
|
("select failed: %s", g_strerror (errno)));
|
|
return GST_FLOW_ERROR;
|
|
}
|
|
ioctl_error:
|
|
{
|
|
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
|
|
("ioctl failed: %s", g_strerror (errno)));
|
|
return GST_FLOW_ERROR;
|
|
}
|
|
hit_eos:
|
|
{
|
|
return GST_FLOW_WRONG_STATE;
|
|
}
|
|
read_error:
|
|
{
|
|
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
|
|
gst_buffer_unref (buf);
|
|
return GST_FLOW_ERROR;
|
|
}
|
|
zero_read:
|
|
{
|
|
GST_DEBUG_OBJECT (src, "blocking read returns 0, EOS");
|
|
gst_buffer_unref (buf);
|
|
return GST_FLOW_WRONG_STATE;
|
|
}
|
|
}
|
|
|
|
static void
|
|
gst_tcpclientsrc_set_property (GObject * object, guint prop_id,
|
|
const GValue * value, GParamSpec * pspec)
|
|
{
|
|
GstTCPClientSrc *tcpclientsrc;
|
|
|
|
g_return_if_fail (GST_IS_TCPCLIENTSRC (object));
|
|
tcpclientsrc = GST_TCPCLIENTSRC (object);
|
|
|
|
switch (prop_id) {
|
|
case ARG_HOST:
|
|
if (!g_value_get_string (value)) {
|
|
g_warning ("host property cannot be NULL");
|
|
break;
|
|
}
|
|
g_free (tcpclientsrc->host);
|
|
tcpclientsrc->host = g_strdup (g_value_get_string (value));
|
|
break;
|
|
case ARG_PORT:
|
|
tcpclientsrc->port = g_value_get_int (value);
|
|
break;
|
|
case ARG_PROTOCOL:
|
|
tcpclientsrc->protocol = g_value_get_enum (value);
|
|
break;
|
|
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void
|
|
gst_tcpclientsrc_get_property (GObject * object, guint prop_id, GValue * value,
|
|
GParamSpec * pspec)
|
|
{
|
|
GstTCPClientSrc *tcpclientsrc;
|
|
|
|
g_return_if_fail (GST_IS_TCPCLIENTSRC (object));
|
|
tcpclientsrc = GST_TCPCLIENTSRC (object);
|
|
|
|
switch (prop_id) {
|
|
case ARG_HOST:
|
|
g_value_set_string (value, tcpclientsrc->host);
|
|
break;
|
|
case ARG_PORT:
|
|
g_value_set_int (value, tcpclientsrc->port);
|
|
break;
|
|
case ARG_PROTOCOL:
|
|
g_value_set_enum (value, tcpclientsrc->protocol);
|
|
break;
|
|
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* create a socket for connecting to remote server */
|
|
static gboolean
|
|
gst_tcpclientsrc_start (GstBaseSrc * bsrc)
|
|
{
|
|
int ret;
|
|
gchar *ip;
|
|
GstTCPClientSrc *src = GST_TCPCLIENTSRC (bsrc);
|
|
|
|
/* create receiving client socket */
|
|
GST_DEBUG_OBJECT (src, "opening receiving client socket to %s:%d",
|
|
src->host, src->port);
|
|
|
|
if ((src->sock_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
|
|
goto no_socket;
|
|
|
|
GST_DEBUG_OBJECT (src, "opened receiving client socket with fd %d",
|
|
src->sock_fd);
|
|
GST_FLAG_SET (src, GST_TCPCLIENTSRC_OPEN);
|
|
|
|
/* look up name if we need to */
|
|
if (!(ip = gst_tcp_host_to_ip (GST_ELEMENT (src), src->host)))
|
|
goto name_resolv;
|
|
|
|
GST_DEBUG_OBJECT (src, "IP address for host %s is %s", src->host, ip);
|
|
|
|
/* connect to server */
|
|
memset (&src->server_sin, 0, sizeof (src->server_sin));
|
|
src->server_sin.sin_family = AF_INET; /* network socket */
|
|
src->server_sin.sin_port = htons (src->port); /* on port */
|
|
src->server_sin.sin_addr.s_addr = inet_addr (ip); /* on host ip */
|
|
g_free (ip);
|
|
|
|
GST_DEBUG_OBJECT (src, "connecting to server");
|
|
ret = connect (src->sock_fd, (struct sockaddr *) &src->server_sin,
|
|
sizeof (src->server_sin));
|
|
|
|
if (ret) {
|
|
gst_tcpclientsrc_stop (GST_BASE_SRC (src));
|
|
switch (errno) {
|
|
case ECONNREFUSED:
|
|
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
|
|
(_("Connection to %s:%d refused."), src->host, src->port), (NULL));
|
|
return FALSE;
|
|
break;
|
|
default:
|
|
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
|
|
("connect to %s:%d failed: %s", src->host, src->port,
|
|
g_strerror (errno)));
|
|
return FALSE;
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* get the caps if we're using GDP */
|
|
if (src->protocol == GST_TCP_PROTOCOL_TYPE_GDP) {
|
|
/* if we haven't received caps yet, we should get them first */
|
|
if (!src->caps_received) {
|
|
GstCaps *caps;
|
|
|
|
GST_DEBUG_OBJECT (src, "getting caps through GDP");
|
|
if (!(caps = gst_tcp_gdp_read_caps (GST_ELEMENT (src), src->sock_fd)))
|
|
goto no_caps;
|
|
|
|
if (!GST_IS_CAPS (caps))
|
|
goto no_caps;
|
|
|
|
GST_DEBUG_OBJECT (src, "Received caps through GDP: %" GST_PTR_FORMAT,
|
|
caps);
|
|
|
|
src->caps_received = TRUE;
|
|
src->caps = caps;
|
|
}
|
|
}
|
|
return TRUE;
|
|
|
|
no_socket:
|
|
{
|
|
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
|
|
return FALSE;
|
|
}
|
|
name_resolv:
|
|
{
|
|
gst_tcpclientsrc_stop (GST_BASE_SRC (src));
|
|
return FALSE;
|
|
}
|
|
no_caps:
|
|
{
|
|
gst_tcpclientsrc_stop (GST_BASE_SRC (src));
|
|
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
|
|
("Could not read caps through GDP"));
|
|
return FALSE;
|
|
}
|
|
}
|
|
|
|
/* close the socket and associated resources
|
|
* unset OPEN flag
|
|
* used both to recover from errors and go to NULL state */
|
|
static gboolean
|
|
gst_tcpclientsrc_stop (GstBaseSrc * bsrc)
|
|
{
|
|
GstTCPClientSrc *src;
|
|
|
|
src = GST_TCPCLIENTSRC (bsrc);
|
|
|
|
GST_DEBUG_OBJECT (src, "closing socket");
|
|
if (src->sock_fd != -1) {
|
|
close (src->sock_fd);
|
|
src->sock_fd = -1;
|
|
}
|
|
src->caps_received = FALSE;
|
|
if (src->caps) {
|
|
gst_caps_unref (src->caps);
|
|
src->caps = NULL;
|
|
}
|
|
GST_FLAG_UNSET (src, GST_TCPCLIENTSRC_OPEN);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
static gboolean
|
|
gst_tcpclientsrc_unlock (GstBaseSrc * bsrc)
|
|
{
|
|
return TRUE;
|
|
}
|