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 */
140 /* Preparse mode ? */
145 static int AStreamReadBlock( stream_t *, void *p_read, int i_read );
146 static int AStreamPeekBlock( stream_t *, uint8_t **p_peek, int i_read );
147 static int AStreamSeekBlock( stream_t *s, int64_t i_pos );
148 static void AStreamPrebufferBlock( stream_t * );
151 static int AStreamReadStream( stream_t *, void *p_read, int i_read );
152 static int AStreamPeekStream( stream_t *, uint8_t **pp_peek, int i_read );
153 static int AStreamSeekStream( stream_t *s, int64_t i_pos );
154 static void AStreamPrebufferStream( stream_t * );
157 static int AStreamControl( stream_t *, int i_query, va_list );
160 /****************************************************************************
161 * stream_AccessNew: create a stream from a access
162 ****************************************************************************/
163 stream_t *stream_AccessNew( access_t *p_access, vlc_bool_t b_quick )
165 stream_t *s = vlc_object_create( p_access, VLC_OBJECT_STREAM );
171 /* Attach it now, needed for b_die */
172 vlc_object_attach( s, p_access );
175 s->pf_read = NULL; /* Set up later */
177 s->pf_control= AStreamControl;
179 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
182 p_sys->p_access = p_access;
183 p_sys->b_block = p_access->pf_block ? VLC_TRUE : VLC_FALSE;
184 p_sys->i_pos = p_access->info.i_pos;
187 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
188 p_sys->stat.i_bytes = 0;
189 p_sys->stat.i_read_time = 0;
190 p_sys->stat.i_read_count = 0;
191 p_sys->stat.i_seek_count = 0;
192 p_sys->stat.i_seek_time = 0;
194 p_sys->b_quick = b_quick;
198 p_sys->p_peek = NULL;
202 s->pf_read = AStreamReadBlock;
203 s->pf_peek = AStreamPeekBlock;
205 /* Init all fields of p_sys->block */
206 p_sys->block.i_start = p_sys->i_pos;
207 p_sys->block.i_offset = 0;
208 p_sys->block.p_current = NULL;
209 p_sys->block.i_size = 0;
210 p_sys->block.p_first = NULL;
211 p_sys->block.pp_last = &p_sys->block.p_first;
213 /* Do the prebuffering */
214 AStreamPrebufferBlock( s );
216 if( p_sys->block.i_size <= 0 )
218 msg_Err( s, "cannot pre fill buffer" );
226 s->pf_read = AStreamReadStream;
227 s->pf_peek = AStreamPeekStream;
229 /* Allocate/Setup our tracks */
230 p_sys->stream.i_offset = 0;
231 p_sys->stream.i_tk = 0;
232 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
233 p_sys->stream.i_used = 0;
234 access2_Control( p_access, ACCESS_GET_MTU,
235 &p_sys->stream.i_read_size );
236 if( p_sys->stream.i_read_size <= 0 )
237 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
238 else if( p_sys->stream.i_read_size <= 256 )
239 p_sys->stream.i_read_size = 256;
241 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
243 p_sys->stream.tk[i].i_date = 0;
244 p_sys->stream.tk[i].i_start = p_sys->i_pos;
245 p_sys->stream.tk[i].i_end = p_sys->i_pos;
246 p_sys->stream.tk[i].p_buffer=
247 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
250 /* Do the prebuffering */
251 AStreamPrebufferStream( s );
253 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
255 msg_Err( s, "cannot pre fill buffer" );
269 free( p_sys->stream.p_buffer );
272 vlc_object_detach( s );
273 vlc_object_destroy( s );
277 /****************************************************************************
278 * stream_AccessDelete:
279 ****************************************************************************/
280 void stream_AccessDelete( stream_t *s )
282 stream_sys_t *p_sys = s->p_sys;
284 vlc_object_detach( s );
288 block_ChainRelease( p_sys->block.p_first );
292 free( p_sys->stream.p_buffer );
296 free( p_sys->p_peek );
299 vlc_object_destroy( s );
302 /****************************************************************************
303 * stream_AccessReset:
304 ****************************************************************************/
305 void stream_AccessReset( stream_t *s )
307 stream_sys_t *p_sys = s->p_sys;
309 p_sys->i_pos = p_sys->p_access->info.i_pos;
313 block_ChainRelease( p_sys->block.p_first );
315 /* Init all fields of p_sys->block */
316 p_sys->block.i_start = p_sys->i_pos;
317 p_sys->block.i_offset = 0;
318 p_sys->block.p_current = NULL;
319 p_sys->block.i_size = 0;
320 p_sys->block.p_first = NULL;
321 p_sys->block.pp_last = &p_sys->block.p_first;
323 /* Do the prebuffering */
324 AStreamPrebufferBlock( s );
330 /* Setup our tracks */
331 p_sys->stream.i_offset = 0;
332 p_sys->stream.i_tk = 0;
333 p_sys->stream.i_used = 0;
335 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
337 p_sys->stream.tk[i].i_date = 0;
338 p_sys->stream.tk[i].i_start = p_sys->i_pos;
339 p_sys->stream.tk[i].i_end = p_sys->i_pos;
342 /* Do the prebuffering */
343 AStreamPrebufferStream( s );
347 /****************************************************************************
348 * stream_AccessUpdate:
349 ****************************************************************************/
350 void stream_AccessUpdate( stream_t *s )
352 stream_sys_t *p_sys = s->p_sys;
353 p_sys->i_pos = p_sys->p_access->info.i_pos;
356 /****************************************************************************
358 ****************************************************************************/
359 static int AStreamControl( stream_t *s, int i_query, va_list args )
361 stream_sys_t *p_sys = s->p_sys;
362 access_t *p_access = p_sys->p_access;
365 int64_t *pi_64, i_64;
370 case STREAM_GET_SIZE:
371 pi_64 = (int64_t*)va_arg( args, int64_t * );
372 *pi_64 = p_access->info.i_size;
375 case STREAM_CAN_SEEK:
376 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
377 access2_Control( p_access, ACCESS_CAN_SEEK, p_bool );
380 case STREAM_CAN_FASTSEEK:
381 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
382 access2_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
385 case STREAM_GET_POSITION:
386 pi_64 = (int64_t*)va_arg( args, int64_t * );
387 *pi_64 = p_sys->i_pos;
390 case STREAM_SET_POSITION:
391 i_64 = (int64_t)va_arg( args, int64_t );
393 return AStreamSeekBlock( s, i_64 );
395 return AStreamSeekStream( s, i_64 );
400 case STREAM_CONTROL_ACCESS:
401 i_int = (int) va_arg( args, int );
402 if( i_int != ACCESS_SET_PRIVATE_ID_STATE
403 && i_int != ACCESS_SET_PRIVATE_ID_CA )
405 msg_Err( s, "Hey, what are you thinking ?"
406 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
409 return access2_vaControl( p_access, i_int, args );
412 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
420 /****************************************************************************
422 ****************************************************************************/
423 static void AStreamPrebufferBlock( stream_t *s )
425 stream_sys_t *p_sys = s->p_sys;
426 access_t *p_access = p_sys->p_access;
431 msg_Dbg( s, "pre buffering" );
435 int64_t i_date = mdate();
438 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
439 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
444 p_sys->stat.i_bytes = p_sys->block.i_size;
445 p_sys->stat.i_read_time = i_date - i_start;
446 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
447 (p_sys->stat.i_read_time + 1);
449 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
452 p_sys->stat.i_read_time / I64C(1000000),
458 if( ( b = p_access->pf_block( p_access ) ) == NULL )
460 if( p_access->info.b_eof )
463 msleep( STREAM_DATA_WAIT );
470 msg_Dbg( s, "received first data for our buffer");
473 /* Append the block */
474 p_sys->block.i_size += b->i_buffer;
475 *p_sys->block.pp_last = b;
476 p_sys->block.pp_last = &b->p_next;
478 p_sys->stat.i_read_count++;
481 p_sys->block.p_current = p_sys->block.p_first;
484 static int AStreamRefillBlock( stream_t *s );
486 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
488 stream_sys_t *p_sys = s->p_sys;
490 uint8_t *p_data= (uint8_t*)p_read;
494 if( p_sys->block.p_current == NULL )
499 /* seek within this stream if possible, else use plain old read and discard */
500 stream_sys_t *p_sys = s->p_sys;
501 access_t *p_access = p_sys->p_access;
503 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
505 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
508 while( i_data < i_read )
511 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
512 int i_copy = __MIN( i_current, i_read - i_data);
518 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
524 p_sys->block.i_offset += i_copy;
525 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
527 /* Current block is now empty, switch to next */
528 if( p_sys->block.p_current )
530 p_sys->block.i_offset = 0;
531 p_sys->block.p_current = p_sys->block.p_current->p_next;
534 if( AStreamRefillBlock( s ) )
541 p_sys->i_pos += i_data;
545 static int AStreamPeekBlock( stream_t *s, uint8_t **pp_peek, int i_read )
547 stream_sys_t *p_sys = s->p_sys;
553 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
555 /* We can directly give a pointer over our buffer */
556 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
558 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
562 /* We need to create a local copy */
563 if( p_sys->i_peek < i_read )
566 free( p_sys->p_peek );
567 p_sys->i_peek = i_read;
568 p_sys->p_peek = malloc( p_sys->i_peek );
571 /* Fill enough data */
572 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
575 block_t **pp_last = p_sys->block.pp_last;
577 if( AStreamRefillBlock( s ) )
580 /* Our buffer are probably filled enough, don't try anymore */
581 if( pp_last == p_sys->block.pp_last )
585 /* Copy what we have */
586 b = p_sys->block.p_current;
587 i_offset = p_sys->block.i_offset;
588 p_data = p_sys->p_peek;
590 while( b && i_data < i_read )
592 int i_current = b->i_buffer - i_offset;
593 int i_copy = __MIN( i_current, i_read - i_data );
595 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
600 if( i_offset >= b->i_buffer )
607 *pp_peek = p_sys->p_peek;
611 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
613 stream_sys_t *p_sys = s->p_sys;
614 access_t *p_access = p_sys->p_access;
615 int64_t i_offset = i_pos - p_sys->block.i_start;
618 /* We already have thoses data, just update p_current/i_offset */
619 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
621 block_t *b = p_sys->block.p_first;
624 while( i_current + b->i_buffer < i_offset )
626 i_current += b->i_buffer;
630 p_sys->block.p_current = b;
631 p_sys->block.i_offset = i_offset - i_current;
633 p_sys->i_pos = i_pos;
638 /* We may need to seek or to read data */
642 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
646 msg_Err( s, "backward seek impossible (access non seekable)" );
654 vlc_bool_t b_aseek, b_aseekfast;
656 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
657 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
662 msg_Warn( s, I64Fd" bytes need to be skipped "
663 "(access non seekable)",
664 i_offset - p_sys->block.i_size );
668 int64_t i_skip = i_offset - p_sys->block.i_size;
670 /* Avg bytes per packets */
671 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
672 /* TODO compute a seek cost instead of fixed threshold */
673 int i_th = b_aseekfast ? 1 : 5;
675 if( i_skip <= i_th * i_avg &&
676 i_skip < STREAM_CACHE_SIZE )
681 msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
682 b_seek, i_th*i_avg, i_skip );
688 int64_t i_start, i_end;
689 /* Do the access seek */
691 if( p_access->pf_seek( p_access, i_pos ) )
696 block_ChainRelease( p_sys->block.p_first );
699 p_sys->block.i_start = p_sys->i_pos = i_pos;
700 p_sys->block.i_offset = 0;
701 p_sys->block.p_current = NULL;
702 p_sys->block.i_size = 0;
703 p_sys->block.p_first = NULL;
704 p_sys->block.pp_last = &p_sys->block.p_first;
707 if( AStreamRefillBlock( s ) )
709 msg_Err( s, "cannot re fill buffer" );
713 p_sys->stat.i_seek_time += i_end - i_start;
714 p_sys->stat.i_seek_count++;
719 /* Read enought data */
720 while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
722 if( AStreamRefillBlock( s ) )
724 msg_Err( s, "can't read enough data in seek" );
727 while( p_sys->block.p_current &&
728 p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
730 p_sys->i_pos += p_sys->block.p_current->i_buffer;
731 p_sys->block.p_current = p_sys->block.p_current->p_next;
735 p_sys->block.i_offset = i_pos - p_sys->i_pos;
736 p_sys->i_pos = i_pos;
745 static int AStreamRefillBlock( stream_t *s )
747 stream_sys_t *p_sys = s->p_sys;
748 access_t *p_access = p_sys->p_access;
749 int64_t i_start, i_stop;
753 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
754 p_sys->block.p_first != p_sys->block.p_current )
756 block_t *b = p_sys->block.p_first;
758 p_sys->block.i_start += b->i_buffer;
759 p_sys->block.i_size -= b->i_buffer;
760 p_sys->block.p_first = b->p_next;
764 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
765 p_sys->block.p_current == p_sys->block.p_first &&
766 p_sys->block.p_current->p_next ) /* At least 2 packets */
768 /* Enough data, don't read more */
772 /* Now read a new block */
781 if( ( b = p_access->pf_block( p_access ) ) )
784 if( p_access->info.b_eof )
787 msleep( STREAM_DATA_WAIT );
791 /* Append the block */
792 p_sys->block.i_size += b->i_buffer;
793 *p_sys->block.pp_last = b;
794 p_sys->block.pp_last = &b->p_next;
797 if( p_sys->block.p_current == NULL )
798 p_sys->block.p_current = b;
801 p_sys->stat.i_bytes += b->i_buffer;
802 p_sys->stat.i_read_time += i_stop - i_start;
803 p_sys->stat.i_read_count++;
809 /****************************************************************************
811 ****************************************************************************/
812 static int AStreamRefillStream( stream_t *s );
814 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
816 stream_sys_t *p_sys = s->p_sys;
817 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
819 uint8_t *p_data = (uint8_t *)p_read;
822 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
826 /* seek within this stream if possible, else use plain old read and discard */
827 stream_sys_t *p_sys = s->p_sys;
828 access_t *p_access = p_sys->p_access;
830 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
832 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
836 msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
837 " offset=%d end="I64Fd,
838 i_read, p_sys->i_pos, p_sys->stream.i_tk,
839 tk->i_start, p_sys->stream.i_offset, tk->i_end );
842 while( i_data < i_read )
844 int i_off = (tk->i_start + p_sys->stream.i_offset) %
845 STREAM_CACHE_TRACK_SIZE;
847 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
848 STREAM_CACHE_TRACK_SIZE - i_off );
849 int i_copy = __MIN( i_current, i_read - i_data );
851 if( i_copy <= 0 ) break; /* EOF */
854 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
857 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
861 p_sys->stream.i_offset += i_copy;
864 p_sys->i_pos += i_copy;
867 p_sys->stream.i_used += i_copy;
868 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
869 p_sys->stream.i_used >= p_sys->stream.i_read_size )
871 if( AStreamRefillStream( s ) )
874 if( tk->i_start >= tk->i_end ) break;
882 static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read )
884 stream_sys_t *p_sys = s->p_sys;
885 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
888 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
891 msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
892 "start="I64Fd" offset=%d end="I64Fd,
893 i_read, p_sys->i_pos, p_sys->stream.i_tk,
894 tk->i_start, p_sys->stream.i_offset, tk->i_end );
897 /* Avoid problem, but that should *never* happen */
898 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
899 i_read = STREAM_CACHE_TRACK_SIZE / 2;
901 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
903 if( p_sys->stream.i_used <= 1 )
905 /* Be sure we will read something */
906 p_sys->stream.i_used += i_read - (tk->i_end - tk->i_start - p_sys->stream.i_offset);
908 if( AStreamRefillStream( s ) )
912 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
913 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
915 /* Now, direct pointer or a copy ? */
916 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
917 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
919 *pp_peek = &tk->p_buffer[i_off];
923 if( p_sys->i_peek < i_read )
925 if( p_sys->p_peek ) free( p_sys->p_peek );
926 p_sys->i_peek = i_read;
927 p_sys->p_peek = malloc( i_read );
930 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
931 STREAM_CACHE_TRACK_SIZE - i_off );
932 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
933 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
935 *pp_peek = p_sys->p_peek;
939 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
941 stream_sys_t *p_sys = s->p_sys;
942 access_t *p_access = p_sys->p_access;
944 vlc_bool_t b_afastseek;
950 msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
951 "tk=%d start="I64Fd" offset=%d end="I64Fd,
952 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
953 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
954 p_sys->stream.i_offset,
955 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
959 /* Seek in our current track ? */
960 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
961 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
963 //msg_Dbg( s, "AStreamSeekStream: current track" );
964 p_sys->i_pos = i_pos;
965 p_sys->stream.i_offset = i_pos - p_sys->stream.tk[p_sys->stream.i_tk].i_start;
969 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
972 /* We can't do nothing */
973 msg_Dbg( s, "AStreamSeekStream: can't seek" );
977 /* Date the current track */
978 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
980 /* Try to reuse already read data */
981 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
983 stream_track_t *tk = &p_sys->stream.tk[i];
985 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
988 msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
989 " end="I64Fd, i, tk->i_start, tk->i_end );
991 /* Seek at the end of the buffer */
992 if( p_access->pf_seek( p_access, tk->i_end ) )
996 p_sys->i_pos = i_pos;
997 p_sys->stream.i_tk = i;
998 p_sys->stream.i_offset = i_pos - tk->i_start;
1000 if( p_sys->stream.i_used < 1024 )
1001 p_sys->stream.i_used = 1024;
1003 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1004 return VLC_EGENERIC;
1010 access2_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1011 /* FIXME compute seek cost (instead of static 'stupid' value) */
1012 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1018 /* Search closest segment TODO */
1019 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1021 stream_track_t *tk = &p_sys->stream.tk[i];
1023 if( i_pos + i_maxth >= tk->i_start )
1025 msg_Dbg( s, "good segment before current pos, TODO" );
1027 if( i_pos - i_maxth <= tk->i_end )
1029 msg_Dbg( s, "good segment after current pos, TODO" );
1034 /* Nothing good, seek and choose oldest segment */
1035 if( p_access->pf_seek( p_access, i_pos ) )
1036 return VLC_EGENERIC;
1037 p_sys->i_pos = i_pos;
1040 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1042 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1046 /* Reset the segment */
1047 p_sys->stream.i_tk = i_new;
1048 p_sys->stream.i_offset = 0;
1049 p_sys->stream.tk[i_new].i_start = i_pos;
1050 p_sys->stream.tk[i_new].i_end = i_pos;
1053 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1054 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1056 if( AStreamRefillStream( s ) )
1057 return VLC_EGENERIC;
1062 static int AStreamRefillStream( stream_t *s )
1064 stream_sys_t *p_sys = s->p_sys;
1065 access_t *p_access = p_sys->p_access;
1066 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1068 /* We read but won't increase i_start after initial start + offset */
1070 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1071 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1072 vlc_bool_t b_read = VLC_FALSE;
1073 int64_t i_start, i_stop;
1075 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1077 /* msg_Dbg( s, "AStreamRefillStream: toread=%d", i_toread ); */
1080 while( i_toread > 0 )
1082 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1086 return VLC_EGENERIC;
1088 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1089 i_read = p_access->pf_read( p_access, &tk->p_buffer[i_off], i_read );
1091 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1094 msleep( STREAM_DATA_WAIT );
1097 else if( i_read == 0 )
1100 return VLC_EGENERIC;
1106 tk->i_end += i_read;
1108 /* Windows of STREAM_CACHE_TRACK_SIZE */
1109 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1111 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1113 tk->i_start += i_invalid;
1114 p_sys->stream.i_offset -= i_invalid;
1118 p_sys->stream.i_used -= i_read;
1120 p_sys->stat.i_bytes += i_read;
1121 p_sys->stat.i_read_count++;
1125 p_sys->stat.i_read_time += i_stop - i_start;
1130 static void AStreamPrebufferStream( stream_t *s )
1132 stream_sys_t *p_sys = s->p_sys;
1133 access_t *p_access = p_sys->p_access;
1135 int64_t i_first = 0;
1137 int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1138 ((s->p_sys->p_access->info.i_title > 1 ||
1139 s->p_sys->p_access->info.i_seekpoint > 1) ? STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3);
1141 msg_Dbg( s, "pre buffering" );
1145 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1147 int64_t i_date = mdate();
1150 if( s->b_die || tk->i_end >= i_prebuffer ||
1151 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1156 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1157 p_sys->stat.i_read_time = i_date - i_start;
1158 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
1159 (p_sys->stat.i_read_time+1);
1161 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
1163 p_sys->stat.i_bytes,
1164 p_sys->stat.i_read_time / I64C(1000000),
1165 i_byterate / 1024 );
1170 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1171 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1172 i_read = p_access->pf_read( p_access, &tk->p_buffer[tk->i_end],
1176 msleep( STREAM_DATA_WAIT );
1179 else if( i_read == 0 )
1188 msg_Dbg( s, "received first data for our buffer");
1191 tk->i_end += i_read;
1193 p_sys->stat.i_read_count++;
1198 /****************************************************************************
1200 ****************************************************************************/
1202 * Read from the stream untill first newline.
1203 * \param s Stream handle to read from
1204 * \return A null-terminated string. This must be freed,
1206 #define STREAM_PROBE_LINE 2048
1207 #define STREAM_LINE_MAX (2048*100)
1208 char *stream_ReadLine( stream_t *s )
1210 char *p_line = NULL;
1211 int i_line = 0, i_read = 0;
1213 while( i_read < STREAM_LINE_MAX )
1219 /* Probe new data */
1220 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1221 if( i_data <= 0 ) break; /* No more data */
1223 /* Check if there is an EOL */
1224 if( ( psz_eol = memchr( p_data, '\n', i_data ) ) )
1226 i_data = (psz_eol - (char *)p_data) + 1;
1227 p_line = realloc( p_line, i_line + i_data + 1 );
1228 i_data = stream_Read( s, &p_line[i_line], i_data );
1229 if( i_data <= 0 ) break; /* Hmmm */
1230 i_line += (i_data - 1);
1233 /* We have our line */
1237 /* Read data (+1 for easy \0 append) */
1238 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + 1 );
1239 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1240 if( i_data <= 0 ) break; /* Hmmm */
1245 /* Remove trailing LF/CR */
1246 while( i_line > 0 && ( p_line[i_line-1] == '\r' ||
1247 p_line[i_line-1] == '\n') ) i_line--;
1251 p_line[i_line] = '\0';
1255 /* We failed to read any data, probably EOF */
1256 if( p_line ) free( p_line );