]> git.sesse.net Git - vlc/blob - src/input/stream.c
48174e7b8544f287539a73f636696ea3ba754909
[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 *)s->p_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         if( p_data )
808         {
809             memcpy( p_data,
810                     &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
811                     i_copy );
812             p_data += i_copy;
813         }
814         i_data += i_copy;
815
816         p_sys->block.i_offset += i_copy;
817         if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
818         {
819             /* Current block is now empty, switch to next */
820             if( p_sys->block.p_current )
821             {
822                 p_sys->block.i_offset = 0;
823                 p_sys->block.p_current = p_sys->block.p_current->p_next;
824             }
825             /*Get a new block if needed */
826             if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
827             {
828                 break;
829             }
830         }
831     }
832
833     p_sys->i_pos += i_data;
834     return i_data;
835 }
836
837 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, int i_read )
838 {
839     stream_sys_t *p_sys = s->p_sys;
840     uint8_t *p_data;
841     int      i_data = 0;
842     block_t *b;
843     int      i_offset;
844
845     if( p_sys->block.p_current == NULL ) return 0; /* EOF */
846
847     /* We can directly give a pointer over our buffer */
848     if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
849     {
850         *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
851         return i_read;
852     }
853
854     /* We need to create a local copy */
855     if( p_sys->i_peek < i_read )
856     {
857         p_sys->p_peek = realloc( p_sys->p_peek, i_read );
858         if( !p_sys->p_peek )
859         {
860             p_sys->i_peek = 0;
861             return 0;
862         }
863         p_sys->i_peek = i_read;
864     }
865
866     /* Fill enough data */
867     while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
868            < i_read )
869     {
870         block_t **pp_last = p_sys->block.pp_last;
871
872         if( AStreamRefillBlock( s ) ) break;
873
874         /* Our buffer are probably filled enough, don't try anymore */
875         if( pp_last == p_sys->block.pp_last ) break;
876     }
877
878     /* Copy what we have */
879     b = p_sys->block.p_current;
880     i_offset = p_sys->block.i_offset;
881     p_data = p_sys->p_peek;
882
883     while( b && i_data < i_read )
884     {
885         int i_current = b->i_buffer - i_offset;
886         int i_copy = __MIN( i_current, i_read - i_data );
887
888         memcpy( p_data, &b->p_buffer[i_offset], i_copy );
889         i_data += i_copy;
890         p_data += i_copy;
891         i_offset += i_copy;
892
893         if( i_offset >= b->i_buffer )
894         {
895             i_offset = 0;
896             b = b->p_next;
897         }
898     }
899
900     *pp_peek = p_sys->p_peek;
901     return i_data;
902 }
903
904 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
905 {
906     stream_sys_t *p_sys = s->p_sys;
907     access_t   *p_access = p_sys->p_access;
908     int64_t    i_offset = i_pos - p_sys->block.i_start;
909     bool b_seek;
910
911     /* We already have thoses data, just update p_current/i_offset */
912     if( i_offset >= 0 && i_offset < p_sys->block.i_size )
913     {
914         block_t *b = p_sys->block.p_first;
915         int i_current = 0;
916
917         while( i_current + b->i_buffer < i_offset )
918         {
919             i_current += b->i_buffer;
920             b = b->p_next;
921         }
922
923         p_sys->block.p_current = b;
924         p_sys->block.i_offset = i_offset - i_current;
925
926         p_sys->i_pos = i_pos;
927
928         return VLC_SUCCESS;
929     }
930
931     /* We may need to seek or to read data */
932     if( i_offset < 0 )
933     {
934         bool b_aseek;
935         access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
936
937         if( !b_aseek )
938         {
939             msg_Err( s, "backward seeking impossible (access not seekable)" );
940             return VLC_EGENERIC;
941         }
942
943         b_seek = true;
944     }
945     else
946     {
947         bool b_aseek, b_aseekfast;
948
949         access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
950         access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
951
952         if( !b_aseek )
953         {
954             b_seek = false;
955             msg_Warn( s, "%"PRId64" bytes need to be skipped "
956                       "(access non seekable)",
957                       i_offset - p_sys->block.i_size );
958         }
959         else
960         {
961             int64_t i_skip = i_offset - p_sys->block.i_size;
962
963             /* Avg bytes per packets */
964             int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
965             /* TODO compute a seek cost instead of fixed threshold */
966             int i_th = b_aseekfast ? 1 : 5;
967
968             if( i_skip <= i_th * i_avg &&
969                 i_skip < STREAM_CACHE_SIZE )
970                 b_seek = false;
971             else
972                 b_seek = true;
973
974             msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
975                      b_seek, i_th*i_avg, i_skip );
976         }
977     }
978
979     if( b_seek )
980     {
981         int64_t i_start, i_end;
982         /* Do the access seek */
983         i_start = mdate();
984         if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
985         i_end = mdate();
986
987         /* Release data */
988         block_ChainRelease( p_sys->block.p_first );
989
990         /* Reinit */
991         p_sys->block.i_start = p_sys->i_pos = i_pos;
992         p_sys->block.i_offset = 0;
993         p_sys->block.p_current = NULL;
994         p_sys->block.i_size = 0;
995         p_sys->block.p_first = NULL;
996         p_sys->block.pp_last = &p_sys->block.p_first;
997
998         /* Refill a block */
999         if( AStreamRefillBlock( s ) )
1000             return VLC_EGENERIC;
1001
1002         /* Update stat */
1003         p_sys->stat.i_seek_time += i_end - i_start;
1004         p_sys->stat.i_seek_count++;
1005         return VLC_SUCCESS;
1006     }
1007     else
1008     {
1009         /* Read enough data */
1010         while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
1011         {
1012             if( AStreamRefillBlock( s ) )
1013                 return VLC_EGENERIC;
1014
1015             while( p_sys->block.p_current &&
1016                    p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
1017             {
1018                 p_sys->i_pos += p_sys->block.p_current->i_buffer;
1019                 p_sys->block.p_current = p_sys->block.p_current->p_next;
1020             }
1021         }
1022
1023         p_sys->block.i_offset = i_pos - p_sys->i_pos;
1024         p_sys->i_pos = i_pos;
1025
1026         return VLC_SUCCESS;
1027     }
1028
1029     return VLC_EGENERIC;
1030 }
1031
1032 static int AStreamRefillBlock( stream_t *s )
1033 {
1034     stream_sys_t *p_sys = s->p_sys;
1035     int64_t      i_start, i_stop;
1036     block_t      *b;
1037
1038     /* Release data */
1039     while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1040            p_sys->block.p_first != p_sys->block.p_current )
1041     {
1042         block_t *b = p_sys->block.p_first;
1043
1044         p_sys->block.i_start += b->i_buffer;
1045         p_sys->block.i_size  -= b->i_buffer;
1046         p_sys->block.p_first  = b->p_next;
1047
1048         block_Release( b );
1049     }
1050     if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1051         p_sys->block.p_current == p_sys->block.p_first &&
1052         p_sys->block.p_current->p_next )    /* At least 2 packets */
1053     {
1054         /* Enough data, don't read more */
1055         return VLC_SUCCESS;
1056     }
1057
1058     /* Now read a new block */
1059     i_start = mdate();
1060     for( ;; )
1061     {
1062         bool b_eof;
1063
1064         if( s->b_die ) return VLC_EGENERIC;
1065
1066
1067         /* Fetch a block */
1068         if( ( b = AReadBlock( s, &b_eof ) ) ) break;
1069
1070         if( b_eof ) return VLC_EGENERIC;
1071
1072         msleep( STREAM_DATA_WAIT );
1073     }
1074
1075     while( b )
1076     {
1077         i_stop = mdate();
1078
1079         /* Append the block */
1080         p_sys->block.i_size += b->i_buffer;
1081         *p_sys->block.pp_last = b;
1082         p_sys->block.pp_last = &b->p_next;
1083
1084         /* Fix p_current */
1085         if( p_sys->block.p_current == NULL )
1086             p_sys->block.p_current = b;
1087
1088         /* Update stat */
1089         p_sys->stat.i_bytes += b->i_buffer;
1090         p_sys->stat.i_read_time += i_stop - i_start;
1091         p_sys->stat.i_read_count++;
1092
1093         b = b->p_next;
1094         i_start = mdate();
1095     }
1096     return VLC_SUCCESS;
1097 }
1098
1099
1100 /****************************************************************************
1101  * Method 2:
1102  ****************************************************************************/
1103 static int AStreamRefillStream( stream_t *s );
1104
1105 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
1106 {
1107     stream_sys_t *p_sys = s->p_sys;
1108     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1109
1110     uint8_t *p_data = (uint8_t *)p_read;
1111     int      i_data = 0;
1112
1113     if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1114
1115     if( p_read == NULL )
1116     {
1117         /* seek within this stream if possible, else use plain old read and discard */
1118         stream_sys_t *p_sys = s->p_sys;
1119         access_t     *p_access = p_sys->p_access;
1120
1121         /* seeking after EOF is not what we want */
1122         if( !( p_access->info.b_eof ) )
1123         {
1124             bool   b_aseek;
1125             access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1126             if( b_aseek )
1127                 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1128         }
1129     }
1130
1131 #ifdef STREAM_DEBUG
1132     msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1133              " offset=%d end=%"PRId64,
1134              i_read, p_sys->i_pos, p_sys->stream.i_tk,
1135              tk->i_start, p_sys->stream.i_offset, tk->i_end );
1136 #endif
1137
1138     while( i_data < i_read )
1139     {
1140         int i_off = (tk->i_start + p_sys->stream.i_offset) %
1141                     STREAM_CACHE_TRACK_SIZE;
1142         int i_current =
1143             __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1144                    STREAM_CACHE_TRACK_SIZE - i_off );
1145         int i_copy = __MIN( i_current, i_read - i_data );
1146
1147         if( i_copy <= 0 ) break; /* EOF */
1148
1149         /* Copy data */
1150         /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1151         if( p_data )
1152         {
1153             memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1154             p_data += i_copy;
1155         }
1156         i_data += i_copy;
1157         p_sys->stream.i_offset += i_copy;
1158
1159         /* Update pos now */
1160         p_sys->i_pos += i_copy;
1161
1162         /* */
1163         p_sys->stream.i_used += i_copy;
1164         if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1165             p_sys->stream.i_used >= p_sys->stream.i_read_size )
1166         {
1167             if( AStreamRefillStream( s ) )
1168             {
1169                 /* EOF */
1170                 if( tk->i_start >= tk->i_end ) break;
1171             }
1172         }
1173     }
1174
1175     return i_data;
1176 }
1177
1178 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, int i_read )
1179 {
1180     stream_sys_t *p_sys = s->p_sys;
1181     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1182     int64_t i_off;
1183
1184     if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1185
1186 #ifdef STREAM_DEBUG
1187     msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1188              "start=%"PRId64" offset=%d end=%"PRId64,
1189              i_read, p_sys->i_pos, p_sys->stream.i_tk,
1190              tk->i_start, p_sys->stream.i_offset, tk->i_end );
1191 #endif
1192
1193     /* Avoid problem, but that should *never* happen */
1194     if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1195         i_read = STREAM_CACHE_TRACK_SIZE / 2;
1196
1197     while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1198     {
1199         if( p_sys->stream.i_used <= 1 )
1200         {
1201             /* Be sure we will read something */
1202             p_sys->stream.i_used += i_read -
1203                 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1204         }
1205         if( AStreamRefillStream( s ) ) break;
1206     }
1207
1208     if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1209         i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1210
1211     /* Now, direct pointer or a copy ? */
1212     i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1213     if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1214     {
1215         *pp_peek = &tk->p_buffer[i_off];
1216         return i_read;
1217     }
1218
1219     if( p_sys->i_peek < i_read )
1220     {
1221         p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1222         if( !p_sys->p_peek )
1223         {
1224             p_sys->i_peek = 0;
1225             return 0;
1226         }
1227         p_sys->i_peek = i_read;
1228     }
1229
1230     memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1231             STREAM_CACHE_TRACK_SIZE - i_off );
1232     memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1233             &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1234
1235     *pp_peek = p_sys->p_peek;
1236     return i_read;
1237 }
1238
1239 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1240 {
1241     stream_sys_t *p_sys = s->p_sys;
1242     access_t     *p_access = p_sys->p_access;
1243     bool   b_aseek;
1244     bool   b_afastseek;
1245     int i_maxth;
1246     int i_new;
1247     int i;
1248
1249 #ifdef STREAM_DEBUG
1250     msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1251              " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1252              i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1253              p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1254              p_sys->stream.i_offset,
1255              p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1256 #endif
1257
1258
1259     /* Seek in our current track ? */
1260     if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1261         i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1262     {
1263         stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1264 #ifdef STREAM_DEBUG
1265         msg_Dbg( s, "AStreamSeekStream: current track" );
1266 #endif
1267         p_sys->i_pos = i_pos;
1268         p_sys->stream.i_offset = i_pos - tk->i_start;
1269
1270         /* If there is not enough data left in the track, refill  */
1271         /* \todo How to get a correct value for
1272          *    - refilling threshold
1273          *    - how much to refill
1274          */
1275         if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1276                                              p_sys->stream.i_read_size )
1277         {
1278             if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2  )
1279             {
1280                 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1281                 AStreamRefillStream( s );
1282             }
1283         }
1284         return VLC_SUCCESS;
1285     }
1286
1287     access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1288     if( !b_aseek )
1289     {
1290         /* We can't do nothing */
1291         msg_Dbg( s, "AStreamSeekStream: can't seek" );
1292         return VLC_EGENERIC;
1293     }
1294
1295     /* Date the current track */
1296     p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1297
1298     /* Try to reuse already read data */
1299     for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1300     {
1301         stream_track_t *tk = &p_sys->stream.tk[i];
1302
1303         if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1304         {
1305 #ifdef STREAM_DEBUG
1306             msg_Dbg( s, "AStreamSeekStream: reusing %d start=%"PRId64
1307                      " end=%"PRId64, i, tk->i_start, tk->i_end );
1308 #endif
1309
1310             /* Seek at the end of the buffer */
1311             if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1312
1313             /* That's it */
1314             p_sys->i_pos = i_pos;
1315             p_sys->stream.i_tk = i;
1316             p_sys->stream.i_offset = i_pos - tk->i_start;
1317
1318             if( p_sys->stream.i_used < 1024 )
1319                 p_sys->stream.i_used = 1024;
1320
1321             if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1322                 return VLC_EGENERIC;
1323
1324             return VLC_SUCCESS;
1325         }
1326     }
1327
1328     access_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1329     /* FIXME compute seek cost (instead of static 'stupid' value) */
1330     i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1331     if( !b_afastseek )
1332         i_maxth *= 3;
1333
1334     /* FIXME TODO */
1335 #if 0
1336     /* Search closest segment TODO */
1337     for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1338     {
1339         stream_track_t *tk = &p_sys->stream.tk[i];
1340
1341         if( i_pos + i_maxth >= tk->i_start )
1342         {
1343             msg_Dbg( s, "good segment before current pos, TODO" );
1344         }
1345         if( i_pos - i_maxth <= tk->i_end )
1346         {
1347             msg_Dbg( s, "good segment after current pos, TODO" );
1348         }
1349     }
1350 #endif
1351
1352     /* Nothing good, seek and choose oldest segment */
1353     if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1354     p_sys->i_pos = i_pos;
1355
1356     i_new = 0;
1357     for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1358     {
1359         if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1360             i_new = i;
1361     }
1362
1363     /* Reset the segment */
1364     p_sys->stream.i_tk     = i_new;
1365     p_sys->stream.i_offset =  0;
1366     p_sys->stream.tk[i_new].i_start = i_pos;
1367     p_sys->stream.tk[i_new].i_end   = i_pos;
1368
1369     /* Read data */
1370     if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1371         p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1372
1373     if( AStreamRefillStream( s ) )
1374         return VLC_EGENERIC;
1375
1376     return VLC_SUCCESS;
1377 }
1378
1379 static int AStreamRefillStream( stream_t *s )
1380 {
1381     stream_sys_t *p_sys = s->p_sys;
1382     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1383
1384     /* We read but won't increase i_start after initial start + offset */
1385     int i_toread =
1386         __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1387                (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1388     bool b_read = false;
1389     int64_t i_start, i_stop;
1390
1391     if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1392
1393 #ifdef STREAM_DEBUG
1394     msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1395                  p_sys->stream.i_used, i_toread );
1396 #endif
1397
1398     i_start = mdate();
1399     while( i_toread > 0 )
1400     {
1401         int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1402         int i_read;
1403
1404         if( s->b_die )
1405             return VLC_EGENERIC;
1406
1407         i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1408         i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1409
1410         /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1411         if( i_read <  0 )
1412         {
1413             msleep( STREAM_DATA_WAIT );
1414             continue;
1415         }
1416         else if( i_read == 0 )
1417         {
1418             if( !b_read ) return VLC_EGENERIC;
1419             return VLC_SUCCESS;
1420         }
1421         b_read = true;
1422
1423         /* Update end */
1424         tk->i_end += i_read;
1425
1426         /* Windows of STREAM_CACHE_TRACK_SIZE */
1427         if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1428         {
1429             int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1430
1431             tk->i_start += i_invalid;
1432             p_sys->stream.i_offset -= i_invalid;
1433         }
1434
1435         i_toread -= i_read;
1436         p_sys->stream.i_used -= i_read;
1437
1438         p_sys->stat.i_bytes += i_read;
1439         p_sys->stat.i_read_count++;
1440     }
1441     i_stop = mdate();
1442
1443     p_sys->stat.i_read_time += i_stop - i_start;
1444
1445     return VLC_SUCCESS;
1446 }
1447
1448 static void AStreamPrebufferStream( stream_t *s )
1449 {
1450     stream_sys_t *p_sys = s->p_sys;
1451     access_t     *p_access = p_sys->p_access;
1452
1453     int64_t i_first = 0;
1454     int64_t i_start;
1455     int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1456         ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1457           STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1458
1459     msg_Dbg( s, "pre-buffering..." );
1460     i_start = mdate();
1461     for( ;; )
1462     {
1463         stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1464
1465         int64_t i_date = mdate();
1466         int i_read;
1467
1468         if( s->b_die || tk->i_end >= i_prebuffer ||
1469             (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1470         {
1471             int64_t i_byterate;
1472
1473             /* Update stat */
1474             p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1475             p_sys->stat.i_read_time = i_date - i_start;
1476             i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1477                          (p_sys->stat.i_read_time+1);
1478
1479             msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1480                      "%"PRId64" kbytes/s",
1481                      p_sys->stat.i_bytes,
1482                      p_sys->stat.i_read_time / INT64_C(1000000),
1483                      i_byterate / 1024 );
1484             break;
1485         }
1486
1487         /* */
1488         i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1489         i_read = __MIN( p_sys->stream.i_read_size, i_read );
1490         i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1491         if( i_read <  0 )
1492         {
1493             msleep( STREAM_DATA_WAIT );
1494             continue;
1495         }
1496         else if( i_read == 0 )
1497         {
1498             /* EOF */
1499             break;
1500         }
1501
1502         if( i_first == 0 )
1503         {
1504             i_first = mdate();
1505             msg_Dbg( s, "received first data for our buffer");
1506         }
1507
1508         tk->i_end += i_read;
1509
1510         p_sys->stat.i_read_count++;
1511     }
1512 }
1513
1514 /****************************************************************************
1515  * Method 3:
1516  ****************************************************************************/
1517
1518 static int AStreamReadImmediate( stream_t *s, void *p_read, int i_read )
1519 {
1520     stream_sys_t *p_sys = s->p_sys;
1521
1522 #ifdef STREAM_DEBUG
1523     msg_Dbg( s, "AStreamReadImmediate p_read=%p i_read=%d",
1524              p_read, i_read );
1525 #endif
1526
1527     /* First, check if we already have some data in the buffer,
1528      * that we could copy directly */
1529     int i_copy = __MIN( stream_buffered_size( s ), i_read );
1530     if( i_copy )
1531     {
1532 #ifdef STREAM_DEBUG
1533         msg_Dbg( s, "AStreamReadImmediate: copy %d from %p", i_copy, stream_buffer( s ) );
1534 #endif
1535
1536         assert( i_copy <= STREAM_CACHE_SIZE );
1537
1538         if( p_read )
1539         {
1540             memcpy( p_read, stream_buffer( s ), i_copy );
1541             p_read = (uint8_t *)p_read + i_copy;
1542         }
1543     }
1544
1545     /* Now that we've read our buffer we don't need its i_copy bytes */
1546     stream_buffer_empty( s, i_copy );
1547
1548     /* Now check if we have still to really read some data */
1549     int i_to_read = i_read - i_copy;
1550     if( i_to_read )
1551     {
1552         if( p_read )
1553             i_to_read = AReadStream( s, p_read, i_to_read );
1554         else
1555         {
1556             void * dummy = malloc(i_to_read);
1557             i_to_read = AReadStream( s, dummy, i_to_read );
1558             free(dummy);
1559         }
1560     }
1561
1562     p_sys->i_pos += i_to_read;
1563
1564     return i_to_read + i_copy;
1565 }
1566
1567 static int AStreamPeekImmediate( stream_t *s, const uint8_t **pp_peek, int i_read )
1568 {
1569 #ifdef STREAM_DEBUG
1570     msg_Dbg( s, "AStreamPeekImmediate: %d  size=%"PRId64,
1571              i_read, size_buffered_size( s ) );
1572 #endif
1573
1574     /* Avoid problem, but that shouldn't happen */
1575     if( i_read > STREAM_CACHE_SIZE / 2 )
1576         i_read = STREAM_CACHE_SIZE / 2;
1577
1578     int i_to_read = i_read - stream_buffered_size( s );
1579     if( i_to_read > 0 )
1580     {
1581 #ifdef STREAM_DEBUG
1582         msg_Dbg( s, "AStreamPeekImmediate: Reading %d",
1583              i_to_read );
1584 #endif
1585         i_to_read = AReadStream( s, stream_buffer( s ) + stream_buffered_size( s ),
1586                                  i_to_read );
1587
1588         if( i_to_read > 0 )
1589             stream_buffer_fill( s, i_to_read );
1590     }
1591
1592     *pp_peek = stream_buffer( s );
1593
1594     return __MIN(stream_buffered_size( s ), i_read);
1595 }
1596
1597 static int AStreamSeekImmediate( stream_t *s, int64_t i_pos )
1598 {
1599     stream_sys_t *p_sys = s->p_sys;
1600     access_t     *p_access = p_sys->p_access;
1601     bool   b_aseek;
1602
1603 #ifdef STREAM_DEBUG
1604     msg_Dbg( s, "AStreamSeekImmediate to %"PRId64" pos=%"PRId64
1605              i_pos, p_sys->i_pos );
1606 #endif
1607
1608     access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1609     if( !b_aseek )
1610     {
1611         /* We can't do nothing */
1612         msg_Dbg( s, "AStreamSeekImmediate: can't seek" );
1613         return VLC_EGENERIC;
1614     }
1615
1616     /* Just reset our buffer */
1617     stream_buffer_empty( s, stream_buffered_size( s ) );
1618
1619     if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1620
1621     return VLC_SUCCESS;
1622 }
1623
1624 /****************************************************************************
1625  * stream_ReadLine:
1626  ****************************************************************************/
1627 /**
1628  * Read from the stream untill first newline.
1629  * \param s Stream handle to read from
1630  * \return A pointer to the allocated output string. You need to free this when you are done.
1631  */
1632 #define STREAM_PROBE_LINE 2048
1633 #define STREAM_LINE_MAX (2048*100)
1634 char * stream_ReadLine( stream_t *s )
1635 {
1636     char *p_line = NULL;
1637     int i_line = 0, i_read = 0;
1638
1639     while( i_read < STREAM_LINE_MAX )
1640     {
1641         char *psz_eol;
1642         const uint8_t *p_data;
1643         int i_data;
1644         int64_t i_pos;
1645
1646         /* Probe new data */
1647         i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1648         if( i_data <= 0 ) break; /* No more data */
1649
1650         /* BOM detection */
1651         i_pos = stream_Tell( s );
1652         if( i_pos == 0 && i_data > 4 )
1653         {
1654             int i_bom_size = 0;
1655             char *psz_encoding = NULL;
1656
1657             if( p_data[0] == 0xEF && p_data[1] == 0xBB && p_data[2] == 0xBF )
1658             {
1659                 psz_encoding = strdup( "UTF-8" );
1660                 i_bom_size = 3;
1661             }
1662             else if( p_data[0] == 0x00 && p_data[1] == 0x00 )
1663             {
1664                 if( p_data[2] == 0xFE && p_data[3] == 0xFF )
1665                 {
1666                     psz_encoding = strdup( "UTF-32BE" );
1667                     s->i_char_width = 4;
1668                     i_bom_size = 4;
1669                 }
1670             }
1671             else if( p_data[0] == 0xFF && p_data[1] == 0xFE )
1672             {
1673                 if( p_data[2] == 0x00 && p_data[3] == 0x00 )
1674                 {
1675                     psz_encoding = strdup( "UTF-32LE" );
1676                     s->i_char_width = 4;
1677                     s->b_little_endian = true;
1678                     i_bom_size = 4;
1679                 }
1680                 else
1681                 {
1682                     psz_encoding = strdup( "UTF-16LE" );
1683                     s->b_little_endian = true;
1684                     s->i_char_width = 2;
1685                     i_bom_size = 2;
1686                 }
1687             }
1688             else if( p_data[0] == 0xFE && p_data[1] == 0xFF )
1689             {
1690                 psz_encoding = strdup( "UTF-16BE" );
1691                 s->i_char_width = 2;
1692                 i_bom_size = 2;
1693             }
1694
1695             /* Seek past the BOM */
1696             if( i_bom_size )
1697             {
1698                 stream_Seek( s, i_bom_size );
1699                 p_data += i_bom_size;
1700                 i_data -= i_bom_size;
1701             }
1702
1703             /* Open the converter if we need it */
1704             if( psz_encoding != NULL )
1705             {
1706                 input_thread_t *p_input;
1707                 msg_Dbg( s, "%s BOM detected", psz_encoding );
1708                 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1709                 if( s->i_char_width > 1 )
1710                 {
1711                     s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1712                     if( s->conv == (vlc_iconv_t)-1 )
1713                     {
1714                         msg_Err( s, "iconv_open failed" );
1715                     }
1716                 }
1717                 if( p_input != NULL)
1718                 {
1719                     var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1720                     var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1721                     vlc_object_release( p_input );
1722                 }
1723                 free( psz_encoding );
1724             }
1725         }
1726
1727         if( i_data % s->i_char_width )
1728         {
1729             /* keep i_char_width boundary */
1730             i_data = i_data - ( i_data % s->i_char_width );
1731             msg_Warn( s, "the read is not i_char_width compatible");
1732         }
1733
1734         if( i_data == 0 )
1735             break;
1736
1737         /* Check if there is an EOL */
1738         if( s->i_char_width == 1 )
1739         {
1740             /* UTF-8: 0A <LF> */
1741             psz_eol = memchr( p_data, '\n', i_data );
1742         }
1743         else
1744         {
1745             const uint8_t *p = p_data;
1746             const uint8_t *p_last = p + i_data - s->i_char_width;
1747
1748             if( s->i_char_width == 2 )
1749             {
1750                 if( s->b_little_endian == true)
1751                 {
1752                     /* UTF-16LE: 0A 00 <LF> */
1753                     while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1754                         p += 2;
1755                 }
1756                 else
1757                 {
1758                     /* UTF-16BE: 00 0A <LF> */
1759                     while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1760                         p += 2;
1761                 }
1762             }
1763             else if( s->i_char_width == 4 )
1764             {
1765                 if( s->b_little_endian == true)
1766                 {
1767                     /* UTF-32LE: 0A 00 00 00 <LF> */
1768                     while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ||
1769                            p[2] != 0x00 || p[3] != 0x00 ) )
1770                         p += 4;
1771                 }
1772                 else
1773                 {
1774                     /* UTF-32BE: 00 00 00 0A <LF> */
1775                     while( p <= p_last && ( p[3] != 0x0A || p[2] != 0x00 ||
1776                            p[1] != 0x00 || p[0] != 0x00 ) )
1777                         p += 4;
1778                 }
1779             }
1780
1781             if( p > p_last )
1782             {
1783                 psz_eol = NULL;
1784             }
1785             else
1786             {
1787                 psz_eol = (char *)p + ( s->i_char_width - 1 );
1788             }
1789         }
1790
1791         if(psz_eol)
1792         {
1793             i_data = (psz_eol - (char *)p_data) + 1;
1794             p_line = realloc( p_line, i_line + i_data + s->i_char_width ); /* add \0 */
1795             if( !p_line )
1796             {
1797                 msg_Err( s, "Out of memory when reallocating p_line" );
1798                 goto error;
1799             }
1800             i_data = stream_Read( s, &p_line[i_line], i_data );
1801             if( i_data <= 0 ) break; /* Hmmm */
1802             i_line += i_data - s->i_char_width; /* skip \n */;
1803             i_read += i_data;
1804
1805             /* We have our line */
1806             break;
1807         }
1808
1809         /* Read data (+1 for easy \0 append) */
1810         p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->i_char_width );
1811         if( !p_line )
1812         {
1813             msg_Err( s, "Out of memory when reallocating p_line" );
1814             goto error;
1815         }
1816         i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1817         if( i_data <= 0 ) break; /* Hmmm */
1818         i_line += i_data;
1819         i_read += i_data;
1820     }
1821
1822     if( i_read > 0 )
1823     {
1824         int j;
1825         for( j = 0; j < s->i_char_width; j++ )
1826         {
1827             p_line[i_line + j] = '\0';
1828         }
1829         i_line += s->i_char_width; /* the added \0 */
1830         if( s->i_char_width > 1 )
1831         {
1832             size_t i_in = 0, i_out = 0;
1833             const char * p_in = NULL;
1834             char * p_out = NULL;
1835             char * psz_new_line = NULL;
1836
1837             /* iconv */
1838             psz_new_line = malloc( i_line );
1839             if( psz_new_line == NULL )
1840             {
1841                 msg_Err( s, "Out of memory when allocating psz_new_line" );
1842                 goto error;
1843             }
1844             i_in = i_out = (size_t)i_line;
1845             p_in = p_line;
1846             p_out = psz_new_line;
1847
1848             if( vlc_iconv( s->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1849             {
1850                 msg_Err( s, "iconv failed" );
1851                 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1852             }
1853             free( p_line );
1854             p_line = psz_new_line;
1855             i_line = (size_t)i_line - i_out; /* does not include \0 */
1856         }
1857
1858         /* Remove trailing LF/CR */
1859         while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1860             p_line[i_line-2] == '\n') ) i_line--;
1861
1862         /* Make sure the \0 is there */
1863         p_line[i_line-1] = '\0';
1864
1865         return p_line;
1866     }
1867
1868 error:
1869
1870     /* We failed to read any data, probably EOF */
1871     free( p_line );
1872     if( s->conv != (vlc_iconv_t)(-1) ) vlc_iconv_close( s->conv );
1873     return NULL;
1874 }
1875
1876 /****************************************************************************
1877  * Access reading/seeking wrappers to handle concatenated streams.
1878  ****************************************************************************/
1879 static int AReadStream( stream_t *s, void *p_read, int i_read )
1880 {
1881     stream_sys_t *p_sys = s->p_sys;
1882     access_t *p_access = p_sys->p_access;
1883     input_thread_t *p_input = NULL;
1884     int i_read_orig = i_read;
1885     int i_total = 0;
1886
1887     if( s->p_parent && s->p_parent->p_parent &&
1888         s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1889         p_input = (input_thread_t *)s->p_parent->p_parent;
1890
1891     if( !p_sys->i_list )
1892     {
1893         i_read = p_access->pf_read( p_access, p_read, i_read );
1894         if( p_input )
1895         {
1896             vlc_mutex_lock( &p_input->p->counters.counters_lock );
1897             stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
1898                              &i_total );
1899             stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1900                            (float)i_total, NULL );
1901             stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1902             vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1903         }
1904         return i_read;
1905     }
1906
1907     i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1908                                             i_read );
1909
1910     /* If we reached an EOF then switch to the next stream in the list */
1911     if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1912     {
1913         char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1914         access_t *p_list_access;
1915
1916         msg_Dbg( s, "opening input `%s'", psz_name );
1917
1918         p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1919
1920         if( !p_list_access ) return 0;
1921
1922         if( p_sys->p_list_access != p_access )
1923             access_Delete( p_sys->p_list_access );
1924
1925         p_sys->p_list_access = p_list_access;
1926
1927         /* We have to read some data */
1928         return AReadStream( s, p_read, i_read_orig );
1929     }
1930
1931     /* Update read bytes in input */
1932     if( p_input )
1933     {
1934         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1935         stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
1936         stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1937                        (float)i_total, NULL );
1938         stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1939         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1940     }
1941     return i_read;
1942 }
1943
1944 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
1945 {
1946     stream_sys_t *p_sys = s->p_sys;
1947     access_t *p_access = p_sys->p_access;
1948     input_thread_t *p_input = NULL;
1949     block_t *p_block;
1950     bool b_eof;
1951     int i_total = 0;
1952
1953     if( s->p_parent && s->p_parent->p_parent &&
1954         s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1955         p_input = (input_thread_t *)s->p_parent->p_parent;
1956
1957     if( !p_sys->i_list )
1958     {
1959         p_block = p_access->pf_block( p_access );
1960         if( pb_eof ) *pb_eof = p_access->info.b_eof;
1961         if( p_input && p_block && libvlc_stats (p_access) )
1962         {
1963             vlc_mutex_lock( &p_input->p->counters.counters_lock );
1964             stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1965                                  p_block->i_buffer, &i_total );
1966             stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1967                               (float)i_total, NULL );
1968             stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1969             vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1970         }
1971         return p_block;
1972     }
1973
1974     p_block = p_sys->p_list_access->pf_block( p_access );
1975     b_eof = p_sys->p_list_access->info.b_eof;
1976     if( pb_eof ) *pb_eof = b_eof;
1977
1978     /* If we reached an EOF then switch to the next stream in the list */
1979     if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1980     {
1981         char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1982         access_t *p_list_access;
1983
1984         msg_Dbg( s, "opening input `%s'", psz_name );
1985
1986         p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1987
1988         if( !p_list_access ) return 0;
1989
1990         if( p_sys->p_list_access != p_access )
1991             access_Delete( p_sys->p_list_access );
1992
1993         p_sys->p_list_access = p_list_access;
1994
1995         /* We have to read some data */
1996         return AReadBlock( s, pb_eof );
1997     }
1998     if( p_block )
1999     {
2000         if( p_input )
2001         {
2002             vlc_mutex_lock( &p_input->p->counters.counters_lock );
2003             stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
2004                                  p_block->i_buffer, &i_total );
2005             stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2006                               (float)i_total, NULL );
2007             stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
2008                                  1 , NULL);
2009             vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2010         }
2011     }
2012     return p_block;
2013 }
2014
2015 static int ASeek( stream_t *s, int64_t i_pos )
2016 {
2017     stream_sys_t *p_sys = s->p_sys;
2018     access_t *p_access = p_sys->p_access;
2019
2020     /* Check which stream we need to access */
2021     if( p_sys->i_list )
2022     {
2023         int i;
2024         char *psz_name;
2025         int64_t i_size = 0;
2026         access_t *p_list_access = 0;
2027
2028         for( i = 0; i < p_sys->i_list - 1; i++ )
2029         {
2030             if( i_pos < p_sys->list[i]->i_size + i_size ) break;
2031             i_size += p_sys->list[i]->i_size;
2032         }
2033         psz_name = p_sys->list[i]->psz_path;
2034
2035         if( i != p_sys->i_list_index )
2036             msg_Dbg( s, "opening input `%s'", psz_name );
2037
2038         if( i != p_sys->i_list_index && i != 0 )
2039         {
2040             p_list_access =
2041                 access_New( s, p_access->psz_access, "", psz_name );
2042         }
2043         else if( i != p_sys->i_list_index )
2044         {
2045             p_list_access = p_access;
2046         }
2047
2048         if( p_list_access )
2049         {
2050             if( p_sys->p_list_access != p_access )
2051                 access_Delete( p_sys->p_list_access );
2052
2053             p_sys->p_list_access = p_list_access;
2054         }
2055
2056         p_sys->i_list_index = i;
2057         return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
2058                                               i_pos - i_size );
2059     }
2060
2061     return p_access->pf_seek( p_access, i_pos );
2062 }
2063
2064
2065 /**
2066  * Try to read "i_read" bytes into a buffer pointed by "p_read".  If
2067  * "p_read" is NULL then data are skipped instead of read.  The return
2068  * value is the real numbers of bytes read/skip. If this value is less
2069  * than i_read that means that it's the end of the stream.
2070  */
2071 int stream_Read( stream_t *s, void *p_read, int i_read )
2072 {
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 }