]> git.sesse.net Git - vlc/blob - src/input/stream.c
Sanitize input headers (pass 3).
[vlc] / src / input / stream.c
1 /*****************************************************************************
2  * stream.c
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <dirent.h>
29
30 #include <vlc_common.h>
31 #include <vlc_strings.h>
32 #include <vlc_osd.h>
33 #include <vlc_charset.h>
34
35 #include <assert.h>
36
37 #include "input_internal.h"
38 #include "access.h"
39
40 #undef STREAM_DEBUG
41
42 /* TODO:
43  *  - tune the 2 methods (block/stream)
44  *  - compute cost for seek
45  *  - improve stream mode seeking with closest segments
46  *  - ...
47  *  - Maybe remove (block/stream) in favour of immediate
48  */
49
50 /* Two methods:
51  *  - using pf_block
52  *      One linked list of data read
53  *  - using pf_read
54  *      More complex scheme using mutliple track to avoid seeking
55  *  - using directly the access (only indirection for peeking).
56  *      This method is known to introduce much less latency.
57  *      It should probably defaulted (instead of the stream method (2)).
58  */
59
60 /* How many tracks we have, currently only used for stream mode */
61 #ifdef OPTIMIZE_MEMORY
62 #   define STREAM_CACHE_TRACK 1
63     /* Max size of our cache 128Ko per track */
64 #   define STREAM_CACHE_SIZE  (STREAM_CACHE_TRACK*1024*128)
65 #else
66 #   define STREAM_CACHE_TRACK 3
67     /* Max size of our cache 4Mo per track */
68 #   define STREAM_CACHE_SIZE  (4*STREAM_CACHE_TRACK*1024*1024)
69 #endif
70
71 /* How many data we try to prebuffer */
72 #define STREAM_CACHE_PREBUFFER_SIZE (32767)
73 /* Maximum time we take to pre-buffer */
74 #define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
75
76 /* Method1: Simple, for pf_block.
77  *  We get blocks and put them in the linked list.
78  *  We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
79  */
80
81 /* Method2: A bit more complex, for pf_read
82  *  - We use ring buffers, only one if unseekable, all if seekable
83  *  - Upon seek date current ring, then search if one ring match the pos,
84  *      yes: switch to it, seek the access to match the end of the ring
85  *      no: search the ring with i_end the closer to i_pos,
86  *          if close enough, read data and use this ring
87  *          else use the oldest ring, seek and use it.
88  *
89  *  TODO: - with access non seekable: use all space available for only one ring, but
90  *          we have to support seekable/non-seekable switch on the fly.
91  *        - compute a good value for i_read_size
92  *        - ?
93  */
94 #define STREAM_READ_ATONCE 32767
95 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
96
97 typedef struct
98 {
99     int64_t i_date;
100
101     int64_t i_start;
102     int64_t i_end;
103
104     uint8_t *p_buffer;
105
106 } stream_track_t;
107
108 typedef struct
109 {
110     char     *psz_path;
111     int64_t  i_size;
112
113 } access_entry_t;
114
115 typedef enum stream_read_method_t
116 {
117     STREAM_METHOD_IMMEDIATE,
118     STREAM_METHOD_BLOCK,
119     STREAM_METHOD_STREAM
120 } stream_read_method_t;
121
122 struct stream_sys_t
123 {
124     access_t    *p_access;
125
126     stream_read_method_t   method;    /* method to use */
127
128     int64_t     i_pos;      /* Current reading offset */
129
130     /* Method 1: pf_block */
131     struct
132     {
133         int64_t i_start;        /* Offset of block for p_first */
134         int64_t i_offset;       /* Offset for data in p_current */
135         block_t *p_current;     /* Current block */
136
137         int     i_size;         /* Total amount of data in the list */
138         block_t *p_first;
139         block_t **pp_last;
140
141     } block;
142
143     /* Method 2: for pf_read */
144     struct
145     {
146         int i_offset;   /* Buffer offset in the current track */
147         int i_tk;       /* Current track */
148         stream_track_t tk[STREAM_CACHE_TRACK];
149
150         /* Global buffer */
151         uint8_t *p_buffer;
152
153         /* */
154         int i_used; /* Used since last read */
155         int i_read_size;
156
157     } stream;
158
159     /* Method 3: for pf_read */
160     struct
161     {
162         int64_t i_end;
163         uint8_t *p_buffer;
164     } immediate;
165
166     /* Peek temporary buffer */
167     unsigned int i_peek;
168     uint8_t *p_peek;
169
170     /* Stat for both method */
171     struct
172     {
173         bool b_fastseek;  /* From access */
174
175         /* Stat about reading data */
176         int64_t i_read_count;
177         int64_t i_bytes;
178         int64_t i_read_time;
179
180         /* Stat about seek */
181         int     i_seek_count;
182         int64_t i_seek_time;
183
184     } stat;
185
186     /* Streams list */
187     int            i_list;
188     access_entry_t **list;
189     int            i_list_index;
190     access_t       *p_list_access;
191
192     /* Preparse mode ? */
193     bool      b_quick;
194
195     /* */
196     struct
197     {
198         bool b_active;
199
200         FILE *f;        /* TODO it could be replaced by access_output_t one day */
201         bool b_error;
202     } record;
203 };
204
205 /* Method 1: */
206 static int  AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read );
207 static int  AStreamPeekBlock( stream_t *s, const uint8_t **p_peek, unsigned int i_read );
208 static int  AStreamSeekBlock( stream_t *s, int64_t i_pos );
209 static void AStreamPrebufferBlock( stream_t *s );
210 static block_t *AReadBlock( stream_t *s, bool *pb_eof );
211
212 /* Method 2 */
213 static int  AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read );
214 static int  AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read );
215 static int  AStreamSeekStream( stream_t *s, int64_t i_pos );
216 static void AStreamPrebufferStream( stream_t *s );
217 static int  AReadStream( stream_t *s, void *p_read, unsigned int i_read );
218
219 /* Method 3 */
220 static int  AStreamReadImmediate( stream_t *s, void *p_read, unsigned int i_read );
221 static int  AStreamPeekImmediate( stream_t *s, const uint8_t **pp_peek, unsigned int i_read );
222 static int  AStreamSeekImmediate( stream_t *s, int64_t i_pos );
223
224 /* Common */
225 static int AStreamControl( stream_t *s, int i_query, va_list );
226 static void AStreamDestroy( stream_t *s );
227 static void UStreamDestroy( stream_t *s );
228 static int  ASeek( stream_t *s, int64_t i_pos );
229 static int  ARecordSetState( stream_t *s, bool b_record, const char *psz_extension );
230 static void ARecordWrite( stream_t *s, const uint8_t *p_buffer, size_t i_buffer );
231
232 /****************************************************************************
233  * Method 3 helpers:
234  ****************************************************************************/
235
236 static inline int64_t stream_buffered_size( stream_t *s )
237 {
238     return s->p_sys->immediate.i_end;
239 }
240
241 static inline void stream_buffer_empty( stream_t *s, int length )
242 {
243     length = __MAX( stream_buffered_size( s ), length );
244     if( length )
245     {
246         memmove( s->p_sys->immediate.p_buffer,
247                  s->p_sys->immediate.p_buffer + length,
248                  stream_buffered_size( s ) - length );
249     }
250     s->p_sys->immediate.i_end -= length;
251 }
252
253 static inline void stream_buffer_fill( stream_t *s, int length )
254 {
255     s->p_sys->immediate.i_end += length;
256 }
257
258 static inline uint8_t * stream_buffer( stream_t *s )
259 {
260     return s->p_sys->immediate.p_buffer;
261 }
262
263 /****************************************************************************
264  * stream_UrlNew: create a stream from a access
265  ****************************************************************************/
266 stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
267 {
268     const char *psz_access, *psz_demux;
269     char *psz_path;
270     access_t *p_access;
271     stream_t *p_res;
272
273     if( !psz_url )
274         return NULL;
275
276     char psz_dup[strlen( psz_url ) + 1];
277     strcpy( psz_dup, psz_url );
278     input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
279
280     /* Now try a real access */
281     p_access = access_New( p_parent, psz_access, psz_demux, psz_path );
282
283     if( p_access == NULL )
284     {
285         msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
286         return NULL;
287     }
288
289     if( !( p_res = stream_AccessNew( p_access, true ) ) )
290     {
291         access_Delete( p_access );
292         return NULL;
293     }
294
295     p_res->pf_destroy = UStreamDestroy;
296     return p_res;
297 }
298
299 stream_t *stream_AccessNew( access_t *p_access, bool b_quick )
300 {
301     stream_t *s = vlc_stream_create( VLC_OBJECT(p_access) );
302     stream_sys_t *p_sys;
303     char *psz_list = NULL;
304
305     if( !s )
306         return NULL;
307
308     s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
309     if( !p_sys )
310     {
311         vlc_object_release( s );
312         return NULL;
313     }
314
315     /* Attach it now, needed for b_die */
316     vlc_object_attach( s, p_access );
317
318     s->pf_read   = NULL;    /* Set up later */
319     s->pf_peek   = NULL;
320     s->pf_control = AStreamControl;
321     s->pf_destroy = AStreamDestroy;
322
323     /* UTF16 and UTF32 text file conversion */
324     s->i_char_width = 1;
325     s->b_little_endian = false;
326     s->conv = (vlc_iconv_t)(-1);
327
328     /* Common field */
329     p_sys->p_access = p_access;
330     if( p_access->pf_block )
331         p_sys->method = STREAM_METHOD_BLOCK;
332     else if( var_CreateGetBool( s, "use-stream-immediate" ) )
333         p_sys->method = STREAM_METHOD_IMMEDIATE;
334     else
335         p_sys->method = STREAM_METHOD_STREAM;
336
337     p_sys->record.b_active = false;
338
339     p_sys->i_pos = p_access->info.i_pos;
340
341     /* Stats */
342     access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
343     p_sys->stat.i_bytes = 0;
344     p_sys->stat.i_read_time = 0;
345     p_sys->stat.i_read_count = 0;
346     p_sys->stat.i_seek_count = 0;
347     p_sys->stat.i_seek_time = 0;
348
349     p_sys->i_list = 0;
350     p_sys->list = 0;
351     p_sys->i_list_index = 0;
352     p_sys->p_list_access = 0;
353
354     p_sys->b_quick = b_quick;
355
356     /* Get the additional list of inputs if any (for concatenation) */
357     if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
358     {
359         access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
360         if( p_entry == NULL )
361             goto error;
362         char *psz_name, *psz_parser = psz_name = psz_list;
363
364         p_sys->p_list_access = p_access;
365         p_entry->i_size = p_access->info.i_size;
366         p_entry->psz_path = strdup( p_access->psz_path );
367         if( p_entry->psz_path == NULL )
368         {
369             free( p_entry );
370             goto error;
371         }
372         TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
373         msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
374                  p_entry->psz_path, p_access->info.i_size );
375
376         while( psz_name && *psz_name )
377         {
378             psz_parser = strchr( psz_name, ',' );
379             if( psz_parser ) *psz_parser = 0;
380
381             psz_name = strdup( psz_name );
382             if( psz_name )
383             {
384                 access_t *p_tmp = access_New( p_access, p_access->psz_access,
385                                                "", psz_name );
386
387                 if( !p_tmp )
388                 {
389                     psz_name = psz_parser;
390                     if( psz_name ) psz_name++;
391                     continue;
392                 }
393
394                 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
395                          psz_name, p_tmp->info.i_size );
396
397                 p_entry = malloc( sizeof(access_entry_t) );
398                 if( p_entry == NULL )
399                     goto error;
400                 p_entry->i_size = p_tmp->info.i_size;
401                 p_entry->psz_path = psz_name;
402                 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
403
404                 access_Delete( p_tmp );
405             }
406
407             psz_name = psz_parser;
408             if( psz_name ) psz_name++;
409         }
410     }
411     FREENULL( psz_list );
412
413     /* Peek */
414     p_sys->i_peek = 0;
415     p_sys->p_peek = NULL;
416
417     if( p_sys->method == STREAM_METHOD_BLOCK )
418     {
419         msg_Dbg( s, "Using AStream*Block" );
420         s->pf_read = AStreamReadBlock;
421         s->pf_peek = AStreamPeekBlock;
422
423         /* Init all fields of p_sys->block */
424         p_sys->block.i_start = p_sys->i_pos;
425         p_sys->block.i_offset = 0;
426         p_sys->block.p_current = NULL;
427         p_sys->block.i_size = 0;
428         p_sys->block.p_first = NULL;
429         p_sys->block.pp_last = &p_sys->block.p_first;
430
431         /* Do the prebuffering */
432         AStreamPrebufferBlock( s );
433
434         if( p_sys->block.i_size <= 0 )
435         {
436             msg_Err( s, "cannot pre fill buffer" );
437             goto error;
438         }
439     }
440     else if( p_sys->method == STREAM_METHOD_IMMEDIATE )
441     {
442         msg_Dbg( s, "Using AStream*Immediate" );
443
444         s->pf_read = AStreamReadImmediate;
445         s->pf_peek = AStreamPeekImmediate;
446
447         /* Allocate/Setup our tracks (useful to peek)*/
448         p_sys->immediate.i_end = 0;
449         p_sys->immediate.p_buffer = malloc( STREAM_CACHE_SIZE );
450
451         msg_Dbg( s, "p_buffer %p-%p",
452                  p_sys->immediate.p_buffer,
453                  &p_sys->immediate.p_buffer[STREAM_CACHE_SIZE] );
454
455         if( p_sys->immediate.p_buffer == NULL )
456         {
457             msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
458                         STREAM_CACHE_SIZE );
459             goto error;
460         }
461     }
462     else
463     {
464         int i;
465
466         assert( p_sys->method == STREAM_METHOD_STREAM );
467
468         msg_Dbg( s, "Using AStream*Stream" );
469
470         s->pf_read = AStreamReadStream;
471         s->pf_peek = AStreamPeekStream;
472
473         /* Allocate/Setup our tracks */
474         p_sys->stream.i_offset = 0;
475         p_sys->stream.i_tk     = 0;
476         p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
477         if( p_sys->stream.p_buffer == NULL )
478         {
479             msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
480                         STREAM_CACHE_SIZE );
481             goto error;
482         }
483         p_sys->stream.i_used   = 0;
484         access_Control( p_access, ACCESS_GET_MTU,
485                          &p_sys->stream.i_read_size );
486         if( p_sys->stream.i_read_size <= 0 )
487             p_sys->stream.i_read_size = STREAM_READ_ATONCE;
488         else if( p_sys->stream.i_read_size <= 256 )
489             p_sys->stream.i_read_size = 256;
490
491         for( i = 0; i < STREAM_CACHE_TRACK; i++ )
492         {
493             p_sys->stream.tk[i].i_date  = 0;
494             p_sys->stream.tk[i].i_start = p_sys->i_pos;
495             p_sys->stream.tk[i].i_end   = p_sys->i_pos;
496             p_sys->stream.tk[i].p_buffer=
497                 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
498         }
499
500         /* Do the prebuffering */
501         AStreamPrebufferStream( s );
502
503         if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
504         {
505             msg_Err( s, "cannot pre fill buffer" );
506             goto error;
507         }
508     }
509
510     return s;
511
512 error:
513     if( p_sys->method == STREAM_METHOD_BLOCK )
514     {
515         /* Nothing yet */
516     }
517     else
518     {
519         free( p_sys->stream.p_buffer );
520     }
521     while( p_sys->i_list > 0 )
522         free( p_sys->list[--(p_sys->i_list)] );
523     free( p_sys->list );
524     free( psz_list );
525     free( s->p_sys );
526     vlc_object_detach( s );
527     vlc_object_release( s );
528     return NULL;
529 }
530
531 /****************************************************************************
532  * AStreamDestroy:
533  ****************************************************************************/
534 static void AStreamDestroy( stream_t *s )
535 {
536     stream_sys_t *p_sys = s->p_sys;
537
538     vlc_object_detach( s );
539
540     if( p_sys->record.b_active )
541         ARecordSetState( s, false, NULL );
542
543     if( p_sys->method == STREAM_METHOD_BLOCK )
544         block_ChainRelease( p_sys->block.p_first );
545     else if( p_sys->method == STREAM_METHOD_IMMEDIATE )
546         free( p_sys->immediate.p_buffer );
547     else
548         free( p_sys->stream.p_buffer );
549
550     free( p_sys->p_peek );
551
552     if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
553         access_Delete( p_sys->p_list_access );
554
555     while( p_sys->i_list-- )
556     {
557         free( p_sys->list[p_sys->i_list]->psz_path );
558         free( p_sys->list[p_sys->i_list] );
559     }
560
561     free( p_sys->list );
562     free( p_sys );
563
564     vlc_object_release( s );
565 }
566
567 static void UStreamDestroy( stream_t *s )
568 {
569     access_t *p_access = (access_t *)s->p_parent;
570     AStreamDestroy( s );
571     access_Delete( p_access );
572 }
573
574 /****************************************************************************
575  * stream_AccessReset:
576  ****************************************************************************/
577 void stream_AccessReset( stream_t *s )
578 {
579     stream_sys_t *p_sys = s->p_sys;
580
581     p_sys->i_pos = p_sys->p_access->info.i_pos;
582
583     if( p_sys->method == STREAM_METHOD_BLOCK )
584     {
585         block_ChainRelease( p_sys->block.p_first );
586
587         /* Init all fields of p_sys->block */
588         p_sys->block.i_start = p_sys->i_pos;
589         p_sys->block.i_offset = 0;
590         p_sys->block.p_current = NULL;
591         p_sys->block.i_size = 0;
592         p_sys->block.p_first = NULL;
593         p_sys->block.pp_last = &p_sys->block.p_first;
594
595         /* Do the prebuffering */
596         AStreamPrebufferBlock( s );
597     }
598     else if( p_sys->method == STREAM_METHOD_IMMEDIATE )
599     {
600         stream_buffer_empty( s, stream_buffered_size( s ) );
601     }
602     else
603     {
604         int i;
605
606         assert( p_sys->method == STREAM_METHOD_STREAM );
607
608         /* Setup our tracks */
609         p_sys->stream.i_offset = 0;
610         p_sys->stream.i_tk     = 0;
611         p_sys->stream.i_used   = 0;
612
613         for( i = 0; i < STREAM_CACHE_TRACK; i++ )
614         {
615             p_sys->stream.tk[i].i_date  = 0;
616             p_sys->stream.tk[i].i_start = p_sys->i_pos;
617             p_sys->stream.tk[i].i_end   = p_sys->i_pos;
618         }
619
620         /* Do the prebuffering */
621         AStreamPrebufferStream( s );
622     }
623 }
624
625 /****************************************************************************
626  * stream_AccessUpdate:
627  ****************************************************************************/
628 void stream_AccessUpdate( stream_t *s )
629 {
630     stream_sys_t *p_sys = s->p_sys;
631
632     p_sys->i_pos = p_sys->p_access->info.i_pos;
633
634     if( p_sys->i_list )
635     {
636         int i;
637         for( i = 0; i < p_sys->i_list_index; i++ )
638         {
639             p_sys->i_pos += p_sys->list[i]->i_size;
640         }
641     }
642 }
643
644 /****************************************************************************
645  * AStreamControl:
646  ****************************************************************************/
647 static int AStreamControl( stream_t *s, int i_query, va_list args )
648 {
649     stream_sys_t *p_sys = s->p_sys;
650     access_t     *p_access = p_sys->p_access;
651
652     bool *p_bool;
653     bool b_bool;
654     const char *psz_string;
655     int64_t    *pi_64, i_64;
656     int        i_int;
657
658     switch( i_query )
659     {
660         case STREAM_GET_SIZE:
661             pi_64 = (int64_t*)va_arg( args, int64_t * );
662             if( s->p_sys->i_list )
663             {
664                 int i;
665                 *pi_64 = 0;
666                 for( i = 0; i < s->p_sys->i_list; i++ )
667                     *pi_64 += s->p_sys->list[i]->i_size;
668                 break;
669             }
670             *pi_64 = p_access->info.i_size;
671             break;
672
673         case STREAM_CAN_SEEK:
674             p_bool = (bool*)va_arg( args, bool * );
675             access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
676             break;
677
678         case STREAM_CAN_FASTSEEK:
679             p_bool = (bool*)va_arg( args, bool * );
680             access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
681             break;
682
683         case STREAM_GET_POSITION:
684             pi_64 = (int64_t*)va_arg( args, int64_t * );
685             *pi_64 = p_sys->i_pos;
686             break;
687
688         case STREAM_SET_POSITION:
689             i_64 = (int64_t)va_arg( args, int64_t );
690             switch( p_sys->method )
691             {
692             case STREAM_METHOD_BLOCK:
693                 return AStreamSeekBlock( s, i_64 );
694             case STREAM_METHOD_IMMEDIATE:
695                 return AStreamSeekImmediate( s, i_64 );
696             case STREAM_METHOD_STREAM:
697                 return AStreamSeekStream( s, i_64 );
698             default:
699                 assert(0);
700                 return VLC_EGENERIC;
701             }
702
703         case STREAM_GET_MTU:
704             return VLC_EGENERIC;
705
706         case STREAM_CONTROL_ACCESS:
707             i_int = (int) va_arg( args, int );
708             if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
709                 i_int != ACCESS_SET_PRIVATE_ID_CA &&
710                 i_int != ACCESS_GET_PRIVATE_ID_STATE )
711             {
712                 msg_Err( s, "Hey, what are you thinking ?"
713                             "DON'T USE STREAM_CONTROL_ACCESS !!!" );
714                 return VLC_EGENERIC;
715             }
716             return access_vaControl( p_access, i_int, args );
717
718         case STREAM_GET_CONTENT_TYPE:
719             return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
720                                     va_arg( args, char ** ) );
721         case STREAM_SET_RECORD_STATE:
722             b_bool = (bool)va_arg( args, int );
723             psz_string = NULL;
724             if( b_bool )
725                 psz_string = (const char*)va_arg( args, const char* );
726             return ARecordSetState( s, b_bool, psz_string );
727
728         default:
729             msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
730             return VLC_EGENERIC;
731     }
732     return VLC_SUCCESS;
733 }
734
735 /****************************************************************************
736  * ARecord*: record stream functions
737  ****************************************************************************/
738 static int  ARecordStart( stream_t *s, const char *psz_extension )
739 {
740     stream_sys_t *p_sys = s->p_sys;
741
742     char *psz_file;
743     FILE *f;
744
745     /* */
746     if( !psz_extension )
747         psz_extension = "dat";
748
749     /* Retreive path */
750     char *psz_path = var_CreateGetString( s, "input-record-path" );
751     if( !psz_path || *psz_path == '\0' )
752     {
753         free( psz_path );
754         psz_path = strdup( config_GetHomeDir() );
755     }
756
757     if( !psz_path )
758         return VLC_ENOMEM;
759
760     /* Create file name
761      * TODO allow prefix configuration */
762     psz_file = input_CreateFilename( VLC_OBJECT(s), psz_path, INPUT_RECORD_PREFIX, psz_extension );
763
764     free( psz_path );
765
766     if( !psz_file )
767         return VLC_ENOMEM;
768
769     f = utf8_fopen( psz_file, "wb" );
770     if( !f )
771     {
772         free( psz_file );
773         return VLC_EGENERIC;
774     }
775     msg_Dbg( s, "Recording into %s", psz_file );
776     free( psz_file );
777
778     /* */
779     p_sys->record.f = f;
780     p_sys->record.b_active = true;
781     p_sys->record.b_error = false;
782     return VLC_SUCCESS;
783 }
784 static int  ARecordStop( stream_t *s )
785 {
786     stream_sys_t *p_sys = s->p_sys;
787
788     assert( p_sys->record.b_active );
789
790     msg_Dbg( s, "Recording completed" );
791     fclose( p_sys->record.f );
792     p_sys->record.b_active = false;
793     return VLC_SUCCESS;
794 }
795
796 static int  ARecordSetState( stream_t *s, bool b_record, const char *psz_extension )
797 {
798     stream_sys_t *p_sys = s->p_sys;
799
800     if( !!p_sys->record.b_active == !!b_record )
801         return VLC_SUCCESS;
802
803     if( b_record )
804         return ARecordStart( s, psz_extension );
805     else
806         return ARecordStop( s );
807 }
808 static void ARecordWrite( stream_t *s, const uint8_t *p_buffer, size_t i_buffer )
809 {
810     stream_sys_t *p_sys = s->p_sys;
811
812     assert( p_sys->record.b_active );
813
814     if( i_buffer > 0 )
815     {
816         const bool b_previous_error = p_sys->record.b_error;
817         const size_t i_written = fwrite( p_buffer, 1, i_buffer, p_sys->record.f );
818
819         p_sys->record.b_error = i_written != i_buffer;
820
821         /* TODO maybe a intf_UserError or something like that ? */
822         if( p_sys->record.b_error && !b_previous_error )
823             msg_Err( s, "Failed to record data (begin)" );
824         else if( !p_sys->record.b_error && b_previous_error )
825             msg_Err( s, "Failed to record data (end)" );
826     }
827 }
828
829 /****************************************************************************
830  * Method 1:
831  ****************************************************************************/
832 static void AStreamPrebufferBlock( stream_t *s )
833 {
834     stream_sys_t *p_sys = s->p_sys;
835     access_t     *p_access = p_sys->p_access;
836
837     int64_t i_first = 0;
838     int64_t i_start;
839
840     msg_Dbg( s, "pre buffering" );
841     i_start = mdate();
842     for( ;; )
843     {
844         int64_t i_date = mdate();
845         bool b_eof;
846         block_t *b;
847
848         if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
849             ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
850         {
851             int64_t i_byterate;
852
853             /* Update stat */
854             p_sys->stat.i_bytes = p_sys->block.i_size;
855             p_sys->stat.i_read_time = i_date - i_start;
856             i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
857                          (p_sys->stat.i_read_time + 1);
858
859             msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
860                      "%"PRId64" kbytes/s",
861                      p_sys->stat.i_bytes,
862                      p_sys->stat.i_read_time / INT64_C(1000000),
863                      i_byterate / 1024 );
864             break;
865         }
866
867         /* Fetch a block */
868         if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
869         {
870             if( b_eof )
871                 break;
872             continue;
873         }
874
875         while( b )
876         {
877             /* Append the block */
878             p_sys->block.i_size += b->i_buffer;
879             *p_sys->block.pp_last = b;
880             p_sys->block.pp_last = &b->p_next;
881
882             p_sys->stat.i_read_count++;
883             b = b->p_next;
884         }
885
886         if( p_access->info.b_prebuffered )
887         {
888             /* Access has already prebufferred - update stats and exit */
889             p_sys->stat.i_bytes = p_sys->block.i_size;
890             p_sys->stat.i_read_time = mdate() - i_start;
891             break;
892         }
893
894         if( i_first == 0 )
895         {
896             i_first = mdate();
897             msg_Dbg( s, "received first data for our buffer");
898         }
899
900     }
901
902     p_sys->block.p_current = p_sys->block.p_first;
903 }
904
905 static int AStreamRefillBlock( stream_t *s );
906
907 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
908 {
909     stream_sys_t *p_sys = s->p_sys;
910
911     uint8_t *p_data = p_read;
912     uint8_t *p_record = p_data;
913     unsigned int i_data = 0;
914
915     /* It means EOF */
916     if( p_sys->block.p_current == NULL )
917         return 0;
918
919     if( p_sys->record.b_active && !p_data )
920         p_record = p_data = malloc( i_read );
921
922     if( p_data == NULL )
923     {
924         /* seek within this stream if possible, else use plain old read and discard */
925         stream_sys_t *p_sys = s->p_sys;
926         access_t     *p_access = p_sys->p_access;
927         bool   b_aseek;
928         access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
929         if( b_aseek )
930             return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
931     }
932
933     while( i_data < i_read )
934     {
935         int i_current =
936             p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
937         unsigned int i_copy = __MIN( (unsigned int)__MAX(i_current,0), i_read - i_data);
938
939         /* Copy data */
940         if( p_data )
941         {
942             memcpy( p_data,
943                     &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
944                     i_copy );
945             p_data += i_copy;
946         }
947         i_data += i_copy;
948
949         p_sys->block.i_offset += i_copy;
950         if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
951         {
952             /* Current block is now empty, switch to next */
953             if( p_sys->block.p_current )
954             {
955                 p_sys->block.i_offset = 0;
956                 p_sys->block.p_current = p_sys->block.p_current->p_next;
957             }
958             /*Get a new block if needed */
959             if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
960             {
961                 break;
962             }
963         }
964     }
965
966     if( p_sys->record.b_active )
967     {
968         if( i_data > 0 && p_record != NULL)
969             ARecordWrite( s, p_record, i_data );
970         if( !p_read )
971             free( p_record );
972     }
973
974     p_sys->i_pos += i_data;
975     return i_data;
976 }
977
978 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
979 {
980     stream_sys_t *p_sys = s->p_sys;
981     uint8_t *p_data;
982     unsigned int i_data = 0;
983     block_t *b;
984     unsigned int i_offset;
985
986     if( p_sys->block.p_current == NULL ) return 0; /* EOF */
987
988     /* We can directly give a pointer over our buffer */
989     if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
990     {
991         *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
992         return i_read;
993     }
994
995     /* We need to create a local copy */
996     if( p_sys->i_peek < i_read )
997     {
998         p_sys->p_peek = realloc( p_sys->p_peek, i_read );
999         if( !p_sys->p_peek )
1000         {
1001             p_sys->i_peek = 0;
1002             return 0;
1003         }
1004         p_sys->i_peek = i_read;
1005     }
1006
1007     /* Fill enough data */
1008     while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
1009            < i_read )
1010     {
1011         block_t **pp_last = p_sys->block.pp_last;
1012
1013         if( AStreamRefillBlock( s ) ) break;
1014
1015         /* Our buffer are probably filled enough, don't try anymore */
1016         if( pp_last == p_sys->block.pp_last ) break;
1017     }
1018
1019     /* Copy what we have */
1020     b = p_sys->block.p_current;
1021     i_offset = p_sys->block.i_offset;
1022     p_data = p_sys->p_peek;
1023
1024     while( b && i_data < i_read )
1025     {
1026         unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
1027         int i_copy = __MIN( i_current, i_read - i_data );
1028
1029         memcpy( p_data, &b->p_buffer[i_offset], i_copy );
1030         i_data += i_copy;
1031         p_data += i_copy;
1032         i_offset += i_copy;
1033
1034         if( i_offset >= b->i_buffer )
1035         {
1036             i_offset = 0;
1037             b = b->p_next;
1038         }
1039     }
1040
1041     *pp_peek = p_sys->p_peek;
1042     return i_data;
1043 }
1044
1045 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
1046 {
1047     stream_sys_t *p_sys = s->p_sys;
1048     access_t   *p_access = p_sys->p_access;
1049     int64_t    i_offset = i_pos - p_sys->block.i_start;
1050     bool b_seek;
1051
1052     /* We already have thoses data, just update p_current/i_offset */
1053     if( i_offset >= 0 && i_offset < p_sys->block.i_size )
1054     {
1055         block_t *b = p_sys->block.p_first;
1056         int i_current = 0;
1057
1058         while( i_current + b->i_buffer < i_offset )
1059         {
1060             i_current += b->i_buffer;
1061             b = b->p_next;
1062         }
1063
1064         p_sys->block.p_current = b;
1065         p_sys->block.i_offset = i_offset - i_current;
1066
1067         p_sys->i_pos = i_pos;
1068
1069         return VLC_SUCCESS;
1070     }
1071
1072     /* We may need to seek or to read data */
1073     if( i_offset < 0 )
1074     {
1075         bool b_aseek;
1076         access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1077
1078         if( !b_aseek )
1079         {
1080             msg_Err( s, "backward seeking impossible (access not seekable)" );
1081             return VLC_EGENERIC;
1082         }
1083
1084         b_seek = true;
1085     }
1086     else
1087     {
1088         bool b_aseek, b_aseekfast;
1089
1090         access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1091         access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
1092
1093         if( !b_aseek )
1094         {
1095             b_seek = false;
1096             msg_Warn( s, "%"PRId64" bytes need to be skipped "
1097                       "(access non seekable)",
1098                       i_offset - p_sys->block.i_size );
1099         }
1100         else
1101         {
1102             int64_t i_skip = i_offset - p_sys->block.i_size;
1103
1104             /* Avg bytes per packets */
1105             int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
1106             /* TODO compute a seek cost instead of fixed threshold */
1107             int i_th = b_aseekfast ? 1 : 5;
1108
1109             if( i_skip <= i_th * i_avg &&
1110                 i_skip < STREAM_CACHE_SIZE )
1111                 b_seek = false;
1112             else
1113                 b_seek = true;
1114
1115             msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
1116                      b_seek, i_th*i_avg, i_skip );
1117         }
1118     }
1119
1120     if( b_seek )
1121     {
1122         int64_t i_start, i_end;
1123         /* Do the access seek */
1124         i_start = mdate();
1125         if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1126         i_end = mdate();
1127
1128         /* Release data */
1129         block_ChainRelease( p_sys->block.p_first );
1130
1131         /* Reinit */
1132         p_sys->block.i_start = p_sys->i_pos = i_pos;
1133         p_sys->block.i_offset = 0;
1134         p_sys->block.p_current = NULL;
1135         p_sys->block.i_size = 0;
1136         p_sys->block.p_first = NULL;
1137         p_sys->block.pp_last = &p_sys->block.p_first;
1138
1139         /* Refill a block */
1140         if( AStreamRefillBlock( s ) )
1141             return VLC_EGENERIC;
1142
1143         /* Update stat */
1144         p_sys->stat.i_seek_time += i_end - i_start;
1145         p_sys->stat.i_seek_count++;
1146         return VLC_SUCCESS;
1147     }
1148     else
1149     {
1150         do
1151         {
1152             /* Read and skip enough data */
1153             if( AStreamRefillBlock( s ) )
1154                 return VLC_EGENERIC;
1155
1156             while( p_sys->block.p_current &&
1157                    p_sys->i_pos + p_sys->block.p_current->i_buffer - p_sys->block.i_offset < i_pos )
1158             {
1159                 p_sys->i_pos += p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
1160                 p_sys->block.p_current = p_sys->block.p_current->p_next;
1161                 p_sys->block.i_offset = 0;
1162             }
1163         }
1164         while( p_sys->block.i_start + p_sys->block.i_size < i_pos );
1165
1166         p_sys->block.i_offset = i_pos - p_sys->i_pos;
1167         p_sys->i_pos = i_pos;
1168
1169         return VLC_SUCCESS;
1170     }
1171
1172     return VLC_EGENERIC;
1173 }
1174
1175 static int AStreamRefillBlock( stream_t *s )
1176 {
1177     stream_sys_t *p_sys = s->p_sys;
1178     int64_t      i_start, i_stop;
1179     block_t      *b;
1180
1181     /* Release data */
1182     while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1183            p_sys->block.p_first != p_sys->block.p_current )
1184     {
1185         block_t *b = p_sys->block.p_first;
1186
1187         p_sys->block.i_start += b->i_buffer;
1188         p_sys->block.i_size  -= b->i_buffer;
1189         p_sys->block.p_first  = b->p_next;
1190
1191         block_Release( b );
1192     }
1193     if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1194         p_sys->block.p_current == p_sys->block.p_first &&
1195         p_sys->block.p_current->p_next )    /* At least 2 packets */
1196     {
1197         /* Enough data, don't read more */
1198         return VLC_SUCCESS;
1199     }
1200
1201     /* Now read a new block */
1202     i_start = mdate();
1203     for( ;; )
1204     {
1205         bool b_eof;
1206
1207         if( s->b_die )
1208             return VLC_EGENERIC;
1209
1210         /* Fetch a block */
1211         if( ( b = AReadBlock( s, &b_eof ) ) )
1212             break;
1213         if( b_eof )
1214             return VLC_EGENERIC;
1215     }
1216
1217     while( b )
1218     {
1219         i_stop = mdate();
1220
1221         /* Append the block */
1222         p_sys->block.i_size += b->i_buffer;
1223         *p_sys->block.pp_last = b;
1224         p_sys->block.pp_last = &b->p_next;
1225
1226         /* Fix p_current */
1227         if( p_sys->block.p_current == NULL )
1228             p_sys->block.p_current = b;
1229
1230         /* Update stat */
1231         p_sys->stat.i_bytes += b->i_buffer;
1232         p_sys->stat.i_read_time += i_stop - i_start;
1233         p_sys->stat.i_read_count++;
1234
1235         b = b->p_next;
1236         i_start = mdate();
1237     }
1238     return VLC_SUCCESS;
1239 }
1240
1241
1242 /****************************************************************************
1243  * Method 2:
1244  ****************************************************************************/
1245 static int AStreamRefillStream( stream_t *s );
1246
1247 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read )
1248 {
1249     stream_sys_t *p_sys = s->p_sys;
1250     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1251
1252     uint8_t *p_data = (uint8_t *)p_read;
1253     uint8_t *p_record = p_data;
1254     unsigned int i_data = 0;
1255
1256     if( tk->i_start >= tk->i_end )
1257         return 0; /* EOF */
1258
1259     if( p_sys->record.b_active && !p_data )
1260         p_record = p_data = malloc( i_read );
1261
1262     if( p_data == NULL )
1263     {
1264         /* seek within this stream if possible, else use plain old read and discard */
1265         stream_sys_t *p_sys = s->p_sys;
1266         access_t     *p_access = p_sys->p_access;
1267
1268         /* seeking after EOF is not what we want */
1269         if( !( p_access->info.b_eof ) )
1270         {
1271             bool   b_aseek;
1272             access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1273             if( b_aseek )
1274                 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1275         }
1276     }
1277
1278 #ifdef STREAM_DEBUG
1279     msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1280              " offset=%d end=%"PRId64,
1281              i_read, p_sys->i_pos, p_sys->stream.i_tk,
1282              tk->i_start, p_sys->stream.i_offset, tk->i_end );
1283 #endif
1284
1285     while( i_data < i_read )
1286     {
1287         int i_off = (tk->i_start + p_sys->stream.i_offset) %
1288                     STREAM_CACHE_TRACK_SIZE;
1289         unsigned int i_current =
1290             __MAX(0,__MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1291                    STREAM_CACHE_TRACK_SIZE - i_off ));
1292         int i_copy = __MIN( i_current, i_read - i_data );
1293
1294         if( i_copy <= 0 ) break; /* EOF */
1295
1296         /* Copy data */
1297         /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1298         if( p_data )
1299         {
1300             memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1301             p_data += i_copy;
1302         }
1303         i_data += i_copy;
1304         p_sys->stream.i_offset += i_copy;
1305
1306         /* Update pos now */
1307         p_sys->i_pos += i_copy;
1308
1309         /* */
1310         p_sys->stream.i_used += i_copy;
1311         if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1312             p_sys->stream.i_used >= p_sys->stream.i_read_size )
1313         {
1314             if( AStreamRefillStream( s ) )
1315             {
1316                 /* EOF */
1317                 if( tk->i_start >= tk->i_end ) break;
1318             }
1319         }
1320     }
1321
1322     if( p_sys->record.b_active )
1323     {
1324         if( i_data > 0 && p_record != NULL)
1325             ARecordWrite( s, p_record, i_data );
1326         if( !p_read )
1327             free( p_record );
1328     }
1329
1330     return i_data;
1331 }
1332
1333 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1334 {
1335     stream_sys_t *p_sys = s->p_sys;
1336     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1337     int64_t i_off;
1338
1339     if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1340
1341 #ifdef STREAM_DEBUG
1342     msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1343              "start=%"PRId64" offset=%d end=%"PRId64,
1344              i_read, p_sys->i_pos, p_sys->stream.i_tk,
1345              tk->i_start, p_sys->stream.i_offset, tk->i_end );
1346 #endif
1347
1348     /* Avoid problem, but that should *never* happen */
1349     if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1350         i_read = STREAM_CACHE_TRACK_SIZE / 2;
1351
1352     while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1353     {
1354         if( p_sys->stream.i_used <= 1 )
1355         {
1356             /* Be sure we will read something */
1357             p_sys->stream.i_used += i_read -
1358                 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1359         }
1360         if( AStreamRefillStream( s ) ) break;
1361     }
1362
1363     if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1364         i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1365
1366     /* Now, direct pointer or a copy ? */
1367     i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1368     if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1369     {
1370         *pp_peek = &tk->p_buffer[i_off];
1371         return i_read;
1372     }
1373
1374     if( p_sys->i_peek < i_read )
1375     {
1376         p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1377         if( !p_sys->p_peek )
1378         {
1379             p_sys->i_peek = 0;
1380             return 0;
1381         }
1382         p_sys->i_peek = i_read;
1383     }
1384
1385     memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1386             STREAM_CACHE_TRACK_SIZE - i_off );
1387     memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1388             &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1389
1390     *pp_peek = p_sys->p_peek;
1391     return i_read;
1392 }
1393
1394 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1395 {
1396     stream_sys_t *p_sys = s->p_sys;
1397     access_t     *p_access = p_sys->p_access;
1398     bool   b_aseek;
1399     bool   b_afastseek;
1400     int i_maxth;
1401     int i_new;
1402     int i;
1403
1404 #ifdef STREAM_DEBUG
1405     msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1406              " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1407              i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1408              p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1409              p_sys->stream.i_offset,
1410              p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1411 #endif
1412
1413
1414     /* Seek in our current track ? */
1415     if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1416         i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1417     {
1418         stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1419 #ifdef STREAM_DEBUG
1420         msg_Dbg( s, "AStreamSeekStream: current track" );
1421 #endif
1422         p_sys->i_pos = i_pos;
1423         p_sys->stream.i_offset = i_pos - tk->i_start;
1424
1425         /* If there is not enough data left in the track, refill  */
1426         /* \todo How to get a correct value for
1427          *    - refilling threshold
1428          *    - how much to refill
1429          */
1430         if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1431                                              p_sys->stream.i_read_size )
1432         {
1433             if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2  )
1434             {
1435                 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1436                 AStreamRefillStream( s );
1437             }
1438         }
1439         return VLC_SUCCESS;
1440     }
1441
1442     access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1443     if( !b_aseek )
1444     {
1445         /* We can't do nothing */
1446         msg_Dbg( s, "AStreamSeekStream: can't seek" );
1447         return VLC_EGENERIC;
1448     }
1449
1450     /* Date the current track */
1451     p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1452
1453     /* Try to reuse already read data */
1454     for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1455     {
1456         stream_track_t *tk = &p_sys->stream.tk[i];
1457
1458         if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1459         {
1460 #ifdef STREAM_DEBUG
1461             msg_Dbg( s, "AStreamSeekStream: reusing %d start=%"PRId64
1462                      " end=%"PRId64, i, tk->i_start, tk->i_end );
1463 #endif
1464
1465             /* Seek at the end of the buffer */
1466             if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1467
1468             /* That's it */
1469             p_sys->i_pos = i_pos;
1470             p_sys->stream.i_tk = i;
1471             p_sys->stream.i_offset = i_pos - tk->i_start;
1472
1473             if( p_sys->stream.i_used < 1024 )
1474                 p_sys->stream.i_used = 1024;
1475
1476             if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1477                 return VLC_EGENERIC;
1478
1479             return VLC_SUCCESS;
1480         }
1481     }
1482
1483     access_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1484     /* FIXME compute seek cost (instead of static 'stupid' value) */
1485     i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1486     if( !b_afastseek )
1487         i_maxth *= 3;
1488
1489     /* FIXME TODO */
1490 #if 0
1491     /* Search closest segment TODO */
1492     for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1493     {
1494         stream_track_t *tk = &p_sys->stream.tk[i];
1495
1496         if( i_pos + i_maxth >= tk->i_start )
1497         {
1498             msg_Dbg( s, "good segment before current pos, TODO" );
1499         }
1500         if( i_pos - i_maxth <= tk->i_end )
1501         {
1502             msg_Dbg( s, "good segment after current pos, TODO" );
1503         }
1504     }
1505 #endif
1506
1507     /* Nothing good, seek and choose oldest segment */
1508     if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1509     p_sys->i_pos = i_pos;
1510
1511     i_new = 0;
1512     for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1513     {
1514         if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1515             i_new = i;
1516     }
1517
1518     /* Reset the segment */
1519     p_sys->stream.i_tk     = i_new;
1520     p_sys->stream.i_offset =  0;
1521     p_sys->stream.tk[i_new].i_start = i_pos;
1522     p_sys->stream.tk[i_new].i_end   = i_pos;
1523
1524     /* Read data */
1525     if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1526         p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1527
1528     if( AStreamRefillStream( s ) )
1529         return VLC_EGENERIC;
1530
1531     return VLC_SUCCESS;
1532 }
1533
1534 static int AStreamRefillStream( stream_t *s )
1535 {
1536     stream_sys_t *p_sys = s->p_sys;
1537     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1538
1539     /* We read but won't increase i_start after initial start + offset */
1540     int i_toread =
1541         __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1542                (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1543     bool b_read = false;
1544     int64_t i_start, i_stop;
1545
1546     if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1547
1548 #ifdef STREAM_DEBUG
1549     msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1550                  p_sys->stream.i_used, i_toread );
1551 #endif
1552
1553     i_start = mdate();
1554     while( i_toread > 0 )
1555     {
1556         int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1557         int i_read;
1558
1559         if( s->b_die )
1560             return VLC_EGENERIC;
1561
1562         i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1563         i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1564
1565         /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1566         if( i_read <  0 )
1567         {
1568             continue;
1569         }
1570         else if( i_read == 0 )
1571         {
1572             if( !b_read )
1573                 return VLC_EGENERIC;
1574             return VLC_SUCCESS;
1575         }
1576         b_read = true;
1577
1578         /* Update end */
1579         tk->i_end += i_read;
1580
1581         /* Windows of STREAM_CACHE_TRACK_SIZE */
1582         if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1583         {
1584             int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1585
1586             tk->i_start += i_invalid;
1587             p_sys->stream.i_offset -= i_invalid;
1588         }
1589
1590         i_toread -= i_read;
1591         p_sys->stream.i_used -= i_read;
1592
1593         p_sys->stat.i_bytes += i_read;
1594         p_sys->stat.i_read_count++;
1595     }
1596     i_stop = mdate();
1597
1598     p_sys->stat.i_read_time += i_stop - i_start;
1599
1600     return VLC_SUCCESS;
1601 }
1602
1603 static void AStreamPrebufferStream( stream_t *s )
1604 {
1605     stream_sys_t *p_sys = s->p_sys;
1606     access_t     *p_access = p_sys->p_access;
1607
1608     int64_t i_first = 0;
1609     int64_t i_start;
1610     int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1611         ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1612           STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1613
1614     msg_Dbg( s, "pre-buffering..." );
1615     i_start = mdate();
1616     for( ;; )
1617     {
1618         stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1619
1620         int64_t i_date = mdate();
1621         int i_read;
1622
1623         if( s->b_die || tk->i_end >= i_prebuffer ||
1624             (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1625         {
1626             int64_t i_byterate;
1627
1628             /* Update stat */
1629             p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1630             p_sys->stat.i_read_time = i_date - i_start;
1631             i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1632                          (p_sys->stat.i_read_time+1);
1633
1634             msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1635                      "%"PRId64" kbytes/s",
1636                      p_sys->stat.i_bytes,
1637                      p_sys->stat.i_read_time / INT64_C(1000000),
1638                      i_byterate / 1024 );
1639             break;
1640         }
1641
1642         /* */
1643         i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1644         i_read = __MIN( p_sys->stream.i_read_size, i_read );
1645         i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1646         if( i_read <  0 )
1647             continue;
1648         else if( i_read == 0 )
1649             break;  /* EOF */
1650
1651         if( i_first == 0 )
1652         {
1653             i_first = mdate();
1654             msg_Dbg( s, "received first data for our buffer");
1655         }
1656
1657         tk->i_end += i_read;
1658
1659         p_sys->stat.i_read_count++;
1660     }
1661 }
1662
1663 /****************************************************************************
1664  * Method 3:
1665  ****************************************************************************/
1666
1667 static int AStreamReadImmediate( stream_t *s, void *p_read, unsigned int i_read )
1668 {
1669     stream_sys_t *p_sys = s->p_sys;
1670     uint8_t *p_data= (uint8_t*)p_read;
1671     uint8_t *p_record = p_data;
1672     unsigned int i_data;
1673
1674 #ifdef STREAM_DEBUG
1675     msg_Dbg( s, "AStreamReadImmediate p_read=%p i_read=%d",
1676              p_read, i_read );
1677 #endif
1678
1679     if( p_sys->record.b_active && !p_data )
1680         p_record = p_data = malloc( i_read );
1681
1682     /* First, check if we already have some data in the buffer,
1683      * that we could copy directly */
1684     i_data = __MIN( stream_buffered_size( s ), i_read );
1685     if( i_data > 0 )
1686     {
1687 #ifdef STREAM_DEBUG
1688         msg_Dbg( s, "AStreamReadImmediate: copy %u from %p", i_data, stream_buffer( s ) );
1689 #endif
1690
1691         assert( i_data <= STREAM_CACHE_SIZE );
1692
1693         if( p_data )
1694         {
1695             memcpy( p_data, stream_buffer( s ), i_data );
1696             p_data += i_data;
1697         }
1698     }
1699
1700     /* Now that we've read our buffer we don't need its i_copy bytes */
1701     stream_buffer_empty( s, i_data );
1702
1703     /* Now check if we have still to really read some data */
1704     while( i_data < i_read )
1705     {
1706         const unsigned int i_to_read = i_read - i_data;
1707         int i_result;
1708
1709         if( p_data )
1710         {
1711             i_result = AReadStream( s, p_data, i_to_read );
1712         }
1713         else
1714         {
1715             void *p_dummy = malloc( i_to_read );
1716
1717             if( !p_dummy )
1718                 break;
1719
1720             i_result = AReadStream( s, p_dummy, i_to_read );
1721
1722             free( p_dummy );
1723         }
1724         if( i_result <= 0 )
1725             break;
1726
1727         p_sys->i_pos += i_data;
1728         if( p_data )
1729             p_data += i_result;
1730         i_data += i_result;
1731     }
1732
1733     if( p_sys->record.b_active )
1734     {
1735         if( i_data > 0 && p_record != NULL)
1736             ARecordWrite( s, p_record, i_data );
1737         if( !p_read )
1738             free( p_record );
1739     }
1740     return i_data;
1741 }
1742
1743 static int AStreamPeekImmediate( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1744 {
1745 #ifdef STREAM_DEBUG
1746     msg_Dbg( s, "AStreamPeekImmediate: %d  size=%"PRId64,
1747              i_read, size_buffered_size( s ) );
1748 #endif
1749
1750     /* Avoid problem, but that shouldn't happen
1751      * FIXME yes it can */
1752     if( i_read > STREAM_CACHE_SIZE / 2 )
1753         i_read = STREAM_CACHE_SIZE / 2;
1754
1755     int i_to_read = i_read - stream_buffered_size( s );
1756     if( i_to_read > 0 )
1757     {
1758 #ifdef STREAM_DEBUG
1759         msg_Dbg( s, "AStreamPeekImmediate: Reading %d",
1760              i_to_read );
1761 #endif
1762         i_to_read = AReadStream( s, stream_buffer( s ) + stream_buffered_size( s ),
1763                                  i_to_read );
1764
1765         if( i_to_read > 0 )
1766             stream_buffer_fill( s, i_to_read );
1767     }
1768
1769     *pp_peek = stream_buffer( s );
1770
1771     return __MIN( stream_buffered_size( s ), i_read );
1772 }
1773
1774 static int AStreamSeekImmediate( stream_t *s, int64_t i_pos )
1775 {
1776     stream_sys_t *p_sys = s->p_sys;
1777     access_t     *p_access = p_sys->p_access;
1778     bool   b_aseek;
1779
1780 #ifdef STREAM_DEBUG
1781     msg_Dbg( s, "AStreamSeekImmediate to %"PRId64" pos=%"PRId64
1782              i_pos, p_sys->i_pos );
1783 #endif
1784
1785     access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1786     if( !b_aseek )
1787     {
1788         /* We can't do nothing */
1789         msg_Dbg( s, "AStreamSeekImmediate: can't seek" );
1790         return VLC_EGENERIC;
1791     }
1792
1793     /* Just reset our buffer */
1794     stream_buffer_empty( s, stream_buffered_size( s ) );
1795
1796     if( ASeek( s, i_pos ) )
1797         return VLC_EGENERIC;
1798
1799     return VLC_SUCCESS;
1800 }
1801
1802 /****************************************************************************
1803  * stream_ReadLine:
1804  ****************************************************************************/
1805 /**
1806  * Read from the stream untill first newline.
1807  * \param s Stream handle to read from
1808  * \return A pointer to the allocated output string. You need to free this when you are done.
1809  */
1810 #define STREAM_PROBE_LINE 2048
1811 #define STREAM_LINE_MAX (2048*100)
1812 char *stream_ReadLine( stream_t *s )
1813 {
1814     char *p_line = NULL;
1815     int i_line = 0, i_read = 0;
1816
1817     while( i_read < STREAM_LINE_MAX )
1818     {
1819         char *psz_eol;
1820         const uint8_t *p_data;
1821         int i_data;
1822         int64_t i_pos;
1823
1824         /* Probe new data */
1825         i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1826         if( i_data <= 0 ) break; /* No more data */
1827
1828         /* BOM detection */
1829         i_pos = stream_Tell( s );
1830         if( i_pos == 0 && i_data >= 3 )
1831         {
1832             int i_bom_size = 0;
1833             const char *psz_encoding = NULL;
1834
1835             if( !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
1836             {
1837                 psz_encoding = "UTF-8";
1838                 i_bom_size = 3;
1839             }
1840             else if( !memcmp( p_data, "\xFF\xFE", 2 ) )
1841             {
1842                 psz_encoding = "UTF-16LE";
1843                 s->b_little_endian = true;
1844                 s->i_char_width = 2;
1845                 i_bom_size = 2;
1846             }
1847             else if( !memcmp( p_data, "\xFE\xFF", 2 ) )
1848             {
1849                 psz_encoding = "UTF-16BE";
1850                 s->i_char_width = 2;
1851                 i_bom_size = 2;
1852             }
1853
1854             /* Seek past the BOM */
1855             if( i_bom_size )
1856             {
1857                 stream_Seek( s, i_bom_size );
1858                 p_data += i_bom_size;
1859                 i_data -= i_bom_size;
1860             }
1861
1862             /* Open the converter if we need it */
1863             if( psz_encoding != NULL )
1864             {
1865                 input_thread_t *p_input;
1866                 msg_Dbg( s, "%s BOM detected", psz_encoding );
1867                 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1868                 if( s->i_char_width > 1 )
1869                 {
1870                     s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1871                     if( s->conv == (vlc_iconv_t)-1 )
1872                     {
1873                         msg_Err( s, "iconv_open failed" );
1874                     }
1875                 }
1876                 if( p_input != NULL)
1877                 {
1878                     var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1879                     var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1880                     vlc_object_release( p_input );
1881                 }
1882             }
1883         }
1884
1885         if( i_data % s->i_char_width )
1886         {
1887             /* keep i_char_width boundary */
1888             i_data = i_data - ( i_data % s->i_char_width );
1889             msg_Warn( s, "the read is not i_char_width compatible");
1890         }
1891
1892         if( i_data == 0 )
1893             break;
1894
1895         /* Check if there is an EOL */
1896         if( s->i_char_width == 1 )
1897         {
1898             /* UTF-8: 0A <LF> */
1899             psz_eol = memchr( p_data, '\n', i_data );
1900         }
1901         else
1902         {
1903             const uint8_t *p = p_data;
1904             const uint8_t *p_last = p + i_data - s->i_char_width;
1905
1906             if( s->i_char_width == 2 )
1907             {
1908                 if( s->b_little_endian == true)
1909                 {
1910                     /* UTF-16LE: 0A 00 <LF> */
1911                     while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1912                         p += 2;
1913                 }
1914                 else
1915                 {
1916                     /* UTF-16BE: 00 0A <LF> */
1917                     while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1918                         p += 2;
1919                 }
1920             }
1921
1922             if( p > p_last )
1923             {
1924                 psz_eol = NULL;
1925             }
1926             else
1927             {
1928                 psz_eol = (char *)p + ( s->i_char_width - 1 );
1929             }
1930         }
1931
1932         if(psz_eol)
1933         {
1934             i_data = (psz_eol - (char *)p_data) + 1;
1935             p_line = realloc( p_line, i_line + i_data + s->i_char_width ); /* add \0 */
1936             if( !p_line )
1937                 goto error;
1938             i_data = stream_Read( s, &p_line[i_line], i_data );
1939             if( i_data <= 0 ) break; /* Hmmm */
1940             i_line += i_data - s->i_char_width; /* skip \n */;
1941             i_read += i_data;
1942
1943             /* We have our line */
1944             break;
1945         }
1946
1947         /* Read data (+1 for easy \0 append) */
1948         p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->i_char_width );
1949         if( !p_line )
1950             goto error;
1951         i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1952         if( i_data <= 0 ) break; /* Hmmm */
1953         i_line += i_data;
1954         i_read += i_data;
1955     }
1956
1957     if( i_read > 0 )
1958     {
1959         int j;
1960         for( j = 0; j < s->i_char_width; j++ )
1961         {
1962             p_line[i_line + j] = '\0';
1963         }
1964         i_line += s->i_char_width; /* the added \0 */
1965         if( s->i_char_width > 1 )
1966         {
1967             size_t i_in = 0, i_out = 0;
1968             const char * p_in = NULL;
1969             char * p_out = NULL;
1970             char * psz_new_line = NULL;
1971
1972             /* iconv */
1973             psz_new_line = malloc( i_line );
1974             if( psz_new_line == NULL )
1975                 goto error;
1976             i_in = i_out = (size_t)i_line;
1977             p_in = p_line;
1978             p_out = psz_new_line;
1979
1980             if( vlc_iconv( s->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1981             {
1982                 msg_Err( s, "iconv failed" );
1983                 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1984             }
1985             free( p_line );
1986             p_line = psz_new_line;
1987             i_line = (size_t)i_line - i_out; /* does not include \0 */
1988         }
1989
1990         /* Remove trailing LF/CR */
1991         while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1992             p_line[i_line-2] == '\n') ) i_line--;
1993
1994         /* Make sure the \0 is there */
1995         p_line[i_line-1] = '\0';
1996
1997         return p_line;
1998     }
1999
2000 error:
2001
2002     /* We failed to read any data, probably EOF */
2003     free( p_line );
2004     if( s->conv != (vlc_iconv_t)(-1) ) vlc_iconv_close( s->conv );
2005     return NULL;
2006 }
2007
2008 /****************************************************************************
2009  * Access reading/seeking wrappers to handle concatenated streams.
2010  ****************************************************************************/
2011 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read )
2012 {
2013     stream_sys_t *p_sys = s->p_sys;
2014     access_t *p_access = p_sys->p_access;
2015     input_thread_t *p_input = NULL;
2016     int i_read_orig = i_read;
2017     int i_total = 0;
2018
2019     if( s->p_parent && s->p_parent->p_parent &&
2020         s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
2021         p_input = (input_thread_t *)s->p_parent->p_parent;
2022
2023     if( !p_sys->i_list )
2024     {
2025         i_read = p_access->pf_read( p_access, p_read, i_read );
2026         if( p_access->b_die )
2027             vlc_object_kill( s );
2028         if( p_input )
2029         {
2030             vlc_mutex_lock( &p_input->p->counters.counters_lock );
2031             stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
2032                              &i_total );
2033             stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2034                            (float)i_total, NULL );
2035             stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2036             vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2037         }
2038         return i_read;
2039     }
2040
2041     i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
2042                                             i_read );
2043     if( p_access->b_die )
2044         vlc_object_kill( s );
2045
2046     /* If we reached an EOF then switch to the next stream in the list */
2047     if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
2048     {
2049         char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
2050         access_t *p_list_access;
2051
2052         msg_Dbg( s, "opening input `%s'", psz_name );
2053
2054         p_list_access = access_New( s, p_access->psz_access, "", psz_name );
2055
2056         if( !p_list_access ) return 0;
2057
2058         if( p_sys->p_list_access != p_access )
2059             access_Delete( p_sys->p_list_access );
2060
2061         p_sys->p_list_access = p_list_access;
2062
2063         /* We have to read some data */
2064         return AReadStream( s, p_read, i_read_orig );
2065     }
2066
2067     /* Update read bytes in input */
2068     if( p_input )
2069     {
2070         vlc_mutex_lock( &p_input->p->counters.counters_lock );
2071         stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
2072         stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2073                        (float)i_total, NULL );
2074         stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2075         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2076     }
2077     return i_read;
2078 }
2079
2080 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
2081 {
2082     stream_sys_t *p_sys = s->p_sys;
2083     access_t *p_access = p_sys->p_access;
2084     input_thread_t *p_input = NULL;
2085     block_t *p_block;
2086     bool b_eof;
2087     int i_total = 0;
2088
2089     if( s->p_parent && s->p_parent->p_parent &&
2090         s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
2091         p_input = (input_thread_t *)s->p_parent->p_parent;
2092
2093     if( !p_sys->i_list )
2094     {
2095         p_block = p_access->pf_block( p_access );
2096         if( p_access->b_die )
2097             vlc_object_kill( s );
2098         if( pb_eof ) *pb_eof = p_access->info.b_eof;
2099         if( p_input && p_block && libvlc_stats (p_access) )
2100         {
2101             vlc_mutex_lock( &p_input->p->counters.counters_lock );
2102             stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
2103                                  p_block->i_buffer, &i_total );
2104             stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2105                               (float)i_total, NULL );
2106             stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2107             vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2108         }
2109         return p_block;
2110     }
2111
2112     p_block = p_sys->p_list_access->pf_block( p_sys->p_list_access );
2113     if( p_access->b_die )
2114         vlc_object_kill( s );
2115     b_eof = p_sys->p_list_access->info.b_eof;
2116     if( pb_eof ) *pb_eof = b_eof;
2117
2118     /* If we reached an EOF then switch to the next stream in the list */
2119     if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
2120     {
2121         char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
2122         access_t *p_list_access;
2123
2124         msg_Dbg( s, "opening input `%s'", psz_name );
2125
2126         p_list_access = access_New( s, p_access->psz_access, "", psz_name );
2127
2128         if( !p_list_access ) return 0;
2129
2130         if( p_sys->p_list_access != p_access )
2131             access_Delete( p_sys->p_list_access );
2132
2133         p_sys->p_list_access = p_list_access;
2134
2135         /* We have to read some data */
2136         return AReadBlock( s, pb_eof );
2137     }
2138     if( p_block )
2139     {
2140         if( p_input )
2141         {
2142             vlc_mutex_lock( &p_input->p->counters.counters_lock );
2143             stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
2144                                  p_block->i_buffer, &i_total );
2145             stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2146                               (float)i_total, NULL );
2147             stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
2148                                  1 , NULL);
2149             vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2150         }
2151     }
2152     return p_block;
2153 }
2154
2155 static int ASeek( stream_t *s, int64_t i_pos )
2156 {
2157     stream_sys_t *p_sys = s->p_sys;
2158     access_t *p_access = p_sys->p_access;
2159
2160     /* Check which stream we need to access */
2161     if( p_sys->i_list )
2162     {
2163         int i;
2164         char *psz_name;
2165         int64_t i_size = 0;
2166         access_t *p_list_access = 0;
2167
2168         for( i = 0; i < p_sys->i_list - 1; i++ )
2169         {
2170             if( i_pos < p_sys->list[i]->i_size + i_size ) break;
2171             i_size += p_sys->list[i]->i_size;
2172         }
2173         psz_name = p_sys->list[i]->psz_path;
2174
2175         if( i != p_sys->i_list_index )
2176             msg_Dbg( s, "opening input `%s'", psz_name );
2177
2178         if( i != p_sys->i_list_index && i != 0 )
2179         {
2180             p_list_access =
2181                 access_New( s, p_access->psz_access, "", psz_name );
2182         }
2183         else if( i != p_sys->i_list_index )
2184         {
2185             p_list_access = p_access;
2186         }
2187
2188         if( p_list_access )
2189         {
2190             if( p_sys->p_list_access != p_access )
2191                 access_Delete( p_sys->p_list_access );
2192
2193             p_sys->p_list_access = p_list_access;
2194         }
2195
2196         p_sys->i_list_index = i;
2197         return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
2198                                               i_pos - i_size );
2199     }
2200
2201     return p_access->pf_seek( p_access, i_pos );
2202 }
2203
2204
2205 /**
2206  * Try to read "i_read" bytes into a buffer pointed by "p_read".  If
2207  * "p_read" is NULL then data are skipped instead of read.  The return
2208  * value is the real numbers of bytes read/skip. If this value is less
2209  * than i_read that means that it's the end of the stream.
2210  */
2211 int stream_Read( stream_t *s, void *p_read, int i_read )
2212 {
2213     return s->pf_read( s, p_read, i_read );
2214 }
2215
2216 /**
2217  * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
2218  * \return The real numbers of valid bytes, if it's less
2219  * or equal to 0, *pp_peek is invalid.
2220  * \note pp_peek is a pointer to internal buffer and it will be invalid as
2221  * soons as other stream_* functions are called.
2222  * \note Due to input limitation, it could be less than i_peek without meaning
2223  * the end of the stream (but only when you have i_peek >=
2224  * p_input->i_bufsize)
2225  */
2226 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
2227 {
2228     return s->pf_peek( s, pp_peek, i_peek );
2229 }
2230
2231 /**
2232  * Use to control the "stream_t *". Look at #stream_query_e for
2233  * possible "i_query" value and format arguments.  Return VLC_SUCCESS
2234  * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
2235  */
2236 int stream_vaControl( stream_t *s, int i_query, va_list args )
2237 {
2238     return s->pf_control( s, i_query, args );
2239 }
2240
2241 /**
2242  * Destroy a stream
2243  */
2244 void stream_Delete( stream_t *s )
2245 {
2246     s->pf_destroy( s );
2247 }
2248
2249 int stream_Control( stream_t *s, int i_query, ... )
2250 {
2251     va_list args;
2252     int     i_result;
2253
2254     if( s == NULL )
2255         return VLC_EGENERIC;
2256
2257     va_start( args, i_query );
2258     i_result = s->pf_control( s, i_query, args );
2259     va_end( args );
2260     return i_result;
2261 }
2262
2263 /**
2264  * Read "i_size" bytes and store them in a block_t.
2265  * It always read i_size bytes unless you are at the end of the stream
2266  * where it return what is available.
2267  */
2268 block_t *stream_Block( stream_t *s, int i_size )
2269 {
2270     if( i_size <= 0 ) return NULL;
2271
2272     /* emulate block read */
2273     block_t *p_bk = block_New( s, i_size );
2274     if( p_bk )
2275     {
2276         int i_read = stream_Read( s, p_bk->p_buffer, i_size );
2277         if( i_read > 0 )
2278         {
2279             p_bk->i_buffer = i_read;
2280             return p_bk;
2281         }
2282         block_Release( p_bk );
2283     }
2284     return NULL;
2285 }