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 /****************************************************************************
343 * stream_AccessUpdate:
344 ****************************************************************************/
345 void stream_AccessUpdate( stream_t *s )
347 stream_sys_t *p_sys = s->p_sys;
348 p_sys->i_pos = p_sys->p_access->info.i_pos;
351 /****************************************************************************
353 ****************************************************************************/
354 static int AStreamControl( stream_t *s, int i_query, va_list args )
356 stream_sys_t *p_sys = s->p_sys;
357 access_t *p_access = p_sys->p_access;
360 int64_t *pi_64, i_64;
365 case STREAM_GET_SIZE:
366 pi_64 = (int64_t*)va_arg( args, int64_t * );
367 *pi_64 = p_access->info.i_size;
370 case STREAM_CAN_SEEK:
371 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
372 access2_Control( p_access, ACCESS_CAN_SEEK, p_bool );
375 case STREAM_CAN_FASTSEEK:
376 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
377 access2_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
380 case STREAM_GET_POSITION:
381 pi_64 = (int64_t*)va_arg( args, int64_t * );
382 *pi_64 = p_sys->i_pos;
385 case STREAM_SET_POSITION:
386 i_64 = (int64_t)va_arg( args, int64_t );
388 return AStreamSeekBlock( s, i_64 );
390 return AStreamSeekStream( s, i_64 );
395 case STREAM_CONTROL_ACCESS:
396 i_int = (int) va_arg( args, int );
397 if( i_int != ACCESS_SET_PRIVATE_ID_STATE )
399 msg_Err( s, "Hey, what are you thinking ?"
400 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
403 return access2_vaControl( p_access, i_int, args );
406 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
414 /****************************************************************************
416 ****************************************************************************/
417 static void AStreamPrebufferBlock( stream_t *s )
419 stream_sys_t *p_sys = s->p_sys;
420 access_t *p_access = p_sys->p_access;
425 msg_Dbg( s, "pre buffering" );
429 int64_t i_date = mdate();
432 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
433 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
438 p_sys->stat.i_bytes = p_sys->block.i_size;
439 p_sys->stat.i_read_time = i_date - i_start;
440 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
441 (p_sys->stat.i_read_time + 1);
443 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
446 p_sys->stat.i_read_time / I64C(1000000),
452 if( ( b = p_access->pf_block( p_access ) ) == NULL )
454 if( p_access->info.b_eof )
457 msleep( STREAM_DATA_WAIT );
464 msg_Dbg( s, "received first data for our buffer");
467 /* Append the block */
468 p_sys->block.i_size += b->i_buffer;
469 *p_sys->block.pp_last = b;
470 p_sys->block.pp_last = &b->p_next;
472 p_sys->stat.i_read_count++;
475 p_sys->block.p_current = p_sys->block.p_first;
478 static int AStreamRefillBlock( stream_t *s );
480 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
482 stream_sys_t *p_sys = s->p_sys;
484 uint8_t *p_data= (uint8_t*)p_read;
488 if( p_sys->block.p_current == NULL )
493 /* seek within this stream if possible, else use plain old read and discard */
494 stream_sys_t *p_sys = s->p_sys;
495 access_t *p_access = p_sys->p_access;
497 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
499 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
502 while( i_data < i_read )
505 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
506 int i_copy = __MIN( i_current, i_read - i_data);
512 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
518 p_sys->block.i_offset += i_copy;
519 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
521 /* Current block is now empty, switch to next */
522 if( p_sys->block.p_current )
524 p_sys->block.i_offset = 0;
525 p_sys->block.p_current = p_sys->block.p_current->p_next;
528 if( AStreamRefillBlock( s ) )
535 p_sys->i_pos += i_data;
539 static int AStreamPeekBlock( stream_t *s, uint8_t **pp_peek, int i_read )
541 stream_sys_t *p_sys = s->p_sys;
547 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
549 /* We can directly give a pointer over our buffer */
550 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
552 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
556 /* We need to create a local copy */
557 if( p_sys->i_peek < i_read )
560 free( p_sys->p_peek );
561 p_sys->i_peek = i_read;
562 p_sys->p_peek = malloc( p_sys->i_peek );
565 /* Fill enough data */
566 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
569 block_t **pp_last = p_sys->block.pp_last;
571 if( AStreamRefillBlock( s ) )
574 /* Our buffer are probably filled enough, don't try anymore */
575 if( pp_last == p_sys->block.pp_last )
579 /* Copy what we have */
580 b = p_sys->block.p_current;
581 i_offset = p_sys->block.i_offset;
582 p_data = p_sys->p_peek;
584 while( b && i_data < i_read )
586 int i_current = b->i_buffer - i_offset;
587 int i_copy = __MIN( i_current, i_read - i_data );
589 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
594 if( i_offset >= b->i_buffer )
601 *pp_peek = p_sys->p_peek;
605 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
607 stream_sys_t *p_sys = s->p_sys;
608 access_t *p_access = p_sys->p_access;
609 int64_t i_offset = i_pos - p_sys->block.i_start;
612 /* We already have thoses data, just update p_current/i_offset */
613 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
615 block_t *b = p_sys->block.p_first;
618 while( i_current + b->i_buffer < i_offset )
620 i_current += b->i_buffer;
624 p_sys->block.p_current = b;
625 p_sys->block.i_offset = i_offset - i_current;
627 p_sys->i_pos = i_pos;
632 /* We may need to seek or to read data */
636 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
640 msg_Err( s, "backward seek impossible (access non seekable)" );
648 vlc_bool_t b_aseek, b_aseekfast;
650 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
651 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
656 msg_Warn( s, I64Fd" bytes need to be skipped "
657 "(access non seekable)",
658 i_offset - p_sys->block.i_size );
662 int64_t i_skip = i_offset - p_sys->block.i_size;
664 /* Avg bytes per packets */
665 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
666 /* TODO compute a seek cost instead of fixed threshold */
667 int i_th = b_aseekfast ? 1 : 5;
669 if( i_skip <= i_th * i_avg &&
670 i_skip < STREAM_CACHE_SIZE )
675 msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
676 b_seek, i_th*i_avg, i_skip );
682 int64_t i_start, i_end;
683 /* Do the access seek */
685 if( p_access->pf_seek( p_access, i_pos ) )
690 block_ChainRelease( p_sys->block.p_first );
693 p_sys->block.i_start = p_sys->i_pos = i_pos;
694 p_sys->block.i_offset = 0;
695 p_sys->block.p_current = NULL;
696 p_sys->block.i_size = 0;
697 p_sys->block.p_first = NULL;
698 p_sys->block.pp_last = &p_sys->block.p_first;
701 if( AStreamRefillBlock( s ) )
703 msg_Err( s, "cannot re fill buffer" );
707 p_sys->stat.i_seek_time += i_end - i_start;
708 p_sys->stat.i_seek_count++;
713 /* Read enought data */
714 while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
716 if( AStreamRefillBlock( s ) )
718 msg_Err( s, "can't read enough data in seek" );
721 while( p_sys->block.p_current &&
722 p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
724 p_sys->i_pos += p_sys->block.p_current->i_buffer;
725 p_sys->block.p_current = p_sys->block.p_current->p_next;
729 p_sys->block.i_offset = i_pos - p_sys->i_pos;
730 p_sys->i_pos = i_pos;
739 static int AStreamRefillBlock( stream_t *s )
741 stream_sys_t *p_sys = s->p_sys;
742 access_t *p_access = p_sys->p_access;
743 int64_t i_start, i_stop;
747 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
748 p_sys->block.p_first != p_sys->block.p_current )
750 block_t *b = p_sys->block.p_first;
752 p_sys->block.i_start += b->i_buffer;
753 p_sys->block.i_size -= b->i_buffer;
754 p_sys->block.p_first = b->p_next;
758 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
759 p_sys->block.p_current == p_sys->block.p_first &&
760 p_sys->block.p_current->p_next ) /* At least 2 packets */
762 /* Enough data, don't read more */
766 /* Now read a new block */
775 if( ( b = p_access->pf_block( p_access ) ) )
778 if( p_access->info.b_eof )
781 msleep( STREAM_DATA_WAIT );
785 /* Append the block */
786 p_sys->block.i_size += b->i_buffer;
787 *p_sys->block.pp_last = b;
788 p_sys->block.pp_last = &b->p_next;
791 if( p_sys->block.p_current == NULL )
792 p_sys->block.p_current = b;
795 p_sys->stat.i_bytes += b->i_buffer;
796 p_sys->stat.i_read_time += i_stop - i_start;
797 p_sys->stat.i_read_count++;
803 /****************************************************************************
805 ****************************************************************************/
806 static int AStreamRefillStream( stream_t *s );
808 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
810 stream_sys_t *p_sys = s->p_sys;
811 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
813 uint8_t *p_data = (uint8_t *)p_read;
816 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
820 /* seek within this stream if possible, else use plain old read and discard */
821 stream_sys_t *p_sys = s->p_sys;
822 access_t *p_access = p_sys->p_access;
824 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
826 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
830 msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
831 " offset=%d end="I64Fd,
832 i_read, p_sys->i_pos, p_sys->stream.i_tk,
833 tk->i_start, p_sys->stream.i_offset, tk->i_end );
836 while( i_data < i_read )
838 int i_off = (tk->i_start + p_sys->stream.i_offset) %
839 STREAM_CACHE_TRACK_SIZE;
841 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
842 STREAM_CACHE_TRACK_SIZE - i_off );
843 int i_copy = __MIN( i_current, i_read - i_data );
845 if( i_copy <= 0 ) break; /* EOF */
848 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
851 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
855 p_sys->stream.i_offset += i_copy;
858 p_sys->i_pos += i_copy;
861 p_sys->stream.i_used += i_copy;
862 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
863 p_sys->stream.i_used >= p_sys->stream.i_read_size )
865 if( AStreamRefillStream( s ) )
868 if( tk->i_start >= tk->i_end ) break;
876 static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read )
878 stream_sys_t *p_sys = s->p_sys;
879 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
882 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
885 msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
886 "start="I64Fd" offset=%d end="I64Fd,
887 i_read, p_sys->i_pos, p_sys->stream.i_tk,
888 tk->i_start, p_sys->stream.i_offset, tk->i_end );
891 /* Avoid problem, but that should *never* happen */
892 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
893 i_read = STREAM_CACHE_TRACK_SIZE / 2;
895 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
897 if( p_sys->stream.i_used <= 1 )
899 /* Be sure we will read something */
900 p_sys->stream.i_used += i_read - (tk->i_end - tk->i_start - p_sys->stream.i_offset);
902 if( AStreamRefillStream( s ) )
906 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
907 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
909 /* Now, direct pointer or a copy ? */
910 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
911 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
913 *pp_peek = &tk->p_buffer[i_off];
917 if( p_sys->i_peek < i_read )
919 if( p_sys->p_peek ) free( p_sys->p_peek );
920 p_sys->i_peek = i_read;
921 p_sys->p_peek = malloc( i_read );
924 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
925 STREAM_CACHE_TRACK_SIZE - i_off );
926 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
927 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
929 *pp_peek = p_sys->p_peek;
933 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
935 stream_sys_t *p_sys = s->p_sys;
936 access_t *p_access = p_sys->p_access;
938 vlc_bool_t b_afastseek;
944 msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
945 "tk=%d start="I64Fd" offset=%d end="I64Fd,
946 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
947 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
948 p_sys->stream.i_offset,
949 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
953 /* Seek in our current track ? */
954 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
955 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
957 //msg_Dbg( s, "AStreamSeekStream: current track" );
958 p_sys->i_pos = i_pos;
959 p_sys->stream.i_offset = i_pos - p_sys->stream.tk[p_sys->stream.i_tk].i_start;
963 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
966 /* We can't do nothing */
967 msg_Dbg( s, "AStreamSeekStream: can't seek" );
971 /* Date the current track */
972 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
974 /* Try to reuse already read data */
975 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
977 stream_track_t *tk = &p_sys->stream.tk[i];
979 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
982 msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
983 " end="I64Fd, i, tk->i_start, tk->i_end );
985 /* Seek at the end of the buffer */
986 if( p_access->pf_seek( p_access, tk->i_end ) )
990 p_sys->i_pos = i_pos;
991 p_sys->stream.i_tk = i;
992 p_sys->stream.i_offset = i_pos - tk->i_start;
994 if( p_sys->stream.i_used < 1024 )
995 p_sys->stream.i_used = 1024;
997 if( AStreamRefillStream( s ) )
1004 access2_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1005 /* FIXME compute seek cost (instead of static 'stupid' value) */
1006 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1012 /* Search closest segment TODO */
1013 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1015 stream_track_t *tk = &p_sys->stream.tk[i];
1017 if( i_pos + i_maxth >= tk->i_start )
1019 msg_Dbg( s, "good segment before current pos, TODO" );
1021 if( i_pos - i_maxth <= tk->i_end )
1023 msg_Dbg( s, "good segment after current pos, TODO" );
1028 /* Nothing good, seek and choose oldest segment */
1029 if( p_access->pf_seek( p_access, i_pos ) )
1030 return VLC_EGENERIC;
1031 p_sys->i_pos = i_pos;
1034 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1036 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1040 /* Reset the segment */
1041 p_sys->stream.i_tk = i_new;
1042 p_sys->stream.i_offset = 0;
1043 p_sys->stream.tk[i_new].i_start = i_pos;
1044 p_sys->stream.tk[i_new].i_end = i_pos;
1047 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1048 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1050 if( AStreamRefillStream( s ) )
1051 return VLC_EGENERIC;
1056 static int AStreamRefillStream( stream_t *s )
1058 stream_sys_t *p_sys = s->p_sys;
1059 access_t *p_access = p_sys->p_access;
1060 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1062 /* We read but won't increase i_start after initial start + offset */
1064 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1065 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1066 vlc_bool_t b_read = VLC_FALSE;
1067 int64_t i_start, i_stop;
1069 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1071 /* msg_Dbg( s, "AStreamRefillStream: toread=%d", i_toread ); */
1074 while( i_toread > 0 )
1076 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1080 return VLC_EGENERIC;
1082 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1083 i_read = p_access->pf_read( p_access, &tk->p_buffer[i_off], i_read );
1085 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1088 msleep( STREAM_DATA_WAIT );
1091 else if( i_read == 0 )
1094 return VLC_EGENERIC;
1100 tk->i_end += i_read;
1102 /* Windows of STREAM_CACHE_TRACK_SIZE */
1103 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1105 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1107 tk->i_start += i_invalid;
1108 p_sys->stream.i_offset -= i_invalid;
1112 p_sys->stream.i_used -= i_read;
1114 p_sys->stat.i_bytes += i_read;
1115 p_sys->stat.i_read_count++;
1119 p_sys->stat.i_read_time += i_stop - i_start;
1124 static void AStreamPrebufferStream( stream_t *s )
1126 stream_sys_t *p_sys = s->p_sys;
1127 access_t *p_access = p_sys->p_access;
1129 int64_t i_first = 0;
1131 int64_t i_prebuffer = (s->p_sys->p_access->info.i_title > 1 ||
1132 s->p_sys->p_access->info.i_seekpoint > 1) ? STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3;
1134 msg_Dbg( s, "pre buffering" );
1138 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1140 int64_t i_date = mdate();
1143 if( s->b_die || tk->i_end >= i_prebuffer ||
1144 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1149 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1150 p_sys->stat.i_read_time = i_date - i_start;
1151 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
1152 (p_sys->stat.i_read_time+1);
1154 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
1156 p_sys->stat.i_bytes,
1157 p_sys->stat.i_read_time / I64C(1000000),
1158 i_byterate / 1024 );
1163 i_read = __MIN( p_sys->stream.i_read_size,
1164 STREAM_CACHE_TRACK_SIZE - tk->i_end );
1165 i_read = p_access->pf_read( p_access, &tk->p_buffer[tk->i_end],
1169 msleep( STREAM_DATA_WAIT );
1172 else if( i_read == 0 )
1181 msg_Dbg( s, "received first data for our buffer");
1184 tk->i_end += i_read;
1186 p_sys->stat.i_read_count++;
1191 /****************************************************************************
1193 ****************************************************************************/
1195 * Read from the stream untill first newline.
1196 * \param s Stream handle to read from
1197 * \return A null-terminated string. This must be freed,
1199 #define STREAM_PROBE_LINE 2048
1200 #define STREAM_LINE_MAX (2048*100)
1201 char *stream_ReadLine( stream_t *s )
1203 char *p_line = NULL;
1204 int i_line = 0, i_read = 0;
1206 while( i_read < STREAM_LINE_MAX )
1212 /* Probe new data */
1213 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1214 if( i_data <= 0 ) break; /* No more data */
1216 /* Check if there is an EOL */
1217 if( ( psz_eol = memchr( p_data, '\n', i_data ) ) )
1219 i_data = (psz_eol - (char *)p_data) + 1;
1220 p_line = realloc( p_line, i_line + i_data + 1 );
1221 i_data = stream_Read( s, &p_line[i_line], i_data );
1222 if( i_data <= 0 ) break; /* Hmmm */
1223 i_line += (i_data - 1);
1226 /* We have our line */
1230 /* Read data (+1 for easy \0 append) */
1231 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + 1 );
1232 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1233 if( i_data <= 0 ) break; /* Hmmm */
1238 /* Remove trailing LF/CR */
1239 while( i_line > 0 && ( p_line[i_line-1] == '\r' ||
1240 p_line[i_line-1] == '\n') ) i_line--;
1244 p_line[i_line] = '\0';
1248 /* We failed to read any data, probably EOF */
1249 if( p_line ) free( p_line );