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;
665 msg_Dbg( s, "pre buffering" );
669 const int64_t i_date = mdate();
673 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE )
678 p_sys->stat.i_bytes = p_sys->block.i_size;
679 p_sys->stat.i_read_time = i_date - i_start;
680 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
681 (p_sys->stat.i_read_time + 1);
683 msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
684 "%"PRId64" kbytes/s",
686 p_sys->stat.i_read_time / INT64_C(1000000),
692 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
701 /* Append the block */
702 p_sys->block.i_size += b->i_buffer;
703 *p_sys->block.pp_last = b;
704 p_sys->block.pp_last = &b->p_next;
706 p_sys->stat.i_read_count++;
713 msg_Dbg( s, "received first data after %d ms",
714 (int)((i_first-i_start)/1000) );
718 p_sys->block.p_current = p_sys->block.p_first;
721 static int AStreamRefillBlock( stream_t *s );
723 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
725 stream_sys_t *p_sys = s->p_sys;
727 uint8_t *p_data = p_read;
728 unsigned int i_data = 0;
731 if( p_sys->block.p_current == NULL )
736 /* seek within this stream if possible, else use plain old read and discard */
737 stream_sys_t *p_sys = s->p_sys;
738 access_t *p_access = p_sys->p_access;
740 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
742 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
745 while( i_data < i_read )
748 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
749 unsigned int i_copy = __MIN( (unsigned int)__MAX(i_current,0), i_read - i_data);
755 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
761 p_sys->block.i_offset += i_copy;
762 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
764 /* Current block is now empty, switch to next */
765 if( p_sys->block.p_current )
767 p_sys->block.i_offset = 0;
768 p_sys->block.p_current = p_sys->block.p_current->p_next;
770 /*Get a new block if needed */
771 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
778 p_sys->i_pos += i_data;
782 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
784 stream_sys_t *p_sys = s->p_sys;
786 unsigned int i_data = 0;
788 unsigned int i_offset;
790 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
792 /* We can directly give a pointer over our buffer */
793 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
795 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
799 /* We need to create a local copy */
800 if( p_sys->i_peek < i_read )
802 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
808 p_sys->i_peek = i_read;
811 /* Fill enough data */
812 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
815 block_t **pp_last = p_sys->block.pp_last;
817 if( AStreamRefillBlock( s ) ) break;
819 /* Our buffer are probably filled enough, don't try anymore */
820 if( pp_last == p_sys->block.pp_last ) break;
823 /* Copy what we have */
824 b = p_sys->block.p_current;
825 i_offset = p_sys->block.i_offset;
826 p_data = p_sys->p_peek;
828 while( b && i_data < i_read )
830 unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
831 int i_copy = __MIN( i_current, i_read - i_data );
833 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
838 if( i_offset >= b->i_buffer )
845 *pp_peek = p_sys->p_peek;
849 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
851 stream_sys_t *p_sys = s->p_sys;
852 access_t *p_access = p_sys->p_access;
853 int64_t i_offset = i_pos - p_sys->block.i_start;
856 /* We already have thoses data, just update p_current/i_offset */
857 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
859 block_t *b = p_sys->block.p_first;
862 while( i_current + b->i_buffer < i_offset )
864 i_current += b->i_buffer;
868 p_sys->block.p_current = b;
869 p_sys->block.i_offset = i_offset - i_current;
871 p_sys->i_pos = i_pos;
876 /* We may need to seek or to read data */
880 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
884 msg_Err( s, "backward seeking impossible (access not seekable)" );
892 bool b_aseek, b_aseekfast;
894 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
895 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
900 msg_Warn( s, "%"PRId64" bytes need to be skipped "
901 "(access non seekable)",
902 i_offset - p_sys->block.i_size );
906 int64_t i_skip = i_offset - p_sys->block.i_size;
908 /* Avg bytes per packets */
909 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
910 /* TODO compute a seek cost instead of fixed threshold */
911 int i_th = b_aseekfast ? 1 : 5;
913 if( i_skip <= i_th * i_avg &&
914 i_skip < STREAM_CACHE_SIZE )
919 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
920 b_seek, i_th*i_avg, i_skip );
926 int64_t i_start, i_end;
927 /* Do the access seek */
929 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
933 block_ChainRelease( p_sys->block.p_first );
936 p_sys->block.i_start = p_sys->i_pos = i_pos;
937 p_sys->block.i_offset = 0;
938 p_sys->block.p_current = NULL;
939 p_sys->block.i_size = 0;
940 p_sys->block.p_first = NULL;
941 p_sys->block.pp_last = &p_sys->block.p_first;
944 if( AStreamRefillBlock( s ) )
948 p_sys->stat.i_seek_time += i_end - i_start;
949 p_sys->stat.i_seek_count++;
956 /* Read and skip enough data */
957 if( AStreamRefillBlock( s ) )
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;
968 while( p_sys->block.i_start + p_sys->block.i_size < i_pos );
970 p_sys->block.i_offset = i_pos - p_sys->i_pos;
971 p_sys->i_pos = i_pos;
979 static int AStreamRefillBlock( stream_t *s )
981 stream_sys_t *p_sys = s->p_sys;
982 int64_t i_start, i_stop;
986 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
987 p_sys->block.p_first != p_sys->block.p_current )
989 block_t *b = p_sys->block.p_first;
991 p_sys->block.i_start += b->i_buffer;
992 p_sys->block.i_size -= b->i_buffer;
993 p_sys->block.p_first = b->p_next;
997 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
998 p_sys->block.p_current == p_sys->block.p_first &&
999 p_sys->block.p_current->p_next ) /* At least 2 packets */
1001 /* Enough data, don't read more */
1005 /* Now read a new block */
1012 return VLC_EGENERIC;
1015 if( ( b = AReadBlock( s, &b_eof ) ) )
1018 return VLC_EGENERIC;
1025 /* Append the block */
1026 p_sys->block.i_size += b->i_buffer;
1027 *p_sys->block.pp_last = b;
1028 p_sys->block.pp_last = &b->p_next;
1031 if( p_sys->block.p_current == NULL )
1032 p_sys->block.p_current = b;
1035 p_sys->stat.i_bytes += b->i_buffer;
1036 p_sys->stat.i_read_time += i_stop - i_start;
1037 p_sys->stat.i_read_count++;
1046 /****************************************************************************
1048 ****************************************************************************/
1049 static int AStreamRefillStream( stream_t *s );
1051 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read )
1053 stream_sys_t *p_sys = s->p_sys;
1054 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1056 uint8_t *p_data = (uint8_t *)p_read;
1057 unsigned int i_data = 0;
1059 if( tk->i_start >= tk->i_end )
1062 if( p_data == NULL )
1064 /* seek within this stream if possible, else use plain old read and discard */
1065 stream_sys_t *p_sys = s->p_sys;
1066 access_t *p_access = p_sys->p_access;
1068 /* seeking after EOF is not what we want */
1069 if( !( p_access->info.b_eof ) )
1072 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1074 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1079 msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1080 " offset=%d end=%"PRId64,
1081 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1082 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1085 while( i_data < i_read )
1087 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1088 STREAM_CACHE_TRACK_SIZE;
1089 unsigned int i_current =
1090 __MAX(0,__MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1091 STREAM_CACHE_TRACK_SIZE - i_off ));
1092 int i_copy = __MIN( i_current, i_read - i_data );
1094 if( i_copy <= 0 ) break; /* EOF */
1097 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1100 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1104 p_sys->stream.i_offset += i_copy;
1106 /* Update pos now */
1107 p_sys->i_pos += i_copy;
1110 p_sys->stream.i_used += i_copy;
1111 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1112 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1114 if( p_sys->stream.i_used < i_read - i_data )
1115 p_sys->stream.i_used = __MIN( i_read - i_data, STREAM_READ_ATONCE * 10 );
1117 if( AStreamRefillStream( s ) )
1120 if( tk->i_start >= tk->i_end ) break;
1128 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1130 stream_sys_t *p_sys = s->p_sys;
1131 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1134 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1137 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1138 "start=%"PRId64" offset=%d end=%"PRId64,
1139 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1140 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1143 /* Avoid problem, but that should *never* happen */
1144 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1145 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1147 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1149 if( p_sys->stream.i_used <= 1 )
1151 /* Be sure we will read something */
1152 p_sys->stream.i_used += i_read -
1153 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1155 if( AStreamRefillStream( s ) ) break;
1158 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1159 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1161 /* Now, direct pointer or a copy ? */
1162 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1163 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1165 *pp_peek = &tk->p_buffer[i_off];
1169 if( p_sys->i_peek < i_read )
1171 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1172 if( !p_sys->p_peek )
1177 p_sys->i_peek = i_read;
1180 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1181 STREAM_CACHE_TRACK_SIZE - i_off );
1182 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1183 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1185 *pp_peek = p_sys->p_peek;
1189 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1191 stream_sys_t *p_sys = s->p_sys;
1192 access_t *p_access = p_sys->p_access;
1200 msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1201 " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1202 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1203 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1204 p_sys->stream.i_offset,
1205 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1209 /* Seek in our current track ? */
1210 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1211 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1213 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1215 msg_Dbg( s, "AStreamSeekStream: current track" );
1217 p_sys->i_pos = i_pos;
1218 p_sys->stream.i_offset = i_pos - tk->i_start;
1220 /* If there is not enough data left in the track, refill */
1221 /* \todo How to get a correct value for
1222 * - refilling threshold
1223 * - how much to refill
1225 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1226 p_sys->stream.i_read_size )
1228 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1230 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1231 AStreamRefillStream( s );
1237 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1240 /* We can't do nothing */
1241 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1242 return VLC_EGENERIC;
1245 /* Date the current track */
1246 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1248 /* Try to reuse already read data */
1249 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1251 stream_track_t *tk = &p_sys->stream.tk[i];
1253 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1256 msg_Dbg( s, "AStreamSeekStream: reusing %d start=%"PRId64
1257 " end=%"PRId64, i, tk->i_start, tk->i_end );
1260 /* Seek at the end of the buffer */
1261 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1264 p_sys->i_pos = i_pos;
1265 p_sys->stream.i_tk = i;
1266 p_sys->stream.i_offset = i_pos - tk->i_start;
1268 if( p_sys->stream.i_used < 1024 )
1269 p_sys->stream.i_used = 1024;
1271 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1272 return VLC_EGENERIC;
1278 access_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1279 /* FIXME compute seek cost (instead of static 'stupid' value) */
1280 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1286 /* Search closest segment TODO */
1287 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1289 stream_track_t *tk = &p_sys->stream.tk[i];
1291 if( i_pos + i_maxth >= tk->i_start )
1293 msg_Dbg( s, "good segment before current pos, TODO" );
1295 if( i_pos - i_maxth <= tk->i_end )
1297 msg_Dbg( s, "good segment after current pos, TODO" );
1302 /* Nothing good, seek and choose oldest segment */
1303 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1304 p_sys->i_pos = i_pos;
1307 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1309 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1313 /* Reset the segment */
1314 p_sys->stream.i_tk = i_new;
1315 p_sys->stream.i_offset = 0;
1316 p_sys->stream.tk[i_new].i_start = i_pos;
1317 p_sys->stream.tk[i_new].i_end = i_pos;
1320 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1321 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1323 if( AStreamRefillStream( s ) )
1324 return VLC_EGENERIC;
1329 static int AStreamRefillStream( stream_t *s )
1331 stream_sys_t *p_sys = s->p_sys;
1332 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1334 /* We read but won't increase i_start after initial start + offset */
1336 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1337 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1338 bool b_read = false;
1339 int64_t i_start, i_stop;
1341 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1344 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1345 p_sys->stream.i_used, i_toread );
1349 while( i_toread > 0 )
1351 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1355 return VLC_EGENERIC;
1357 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1358 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1360 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1365 else if( i_read == 0 )
1368 return VLC_EGENERIC;
1374 tk->i_end += i_read;
1376 /* Windows of STREAM_CACHE_TRACK_SIZE */
1377 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1379 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1381 tk->i_start += i_invalid;
1382 p_sys->stream.i_offset -= i_invalid;
1386 p_sys->stream.i_used -= i_read;
1388 p_sys->stat.i_bytes += i_read;
1389 p_sys->stat.i_read_count++;
1393 p_sys->stat.i_read_time += i_stop - i_start;
1398 static void AStreamPrebufferStream( stream_t *s )
1400 stream_sys_t *p_sys = s->p_sys;
1402 int64_t i_first = 0;
1405 msg_Dbg( s, "pre buffering" );
1409 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1411 int64_t i_date = mdate();
1414 if( s->b_die || tk->i_end >= STREAM_CACHE_PREBUFFER_SIZE )
1419 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1420 p_sys->stat.i_read_time = i_date - i_start;
1421 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1422 (p_sys->stat.i_read_time+1);
1424 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1425 "%"PRId64" kbytes/s",
1426 p_sys->stat.i_bytes,
1427 p_sys->stat.i_read_time / INT64_C(1000000),
1428 i_byterate / 1024 );
1433 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1434 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1435 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1438 else if( i_read == 0 )
1444 msg_Dbg( s, "received first data after %d ms",
1445 (int)((i_first-i_start)/1000) );
1448 tk->i_end += i_read;
1450 p_sys->stat.i_read_count++;
1454 /****************************************************************************
1456 ****************************************************************************/
1458 * Read from the stream untill first newline.
1459 * \param s Stream handle to read from
1460 * \return A pointer to the allocated output string. You need to free this when you are done.
1462 #define STREAM_PROBE_LINE 2048
1463 #define STREAM_LINE_MAX (2048*100)
1464 char *stream_ReadLine( stream_t *s )
1466 char *p_line = NULL;
1467 int i_line = 0, i_read = 0;
1469 while( i_read < STREAM_LINE_MAX )
1472 const uint8_t *p_data;
1476 /* Probe new data */
1477 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1478 if( i_data <= 0 ) break; /* No more data */
1481 i_pos = stream_Tell( s );
1482 if( i_pos == 0 && i_data >= 3 )
1485 const char *psz_encoding = NULL;
1487 if( !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
1489 psz_encoding = "UTF-8";
1492 else if( !memcmp( p_data, "\xFF\xFE", 2 ) )
1494 psz_encoding = "UTF-16LE";
1495 s->p_text->b_little_endian = true;
1496 s->p_text->i_char_width = 2;
1499 else if( !memcmp( p_data, "\xFE\xFF", 2 ) )
1501 psz_encoding = "UTF-16BE";
1502 s->p_text->i_char_width = 2;
1506 /* Seek past the BOM */
1509 stream_Seek( s, i_bom_size );
1510 p_data += i_bom_size;
1511 i_data -= i_bom_size;
1514 /* Open the converter if we need it */
1515 if( psz_encoding != NULL )
1517 msg_Dbg( s, "%s BOM detected", psz_encoding );
1518 if( s->p_text->i_char_width > 1 )
1520 s->p_text->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1521 if( s->p_text->conv == (vlc_iconv_t)-1 )
1523 msg_Err( s, "iconv_open failed" );
1527 /* FIXME that's UGLY */
1528 input_thread_t *p_input;
1529 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1530 if( p_input != NULL)
1532 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1533 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1534 vlc_object_release( p_input );
1539 if( i_data % s->p_text->i_char_width )
1541 /* keep i_char_width boundary */
1542 i_data = i_data - ( i_data % s->p_text->i_char_width );
1543 msg_Warn( s, "the read is not i_char_width compatible");
1549 /* Check if there is an EOL */
1550 if( s->p_text->i_char_width == 1 )
1552 /* UTF-8: 0A <LF> */
1553 psz_eol = memchr( p_data, '\n', i_data );
1557 const uint8_t *p = p_data;
1558 const uint8_t *p_last = p + i_data - s->p_text->i_char_width;
1560 if( s->p_text->i_char_width == 2 )
1562 if( s->p_text->b_little_endian == true)
1564 /* UTF-16LE: 0A 00 <LF> */
1565 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1570 /* UTF-16BE: 00 0A <LF> */
1571 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1582 psz_eol = (char *)p + ( s->p_text->i_char_width - 1 );
1588 i_data = (psz_eol - (char *)p_data) + 1;
1589 p_line = realloc( p_line, i_line + i_data + s->p_text->i_char_width ); /* add \0 */
1592 i_data = stream_Read( s, &p_line[i_line], i_data );
1593 if( i_data <= 0 ) break; /* Hmmm */
1594 i_line += i_data - s->p_text->i_char_width; /* skip \n */;
1597 /* We have our line */
1601 /* Read data (+1 for easy \0 append) */
1602 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->p_text->i_char_width );
1605 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1606 if( i_data <= 0 ) break; /* Hmmm */
1614 for( j = 0; j < s->p_text->i_char_width; j++ )
1616 p_line[i_line + j] = '\0';
1618 i_line += s->p_text->i_char_width; /* the added \0 */
1619 if( s->p_text->i_char_width > 1 )
1621 size_t i_in = 0, i_out = 0;
1622 const char * p_in = NULL;
1623 char * p_out = NULL;
1624 char * psz_new_line = NULL;
1627 psz_new_line = malloc( i_line );
1628 if( psz_new_line == NULL )
1630 i_in = i_out = (size_t)i_line;
1632 p_out = psz_new_line;
1634 if( vlc_iconv( s->p_text->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1636 msg_Err( s, "iconv failed" );
1637 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1640 p_line = psz_new_line;
1641 i_line = (size_t)i_line - i_out; /* does not include \0 */
1644 /* Remove trailing LF/CR */
1645 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1646 p_line[i_line-2] == '\n') ) i_line--;
1648 /* Make sure the \0 is there */
1649 p_line[i_line-1] = '\0';
1655 /* We failed to read any data, probably EOF */
1659 if( s->p_text->conv != (vlc_iconv_t)(-1) )
1660 vlc_iconv_close( s->p_text->conv );
1661 s->p_text->conv = (vlc_iconv_t)(-1);
1665 /****************************************************************************
1666 * Access reading/seeking wrappers to handle concatenated streams.
1667 ****************************************************************************/
1668 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read )
1670 stream_sys_t *p_sys = s->p_sys;
1671 access_t *p_access = p_sys->p_access;
1672 input_thread_t *p_input = NULL;
1673 int i_read_orig = i_read;
1676 if( s->p_parent && s->p_parent->p_parent &&
1677 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1678 p_input = (input_thread_t *)s->p_parent->p_parent;
1680 if( !p_sys->i_list )
1682 i_read = p_access->pf_read( p_access, p_read, i_read );
1683 if( p_access->b_die )
1684 vlc_object_kill( s );
1687 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1688 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
1690 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1691 (float)i_total, NULL );
1692 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1693 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1698 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1700 if( p_access->b_die )
1701 vlc_object_kill( s );
1703 /* If we reached an EOF then switch to the next stream in the list */
1704 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1706 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1707 access_t *p_list_access;
1709 msg_Dbg( s, "opening input `%s'", psz_name );
1711 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1713 if( !p_list_access ) return 0;
1715 if( p_sys->p_list_access != p_access )
1716 access_Delete( p_sys->p_list_access );
1718 p_sys->p_list_access = p_list_access;
1720 /* We have to read some data */
1721 return AReadStream( s, p_read, i_read_orig );
1724 /* Update read bytes in input */
1727 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1728 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
1729 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1730 (float)i_total, NULL );
1731 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1732 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1737 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
1739 stream_sys_t *p_sys = s->p_sys;
1740 access_t *p_access = p_sys->p_access;
1741 input_thread_t *p_input = NULL;
1746 if( s->p_parent && s->p_parent->p_parent &&
1747 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1748 p_input = (input_thread_t *)s->p_parent->p_parent;
1750 if( !p_sys->i_list )
1752 p_block = p_access->pf_block( p_access );
1753 if( p_access->b_die )
1754 vlc_object_kill( s );
1755 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1756 if( p_input && p_block && libvlc_stats (p_access) )
1758 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1759 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1760 p_block->i_buffer, &i_total );
1761 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1762 (float)i_total, NULL );
1763 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1764 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1769 p_block = p_sys->p_list_access->pf_block( p_sys->p_list_access );
1770 if( p_access->b_die )
1771 vlc_object_kill( s );
1772 b_eof = p_sys->p_list_access->info.b_eof;
1773 if( pb_eof ) *pb_eof = b_eof;
1775 /* If we reached an EOF then switch to the next stream in the list */
1776 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1778 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1779 access_t *p_list_access;
1781 msg_Dbg( s, "opening input `%s'", psz_name );
1783 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1785 if( !p_list_access ) return 0;
1787 if( p_sys->p_list_access != p_access )
1788 access_Delete( p_sys->p_list_access );
1790 p_sys->p_list_access = p_list_access;
1792 /* We have to read some data */
1793 return AReadBlock( s, pb_eof );
1799 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1800 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1801 p_block->i_buffer, &i_total );
1802 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1803 (float)i_total, NULL );
1804 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
1806 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1812 static int ASeek( stream_t *s, int64_t i_pos )
1814 stream_sys_t *p_sys = s->p_sys;
1815 access_t *p_access = p_sys->p_access;
1817 /* Check which stream we need to access */
1823 access_t *p_list_access = 0;
1825 for( i = 0; i < p_sys->i_list - 1; i++ )
1827 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1828 i_size += p_sys->list[i]->i_size;
1830 psz_name = p_sys->list[i]->psz_path;
1832 if( i != p_sys->i_list_index )
1833 msg_Dbg( s, "opening input `%s'", psz_name );
1835 if( i != p_sys->i_list_index && i != 0 )
1838 access_New( s, p_access->psz_access, "", psz_name );
1840 else if( i != p_sys->i_list_index )
1842 p_list_access = p_access;
1847 if( p_sys->p_list_access != p_access )
1848 access_Delete( p_sys->p_list_access );
1850 p_sys->p_list_access = p_list_access;
1853 p_sys->i_list_index = i;
1854 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1858 return p_access->pf_seek( p_access, i_pos );
1863 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
1864 * "p_read" is NULL then data are skipped instead of read. The return
1865 * value is the real numbers of bytes read/skip. If this value is less
1866 * than i_read that means that it's the end of the stream.
1868 int stream_Read( stream_t *s, void *p_read, int i_read )
1870 return s->pf_read( s, p_read, i_read );
1874 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
1875 * \return The real numbers of valid bytes, if it's less
1876 * or equal to 0, *pp_peek is invalid.
1877 * \note pp_peek is a pointer to internal buffer and it will be invalid as
1878 * soons as other stream_* functions are called.
1879 * \note Due to input limitation, it could be less than i_peek without meaning
1880 * the end of the stream (but only when you have i_peek >=
1881 * p_input->i_bufsize)
1883 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
1885 return s->pf_peek( s, pp_peek, i_peek );
1889 * Use to control the "stream_t *". Look at #stream_query_e for
1890 * possible "i_query" value and format arguments. Return VLC_SUCCESS
1891 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
1893 int stream_vaControl( stream_t *s, int i_query, va_list args )
1895 return s->pf_control( s, i_query, args );
1901 void stream_Delete( stream_t *s )
1906 int stream_Control( stream_t *s, int i_query, ... )
1912 return VLC_EGENERIC;
1914 va_start( args, i_query );
1915 i_result = s->pf_control( s, i_query, args );
1921 * Read "i_size" bytes and store them in a block_t.
1922 * It always read i_size bytes unless you are at the end of the stream
1923 * where it return what is available.
1925 block_t *stream_Block( stream_t *s, int i_size )
1927 if( i_size <= 0 ) return NULL;
1929 /* emulate block read */
1930 block_t *p_bk = block_New( s, i_size );
1933 int i_read = stream_Read( s, p_bk->p_buffer, i_size );
1936 p_bk->i_buffer = i_read;
1939 block_Release( p_bk );