1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_strings.h>
34 #include <vlc_charset.h>
41 #include "input_internal.h"
46 * - tune the 2 methods (block/stream)
47 * - compute cost for seek
48 * - improve stream mode seeking with closest segments
54 * One linked list of data read
56 * More complex scheme using mutliple track to avoid seeking
57 * - using directly the access (only indirection for peeking).
58 * This method is known to introduce much less latency.
59 * It should probably defaulted (instead of the stream method (2)).
62 /* How many tracks we have, currently only used for stream mode */
63 #ifdef OPTIMIZE_MEMORY
64 # define STREAM_CACHE_TRACK 1
65 /* Max size of our cache 128Ko per track */
66 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
68 # define STREAM_CACHE_TRACK 3
69 /* Max size of our cache 4Mo per track */
70 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
73 /* How many data we try to prebuffer
74 * XXX it should be small to avoid useless latency but big enough for
75 * efficient demux probing */
76 #define STREAM_CACHE_PREBUFFER_SIZE (128)
78 /* Method1: Simple, for pf_block.
79 * We get blocks and put them in the linked list.
80 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
83 /* Method2: A bit more complex, for pf_read
84 * - We use ring buffers, only one if unseekable, all if seekable
85 * - Upon seek date current ring, then search if one ring match the pos,
86 * yes: switch to it, seek the access to match the end of the ring
87 * no: search the ring with i_end the closer to i_pos,
88 * if close enough, read data and use this ring
89 * else use the oldest ring, seek and use it.
91 * TODO: - with access non seekable: use all space available for only one ring, but
92 * we have to support seekable/non-seekable switch on the fly.
93 * - compute a good value for i_read_size
96 #define STREAM_READ_ATONCE 1024
97 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
121 } stream_read_method_t;
127 stream_read_method_t method; /* method to use */
129 int64_t i_pos; /* Current reading offset */
131 /* Method 1: pf_block */
134 int64_t i_start; /* Offset of block for p_first */
135 int64_t i_offset; /* Offset for data in p_current */
136 block_t *p_current; /* Current block */
138 int i_size; /* Total amount of data in the list */
144 /* Method 2: for pf_read */
147 int i_offset; /* Buffer offset in the current track */
148 int i_tk; /* Current track */
149 stream_track_t tk[STREAM_CACHE_TRACK];
155 int i_used; /* Used since last read */
160 /* Peek temporary buffer */
164 /* Stat for both method */
167 bool b_fastseek; /* From access */
169 /* Stat about reading data */
170 int64_t i_read_count;
174 /* Stat about seek */
182 access_entry_t **list;
184 access_t *p_list_access;
186 /* Preparse mode ? */
191 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read );
192 static int AStreamPeekBlock( stream_t *s, const uint8_t **p_peek, unsigned int i_read );
193 static int AStreamSeekBlock( stream_t *s, int64_t i_pos );
194 static void AStreamPrebufferBlock( stream_t *s );
195 static block_t *AReadBlock( stream_t *s, bool *pb_eof );
198 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read );
199 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read );
200 static int AStreamSeekStream( stream_t *s, int64_t i_pos );
201 static void AStreamPrebufferStream( stream_t *s );
202 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read );
205 static int AStreamControl( stream_t *s, int i_query, va_list );
206 static void AStreamDestroy( stream_t *s );
207 static void UStreamDestroy( stream_t *s );
208 static int ASeek( stream_t *s, int64_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),
216 VLC_OBJECT_GENERIC, "stream" );
221 s->p_text = malloc( sizeof(*s->p_text) );
224 vlc_object_release( s );
228 /* UTF16 and UTF32 text file conversion */
229 s->p_text->conv = (vlc_iconv_t)(-1);
230 s->p_text->i_char_width = 1;
231 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 vlc_object_release( s );
246 /****************************************************************************
247 * stream_UrlNew: create a stream from a access
248 ****************************************************************************/
249 stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
251 const char *psz_access, *psz_demux;
259 char psz_dup[strlen( psz_url ) + 1];
260 strcpy( psz_dup, psz_url );
261 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
263 /* Now try a real access */
264 p_access = access_New( p_parent, 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 if( !( p_res = stream_AccessNew( p_access, true ) ) )
274 access_Delete( p_access );
278 p_res->pf_destroy = UStreamDestroy;
282 stream_t *stream_AccessNew( access_t *p_access, bool b_quick )
284 stream_t *s = stream_CommonNew( VLC_OBJECT(p_access) );
286 char *psz_list = NULL;
291 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
294 stream_CommonDelete( s );
298 /* Attach it now, needed for b_die */
299 vlc_object_attach( s, p_access );
301 s->pf_read = NULL; /* Set up later */
303 s->pf_control = AStreamControl;
304 s->pf_destroy = AStreamDestroy;
307 p_sys->p_access = p_access;
308 if( p_access->pf_block )
309 p_sys->method = STREAM_METHOD_BLOCK;
311 p_sys->method = STREAM_METHOD_STREAM;
313 p_sys->i_pos = p_access->info.i_pos;
316 access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
317 p_sys->stat.i_bytes = 0;
318 p_sys->stat.i_read_time = 0;
319 p_sys->stat.i_read_count = 0;
320 p_sys->stat.i_seek_count = 0;
321 p_sys->stat.i_seek_time = 0;
325 p_sys->i_list_index = 0;
326 p_sys->p_list_access = 0;
328 p_sys->b_quick = b_quick;
330 /* Get the additional list of inputs if any (for concatenation) */
331 if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
333 access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
334 if( p_entry == NULL )
336 char *psz_name, *psz_parser = psz_name = psz_list;
338 p_sys->p_list_access = p_access;
339 p_entry->i_size = p_access->info.i_size;
340 p_entry->psz_path = strdup( p_access->psz_path );
341 if( p_entry->psz_path == NULL )
346 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
347 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
348 p_entry->psz_path, p_access->info.i_size );
350 while( psz_name && *psz_name )
352 psz_parser = strchr( psz_name, ',' );
353 if( psz_parser ) *psz_parser = 0;
355 psz_name = strdup( psz_name );
358 access_t *p_tmp = access_New( p_access, p_access->psz_access,
363 psz_name = psz_parser;
364 if( psz_name ) psz_name++;
368 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
369 psz_name, p_tmp->info.i_size );
371 p_entry = malloc( sizeof(access_entry_t) );
372 if( p_entry == NULL )
374 p_entry->i_size = p_tmp->info.i_size;
375 p_entry->psz_path = psz_name;
376 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
378 access_Delete( p_tmp );
381 psz_name = psz_parser;
382 if( psz_name ) psz_name++;
385 FREENULL( psz_list );
389 p_sys->p_peek = NULL;
391 if( p_sys->method == STREAM_METHOD_BLOCK )
393 msg_Dbg( s, "Using AStream*Block" );
394 s->pf_read = AStreamReadBlock;
395 s->pf_peek = AStreamPeekBlock;
397 /* Init all fields of p_sys->block */
398 p_sys->block.i_start = p_sys->i_pos;
399 p_sys->block.i_offset = 0;
400 p_sys->block.p_current = NULL;
401 p_sys->block.i_size = 0;
402 p_sys->block.p_first = NULL;
403 p_sys->block.pp_last = &p_sys->block.p_first;
405 /* Do the prebuffering */
406 AStreamPrebufferBlock( s );
408 if( p_sys->block.i_size <= 0 )
410 msg_Err( s, "cannot pre fill buffer" );
418 assert( p_sys->method == STREAM_METHOD_STREAM );
420 msg_Dbg( s, "Using AStream*Stream" );
422 s->pf_read = AStreamReadStream;
423 s->pf_peek = AStreamPeekStream;
425 /* Allocate/Setup our tracks */
426 p_sys->stream.i_offset = 0;
427 p_sys->stream.i_tk = 0;
428 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
429 if( p_sys->stream.p_buffer == NULL )
431 p_sys->stream.i_used = 0;
432 access_Control( p_access, ACCESS_GET_MTU,
433 &p_sys->stream.i_read_size );
434 if( p_sys->stream.i_read_size <= 0 )
435 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
436 else if( p_sys->stream.i_read_size <= 256 )
437 p_sys->stream.i_read_size = 256;
439 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
441 p_sys->stream.tk[i].i_date = 0;
442 p_sys->stream.tk[i].i_start = p_sys->i_pos;
443 p_sys->stream.tk[i].i_end = p_sys->i_pos;
444 p_sys->stream.tk[i].p_buffer=
445 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
448 /* Do the prebuffering */
449 AStreamPrebufferStream( s );
451 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
453 msg_Err( s, "cannot pre fill buffer" );
461 if( p_sys->method == STREAM_METHOD_BLOCK )
467 free( p_sys->stream.p_buffer );
469 while( p_sys->i_list > 0 )
470 free( p_sys->list[--(p_sys->i_list)] );
474 vlc_object_detach( s );
475 stream_CommonDelete( s );
479 /****************************************************************************
481 ****************************************************************************/
482 static void AStreamDestroy( stream_t *s )
484 stream_sys_t *p_sys = s->p_sys;
486 vlc_object_detach( s );
488 if( p_sys->method == STREAM_METHOD_BLOCK )
489 block_ChainRelease( p_sys->block.p_first );
491 free( p_sys->stream.p_buffer );
493 free( p_sys->p_peek );
495 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
496 access_Delete( p_sys->p_list_access );
498 while( p_sys->i_list-- )
500 free( p_sys->list[p_sys->i_list]->psz_path );
501 free( p_sys->list[p_sys->i_list] );
507 stream_CommonDelete( s );
510 static void UStreamDestroy( stream_t *s )
512 access_t *p_access = (access_t *)s->p_parent;
514 access_Delete( p_access );
517 /****************************************************************************
518 * AStreamControlReset:
519 ****************************************************************************/
520 static void AStreamControlReset( stream_t *s )
522 stream_sys_t *p_sys = s->p_sys;
524 p_sys->i_pos = p_sys->p_access->info.i_pos;
526 if( p_sys->method == STREAM_METHOD_BLOCK )
528 block_ChainRelease( p_sys->block.p_first );
530 /* Init all fields of p_sys->block */
531 p_sys->block.i_start = p_sys->i_pos;
532 p_sys->block.i_offset = 0;
533 p_sys->block.p_current = NULL;
534 p_sys->block.i_size = 0;
535 p_sys->block.p_first = NULL;
536 p_sys->block.pp_last = &p_sys->block.p_first;
538 /* Do the prebuffering */
539 AStreamPrebufferBlock( s );
545 assert( p_sys->method == STREAM_METHOD_STREAM );
547 /* Setup our tracks */
548 p_sys->stream.i_offset = 0;
549 p_sys->stream.i_tk = 0;
550 p_sys->stream.i_used = 0;
552 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
554 p_sys->stream.tk[i].i_date = 0;
555 p_sys->stream.tk[i].i_start = p_sys->i_pos;
556 p_sys->stream.tk[i].i_end = p_sys->i_pos;
559 /* Do the prebuffering */
560 AStreamPrebufferStream( s );
564 /****************************************************************************
565 * AStreamControlUpdate:
566 ****************************************************************************/
567 static void AStreamControlUpdate( stream_t *s )
569 stream_sys_t *p_sys = s->p_sys;
571 p_sys->i_pos = p_sys->p_access->info.i_pos;
576 for( i = 0; i < p_sys->i_list_index; i++ )
578 p_sys->i_pos += p_sys->list[i]->i_size;
583 /****************************************************************************
585 ****************************************************************************/
586 static int AStreamControl( stream_t *s, int i_query, va_list args )
588 stream_sys_t *p_sys = s->p_sys;
589 access_t *p_access = p_sys->p_access;
592 int64_t *pi_64, i_64;
597 case STREAM_GET_SIZE:
598 pi_64 = (int64_t*)va_arg( args, int64_t * );
599 if( s->p_sys->i_list )
603 for( i = 0; i < s->p_sys->i_list; i++ )
604 *pi_64 += s->p_sys->list[i]->i_size;
607 *pi_64 = p_access->info.i_size;
610 case STREAM_CAN_SEEK:
611 p_bool = (bool*)va_arg( args, bool * );
612 access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
615 case STREAM_CAN_FASTSEEK:
616 p_bool = (bool*)va_arg( args, bool * );
617 access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
620 case STREAM_GET_POSITION:
621 pi_64 = (int64_t*)va_arg( args, int64_t * );
622 *pi_64 = p_sys->i_pos;
625 case STREAM_SET_POSITION:
626 i_64 = (int64_t)va_arg( args, int64_t );
627 switch( p_sys->method )
629 case STREAM_METHOD_BLOCK:
630 return AStreamSeekBlock( s, i_64 );
631 case STREAM_METHOD_STREAM:
632 return AStreamSeekStream( s, i_64 );
641 case STREAM_CONTROL_ACCESS:
643 i_int = (int) va_arg( args, int );
644 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
645 i_int != ACCESS_SET_PRIVATE_ID_CA &&
646 i_int != ACCESS_GET_PRIVATE_ID_STATE &&
647 i_int != ACCESS_SET_TITLE &&
648 i_int != ACCESS_SET_SEEKPOINT )
650 msg_Err( s, "Hey, what are you thinking ?"
651 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
654 int i_ret = access_vaControl( p_access, i_int, args );
655 if( i_int == ACCESS_SET_TITLE || i_int == ACCESS_SET_SEEKPOINT )
656 AStreamControlReset( s );
660 case STREAM_UPDATE_SIZE:
661 AStreamControlUpdate( s );
664 case STREAM_GET_CONTENT_TYPE:
665 return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
666 va_arg( args, char ** ) );
667 case STREAM_SET_RECORD_STATE:
669 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
675 /****************************************************************************
677 ****************************************************************************/
678 static void AStreamPrebufferBlock( stream_t *s )
680 stream_sys_t *p_sys = s->p_sys;
681 access_t *p_access = p_sys->p_access;
686 msg_Dbg( s, "pre buffering" );
690 const int64_t i_date = mdate();
694 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE )
699 p_sys->stat.i_bytes = p_sys->block.i_size;
700 p_sys->stat.i_read_time = i_date - i_start;
701 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
702 (p_sys->stat.i_read_time + 1);
704 msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
705 "%"PRId64" kbytes/s",
707 p_sys->stat.i_read_time / INT64_C(1000000),
713 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
722 /* Append the block */
723 p_sys->block.i_size += b->i_buffer;
724 *p_sys->block.pp_last = b;
725 p_sys->block.pp_last = &b->p_next;
727 p_sys->stat.i_read_count++;
734 msg_Dbg( s, "received first data after %d ms",
735 (int)((i_first-i_start)/1000) );
739 p_sys->block.p_current = p_sys->block.p_first;
742 static int AStreamRefillBlock( stream_t *s );
744 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
746 stream_sys_t *p_sys = s->p_sys;
748 uint8_t *p_data = p_read;
749 unsigned int i_data = 0;
752 if( p_sys->block.p_current == NULL )
757 /* seek within this stream if possible, else use plain old read and discard */
758 stream_sys_t *p_sys = s->p_sys;
759 access_t *p_access = p_sys->p_access;
761 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
763 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
766 while( i_data < i_read )
769 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
770 unsigned int i_copy = __MIN( (unsigned int)__MAX(i_current,0), i_read - i_data);
776 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
782 p_sys->block.i_offset += i_copy;
783 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
785 /* Current block is now empty, switch to next */
786 if( p_sys->block.p_current )
788 p_sys->block.i_offset = 0;
789 p_sys->block.p_current = p_sys->block.p_current->p_next;
791 /*Get a new block if needed */
792 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
799 p_sys->i_pos += i_data;
803 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
805 stream_sys_t *p_sys = s->p_sys;
807 unsigned int i_data = 0;
809 unsigned int i_offset;
811 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
813 /* We can directly give a pointer over our buffer */
814 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
816 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
820 /* We need to create a local copy */
821 if( p_sys->i_peek < i_read )
823 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
829 p_sys->i_peek = i_read;
832 /* Fill enough data */
833 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
836 block_t **pp_last = p_sys->block.pp_last;
838 if( AStreamRefillBlock( s ) ) break;
840 /* Our buffer are probably filled enough, don't try anymore */
841 if( pp_last == p_sys->block.pp_last ) break;
844 /* Copy what we have */
845 b = p_sys->block.p_current;
846 i_offset = p_sys->block.i_offset;
847 p_data = p_sys->p_peek;
849 while( b && i_data < i_read )
851 unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
852 int i_copy = __MIN( i_current, i_read - i_data );
854 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
859 if( i_offset >= b->i_buffer )
866 *pp_peek = p_sys->p_peek;
870 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
872 stream_sys_t *p_sys = s->p_sys;
873 access_t *p_access = p_sys->p_access;
874 int64_t i_offset = i_pos - p_sys->block.i_start;
877 /* We already have thoses data, just update p_current/i_offset */
878 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
880 block_t *b = p_sys->block.p_first;
883 while( i_current + b->i_buffer < i_offset )
885 i_current += b->i_buffer;
889 p_sys->block.p_current = b;
890 p_sys->block.i_offset = i_offset - i_current;
892 p_sys->i_pos = i_pos;
897 /* We may need to seek or to read data */
901 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
905 msg_Err( s, "backward seeking impossible (access not seekable)" );
913 bool b_aseek, b_aseekfast;
915 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
916 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
921 msg_Warn( s, "%"PRId64" bytes need to be skipped "
922 "(access non seekable)",
923 i_offset - p_sys->block.i_size );
927 int64_t i_skip = i_offset - p_sys->block.i_size;
929 /* Avg bytes per packets */
930 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
931 /* TODO compute a seek cost instead of fixed threshold */
932 int i_th = b_aseekfast ? 1 : 5;
934 if( i_skip <= i_th * i_avg &&
935 i_skip < STREAM_CACHE_SIZE )
940 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
941 b_seek, i_th*i_avg, i_skip );
947 int64_t i_start, i_end;
948 /* Do the access seek */
950 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
954 block_ChainRelease( p_sys->block.p_first );
957 p_sys->block.i_start = p_sys->i_pos = i_pos;
958 p_sys->block.i_offset = 0;
959 p_sys->block.p_current = NULL;
960 p_sys->block.i_size = 0;
961 p_sys->block.p_first = NULL;
962 p_sys->block.pp_last = &p_sys->block.p_first;
965 if( AStreamRefillBlock( s ) )
969 p_sys->stat.i_seek_time += i_end - i_start;
970 p_sys->stat.i_seek_count++;
977 /* Read and skip enough data */
978 if( AStreamRefillBlock( s ) )
981 while( p_sys->block.p_current &&
982 p_sys->i_pos + p_sys->block.p_current->i_buffer - p_sys->block.i_offset < i_pos )
984 p_sys->i_pos += p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
985 p_sys->block.p_current = p_sys->block.p_current->p_next;
986 p_sys->block.i_offset = 0;
989 while( p_sys->block.i_start + p_sys->block.i_size < i_pos );
991 p_sys->block.i_offset = i_pos - p_sys->i_pos;
992 p_sys->i_pos = i_pos;
1000 static int AStreamRefillBlock( stream_t *s )
1002 stream_sys_t *p_sys = s->p_sys;
1003 int64_t i_start, i_stop;
1007 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1008 p_sys->block.p_first != p_sys->block.p_current )
1010 block_t *b = p_sys->block.p_first;
1012 p_sys->block.i_start += b->i_buffer;
1013 p_sys->block.i_size -= b->i_buffer;
1014 p_sys->block.p_first = b->p_next;
1018 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1019 p_sys->block.p_current == p_sys->block.p_first &&
1020 p_sys->block.p_current->p_next ) /* At least 2 packets */
1022 /* Enough data, don't read more */
1026 /* Now read a new block */
1033 return VLC_EGENERIC;
1036 if( ( b = AReadBlock( s, &b_eof ) ) )
1039 return VLC_EGENERIC;
1046 /* Append the block */
1047 p_sys->block.i_size += b->i_buffer;
1048 *p_sys->block.pp_last = b;
1049 p_sys->block.pp_last = &b->p_next;
1052 if( p_sys->block.p_current == NULL )
1053 p_sys->block.p_current = b;
1056 p_sys->stat.i_bytes += b->i_buffer;
1057 p_sys->stat.i_read_time += i_stop - i_start;
1058 p_sys->stat.i_read_count++;
1067 /****************************************************************************
1069 ****************************************************************************/
1070 static int AStreamRefillStream( stream_t *s );
1072 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read )
1074 stream_sys_t *p_sys = s->p_sys;
1075 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1077 uint8_t *p_data = (uint8_t *)p_read;
1078 unsigned int i_data = 0;
1080 if( tk->i_start >= tk->i_end )
1083 if( p_data == NULL )
1085 /* seek within this stream if possible, else use plain old read and discard */
1086 stream_sys_t *p_sys = s->p_sys;
1087 access_t *p_access = p_sys->p_access;
1089 /* seeking after EOF is not what we want */
1090 if( !( p_access->info.b_eof ) )
1093 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1095 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1100 msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1101 " offset=%d end=%"PRId64,
1102 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1103 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1106 while( i_data < i_read )
1108 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1109 STREAM_CACHE_TRACK_SIZE;
1110 unsigned int i_current =
1111 __MAX(0,__MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1112 STREAM_CACHE_TRACK_SIZE - i_off ));
1113 int i_copy = __MIN( i_current, i_read - i_data );
1115 if( i_copy <= 0 ) break; /* EOF */
1118 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1121 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1125 p_sys->stream.i_offset += i_copy;
1127 /* Update pos now */
1128 p_sys->i_pos += i_copy;
1131 p_sys->stream.i_used += i_copy;
1132 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1133 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1135 if( p_sys->stream.i_used < i_read - i_data )
1136 p_sys->stream.i_used = __MIN( i_read - i_data, STREAM_READ_ATONCE * 10 );
1138 if( AStreamRefillStream( s ) )
1141 if( tk->i_start >= tk->i_end ) break;
1149 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1151 stream_sys_t *p_sys = s->p_sys;
1152 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1155 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1158 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1159 "start=%"PRId64" offset=%d end=%"PRId64,
1160 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1161 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1164 /* Avoid problem, but that should *never* happen */
1165 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1166 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1168 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1170 if( p_sys->stream.i_used <= 1 )
1172 /* Be sure we will read something */
1173 p_sys->stream.i_used += i_read -
1174 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1176 if( AStreamRefillStream( s ) ) break;
1179 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1180 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1182 /* Now, direct pointer or a copy ? */
1183 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1184 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1186 *pp_peek = &tk->p_buffer[i_off];
1190 if( p_sys->i_peek < i_read )
1192 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1193 if( !p_sys->p_peek )
1198 p_sys->i_peek = i_read;
1201 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1202 STREAM_CACHE_TRACK_SIZE - i_off );
1203 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1204 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1206 *pp_peek = p_sys->p_peek;
1210 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1212 stream_sys_t *p_sys = s->p_sys;
1213 access_t *p_access = p_sys->p_access;
1221 msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1222 " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1223 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1224 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1225 p_sys->stream.i_offset,
1226 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1230 /* Seek in our current track ? */
1231 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1232 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1234 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1236 msg_Dbg( s, "AStreamSeekStream: current track" );
1238 p_sys->i_pos = i_pos;
1239 p_sys->stream.i_offset = i_pos - tk->i_start;
1241 /* If there is not enough data left in the track, refill */
1242 /* \todo How to get a correct value for
1243 * - refilling threshold
1244 * - how much to refill
1246 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1247 p_sys->stream.i_read_size )
1249 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1251 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1252 AStreamRefillStream( s );
1258 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1261 /* We can't do nothing */
1262 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1263 return VLC_EGENERIC;
1266 /* Date the current track */
1267 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1269 /* Try to reuse already read data */
1270 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1272 stream_track_t *tk = &p_sys->stream.tk[i];
1274 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1277 msg_Dbg( s, "AStreamSeekStream: reusing %d start=%"PRId64
1278 " end=%"PRId64, i, tk->i_start, tk->i_end );
1281 /* Seek at the end of the buffer */
1282 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1285 p_sys->i_pos = i_pos;
1286 p_sys->stream.i_tk = i;
1287 p_sys->stream.i_offset = i_pos - tk->i_start;
1289 if( p_sys->stream.i_used < 1024 )
1290 p_sys->stream.i_used = 1024;
1292 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1293 return VLC_EGENERIC;
1299 access_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1300 /* FIXME compute seek cost (instead of static 'stupid' value) */
1301 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1307 /* Search closest segment TODO */
1308 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1310 stream_track_t *tk = &p_sys->stream.tk[i];
1312 if( i_pos + i_maxth >= tk->i_start )
1314 msg_Dbg( s, "good segment before current pos, TODO" );
1316 if( i_pos - i_maxth <= tk->i_end )
1318 msg_Dbg( s, "good segment after current pos, TODO" );
1323 /* Nothing good, seek and choose oldest segment */
1324 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1325 p_sys->i_pos = i_pos;
1328 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1330 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1334 /* Reset the segment */
1335 p_sys->stream.i_tk = i_new;
1336 p_sys->stream.i_offset = 0;
1337 p_sys->stream.tk[i_new].i_start = i_pos;
1338 p_sys->stream.tk[i_new].i_end = i_pos;
1341 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1342 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1344 if( AStreamRefillStream( s ) )
1345 return VLC_EGENERIC;
1350 static int AStreamRefillStream( stream_t *s )
1352 stream_sys_t *p_sys = s->p_sys;
1353 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1355 /* We read but won't increase i_start after initial start + offset */
1357 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1358 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1359 bool b_read = false;
1360 int64_t i_start, i_stop;
1362 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1365 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1366 p_sys->stream.i_used, i_toread );
1370 while( i_toread > 0 )
1372 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1376 return VLC_EGENERIC;
1378 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1379 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1381 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1386 else if( i_read == 0 )
1389 return VLC_EGENERIC;
1395 tk->i_end += i_read;
1397 /* Windows of STREAM_CACHE_TRACK_SIZE */
1398 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1400 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1402 tk->i_start += i_invalid;
1403 p_sys->stream.i_offset -= i_invalid;
1407 p_sys->stream.i_used -= i_read;
1409 p_sys->stat.i_bytes += i_read;
1410 p_sys->stat.i_read_count++;
1414 p_sys->stat.i_read_time += i_stop - i_start;
1419 static void AStreamPrebufferStream( stream_t *s )
1421 stream_sys_t *p_sys = s->p_sys;
1422 access_t *p_access = p_sys->p_access;
1424 int64_t i_first = 0;
1427 msg_Dbg( s, "pre buffering" );
1431 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1433 int64_t i_date = mdate();
1436 if( s->b_die || tk->i_end >= STREAM_CACHE_PREBUFFER_SIZE )
1441 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1442 p_sys->stat.i_read_time = i_date - i_start;
1443 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1444 (p_sys->stat.i_read_time+1);
1446 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1447 "%"PRId64" kbytes/s",
1448 p_sys->stat.i_bytes,
1449 p_sys->stat.i_read_time / INT64_C(1000000),
1450 i_byterate / 1024 );
1455 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1456 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1457 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1460 else if( i_read == 0 )
1466 msg_Dbg( s, "received first data after %d ms",
1467 (int)((i_first-i_start)/1000) );
1470 tk->i_end += i_read;
1472 p_sys->stat.i_read_count++;
1476 /****************************************************************************
1478 ****************************************************************************/
1480 * Read from the stream untill first newline.
1481 * \param s Stream handle to read from
1482 * \return A pointer to the allocated output string. You need to free this when you are done.
1484 #define STREAM_PROBE_LINE 2048
1485 #define STREAM_LINE_MAX (2048*100)
1486 char *stream_ReadLine( stream_t *s )
1488 char *p_line = NULL;
1489 int i_line = 0, i_read = 0;
1491 while( i_read < STREAM_LINE_MAX )
1494 const uint8_t *p_data;
1498 /* Probe new data */
1499 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1500 if( i_data <= 0 ) break; /* No more data */
1503 i_pos = stream_Tell( s );
1504 if( i_pos == 0 && i_data >= 3 )
1507 const char *psz_encoding = NULL;
1509 if( !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
1511 psz_encoding = "UTF-8";
1514 else if( !memcmp( p_data, "\xFF\xFE", 2 ) )
1516 psz_encoding = "UTF-16LE";
1517 s->p_text->b_little_endian = true;
1518 s->p_text->i_char_width = 2;
1521 else if( !memcmp( p_data, "\xFE\xFF", 2 ) )
1523 psz_encoding = "UTF-16BE";
1524 s->p_text->i_char_width = 2;
1528 /* Seek past the BOM */
1531 stream_Seek( s, i_bom_size );
1532 p_data += i_bom_size;
1533 i_data -= i_bom_size;
1536 /* Open the converter if we need it */
1537 if( psz_encoding != NULL )
1539 msg_Dbg( s, "%s BOM detected", psz_encoding );
1540 if( s->p_text->i_char_width > 1 )
1542 s->p_text->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1543 if( s->p_text->conv == (vlc_iconv_t)-1 )
1545 msg_Err( s, "iconv_open failed" );
1549 /* FIXME that's UGLY */
1550 input_thread_t *p_input;
1551 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1552 if( p_input != NULL)
1554 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1555 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1556 vlc_object_release( p_input );
1561 if( i_data % s->p_text->i_char_width )
1563 /* keep i_char_width boundary */
1564 i_data = i_data - ( i_data % s->p_text->i_char_width );
1565 msg_Warn( s, "the read is not i_char_width compatible");
1571 /* Check if there is an EOL */
1572 if( s->p_text->i_char_width == 1 )
1574 /* UTF-8: 0A <LF> */
1575 psz_eol = memchr( p_data, '\n', i_data );
1579 const uint8_t *p = p_data;
1580 const uint8_t *p_last = p + i_data - s->p_text->i_char_width;
1582 if( s->p_text->i_char_width == 2 )
1584 if( s->p_text->b_little_endian == true)
1586 /* UTF-16LE: 0A 00 <LF> */
1587 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1592 /* UTF-16BE: 00 0A <LF> */
1593 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1604 psz_eol = (char *)p + ( s->p_text->i_char_width - 1 );
1610 i_data = (psz_eol - (char *)p_data) + 1;
1611 p_line = realloc( p_line, i_line + i_data + s->p_text->i_char_width ); /* add \0 */
1614 i_data = stream_Read( s, &p_line[i_line], i_data );
1615 if( i_data <= 0 ) break; /* Hmmm */
1616 i_line += i_data - s->p_text->i_char_width; /* skip \n */;
1619 /* We have our line */
1623 /* Read data (+1 for easy \0 append) */
1624 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->p_text->i_char_width );
1627 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1628 if( i_data <= 0 ) break; /* Hmmm */
1636 for( j = 0; j < s->p_text->i_char_width; j++ )
1638 p_line[i_line + j] = '\0';
1640 i_line += s->p_text->i_char_width; /* the added \0 */
1641 if( s->p_text->i_char_width > 1 )
1643 size_t i_in = 0, i_out = 0;
1644 const char * p_in = NULL;
1645 char * p_out = NULL;
1646 char * psz_new_line = NULL;
1649 psz_new_line = malloc( i_line );
1650 if( psz_new_line == NULL )
1652 i_in = i_out = (size_t)i_line;
1654 p_out = psz_new_line;
1656 if( vlc_iconv( s->p_text->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1658 msg_Err( s, "iconv failed" );
1659 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1662 p_line = psz_new_line;
1663 i_line = (size_t)i_line - i_out; /* does not include \0 */
1666 /* Remove trailing LF/CR */
1667 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1668 p_line[i_line-2] == '\n') ) i_line--;
1670 /* Make sure the \0 is there */
1671 p_line[i_line-1] = '\0';
1677 /* We failed to read any data, probably EOF */
1681 if( s->p_text->conv != (vlc_iconv_t)(-1) )
1682 vlc_iconv_close( s->p_text->conv );
1683 s->p_text->conv = (vlc_iconv_t)(-1);
1687 /****************************************************************************
1688 * Access reading/seeking wrappers to handle concatenated streams.
1689 ****************************************************************************/
1690 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read )
1692 stream_sys_t *p_sys = s->p_sys;
1693 access_t *p_access = p_sys->p_access;
1694 input_thread_t *p_input = NULL;
1695 int i_read_orig = i_read;
1698 if( s->p_parent && s->p_parent->p_parent &&
1699 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1700 p_input = (input_thread_t *)s->p_parent->p_parent;
1702 if( !p_sys->i_list )
1704 i_read = p_access->pf_read( p_access, p_read, i_read );
1705 if( p_access->b_die )
1706 vlc_object_kill( s );
1709 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1710 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
1712 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1713 (float)i_total, NULL );
1714 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1715 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1720 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1722 if( p_access->b_die )
1723 vlc_object_kill( s );
1725 /* If we reached an EOF then switch to the next stream in the list */
1726 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1728 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1729 access_t *p_list_access;
1731 msg_Dbg( s, "opening input `%s'", psz_name );
1733 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1735 if( !p_list_access ) return 0;
1737 if( p_sys->p_list_access != p_access )
1738 access_Delete( p_sys->p_list_access );
1740 p_sys->p_list_access = p_list_access;
1742 /* We have to read some data */
1743 return AReadStream( s, p_read, i_read_orig );
1746 /* Update read bytes in input */
1749 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1750 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
1751 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1752 (float)i_total, NULL );
1753 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1754 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1759 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
1761 stream_sys_t *p_sys = s->p_sys;
1762 access_t *p_access = p_sys->p_access;
1763 input_thread_t *p_input = NULL;
1768 if( s->p_parent && s->p_parent->p_parent &&
1769 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1770 p_input = (input_thread_t *)s->p_parent->p_parent;
1772 if( !p_sys->i_list )
1774 p_block = p_access->pf_block( p_access );
1775 if( p_access->b_die )
1776 vlc_object_kill( s );
1777 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1778 if( p_input && p_block && libvlc_stats (p_access) )
1780 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1781 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1782 p_block->i_buffer, &i_total );
1783 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1784 (float)i_total, NULL );
1785 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1786 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1791 p_block = p_sys->p_list_access->pf_block( p_sys->p_list_access );
1792 if( p_access->b_die )
1793 vlc_object_kill( s );
1794 b_eof = p_sys->p_list_access->info.b_eof;
1795 if( pb_eof ) *pb_eof = b_eof;
1797 /* If we reached an EOF then switch to the next stream in the list */
1798 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1800 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1801 access_t *p_list_access;
1803 msg_Dbg( s, "opening input `%s'", psz_name );
1805 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1807 if( !p_list_access ) return 0;
1809 if( p_sys->p_list_access != p_access )
1810 access_Delete( p_sys->p_list_access );
1812 p_sys->p_list_access = p_list_access;
1814 /* We have to read some data */
1815 return AReadBlock( s, pb_eof );
1821 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1822 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1823 p_block->i_buffer, &i_total );
1824 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1825 (float)i_total, NULL );
1826 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
1828 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1834 static int ASeek( stream_t *s, int64_t i_pos )
1836 stream_sys_t *p_sys = s->p_sys;
1837 access_t *p_access = p_sys->p_access;
1839 /* Check which stream we need to access */
1845 access_t *p_list_access = 0;
1847 for( i = 0; i < p_sys->i_list - 1; i++ )
1849 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1850 i_size += p_sys->list[i]->i_size;
1852 psz_name = p_sys->list[i]->psz_path;
1854 if( i != p_sys->i_list_index )
1855 msg_Dbg( s, "opening input `%s'", psz_name );
1857 if( i != p_sys->i_list_index && i != 0 )
1860 access_New( s, p_access->psz_access, "", psz_name );
1862 else if( i != p_sys->i_list_index )
1864 p_list_access = p_access;
1869 if( p_sys->p_list_access != p_access )
1870 access_Delete( p_sys->p_list_access );
1872 p_sys->p_list_access = p_list_access;
1875 p_sys->i_list_index = i;
1876 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1880 return p_access->pf_seek( p_access, i_pos );
1885 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
1886 * "p_read" is NULL then data are skipped instead of read. The return
1887 * value is the real numbers of bytes read/skip. If this value is less
1888 * than i_read that means that it's the end of the stream.
1890 int stream_Read( stream_t *s, void *p_read, int i_read )
1892 return s->pf_read( s, p_read, i_read );
1896 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
1897 * \return The real numbers of valid bytes, if it's less
1898 * or equal to 0, *pp_peek is invalid.
1899 * \note pp_peek is a pointer to internal buffer and it will be invalid as
1900 * soons as other stream_* functions are called.
1901 * \note Due to input limitation, it could be less than i_peek without meaning
1902 * the end of the stream (but only when you have i_peek >=
1903 * p_input->i_bufsize)
1905 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
1907 return s->pf_peek( s, pp_peek, i_peek );
1911 * Use to control the "stream_t *". Look at #stream_query_e for
1912 * possible "i_query" value and format arguments. Return VLC_SUCCESS
1913 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
1915 int stream_vaControl( stream_t *s, int i_query, va_list args )
1917 return s->pf_control( s, i_query, args );
1923 void stream_Delete( stream_t *s )
1928 int stream_Control( stream_t *s, int i_query, ... )
1934 return VLC_EGENERIC;
1936 va_start( args, i_query );
1937 i_result = s->pf_control( s, i_query, args );
1943 * Read "i_size" bytes and store them in a block_t.
1944 * It always read i_size bytes unless you are at the end of the stream
1945 * where it return what is available.
1947 block_t *stream_Block( stream_t *s, int i_size )
1949 if( i_size <= 0 ) return NULL;
1951 /* emulate block read */
1952 block_t *p_bk = block_New( s, i_size );
1955 int i_read = stream_Read( s, p_bk->p_buffer, i_size );
1958 p_bk->i_buffer = i_read;
1961 block_Release( p_bk );