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