]> git.sesse.net Git - vlc/blob - src/input/stream.c
* stream.c: fixed a problem with peek.
[vlc] / src / input / stream.c
1 /*****************************************************************************
2  * stream.c
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include <stdlib.h>
25 #include <vlc/vlc.h>
26 #include <vlc/input.h>
27
28 #include "input_internal.h"
29
30 /* 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 track we have, currently only used for stream mode */
45 #define STREAM_CACHE_TRACK 3
46 /* Max size of our cache 4Mo per track */
47 #define STREAM_CACHE_SIZE  (4*STREAM_CACHE_TRACK*1024*1024)
48   /* How many data we try to prebuffer */
49 #define STREAM_CACHE_PREBUFFER_SIZE (32767)
50 /* Maximum time we take to pre-buffer */
51 #define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
52
53
54 /* Method1: Simple, for pf_block.
55  *  We get blocks and put them in the linked list.
56  *  We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
57  */
58 #define STREAM_DATA_WAIT 40000       /* Time between before a pf_block retry */
59
60 /* Method2: A bit more complex, for pf_read
61  *  - We use ring buffers, only one if unseekable, all if seekable
62  *  - Upon seek date current ring, then search if one ring match the pos,
63  *      yes: switch to it, seek the access to match the end of the ring
64  *      no: search the ring with i_end the closer to i_pos,
65  *          if close enough, read data and use this ring
66  *          else use the oldest ring, seek and use it.
67  *
68  *  TODO: - with access non seekable: use all space available for only one ring, but
69  *          we have to support seekable/non-seekable switch on the fly.
70  *        - compute a good value for i_read_size
71  *        - ?
72  */
73 #define STREAM_READ_ATONCE 32767
74 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
75
76 typedef struct
77 {
78     int64_t i_date;
79
80     int64_t i_start;
81     int64_t i_end;
82
83     uint8_t *p_buffer;
84 } stream_track_t;
85
86 struct stream_sys_t
87 {
88     access_t    *p_access;
89
90     vlc_bool_t  b_block;    /* Block method (1) or stream */
91
92     int64_t     i_pos;      /* Current reading offset */
93
94     /* Method 1: pf_block */
95     struct
96     {
97         int64_t i_start;        /* Offset of block for p_first */
98         int     i_offset;       /* Offset for data in p_current */
99         block_t *p_current;     /* Current block */
100
101         int     i_size;         /* Total amount of data in the list */
102         block_t *p_first;
103         block_t **pp_last;
104     } block;
105
106     /* Method 2: for pf_read */
107     struct
108     {
109         int i_offset;   /* Buffer offset in the current track */
110         int i_tk;       /* Current track */
111         stream_track_t tk[STREAM_CACHE_TRACK];
112
113         /* Global buffer */
114         uint8_t *p_buffer;
115
116         /* */
117         int i_used; /* Used since last read */
118         int i_read_size;
119     } stream;
120
121     /* Peek temporary buffer */
122     int     i_peek;
123     uint8_t *p_peek;
124
125     /* Stat for both method */
126     struct
127     {
128         vlc_bool_t b_fastseek;  /* From access */
129
130         /* Stat about reading data */
131         int64_t i_read_count;
132         int64_t i_bytes;
133         int64_t i_read_time;
134
135         /* Stat about seek */
136         int     i_seek_count;
137         int64_t i_seek_time;
138     } stat;
139 };
140
141 /* Method 1: */
142 static int  AStreamReadBlock( stream_t *, void *p_read, int i_read );
143 static int  AStreamPeekBlock( stream_t *, uint8_t **p_peek, int i_read );
144 static int  AStreamSeekBlock( stream_t *s, int64_t i_pos );
145 static void AStreamPrebufferBlock( stream_t * );
146
147 /* Method 2 */
148 static int  AStreamReadStream( stream_t *, void *p_read, int i_read );
149 static int  AStreamPeekStream( stream_t *, uint8_t **pp_peek, int i_read );
150 static int  AStreamSeekStream( stream_t *s, int64_t i_pos );
151 static void AStreamPrebufferStream( stream_t * );
152
153 /* Common */
154 static int AStreamControl( stream_t *, int i_query, va_list );
155
156
157 /****************************************************************************
158  * stream_AccessNew: create a stream from a access
159  ****************************************************************************/
160 stream_t *stream_AccessNew( access_t *p_access )
161 {
162     stream_t *s = vlc_object_create( p_access, VLC_OBJECT_STREAM );
163     stream_sys_t *p_sys;
164
165     if( !s )
166         return NULL;
167
168     /* Attach it now, needed for b_die */
169     vlc_object_attach( s, p_access );
170
171     s->pf_block  = NULL;
172     s->pf_read   = NULL;    /* Set up later */
173     s->pf_peek   = NULL;
174     s->pf_control= AStreamControl;
175
176     s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
177
178     /* Common field */
179     p_sys->p_access = p_access;
180     p_sys->b_block = p_access->pf_block ? VLC_TRUE : VLC_FALSE;
181     p_sys->i_pos = p_access->info.i_pos;
182
183     /* Stats */
184     access2_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
185     p_sys->stat.i_bytes = 0;
186     p_sys->stat.i_read_time = 0;
187     p_sys->stat.i_read_count = 0;
188     p_sys->stat.i_seek_count = 0;
189     p_sys->stat.i_seek_time = 0;
190
191     /* Peek */
192     p_sys->i_peek = 0;
193     p_sys->p_peek = NULL;
194
195     if( p_sys->b_block )
196     {
197         s->pf_read = AStreamReadBlock;
198         s->pf_peek = AStreamPeekBlock;
199
200         /* Init all fields of p_sys->block */
201         p_sys->block.i_start = p_sys->i_pos;
202         p_sys->block.i_offset = 0;
203         p_sys->block.p_current = NULL;
204         p_sys->block.i_size = 0;
205         p_sys->block.p_first = NULL;
206         p_sys->block.pp_last = &p_sys->block.p_first;
207
208         /* Do the prebuffering */
209         AStreamPrebufferBlock( s );
210
211         if( p_sys->block.i_size <= 0 )
212         {
213             msg_Err( s, "cannot pre fill buffer" );
214             goto error;
215         }
216     }
217     else
218     {
219         int i;
220
221         s->pf_read = AStreamReadStream;
222         s->pf_peek = AStreamPeekStream;
223
224         /* Allocate/Setup our tracks */
225         p_sys->stream.i_offset = 0;
226         p_sys->stream.i_tk     = 0;
227         p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
228         p_sys->stream.i_used   = 0;
229         access2_Control( p_access, ACCESS_GET_MTU,
230                          &p_sys->stream.i_read_size );
231         if( p_sys->stream.i_read_size <= 0 )
232             p_sys->stream.i_read_size = STREAM_READ_ATONCE;
233         else if( p_sys->stream.i_read_size <= 256 )
234             p_sys->stream.i_read_size = 256;
235
236         for( i = 0; i < STREAM_CACHE_TRACK; i++ )
237         {
238             p_sys->stream.tk[i].i_date  = 0;
239             p_sys->stream.tk[i].i_start = p_sys->i_pos;
240             p_sys->stream.tk[i].i_end   = p_sys->i_pos;
241             p_sys->stream.tk[i].p_buffer=
242                 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
243         }
244
245         /* Do the prebuffering */
246         AStreamPrebufferStream( s );
247
248         if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
249         {
250             msg_Err( s, "cannot pre fill buffer" );
251             goto error;
252         }
253     }
254
255     return s;
256
257 error:
258     if( p_sys->b_block )
259     {
260         /* Nothing yet */
261     }
262     else
263     {
264         free( p_sys->stream.p_buffer );
265     }
266     free( s->p_sys );
267     vlc_object_detach( s );
268     vlc_object_destroy( s );
269     return NULL;
270 }
271
272 /****************************************************************************
273  * stream_AccessDelete:
274  ****************************************************************************/
275 void stream_AccessDelete( stream_t *s )
276 {
277     stream_sys_t *p_sys = s->p_sys;
278
279     vlc_object_detach( s );
280
281     if( p_sys->b_block )
282     {
283         block_ChainRelease( p_sys->block.p_first );
284     }
285     else
286     {
287         free( p_sys->stream.p_buffer );
288     }
289
290     if( p_sys->p_peek )
291         free( p_sys->p_peek );
292
293     free( s->p_sys );
294     vlc_object_destroy( s );
295 }
296
297 /****************************************************************************
298  * stream_AccessReset:
299  ****************************************************************************/
300 void stream_AccessReset( stream_t *s )
301 {
302     stream_sys_t *p_sys = s->p_sys;
303
304     p_sys->i_pos = p_sys->p_access->info.i_pos;
305
306     if( p_sys->b_block )
307     {
308         block_ChainRelease( p_sys->block.p_first );
309
310         /* Init all fields of p_sys->block */
311         p_sys->block.i_start = p_sys->i_pos;
312         p_sys->block.i_offset = 0;
313         p_sys->block.p_current = NULL;
314         p_sys->block.i_size = 0;
315         p_sys->block.p_first = NULL;
316         p_sys->block.pp_last = &p_sys->block.p_first;
317
318         /* Do the prebuffering */
319         AStreamPrebufferBlock( s );
320     }
321     else
322     {
323         int i;
324
325         /* Setup our tracks */
326         p_sys->stream.i_offset = 0;
327         p_sys->stream.i_tk     = 0;
328         p_sys->stream.i_used   = 0;
329
330         for( i = 0; i < STREAM_CACHE_TRACK; i++ )
331         {
332             p_sys->stream.tk[i].i_date  = 0;
333             p_sys->stream.tk[i].i_start = p_sys->i_pos;
334             p_sys->stream.tk[i].i_end   = p_sys->i_pos;
335         }
336
337         /* Do the prebuffering */
338         AStreamPrebufferStream( s );
339     }
340 }
341
342 /****************************************************************************
343  * AStreamControl:
344  ****************************************************************************/
345 static int AStreamControl( stream_t *s, int i_query, va_list args )
346 {
347     stream_sys_t *p_sys = s->p_sys;
348     access_t     *p_access = p_sys->p_access;
349
350     vlc_bool_t *p_bool;
351     int64_t    *pi_64, i_64;
352     int        i_int;
353
354     switch( i_query )
355     {
356         case STREAM_GET_SIZE:
357             pi_64 = (int64_t*)va_arg( args, int64_t * );
358             *pi_64 = p_access->info.i_size;
359             break;
360
361         case STREAM_CAN_SEEK:
362             p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
363             access2_Control( p_access, ACCESS_CAN_SEEK, p_bool );
364             break;
365
366         case STREAM_CAN_FASTSEEK:
367             p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
368             access2_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
369             break;
370
371         case STREAM_GET_POSITION:
372             pi_64 = (int64_t*)va_arg( args, int64_t * );
373             *pi_64 = p_sys->i_pos;
374             break;
375
376         case STREAM_SET_POSITION:
377             i_64 = (int64_t)va_arg( args, int64_t );
378             if( p_sys->b_block )
379                 return AStreamSeekBlock( s, i_64 );
380             else
381                 return AStreamSeekStream( s, i_64 );
382
383         case STREAM_GET_MTU:
384             return VLC_EGENERIC;
385
386         case STREAM_CONTROL_ACCESS:
387             i_int = (int) va_arg( args, int );
388             if( i_int != ACCESS_SET_PRIVATE_ID_STATE )
389             {
390                 msg_Err( s, "Hey, what are you thinking ?"
391                             "DON'T USE STREAM_CONTROL_ACCESS !!!" );
392                 return VLC_EGENERIC;
393             }
394             return access2_Control( p_access, i_int, args );
395
396         default:
397             msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
398             return VLC_EGENERIC;
399     }
400     return VLC_SUCCESS;
401 }
402
403
404
405 /****************************************************************************
406  * Method 1:
407  ****************************************************************************/
408 static void AStreamPrebufferBlock( stream_t *s )
409 {
410     stream_sys_t *p_sys = s->p_sys;
411     access_t     *p_access = p_sys->p_access;
412
413     int64_t i_first = 0;
414     int64_t i_start;
415
416     msg_Dbg( s, "pre buffering" );
417     i_start = mdate();
418     for( ;; )
419     {
420         int64_t i_date = mdate();
421         block_t *b;
422
423         if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
424             ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
425         {
426             int64_t i_byterate;
427
428             /* Update stat */
429             p_sys->stat.i_bytes = p_sys->block.i_size;
430             p_sys->stat.i_read_time = i_date - i_start;
431             i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
432                          (p_sys->stat.i_read_time + 1);
433
434             msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
435                      I64Fd" kbytes/s",
436                      p_sys->stat.i_bytes,
437                      p_sys->stat.i_read_time / I64C(1000000),
438                      i_byterate / 1024 );
439             break;
440         }
441
442         /* Fetch a block */
443         if( ( b = p_access->pf_block( p_access ) ) == NULL )
444         {
445             if( p_access->info.b_eof )
446                 break;
447
448             msleep( STREAM_DATA_WAIT );
449             continue;
450         }
451
452         if( i_first == 0 )
453             i_first = mdate();
454
455         /* Append the block */
456         p_sys->block.i_size += b->i_buffer;
457         *p_sys->block.pp_last = b;
458         p_sys->block.pp_last = &b->p_next;
459
460         p_sys->stat.i_read_count++;
461     }
462
463     p_sys->block.p_current = p_sys->block.p_first;
464 }
465
466 static int AStreamRefillBlock( stream_t *s );
467
468 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
469 {
470     stream_sys_t *p_sys = s->p_sys;
471
472     uint8_t *p_data= (uint8_t*)p_read;
473     int     i_data = 0;
474
475     /* It means EOF */
476     if( p_sys->block.p_current == NULL )
477         return 0;
478
479     while( i_data < i_read )
480     {
481         int i_current =
482             p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
483         int i_copy = __MIN( i_current, i_read - i_data);
484
485         /* Copy data */
486         if( p_data )
487         {
488             memcpy( p_data,
489                     &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
490                     i_copy );
491             p_data += i_copy;
492         }
493         i_data += i_copy;
494
495         p_sys->block.i_offset += i_copy;
496         if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
497         {
498             /* Current block is now empty, switch to next */
499             if( p_sys->block.p_current )
500             {
501                 p_sys->block.i_offset = 0;
502                 p_sys->block.p_current = p_sys->block.p_current->p_next;
503             }
504             /*Get a new block */
505             if( AStreamRefillBlock( s ) )
506             {
507                 break;
508             }
509         }
510     }
511
512     p_sys->i_pos += i_data;
513     return i_data;
514 }
515
516 static int AStreamPeekBlock( stream_t *s, uint8_t **pp_peek, int i_read )
517 {
518     stream_sys_t *p_sys = s->p_sys;
519     uint8_t *p_data;
520     int      i_data = 0;
521     block_t *b;
522     int      i_offset;
523
524     if( p_sys->block.p_current == NULL ) return 0; /* EOF */
525
526     /* We can directly give a pointer over our buffer */
527     if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
528     {
529         *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
530         return i_read;
531     }
532
533     /* We need to create a local copy */
534     if( p_sys->i_peek < i_read )
535     {
536         if( p_sys->p_peek )
537             free( p_sys->p_peek );
538         p_sys->i_peek = i_read;
539         p_sys->p_peek = malloc( p_sys->i_peek );
540     }
541
542     /* Fill enough data */
543     while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
544            < i_read )
545     {
546         block_t **pp_last = p_sys->block.pp_last;
547
548         if( AStreamRefillBlock( s ) )
549             break;
550
551         /* Our buffer are probably filled enough, don't try anymore */
552         if( pp_last == p_sys->block.pp_last )
553             break;
554     }
555
556     /* Copy what we have */
557     b = p_sys->block.p_current;
558     i_offset = p_sys->block.i_offset;
559     p_data = p_sys->p_peek;
560
561     while( b && i_data < i_read )
562     {
563         int i_current = b->i_buffer - i_offset;
564         int i_copy = __MIN( i_current, i_read - i_data );
565
566         memcpy( p_data, &b->p_buffer[i_offset], i_copy );
567         i_data += i_copy;
568         p_data += i_copy;
569         i_offset += i_copy;
570
571         if( i_offset >= b->i_buffer )
572         {
573             i_offset = 0;
574             b = b->p_next;
575         }
576     }
577
578     *pp_peek = p_sys->p_peek;
579     return i_data;
580 }
581
582 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
583 {
584     stream_sys_t *p_sys = s->p_sys;
585     access_t   *p_access = p_sys->p_access;
586     int64_t    i_offset = i_pos - p_sys->block.i_start;
587     vlc_bool_t b_seek;
588
589     /* We already have thoses data, just update p_current/i_offset */
590     if( i_offset >= 0 && i_offset < p_sys->block.i_size )
591     {
592         block_t *b = p_sys->block.p_first;
593         int i_current = 0;
594
595         while( i_current + b->i_buffer < i_offset )
596         {
597             i_current += b->i_buffer;
598             b = b->p_next;
599         }
600
601         p_sys->block.p_current = b;
602         p_sys->block.i_offset = i_offset - i_current;
603
604         p_sys->i_pos = i_pos;
605
606         return VLC_SUCCESS;
607     }
608
609     /* We may need to seek or to read data */
610     if( i_offset < 0 )
611     {
612         vlc_bool_t b_aseek;
613         access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
614
615         if( !b_aseek )
616         {
617             msg_Err( s, "backward seek impossible (access non seekable)" );
618             return VLC_EGENERIC;
619         }
620
621         b_seek = VLC_TRUE;
622     }
623     else
624     {
625         vlc_bool_t b_aseek, b_aseekfast;
626
627         access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
628         access2_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
629
630         if( !b_aseek )
631         {
632             b_seek = VLC_FALSE;
633             msg_Warn( s, I64Fd" bytes need to be skipped "
634                       "(access non seekable)",
635                       i_offset - p_sys->block.i_size );
636         }
637         else
638         {
639             int64_t i_skip = i_offset - p_sys->block.i_size;
640
641             /* Avg bytes per packets */
642             int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
643             /* TODO compute a seek cost instead of fixed threshold */
644             int i_th = b_aseekfast ? 1 : 5;
645
646             if( i_skip <= i_th * i_avg &&
647                 i_skip < STREAM_CACHE_SIZE )
648                 b_seek = VLC_FALSE;
649             else
650                 b_seek = VLC_TRUE;
651
652             msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
653                      b_seek, i_th*i_avg, i_skip );
654         }
655     }
656
657     if( b_seek )
658     {
659         int64_t i_start, i_end;
660         /* Do the access seek */
661         i_start = mdate();
662         if( p_access->pf_seek( p_access, i_pos ) )
663             return VLC_EGENERIC;
664         i_end = mdate();
665
666         /* Release data */
667         block_ChainRelease( p_sys->block.p_first );
668
669         /* Reinit */
670         p_sys->block.i_start = p_sys->i_pos = i_pos;
671         p_sys->block.i_offset = 0;
672         p_sys->block.p_current = NULL;
673         p_sys->block.i_size = 0;
674         p_sys->block.p_first = NULL;
675         p_sys->block.pp_last = &p_sys->block.p_first;
676
677         /* Refill a block */
678         if( AStreamRefillBlock( s ) )
679         {
680             msg_Err( s, "cannot re fill buffer" );
681             return VLC_EGENERIC;
682         }
683         /* Update stat */
684         p_sys->stat.i_seek_time += i_end - i_start;
685         p_sys->stat.i_seek_count++;
686         return VLC_SUCCESS;
687     }
688     else
689     {
690         /* Read enought data */
691         while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
692         {
693             if( AStreamRefillBlock( s ) )
694             {
695                 msg_Err( s, "can't read enough data in seek" );
696                 return VLC_EGENERIC;
697             }
698             while( p_sys->block.p_current &&
699                    p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
700             {
701                 p_sys->i_pos += p_sys->block.p_current->i_buffer;
702                 p_sys->block.p_current = p_sys->block.p_current->p_next;
703             }
704         }
705
706         p_sys->block.i_offset = i_pos - p_sys->i_pos;
707         p_sys->i_pos = i_pos;
708
709         /* TODO read data */
710         return VLC_SUCCESS;
711     }
712
713     return VLC_EGENERIC;
714 }
715
716 static int AStreamRefillBlock( stream_t *s )
717 {
718     stream_sys_t *p_sys = s->p_sys;
719     access_t     *p_access = p_sys->p_access;
720     int64_t      i_start, i_stop;
721     block_t      *b;
722
723     /* Release data */
724     while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
725            p_sys->block.p_first != p_sys->block.p_current )
726     {
727         block_t *b = p_sys->block.p_first;
728
729         p_sys->block.i_start += b->i_buffer;
730         p_sys->block.i_size  -= b->i_buffer;
731         p_sys->block.p_first  = b->p_next;
732
733         block_Release( b );
734     }
735     if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
736         p_sys->block.p_current == p_sys->block.p_first &&
737         p_sys->block.p_current->p_next )    /* At least 2 packets */
738     {
739         /* Enough data, don't read more */
740         return VLC_SUCCESS;
741     }
742
743     /* Now read a new block */
744     i_start = mdate();
745     for( ;; )
746     {
747         if( s->b_die )
748             return VLC_EGENERIC;
749
750
751         /* Fetch a block */
752         if( ( b = p_access->pf_block( p_access ) ) )
753             break;
754
755         if( p_access->info.b_eof )
756             return VLC_EGENERIC;
757
758         msleep( STREAM_DATA_WAIT );
759     }
760     i_stop = mdate();
761
762     /* Append the block */
763     p_sys->block.i_size += b->i_buffer;
764     *p_sys->block.pp_last = b;
765     p_sys->block.pp_last = &b->p_next;
766
767     /* Fix p_current */
768     if( p_sys->block.p_current == NULL )
769         p_sys->block.p_current = b;
770
771     /* Update stat */
772     p_sys->stat.i_bytes += b->i_buffer;
773     p_sys->stat.i_read_time += i_stop - i_start;
774     p_sys->stat.i_read_count++;
775
776     return VLC_SUCCESS;
777 }
778
779
780 /****************************************************************************
781  * Method 2:
782  ****************************************************************************/
783 static int AStreamRefillStream( stream_t *s );
784
785 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
786 {
787     stream_sys_t *p_sys = s->p_sys;
788     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
789
790     uint8_t *p_data = (uint8_t *)p_read;
791     int      i_data = 0;
792
793     if( tk->i_start >= tk->i_end ) return 0; /* EOF */
794
795 #if 0
796     msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
797              " offset=%d end="I64Fd,
798              i_read, p_sys->i_pos, p_sys->stream.i_tk,
799              tk->i_start, p_sys->stream.i_offset, tk->i_end );
800 #endif
801
802     while( i_data < i_read )
803     {
804         int i_off = (tk->i_start + p_sys->stream.i_offset) %
805                     STREAM_CACHE_TRACK_SIZE;
806         int i_current =
807             __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
808                    STREAM_CACHE_TRACK_SIZE - i_off );
809         int i_copy = __MIN( i_current, i_read - i_data );
810
811         if( i_copy <= 0 ) break; /* EOF */
812
813         /* Copy data */
814         /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
815         if( p_data )
816         {
817             memcpy( p_data, &tk->p_buffer[i_off], i_copy );
818             p_data += i_copy;
819         }
820         i_data += i_copy;
821         p_sys->stream.i_offset += i_copy;
822
823         /* Update pos now */
824         p_sys->i_pos += i_copy;
825
826         /* */
827         p_sys->stream.i_used += i_copy;
828         if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
829             p_sys->stream.i_used >= p_sys->stream.i_read_size )
830         {
831             if( AStreamRefillStream( s ) )
832             {
833                 /* EOF */
834                 if( tk->i_start >= tk->i_end ) break;
835             }
836         }
837     }
838
839     return i_data;
840 }
841
842 static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read )
843 {
844     stream_sys_t *p_sys = s->p_sys;
845     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
846     int64_t i_off;
847
848     if( tk->i_start >= tk->i_end ) return 0; /* EOF */
849
850 #if 0
851     msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
852              "start="I64Fd" offset=%d end="I64Fd,
853              i_read, p_sys->i_pos, p_sys->stream.i_tk,
854              tk->i_start, p_sys->stream.i_offset, tk->i_end );
855 #endif
856
857     /* Avoid problem, but that should *never* happen */
858     if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
859         i_read = STREAM_CACHE_TRACK_SIZE / 2;
860
861     while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
862     {
863         if( p_sys->stream.i_used <= 1 )
864         {
865             /* Be sure we will read something */
866             p_sys->stream.i_used += i_read - (tk->i_end - tk->i_start - p_sys->stream.i_offset);
867         }
868         if( AStreamRefillStream( s ) )
869             break;
870     }
871
872     if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
873         i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
874
875     /* Now, direct pointer or a copy ? */
876     i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
877     if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
878     {
879         *pp_peek = &tk->p_buffer[i_off];
880         return i_read;
881     }
882
883     if( p_sys->i_peek < i_read )
884     {
885         if( p_sys->p_peek ) free( p_sys->p_peek );
886         p_sys->i_peek = i_read;
887         p_sys->p_peek = malloc( i_read );
888     }
889
890     memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
891             STREAM_CACHE_TRACK_SIZE - i_off );
892     memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
893             &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
894
895     *pp_peek = p_sys->p_peek;
896     return i_read;
897 }
898
899 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
900 {
901     stream_sys_t *p_sys = s->p_sys;
902     access_t     *p_access = p_sys->p_access;
903     vlc_bool_t   b_aseek;
904     vlc_bool_t   b_afastseek;
905     int i_maxth;
906     int i_new;
907     int i;
908
909 #if 0
910     msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
911              "tk=%d start="I64Fd" offset=%d end="I64Fd,
912              i_pos, p_sys->i_pos, p_sys->stream.i_tk,
913              p_sys->stream.tk[p_sys->stream.i_tk].i_start,
914              p_sys->stream.i_offset,
915              p_sys->stream.tk[p_sys->stream.i_tk].i_end );
916 #endif
917
918
919     /* Seek in our current track ? */
920     if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
921         i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
922     {
923         //msg_Dbg( s, "AStreamSeekStream: current track" );
924         p_sys->i_pos = i_pos;
925         p_sys->stream.i_offset = i_pos - p_sys->stream.tk[p_sys->stream.i_tk].i_start;
926         return VLC_SUCCESS;
927     }
928
929     access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
930     if( !b_aseek )
931     {
932         /* We can't do nothing */
933         return VLC_EGENERIC;
934     }
935
936     /* Date the current track */
937     p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
938
939     /* Try to reuse already read data */
940     for( i = 0; i < STREAM_CACHE_TRACK; i++ )
941     {
942         stream_track_t *tk = &p_sys->stream.tk[i];
943
944         if( i_pos >= tk->i_start && i_pos <= tk->i_end )
945         {
946 #if 0
947             msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
948                      " end="I64Fd, i, tk->i_start, tk->i_end );
949 #endif
950             /* Seek at the end of the buffer */
951             if( p_access->pf_seek( p_access, tk->i_end ) )
952                 return VLC_EGENERIC;
953
954             /* That's it */
955             p_sys->i_pos = i_pos;
956             p_sys->stream.i_tk = i;
957             p_sys->stream.i_offset = i_pos - tk->i_start;
958
959             if( p_sys->stream.i_used < 1024 )
960                 p_sys->stream.i_used = 1024;
961
962             if( AStreamRefillStream( s ) )
963                 return VLC_EGENERIC;
964
965             return VLC_SUCCESS;
966         }
967     }
968
969     access2_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
970     /* FIXME compute seek cost (instead of static 'stupid' value) */
971     i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
972     if( !b_afastseek )
973         i_maxth *= 3;
974
975     /* FIXME TODO */
976 #if 0
977     /* Search closest segment TODO */
978     for( i = 0; i < STREAM_CACHE_TRACK; i++ )
979     {
980         stream_track_t *tk = &p_sys->stream.tk[i];
981
982         if( i_pos + i_maxth >= tk->i_start )
983         {
984             msg_Dbg( s, "good segment before current pos, TODO" );
985         }
986         if( i_pos - i_maxth <= tk->i_end )
987         {
988             msg_Dbg( s, "good segment after current pos, TODO" );
989         }
990     }
991 #endif
992
993     /* Nothing good, seek and choose oldest segment */
994     if( p_access->pf_seek( p_access, i_pos ) )
995         return VLC_EGENERIC;
996     p_sys->i_pos = i_pos;
997
998     i_new = 0;
999     for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1000     {
1001         if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1002             i_new = i;
1003     }
1004
1005     /* Reset the segment */
1006     p_sys->stream.i_tk     = i_new;
1007     p_sys->stream.i_offset =  0;
1008     p_sys->stream.tk[i_new].i_start = i_pos;
1009     p_sys->stream.tk[i_new].i_end   = i_pos;
1010
1011     /* Read data */
1012     if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1013         p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1014
1015     if( AStreamRefillStream( s ) )
1016         return VLC_EGENERIC;
1017
1018     return VLC_SUCCESS;
1019 }
1020
1021 static int AStreamRefillStream( stream_t *s )
1022 {
1023     stream_sys_t *p_sys = s->p_sys;
1024     access_t     *p_access = p_sys->p_access;
1025     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1026
1027     /* We read but won't increase i_start after initial start + offset */
1028     int i_toread =
1029         __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1030                (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1031     int64_t i_start, i_stop;
1032
1033     if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1034
1035     /* msg_Dbg( s, "AStreamRefillStream: toread=%d", i_toread ); */
1036
1037     i_start = mdate();
1038     while( i_toread > 0 )
1039     {
1040         int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1041         int i_read;
1042
1043         if( s->b_die )
1044             return VLC_EGENERIC;
1045
1046         i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1047         i_read = p_access->pf_read( p_access, &tk->p_buffer[i_off], i_read );
1048
1049         /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1050         if( i_read <  0 )
1051         {
1052             msleep( STREAM_DATA_WAIT );
1053             continue;
1054         }
1055         else if( i_read == 0 )
1056         {
1057             return VLC_EGENERIC;
1058         }
1059
1060         /* Update end */
1061         tk->i_end += i_read;
1062
1063         /* Windows of STREAM_CACHE_TRACK_SIZE */
1064         if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1065         {
1066             int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1067
1068             tk->i_start += i_invalid;
1069             p_sys->stream.i_offset -= i_invalid;
1070         }
1071
1072         i_toread -= i_read;
1073         p_sys->stream.i_used -= i_read;
1074
1075         p_sys->stat.i_bytes += i_read;
1076         p_sys->stat.i_read_count++;
1077     }
1078     i_stop = mdate();
1079
1080     p_sys->stat.i_read_time += i_stop - i_start;
1081
1082     return VLC_SUCCESS;
1083 }
1084
1085 static void AStreamPrebufferStream( stream_t *s )
1086 {
1087     stream_sys_t *p_sys = s->p_sys;
1088     access_t     *p_access = p_sys->p_access;
1089
1090     int64_t i_first = 0;
1091     int64_t i_start;
1092
1093     msg_Dbg( s, "pre buffering" );
1094     i_start = mdate();
1095     for( ;; )
1096     {
1097         stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1098
1099         int64_t i_date = mdate();
1100         int i_read;
1101
1102         if( s->b_die || tk->i_end >= STREAM_CACHE_PREBUFFER_SIZE ||
1103             (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1104         {
1105             int64_t i_byterate;
1106
1107             /* Update stat */
1108             p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1109             p_sys->stat.i_read_time = i_date - i_start;
1110             i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
1111                          (p_sys->stat.i_read_time+1);
1112
1113             msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
1114                      I64Fd" kbytes/s",
1115                      p_sys->stat.i_bytes,
1116                      p_sys->stat.i_read_time / I64C(1000000),
1117                      i_byterate / 1024 );
1118             break;
1119         }
1120
1121         /* */
1122         i_read = __MIN( p_sys->stream.i_read_size,
1123                         STREAM_CACHE_TRACK_SIZE - tk->i_end );
1124         i_read = p_access->pf_read( p_access, &tk->p_buffer[tk->i_end],
1125                                     i_read );
1126         if( i_read <  0 )
1127         {
1128             msleep( STREAM_DATA_WAIT );
1129             continue;
1130         }
1131         else if( i_read == 0 )
1132         {
1133             /* EOF */
1134             break;
1135         }
1136
1137         if( i_first == 0 )
1138             i_first = mdate();
1139
1140         tk->i_end += i_read;
1141
1142         p_sys->stat.i_read_count++;
1143     }
1144 }
1145
1146
1147 /****************************************************************************
1148  * stream_ReadLine:
1149  ****************************************************************************/
1150 /**
1151  * Read from the stream untill first newline.
1152  * \param s Stream handle to read from
1153  * \return A null-terminated string. This must be freed,
1154  */
1155 /* FIXME don't use stupid MAX_LINE -> do the same than net_ReadLine */
1156 #define MAX_LINE 1024
1157 char *stream_ReadLine( stream_t *s )
1158 {
1159     uint8_t *p_data;
1160     char    *p_line;
1161     int      i_data;
1162     int      i = 0;
1163     i_data = stream_Peek( s, &p_data, MAX_LINE );
1164
1165     while( i < i_data && p_data[i] != '\n' &&  p_data[i] != '\r' )
1166     {
1167         i++;
1168     }
1169     if( i_data <= 0 )
1170     {
1171         return NULL;
1172     }
1173     else
1174     {
1175         p_line = malloc( i + 1 );
1176         if( p_line == NULL )
1177         {
1178             msg_Err( s, "out of memory" );
1179             return NULL;
1180         }
1181         i = stream_Read( s, p_line, i + 1 );
1182         p_line[ i - 1 ] = '\0';
1183
1184         return p_line;
1185     }
1186 }