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>
33 #include <vlc_memory.h>
40 #include "input_internal.h"
42 // #define STREAM_DEBUG 1
45 * - tune the 2 methods (block/stream)
46 * - compute cost for seek
47 * - improve stream mode seeking with closest segments
53 * One linked list of data read
55 * More complex scheme using mutliple track to avoid seeking
56 * - using directly the access (only indirection for peeking).
57 * This method is known to introduce much less latency.
58 * It should probably defaulted (instead of the stream method (2)).
61 /* How many tracks we have, currently only used for stream mode */
62 #ifdef OPTIMIZE_MEMORY
63 # define STREAM_CACHE_TRACK 1
64 /* Max size of our cache 128Ko per track */
65 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
67 # define STREAM_CACHE_TRACK 3
68 /* Max size of our cache 4Mo per track */
69 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
72 /* How many data we try to prebuffer
73 * XXX it should be small to avoid useless latency but big enough for
74 * efficient demux probing */
75 #define STREAM_CACHE_PREBUFFER_SIZE (128)
77 /* Method1: Simple, for pf_block.
78 * We get blocks and put them in the linked list.
79 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
82 /* Method2: A bit more complex, for pf_read
83 * - We use ring buffers, only one if unseekable, all if seekable
84 * - Upon seek date current ring, then search if one ring match the pos,
85 * yes: switch to it, seek the access to match the end of the ring
86 * no: search the ring with i_end the closer to i_pos,
87 * if close enough, read data and use this ring
88 * else use the oldest ring, seek and use it.
90 * TODO: - with access non seekable: use all space available for only one ring, but
91 * we have to support seekable/non-seekable switch on the fly.
92 * - compute a good value for i_read_size
95 #define STREAM_READ_ATONCE 1024
96 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
120 } stream_read_method_t;
126 stream_read_method_t method; /* method to use */
128 uint64_t i_pos; /* Current reading offset */
130 /* Method 1: pf_block */
133 uint64_t i_start; /* Offset of block for p_first */
134 uint64_t i_offset; /* Offset for data in p_current */
135 block_t *p_current; /* Current block */
137 uint64_t i_size; /* Total amount of data in the list */
143 /* Method 2: for pf_read */
146 unsigned i_offset; /* Buffer offset in the current track */
147 int i_tk; /* Current track */
148 stream_track_t tk[STREAM_CACHE_TRACK];
154 unsigned i_used; /* Used since last read */
155 unsigned i_read_size;
159 /* Peek temporary buffer */
163 /* Stat for both method */
166 bool b_fastseek; /* From access */
168 /* Stat about reading data */
169 uint64_t i_read_count;
171 uint64_t i_read_time;
173 /* Stat about seek */
174 unsigned i_seek_count;
175 uint64_t i_seek_time;
181 access_entry_t **list;
183 access_t *p_list_access;
187 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read );
188 static int AStreamPeekBlock( stream_t *s, const uint8_t **p_peek, unsigned int i_read );
189 static int AStreamSeekBlock( stream_t *s, uint64_t i_pos );
190 static void AStreamPrebufferBlock( stream_t *s );
191 static block_t *AReadBlock( stream_t *s, bool *pb_eof );
194 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read );
195 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read );
196 static int AStreamSeekStream( stream_t *s, uint64_t i_pos );
197 static void AStreamPrebufferStream( stream_t *s );
198 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read );
201 static int AStreamControl( stream_t *s, int i_query, va_list );
202 static void AStreamDestroy( stream_t *s );
203 static void UStreamDestroy( stream_t *s );
204 static int ASeek( stream_t *s, uint64_t i_pos );
206 /****************************************************************************
207 * stream_CommonNew: create an empty stream structure
208 ****************************************************************************/
209 stream_t *stream_CommonNew( vlc_object_t *p_obj )
211 stream_t *s = (stream_t *)vlc_custom_create( p_obj, sizeof(*s), "stream" );
216 s->p_text = malloc( sizeof(*s->p_text) );
219 vlc_object_release( s );
223 /* UTF16 and UTF32 text file conversion */
224 s->p_text->conv = (vlc_iconv_t)(-1);
225 s->p_text->i_char_width = 1;
226 s->p_text->b_little_endian = false;
231 void stream_CommonDelete( stream_t *s )
235 if( s->p_text->conv != (vlc_iconv_t)(-1) )
236 vlc_iconv_close( s->p_text->conv );
239 free( s->psz_access );
241 vlc_object_release( s );
245 /****************************************************************************
246 * stream_UrlNew: create a stream from a access
247 ****************************************************************************/
248 stream_t *stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
250 const char *psz_access, *psz_demux;
258 char psz_dup[strlen( psz_url ) + 1];
259 strcpy( psz_dup, psz_url );
260 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
262 /* Now try a real access */
263 p_access = access_New( p_parent, NULL, 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->p_input = p_access->p_input;
289 s->psz_access = strdup( p_access->psz_access );
290 s->psz_path = strdup( p_access->psz_location );
291 s->p_sys = p_sys = malloc( sizeof( *p_sys ) );
292 if( !s->psz_access || !s->psz_path || !s->p_sys )
294 stream_CommonDelete( s );
298 s->pf_read = NULL; /* Set up later */
300 s->pf_control = AStreamControl;
301 s->pf_destroy = AStreamDestroy;
304 p_sys->p_access = p_access;
305 if( p_access->pf_block )
306 p_sys->method = STREAM_METHOD_BLOCK;
308 p_sys->method = STREAM_METHOD_STREAM;
310 p_sys->i_pos = p_access->info.i_pos;
313 access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
314 p_sys->stat.i_bytes = 0;
315 p_sys->stat.i_read_time = 0;
316 p_sys->stat.i_read_count = 0;
317 p_sys->stat.i_seek_count = 0;
318 p_sys->stat.i_seek_time = 0;
320 TAB_INIT( p_sys->i_list, p_sys->list );
321 p_sys->i_list_index = 0;
322 p_sys->p_list_access = NULL;
324 /* Get the additional list of inputs if any (for concatenation) */
325 if( ppsz_list && ppsz_list[0] )
327 access_entry_t *p_entry = malloc( sizeof(*p_entry) );
331 p_entry->i_size = p_access->info.i_size;
332 p_entry->psz_path = strdup( p_access->psz_location );
333 if( !p_entry->psz_path )
338 p_sys->p_list_access = p_access;
339 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
340 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
341 p_entry->psz_path, p_access->info.i_size );
343 for( int i = 0; ppsz_list[i] != NULL; i++ )
345 char *psz_name = strdup( ppsz_list[i] );
350 access_t *p_tmp = access_New( p_access, p_access->p_input,
351 p_access->psz_access, "", psz_name );
355 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
356 psz_name, p_tmp->info.i_size );
358 p_entry = malloc( sizeof(*p_entry) );
361 p_entry->i_size = p_tmp->info.i_size;
362 p_entry->psz_path = psz_name;
363 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
365 access_Delete( p_tmp );
371 p_sys->p_peek = NULL;
373 if( p_sys->method == STREAM_METHOD_BLOCK )
375 msg_Dbg( s, "Using block method for AStream*" );
376 s->pf_read = AStreamReadBlock;
377 s->pf_peek = AStreamPeekBlock;
379 /* Init all fields of p_sys->block */
380 p_sys->block.i_start = p_sys->i_pos;
381 p_sys->block.i_offset = 0;
382 p_sys->block.p_current = NULL;
383 p_sys->block.i_size = 0;
384 p_sys->block.p_first = NULL;
385 p_sys->block.pp_last = &p_sys->block.p_first;
387 /* Do the prebuffering */
388 AStreamPrebufferBlock( s );
390 if( p_sys->block.i_size <= 0 )
392 msg_Err( s, "cannot pre fill buffer" );
400 assert( p_sys->method == STREAM_METHOD_STREAM );
402 msg_Dbg( s, "Using stream method for AStream*" );
404 s->pf_read = AStreamReadStream;
405 s->pf_peek = AStreamPeekStream;
407 /* Allocate/Setup our tracks */
408 p_sys->stream.i_offset = 0;
409 p_sys->stream.i_tk = 0;
410 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
411 if( p_sys->stream.p_buffer == NULL )
413 p_sys->stream.i_used = 0;
414 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
415 #if STREAM_READ_ATONCE < 256
416 # error "Invalid STREAM_READ_ATONCE value"
419 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
421 p_sys->stream.tk[i].i_date = 0;
422 p_sys->stream.tk[i].i_start = p_sys->i_pos;
423 p_sys->stream.tk[i].i_end = p_sys->i_pos;
424 p_sys->stream.tk[i].p_buffer=
425 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
428 /* Do the prebuffering */
429 AStreamPrebufferStream( s );
431 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
433 msg_Err( s, "cannot pre fill buffer" );
441 if( p_sys->method == STREAM_METHOD_BLOCK )
447 free( p_sys->stream.p_buffer );
449 while( p_sys->i_list > 0 )
450 free( p_sys->list[--(p_sys->i_list)] );
453 stream_CommonDelete( s );
457 /****************************************************************************
459 ****************************************************************************/
460 static void AStreamDestroy( stream_t *s )
462 stream_sys_t *p_sys = s->p_sys;
464 if( p_sys->method == STREAM_METHOD_BLOCK )
465 block_ChainRelease( p_sys->block.p_first );
467 free( p_sys->stream.p_buffer );
469 free( p_sys->p_peek );
471 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
472 access_Delete( p_sys->p_list_access );
474 while( p_sys->i_list-- )
476 free( p_sys->list[p_sys->i_list]->psz_path );
477 free( p_sys->list[p_sys->i_list] );
483 stream_CommonDelete( s );
486 static void UStreamDestroy( stream_t *s )
488 access_t *p_access = (access_t *)s->p_parent;
490 access_Delete( p_access );
493 /****************************************************************************
494 * AStreamControlReset:
495 ****************************************************************************/
496 static void AStreamControlReset( stream_t *s )
498 stream_sys_t *p_sys = s->p_sys;
500 p_sys->i_pos = p_sys->p_access->info.i_pos;
502 if( p_sys->method == STREAM_METHOD_BLOCK )
504 block_ChainRelease( p_sys->block.p_first );
506 /* Init all fields of p_sys->block */
507 p_sys->block.i_start = p_sys->i_pos;
508 p_sys->block.i_offset = 0;
509 p_sys->block.p_current = NULL;
510 p_sys->block.i_size = 0;
511 p_sys->block.p_first = NULL;
512 p_sys->block.pp_last = &p_sys->block.p_first;
514 /* Do the prebuffering */
515 AStreamPrebufferBlock( s );
521 assert( p_sys->method == STREAM_METHOD_STREAM );
523 /* Setup our tracks */
524 p_sys->stream.i_offset = 0;
525 p_sys->stream.i_tk = 0;
526 p_sys->stream.i_used = 0;
528 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
530 p_sys->stream.tk[i].i_date = 0;
531 p_sys->stream.tk[i].i_start = p_sys->i_pos;
532 p_sys->stream.tk[i].i_end = p_sys->i_pos;
535 /* Do the prebuffering */
536 AStreamPrebufferStream( s );
540 /****************************************************************************
541 * AStreamControlUpdate:
542 ****************************************************************************/
543 static void AStreamControlUpdate( stream_t *s )
545 stream_sys_t *p_sys = s->p_sys;
547 p_sys->i_pos = p_sys->p_access->info.i_pos;
552 for( i = 0; i < p_sys->i_list_index; i++ )
554 p_sys->i_pos += p_sys->list[i]->i_size;
559 /****************************************************************************
561 ****************************************************************************/
562 static int AStreamControl( stream_t *s, int i_query, va_list args )
564 stream_sys_t *p_sys = s->p_sys;
565 access_t *p_access = p_sys->p_access;
568 uint64_t *pi_64, i_64;
573 case STREAM_GET_SIZE:
574 pi_64 = va_arg( args, uint64_t * );
575 if( s->p_sys->i_list )
579 for( i = 0; i < s->p_sys->i_list; i++ )
580 *pi_64 += s->p_sys->list[i]->i_size;
583 *pi_64 = p_access->info.i_size;
586 case STREAM_CAN_SEEK:
587 p_bool = (bool*)va_arg( args, bool * );
588 access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
591 case STREAM_CAN_FASTSEEK:
592 p_bool = (bool*)va_arg( args, bool * );
593 access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
596 case STREAM_GET_POSITION:
597 pi_64 = va_arg( args, uint64_t * );
598 *pi_64 = p_sys->i_pos;
601 case STREAM_SET_POSITION:
602 i_64 = va_arg( args, uint64_t );
603 switch( p_sys->method )
605 case STREAM_METHOD_BLOCK:
606 return AStreamSeekBlock( s, i_64 );
607 case STREAM_METHOD_STREAM:
608 return AStreamSeekStream( s, i_64 );
614 case STREAM_CONTROL_ACCESS:
616 i_int = (int) va_arg( args, int );
617 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
618 i_int != ACCESS_SET_PRIVATE_ID_CA &&
619 i_int != ACCESS_GET_PRIVATE_ID_STATE &&
620 i_int != ACCESS_SET_TITLE &&
621 i_int != ACCESS_SET_SEEKPOINT )
623 msg_Err( s, "Hey, what are you thinking ?"
624 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
627 int i_ret = access_vaControl( p_access, i_int, args );
628 if( i_int == ACCESS_SET_TITLE || i_int == ACCESS_SET_SEEKPOINT )
629 AStreamControlReset( s );
633 case STREAM_UPDATE_SIZE:
634 AStreamControlUpdate( s );
637 case STREAM_GET_CONTENT_TYPE:
638 return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
639 va_arg( args, char ** ) );
640 case STREAM_SET_RECORD_STATE:
642 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
648 /****************************************************************************
650 ****************************************************************************/
651 static void AStreamPrebufferBlock( stream_t *s )
653 stream_sys_t *p_sys = s->p_sys;
658 msg_Dbg( s, "starting pre-buffering" );
662 const int64_t i_date = mdate();
666 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE )
671 p_sys->stat.i_bytes = p_sys->block.i_size;
672 p_sys->stat.i_read_time = i_date - i_start;
673 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
674 (p_sys->stat.i_read_time + 1);
676 msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
679 p_sys->stat.i_read_time / INT64_C(1000000),
685 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
694 /* Append the block */
695 p_sys->block.i_size += b->i_buffer;
696 *p_sys->block.pp_last = b;
697 p_sys->block.pp_last = &b->p_next;
699 p_sys->stat.i_read_count++;
706 msg_Dbg( s, "received first data after %d ms",
707 (int)((i_first-i_start)/1000) );
711 p_sys->block.p_current = p_sys->block.p_first;
714 static int AStreamRefillBlock( stream_t *s );
716 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
718 stream_sys_t *p_sys = s->p_sys;
720 uint8_t *p_data = p_read;
721 unsigned int i_data = 0;
724 if( p_sys->block.p_current == NULL )
729 /* seek within this stream if possible, else use plain old read and discard */
730 stream_sys_t *p_sys = s->p_sys;
731 access_t *p_access = p_sys->p_access;
733 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
735 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
738 while( i_data < i_read )
741 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
742 unsigned int i_copy = __MIN( (unsigned int)__MAX(i_current,0), i_read - i_data);
748 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
754 p_sys->block.i_offset += i_copy;
755 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
757 /* Current block is now empty, switch to next */
758 if( p_sys->block.p_current )
760 p_sys->block.i_offset = 0;
761 p_sys->block.p_current = p_sys->block.p_current->p_next;
763 /*Get a new block if needed */
764 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
771 p_sys->i_pos += i_data;
775 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
777 stream_sys_t *p_sys = s->p_sys;
779 unsigned int i_data = 0;
781 unsigned int i_offset;
783 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
785 /* We can directly give a pointer over our buffer */
786 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
788 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
792 /* We need to create a local copy */
793 if( p_sys->i_peek < i_read )
795 p_sys->p_peek = realloc_or_free( p_sys->p_peek, i_read );
801 p_sys->i_peek = i_read;
804 /* Fill enough data */
805 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
808 block_t **pp_last = p_sys->block.pp_last;
810 if( AStreamRefillBlock( s ) ) break;
812 /* Our buffer are probably filled enough, don't try anymore */
813 if( pp_last == p_sys->block.pp_last ) break;
816 /* Copy what we have */
817 b = p_sys->block.p_current;
818 i_offset = p_sys->block.i_offset;
819 p_data = p_sys->p_peek;
821 while( b && i_data < i_read )
823 unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
824 int i_copy = __MIN( i_current, i_read - i_data );
826 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
831 if( i_offset >= b->i_buffer )
838 *pp_peek = p_sys->p_peek;
842 static int AStreamSeekBlock( stream_t *s, uint64_t i_pos )
844 stream_sys_t *p_sys = s->p_sys;
845 access_t *p_access = p_sys->p_access;
846 int64_t i_offset = i_pos - p_sys->block.i_start;
849 /* We already have thoses data, just update p_current/i_offset */
850 if( i_offset >= 0 && (uint64_t)i_offset < p_sys->block.i_size )
852 block_t *b = p_sys->block.p_first;
855 while( i_current + b->i_buffer < (uint64_t)i_offset )
857 i_current += b->i_buffer;
861 p_sys->block.p_current = b;
862 p_sys->block.i_offset = i_offset - i_current;
864 p_sys->i_pos = i_pos;
869 /* We may need to seek or to read data */
873 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
877 msg_Err( s, "backward seeking impossible (access not seekable)" );
885 bool b_aseek, b_aseekfast;
887 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
888 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
893 msg_Warn( s, "%"PRId64" bytes need to be skipped "
894 "(access non seekable)",
895 i_offset - p_sys->block.i_size );
899 int64_t i_skip = i_offset - p_sys->block.i_size;
901 /* Avg bytes per packets */
902 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
903 /* TODO compute a seek cost instead of fixed threshold */
904 int i_th = b_aseekfast ? 1 : 5;
906 if( i_skip <= i_th * i_avg &&
907 i_skip < STREAM_CACHE_SIZE )
912 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
913 b_seek, i_th*i_avg, i_skip );
919 int64_t i_start, i_end;
920 /* Do the access seek */
922 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
926 block_ChainRelease( p_sys->block.p_first );
929 p_sys->block.i_start = p_sys->i_pos = i_pos;
930 p_sys->block.i_offset = 0;
931 p_sys->block.p_current = NULL;
932 p_sys->block.i_size = 0;
933 p_sys->block.p_first = NULL;
934 p_sys->block.pp_last = &p_sys->block.p_first;
937 if( AStreamRefillBlock( s ) )
941 p_sys->stat.i_seek_time += i_end - i_start;
942 p_sys->stat.i_seek_count++;
949 while( p_sys->block.p_current &&
950 p_sys->i_pos + p_sys->block.p_current->i_buffer - p_sys->block.i_offset <= i_pos )
952 p_sys->i_pos += p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
953 p_sys->block.p_current = p_sys->block.p_current->p_next;
954 p_sys->block.i_offset = 0;
956 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
958 if( p_sys->i_pos != i_pos )
962 while( p_sys->block.i_start + p_sys->block.i_size < i_pos );
964 p_sys->block.i_offset += i_pos - p_sys->i_pos;
965 p_sys->i_pos = i_pos;
973 static int AStreamRefillBlock( stream_t *s )
975 stream_sys_t *p_sys = s->p_sys;
979 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
980 p_sys->block.p_first != p_sys->block.p_current )
982 block_t *b = p_sys->block.p_first;
984 p_sys->block.i_start += b->i_buffer;
985 p_sys->block.i_size -= b->i_buffer;
986 p_sys->block.p_first = b->p_next;
990 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
991 p_sys->block.p_current == p_sys->block.p_first &&
992 p_sys->block.p_current->p_next ) /* At least 2 packets */
994 /* Enough data, don't read more */
998 /* Now read a new block */
999 const int64_t i_start = mdate();
1005 return VLC_EGENERIC;
1008 if( ( b = AReadBlock( s, &b_eof ) ) )
1011 return VLC_EGENERIC;
1014 p_sys->stat.i_read_time += mdate() - i_start;
1017 /* Append the block */
1018 p_sys->block.i_size += b->i_buffer;
1019 *p_sys->block.pp_last = b;
1020 p_sys->block.pp_last = &b->p_next;
1023 if( p_sys->block.p_current == NULL )
1024 p_sys->block.p_current = b;
1027 p_sys->stat.i_bytes += b->i_buffer;
1028 p_sys->stat.i_read_count++;
1036 /****************************************************************************
1038 ****************************************************************************/
1039 static int AStreamRefillStream( stream_t *s );
1040 static int AStreamReadNoSeekStream( stream_t *s, void *p_read, unsigned int i_read );
1042 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read )
1044 stream_sys_t *p_sys = s->p_sys;
1048 const uint64_t i_pos_wanted = p_sys->i_pos + i_read;
1050 if( AStreamSeekStream( s, i_pos_wanted ) )
1052 if( p_sys->i_pos != i_pos_wanted )
1057 return AStreamReadNoSeekStream( s, p_read, i_read );
1060 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1062 stream_sys_t *p_sys = s->p_sys;
1063 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1066 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1069 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1070 "start=%"PRId64" offset=%d end=%"PRId64,
1071 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1072 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1075 /* Avoid problem, but that should *never* happen */
1076 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1077 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1079 while( tk->i_end < tk->i_start + p_sys->stream.i_offset + i_read )
1081 if( p_sys->stream.i_used <= 1 )
1083 /* Be sure we will read something */
1084 p_sys->stream.i_used += tk->i_start + p_sys->stream.i_offset + i_read - tk->i_end;
1086 if( AStreamRefillStream( s ) ) break;
1089 if( tk->i_end < tk->i_start + p_sys->stream.i_offset + i_read )
1091 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1095 /* Now, direct pointer or a copy ? */
1096 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1097 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1099 *pp_peek = &tk->p_buffer[i_off];
1103 if( p_sys->i_peek < i_read )
1105 p_sys->p_peek = realloc_or_free( p_sys->p_peek, i_read );
1106 if( !p_sys->p_peek )
1111 p_sys->i_peek = i_read;
1114 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1115 STREAM_CACHE_TRACK_SIZE - i_off );
1116 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1117 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1119 *pp_peek = p_sys->p_peek;
1123 static int AStreamSeekStream( stream_t *s, uint64_t i_pos )
1125 stream_sys_t *p_sys = s->p_sys;
1127 stream_track_t *p_current = &p_sys->stream.tk[p_sys->stream.i_tk];
1128 access_t *p_access = p_sys->p_access;
1130 if( p_current->i_start >= p_current->i_end && i_pos >= p_current->i_end )
1134 msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1135 " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1136 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1138 p_sys->stream.i_offset,
1143 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1144 if( !b_aseek && i_pos < p_current->i_start )
1146 msg_Warn( s, "AStreamSeekStream: can't seek" );
1147 return VLC_EGENERIC;
1151 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_afastseek );
1153 /* FIXME compute seek cost (instead of static 'stupid' value) */
1154 uint64_t i_skip_threshold;
1156 i_skip_threshold = b_afastseek ? 128 : 3*p_sys->stream.i_read_size;
1158 i_skip_threshold = INT64_MAX;
1160 /* Date the current track */
1161 p_current->i_date = mdate();
1163 /* Search a new track slot */
1164 stream_track_t *tk = NULL;
1167 /* Prefer the current track */
1168 if( p_current->i_start <= i_pos && i_pos <= p_current->i_end + i_skip_threshold )
1171 i_tk_idx = p_sys->stream.i_tk;
1175 /* Try to maximize already read data */
1176 for( int i = 0; i < STREAM_CACHE_TRACK; i++ )
1178 stream_track_t *t = &p_sys->stream.tk[i];
1180 if( t->i_start > i_pos || i_pos > t->i_end )
1183 if( !tk || tk->i_end < t->i_end )
1192 /* Use the oldest unused */
1193 for( int i = 0; i < STREAM_CACHE_TRACK; i++ )
1195 stream_track_t *t = &p_sys->stream.tk[i];
1197 if( !tk || tk->i_date > t->i_date )
1204 assert( i_tk_idx >= 0 && i_tk_idx < STREAM_CACHE_TRACK );
1206 if( tk != p_current )
1207 i_skip_threshold = 0;
1208 if( tk->i_start <= i_pos && i_pos <= tk->i_end + i_skip_threshold )
1211 msg_Err( s, "AStreamSeekStream: reusing %d start=%"PRId64
1212 " end=%"PRId64"(%s)",
1213 i_tk_idx, tk->i_start, tk->i_end,
1214 tk != p_current ? "seek" : i_pos > tk->i_end ? "skip" : "noseek" );
1216 if( tk != p_current )
1220 /* Seek at the end of the buffer
1221 * TODO it is stupid to seek now, it would be better to delay it
1223 if( ASeek( s, tk->i_end ) )
1224 return VLC_EGENERIC;
1226 else if( i_pos > tk->i_end )
1228 uint64_t i_skip = i_pos - tk->i_end;
1231 const int i_read_max = __MIN( 10 * STREAM_READ_ATONCE, i_skip );
1232 if( AStreamReadNoSeekStream( s, NULL, i_read_max ) != i_read_max )
1233 return VLC_EGENERIC;
1234 i_skip -= i_read_max;
1241 msg_Err( s, "AStreamSeekStream: hard seek" );
1243 /* Nothing good, seek and choose oldest segment */
1244 if( ASeek( s, i_pos ) )
1245 return VLC_EGENERIC;
1247 tk->i_start = i_pos;
1250 p_sys->stream.i_offset = i_pos - tk->i_start;
1251 p_sys->stream.i_tk = i_tk_idx;
1252 p_sys->i_pos = i_pos;
1254 /* If there is not enough data left in the track, refill */
1255 /* TODO How to get a correct value for
1256 * - refilling threshold
1257 * - how much to refill
1259 if( tk->i_end < tk->i_start + p_sys->stream.i_offset + p_sys->stream.i_read_size )
1261 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1262 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1264 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1265 return VLC_EGENERIC;
1270 static int AStreamReadNoSeekStream( stream_t *s, void *p_read, unsigned int i_read )
1272 stream_sys_t *p_sys = s->p_sys;
1273 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1275 uint8_t *p_data = (uint8_t *)p_read;
1276 unsigned int i_data = 0;
1278 if( tk->i_start >= tk->i_end )
1282 msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1283 " offset=%d end=%"PRId64,
1284 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1285 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1288 while( i_data < i_read )
1290 unsigned i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1291 unsigned int i_current =
1292 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1293 STREAM_CACHE_TRACK_SIZE - i_off );
1294 int i_copy = __MIN( i_current, i_read - i_data );
1296 if( i_copy <= 0 ) break; /* EOF */
1299 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1302 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1306 p_sys->stream.i_offset += i_copy;
1308 /* Update pos now */
1309 p_sys->i_pos += i_copy;
1312 p_sys->stream.i_used += i_copy;
1314 if( tk->i_end + i_data <= tk->i_start + p_sys->stream.i_offset + i_read )
1316 const unsigned i_read_requested = __MAX( __MIN( i_read - i_data,
1317 STREAM_READ_ATONCE * 10 ),
1318 STREAM_READ_ATONCE / 2 );
1320 if( p_sys->stream.i_used < i_read_requested )
1321 p_sys->stream.i_used = i_read_requested;
1323 if( AStreamRefillStream( s ) )
1326 if( tk->i_start >= tk->i_end ) break;
1335 static int AStreamRefillStream( stream_t *s )
1337 stream_sys_t *p_sys = s->p_sys;
1338 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1340 /* We read but won't increase i_start after initial start + offset */
1342 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1343 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1344 bool b_read = false;
1345 int64_t i_start, i_stop;
1347 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1350 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1351 p_sys->stream.i_used, i_toread );
1355 while( i_toread > 0 )
1357 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1361 return VLC_EGENERIC;
1363 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1364 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1366 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1371 else if( i_read == 0 )
1374 return VLC_EGENERIC;
1380 tk->i_end += i_read;
1382 /* Windows of STREAM_CACHE_TRACK_SIZE */
1383 if( tk->i_start + STREAM_CACHE_TRACK_SIZE < tk->i_end )
1385 unsigned i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1387 tk->i_start += i_invalid;
1388 p_sys->stream.i_offset -= i_invalid;
1392 p_sys->stream.i_used -= i_read;
1394 p_sys->stat.i_bytes += i_read;
1395 p_sys->stat.i_read_count++;
1399 p_sys->stat.i_read_time += i_stop - i_start;
1404 static void AStreamPrebufferStream( stream_t *s )
1406 stream_sys_t *p_sys = s->p_sys;
1408 int64_t i_first = 0;
1411 msg_Dbg( s, "starting pre-buffering" );
1415 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1417 int64_t i_date = mdate();
1419 int i_buffered = tk->i_end - tk->i_start;
1421 if( s->b_die || i_buffered >= STREAM_CACHE_PREBUFFER_SIZE )
1426 p_sys->stat.i_bytes = i_buffered;
1427 p_sys->stat.i_read_time = i_date - i_start;
1428 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1429 (p_sys->stat.i_read_time+1);
1431 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1433 p_sys->stat.i_bytes,
1434 p_sys->stat.i_read_time / INT64_C(1000000),
1435 i_byterate / 1024 );
1440 i_read = STREAM_CACHE_TRACK_SIZE - i_buffered;
1441 i_read = __MIN( (int)p_sys->stream.i_read_size, i_read );
1442 i_read = AReadStream( s, &tk->p_buffer[i_buffered], i_read );
1445 else if( i_read == 0 )
1451 msg_Dbg( s, "received first data after %d ms",
1452 (int)((i_first-i_start)/1000) );
1455 tk->i_end += i_read;
1457 p_sys->stat.i_read_count++;
1461 /****************************************************************************
1463 ****************************************************************************/
1465 * Read from the stream untill first newline.
1466 * \param s Stream handle to read from
1467 * \return A pointer to the allocated output string. You need to free this when you are done.
1469 #define STREAM_PROBE_LINE 2048
1470 #define STREAM_LINE_MAX (2048*100)
1471 char *stream_ReadLine( stream_t *s )
1473 char *p_line = NULL;
1474 int i_line = 0, i_read = 0;
1476 while( i_read < STREAM_LINE_MAX )
1479 const uint8_t *p_data;
1483 /* Probe new data */
1484 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1485 if( i_data <= 0 ) break; /* No more data */
1488 i_pos = stream_Tell( s );
1489 if( i_pos == 0 && i_data >= 3 )
1492 const char *psz_encoding = NULL;
1494 if( !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
1496 psz_encoding = "UTF-8";
1499 else if( !memcmp( p_data, "\xFF\xFE", 2 ) )
1501 psz_encoding = "UTF-16LE";
1502 s->p_text->b_little_endian = true;
1503 s->p_text->i_char_width = 2;
1506 else if( !memcmp( p_data, "\xFE\xFF", 2 ) )
1508 psz_encoding = "UTF-16BE";
1509 s->p_text->i_char_width = 2;
1513 /* Seek past the BOM */
1516 stream_Seek( s, i_bom_size );
1517 p_data += i_bom_size;
1518 i_data -= i_bom_size;
1521 /* Open the converter if we need it */
1522 if( psz_encoding != NULL )
1524 msg_Dbg( s, "%s BOM detected", psz_encoding );
1525 if( s->p_text->i_char_width > 1 )
1527 s->p_text->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1528 if( s->p_text->conv == (vlc_iconv_t)-1 )
1530 msg_Err( s, "iconv_open failed" );
1534 /* FIXME that's UGLY */
1535 input_thread_t *p_input = s->p_input;
1536 if( p_input != NULL)
1538 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1539 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1544 if( i_data % s->p_text->i_char_width )
1546 /* keep i_char_width boundary */
1547 i_data = i_data - ( i_data % s->p_text->i_char_width );
1548 msg_Warn( s, "the read is not i_char_width compatible");
1554 /* Check if there is an EOL */
1555 if( s->p_text->i_char_width == 1 )
1557 /* UTF-8: 0A <LF> */
1558 psz_eol = memchr( p_data, '\n', i_data );
1559 if( psz_eol == NULL )
1560 /* UTF-8: 0D <CR> */
1561 psz_eol = memchr( p_data, '\r', i_data );
1565 const uint8_t *p_last = p_data + i_data - s->p_text->i_char_width;
1566 uint16_t eol = s->p_text->b_little_endian ? 0x0A00 : 0x00A0;
1568 assert( s->p_text->i_char_width == 2 );
1570 /* UTF-16: 000A <LF> */
1571 for( const uint8_t *p = p_data; p <= p_last; p += 2 )
1573 if( U16_AT( p ) == eol )
1575 psz_eol = (char *)p + 1;
1580 if( psz_eol == NULL )
1581 { /* UTF-16: 000D <CR> */
1582 eol = s->p_text->b_little_endian ? 0x0D00 : 0x00D0;
1583 for( const uint8_t *p = p_data; p <= p_last; p += 2 )
1585 if( U16_AT( p ) == eol )
1587 psz_eol = (char *)p + 1;
1596 i_data = (psz_eol - (char *)p_data) + 1;
1597 p_line = realloc_or_free( p_line,
1598 i_line + i_data + s->p_text->i_char_width ); /* add \0 */
1601 i_data = stream_Read( s, &p_line[i_line], i_data );
1602 if( i_data <= 0 ) break; /* Hmmm */
1603 i_line += i_data - s->p_text->i_char_width; /* skip \n */;
1606 /* We have our line */
1610 /* Read data (+1 for easy \0 append) */
1611 p_line = realloc_or_free( p_line,
1612 i_line + STREAM_PROBE_LINE + s->p_text->i_char_width );
1615 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1616 if( i_data <= 0 ) break; /* Hmmm */
1624 for( j = 0; j < s->p_text->i_char_width; j++ )
1626 p_line[i_line + j] = '\0';
1628 i_line += s->p_text->i_char_width; /* the added \0 */
1629 if( s->p_text->i_char_width > 1 )
1631 size_t i_in = 0, i_out = 0;
1632 const char * p_in = NULL;
1633 char * p_out = NULL;
1634 char * psz_new_line = NULL;
1637 psz_new_line = malloc( i_line );
1638 if( psz_new_line == NULL )
1640 i_in = i_out = (size_t)i_line;
1642 p_out = psz_new_line;
1644 if( vlc_iconv( s->p_text->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1646 msg_Err( s, "iconv failed" );
1647 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1650 p_line = psz_new_line;
1651 i_line = (size_t)i_line - i_out; /* does not include \0 */
1654 /* Remove trailing LF/CR */
1655 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1656 p_line[i_line-2] == '\n') ) i_line--;
1658 /* Make sure the \0 is there */
1659 p_line[i_line-1] = '\0';
1665 /* We failed to read any data, probably EOF */
1669 if( s->p_text->conv != (vlc_iconv_t)(-1) )
1670 vlc_iconv_close( s->p_text->conv );
1671 s->p_text->conv = (vlc_iconv_t)(-1);
1675 /****************************************************************************
1676 * Access reading/seeking wrappers to handle concatenated streams.
1677 ****************************************************************************/
1678 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read )
1680 stream_sys_t *p_sys = s->p_sys;
1681 access_t *p_access = p_sys->p_access;
1682 input_thread_t *p_input = s->p_input;
1683 int i_read_orig = i_read;
1686 if( !p_sys->i_list )
1688 i_read = p_access->pf_read( p_access, p_read, i_read );
1689 if( p_access->b_die )
1690 vlc_object_kill( s );
1693 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1694 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
1696 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1697 (float)i_total, NULL );
1698 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1699 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1704 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1706 if( p_access->b_die )
1707 vlc_object_kill( s );
1709 /* If we reached an EOF then switch to the next stream in the list */
1710 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1712 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1713 access_t *p_list_access;
1715 msg_Dbg( s, "opening input `%s'", psz_name );
1717 p_list_access = access_New( s, s->p_input, p_access->psz_access, "", psz_name );
1719 if( !p_list_access ) return 0;
1721 if( p_sys->p_list_access != p_access )
1722 access_Delete( p_sys->p_list_access );
1724 p_sys->p_list_access = p_list_access;
1726 /* We have to read some data */
1727 return AReadStream( s, p_read, i_read_orig );
1730 /* Update read bytes in input */
1733 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1734 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
1735 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1736 (float)i_total, NULL );
1737 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1738 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1743 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
1745 stream_sys_t *p_sys = s->p_sys;
1746 access_t *p_access = p_sys->p_access;
1747 input_thread_t *p_input = s->p_input;
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, s->p_input, 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, uint64_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, s->p_input, 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 );