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