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 *****************************************************************************/
30 #include "input_internal.h"
35 * - tune the 2 methods
36 * - compute cost for seek
37 * - improve stream mode seeking with closest segments
43 * One linked list of data read
45 * More complex scheme using mutliple track to avoid seeking
48 /* How many tracks we have, currently only used for stream mode */
49 #ifdef OPTIMIZE_MEMORY
50 # define STREAM_CACHE_TRACK 1
51 /* Max size of our cache 128Ko per track */
52 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
54 # define STREAM_CACHE_TRACK 3
55 /* Max size of our cache 4Mo per track */
56 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
59 /* How many data we try to prebuffer */
60 #define STREAM_CACHE_PREBUFFER_SIZE (32767)
61 /* Maximum time we take to pre-buffer */
62 #define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
64 /* Method1: Simple, for pf_block.
65 * We get blocks and put them in the linked list.
66 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
68 #define STREAM_DATA_WAIT 40000 /* Time between before a pf_block retry */
70 /* Method2: A bit more complex, for pf_read
71 * - We use ring buffers, only one if unseekable, all if seekable
72 * - Upon seek date current ring, then search if one ring match the pos,
73 * yes: switch to it, seek the access to match the end of the ring
74 * no: search the ring with i_end the closer to i_pos,
75 * if close enough, read data and use this ring
76 * else use the oldest ring, seek and use it.
78 * TODO: - with access non seekable: use all space available for only one ring, but
79 * we have to support seekable/non-seekable switch on the fly.
80 * - compute a good value for i_read_size
83 #define STREAM_READ_ATONCE 32767
84 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
108 bool b_block; /* Block method (1) or stream */
110 int64_t i_pos; /* Current reading offset */
112 /* Method 1: pf_block */
115 int64_t i_start; /* Offset of block for p_first */
116 int i_offset; /* Offset for data in p_current */
117 block_t *p_current; /* Current block */
119 int i_size; /* Total amount of data in the list */
125 /* Method 2: for pf_read */
128 int i_offset; /* Buffer offset in the current track */
129 int i_tk; /* Current track */
130 stream_track_t tk[STREAM_CACHE_TRACK];
136 int i_used; /* Used since last read */
141 /* Peek temporary buffer */
145 /* Stat for both method */
148 bool b_fastseek; /* From access */
150 /* Stat about reading data */
151 int64_t i_read_count;
155 /* Stat about seek */
163 access_entry_t **list;
165 access_t *p_list_access;
167 /* Preparse mode ? */
172 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read );
173 static int AStreamPeekBlock( stream_t *s, const uint8_t **p_peek, int i_read );
174 static int AStreamSeekBlock( stream_t *s, int64_t i_pos );
175 static void AStreamPrebufferBlock( stream_t *s );
176 static block_t *AReadBlock( stream_t *s, bool *pb_eof );
179 static int AStreamReadStream( stream_t *s, void *p_read, int i_read );
180 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, int i_read );
181 static int AStreamSeekStream( stream_t *s, int64_t i_pos );
182 static void AStreamPrebufferStream( stream_t *s );
183 static int AReadStream( stream_t *s, void *p_read, int i_read );
186 static int AStreamControl( stream_t *s, int i_query, va_list );
187 static void AStreamDestroy( stream_t *s );
188 static void UStreamDestroy( stream_t *s );
189 static int ASeek( stream_t *s, int64_t i_pos );
192 /****************************************************************************
193 * stream_UrlNew: create a stream from a access
194 ****************************************************************************/
195 stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
197 const char *psz_access, *psz_demux;
202 if( !psz_url ) return 0;
204 char psz_dup[strlen (psz_url) + 1];
205 strcpy (psz_dup, psz_url);;
206 MRLSplit( psz_dup, &psz_access, &psz_demux, &psz_path );
208 /* Now try a real access */
209 p_access = access_New( p_parent, psz_access, psz_demux, psz_path );
211 if( p_access == NULL )
213 msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
217 if( !( p_res = stream_AccessNew( p_access, true ) ) )
219 access_Delete( p_access );
223 p_res->pf_destroy = UStreamDestroy;
227 stream_t *stream_AccessNew( access_t *p_access, bool b_quick )
229 stream_t *s = vlc_stream_create( VLC_OBJECT(p_access) );
231 char *psz_list = NULL;
233 if( !s ) return NULL;
235 /* Attach it now, needed for b_die */
236 vlc_object_attach( s, p_access );
238 s->pf_read = NULL; /* Set up later */
240 s->pf_control = AStreamControl;
241 s->pf_destroy = AStreamDestroy;
243 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
246 msg_Err( s, "Out of memory when allocating stream_sys_t" );
250 /* UTF16 and UTF32 text file conversion */
252 s->b_little_endian = false;
253 s->conv = (vlc_iconv_t)(-1);
256 p_sys->p_access = p_access;
257 p_sys->b_block = p_access->pf_block ? true : false;
258 p_sys->i_pos = p_access->info.i_pos;
261 access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
262 p_sys->stat.i_bytes = 0;
263 p_sys->stat.i_read_time = 0;
264 p_sys->stat.i_read_count = 0;
265 p_sys->stat.i_seek_count = 0;
266 p_sys->stat.i_seek_time = 0;
270 p_sys->i_list_index = 0;
271 p_sys->p_list_access = 0;
273 p_sys->b_quick = b_quick;
275 /* Get the additional list of inputs if any (for concatenation) */
276 if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
278 access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
279 if( p_entry == NULL )
281 msg_Err( s, "Out of memory when allocating access_entry_t" );
284 char *psz_name, *psz_parser = psz_name = psz_list;
286 p_sys->p_list_access = p_access;
287 p_entry->i_size = p_access->info.i_size;
288 p_entry->psz_path = strdup( p_access->psz_path );
289 if( p_entry->psz_path == NULL )
291 msg_Err( s, "Out of memory when duplicating p_access->psz_path" );
295 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
296 msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
297 p_entry->psz_path, p_access->info.i_size );
299 while( psz_name && *psz_name )
301 psz_parser = strchr( psz_name, ',' );
302 if( psz_parser ) *psz_parser = 0;
304 psz_name = strdup( psz_name );
307 access_t *p_tmp = access_New( p_access, p_access->psz_access,
312 psz_name = psz_parser;
313 if( psz_name ) psz_name++;
317 msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
318 psz_name, p_tmp->info.i_size );
320 p_entry = malloc( sizeof(access_entry_t) );
321 if( p_entry == NULL )
323 msg_Err( p_access, "Out of memory when allocating access_entry_t" );
326 p_entry->i_size = p_tmp->info.i_size;
327 p_entry->psz_path = psz_name;
328 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
330 access_Delete( p_tmp );
333 psz_name = psz_parser;
334 if( psz_name ) psz_name++;
337 FREENULL( psz_list );
341 p_sys->p_peek = NULL;
345 s->pf_read = AStreamReadBlock;
346 s->pf_peek = AStreamPeekBlock;
348 /* Init all fields of p_sys->block */
349 p_sys->block.i_start = p_sys->i_pos;
350 p_sys->block.i_offset = 0;
351 p_sys->block.p_current = NULL;
352 p_sys->block.i_size = 0;
353 p_sys->block.p_first = NULL;
354 p_sys->block.pp_last = &p_sys->block.p_first;
356 /* Do the prebuffering */
357 AStreamPrebufferBlock( s );
359 if( p_sys->block.i_size <= 0 )
361 msg_Err( s, "cannot pre fill buffer" );
369 s->pf_read = AStreamReadStream;
370 s->pf_peek = AStreamPeekStream;
372 /* Allocate/Setup our tracks */
373 p_sys->stream.i_offset = 0;
374 p_sys->stream.i_tk = 0;
375 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
376 if( p_sys->stream.p_buffer == NULL )
378 msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
382 p_sys->stream.i_used = 0;
383 access_Control( p_access, ACCESS_GET_MTU,
384 &p_sys->stream.i_read_size );
385 if( p_sys->stream.i_read_size <= 0 )
386 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
387 else if( p_sys->stream.i_read_size <= 256 )
388 p_sys->stream.i_read_size = 256;
390 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
392 p_sys->stream.tk[i].i_date = 0;
393 p_sys->stream.tk[i].i_start = p_sys->i_pos;
394 p_sys->stream.tk[i].i_end = p_sys->i_pos;
395 p_sys->stream.tk[i].p_buffer=
396 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
399 /* Do the prebuffering */
400 AStreamPrebufferStream( s );
402 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
404 msg_Err( s, "cannot pre fill buffer" );
418 free( p_sys->stream.p_buffer );
420 while( p_sys->i_list > 0 )
421 free( p_sys->list[--(p_sys->i_list)] );
425 vlc_object_detach( s );
426 vlc_object_release( s );
430 /****************************************************************************
432 ****************************************************************************/
433 static void AStreamDestroy( stream_t *s )
435 stream_sys_t *p_sys = s->p_sys;
437 vlc_object_detach( s );
439 if( p_sys->b_block ) block_ChainRelease( p_sys->block.p_first );
440 else free( p_sys->stream.p_buffer );
442 free( p_sys->p_peek );
444 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
445 access_Delete( p_sys->p_list_access );
447 while( p_sys->i_list-- )
449 free( p_sys->list[p_sys->i_list]->psz_path );
450 free( p_sys->list[p_sys->i_list] );
455 vlc_object_release( s );
458 static void UStreamDestroy( stream_t *s )
460 access_t *p_access = (access_t*)vlc_object_find( s, VLC_OBJECT_ACCESS, FIND_PARENT );
462 vlc_object_release( p_access );
463 access_Delete( p_access );
466 /****************************************************************************
467 * stream_AccessReset:
468 ****************************************************************************/
469 void stream_AccessReset( stream_t *s )
471 stream_sys_t *p_sys = s->p_sys;
473 p_sys->i_pos = p_sys->p_access->info.i_pos;
477 block_ChainRelease( p_sys->block.p_first );
479 /* Init all fields of p_sys->block */
480 p_sys->block.i_start = p_sys->i_pos;
481 p_sys->block.i_offset = 0;
482 p_sys->block.p_current = NULL;
483 p_sys->block.i_size = 0;
484 p_sys->block.p_first = NULL;
485 p_sys->block.pp_last = &p_sys->block.p_first;
487 /* Do the prebuffering */
488 AStreamPrebufferBlock( s );
494 /* Setup our tracks */
495 p_sys->stream.i_offset = 0;
496 p_sys->stream.i_tk = 0;
497 p_sys->stream.i_used = 0;
499 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
501 p_sys->stream.tk[i].i_date = 0;
502 p_sys->stream.tk[i].i_start = p_sys->i_pos;
503 p_sys->stream.tk[i].i_end = p_sys->i_pos;
506 /* Do the prebuffering */
507 AStreamPrebufferStream( s );
511 /****************************************************************************
512 * stream_AccessUpdate:
513 ****************************************************************************/
514 void stream_AccessUpdate( stream_t *s )
516 stream_sys_t *p_sys = s->p_sys;
518 p_sys->i_pos = p_sys->p_access->info.i_pos;
523 for( i = 0; i < p_sys->i_list_index; i++ )
525 p_sys->i_pos += p_sys->list[i]->i_size;
530 /****************************************************************************
532 ****************************************************************************/
533 static int AStreamControl( stream_t *s, int i_query, va_list args )
535 stream_sys_t *p_sys = s->p_sys;
536 access_t *p_access = p_sys->p_access;
539 int64_t *pi_64, i_64;
544 case STREAM_GET_SIZE:
545 pi_64 = (int64_t*)va_arg( args, int64_t * );
546 if( s->p_sys->i_list )
550 for( i = 0; i < s->p_sys->i_list; i++ )
551 *pi_64 += s->p_sys->list[i]->i_size;
554 *pi_64 = p_access->info.i_size;
557 case STREAM_CAN_SEEK:
558 p_bool = (bool*)va_arg( args, bool * );
559 access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
562 case STREAM_CAN_FASTSEEK:
563 p_bool = (bool*)va_arg( args, bool * );
564 access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
567 case STREAM_GET_POSITION:
568 pi_64 = (int64_t*)va_arg( args, int64_t * );
569 *pi_64 = p_sys->i_pos;
572 case STREAM_SET_POSITION:
573 i_64 = (int64_t)va_arg( args, int64_t );
575 return AStreamSeekBlock( s, i_64 );
577 return AStreamSeekStream( s, i_64 );
582 case STREAM_CONTROL_ACCESS:
583 i_int = (int) va_arg( args, int );
584 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
585 i_int != ACCESS_SET_PRIVATE_ID_CA &&
586 i_int != ACCESS_GET_PRIVATE_ID_STATE )
588 msg_Err( s, "Hey, what are you thinking ?"
589 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
592 return access_vaControl( p_access, i_int, args );
594 case STREAM_GET_CONTENT_TYPE:
595 return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
596 va_arg( args, char ** ) );
599 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
607 /****************************************************************************
609 ****************************************************************************/
610 static void AStreamPrebufferBlock( stream_t *s )
612 stream_sys_t *p_sys = s->p_sys;
613 access_t *p_access = p_sys->p_access;
618 msg_Dbg( s, "pre buffering" );
622 int64_t i_date = mdate();
626 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
627 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
632 p_sys->stat.i_bytes = p_sys->block.i_size;
633 p_sys->stat.i_read_time = i_date - i_start;
634 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
635 (p_sys->stat.i_read_time + 1);
637 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
640 p_sys->stat.i_read_time / I64C(1000000),
646 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
650 msleep( STREAM_DATA_WAIT );
656 /* Append the block */
657 p_sys->block.i_size += b->i_buffer;
658 *p_sys->block.pp_last = b;
659 p_sys->block.pp_last = &b->p_next;
661 p_sys->stat.i_read_count++;
665 if( p_access->info.b_prebuffered )
667 /* Access has already prebufferred - update stats and exit */
668 p_sys->stat.i_bytes = p_sys->block.i_size;
669 p_sys->stat.i_read_time = mdate() - i_start;
676 msg_Dbg( s, "received first data for our buffer");
681 p_sys->block.p_current = p_sys->block.p_first;
684 static int AStreamRefillBlock( stream_t *s );
686 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
688 stream_sys_t *p_sys = s->p_sys;
690 uint8_t *p_data= (uint8_t*)p_read;
694 if( p_sys->block.p_current == NULL )
699 /* seek within this stream if possible, else use plain old read and discard */
700 stream_sys_t *p_sys = s->p_sys;
701 access_t *p_access = p_sys->p_access;
703 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
705 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
708 while( i_data < i_read )
711 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
712 int i_copy = __MIN( i_current, i_read - i_data);
718 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
724 p_sys->block.i_offset += i_copy;
725 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
727 /* Current block is now empty, switch to next */
728 if( p_sys->block.p_current )
730 p_sys->block.i_offset = 0;
731 p_sys->block.p_current = p_sys->block.p_current->p_next;
733 /*Get a new block if needed */
734 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
741 p_sys->i_pos += i_data;
745 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, int i_read )
747 stream_sys_t *p_sys = s->p_sys;
753 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
755 /* We can directly give a pointer over our buffer */
756 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
758 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
762 /* We need to create a local copy */
763 if( p_sys->i_peek < i_read )
765 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
771 p_sys->i_peek = i_read;
774 /* Fill enough data */
775 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
778 block_t **pp_last = p_sys->block.pp_last;
780 if( AStreamRefillBlock( s ) ) break;
782 /* Our buffer are probably filled enough, don't try anymore */
783 if( pp_last == p_sys->block.pp_last ) break;
786 /* Copy what we have */
787 b = p_sys->block.p_current;
788 i_offset = p_sys->block.i_offset;
789 p_data = p_sys->p_peek;
791 while( b && i_data < i_read )
793 int i_current = b->i_buffer - i_offset;
794 int i_copy = __MIN( i_current, i_read - i_data );
796 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
801 if( i_offset >= b->i_buffer )
808 *pp_peek = p_sys->p_peek;
812 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
814 stream_sys_t *p_sys = s->p_sys;
815 access_t *p_access = p_sys->p_access;
816 int64_t i_offset = i_pos - p_sys->block.i_start;
819 /* We already have thoses data, just update p_current/i_offset */
820 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
822 block_t *b = p_sys->block.p_first;
825 while( i_current + b->i_buffer < i_offset )
827 i_current += b->i_buffer;
831 p_sys->block.p_current = b;
832 p_sys->block.i_offset = i_offset - i_current;
834 p_sys->i_pos = i_pos;
839 /* We may need to seek or to read data */
843 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
847 msg_Err( s, "backward seeking impossible (access not seekable)" );
855 bool b_aseek, b_aseekfast;
857 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
858 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
863 msg_Warn( s, I64Fd" bytes need to be skipped "
864 "(access non seekable)",
865 i_offset - p_sys->block.i_size );
869 int64_t i_skip = i_offset - p_sys->block.i_size;
871 /* Avg bytes per packets */
872 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
873 /* TODO compute a seek cost instead of fixed threshold */
874 int i_th = b_aseekfast ? 1 : 5;
876 if( i_skip <= i_th * i_avg &&
877 i_skip < STREAM_CACHE_SIZE )
882 msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
883 b_seek, i_th*i_avg, i_skip );
889 int64_t i_start, i_end;
890 /* Do the access seek */
892 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
896 block_ChainRelease( p_sys->block.p_first );
899 p_sys->block.i_start = p_sys->i_pos = i_pos;
900 p_sys->block.i_offset = 0;
901 p_sys->block.p_current = NULL;
902 p_sys->block.i_size = 0;
903 p_sys->block.p_first = NULL;
904 p_sys->block.pp_last = &p_sys->block.p_first;
907 if( AStreamRefillBlock( s ) )
911 p_sys->stat.i_seek_time += i_end - i_start;
912 p_sys->stat.i_seek_count++;
917 /* Read enough data */
918 while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
920 if( AStreamRefillBlock( s ) )
923 while( p_sys->block.p_current &&
924 p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
926 p_sys->i_pos += p_sys->block.p_current->i_buffer;
927 p_sys->block.p_current = p_sys->block.p_current->p_next;
931 p_sys->block.i_offset = i_pos - p_sys->i_pos;
932 p_sys->i_pos = i_pos;
940 static int AStreamRefillBlock( stream_t *s )
942 stream_sys_t *p_sys = s->p_sys;
943 int64_t i_start, i_stop;
947 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
948 p_sys->block.p_first != p_sys->block.p_current )
950 block_t *b = p_sys->block.p_first;
952 p_sys->block.i_start += b->i_buffer;
953 p_sys->block.i_size -= b->i_buffer;
954 p_sys->block.p_first = b->p_next;
958 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
959 p_sys->block.p_current == p_sys->block.p_first &&
960 p_sys->block.p_current->p_next ) /* At least 2 packets */
962 /* Enough data, don't read more */
966 /* Now read a new block */
972 if( s->b_die ) return VLC_EGENERIC;
976 if( ( b = AReadBlock( s, &b_eof ) ) ) break;
978 if( b_eof ) return VLC_EGENERIC;
980 msleep( STREAM_DATA_WAIT );
987 /* Append the block */
988 p_sys->block.i_size += b->i_buffer;
989 *p_sys->block.pp_last = b;
990 p_sys->block.pp_last = &b->p_next;
993 if( p_sys->block.p_current == NULL )
994 p_sys->block.p_current = b;
997 p_sys->stat.i_bytes += b->i_buffer;
998 p_sys->stat.i_read_time += i_stop - i_start;
999 p_sys->stat.i_read_count++;
1008 /****************************************************************************
1010 ****************************************************************************/
1011 static int AStreamRefillStream( stream_t *s );
1013 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
1015 stream_sys_t *p_sys = s->p_sys;
1016 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1018 uint8_t *p_data = (uint8_t *)p_read;
1021 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1023 if( p_read == NULL )
1025 /* seek within this stream if possible, else use plain old read and discard */
1026 stream_sys_t *p_sys = s->p_sys;
1027 access_t *p_access = p_sys->p_access;
1029 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1031 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1035 msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
1036 " offset=%d end="I64Fd,
1037 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1038 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1041 while( i_data < i_read )
1043 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1044 STREAM_CACHE_TRACK_SIZE;
1046 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1047 STREAM_CACHE_TRACK_SIZE - i_off );
1048 int i_copy = __MIN( i_current, i_read - i_data );
1050 if( i_copy <= 0 ) break; /* EOF */
1053 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1056 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1060 p_sys->stream.i_offset += i_copy;
1062 /* Update pos now */
1063 p_sys->i_pos += i_copy;
1066 p_sys->stream.i_used += i_copy;
1067 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1068 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1070 if( AStreamRefillStream( s ) )
1073 if( tk->i_start >= tk->i_end ) break;
1081 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, int i_read )
1083 stream_sys_t *p_sys = s->p_sys;
1084 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1087 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1090 msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
1091 "start="I64Fd" offset=%d end="I64Fd,
1092 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1093 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1096 /* Avoid problem, but that should *never* happen */
1097 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1098 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1100 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1102 if( p_sys->stream.i_used <= 1 )
1104 /* Be sure we will read something */
1105 p_sys->stream.i_used += i_read -
1106 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1108 if( AStreamRefillStream( s ) ) break;
1111 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1112 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1114 /* Now, direct pointer or a copy ? */
1115 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1116 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1118 *pp_peek = &tk->p_buffer[i_off];
1122 if( p_sys->i_peek < i_read )
1124 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1125 if( !p_sys->p_peek )
1130 p_sys->i_peek = i_read;
1133 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1134 STREAM_CACHE_TRACK_SIZE - i_off );
1135 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1136 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1138 *pp_peek = p_sys->p_peek;
1142 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1144 stream_sys_t *p_sys = s->p_sys;
1145 access_t *p_access = p_sys->p_access;
1153 msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
1154 " tk=%d start="I64Fd" offset=%d end="I64Fd,
1155 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1156 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1157 p_sys->stream.i_offset,
1158 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1162 /* Seek in our current track ? */
1163 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1164 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1166 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1168 msg_Dbg( s, "AStreamSeekStream: current track" );
1170 p_sys->i_pos = i_pos;
1171 p_sys->stream.i_offset = i_pos - tk->i_start;
1173 /* If there is not enough data left in the track, refill */
1174 /* \todo How to get a correct value for
1175 * - refilling threshold
1176 * - how much to refill
1178 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1179 p_sys->stream.i_read_size )
1181 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1183 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1184 AStreamRefillStream( s );
1190 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1193 /* We can't do nothing */
1194 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1195 return VLC_EGENERIC;
1198 /* Date the current track */
1199 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1201 /* Try to reuse already read data */
1202 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1204 stream_track_t *tk = &p_sys->stream.tk[i];
1206 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1209 msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
1210 " end="I64Fd, i, tk->i_start, tk->i_end );
1213 /* Seek at the end of the buffer */
1214 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1217 p_sys->i_pos = i_pos;
1218 p_sys->stream.i_tk = i;
1219 p_sys->stream.i_offset = i_pos - tk->i_start;
1221 if( p_sys->stream.i_used < 1024 )
1222 p_sys->stream.i_used = 1024;
1224 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1225 return VLC_EGENERIC;
1231 access_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1232 /* FIXME compute seek cost (instead of static 'stupid' value) */
1233 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1239 /* Search closest segment TODO */
1240 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1242 stream_track_t *tk = &p_sys->stream.tk[i];
1244 if( i_pos + i_maxth >= tk->i_start )
1246 msg_Dbg( s, "good segment before current pos, TODO" );
1248 if( i_pos - i_maxth <= tk->i_end )
1250 msg_Dbg( s, "good segment after current pos, TODO" );
1255 /* Nothing good, seek and choose oldest segment */
1256 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1257 p_sys->i_pos = i_pos;
1260 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1262 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1266 /* Reset the segment */
1267 p_sys->stream.i_tk = i_new;
1268 p_sys->stream.i_offset = 0;
1269 p_sys->stream.tk[i_new].i_start = i_pos;
1270 p_sys->stream.tk[i_new].i_end = i_pos;
1273 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1274 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1276 if( AStreamRefillStream( s ) )
1277 return VLC_EGENERIC;
1282 static int AStreamRefillStream( stream_t *s )
1284 stream_sys_t *p_sys = s->p_sys;
1285 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1287 /* We read but won't increase i_start after initial start + offset */
1289 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1290 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1291 bool b_read = false;
1292 int64_t i_start, i_stop;
1294 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1297 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1298 p_sys->stream.i_used, i_toread );
1302 while( i_toread > 0 )
1304 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1308 return VLC_EGENERIC;
1310 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1311 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1313 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1316 msleep( STREAM_DATA_WAIT );
1319 else if( i_read == 0 )
1321 if( !b_read ) return VLC_EGENERIC;
1327 tk->i_end += i_read;
1329 /* Windows of STREAM_CACHE_TRACK_SIZE */
1330 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1332 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1334 tk->i_start += i_invalid;
1335 p_sys->stream.i_offset -= i_invalid;
1339 p_sys->stream.i_used -= i_read;
1341 p_sys->stat.i_bytes += i_read;
1342 p_sys->stat.i_read_count++;
1346 p_sys->stat.i_read_time += i_stop - i_start;
1351 static void AStreamPrebufferStream( stream_t *s )
1353 stream_sys_t *p_sys = s->p_sys;
1354 access_t *p_access = p_sys->p_access;
1356 int64_t i_first = 0;
1358 int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1359 ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1360 STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1362 msg_Dbg( s, "pre-buffering..." );
1366 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1368 int64_t i_date = mdate();
1371 if( s->b_die || tk->i_end >= i_prebuffer ||
1372 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1377 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1378 p_sys->stat.i_read_time = i_date - i_start;
1379 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
1380 (p_sys->stat.i_read_time+1);
1382 msg_Dbg( s, "pre-buffering done "I64Fd" bytes in "I64Fd"s - "
1384 p_sys->stat.i_bytes,
1385 p_sys->stat.i_read_time / I64C(1000000),
1386 i_byterate / 1024 );
1391 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1392 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1393 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1396 msleep( STREAM_DATA_WAIT );
1399 else if( i_read == 0 )
1408 msg_Dbg( s, "received first data for our buffer");
1411 tk->i_end += i_read;
1413 p_sys->stat.i_read_count++;
1418 /****************************************************************************
1420 ****************************************************************************/
1422 * Read from the stream untill first newline.
1423 * \param s Stream handle to read from
1424 * \return A pointer to the allocated output string. You need to free this when you are done.
1426 #define STREAM_PROBE_LINE 2048
1427 #define STREAM_LINE_MAX (2048*100)
1428 char * stream_ReadLine( stream_t *s )
1430 char *p_line = NULL;
1431 int i_line = 0, i_read = 0;
1433 while( i_read < STREAM_LINE_MAX )
1436 const uint8_t *p_data;
1440 /* Probe new data */
1441 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1442 if( i_data <= 0 ) break; /* No more data */
1445 i_pos = stream_Tell( s );
1446 if( i_pos == 0 && i_data > 4 )
1449 char *psz_encoding = NULL;
1451 if( p_data[0] == 0xEF && p_data[1] == 0xBB && p_data[2] == 0xBF )
1453 psz_encoding = strdup( "UTF-8" );
1456 else if( p_data[0] == 0x00 && p_data[1] == 0x00 )
1458 if( p_data[2] == 0xFE && p_data[3] == 0xFF )
1460 psz_encoding = strdup( "UTF-32BE" );
1461 s->i_char_width = 4;
1465 else if( p_data[0] == 0xFF && p_data[1] == 0xFE )
1467 if( p_data[2] == 0x00 && p_data[3] == 0x00 )
1469 psz_encoding = strdup( "UTF-32LE" );
1470 s->i_char_width = 4;
1471 s->b_little_endian = true;
1476 psz_encoding = strdup( "UTF-16LE" );
1477 s->b_little_endian = true;
1478 s->i_char_width = 2;
1482 else if( p_data[0] == 0xFE && p_data[1] == 0xFF )
1484 psz_encoding = strdup( "UTF-16BE" );
1485 s->i_char_width = 2;
1489 /* Seek past the BOM */
1492 stream_Seek( s, i_bom_size );
1493 p_data += i_bom_size;
1494 i_data -= i_bom_size;
1497 /* Open the converter if we need it */
1498 if( psz_encoding != NULL )
1500 input_thread_t *p_input;
1501 msg_Dbg( s, "%s BOM detected", psz_encoding );
1502 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1503 if( s->i_char_width > 1 )
1505 s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1506 if( s->conv == (vlc_iconv_t)-1 )
1508 msg_Err( s, "iconv_open failed" );
1511 if( p_input != NULL)
1513 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1514 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1515 vlc_object_release( p_input );
1517 free( psz_encoding );
1521 if( i_data % s->i_char_width )
1523 /* keep i_char_width boundary */
1524 i_data = i_data - ( i_data % s->i_char_width );
1525 msg_Warn( s, "the read is not i_char_width compatible");
1531 /* Check if there is an EOL */
1532 if( s->i_char_width == 1 )
1534 /* UTF-8: 0A <LF> */
1535 psz_eol = memchr( p_data, '\n', i_data );
1539 const uint8_t *p = p_data;
1540 const uint8_t *p_last = p + i_data - s->i_char_width;
1542 if( s->i_char_width == 2 )
1544 if( s->b_little_endian == true)
1546 /* UTF-16LE: 0A 00 <LF> */
1547 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1552 /* UTF-16BE: 00 0A <LF> */
1553 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1557 else if( s->i_char_width == 4 )
1559 if( s->b_little_endian == true)
1561 /* UTF-32LE: 0A 00 00 00 <LF> */
1562 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ||
1563 p[2] != 0x00 || p[3] != 0x00 ) )
1568 /* UTF-32BE: 00 00 00 0A <LF> */
1569 while( p <= p_last && ( p[3] != 0x0A || p[2] != 0x00 ||
1570 p[1] != 0x00 || p[0] != 0x00 ) )
1581 psz_eol = (char *)p + ( s->i_char_width - 1 );
1587 i_data = (psz_eol - (char *)p_data) + 1;
1588 p_line = realloc( p_line, i_line + i_data + s->i_char_width ); /* add \0 */
1591 msg_Err( s, "Out of memory when reallocating p_line" );
1594 i_data = stream_Read( s, &p_line[i_line], i_data );
1595 if( i_data <= 0 ) break; /* Hmmm */
1596 i_line += i_data - s->i_char_width; /* skip \n */;
1599 /* We have our line */
1603 /* Read data (+1 for easy \0 append) */
1604 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->i_char_width );
1607 msg_Err( s, "Out of memory when reallocating p_line" );
1610 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1611 if( i_data <= 0 ) break; /* Hmmm */
1619 for( j = 0; j < s->i_char_width; j++ )
1621 p_line[i_line + j] = '\0';
1623 i_line += s->i_char_width; /* the added \0 */
1624 if( s->i_char_width > 1 )
1626 size_t i_in = 0, i_out = 0;
1627 const char * p_in = NULL;
1628 char * p_out = NULL;
1629 char * psz_new_line = NULL;
1632 psz_new_line = malloc( i_line );
1633 if( psz_new_line == NULL )
1635 msg_Err( s, "Out of memory when allocating psz_new_line" );
1638 i_in = i_out = (size_t)i_line;
1640 p_out = psz_new_line;
1642 if( vlc_iconv( s->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1644 msg_Err( s, "iconv failed" );
1645 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1648 p_line = psz_new_line;
1649 i_line = (size_t)i_line - i_out; /* does not include \0 */
1652 /* Remove trailing LF/CR */
1653 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1654 p_line[i_line-2] == '\n') ) i_line--;
1656 /* Make sure the \0 is there */
1657 p_line[i_line-1] = '\0';
1664 /* We failed to read any data, probably EOF */
1666 if( s->conv != (vlc_iconv_t)(-1) ) vlc_iconv_close( s->conv );
1670 /****************************************************************************
1671 * Access reading/seeking wrappers to handle concatenated streams.
1672 ****************************************************************************/
1673 static int AReadStream( stream_t *s, void *p_read, int i_read )
1675 stream_sys_t *p_sys = s->p_sys;
1676 access_t *p_access = p_sys->p_access;
1677 input_thread_t *p_input = NULL;
1678 int i_read_orig = i_read;
1681 if( s->p_parent && s->p_parent->p_parent &&
1682 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1683 p_input = (input_thread_t *)s->p_parent->p_parent;
1685 if( !p_sys->i_list )
1687 i_read = p_access->pf_read( p_access, p_read, i_read );
1690 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1691 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
1693 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1694 (float)i_total, NULL );
1695 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1696 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1701 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1704 /* If we reached an EOF then switch to the next stream in the list */
1705 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1707 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1708 access_t *p_list_access;
1710 msg_Dbg( s, "opening input `%s'", psz_name );
1712 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1714 if( !p_list_access ) return 0;
1716 if( p_sys->p_list_access != p_access )
1717 access_Delete( p_sys->p_list_access );
1719 p_sys->p_list_access = p_list_access;
1721 /* We have to read some data */
1722 return AReadStream( s, p_read, i_read_orig );
1725 /* Update read bytes in input */
1728 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1729 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
1730 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1731 (float)i_total, NULL );
1732 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1733 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1738 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
1740 stream_sys_t *p_sys = s->p_sys;
1741 access_t *p_access = p_sys->p_access;
1742 input_thread_t *p_input = NULL;
1747 if( s->p_parent && s->p_parent->p_parent &&
1748 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1749 p_input = (input_thread_t *)s->p_parent->p_parent;
1751 if( !p_sys->i_list )
1753 p_block = p_access->pf_block( p_access );
1754 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1755 if( p_input && p_block && p_access->p_libvlc->b_stats )
1757 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1758 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1759 p_block->i_buffer, &i_total );
1760 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1761 (float)i_total, NULL );
1762 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1763 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1768 p_block = p_sys->p_list_access->pf_block( p_access );
1769 b_eof = p_sys->p_list_access->info.b_eof;
1770 if( pb_eof ) *pb_eof = b_eof;
1772 /* If we reached an EOF then switch to the next stream in the list */
1773 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1775 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1776 access_t *p_list_access;
1778 msg_Dbg( s, "opening input `%s'", psz_name );
1780 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1782 if( !p_list_access ) return 0;
1784 if( p_sys->p_list_access != p_access )
1785 access_Delete( p_sys->p_list_access );
1787 p_sys->p_list_access = p_list_access;
1789 /* We have to read some data */
1790 return AReadBlock( s, pb_eof );
1796 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1797 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1798 p_block->i_buffer, &i_total );
1799 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1800 (float)i_total, NULL );
1801 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
1803 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1809 static int ASeek( stream_t *s, int64_t i_pos )
1811 stream_sys_t *p_sys = s->p_sys;
1812 access_t *p_access = p_sys->p_access;
1814 /* Check which stream we need to access */
1820 access_t *p_list_access = 0;
1822 for( i = 0; i < p_sys->i_list - 1; i++ )
1824 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1825 i_size += p_sys->list[i]->i_size;
1827 psz_name = p_sys->list[i]->psz_path;
1829 if( i != p_sys->i_list_index )
1830 msg_Dbg( s, "opening input `%s'", psz_name );
1832 if( i != p_sys->i_list_index && i != 0 )
1835 access_New( s, p_access->psz_access, "", psz_name );
1837 else if( i != p_sys->i_list_index )
1839 p_list_access = p_access;
1844 if( p_sys->p_list_access != p_access )
1845 access_Delete( p_sys->p_list_access );
1847 p_sys->p_list_access = p_list_access;
1850 p_sys->i_list_index = i;
1851 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1855 return p_access->pf_seek( p_access, i_pos );
1860 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
1861 * "p_read" is NULL then data are skipped instead of read. The return
1862 * value is the real numbers of bytes read/skip. If this value is less
1863 * than i_read that means that it's the end of the stream.
1865 int stream_Read( stream_t *s, void *p_read, int i_read )
1867 return s->pf_read( s, p_read, i_read );
1871 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
1872 * \return The real numbers of valid bytes, if it's less
1873 * or equal to 0, *pp_peek is invalid.
1874 * \note pp_peek is a pointer to internal buffer and it will be invalid as
1875 * soons as other stream_* functions are called.
1876 * \note Due to input limitation, it could be less than i_peek without meaning
1877 * the end of the stream (but only when you have i_peek >=
1878 * p_input->i_bufsize)
1880 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
1882 return s->pf_peek( s, pp_peek, i_peek );
1886 * Use to control the "stream_t *". Look at #stream_query_e for
1887 * possible "i_query" value and format arguments. Return VLC_SUCCESS
1888 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
1890 int stream_vaControl( stream_t *s, int i_query, va_list args )
1892 return s->pf_control( s, i_query, args );
1898 void stream_Delete( stream_t *s )
1903 int stream_Control( stream_t *s, int i_query, ... )
1909 return VLC_EGENERIC;
1911 va_start( args, i_query );
1912 i_result = s->pf_control( s, i_query, args );
1918 * Read "i_size" bytes and store them in a block_t.
1919 * It always read i_size bytes unless you are at the end of the stream
1920 * where it return what is available.
1922 block_t *stream_Block( stream_t *s, int i_size )
1924 if( i_size <= 0 ) return NULL;
1926 /* emulate block read */
1927 block_t *p_bk = block_New( s, i_size );
1930 p_bk->i_buffer = stream_Read( s, p_bk->p_buffer, i_size );
1931 if( p_bk->i_buffer > 0 )
1936 if( p_bk ) block_Release( p_bk );