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