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