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)
119 STREAM_METHOD_STREAM,
120 STREAM_METHOD_READDIR
121 } stream_read_method_t;
127 stream_read_method_t method; /* method to use */
129 uint64_t i_pos; /* Current reading offset */
131 /* Method 1: pf_block */
134 uint64_t i_start; /* Offset of block for p_first */
135 uint64_t i_offset; /* Offset for data in p_current */
136 block_t *p_current; /* Current block */
138 uint64_t i_size; /* Total amount of data in the list */
144 /* Method 2: for pf_read */
147 unsigned i_offset; /* Buffer offset in the current track */
148 int i_tk; /* Current track */
149 stream_track_t tk[STREAM_CACHE_TRACK];
155 unsigned i_used; /* Used since last read */
156 unsigned i_read_size;
160 /* Peek temporary buffer */
164 /* Stat for both method */
167 bool b_fastseek; /* From access */
169 /* Stat about reading data */
170 uint64_t i_read_count;
172 uint64_t i_read_time;
174 /* Stat about seek */
175 unsigned i_seek_count;
176 uint64_t i_seek_time;
182 access_entry_t **list;
184 access_t *p_list_access;
188 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read );
189 static int AStreamPeekBlock( stream_t *s, const uint8_t **p_peek, unsigned int i_read );
190 static int AStreamSeekBlock( stream_t *s, uint64_t i_pos );
191 static void AStreamPrebufferBlock( stream_t *s );
192 static block_t *AReadBlock( stream_t *s, bool *pb_eof );
195 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read );
196 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read );
197 static int AStreamSeekStream( stream_t *s, uint64_t i_pos );
198 static void AStreamPrebufferStream( stream_t *s );
199 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read );
202 static int AStreamReadDir( stream_t *s, input_item_node_t *p_node );
205 static int AStreamGenericError( ) { return VLC_EGENERIC; }
206 static int AStreamControl( stream_t *s, int i_query, va_list );
207 static void AStreamDestroy( stream_t *s );
208 static int ASeek( stream_t *s, uint64_t i_pos );
210 /****************************************************************************
211 * stream_CommonNew: create an empty stream structure
212 ****************************************************************************/
213 stream_t *stream_CommonNew( vlc_object_t *p_obj )
215 stream_t *s = (stream_t *)vlc_custom_create( p_obj, sizeof(*s), "stream" );
220 s->p_text = malloc( sizeof(*s->p_text) );
223 vlc_object_release( s );
227 /* UTF16 and UTF32 text file conversion */
228 s->p_text->conv = (vlc_iconv_t)(-1);
229 s->p_text->i_char_width = 1;
230 s->p_text->b_little_endian = false;
235 void stream_CommonDelete( stream_t *s )
239 if( s->p_text->conv != (vlc_iconv_t)(-1) )
240 vlc_iconv_close( s->p_text->conv );
243 free( s->psz_access );
245 vlc_object_release( s );
249 /****************************************************************************
250 * stream_UrlNew: create a stream from a access
251 ****************************************************************************/
252 stream_t *stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
254 const char *psz_access, *psz_demux, *psz_path, *psz_anchor;
260 char psz_dup[strlen( psz_url ) + 1];
261 strcpy( psz_dup, psz_url );
262 input_SplitMRL( &psz_access, &psz_demux, &psz_path, &psz_anchor, psz_dup );
264 /* Now try a real access */
265 p_access = access_New( p_parent, NULL, psz_access, psz_demux, psz_path );
266 if( p_access == NULL )
268 msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
272 return stream_AccessNew( p_access, NULL );
275 stream_t *stream_AccessNew( access_t *p_access, char **ppsz_list )
277 stream_t *s = stream_CommonNew( VLC_OBJECT(p_access) );
283 s->p_input = p_access->p_input;
284 s->psz_access = strdup( p_access->psz_access );
285 s->psz_path = strdup( p_access->psz_location );
286 s->p_sys = p_sys = malloc( sizeof( *p_sys ) );
287 if( !s->psz_access || !s->psz_path || !s->p_sys )
289 stream_CommonDelete( s );
293 s->pf_read = AStreamGenericError; /* Replaced later */
294 s->pf_peek = AStreamGenericError;
295 s->pf_readdir = AStreamGenericError;
296 s->pf_control = AStreamControl;
297 s->pf_destroy = AStreamDestroy;
300 p_sys->p_access = p_access;
301 assert( p_access->pf_block || p_access->pf_read || p_access->pf_readdir );
302 if( p_access->pf_block )
303 p_sys->method = STREAM_METHOD_BLOCK;
304 else if( p_access->pf_read )
305 p_sys->method = STREAM_METHOD_STREAM;
307 p_sys->method = STREAM_METHOD_READDIR;
309 p_sys->i_pos = p_access->info.i_pos;
312 access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
313 p_sys->stat.i_bytes = 0;
314 p_sys->stat.i_read_time = 0;
315 p_sys->stat.i_read_count = 0;
316 p_sys->stat.i_seek_count = 0;
317 p_sys->stat.i_seek_time = 0;
319 TAB_INIT( p_sys->i_list, p_sys->list );
320 p_sys->i_list_index = 0;
321 p_sys->p_list_access = NULL;
323 /* Get the additional list of inputs if any (for concatenation) */
324 if( ppsz_list && ppsz_list[0] )
326 access_entry_t *p_entry = malloc( sizeof(*p_entry) );
330 p_entry->i_size = access_GetSize( p_access );
331 p_entry->psz_path = strdup( p_access->psz_location );
332 if( !p_entry->psz_path )
337 p_sys->p_list_access = p_access;
338 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
339 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
340 p_entry->psz_path, p_entry->i_size );
342 for( int i = 0; ppsz_list[i] != NULL; i++ )
344 char *psz_name = strdup( ppsz_list[i] );
349 access_t *p_tmp = access_New( p_access, p_access->p_input,
350 p_access->psz_access, "", psz_name );
357 p_entry = malloc( sizeof(*p_entry) );
360 p_entry->i_size = access_GetSize( p_tmp );
361 p_entry->psz_path = psz_name;
362 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
363 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
364 p_entry->psz_path, p_entry->i_size );
366 access_Delete( p_tmp );
372 p_sys->p_peek = NULL;
374 if( p_sys->method == STREAM_METHOD_BLOCK )
376 msg_Dbg( s, "Using block method for AStream*" );
377 s->pf_read = AStreamReadBlock;
378 s->pf_peek = AStreamPeekBlock;
380 /* Init all fields of p_sys->block */
381 p_sys->block.i_start = p_sys->i_pos;
382 p_sys->block.i_offset = 0;
383 p_sys->block.p_current = NULL;
384 p_sys->block.i_size = 0;
385 p_sys->block.p_first = NULL;
386 p_sys->block.pp_last = &p_sys->block.p_first;
388 /* Do the prebuffering */
389 AStreamPrebufferBlock( s );
391 if( p_sys->block.i_size <= 0 )
393 msg_Err( s, "cannot pre fill buffer" );
397 else if ( p_sys->method == STREAM_METHOD_STREAM )
401 msg_Dbg( s, "Using stream method for AStream*" );
403 s->pf_read = AStreamReadStream;
404 s->pf_peek = AStreamPeekStream;
406 /* Allocate/Setup our tracks */
407 p_sys->stream.i_offset = 0;
408 p_sys->stream.i_tk = 0;
409 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
410 if( p_sys->stream.p_buffer == NULL )
412 p_sys->stream.i_used = 0;
413 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
414 #if STREAM_READ_ATONCE < 256
415 # error "Invalid STREAM_READ_ATONCE value"
418 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
420 p_sys->stream.tk[i].i_date = 0;
421 p_sys->stream.tk[i].i_start = p_sys->i_pos;
422 p_sys->stream.tk[i].i_end = p_sys->i_pos;
423 p_sys->stream.tk[i].p_buffer=
424 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
427 /* Do the prebuffering */
428 AStreamPrebufferStream( s );
430 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
432 msg_Err( s, "cannot pre fill buffer" );
438 msg_Dbg( s, "Using readdir method for AStream*" );
440 assert( p_sys->method == STREAM_METHOD_READDIR );
441 s->pf_readdir = AStreamReadDir;
447 if( p_sys->method == STREAM_METHOD_BLOCK )
451 else if( p_sys->method == STREAM_METHOD_STREAM )
453 free( p_sys->stream.p_buffer );
455 while( p_sys->i_list > 0 )
456 free( p_sys->list[--(p_sys->i_list)] );
459 stream_CommonDelete( s );
460 access_Delete( p_access );
464 /****************************************************************************
466 ****************************************************************************/
467 static void AStreamDestroy( stream_t *s )
469 stream_sys_t *p_sys = s->p_sys;
471 if( p_sys->method == STREAM_METHOD_BLOCK )
472 block_ChainRelease( p_sys->block.p_first );
473 else if( p_sys->method == STREAM_METHOD_STREAM )
474 free( p_sys->stream.p_buffer );
476 free( p_sys->p_peek );
478 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
479 access_Delete( p_sys->p_list_access );
481 while( p_sys->i_list-- )
483 free( p_sys->list[p_sys->i_list]->psz_path );
484 free( p_sys->list[p_sys->i_list] );
488 stream_CommonDelete( s );
489 access_Delete( p_sys->p_access );
493 /****************************************************************************
494 * AStreamControlReset:
495 ****************************************************************************/
496 static void AStreamControlReset( stream_t *s )
498 stream_sys_t *p_sys = s->p_sys;
500 p_sys->i_pos = p_sys->p_access->info.i_pos;
502 if( p_sys->method == STREAM_METHOD_BLOCK )
504 block_ChainRelease( p_sys->block.p_first );
506 /* Init all fields of p_sys->block */
507 p_sys->block.i_start = p_sys->i_pos;
508 p_sys->block.i_offset = 0;
509 p_sys->block.p_current = NULL;
510 p_sys->block.i_size = 0;
511 p_sys->block.p_first = NULL;
512 p_sys->block.pp_last = &p_sys->block.p_first;
514 /* Do the prebuffering */
515 AStreamPrebufferBlock( s );
521 assert( p_sys->method == STREAM_METHOD_STREAM );
523 /* Setup our tracks */
524 p_sys->stream.i_offset = 0;
525 p_sys->stream.i_tk = 0;
526 p_sys->stream.i_used = 0;
528 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
530 p_sys->stream.tk[i].i_date = 0;
531 p_sys->stream.tk[i].i_start = p_sys->i_pos;
532 p_sys->stream.tk[i].i_end = p_sys->i_pos;
535 /* Do the prebuffering */
536 AStreamPrebufferStream( s );
540 /****************************************************************************
541 * AStreamControlUpdate:
542 ****************************************************************************/
543 static void AStreamControlUpdate( stream_t *s )
545 stream_sys_t *p_sys = s->p_sys;
547 p_sys->i_pos = p_sys->p_access->info.i_pos;
552 for( i = 0; i < p_sys->i_list_index; i++ )
554 p_sys->i_pos += p_sys->list[i]->i_size;
559 #define static_control_match(foo) \
560 static_assert((unsigned) STREAM_##foo == ACCESS_##foo, "Mismatch")
562 /****************************************************************************
564 ****************************************************************************/
565 static int AStreamControl( stream_t *s, int i_query, va_list args )
567 stream_sys_t *p_sys = s->p_sys;
568 access_t *p_access = p_sys->p_access;
570 static_control_match(CAN_SEEK);
571 static_control_match(CAN_FASTSEEK);
572 static_control_match(CAN_PAUSE);
573 static_control_match(CAN_CONTROL_PACE);
574 static_control_match(GET_PTS_DELAY);
575 static_control_match(GET_TITLE_INFO);
576 static_control_match(GET_TITLE);
577 static_control_match(GET_SEEKPOINT);
578 static_control_match(GET_META);
579 static_control_match(GET_CONTENT_TYPE);
580 static_control_match(GET_SIGNAL);
581 static_control_match(SET_PAUSE_STATE);
582 static_control_match(SET_TITLE);
583 static_control_match(SET_SEEKPOINT);
584 static_control_match(SET_PRIVATE_ID_STATE);
585 static_control_match(SET_PRIVATE_ID_CA);
586 static_control_match(GET_PRIVATE_ID_STATE);
590 case STREAM_CAN_SEEK:
591 case STREAM_CAN_FASTSEEK:
592 case STREAM_CAN_PAUSE:
593 case STREAM_CAN_CONTROL_PACE:
594 case STREAM_GET_PTS_DELAY:
595 case STREAM_GET_TITLE_INFO:
596 case STREAM_GET_TITLE:
597 case STREAM_GET_SEEKPOINT:
598 case STREAM_GET_META:
599 case STREAM_GET_CONTENT_TYPE:
600 case STREAM_GET_SIGNAL:
601 case STREAM_SET_PAUSE_STATE:
602 case STREAM_SET_PRIVATE_ID_STATE:
603 case STREAM_SET_PRIVATE_ID_CA:
604 case STREAM_GET_PRIVATE_ID_STATE:
605 return access_vaControl( p_access, i_query, args );
607 case STREAM_GET_SIZE:
609 uint64_t *pi_64 = va_arg( args, uint64_t * );
610 if( s->p_sys->i_list )
614 for( i = 0; i < s->p_sys->i_list; i++ )
615 *pi_64 += s->p_sys->list[i]->i_size;
618 *pi_64 = access_GetSize( p_access );
622 case STREAM_GET_POSITION:
623 *va_arg( args, uint64_t * ) = p_sys->i_pos;
626 case STREAM_SET_POSITION:
628 uint64_t offset = va_arg( args, uint64_t );
629 switch( p_sys->method )
631 case STREAM_METHOD_BLOCK:
632 return AStreamSeekBlock( s, offset );
633 case STREAM_METHOD_STREAM:
634 return AStreamSeekStream( s, offset );
641 case STREAM_UPDATE_SIZE:
642 AStreamControlUpdate( s );
645 case STREAM_SET_TITLE:
646 case STREAM_SET_SEEKPOINT:
648 int ret = access_vaControl( p_access, i_query, args );
649 if( ret == VLC_SUCCESS )
650 AStreamControlReset( s );
654 case STREAM_IS_DIRECTORY:
656 bool *pb_canreaddir = va_arg( args, bool * );
657 *pb_canreaddir = p_sys->method == STREAM_METHOD_READDIR;
661 case STREAM_SET_RECORD_STATE:
663 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
669 /****************************************************************************
671 ****************************************************************************/
672 static void AStreamPrebufferBlock( stream_t *s )
674 stream_sys_t *p_sys = s->p_sys;
679 msg_Dbg( s, "starting pre-buffering" );
683 const int64_t i_date = mdate();
687 if( !vlc_object_alive(s) || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE )
692 p_sys->stat.i_bytes = p_sys->block.i_size;
693 p_sys->stat.i_read_time = i_date - i_start;
694 i_byterate = ( CLOCK_FREQ * p_sys->stat.i_bytes ) /
695 (p_sys->stat.i_read_time + 1);
697 msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
700 p_sys->stat.i_read_time / CLOCK_FREQ,
706 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
715 /* Append the block */
716 p_sys->block.i_size += b->i_buffer;
717 *p_sys->block.pp_last = b;
718 p_sys->block.pp_last = &b->p_next;
720 p_sys->stat.i_read_count++;
727 msg_Dbg( s, "received first data after %d ms",
728 (int)((i_first-i_start)/1000) );
732 p_sys->block.p_current = p_sys->block.p_first;
735 static int AStreamRefillBlock( stream_t *s );
737 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
739 stream_sys_t *p_sys = s->p_sys;
741 uint8_t *p_data = p_read;
742 unsigned int i_data = 0;
745 if( p_sys->block.p_current == NULL )
750 /* seek within this stream if possible, else use plain old read and discard */
751 access_t *p_access = p_sys->p_access;
754 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
756 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
759 while( i_data < i_read )
762 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
763 unsigned int i_copy = VLC_CLIP( (unsigned int)i_current, 0, i_read - i_data);
769 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
775 p_sys->block.i_offset += i_copy;
776 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
778 /* Current block is now empty, switch to next */
779 p_sys->block.i_offset = 0;
780 p_sys->block.p_current = p_sys->block.p_current->p_next;
782 /*Get a new block if needed */
783 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
785 assert( p_sys->block.p_current );
789 p_sys->i_pos += i_data;
793 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
795 stream_sys_t *p_sys = s->p_sys;
797 unsigned int i_data = 0;
799 unsigned int i_offset;
801 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
803 /* We can directly give a pointer over our buffer */
804 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
806 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
810 /* We need to create a local copy */
811 if( p_sys->i_peek < i_read )
813 p_sys->p_peek = realloc_or_free( p_sys->p_peek, i_read );
819 p_sys->i_peek = i_read;
822 /* Fill enough data */
823 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
826 block_t **pp_last = p_sys->block.pp_last;
828 if( AStreamRefillBlock( s ) ) break;
830 /* Our buffer are probably filled enough, don't try anymore */
831 if( pp_last == p_sys->block.pp_last ) break;
834 /* Copy what we have */
835 b = p_sys->block.p_current;
836 i_offset = p_sys->block.i_offset;
837 p_data = p_sys->p_peek;
839 while( b && i_data < i_read )
841 unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
842 int i_copy = __MIN( i_current, i_read - i_data );
844 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
849 if( i_offset >= b->i_buffer )
856 *pp_peek = p_sys->p_peek;
860 static int AStreamSeekBlock( stream_t *s, uint64_t i_pos )
862 stream_sys_t *p_sys = s->p_sys;
863 access_t *p_access = p_sys->p_access;
864 int64_t i_offset = i_pos - p_sys->block.i_start;
867 /* We already have thoses data, just update p_current/i_offset */
868 if( i_offset >= 0 && (uint64_t)i_offset < p_sys->block.i_size )
870 block_t *b = p_sys->block.p_first;
873 while( i_current + b->i_buffer < (uint64_t)i_offset )
875 i_current += b->i_buffer;
879 p_sys->block.p_current = b;
880 p_sys->block.i_offset = i_offset - i_current;
882 p_sys->i_pos = i_pos;
887 /* We may need to seek or to read data */
891 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
895 msg_Err( s, "backward seeking impossible (access not seekable)" );
903 bool b_aseek, b_aseekfast;
905 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
906 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
911 msg_Warn( s, "%"PRId64" bytes need to be skipped "
912 "(access non seekable)",
913 i_offset - p_sys->block.i_size );
917 int64_t i_skip = i_offset - p_sys->block.i_size;
919 /* Avg bytes per packets */
920 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
921 /* TODO compute a seek cost instead of fixed threshold */
922 int i_th = b_aseekfast ? 1 : 5;
924 if( i_skip <= i_th * i_avg &&
925 i_skip < STREAM_CACHE_SIZE )
930 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
931 b_seek, i_th*i_avg, i_skip );
937 int64_t i_start, i_end;
938 /* Do the access seek */
940 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
944 block_ChainRelease( p_sys->block.p_first );
947 p_sys->block.i_start = p_sys->i_pos = i_pos;
948 p_sys->block.i_offset = 0;
949 p_sys->block.p_current = NULL;
950 p_sys->block.i_size = 0;
951 p_sys->block.p_first = NULL;
952 p_sys->block.pp_last = &p_sys->block.p_first;
955 if( AStreamRefillBlock( s ) )
959 p_sys->stat.i_seek_time += i_end - i_start;
960 p_sys->stat.i_seek_count++;
967 while( p_sys->block.p_current &&
968 p_sys->i_pos + p_sys->block.p_current->i_buffer - p_sys->block.i_offset <= i_pos )
970 p_sys->i_pos += p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
971 p_sys->block.p_current = p_sys->block.p_current->p_next;
972 p_sys->block.i_offset = 0;
974 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
976 if( p_sys->i_pos != i_pos )
980 while( p_sys->block.i_start + p_sys->block.i_size < i_pos );
982 p_sys->block.i_offset += i_pos - p_sys->i_pos;
983 p_sys->i_pos = i_pos;
991 static int AStreamRefillBlock( stream_t *s )
993 stream_sys_t *p_sys = s->p_sys;
996 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
997 p_sys->block.p_first != p_sys->block.p_current )
999 block_t *b = p_sys->block.p_first;
1001 p_sys->block.i_start += b->i_buffer;
1002 p_sys->block.i_size -= b->i_buffer;
1003 p_sys->block.p_first = b->p_next;
1007 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1008 p_sys->block.p_current == p_sys->block.p_first &&
1009 p_sys->block.p_current->p_next ) /* At least 2 packets */
1011 /* Enough data, don't read more */
1015 /* Now read a new block */
1016 const int64_t i_start = mdate();
1023 if( !vlc_object_alive(s) )
1024 return VLC_EGENERIC;
1027 if( ( b = AReadBlock( s, &b_eof ) ) )
1030 return VLC_EGENERIC;
1033 p_sys->stat.i_read_time += mdate() - i_start;
1036 /* Append the block */
1037 p_sys->block.i_size += b->i_buffer;
1038 *p_sys->block.pp_last = b;
1039 p_sys->block.pp_last = &b->p_next;
1042 if( p_sys->block.p_current == NULL )
1043 p_sys->block.p_current = b;
1046 p_sys->stat.i_bytes += b->i_buffer;
1047 p_sys->stat.i_read_count++;
1055 /****************************************************************************
1057 ****************************************************************************/
1058 static int AStreamRefillStream( stream_t *s );
1059 static int AStreamReadNoSeekStream( stream_t *s, void *p_read, unsigned int i_read );
1061 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read )
1063 stream_sys_t *p_sys = s->p_sys;
1067 const uint64_t i_pos_wanted = p_sys->i_pos + i_read;
1069 if( AStreamSeekStream( s, i_pos_wanted ) )
1071 if( p_sys->i_pos != i_pos_wanted )
1076 return AStreamReadNoSeekStream( s, p_read, i_read );
1079 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1081 stream_sys_t *p_sys = s->p_sys;
1082 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1085 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1088 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1089 "start=%"PRId64" offset=%d end=%"PRId64,
1090 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1091 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1094 /* Avoid problem, but that should *never* happen */
1095 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1096 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1098 while( tk->i_end < tk->i_start + p_sys->stream.i_offset + i_read )
1100 if( p_sys->stream.i_used <= 1 )
1102 /* Be sure we will read something */
1103 p_sys->stream.i_used += tk->i_start + p_sys->stream.i_offset + i_read - tk->i_end;
1105 if( AStreamRefillStream( s ) )
1107 if( tk->i_end < tk->i_start + p_sys->stream.i_offset )
1109 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1114 /* Now, direct pointer or a copy ? */
1115 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1116 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1118 *pp_peek = &tk->p_buffer[i_off];
1122 if( p_sys->i_peek < i_read )
1124 p_sys->p_peek = realloc_or_free( p_sys->p_peek, i_read );
1125 if( !p_sys->p_peek )
1130 p_sys->i_peek = i_read;
1133 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1134 STREAM_CACHE_TRACK_SIZE - i_off );
1135 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1136 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1138 *pp_peek = p_sys->p_peek;
1142 static int AStreamSeekStream( stream_t *s, uint64_t i_pos )
1144 stream_sys_t *p_sys = s->p_sys;
1146 stream_track_t *p_current = &p_sys->stream.tk[p_sys->stream.i_tk];
1147 access_t *p_access = p_sys->p_access;
1149 if( p_current->i_start >= p_current->i_end && i_pos >= p_current->i_end )
1153 msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1154 " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1155 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1157 p_sys->stream.i_offset,
1162 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1163 if( !b_aseek && i_pos < p_current->i_start )
1165 msg_Warn( s, "AStreamSeekStream: can't seek" );
1166 return VLC_EGENERIC;
1170 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_afastseek );
1172 /* FIXME compute seek cost (instead of static 'stupid' value) */
1173 uint64_t i_skip_threshold;
1175 i_skip_threshold = b_afastseek ? 128 : 3*p_sys->stream.i_read_size;
1177 i_skip_threshold = INT64_MAX;
1179 /* Date the current track */
1180 p_current->i_date = mdate();
1182 /* Search a new track slot */
1183 stream_track_t *tk = NULL;
1186 /* Prefer the current track */
1187 if( p_current->i_start <= i_pos && i_pos <= p_current->i_end + i_skip_threshold )
1190 i_tk_idx = p_sys->stream.i_tk;
1194 /* Try to maximize already read data */
1195 for( int i = 0; i < STREAM_CACHE_TRACK; i++ )
1197 stream_track_t *t = &p_sys->stream.tk[i];
1199 if( t->i_start > i_pos || i_pos > t->i_end )
1202 if( !tk || tk->i_end < t->i_end )
1211 /* Use the oldest unused */
1212 for( int i = 0; i < STREAM_CACHE_TRACK; i++ )
1214 stream_track_t *t = &p_sys->stream.tk[i];
1216 if( !tk || tk->i_date > t->i_date )
1223 assert( i_tk_idx >= 0 && i_tk_idx < STREAM_CACHE_TRACK );
1225 if( tk != p_current )
1226 i_skip_threshold = 0;
1227 if( tk->i_start <= i_pos && i_pos <= tk->i_end + i_skip_threshold )
1230 msg_Err( s, "AStreamSeekStream: reusing %d start=%"PRId64
1231 " end=%"PRId64"(%s)",
1232 i_tk_idx, tk->i_start, tk->i_end,
1233 tk != p_current ? "seek" : i_pos > tk->i_end ? "skip" : "noseek" );
1235 if( tk != p_current )
1239 /* Seek at the end of the buffer
1240 * TODO it is stupid to seek now, it would be better to delay it
1242 if( ASeek( s, tk->i_end ) )
1243 return VLC_EGENERIC;
1245 else if( i_pos > tk->i_end )
1247 uint64_t i_skip = i_pos - tk->i_end;
1250 const int i_read_max = __MIN( 10 * STREAM_READ_ATONCE, i_skip );
1251 if( AStreamReadNoSeekStream( s, NULL, i_read_max ) != i_read_max )
1252 return VLC_EGENERIC;
1253 i_skip -= i_read_max;
1260 msg_Err( s, "AStreamSeekStream: hard seek" );
1262 /* Nothing good, seek and choose oldest segment */
1263 if( ASeek( s, i_pos ) )
1264 return VLC_EGENERIC;
1266 tk->i_start = i_pos;
1269 p_sys->stream.i_offset = i_pos - tk->i_start;
1270 p_sys->stream.i_tk = i_tk_idx;
1271 p_sys->i_pos = i_pos;
1273 /* If there is not enough data left in the track, refill */
1274 /* TODO How to get a correct value for
1275 * - refilling threshold
1276 * - how much to refill
1278 if( tk->i_end < tk->i_start + p_sys->stream.i_offset + p_sys->stream.i_read_size )
1280 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1281 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1283 if( AStreamRefillStream( s ) && i_pos >= tk->i_end )
1284 return VLC_EGENERIC;
1289 static int AStreamReadNoSeekStream( stream_t *s, void *p_read, unsigned int i_read )
1291 stream_sys_t *p_sys = s->p_sys;
1292 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1294 uint8_t *p_data = (uint8_t *)p_read;
1295 unsigned int i_data = 0;
1297 if( tk->i_start >= tk->i_end )
1301 msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1302 " offset=%d end=%"PRId64,
1303 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1304 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1307 while( i_data < i_read )
1309 unsigned i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1310 unsigned int i_current =
1311 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1312 STREAM_CACHE_TRACK_SIZE - i_off );
1313 int i_copy = __MIN( i_current, i_read - i_data );
1315 if( i_copy <= 0 ) break; /* EOF */
1318 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1321 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1325 p_sys->stream.i_offset += i_copy;
1327 /* Update pos now */
1328 p_sys->i_pos += i_copy;
1331 p_sys->stream.i_used += i_copy;
1333 if( tk->i_end + i_data <= tk->i_start + p_sys->stream.i_offset + i_read )
1335 const unsigned i_read_requested = VLC_CLIP( i_read - i_data,
1336 STREAM_READ_ATONCE / 2,
1337 STREAM_READ_ATONCE * 10 );
1339 if( p_sys->stream.i_used < i_read_requested )
1340 p_sys->stream.i_used = i_read_requested;
1342 if( AStreamRefillStream( s ) )
1345 if( tk->i_start >= tk->i_end ) break;
1354 static int AStreamRefillStream( stream_t *s )
1356 stream_sys_t *p_sys = s->p_sys;
1357 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1359 /* We read but won't increase i_start after initial start + offset */
1361 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1362 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1363 bool b_read = false;
1364 int64_t i_start, i_stop;
1366 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1369 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1370 p_sys->stream.i_used, i_toread );
1374 while( i_toread > 0 )
1376 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1379 if( !vlc_object_alive(s) )
1380 return VLC_EGENERIC;
1382 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1383 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1385 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1390 else if( i_read == 0 )
1393 return VLC_EGENERIC;
1399 tk->i_end += i_read;
1401 /* Windows of STREAM_CACHE_TRACK_SIZE */
1402 if( tk->i_start + STREAM_CACHE_TRACK_SIZE < tk->i_end )
1404 unsigned i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1406 tk->i_start += i_invalid;
1407 p_sys->stream.i_offset -= i_invalid;
1411 p_sys->stream.i_used -= i_read;
1413 p_sys->stat.i_bytes += i_read;
1414 p_sys->stat.i_read_count++;
1418 p_sys->stat.i_read_time += i_stop - i_start;
1423 static void AStreamPrebufferStream( stream_t *s )
1425 stream_sys_t *p_sys = s->p_sys;
1427 int64_t i_first = 0;
1430 msg_Dbg( s, "starting pre-buffering" );
1434 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1436 int64_t i_date = mdate();
1438 int i_buffered = tk->i_end - tk->i_start;
1440 if( !vlc_object_alive(s) || i_buffered >= STREAM_CACHE_PREBUFFER_SIZE )
1445 p_sys->stat.i_bytes = i_buffered;
1446 p_sys->stat.i_read_time = i_date - i_start;
1447 i_byterate = ( CLOCK_FREQ * p_sys->stat.i_bytes ) /
1448 (p_sys->stat.i_read_time+1);
1450 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1452 p_sys->stat.i_bytes,
1453 p_sys->stat.i_read_time / CLOCK_FREQ,
1454 i_byterate / 1024 );
1459 i_read = STREAM_CACHE_TRACK_SIZE - i_buffered;
1460 i_read = __MIN( (int)p_sys->stream.i_read_size, i_read );
1461 i_read = AReadStream( s, &tk->p_buffer[i_buffered], i_read );
1464 else if( i_read == 0 )
1470 msg_Dbg( s, "received first data after %d ms",
1471 (int)((i_first-i_start)/1000) );
1474 tk->i_end += i_read;
1476 p_sys->stat.i_read_count++;
1480 /****************************************************************************
1482 ****************************************************************************/
1484 * Read from the stream untill first newline.
1485 * \param s Stream handle to read from
1486 * \return A pointer to the allocated output string. You need to free this when you are done.
1488 #define STREAM_PROBE_LINE 2048
1489 #define STREAM_LINE_MAX (2048*100)
1490 char *stream_ReadLine( stream_t *s )
1492 char *p_line = NULL;
1493 int i_line = 0, i_read = 0;
1495 /* Let's fail quickly if this is a readdir access */
1496 if( s->pf_read == NULL )
1502 const uint8_t *p_data;
1506 /* Probe new data */
1507 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1508 if( i_data <= 0 ) break; /* No more data */
1511 i_pos = stream_Tell( s );
1512 if( i_pos == 0 && i_data >= 2 )
1514 const char *psz_encoding = NULL;
1516 if( !memcmp( p_data, "\xFF\xFE", 2 ) )
1518 psz_encoding = "UTF-16LE";
1519 s->p_text->b_little_endian = true;
1521 else if( !memcmp( p_data, "\xFE\xFF", 2 ) )
1523 psz_encoding = "UTF-16BE";
1526 /* Open the converter if we need it */
1527 if( psz_encoding != NULL )
1529 msg_Dbg( s, "UTF-16 BOM detected" );
1530 s->p_text->i_char_width = 2;
1531 s->p_text->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1532 if( s->p_text->conv == (vlc_iconv_t)-1 )
1533 msg_Err( s, "iconv_open failed" );
1537 if( i_data % s->p_text->i_char_width )
1539 /* keep i_char_width boundary */
1540 i_data = i_data - ( i_data % s->p_text->i_char_width );
1541 msg_Warn( s, "the read is not i_char_width compatible");
1547 /* Check if there is an EOL */
1548 if( s->p_text->i_char_width == 1 )
1550 /* UTF-8: 0A <LF> */
1551 psz_eol = memchr( p_data, '\n', i_data );
1552 if( psz_eol == NULL )
1553 /* UTF-8: 0D <CR> */
1554 psz_eol = memchr( p_data, '\r', i_data );
1558 const uint8_t *p_last = p_data + i_data - s->p_text->i_char_width;
1559 uint16_t eol = s->p_text->b_little_endian ? 0x0A00 : 0x00A0;
1561 assert( s->p_text->i_char_width == 2 );
1563 /* UTF-16: 000A <LF> */
1564 for( const uint8_t *p = p_data; p <= p_last; p += 2 )
1566 if( U16_AT( p ) == eol )
1568 psz_eol = (char *)p + 1;
1573 if( psz_eol == NULL )
1574 { /* UTF-16: 000D <CR> */
1575 eol = s->p_text->b_little_endian ? 0x0D00 : 0x00D0;
1576 for( const uint8_t *p = p_data; p <= p_last; p += 2 )
1578 if( U16_AT( p ) == eol )
1580 psz_eol = (char *)p + 1;
1589 i_data = (psz_eol - (char *)p_data) + 1;
1590 p_line = realloc_or_free( p_line,
1591 i_line + i_data + s->p_text->i_char_width ); /* add \0 */
1594 i_data = stream_Read( s, &p_line[i_line], i_data );
1595 if( i_data <= 0 ) break; /* Hmmm */
1596 i_line += i_data - s->p_text->i_char_width; /* skip \n */;
1599 /* We have our line */
1603 /* Read data (+1 for easy \0 append) */
1604 p_line = realloc_or_free( p_line,
1605 i_line + STREAM_PROBE_LINE + s->p_text->i_char_width );
1608 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1609 if( i_data <= 0 ) break; /* Hmmm */
1613 if( i_read >= STREAM_LINE_MAX )
1614 goto error; /* line too long */
1620 for( j = 0; j < s->p_text->i_char_width; j++ )
1622 p_line[i_line + j] = '\0';
1624 i_line += s->p_text->i_char_width; /* the added \0 */
1625 if( s->p_text->i_char_width > 1 )
1628 size_t i_in = 0, i_out = 0;
1629 const char * p_in = NULL;
1630 char * p_out = NULL;
1631 char * psz_new_line = NULL;
1634 /* UTF-8 needs at most 150% of the buffer as many as UTF-16 */
1635 i_new_line = i_line * 3 / 2;
1636 psz_new_line = malloc( i_new_line );
1637 if( psz_new_line == NULL )
1639 i_in = (size_t)i_line;
1640 i_out = (size_t)i_new_line;
1642 p_out = psz_new_line;
1644 if( vlc_iconv( s->p_text->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1646 msg_Err( s, "iconv failed" );
1647 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1650 p_line = psz_new_line;
1651 i_line = (size_t)i_new_line - i_out; /* does not include \0 */
1654 /* Remove trailing LF/CR */
1655 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1656 p_line[i_line-2] == '\n') ) i_line--;
1658 /* Make sure the \0 is there */
1659 p_line[i_line-1] = '\0';
1665 /* We failed to read any data, probably EOF */
1669 if( s->p_text->conv != (vlc_iconv_t)(-1) )
1670 vlc_iconv_close( s->p_text->conv );
1671 s->p_text->conv = (vlc_iconv_t)(-1);
1675 /****************************************************************************
1676 * Access reading/seeking wrappers to handle concatenated streams.
1677 ****************************************************************************/
1678 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read )
1680 stream_sys_t *p_sys = s->p_sys;
1681 access_t *p_access = p_sys->p_access;
1682 input_thread_t *p_input = s->p_input;
1683 int i_read_orig = i_read;
1685 if( !p_sys->i_list )
1687 i_read = p_access->pf_read( p_access, p_read, i_read );
1692 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1693 stats_Update( p_input->p->counters.p_read_bytes, i_read, &total );
1694 stats_Update( p_input->p->counters.p_input_bitrate, total, NULL );
1695 stats_Update( p_input->p->counters.p_read_packets, 1, NULL );
1696 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1701 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1704 /* If we reached an EOF then switch to the next stream in the list */
1705 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1707 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1708 access_t *p_list_access;
1710 msg_Dbg( s, "opening input `%s'", psz_name );
1712 p_list_access = access_New( s, s->p_input, p_access->psz_access, "", psz_name );
1714 if( !p_list_access ) return 0;
1716 if( p_sys->p_list_access != p_access )
1717 access_Delete( p_sys->p_list_access );
1719 p_sys->p_list_access = p_list_access;
1721 /* We have to read some data */
1722 return AReadStream( s, p_read, i_read_orig );
1725 /* Update read bytes in input */
1730 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1731 stats_Update( p_input->p->counters.p_read_bytes, i_read, &total );
1732 stats_Update( p_input->p->counters.p_input_bitrate, total, NULL );
1733 stats_Update( p_input->p->counters.p_read_packets, 1, NULL );
1734 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1739 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
1741 stream_sys_t *p_sys = s->p_sys;
1742 access_t *p_access = p_sys->p_access;
1743 input_thread_t *p_input = s->p_input;
1747 if( !p_sys->i_list )
1749 p_block = p_access->pf_block( p_access );
1750 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1751 if( p_input && p_block && libvlc_stats (p_access) )
1755 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1756 stats_Update( p_input->p->counters.p_read_bytes,
1757 p_block->i_buffer, &total );
1758 stats_Update( p_input->p->counters.p_input_bitrate,
1760 stats_Update( p_input->p->counters.p_read_packets, 1, NULL );
1761 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1766 p_block = p_sys->p_list_access->pf_block( p_sys->p_list_access );
1767 b_eof = p_sys->p_list_access->info.b_eof;
1768 if( pb_eof ) *pb_eof = b_eof;
1770 /* If we reached an EOF then switch to the next stream in the list */
1771 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1773 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1774 access_t *p_list_access;
1776 msg_Dbg( s, "opening input `%s'", psz_name );
1778 p_list_access = access_New( s, s->p_input, p_access->psz_access, "", psz_name );
1780 if( !p_list_access ) return 0;
1782 if( p_sys->p_list_access != p_access )
1783 access_Delete( p_sys->p_list_access );
1785 p_sys->p_list_access = p_list_access;
1787 /* We have to read some data */
1788 return AReadBlock( s, pb_eof );
1796 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1797 stats_Update( p_input->p->counters.p_read_bytes,
1798 p_block->i_buffer, &total );
1799 stats_Update( p_input->p->counters.p_input_bitrate, total, NULL );
1800 stats_Update( p_input->p->counters.p_read_packets, 1 , NULL);
1801 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1807 static int ASeek( stream_t *s, uint64_t i_pos )
1809 stream_sys_t *p_sys = s->p_sys;
1810 access_t *p_access = p_sys->p_access;
1812 /* Check which stream we need to access */
1818 access_t *p_list_access = 0;
1820 for( i = 0; i < p_sys->i_list - 1; i++ )
1822 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1823 i_size += p_sys->list[i]->i_size;
1825 psz_name = p_sys->list[i]->psz_path;
1827 if( i != p_sys->i_list_index )
1828 msg_Dbg( s, "opening input `%s'", psz_name );
1830 if( i != p_sys->i_list_index && i != 0 )
1833 access_New( s, s->p_input, p_access->psz_access, "", psz_name );
1835 else if( i != p_sys->i_list_index )
1837 p_list_access = p_access;
1842 if( p_sys->p_list_access != p_access )
1843 access_Delete( p_sys->p_list_access );
1845 p_sys->p_list_access = p_list_access;
1848 p_sys->i_list_index = i;
1849 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1853 return p_access->pf_seek( p_access, i_pos );
1856 static int AStreamReadDir( stream_t *s, input_item_node_t *p_node )
1858 access_t *p_access = s->p_sys->p_access;
1860 return p_access->pf_readdir( p_access, p_node );
1864 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
1865 * "p_read" is NULL then data are skipped instead of read.
1866 * \return The real number of bytes read/skip. If this value is less
1867 * than i_read that means that it's the end of the stream.
1868 * \note stream_Read increments the stream position, and when p_read is NULL,
1869 * this is its only task.
1871 int stream_Read( stream_t *s, void *p_read, int i_read )
1873 return s->pf_read( s, p_read, i_read );
1877 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
1878 * \return The real number of valid bytes. If it's less
1879 * or equal to 0, *pp_peek is invalid.
1880 * \note pp_peek is a pointer to internal buffer and it will be invalid as
1881 * soons as other stream_* functions are called.
1882 * \note Contrary to stream_Read, stream_Peek doesn't modify the stream
1883 * position, and doesn't necessarily involve copying of data. It's mainly
1884 * used by the modules to quickly probe the (head of the) stream.
1885 * \note Due to input limitation, the return value could be less than i_peek
1886 * without meaning the end of the stream (but only when you have i_peek >=
1887 * p_input->i_bufsize)
1889 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
1891 return s->pf_peek( s, pp_peek, i_peek );
1895 * Use to control the "stream_t *". Look at #stream_query_e for
1896 * possible "i_query" value and format arguments. Return VLC_SUCCESS
1897 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
1899 int stream_vaControl( stream_t *s, int i_query, va_list args )
1901 return s->pf_control( s, i_query, args );
1907 void stream_Delete( stream_t *s )
1912 int stream_Control( stream_t *s, int i_query, ... )
1918 return VLC_EGENERIC;
1920 va_start( args, i_query );
1921 i_result = s->pf_control( s, i_query, args );
1927 * Read "i_size" bytes and store them in a block_t.
1928 * It always read i_size bytes unless you are at the end of the stream
1929 * where it return what is available.
1931 block_t *stream_Block( stream_t *s, int i_size )
1933 if( i_size <= 0 ) return NULL;
1935 /* emulate block read */
1936 block_t *p_bk = block_Alloc( i_size );
1939 int i_read = stream_Read( s, p_bk->p_buffer, i_size );
1942 p_bk->i_buffer = i_read;
1945 block_Release( p_bk );
1951 * Read the remaining of the data if there is less than i_max_size bytes, otherwise
1954 * The stream position is unknown after the call.
1956 block_t *stream_BlockRemaining( stream_t *s, int i_max_size )
1958 int i_allocate = __MIN(1000000, i_max_size);
1959 int64_t i_size = stream_Size( s );
1962 int64_t i_position = stream_Tell( s );
1963 if( i_position + i_max_size < i_size )
1965 msg_Err( s, "Remaining stream size is greater than %d bytes",
1969 i_allocate = i_size - i_position;
1971 if( i_allocate <= 0 )
1974 block_t *p_block = block_Alloc( i_allocate );
1978 int i_read = stream_Read( s, &p_block->p_buffer[i_index],
1979 p_block->i_buffer - i_index);
1983 i_max_size -= i_read;
1984 if( i_max_size <= 0 )
1986 p_block = block_Realloc( p_block, 0, p_block->i_buffer +
1987 __MIN(1000000, i_max_size) );
1990 p_block->i_buffer = i_index;
1995 * Returns a node containing all the input_item of the directory pointer by
1996 * this stream. returns VLC_SUCCESS on success.
1998 int stream_ReadDir( stream_t *s, input_item_node_t *p_node )
2000 return s->pf_readdir( s, p_node );