vlc/vlc-1.2 | branch: master | Laurent Aimar <[email protected]> | Sat Jan 7 01:14:03 2012 +0100| [025280c1fde9f85aa0e7ef34e36c039c823db5a4] | committer: Jean-Baptiste Kempf
Added stream_BlockRemaining() helper. It is usefull to load a whole file to memory. (cherry picked from commit 63af7c781fe9c3cf07babb8befacd1d87b2182ca) Signed-off-by: Jean-Baptiste Kempf <[email protected]> > http://git.videolan.org/gitweb.cgi/vlc/vlc-1.2.git/?a=commit;h=025280c1fde9f85aa0e7ef34e36c039c823db5a4 --- include/vlc_stream.h | 1 + src/input/stream.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/libvlccore.sym | 1 + 3 files changed, 47 insertions(+), 0 deletions(-) diff --git a/include/vlc_stream.h b/include/vlc_stream.h index 61b1726..c929836 100644 --- a/include/vlc_stream.h +++ b/include/vlc_stream.h @@ -119,6 +119,7 @@ VLC_API int stream_vaControl( stream_t *s, int i_query, va_list args ); VLC_API void stream_Delete( stream_t *s ); VLC_API int stream_Control( stream_t *s, int i_query, ... ); VLC_API block_t * stream_Block( stream_t *s, int i_size ); +VLC_API block_t * stream_BlockRemaining( stream_t *s, int i_max_size ); VLC_API char * stream_ReadLine( stream_t * ); /** diff --git a/src/input/stream.c b/src/input/stream.c index 1d9ae1b..197f56c 100644 --- a/src/input/stream.c +++ b/src/input/stream.c @@ -1941,3 +1941,48 @@ block_t *stream_Block( stream_t *s, int i_size ) } return NULL; } + +/** + * Read the remaining of the data if there is less than i_max_size bytes, otherwise + * return NULL. + * + * The stream position is unknown after the call. + */ +block_t *stream_BlockRemaining( stream_t *s, int i_max_size ) +{ + int i_allocate = __MIN(1000000, i_max_size); + int64_t i_size = stream_Size( s ); + if( i_size > 0 ) + { + int64_t i_position = stream_Tell( s ); + if( i_position + i_max_size < i_size ) + { + msg_Err( s, "Remaining stream size is greater than %d bytes", + i_max_size ); + return NULL; + } + i_allocate = i_size - i_position; + } + if( i_allocate <= 0 ) + return NULL; + + block_t *p_block = block_New( s, i_allocate ); + int i_index = 0; + while( p_block ) + { + int i_read = stream_Read( s, &p_block->p_buffer[i_index], + p_block->i_buffer - i_index); + if( i_read <= 0 ) + break; + i_index += i_read; + i_max_size -= i_read; + if( i_max_size <= 0 ) + break; + p_block = block_Realloc( p_block, 0, p_block->i_buffer + + __MIN(1000000, i_max_size) ); + } + if( p_block ) + p_block->i_buffer = i_index; + return p_block; +} + diff --git a/src/libvlccore.sym b/src/libvlccore.sym index 7538bf2..0da29f5 100644 --- a/src/libvlccore.sym +++ b/src/libvlccore.sym @@ -411,6 +411,7 @@ stats_TimersDumpAll stats_TimerStart stats_TimerStop stream_Block +stream_BlockRemaining stream_Control stream_Delete stream_DemuxNew _______________________________________________ vlc-commits mailing list [email protected] http://mailman.videolan.org/listinfo/vlc-commits
