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 int64_t i_pos; /* Current reading offset */
132 /* Method 1: pf_block */
135 int64_t i_start; /* Offset of block for p_first */
136 int64_t i_offset; /* Offset for data in p_current */
137 block_t *p_current; /* Current block */
139 int i_size; /* Total amount of data in the list */
145 /* Method 2: for pf_read */
148 int i_offset; /* Buffer offset in the current track */
149 int i_tk; /* Current track */
150 stream_track_t tk[STREAM_CACHE_TRACK];
156 int i_used; /* Used since last read */
161 /* Peek temporary buffer */
165 /* Stat for both method */
168 bool b_fastseek; /* From access */
170 /* Stat about reading data */
171 int64_t i_read_count;
175 /* Stat about seek */
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, int64_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, int64_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, int64_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 vlc_object_detach( s );
465 stream_CommonDelete( s );
469 /****************************************************************************
471 ****************************************************************************/
472 static void AStreamDestroy( stream_t *s )
474 stream_sys_t *p_sys = s->p_sys;
476 vlc_object_detach( s );
478 if( p_sys->method == STREAM_METHOD_BLOCK )
479 block_ChainRelease( p_sys->block.p_first );
481 free( p_sys->stream.p_buffer );
483 free( p_sys->p_peek );
485 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
486 access_Delete( p_sys->p_list_access );
488 while( p_sys->i_list-- )
490 free( p_sys->list[p_sys->i_list]->psz_path );
491 free( p_sys->list[p_sys->i_list] );
497 stream_CommonDelete( s );
500 static void UStreamDestroy( stream_t *s )
502 access_t *p_access = (access_t *)s->p_parent;
504 access_Delete( p_access );
507 /****************************************************************************
508 * AStreamControlReset:
509 ****************************************************************************/
510 static void AStreamControlReset( stream_t *s )
512 stream_sys_t *p_sys = s->p_sys;
514 p_sys->i_pos = p_sys->p_access->info.i_pos;
516 if( p_sys->method == STREAM_METHOD_BLOCK )
518 block_ChainRelease( p_sys->block.p_first );
520 /* Init all fields of p_sys->block */
521 p_sys->block.i_start = p_sys->i_pos;
522 p_sys->block.i_offset = 0;
523 p_sys->block.p_current = NULL;
524 p_sys->block.i_size = 0;
525 p_sys->block.p_first = NULL;
526 p_sys->block.pp_last = &p_sys->block.p_first;
528 /* Do the prebuffering */
529 AStreamPrebufferBlock( s );
535 assert( p_sys->method == STREAM_METHOD_STREAM );
537 /* Setup our tracks */
538 p_sys->stream.i_offset = 0;
539 p_sys->stream.i_tk = 0;
540 p_sys->stream.i_used = 0;
542 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
544 p_sys->stream.tk[i].i_date = 0;
545 p_sys->stream.tk[i].i_start = p_sys->i_pos;
546 p_sys->stream.tk[i].i_end = p_sys->i_pos;
549 /* Do the prebuffering */
550 AStreamPrebufferStream( s );
554 /****************************************************************************
555 * AStreamControlUpdate:
556 ****************************************************************************/
557 static void AStreamControlUpdate( stream_t *s )
559 stream_sys_t *p_sys = s->p_sys;
561 p_sys->i_pos = p_sys->p_access->info.i_pos;
566 for( i = 0; i < p_sys->i_list_index; i++ )
568 p_sys->i_pos += p_sys->list[i]->i_size;
573 /****************************************************************************
575 ****************************************************************************/
576 static int AStreamControl( stream_t *s, int i_query, va_list args )
578 stream_sys_t *p_sys = s->p_sys;
579 access_t *p_access = p_sys->p_access;
582 int64_t *pi_64, i_64;
587 case STREAM_GET_SIZE:
588 pi_64 = (int64_t*)va_arg( args, int64_t * );
589 if( s->p_sys->i_list )
593 for( i = 0; i < s->p_sys->i_list; i++ )
594 *pi_64 += s->p_sys->list[i]->i_size;
597 *pi_64 = p_access->info.i_size;
600 case STREAM_CAN_SEEK:
601 p_bool = (bool*)va_arg( args, bool * );
602 access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
605 case STREAM_CAN_FASTSEEK:
606 p_bool = (bool*)va_arg( args, bool * );
607 access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
610 case STREAM_GET_POSITION:
611 pi_64 = (int64_t*)va_arg( args, int64_t * );
612 *pi_64 = p_sys->i_pos;
615 case STREAM_SET_POSITION:
616 i_64 = (int64_t)va_arg( args, int64_t );
617 switch( p_sys->method )
619 case STREAM_METHOD_BLOCK:
620 return AStreamSeekBlock( s, i_64 );
621 case STREAM_METHOD_STREAM:
622 return AStreamSeekStream( s, i_64 );
628 case STREAM_CONTROL_ACCESS:
630 i_int = (int) va_arg( args, int );
631 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
632 i_int != ACCESS_SET_PRIVATE_ID_CA &&
633 i_int != ACCESS_GET_PRIVATE_ID_STATE &&
634 i_int != ACCESS_SET_TITLE &&
635 i_int != ACCESS_SET_SEEKPOINT )
637 msg_Err( s, "Hey, what are you thinking ?"
638 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
641 int i_ret = access_vaControl( p_access, i_int, args );
642 if( i_int == ACCESS_SET_TITLE || i_int == ACCESS_SET_SEEKPOINT )
643 AStreamControlReset( s );
647 case STREAM_UPDATE_SIZE:
648 AStreamControlUpdate( s );
651 case STREAM_GET_CONTENT_TYPE:
652 return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
653 va_arg( args, char ** ) );
654 case STREAM_SET_RECORD_STATE:
656 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
662 /****************************************************************************
664 ****************************************************************************/
665 static void AStreamPrebufferBlock( stream_t *s )
667 stream_sys_t *p_sys = s->p_sys;
672 msg_Dbg( s, "pre buffering" );
676 const int64_t i_date = mdate();
680 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE )
685 p_sys->stat.i_bytes = p_sys->block.i_size;
686 p_sys->stat.i_read_time = i_date - i_start;
687 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
688 (p_sys->stat.i_read_time + 1);
690 msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
691 "%"PRId64" kbytes/s",
693 p_sys->stat.i_read_time / INT64_C(1000000),
699 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
708 /* Append the block */
709 p_sys->block.i_size += b->i_buffer;
710 *p_sys->block.pp_last = b;
711 p_sys->block.pp_last = &b->p_next;
713 p_sys->stat.i_read_count++;
720 msg_Dbg( s, "received first data after %d ms",
721 (int)((i_first-i_start)/1000) );
725 p_sys->block.p_current = p_sys->block.p_first;
728 static int AStreamRefillBlock( stream_t *s );
730 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
732 stream_sys_t *p_sys = s->p_sys;
734 uint8_t *p_data = p_read;
735 unsigned int i_data = 0;
738 if( p_sys->block.p_current == NULL )
743 /* seek within this stream if possible, else use plain old read and discard */
744 stream_sys_t *p_sys = s->p_sys;
745 access_t *p_access = p_sys->p_access;
747 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
749 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
752 while( i_data < i_read )
755 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
756 unsigned int i_copy = __MIN( (unsigned int)__MAX(i_current,0), i_read - i_data);
762 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
768 p_sys->block.i_offset += i_copy;
769 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
771 /* Current block is now empty, switch to next */
772 if( p_sys->block.p_current )
774 p_sys->block.i_offset = 0;
775 p_sys->block.p_current = p_sys->block.p_current->p_next;
777 /*Get a new block if needed */
778 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
785 p_sys->i_pos += i_data;
789 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
791 stream_sys_t *p_sys = s->p_sys;
793 unsigned int i_data = 0;
795 unsigned int i_offset;
797 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
799 /* We can directly give a pointer over our buffer */
800 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
802 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
806 /* We need to create a local copy */
807 if( p_sys->i_peek < i_read )
809 p_sys->p_peek = realloc_or_free( p_sys->p_peek, i_read );
815 p_sys->i_peek = i_read;
818 /* Fill enough data */
819 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
822 block_t **pp_last = p_sys->block.pp_last;
824 if( AStreamRefillBlock( s ) ) break;
826 /* Our buffer are probably filled enough, don't try anymore */
827 if( pp_last == p_sys->block.pp_last ) break;
830 /* Copy what we have */
831 b = p_sys->block.p_current;
832 i_offset = p_sys->block.i_offset;
833 p_data = p_sys->p_peek;
835 while( b && i_data < i_read )
837 unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
838 int i_copy = __MIN( i_current, i_read - i_data );
840 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
845 if( i_offset >= b->i_buffer )
852 *pp_peek = p_sys->p_peek;
856 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
858 stream_sys_t *p_sys = s->p_sys;
859 access_t *p_access = p_sys->p_access;
860 int64_t i_offset = i_pos - p_sys->block.i_start;
863 /* We already have thoses data, just update p_current/i_offset */
864 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
866 block_t *b = p_sys->block.p_first;
869 while( i_current + b->i_buffer < i_offset )
871 i_current += b->i_buffer;
875 p_sys->block.p_current = b;
876 p_sys->block.i_offset = i_offset - i_current;
878 p_sys->i_pos = i_pos;
883 /* We may need to seek or to read data */
887 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
891 msg_Err( s, "backward seeking impossible (access not seekable)" );
899 bool b_aseek, b_aseekfast;
901 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
902 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
907 msg_Warn( s, "%"PRId64" bytes need to be skipped "
908 "(access non seekable)",
909 i_offset - p_sys->block.i_size );
913 int64_t i_skip = i_offset - p_sys->block.i_size;
915 /* Avg bytes per packets */
916 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
917 /* TODO compute a seek cost instead of fixed threshold */
918 int i_th = b_aseekfast ? 1 : 5;
920 if( i_skip <= i_th * i_avg &&
921 i_skip < STREAM_CACHE_SIZE )
926 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
927 b_seek, i_th*i_avg, i_skip );
933 int64_t i_start, i_end;
934 /* Do the access seek */
936 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
940 block_ChainRelease( p_sys->block.p_first );
943 p_sys->block.i_start = p_sys->i_pos = i_pos;
944 p_sys->block.i_offset = 0;
945 p_sys->block.p_current = NULL;
946 p_sys->block.i_size = 0;
947 p_sys->block.p_first = NULL;
948 p_sys->block.pp_last = &p_sys->block.p_first;
951 if( AStreamRefillBlock( s ) )
955 p_sys->stat.i_seek_time += i_end - i_start;
956 p_sys->stat.i_seek_count++;
963 /* Read and skip enough data */
964 if( AStreamRefillBlock( s ) )
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;
975 while( p_sys->block.i_start + p_sys->block.i_size < i_pos );
977 p_sys->block.i_offset += i_pos - p_sys->i_pos;
978 p_sys->i_pos = i_pos;
986 static int AStreamRefillBlock( stream_t *s )
988 stream_sys_t *p_sys = s->p_sys;
992 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
993 p_sys->block.p_first != p_sys->block.p_current )
995 block_t *b = p_sys->block.p_first;
997 p_sys->block.i_start += b->i_buffer;
998 p_sys->block.i_size -= b->i_buffer;
999 p_sys->block.p_first = b->p_next;
1003 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1004 p_sys->block.p_current == p_sys->block.p_first &&
1005 p_sys->block.p_current->p_next ) /* At least 2 packets */
1007 /* Enough data, don't read more */
1011 /* Now read a new block */
1012 const int64_t i_start = mdate();
1018 return VLC_EGENERIC;
1021 if( ( b = AReadBlock( s, &b_eof ) ) )
1024 return VLC_EGENERIC;
1027 p_sys->stat.i_read_time += mdate() - i_start;
1030 /* Append the block */
1031 p_sys->block.i_size += b->i_buffer;
1032 *p_sys->block.pp_last = b;
1033 p_sys->block.pp_last = &b->p_next;
1036 if( p_sys->block.p_current == NULL )
1037 p_sys->block.p_current = b;
1040 p_sys->stat.i_bytes += b->i_buffer;
1041 p_sys->stat.i_read_count++;
1049 /****************************************************************************
1051 ****************************************************************************/
1052 static int AStreamRefillStream( stream_t *s );
1053 static int AStreamReadNoSeekStream( stream_t *s, void *p_read, unsigned int i_read );
1055 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read )
1057 stream_sys_t *p_sys = s->p_sys;
1061 const int64_t i_pos_wanted = p_sys->i_pos + i_read;
1063 if( AStreamSeekStream( s, i_pos_wanted ) )
1065 if( p_sys->i_pos != i_pos_wanted )
1070 return AStreamReadNoSeekStream( s, p_read, i_read );
1073 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1075 stream_sys_t *p_sys = s->p_sys;
1076 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1079 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1082 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1083 "start=%"PRId64" offset=%d end=%"PRId64,
1084 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1085 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1088 /* Avoid problem, but that should *never* happen */
1089 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1090 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1092 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1094 if( p_sys->stream.i_used <= 1 )
1096 /* Be sure we will read something */
1097 p_sys->stream.i_used += i_read -
1098 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1100 if( AStreamRefillStream( s ) ) break;
1103 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1104 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, int64_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 int64_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;
1239 int64_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 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1302 STREAM_CACHE_TRACK_SIZE;
1303 unsigned int i_current =
1304 __MAX(0,__MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1305 STREAM_CACHE_TRACK_SIZE - i_off ));
1306 int i_copy = __MIN( i_current, i_read - i_data );
1308 if( i_copy <= 0 ) break; /* EOF */
1311 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1314 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1318 p_sys->stream.i_offset += i_copy;
1320 /* Update pos now */
1321 p_sys->i_pos += i_copy;
1324 p_sys->stream.i_used += i_copy;
1326 if( tk->i_end - tk->i_start - p_sys->stream.i_offset <= i_read -i_data )
1328 const int i_read_requested = __MAX( __MIN( i_read - i_data,
1329 STREAM_READ_ATONCE * 10 ),
1330 STREAM_READ_ATONCE / 2 );
1332 if( p_sys->stream.i_used < i_read_requested )
1333 p_sys->stream.i_used = i_read_requested;
1335 if( AStreamRefillStream( s ) )
1338 if( tk->i_start >= tk->i_end ) break;
1347 static int AStreamRefillStream( stream_t *s )
1349 stream_sys_t *p_sys = s->p_sys;
1350 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1352 /* We read but won't increase i_start after initial start + offset */
1354 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1355 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1356 bool b_read = false;
1357 int64_t i_start, i_stop;
1359 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1362 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1363 p_sys->stream.i_used, i_toread );
1367 while( i_toread > 0 )
1369 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1373 return VLC_EGENERIC;
1375 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1376 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1378 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1383 else if( i_read == 0 )
1386 return VLC_EGENERIC;
1392 tk->i_end += i_read;
1394 /* Windows of STREAM_CACHE_TRACK_SIZE */
1395 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1397 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1399 tk->i_start += i_invalid;
1400 p_sys->stream.i_offset -= i_invalid;
1404 p_sys->stream.i_used -= i_read;
1406 p_sys->stat.i_bytes += i_read;
1407 p_sys->stat.i_read_count++;
1411 p_sys->stat.i_read_time += i_stop - i_start;
1416 static void AStreamPrebufferStream( stream_t *s )
1418 stream_sys_t *p_sys = s->p_sys;
1420 int64_t i_first = 0;
1423 msg_Dbg( s, "pre buffering" );
1427 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1429 int64_t i_date = mdate();
1431 int i_buffered = tk->i_end - tk->i_start;
1433 if( s->b_die || i_buffered >= STREAM_CACHE_PREBUFFER_SIZE )
1438 p_sys->stat.i_bytes = i_buffered;
1439 p_sys->stat.i_read_time = i_date - i_start;
1440 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1441 (p_sys->stat.i_read_time+1);
1443 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1444 "%"PRId64" kbytes/s",
1445 p_sys->stat.i_bytes,
1446 p_sys->stat.i_read_time / INT64_C(1000000),
1447 i_byterate / 1024 );
1452 i_read = STREAM_CACHE_TRACK_SIZE - i_buffered;
1453 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1454 i_read = AReadStream( s, &tk->p_buffer[i_buffered], i_read );
1457 else if( i_read == 0 )
1463 msg_Dbg( s, "received first data after %d ms",
1464 (int)((i_first-i_start)/1000) );
1467 tk->i_end += i_read;
1469 p_sys->stat.i_read_count++;
1473 /****************************************************************************
1475 ****************************************************************************/
1477 * Read from the stream untill first newline.
1478 * \param s Stream handle to read from
1479 * \return A pointer to the allocated output string. You need to free this when you are done.
1481 #define STREAM_PROBE_LINE 2048
1482 #define STREAM_LINE_MAX (2048*100)
1483 char *stream_ReadLine( stream_t *s )
1485 char *p_line = NULL;
1486 int i_line = 0, i_read = 0;
1488 while( i_read < STREAM_LINE_MAX )
1491 const uint8_t *p_data;
1495 /* Probe new data */
1496 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1497 if( i_data <= 0 ) break; /* No more data */
1500 i_pos = stream_Tell( s );
1501 if( i_pos == 0 && i_data >= 3 )
1504 const char *psz_encoding = NULL;
1506 if( !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
1508 psz_encoding = "UTF-8";
1511 else if( !memcmp( p_data, "\xFF\xFE", 2 ) )
1513 psz_encoding = "UTF-16LE";
1514 s->p_text->b_little_endian = true;
1515 s->p_text->i_char_width = 2;
1518 else if( !memcmp( p_data, "\xFE\xFF", 2 ) )
1520 psz_encoding = "UTF-16BE";
1521 s->p_text->i_char_width = 2;
1525 /* Seek past the BOM */
1528 stream_Seek( s, i_bom_size );
1529 p_data += i_bom_size;
1530 i_data -= i_bom_size;
1533 /* Open the converter if we need it */
1534 if( psz_encoding != NULL )
1536 msg_Dbg( s, "%s BOM detected", psz_encoding );
1537 if( s->p_text->i_char_width > 1 )
1539 s->p_text->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1540 if( s->p_text->conv == (vlc_iconv_t)-1 )
1542 msg_Err( s, "iconv_open failed" );
1546 /* FIXME that's UGLY */
1547 input_thread_t *p_input = s->p_input;
1548 if( p_input != NULL)
1550 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1551 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1556 if( i_data % s->p_text->i_char_width )
1558 /* keep i_char_width boundary */
1559 i_data = i_data - ( i_data % s->p_text->i_char_width );
1560 msg_Warn( s, "the read is not i_char_width compatible");
1566 /* Check if there is an EOL */
1567 if( s->p_text->i_char_width == 1 )
1569 /* UTF-8: 0A <LF> */
1570 psz_eol = memchr( p_data, '\n', i_data );
1574 const uint8_t *p = p_data;
1575 const uint8_t *p_last = p + i_data - s->p_text->i_char_width;
1577 if( s->p_text->i_char_width == 2 )
1579 if( s->p_text->b_little_endian == true)
1581 /* UTF-16LE: 0A 00 <LF> */
1582 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1587 /* UTF-16BE: 00 0A <LF> */
1588 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1599 psz_eol = (char *)p + ( s->p_text->i_char_width - 1 );
1605 i_data = (psz_eol - (char *)p_data) + 1;
1606 p_line = realloc_or_free( p_line,
1607 i_line + i_data + s->p_text->i_char_width ); /* add \0 */
1610 i_data = stream_Read( s, &p_line[i_line], i_data );
1611 if( i_data <= 0 ) break; /* Hmmm */
1612 i_line += i_data - s->p_text->i_char_width; /* skip \n */;
1615 /* We have our line */
1619 /* Read data (+1 for easy \0 append) */
1620 p_line = realloc_or_free( p_line,
1621 i_line + STREAM_PROBE_LINE + s->p_text->i_char_width );
1624 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1625 if( i_data <= 0 ) break; /* Hmmm */
1633 for( j = 0; j < s->p_text->i_char_width; j++ )
1635 p_line[i_line + j] = '\0';
1637 i_line += s->p_text->i_char_width; /* the added \0 */
1638 if( s->p_text->i_char_width > 1 )
1640 size_t i_in = 0, i_out = 0;
1641 const char * p_in = NULL;
1642 char * p_out = NULL;
1643 char * psz_new_line = NULL;
1646 psz_new_line = malloc( i_line );
1647 if( psz_new_line == NULL )
1649 i_in = i_out = (size_t)i_line;
1651 p_out = psz_new_line;
1653 if( vlc_iconv( s->p_text->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1655 msg_Err( s, "iconv failed" );
1656 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1659 p_line = psz_new_line;
1660 i_line = (size_t)i_line - i_out; /* does not include \0 */
1663 /* Remove trailing LF/CR */
1664 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1665 p_line[i_line-2] == '\n') ) i_line--;
1667 /* Make sure the \0 is there */
1668 p_line[i_line-1] = '\0';
1674 /* We failed to read any data, probably EOF */
1678 if( s->p_text->conv != (vlc_iconv_t)(-1) )
1679 vlc_iconv_close( s->p_text->conv );
1680 s->p_text->conv = (vlc_iconv_t)(-1);
1684 /****************************************************************************
1685 * Access reading/seeking wrappers to handle concatenated streams.
1686 ****************************************************************************/
1687 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read )
1689 stream_sys_t *p_sys = s->p_sys;
1690 access_t *p_access = p_sys->p_access;
1691 input_thread_t *p_input = NULL;
1692 int i_read_orig = i_read;
1695 if( s->p_parent && s->p_parent->p_parent &&
1696 vlc_internals( s->p_parent->p_parent )->i_object_type == VLC_OBJECT_INPUT )
1697 p_input = (input_thread_t *)s->p_parent->p_parent;
1699 if( !p_sys->i_list )
1701 i_read = p_access->pf_read( p_access, p_read, i_read );
1702 if( p_access->b_die )
1703 vlc_object_kill( s );
1706 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1707 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
1709 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1710 (float)i_total, NULL );
1711 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1712 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1717 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1719 if( p_access->b_die )
1720 vlc_object_kill( s );
1722 /* If we reached an EOF then switch to the next stream in the list */
1723 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1725 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1726 access_t *p_list_access;
1728 msg_Dbg( s, "opening input `%s'", psz_name );
1730 p_list_access = access_New( s, s->p_input, p_access->psz_access, "", psz_name );
1732 if( !p_list_access ) return 0;
1734 if( p_sys->p_list_access != p_access )
1735 access_Delete( p_sys->p_list_access );
1737 p_sys->p_list_access = p_list_access;
1739 /* We have to read some data */
1740 return AReadStream( s, p_read, i_read_orig );
1743 /* Update read bytes in input */
1746 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1747 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
1748 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1749 (float)i_total, NULL );
1750 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1751 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1756 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
1758 stream_sys_t *p_sys = s->p_sys;
1759 access_t *p_access = p_sys->p_access;
1760 input_thread_t *p_input = NULL;
1765 if( s->p_parent && s->p_parent->p_parent &&
1766 vlc_internals( s->p_parent->p_parent )->i_object_type == VLC_OBJECT_INPUT )
1767 p_input = (input_thread_t *)s->p_parent->p_parent;
1769 if( !p_sys->i_list )
1771 p_block = p_access->pf_block( p_access );
1772 if( p_access->b_die )
1773 vlc_object_kill( s );
1774 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1775 if( p_input && p_block && libvlc_stats (p_access) )
1777 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1778 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1779 p_block->i_buffer, &i_total );
1780 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1781 (float)i_total, NULL );
1782 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1783 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1788 p_block = p_sys->p_list_access->pf_block( p_sys->p_list_access );
1789 if( p_access->b_die )
1790 vlc_object_kill( s );
1791 b_eof = p_sys->p_list_access->info.b_eof;
1792 if( pb_eof ) *pb_eof = b_eof;
1794 /* If we reached an EOF then switch to the next stream in the list */
1795 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1797 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1798 access_t *p_list_access;
1800 msg_Dbg( s, "opening input `%s'", psz_name );
1802 p_list_access = access_New( s, s->p_input, p_access->psz_access, "", psz_name );
1804 if( !p_list_access ) return 0;
1806 if( p_sys->p_list_access != p_access )
1807 access_Delete( p_sys->p_list_access );
1809 p_sys->p_list_access = p_list_access;
1811 /* We have to read some data */
1812 return AReadBlock( s, pb_eof );
1818 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1819 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1820 p_block->i_buffer, &i_total );
1821 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1822 (float)i_total, NULL );
1823 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
1825 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1831 static int ASeek( stream_t *s, int64_t i_pos )
1833 stream_sys_t *p_sys = s->p_sys;
1834 access_t *p_access = p_sys->p_access;
1836 /* Check which stream we need to access */
1842 access_t *p_list_access = 0;
1844 for( i = 0; i < p_sys->i_list - 1; i++ )
1846 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1847 i_size += p_sys->list[i]->i_size;
1849 psz_name = p_sys->list[i]->psz_path;
1851 if( i != p_sys->i_list_index )
1852 msg_Dbg( s, "opening input `%s'", psz_name );
1854 if( i != p_sys->i_list_index && i != 0 )
1857 access_New( s, s->p_input, p_access->psz_access, "", psz_name );
1859 else if( i != p_sys->i_list_index )
1861 p_list_access = p_access;
1866 if( p_sys->p_list_access != p_access )
1867 access_Delete( p_sys->p_list_access );
1869 p_sys->p_list_access = p_list_access;
1872 p_sys->i_list_index = i;
1873 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1877 return p_access->pf_seek( p_access, i_pos );
1882 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
1883 * "p_read" is NULL then data are skipped instead of read. The return
1884 * value is the real numbers of bytes read/skip. If this value is less
1885 * than i_read that means that it's the end of the stream.
1887 int stream_Read( stream_t *s, void *p_read, int i_read )
1889 return s->pf_read( s, p_read, i_read );
1893 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
1894 * \return The real numbers of valid bytes, if it's less
1895 * or equal to 0, *pp_peek is invalid.
1896 * \note pp_peek is a pointer to internal buffer and it will be invalid as
1897 * soons as other stream_* functions are called.
1898 * \note Due to input limitation, it could be less than i_peek without meaning
1899 * the end of the stream (but only when you have i_peek >=
1900 * p_input->i_bufsize)
1902 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
1904 return s->pf_peek( s, pp_peek, i_peek );
1908 * Use to control the "stream_t *". Look at #stream_query_e for
1909 * possible "i_query" value and format arguments. Return VLC_SUCCESS
1910 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
1912 int stream_vaControl( stream_t *s, int i_query, va_list args )
1914 return s->pf_control( s, i_query, args );
1920 void stream_Delete( stream_t *s )
1925 int stream_Control( stream_t *s, int i_query, ... )
1931 return VLC_EGENERIC;
1933 va_start( args, i_query );
1934 i_result = s->pf_control( s, i_query, args );
1940 * Read "i_size" bytes and store them in a block_t.
1941 * It always read i_size bytes unless you are at the end of the stream
1942 * where it return what is available.
1944 block_t *stream_Block( stream_t *s, int i_size )
1946 if( i_size <= 0 ) return NULL;
1948 /* emulate block read */
1949 block_t *p_bk = block_New( s, i_size );
1952 int i_read = stream_Read( s, p_bk->p_buffer, i_size );
1955 p_bk->i_buffer = i_read;
1958 block_Release( p_bk );