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