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
398 && i_int != ACCESS_SET_PRIVATE_ID_CA )
400 msg_Err( s, "Hey, what are you thinking ?"
401 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
404 return access2_vaControl( p_access, i_int, args );
407 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
415 /****************************************************************************
417 ****************************************************************************/
418 static void AStreamPrebufferBlock( stream_t *s )
420 stream_sys_t *p_sys = s->p_sys;
421 access_t *p_access = p_sys->p_access;
426 msg_Dbg( s, "pre buffering" );
430 int64_t i_date = mdate();
433 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
434 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
439 p_sys->stat.i_bytes = p_sys->block.i_size;
440 p_sys->stat.i_read_time = i_date - i_start;
441 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
442 (p_sys->stat.i_read_time + 1);
444 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
447 p_sys->stat.i_read_time / I64C(1000000),
453 if( ( b = p_access->pf_block( p_access ) ) == NULL )
455 if( p_access->info.b_eof )
458 msleep( STREAM_DATA_WAIT );
465 msg_Dbg( s, "received first data for our buffer");
468 /* Append the block */
469 p_sys->block.i_size += b->i_buffer;
470 *p_sys->block.pp_last = b;
471 p_sys->block.pp_last = &b->p_next;
473 p_sys->stat.i_read_count++;
476 p_sys->block.p_current = p_sys->block.p_first;
479 static int AStreamRefillBlock( stream_t *s );
481 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
483 stream_sys_t *p_sys = s->p_sys;
485 uint8_t *p_data= (uint8_t*)p_read;
489 if( p_sys->block.p_current == NULL )
494 /* seek within this stream if possible, else use plain old read and discard */
495 stream_sys_t *p_sys = s->p_sys;
496 access_t *p_access = p_sys->p_access;
498 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
500 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
503 while( i_data < i_read )
506 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
507 int i_copy = __MIN( i_current, i_read - i_data);
513 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
519 p_sys->block.i_offset += i_copy;
520 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
522 /* Current block is now empty, switch to next */
523 if( p_sys->block.p_current )
525 p_sys->block.i_offset = 0;
526 p_sys->block.p_current = p_sys->block.p_current->p_next;
529 if( AStreamRefillBlock( s ) )
536 p_sys->i_pos += i_data;
540 static int AStreamPeekBlock( stream_t *s, uint8_t **pp_peek, int i_read )
542 stream_sys_t *p_sys = s->p_sys;
548 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
550 /* We can directly give a pointer over our buffer */
551 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
553 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
557 /* We need to create a local copy */
558 if( p_sys->i_peek < i_read )
561 free( p_sys->p_peek );
562 p_sys->i_peek = i_read;
563 p_sys->p_peek = malloc( p_sys->i_peek );
566 /* Fill enough data */
567 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
570 block_t **pp_last = p_sys->block.pp_last;
572 if( AStreamRefillBlock( s ) )
575 /* Our buffer are probably filled enough, don't try anymore */
576 if( pp_last == p_sys->block.pp_last )
580 /* Copy what we have */
581 b = p_sys->block.p_current;
582 i_offset = p_sys->block.i_offset;
583 p_data = p_sys->p_peek;
585 while( b && i_data < i_read )
587 int i_current = b->i_buffer - i_offset;
588 int i_copy = __MIN( i_current, i_read - i_data );
590 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
595 if( i_offset >= b->i_buffer )
602 *pp_peek = p_sys->p_peek;
606 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
608 stream_sys_t *p_sys = s->p_sys;
609 access_t *p_access = p_sys->p_access;
610 int64_t i_offset = i_pos - p_sys->block.i_start;
613 /* We already have thoses data, just update p_current/i_offset */
614 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
616 block_t *b = p_sys->block.p_first;
619 while( i_current + b->i_buffer < i_offset )
621 i_current += b->i_buffer;
625 p_sys->block.p_current = b;
626 p_sys->block.i_offset = i_offset - i_current;
628 p_sys->i_pos = i_pos;
633 /* We may need to seek or to read data */
637 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
641 msg_Err( s, "backward seek impossible (access non seekable)" );
649 vlc_bool_t b_aseek, b_aseekfast;
651 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
652 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
657 msg_Warn( s, I64Fd" bytes need to be skipped "
658 "(access non seekable)",
659 i_offset - p_sys->block.i_size );
663 int64_t i_skip = i_offset - p_sys->block.i_size;
665 /* Avg bytes per packets */
666 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
667 /* TODO compute a seek cost instead of fixed threshold */
668 int i_th = b_aseekfast ? 1 : 5;
670 if( i_skip <= i_th * i_avg &&
671 i_skip < STREAM_CACHE_SIZE )
676 msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
677 b_seek, i_th*i_avg, i_skip );
683 int64_t i_start, i_end;
684 /* Do the access seek */
686 if( p_access->pf_seek( p_access, i_pos ) )
691 block_ChainRelease( p_sys->block.p_first );
694 p_sys->block.i_start = p_sys->i_pos = i_pos;
695 p_sys->block.i_offset = 0;
696 p_sys->block.p_current = NULL;
697 p_sys->block.i_size = 0;
698 p_sys->block.p_first = NULL;
699 p_sys->block.pp_last = &p_sys->block.p_first;
702 if( AStreamRefillBlock( s ) )
704 msg_Err( s, "cannot re fill buffer" );
708 p_sys->stat.i_seek_time += i_end - i_start;
709 p_sys->stat.i_seek_count++;
714 /* Read enought data */
715 while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
717 if( AStreamRefillBlock( s ) )
719 msg_Err( s, "can't read enough data in seek" );
722 while( p_sys->block.p_current &&
723 p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
725 p_sys->i_pos += p_sys->block.p_current->i_buffer;
726 p_sys->block.p_current = p_sys->block.p_current->p_next;
730 p_sys->block.i_offset = i_pos - p_sys->i_pos;
731 p_sys->i_pos = i_pos;
740 static int AStreamRefillBlock( stream_t *s )
742 stream_sys_t *p_sys = s->p_sys;
743 access_t *p_access = p_sys->p_access;
744 int64_t i_start, i_stop;
748 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
749 p_sys->block.p_first != p_sys->block.p_current )
751 block_t *b = p_sys->block.p_first;
753 p_sys->block.i_start += b->i_buffer;
754 p_sys->block.i_size -= b->i_buffer;
755 p_sys->block.p_first = b->p_next;
759 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
760 p_sys->block.p_current == p_sys->block.p_first &&
761 p_sys->block.p_current->p_next ) /* At least 2 packets */
763 /* Enough data, don't read more */
767 /* Now read a new block */
776 if( ( b = p_access->pf_block( p_access ) ) )
779 if( p_access->info.b_eof )
782 msleep( STREAM_DATA_WAIT );
786 /* Append the block */
787 p_sys->block.i_size += b->i_buffer;
788 *p_sys->block.pp_last = b;
789 p_sys->block.pp_last = &b->p_next;
792 if( p_sys->block.p_current == NULL )
793 p_sys->block.p_current = b;
796 p_sys->stat.i_bytes += b->i_buffer;
797 p_sys->stat.i_read_time += i_stop - i_start;
798 p_sys->stat.i_read_count++;
804 /****************************************************************************
806 ****************************************************************************/
807 static int AStreamRefillStream( stream_t *s );
809 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
811 stream_sys_t *p_sys = s->p_sys;
812 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
814 uint8_t *p_data = (uint8_t *)p_read;
817 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
821 /* seek within this stream if possible, else use plain old read and discard */
822 stream_sys_t *p_sys = s->p_sys;
823 access_t *p_access = p_sys->p_access;
825 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
827 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
831 msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
832 " offset=%d end="I64Fd,
833 i_read, p_sys->i_pos, p_sys->stream.i_tk,
834 tk->i_start, p_sys->stream.i_offset, tk->i_end );
837 while( i_data < i_read )
839 int i_off = (tk->i_start + p_sys->stream.i_offset) %
840 STREAM_CACHE_TRACK_SIZE;
842 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
843 STREAM_CACHE_TRACK_SIZE - i_off );
844 int i_copy = __MIN( i_current, i_read - i_data );
846 if( i_copy <= 0 ) break; /* EOF */
849 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
852 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
856 p_sys->stream.i_offset += i_copy;
859 p_sys->i_pos += i_copy;
862 p_sys->stream.i_used += i_copy;
863 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
864 p_sys->stream.i_used >= p_sys->stream.i_read_size )
866 if( AStreamRefillStream( s ) )
869 if( tk->i_start >= tk->i_end ) break;
877 static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read )
879 stream_sys_t *p_sys = s->p_sys;
880 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
883 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
886 msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
887 "start="I64Fd" offset=%d end="I64Fd,
888 i_read, p_sys->i_pos, p_sys->stream.i_tk,
889 tk->i_start, p_sys->stream.i_offset, tk->i_end );
892 /* Avoid problem, but that should *never* happen */
893 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
894 i_read = STREAM_CACHE_TRACK_SIZE / 2;
896 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
898 if( p_sys->stream.i_used <= 1 )
900 /* Be sure we will read something */
901 p_sys->stream.i_used += i_read - (tk->i_end - tk->i_start - p_sys->stream.i_offset);
903 if( AStreamRefillStream( s ) )
907 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
908 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
910 /* Now, direct pointer or a copy ? */
911 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
912 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
914 *pp_peek = &tk->p_buffer[i_off];
918 if( p_sys->i_peek < i_read )
920 if( p_sys->p_peek ) free( p_sys->p_peek );
921 p_sys->i_peek = i_read;
922 p_sys->p_peek = malloc( i_read );
925 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
926 STREAM_CACHE_TRACK_SIZE - i_off );
927 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
928 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
930 *pp_peek = p_sys->p_peek;
934 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
936 stream_sys_t *p_sys = s->p_sys;
937 access_t *p_access = p_sys->p_access;
939 vlc_bool_t b_afastseek;
945 msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
946 "tk=%d start="I64Fd" offset=%d end="I64Fd,
947 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
948 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
949 p_sys->stream.i_offset,
950 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
954 /* Seek in our current track ? */
955 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
956 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
958 //msg_Dbg( s, "AStreamSeekStream: current track" );
959 p_sys->i_pos = i_pos;
960 p_sys->stream.i_offset = i_pos - p_sys->stream.tk[p_sys->stream.i_tk].i_start;
964 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
967 /* We can't do nothing */
968 msg_Dbg( s, "AStreamSeekStream: can't seek" );
972 /* Date the current track */
973 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
975 /* Try to reuse already read data */
976 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
978 stream_track_t *tk = &p_sys->stream.tk[i];
980 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
983 msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
984 " end="I64Fd, i, tk->i_start, tk->i_end );
986 /* Seek at the end of the buffer */
987 if( p_access->pf_seek( p_access, tk->i_end ) )
991 p_sys->i_pos = i_pos;
992 p_sys->stream.i_tk = i;
993 p_sys->stream.i_offset = i_pos - tk->i_start;
995 if( p_sys->stream.i_used < 1024 )
996 p_sys->stream.i_used = 1024;
998 if( AStreamRefillStream( s ) )
1005 access2_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1006 /* FIXME compute seek cost (instead of static 'stupid' value) */
1007 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1013 /* Search closest segment TODO */
1014 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1016 stream_track_t *tk = &p_sys->stream.tk[i];
1018 if( i_pos + i_maxth >= tk->i_start )
1020 msg_Dbg( s, "good segment before current pos, TODO" );
1022 if( i_pos - i_maxth <= tk->i_end )
1024 msg_Dbg( s, "good segment after current pos, TODO" );
1029 /* Nothing good, seek and choose oldest segment */
1030 if( p_access->pf_seek( p_access, i_pos ) )
1031 return VLC_EGENERIC;
1032 p_sys->i_pos = i_pos;
1035 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1037 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1041 /* Reset the segment */
1042 p_sys->stream.i_tk = i_new;
1043 p_sys->stream.i_offset = 0;
1044 p_sys->stream.tk[i_new].i_start = i_pos;
1045 p_sys->stream.tk[i_new].i_end = i_pos;
1048 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1049 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1051 if( AStreamRefillStream( s ) )
1052 return VLC_EGENERIC;
1057 static int AStreamRefillStream( stream_t *s )
1059 stream_sys_t *p_sys = s->p_sys;
1060 access_t *p_access = p_sys->p_access;
1061 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1063 /* We read but won't increase i_start after initial start + offset */
1065 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1066 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1067 vlc_bool_t b_read = VLC_FALSE;
1068 int64_t i_start, i_stop;
1070 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1072 /* msg_Dbg( s, "AStreamRefillStream: toread=%d", i_toread ); */
1075 while( i_toread > 0 )
1077 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1081 return VLC_EGENERIC;
1083 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1084 i_read = p_access->pf_read( p_access, &tk->p_buffer[i_off], i_read );
1086 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1089 msleep( STREAM_DATA_WAIT );
1092 else if( i_read == 0 )
1095 return VLC_EGENERIC;
1101 tk->i_end += i_read;
1103 /* Windows of STREAM_CACHE_TRACK_SIZE */
1104 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1106 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1108 tk->i_start += i_invalid;
1109 p_sys->stream.i_offset -= i_invalid;
1113 p_sys->stream.i_used -= i_read;
1115 p_sys->stat.i_bytes += i_read;
1116 p_sys->stat.i_read_count++;
1120 p_sys->stat.i_read_time += i_stop - i_start;
1125 static void AStreamPrebufferStream( stream_t *s )
1127 stream_sys_t *p_sys = s->p_sys;
1128 access_t *p_access = p_sys->p_access;
1130 int64_t i_first = 0;
1132 int64_t i_prebuffer = (s->p_sys->p_access->info.i_title > 1 ||
1133 s->p_sys->p_access->info.i_seekpoint > 1) ? STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3;
1135 msg_Dbg( s, "pre buffering" );
1139 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1141 int64_t i_date = mdate();
1144 if( s->b_die || tk->i_end >= i_prebuffer ||
1145 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1150 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1151 p_sys->stat.i_read_time = i_date - i_start;
1152 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
1153 (p_sys->stat.i_read_time+1);
1155 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
1157 p_sys->stat.i_bytes,
1158 p_sys->stat.i_read_time / I64C(1000000),
1159 i_byterate / 1024 );
1164 i_read = __MIN( p_sys->stream.i_read_size,
1165 STREAM_CACHE_TRACK_SIZE - tk->i_end );
1166 i_read = p_access->pf_read( p_access, &tk->p_buffer[tk->i_end],
1170 msleep( STREAM_DATA_WAIT );
1173 else if( i_read == 0 )
1182 msg_Dbg( s, "received first data for our buffer");
1185 tk->i_end += i_read;
1187 p_sys->stat.i_read_count++;
1192 /****************************************************************************
1194 ****************************************************************************/
1196 * Read from the stream untill first newline.
1197 * \param s Stream handle to read from
1198 * \return A null-terminated string. This must be freed,
1200 #define STREAM_PROBE_LINE 2048
1201 #define STREAM_LINE_MAX (2048*100)
1202 char *stream_ReadLine( stream_t *s )
1204 char *p_line = NULL;
1205 int i_line = 0, i_read = 0;
1207 while( i_read < STREAM_LINE_MAX )
1213 /* Probe new data */
1214 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1215 if( i_data <= 0 ) break; /* No more data */
1217 /* Check if there is an EOL */
1218 if( ( psz_eol = memchr( p_data, '\n', i_data ) ) )
1220 i_data = (psz_eol - (char *)p_data) + 1;
1221 p_line = realloc( p_line, i_line + i_data + 1 );
1222 i_data = stream_Read( s, &p_line[i_line], i_data );
1223 if( i_data <= 0 ) break; /* Hmmm */
1224 i_line += (i_data - 1);
1227 /* We have our line */
1231 /* Read data (+1 for easy \0 append) */
1232 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + 1 );
1233 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1234 if( i_data <= 0 ) break; /* Hmmm */
1239 /* Remove trailing LF/CR */
1240 while( i_line > 0 && ( p_line[i_line-1] == '\r' ||
1241 p_line[i_line-1] == '\n') ) i_line--;
1245 p_line[i_line] = '\0';
1249 /* We failed to read any data, probably EOF */
1250 if( p_line ) free( p_line );