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;
188 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read );
189 static int AStreamPeekBlock( stream_t *s, const uint8_t **p_peek, unsigned int i_read );
190 static int AStreamSeekBlock( stream_t *s, int64_t i_pos );
191 static void AStreamPrebufferBlock( stream_t *s );
192 static block_t *AReadBlock( stream_t *s, bool *pb_eof );
195 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read );
196 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read );
197 static int AStreamSeekStream( stream_t *s, int64_t i_pos );
198 static void AStreamPrebufferStream( stream_t *s );
199 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read );
202 static int AStreamControl( stream_t *s, int i_query, va_list );
203 static void AStreamDestroy( stream_t *s );
204 static void UStreamDestroy( stream_t *s );
205 static int ASeek( stream_t *s, int64_t i_pos );
207 /****************************************************************************
208 * stream_CommonNew: create an empty stream structure
209 ****************************************************************************/
210 stream_t *stream_CommonNew( vlc_object_t *p_obj )
212 stream_t *s = (stream_t *)vlc_custom_create( p_obj, sizeof(*s),
213 VLC_OBJECT_GENERIC, "stream" );
218 s->p_text = malloc( sizeof(*s->p_text) );
221 vlc_object_release( s );
225 /* UTF16 and UTF32 text file conversion */
226 s->p_text->conv = (vlc_iconv_t)(-1);
227 s->p_text->i_char_width = 1;
228 s->p_text->b_little_endian = false;
232 void stream_CommonDelete( stream_t *s )
236 if( s->p_text->conv != (vlc_iconv_t)(-1) )
237 vlc_iconv_close( s->p_text->conv );
240 vlc_object_release( s );
243 /****************************************************************************
244 * stream_UrlNew: create a stream from a access
245 ****************************************************************************/
246 stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
248 const char *psz_access, *psz_demux;
256 char psz_dup[strlen( psz_url ) + 1];
257 strcpy( psz_dup, psz_url );
258 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
260 /* Now try a real access */
261 p_access = access_New( p_parent, psz_access, psz_demux, psz_path );
263 if( p_access == NULL )
265 msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
269 if( !( p_res = stream_AccessNew( p_access, NULL ) ) )
271 access_Delete( p_access );
275 p_res->pf_destroy = UStreamDestroy;
279 stream_t *stream_AccessNew( access_t *p_access, char **ppsz_list )
281 stream_t *s = stream_CommonNew( VLC_OBJECT(p_access) );
287 s->p_sys = p_sys = malloc( sizeof( *p_sys ) );
290 stream_CommonDelete( s );
294 /* Attach it now, needed for b_die */
295 vlc_object_attach( s, p_access );
297 s->pf_read = NULL; /* Set up later */
299 s->pf_control = AStreamControl;
300 s->pf_destroy = AStreamDestroy;
303 p_sys->p_access = p_access;
304 if( p_access->pf_block )
305 p_sys->method = STREAM_METHOD_BLOCK;
307 p_sys->method = STREAM_METHOD_STREAM;
309 p_sys->i_pos = p_access->info.i_pos;
312 access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
313 p_sys->stat.i_bytes = 0;
314 p_sys->stat.i_read_time = 0;
315 p_sys->stat.i_read_count = 0;
316 p_sys->stat.i_seek_count = 0;
317 p_sys->stat.i_seek_time = 0;
319 TAB_INIT( p_sys->i_list, p_sys->list );
320 p_sys->i_list_index = 0;
321 p_sys->p_list_access = NULL;
323 /* Get the additional list of inputs if any (for concatenation) */
324 if( ppsz_list && ppsz_list[0] )
326 access_entry_t *p_entry = malloc( sizeof(*p_entry) );
330 p_entry->i_size = p_access->info.i_size;
331 p_entry->psz_path = strdup( p_access->psz_path );
332 if( !p_entry->psz_path )
337 p_sys->p_list_access = p_access;
338 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
339 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
340 p_entry->psz_path, p_access->info.i_size );
342 for( int i = 0; ppsz_list[i] != NULL; i++ )
344 char *psz_name = strdup( ppsz_list[i] );
349 access_t *p_tmp = access_New( p_access,
350 p_access->psz_access, "", psz_name );
354 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
355 psz_name, p_tmp->info.i_size );
357 p_entry = malloc( sizeof(*p_entry) );
360 p_entry->i_size = p_tmp->info.i_size;
361 p_entry->psz_path = psz_name;
362 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
364 access_Delete( p_tmp );
370 p_sys->p_peek = NULL;
372 if( p_sys->method == STREAM_METHOD_BLOCK )
374 msg_Dbg( s, "Using AStream*Block" );
375 s->pf_read = AStreamReadBlock;
376 s->pf_peek = AStreamPeekBlock;
378 /* Init all fields of p_sys->block */
379 p_sys->block.i_start = p_sys->i_pos;
380 p_sys->block.i_offset = 0;
381 p_sys->block.p_current = NULL;
382 p_sys->block.i_size = 0;
383 p_sys->block.p_first = NULL;
384 p_sys->block.pp_last = &p_sys->block.p_first;
386 /* Do the prebuffering */
387 AStreamPrebufferBlock( s );
389 if( p_sys->block.i_size <= 0 )
391 msg_Err( s, "cannot pre fill buffer" );
399 assert( p_sys->method == STREAM_METHOD_STREAM );
401 msg_Dbg( s, "Using AStream*Stream" );
403 s->pf_read = AStreamReadStream;
404 s->pf_peek = AStreamPeekStream;
406 /* Allocate/Setup our tracks */
407 p_sys->stream.i_offset = 0;
408 p_sys->stream.i_tk = 0;
409 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
410 if( p_sys->stream.p_buffer == NULL )
412 p_sys->stream.i_used = 0;
413 access_Control( p_access, ACCESS_GET_MTU,
414 &p_sys->stream.i_read_size );
415 if( p_sys->stream.i_read_size <= 0 )
416 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
417 else if( p_sys->stream.i_read_size <= 256 )
418 p_sys->stream.i_read_size = 256;
420 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
422 p_sys->stream.tk[i].i_date = 0;
423 p_sys->stream.tk[i].i_start = p_sys->i_pos;
424 p_sys->stream.tk[i].i_end = p_sys->i_pos;
425 p_sys->stream.tk[i].p_buffer=
426 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
429 /* Do the prebuffering */
430 AStreamPrebufferStream( s );
432 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
434 msg_Err( s, "cannot pre fill buffer" );
442 if( p_sys->method == STREAM_METHOD_BLOCK )
448 free( p_sys->stream.p_buffer );
450 while( p_sys->i_list > 0 )
451 free( p_sys->list[--(p_sys->i_list)] );
454 vlc_object_detach( s );
455 stream_CommonDelete( s );
459 /****************************************************************************
461 ****************************************************************************/
462 static void AStreamDestroy( stream_t *s )
464 stream_sys_t *p_sys = s->p_sys;
466 vlc_object_detach( s );
468 if( p_sys->method == STREAM_METHOD_BLOCK )
469 block_ChainRelease( p_sys->block.p_first );
471 free( p_sys->stream.p_buffer );
473 free( p_sys->p_peek );
475 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
476 access_Delete( p_sys->p_list_access );
478 while( p_sys->i_list-- )
480 free( p_sys->list[p_sys->i_list]->psz_path );
481 free( p_sys->list[p_sys->i_list] );
487 stream_CommonDelete( s );
490 static void UStreamDestroy( stream_t *s )
492 access_t *p_access = (access_t *)s->p_parent;
494 access_Delete( p_access );
497 /****************************************************************************
498 * AStreamControlReset:
499 ****************************************************************************/
500 static void AStreamControlReset( stream_t *s )
502 stream_sys_t *p_sys = s->p_sys;
504 p_sys->i_pos = p_sys->p_access->info.i_pos;
506 if( p_sys->method == STREAM_METHOD_BLOCK )
508 block_ChainRelease( p_sys->block.p_first );
510 /* Init all fields of p_sys->block */
511 p_sys->block.i_start = p_sys->i_pos;
512 p_sys->block.i_offset = 0;
513 p_sys->block.p_current = NULL;
514 p_sys->block.i_size = 0;
515 p_sys->block.p_first = NULL;
516 p_sys->block.pp_last = &p_sys->block.p_first;
518 /* Do the prebuffering */
519 AStreamPrebufferBlock( s );
525 assert( p_sys->method == STREAM_METHOD_STREAM );
527 /* Setup our tracks */
528 p_sys->stream.i_offset = 0;
529 p_sys->stream.i_tk = 0;
530 p_sys->stream.i_used = 0;
532 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
534 p_sys->stream.tk[i].i_date = 0;
535 p_sys->stream.tk[i].i_start = p_sys->i_pos;
536 p_sys->stream.tk[i].i_end = p_sys->i_pos;
539 /* Do the prebuffering */
540 AStreamPrebufferStream( s );
544 /****************************************************************************
545 * AStreamControlUpdate:
546 ****************************************************************************/
547 static void AStreamControlUpdate( stream_t *s )
549 stream_sys_t *p_sys = s->p_sys;
551 p_sys->i_pos = p_sys->p_access->info.i_pos;
556 for( i = 0; i < p_sys->i_list_index; i++ )
558 p_sys->i_pos += p_sys->list[i]->i_size;
563 /****************************************************************************
565 ****************************************************************************/
566 static int AStreamControl( stream_t *s, int i_query, va_list args )
568 stream_sys_t *p_sys = s->p_sys;
569 access_t *p_access = p_sys->p_access;
572 int64_t *pi_64, i_64;
577 case STREAM_GET_SIZE:
578 pi_64 = (int64_t*)va_arg( args, int64_t * );
579 if( s->p_sys->i_list )
583 for( i = 0; i < s->p_sys->i_list; i++ )
584 *pi_64 += s->p_sys->list[i]->i_size;
587 *pi_64 = p_access->info.i_size;
590 case STREAM_CAN_SEEK:
591 p_bool = (bool*)va_arg( args, bool * );
592 access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
595 case STREAM_CAN_FASTSEEK:
596 p_bool = (bool*)va_arg( args, bool * );
597 access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
600 case STREAM_GET_POSITION:
601 pi_64 = (int64_t*)va_arg( args, int64_t * );
602 *pi_64 = p_sys->i_pos;
605 case STREAM_SET_POSITION:
606 i_64 = (int64_t)va_arg( args, int64_t );
607 switch( p_sys->method )
609 case STREAM_METHOD_BLOCK:
610 return AStreamSeekBlock( s, i_64 );
611 case STREAM_METHOD_STREAM:
612 return AStreamSeekStream( s, i_64 );
621 case STREAM_CONTROL_ACCESS:
623 i_int = (int) va_arg( args, int );
624 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
625 i_int != ACCESS_SET_PRIVATE_ID_CA &&
626 i_int != ACCESS_GET_PRIVATE_ID_STATE &&
627 i_int != ACCESS_SET_TITLE &&
628 i_int != ACCESS_SET_SEEKPOINT )
630 msg_Err( s, "Hey, what are you thinking ?"
631 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
634 int i_ret = access_vaControl( p_access, i_int, args );
635 if( i_int == ACCESS_SET_TITLE || i_int == ACCESS_SET_SEEKPOINT )
636 AStreamControlReset( s );
640 case STREAM_UPDATE_SIZE:
641 AStreamControlUpdate( s );
644 case STREAM_GET_CONTENT_TYPE:
645 return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
646 va_arg( args, char ** ) );
647 case STREAM_SET_RECORD_STATE:
649 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
655 /****************************************************************************
657 ****************************************************************************/
658 static void AStreamPrebufferBlock( stream_t *s )
660 stream_sys_t *p_sys = s->p_sys;
661 access_t *p_access = p_sys->p_access;
666 msg_Dbg( s, "pre buffering" );
670 const int64_t i_date = mdate();
674 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE )
679 p_sys->stat.i_bytes = p_sys->block.i_size;
680 p_sys->stat.i_read_time = i_date - i_start;
681 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
682 (p_sys->stat.i_read_time + 1);
684 msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
685 "%"PRId64" kbytes/s",
687 p_sys->stat.i_read_time / INT64_C(1000000),
693 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
702 /* Append the block */
703 p_sys->block.i_size += b->i_buffer;
704 *p_sys->block.pp_last = b;
705 p_sys->block.pp_last = &b->p_next;
707 p_sys->stat.i_read_count++;
714 msg_Dbg( s, "received first data after %d ms",
715 (int)((i_first-i_start)/1000) );
719 p_sys->block.p_current = p_sys->block.p_first;
722 static int AStreamRefillBlock( stream_t *s );
724 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
726 stream_sys_t *p_sys = s->p_sys;
728 uint8_t *p_data = p_read;
729 unsigned int i_data = 0;
732 if( p_sys->block.p_current == NULL )
737 /* seek within this stream if possible, else use plain old read and discard */
738 stream_sys_t *p_sys = s->p_sys;
739 access_t *p_access = p_sys->p_access;
741 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
743 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
746 while( i_data < i_read )
749 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
750 unsigned int i_copy = __MIN( (unsigned int)__MAX(i_current,0), i_read - i_data);
756 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
762 p_sys->block.i_offset += i_copy;
763 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
765 /* Current block is now empty, switch to next */
766 if( p_sys->block.p_current )
768 p_sys->block.i_offset = 0;
769 p_sys->block.p_current = p_sys->block.p_current->p_next;
771 /*Get a new block if needed */
772 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
779 p_sys->i_pos += i_data;
783 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
785 stream_sys_t *p_sys = s->p_sys;
787 unsigned int i_data = 0;
789 unsigned int i_offset;
791 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
793 /* We can directly give a pointer over our buffer */
794 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
796 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
800 /* We need to create a local copy */
801 if( p_sys->i_peek < i_read )
803 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
809 p_sys->i_peek = i_read;
812 /* Fill enough data */
813 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
816 block_t **pp_last = p_sys->block.pp_last;
818 if( AStreamRefillBlock( s ) ) break;
820 /* Our buffer are probably filled enough, don't try anymore */
821 if( pp_last == p_sys->block.pp_last ) break;
824 /* Copy what we have */
825 b = p_sys->block.p_current;
826 i_offset = p_sys->block.i_offset;
827 p_data = p_sys->p_peek;
829 while( b && i_data < i_read )
831 unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
832 int i_copy = __MIN( i_current, i_read - i_data );
834 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
839 if( i_offset >= b->i_buffer )
846 *pp_peek = p_sys->p_peek;
850 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
852 stream_sys_t *p_sys = s->p_sys;
853 access_t *p_access = p_sys->p_access;
854 int64_t i_offset = i_pos - p_sys->block.i_start;
857 /* We already have thoses data, just update p_current/i_offset */
858 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
860 block_t *b = p_sys->block.p_first;
863 while( i_current + b->i_buffer < i_offset )
865 i_current += b->i_buffer;
869 p_sys->block.p_current = b;
870 p_sys->block.i_offset = i_offset - i_current;
872 p_sys->i_pos = i_pos;
877 /* We may need to seek or to read data */
881 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
885 msg_Err( s, "backward seeking impossible (access not seekable)" );
893 bool b_aseek, b_aseekfast;
895 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
896 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
901 msg_Warn( s, "%"PRId64" bytes need to be skipped "
902 "(access non seekable)",
903 i_offset - p_sys->block.i_size );
907 int64_t i_skip = i_offset - p_sys->block.i_size;
909 /* Avg bytes per packets */
910 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
911 /* TODO compute a seek cost instead of fixed threshold */
912 int i_th = b_aseekfast ? 1 : 5;
914 if( i_skip <= i_th * i_avg &&
915 i_skip < STREAM_CACHE_SIZE )
920 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
921 b_seek, i_th*i_avg, i_skip );
927 int64_t i_start, i_end;
928 /* Do the access seek */
930 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
934 block_ChainRelease( p_sys->block.p_first );
937 p_sys->block.i_start = p_sys->i_pos = i_pos;
938 p_sys->block.i_offset = 0;
939 p_sys->block.p_current = NULL;
940 p_sys->block.i_size = 0;
941 p_sys->block.p_first = NULL;
942 p_sys->block.pp_last = &p_sys->block.p_first;
945 if( AStreamRefillBlock( s ) )
949 p_sys->stat.i_seek_time += i_end - i_start;
950 p_sys->stat.i_seek_count++;
957 /* Read and skip enough data */
958 if( AStreamRefillBlock( s ) )
961 while( p_sys->block.p_current &&
962 p_sys->i_pos + p_sys->block.p_current->i_buffer - p_sys->block.i_offset < i_pos )
964 p_sys->i_pos += p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
965 p_sys->block.p_current = p_sys->block.p_current->p_next;
966 p_sys->block.i_offset = 0;
969 while( p_sys->block.i_start + p_sys->block.i_size < i_pos );
971 p_sys->block.i_offset = i_pos - p_sys->i_pos;
972 p_sys->i_pos = i_pos;
980 static int AStreamRefillBlock( stream_t *s )
982 stream_sys_t *p_sys = s->p_sys;
983 int64_t i_start, i_stop;
987 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
988 p_sys->block.p_first != p_sys->block.p_current )
990 block_t *b = p_sys->block.p_first;
992 p_sys->block.i_start += b->i_buffer;
993 p_sys->block.i_size -= b->i_buffer;
994 p_sys->block.p_first = b->p_next;
998 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
999 p_sys->block.p_current == p_sys->block.p_first &&
1000 p_sys->block.p_current->p_next ) /* At least 2 packets */
1002 /* Enough data, don't read more */
1006 /* Now read a new block */
1013 return VLC_EGENERIC;
1016 if( ( b = AReadBlock( s, &b_eof ) ) )
1019 return VLC_EGENERIC;
1026 /* Append the block */
1027 p_sys->block.i_size += b->i_buffer;
1028 *p_sys->block.pp_last = b;
1029 p_sys->block.pp_last = &b->p_next;
1032 if( p_sys->block.p_current == NULL )
1033 p_sys->block.p_current = b;
1036 p_sys->stat.i_bytes += b->i_buffer;
1037 p_sys->stat.i_read_time += i_stop - i_start;
1038 p_sys->stat.i_read_count++;
1047 /****************************************************************************
1049 ****************************************************************************/
1050 static int AStreamRefillStream( stream_t *s );
1052 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read )
1054 stream_sys_t *p_sys = s->p_sys;
1055 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1057 uint8_t *p_data = (uint8_t *)p_read;
1058 unsigned int i_data = 0;
1060 if( tk->i_start >= tk->i_end )
1063 if( p_data == NULL )
1065 /* seek within this stream if possible, else use plain old read and discard */
1066 stream_sys_t *p_sys = s->p_sys;
1067 access_t *p_access = p_sys->p_access;
1069 /* seeking after EOF is not what we want */
1070 if( !( p_access->info.b_eof ) )
1073 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1075 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1080 msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1081 " 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 while( i_data < i_read )
1088 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1089 STREAM_CACHE_TRACK_SIZE;
1090 unsigned int i_current =
1091 __MAX(0,__MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1092 STREAM_CACHE_TRACK_SIZE - i_off ));
1093 int i_copy = __MIN( i_current, i_read - i_data );
1095 if( i_copy <= 0 ) break; /* EOF */
1098 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1101 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1105 p_sys->stream.i_offset += i_copy;
1107 /* Update pos now */
1108 p_sys->i_pos += i_copy;
1111 p_sys->stream.i_used += i_copy;
1112 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1113 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1115 if( p_sys->stream.i_used < i_read - i_data )
1116 p_sys->stream.i_used = __MIN( i_read - i_data, STREAM_READ_ATONCE * 10 );
1118 if( AStreamRefillStream( s ) )
1121 if( tk->i_start >= tk->i_end ) break;
1129 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1131 stream_sys_t *p_sys = s->p_sys;
1132 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1135 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1138 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1139 "start=%"PRId64" offset=%d end=%"PRId64,
1140 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1141 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1144 /* Avoid problem, but that should *never* happen */
1145 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1146 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1148 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1150 if( p_sys->stream.i_used <= 1 )
1152 /* Be sure we will read something */
1153 p_sys->stream.i_used += i_read -
1154 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1156 if( AStreamRefillStream( s ) ) break;
1159 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1160 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1162 /* Now, direct pointer or a copy ? */
1163 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1164 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1166 *pp_peek = &tk->p_buffer[i_off];
1170 if( p_sys->i_peek < i_read )
1172 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1173 if( !p_sys->p_peek )
1178 p_sys->i_peek = i_read;
1181 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1182 STREAM_CACHE_TRACK_SIZE - i_off );
1183 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1184 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1186 *pp_peek = p_sys->p_peek;
1190 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1192 stream_sys_t *p_sys = s->p_sys;
1193 access_t *p_access = p_sys->p_access;
1201 msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1202 " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1203 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1204 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1205 p_sys->stream.i_offset,
1206 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1210 /* Seek in our current track ? */
1211 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1212 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1214 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1216 msg_Dbg( s, "AStreamSeekStream: current track" );
1218 p_sys->i_pos = i_pos;
1219 p_sys->stream.i_offset = i_pos - tk->i_start;
1221 /* If there is not enough data left in the track, refill */
1222 /* \todo How to get a correct value for
1223 * - refilling threshold
1224 * - how much to refill
1226 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1227 p_sys->stream.i_read_size )
1229 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1231 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1232 AStreamRefillStream( s );
1238 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1241 /* We can't do nothing */
1242 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1243 return VLC_EGENERIC;
1246 /* Date the current track */
1247 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1249 /* Try to reuse already read data */
1250 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1252 stream_track_t *tk = &p_sys->stream.tk[i];
1254 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1257 msg_Dbg( s, "AStreamSeekStream: reusing %d start=%"PRId64
1258 " end=%"PRId64, i, tk->i_start, tk->i_end );
1261 /* Seek at the end of the buffer */
1262 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1265 p_sys->i_pos = i_pos;
1266 p_sys->stream.i_tk = i;
1267 p_sys->stream.i_offset = i_pos - tk->i_start;
1269 if( p_sys->stream.i_used < 1024 )
1270 p_sys->stream.i_used = 1024;
1272 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1273 return VLC_EGENERIC;
1279 access_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1280 /* FIXME compute seek cost (instead of static 'stupid' value) */
1281 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1287 /* Search closest segment TODO */
1288 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1290 stream_track_t *tk = &p_sys->stream.tk[i];
1292 if( i_pos + i_maxth >= tk->i_start )
1294 msg_Dbg( s, "good segment before current pos, TODO" );
1296 if( i_pos - i_maxth <= tk->i_end )
1298 msg_Dbg( s, "good segment after current pos, TODO" );
1303 /* Nothing good, seek and choose oldest segment */
1304 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1305 p_sys->i_pos = i_pos;
1308 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1310 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1314 /* Reset the segment */
1315 p_sys->stream.i_tk = i_new;
1316 p_sys->stream.i_offset = 0;
1317 p_sys->stream.tk[i_new].i_start = i_pos;
1318 p_sys->stream.tk[i_new].i_end = i_pos;
1321 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1322 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1324 if( AStreamRefillStream( s ) )
1325 return VLC_EGENERIC;
1330 static int AStreamRefillStream( stream_t *s )
1332 stream_sys_t *p_sys = s->p_sys;
1333 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1335 /* We read but won't increase i_start after initial start + offset */
1337 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1338 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1339 bool b_read = false;
1340 int64_t i_start, i_stop;
1342 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1345 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1346 p_sys->stream.i_used, i_toread );
1350 while( i_toread > 0 )
1352 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1356 return VLC_EGENERIC;
1358 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1359 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1361 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1366 else if( i_read == 0 )
1369 return VLC_EGENERIC;
1375 tk->i_end += i_read;
1377 /* Windows of STREAM_CACHE_TRACK_SIZE */
1378 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1380 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1382 tk->i_start += i_invalid;
1383 p_sys->stream.i_offset -= i_invalid;
1387 p_sys->stream.i_used -= i_read;
1389 p_sys->stat.i_bytes += i_read;
1390 p_sys->stat.i_read_count++;
1394 p_sys->stat.i_read_time += i_stop - i_start;
1399 static void AStreamPrebufferStream( stream_t *s )
1401 stream_sys_t *p_sys = s->p_sys;
1402 access_t *p_access = p_sys->p_access;
1404 int64_t i_first = 0;
1407 msg_Dbg( s, "pre buffering" );
1411 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1413 int64_t i_date = mdate();
1416 if( s->b_die || tk->i_end >= STREAM_CACHE_PREBUFFER_SIZE )
1421 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1422 p_sys->stat.i_read_time = i_date - i_start;
1423 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1424 (p_sys->stat.i_read_time+1);
1426 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1427 "%"PRId64" kbytes/s",
1428 p_sys->stat.i_bytes,
1429 p_sys->stat.i_read_time / INT64_C(1000000),
1430 i_byterate / 1024 );
1435 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1436 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1437 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1440 else if( i_read == 0 )
1446 msg_Dbg( s, "received first data after %d ms",
1447 (int)((i_first-i_start)/1000) );
1450 tk->i_end += i_read;
1452 p_sys->stat.i_read_count++;
1456 /****************************************************************************
1458 ****************************************************************************/
1460 * Read from the stream untill first newline.
1461 * \param s Stream handle to read from
1462 * \return A pointer to the allocated output string. You need to free this when you are done.
1464 #define STREAM_PROBE_LINE 2048
1465 #define STREAM_LINE_MAX (2048*100)
1466 char *stream_ReadLine( stream_t *s )
1468 char *p_line = NULL;
1469 int i_line = 0, i_read = 0;
1471 while( i_read < STREAM_LINE_MAX )
1474 const uint8_t *p_data;
1478 /* Probe new data */
1479 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1480 if( i_data <= 0 ) break; /* No more data */
1483 i_pos = stream_Tell( s );
1484 if( i_pos == 0 && i_data >= 3 )
1487 const char *psz_encoding = NULL;
1489 if( !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
1491 psz_encoding = "UTF-8";
1494 else if( !memcmp( p_data, "\xFF\xFE", 2 ) )
1496 psz_encoding = "UTF-16LE";
1497 s->p_text->b_little_endian = true;
1498 s->p_text->i_char_width = 2;
1501 else if( !memcmp( p_data, "\xFE\xFF", 2 ) )
1503 psz_encoding = "UTF-16BE";
1504 s->p_text->i_char_width = 2;
1508 /* Seek past the BOM */
1511 stream_Seek( s, i_bom_size );
1512 p_data += i_bom_size;
1513 i_data -= i_bom_size;
1516 /* Open the converter if we need it */
1517 if( psz_encoding != NULL )
1519 msg_Dbg( s, "%s BOM detected", psz_encoding );
1520 if( s->p_text->i_char_width > 1 )
1522 s->p_text->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1523 if( s->p_text->conv == (vlc_iconv_t)-1 )
1525 msg_Err( s, "iconv_open failed" );
1529 /* FIXME that's UGLY */
1530 input_thread_t *p_input;
1531 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1532 if( p_input != NULL)
1534 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1535 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1536 vlc_object_release( p_input );
1541 if( i_data % s->p_text->i_char_width )
1543 /* keep i_char_width boundary */
1544 i_data = i_data - ( i_data % s->p_text->i_char_width );
1545 msg_Warn( s, "the read is not i_char_width compatible");
1551 /* Check if there is an EOL */
1552 if( s->p_text->i_char_width == 1 )
1554 /* UTF-8: 0A <LF> */
1555 psz_eol = memchr( p_data, '\n', i_data );
1559 const uint8_t *p = p_data;
1560 const uint8_t *p_last = p + i_data - s->p_text->i_char_width;
1562 if( s->p_text->i_char_width == 2 )
1564 if( s->p_text->b_little_endian == true)
1566 /* UTF-16LE: 0A 00 <LF> */
1567 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1572 /* UTF-16BE: 00 0A <LF> */
1573 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1584 psz_eol = (char *)p + ( s->p_text->i_char_width - 1 );
1590 i_data = (psz_eol - (char *)p_data) + 1;
1591 p_line = realloc( p_line, i_line + i_data + s->p_text->i_char_width ); /* add \0 */
1594 i_data = stream_Read( s, &p_line[i_line], i_data );
1595 if( i_data <= 0 ) break; /* Hmmm */
1596 i_line += i_data - s->p_text->i_char_width; /* skip \n */;
1599 /* We have our line */
1603 /* Read data (+1 for easy \0 append) */
1604 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->p_text->i_char_width );
1607 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1608 if( i_data <= 0 ) break; /* Hmmm */
1616 for( j = 0; j < s->p_text->i_char_width; j++ )
1618 p_line[i_line + j] = '\0';
1620 i_line += s->p_text->i_char_width; /* the added \0 */
1621 if( s->p_text->i_char_width > 1 )
1623 size_t i_in = 0, i_out = 0;
1624 const char * p_in = NULL;
1625 char * p_out = NULL;
1626 char * psz_new_line = NULL;
1629 psz_new_line = malloc( i_line );
1630 if( psz_new_line == NULL )
1632 i_in = i_out = (size_t)i_line;
1634 p_out = psz_new_line;
1636 if( vlc_iconv( s->p_text->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1638 msg_Err( s, "iconv failed" );
1639 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1642 p_line = psz_new_line;
1643 i_line = (size_t)i_line - i_out; /* does not include \0 */
1646 /* Remove trailing LF/CR */
1647 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1648 p_line[i_line-2] == '\n') ) i_line--;
1650 /* Make sure the \0 is there */
1651 p_line[i_line-1] = '\0';
1657 /* We failed to read any data, probably EOF */
1661 if( s->p_text->conv != (vlc_iconv_t)(-1) )
1662 vlc_iconv_close( s->p_text->conv );
1663 s->p_text->conv = (vlc_iconv_t)(-1);
1667 /****************************************************************************
1668 * Access reading/seeking wrappers to handle concatenated streams.
1669 ****************************************************************************/
1670 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read )
1672 stream_sys_t *p_sys = s->p_sys;
1673 access_t *p_access = p_sys->p_access;
1674 input_thread_t *p_input = NULL;
1675 int i_read_orig = i_read;
1678 if( s->p_parent && s->p_parent->p_parent &&
1679 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1680 p_input = (input_thread_t *)s->p_parent->p_parent;
1682 if( !p_sys->i_list )
1684 i_read = p_access->pf_read( p_access, p_read, i_read );
1685 if( p_access->b_die )
1686 vlc_object_kill( s );
1689 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1690 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
1692 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1693 (float)i_total, NULL );
1694 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1695 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1700 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1702 if( p_access->b_die )
1703 vlc_object_kill( s );
1705 /* If we reached an EOF then switch to the next stream in the list */
1706 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1708 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1709 access_t *p_list_access;
1711 msg_Dbg( s, "opening input `%s'", psz_name );
1713 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1715 if( !p_list_access ) return 0;
1717 if( p_sys->p_list_access != p_access )
1718 access_Delete( p_sys->p_list_access );
1720 p_sys->p_list_access = p_list_access;
1722 /* We have to read some data */
1723 return AReadStream( s, p_read, i_read_orig );
1726 /* Update read bytes in input */
1729 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1730 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
1731 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1732 (float)i_total, NULL );
1733 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1734 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1739 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
1741 stream_sys_t *p_sys = s->p_sys;
1742 access_t *p_access = p_sys->p_access;
1743 input_thread_t *p_input = NULL;
1748 if( s->p_parent && s->p_parent->p_parent &&
1749 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1750 p_input = (input_thread_t *)s->p_parent->p_parent;
1752 if( !p_sys->i_list )
1754 p_block = p_access->pf_block( p_access );
1755 if( p_access->b_die )
1756 vlc_object_kill( s );
1757 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1758 if( p_input && p_block && libvlc_stats (p_access) )
1760 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1761 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1762 p_block->i_buffer, &i_total );
1763 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1764 (float)i_total, NULL );
1765 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1766 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1771 p_block = p_sys->p_list_access->pf_block( p_sys->p_list_access );
1772 if( p_access->b_die )
1773 vlc_object_kill( s );
1774 b_eof = p_sys->p_list_access->info.b_eof;
1775 if( pb_eof ) *pb_eof = b_eof;
1777 /* If we reached an EOF then switch to the next stream in the list */
1778 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1780 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1781 access_t *p_list_access;
1783 msg_Dbg( s, "opening input `%s'", psz_name );
1785 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1787 if( !p_list_access ) return 0;
1789 if( p_sys->p_list_access != p_access )
1790 access_Delete( p_sys->p_list_access );
1792 p_sys->p_list_access = p_list_access;
1794 /* We have to read some data */
1795 return AReadBlock( s, pb_eof );
1801 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1802 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1803 p_block->i_buffer, &i_total );
1804 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1805 (float)i_total, NULL );
1806 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
1808 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1814 static int ASeek( stream_t *s, int64_t i_pos )
1816 stream_sys_t *p_sys = s->p_sys;
1817 access_t *p_access = p_sys->p_access;
1819 /* Check which stream we need to access */
1825 access_t *p_list_access = 0;
1827 for( i = 0; i < p_sys->i_list - 1; i++ )
1829 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1830 i_size += p_sys->list[i]->i_size;
1832 psz_name = p_sys->list[i]->psz_path;
1834 if( i != p_sys->i_list_index )
1835 msg_Dbg( s, "opening input `%s'", psz_name );
1837 if( i != p_sys->i_list_index && i != 0 )
1840 access_New( s, p_access->psz_access, "", psz_name );
1842 else if( i != p_sys->i_list_index )
1844 p_list_access = p_access;
1849 if( p_sys->p_list_access != p_access )
1850 access_Delete( p_sys->p_list_access );
1852 p_sys->p_list_access = p_list_access;
1855 p_sys->i_list_index = i;
1856 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1860 return p_access->pf_seek( p_access, i_pos );
1865 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
1866 * "p_read" is NULL then data are skipped instead of read. The return
1867 * value is the real numbers of bytes read/skip. If this value is less
1868 * than i_read that means that it's the end of the stream.
1870 int stream_Read( stream_t *s, void *p_read, int i_read )
1872 return s->pf_read( s, p_read, i_read );
1876 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
1877 * \return The real numbers of valid bytes, if it's less
1878 * or equal to 0, *pp_peek is invalid.
1879 * \note pp_peek is a pointer to internal buffer and it will be invalid as
1880 * soons as other stream_* functions are called.
1881 * \note Due to input limitation, it could be less than i_peek without meaning
1882 * the end of the stream (but only when you have i_peek >=
1883 * p_input->i_bufsize)
1885 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
1887 return s->pf_peek( s, pp_peek, i_peek );
1891 * Use to control the "stream_t *". Look at #stream_query_e for
1892 * possible "i_query" value and format arguments. Return VLC_SUCCESS
1893 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
1895 int stream_vaControl( stream_t *s, int i_query, va_list args )
1897 return s->pf_control( s, i_query, args );
1903 void stream_Delete( stream_t *s )
1908 int stream_Control( stream_t *s, int i_query, ... )
1914 return VLC_EGENERIC;
1916 va_start( args, i_query );
1917 i_result = s->pf_control( s, i_query, args );
1923 * Read "i_size" bytes and store them in a block_t.
1924 * It always read i_size bytes unless you are at the end of the stream
1925 * where it return what is available.
1927 block_t *stream_Block( stream_t *s, int i_size )
1929 if( i_size <= 0 ) return NULL;
1931 /* emulate block read */
1932 block_t *p_bk = block_New( s, i_size );
1935 int i_read = stream_Read( s, p_bk->p_buffer, i_size );
1938 p_bk->i_buffer = i_read;
1941 block_Release( p_bk );