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"
43 // #define STREAM_DEBUG 1
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 );
241 vlc_object_release( s );
244 /****************************************************************************
245 * stream_UrlNew: create a stream from a access
246 ****************************************************************************/
247 stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
249 const char *psz_access, *psz_demux;
257 char psz_dup[strlen( psz_url ) + 1];
258 strcpy( psz_dup, psz_url );
259 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
261 /* Now try a real access */
262 p_access = access_New( p_parent, psz_access, psz_demux, psz_path );
264 if( p_access == NULL )
266 msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
270 if( !( p_res = stream_AccessNew( p_access, NULL ) ) )
272 access_Delete( p_access );
276 p_res->pf_destroy = UStreamDestroy;
280 stream_t *stream_AccessNew( access_t *p_access, char **ppsz_list )
282 stream_t *s = stream_CommonNew( VLC_OBJECT(p_access) );
288 s->psz_path = strdup( p_access->psz_path );
289 s->p_sys = p_sys = malloc( sizeof( *p_sys ) );
290 if( !s->psz_path || !s->p_sys )
292 stream_CommonDelete( s );
296 /* Attach it now, needed for b_die */
297 vlc_object_attach( s, p_access );
299 s->pf_read = NULL; /* Set up later */
301 s->pf_control = AStreamControl;
302 s->pf_destroy = AStreamDestroy;
305 p_sys->p_access = p_access;
306 if( p_access->pf_block )
307 p_sys->method = STREAM_METHOD_BLOCK;
309 p_sys->method = STREAM_METHOD_STREAM;
311 p_sys->i_pos = p_access->info.i_pos;
314 access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
315 p_sys->stat.i_bytes = 0;
316 p_sys->stat.i_read_time = 0;
317 p_sys->stat.i_read_count = 0;
318 p_sys->stat.i_seek_count = 0;
319 p_sys->stat.i_seek_time = 0;
321 TAB_INIT( p_sys->i_list, p_sys->list );
322 p_sys->i_list_index = 0;
323 p_sys->p_list_access = NULL;
325 /* Get the additional list of inputs if any (for concatenation) */
326 if( ppsz_list && ppsz_list[0] )
328 access_entry_t *p_entry = malloc( sizeof(*p_entry) );
332 p_entry->i_size = p_access->info.i_size;
333 p_entry->psz_path = strdup( p_access->psz_path );
334 if( !p_entry->psz_path )
339 p_sys->p_list_access = p_access;
340 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
341 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
342 p_entry->psz_path, p_access->info.i_size );
344 for( int i = 0; ppsz_list[i] != NULL; i++ )
346 char *psz_name = strdup( ppsz_list[i] );
351 access_t *p_tmp = access_New( p_access,
352 p_access->psz_access, "", psz_name );
356 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
357 psz_name, p_tmp->info.i_size );
359 p_entry = malloc( sizeof(*p_entry) );
362 p_entry->i_size = p_tmp->info.i_size;
363 p_entry->psz_path = psz_name;
364 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
366 access_Delete( p_tmp );
372 p_sys->p_peek = NULL;
374 if( p_sys->method == STREAM_METHOD_BLOCK )
376 msg_Dbg( s, "Using AStream*Block" );
377 s->pf_read = AStreamReadBlock;
378 s->pf_peek = AStreamPeekBlock;
380 /* Init all fields of p_sys->block */
381 p_sys->block.i_start = p_sys->i_pos;
382 p_sys->block.i_offset = 0;
383 p_sys->block.p_current = NULL;
384 p_sys->block.i_size = 0;
385 p_sys->block.p_first = NULL;
386 p_sys->block.pp_last = &p_sys->block.p_first;
388 /* Do the prebuffering */
389 AStreamPrebufferBlock( s );
391 if( p_sys->block.i_size <= 0 )
393 msg_Err( s, "cannot pre fill buffer" );
401 assert( p_sys->method == STREAM_METHOD_STREAM );
403 msg_Dbg( s, "Using AStream*Stream" );
405 s->pf_read = AStreamReadStream;
406 s->pf_peek = AStreamPeekStream;
408 /* Allocate/Setup our tracks */
409 p_sys->stream.i_offset = 0;
410 p_sys->stream.i_tk = 0;
411 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
412 if( p_sys->stream.p_buffer == NULL )
414 p_sys->stream.i_used = 0;
415 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
416 #if STREAM_READ_ATONCE < 256
417 # error "Invalid STREAM_READ_ATONCE value"
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 );
618 case STREAM_CONTROL_ACCESS:
620 i_int = (int) va_arg( args, int );
621 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
622 i_int != ACCESS_SET_PRIVATE_ID_CA &&
623 i_int != ACCESS_GET_PRIVATE_ID_STATE &&
624 i_int != ACCESS_SET_TITLE &&
625 i_int != ACCESS_SET_SEEKPOINT )
627 msg_Err( s, "Hey, what are you thinking ?"
628 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
631 int i_ret = access_vaControl( p_access, i_int, args );
632 if( i_int == ACCESS_SET_TITLE || i_int == ACCESS_SET_SEEKPOINT )
633 AStreamControlReset( s );
637 case STREAM_UPDATE_SIZE:
638 AStreamControlUpdate( s );
641 case STREAM_GET_CONTENT_TYPE:
642 return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
643 va_arg( args, char ** ) );
644 case STREAM_SET_RECORD_STATE:
646 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
652 /****************************************************************************
654 ****************************************************************************/
655 static void AStreamPrebufferBlock( stream_t *s )
657 stream_sys_t *p_sys = s->p_sys;
662 msg_Dbg( s, "pre buffering" );
666 const int64_t i_date = mdate();
670 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE )
675 p_sys->stat.i_bytes = p_sys->block.i_size;
676 p_sys->stat.i_read_time = i_date - i_start;
677 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
678 (p_sys->stat.i_read_time + 1);
680 msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
681 "%"PRId64" kbytes/s",
683 p_sys->stat.i_read_time / INT64_C(1000000),
689 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
698 /* Append the block */
699 p_sys->block.i_size += b->i_buffer;
700 *p_sys->block.pp_last = b;
701 p_sys->block.pp_last = &b->p_next;
703 p_sys->stat.i_read_count++;
710 msg_Dbg( s, "received first data after %d ms",
711 (int)((i_first-i_start)/1000) );
715 p_sys->block.p_current = p_sys->block.p_first;
718 static int AStreamRefillBlock( stream_t *s );
720 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
722 stream_sys_t *p_sys = s->p_sys;
724 uint8_t *p_data = p_read;
725 unsigned int i_data = 0;
728 if( p_sys->block.p_current == NULL )
733 /* seek within this stream if possible, else use plain old read and discard */
734 stream_sys_t *p_sys = s->p_sys;
735 access_t *p_access = p_sys->p_access;
737 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
739 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
742 while( i_data < i_read )
745 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
746 unsigned int i_copy = __MIN( (unsigned int)__MAX(i_current,0), i_read - i_data);
752 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
758 p_sys->block.i_offset += i_copy;
759 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
761 /* Current block is now empty, switch to next */
762 if( p_sys->block.p_current )
764 p_sys->block.i_offset = 0;
765 p_sys->block.p_current = p_sys->block.p_current->p_next;
767 /*Get a new block if needed */
768 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
775 p_sys->i_pos += i_data;
779 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
781 stream_sys_t *p_sys = s->p_sys;
783 unsigned int i_data = 0;
785 unsigned int i_offset;
787 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
789 /* We can directly give a pointer over our buffer */
790 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
792 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
796 /* We need to create a local copy */
797 if( p_sys->i_peek < i_read )
799 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
805 p_sys->i_peek = i_read;
808 /* Fill enough data */
809 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
812 block_t **pp_last = p_sys->block.pp_last;
814 if( AStreamRefillBlock( s ) ) break;
816 /* Our buffer are probably filled enough, don't try anymore */
817 if( pp_last == p_sys->block.pp_last ) break;
820 /* Copy what we have */
821 b = p_sys->block.p_current;
822 i_offset = p_sys->block.i_offset;
823 p_data = p_sys->p_peek;
825 while( b && i_data < i_read )
827 unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
828 int i_copy = __MIN( i_current, i_read - i_data );
830 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
835 if( i_offset >= b->i_buffer )
842 *pp_peek = p_sys->p_peek;
846 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
848 stream_sys_t *p_sys = s->p_sys;
849 access_t *p_access = p_sys->p_access;
850 int64_t i_offset = i_pos - p_sys->block.i_start;
853 /* We already have thoses data, just update p_current/i_offset */
854 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
856 block_t *b = p_sys->block.p_first;
859 while( i_current + b->i_buffer < i_offset )
861 i_current += b->i_buffer;
865 p_sys->block.p_current = b;
866 p_sys->block.i_offset = i_offset - i_current;
868 p_sys->i_pos = i_pos;
873 /* We may need to seek or to read data */
877 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
881 msg_Err( s, "backward seeking impossible (access not seekable)" );
889 bool b_aseek, b_aseekfast;
891 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
892 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
897 msg_Warn( s, "%"PRId64" bytes need to be skipped "
898 "(access non seekable)",
899 i_offset - p_sys->block.i_size );
903 int64_t i_skip = i_offset - p_sys->block.i_size;
905 /* Avg bytes per packets */
906 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
907 /* TODO compute a seek cost instead of fixed threshold */
908 int i_th = b_aseekfast ? 1 : 5;
910 if( i_skip <= i_th * i_avg &&
911 i_skip < STREAM_CACHE_SIZE )
916 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
917 b_seek, i_th*i_avg, i_skip );
923 int64_t i_start, i_end;
924 /* Do the access seek */
926 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
930 block_ChainRelease( p_sys->block.p_first );
933 p_sys->block.i_start = p_sys->i_pos = i_pos;
934 p_sys->block.i_offset = 0;
935 p_sys->block.p_current = NULL;
936 p_sys->block.i_size = 0;
937 p_sys->block.p_first = NULL;
938 p_sys->block.pp_last = &p_sys->block.p_first;
941 if( AStreamRefillBlock( s ) )
945 p_sys->stat.i_seek_time += i_end - i_start;
946 p_sys->stat.i_seek_count++;
953 /* Read and skip enough data */
954 if( AStreamRefillBlock( s ) )
957 while( p_sys->block.p_current &&
958 p_sys->i_pos + p_sys->block.p_current->i_buffer - p_sys->block.i_offset < i_pos )
960 p_sys->i_pos += p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
961 p_sys->block.p_current = p_sys->block.p_current->p_next;
962 p_sys->block.i_offset = 0;
965 while( p_sys->block.i_start + p_sys->block.i_size < i_pos );
967 p_sys->block.i_offset += i_pos - p_sys->i_pos;
968 p_sys->i_pos = i_pos;
976 static int AStreamRefillBlock( stream_t *s )
978 stream_sys_t *p_sys = s->p_sys;
982 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
983 p_sys->block.p_first != p_sys->block.p_current )
985 block_t *b = p_sys->block.p_first;
987 p_sys->block.i_start += b->i_buffer;
988 p_sys->block.i_size -= b->i_buffer;
989 p_sys->block.p_first = b->p_next;
993 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
994 p_sys->block.p_current == p_sys->block.p_first &&
995 p_sys->block.p_current->p_next ) /* At least 2 packets */
997 /* Enough data, don't read more */
1001 /* Now read a new block */
1002 const int64_t i_start = mdate();
1008 return VLC_EGENERIC;
1011 if( ( b = AReadBlock( s, &b_eof ) ) )
1014 return VLC_EGENERIC;
1017 p_sys->stat.i_read_time += mdate() - i_start;
1020 /* Append the block */
1021 p_sys->block.i_size += b->i_buffer;
1022 *p_sys->block.pp_last = b;
1023 p_sys->block.pp_last = &b->p_next;
1026 if( p_sys->block.p_current == NULL )
1027 p_sys->block.p_current = b;
1030 p_sys->stat.i_bytes += b->i_buffer;
1031 p_sys->stat.i_read_count++;
1039 /****************************************************************************
1041 ****************************************************************************/
1042 static int AStreamRefillStream( stream_t *s );
1044 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read )
1046 stream_sys_t *p_sys = s->p_sys;
1047 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1049 uint8_t *p_data = (uint8_t *)p_read;
1050 unsigned int i_data = 0;
1052 if( tk->i_start >= tk->i_end )
1055 if( p_data == NULL )
1057 /* seek within this stream if possible, else use plain old read and discard */
1058 stream_sys_t *p_sys = s->p_sys;
1059 access_t *p_access = p_sys->p_access;
1061 /* seeking after EOF is not what we want */
1062 if( !( p_access->info.b_eof ) )
1065 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1067 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1072 msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1073 " offset=%d end=%"PRId64,
1074 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1075 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1078 while( i_data < i_read )
1080 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1081 STREAM_CACHE_TRACK_SIZE;
1082 unsigned int i_current =
1083 __MAX(0,__MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1084 STREAM_CACHE_TRACK_SIZE - i_off ));
1085 int i_copy = __MIN( i_current, i_read - i_data );
1087 if( i_copy <= 0 ) break; /* EOF */
1090 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1093 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1097 p_sys->stream.i_offset += i_copy;
1099 /* Update pos now */
1100 p_sys->i_pos += i_copy;
1103 p_sys->stream.i_used += i_copy;
1105 if( tk->i_end - tk->i_start - p_sys->stream.i_offset <= i_read -i_data )
1107 const int i_read_requested = __MAX( __MIN( i_read - i_data,
1108 STREAM_READ_ATONCE * 10 ),
1109 STREAM_READ_ATONCE / 2 );
1111 if( p_sys->stream.i_used < i_read_requested )
1112 p_sys->stream.i_used = i_read_requested;
1114 if( AStreamRefillStream( s ) )
1117 if( tk->i_start >= tk->i_end ) break;
1125 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1127 stream_sys_t *p_sys = s->p_sys;
1128 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1131 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1134 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1135 "start=%"PRId64" offset=%d end=%"PRId64,
1136 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1137 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1140 /* Avoid problem, but that should *never* happen */
1141 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1142 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1144 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1146 if( p_sys->stream.i_used <= 1 )
1148 /* Be sure we will read something */
1149 p_sys->stream.i_used += i_read -
1150 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1152 if( AStreamRefillStream( s ) ) break;
1155 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1156 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1158 /* Now, direct pointer or a copy ? */
1159 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1160 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1162 *pp_peek = &tk->p_buffer[i_off];
1166 if( p_sys->i_peek < i_read )
1168 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1169 if( !p_sys->p_peek )
1174 p_sys->i_peek = i_read;
1177 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1178 STREAM_CACHE_TRACK_SIZE - i_off );
1179 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1180 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1182 *pp_peek = p_sys->p_peek;
1186 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1188 stream_sys_t *p_sys = s->p_sys;
1189 access_t *p_access = p_sys->p_access;
1197 msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1198 " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1199 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1200 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1201 p_sys->stream.i_offset,
1202 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1206 /* Seek in our current track ? */
1207 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1208 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1210 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1212 msg_Dbg( s, "AStreamSeekStream: current track" );
1214 p_sys->i_pos = i_pos;
1215 p_sys->stream.i_offset = i_pos - tk->i_start;
1217 /* If there is not enough data left in the track, refill */
1218 /* \todo How to get a correct value for
1219 * - refilling threshold
1220 * - how much to refill
1222 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1223 p_sys->stream.i_read_size )
1225 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1227 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1228 AStreamRefillStream( s );
1234 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1237 /* We can't do nothing */
1238 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1239 return VLC_EGENERIC;
1242 /* Date the current track */
1243 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1245 /* Try to reuse already read data */
1246 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1248 stream_track_t *tk = &p_sys->stream.tk[i];
1250 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1253 msg_Dbg( s, "AStreamSeekStream: reusing %d start=%"PRId64
1254 " end=%"PRId64, i, tk->i_start, tk->i_end );
1257 /* Seek at the end of the buffer */
1258 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1261 p_sys->i_pos = i_pos;
1262 p_sys->stream.i_tk = i;
1263 p_sys->stream.i_offset = i_pos - tk->i_start;
1265 if( p_sys->stream.i_used < STREAM_READ_ATONCE )
1266 p_sys->stream.i_used = STREAM_READ_ATONCE;
1268 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1269 return VLC_EGENERIC;
1275 access_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1276 /* FIXME compute seek cost (instead of static 'stupid' value) */
1277 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1283 /* Search closest segment TODO */
1284 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1286 stream_track_t *tk = &p_sys->stream.tk[i];
1288 if( i_pos + i_maxth >= tk->i_start )
1290 msg_Dbg( s, "good segment before current pos, TODO" );
1292 if( i_pos - i_maxth <= tk->i_end )
1294 msg_Dbg( s, "good segment after current pos, TODO" );
1299 /* Nothing good, seek and choose oldest segment */
1300 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1301 p_sys->i_pos = i_pos;
1304 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1306 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1310 /* Reset the segment */
1311 p_sys->stream.i_tk = i_new;
1312 p_sys->stream.i_offset = 0;
1313 p_sys->stream.tk[i_new].i_start = i_pos;
1314 p_sys->stream.tk[i_new].i_end = i_pos;
1317 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1318 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1320 if( AStreamRefillStream( s ) )
1321 return VLC_EGENERIC;
1326 static int AStreamRefillStream( stream_t *s )
1328 stream_sys_t *p_sys = s->p_sys;
1329 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1331 /* We read but won't increase i_start after initial start + offset */
1333 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1334 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1335 bool b_read = false;
1336 int64_t i_start, i_stop;
1338 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1341 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1342 p_sys->stream.i_used, i_toread );
1346 while( i_toread > 0 )
1348 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1352 return VLC_EGENERIC;
1354 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1355 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1357 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1362 else if( i_read == 0 )
1365 return VLC_EGENERIC;
1371 tk->i_end += i_read;
1373 /* Windows of STREAM_CACHE_TRACK_SIZE */
1374 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1376 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1378 tk->i_start += i_invalid;
1379 p_sys->stream.i_offset -= i_invalid;
1383 p_sys->stream.i_used -= i_read;
1385 p_sys->stat.i_bytes += i_read;
1386 p_sys->stat.i_read_count++;
1390 p_sys->stat.i_read_time += i_stop - i_start;
1395 static void AStreamPrebufferStream( stream_t *s )
1397 stream_sys_t *p_sys = s->p_sys;
1399 int64_t i_first = 0;
1402 msg_Dbg( s, "pre buffering" );
1406 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1408 int64_t i_date = mdate();
1411 if( s->b_die || tk->i_end >= STREAM_CACHE_PREBUFFER_SIZE )
1416 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1417 p_sys->stat.i_read_time = i_date - i_start;
1418 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1419 (p_sys->stat.i_read_time+1);
1421 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1422 "%"PRId64" kbytes/s",
1423 p_sys->stat.i_bytes,
1424 p_sys->stat.i_read_time / INT64_C(1000000),
1425 i_byterate / 1024 );
1430 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1431 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1432 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1435 else if( i_read == 0 )
1441 msg_Dbg( s, "received first data after %d ms",
1442 (int)((i_first-i_start)/1000) );
1445 tk->i_end += i_read;
1447 p_sys->stat.i_read_count++;
1451 /****************************************************************************
1453 ****************************************************************************/
1455 * Read from the stream untill first newline.
1456 * \param s Stream handle to read from
1457 * \return A pointer to the allocated output string. You need to free this when you are done.
1459 #define STREAM_PROBE_LINE 2048
1460 #define STREAM_LINE_MAX (2048*100)
1461 char *stream_ReadLine( stream_t *s )
1463 char *p_line = NULL;
1464 int i_line = 0, i_read = 0;
1466 while( i_read < STREAM_LINE_MAX )
1469 const uint8_t *p_data;
1473 /* Probe new data */
1474 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1475 if( i_data <= 0 ) break; /* No more data */
1478 i_pos = stream_Tell( s );
1479 if( i_pos == 0 && i_data >= 3 )
1482 const char *psz_encoding = NULL;
1484 if( !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
1486 psz_encoding = "UTF-8";
1489 else if( !memcmp( p_data, "\xFF\xFE", 2 ) )
1491 psz_encoding = "UTF-16LE";
1492 s->p_text->b_little_endian = true;
1493 s->p_text->i_char_width = 2;
1496 else if( !memcmp( p_data, "\xFE\xFF", 2 ) )
1498 psz_encoding = "UTF-16BE";
1499 s->p_text->i_char_width = 2;
1503 /* Seek past the BOM */
1506 stream_Seek( s, i_bom_size );
1507 p_data += i_bom_size;
1508 i_data -= i_bom_size;
1511 /* Open the converter if we need it */
1512 if( psz_encoding != NULL )
1514 msg_Dbg( s, "%s BOM detected", psz_encoding );
1515 if( s->p_text->i_char_width > 1 )
1517 s->p_text->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1518 if( s->p_text->conv == (vlc_iconv_t)-1 )
1520 msg_Err( s, "iconv_open failed" );
1524 /* FIXME that's UGLY */
1525 input_thread_t *p_input;
1526 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1527 if( p_input != NULL)
1529 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1530 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1531 vlc_object_release( p_input );
1536 if( i_data % s->p_text->i_char_width )
1538 /* keep i_char_width boundary */
1539 i_data = i_data - ( i_data % s->p_text->i_char_width );
1540 msg_Warn( s, "the read is not i_char_width compatible");
1546 /* Check if there is an EOL */
1547 if( s->p_text->i_char_width == 1 )
1549 /* UTF-8: 0A <LF> */
1550 psz_eol = memchr( p_data, '\n', i_data );
1554 const uint8_t *p = p_data;
1555 const uint8_t *p_last = p + i_data - s->p_text->i_char_width;
1557 if( s->p_text->i_char_width == 2 )
1559 if( s->p_text->b_little_endian == true)
1561 /* UTF-16LE: 0A 00 <LF> */
1562 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1567 /* UTF-16BE: 00 0A <LF> */
1568 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1579 psz_eol = (char *)p + ( s->p_text->i_char_width - 1 );
1585 i_data = (psz_eol - (char *)p_data) + 1;
1586 p_line = realloc( p_line, i_line + i_data + s->p_text->i_char_width ); /* add \0 */
1589 i_data = stream_Read( s, &p_line[i_line], i_data );
1590 if( i_data <= 0 ) break; /* Hmmm */
1591 i_line += i_data - s->p_text->i_char_width; /* skip \n */;
1594 /* We have our line */
1598 /* Read data (+1 for easy \0 append) */
1599 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->p_text->i_char_width );
1602 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1603 if( i_data <= 0 ) break; /* Hmmm */
1611 for( j = 0; j < s->p_text->i_char_width; j++ )
1613 p_line[i_line + j] = '\0';
1615 i_line += s->p_text->i_char_width; /* the added \0 */
1616 if( s->p_text->i_char_width > 1 )
1618 size_t i_in = 0, i_out = 0;
1619 const char * p_in = NULL;
1620 char * p_out = NULL;
1621 char * psz_new_line = NULL;
1624 psz_new_line = malloc( i_line );
1625 if( psz_new_line == NULL )
1627 i_in = i_out = (size_t)i_line;
1629 p_out = psz_new_line;
1631 if( vlc_iconv( s->p_text->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1633 msg_Err( s, "iconv failed" );
1634 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1637 p_line = psz_new_line;
1638 i_line = (size_t)i_line - i_out; /* does not include \0 */
1641 /* Remove trailing LF/CR */
1642 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1643 p_line[i_line-2] == '\n') ) i_line--;
1645 /* Make sure the \0 is there */
1646 p_line[i_line-1] = '\0';
1652 /* We failed to read any data, probably EOF */
1656 if( s->p_text->conv != (vlc_iconv_t)(-1) )
1657 vlc_iconv_close( s->p_text->conv );
1658 s->p_text->conv = (vlc_iconv_t)(-1);
1662 /****************************************************************************
1663 * Access reading/seeking wrappers to handle concatenated streams.
1664 ****************************************************************************/
1665 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read )
1667 stream_sys_t *p_sys = s->p_sys;
1668 access_t *p_access = p_sys->p_access;
1669 input_thread_t *p_input = NULL;
1670 int i_read_orig = i_read;
1673 if( s->p_parent && s->p_parent->p_parent &&
1674 vlc_internals( s->p_parent->p_parent )->i_object_type == VLC_OBJECT_INPUT )
1675 p_input = (input_thread_t *)s->p_parent->p_parent;
1677 if( !p_sys->i_list )
1679 i_read = p_access->pf_read( p_access, p_read, i_read );
1680 if( p_access->b_die )
1681 vlc_object_kill( s );
1684 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1685 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
1687 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1688 (float)i_total, NULL );
1689 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1690 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1695 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1697 if( p_access->b_die )
1698 vlc_object_kill( s );
1700 /* If we reached an EOF then switch to the next stream in the list */
1701 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1703 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1704 access_t *p_list_access;
1706 msg_Dbg( s, "opening input `%s'", psz_name );
1708 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1710 if( !p_list_access ) return 0;
1712 if( p_sys->p_list_access != p_access )
1713 access_Delete( p_sys->p_list_access );
1715 p_sys->p_list_access = p_list_access;
1717 /* We have to read some data */
1718 return AReadStream( s, p_read, i_read_orig );
1721 /* Update read bytes in input */
1724 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1725 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
1726 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1727 (float)i_total, NULL );
1728 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1729 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1734 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
1736 stream_sys_t *p_sys = s->p_sys;
1737 access_t *p_access = p_sys->p_access;
1738 input_thread_t *p_input = NULL;
1743 if( s->p_parent && s->p_parent->p_parent &&
1744 vlc_internals( s->p_parent->p_parent )->i_object_type == VLC_OBJECT_INPUT )
1745 p_input = (input_thread_t *)s->p_parent->p_parent;
1747 if( !p_sys->i_list )
1749 p_block = p_access->pf_block( p_access );
1750 if( p_access->b_die )
1751 vlc_object_kill( s );
1752 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1753 if( p_input && p_block && libvlc_stats (p_access) )
1755 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1756 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1757 p_block->i_buffer, &i_total );
1758 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1759 (float)i_total, NULL );
1760 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1761 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1766 p_block = p_sys->p_list_access->pf_block( p_sys->p_list_access );
1767 if( p_access->b_die )
1768 vlc_object_kill( s );
1769 b_eof = p_sys->p_list_access->info.b_eof;
1770 if( pb_eof ) *pb_eof = b_eof;
1772 /* If we reached an EOF then switch to the next stream in the list */
1773 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1775 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1776 access_t *p_list_access;
1778 msg_Dbg( s, "opening input `%s'", psz_name );
1780 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1782 if( !p_list_access ) return 0;
1784 if( p_sys->p_list_access != p_access )
1785 access_Delete( p_sys->p_list_access );
1787 p_sys->p_list_access = p_list_access;
1789 /* We have to read some data */
1790 return AReadBlock( s, pb_eof );
1796 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1797 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1798 p_block->i_buffer, &i_total );
1799 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1800 (float)i_total, NULL );
1801 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
1803 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1809 static int ASeek( stream_t *s, int64_t i_pos )
1811 stream_sys_t *p_sys = s->p_sys;
1812 access_t *p_access = p_sys->p_access;
1814 /* Check which stream we need to access */
1820 access_t *p_list_access = 0;
1822 for( i = 0; i < p_sys->i_list - 1; i++ )
1824 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1825 i_size += p_sys->list[i]->i_size;
1827 psz_name = p_sys->list[i]->psz_path;
1829 if( i != p_sys->i_list_index )
1830 msg_Dbg( s, "opening input `%s'", psz_name );
1832 if( i != p_sys->i_list_index && i != 0 )
1835 access_New( s, p_access->psz_access, "", psz_name );
1837 else if( i != p_sys->i_list_index )
1839 p_list_access = p_access;
1844 if( p_sys->p_list_access != p_access )
1845 access_Delete( p_sys->p_list_access );
1847 p_sys->p_list_access = p_list_access;
1850 p_sys->i_list_index = i;
1851 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1855 return p_access->pf_seek( p_access, i_pos );
1860 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
1861 * "p_read" is NULL then data are skipped instead of read. The return
1862 * value is the real numbers of bytes read/skip. If this value is less
1863 * than i_read that means that it's the end of the stream.
1865 int stream_Read( stream_t *s, void *p_read, int i_read )
1867 return s->pf_read( s, p_read, i_read );
1871 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
1872 * \return The real numbers of valid bytes, if it's less
1873 * or equal to 0, *pp_peek is invalid.
1874 * \note pp_peek is a pointer to internal buffer and it will be invalid as
1875 * soons as other stream_* functions are called.
1876 * \note Due to input limitation, it could be less than i_peek without meaning
1877 * the end of the stream (but only when you have i_peek >=
1878 * p_input->i_bufsize)
1880 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
1882 return s->pf_peek( s, pp_peek, i_peek );
1886 * Use to control the "stream_t *". Look at #stream_query_e for
1887 * possible "i_query" value and format arguments. Return VLC_SUCCESS
1888 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
1890 int stream_vaControl( stream_t *s, int i_query, va_list args )
1892 return s->pf_control( s, i_query, args );
1898 void stream_Delete( stream_t *s )
1903 int stream_Control( stream_t *s, int i_query, ... )
1909 return VLC_EGENERIC;
1911 va_start( args, i_query );
1912 i_result = s->pf_control( s, i_query, args );
1918 * Read "i_size" bytes and store them in a block_t.
1919 * It always read i_size bytes unless you are at the end of the stream
1920 * where it return what is available.
1922 block_t *stream_Block( stream_t *s, int i_size )
1924 if( i_size <= 0 ) return NULL;
1926 /* emulate block read */
1927 block_t *p_bk = block_New( s, i_size );
1930 int i_read = stream_Read( s, p_bk->p_buffer, i_size );
1933 p_bk->i_buffer = i_read;
1936 block_Release( p_bk );