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