mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2025-04-23 17:14:23 +00:00
gst/elements/gstfdsrc.*: Properly implement fdsrc. Removed signal and timeout, better implemented somewhere else.
Original commit message from CVS: * gst/elements/gstfdsrc.c: (gst_fdsrc_class_init), (gst_fdsrc_init), (gst_fdsrc_start), (gst_fdsrc_stop), (gst_fdsrc_unlock), (gst_fdsrc_set_property), (gst_fdsrc_get_property), (gst_fdsrc_create): * gst/elements/gstfdsrc.h: Properly implement fdsrc. Removed signal and timeout, better implemented somewhere else.
This commit is contained in:
parent
0371ed76ee
commit
71491998d5
5 changed files with 280 additions and 212 deletions
10
ChangeLog
10
ChangeLog
|
@ -1,3 +1,13 @@
|
||||||
|
2005-09-21 Wim Taymans <wim@fluendo.com>
|
||||||
|
|
||||||
|
* gst/elements/gstfdsrc.c: (gst_fdsrc_class_init),
|
||||||
|
(gst_fdsrc_init), (gst_fdsrc_start), (gst_fdsrc_stop),
|
||||||
|
(gst_fdsrc_unlock), (gst_fdsrc_set_property),
|
||||||
|
(gst_fdsrc_get_property), (gst_fdsrc_create):
|
||||||
|
* gst/elements/gstfdsrc.h:
|
||||||
|
Properly implement fdsrc. Removed signal and timeout,
|
||||||
|
better implemented somewhere else.
|
||||||
|
|
||||||
2005-09-21 Stefan Kost <ensonic@users.sf.net>
|
2005-09-21 Stefan Kost <ensonic@users.sf.net>
|
||||||
|
|
||||||
* docs/gst/tmpl/.cvsignore:
|
* docs/gst/tmpl/.cvsignore:
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#ifdef HAVE_UNISTD_H
|
#ifdef HAVE_UNISTD_H
|
||||||
|
@ -38,6 +39,24 @@
|
||||||
|
|
||||||
#include "gstfdsrc.h"
|
#include "gstfdsrc.h"
|
||||||
|
|
||||||
|
/* the select call is also performed on the control sockets, that way
|
||||||
|
* we can send special commands to unblock the select call */
|
||||||
|
#define CONTROL_STOP 'S' /* stop the select call */
|
||||||
|
#define CONTROL_SOCKETS(src) src->control_sock
|
||||||
|
#define WRITE_SOCKET(src) src->control_sock[1]
|
||||||
|
#define READ_SOCKET(src) src->control_sock[0]
|
||||||
|
|
||||||
|
#define SEND_COMMAND(src, command) \
|
||||||
|
G_STMT_START { \
|
||||||
|
unsigned char c; c = command; \
|
||||||
|
write (WRITE_SOCKET(src), &c, 1); \
|
||||||
|
} G_STMT_END
|
||||||
|
|
||||||
|
#define READ_COMMAND(src, command, res) \
|
||||||
|
G_STMT_START { \
|
||||||
|
res = read(READ_SOCKET(src), &command, 1); \
|
||||||
|
} G_STMT_END
|
||||||
|
|
||||||
#define DEFAULT_BLOCKSIZE 4096
|
#define DEFAULT_BLOCKSIZE 4096
|
||||||
|
|
||||||
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
|
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
|
||||||
|
@ -69,8 +88,6 @@ enum
|
||||||
ARG_TIMEOUT
|
ARG_TIMEOUT
|
||||||
};
|
};
|
||||||
|
|
||||||
static guint gst_fdsrc_signals[LAST_SIGNAL] = { 0 };
|
|
||||||
|
|
||||||
#define _do_init(bla) \
|
#define _do_init(bla) \
|
||||||
GST_DEBUG_CATEGORY_INIT (gst_fdsrc_debug, "fdsrc", 0, "fdsrc element");
|
GST_DEBUG_CATEGORY_INIT (gst_fdsrc_debug, "fdsrc", 0, "fdsrc element");
|
||||||
|
|
||||||
|
@ -82,8 +99,9 @@ static void gst_fdsrc_set_property (GObject * object, guint prop_id,
|
||||||
static void gst_fdsrc_get_property (GObject * object, guint prop_id,
|
static void gst_fdsrc_get_property (GObject * object, guint prop_id,
|
||||||
GValue * value, GParamSpec * pspec);
|
GValue * value, GParamSpec * pspec);
|
||||||
|
|
||||||
static GstStateChangeReturn gst_fdsrc_change_state (GstElement * element,
|
static gboolean gst_fdsrc_start (GstBaseSrc * bsrc);
|
||||||
GstStateChange transition);
|
static gboolean gst_fdsrc_stop (GstBaseSrc * bsrc);
|
||||||
|
static gboolean gst_fdsrc_unlock (GstBaseSrc * bsrc);
|
||||||
|
|
||||||
static GstFlowReturn gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf);
|
static GstFlowReturn gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf);
|
||||||
|
|
||||||
|
@ -101,10 +119,11 @@ gst_fdsrc_class_init (GstFdSrcClass * klass)
|
||||||
{
|
{
|
||||||
GObjectClass *gobject_class;
|
GObjectClass *gobject_class;
|
||||||
GstBaseSrcClass *gstbasesrc_class;
|
GstBaseSrcClass *gstbasesrc_class;
|
||||||
GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
|
GstElementClass *gstelement_class;
|
||||||
GstPushSrcClass *gstpush_src_class;
|
GstPushSrcClass *gstpush_src_class;
|
||||||
|
|
||||||
gobject_class = G_OBJECT_CLASS (klass);
|
gobject_class = G_OBJECT_CLASS (klass);
|
||||||
|
gstelement_class = GST_ELEMENT_CLASS (klass);
|
||||||
gstbasesrc_class = (GstBaseSrcClass *) klass;
|
gstbasesrc_class = (GstBaseSrcClass *) klass;
|
||||||
gstpush_src_class = (GstPushSrcClass *) klass;
|
gstpush_src_class = (GstPushSrcClass *) klass;
|
||||||
|
|
||||||
|
@ -116,23 +135,12 @@ gst_fdsrc_class_init (GstFdSrcClass * klass)
|
||||||
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FD,
|
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FD,
|
||||||
g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
|
g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
|
||||||
0, G_MAXINT, 0, G_PARAM_READWRITE));
|
0, G_MAXINT, 0, G_PARAM_READWRITE));
|
||||||
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BLOCKSIZE,
|
|
||||||
g_param_spec_ulong ("blocksize", "Block size",
|
|
||||||
"Size in bytes to read per buffer", 1, G_MAXULONG, DEFAULT_BLOCKSIZE,
|
|
||||||
G_PARAM_READWRITE));
|
|
||||||
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TIMEOUT,
|
|
||||||
g_param_spec_uint64 ("timeout", "Timeout", "Read timeout in nanoseconds",
|
|
||||||
0, G_MAXUINT64, 0, G_PARAM_READWRITE));
|
|
||||||
|
|
||||||
gst_fdsrc_signals[SIGNAL_TIMEOUT] =
|
gstbasesrc_class->start = gst_fdsrc_start;
|
||||||
g_signal_new ("timeout", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
|
gstbasesrc_class->stop = gst_fdsrc_stop;
|
||||||
G_STRUCT_OFFSET (GstFdSrcClass, timeout), NULL, NULL,
|
gstbasesrc_class->unlock = gst_fdsrc_unlock;
|
||||||
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
|
|
||||||
|
|
||||||
gstelement_class->change_state = gst_fdsrc_change_state;
|
|
||||||
|
|
||||||
gstpush_src_class->create = gst_fdsrc_create;
|
gstpush_src_class->create = gst_fdsrc_create;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -144,36 +152,57 @@ gst_fdsrc_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
|
||||||
|
|
||||||
fdsrc->fd = 0;
|
fdsrc->fd = 0;
|
||||||
fdsrc->curoffset = 0;
|
fdsrc->curoffset = 0;
|
||||||
fdsrc->blocksize = DEFAULT_BLOCKSIZE;
|
|
||||||
fdsrc->timeout = 0;
|
|
||||||
fdsrc->seq = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static GstStateChangeReturn
|
|
||||||
gst_fdsrc_change_state (GstElement * element, GstStateChange transition)
|
static gboolean
|
||||||
|
gst_fdsrc_start (GstBaseSrc * bsrc)
|
||||||
{
|
{
|
||||||
GstFdSrc *src = GST_FDSRC (element);
|
GstFdSrc *src = GST_FDSRC (bsrc);
|
||||||
|
gint control_sock[2];
|
||||||
|
|
||||||
switch (transition) {
|
src->curoffset = 0;
|
||||||
case GST_STATE_CHANGE_NULL_TO_READY:
|
|
||||||
break;
|
if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
|
||||||
case GST_STATE_CHANGE_READY_TO_NULL:
|
goto socket_pair;
|
||||||
break;
|
|
||||||
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
READ_SOCKET (src) = control_sock[0];
|
||||||
src->curoffset = 0;
|
WRITE_SOCKET (src) = control_sock[1];
|
||||||
break;
|
|
||||||
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
|
||||||
break;
|
fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
|
||||||
default:
|
|
||||||
break;
|
return TRUE;
|
||||||
|
|
||||||
|
/* ERRORS */
|
||||||
|
socket_pair:
|
||||||
|
{
|
||||||
|
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
|
||||||
|
GST_ERROR_SYSTEM);
|
||||||
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (GST_ELEMENT_CLASS (parent_class)->change_state)
|
|
||||||
return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
||||||
|
|
||||||
return GST_STATE_CHANGE_SUCCESS;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static gboolean
|
||||||
|
gst_fdsrc_stop (GstBaseSrc * bsrc)
|
||||||
|
{
|
||||||
|
GstFdSrc *src = GST_FDSRC (bsrc);
|
||||||
|
|
||||||
|
close (READ_SOCKET (src));
|
||||||
|
close (WRITE_SOCKET (src));
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean
|
||||||
|
gst_fdsrc_unlock (GstBaseSrc * bsrc)
|
||||||
|
{
|
||||||
|
GstFdSrc *src = GST_FDSRC (bsrc);
|
||||||
|
|
||||||
|
SEND_COMMAND (src, CONTROL_STOP);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
gst_fdsrc_set_property (GObject * object, guint prop_id, const GValue * value,
|
gst_fdsrc_set_property (GObject * object, guint prop_id, const GValue * value,
|
||||||
|
@ -189,12 +218,6 @@ gst_fdsrc_set_property (GObject * object, guint prop_id, const GValue * value,
|
||||||
case ARG_FD:
|
case ARG_FD:
|
||||||
src->fd = g_value_get_int (value);
|
src->fd = g_value_get_int (value);
|
||||||
break;
|
break;
|
||||||
case ARG_BLOCKSIZE:
|
|
||||||
src->blocksize = g_value_get_ulong (value);
|
|
||||||
break;
|
|
||||||
case ARG_TIMEOUT:
|
|
||||||
src->timeout = g_value_get_uint64 (value);
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
||||||
break;
|
break;
|
||||||
|
@ -212,34 +235,25 @@ gst_fdsrc_get_property (GObject * object, guint prop_id, GValue * value,
|
||||||
src = GST_FDSRC (object);
|
src = GST_FDSRC (object);
|
||||||
|
|
||||||
switch (prop_id) {
|
switch (prop_id) {
|
||||||
case ARG_BLOCKSIZE:
|
|
||||||
g_value_set_ulong (value, src->blocksize);
|
|
||||||
break;
|
|
||||||
case ARG_FD:
|
case ARG_FD:
|
||||||
g_value_set_int (value, src->fd);
|
g_value_set_int (value, src->fd);
|
||||||
break;
|
break;
|
||||||
case ARG_TIMEOUT:
|
|
||||||
g_value_set_uint64 (value, src->timeout);
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#define SELECT_TIMEOUT (GST_SECOND / 20)
|
|
||||||
|
|
||||||
static GstFlowReturn
|
static GstFlowReturn
|
||||||
gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf)
|
gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf)
|
||||||
{
|
{
|
||||||
GstFdSrc *src;
|
GstFdSrc *src;
|
||||||
GstBuffer *buf;
|
GstBuffer *buf;
|
||||||
glong readbytes;
|
glong readbytes;
|
||||||
GstClockTime timeout;
|
guint blocksize;
|
||||||
|
|
||||||
#ifndef HAVE_WIN32
|
#ifndef HAVE_WIN32
|
||||||
fd_set readfds;
|
fd_set readfds;
|
||||||
struct timeval t, *tp = &t;
|
|
||||||
gint retval;
|
gint retval;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -248,65 +262,82 @@ gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf)
|
||||||
#ifndef HAVE_WIN32
|
#ifndef HAVE_WIN32
|
||||||
FD_ZERO (&readfds);
|
FD_ZERO (&readfds);
|
||||||
FD_SET (src->fd, &readfds);
|
FD_SET (src->fd, &readfds);
|
||||||
|
FD_SET (READ_SOCKET (src), &readfds);
|
||||||
if (src->timeout != 0) {
|
|
||||||
timeout = MIN (SELECT_TIMEOUT, src->timeout);
|
|
||||||
} else {
|
|
||||||
timeout = SELECT_TIMEOUT;
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
GST_TIME_TO_TIMEVAL (timeout, t);
|
retval = select (FD_SETSIZE, &readfds, NULL, NULL, NULL);
|
||||||
if (src->timeout != 0)
|
} while ((retval == -1 && errno == EINTR));
|
||||||
timeout -= MIN (timeout, SELECT_TIMEOUT);
|
|
||||||
|
|
||||||
retval = select (src->fd + 1, &readfds, NULL, NULL, tp);
|
if (retval == -1)
|
||||||
|
goto select_error;
|
||||||
|
|
||||||
/* Check whether the element got shutdown before full timeout */
|
if (FD_ISSET (READ_SOCKET (src), &readfds)) {
|
||||||
if (retval == 0) {
|
/* read all stop commands */
|
||||||
if (GST_PAD_IS_FLUSHING (GST_BASE_SRC_PAD (src))) {
|
while (TRUE) {
|
||||||
GST_DEBUG_OBJECT (src, "Shutting down with no buffer.");
|
gchar command;
|
||||||
return GST_FLOW_WRONG_STATE;
|
int res;
|
||||||
|
|
||||||
|
READ_COMMAND (src, command, res);
|
||||||
|
if (res < 0) {
|
||||||
|
GST_LOG_OBJECT (src, "no more commands");
|
||||||
|
/* no more commands */
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while ((retval == -1 && errno == EINTR) || /* retry if interrupted */
|
goto stopped;
|
||||||
(retval == 0 && timeout > 0)); /* Retry on incomplete timeout */
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (retval == -1) {
|
blocksize = GST_BASE_SRC (src)->blocksize;
|
||||||
|
|
||||||
|
/* create the buffer */
|
||||||
|
buf = gst_buffer_new_and_alloc (blocksize);
|
||||||
|
|
||||||
|
do {
|
||||||
|
readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
|
||||||
|
} while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
|
||||||
|
|
||||||
|
if (readbytes < 0)
|
||||||
|
goto read_error;
|
||||||
|
|
||||||
|
if (readbytes == 0)
|
||||||
|
goto eos;
|
||||||
|
|
||||||
|
GST_BUFFER_OFFSET (buf) = src->curoffset;
|
||||||
|
GST_BUFFER_SIZE (buf) = readbytes;
|
||||||
|
GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
|
||||||
|
src->curoffset += readbytes;
|
||||||
|
|
||||||
|
GST_DEBUG_OBJECT (psrc, "Read buffer of size %u.", readbytes);
|
||||||
|
|
||||||
|
/* we're done, return the buffer */
|
||||||
|
*outbuf = buf;
|
||||||
|
|
||||||
|
return GST_FLOW_OK;
|
||||||
|
|
||||||
|
/* ERRORS */
|
||||||
|
#ifndef HAVE_WIN32
|
||||||
|
select_error:
|
||||||
|
{
|
||||||
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
|
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
|
||||||
("select on file descriptor: %s.", g_strerror (errno)));
|
("select on file descriptor: %s.", g_strerror (errno)));
|
||||||
GST_DEBUG_OBJECT (psrc, "Error during select");
|
GST_DEBUG_OBJECT (psrc, "Error during select");
|
||||||
return GST_FLOW_ERROR;
|
return GST_FLOW_ERROR;
|
||||||
} else if (retval == 0) {
|
}
|
||||||
g_signal_emit (G_OBJECT (src), gst_fdsrc_signals[SIGNAL_TIMEOUT], 0);
|
stopped:
|
||||||
GST_DEBUG_OBJECT (psrc, "Timeout in select");
|
{
|
||||||
return GST_FLOW_ERROR;
|
GST_DEBUG_OBJECT (psrc, "Select stopped");
|
||||||
|
return GST_FLOW_WRONG_STATE;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
eos:
|
||||||
/* create the buffer */
|
{
|
||||||
buf = gst_buffer_new_and_alloc (src->blocksize);
|
|
||||||
|
|
||||||
do {
|
|
||||||
readbytes = read (src->fd, GST_BUFFER_DATA (buf), src->blocksize);
|
|
||||||
} while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
|
|
||||||
|
|
||||||
if (readbytes > 0) {
|
|
||||||
GST_BUFFER_OFFSET (buf) = src->curoffset;
|
|
||||||
GST_BUFFER_SIZE (buf) = readbytes;
|
|
||||||
GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
|
|
||||||
src->curoffset += readbytes;
|
|
||||||
|
|
||||||
GST_DEBUG_OBJECT (psrc, "Read buffer of size %u.", readbytes);
|
|
||||||
|
|
||||||
/* we're done, return the buffer */
|
|
||||||
*outbuf = buf;
|
|
||||||
return GST_FLOW_OK;
|
|
||||||
} else if (readbytes == 0) {
|
|
||||||
GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
|
GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
|
||||||
gst_buffer_unref (buf);
|
gst_buffer_unref (buf);
|
||||||
return GST_FLOW_ERROR;
|
return GST_FLOW_UNEXPECTED;
|
||||||
} else {
|
}
|
||||||
|
read_error:
|
||||||
|
{
|
||||||
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
|
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
|
||||||
("read on file descriptor: %s.", g_strerror (errno)));
|
("read on file descriptor: %s.", g_strerror (errno)));
|
||||||
GST_DEBUG_OBJECT (psrc, "Error reading from fd");
|
GST_DEBUG_OBJECT (psrc, "Error reading from fd");
|
||||||
|
|
|
@ -52,11 +52,9 @@ struct _GstFdSrc {
|
||||||
/* fd */
|
/* fd */
|
||||||
gint fd;
|
gint fd;
|
||||||
|
|
||||||
|
gint control_sock[2];
|
||||||
|
|
||||||
gulong curoffset; /* current offset in file */
|
gulong curoffset; /* current offset in file */
|
||||||
gulong blocksize; /* bytes per read */
|
|
||||||
guint64 timeout; /* read timeout, in nanoseconds */
|
|
||||||
|
|
||||||
gulong seq; /* buffer sequence number */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _GstFdSrcClass {
|
struct _GstFdSrcClass {
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#ifdef HAVE_UNISTD_H
|
#ifdef HAVE_UNISTD_H
|
||||||
|
@ -38,6 +39,24 @@
|
||||||
|
|
||||||
#include "gstfdsrc.h"
|
#include "gstfdsrc.h"
|
||||||
|
|
||||||
|
/* the select call is also performed on the control sockets, that way
|
||||||
|
* we can send special commands to unblock the select call */
|
||||||
|
#define CONTROL_STOP 'S' /* stop the select call */
|
||||||
|
#define CONTROL_SOCKETS(src) src->control_sock
|
||||||
|
#define WRITE_SOCKET(src) src->control_sock[1]
|
||||||
|
#define READ_SOCKET(src) src->control_sock[0]
|
||||||
|
|
||||||
|
#define SEND_COMMAND(src, command) \
|
||||||
|
G_STMT_START { \
|
||||||
|
unsigned char c; c = command; \
|
||||||
|
write (WRITE_SOCKET(src), &c, 1); \
|
||||||
|
} G_STMT_END
|
||||||
|
|
||||||
|
#define READ_COMMAND(src, command, res) \
|
||||||
|
G_STMT_START { \
|
||||||
|
res = read(READ_SOCKET(src), &command, 1); \
|
||||||
|
} G_STMT_END
|
||||||
|
|
||||||
#define DEFAULT_BLOCKSIZE 4096
|
#define DEFAULT_BLOCKSIZE 4096
|
||||||
|
|
||||||
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
|
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
|
||||||
|
@ -69,8 +88,6 @@ enum
|
||||||
ARG_TIMEOUT
|
ARG_TIMEOUT
|
||||||
};
|
};
|
||||||
|
|
||||||
static guint gst_fdsrc_signals[LAST_SIGNAL] = { 0 };
|
|
||||||
|
|
||||||
#define _do_init(bla) \
|
#define _do_init(bla) \
|
||||||
GST_DEBUG_CATEGORY_INIT (gst_fdsrc_debug, "fdsrc", 0, "fdsrc element");
|
GST_DEBUG_CATEGORY_INIT (gst_fdsrc_debug, "fdsrc", 0, "fdsrc element");
|
||||||
|
|
||||||
|
@ -82,8 +99,9 @@ static void gst_fdsrc_set_property (GObject * object, guint prop_id,
|
||||||
static void gst_fdsrc_get_property (GObject * object, guint prop_id,
|
static void gst_fdsrc_get_property (GObject * object, guint prop_id,
|
||||||
GValue * value, GParamSpec * pspec);
|
GValue * value, GParamSpec * pspec);
|
||||||
|
|
||||||
static GstStateChangeReturn gst_fdsrc_change_state (GstElement * element,
|
static gboolean gst_fdsrc_start (GstBaseSrc * bsrc);
|
||||||
GstStateChange transition);
|
static gboolean gst_fdsrc_stop (GstBaseSrc * bsrc);
|
||||||
|
static gboolean gst_fdsrc_unlock (GstBaseSrc * bsrc);
|
||||||
|
|
||||||
static GstFlowReturn gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf);
|
static GstFlowReturn gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf);
|
||||||
|
|
||||||
|
@ -101,10 +119,11 @@ gst_fdsrc_class_init (GstFdSrcClass * klass)
|
||||||
{
|
{
|
||||||
GObjectClass *gobject_class;
|
GObjectClass *gobject_class;
|
||||||
GstBaseSrcClass *gstbasesrc_class;
|
GstBaseSrcClass *gstbasesrc_class;
|
||||||
GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
|
GstElementClass *gstelement_class;
|
||||||
GstPushSrcClass *gstpush_src_class;
|
GstPushSrcClass *gstpush_src_class;
|
||||||
|
|
||||||
gobject_class = G_OBJECT_CLASS (klass);
|
gobject_class = G_OBJECT_CLASS (klass);
|
||||||
|
gstelement_class = GST_ELEMENT_CLASS (klass);
|
||||||
gstbasesrc_class = (GstBaseSrcClass *) klass;
|
gstbasesrc_class = (GstBaseSrcClass *) klass;
|
||||||
gstpush_src_class = (GstPushSrcClass *) klass;
|
gstpush_src_class = (GstPushSrcClass *) klass;
|
||||||
|
|
||||||
|
@ -116,23 +135,12 @@ gst_fdsrc_class_init (GstFdSrcClass * klass)
|
||||||
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FD,
|
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FD,
|
||||||
g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
|
g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
|
||||||
0, G_MAXINT, 0, G_PARAM_READWRITE));
|
0, G_MAXINT, 0, G_PARAM_READWRITE));
|
||||||
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BLOCKSIZE,
|
|
||||||
g_param_spec_ulong ("blocksize", "Block size",
|
|
||||||
"Size in bytes to read per buffer", 1, G_MAXULONG, DEFAULT_BLOCKSIZE,
|
|
||||||
G_PARAM_READWRITE));
|
|
||||||
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TIMEOUT,
|
|
||||||
g_param_spec_uint64 ("timeout", "Timeout", "Read timeout in nanoseconds",
|
|
||||||
0, G_MAXUINT64, 0, G_PARAM_READWRITE));
|
|
||||||
|
|
||||||
gst_fdsrc_signals[SIGNAL_TIMEOUT] =
|
gstbasesrc_class->start = gst_fdsrc_start;
|
||||||
g_signal_new ("timeout", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
|
gstbasesrc_class->stop = gst_fdsrc_stop;
|
||||||
G_STRUCT_OFFSET (GstFdSrcClass, timeout), NULL, NULL,
|
gstbasesrc_class->unlock = gst_fdsrc_unlock;
|
||||||
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
|
|
||||||
|
|
||||||
gstelement_class->change_state = gst_fdsrc_change_state;
|
|
||||||
|
|
||||||
gstpush_src_class->create = gst_fdsrc_create;
|
gstpush_src_class->create = gst_fdsrc_create;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -144,36 +152,57 @@ gst_fdsrc_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
|
||||||
|
|
||||||
fdsrc->fd = 0;
|
fdsrc->fd = 0;
|
||||||
fdsrc->curoffset = 0;
|
fdsrc->curoffset = 0;
|
||||||
fdsrc->blocksize = DEFAULT_BLOCKSIZE;
|
|
||||||
fdsrc->timeout = 0;
|
|
||||||
fdsrc->seq = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static GstStateChangeReturn
|
|
||||||
gst_fdsrc_change_state (GstElement * element, GstStateChange transition)
|
static gboolean
|
||||||
|
gst_fdsrc_start (GstBaseSrc * bsrc)
|
||||||
{
|
{
|
||||||
GstFdSrc *src = GST_FDSRC (element);
|
GstFdSrc *src = GST_FDSRC (bsrc);
|
||||||
|
gint control_sock[2];
|
||||||
|
|
||||||
switch (transition) {
|
src->curoffset = 0;
|
||||||
case GST_STATE_CHANGE_NULL_TO_READY:
|
|
||||||
break;
|
if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
|
||||||
case GST_STATE_CHANGE_READY_TO_NULL:
|
goto socket_pair;
|
||||||
break;
|
|
||||||
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
READ_SOCKET (src) = control_sock[0];
|
||||||
src->curoffset = 0;
|
WRITE_SOCKET (src) = control_sock[1];
|
||||||
break;
|
|
||||||
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
|
||||||
break;
|
fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
|
||||||
default:
|
|
||||||
break;
|
return TRUE;
|
||||||
|
|
||||||
|
/* ERRORS */
|
||||||
|
socket_pair:
|
||||||
|
{
|
||||||
|
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
|
||||||
|
GST_ERROR_SYSTEM);
|
||||||
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (GST_ELEMENT_CLASS (parent_class)->change_state)
|
|
||||||
return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
||||||
|
|
||||||
return GST_STATE_CHANGE_SUCCESS;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static gboolean
|
||||||
|
gst_fdsrc_stop (GstBaseSrc * bsrc)
|
||||||
|
{
|
||||||
|
GstFdSrc *src = GST_FDSRC (bsrc);
|
||||||
|
|
||||||
|
close (READ_SOCKET (src));
|
||||||
|
close (WRITE_SOCKET (src));
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean
|
||||||
|
gst_fdsrc_unlock (GstBaseSrc * bsrc)
|
||||||
|
{
|
||||||
|
GstFdSrc *src = GST_FDSRC (bsrc);
|
||||||
|
|
||||||
|
SEND_COMMAND (src, CONTROL_STOP);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
gst_fdsrc_set_property (GObject * object, guint prop_id, const GValue * value,
|
gst_fdsrc_set_property (GObject * object, guint prop_id, const GValue * value,
|
||||||
|
@ -189,12 +218,6 @@ gst_fdsrc_set_property (GObject * object, guint prop_id, const GValue * value,
|
||||||
case ARG_FD:
|
case ARG_FD:
|
||||||
src->fd = g_value_get_int (value);
|
src->fd = g_value_get_int (value);
|
||||||
break;
|
break;
|
||||||
case ARG_BLOCKSIZE:
|
|
||||||
src->blocksize = g_value_get_ulong (value);
|
|
||||||
break;
|
|
||||||
case ARG_TIMEOUT:
|
|
||||||
src->timeout = g_value_get_uint64 (value);
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
||||||
break;
|
break;
|
||||||
|
@ -212,34 +235,25 @@ gst_fdsrc_get_property (GObject * object, guint prop_id, GValue * value,
|
||||||
src = GST_FDSRC (object);
|
src = GST_FDSRC (object);
|
||||||
|
|
||||||
switch (prop_id) {
|
switch (prop_id) {
|
||||||
case ARG_BLOCKSIZE:
|
|
||||||
g_value_set_ulong (value, src->blocksize);
|
|
||||||
break;
|
|
||||||
case ARG_FD:
|
case ARG_FD:
|
||||||
g_value_set_int (value, src->fd);
|
g_value_set_int (value, src->fd);
|
||||||
break;
|
break;
|
||||||
case ARG_TIMEOUT:
|
|
||||||
g_value_set_uint64 (value, src->timeout);
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#define SELECT_TIMEOUT (GST_SECOND / 20)
|
|
||||||
|
|
||||||
static GstFlowReturn
|
static GstFlowReturn
|
||||||
gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf)
|
gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf)
|
||||||
{
|
{
|
||||||
GstFdSrc *src;
|
GstFdSrc *src;
|
||||||
GstBuffer *buf;
|
GstBuffer *buf;
|
||||||
glong readbytes;
|
glong readbytes;
|
||||||
GstClockTime timeout;
|
guint blocksize;
|
||||||
|
|
||||||
#ifndef HAVE_WIN32
|
#ifndef HAVE_WIN32
|
||||||
fd_set readfds;
|
fd_set readfds;
|
||||||
struct timeval t, *tp = &t;
|
|
||||||
gint retval;
|
gint retval;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -248,65 +262,82 @@ gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf)
|
||||||
#ifndef HAVE_WIN32
|
#ifndef HAVE_WIN32
|
||||||
FD_ZERO (&readfds);
|
FD_ZERO (&readfds);
|
||||||
FD_SET (src->fd, &readfds);
|
FD_SET (src->fd, &readfds);
|
||||||
|
FD_SET (READ_SOCKET (src), &readfds);
|
||||||
if (src->timeout != 0) {
|
|
||||||
timeout = MIN (SELECT_TIMEOUT, src->timeout);
|
|
||||||
} else {
|
|
||||||
timeout = SELECT_TIMEOUT;
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
GST_TIME_TO_TIMEVAL (timeout, t);
|
retval = select (FD_SETSIZE, &readfds, NULL, NULL, NULL);
|
||||||
if (src->timeout != 0)
|
} while ((retval == -1 && errno == EINTR));
|
||||||
timeout -= MIN (timeout, SELECT_TIMEOUT);
|
|
||||||
|
|
||||||
retval = select (src->fd + 1, &readfds, NULL, NULL, tp);
|
if (retval == -1)
|
||||||
|
goto select_error;
|
||||||
|
|
||||||
/* Check whether the element got shutdown before full timeout */
|
if (FD_ISSET (READ_SOCKET (src), &readfds)) {
|
||||||
if (retval == 0) {
|
/* read all stop commands */
|
||||||
if (GST_PAD_IS_FLUSHING (GST_BASE_SRC_PAD (src))) {
|
while (TRUE) {
|
||||||
GST_DEBUG_OBJECT (src, "Shutting down with no buffer.");
|
gchar command;
|
||||||
return GST_FLOW_WRONG_STATE;
|
int res;
|
||||||
|
|
||||||
|
READ_COMMAND (src, command, res);
|
||||||
|
if (res < 0) {
|
||||||
|
GST_LOG_OBJECT (src, "no more commands");
|
||||||
|
/* no more commands */
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while ((retval == -1 && errno == EINTR) || /* retry if interrupted */
|
goto stopped;
|
||||||
(retval == 0 && timeout > 0)); /* Retry on incomplete timeout */
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (retval == -1) {
|
blocksize = GST_BASE_SRC (src)->blocksize;
|
||||||
|
|
||||||
|
/* create the buffer */
|
||||||
|
buf = gst_buffer_new_and_alloc (blocksize);
|
||||||
|
|
||||||
|
do {
|
||||||
|
readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
|
||||||
|
} while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
|
||||||
|
|
||||||
|
if (readbytes < 0)
|
||||||
|
goto read_error;
|
||||||
|
|
||||||
|
if (readbytes == 0)
|
||||||
|
goto eos;
|
||||||
|
|
||||||
|
GST_BUFFER_OFFSET (buf) = src->curoffset;
|
||||||
|
GST_BUFFER_SIZE (buf) = readbytes;
|
||||||
|
GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
|
||||||
|
src->curoffset += readbytes;
|
||||||
|
|
||||||
|
GST_DEBUG_OBJECT (psrc, "Read buffer of size %u.", readbytes);
|
||||||
|
|
||||||
|
/* we're done, return the buffer */
|
||||||
|
*outbuf = buf;
|
||||||
|
|
||||||
|
return GST_FLOW_OK;
|
||||||
|
|
||||||
|
/* ERRORS */
|
||||||
|
#ifndef HAVE_WIN32
|
||||||
|
select_error:
|
||||||
|
{
|
||||||
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
|
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
|
||||||
("select on file descriptor: %s.", g_strerror (errno)));
|
("select on file descriptor: %s.", g_strerror (errno)));
|
||||||
GST_DEBUG_OBJECT (psrc, "Error during select");
|
GST_DEBUG_OBJECT (psrc, "Error during select");
|
||||||
return GST_FLOW_ERROR;
|
return GST_FLOW_ERROR;
|
||||||
} else if (retval == 0) {
|
}
|
||||||
g_signal_emit (G_OBJECT (src), gst_fdsrc_signals[SIGNAL_TIMEOUT], 0);
|
stopped:
|
||||||
GST_DEBUG_OBJECT (psrc, "Timeout in select");
|
{
|
||||||
return GST_FLOW_ERROR;
|
GST_DEBUG_OBJECT (psrc, "Select stopped");
|
||||||
|
return GST_FLOW_WRONG_STATE;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
eos:
|
||||||
/* create the buffer */
|
{
|
||||||
buf = gst_buffer_new_and_alloc (src->blocksize);
|
|
||||||
|
|
||||||
do {
|
|
||||||
readbytes = read (src->fd, GST_BUFFER_DATA (buf), src->blocksize);
|
|
||||||
} while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
|
|
||||||
|
|
||||||
if (readbytes > 0) {
|
|
||||||
GST_BUFFER_OFFSET (buf) = src->curoffset;
|
|
||||||
GST_BUFFER_SIZE (buf) = readbytes;
|
|
||||||
GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
|
|
||||||
src->curoffset += readbytes;
|
|
||||||
|
|
||||||
GST_DEBUG_OBJECT (psrc, "Read buffer of size %u.", readbytes);
|
|
||||||
|
|
||||||
/* we're done, return the buffer */
|
|
||||||
*outbuf = buf;
|
|
||||||
return GST_FLOW_OK;
|
|
||||||
} else if (readbytes == 0) {
|
|
||||||
GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
|
GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
|
||||||
gst_buffer_unref (buf);
|
gst_buffer_unref (buf);
|
||||||
return GST_FLOW_ERROR;
|
return GST_FLOW_UNEXPECTED;
|
||||||
} else {
|
}
|
||||||
|
read_error:
|
||||||
|
{
|
||||||
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
|
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
|
||||||
("read on file descriptor: %s.", g_strerror (errno)));
|
("read on file descriptor: %s.", g_strerror (errno)));
|
||||||
GST_DEBUG_OBJECT (psrc, "Error reading from fd");
|
GST_DEBUG_OBJECT (psrc, "Error reading from fd");
|
||||||
|
|
|
@ -52,11 +52,9 @@ struct _GstFdSrc {
|
||||||
/* fd */
|
/* fd */
|
||||||
gint fd;
|
gint fd;
|
||||||
|
|
||||||
|
gint control_sock[2];
|
||||||
|
|
||||||
gulong curoffset; /* current offset in file */
|
gulong curoffset; /* current offset in file */
|
||||||
gulong blocksize; /* bytes per read */
|
|
||||||
guint64 timeout; /* read timeout, in nanoseconds */
|
|
||||||
|
|
||||||
gulong seq; /* buffer sequence number */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _GstFdSrcClass {
|
struct _GstFdSrcClass {
|
||||||
|
|
Loading…
Reference in a new issue