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>
35 #include <vlc_memory.h>
42 #include "input_internal.h"
44 // #define STREAM_DEBUG 1
47 * - tune the 2 methods (block/stream)
48 * - compute cost for seek
49 * - improve stream mode seeking with closest segments
55 * One linked list of data read
57 * More complex scheme using mutliple track to avoid seeking
58 * - using directly the access (only indirection for peeking).
59 * This method is known to introduce much less latency.
60 * It should probably defaulted (instead of the stream method (2)).
63 /* How many tracks we have, currently only used for stream mode */
64 #ifdef OPTIMIZE_MEMORY
65 # define STREAM_CACHE_TRACK 1
66 /* Max size of our cache 128Ko per track */
67 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
69 # define STREAM_CACHE_TRACK 3
70 /* Max size of our cache 4Mo per track */
71 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
74 /* How many data we try to prebuffer
75 * XXX it should be small to avoid useless latency but big enough for
76 * efficient demux probing */
77 #define STREAM_CACHE_PREBUFFER_SIZE (128)
79 /* Method1: Simple, for pf_block.
80 * We get blocks and put them in the linked list.
81 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
84 /* Method2: A bit more complex, for pf_read
85 * - We use ring buffers, only one if unseekable, all if seekable
86 * - Upon seek date current ring, then search if one ring match the pos,
87 * yes: switch to it, seek the access to match the end of the ring
88 * no: search the ring with i_end the closer to i_pos,
89 * if close enough, read data and use this ring
90 * else use the oldest ring, seek and use it.
92 * TODO: - with access non seekable: use all space available for only one ring, but
93 * we have to support seekable/non-seekable switch on the fly.
94 * - compute a good value for i_read_size
97 #define STREAM_READ_ATONCE 1024
98 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
122 } stream_read_method_t;
128 stream_read_method_t method; /* method to use */
130 uint64_t i_pos; /* Current reading offset */
132 /* Method 1: pf_block */
135 uint64_t i_start; /* Offset of block for p_first */
136 uint64_t i_offset; /* Offset for data in p_current */
137 block_t *p_current; /* Current block */
139 uint64_t i_size; /* Total amount of data in the list */
145 /* Method 2: for pf_read */
148 unsigned i_offset; /* Buffer offset in the current track */
149 int i_tk; /* Current track */
150 stream_track_t tk[STREAM_CACHE_TRACK];
156 unsigned i_used; /* Used since last read */
157 unsigned i_read_size;
161 /* Peek temporary buffer */
165 /* Stat for both method */
168 bool b_fastseek; /* From access */
170 /* Stat about reading data */
171 uint64_t i_read_count;
173 uint64_t i_read_time;
175 /* Stat about seek */
176 unsigned i_seek_count;
177 uint64_t i_seek_time;
183 access_entry_t **list;
185 access_t *p_list_access;
189 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read );
190 static int AStreamPeekBlock( stream_t *s, const uint8_t **p_peek, unsigned int i_read );
191 static int AStreamSeekBlock( stream_t *s, uint64_t i_pos );
192 static void AStreamPrebufferBlock( stream_t *s );
193 static block_t *AReadBlock( stream_t *s, bool *pb_eof );
196 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read );
197 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read );
198 static int AStreamSeekStream( stream_t *s, uint64_t i_pos );
199 static void AStreamPrebufferStream( stream_t *s );
200 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read );
203 static int AStreamControl( stream_t *s, int i_query, va_list );
204 static void AStreamDestroy( stream_t *s );
205 static void UStreamDestroy( stream_t *s );
206 static int ASeek( stream_t *s, uint64_t i_pos );
208 /****************************************************************************
209 * stream_CommonNew: create an empty stream structure
210 ****************************************************************************/
211 stream_t *stream_CommonNew( vlc_object_t *p_obj )
213 stream_t *s = (stream_t *)vlc_custom_create( p_obj, sizeof(*s),
214 VLC_OBJECT_GENERIC, "stream" );
219 s->p_text = malloc( sizeof(*s->p_text) );
222 vlc_object_release( s );
226 /* UTF16 and UTF32 text file conversion */
227 s->p_text->conv = (vlc_iconv_t)(-1);
228 s->p_text->i_char_width = 1;
229 s->p_text->b_little_endian = false;
234 void stream_CommonDelete( stream_t *s )
238 if( s->p_text->conv != (vlc_iconv_t)(-1) )
239 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 /* Get a weak link to the parent input */
264 /* FIXME: This should probably be removed in favor of a NULL input. */
265 input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_parent, VLC_OBJECT_INPUT, FIND_PARENT );
267 /* Now try a real access */
268 p_access = access_New( p_parent, p_input, psz_access, psz_demux, psz_path );
271 vlc_object_release((vlc_object_t*)p_input);
273 if( p_access == NULL )
275 msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
279 if( !( p_res = stream_AccessNew( p_access, NULL ) ) )
281 access_Delete( p_access );
285 p_res->pf_destroy = UStreamDestroy;
289 stream_t *stream_AccessNew( access_t *p_access, char **ppsz_list )
291 stream_t *s = stream_CommonNew( VLC_OBJECT(p_access) );
297 s->p_input = p_access->p_input;
298 s->psz_path = strdup( p_access->psz_path );
299 s->p_sys = p_sys = malloc( sizeof( *p_sys ) );
300 if( !s->psz_path || !s->p_sys )
302 stream_CommonDelete( s );
306 /* Attach it now, needed for b_die */
307 vlc_object_attach( s, p_access );
309 s->pf_read = NULL; /* Set up later */
311 s->pf_control = AStreamControl;
312 s->pf_destroy = AStreamDestroy;
315 p_sys->p_access = p_access;
316 if( p_access->pf_block )
317 p_sys->method = STREAM_METHOD_BLOCK;
319 p_sys->method = STREAM_METHOD_STREAM;
321 p_sys->i_pos = p_access->info.i_pos;
324 access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
325 p_sys->stat.i_bytes = 0;
326 p_sys->stat.i_read_time = 0;
327 p_sys->stat.i_read_count = 0;
328 p_sys->stat.i_seek_count = 0;
329 p_sys->stat.i_seek_time = 0;
331 TAB_INIT( p_sys->i_list, p_sys->list );
332 p_sys->i_list_index = 0;
333 p_sys->p_list_access = NULL;
335 /* Get the additional list of inputs if any (for concatenation) */
336 if( ppsz_list && ppsz_list[0] )
338 access_entry_t *p_entry = malloc( sizeof(*p_entry) );
342 p_entry->i_size = p_access->info.i_size;
343 p_entry->psz_path = strdup( p_access->psz_path );
344 if( !p_entry->psz_path )
349 p_sys->p_list_access = p_access;
350 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
351 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
352 p_entry->psz_path, p_access->info.i_size );
354 for( int i = 0; ppsz_list[i] != NULL; i++ )
356 char *psz_name = strdup( ppsz_list[i] );
361 access_t *p_tmp = access_New( p_access, p_access->p_input,
362 p_access->psz_access, "", psz_name );
366 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
367 psz_name, p_tmp->info.i_size );
369 p_entry = malloc( sizeof(*p_entry) );
372 p_entry->i_size = p_tmp->info.i_size;
373 p_entry->psz_path = psz_name;
374 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
376 access_Delete( p_tmp );
382 p_sys->p_peek = NULL;
384 if( p_sys->method == STREAM_METHOD_BLOCK )
386 msg_Dbg( s, "Using AStream*Block" );
387 s->pf_read = AStreamReadBlock;
388 s->pf_peek = AStreamPeekBlock;
390 /* Init all fields of p_sys->block */
391 p_sys->block.i_start = p_sys->i_pos;
392 p_sys->block.i_offset = 0;
393 p_sys->block.p_current = NULL;
394 p_sys->block.i_size = 0;
395 p_sys->block.p_first = NULL;
396 p_sys->block.pp_last = &p_sys->block.p_first;
398 /* Do the prebuffering */
399 AStreamPrebufferBlock( s );
401 if( p_sys->block.i_size <= 0 )
403 msg_Err( s, "cannot pre fill buffer" );
411 assert( p_sys->method == STREAM_METHOD_STREAM );
413 msg_Dbg( s, "Using AStream*Stream" );
415 s->pf_read = AStreamReadStream;
416 s->pf_peek = AStreamPeekStream;
418 /* Allocate/Setup our tracks */
419 p_sys->stream.i_offset = 0;
420 p_sys->stream.i_tk = 0;
421 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
422 if( p_sys->stream.p_buffer == NULL )
424 p_sys->stream.i_used = 0;
425 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
426 #if STREAM_READ_ATONCE < 256
427 # error "Invalid STREAM_READ_ATONCE value"
430 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
432 p_sys->stream.tk[i].i_date = 0;
433 p_sys->stream.tk[i].i_start = p_sys->i_pos;
434 p_sys->stream.tk[i].i_end = p_sys->i_pos;
435 p_sys->stream.tk[i].p_buffer=
436 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
439 /* Do the prebuffering */
440 AStreamPrebufferStream( s );
442 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
444 msg_Err( s, "cannot pre fill buffer" );
452 if( p_sys->method == STREAM_METHOD_BLOCK )
458 free( p_sys->stream.p_buffer );
460 while( p_sys->i_list > 0 )
461 free( p_sys->list[--(p_sys->i_list)] );
464 stream_CommonDelete( s );
468 /****************************************************************************
470 ****************************************************************************/
471 static void AStreamDestroy( stream_t *s )
473 stream_sys_t *p_sys = s->p_sys;
475 if( p_sys->method == STREAM_METHOD_BLOCK )
476 block_ChainRelease( p_sys->block.p_first );
478 free( p_sys->stream.p_buffer );
480 free( p_sys->p_peek );
482 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
483 access_Delete( p_sys->p_list_access );
485 while( p_sys->i_list-- )
487 free( p_sys->list[p_sys->i_list]->psz_path );
488 free( p_sys->list[p_sys->i_list] );
494 stream_CommonDelete( s );
497 static void UStreamDestroy( stream_t *s )
499 access_t *p_access = (access_t *)s->p_parent;
501 access_Delete( p_access );
504 /****************************************************************************
505 * AStreamControlReset:
506 ****************************************************************************/
507 static void AStreamControlReset( stream_t *s )
509 stream_sys_t *p_sys = s->p_sys;
511 p_sys->i_pos = p_sys->p_access->info.i_pos;
513 if( p_sys->method == STREAM_METHOD_BLOCK )
515 block_ChainRelease( p_sys->block.p_first );
517 /* Init all fields of p_sys->block */
518 p_sys->block.i_start = p_sys->i_pos;
519 p_sys->block.i_offset = 0;
520 p_sys->block.p_current = NULL;
521 p_sys->block.i_size = 0;
522 p_sys->block.p_first = NULL;
523 p_sys->block.pp_last = &p_sys->block.p_first;
525 /* Do the prebuffering */
526 AStreamPrebufferBlock( s );
532 assert( p_sys->method == STREAM_METHOD_STREAM );
534 /* Setup our tracks */
535 p_sys->stream.i_offset = 0;
536 p_sys->stream.i_tk = 0;
537 p_sys->stream.i_used = 0;
539 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
541 p_sys->stream.tk[i].i_date = 0;
542 p_sys->stream.tk[i].i_start = p_sys->i_pos;
543 p_sys->stream.tk[i].i_end = p_sys->i_pos;
546 /* Do the prebuffering */
547 AStreamPrebufferStream( s );
551 /****************************************************************************
552 * AStreamControlUpdate:
553 ****************************************************************************/
554 static void AStreamControlUpdate( stream_t *s )
556 stream_sys_t *p_sys = s->p_sys;
558 p_sys->i_pos = p_sys->p_access->info.i_pos;
563 for( i = 0; i < p_sys->i_list_index; i++ )
565 p_sys->i_pos += p_sys->list[i]->i_size;
570 /****************************************************************************
572 ****************************************************************************/
573 static int AStreamControl( stream_t *s, int i_query, va_list args )
575 stream_sys_t *p_sys = s->p_sys;
576 access_t *p_access = p_sys->p_access;
579 uint64_t *pi_64, i_64;
584 case STREAM_GET_SIZE:
585 pi_64 = va_arg( args, uint64_t * );
586 if( s->p_sys->i_list )
590 for( i = 0; i < s->p_sys->i_list; i++ )
591 *pi_64 += s->p_sys->list[i]->i_size;
594 *pi_64 = p_access->info.i_size;
597 case STREAM_CAN_SEEK:
598 p_bool = (bool*)va_arg( args, bool * );
599 access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
602 case STREAM_CAN_FASTSEEK:
603 p_bool = (bool*)va_arg( args, bool * );
604 access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
607 case STREAM_GET_POSITION:
608 pi_64 = va_arg( args, uint64_t * );
609 *pi_64 = p_sys->i_pos;
612 case STREAM_SET_POSITION:
613 i_64 = va_arg( args, uint64_t );
614 switch( p_sys->method )
616 case STREAM_METHOD_BLOCK:
617 return AStreamSeekBlock( s, i_64 );
618 case STREAM_METHOD_STREAM:
619 return AStreamSeekStream( s, i_64 );
625 case STREAM_CONTROL_ACCESS:
627 i_int = (int) va_arg( args, int );
628 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
629 i_int != ACCESS_SET_PRIVATE_ID_CA &&
630 i_int != ACCESS_GET_PRIVATE_ID_STATE &&
631 i_int != ACCESS_SET_TITLE &&
632 i_int != ACCESS_SET_SEEKPOINT )
634 msg_Err( s, "Hey, what are you thinking ?"
635 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
638 int i_ret = access_vaControl( p_access, i_int, args );
639 if( i_int == ACCESS_SET_TITLE || i_int == ACCESS_SET_SEEKPOINT )
640 AStreamControlReset( s );
644 case STREAM_UPDATE_SIZE:
645 AStreamControlUpdate( s );
648 case STREAM_GET_CONTENT_TYPE:
649 return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
650 va_arg( args, char ** ) );
651 case STREAM_SET_RECORD_STATE:
653 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
659 /****************************************************************************
661 ****************************************************************************/
662 static void AStreamPrebufferBlock( stream_t *s )
664 stream_sys_t *p_sys = s->p_sys;
669 msg_Dbg( s, "pre buffering" );
673 const int64_t i_date = mdate();
677 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE )
682 p_sys->stat.i_bytes = p_sys->block.i_size;
683 p_sys->stat.i_read_time = i_date - i_start;
684 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
685 (p_sys->stat.i_read_time + 1);
687 msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
688 "%"PRId64" kbytes/s",
690 p_sys->stat.i_read_time / INT64_C(1000000),
696 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
705 /* Append the block */
706 p_sys->block.i_size += b->i_buffer;
707 *p_sys->block.pp_last = b;
708 p_sys->block.pp_last = &b->p_next;
710 p_sys->stat.i_read_count++;
717 msg_Dbg( s, "received first data after %d ms",
718 (int)((i_first-i_start)/1000) );
722 p_sys->block.p_current = p_sys->block.p_first;
725 static int AStreamRefillBlock( stream_t *s );
727 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
729 stream_sys_t *p_sys = s->p_sys;
731 uint8_t *p_data = p_read;
732 unsigned int i_data = 0;
735 if( p_sys->block.p_current == NULL )
740 /* seek within this stream if possible, else use plain old read and discard */
741 stream_sys_t *p_sys = s->p_sys;
742 access_t *p_access = p_sys->p_access;
744 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
746 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
749 while( i_data < i_read )
752 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
753 unsigned int i_copy = __MIN( (unsigned int)__MAX(i_current,0), i_read - i_data);
759 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
765 p_sys->block.i_offset += i_copy;
766 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
768 /* Current block is now empty, switch to next */
769 if( p_sys->block.p_current )
771 p_sys->block.i_offset = 0;
772 p_sys->block.p_current = p_sys->block.p_current->p_next;
774 /*Get a new block if needed */
775 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
782 p_sys->i_pos += i_data;
786 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
788 stream_sys_t *p_sys = s->p_sys;
790 unsigned int i_data = 0;
792 unsigned int i_offset;
794 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
796 /* We can directly give a pointer over our buffer */
797 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
799 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
803 /* We need to create a local copy */
804 if( p_sys->i_peek < i_read )
806 p_sys->p_peek = realloc_or_free( p_sys->p_peek, i_read );
812 p_sys->i_peek = i_read;
815 /* Fill enough data */
816 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
819 block_t **pp_last = p_sys->block.pp_last;
821 if( AStreamRefillBlock( s ) ) break;
823 /* Our buffer are probably filled enough, don't try anymore */
824 if( pp_last == p_sys->block.pp_last ) break;
827 /* Copy what we have */
828 b = p_sys->block.p_current;
829 i_offset = p_sys->block.i_offset;
830 p_data = p_sys->p_peek;
832 while( b && i_data < i_read )
834 unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
835 int i_copy = __MIN( i_current, i_read - i_data );
837 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
842 if( i_offset >= b->i_buffer )
849 *pp_peek = p_sys->p_peek;
853 static int AStreamSeekBlock( stream_t *s, uint64_t i_pos )
855 stream_sys_t *p_sys = s->p_sys;
856 access_t *p_access = p_sys->p_access;
857 int64_t i_offset = i_pos - p_sys->block.i_start;
860 /* We already have thoses data, just update p_current/i_offset */
861 if( i_offset >= 0 && (uint64_t)i_offset < p_sys->block.i_size )
863 block_t *b = p_sys->block.p_first;
866 while( i_current + b->i_buffer < (uint64_t)i_offset )
868 i_current += b->i_buffer;
872 p_sys->block.p_current = b;
873 p_sys->block.i_offset = i_offset - i_current;
875 p_sys->i_pos = i_pos;
880 /* We may need to seek or to read data */
884 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
888 msg_Err( s, "backward seeking impossible (access not seekable)" );
896 bool b_aseek, b_aseekfast;
898 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
899 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
904 msg_Warn( s, "%"PRId64" bytes need to be skipped "
905 "(access non seekable)",
906 i_offset - p_sys->block.i_size );
910 int64_t i_skip = i_offset - p_sys->block.i_size;
912 /* Avg bytes per packets */
913 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
914 /* TODO compute a seek cost instead of fixed threshold */
915 int i_th = b_aseekfast ? 1 : 5;
917 if( i_skip <= i_th * i_avg &&
918 i_skip < STREAM_CACHE_SIZE )
923 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
924 b_seek, i_th*i_avg, i_skip );
930 int64_t i_start, i_end;
931 /* Do the access seek */
933 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
937 block_ChainRelease( p_sys->block.p_first );
940 p_sys->block.i_start = p_sys->i_pos = i_pos;
941 p_sys->block.i_offset = 0;
942 p_sys->block.p_current = NULL;
943 p_sys->block.i_size = 0;
944 p_sys->block.p_first = NULL;
945 p_sys->block.pp_last = &p_sys->block.p_first;
948 if( AStreamRefillBlock( s ) )
952 p_sys->stat.i_seek_time += i_end - i_start;
953 p_sys->stat.i_seek_count++;
960 while( p_sys->block.p_current &&
961 p_sys->i_pos + p_sys->block.p_current->i_buffer - p_sys->block.i_offset <= i_pos )
963 p_sys->i_pos += p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
964 p_sys->block.p_current = p_sys->block.p_current->p_next;
965 p_sys->block.i_offset = 0;
967 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
969 if( p_sys->i_pos != i_pos )
973 while( p_sys->block.i_start + p_sys->block.i_size < i_pos );
975 p_sys->block.i_offset += i_pos - p_sys->i_pos;
976 p_sys->i_pos = i_pos;
984 static int AStreamRefillBlock( stream_t *s )
986 stream_sys_t *p_sys = s->p_sys;
990 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
991 p_sys->block.p_first != p_sys->block.p_current )
993 block_t *b = p_sys->block.p_first;
995 p_sys->block.i_start += b->i_buffer;
996 p_sys->block.i_size -= b->i_buffer;
997 p_sys->block.p_first = b->p_next;
1001 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1002 p_sys->block.p_current == p_sys->block.p_first &&
1003 p_sys->block.p_current->p_next ) /* At least 2 packets */
1005 /* Enough data, don't read more */
1009 /* Now read a new block */
1010 const int64_t i_start = mdate();
1016 return VLC_EGENERIC;
1019 if( ( b = AReadBlock( s, &b_eof ) ) )
1022 return VLC_EGENERIC;
1025 p_sys->stat.i_read_time += mdate() - i_start;
1028 /* Append the block */
1029 p_sys->block.i_size += b->i_buffer;
1030 *p_sys->block.pp_last = b;
1031 p_sys->block.pp_last = &b->p_next;
1034 if( p_sys->block.p_current == NULL )
1035 p_sys->block.p_current = b;
1038 p_sys->stat.i_bytes += b->i_buffer;
1039 p_sys->stat.i_read_count++;
1047 /****************************************************************************
1049 ****************************************************************************/
1050 static int AStreamRefillStream( stream_t *s );
1051 static int AStreamReadNoSeekStream( stream_t *s, void *p_read, unsigned int i_read );
1053 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read )
1055 stream_sys_t *p_sys = s->p_sys;
1059 const uint64_t i_pos_wanted = p_sys->i_pos + i_read;
1061 if( AStreamSeekStream( s, i_pos_wanted ) )
1063 if( p_sys->i_pos != i_pos_wanted )
1068 return AStreamReadNoSeekStream( s, p_read, i_read );
1071 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1073 stream_sys_t *p_sys = s->p_sys;
1074 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1077 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1080 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1081 "start=%"PRId64" offset=%d end=%"PRId64,
1082 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1083 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1086 /* Avoid problem, but that should *never* happen */
1087 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1088 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1090 while( tk->i_end < tk->i_start + p_sys->stream.i_offset + i_read )
1092 if( p_sys->stream.i_used <= 1 )
1094 /* Be sure we will read something */
1095 p_sys->stream.i_used += tk->i_start + p_sys->stream.i_offset + i_read - tk->i_end;
1097 if( AStreamRefillStream( s ) ) break;
1100 if( tk->i_end < tk->i_start + p_sys->stream.i_offset + i_read )
1102 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1106 /* Now, direct pointer or a copy ? */
1107 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1108 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1110 *pp_peek = &tk->p_buffer[i_off];
1114 if( p_sys->i_peek < i_read )
1116 p_sys->p_peek = realloc_or_free( p_sys->p_peek, i_read );
1117 if( !p_sys->p_peek )
1122 p_sys->i_peek = i_read;
1125 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1126 STREAM_CACHE_TRACK_SIZE - i_off );
1127 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1128 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1130 *pp_peek = p_sys->p_peek;
1134 static int AStreamSeekStream( stream_t *s, uint64_t i_pos )
1136 stream_sys_t *p_sys = s->p_sys;
1138 stream_track_t *p_current = &p_sys->stream.tk[p_sys->stream.i_tk];
1139 access_t *p_access = p_sys->p_access;
1141 if( p_current->i_start >= p_current->i_end && i_pos >= p_current->i_end )
1145 msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1146 " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1147 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1149 p_sys->stream.i_offset,
1154 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1155 if( !b_aseek && i_pos < p_current->i_start )
1157 msg_Warn( s, "AStreamSeekStream: can't seek" );
1158 return VLC_EGENERIC;
1162 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_afastseek );
1164 /* FIXME compute seek cost (instead of static 'stupid' value) */
1165 uint64_t i_skip_threshold;
1167 i_skip_threshold = b_afastseek ? 128 : 3*p_sys->stream.i_read_size;
1169 i_skip_threshold = INT64_MAX;
1171 /* Date the current track */
1172 p_current->i_date = mdate();
1174 /* Search a new track slot */
1175 stream_track_t *tk = NULL;
1178 /* Prefer the current track */
1179 if( p_current->i_start <= i_pos && i_pos <= p_current->i_end + i_skip_threshold )
1182 i_tk_idx = p_sys->stream.i_tk;
1186 /* Try to maximize already read data */
1187 for( int i = 0; i < STREAM_CACHE_TRACK; i++ )
1189 stream_track_t *t = &p_sys->stream.tk[i];
1191 if( t->i_start > i_pos || i_pos > t->i_end )
1194 if( !tk || tk->i_end < t->i_end )
1203 /* Use the oldest unused */
1204 for( int i = 0; i < STREAM_CACHE_TRACK; i++ )
1206 stream_track_t *t = &p_sys->stream.tk[i];
1208 if( !tk || tk->i_date > t->i_date )
1215 assert( i_tk_idx >= 0 && i_tk_idx < STREAM_CACHE_TRACK );
1217 if( tk != p_current )
1218 i_skip_threshold = 0;
1219 if( tk->i_start <= i_pos && i_pos <= tk->i_end + i_skip_threshold )
1222 msg_Err( s, "AStreamSeekStream: reusing %d start=%"PRId64
1223 " end=%"PRId64"(%s)",
1224 i_tk_idx, tk->i_start, tk->i_end,
1225 tk != p_current ? "seek" : i_pos > tk->i_end ? "skip" : "noseek" );
1227 if( tk != p_current )
1231 /* Seek at the end of the buffer
1232 * TODO it is stupid to seek now, it would be better to delay it
1234 if( ASeek( s, tk->i_end ) )
1235 return VLC_EGENERIC;
1237 else if( i_pos > tk->i_end )
1239 uint64_t i_skip = i_pos - tk->i_end;
1242 const int i_read_max = __MIN( 10 * STREAM_READ_ATONCE, i_skip );
1243 if( AStreamReadNoSeekStream( s, NULL, i_read_max ) != i_read_max )
1244 return VLC_EGENERIC;
1245 i_skip -= i_read_max;
1252 msg_Err( s, "AStreamSeekStream: hard seek" );
1254 /* Nothing good, seek and choose oldest segment */
1255 if( ASeek( s, i_pos ) )
1256 return VLC_EGENERIC;
1258 tk->i_start = i_pos;
1261 p_sys->stream.i_offset = i_pos - tk->i_start;
1262 p_sys->stream.i_tk = i_tk_idx;
1263 p_sys->i_pos = i_pos;
1265 /* If there is not enough data left in the track, refill */
1266 /* TODO How to get a correct value for
1267 * - refilling threshold
1268 * - how much to refill
1270 if( tk->i_end < tk->i_start + p_sys->stream.i_offset + p_sys->stream.i_read_size )
1272 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1273 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1275 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1276 return VLC_EGENERIC;
1281 static int AStreamReadNoSeekStream( stream_t *s, void *p_read, unsigned int i_read )
1283 stream_sys_t *p_sys = s->p_sys;
1284 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1286 uint8_t *p_data = (uint8_t *)p_read;
1287 unsigned int i_data = 0;
1289 if( tk->i_start >= tk->i_end )
1293 msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1294 " offset=%d end=%"PRId64,
1295 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1296 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1299 while( i_data < i_read )
1301 unsigned i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1302 unsigned int i_current =
1303 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1304 STREAM_CACHE_TRACK_SIZE - i_off );
1305 int i_copy = __MIN( i_current, i_read - i_data );
1307 if( i_copy <= 0 ) break; /* EOF */
1310 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1313 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1317 p_sys->stream.i_offset += i_copy;
1319 /* Update pos now */
1320 p_sys->i_pos += i_copy;
1323 p_sys->stream.i_used += i_copy;
1325 if( tk->i_end + i_data <= tk->i_start + p_sys->stream.i_offset + i_read )
1327 const unsigned i_read_requested = __MAX( __MIN( i_read - i_data,
1328 STREAM_READ_ATONCE * 10 ),
1329 STREAM_READ_ATONCE / 2 );
1331 if( p_sys->stream.i_used < i_read_requested )
1332 p_sys->stream.i_used = i_read_requested;
1334 if( AStreamRefillStream( s ) )
1337 if( tk->i_start >= tk->i_end ) break;
1346 static int AStreamRefillStream( stream_t *s )
1348 stream_sys_t *p_sys = s->p_sys;
1349 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1351 /* We read but won't increase i_start after initial start + offset */
1353 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1354 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1355 bool b_read = false;
1356 int64_t i_start, i_stop;
1358 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1361 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1362 p_sys->stream.i_used, i_toread );
1366 while( i_toread > 0 )
1368 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1372 return VLC_EGENERIC;
1374 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1375 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1377 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1382 else if( i_read == 0 )
1385 return VLC_EGENERIC;
1391 tk->i_end += i_read;
1393 /* Windows of STREAM_CACHE_TRACK_SIZE */
1394 if( tk->i_start + STREAM_CACHE_TRACK_SIZE < tk->i_end )
1396 unsigned i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1398 tk->i_start += i_invalid;
1399 p_sys->stream.i_offset -= i_invalid;
1403 p_sys->stream.i_used -= i_read;
1405 p_sys->stat.i_bytes += i_read;
1406 p_sys->stat.i_read_count++;
1410 p_sys->stat.i_read_time += i_stop - i_start;
1415 static void AStreamPrebufferStream( stream_t *s )
1417 stream_sys_t *p_sys = s->p_sys;
1419 int64_t i_first = 0;
1422 msg_Dbg( s, "pre buffering" );
1426 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1428 int64_t i_date = mdate();
1430 int i_buffered = tk->i_end - tk->i_start;
1432 if( s->b_die || i_buffered >= STREAM_CACHE_PREBUFFER_SIZE )
1437 p_sys->stat.i_bytes = i_buffered;
1438 p_sys->stat.i_read_time = i_date - i_start;
1439 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1440 (p_sys->stat.i_read_time+1);
1442 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1443 "%"PRId64" kbytes/s",
1444 p_sys->stat.i_bytes,
1445 p_sys->stat.i_read_time / INT64_C(1000000),
1446 i_byterate / 1024 );
1451 i_read = STREAM_CACHE_TRACK_SIZE - i_buffered;
1452 i_read = __MIN( (int)p_sys->stream.i_read_size, i_read );
1453 i_read = AReadStream( s, &tk->p_buffer[i_buffered], i_read );
1456 else if( i_read == 0 )
1462 msg_Dbg( s, "received first data after %d ms",
1463 (int)((i_first-i_start)/1000) );
1466 tk->i_end += i_read;
1468 p_sys->stat.i_read_count++;
1472 /****************************************************************************
1474 ****************************************************************************/
1476 * Read from the stream untill first newline.
1477 * \param s Stream handle to read from
1478 * \return A pointer to the allocated output string. You need to free this when you are done.
1480 #define STREAM_PROBE_LINE 2048
1481 #define STREAM_LINE_MAX (2048*100)
1482 char *stream_ReadLine( stream_t *s )
1484 char *p_line = NULL;
1485 int i_line = 0, i_read = 0;
1487 while( i_read < STREAM_LINE_MAX )
1490 const uint8_t *p_data;
1494 /* Probe new data */
1495 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1496 if( i_data <= 0 ) break; /* No more data */
1499 i_pos = stream_Tell( s );
1500 if( i_pos == 0 && i_data >= 3 )
1503 const char *psz_encoding = NULL;
1505 if( !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
1507 psz_encoding = "UTF-8";
1510 else if( !memcmp( p_data, "\xFF\xFE", 2 ) )
1512 psz_encoding = "UTF-16LE";
1513 s->p_text->b_little_endian = true;
1514 s->p_text->i_char_width = 2;
1517 else if( !memcmp( p_data, "\xFE\xFF", 2 ) )
1519 psz_encoding = "UTF-16BE";
1520 s->p_text->i_char_width = 2;
1524 /* Seek past the BOM */
1527 stream_Seek( s, i_bom_size );
1528 p_data += i_bom_size;
1529 i_data -= i_bom_size;
1532 /* Open the converter if we need it */
1533 if( psz_encoding != NULL )
1535 msg_Dbg( s, "%s BOM detected", psz_encoding );
1536 if( s->p_text->i_char_width > 1 )
1538 s->p_text->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1539 if( s->p_text->conv == (vlc_iconv_t)-1 )
1541 msg_Err( s, "iconv_open failed" );
1545 /* FIXME that's UGLY */
1546 input_thread_t *p_input = s->p_input;
1547 if( p_input != NULL)
1549 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1550 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1555 if( i_data % s->p_text->i_char_width )
1557 /* keep i_char_width boundary */
1558 i_data = i_data - ( i_data % s->p_text->i_char_width );
1559 msg_Warn( s, "the read is not i_char_width compatible");
1565 /* Check if there is an EOL */
1566 if( s->p_text->i_char_width == 1 )
1568 /* UTF-8: 0A <LF> */
1569 psz_eol = memchr( p_data, '\n', i_data );
1573 const uint8_t *p = p_data;
1574 const uint8_t *p_last = p + i_data - s->p_text->i_char_width;
1576 if( s->p_text->i_char_width == 2 )
1578 if( s->p_text->b_little_endian == true)
1580 /* UTF-16LE: 0A 00 <LF> */
1581 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1586 /* UTF-16BE: 00 0A <LF> */
1587 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1598 psz_eol = (char *)p + ( s->p_text->i_char_width - 1 );
1604 i_data = (psz_eol - (char *)p_data) + 1;
1605 p_line = realloc_or_free( p_line,
1606 i_line + i_data + s->p_text->i_char_width ); /* add \0 */
1609 i_data = stream_Read( s, &p_line[i_line], i_data );
1610 if( i_data <= 0 ) break; /* Hmmm */
1611 i_line += i_data - s->p_text->i_char_width; /* skip \n */;
1614 /* We have our line */
1618 /* Read data (+1 for easy \0 append) */
1619 p_line = realloc_or_free( p_line,
1620 i_line + STREAM_PROBE_LINE + s->p_text->i_char_width );
1623 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1624 if( i_data <= 0 ) break; /* Hmmm */
1632 for( j = 0; j < s->p_text->i_char_width; j++ )
1634 p_line[i_line + j] = '\0';
1636 i_line += s->p_text->i_char_width; /* the added \0 */
1637 if( s->p_text->i_char_width > 1 )
1639 size_t i_in = 0, i_out = 0;
1640 const char * p_in = NULL;
1641 char * p_out = NULL;
1642 char * psz_new_line = NULL;
1645 psz_new_line = malloc( i_line );
1646 if( psz_new_line == NULL )
1648 i_in = i_out = (size_t)i_line;
1650 p_out = psz_new_line;
1652 if( vlc_iconv( s->p_text->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1654 msg_Err( s, "iconv failed" );
1655 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1658 p_line = psz_new_line;
1659 i_line = (size_t)i_line - i_out; /* does not include \0 */
1662 /* Remove trailing LF/CR */
1663 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1664 p_line[i_line-2] == '\n') ) i_line--;
1666 /* Make sure the \0 is there */
1667 p_line[i_line-1] = '\0';
1673 /* We failed to read any data, probably EOF */
1677 if( s->p_text->conv != (vlc_iconv_t)(-1) )
1678 vlc_iconv_close( s->p_text->conv );
1679 s->p_text->conv = (vlc_iconv_t)(-1);
1683 /****************************************************************************
1684 * Access reading/seeking wrappers to handle concatenated streams.
1685 ****************************************************************************/
1686 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read )
1688 stream_sys_t *p_sys = s->p_sys;
1689 access_t *p_access = p_sys->p_access;
1690 input_thread_t *p_input = NULL;
1691 int i_read_orig = i_read;
1694 if( s->p_parent && s->p_parent->p_parent &&
1695 vlc_internals( s->p_parent->p_parent )->i_object_type == VLC_OBJECT_INPUT )
1696 p_input = (input_thread_t *)s->p_parent->p_parent;
1698 if( !p_sys->i_list )
1700 i_read = p_access->pf_read( p_access, p_read, i_read );
1701 if( p_access->b_die )
1702 vlc_object_kill( s );
1705 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1706 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
1708 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1709 (float)i_total, NULL );
1710 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1711 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1716 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1718 if( p_access->b_die )
1719 vlc_object_kill( s );
1721 /* If we reached an EOF then switch to the next stream in the list */
1722 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1724 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1725 access_t *p_list_access;
1727 msg_Dbg( s, "opening input `%s'", psz_name );
1729 p_list_access = access_New( s, s->p_input, p_access->psz_access, "", psz_name );
1731 if( !p_list_access ) return 0;
1733 if( p_sys->p_list_access != p_access )
1734 access_Delete( p_sys->p_list_access );
1736 p_sys->p_list_access = p_list_access;
1738 /* We have to read some data */
1739 return AReadStream( s, p_read, i_read_orig );
1742 /* Update read bytes in input */
1745 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1746 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
1747 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1748 (float)i_total, NULL );
1749 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1750 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1755 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
1757 stream_sys_t *p_sys = s->p_sys;
1758 access_t *p_access = p_sys->p_access;
1759 input_thread_t *p_input = NULL;
1764 if( s->p_parent && s->p_parent->p_parent &&
1765 vlc_internals( s->p_parent->p_parent )->i_object_type == VLC_OBJECT_INPUT )
1766 p_input = (input_thread_t *)s->p_parent->p_parent;
1768 if( !p_sys->i_list )
1770 p_block = p_access->pf_block( p_access );
1771 if( p_access->b_die )
1772 vlc_object_kill( s );
1773 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1774 if( p_input && p_block && libvlc_stats (p_access) )
1776 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1777 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1778 p_block->i_buffer, &i_total );
1779 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1780 (float)i_total, NULL );
1781 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1782 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1787 p_block = p_sys->p_list_access->pf_block( p_sys->p_list_access );
1788 if( p_access->b_die )
1789 vlc_object_kill( s );
1790 b_eof = p_sys->p_list_access->info.b_eof;
1791 if( pb_eof ) *pb_eof = b_eof;
1793 /* If we reached an EOF then switch to the next stream in the list */
1794 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1796 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1797 access_t *p_list_access;
1799 msg_Dbg( s, "opening input `%s'", psz_name );
1801 p_list_access = access_New( s, s->p_input, p_access->psz_access, "", psz_name );
1803 if( !p_list_access ) return 0;
1805 if( p_sys->p_list_access != p_access )
1806 access_Delete( p_sys->p_list_access );
1808 p_sys->p_list_access = p_list_access;
1810 /* We have to read some data */
1811 return AReadBlock( s, pb_eof );
1817 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1818 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1819 p_block->i_buffer, &i_total );
1820 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1821 (float)i_total, NULL );
1822 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
1824 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1830 static int ASeek( stream_t *s, uint64_t i_pos )
1832 stream_sys_t *p_sys = s->p_sys;
1833 access_t *p_access = p_sys->p_access;
1835 /* Check which stream we need to access */
1841 access_t *p_list_access = 0;
1843 for( i = 0; i < p_sys->i_list - 1; i++ )
1845 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1846 i_size += p_sys->list[i]->i_size;
1848 psz_name = p_sys->list[i]->psz_path;
1850 if( i != p_sys->i_list_index )
1851 msg_Dbg( s, "opening input `%s'", psz_name );
1853 if( i != p_sys->i_list_index && i != 0 )
1856 access_New( s, s->p_input, p_access->psz_access, "", psz_name );
1858 else if( i != p_sys->i_list_index )
1860 p_list_access = p_access;
1865 if( p_sys->p_list_access != p_access )
1866 access_Delete( p_sys->p_list_access );
1868 p_sys->p_list_access = p_list_access;
1871 p_sys->i_list_index = i;
1872 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1876 return p_access->pf_seek( p_access, i_pos );
1881 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
1882 * "p_read" is NULL then data are skipped instead of read. The return
1883 * value is the real numbers of bytes read/skip. If this value is less
1884 * than i_read that means that it's the end of the stream.
1886 int stream_Read( stream_t *s, void *p_read, int i_read )
1888 return s->pf_read( s, p_read, i_read );
1892 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
1893 * \return The real numbers of valid bytes, if it's less
1894 * or equal to 0, *pp_peek is invalid.
1895 * \note pp_peek is a pointer to internal buffer and it will be invalid as
1896 * soons as other stream_* functions are called.
1897 * \note Due to input limitation, it could be less than i_peek without meaning
1898 * the end of the stream (but only when you have i_peek >=
1899 * p_input->i_bufsize)
1901 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
1903 return s->pf_peek( s, pp_peek, i_peek );
1907 * Use to control the "stream_t *". Look at #stream_query_e for
1908 * possible "i_query" value and format arguments. Return VLC_SUCCESS
1909 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
1911 int stream_vaControl( stream_t *s, int i_query, va_list args )
1913 return s->pf_control( s, i_query, args );
1919 void stream_Delete( stream_t *s )
1924 int stream_Control( stream_t *s, int i_query, ... )
1930 return VLC_EGENERIC;
1932 va_start( args, i_query );
1933 i_result = s->pf_control( s, i_query, args );
1939 * Read "i_size" bytes and store them in a block_t.
1940 * It always read i_size bytes unless you are at the end of the stream
1941 * where it return what is available.
1943 block_t *stream_Block( stream_t *s, int i_size )
1945 if( i_size <= 0 ) return NULL;
1947 /* emulate block read */
1948 block_t *p_bk = block_New( s, i_size );
1951 int i_read = stream_Read( s, p_bk->p_buffer, i_size );
1954 p_bk->i_buffer = i_read;
1957 block_Release( p_bk );