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