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 *****************************************************************************/
27 #include "input_internal.h"
32 * - tune the 2 methods
33 * - compute cost for seek
34 * - improve stream mode seeking with closest segments
40 * One linked list of data read
42 * More complex scheme using mutliple track to avoid seeking
45 /* How many tracks we have, currently only used for stream mode */
46 #ifdef OPTIMIZE_MEMORY
47 # define STREAM_CACHE_TRACK 1
48 /* Max size of our cache 128Ko per track */
49 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
51 # define STREAM_CACHE_TRACK 3
52 /* Max size of our cache 4Mo per track */
53 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
56 /* How many data we try to prebuffer */
57 #define STREAM_CACHE_PREBUFFER_SIZE (32767)
58 /* Maximum time we take to pre-buffer */
59 #define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
61 /* Method1: Simple, for pf_block.
62 * We get blocks and put them in the linked list.
63 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
65 #define STREAM_DATA_WAIT 40000 /* Time between before a pf_block retry */
67 /* Method2: A bit more complex, for pf_read
68 * - We use ring buffers, only one if unseekable, all if seekable
69 * - Upon seek date current ring, then search if one ring match the pos,
70 * yes: switch to it, seek the access to match the end of the ring
71 * no: search the ring with i_end the closer to i_pos,
72 * if close enough, read data and use this ring
73 * else use the oldest ring, seek and use it.
75 * TODO: - with access non seekable: use all space available for only one ring, but
76 * we have to support seekable/non-seekable switch on the fly.
77 * - compute a good value for i_read_size
80 #define STREAM_READ_ATONCE 32767
81 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
105 vlc_bool_t b_block; /* Block method (1) or stream */
107 int64_t i_pos; /* Current reading offset */
109 /* Method 1: pf_block */
112 int64_t i_start; /* Offset of block for p_first */
113 int i_offset; /* Offset for data in p_current */
114 block_t *p_current; /* Current block */
116 int i_size; /* Total amount of data in the list */
122 /* Method 2: for pf_read */
125 int i_offset; /* Buffer offset in the current track */
126 int i_tk; /* Current track */
127 stream_track_t tk[STREAM_CACHE_TRACK];
133 int i_used; /* Used since last read */
138 /* Peek temporary buffer */
142 /* Stat for both method */
145 vlc_bool_t b_fastseek; /* From access */
147 /* Stat about reading data */
148 int64_t i_read_count;
152 /* Stat about seek */
160 access_entry_t **list;
162 access_t *p_list_access;
164 /* Preparse mode ? */
169 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read );
170 static int AStreamPeekBlock( stream_t *s, const uint8_t **p_peek, int i_read );
171 static int AStreamSeekBlock( stream_t *s, int64_t i_pos );
172 static void AStreamPrebufferBlock( stream_t *s );
173 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof );
176 static int AStreamReadStream( stream_t *s, void *p_read, int i_read );
177 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, int i_read );
178 static int AStreamSeekStream( stream_t *s, int64_t i_pos );
179 static void AStreamPrebufferStream( stream_t *s );
180 static int AReadStream( stream_t *s, void *p_read, int i_read );
183 static int AStreamControl( stream_t *s, int i_query, va_list );
184 static void AStreamDestroy( stream_t *s );
185 static void UStreamDestroy( stream_t *s );
186 static int ASeek( stream_t *s, int64_t i_pos );
189 /****************************************************************************
190 * stream_UrlNew: create a stream from a access
191 ****************************************************************************/
192 stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
194 const char *psz_access, *psz_demux;
199 if( !psz_url ) return 0;
201 char psz_dup[strlen (psz_url) + 1];
202 strcpy (psz_dup, psz_url);;
203 MRLSplit( p_parent, psz_dup, &psz_access, &psz_demux, &psz_path );
205 /* Now try a real access */
206 p_access = access2_New( p_parent, psz_access, psz_demux, psz_path, 0 );
208 if( p_access == NULL )
210 msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
214 if( !( p_res = stream_AccessNew( p_access, VLC_TRUE ) ) )
216 access2_Delete( p_access );
220 p_res->pf_destroy = UStreamDestroy;
224 stream_t *stream_AccessNew( access_t *p_access, vlc_bool_t b_quick )
226 stream_t *s = vlc_stream_create( VLC_OBJECT(p_access) );
230 if( !s ) return NULL;
232 /* Attach it now, needed for b_die */
233 vlc_object_attach( s, p_access );
235 s->pf_read = NULL; /* Set up later */
237 s->pf_control = AStreamControl;
238 s->pf_destroy = AStreamDestroy;
240 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
242 /* UTF16 and UTF32 text file conversion */
244 s->b_little_endian = VLC_FALSE;
245 s->conv = (vlc_iconv_t)(-1);
248 p_sys->p_access = p_access;
249 p_sys->b_block = p_access->pf_block ? VLC_TRUE : VLC_FALSE;
250 p_sys->i_pos = p_access->info.i_pos;
253 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
254 p_sys->stat.i_bytes = 0;
255 p_sys->stat.i_read_time = 0;
256 p_sys->stat.i_read_count = 0;
257 p_sys->stat.i_seek_count = 0;
258 p_sys->stat.i_seek_time = 0;
262 p_sys->i_list_index = 0;
263 p_sys->p_list_access = 0;
265 p_sys->b_quick = b_quick;
267 /* Get the additional list of inputs if any (for concatenation) */
268 if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
270 access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
271 char *psz_name, *psz_parser = psz_name = psz_list;
273 p_sys->p_list_access = p_access;
274 p_entry->i_size = p_access->info.i_size;
275 p_entry->psz_path = strdup( p_access->psz_path );
276 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
277 msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
278 p_entry->psz_path, p_access->info.i_size );
280 while( psz_name && *psz_name )
282 psz_parser = strchr( psz_name, ',' );
283 if( psz_parser ) *psz_parser = 0;
285 psz_name = strdup( psz_name );
288 access_t *p_tmp = access2_New( p_access, p_access->psz_access,
293 psz_name = psz_parser;
294 if( psz_name ) psz_name++;
298 msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
299 psz_name, p_tmp->info.i_size );
301 p_entry = malloc( sizeof(access_entry_t) );
302 p_entry->i_size = p_tmp->info.i_size;
303 p_entry->psz_path = psz_name;
304 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
306 access2_Delete( p_tmp );
309 psz_name = psz_parser;
310 if( psz_name ) psz_name++;
313 if( psz_list ) free( psz_list );
317 p_sys->p_peek = NULL;
321 s->pf_read = AStreamReadBlock;
322 s->pf_peek = AStreamPeekBlock;
324 /* Init all fields of p_sys->block */
325 p_sys->block.i_start = p_sys->i_pos;
326 p_sys->block.i_offset = 0;
327 p_sys->block.p_current = NULL;
328 p_sys->block.i_size = 0;
329 p_sys->block.p_first = NULL;
330 p_sys->block.pp_last = &p_sys->block.p_first;
332 /* Do the prebuffering */
333 AStreamPrebufferBlock( s );
335 if( p_sys->block.i_size <= 0 )
337 msg_Err( s, "cannot pre fill buffer" );
345 s->pf_read = AStreamReadStream;
346 s->pf_peek = AStreamPeekStream;
348 /* Allocate/Setup our tracks */
349 p_sys->stream.i_offset = 0;
350 p_sys->stream.i_tk = 0;
351 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
352 p_sys->stream.i_used = 0;
353 access2_Control( p_access, ACCESS_GET_MTU,
354 &p_sys->stream.i_read_size );
355 if( p_sys->stream.i_read_size <= 0 )
356 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
357 else if( p_sys->stream.i_read_size <= 256 )
358 p_sys->stream.i_read_size = 256;
360 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
362 p_sys->stream.tk[i].i_date = 0;
363 p_sys->stream.tk[i].i_start = p_sys->i_pos;
364 p_sys->stream.tk[i].i_end = p_sys->i_pos;
365 p_sys->stream.tk[i].p_buffer=
366 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
369 /* Do the prebuffering */
370 AStreamPrebufferStream( s );
372 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
374 msg_Err( s, "cannot pre fill buffer" );
388 free( p_sys->stream.p_buffer );
391 vlc_object_detach( s );
392 vlc_object_destroy( s );
396 /****************************************************************************
398 ****************************************************************************/
399 static void AStreamDestroy( stream_t *s )
401 stream_sys_t *p_sys = s->p_sys;
403 vlc_object_detach( s );
405 if( p_sys->b_block ) block_ChainRelease( p_sys->block.p_first );
406 else free( p_sys->stream.p_buffer );
408 if( p_sys->p_peek ) free( p_sys->p_peek );
410 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
411 access2_Delete( p_sys->p_list_access );
413 while( p_sys->i_list-- )
415 free( p_sys->list[p_sys->i_list]->psz_path );
416 free( p_sys->list[p_sys->i_list] );
417 if( !p_sys->i_list ) free( p_sys->list );
421 vlc_object_destroy( s );
424 static void UStreamDestroy( stream_t *s )
426 access_t *p_access = (access_t*)vlc_object_find( s, VLC_OBJECT_ACCESS, FIND_PARENT );
428 vlc_object_release( p_access );
429 access2_Delete( p_access );
432 /****************************************************************************
433 * stream_AccessReset:
434 ****************************************************************************/
435 void stream_AccessReset( stream_t *s )
437 stream_sys_t *p_sys = s->p_sys;
439 p_sys->i_pos = p_sys->p_access->info.i_pos;
443 block_ChainRelease( p_sys->block.p_first );
445 /* Init all fields of p_sys->block */
446 p_sys->block.i_start = p_sys->i_pos;
447 p_sys->block.i_offset = 0;
448 p_sys->block.p_current = NULL;
449 p_sys->block.i_size = 0;
450 p_sys->block.p_first = NULL;
451 p_sys->block.pp_last = &p_sys->block.p_first;
453 /* Do the prebuffering */
454 AStreamPrebufferBlock( s );
460 /* Setup our tracks */
461 p_sys->stream.i_offset = 0;
462 p_sys->stream.i_tk = 0;
463 p_sys->stream.i_used = 0;
465 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
467 p_sys->stream.tk[i].i_date = 0;
468 p_sys->stream.tk[i].i_start = p_sys->i_pos;
469 p_sys->stream.tk[i].i_end = p_sys->i_pos;
472 /* Do the prebuffering */
473 AStreamPrebufferStream( s );
477 /****************************************************************************
478 * stream_AccessUpdate:
479 ****************************************************************************/
480 void stream_AccessUpdate( stream_t *s )
482 stream_sys_t *p_sys = s->p_sys;
484 p_sys->i_pos = p_sys->p_access->info.i_pos;
489 for( i = 0; i < p_sys->i_list_index; i++ )
491 p_sys->i_pos += p_sys->list[i]->i_size;
496 /****************************************************************************
498 ****************************************************************************/
499 static int AStreamControl( stream_t *s, int i_query, va_list args )
501 stream_sys_t *p_sys = s->p_sys;
502 access_t *p_access = p_sys->p_access;
505 int64_t *pi_64, i_64;
510 case STREAM_GET_SIZE:
511 pi_64 = (int64_t*)va_arg( args, int64_t * );
512 if( s->p_sys->i_list )
516 for( i = 0; i < s->p_sys->i_list; i++ )
517 *pi_64 += s->p_sys->list[i]->i_size;
520 *pi_64 = p_access->info.i_size;
523 case STREAM_CAN_SEEK:
524 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
525 access2_Control( p_access, ACCESS_CAN_SEEK, p_bool );
528 case STREAM_CAN_FASTSEEK:
529 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
530 access2_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
533 case STREAM_GET_POSITION:
534 pi_64 = (int64_t*)va_arg( args, int64_t * );
535 *pi_64 = p_sys->i_pos;
538 case STREAM_SET_POSITION:
539 i_64 = (int64_t)va_arg( args, int64_t );
541 return AStreamSeekBlock( s, i_64 );
543 return AStreamSeekStream( s, i_64 );
548 case STREAM_CONTROL_ACCESS:
549 i_int = (int) va_arg( args, int );
550 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
551 i_int != ACCESS_SET_PRIVATE_ID_CA &&
552 i_int != ACCESS_GET_PRIVATE_ID_STATE )
554 msg_Err( s, "Hey, what are you thinking ?"
555 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
558 return access2_vaControl( p_access, i_int, args );
561 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
569 /****************************************************************************
571 ****************************************************************************/
572 static void AStreamPrebufferBlock( stream_t *s )
574 stream_sys_t *p_sys = s->p_sys;
575 access_t *p_access = p_sys->p_access;
580 msg_Dbg( s, "pre buffering" );
584 int64_t i_date = mdate();
588 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
589 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
594 p_sys->stat.i_bytes = p_sys->block.i_size;
595 p_sys->stat.i_read_time = i_date - i_start;
596 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
597 (p_sys->stat.i_read_time + 1);
599 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
602 p_sys->stat.i_read_time / I64C(1000000),
608 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
612 msleep( STREAM_DATA_WAIT );
618 /* Append the block */
619 p_sys->block.i_size += b->i_buffer;
620 *p_sys->block.pp_last = b;
621 p_sys->block.pp_last = &b->p_next;
623 p_sys->stat.i_read_count++;
627 if( p_access->info.b_prebuffered )
629 /* Access has already prebufferred - update stats and exit */
630 p_sys->stat.i_bytes = p_sys->block.i_size;
631 p_sys->stat.i_read_time = mdate() - i_start;
638 msg_Dbg( s, "received first data for our buffer");
643 p_sys->block.p_current = p_sys->block.p_first;
646 static int AStreamRefillBlock( stream_t *s );
648 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
650 stream_sys_t *p_sys = s->p_sys;
652 uint8_t *p_data= (uint8_t*)p_read;
656 if( p_sys->block.p_current == NULL )
661 /* seek within this stream if possible, else use plain old read and discard */
662 stream_sys_t *p_sys = s->p_sys;
663 access_t *p_access = p_sys->p_access;
665 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
667 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
670 while( i_data < i_read )
673 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
674 int i_copy = __MIN( i_current, i_read - i_data);
680 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
686 p_sys->block.i_offset += i_copy;
687 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
689 /* Current block is now empty, switch to next */
690 if( p_sys->block.p_current )
692 p_sys->block.i_offset = 0;
693 p_sys->block.p_current = p_sys->block.p_current->p_next;
696 if( AStreamRefillBlock( s ) )
703 p_sys->i_pos += i_data;
707 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, int i_read )
709 stream_sys_t *p_sys = s->p_sys;
715 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
717 /* We can directly give a pointer over our buffer */
718 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
720 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
724 /* We need to create a local copy */
725 if( p_sys->i_peek < i_read )
727 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
733 p_sys->i_peek = i_read;
736 /* Fill enough data */
737 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
740 block_t **pp_last = p_sys->block.pp_last;
742 if( AStreamRefillBlock( s ) ) break;
744 /* Our buffer are probably filled enough, don't try anymore */
745 if( pp_last == p_sys->block.pp_last ) break;
748 /* Copy what we have */
749 b = p_sys->block.p_current;
750 i_offset = p_sys->block.i_offset;
751 p_data = p_sys->p_peek;
753 while( b && i_data < i_read )
755 int i_current = b->i_buffer - i_offset;
756 int i_copy = __MIN( i_current, i_read - i_data );
758 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
763 if( i_offset >= b->i_buffer )
770 *pp_peek = p_sys->p_peek;
774 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
776 stream_sys_t *p_sys = s->p_sys;
777 access_t *p_access = p_sys->p_access;
778 int64_t i_offset = i_pos - p_sys->block.i_start;
781 /* We already have thoses data, just update p_current/i_offset */
782 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
784 block_t *b = p_sys->block.p_first;
787 while( i_current + b->i_buffer < i_offset )
789 i_current += b->i_buffer;
793 p_sys->block.p_current = b;
794 p_sys->block.i_offset = i_offset - i_current;
796 p_sys->i_pos = i_pos;
801 /* We may need to seek or to read data */
805 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
809 msg_Err( s, "backward seeking impossible (access not seekable)" );
817 vlc_bool_t b_aseek, b_aseekfast;
819 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
820 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
825 msg_Warn( s, I64Fd" bytes need to be skipped "
826 "(access non seekable)",
827 i_offset - p_sys->block.i_size );
831 int64_t i_skip = i_offset - p_sys->block.i_size;
833 /* Avg bytes per packets */
834 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
835 /* TODO compute a seek cost instead of fixed threshold */
836 int i_th = b_aseekfast ? 1 : 5;
838 if( i_skip <= i_th * i_avg &&
839 i_skip < STREAM_CACHE_SIZE )
844 msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
845 b_seek, i_th*i_avg, i_skip );
851 int64_t i_start, i_end;
852 /* Do the access seek */
854 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
858 block_ChainRelease( p_sys->block.p_first );
861 p_sys->block.i_start = p_sys->i_pos = i_pos;
862 p_sys->block.i_offset = 0;
863 p_sys->block.p_current = NULL;
864 p_sys->block.i_size = 0;
865 p_sys->block.p_first = NULL;
866 p_sys->block.pp_last = &p_sys->block.p_first;
869 if( AStreamRefillBlock( s ) )
871 msg_Err( s, "cannot re fill buffer" );
875 p_sys->stat.i_seek_time += i_end - i_start;
876 p_sys->stat.i_seek_count++;
881 /* Read enough data */
882 while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
884 if( AStreamRefillBlock( s ) )
886 msg_Err( s, "can't read enough data in seek" );
889 while( p_sys->block.p_current &&
890 p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
892 p_sys->i_pos += p_sys->block.p_current->i_buffer;
893 p_sys->block.p_current = p_sys->block.p_current->p_next;
897 p_sys->block.i_offset = i_pos - p_sys->i_pos;
898 p_sys->i_pos = i_pos;
907 static int AStreamRefillBlock( stream_t *s )
909 stream_sys_t *p_sys = s->p_sys;
910 int64_t i_start, i_stop;
914 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
915 p_sys->block.p_first != p_sys->block.p_current )
917 block_t *b = p_sys->block.p_first;
919 p_sys->block.i_start += b->i_buffer;
920 p_sys->block.i_size -= b->i_buffer;
921 p_sys->block.p_first = b->p_next;
925 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
926 p_sys->block.p_current == p_sys->block.p_first &&
927 p_sys->block.p_current->p_next ) /* At least 2 packets */
929 /* Enough data, don't read more */
933 /* Now read a new block */
939 if( s->b_die ) return VLC_EGENERIC;
943 if( ( b = AReadBlock( s, &b_eof ) ) ) break;
945 if( b_eof ) return VLC_EGENERIC;
947 msleep( STREAM_DATA_WAIT );
954 /* Append the block */
955 p_sys->block.i_size += b->i_buffer;
956 *p_sys->block.pp_last = b;
957 p_sys->block.pp_last = &b->p_next;
960 if( p_sys->block.p_current == NULL )
961 p_sys->block.p_current = b;
964 p_sys->stat.i_bytes += b->i_buffer;
965 p_sys->stat.i_read_time += i_stop - i_start;
966 p_sys->stat.i_read_count++;
975 /****************************************************************************
977 ****************************************************************************/
978 static int AStreamRefillStream( stream_t *s );
980 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
982 stream_sys_t *p_sys = s->p_sys;
983 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
985 uint8_t *p_data = (uint8_t *)p_read;
988 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
992 /* seek within this stream if possible, else use plain old read and discard */
993 stream_sys_t *p_sys = s->p_sys;
994 access_t *p_access = p_sys->p_access;
996 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
998 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1002 msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
1003 " offset=%d end="I64Fd,
1004 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1005 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1008 while( i_data < i_read )
1010 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1011 STREAM_CACHE_TRACK_SIZE;
1013 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1014 STREAM_CACHE_TRACK_SIZE - i_off );
1015 int i_copy = __MIN( i_current, i_read - i_data );
1017 if( i_copy <= 0 ) break; /* EOF */
1020 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1023 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1027 p_sys->stream.i_offset += i_copy;
1029 /* Update pos now */
1030 p_sys->i_pos += i_copy;
1033 p_sys->stream.i_used += i_copy;
1034 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1035 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1037 if( AStreamRefillStream( s ) )
1040 if( tk->i_start >= tk->i_end ) break;
1048 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, int i_read )
1050 stream_sys_t *p_sys = s->p_sys;
1051 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1054 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1057 msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
1058 "start="I64Fd" offset=%d end="I64Fd,
1059 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1060 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1063 /* Avoid problem, but that should *never* happen */
1064 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1065 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1067 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1069 if( p_sys->stream.i_used <= 1 )
1071 /* Be sure we will read something */
1072 p_sys->stream.i_used += i_read -
1073 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1075 if( AStreamRefillStream( s ) ) break;
1078 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1079 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1081 /* Now, direct pointer or a copy ? */
1082 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1083 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1085 *pp_peek = &tk->p_buffer[i_off];
1089 if( p_sys->i_peek < i_read )
1091 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1092 if( !p_sys->p_peek )
1097 p_sys->i_peek = i_read;
1100 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1101 STREAM_CACHE_TRACK_SIZE - i_off );
1102 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1103 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1105 *pp_peek = p_sys->p_peek;
1109 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1111 stream_sys_t *p_sys = s->p_sys;
1112 access_t *p_access = p_sys->p_access;
1114 vlc_bool_t b_afastseek;
1120 msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
1121 " tk=%d start="I64Fd" offset=%d end="I64Fd,
1122 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1123 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1124 p_sys->stream.i_offset,
1125 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1129 /* Seek in our current track ? */
1130 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1131 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1133 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1135 msg_Dbg( s, "AStreamSeekStream: current track" );
1137 p_sys->i_pos = i_pos;
1138 p_sys->stream.i_offset = i_pos - tk->i_start;
1140 /* If there is not enough data left in the track, refill */
1141 /* \todo How to get a correct value for
1142 * - refilling threshold
1143 * - how much to refill
1145 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1146 p_sys->stream.i_read_size )
1148 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1150 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1151 AStreamRefillStream( s );
1157 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1160 /* We can't do nothing */
1161 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1162 return VLC_EGENERIC;
1165 /* Date the current track */
1166 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1168 /* Try to reuse already read data */
1169 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1171 stream_track_t *tk = &p_sys->stream.tk[i];
1173 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1176 msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
1177 " end="I64Fd, i, tk->i_start, tk->i_end );
1180 /* Seek at the end of the buffer */
1181 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1184 p_sys->i_pos = i_pos;
1185 p_sys->stream.i_tk = i;
1186 p_sys->stream.i_offset = i_pos - tk->i_start;
1188 if( p_sys->stream.i_used < 1024 )
1189 p_sys->stream.i_used = 1024;
1191 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1192 return VLC_EGENERIC;
1198 access2_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1199 /* FIXME compute seek cost (instead of static 'stupid' value) */
1200 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1206 /* Search closest segment TODO */
1207 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1209 stream_track_t *tk = &p_sys->stream.tk[i];
1211 if( i_pos + i_maxth >= tk->i_start )
1213 msg_Dbg( s, "good segment before current pos, TODO" );
1215 if( i_pos - i_maxth <= tk->i_end )
1217 msg_Dbg( s, "good segment after current pos, TODO" );
1222 /* Nothing good, seek and choose oldest segment */
1223 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1224 p_sys->i_pos = i_pos;
1227 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1229 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1233 /* Reset the segment */
1234 p_sys->stream.i_tk = i_new;
1235 p_sys->stream.i_offset = 0;
1236 p_sys->stream.tk[i_new].i_start = i_pos;
1237 p_sys->stream.tk[i_new].i_end = i_pos;
1240 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1241 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1243 if( AStreamRefillStream( s ) )
1244 return VLC_EGENERIC;
1249 static int AStreamRefillStream( stream_t *s )
1251 stream_sys_t *p_sys = s->p_sys;
1252 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1254 /* We read but won't increase i_start after initial start + offset */
1256 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1257 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1258 vlc_bool_t b_read = VLC_FALSE;
1259 int64_t i_start, i_stop;
1261 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1264 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1265 p_sys->stream.i_used, i_toread );
1269 while( i_toread > 0 )
1271 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1275 return VLC_EGENERIC;
1277 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1278 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1280 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1283 msleep( STREAM_DATA_WAIT );
1286 else if( i_read == 0 )
1288 if( !b_read ) return VLC_EGENERIC;
1294 tk->i_end += i_read;
1296 /* Windows of STREAM_CACHE_TRACK_SIZE */
1297 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1299 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1301 tk->i_start += i_invalid;
1302 p_sys->stream.i_offset -= i_invalid;
1306 p_sys->stream.i_used -= i_read;
1308 p_sys->stat.i_bytes += i_read;
1309 p_sys->stat.i_read_count++;
1313 p_sys->stat.i_read_time += i_stop - i_start;
1318 static void AStreamPrebufferStream( stream_t *s )
1320 stream_sys_t *p_sys = s->p_sys;
1321 access_t *p_access = p_sys->p_access;
1323 int64_t i_first = 0;
1325 int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1326 ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1327 STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1329 msg_Dbg( s, "pre-buffering..." );
1333 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1335 int64_t i_date = mdate();
1338 if( s->b_die || tk->i_end >= i_prebuffer ||
1339 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1344 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1345 p_sys->stat.i_read_time = i_date - i_start;
1346 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
1347 (p_sys->stat.i_read_time+1);
1349 msg_Dbg( s, "pre-buffering done "I64Fd" bytes in "I64Fd"s - "
1351 p_sys->stat.i_bytes,
1352 p_sys->stat.i_read_time / I64C(1000000),
1353 i_byterate / 1024 );
1358 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1359 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1360 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1363 msleep( STREAM_DATA_WAIT );
1366 else if( i_read == 0 )
1375 msg_Dbg( s, "received first data for our buffer");
1378 tk->i_end += i_read;
1380 p_sys->stat.i_read_count++;
1385 /****************************************************************************
1387 ****************************************************************************/
1389 * Read from the stream untill first newline.
1390 * \param s Stream handle to read from
1391 * \return A pointer to the allocated output string. You need to free this when you are done.
1393 #define STREAM_PROBE_LINE 2048
1394 #define STREAM_LINE_MAX (2048*100)
1395 char * stream_ReadLine( stream_t *s )
1397 char *p_line = NULL;
1398 int i_line = 0, i_read = 0;
1400 while( i_read < STREAM_LINE_MAX )
1403 const uint8_t *p_data;
1407 /* Probe new data */
1408 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1409 if( i_data <= 0 ) break; /* No more data */
1412 i_pos = stream_Tell( s );
1413 if( i_pos == 0 && i_data > 4 )
1416 char *psz_encoding = NULL;
1418 if( p_data[0] == 0xEF && p_data[1] == 0xBB && p_data[2] == 0xBF )
1420 psz_encoding = strdup( "UTF-8" );
1423 else if( p_data[0] == 0x00 && p_data[1] == 0x00 )
1425 if( p_data[2] == 0xFE && p_data[3] == 0xFF )
1427 psz_encoding = strdup( "UTF-32BE" );
1428 s->i_char_width = 4;
1432 else if( p_data[0] == 0xFF && p_data[1] == 0xFE )
1434 if( p_data[2] == 0x00 && p_data[3] == 0x00 )
1436 psz_encoding = strdup( "UTF-32LE" );
1437 s->i_char_width = 4;
1438 s->b_little_endian = VLC_TRUE;
1443 psz_encoding = strdup( "UTF-16LE" );
1444 s->b_little_endian = VLC_TRUE;
1445 s->i_char_width = 2;
1449 else if( p_data[0] == 0xFE && p_data[1] == 0xFF )
1451 psz_encoding = strdup( "UTF-16BE" );
1452 s->i_char_width = 2;
1456 /* Seek past the BOM */
1459 stream_Seek( s, i_bom_size );
1460 p_data += i_bom_size;
1461 i_data -= i_bom_size;
1464 /* Open the converter if we need it */
1465 if( psz_encoding != NULL )
1467 input_thread_t *p_input;
1468 msg_Dbg( s, "%s BOM detected", psz_encoding );
1469 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1470 if( s->i_char_width > 1 )
1472 s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1473 if( s->conv == (vlc_iconv_t)-1 )
1475 msg_Err( s, "iconv_open failed" );
1478 if( p_input != NULL)
1480 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1481 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1482 vlc_object_release( p_input );
1484 if( psz_encoding ) free( psz_encoding );
1488 if( i_data % s->i_char_width )
1490 /* keep i_char_width boundary */
1491 i_data = i_data - ( i_data % s->i_char_width );
1492 msg_Warn( s, "the read is not i_char_width compatible");
1498 /* Check if there is an EOL */
1499 if( s->i_char_width == 1 )
1501 /* UTF-8: 0A <LF> */
1502 psz_eol = memchr( p_data, '\n', i_data );
1506 const uint8_t *p = p_data;
1507 const uint8_t *p_last = p + i_data - s->i_char_width;
1509 if( s->i_char_width == 2 )
1511 if( s->b_little_endian == VLC_TRUE)
1513 /* UTF-16LE: 0A 00 <LF> */
1514 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1519 /* UTF-16BE: 00 0A <LF> */
1520 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1524 else if( s->i_char_width == 4 )
1526 if( s->b_little_endian == VLC_TRUE)
1528 /* UTF-32LE: 0A 00 00 00 <LF> */
1529 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ||
1530 p[2] != 0x00 || p[3] != 0x00 ) )
1535 /* UTF-32BE: 00 00 00 0A <LF> */
1536 while( p <= p_last && ( p[3] != 0x0A || p[2] != 0x00 ||
1537 p[1] != 0x00 || p[0] != 0x00 ) )
1548 psz_eol = (char *)p + ( s->i_char_width - 1 );
1554 i_data = (psz_eol - (char *)p_data) + 1;
1555 p_line = realloc( p_line, i_line + i_data + s->i_char_width ); /* add \0 */
1556 i_data = stream_Read( s, &p_line[i_line], i_data );
1557 if( i_data <= 0 ) break; /* Hmmm */
1558 i_line += i_data - s->i_char_width; /* skip \n */;
1561 /* We have our line */
1565 /* Read data (+1 for easy \0 append) */
1566 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->i_char_width );
1567 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1568 if( i_data <= 0 ) break; /* Hmmm */
1576 for( j = 0; j < s->i_char_width; j++ )
1578 p_line[i_line + j] = '\0';
1580 i_line += s->i_char_width; /* the added \0 */
1581 if( s->i_char_width > 1 )
1583 size_t i_in = 0, i_out = 0;
1584 const char * p_in = NULL;
1585 char * p_out = NULL;
1586 char * psz_new_line = NULL;
1589 psz_new_line = malloc( i_line );
1591 i_in = i_out = (size_t)i_line;
1593 p_out = psz_new_line;
1595 if( vlc_iconv( s->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1597 msg_Err( s, "iconv failed" );
1598 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1600 if( p_line ) free( p_line );
1601 p_line = psz_new_line;
1602 i_line = (size_t)i_line - i_out; /* does not include \0 */
1605 /* Remove trailing LF/CR */
1606 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1607 p_line[i_line-2] == '\n') ) i_line--;
1609 /* Make sure the \0 is there */
1610 p_line[i_line-1] = '\0';
1615 /* We failed to read any data, probably EOF */
1616 if( p_line ) free( p_line );
1617 if( s->conv != (vlc_iconv_t)(-1) ) vlc_iconv_close( s->conv );
1621 /****************************************************************************
1622 * Access reading/seeking wrappers to handle concatenated streams.
1623 ****************************************************************************/
1624 static int AReadStream( stream_t *s, void *p_read, int i_read )
1626 stream_sys_t *p_sys = s->p_sys;
1627 access_t *p_access = p_sys->p_access;
1628 input_thread_t *p_input = NULL;
1629 int i_read_orig = i_read;
1632 if( s->p_parent && s->p_parent->p_parent &&
1633 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1634 p_input = (input_thread_t *)s->p_parent->p_parent;
1636 if( !p_sys->i_list )
1638 i_read = p_access->pf_read( p_access, p_read, i_read );
1641 vlc_object_yield( p_input );
1642 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1643 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
1645 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1646 (float)i_total, NULL );
1647 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1648 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1649 vlc_object_release( p_input );
1654 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1657 /* If we reached an EOF then switch to the next stream in the list */
1658 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1660 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1661 access_t *p_list_access;
1663 msg_Dbg( s, "opening input `%s'", psz_name );
1665 p_list_access = access2_New( s, p_access->psz_access, "", psz_name, 0 );
1667 if( !p_list_access ) return 0;
1669 if( p_sys->p_list_access != p_access )
1670 access2_Delete( p_sys->p_list_access );
1672 p_sys->p_list_access = p_list_access;
1674 /* We have to read some data */
1675 return AReadStream( s, p_read, i_read_orig );
1678 /* Update read bytes in input */
1681 vlc_object_yield( p_input );
1682 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1683 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
1684 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1685 (float)i_total, NULL );
1686 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1687 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1688 vlc_object_release( p_input );
1693 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof )
1695 stream_sys_t *p_sys = s->p_sys;
1696 access_t *p_access = p_sys->p_access;
1697 input_thread_t *p_input = NULL;
1702 if( s->p_parent && s->p_parent->p_parent &&
1703 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1704 p_input = (input_thread_t *)s->p_parent->p_parent;
1706 if( !p_sys->i_list )
1708 p_block = p_access->pf_block( p_access );
1709 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1710 if( p_input && p_block && p_access->p_libvlc->b_stats )
1712 vlc_object_yield( p_input );
1713 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1714 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1715 p_block->i_buffer, &i_total );
1716 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1717 (float)i_total, NULL );
1718 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1719 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1720 vlc_object_release( p_input );
1725 p_block = p_sys->p_list_access->pf_block( p_access );
1726 b_eof = p_sys->p_list_access->info.b_eof;
1727 if( pb_eof ) *pb_eof = b_eof;
1729 /* If we reached an EOF then switch to the next stream in the list */
1730 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1732 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1733 access_t *p_list_access;
1735 msg_Dbg( s, "opening input `%s'", psz_name );
1737 p_list_access = access2_New( s, p_access->psz_access, "", psz_name, 0 );
1739 if( !p_list_access ) return 0;
1741 if( p_sys->p_list_access != p_access )
1742 access2_Delete( p_sys->p_list_access );
1744 p_sys->p_list_access = p_list_access;
1746 /* We have to read some data */
1747 return AReadBlock( s, pb_eof );
1753 vlc_object_yield( p_input );
1754 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1755 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1756 p_block->i_buffer, &i_total );
1757 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1758 (float)i_total, NULL );
1759 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
1761 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1762 vlc_object_release( p_input );
1768 static int ASeek( stream_t *s, int64_t i_pos )
1770 stream_sys_t *p_sys = s->p_sys;
1771 access_t *p_access = p_sys->p_access;
1773 /* Check which stream we need to access */
1779 access_t *p_list_access = 0;
1781 for( i = 0; i < p_sys->i_list - 1; i++ )
1783 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1784 i_size += p_sys->list[i]->i_size;
1786 psz_name = p_sys->list[i]->psz_path;
1788 if( i != p_sys->i_list_index )
1789 msg_Dbg( s, "opening input `%s'", psz_name );
1791 if( i != p_sys->i_list_index && i != 0 )
1794 access2_New( s, p_access->psz_access, "", psz_name, 0 );
1796 else if( i != p_sys->i_list_index )
1798 p_list_access = p_access;
1803 if( p_sys->p_list_access != p_access )
1804 access2_Delete( p_sys->p_list_access );
1806 p_sys->p_list_access = p_list_access;
1809 p_sys->i_list_index = i;
1810 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1814 return p_access->pf_seek( p_access, i_pos );
1819 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
1820 * "p_read" is NULL then data are skipped instead of read. The return
1821 * value is the real numbers of bytes read/skip. If this value is less
1822 * than i_read that means that it's the end of the stream.
1824 int stream_Read( stream_t *s, void *p_read, int i_read )
1826 return s->pf_read( s, p_read, i_read );
1830 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
1831 * \return The real numbers of valid bytes, if it's less
1832 * or equal to 0, *pp_peek is invalid.
1833 * \note pp_peek is a pointer to internal buffer and it will be invalid as
1834 * soons as other stream_* functions are called.
1835 * \note Due to input limitation, it could be less than i_peek without meaning
1836 * the end of the stream (but only when you have i_peek >=
1837 * p_input->i_bufsize)
1839 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
1841 return s->pf_peek( s, pp_peek, i_peek );
1845 * Use to control the "stream_t *". Look at #stream_query_e for
1846 * possible "i_query" value and format arguments. Return VLC_SUCCESS
1847 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
1849 int stream_vaControl( stream_t *s, int i_query, va_list args )
1851 return s->pf_control( s, i_query, args );
1857 void stream_Delete( stream_t *s )
1862 int stream_Control( stream_t *s, int i_query, ... )
1868 return VLC_EGENERIC;
1870 va_start( args, i_query );
1871 i_result = s->pf_control( s, i_query, args );
1877 * Read "i_size" bytes and store them in a block_t.
1878 * It always read i_size bytes unless you are at the end of the stream
1879 * where it return what is available.
1881 block_t *stream_Block( stream_t *s, int i_size )
1883 if( i_size <= 0 ) return NULL;
1885 /* emulate block read */
1886 block_t *p_bk = block_New( s, i_size );
1889 p_bk->i_buffer = stream_Read( s, p_bk->p_buffer, i_size );
1890 if( p_bk->i_buffer > 0 )
1895 if( p_bk ) block_Release( p_bk );