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 *****************************************************************************/
26 #include "input_internal.h"
31 * - tune the 2 methods
32 * - compute cost for seek
33 * - improve stream mode seeking with closest segments
39 * One linked list of data read
41 * More complex scheme using mutliple track to avoid seeking
44 /* How many tracks we have, currently only used for stream mode */
45 #ifdef OPTIMIZE_MEMORY
46 # define STREAM_CACHE_TRACK 1
47 /* Max size of our cache 128Ko per track */
48 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
50 # define STREAM_CACHE_TRACK 3
51 /* Max size of our cache 4Mo per track */
52 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
55 /* How many data we try to prebuffer */
56 #define STREAM_CACHE_PREBUFFER_SIZE (32767)
57 /* Maximum time we take to pre-buffer */
58 #define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
60 /* Method1: Simple, for pf_block.
61 * We get blocks and put them in the linked list.
62 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
64 #define STREAM_DATA_WAIT 40000 /* Time between before a pf_block retry */
66 /* Method2: A bit more complex, for pf_read
67 * - We use ring buffers, only one if unseekable, all if seekable
68 * - Upon seek date current ring, then search if one ring match the pos,
69 * yes: switch to it, seek the access to match the end of the ring
70 * no: search the ring with i_end the closer to i_pos,
71 * if close enough, read data and use this ring
72 * else use the oldest ring, seek and use it.
74 * TODO: - with access non seekable: use all space available for only one ring, but
75 * we have to support seekable/non-seekable switch on the fly.
76 * - compute a good value for i_read_size
79 #define STREAM_READ_ATONCE 32767
80 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
104 vlc_bool_t b_block; /* Block method (1) or stream */
106 int64_t i_pos; /* Current reading offset */
108 /* Method 1: pf_block */
111 int64_t i_start; /* Offset of block for p_first */
112 int i_offset; /* Offset for data in p_current */
113 block_t *p_current; /* Current block */
115 int i_size; /* Total amount of data in the list */
121 /* Method 2: for pf_read */
124 int i_offset; /* Buffer offset in the current track */
125 int i_tk; /* Current track */
126 stream_track_t tk[STREAM_CACHE_TRACK];
132 int i_used; /* Used since last read */
137 /* Peek temporary buffer */
141 /* Stat for both method */
144 vlc_bool_t b_fastseek; /* From access */
146 /* Stat about reading data */
147 int64_t i_read_count;
151 /* Stat about seek */
159 access_entry_t **list;
161 access_t *p_list_access;
163 /* Preparse mode ? */
168 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read );
169 static int AStreamPeekBlock( stream_t *s, const uint8_t **p_peek, int i_read );
170 static int AStreamSeekBlock( stream_t *s, int64_t i_pos );
171 static void AStreamPrebufferBlock( stream_t *s );
172 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof );
175 static int AStreamReadStream( stream_t *s, void *p_read, int i_read );
176 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, int i_read );
177 static int AStreamSeekStream( stream_t *s, int64_t i_pos );
178 static void AStreamPrebufferStream( stream_t *s );
179 static int AReadStream( stream_t *s, void *p_read, int i_read );
182 static int AStreamControl( stream_t *s, int i_query, va_list );
183 static void AStreamDestroy( stream_t *s );
184 static void UStreamDestroy( stream_t *s );
185 static int ASeek( stream_t *s, int64_t i_pos );
188 /****************************************************************************
189 * stream_UrlNew: create a stream from a access
190 ****************************************************************************/
191 stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
193 const char *psz_access, *psz_demux;
198 if( !psz_url ) return 0;
200 char psz_dup[strlen (psz_url) + 1];
201 strcpy (psz_dup, psz_url);;
202 MRLSplit( p_parent, psz_dup, &psz_access, &psz_demux, &psz_path );
204 /* Now try a real access */
205 p_access = access2_New( p_parent, psz_access, psz_demux, psz_path, 0 );
207 if( p_access == NULL )
209 msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
213 if( !( p_res = stream_AccessNew( p_access, VLC_TRUE ) ) )
215 access2_Delete( p_access );
219 p_res->pf_destroy = UStreamDestroy;
223 stream_t *stream_AccessNew( access_t *p_access, vlc_bool_t b_quick )
225 stream_t *s = vlc_stream_create( VLC_OBJECT(p_access) );
229 if( !s ) return NULL;
231 /* Attach it now, needed for b_die */
232 vlc_object_attach( s, p_access );
234 s->pf_read = NULL; /* Set up later */
236 s->pf_control = AStreamControl;
237 s->pf_destroy = AStreamDestroy;
239 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
241 /* UTF16 and UTF32 text file conversion */
243 s->b_little_endian = VLC_FALSE;
244 s->conv = (vlc_iconv_t)(-1);
247 p_sys->p_access = p_access;
248 p_sys->b_block = p_access->pf_block ? VLC_TRUE : VLC_FALSE;
249 p_sys->i_pos = p_access->info.i_pos;
252 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
253 p_sys->stat.i_bytes = 0;
254 p_sys->stat.i_read_time = 0;
255 p_sys->stat.i_read_count = 0;
256 p_sys->stat.i_seek_count = 0;
257 p_sys->stat.i_seek_time = 0;
261 p_sys->i_list_index = 0;
262 p_sys->p_list_access = 0;
264 p_sys->b_quick = b_quick;
266 /* Get the additional list of inputs if any (for concatenation) */
267 if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
269 access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
270 char *psz_name, *psz_parser = psz_name = psz_list;
272 p_sys->p_list_access = p_access;
273 p_entry->i_size = p_access->info.i_size;
274 p_entry->psz_path = strdup( p_access->psz_path );
275 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
276 msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
277 p_entry->psz_path, p_access->info.i_size );
279 while( psz_name && *psz_name )
281 psz_parser = strchr( psz_name, ',' );
282 if( psz_parser ) *psz_parser = 0;
284 psz_name = strdup( psz_name );
287 access_t *p_tmp = access2_New( p_access, p_access->psz_access,
292 psz_name = psz_parser;
293 if( psz_name ) psz_name++;
297 msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
298 psz_name, p_tmp->info.i_size );
300 p_entry = malloc( sizeof(access_entry_t) );
301 p_entry->i_size = p_tmp->info.i_size;
302 p_entry->psz_path = psz_name;
303 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
305 access2_Delete( p_tmp );
308 psz_name = psz_parser;
309 if( psz_name ) psz_name++;
312 if( psz_list ) free( psz_list );
316 p_sys->p_peek = NULL;
320 s->pf_read = AStreamReadBlock;
321 s->pf_peek = AStreamPeekBlock;
323 /* Init all fields of p_sys->block */
324 p_sys->block.i_start = p_sys->i_pos;
325 p_sys->block.i_offset = 0;
326 p_sys->block.p_current = NULL;
327 p_sys->block.i_size = 0;
328 p_sys->block.p_first = NULL;
329 p_sys->block.pp_last = &p_sys->block.p_first;
331 /* Do the prebuffering */
332 AStreamPrebufferBlock( s );
334 if( p_sys->block.i_size <= 0 )
336 msg_Err( s, "cannot pre fill buffer" );
344 s->pf_read = AStreamReadStream;
345 s->pf_peek = AStreamPeekStream;
347 /* Allocate/Setup our tracks */
348 p_sys->stream.i_offset = 0;
349 p_sys->stream.i_tk = 0;
350 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
351 p_sys->stream.i_used = 0;
352 access2_Control( p_access, ACCESS_GET_MTU,
353 &p_sys->stream.i_read_size );
354 if( p_sys->stream.i_read_size <= 0 )
355 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
356 else if( p_sys->stream.i_read_size <= 256 )
357 p_sys->stream.i_read_size = 256;
359 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
361 p_sys->stream.tk[i].i_date = 0;
362 p_sys->stream.tk[i].i_start = p_sys->i_pos;
363 p_sys->stream.tk[i].i_end = p_sys->i_pos;
364 p_sys->stream.tk[i].p_buffer=
365 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
368 /* Do the prebuffering */
369 AStreamPrebufferStream( s );
371 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
373 msg_Err( s, "cannot pre fill buffer" );
387 free( p_sys->stream.p_buffer );
390 vlc_object_detach( s );
391 vlc_object_destroy( s );
395 /****************************************************************************
397 ****************************************************************************/
398 static void AStreamDestroy( stream_t *s )
400 stream_sys_t *p_sys = s->p_sys;
402 vlc_object_detach( s );
404 if( p_sys->b_block ) block_ChainRelease( p_sys->block.p_first );
405 else free( p_sys->stream.p_buffer );
407 if( p_sys->p_peek ) free( p_sys->p_peek );
409 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
410 access2_Delete( p_sys->p_list_access );
412 while( p_sys->i_list-- )
414 free( p_sys->list[p_sys->i_list]->psz_path );
415 free( p_sys->list[p_sys->i_list] );
416 if( !p_sys->i_list ) free( p_sys->list );
420 vlc_object_destroy( s );
423 static void UStreamDestroy( stream_t *s )
425 access_t *p_access = (access_t*)vlc_object_find( s, VLC_OBJECT_ACCESS, FIND_PARENT );
427 vlc_object_release( p_access );
428 access2_Delete( p_access );
431 /****************************************************************************
432 * stream_AccessReset:
433 ****************************************************************************/
434 void stream_AccessReset( stream_t *s )
436 stream_sys_t *p_sys = s->p_sys;
438 p_sys->i_pos = p_sys->p_access->info.i_pos;
442 block_ChainRelease( p_sys->block.p_first );
444 /* Init all fields of p_sys->block */
445 p_sys->block.i_start = p_sys->i_pos;
446 p_sys->block.i_offset = 0;
447 p_sys->block.p_current = NULL;
448 p_sys->block.i_size = 0;
449 p_sys->block.p_first = NULL;
450 p_sys->block.pp_last = &p_sys->block.p_first;
452 /* Do the prebuffering */
453 AStreamPrebufferBlock( s );
459 /* Setup our tracks */
460 p_sys->stream.i_offset = 0;
461 p_sys->stream.i_tk = 0;
462 p_sys->stream.i_used = 0;
464 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
466 p_sys->stream.tk[i].i_date = 0;
467 p_sys->stream.tk[i].i_start = p_sys->i_pos;
468 p_sys->stream.tk[i].i_end = p_sys->i_pos;
471 /* Do the prebuffering */
472 AStreamPrebufferStream( s );
476 /****************************************************************************
477 * stream_AccessUpdate:
478 ****************************************************************************/
479 void stream_AccessUpdate( stream_t *s )
481 stream_sys_t *p_sys = s->p_sys;
483 p_sys->i_pos = p_sys->p_access->info.i_pos;
488 for( i = 0; i < p_sys->i_list_index; i++ )
490 p_sys->i_pos += p_sys->list[i]->i_size;
495 /****************************************************************************
497 ****************************************************************************/
498 static int AStreamControl( stream_t *s, int i_query, va_list args )
500 stream_sys_t *p_sys = s->p_sys;
501 access_t *p_access = p_sys->p_access;
504 int64_t *pi_64, i_64;
509 case STREAM_GET_SIZE:
510 pi_64 = (int64_t*)va_arg( args, int64_t * );
511 if( s->p_sys->i_list )
515 for( i = 0; i < s->p_sys->i_list; i++ )
516 *pi_64 += s->p_sys->list[i]->i_size;
519 *pi_64 = p_access->info.i_size;
522 case STREAM_CAN_SEEK:
523 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
524 access2_Control( p_access, ACCESS_CAN_SEEK, p_bool );
527 case STREAM_CAN_FASTSEEK:
528 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
529 access2_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
532 case STREAM_GET_POSITION:
533 pi_64 = (int64_t*)va_arg( args, int64_t * );
534 *pi_64 = p_sys->i_pos;
537 case STREAM_SET_POSITION:
538 i_64 = (int64_t)va_arg( args, int64_t );
540 return AStreamSeekBlock( s, i_64 );
542 return AStreamSeekStream( s, i_64 );
547 case STREAM_CONTROL_ACCESS:
548 i_int = (int) va_arg( args, int );
549 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
550 i_int != ACCESS_SET_PRIVATE_ID_CA &&
551 i_int != ACCESS_GET_PRIVATE_ID_STATE )
553 msg_Err( s, "Hey, what are you thinking ?"
554 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
557 return access2_vaControl( p_access, i_int, args );
560 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
568 /****************************************************************************
570 ****************************************************************************/
571 static void AStreamPrebufferBlock( stream_t *s )
573 stream_sys_t *p_sys = s->p_sys;
574 access_t *p_access = p_sys->p_access;
579 msg_Dbg( s, "pre buffering" );
583 int64_t i_date = mdate();
587 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
588 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
593 p_sys->stat.i_bytes = p_sys->block.i_size;
594 p_sys->stat.i_read_time = i_date - i_start;
595 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
596 (p_sys->stat.i_read_time + 1);
598 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
601 p_sys->stat.i_read_time / I64C(1000000),
607 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
611 msleep( STREAM_DATA_WAIT );
617 /* Append the block */
618 p_sys->block.i_size += b->i_buffer;
619 *p_sys->block.pp_last = b;
620 p_sys->block.pp_last = &b->p_next;
622 p_sys->stat.i_read_count++;
626 if( p_access->info.b_prebuffered )
628 /* Access has already prebufferred - update stats and exit */
629 p_sys->stat.i_bytes = p_sys->block.i_size;
630 p_sys->stat.i_read_time = mdate() - i_start;
637 msg_Dbg( s, "received first data for our buffer");
642 p_sys->block.p_current = p_sys->block.p_first;
645 static int AStreamRefillBlock( stream_t *s );
647 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
649 stream_sys_t *p_sys = s->p_sys;
651 uint8_t *p_data= (uint8_t*)p_read;
655 if( p_sys->block.p_current == NULL )
660 /* seek within this stream if possible, else use plain old read and discard */
661 stream_sys_t *p_sys = s->p_sys;
662 access_t *p_access = p_sys->p_access;
664 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
666 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
669 while( i_data < i_read )
672 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
673 int i_copy = __MIN( i_current, i_read - i_data);
679 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
685 p_sys->block.i_offset += i_copy;
686 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
688 /* Current block is now empty, switch to next */
689 if( p_sys->block.p_current )
691 p_sys->block.i_offset = 0;
692 p_sys->block.p_current = p_sys->block.p_current->p_next;
695 if( AStreamRefillBlock( s ) )
702 p_sys->i_pos += i_data;
706 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, int i_read )
708 stream_sys_t *p_sys = s->p_sys;
714 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
716 /* We can directly give a pointer over our buffer */
717 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
719 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
723 /* We need to create a local copy */
724 if( p_sys->i_peek < i_read )
726 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
732 p_sys->i_peek = i_read;
735 /* Fill enough data */
736 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
739 block_t **pp_last = p_sys->block.pp_last;
741 if( AStreamRefillBlock( s ) ) break;
743 /* Our buffer are probably filled enough, don't try anymore */
744 if( pp_last == p_sys->block.pp_last ) break;
747 /* Copy what we have */
748 b = p_sys->block.p_current;
749 i_offset = p_sys->block.i_offset;
750 p_data = p_sys->p_peek;
752 while( b && i_data < i_read )
754 int i_current = b->i_buffer - i_offset;
755 int i_copy = __MIN( i_current, i_read - i_data );
757 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
762 if( i_offset >= b->i_buffer )
769 *pp_peek = p_sys->p_peek;
773 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
775 stream_sys_t *p_sys = s->p_sys;
776 access_t *p_access = p_sys->p_access;
777 int64_t i_offset = i_pos - p_sys->block.i_start;
780 /* We already have thoses data, just update p_current/i_offset */
781 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
783 block_t *b = p_sys->block.p_first;
786 while( i_current + b->i_buffer < i_offset )
788 i_current += b->i_buffer;
792 p_sys->block.p_current = b;
793 p_sys->block.i_offset = i_offset - i_current;
795 p_sys->i_pos = i_pos;
800 /* We may need to seek or to read data */
804 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
808 msg_Err( s, "backward seeking impossible (access not seekable)" );
816 vlc_bool_t b_aseek, b_aseekfast;
818 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
819 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
824 msg_Warn( s, I64Fd" bytes need to be skipped "
825 "(access non seekable)",
826 i_offset - p_sys->block.i_size );
830 int64_t i_skip = i_offset - p_sys->block.i_size;
832 /* Avg bytes per packets */
833 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
834 /* TODO compute a seek cost instead of fixed threshold */
835 int i_th = b_aseekfast ? 1 : 5;
837 if( i_skip <= i_th * i_avg &&
838 i_skip < STREAM_CACHE_SIZE )
843 msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
844 b_seek, i_th*i_avg, i_skip );
850 int64_t i_start, i_end;
851 /* Do the access seek */
853 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
857 block_ChainRelease( p_sys->block.p_first );
860 p_sys->block.i_start = p_sys->i_pos = i_pos;
861 p_sys->block.i_offset = 0;
862 p_sys->block.p_current = NULL;
863 p_sys->block.i_size = 0;
864 p_sys->block.p_first = NULL;
865 p_sys->block.pp_last = &p_sys->block.p_first;
868 if( AStreamRefillBlock( s ) )
870 msg_Err( s, "cannot re fill buffer" );
874 p_sys->stat.i_seek_time += i_end - i_start;
875 p_sys->stat.i_seek_count++;
880 /* Read enough data */
881 while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
883 if( AStreamRefillBlock( s ) )
885 msg_Err( s, "can't read enough data in seek" );
888 while( p_sys->block.p_current &&
889 p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
891 p_sys->i_pos += p_sys->block.p_current->i_buffer;
892 p_sys->block.p_current = p_sys->block.p_current->p_next;
896 p_sys->block.i_offset = i_pos - p_sys->i_pos;
897 p_sys->i_pos = i_pos;
906 static int AStreamRefillBlock( stream_t *s )
908 stream_sys_t *p_sys = s->p_sys;
909 int64_t i_start, i_stop;
913 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
914 p_sys->block.p_first != p_sys->block.p_current )
916 block_t *b = p_sys->block.p_first;
918 p_sys->block.i_start += b->i_buffer;
919 p_sys->block.i_size -= b->i_buffer;
920 p_sys->block.p_first = b->p_next;
924 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
925 p_sys->block.p_current == p_sys->block.p_first &&
926 p_sys->block.p_current->p_next ) /* At least 2 packets */
928 /* Enough data, don't read more */
932 /* Now read a new block */
938 if( s->b_die ) return VLC_EGENERIC;
942 if( ( b = AReadBlock( s, &b_eof ) ) ) break;
944 if( b_eof ) return VLC_EGENERIC;
946 msleep( STREAM_DATA_WAIT );
953 /* Append the block */
954 p_sys->block.i_size += b->i_buffer;
955 *p_sys->block.pp_last = b;
956 p_sys->block.pp_last = &b->p_next;
959 if( p_sys->block.p_current == NULL )
960 p_sys->block.p_current = b;
963 p_sys->stat.i_bytes += b->i_buffer;
964 p_sys->stat.i_read_time += i_stop - i_start;
965 p_sys->stat.i_read_count++;
974 /****************************************************************************
976 ****************************************************************************/
977 static int AStreamRefillStream( stream_t *s );
979 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
981 stream_sys_t *p_sys = s->p_sys;
982 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
984 uint8_t *p_data = (uint8_t *)p_read;
987 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
991 /* seek within this stream if possible, else use plain old read and discard */
992 stream_sys_t *p_sys = s->p_sys;
993 access_t *p_access = p_sys->p_access;
995 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
997 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1001 msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
1002 " offset=%d end="I64Fd,
1003 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1004 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1007 while( i_data < i_read )
1009 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1010 STREAM_CACHE_TRACK_SIZE;
1012 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1013 STREAM_CACHE_TRACK_SIZE - i_off );
1014 int i_copy = __MIN( i_current, i_read - i_data );
1016 if( i_copy <= 0 ) break; /* EOF */
1019 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1022 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1026 p_sys->stream.i_offset += i_copy;
1028 /* Update pos now */
1029 p_sys->i_pos += i_copy;
1032 p_sys->stream.i_used += i_copy;
1033 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1034 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1036 if( AStreamRefillStream( s ) )
1039 if( tk->i_start >= tk->i_end ) break;
1047 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, int i_read )
1049 stream_sys_t *p_sys = s->p_sys;
1050 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1053 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1056 msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
1057 "start="I64Fd" offset=%d end="I64Fd,
1058 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1059 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1062 /* Avoid problem, but that should *never* happen */
1063 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1064 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1066 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1068 if( p_sys->stream.i_used <= 1 )
1070 /* Be sure we will read something */
1071 p_sys->stream.i_used += i_read -
1072 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1074 if( AStreamRefillStream( s ) ) break;
1077 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1078 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1080 /* Now, direct pointer or a copy ? */
1081 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1082 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1084 *pp_peek = &tk->p_buffer[i_off];
1088 if( p_sys->i_peek < i_read )
1090 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1091 if( !p_sys->p_peek )
1096 p_sys->i_peek = i_read;
1099 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1100 STREAM_CACHE_TRACK_SIZE - i_off );
1101 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1102 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1104 *pp_peek = p_sys->p_peek;
1108 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1110 stream_sys_t *p_sys = s->p_sys;
1111 access_t *p_access = p_sys->p_access;
1113 vlc_bool_t b_afastseek;
1119 msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
1120 " tk=%d start="I64Fd" offset=%d end="I64Fd,
1121 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1122 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1123 p_sys->stream.i_offset,
1124 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1128 /* Seek in our current track ? */
1129 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1130 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1132 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1134 msg_Dbg( s, "AStreamSeekStream: current track" );
1136 p_sys->i_pos = i_pos;
1137 p_sys->stream.i_offset = i_pos - tk->i_start;
1139 /* If there is not enough data left in the track, refill */
1140 /* \todo How to get a correct value for
1141 * - refilling threshold
1142 * - how much to refill
1144 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1145 p_sys->stream.i_read_size )
1147 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1149 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1150 AStreamRefillStream( s );
1156 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1159 /* We can't do nothing */
1160 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1161 return VLC_EGENERIC;
1164 /* Date the current track */
1165 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1167 /* Try to reuse already read data */
1168 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1170 stream_track_t *tk = &p_sys->stream.tk[i];
1172 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1175 msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
1176 " end="I64Fd, i, tk->i_start, tk->i_end );
1179 /* Seek at the end of the buffer */
1180 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1183 p_sys->i_pos = i_pos;
1184 p_sys->stream.i_tk = i;
1185 p_sys->stream.i_offset = i_pos - tk->i_start;
1187 if( p_sys->stream.i_used < 1024 )
1188 p_sys->stream.i_used = 1024;
1190 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1191 return VLC_EGENERIC;
1197 access2_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1198 /* FIXME compute seek cost (instead of static 'stupid' value) */
1199 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1205 /* Search closest segment TODO */
1206 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1208 stream_track_t *tk = &p_sys->stream.tk[i];
1210 if( i_pos + i_maxth >= tk->i_start )
1212 msg_Dbg( s, "good segment before current pos, TODO" );
1214 if( i_pos - i_maxth <= tk->i_end )
1216 msg_Dbg( s, "good segment after current pos, TODO" );
1221 /* Nothing good, seek and choose oldest segment */
1222 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1223 p_sys->i_pos = i_pos;
1226 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1228 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1232 /* Reset the segment */
1233 p_sys->stream.i_tk = i_new;
1234 p_sys->stream.i_offset = 0;
1235 p_sys->stream.tk[i_new].i_start = i_pos;
1236 p_sys->stream.tk[i_new].i_end = i_pos;
1239 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1240 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1242 if( AStreamRefillStream( s ) )
1243 return VLC_EGENERIC;
1248 static int AStreamRefillStream( stream_t *s )
1250 stream_sys_t *p_sys = s->p_sys;
1251 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1253 /* We read but won't increase i_start after initial start + offset */
1255 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1256 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1257 vlc_bool_t b_read = VLC_FALSE;
1258 int64_t i_start, i_stop;
1260 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1263 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1264 p_sys->stream.i_used, i_toread );
1268 while( i_toread > 0 )
1270 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1274 return VLC_EGENERIC;
1276 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1277 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1279 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1282 msleep( STREAM_DATA_WAIT );
1285 else if( i_read == 0 )
1287 if( !b_read ) return VLC_EGENERIC;
1293 tk->i_end += i_read;
1295 /* Windows of STREAM_CACHE_TRACK_SIZE */
1296 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1298 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1300 tk->i_start += i_invalid;
1301 p_sys->stream.i_offset -= i_invalid;
1305 p_sys->stream.i_used -= i_read;
1307 p_sys->stat.i_bytes += i_read;
1308 p_sys->stat.i_read_count++;
1312 p_sys->stat.i_read_time += i_stop - i_start;
1317 static void AStreamPrebufferStream( stream_t *s )
1319 stream_sys_t *p_sys = s->p_sys;
1320 access_t *p_access = p_sys->p_access;
1322 int64_t i_first = 0;
1324 int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1325 ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1326 STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1328 msg_Dbg( s, "pre-buffering..." );
1332 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1334 int64_t i_date = mdate();
1337 if( s->b_die || tk->i_end >= i_prebuffer ||
1338 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1343 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1344 p_sys->stat.i_read_time = i_date - i_start;
1345 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
1346 (p_sys->stat.i_read_time+1);
1348 msg_Dbg( s, "pre-buffering done "I64Fd" bytes in "I64Fd"s - "
1350 p_sys->stat.i_bytes,
1351 p_sys->stat.i_read_time / I64C(1000000),
1352 i_byterate / 1024 );
1357 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1358 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1359 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1362 msleep( STREAM_DATA_WAIT );
1365 else if( i_read == 0 )
1374 msg_Dbg( s, "received first data for our buffer");
1377 tk->i_end += i_read;
1379 p_sys->stat.i_read_count++;
1384 /****************************************************************************
1386 ****************************************************************************/
1388 * Read from the stream untill first newline.
1389 * \param s Stream handle to read from
1390 * \return A pointer to the allocated output string. You need to free this when you are done.
1392 #define STREAM_PROBE_LINE 2048
1393 #define STREAM_LINE_MAX (2048*100)
1394 char * stream_ReadLine( stream_t *s )
1396 char *p_line = NULL;
1397 int i_line = 0, i_read = 0;
1399 while( i_read < STREAM_LINE_MAX )
1402 const uint8_t *p_data;
1406 /* Probe new data */
1407 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1408 if( i_data <= 0 ) break; /* No more data */
1411 i_pos = stream_Tell( s );
1412 if( i_pos == 0 && i_data > 4 )
1415 char *psz_encoding = NULL;
1417 if( p_data[0] == 0xEF && p_data[1] == 0xBB && p_data[2] == 0xBF )
1419 psz_encoding = strdup( "UTF-8" );
1422 else if( p_data[0] == 0x00 && p_data[1] == 0x00 )
1424 if( p_data[2] == 0xFE && p_data[3] == 0xFF )
1426 psz_encoding = strdup( "UTF-32BE" );
1427 s->i_char_width = 4;
1431 else if( p_data[0] == 0xFF && p_data[1] == 0xFE )
1433 if( p_data[2] == 0x00 && p_data[3] == 0x00 )
1435 psz_encoding = strdup( "UTF-32LE" );
1436 s->i_char_width = 4;
1437 s->b_little_endian = VLC_TRUE;
1442 psz_encoding = strdup( "UTF-16LE" );
1443 s->b_little_endian = VLC_TRUE;
1444 s->i_char_width = 2;
1448 else if( p_data[0] == 0xFE && p_data[1] == 0xFF )
1450 psz_encoding = strdup( "UTF-16BE" );
1451 s->i_char_width = 2;
1455 /* Seek past the BOM */
1458 stream_Seek( s, i_bom_size );
1459 p_data += i_bom_size;
1460 i_data -= i_bom_size;
1463 /* Open the converter if we need it */
1464 if( psz_encoding != NULL )
1466 input_thread_t *p_input;
1467 msg_Dbg( s, "%s BOM detected", psz_encoding );
1468 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1469 if( s->i_char_width > 1 )
1471 s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1472 if( s->conv == (vlc_iconv_t)-1 )
1474 msg_Err( s, "iconv_open failed" );
1477 if( p_input != NULL)
1479 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1480 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1481 vlc_object_release( p_input );
1483 if( psz_encoding ) free( psz_encoding );
1487 if( i_data % s->i_char_width )
1489 /* keep i_char_width boundary */
1490 i_data = i_data - ( i_data % s->i_char_width );
1491 msg_Warn( s, "the read is not i_char_width compatible");
1497 /* Check if there is an EOL */
1498 if( s->i_char_width == 1 )
1500 /* UTF-8: 0A <LF> */
1501 psz_eol = memchr( p_data, '\n', i_data );
1505 const uint8_t *p = p_data;
1506 const uint8_t *p_last = p + i_data - s->i_char_width;
1508 if( s->i_char_width == 2 )
1510 if( s->b_little_endian == VLC_TRUE)
1512 /* UTF-16LE: 0A 00 <LF> */
1513 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1518 /* UTF-16BE: 00 0A <LF> */
1519 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1523 else if( s->i_char_width == 4 )
1525 if( s->b_little_endian == VLC_TRUE)
1527 /* UTF-32LE: 0A 00 00 00 <LF> */
1528 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ||
1529 p[2] != 0x00 || p[3] != 0x00 ) )
1534 /* UTF-32BE: 00 00 00 0A <LF> */
1535 while( p <= p_last && ( p[3] != 0x0A || p[2] != 0x00 ||
1536 p[1] != 0x00 || p[0] != 0x00 ) )
1547 psz_eol = (char *)p + ( s->i_char_width - 1 );
1553 i_data = (psz_eol - (char *)p_data) + 1;
1554 p_line = realloc( p_line, i_line + i_data + s->i_char_width ); /* add \0 */
1555 i_data = stream_Read( s, &p_line[i_line], i_data );
1556 if( i_data <= 0 ) break; /* Hmmm */
1557 i_line += i_data - s->i_char_width; /* skip \n */;
1560 /* We have our line */
1564 /* Read data (+1 for easy \0 append) */
1565 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->i_char_width );
1566 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1567 if( i_data <= 0 ) break; /* Hmmm */
1575 for( j = 0; j < s->i_char_width; j++ )
1577 p_line[i_line + j] = '\0';
1579 i_line += s->i_char_width; /* the added \0 */
1580 if( s->i_char_width > 1 )
1582 size_t i_in = 0, i_out = 0;
1583 const char * p_in = NULL;
1584 char * p_out = NULL;
1585 char * psz_new_line = NULL;
1588 psz_new_line = malloc( i_line );
1590 i_in = i_out = (size_t)i_line;
1592 p_out = psz_new_line;
1594 if( vlc_iconv( s->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1596 msg_Err( s, "iconv failed" );
1597 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1599 if( p_line ) free( p_line );
1600 p_line = psz_new_line;
1601 i_line = (size_t)i_line - i_out; /* does not include \0 */
1604 /* Remove trailing LF/CR */
1605 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1606 p_line[i_line-2] == '\n') ) i_line--;
1608 /* Make sure the \0 is there */
1609 p_line[i_line-1] = '\0';
1614 /* We failed to read any data, probably EOF */
1615 if( p_line ) free( p_line );
1616 if( s->conv != (vlc_iconv_t)(-1) ) vlc_iconv_close( s->conv );
1620 /****************************************************************************
1621 * Access reading/seeking wrappers to handle concatenated streams.
1622 ****************************************************************************/
1623 static int AReadStream( stream_t *s, void *p_read, int i_read )
1625 stream_sys_t *p_sys = s->p_sys;
1626 access_t *p_access = p_sys->p_access;
1627 input_thread_t *p_input = NULL;
1628 int i_read_orig = i_read;
1631 if( s->p_parent && s->p_parent->p_parent &&
1632 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1633 p_input = (input_thread_t *)s->p_parent->p_parent;
1635 if( !p_sys->i_list )
1637 i_read = p_access->pf_read( p_access, p_read, i_read );
1640 vlc_object_yield( p_input );
1641 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1642 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
1644 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1645 (float)i_total, NULL );
1646 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1647 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1648 vlc_object_release( p_input );
1653 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1656 /* If we reached an EOF then switch to the next stream in the list */
1657 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1659 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1660 access_t *p_list_access;
1662 msg_Dbg( s, "opening input `%s'", psz_name );
1664 p_list_access = access2_New( s, p_access->psz_access, "", psz_name, 0 );
1666 if( !p_list_access ) return 0;
1668 if( p_sys->p_list_access != p_access )
1669 access2_Delete( p_sys->p_list_access );
1671 p_sys->p_list_access = p_list_access;
1673 /* We have to read some data */
1674 return AReadStream( s, p_read, i_read_orig );
1677 /* Update read bytes in input */
1680 vlc_object_yield( p_input );
1681 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1682 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
1683 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1684 (float)i_total, NULL );
1685 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1686 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1687 vlc_object_release( p_input );
1692 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof )
1694 stream_sys_t *p_sys = s->p_sys;
1695 access_t *p_access = p_sys->p_access;
1696 input_thread_t *p_input = NULL;
1701 if( s->p_parent && s->p_parent->p_parent &&
1702 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1703 p_input = (input_thread_t *)s->p_parent->p_parent;
1705 if( !p_sys->i_list )
1707 p_block = p_access->pf_block( p_access );
1708 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1709 if( p_input && p_block && p_access->p_libvlc->b_stats )
1711 vlc_object_yield( p_input );
1712 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1713 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1714 p_block->i_buffer, &i_total );
1715 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1716 (float)i_total, NULL );
1717 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1718 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1719 vlc_object_release( p_input );
1724 p_block = p_sys->p_list_access->pf_block( p_access );
1725 b_eof = p_sys->p_list_access->info.b_eof;
1726 if( pb_eof ) *pb_eof = b_eof;
1728 /* If we reached an EOF then switch to the next stream in the list */
1729 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1731 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1732 access_t *p_list_access;
1734 msg_Dbg( s, "opening input `%s'", psz_name );
1736 p_list_access = access2_New( s, p_access->psz_access, "", psz_name, 0 );
1738 if( !p_list_access ) return 0;
1740 if( p_sys->p_list_access != p_access )
1741 access2_Delete( p_sys->p_list_access );
1743 p_sys->p_list_access = p_list_access;
1745 /* We have to read some data */
1746 return AReadBlock( s, pb_eof );
1752 vlc_object_yield( p_input );
1753 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1754 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1755 p_block->i_buffer, &i_total );
1756 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1757 (float)i_total, NULL );
1758 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
1760 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1761 vlc_object_release( p_input );
1767 static int ASeek( stream_t *s, int64_t i_pos )
1769 stream_sys_t *p_sys = s->p_sys;
1770 access_t *p_access = p_sys->p_access;
1772 /* Check which stream we need to access */
1778 access_t *p_list_access = 0;
1780 for( i = 0; i < p_sys->i_list - 1; i++ )
1782 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1783 i_size += p_sys->list[i]->i_size;
1785 psz_name = p_sys->list[i]->psz_path;
1787 if( i != p_sys->i_list_index )
1788 msg_Dbg( s, "opening input `%s'", psz_name );
1790 if( i != p_sys->i_list_index && i != 0 )
1793 access2_New( s, p_access->psz_access, "", psz_name, 0 );
1795 else if( i != p_sys->i_list_index )
1797 p_list_access = p_access;
1802 if( p_sys->p_list_access != p_access )
1803 access2_Delete( p_sys->p_list_access );
1805 p_sys->p_list_access = p_list_access;
1808 p_sys->i_list_index = i;
1809 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1813 return p_access->pf_seek( p_access, i_pos );
1818 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
1819 * "p_read" is NULL then data are skipped instead of read. The return
1820 * value is the real numbers of bytes read/skip. If this value is less
1821 * than i_read that means that it's the end of the stream.
1823 int stream_Read( stream_t *s, void *p_read, int i_read )
1825 return s->pf_read( s, p_read, i_read );
1829 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
1830 * \return The real numbers of valid bytes, if it's less
1831 * or equal to 0, *pp_peek is invalid.
1832 * \note pp_peek is a pointer to internal buffer and it will be invalid as
1833 * soons as other stream_* functions are called.
1834 * \note Due to input limitation, it could be less than i_peek without meaning
1835 * the end of the stream (but only when you have i_peek >=
1836 * p_input->i_bufsize)
1838 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
1840 return s->pf_peek( s, pp_peek, i_peek );
1844 * Use to control the "stream_t *". Look at #stream_query_e for
1845 * possible "i_query" value and format arguments. Return VLC_SUCCESS
1846 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
1848 int stream_vaControl( stream_t *s, int i_query, va_list args )
1850 return s->pf_control( s, i_query, args );
1856 void stream_Delete( stream_t *s )
1861 int stream_Control( stream_t *s, int i_query, ... )
1867 return VLC_EGENERIC;
1869 va_start( args, i_query );
1870 i_result = s->pf_control( s, i_query, args );
1876 * Read "i_size" bytes and store them in a block_t.
1877 * It always read i_size bytes unless you are at the end of the stream
1878 * where it return what is available.
1880 block_t *stream_Block( stream_t *s, int i_size )
1882 if( i_size <= 0 ) return NULL;
1884 /* emulate block read */
1885 block_t *p_bk = block_New( s, i_size );
1888 p_bk->i_buffer = stream_Read( s, p_bk->p_buffer, i_size );
1889 if( p_bk->i_buffer > 0 )
1894 if( p_bk ) block_Release( p_bk );