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