[vlc-commits] stream: add vlc_stream_ReadPartial()

Rémi Denis-Courmont git at videolan.org
Thu Jul 21 21:30:18 CEST 2016


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Thu Jul 21 21:51:38 2016 +0300| [b75462c33eca6e2ec26762e1b47447dc24cdc5c5] | committer: Rémi Denis-Courmont

stream: add vlc_stream_ReadPartial()

This is a variant of vlc_stream_Read(), such that it only waits for
some bytes (i.e. more than zero) rather than the full requested bytes
count.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=b75462c33eca6e2ec26762e1b47447dc24cdc5c5
---

 include/vlc_stream.h |   17 +++++++++++++++++
 src/input/stream.c   |   41 +++++++++++++++++++++++++++++++++++++++++
 src/libvlccore.sym   |    1 +
 3 files changed, 59 insertions(+)

diff --git a/include/vlc_stream.h b/include/vlc_stream.h
index 391bbae..37f7182 100644
--- a/include/vlc_stream.h
+++ b/include/vlc_stream.h
@@ -178,6 +178,23 @@ enum stream_query_e
 VLC_API ssize_t vlc_stream_Read(stream_t *, void *buf, size_t len) VLC_USED;
 
 /**
+ * Reads partial data from a byte stream.
+ *
+ * This function waits until some data is available for reading from the
+ * stream, a fatal error is encountered or the end-of-stream is reached.
+ *
+ * Unlike vlc_stream_Read(), this function does not wait for the full requested
+ * bytes count. It can return a short count even before the end of the stream
+ * and in the absence of any error.
+ *
+ * \param buf start of buffer to read data into [OUT]
+ * \param len buffer size (maximum number of bytes to read)
+ * \return the number of bytes read or a negative value on error.
+ */
+VLC_API ssize_t vlc_stream_ReadPartial(stream_t *, void *buf, size_t len)
+VLC_USED;
+
+/**
  * Peeks at data from a byte stream.
  *
  * This function buffers for the requested number of bytes, waiting if
diff --git a/src/input/stream.c b/src/input/stream.c
index 78b025f..3dbc8e7 100644
--- a/src/input/stream.c
+++ b/src/input/stream.c
@@ -430,6 +430,47 @@ ssize_t vlc_stream_Read(stream_t *s, void *buf, size_t len)
     return copy;
 }
 
+ssize_t vlc_stream_ReadPartial(stream_t *s, void *buf, size_t len)
+{
+    stream_priv_t *priv = (stream_priv_t *)s;
+
+    for (;;)
+    {
+        size_t avail = 0;
+
+        if (priv->peek != NULL)
+            avail += priv->peek->i_buffer;
+        if (priv->block != NULL)
+            avail += priv->block->i_buffer;
+        if (avail > 0)
+            return vlc_stream_Read(s, buf, (avail < len) ? avail : len);
+
+        if (s->pf_read != NULL)
+        {
+            ssize_t ret = s->pf_read(s, buf, len);
+            if (ret < 0)
+                return ret;
+
+            priv->offset += ret;
+            if (likely(len > 0))
+                priv->eof = !ret;
+
+            return ret;
+        }
+
+        if (s->pf_block != NULL)
+        {
+            bool eof;
+
+            priv->block = s->pf_block(s, &eof);
+            if (priv->block == NULL && eof)
+                return 0;
+        }
+        else
+            return 0;
+    }
+}
+
 ssize_t vlc_stream_Peek(stream_t *s, const uint8_t **restrict bufp, size_t len)
 {
     stream_priv_t *priv = (stream_priv_t *)s;
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index 2092a96..79064d3 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -411,6 +411,7 @@ vlc_stream_Peek
 vlc_stream_Read
 vlc_stream_ReadBlock
 vlc_stream_ReadLine
+vlc_stream_ReadPartial
 vlc_stream_Seek
 vlc_stream_Tell
 vlc_stream_NewMRL



More information about the vlc-commits mailing list