1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
26 #include <vlc/input.h>
28 #include "input_internal.h"
31 * - tune the 2 methods
32 * - compute cost for seek
33 * - improve stream mode seeking with closest segments
39 * One linked list of data read
41 * More complex scheme using mutliple track to avoid seeking
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)
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
58 #define STREAM_DATA_WAIT 40000 /* Time between before a pf_block retry */
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.
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
73 #define STREAM_READ_ATONCE 32767
74 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
90 vlc_bool_t b_block; /* Block method (1) or stream */
92 int64_t i_pos; /* Current reading offset */
94 /* Method 1: pf_block */
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 */
101 int i_size; /* Total amount of data in the list */
106 /* Method 2: for pf_read */
109 int i_offset; /* Buffer offset in the current track */
110 int i_tk; /* Current track */
111 stream_track_t tk[STREAM_CACHE_TRACK];
117 int i_used; /* Used since last read */
121 /* Peek temporary buffer */
125 /* Stat for both method */
128 vlc_bool_t b_fastseek; /* From access */
130 /* Stat about reading data */
131 int64_t i_read_count;
135 /* Stat about seek */
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 * );
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 * );
154 static int AStreamControl( stream_t *, int i_query, va_list );
157 /****************************************************************************
158 * stream_AccessNew: create a stream from a access
159 ****************************************************************************/
160 stream_t *stream_AccessNew( access_t *p_access )
162 stream_t *s = vlc_object_create( p_access, VLC_OBJECT_STREAM );
168 /* Attach it now, needed for b_die */
169 vlc_object_attach( s, p_access );
172 s->pf_read = NULL; /* Set up later */
174 s->pf_control= AStreamControl;
176 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
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;
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;
193 p_sys->p_peek = NULL;
197 s->pf_read = AStreamReadBlock;
198 s->pf_peek = AStreamPeekBlock;
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;
208 /* Do the prebuffering */
209 AStreamPrebufferBlock( s );
211 if( p_sys->block.i_size <= 0 )
213 msg_Err( s, "cannot pre fill buffer" );
221 s->pf_read = AStreamReadStream;
222 s->pf_peek = AStreamPeekStream;
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;
236 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
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];
245 /* Do the prebuffering */
246 AStreamPrebufferStream( s );
248 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
250 msg_Err( s, "cannot pre fill buffer" );
264 free( p_sys->stream.p_buffer );
267 vlc_object_detach( s );
268 vlc_object_destroy( s );
272 /****************************************************************************
273 * stream_AccessDelete:
274 ****************************************************************************/
275 void stream_AccessDelete( stream_t *s )
277 stream_sys_t *p_sys = s->p_sys;
279 vlc_object_detach( s );
283 block_ChainRelease( p_sys->block.p_first );
287 free( p_sys->stream.p_buffer );
291 free( p_sys->p_peek );
294 vlc_object_destroy( s );
297 /****************************************************************************
298 * stream_AccessReset:
299 ****************************************************************************/
300 void stream_AccessReset( stream_t *s )
302 stream_sys_t *p_sys = s->p_sys;
304 p_sys->i_pos = p_sys->p_access->info.i_pos;
308 block_ChainRelease( p_sys->block.p_first );
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;
318 /* Do the prebuffering */
319 AStreamPrebufferBlock( s );
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;
330 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
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;
337 /* Do the prebuffering */
338 AStreamPrebufferStream( s );
342 /****************************************************************************
344 ****************************************************************************/
345 static int AStreamControl( stream_t *s, int i_query, va_list args )
347 stream_sys_t *p_sys = s->p_sys;
348 access_t *p_access = p_sys->p_access;
351 int64_t *pi_64, i_64;
356 case STREAM_GET_SIZE:
357 pi_64 = (int64_t*)va_arg( args, int64_t * );
358 *pi_64 = p_access->info.i_size;
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 );
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 );
371 case STREAM_GET_POSITION:
372 pi_64 = (int64_t*)va_arg( args, int64_t * );
373 *pi_64 = p_sys->i_pos;
376 case STREAM_SET_POSITION:
377 i_64 = (int64_t)va_arg( args, int64_t );
379 return AStreamSeekBlock( s, i_64 );
381 return AStreamSeekStream( s, i_64 );
386 case STREAM_CONTROL_ACCESS:
387 i_int = (int) va_arg( args, int );
388 if( i_int != ACCESS_SET_PRIVATE_ID_STATE )
390 msg_Err( s, "Hey, what are you thinking ?"
391 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
394 return access2_Control( p_access, i_int, args );
397 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
405 /****************************************************************************
407 ****************************************************************************/
408 static void AStreamPrebufferBlock( stream_t *s )
410 stream_sys_t *p_sys = s->p_sys;
411 access_t *p_access = p_sys->p_access;
416 msg_Dbg( s, "pre buffering" );
420 int64_t i_date = mdate();
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 ) )
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);
434 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
437 p_sys->stat.i_read_time / I64C(1000000),
443 if( ( b = p_access->pf_block( p_access ) ) == NULL )
445 if( p_access->info.b_eof )
448 msleep( STREAM_DATA_WAIT );
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;
460 p_sys->stat.i_read_count++;
463 p_sys->block.p_current = p_sys->block.p_first;
466 static int AStreamRefillBlock( stream_t *s );
468 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
470 stream_sys_t *p_sys = s->p_sys;
472 uint8_t *p_data= (uint8_t*)p_read;
476 if( p_sys->block.p_current == NULL )
479 while( i_data < i_read )
482 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
483 int i_copy = __MIN( i_current, i_read - i_data);
489 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
495 p_sys->block.i_offset += i_copy;
496 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
498 /* Current block is now empty, switch to next */
499 if( p_sys->block.p_current )
501 p_sys->block.i_offset = 0;
502 p_sys->block.p_current = p_sys->block.p_current->p_next;
505 if( AStreamRefillBlock( s ) )
512 p_sys->i_pos += i_data;
516 static int AStreamPeekBlock( stream_t *s, uint8_t **pp_peek, int i_read )
518 stream_sys_t *p_sys = s->p_sys;
524 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
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 )
529 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
533 /* We need to create a local copy */
534 if( p_sys->i_peek < i_read )
537 free( p_sys->p_peek );
538 p_sys->i_peek = i_read;
539 p_sys->p_peek = malloc( p_sys->i_peek );
542 /* Fill enough data */
543 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
546 block_t **pp_last = p_sys->block.pp_last;
548 if( AStreamRefillBlock( s ) )
551 /* Our buffer are probably filled enough, don't try anymore */
552 if( pp_last == p_sys->block.pp_last )
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;
561 while( b && i_data < i_read )
563 int i_current = b->i_buffer - i_offset;
564 int i_copy = __MIN( i_current, i_read - i_data );
566 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
571 if( i_offset >= b->i_buffer )
578 *pp_peek = p_sys->p_peek;
582 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
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;
589 /* We already have thoses data, just update p_current/i_offset */
590 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
592 block_t *b = p_sys->block.p_first;
595 while( i_current + b->i_buffer < i_offset )
597 i_current += b->i_buffer;
601 p_sys->block.p_current = b;
602 p_sys->block.i_offset = i_offset - i_current;
604 p_sys->i_pos = i_pos;
609 /* We may need to seek or to read data */
613 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
617 msg_Err( s, "backward seek impossible (access non seekable)" );
625 vlc_bool_t b_aseek, b_aseekfast;
627 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
628 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
633 msg_Warn( s, I64Fd" bytes need to be skipped "
634 "(access non seekable)",
635 i_offset - p_sys->block.i_size );
639 int64_t i_skip = i_offset - p_sys->block.i_size;
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;
646 if( i_skip <= i_th * i_avg &&
647 i_skip < STREAM_CACHE_SIZE )
652 msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
653 b_seek, i_th*i_avg, i_skip );
659 int64_t i_start, i_end;
660 /* Do the access seek */
662 if( p_access->pf_seek( p_access, i_pos ) )
667 block_ChainRelease( p_sys->block.p_first );
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;
678 if( AStreamRefillBlock( s ) )
680 msg_Err( s, "cannot re fill buffer" );
684 p_sys->stat.i_seek_time += i_end - i_start;
685 p_sys->stat.i_seek_count++;
690 /* Read enought data */
691 while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
693 if( AStreamRefillBlock( s ) )
695 msg_Err( s, "can't read enough data in seek" );
698 while( p_sys->block.p_current &&
699 p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
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;
706 p_sys->block.i_offset = i_pos - p_sys->i_pos;
707 p_sys->i_pos = i_pos;
716 static int AStreamRefillBlock( stream_t *s )
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;
724 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
725 p_sys->block.p_first != p_sys->block.p_current )
727 block_t *b = p_sys->block.p_first;
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;
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 */
739 /* Enough data, don't read more */
743 /* Now read a new block */
752 if( ( b = p_access->pf_block( p_access ) ) )
755 if( p_access->info.b_eof )
758 msleep( STREAM_DATA_WAIT );
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;
768 if( p_sys->block.p_current == NULL )
769 p_sys->block.p_current = b;
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++;
780 /****************************************************************************
782 ****************************************************************************/
783 static int AStreamRefillStream( stream_t *s );
785 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
787 stream_sys_t *p_sys = s->p_sys;
788 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
790 uint8_t *p_data = (uint8_t *)p_read;
793 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
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 );
802 while( i_data < i_read )
804 int i_off = (tk->i_start + p_sys->stream.i_offset) %
805 STREAM_CACHE_TRACK_SIZE;
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 );
811 if( i_copy <= 0 ) break; /* EOF */
814 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
817 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
821 p_sys->stream.i_offset += i_copy;
824 p_sys->i_pos += i_copy;
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 )
831 if( AStreamRefillStream( s ) )
834 if( tk->i_start >= tk->i_end ) break;
842 static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read )
844 stream_sys_t *p_sys = s->p_sys;
845 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
848 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
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 );
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;
861 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
863 if( p_sys->stream.i_used <= 1 )
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);
868 if( AStreamRefillStream( s ) )
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;
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 )
879 *pp_peek = &tk->p_buffer[i_off];
883 if( p_sys->i_peek < i_read )
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 );
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) );
895 *pp_peek = p_sys->p_peek;
899 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
901 stream_sys_t *p_sys = s->p_sys;
902 access_t *p_access = p_sys->p_access;
904 vlc_bool_t b_afastseek;
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 );
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 )
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;
929 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
932 /* We can't do nothing */
936 /* Date the current track */
937 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
939 /* Try to reuse already read data */
940 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
942 stream_track_t *tk = &p_sys->stream.tk[i];
944 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
947 msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
948 " end="I64Fd, i, tk->i_start, tk->i_end );
950 /* Seek at the end of the buffer */
951 if( p_access->pf_seek( p_access, tk->i_end ) )
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;
959 if( p_sys->stream.i_used < 1024 )
960 p_sys->stream.i_used = 1024;
962 if( AStreamRefillStream( s ) )
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 );
977 /* Search closest segment TODO */
978 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
980 stream_track_t *tk = &p_sys->stream.tk[i];
982 if( i_pos + i_maxth >= tk->i_start )
984 msg_Dbg( s, "good segment before current pos, TODO" );
986 if( i_pos - i_maxth <= tk->i_end )
988 msg_Dbg( s, "good segment after current pos, TODO" );
993 /* Nothing good, seek and choose oldest segment */
994 if( p_access->pf_seek( p_access, i_pos ) )
996 p_sys->i_pos = i_pos;
999 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1001 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
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;
1012 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1013 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1015 if( AStreamRefillStream( s ) )
1016 return VLC_EGENERIC;
1021 static int AStreamRefillStream( stream_t *s )
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];
1027 /* We read but won't increase i_start after initial start + offset */
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;
1033 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1035 /* msg_Dbg( s, "AStreamRefillStream: toread=%d", i_toread ); */
1038 while( i_toread > 0 )
1040 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1044 return VLC_EGENERIC;
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 );
1049 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1052 msleep( STREAM_DATA_WAIT );
1055 else if( i_read == 0 )
1057 return VLC_EGENERIC;
1061 tk->i_end += i_read;
1063 /* Windows of STREAM_CACHE_TRACK_SIZE */
1064 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1066 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1068 tk->i_start += i_invalid;
1069 p_sys->stream.i_offset -= i_invalid;
1073 p_sys->stream.i_used -= i_read;
1075 p_sys->stat.i_bytes += i_read;
1076 p_sys->stat.i_read_count++;
1080 p_sys->stat.i_read_time += i_stop - i_start;
1085 static void AStreamPrebufferStream( stream_t *s )
1087 stream_sys_t *p_sys = s->p_sys;
1088 access_t *p_access = p_sys->p_access;
1090 int64_t i_first = 0;
1093 msg_Dbg( s, "pre buffering" );
1097 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1099 int64_t i_date = mdate();
1102 if( s->b_die || tk->i_end >= STREAM_CACHE_PREBUFFER_SIZE ||
1103 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
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);
1113 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
1115 p_sys->stat.i_bytes,
1116 p_sys->stat.i_read_time / I64C(1000000),
1117 i_byterate / 1024 );
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],
1128 msleep( STREAM_DATA_WAIT );
1131 else if( i_read == 0 )
1140 tk->i_end += i_read;
1142 p_sys->stat.i_read_count++;
1147 /****************************************************************************
1149 ****************************************************************************/
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,
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 )
1163 i_data = stream_Peek( s, &p_data, MAX_LINE );
1165 while( i < i_data && p_data[i] != '\n' && p_data[i] != '\r' )
1175 p_line = malloc( i + 1 );
1176 if( p_line == NULL )
1178 msg_Err( s, "out of memory" );
1181 i = stream_Read( s, p_line, i + 1 );
1182 p_line[ i - 1 ] = '\0';