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