[vlc-commits] stream_fifo: use separate structure for writer side
Rémi Denis-Courmont
git at videolan.org
Fri Mar 15 02:28:22 CET 2019
vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Fri Mar 15 03:26:36 2019 +0200| [db75e293e8257fc51c4a4e0811a2ed65c1659e7b] | committer: Rémi Denis-Courmont
stream_fifo: use separate structure for writer side
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=db75e293e8257fc51c4a4e0811a2ed65c1659e7b
---
include/vlc_stream.h | 13 ++++---
src/input/demux_chained.c | 2 +-
src/input/stream_fifo.c | 81 +++++++++++++++++++++++++++-----------------
test/src/input/stream_fifo.c | 2 +-
4 files changed, 60 insertions(+), 38 deletions(-)
diff --git a/include/vlc_stream.h b/include/vlc_stream.h
index 3fecf5decc..43369ec897 100644
--- a/include/vlc_stream.h
+++ b/include/vlc_stream.h
@@ -434,6 +434,8 @@ VLC_USED;
@{
*/
+typedef struct vlc_stream_fifo vlc_stream_fifo_t;
+
/**
* Creates a FIFO stream.
*
@@ -450,9 +452,10 @@ VLC_USED;
*
* \param parent parent VLC object for the stream
* \param reader location to store read side stream pointer [OUT]
- * \return a stream object or NULL on memory error.
+ * \return a FIFO stream object or NULL on memory error.
*/
-VLC_API stream_t *vlc_stream_fifo_New(vlc_object_t *parent, stream_t **reader);
+VLC_API vlc_stream_fifo_t *vlc_stream_fifo_New(vlc_object_t *parent,
+ stream_t **reader);
/**
* Writes a block to a FIFO stream.
@@ -465,7 +468,7 @@ VLC_API stream_t *vlc_stream_fifo_New(vlc_object_t *parent, stream_t **reader);
* \bug No congestion control is performed. If the reader end is not keeping
* up with the writer end, buffers will accumulate in memory.
*/
-VLC_API int vlc_stream_fifo_Queue(stream_t *s, block_t *block);
+VLC_API int vlc_stream_fifo_Queue(vlc_stream_fifo_t *s, block_t *block);
/**
* Writes data to a FIFO stream.
@@ -476,7 +479,7 @@ VLC_API int vlc_stream_fifo_Queue(stream_t *s, block_t *block);
* \param len length of data to write in bytes
* \return len on success, or -1 on error (errno is set accordingly)
*/
-VLC_API ssize_t vlc_stream_fifo_Write(stream_t *s, const void *buf,
+VLC_API ssize_t vlc_stream_fifo_Write(vlc_stream_fifo_t *s, const void *buf,
size_t len);
/**
@@ -485,7 +488,7 @@ VLC_API ssize_t vlc_stream_fifo_Write(stream_t *s, const void *buf,
* Marks the end of the FIFO stream and releases any underlying resources.
* \param s FIFO stream created by vlc_stream_fifo_New()
*/
-VLC_API void vlc_stream_fifo_Close(stream_t *s);
+VLC_API void vlc_stream_fifo_Close(vlc_stream_fifo_t *s);
/**
* @}
diff --git a/src/input/demux_chained.c b/src/input/demux_chained.c
index 784e0e292a..f2123fd0f7 100644
--- a/src/input/demux_chained.c
+++ b/src/input/demux_chained.c
@@ -34,7 +34,7 @@
struct vlc_demux_chained_t
{
- stream_t *writer;
+ vlc_stream_fifo_t *writer;
stream_t *reader;
vlc_thread_t thread;
diff --git a/src/input/stream_fifo.c b/src/input/stream_fifo.c
index 9bc4d81a2b..4cdd548016 100644
--- a/src/input/stream_fifo.c
+++ b/src/input/stream_fifo.c
@@ -33,34 +33,47 @@
#include "stream.h"
-struct vlc_stream_fifo_private
-{
+struct vlc_stream_fifo {
vlc_fifo_t *fifo;
bool eof;
};
-static void vlc_stream_fifo_Destroy(stream_t *s)
+struct vlc_stream_fifo_private {
+ vlc_stream_fifo_t *writer;
+};
+
+static vlc_stream_fifo_t *vlc_stream_fifo_Writer(stream_t *s)
{
struct vlc_stream_fifo_private *sys = vlc_stream_Private(s);
- vlc_fifo_t *fifo = sys->fifo;
+
+ return sys->writer;
+}
+
+static void vlc_stream_fifo_Destroy(stream_t *s)
+{
+ struct vlc_stream_fifo *writer = vlc_stream_fifo_Writer(s);
+ vlc_fifo_t *fifo = writer->fifo;
block_t *block;
bool closed;
vlc_fifo_Lock(fifo);
block = vlc_fifo_DequeueAllUnlocked(fifo);
- closed = sys->eof;
- sys->eof = true;
+ closed = writer->eof;
+ writer->eof = true;
vlc_fifo_Unlock(fifo);
block_ChainRelease(block);
- if (closed) /* Destroy shared state if write end is already closed */
+ if (closed) {
+ /* Destroy shared state if write end is already closed */
block_FifoRelease(fifo);
+ free(writer);
+ }
}
static block_t *vlc_stream_fifo_Block(stream_t *s, bool *restrict eof)
{
- struct vlc_stream_fifo_private *sys = vlc_stream_Private(s);
+ struct vlc_stream_fifo *sys = vlc_stream_fifo_Writer(s);
vlc_fifo_t *fifo = sys->fifo;
block_t *block;
@@ -103,38 +116,43 @@ static int vlc_stream_fifo_Control(stream_t *s, int query, va_list ap)
return VLC_SUCCESS;
}
-stream_t *vlc_stream_fifo_New(vlc_object_t *parent, stream_t **reader)
+vlc_stream_fifo_t *vlc_stream_fifo_New(vlc_object_t *parent, stream_t **reader)
{
- vlc_fifo_t *fifo = block_FifoNew();
- if (unlikely(fifo == NULL))
+ struct vlc_stream_fifo *writer = malloc(sizeof (*writer));
+ if (unlikely(writer == NULL))
return NULL;
+ writer->fifo = block_FifoNew();
+ if (unlikely(writer->fifo == NULL)) {
+ free(writer);
+ return NULL;
+ }
+ writer->eof = false;
+
struct vlc_stream_fifo_private *sys;
stream_t *s = vlc_stream_CustomNew(parent, vlc_stream_fifo_Destroy,
sizeof (*sys), "stream");
- if (unlikely(s == NULL))
- {
- block_FifoRelease(fifo);
+ if (unlikely(s == NULL)) {
+ block_FifoRelease(writer->fifo);
+ free(writer);
return NULL;
}
sys = vlc_stream_Private(s);
- sys->fifo = fifo;
- sys->eof = false;
+ sys->writer = writer;
s->pf_block = vlc_stream_fifo_Block;
s->pf_seek = NULL;
s->pf_control = vlc_stream_fifo_Control;
*reader = s;
- return vlc_object_hold(s);
+ return writer;
}
-int vlc_stream_fifo_Queue(stream_t *s, block_t *block)
+int vlc_stream_fifo_Queue(vlc_stream_fifo_t *writer, block_t *block)
{
- struct vlc_stream_fifo_private *sys = vlc_stream_Private(s);
- vlc_fifo_t *fifo = sys->fifo;
+ vlc_fifo_t *fifo = writer->fifo;
vlc_fifo_Lock(fifo);
- if (likely(!sys->eof))
+ if (likely(!writer->eof))
{
vlc_fifo_QueueUnlocked(fifo, block);
block = NULL;
@@ -150,30 +168,31 @@ int vlc_stream_fifo_Queue(stream_t *s, block_t *block)
return 0;
}
-ssize_t vlc_stream_fifo_Write(stream_t *s, const void *buf, size_t len)
+ssize_t vlc_stream_fifo_Write(vlc_stream_fifo_t *writer,
+ const void *buf, size_t len)
{
block_t *block = block_Alloc(len);
if (unlikely(block == NULL))
return -1;
memcpy(block->p_buffer, buf, len);
- return vlc_stream_fifo_Queue(s, block) ? -1 : (ssize_t)len;
+ return vlc_stream_fifo_Queue(writer, block) ? -1 : (ssize_t)len;
}
-void vlc_stream_fifo_Close(stream_t *s)
+void vlc_stream_fifo_Close(vlc_stream_fifo_t *writer)
{
- struct vlc_stream_fifo_private *sys = vlc_stream_Private(s);
- vlc_fifo_t *fifo = sys->fifo;
+ vlc_fifo_t *fifo = writer->fifo;
bool closed;
vlc_fifo_Lock(fifo);
- closed = sys->eof;
- sys->eof = true;
+ closed = writer->eof;
+ writer->eof = true;
vlc_fifo_Signal(fifo);
vlc_fifo_Unlock(fifo);
- if (closed) /* Destroy shared state if read end is already closed */
+ if (closed) {
+ /* Destroy shared state if read end is already closed */
block_FifoRelease(fifo);
-
- vlc_object_release(s);
+ free(writer);
+ }
}
diff --git a/test/src/input/stream_fifo.c b/test/src/input/stream_fifo.c
index 950565eb10..b725467df7 100644
--- a/test/src/input/stream_fifo.c
+++ b/test/src/input/stream_fifo.c
@@ -37,7 +37,7 @@
static libvlc_instance_t *vlc;
static vlc_object_t *parent;
-static stream_t *writer;
+static vlc_stream_fifo_t *writer;
static stream_t *reader;
int main(void)
More information about the vlc-commits
mailing list