1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_strings.h>
33 #include <vlc_memory.h>
40 #include "input_internal.h"
42 // #define STREAM_DEBUG 1
45 * - tune the 2 methods (block/stream)
46 * - compute cost for seek
47 * - improve stream mode seeking with closest segments
53 * One linked list of data read
55 * More complex scheme using mutliple track to avoid seeking
56 * - using directly the access (only indirection for peeking).
57 * This method is known to introduce much less latency.
58 * It should probably defaulted (instead of the stream method (2)).
61 /* How many tracks we have, currently only used for stream mode */
62 #ifdef OPTIMIZE_MEMORY
63 # define STREAM_CACHE_TRACK 1
64 /* Max size of our cache 128Ko per track */
65 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
67 # define STREAM_CACHE_TRACK 3
68 /* Max size of our cache 4Mo per track */
69 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
72 /* How many data we try to prebuffer
73 * XXX it should be small to avoid useless latency but big enough for
74 * efficient demux probing */
75 #define STREAM_CACHE_PREBUFFER_SIZE (128)
77 /* Method1: Simple, for pf_block.
78 * We get blocks and put them in the linked list.
79 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
82 /* Method2: A bit more complex, for pf_read
83 * - We use ring buffers, only one if unseekable, all if seekable
84 * - Upon seek date current ring, then search if one ring match the pos,
85 * yes: switch to it, seek the access to match the end of the ring
86 * no: search the ring with i_end the closer to i_pos,
87 * if close enough, read data and use this ring
88 * else use the oldest ring, seek and use it.
90 * TODO: - with access non seekable: use all space available for only one ring, but
91 * we have to support seekable/non-seekable switch on the fly.
92 * - compute a good value for i_read_size
95 #define STREAM_READ_ATONCE 1024
96 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
120 } stream_read_method_t;
126 stream_read_method_t method; /* method to use */
128 uint64_t i_pos; /* Current reading offset */
130 /* Method 1: pf_block */
133 uint64_t i_start; /* Offset of block for p_first */
134 uint64_t i_offset; /* Offset for data in p_current */
135 block_t *p_current; /* Current block */
137 uint64_t i_size; /* Total amount of data in the list */
143 /* Method 2: for pf_read */
146 unsigned i_offset; /* Buffer offset in the current track */
147 int i_tk; /* Current track */
148 stream_track_t tk[STREAM_CACHE_TRACK];
154 unsigned i_used; /* Used since last read */
155 unsigned i_read_size;
159 /* Peek temporary buffer */
163 /* Stat for both method */
166 bool b_fastseek; /* From access */
168 /* Stat about reading data */
169 uint64_t i_read_count;
171 uint64_t i_read_time;
173 /* Stat about seek */
174 unsigned i_seek_count;
175 uint64_t i_seek_time;
181 access_entry_t **list;
183 access_t *p_list_access;
187 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read );
188 static int AStreamPeekBlock( stream_t *s, const uint8_t **p_peek, unsigned int i_read );
189 static int AStreamSeekBlock( stream_t *s, uint64_t i_pos );
190 static void AStreamPrebufferBlock( stream_t *s );
191 static block_t *AReadBlock( stream_t *s, bool *pb_eof );
194 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read );
195 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read );
196 static int AStreamSeekStream( stream_t *s, uint64_t i_pos );
197 static void AStreamPrebufferStream( stream_t *s );
198 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read );
201 static int AStreamControl( stream_t *s, int i_query, va_list );
202 static void AStreamDestroy( stream_t *s );
203 static int ASeek( stream_t *s, uint64_t i_pos );
205 /****************************************************************************
206 * stream_CommonNew: create an empty stream structure
207 ****************************************************************************/
208 stream_t *stream_CommonNew( vlc_object_t *p_obj )
210 stream_t *s = (stream_t *)vlc_custom_create( p_obj, sizeof(*s), "stream" );
215 s->p_text = malloc( sizeof(*s->p_text) );
218 vlc_object_release( s );
222 /* UTF16 and UTF32 text file conversion */
223 s->p_text->conv = (vlc_iconv_t)(-1);
224 s->p_text->i_char_width = 1;
225 s->p_text->b_little_endian = false;
230 void stream_CommonDelete( stream_t *s )
234 if( s->p_text->conv != (vlc_iconv_t)(-1) )
235 vlc_iconv_close( s->p_text->conv );
238 free( s->psz_access );
240 vlc_object_release( s );
244 /****************************************************************************
245 * stream_UrlNew: create a stream from a access
246 ****************************************************************************/
247 stream_t *stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
249 const char *psz_access, *psz_demux, *psz_path, *psz_anchor;
255 char psz_dup[strlen( psz_url ) + 1];
256 strcpy( psz_dup, psz_url );
257 input_SplitMRL( &psz_access, &psz_demux, &psz_path, &psz_anchor, psz_dup );
259 /* Now try a real access */
260 p_access = access_New( p_parent, NULL, psz_access, psz_demux, psz_path );
261 if( p_access == NULL )
263 msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
267 return stream_AccessNew( p_access, NULL );
270 stream_t *stream_AccessNew( access_t *p_access, char **ppsz_list )
272 stream_t *s = stream_CommonNew( VLC_OBJECT(p_access) );
278 s->p_input = p_access->p_input;
279 s->psz_access = strdup( p_access->psz_access );
280 s->psz_path = strdup( p_access->psz_location );
281 s->p_sys = p_sys = malloc( sizeof( *p_sys ) );
282 if( !s->psz_access || !s->psz_path || !s->p_sys )
284 stream_CommonDelete( s );
288 s->pf_read = NULL; /* Set up later */
290 s->pf_control = AStreamControl;
291 s->pf_destroy = AStreamDestroy;
294 p_sys->p_access = p_access;
295 if( p_access->pf_block )
296 p_sys->method = STREAM_METHOD_BLOCK;
298 p_sys->method = STREAM_METHOD_STREAM;
300 p_sys->i_pos = p_access->info.i_pos;
303 access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
304 p_sys->stat.i_bytes = 0;
305 p_sys->stat.i_read_time = 0;
306 p_sys->stat.i_read_count = 0;
307 p_sys->stat.i_seek_count = 0;
308 p_sys->stat.i_seek_time = 0;
310 TAB_INIT( p_sys->i_list, p_sys->list );
311 p_sys->i_list_index = 0;
312 p_sys->p_list_access = NULL;
314 /* Get the additional list of inputs if any (for concatenation) */
315 if( ppsz_list && ppsz_list[0] )
317 access_entry_t *p_entry = malloc( sizeof(*p_entry) );
321 p_entry->i_size = p_access->info.i_size;
322 p_entry->psz_path = strdup( p_access->psz_location );
323 if( !p_entry->psz_path )
328 p_sys->p_list_access = p_access;
329 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
330 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
331 p_entry->psz_path, p_access->info.i_size );
333 for( int i = 0; ppsz_list[i] != NULL; i++ )
335 char *psz_name = strdup( ppsz_list[i] );
340 access_t *p_tmp = access_New( p_access, p_access->p_input,
341 p_access->psz_access, "", psz_name );
345 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
346 psz_name, p_tmp->info.i_size );
348 p_entry = malloc( sizeof(*p_entry) );
351 p_entry->i_size = p_tmp->info.i_size;
352 p_entry->psz_path = psz_name;
353 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
355 access_Delete( p_tmp );
361 p_sys->p_peek = NULL;
363 if( p_sys->method == STREAM_METHOD_BLOCK )
365 msg_Dbg( s, "Using block method for AStream*" );
366 s->pf_read = AStreamReadBlock;
367 s->pf_peek = AStreamPeekBlock;
369 /* Init all fields of p_sys->block */
370 p_sys->block.i_start = p_sys->i_pos;
371 p_sys->block.i_offset = 0;
372 p_sys->block.p_current = NULL;
373 p_sys->block.i_size = 0;
374 p_sys->block.p_first = NULL;
375 p_sys->block.pp_last = &p_sys->block.p_first;
377 /* Do the prebuffering */
378 AStreamPrebufferBlock( s );
380 if( p_sys->block.i_size <= 0 )
382 msg_Err( s, "cannot pre fill buffer" );
390 assert( p_sys->method == STREAM_METHOD_STREAM );
392 msg_Dbg( s, "Using stream method for AStream*" );
394 s->pf_read = AStreamReadStream;
395 s->pf_peek = AStreamPeekStream;
397 /* Allocate/Setup our tracks */
398 p_sys->stream.i_offset = 0;
399 p_sys->stream.i_tk = 0;
400 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
401 if( p_sys->stream.p_buffer == NULL )
403 p_sys->stream.i_used = 0;
404 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
405 #if STREAM_READ_ATONCE < 256
406 # error "Invalid STREAM_READ_ATONCE value"
409 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
411 p_sys->stream.tk[i].i_date = 0;
412 p_sys->stream.tk[i].i_start = p_sys->i_pos;
413 p_sys->stream.tk[i].i_end = p_sys->i_pos;
414 p_sys->stream.tk[i].p_buffer=
415 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
418 /* Do the prebuffering */
419 AStreamPrebufferStream( s );
421 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
423 msg_Err( s, "cannot pre fill buffer" );
431 if( p_sys->method == STREAM_METHOD_BLOCK )
437 free( p_sys->stream.p_buffer );
439 while( p_sys->i_list > 0 )
440 free( p_sys->list[--(p_sys->i_list)] );
443 stream_CommonDelete( s );
444 access_Delete( p_access );
448 /****************************************************************************
450 ****************************************************************************/
451 static void AStreamDestroy( stream_t *s )
453 stream_sys_t *p_sys = s->p_sys;
455 if( p_sys->method == STREAM_METHOD_BLOCK )
456 block_ChainRelease( p_sys->block.p_first );
458 free( p_sys->stream.p_buffer );
460 free( p_sys->p_peek );
462 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
463 access_Delete( p_sys->p_list_access );
465 while( p_sys->i_list-- )
467 free( p_sys->list[p_sys->i_list]->psz_path );
468 free( p_sys->list[p_sys->i_list] );
472 stream_CommonDelete( s );
473 access_Delete( p_sys->p_access );
477 /****************************************************************************
478 * AStreamControlReset:
479 ****************************************************************************/
480 static void AStreamControlReset( stream_t *s )
482 stream_sys_t *p_sys = s->p_sys;
484 p_sys->i_pos = p_sys->p_access->info.i_pos;
486 if( p_sys->method == STREAM_METHOD_BLOCK )
488 block_ChainRelease( p_sys->block.p_first );
490 /* Init all fields of p_sys->block */
491 p_sys->block.i_start = p_sys->i_pos;
492 p_sys->block.i_offset = 0;
493 p_sys->block.p_current = NULL;
494 p_sys->block.i_size = 0;
495 p_sys->block.p_first = NULL;
496 p_sys->block.pp_last = &p_sys->block.p_first;
498 /* Do the prebuffering */
499 AStreamPrebufferBlock( s );
505 assert( p_sys->method == STREAM_METHOD_STREAM );
507 /* Setup our tracks */
508 p_sys->stream.i_offset = 0;
509 p_sys->stream.i_tk = 0;
510 p_sys->stream.i_used = 0;
512 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
514 p_sys->stream.tk[i].i_date = 0;
515 p_sys->stream.tk[i].i_start = p_sys->i_pos;
516 p_sys->stream.tk[i].i_end = p_sys->i_pos;
519 /* Do the prebuffering */
520 AStreamPrebufferStream( s );
524 /****************************************************************************
525 * AStreamControlUpdate:
526 ****************************************************************************/
527 static void AStreamControlUpdate( stream_t *s )
529 stream_sys_t *p_sys = s->p_sys;
531 p_sys->i_pos = p_sys->p_access->info.i_pos;
536 for( i = 0; i < p_sys->i_list_index; i++ )
538 p_sys->i_pos += p_sys->list[i]->i_size;
543 /****************************************************************************
545 ****************************************************************************/
546 static int AStreamControl( stream_t *s, int i_query, va_list args )
548 stream_sys_t *p_sys = s->p_sys;
549 access_t *p_access = p_sys->p_access;
552 uint64_t *pi_64, i_64;
557 case STREAM_GET_SIZE:
558 pi_64 = va_arg( args, uint64_t * );
559 if( s->p_sys->i_list )
563 for( i = 0; i < s->p_sys->i_list; i++ )
564 *pi_64 += s->p_sys->list[i]->i_size;
567 *pi_64 = p_access->info.i_size;
570 case STREAM_CAN_SEEK:
571 p_bool = (bool*)va_arg( args, bool * );
572 access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
575 case STREAM_CAN_FASTSEEK:
576 p_bool = (bool*)va_arg( args, bool * );
577 access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
580 case STREAM_GET_POSITION:
581 pi_64 = va_arg( args, uint64_t * );
582 *pi_64 = p_sys->i_pos;
585 case STREAM_SET_POSITION:
586 i_64 = va_arg( args, uint64_t );
587 switch( p_sys->method )
589 case STREAM_METHOD_BLOCK:
590 return AStreamSeekBlock( s, i_64 );
591 case STREAM_METHOD_STREAM:
592 return AStreamSeekStream( s, i_64 );
598 case STREAM_CONTROL_ACCESS:
600 i_int = (int) va_arg( args, int );
601 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
602 i_int != ACCESS_SET_PRIVATE_ID_CA &&
603 i_int != ACCESS_GET_PRIVATE_ID_STATE &&
604 i_int != ACCESS_SET_TITLE &&
605 i_int != ACCESS_SET_SEEKPOINT )
607 msg_Err( s, "Hey, what are you thinking ?"
608 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
611 int i_ret = access_vaControl( p_access, i_int, args );
612 if( i_int == ACCESS_SET_TITLE || i_int == ACCESS_SET_SEEKPOINT )
613 AStreamControlReset( s );
617 case STREAM_UPDATE_SIZE:
618 AStreamControlUpdate( s );
621 case STREAM_GET_CONTENT_TYPE:
622 return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
623 va_arg( args, char ** ) );
624 case STREAM_SET_RECORD_STATE:
626 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
632 /****************************************************************************
634 ****************************************************************************/
635 static void AStreamPrebufferBlock( stream_t *s )
637 stream_sys_t *p_sys = s->p_sys;
642 msg_Dbg( s, "starting pre-buffering" );
646 const int64_t i_date = mdate();
650 if( !vlc_object_alive(s) || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE )
655 p_sys->stat.i_bytes = p_sys->block.i_size;
656 p_sys->stat.i_read_time = i_date - i_start;
657 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
658 (p_sys->stat.i_read_time + 1);
660 msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
663 p_sys->stat.i_read_time / INT64_C(1000000),
669 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
678 /* Append the block */
679 p_sys->block.i_size += b->i_buffer;
680 *p_sys->block.pp_last = b;
681 p_sys->block.pp_last = &b->p_next;
683 p_sys->stat.i_read_count++;
690 msg_Dbg( s, "received first data after %d ms",
691 (int)((i_first-i_start)/1000) );
695 p_sys->block.p_current = p_sys->block.p_first;
698 static int AStreamRefillBlock( stream_t *s );
700 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
702 stream_sys_t *p_sys = s->p_sys;
704 uint8_t *p_data = p_read;
705 unsigned int i_data = 0;
708 if( p_sys->block.p_current == NULL )
713 /* seek within this stream if possible, else use plain old read and discard */
714 stream_sys_t *p_sys = s->p_sys;
715 access_t *p_access = p_sys->p_access;
717 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
719 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
722 while( i_data < i_read )
725 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
726 unsigned int i_copy = VLC_CLIP( (unsigned int)i_current, 0, i_read - i_data);
732 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
738 p_sys->block.i_offset += i_copy;
739 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
741 /* Current block is now empty, switch to next */
742 if( p_sys->block.p_current )
744 p_sys->block.i_offset = 0;
745 p_sys->block.p_current = p_sys->block.p_current->p_next;
747 /*Get a new block if needed */
748 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
755 p_sys->i_pos += i_data;
759 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
761 stream_sys_t *p_sys = s->p_sys;
763 unsigned int i_data = 0;
765 unsigned int i_offset;
767 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
769 /* We can directly give a pointer over our buffer */
770 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
772 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
776 /* We need to create a local copy */
777 if( p_sys->i_peek < i_read )
779 p_sys->p_peek = realloc_or_free( p_sys->p_peek, i_read );
785 p_sys->i_peek = i_read;
788 /* Fill enough data */
789 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
792 block_t **pp_last = p_sys->block.pp_last;
794 if( AStreamRefillBlock( s ) ) break;
796 /* Our buffer are probably filled enough, don't try anymore */
797 if( pp_last == p_sys->block.pp_last ) break;
800 /* Copy what we have */
801 b = p_sys->block.p_current;
802 i_offset = p_sys->block.i_offset;
803 p_data = p_sys->p_peek;
805 while( b && i_data < i_read )
807 unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
808 int i_copy = __MIN( i_current, i_read - i_data );
810 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
815 if( i_offset >= b->i_buffer )
822 *pp_peek = p_sys->p_peek;
826 static int AStreamSeekBlock( stream_t *s, uint64_t i_pos )
828 stream_sys_t *p_sys = s->p_sys;
829 access_t *p_access = p_sys->p_access;
830 int64_t i_offset = i_pos - p_sys->block.i_start;
833 /* We already have thoses data, just update p_current/i_offset */
834 if( i_offset >= 0 && (uint64_t)i_offset < p_sys->block.i_size )
836 block_t *b = p_sys->block.p_first;
839 while( i_current + b->i_buffer < (uint64_t)i_offset )
841 i_current += b->i_buffer;
845 p_sys->block.p_current = b;
846 p_sys->block.i_offset = i_offset - i_current;
848 p_sys->i_pos = i_pos;
853 /* We may need to seek or to read data */
857 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
861 msg_Err( s, "backward seeking impossible (access not seekable)" );
869 bool b_aseek, b_aseekfast;
871 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
872 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
877 msg_Warn( s, "%"PRId64" bytes need to be skipped "
878 "(access non seekable)",
879 i_offset - p_sys->block.i_size );
883 int64_t i_skip = i_offset - p_sys->block.i_size;
885 /* Avg bytes per packets */
886 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
887 /* TODO compute a seek cost instead of fixed threshold */
888 int i_th = b_aseekfast ? 1 : 5;
890 if( i_skip <= i_th * i_avg &&
891 i_skip < STREAM_CACHE_SIZE )
896 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
897 b_seek, i_th*i_avg, i_skip );
903 int64_t i_start, i_end;
904 /* Do the access seek */
906 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
910 block_ChainRelease( p_sys->block.p_first );
913 p_sys->block.i_start = p_sys->i_pos = i_pos;
914 p_sys->block.i_offset = 0;
915 p_sys->block.p_current = NULL;
916 p_sys->block.i_size = 0;
917 p_sys->block.p_first = NULL;
918 p_sys->block.pp_last = &p_sys->block.p_first;
921 if( AStreamRefillBlock( s ) )
925 p_sys->stat.i_seek_time += i_end - i_start;
926 p_sys->stat.i_seek_count++;
933 while( p_sys->block.p_current &&
934 p_sys->i_pos + p_sys->block.p_current->i_buffer - p_sys->block.i_offset <= i_pos )
936 p_sys->i_pos += p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
937 p_sys->block.p_current = p_sys->block.p_current->p_next;
938 p_sys->block.i_offset = 0;
940 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
942 if( p_sys->i_pos != i_pos )
946 while( p_sys->block.i_start + p_sys->block.i_size < i_pos );
948 p_sys->block.i_offset += i_pos - p_sys->i_pos;
949 p_sys->i_pos = i_pos;
957 static int AStreamRefillBlock( stream_t *s )
959 stream_sys_t *p_sys = s->p_sys;
963 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
964 p_sys->block.p_first != p_sys->block.p_current )
966 block_t *b = p_sys->block.p_first;
968 p_sys->block.i_start += b->i_buffer;
969 p_sys->block.i_size -= b->i_buffer;
970 p_sys->block.p_first = b->p_next;
974 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
975 p_sys->block.p_current == p_sys->block.p_first &&
976 p_sys->block.p_current->p_next ) /* At least 2 packets */
978 /* Enough data, don't read more */
982 /* Now read a new block */
983 const int64_t i_start = mdate();
988 if( !vlc_object_alive(s) )
992 if( ( b = AReadBlock( s, &b_eof ) ) )
998 p_sys->stat.i_read_time += mdate() - i_start;
1001 /* Append the block */
1002 p_sys->block.i_size += b->i_buffer;
1003 *p_sys->block.pp_last = b;
1004 p_sys->block.pp_last = &b->p_next;
1007 if( p_sys->block.p_current == NULL )
1008 p_sys->block.p_current = b;
1011 p_sys->stat.i_bytes += b->i_buffer;
1012 p_sys->stat.i_read_count++;
1020 /****************************************************************************
1022 ****************************************************************************/
1023 static int AStreamRefillStream( stream_t *s );
1024 static int AStreamReadNoSeekStream( stream_t *s, void *p_read, unsigned int i_read );
1026 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read )
1028 stream_sys_t *p_sys = s->p_sys;
1032 const uint64_t i_pos_wanted = p_sys->i_pos + i_read;
1034 if( AStreamSeekStream( s, i_pos_wanted ) )
1036 if( p_sys->i_pos != i_pos_wanted )
1041 return AStreamReadNoSeekStream( s, p_read, i_read );
1044 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1046 stream_sys_t *p_sys = s->p_sys;
1047 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1050 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1053 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1054 "start=%"PRId64" offset=%d end=%"PRId64,
1055 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1056 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1059 /* Avoid problem, but that should *never* happen */
1060 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1061 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1063 while( tk->i_end < tk->i_start + p_sys->stream.i_offset + i_read )
1065 if( p_sys->stream.i_used <= 1 )
1067 /* Be sure we will read something */
1068 p_sys->stream.i_used += tk->i_start + p_sys->stream.i_offset + i_read - tk->i_end;
1070 if( AStreamRefillStream( s ) ) break;
1073 if( tk->i_end < tk->i_start + p_sys->stream.i_offset + i_read )
1075 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1079 /* Now, direct pointer or a copy ? */
1080 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1081 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1083 *pp_peek = &tk->p_buffer[i_off];
1087 if( p_sys->i_peek < i_read )
1089 p_sys->p_peek = realloc_or_free( p_sys->p_peek, i_read );
1090 if( !p_sys->p_peek )
1095 p_sys->i_peek = i_read;
1098 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1099 STREAM_CACHE_TRACK_SIZE - i_off );
1100 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1101 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1103 *pp_peek = p_sys->p_peek;
1107 static int AStreamSeekStream( stream_t *s, uint64_t i_pos )
1109 stream_sys_t *p_sys = s->p_sys;
1111 stream_track_t *p_current = &p_sys->stream.tk[p_sys->stream.i_tk];
1112 access_t *p_access = p_sys->p_access;
1114 if( p_current->i_start >= p_current->i_end && i_pos >= p_current->i_end )
1118 msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1119 " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1120 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1122 p_sys->stream.i_offset,
1127 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1128 if( !b_aseek && i_pos < p_current->i_start )
1130 msg_Warn( s, "AStreamSeekStream: can't seek" );
1131 return VLC_EGENERIC;
1135 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_afastseek );
1137 /* FIXME compute seek cost (instead of static 'stupid' value) */
1138 uint64_t i_skip_threshold;
1140 i_skip_threshold = b_afastseek ? 128 : 3*p_sys->stream.i_read_size;
1142 i_skip_threshold = INT64_MAX;
1144 /* Date the current track */
1145 p_current->i_date = mdate();
1147 /* Search a new track slot */
1148 stream_track_t *tk = NULL;
1151 /* Prefer the current track */
1152 if( p_current->i_start <= i_pos && i_pos <= p_current->i_end + i_skip_threshold )
1155 i_tk_idx = p_sys->stream.i_tk;
1159 /* Try to maximize already read data */
1160 for( int i = 0; i < STREAM_CACHE_TRACK; i++ )
1162 stream_track_t *t = &p_sys->stream.tk[i];
1164 if( t->i_start > i_pos || i_pos > t->i_end )
1167 if( !tk || tk->i_end < t->i_end )
1176 /* Use the oldest unused */
1177 for( int i = 0; i < STREAM_CACHE_TRACK; i++ )
1179 stream_track_t *t = &p_sys->stream.tk[i];
1181 if( !tk || tk->i_date > t->i_date )
1188 assert( i_tk_idx >= 0 && i_tk_idx < STREAM_CACHE_TRACK );
1190 if( tk != p_current )
1191 i_skip_threshold = 0;
1192 if( tk->i_start <= i_pos && i_pos <= tk->i_end + i_skip_threshold )
1195 msg_Err( s, "AStreamSeekStream: reusing %d start=%"PRId64
1196 " end=%"PRId64"(%s)",
1197 i_tk_idx, tk->i_start, tk->i_end,
1198 tk != p_current ? "seek" : i_pos > tk->i_end ? "skip" : "noseek" );
1200 if( tk != p_current )
1204 /* Seek at the end of the buffer
1205 * TODO it is stupid to seek now, it would be better to delay it
1207 if( ASeek( s, tk->i_end ) )
1208 return VLC_EGENERIC;
1210 else if( i_pos > tk->i_end )
1212 uint64_t i_skip = i_pos - tk->i_end;
1215 const int i_read_max = __MIN( 10 * STREAM_READ_ATONCE, i_skip );
1216 if( AStreamReadNoSeekStream( s, NULL, i_read_max ) != i_read_max )
1217 return VLC_EGENERIC;
1218 i_skip -= i_read_max;
1225 msg_Err( s, "AStreamSeekStream: hard seek" );
1227 /* Nothing good, seek and choose oldest segment */
1228 if( ASeek( s, i_pos ) )
1229 return VLC_EGENERIC;
1231 tk->i_start = i_pos;
1234 p_sys->stream.i_offset = i_pos - tk->i_start;
1235 p_sys->stream.i_tk = i_tk_idx;
1236 p_sys->i_pos = i_pos;
1238 /* If there is not enough data left in the track, refill */
1239 /* TODO How to get a correct value for
1240 * - refilling threshold
1241 * - how much to refill
1243 if( tk->i_end < tk->i_start + p_sys->stream.i_offset + p_sys->stream.i_read_size )
1245 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1246 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1248 if( AStreamRefillStream( s ) && i_pos >= tk->i_end )
1249 return VLC_EGENERIC;
1254 static int AStreamReadNoSeekStream( stream_t *s, void *p_read, unsigned int i_read )
1256 stream_sys_t *p_sys = s->p_sys;
1257 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1259 uint8_t *p_data = (uint8_t *)p_read;
1260 unsigned int i_data = 0;
1262 if( tk->i_start >= tk->i_end )
1266 msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1267 " offset=%d end=%"PRId64,
1268 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1269 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1272 while( i_data < i_read )
1274 unsigned i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1275 unsigned int i_current =
1276 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1277 STREAM_CACHE_TRACK_SIZE - i_off );
1278 int i_copy = __MIN( i_current, i_read - i_data );
1280 if( i_copy <= 0 ) break; /* EOF */
1283 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1286 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1290 p_sys->stream.i_offset += i_copy;
1292 /* Update pos now */
1293 p_sys->i_pos += i_copy;
1296 p_sys->stream.i_used += i_copy;
1298 if( tk->i_end + i_data <= tk->i_start + p_sys->stream.i_offset + i_read )
1300 const unsigned i_read_requested = VLC_CLIP( i_read - i_data,
1301 STREAM_READ_ATONCE / 2,
1302 STREAM_READ_ATONCE * 10 );
1304 if( p_sys->stream.i_used < i_read_requested )
1305 p_sys->stream.i_used = i_read_requested;
1307 if( AStreamRefillStream( s ) )
1310 if( tk->i_start >= tk->i_end ) break;
1319 static int AStreamRefillStream( stream_t *s )
1321 stream_sys_t *p_sys = s->p_sys;
1322 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1324 /* We read but won't increase i_start after initial start + offset */
1326 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1327 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1328 bool b_read = false;
1329 int64_t i_start, i_stop;
1331 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1334 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1335 p_sys->stream.i_used, i_toread );
1339 while( i_toread > 0 )
1341 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1344 if( !vlc_object_alive(s) )
1345 return VLC_EGENERIC;
1347 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1348 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1350 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1355 else if( i_read == 0 )
1358 return VLC_EGENERIC;
1364 tk->i_end += i_read;
1366 /* Windows of STREAM_CACHE_TRACK_SIZE */
1367 if( tk->i_start + STREAM_CACHE_TRACK_SIZE < tk->i_end )
1369 unsigned i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1371 tk->i_start += i_invalid;
1372 p_sys->stream.i_offset -= i_invalid;
1376 p_sys->stream.i_used -= i_read;
1378 p_sys->stat.i_bytes += i_read;
1379 p_sys->stat.i_read_count++;
1383 p_sys->stat.i_read_time += i_stop - i_start;
1388 static void AStreamPrebufferStream( stream_t *s )
1390 stream_sys_t *p_sys = s->p_sys;
1392 int64_t i_first = 0;
1395 msg_Dbg( s, "starting pre-buffering" );
1399 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1401 int64_t i_date = mdate();
1403 int i_buffered = tk->i_end - tk->i_start;
1405 if( !vlc_object_alive(s) || i_buffered >= STREAM_CACHE_PREBUFFER_SIZE )
1410 p_sys->stat.i_bytes = i_buffered;
1411 p_sys->stat.i_read_time = i_date - i_start;
1412 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1413 (p_sys->stat.i_read_time+1);
1415 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1417 p_sys->stat.i_bytes,
1418 p_sys->stat.i_read_time / INT64_C(1000000),
1419 i_byterate / 1024 );
1424 i_read = STREAM_CACHE_TRACK_SIZE - i_buffered;
1425 i_read = __MIN( (int)p_sys->stream.i_read_size, i_read );
1426 i_read = AReadStream( s, &tk->p_buffer[i_buffered], i_read );
1429 else if( i_read == 0 )
1435 msg_Dbg( s, "received first data after %d ms",
1436 (int)((i_first-i_start)/1000) );
1439 tk->i_end += i_read;
1441 p_sys->stat.i_read_count++;
1445 /****************************************************************************
1447 ****************************************************************************/
1449 * Read from the stream untill first newline.
1450 * \param s Stream handle to read from
1451 * \return A pointer to the allocated output string. You need to free this when you are done.
1453 #define STREAM_PROBE_LINE 2048
1454 #define STREAM_LINE_MAX (2048*100)
1455 char *stream_ReadLine( stream_t *s )
1457 char *p_line = NULL;
1458 int i_line = 0, i_read = 0;
1460 while( i_read < STREAM_LINE_MAX )
1463 const uint8_t *p_data;
1467 /* Probe new data */
1468 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1469 if( i_data <= 0 ) break; /* No more data */
1472 i_pos = stream_Tell( s );
1473 if( i_pos == 0 && i_data >= 2 )
1475 const char *psz_encoding = NULL;
1477 if( !memcmp( p_data, "\xFF\xFE", 2 ) )
1479 psz_encoding = "UTF-16LE";
1480 s->p_text->b_little_endian = true;
1482 else if( !memcmp( p_data, "\xFE\xFF", 2 ) )
1484 psz_encoding = "UTF-16BE";
1487 /* Open the converter if we need it */
1488 if( psz_encoding != NULL )
1490 msg_Dbg( s, "UTF-16 BOM detected" );
1491 s->p_text->i_char_width = 2;
1492 s->p_text->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1493 if( s->p_text->conv == (vlc_iconv_t)-1 )
1494 msg_Err( s, "iconv_open failed" );
1498 if( i_data % s->p_text->i_char_width )
1500 /* keep i_char_width boundary */
1501 i_data = i_data - ( i_data % s->p_text->i_char_width );
1502 msg_Warn( s, "the read is not i_char_width compatible");
1508 /* Check if there is an EOL */
1509 if( s->p_text->i_char_width == 1 )
1511 /* UTF-8: 0A <LF> */
1512 psz_eol = memchr( p_data, '\n', i_data );
1513 if( psz_eol == NULL )
1514 /* UTF-8: 0D <CR> */
1515 psz_eol = memchr( p_data, '\r', i_data );
1519 const uint8_t *p_last = p_data + i_data - s->p_text->i_char_width;
1520 uint16_t eol = s->p_text->b_little_endian ? 0x0A00 : 0x00A0;
1522 assert( s->p_text->i_char_width == 2 );
1524 /* UTF-16: 000A <LF> */
1525 for( const uint8_t *p = p_data; p <= p_last; p += 2 )
1527 if( U16_AT( p ) == eol )
1529 psz_eol = (char *)p + 1;
1534 if( psz_eol == NULL )
1535 { /* UTF-16: 000D <CR> */
1536 eol = s->p_text->b_little_endian ? 0x0D00 : 0x00D0;
1537 for( const uint8_t *p = p_data; p <= p_last; p += 2 )
1539 if( U16_AT( p ) == eol )
1541 psz_eol = (char *)p + 1;
1550 i_data = (psz_eol - (char *)p_data) + 1;
1551 p_line = realloc_or_free( p_line,
1552 i_line + i_data + s->p_text->i_char_width ); /* add \0 */
1555 i_data = stream_Read( s, &p_line[i_line], i_data );
1556 if( i_data <= 0 ) break; /* Hmmm */
1557 i_line += i_data - s->p_text->i_char_width; /* skip \n */;
1560 /* We have our line */
1564 /* Read data (+1 for easy \0 append) */
1565 p_line = realloc_or_free( p_line,
1566 i_line + STREAM_PROBE_LINE + s->p_text->i_char_width );
1569 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1570 if( i_data <= 0 ) break; /* Hmmm */
1578 for( j = 0; j < s->p_text->i_char_width; j++ )
1580 p_line[i_line + j] = '\0';
1582 i_line += s->p_text->i_char_width; /* the added \0 */
1583 if( s->p_text->i_char_width > 1 )
1586 size_t i_in = 0, i_out = 0;
1587 const char * p_in = NULL;
1588 char * p_out = NULL;
1589 char * psz_new_line = NULL;
1592 /* UTF-8 needs at most 150% of the buffer as many as UTF-16 */
1593 i_new_line = i_line * 3 / 2;
1594 psz_new_line = malloc( i_new_line );
1595 if( psz_new_line == NULL )
1597 i_in = (size_t)i_line;
1598 i_out = (size_t)i_new_line;
1600 p_out = psz_new_line;
1602 if( vlc_iconv( s->p_text->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1604 msg_Err( s, "iconv failed" );
1605 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1608 p_line = psz_new_line;
1609 i_line = (size_t)i_new_line - i_out; /* does not include \0 */
1612 /* Remove trailing LF/CR */
1613 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1614 p_line[i_line-2] == '\n') ) i_line--;
1616 /* Make sure the \0 is there */
1617 p_line[i_line-1] = '\0';
1623 /* We failed to read any data, probably EOF */
1627 if( s->p_text->conv != (vlc_iconv_t)(-1) )
1628 vlc_iconv_close( s->p_text->conv );
1629 s->p_text->conv = (vlc_iconv_t)(-1);
1633 /****************************************************************************
1634 * Access reading/seeking wrappers to handle concatenated streams.
1635 ****************************************************************************/
1636 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read )
1638 stream_sys_t *p_sys = s->p_sys;
1639 access_t *p_access = p_sys->p_access;
1640 input_thread_t *p_input = s->p_input;
1641 int i_read_orig = i_read;
1643 if( !p_sys->i_list )
1645 i_read = p_access->pf_read( p_access, p_read, i_read );
1650 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1651 stats_Update( p_input->p->counters.p_read_bytes, i_read, &total );
1652 stats_Update( p_input->p->counters.p_input_bitrate, total, NULL );
1653 stats_Update( p_input->p->counters.p_read_packets, 1, NULL );
1654 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1659 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1662 /* If we reached an EOF then switch to the next stream in the list */
1663 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1665 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1666 access_t *p_list_access;
1668 msg_Dbg( s, "opening input `%s'", psz_name );
1670 p_list_access = access_New( s, s->p_input, p_access->psz_access, "", psz_name );
1672 if( !p_list_access ) return 0;
1674 if( p_sys->p_list_access != p_access )
1675 access_Delete( p_sys->p_list_access );
1677 p_sys->p_list_access = p_list_access;
1679 /* We have to read some data */
1680 return AReadStream( s, p_read, i_read_orig );
1683 /* Update read bytes in input */
1688 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1689 stats_Update( p_input->p->counters.p_read_bytes, i_read, &total );
1690 stats_Update( p_input->p->counters.p_input_bitrate, total, NULL );
1691 stats_Update( p_input->p->counters.p_read_packets, 1, NULL );
1692 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1697 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
1699 stream_sys_t *p_sys = s->p_sys;
1700 access_t *p_access = p_sys->p_access;
1701 input_thread_t *p_input = s->p_input;
1705 if( !p_sys->i_list )
1707 p_block = p_access->pf_block( p_access );
1708 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1709 if( p_input && p_block && libvlc_stats (p_access) )
1713 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1714 stats_Update( p_input->p->counters.p_read_bytes,
1715 p_block->i_buffer, &total );
1716 stats_Update( p_input->p->counters.p_input_bitrate,
1718 stats_Update( p_input->p->counters.p_read_packets, 1, NULL );
1719 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1724 p_block = p_sys->p_list_access->pf_block( p_sys->p_list_access );
1725 b_eof = p_sys->p_list_access->info.b_eof;
1726 if( pb_eof ) *pb_eof = b_eof;
1728 /* If we reached an EOF then switch to the next stream in the list */
1729 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1731 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1732 access_t *p_list_access;
1734 msg_Dbg( s, "opening input `%s'", psz_name );
1736 p_list_access = access_New( s, s->p_input, p_access->psz_access, "", psz_name );
1738 if( !p_list_access ) return 0;
1740 if( p_sys->p_list_access != p_access )
1741 access_Delete( p_sys->p_list_access );
1743 p_sys->p_list_access = p_list_access;
1745 /* We have to read some data */
1746 return AReadBlock( s, pb_eof );
1754 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1755 stats_Update( p_input->p->counters.p_read_bytes,
1756 p_block->i_buffer, &total );
1757 stats_Update( p_input->p->counters.p_input_bitrate, total, NULL );
1758 stats_Update( p_input->p->counters.p_read_packets, 1 , NULL);
1759 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1765 static int ASeek( stream_t *s, uint64_t i_pos )
1767 stream_sys_t *p_sys = s->p_sys;
1768 access_t *p_access = p_sys->p_access;
1770 /* Check which stream we need to access */
1776 access_t *p_list_access = 0;
1778 for( i = 0; i < p_sys->i_list - 1; i++ )
1780 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1781 i_size += p_sys->list[i]->i_size;
1783 psz_name = p_sys->list[i]->psz_path;
1785 if( i != p_sys->i_list_index )
1786 msg_Dbg( s, "opening input `%s'", psz_name );
1788 if( i != p_sys->i_list_index && i != 0 )
1791 access_New( s, s->p_input, p_access->psz_access, "", psz_name );
1793 else if( i != p_sys->i_list_index )
1795 p_list_access = p_access;
1800 if( p_sys->p_list_access != p_access )
1801 access_Delete( p_sys->p_list_access );
1803 p_sys->p_list_access = p_list_access;
1806 p_sys->i_list_index = i;
1807 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1811 return p_access->pf_seek( p_access, i_pos );
1816 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
1817 * "p_read" is NULL then data are skipped instead of read.
1818 * \return The real number of bytes read/skip. If this value is less
1819 * than i_read that means that it's the end of the stream.
1820 * \note stream_Read increments the stream position, and when p_read is NULL,
1821 * this is its only task.
1823 int stream_Read( stream_t *s, void *p_read, int i_read )
1825 return s->pf_read( s, p_read, i_read );
1829 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
1830 * \return The real number of valid bytes. If it's less
1831 * or equal to 0, *pp_peek is invalid.
1832 * \note pp_peek is a pointer to internal buffer and it will be invalid as
1833 * soons as other stream_* functions are called.
1834 * \note Contrary to stream_Read, stream_Peek doesn't modify the stream
1835 * position, and doesn't necessarily involve copying of data. It's mainly
1836 * used by the modules to quickly probe the (head of the) stream.
1837 * \note Due to input limitation, the return value could be less than i_peek
1838 * without meaning the end of the stream (but only when you have i_peek >=
1839 * p_input->i_bufsize)
1841 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
1843 return s->pf_peek( s, pp_peek, i_peek );
1847 * Use to control the "stream_t *". Look at #stream_query_e for
1848 * possible "i_query" value and format arguments. Return VLC_SUCCESS
1849 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
1851 int stream_vaControl( stream_t *s, int i_query, va_list args )
1853 return s->pf_control( s, i_query, args );
1859 void stream_Delete( stream_t *s )
1864 int stream_Control( stream_t *s, int i_query, ... )
1870 return VLC_EGENERIC;
1872 va_start( args, i_query );
1873 i_result = s->pf_control( s, i_query, args );
1879 * Read "i_size" bytes and store them in a block_t.
1880 * It always read i_size bytes unless you are at the end of the stream
1881 * where it return what is available.
1883 block_t *stream_Block( stream_t *s, int i_size )
1885 if( i_size <= 0 ) return NULL;
1887 /* emulate block read */
1888 block_t *p_bk = block_Alloc( i_size );
1891 int i_read = stream_Read( s, p_bk->p_buffer, i_size );
1894 p_bk->i_buffer = i_read;
1897 block_Release( p_bk );
1903 * Read the remaining of the data if there is less than i_max_size bytes, otherwise
1906 * The stream position is unknown after the call.
1908 block_t *stream_BlockRemaining( stream_t *s, int i_max_size )
1910 int i_allocate = __MIN(1000000, i_max_size);
1911 int64_t i_size = stream_Size( s );
1914 int64_t i_position = stream_Tell( s );
1915 if( i_position + i_max_size < i_size )
1917 msg_Err( s, "Remaining stream size is greater than %d bytes",
1921 i_allocate = i_size - i_position;
1923 if( i_allocate <= 0 )
1926 block_t *p_block = block_Alloc( i_allocate );
1930 int i_read = stream_Read( s, &p_block->p_buffer[i_index],
1931 p_block->i_buffer - i_index);
1935 i_max_size -= i_read;
1936 if( i_max_size <= 0 )
1938 p_block = block_Realloc( p_block, 0, p_block->i_buffer +
1939 __MIN(1000000, i_max_size) );
1942 p_block->i_buffer = i_index;