Commit deb5e45b authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

stream: add stream_ReadBlock()

This reads a block of unspecified size. The main use case will be
support for block-oriented stream filters.
parent 6e1ace8a
......@@ -137,6 +137,24 @@ VLC_API ssize_t stream_Read(stream_t *, void *, size_t) VLC_USED;
*/
VLC_API ssize_t stream_Peek(stream_t *, const uint8_t **, size_t) VLC_USED;
/**
* Reads a data block from a byte stream.
*
* This function dequeues the next block of data from the byte stream. The
* byte stream back-end decides on the size of the block; the caller cannot
* make any assumption about it.
*
* The function might also return NULL spuriously - this does not necessarily
* imply that the stream is ended nor that it has encountered a nonrecoverable
* error.
*
* This function should be used instead of stream_Read() or stream_Peek() when
* the caller can handle reads of any size.
*
* \return either a data block or NULL
*/
VLC_API block_t *stream_ReadBlock(stream_t *) VLC_USED;
/**
* Tells the current stream position.
*
......
......@@ -453,6 +453,44 @@ ssize_t stream_Peek(stream_t *s, const uint8_t **restrict bufp, size_t len)
return len;
}
block_t *stream_ReadBlock(stream_t *s)
{
stream_priv_t *priv = (stream_priv_t *)s;
block_t *block;
if (priv->peek != NULL)
{
block = priv->peek;
priv->peek = NULL;
}
else
{
if (vlc_killed())
return NULL;
block = block_Alloc(4096);
if (unlikely(block == NULL))
return NULL;
ssize_t ret = s->pf_read(s, block->p_buffer, block->i_buffer);
if (ret >= 0)
{
block->i_buffer = ret;
priv->eof = !ret;
}
else
{
block_Release(block);
block = NULL;
}
}
if (block != NULL)
priv->offset += block->i_buffer;
return block;
}
uint64_t stream_Tell(const stream_t *s)
{
const stream_priv_t *priv = (const stream_priv_t *)s;
......
......@@ -410,6 +410,7 @@ stream_FilterNew
stream_MemoryNew
stream_Peek
stream_Read
stream_ReadBlock
stream_ReadLine
stream_Seek
stream_Tell
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment