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