]> git.sesse.net Git - vlc/blob - src/input/stream.c
* all: better support for MBR mms stream (display only received streams).
[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                 i_int != ACCESS_GET_PRIVATE_ID_STATE )
509             {
510                 msg_Err( s, "Hey, what are you thinking ?"
511                             "DON'T USE STREAM_CONTROL_ACCESS !!!" );
512                 return VLC_EGENERIC;
513             }
514             return access2_vaControl( p_access, i_int, args );
515
516         default:
517             msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
518             return VLC_EGENERIC;
519     }
520     return VLC_SUCCESS;
521 }
522
523
524
525 /****************************************************************************
526  * Method 1:
527  ****************************************************************************/
528 static void AStreamPrebufferBlock( stream_t *s )
529 {
530     stream_sys_t *p_sys = s->p_sys;
531
532     int64_t i_first = 0;
533     int64_t i_start;
534
535     msg_Dbg( s, "pre buffering" );
536     i_start = mdate();
537     for( ;; )
538     {
539         int64_t i_date = mdate();
540         vlc_bool_t b_eof;
541         block_t *b;
542
543         if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
544             ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
545         {
546             int64_t i_byterate;
547
548             /* Update stat */
549             p_sys->stat.i_bytes = p_sys->block.i_size;
550             p_sys->stat.i_read_time = i_date - i_start;
551             i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
552                          (p_sys->stat.i_read_time + 1);
553
554             msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
555                      I64Fd" kbytes/s",
556                      p_sys->stat.i_bytes,
557                      p_sys->stat.i_read_time / I64C(1000000),
558                      i_byterate / 1024 );
559             break;
560         }
561
562         /* Fetch a block */
563         if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
564         {
565             if( b_eof ) break;
566
567             msleep( STREAM_DATA_WAIT );
568             continue;
569         }
570
571         if( i_first == 0 )
572         {
573             i_first = mdate();
574             msg_Dbg( s, "received first data for our buffer");
575         }
576
577         /* Append the block */
578         p_sys->block.i_size += b->i_buffer;
579         *p_sys->block.pp_last = b;
580         p_sys->block.pp_last = &b->p_next;
581
582         p_sys->stat.i_read_count++;
583     }
584
585     p_sys->block.p_current = p_sys->block.p_first;
586 }
587
588 static int AStreamRefillBlock( stream_t *s );
589
590 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
591 {
592     stream_sys_t *p_sys = s->p_sys;
593
594     uint8_t *p_data= (uint8_t*)p_read;
595     int     i_data = 0;
596
597     /* It means EOF */
598     if( p_sys->block.p_current == NULL )
599         return 0;
600
601     if( p_read == NULL )
602     {
603         /* seek within this stream if possible, else use plain old read and discard */
604         stream_sys_t *p_sys = s->p_sys;
605         access_t     *p_access = p_sys->p_access;
606         vlc_bool_t   b_aseek;
607         access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
608         if( b_aseek )
609             return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
610     }
611
612     while( i_data < i_read )
613     {
614         int i_current =
615             p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
616         int i_copy = __MIN( i_current, i_read - i_data);
617
618         /* Copy data */
619         if( p_data )
620         {
621             memcpy( p_data,
622                     &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
623                     i_copy );
624             p_data += i_copy;
625         }
626         i_data += i_copy;
627
628         p_sys->block.i_offset += i_copy;
629         if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
630         {
631             /* Current block is now empty, switch to next */
632             if( p_sys->block.p_current )
633             {
634                 p_sys->block.i_offset = 0;
635                 p_sys->block.p_current = p_sys->block.p_current->p_next;
636             }
637             /*Get a new block */
638             if( AStreamRefillBlock( s ) )
639             {
640                 break;
641             }
642         }
643     }
644
645     p_sys->i_pos += i_data;
646     return i_data;
647 }
648
649 static int AStreamPeekBlock( stream_t *s, uint8_t **pp_peek, int i_read )
650 {
651     stream_sys_t *p_sys = s->p_sys;
652     uint8_t *p_data;
653     int      i_data = 0;
654     block_t *b;
655     int      i_offset;
656
657     if( p_sys->block.p_current == NULL ) return 0; /* EOF */
658
659     /* We can directly give a pointer over our buffer */
660     if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
661     {
662         *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
663         return i_read;
664     }
665
666     /* We need to create a local copy */
667     if( p_sys->i_peek < i_read )
668     {
669         if( p_sys->p_peek )
670             free( p_sys->p_peek );
671         p_sys->i_peek = i_read;
672         p_sys->p_peek = malloc( p_sys->i_peek );
673     }
674
675     /* Fill enough data */
676     while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
677            < i_read )
678     {
679         block_t **pp_last = p_sys->block.pp_last;
680
681         if( AStreamRefillBlock( s ) )
682             break;
683
684         /* Our buffer are probably filled enough, don't try anymore */
685         if( pp_last == p_sys->block.pp_last )
686             break;
687     }
688
689     /* Copy what we have */
690     b = p_sys->block.p_current;
691     i_offset = p_sys->block.i_offset;
692     p_data = p_sys->p_peek;
693
694     while( b && i_data < i_read )
695     {
696         int i_current = b->i_buffer - i_offset;
697         int i_copy = __MIN( i_current, i_read - i_data );
698
699         memcpy( p_data, &b->p_buffer[i_offset], i_copy );
700         i_data += i_copy;
701         p_data += i_copy;
702         i_offset += i_copy;
703
704         if( i_offset >= b->i_buffer )
705         {
706             i_offset = 0;
707             b = b->p_next;
708         }
709     }
710
711     *pp_peek = p_sys->p_peek;
712     return i_data;
713 }
714
715 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
716 {
717     stream_sys_t *p_sys = s->p_sys;
718     access_t   *p_access = p_sys->p_access;
719     int64_t    i_offset = i_pos - p_sys->block.i_start;
720     vlc_bool_t b_seek;
721
722     /* We already have thoses data, just update p_current/i_offset */
723     if( i_offset >= 0 && i_offset < p_sys->block.i_size )
724     {
725         block_t *b = p_sys->block.p_first;
726         int i_current = 0;
727
728         while( i_current + b->i_buffer < i_offset )
729         {
730             i_current += b->i_buffer;
731             b = b->p_next;
732         }
733
734         p_sys->block.p_current = b;
735         p_sys->block.i_offset = i_offset - i_current;
736
737         p_sys->i_pos = i_pos;
738
739         return VLC_SUCCESS;
740     }
741
742     /* We may need to seek or to read data */
743     if( i_offset < 0 )
744     {
745         vlc_bool_t b_aseek;
746         access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
747
748         if( !b_aseek )
749         {
750             msg_Err( s, "backward seek impossible (access non seekable)" );
751             return VLC_EGENERIC;
752         }
753
754         b_seek = VLC_TRUE;
755     }
756     else
757     {
758         vlc_bool_t b_aseek, b_aseekfast;
759
760         access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
761         access2_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
762
763         if( !b_aseek )
764         {
765             b_seek = VLC_FALSE;
766             msg_Warn( s, I64Fd" bytes need to be skipped "
767                       "(access non seekable)",
768                       i_offset - p_sys->block.i_size );
769         }
770         else
771         {
772             int64_t i_skip = i_offset - p_sys->block.i_size;
773
774             /* Avg bytes per packets */
775             int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
776             /* TODO compute a seek cost instead of fixed threshold */
777             int i_th = b_aseekfast ? 1 : 5;
778
779             if( i_skip <= i_th * i_avg &&
780                 i_skip < STREAM_CACHE_SIZE )
781                 b_seek = VLC_FALSE;
782             else
783                 b_seek = VLC_TRUE;
784
785             msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
786                      b_seek, i_th*i_avg, i_skip );
787         }
788     }
789
790     if( b_seek )
791     {
792         int64_t i_start, i_end;
793         /* Do the access seek */
794         i_start = mdate();
795         if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
796         i_end = mdate();
797
798         /* Release data */
799         block_ChainRelease( p_sys->block.p_first );
800
801         /* Reinit */
802         p_sys->block.i_start = p_sys->i_pos = i_pos;
803         p_sys->block.i_offset = 0;
804         p_sys->block.p_current = NULL;
805         p_sys->block.i_size = 0;
806         p_sys->block.p_first = NULL;
807         p_sys->block.pp_last = &p_sys->block.p_first;
808
809         /* Refill a block */
810         if( AStreamRefillBlock( s ) )
811         {
812             msg_Err( s, "cannot re fill buffer" );
813             return VLC_EGENERIC;
814         }
815         /* Update stat */
816         p_sys->stat.i_seek_time += i_end - i_start;
817         p_sys->stat.i_seek_count++;
818         return VLC_SUCCESS;
819     }
820     else
821     {
822         /* Read enought data */
823         while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
824         {
825             if( AStreamRefillBlock( s ) )
826             {
827                 msg_Err( s, "can't read enough data in seek" );
828                 return VLC_EGENERIC;
829             }
830             while( p_sys->block.p_current &&
831                    p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
832             {
833                 p_sys->i_pos += p_sys->block.p_current->i_buffer;
834                 p_sys->block.p_current = p_sys->block.p_current->p_next;
835             }
836         }
837
838         p_sys->block.i_offset = i_pos - p_sys->i_pos;
839         p_sys->i_pos = i_pos;
840
841         /* TODO read data */
842         return VLC_SUCCESS;
843     }
844
845     return VLC_EGENERIC;
846 }
847
848 static int AStreamRefillBlock( stream_t *s )
849 {
850     stream_sys_t *p_sys = s->p_sys;
851     int64_t      i_start, i_stop;
852     block_t      *b;
853
854     /* Release data */
855     while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
856            p_sys->block.p_first != p_sys->block.p_current )
857     {
858         block_t *b = p_sys->block.p_first;
859
860         p_sys->block.i_start += b->i_buffer;
861         p_sys->block.i_size  -= b->i_buffer;
862         p_sys->block.p_first  = b->p_next;
863
864         block_Release( b );
865     }
866     if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
867         p_sys->block.p_current == p_sys->block.p_first &&
868         p_sys->block.p_current->p_next )    /* At least 2 packets */
869     {
870         /* Enough data, don't read more */
871         return VLC_SUCCESS;
872     }
873
874     /* Now read a new block */
875     i_start = mdate();
876     for( ;; )
877     {
878         vlc_bool_t b_eof;
879
880         if( s->b_die ) return VLC_EGENERIC;
881
882
883         /* Fetch a block */
884         if( ( b = AReadBlock( s, &b_eof ) ) ) break;
885
886         if( b_eof ) return VLC_EGENERIC;
887
888         msleep( STREAM_DATA_WAIT );
889     }
890     i_stop = mdate();
891
892     /* Append the block */
893     p_sys->block.i_size += b->i_buffer;
894     *p_sys->block.pp_last = b;
895     p_sys->block.pp_last = &b->p_next;
896
897     /* Fix p_current */
898     if( p_sys->block.p_current == NULL )
899         p_sys->block.p_current = b;
900
901     /* Update stat */
902     p_sys->stat.i_bytes += b->i_buffer;
903     p_sys->stat.i_read_time += i_stop - i_start;
904     p_sys->stat.i_read_count++;
905
906     return VLC_SUCCESS;
907 }
908
909
910 /****************************************************************************
911  * Method 2:
912  ****************************************************************************/
913 static int AStreamRefillStream( stream_t *s );
914
915 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
916 {
917     stream_sys_t *p_sys = s->p_sys;
918     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
919
920     uint8_t *p_data = (uint8_t *)p_read;
921     int      i_data = 0;
922
923     if( tk->i_start >= tk->i_end ) return 0; /* EOF */
924
925     if( p_read == NULL )
926     {
927         /* seek within this stream if possible, else use plain old read and discard */
928         stream_sys_t *p_sys = s->p_sys;
929         access_t     *p_access = p_sys->p_access;
930         vlc_bool_t   b_aseek;
931         access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
932         if( b_aseek )
933             return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
934     }
935
936 #if 0
937     msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
938              " offset=%d end="I64Fd,
939              i_read, p_sys->i_pos, p_sys->stream.i_tk,
940              tk->i_start, p_sys->stream.i_offset, tk->i_end );
941 #endif
942
943     while( i_data < i_read )
944     {
945         int i_off = (tk->i_start + p_sys->stream.i_offset) %
946                     STREAM_CACHE_TRACK_SIZE;
947         int i_current =
948             __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
949                    STREAM_CACHE_TRACK_SIZE - i_off );
950         int i_copy = __MIN( i_current, i_read - i_data );
951
952         if( i_copy <= 0 ) break; /* EOF */
953
954         /* Copy data */
955         /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
956         if( p_data )
957         {
958             memcpy( p_data, &tk->p_buffer[i_off], i_copy );
959             p_data += i_copy;
960         }
961         i_data += i_copy;
962         p_sys->stream.i_offset += i_copy;
963
964         /* Update pos now */
965         p_sys->i_pos += i_copy;
966
967         /* */
968         p_sys->stream.i_used += i_copy;
969         if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
970             p_sys->stream.i_used >= p_sys->stream.i_read_size )
971         {
972             if( AStreamRefillStream( s ) )
973             {
974                 /* EOF */
975                 if( tk->i_start >= tk->i_end ) break;
976             }
977         }
978     }
979
980     return i_data;
981 }
982
983 static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read )
984 {
985     stream_sys_t *p_sys = s->p_sys;
986     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
987     int64_t i_off;
988
989     if( tk->i_start >= tk->i_end ) return 0; /* EOF */
990
991 #if 0
992     msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
993              "start="I64Fd" offset=%d end="I64Fd,
994              i_read, p_sys->i_pos, p_sys->stream.i_tk,
995              tk->i_start, p_sys->stream.i_offset, tk->i_end );
996 #endif
997
998     /* Avoid problem, but that should *never* happen */
999     if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1000         i_read = STREAM_CACHE_TRACK_SIZE / 2;
1001
1002     while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1003     {
1004         if( p_sys->stream.i_used <= 1 )
1005         {
1006             /* Be sure we will read something */
1007             p_sys->stream.i_used += i_read - (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1008         }
1009         if( AStreamRefillStream( s ) )
1010             break;
1011     }
1012
1013     if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1014         i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1015
1016     /* Now, direct pointer or a copy ? */
1017     i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1018     if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1019     {
1020         *pp_peek = &tk->p_buffer[i_off];
1021         return i_read;
1022     }
1023
1024     if( p_sys->i_peek < i_read )
1025     {
1026         if( p_sys->p_peek ) free( p_sys->p_peek );
1027         p_sys->i_peek = i_read;
1028         p_sys->p_peek = malloc( i_read );
1029     }
1030
1031     memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1032             STREAM_CACHE_TRACK_SIZE - i_off );
1033     memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1034             &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1035
1036     *pp_peek = p_sys->p_peek;
1037     return i_read;
1038 }
1039
1040 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1041 {
1042     stream_sys_t *p_sys = s->p_sys;
1043     access_t     *p_access = p_sys->p_access;
1044     vlc_bool_t   b_aseek;
1045     vlc_bool_t   b_afastseek;
1046     int i_maxth;
1047     int i_new;
1048     int i;
1049
1050 #if 0
1051     msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
1052              "tk=%d start="I64Fd" offset=%d end="I64Fd,
1053              i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1054              p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1055              p_sys->stream.i_offset,
1056              p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1057 #endif
1058
1059
1060     /* Seek in our current track ? */
1061     if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1062         i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1063     {
1064         //msg_Dbg( s, "AStreamSeekStream: current track" );
1065         p_sys->i_pos = i_pos;
1066         p_sys->stream.i_offset = i_pos - p_sys->stream.tk[p_sys->stream.i_tk].i_start;
1067         return VLC_SUCCESS;
1068     }
1069
1070     access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1071     if( !b_aseek )
1072     {
1073         /* We can't do nothing */
1074         msg_Dbg( s, "AStreamSeekStream: can't seek" );
1075         return VLC_EGENERIC;
1076     }
1077
1078     /* Date the current track */
1079     p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1080
1081     /* Try to reuse already read data */
1082     for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1083     {
1084         stream_track_t *tk = &p_sys->stream.tk[i];
1085
1086         if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1087         {
1088 #if 0
1089             msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
1090                      " end="I64Fd, i, tk->i_start, tk->i_end );
1091 #endif
1092             /* Seek at the end of the buffer */
1093             if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1094
1095             /* That's it */
1096             p_sys->i_pos = i_pos;
1097             p_sys->stream.i_tk = i;
1098             p_sys->stream.i_offset = i_pos - tk->i_start;
1099
1100             if( p_sys->stream.i_used < 1024 )
1101                 p_sys->stream.i_used = 1024;
1102
1103             if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1104                 return VLC_EGENERIC;
1105
1106             return VLC_SUCCESS;
1107         }
1108     }
1109
1110     access2_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1111     /* FIXME compute seek cost (instead of static 'stupid' value) */
1112     i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1113     if( !b_afastseek )
1114         i_maxth *= 3;
1115
1116     /* FIXME TODO */
1117 #if 0
1118     /* Search closest segment TODO */
1119     for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1120     {
1121         stream_track_t *tk = &p_sys->stream.tk[i];
1122
1123         if( i_pos + i_maxth >= tk->i_start )
1124         {
1125             msg_Dbg( s, "good segment before current pos, TODO" );
1126         }
1127         if( i_pos - i_maxth <= tk->i_end )
1128         {
1129             msg_Dbg( s, "good segment after current pos, TODO" );
1130         }
1131     }
1132 #endif
1133
1134     /* Nothing good, seek and choose oldest segment */
1135     if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1136     p_sys->i_pos = i_pos;
1137
1138     i_new = 0;
1139     for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1140     {
1141         if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1142             i_new = i;
1143     }
1144
1145     /* Reset the segment */
1146     p_sys->stream.i_tk     = i_new;
1147     p_sys->stream.i_offset =  0;
1148     p_sys->stream.tk[i_new].i_start = i_pos;
1149     p_sys->stream.tk[i_new].i_end   = i_pos;
1150
1151     /* Read data */
1152     if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1153         p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1154
1155     if( AStreamRefillStream( s ) )
1156         return VLC_EGENERIC;
1157
1158     return VLC_SUCCESS;
1159 }
1160
1161 static int AStreamRefillStream( stream_t *s )
1162 {
1163     stream_sys_t *p_sys = s->p_sys;
1164     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1165
1166     /* We read but won't increase i_start after initial start + offset */
1167     int i_toread =
1168         __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1169                (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1170     vlc_bool_t b_read = VLC_FALSE;
1171     int64_t i_start, i_stop;
1172
1173     if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1174
1175     /* msg_Dbg( s, "AStreamRefillStream: toread=%d", i_toread ); */
1176
1177     i_start = mdate();
1178     while( i_toread > 0 )
1179     {
1180         int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1181         int i_read;
1182
1183         if( s->b_die )
1184             return VLC_EGENERIC;
1185
1186         i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1187         i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1188
1189         /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1190         if( i_read <  0 )
1191         {
1192             msleep( STREAM_DATA_WAIT );
1193             continue;
1194         }
1195         else if( i_read == 0 )
1196         {
1197             if( !b_read ) return VLC_EGENERIC;
1198             return VLC_SUCCESS;
1199         }
1200         b_read = VLC_TRUE;
1201
1202         /* Update end */
1203         tk->i_end += i_read;
1204
1205         /* Windows of STREAM_CACHE_TRACK_SIZE */
1206         if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1207         {
1208             int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1209
1210             tk->i_start += i_invalid;
1211             p_sys->stream.i_offset -= i_invalid;
1212         }
1213
1214         i_toread -= i_read;
1215         p_sys->stream.i_used -= i_read;
1216
1217         p_sys->stat.i_bytes += i_read;
1218         p_sys->stat.i_read_count++;
1219     }
1220     i_stop = mdate();
1221
1222     p_sys->stat.i_read_time += i_stop - i_start;
1223
1224     return VLC_SUCCESS;
1225 }
1226
1227 static void AStreamPrebufferStream( stream_t *s )
1228 {
1229     stream_sys_t *p_sys = s->p_sys;
1230     access_t     *p_access = p_sys->p_access;
1231
1232     int64_t i_first = 0;
1233     int64_t i_start;
1234     int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1235         ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1236           STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1237
1238     msg_Dbg( s, "pre buffering" );
1239     i_start = mdate();
1240     for( ;; )
1241     {
1242         stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1243
1244         int64_t i_date = mdate();
1245         int i_read;
1246
1247         if( s->b_die || tk->i_end >= i_prebuffer ||
1248             (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1249         {
1250             int64_t i_byterate;
1251
1252             /* Update stat */
1253             p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1254             p_sys->stat.i_read_time = i_date - i_start;
1255             i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
1256                          (p_sys->stat.i_read_time+1);
1257
1258             msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
1259                      I64Fd" kbytes/s",
1260                      p_sys->stat.i_bytes,
1261                      p_sys->stat.i_read_time / I64C(1000000),
1262                      i_byterate / 1024 );
1263             break;
1264         }
1265
1266         /* */
1267         i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1268         i_read = __MIN( p_sys->stream.i_read_size, i_read );
1269         i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1270         if( i_read <  0 )
1271         {
1272             msleep( STREAM_DATA_WAIT );
1273             continue;
1274         }
1275         else if( i_read == 0 )
1276         {
1277             /* EOF */
1278             break;
1279         }
1280
1281         if( i_first == 0 )
1282         {
1283             i_first = mdate();
1284             msg_Dbg( s, "received first data for our buffer");
1285         }
1286
1287         tk->i_end += i_read;
1288
1289         p_sys->stat.i_read_count++;
1290     }
1291 }
1292
1293
1294 /****************************************************************************
1295  * stream_ReadLine:
1296  ****************************************************************************/
1297 /**
1298  * Read from the stream untill first newline.
1299  * \param s Stream handle to read from
1300  * \return A null-terminated string. This must be freed,
1301  */
1302 #define STREAM_PROBE_LINE 2048
1303 #define STREAM_LINE_MAX (2048*100)
1304 char *stream_ReadLine( stream_t *s )
1305 {
1306     char *p_line = NULL;
1307     int i_line = 0, i_read = 0;
1308
1309     while( i_read < STREAM_LINE_MAX )
1310     {
1311         char *psz_eol;
1312         uint8_t *p_data;
1313         int i_data;
1314
1315         /* Probe new data */
1316         i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1317         if( i_data <= 0 ) break; /* No more data */
1318
1319         /* Check if there is an EOL */
1320         if( ( psz_eol = memchr( p_data, '\n', i_data ) ) )
1321         {
1322             i_data = (psz_eol - (char *)p_data) + 1;
1323             p_line = realloc( p_line, i_line + i_data + 1 );
1324             i_data = stream_Read( s, &p_line[i_line], i_data );
1325             if( i_data <= 0 ) break; /* Hmmm */
1326             i_line += (i_data - 1);
1327             i_read += i_data;
1328
1329             /* We have our line */
1330             break;
1331         }
1332
1333         /* Read data (+1 for easy \0 append) */
1334         p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + 1 );
1335         i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1336         if( i_data <= 0 ) break; /* Hmmm */
1337         i_line += i_data;
1338         i_read += i_data;
1339     }
1340
1341     /* Remove trailing LF/CR */
1342     while( i_line > 0 && ( p_line[i_line-1] == '\r' ||
1343            p_line[i_line-1] == '\n') ) i_line--;
1344
1345     if( i_read > 0 )
1346     {
1347         p_line[i_line] = '\0';
1348         return p_line;
1349     }
1350
1351     /* We failed to read any data, probably EOF */
1352     if( p_line ) free( p_line );
1353     return NULL;
1354 }
1355
1356 /****************************************************************************
1357  * Access reading/seeking wrappers to handle concatenated streams.
1358  ****************************************************************************/
1359 static int AReadStream( stream_t *s, void *p_read, int i_read )
1360 {
1361     stream_sys_t *p_sys = s->p_sys;
1362     access_t *p_access = p_sys->p_access;
1363     int i_read_orig = i_read;
1364
1365     if( !p_sys->i_list )
1366     {
1367         i_read = p_access->pf_read( p_access, p_read, i_read );
1368         return i_read;
1369     }
1370
1371     i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1372                                             i_read );
1373
1374     /* If we reached an EOF then switch to the next stream in the list */
1375     if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1376     {
1377         char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1378         access_t *p_list_access;
1379
1380         msg_Dbg( s, "opening input `%s'", psz_name );
1381
1382         p_list_access = access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1383
1384         if( !p_list_access ) return 0;
1385
1386         if( p_sys->p_list_access != p_access )
1387             access2_Delete( p_sys->p_list_access );
1388
1389         p_sys->p_list_access = p_list_access;
1390
1391         /* We have to read some data */
1392         return AReadStream( s, p_read, i_read_orig );
1393     }
1394
1395     return i_read;
1396 }
1397
1398 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof )
1399 {
1400     stream_sys_t *p_sys = s->p_sys;
1401     access_t *p_access = p_sys->p_access;
1402     block_t *p_block;
1403     vlc_bool_t b_eof;
1404
1405     if( !p_sys->i_list )
1406     {
1407         p_block = p_access->pf_block( p_access );
1408         if( pb_eof ) *pb_eof = p_access->info.b_eof;
1409         return p_block;
1410     }
1411
1412     p_block = p_sys->p_list_access->pf_block( p_access );
1413     b_eof = p_sys->p_list_access->info.b_eof;
1414     if( pb_eof ) *pb_eof = b_eof;
1415
1416     /* If we reached an EOF then switch to the next stream in the list */
1417     if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1418     {
1419         char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1420         access_t *p_list_access;
1421
1422         msg_Dbg( s, "opening input `%s'", psz_name );
1423
1424         p_list_access = access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1425
1426         if( !p_list_access ) return 0;
1427
1428         if( p_sys->p_list_access != p_access )
1429             access2_Delete( p_sys->p_list_access );
1430
1431         p_sys->p_list_access = p_list_access;
1432
1433         /* We have to read some data */
1434         return AReadBlock( s, pb_eof );
1435     }
1436
1437     return p_block;
1438 }
1439
1440 static int ASeek( stream_t *s, int64_t i_pos )
1441 {
1442     stream_sys_t *p_sys = s->p_sys;
1443     access_t *p_access = p_sys->p_access;
1444
1445     /* Check which stream we need to access */
1446     if( p_sys->i_list )
1447     {
1448         int i;
1449         char *psz_name;
1450         int64_t i_size = 0;
1451         access_t *p_list_access = 0;
1452
1453         for( i = 0; i < p_sys->i_list - 1; i++ )
1454         {
1455             if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1456             i_size += p_sys->list[i]->i_size;
1457         }
1458         psz_name = p_sys->list[i]->psz_path;
1459
1460         if( i != p_sys->i_list_index )
1461             msg_Dbg( s, "opening input `%s'", psz_name );
1462
1463         if( i != p_sys->i_list_index && i != 0 )
1464         {
1465             p_list_access =
1466                 access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1467         }
1468         else if( i != p_sys->i_list_index )
1469         {
1470             p_list_access = p_access;
1471         }
1472
1473         if( p_list_access )
1474         {
1475             if( p_sys->p_list_access != p_access )
1476                 access2_Delete( p_sys->p_list_access );
1477
1478             p_sys->p_list_access = p_list_access;
1479         }
1480
1481         p_sys->i_list_index = i;
1482         return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1483                                               i_pos - i_size );
1484     }
1485
1486     return p_access->pf_seek( p_access, i_pos );
1487 }