]> git.sesse.net Git - vlc/blob - src/input/stream.c
17b967f2897c14c4a0699bf91e28735340b57b47
[vlc] / src / input / stream.c
1 /*****************************************************************************
2  * stream.c
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include <stdlib.h>
25 #include <vlc/vlc.h>
26 #include <vlc/input.h>
27
28 #include "input_internal.h"
29
30 #ifdef UNDER_CE
31 #   define LOW_MEM
32 #endif
33
34 /* TODO:
35  *  - tune the 2 methods
36  *  - compute cost for seek
37  *  - improve stream mode seeking with closest segments
38  *  - ...
39  */
40
41 /* Two methods:
42  *  - using pf_block
43  *      One linked list of data read
44  *  - using pf_read
45  *      More complex scheme using mutliple track to avoid seeking
46  */
47
48 /* How many track we have, currently only used for stream mode */
49 #ifdef LOW_MEM
50 #   define STREAM_CACHE_TRACK 1
51     /* Max size of our cache 128Ko per track */
52 #   define STREAM_CACHE_SIZE  (STREAM_CACHE_TRACK*1024*128)
53 #else
54 #   define STREAM_CACHE_TRACK 3
55     /* Max size of our cache 4Mo per track */
56 #   define STREAM_CACHE_SIZE  (4*STREAM_CACHE_TRACK*1024*1024)
57 #endif
58
59 /* How many data we try to prebuffer */
60 #define STREAM_CACHE_PREBUFFER_SIZE (32767)
61 /* Maximum time we take to pre-buffer */
62 #define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
63
64 /* Method1: Simple, for pf_block.
65  *  We get blocks and put them in the linked list.
66  *  We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
67  */
68 #define STREAM_DATA_WAIT 40000       /* Time between before a pf_block retry */
69
70 /* Method2: A bit more complex, for pf_read
71  *  - We use ring buffers, only one if unseekable, all if seekable
72  *  - Upon seek date current ring, then search if one ring match the pos,
73  *      yes: switch to it, seek the access to match the end of the ring
74  *      no: search the ring with i_end the closer to i_pos,
75  *          if close enough, read data and use this ring
76  *          else use the oldest ring, seek and use it.
77  *
78  *  TODO: - with access non seekable: use all space available for only one ring, but
79  *          we have to support seekable/non-seekable switch on the fly.
80  *        - compute a good value for i_read_size
81  *        - ?
82  */
83 #define STREAM_READ_ATONCE 32767
84 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
85
86 typedef struct
87 {
88     int64_t i_date;
89
90     int64_t i_start;
91     int64_t i_end;
92
93     uint8_t *p_buffer;
94
95 } stream_track_t;
96
97 typedef struct
98 {
99     char     *psz_path;
100     int64_t  i_size;
101
102 } access_entry_t;
103
104 struct stream_sys_t
105 {
106     access_t    *p_access;
107
108     vlc_bool_t  b_block;    /* Block method (1) or stream */
109
110     int64_t     i_pos;      /* Current reading offset */
111
112     /* Method 1: pf_block */
113     struct
114     {
115         int64_t i_start;        /* Offset of block for p_first */
116         int     i_offset;       /* Offset for data in p_current */
117         block_t *p_current;     /* Current block */
118
119         int     i_size;         /* Total amount of data in the list */
120         block_t *p_first;
121         block_t **pp_last;
122
123     } block;
124
125     /* Method 2: for pf_read */
126     struct
127     {
128         int i_offset;   /* Buffer offset in the current track */
129         int i_tk;       /* Current track */
130         stream_track_t tk[STREAM_CACHE_TRACK];
131
132         /* Global buffer */
133         uint8_t *p_buffer;
134
135         /* */
136         int i_used; /* Used since last read */
137         int i_read_size;
138
139     } stream;
140
141     /* Peek temporary buffer */
142     int     i_peek;
143     uint8_t *p_peek;
144
145     /* Stat for both method */
146     struct
147     {
148         vlc_bool_t b_fastseek;  /* From access */
149
150         /* Stat about reading data */
151         int64_t i_read_count;
152         int64_t i_bytes;
153         int64_t i_read_time;
154
155         /* Stat about seek */
156         int     i_seek_count;
157         int64_t i_seek_time;
158
159     } stat;
160
161     /* Streams list */
162     int            i_list;
163     access_entry_t **list;
164     int            i_list_index;
165     access_t       *p_list_access;
166
167     /* Preparse mode ? */
168     vlc_bool_t b_quick;
169 };
170
171 /* Method 1: */
172 static int  AStreamReadBlock( stream_t *s, void *p_read, int i_read );
173 static int  AStreamPeekBlock( stream_t *s, uint8_t **p_peek, int i_read );
174 static int  AStreamSeekBlock( stream_t *s, int64_t i_pos );
175 static void AStreamPrebufferBlock( stream_t *s );
176 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof );
177
178 /* Method 2 */
179 static int  AStreamReadStream( stream_t *s, void *p_read, int i_read );
180 static int  AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read );
181 static int  AStreamSeekStream( stream_t *s, int64_t i_pos );
182 static void AStreamPrebufferStream( stream_t *s );
183 static int  AReadStream( stream_t *s, void *p_read, int i_read );
184
185 /* Common */
186 static int AStreamControl( stream_t *s, int i_query, va_list );
187 static int  ASeek( stream_t *s, int64_t i_pos );
188
189
190 /****************************************************************************
191  * stream_AccessNew: create a stream from a access
192  ****************************************************************************/
193 stream_t *stream_AccessNew( access_t *p_access, vlc_bool_t b_quick )
194 {
195     stream_t *s = vlc_object_create( p_access, VLC_OBJECT_STREAM );
196     stream_sys_t *p_sys;
197     char *psz_list;
198
199     if( !s ) return NULL;
200
201     /* Attach it now, needed for b_die */
202     vlc_object_attach( s, p_access );
203
204     s->pf_block  = NULL;
205     s->pf_read   = NULL;    /* Set up later */
206     s->pf_peek   = NULL;
207     s->pf_control= AStreamControl;
208
209     s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
210
211     /* Common field */
212     p_sys->p_access = p_access;
213     p_sys->b_block = p_access->pf_block ? VLC_TRUE : VLC_FALSE;
214     p_sys->i_pos = p_access->info.i_pos;
215
216     /* Stats */
217     access2_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
218     p_sys->stat.i_bytes = 0;
219     p_sys->stat.i_read_time = 0;
220     p_sys->stat.i_read_count = 0;
221     p_sys->stat.i_seek_count = 0;
222     p_sys->stat.i_seek_time = 0;
223
224     p_sys->i_list = 0;
225     p_sys->list = 0;
226     p_sys->i_list_index = 0;
227     p_sys->p_list_access = 0;
228
229     p_sys->b_quick = b_quick;
230
231     /* Get the additional list of inputs if any (for concatenation) */
232     if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
233     {
234         access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
235         char *psz_name, *psz_parser = psz_name = psz_list;
236
237         p_sys->p_list_access = p_access;
238         p_entry->i_size = p_access->info.i_size;
239         p_entry->psz_path = strdup( p_access->psz_path );
240         TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
241         msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
242                  p_entry->psz_path, p_access->info.i_size );
243
244         while( psz_name && *psz_name )
245         {
246             psz_parser = strchr( psz_name, ',' );
247             if( psz_parser ) *psz_parser = 0;
248
249             psz_name = strdup( psz_name );
250             if( psz_name )
251             {
252                 access_t *p_tmp = access2_New( p_access, p_access->psz_access,
253                                                0, psz_name, 0 );
254
255                 if( !p_tmp )
256                 {
257                     psz_name = psz_parser;
258                     if( psz_name ) psz_name++;
259                     continue;
260                 }
261
262                 msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
263                          psz_name, p_tmp->info.i_size );
264
265                 p_entry = malloc( sizeof(access_entry_t) );
266                 p_entry->i_size = p_tmp->info.i_size;
267                 p_entry->psz_path = psz_name;
268                 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
269
270                 access2_Delete( p_tmp );
271             }
272
273             psz_name = psz_parser;
274             if( psz_name ) psz_name++;
275         }
276     }
277     if( psz_list ) free( psz_list );
278
279     /* Peek */
280     p_sys->i_peek = 0;
281     p_sys->p_peek = NULL;
282
283     if( p_sys->b_block )
284     {
285         s->pf_read = AStreamReadBlock;
286         s->pf_peek = AStreamPeekBlock;
287
288         /* Init all fields of p_sys->block */
289         p_sys->block.i_start = p_sys->i_pos;
290         p_sys->block.i_offset = 0;
291         p_sys->block.p_current = NULL;
292         p_sys->block.i_size = 0;
293         p_sys->block.p_first = NULL;
294         p_sys->block.pp_last = &p_sys->block.p_first;
295
296         /* Do the prebuffering */
297         AStreamPrebufferBlock( s );
298
299         if( p_sys->block.i_size <= 0 )
300         {
301             msg_Err( s, "cannot pre fill buffer" );
302             goto error;
303         }
304     }
305     else
306     {
307         int i;
308
309         s->pf_read = AStreamReadStream;
310         s->pf_peek = AStreamPeekStream;
311
312         /* Allocate/Setup our tracks */
313         p_sys->stream.i_offset = 0;
314         p_sys->stream.i_tk     = 0;
315         p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
316         p_sys->stream.i_used   = 0;
317         access2_Control( p_access, ACCESS_GET_MTU,
318                          &p_sys->stream.i_read_size );
319         if( p_sys->stream.i_read_size <= 0 )
320             p_sys->stream.i_read_size = STREAM_READ_ATONCE;
321         else if( p_sys->stream.i_read_size <= 256 )
322             p_sys->stream.i_read_size = 256;
323
324         for( i = 0; i < STREAM_CACHE_TRACK; i++ )
325         {
326             p_sys->stream.tk[i].i_date  = 0;
327             p_sys->stream.tk[i].i_start = p_sys->i_pos;
328             p_sys->stream.tk[i].i_end   = p_sys->i_pos;
329             p_sys->stream.tk[i].p_buffer=
330                 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
331         }
332
333         /* Do the prebuffering */
334         AStreamPrebufferStream( s );
335
336         if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
337         {
338             msg_Err( s, "cannot pre fill buffer" );
339             goto error;
340         }
341     }
342
343     return s;
344
345 error:
346     if( p_sys->b_block )
347     {
348         /* Nothing yet */
349     }
350     else
351     {
352         free( p_sys->stream.p_buffer );
353     }
354     free( s->p_sys );
355     vlc_object_detach( s );
356     vlc_object_destroy( s );
357     return NULL;
358 }
359
360 /****************************************************************************
361  * stream_AccessDelete:
362  ****************************************************************************/
363 void stream_AccessDelete( stream_t *s )
364 {
365     stream_sys_t *p_sys = s->p_sys;
366
367     vlc_object_detach( s );
368
369     if( p_sys->b_block ) block_ChainRelease( p_sys->block.p_first );
370     else free( p_sys->stream.p_buffer );
371
372     if( p_sys->p_peek ) free( p_sys->p_peek );
373
374     if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
375         access2_Delete( p_sys->p_list_access );
376
377     while( p_sys->i_list-- )
378     {
379         free( p_sys->list[p_sys->i_list]->psz_path );
380         free( p_sys->list[p_sys->i_list] );
381         if( !p_sys->i_list ) free( p_sys->list );
382     }
383
384     free( s->p_sys );
385     vlc_object_destroy( s );
386 }
387
388 /****************************************************************************
389  * stream_AccessReset:
390  ****************************************************************************/
391 void stream_AccessReset( stream_t *s )
392 {
393     stream_sys_t *p_sys = s->p_sys;
394
395     p_sys->i_pos = p_sys->p_access->info.i_pos;
396
397     if( p_sys->b_block )
398     {
399         block_ChainRelease( p_sys->block.p_first );
400
401         /* Init all fields of p_sys->block */
402         p_sys->block.i_start = p_sys->i_pos;
403         p_sys->block.i_offset = 0;
404         p_sys->block.p_current = NULL;
405         p_sys->block.i_size = 0;
406         p_sys->block.p_first = NULL;
407         p_sys->block.pp_last = &p_sys->block.p_first;
408
409         /* Do the prebuffering */
410         AStreamPrebufferBlock( s );
411     }
412     else
413     {
414         int i;
415
416         /* Setup our tracks */
417         p_sys->stream.i_offset = 0;
418         p_sys->stream.i_tk     = 0;
419         p_sys->stream.i_used   = 0;
420
421         for( i = 0; i < STREAM_CACHE_TRACK; i++ )
422         {
423             p_sys->stream.tk[i].i_date  = 0;
424             p_sys->stream.tk[i].i_start = p_sys->i_pos;
425             p_sys->stream.tk[i].i_end   = p_sys->i_pos;
426         }
427
428         /* Do the prebuffering */
429         AStreamPrebufferStream( s );
430     }
431 }
432
433 /****************************************************************************
434  * stream_AccessUpdate:
435  ****************************************************************************/
436 void stream_AccessUpdate( stream_t *s )
437 {
438     stream_sys_t *p_sys = s->p_sys;
439
440     p_sys->i_pos = p_sys->p_access->info.i_pos;
441
442     if( p_sys->i_list )
443     {
444         int i;
445         for( i = 0; i < p_sys->i_list_index; i++ )
446         {
447             p_sys->i_pos += p_sys->list[i]->i_size;
448         }
449     }
450 }
451
452 /****************************************************************************
453  * AStreamControl:
454  ****************************************************************************/
455 static int AStreamControl( stream_t *s, int i_query, va_list args )
456 {
457     stream_sys_t *p_sys = s->p_sys;
458     access_t     *p_access = p_sys->p_access;
459
460     vlc_bool_t *p_bool;
461     int64_t    *pi_64, i_64;
462     int        i_int;
463
464     switch( i_query )
465     {
466         case STREAM_GET_SIZE:
467             pi_64 = (int64_t*)va_arg( args, int64_t * );
468             if( s->p_sys->i_list )
469             {
470                 int i;
471                 *pi_64 = 0;
472                 for( i = 0; i < s->p_sys->i_list; i++ )
473                     *pi_64 += s->p_sys->list[i]->i_size;
474                 break;
475             }
476             *pi_64 = p_access->info.i_size;
477             break;
478
479         case STREAM_CAN_SEEK:
480             p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
481             access2_Control( p_access, ACCESS_CAN_SEEK, p_bool );
482             break;
483
484         case STREAM_CAN_FASTSEEK:
485             p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
486             access2_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
487             break;
488
489         case STREAM_GET_POSITION:
490             pi_64 = (int64_t*)va_arg( args, int64_t * );
491             *pi_64 = p_sys->i_pos;
492             break;
493
494         case STREAM_SET_POSITION:
495             i_64 = (int64_t)va_arg( args, int64_t );
496             if( p_sys->b_block )
497                 return AStreamSeekBlock( s, i_64 );
498             else
499                 return AStreamSeekStream( s, i_64 );
500
501         case STREAM_GET_MTU:
502             return VLC_EGENERIC;
503
504         case STREAM_CONTROL_ACCESS:
505             i_int = (int) va_arg( args, int );
506             if( i_int != ACCESS_SET_PRIVATE_ID_STATE
507                  && i_int != ACCESS_SET_PRIVATE_ID_CA )
508             {
509                 msg_Err( s, "Hey, what are you thinking ?"
510                             "DON'T USE STREAM_CONTROL_ACCESS !!!" );
511                 return VLC_EGENERIC;
512             }
513             return access2_vaControl( p_access, i_int, args );
514
515         default:
516             msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
517             return VLC_EGENERIC;
518     }
519     return VLC_SUCCESS;
520 }
521
522
523
524 /****************************************************************************
525  * Method 1:
526  ****************************************************************************/
527 static void AStreamPrebufferBlock( stream_t *s )
528 {
529     stream_sys_t *p_sys = s->p_sys;
530
531     int64_t i_first = 0;
532     int64_t i_start;
533
534     msg_Dbg( s, "pre buffering" );
535     i_start = mdate();
536     for( ;; )
537     {
538         int64_t i_date = mdate();
539         vlc_bool_t b_eof;
540         block_t *b;
541
542         if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
543             ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
544         {
545             int64_t i_byterate;
546
547             /* Update stat */
548             p_sys->stat.i_bytes = p_sys->block.i_size;
549             p_sys->stat.i_read_time = i_date - i_start;
550             i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
551                          (p_sys->stat.i_read_time + 1);
552
553             msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
554                      I64Fd" kbytes/s",
555                      p_sys->stat.i_bytes,
556                      p_sys->stat.i_read_time / I64C(1000000),
557                      i_byterate / 1024 );
558             break;
559         }
560
561         /* Fetch a block */
562         if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
563         {
564             if( b_eof ) break;
565
566             msleep( STREAM_DATA_WAIT );
567             continue;
568         }
569
570         if( i_first == 0 )
571         {
572             i_first = mdate();
573             msg_Dbg( s, "received first data for our buffer");
574         }
575
576         /* Append the block */
577         p_sys->block.i_size += b->i_buffer;
578         *p_sys->block.pp_last = b;
579         p_sys->block.pp_last = &b->p_next;
580
581         p_sys->stat.i_read_count++;
582     }
583
584     p_sys->block.p_current = p_sys->block.p_first;
585 }
586
587 static int AStreamRefillBlock( stream_t *s );
588
589 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
590 {
591     stream_sys_t *p_sys = s->p_sys;
592
593     uint8_t *p_data= (uint8_t*)p_read;
594     int     i_data = 0;
595
596     /* It means EOF */
597     if( p_sys->block.p_current == NULL )
598         return 0;
599
600     if( p_read == NULL )
601     {
602         /* seek within this stream if possible, else use plain old read and discard */
603         stream_sys_t *p_sys = s->p_sys;
604         access_t     *p_access = p_sys->p_access;
605         vlc_bool_t   b_aseek;
606         access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
607         if( b_aseek )
608             return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
609     }
610
611     while( i_data < i_read )
612     {
613         int i_current =
614             p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
615         int i_copy = __MIN( i_current, i_read - i_data);
616
617         /* Copy data */
618         if( p_data )
619         {
620             memcpy( p_data,
621                     &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
622                     i_copy );
623             p_data += i_copy;
624         }
625         i_data += i_copy;
626
627         p_sys->block.i_offset += i_copy;
628         if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
629         {
630             /* Current block is now empty, switch to next */
631             if( p_sys->block.p_current )
632             {
633                 p_sys->block.i_offset = 0;
634                 p_sys->block.p_current = p_sys->block.p_current->p_next;
635             }
636             /*Get a new block */
637             if( AStreamRefillBlock( s ) )
638             {
639                 break;
640             }
641         }
642     }
643
644     p_sys->i_pos += i_data;
645     return i_data;
646 }
647
648 static int AStreamPeekBlock( stream_t *s, uint8_t **pp_peek, int i_read )
649 {
650     stream_sys_t *p_sys = s->p_sys;
651     uint8_t *p_data;
652     int      i_data = 0;
653     block_t *b;
654     int      i_offset;
655
656     if( p_sys->block.p_current == NULL ) return 0; /* EOF */
657
658     /* We can directly give a pointer over our buffer */
659     if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
660     {
661         *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
662         return i_read;
663     }
664
665     /* We need to create a local copy */
666     if( p_sys->i_peek < i_read )
667     {
668         if( p_sys->p_peek )
669             free( p_sys->p_peek );
670         p_sys->i_peek = i_read;
671         p_sys->p_peek = malloc( p_sys->i_peek );
672     }
673
674     /* Fill enough data */
675     while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
676            < i_read )
677     {
678         block_t **pp_last = p_sys->block.pp_last;
679
680         if( AStreamRefillBlock( s ) )
681             break;
682
683         /* Our buffer are probably filled enough, don't try anymore */
684         if( pp_last == p_sys->block.pp_last )
685             break;
686     }
687
688     /* Copy what we have */
689     b = p_sys->block.p_current;
690     i_offset = p_sys->block.i_offset;
691     p_data = p_sys->p_peek;
692
693     while( b && i_data < i_read )
694     {
695         int i_current = b->i_buffer - i_offset;
696         int i_copy = __MIN( i_current, i_read - i_data );
697
698         memcpy( p_data, &b->p_buffer[i_offset], i_copy );
699         i_data += i_copy;
700         p_data += i_copy;
701         i_offset += i_copy;
702
703         if( i_offset >= b->i_buffer )
704         {
705             i_offset = 0;
706             b = b->p_next;
707         }
708     }
709
710     *pp_peek = p_sys->p_peek;
711     return i_data;
712 }
713
714 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
715 {
716     stream_sys_t *p_sys = s->p_sys;
717     access_t   *p_access = p_sys->p_access;
718     int64_t    i_offset = i_pos - p_sys->block.i_start;
719     vlc_bool_t b_seek;
720
721     /* We already have thoses data, just update p_current/i_offset */
722     if( i_offset >= 0 && i_offset < p_sys->block.i_size )
723     {
724         block_t *b = p_sys->block.p_first;
725         int i_current = 0;
726
727         while( i_current + b->i_buffer < i_offset )
728         {
729             i_current += b->i_buffer;
730             b = b->p_next;
731         }
732
733         p_sys->block.p_current = b;
734         p_sys->block.i_offset = i_offset - i_current;
735
736         p_sys->i_pos = i_pos;
737
738         return VLC_SUCCESS;
739     }
740
741     /* We may need to seek or to read data */
742     if( i_offset < 0 )
743     {
744         vlc_bool_t b_aseek;
745         access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
746
747         if( !b_aseek )
748         {
749             msg_Err( s, "backward seek impossible (access non seekable)" );
750             return VLC_EGENERIC;
751         }
752
753         b_seek = VLC_TRUE;
754     }
755     else
756     {
757         vlc_bool_t b_aseek, b_aseekfast;
758
759         access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
760         access2_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
761
762         if( !b_aseek )
763         {
764             b_seek = VLC_FALSE;
765             msg_Warn( s, I64Fd" bytes need to be skipped "
766                       "(access non seekable)",
767                       i_offset - p_sys->block.i_size );
768         }
769         else
770         {
771             int64_t i_skip = i_offset - p_sys->block.i_size;
772
773             /* Avg bytes per packets */
774             int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
775             /* TODO compute a seek cost instead of fixed threshold */
776             int i_th = b_aseekfast ? 1 : 5;
777
778             if( i_skip <= i_th * i_avg &&
779                 i_skip < STREAM_CACHE_SIZE )
780                 b_seek = VLC_FALSE;
781             else
782                 b_seek = VLC_TRUE;
783
784             msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
785                      b_seek, i_th*i_avg, i_skip );
786         }
787     }
788
789     if( b_seek )
790     {
791         int64_t i_start, i_end;
792         /* Do the access seek */
793         i_start = mdate();
794         if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
795         i_end = mdate();
796
797         /* Release data */
798         block_ChainRelease( p_sys->block.p_first );
799
800         /* Reinit */
801         p_sys->block.i_start = p_sys->i_pos = i_pos;
802         p_sys->block.i_offset = 0;
803         p_sys->block.p_current = NULL;
804         p_sys->block.i_size = 0;
805         p_sys->block.p_first = NULL;
806         p_sys->block.pp_last = &p_sys->block.p_first;
807
808         /* Refill a block */
809         if( AStreamRefillBlock( s ) )
810         {
811             msg_Err( s, "cannot re fill buffer" );
812             return VLC_EGENERIC;
813         }
814         /* Update stat */
815         p_sys->stat.i_seek_time += i_end - i_start;
816         p_sys->stat.i_seek_count++;
817         return VLC_SUCCESS;
818     }
819     else
820     {
821         /* Read enought data */
822         while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
823         {
824             if( AStreamRefillBlock( s ) )
825             {
826                 msg_Err( s, "can't read enough data in seek" );
827                 return VLC_EGENERIC;
828             }
829             while( p_sys->block.p_current &&
830                    p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
831             {
832                 p_sys->i_pos += p_sys->block.p_current->i_buffer;
833                 p_sys->block.p_current = p_sys->block.p_current->p_next;
834             }
835         }
836
837         p_sys->block.i_offset = i_pos - p_sys->i_pos;
838         p_sys->i_pos = i_pos;
839
840         /* TODO read data */
841         return VLC_SUCCESS;
842     }
843
844     return VLC_EGENERIC;
845 }
846
847 static int AStreamRefillBlock( stream_t *s )
848 {
849     stream_sys_t *p_sys = s->p_sys;
850     int64_t      i_start, i_stop;
851     block_t      *b;
852
853     /* Release data */
854     while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
855            p_sys->block.p_first != p_sys->block.p_current )
856     {
857         block_t *b = p_sys->block.p_first;
858
859         p_sys->block.i_start += b->i_buffer;
860         p_sys->block.i_size  -= b->i_buffer;
861         p_sys->block.p_first  = b->p_next;
862
863         block_Release( b );
864     }
865     if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
866         p_sys->block.p_current == p_sys->block.p_first &&
867         p_sys->block.p_current->p_next )    /* At least 2 packets */
868     {
869         /* Enough data, don't read more */
870         return VLC_SUCCESS;
871     }
872
873     /* Now read a new block */
874     i_start = mdate();
875     for( ;; )
876     {
877         vlc_bool_t b_eof;
878
879         if( s->b_die ) return VLC_EGENERIC;
880
881
882         /* Fetch a block */
883         if( ( b = AReadBlock( s, &b_eof ) ) ) break;
884
885         if( b_eof ) return VLC_EGENERIC;
886
887         msleep( STREAM_DATA_WAIT );
888     }
889     i_stop = mdate();
890
891     /* Append the block */
892     p_sys->block.i_size += b->i_buffer;
893     *p_sys->block.pp_last = b;
894     p_sys->block.pp_last = &b->p_next;
895
896     /* Fix p_current */
897     if( p_sys->block.p_current == NULL )
898         p_sys->block.p_current = b;
899
900     /* Update stat */
901     p_sys->stat.i_bytes += b->i_buffer;
902     p_sys->stat.i_read_time += i_stop - i_start;
903     p_sys->stat.i_read_count++;
904
905     return VLC_SUCCESS;
906 }
907
908
909 /****************************************************************************
910  * Method 2:
911  ****************************************************************************/
912 static int AStreamRefillStream( stream_t *s );
913
914 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
915 {
916     stream_sys_t *p_sys = s->p_sys;
917     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
918
919     uint8_t *p_data = (uint8_t *)p_read;
920     int      i_data = 0;
921
922     if( tk->i_start >= tk->i_end ) return 0; /* EOF */
923
924     if( p_read == NULL )
925     {
926         /* seek within this stream if possible, else use plain old read and discard */
927         stream_sys_t *p_sys = s->p_sys;
928         access_t     *p_access = p_sys->p_access;
929         vlc_bool_t   b_aseek;
930         access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
931         if( b_aseek )
932             return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
933     }
934
935 #if 0
936     msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
937              " offset=%d end="I64Fd,
938              i_read, p_sys->i_pos, p_sys->stream.i_tk,
939              tk->i_start, p_sys->stream.i_offset, tk->i_end );
940 #endif
941
942     while( i_data < i_read )
943     {
944         int i_off = (tk->i_start + p_sys->stream.i_offset) %
945                     STREAM_CACHE_TRACK_SIZE;
946         int i_current =
947             __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
948                    STREAM_CACHE_TRACK_SIZE - i_off );
949         int i_copy = __MIN( i_current, i_read - i_data );
950
951         if( i_copy <= 0 ) break; /* EOF */
952
953         /* Copy data */
954         /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
955         if( p_data )
956         {
957             memcpy( p_data, &tk->p_buffer[i_off], i_copy );
958             p_data += i_copy;
959         }
960         i_data += i_copy;
961         p_sys->stream.i_offset += i_copy;
962
963         /* Update pos now */
964         p_sys->i_pos += i_copy;
965
966         /* */
967         p_sys->stream.i_used += i_copy;
968         if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
969             p_sys->stream.i_used >= p_sys->stream.i_read_size )
970         {
971             if( AStreamRefillStream( s ) )
972             {
973                 /* EOF */
974                 if( tk->i_start >= tk->i_end ) break;
975             }
976         }
977     }
978
979     return i_data;
980 }
981
982 static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read )
983 {
984     stream_sys_t *p_sys = s->p_sys;
985     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
986     int64_t i_off;
987
988     if( tk->i_start >= tk->i_end ) return 0; /* EOF */
989
990 #if 0
991     msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
992              "start="I64Fd" offset=%d end="I64Fd,
993              i_read, p_sys->i_pos, p_sys->stream.i_tk,
994              tk->i_start, p_sys->stream.i_offset, tk->i_end );
995 #endif
996
997     /* Avoid problem, but that should *never* happen */
998     if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
999         i_read = STREAM_CACHE_TRACK_SIZE / 2;
1000
1001     while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1002     {
1003         if( p_sys->stream.i_used <= 1 )
1004         {
1005             /* Be sure we will read something */
1006             p_sys->stream.i_used += i_read - (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1007         }
1008         if( AStreamRefillStream( s ) )
1009             break;
1010     }
1011
1012     if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1013         i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1014
1015     /* Now, direct pointer or a copy ? */
1016     i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1017     if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1018     {
1019         *pp_peek = &tk->p_buffer[i_off];
1020         return i_read;
1021     }
1022
1023     if( p_sys->i_peek < i_read )
1024     {
1025         if( p_sys->p_peek ) free( p_sys->p_peek );
1026         p_sys->i_peek = i_read;
1027         p_sys->p_peek = malloc( i_read );
1028     }
1029
1030     memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1031             STREAM_CACHE_TRACK_SIZE - i_off );
1032     memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1033             &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1034
1035     *pp_peek = p_sys->p_peek;
1036     return i_read;
1037 }
1038
1039 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1040 {
1041     stream_sys_t *p_sys = s->p_sys;
1042     access_t     *p_access = p_sys->p_access;
1043     vlc_bool_t   b_aseek;
1044     vlc_bool_t   b_afastseek;
1045     int i_maxth;
1046     int i_new;
1047     int i;
1048
1049 #if 0
1050     msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
1051              "tk=%d start="I64Fd" offset=%d end="I64Fd,
1052              i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1053              p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1054              p_sys->stream.i_offset,
1055              p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1056 #endif
1057
1058
1059     /* Seek in our current track ? */
1060     if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1061         i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1062     {
1063         //msg_Dbg( s, "AStreamSeekStream: current track" );
1064         p_sys->i_pos = i_pos;
1065         p_sys->stream.i_offset = i_pos - p_sys->stream.tk[p_sys->stream.i_tk].i_start;
1066         return VLC_SUCCESS;
1067     }
1068
1069     access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1070     if( !b_aseek )
1071     {
1072         /* We can't do nothing */
1073         msg_Dbg( s, "AStreamSeekStream: can't seek" );
1074         return VLC_EGENERIC;
1075     }
1076
1077     /* Date the current track */
1078     p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1079
1080     /* Try to reuse already read data */
1081     for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1082     {
1083         stream_track_t *tk = &p_sys->stream.tk[i];
1084
1085         if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1086         {
1087 #if 0
1088             msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
1089                      " end="I64Fd, i, tk->i_start, tk->i_end );
1090 #endif
1091             /* Seek at the end of the buffer */
1092             if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1093
1094             /* That's it */
1095             p_sys->i_pos = i_pos;
1096             p_sys->stream.i_tk = i;
1097             p_sys->stream.i_offset = i_pos - tk->i_start;
1098
1099             if( p_sys->stream.i_used < 1024 )
1100                 p_sys->stream.i_used = 1024;
1101
1102             if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1103                 return VLC_EGENERIC;
1104
1105             return VLC_SUCCESS;
1106         }
1107     }
1108
1109     access2_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1110     /* FIXME compute seek cost (instead of static 'stupid' value) */
1111     i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1112     if( !b_afastseek )
1113         i_maxth *= 3;
1114
1115     /* FIXME TODO */
1116 #if 0
1117     /* Search closest segment TODO */
1118     for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1119     {
1120         stream_track_t *tk = &p_sys->stream.tk[i];
1121
1122         if( i_pos + i_maxth >= tk->i_start )
1123         {
1124             msg_Dbg( s, "good segment before current pos, TODO" );
1125         }
1126         if( i_pos - i_maxth <= tk->i_end )
1127         {
1128             msg_Dbg( s, "good segment after current pos, TODO" );
1129         }
1130     }
1131 #endif
1132
1133     /* Nothing good, seek and choose oldest segment */
1134     if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1135     p_sys->i_pos = i_pos;
1136
1137     i_new = 0;
1138     for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1139     {
1140         if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1141             i_new = i;
1142     }
1143
1144     /* Reset the segment */
1145     p_sys->stream.i_tk     = i_new;
1146     p_sys->stream.i_offset =  0;
1147     p_sys->stream.tk[i_new].i_start = i_pos;
1148     p_sys->stream.tk[i_new].i_end   = i_pos;
1149
1150     /* Read data */
1151     if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1152         p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1153
1154     if( AStreamRefillStream( s ) )
1155         return VLC_EGENERIC;
1156
1157     return VLC_SUCCESS;
1158 }
1159
1160 static int AStreamRefillStream( stream_t *s )
1161 {
1162     stream_sys_t *p_sys = s->p_sys;
1163     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1164
1165     /* We read but won't increase i_start after initial start + offset */
1166     int i_toread =
1167         __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1168                (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1169     vlc_bool_t b_read = VLC_FALSE;
1170     int64_t i_start, i_stop;
1171
1172     if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1173
1174     /* msg_Dbg( s, "AStreamRefillStream: toread=%d", i_toread ); */
1175
1176     i_start = mdate();
1177     while( i_toread > 0 )
1178     {
1179         int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1180         int i_read;
1181
1182         if( s->b_die )
1183             return VLC_EGENERIC;
1184
1185         i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1186         i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1187
1188         /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1189         if( i_read <  0 )
1190         {
1191             msleep( STREAM_DATA_WAIT );
1192             continue;
1193         }
1194         else if( i_read == 0 )
1195         {
1196             if( !b_read ) return VLC_EGENERIC;
1197             return VLC_SUCCESS;
1198         }
1199         b_read = VLC_TRUE;
1200
1201         /* Update end */
1202         tk->i_end += i_read;
1203
1204         /* Windows of STREAM_CACHE_TRACK_SIZE */
1205         if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1206         {
1207             int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1208
1209             tk->i_start += i_invalid;
1210             p_sys->stream.i_offset -= i_invalid;
1211         }
1212
1213         i_toread -= i_read;
1214         p_sys->stream.i_used -= i_read;
1215
1216         p_sys->stat.i_bytes += i_read;
1217         p_sys->stat.i_read_count++;
1218     }
1219     i_stop = mdate();
1220
1221     p_sys->stat.i_read_time += i_stop - i_start;
1222
1223     return VLC_SUCCESS;
1224 }
1225
1226 static void AStreamPrebufferStream( stream_t *s )
1227 {
1228     stream_sys_t *p_sys = s->p_sys;
1229     access_t     *p_access = p_sys->p_access;
1230
1231     int64_t i_first = 0;
1232     int64_t i_start;
1233     int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1234         ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1235           STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1236
1237     msg_Dbg( s, "pre buffering" );
1238     i_start = mdate();
1239     for( ;; )
1240     {
1241         stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1242
1243         int64_t i_date = mdate();
1244         int i_read;
1245
1246         if( s->b_die || tk->i_end >= i_prebuffer ||
1247             (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1248         {
1249             int64_t i_byterate;
1250
1251             /* Update stat */
1252             p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1253             p_sys->stat.i_read_time = i_date - i_start;
1254             i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
1255                          (p_sys->stat.i_read_time+1);
1256
1257             msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
1258                      I64Fd" kbytes/s",
1259                      p_sys->stat.i_bytes,
1260                      p_sys->stat.i_read_time / I64C(1000000),
1261                      i_byterate / 1024 );
1262             break;
1263         }
1264
1265         /* */
1266         i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1267         i_read = __MIN( p_sys->stream.i_read_size, i_read );
1268         i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1269         if( i_read <  0 )
1270         {
1271             msleep( STREAM_DATA_WAIT );
1272             continue;
1273         }
1274         else if( i_read == 0 )
1275         {
1276             /* EOF */
1277             break;
1278         }
1279
1280         if( i_first == 0 )
1281         {
1282             i_first = mdate();
1283             msg_Dbg( s, "received first data for our buffer");
1284         }
1285
1286         tk->i_end += i_read;
1287
1288         p_sys->stat.i_read_count++;
1289     }
1290 }
1291
1292
1293 /****************************************************************************
1294  * stream_ReadLine:
1295  ****************************************************************************/
1296 /**
1297  * Read from the stream untill first newline.
1298  * \param s Stream handle to read from
1299  * \return A null-terminated string. This must be freed,
1300  */
1301 #define STREAM_PROBE_LINE 2048
1302 #define STREAM_LINE_MAX (2048*100)
1303 char *stream_ReadLine( stream_t *s )
1304 {
1305     char *p_line = NULL;
1306     int i_line = 0, i_read = 0;
1307
1308     while( i_read < STREAM_LINE_MAX )
1309     {
1310         char *psz_eol;
1311         uint8_t *p_data;
1312         int i_data;
1313
1314         /* Probe new data */
1315         i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1316         if( i_data <= 0 ) break; /* No more data */
1317
1318         /* Check if there is an EOL */
1319         if( ( psz_eol = memchr( p_data, '\n', i_data ) ) )
1320         {
1321             i_data = (psz_eol - (char *)p_data) + 1;
1322             p_line = realloc( p_line, i_line + i_data + 1 );
1323             i_data = stream_Read( s, &p_line[i_line], i_data );
1324             if( i_data <= 0 ) break; /* Hmmm */
1325             i_line += (i_data - 1);
1326             i_read += i_data;
1327
1328             /* We have our line */
1329             break;
1330         }
1331
1332         /* Read data (+1 for easy \0 append) */
1333         p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + 1 );
1334         i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1335         if( i_data <= 0 ) break; /* Hmmm */
1336         i_line += i_data;
1337         i_read += i_data;
1338     }
1339
1340     /* Remove trailing LF/CR */
1341     while( i_line > 0 && ( p_line[i_line-1] == '\r' ||
1342            p_line[i_line-1] == '\n') ) i_line--;
1343
1344     if( i_read > 0 )
1345     {
1346         p_line[i_line] = '\0';
1347         return p_line;
1348     }
1349
1350     /* We failed to read any data, probably EOF */
1351     if( p_line ) free( p_line );
1352     return NULL;
1353 }
1354
1355 /****************************************************************************
1356  * Access reading/seeking wrappers to handle concatenated streams.
1357  ****************************************************************************/
1358 static int AReadStream( stream_t *s, void *p_read, int i_read )
1359 {
1360     stream_sys_t *p_sys = s->p_sys;
1361     access_t *p_access = p_sys->p_access;
1362     int i_read_orig = i_read;
1363
1364     if( !p_sys->i_list )
1365     {
1366         i_read = p_access->pf_read( p_access, p_read, i_read );
1367         return i_read;
1368     }
1369
1370     i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1371                                             i_read );
1372
1373     /* If we reached an EOF then switch to the next stream in the list */
1374     if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1375     {
1376         char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1377         access_t *p_list_access;
1378
1379         msg_Dbg( s, "opening input `%s'", psz_name );
1380
1381         p_list_access = access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1382
1383         if( !p_list_access ) return 0;
1384
1385         if( p_sys->p_list_access != p_access )
1386             access2_Delete( p_sys->p_list_access );
1387
1388         p_sys->p_list_access = p_list_access;
1389
1390         /* We have to read some data */
1391         return AReadStream( s, p_read, i_read_orig );
1392     }
1393
1394     return i_read;
1395 }
1396
1397 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof )
1398 {
1399     stream_sys_t *p_sys = s->p_sys;
1400     access_t *p_access = p_sys->p_access;
1401     block_t *p_block;
1402     vlc_bool_t b_eof;
1403
1404     if( !p_sys->i_list )
1405     {
1406         p_block = p_access->pf_block( p_access );
1407         if( pb_eof ) *pb_eof = p_access->info.b_eof;
1408         return p_block;
1409     }
1410
1411     p_block = p_sys->p_list_access->pf_block( p_access );
1412     b_eof = p_sys->p_list_access->info.b_eof;
1413     if( pb_eof ) *pb_eof = b_eof;
1414
1415     /* If we reached an EOF then switch to the next stream in the list */
1416     if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1417     {
1418         char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1419         access_t *p_list_access;
1420
1421         msg_Dbg( s, "opening input `%s'", psz_name );
1422
1423         p_list_access = access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1424
1425         if( !p_list_access ) return 0;
1426
1427         if( p_sys->p_list_access != p_access )
1428             access2_Delete( p_sys->p_list_access );
1429
1430         p_sys->p_list_access = p_list_access;
1431
1432         /* We have to read some data */
1433         return AReadBlock( s, pb_eof );
1434     }
1435
1436     return p_block;
1437 }
1438
1439 static int ASeek( stream_t *s, int64_t i_pos )
1440 {
1441     stream_sys_t *p_sys = s->p_sys;
1442     access_t *p_access = p_sys->p_access;
1443
1444     /* Check which stream we need to access */
1445     if( p_sys->i_list )
1446     {
1447         int i;
1448         char *psz_name;
1449         int64_t i_size = 0;
1450         access_t *p_list_access = 0;
1451
1452         for( i = 0; i < p_sys->i_list - 1; i++ )
1453         {
1454             if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1455             i_size += p_sys->list[i]->i_size;
1456         }
1457         psz_name = p_sys->list[i]->psz_path;
1458
1459         if( i != p_sys->i_list_index )
1460             msg_Dbg( s, "opening input `%s'", psz_name );
1461
1462         if( i != p_sys->i_list_index && i != 0 )
1463         {
1464             p_list_access =
1465                 access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1466         }
1467         else if( i != p_sys->i_list_index )
1468         {
1469             p_list_access = p_access;
1470         }
1471
1472         if( p_list_access )
1473         {
1474             if( p_sys->p_list_access != p_access )
1475                 access2_Delete( p_sys->p_list_access );
1476
1477             p_sys->p_list_access = p_list_access;
1478         }
1479
1480         p_sys->i_list_index = i;
1481         return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1482                                               i_pos - i_size );
1483     }
1484
1485     return p_access->pf_seek( p_access, i_pos );
1486 }