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 msg_Dbg( s, "received first data for our buffer");
458 /* Append the block */
459 p_sys->block.i_size += b->i_buffer;
460 *p_sys->block.pp_last = b;
461 p_sys->block.pp_last = &b->p_next;
463 p_sys->stat.i_read_count++;
466 p_sys->block.p_current = p_sys->block.p_first;
469 static int AStreamRefillBlock( stream_t *s );
471 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
473 stream_sys_t *p_sys = s->p_sys;
475 uint8_t *p_data= (uint8_t*)p_read;
479 if( p_sys->block.p_current == NULL )
483 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
485 while( i_data < i_read )
488 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
489 int i_copy = __MIN( i_current, i_read - i_data);
495 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
501 p_sys->block.i_offset += i_copy;
502 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
504 /* Current block is now empty, switch to next */
505 if( p_sys->block.p_current )
507 p_sys->block.i_offset = 0;
508 p_sys->block.p_current = p_sys->block.p_current->p_next;
511 if( AStreamRefillBlock( s ) )
518 p_sys->i_pos += i_data;
522 static int AStreamPeekBlock( stream_t *s, uint8_t **pp_peek, int i_read )
524 stream_sys_t *p_sys = s->p_sys;
530 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
532 /* We can directly give a pointer over our buffer */
533 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
535 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
539 /* We need to create a local copy */
540 if( p_sys->i_peek < i_read )
543 free( p_sys->p_peek );
544 p_sys->i_peek = i_read;
545 p_sys->p_peek = malloc( p_sys->i_peek );
548 /* Fill enough data */
549 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
552 block_t **pp_last = p_sys->block.pp_last;
554 if( AStreamRefillBlock( s ) )
557 /* Our buffer are probably filled enough, don't try anymore */
558 if( pp_last == p_sys->block.pp_last )
562 /* Copy what we have */
563 b = p_sys->block.p_current;
564 i_offset = p_sys->block.i_offset;
565 p_data = p_sys->p_peek;
567 while( b && i_data < i_read )
569 int i_current = b->i_buffer - i_offset;
570 int i_copy = __MIN( i_current, i_read - i_data );
572 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
577 if( i_offset >= b->i_buffer )
584 *pp_peek = p_sys->p_peek;
588 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
590 stream_sys_t *p_sys = s->p_sys;
591 access_t *p_access = p_sys->p_access;
592 int64_t i_offset = i_pos - p_sys->block.i_start;
595 /* We already have thoses data, just update p_current/i_offset */
596 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
598 block_t *b = p_sys->block.p_first;
601 while( i_current + b->i_buffer < i_offset )
603 i_current += b->i_buffer;
607 p_sys->block.p_current = b;
608 p_sys->block.i_offset = i_offset - i_current;
610 p_sys->i_pos = i_pos;
615 /* We may need to seek or to read data */
619 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
623 msg_Err( s, "backward seek impossible (access non seekable)" );
631 vlc_bool_t b_aseek, b_aseekfast;
633 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
634 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
639 msg_Warn( s, I64Fd" bytes need to be skipped "
640 "(access non seekable)",
641 i_offset - p_sys->block.i_size );
645 int64_t i_skip = i_offset - p_sys->block.i_size;
647 /* Avg bytes per packets */
648 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
649 /* TODO compute a seek cost instead of fixed threshold */
650 int i_th = b_aseekfast ? 1 : 5;
652 if( i_skip <= i_th * i_avg &&
653 i_skip < STREAM_CACHE_SIZE )
658 msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
659 b_seek, i_th*i_avg, i_skip );
665 int64_t i_start, i_end;
666 /* Do the access seek */
668 if( p_access->pf_seek( p_access, i_pos ) )
673 block_ChainRelease( p_sys->block.p_first );
676 p_sys->block.i_start = p_sys->i_pos = i_pos;
677 p_sys->block.i_offset = 0;
678 p_sys->block.p_current = NULL;
679 p_sys->block.i_size = 0;
680 p_sys->block.p_first = NULL;
681 p_sys->block.pp_last = &p_sys->block.p_first;
684 if( AStreamRefillBlock( s ) )
686 msg_Err( s, "cannot re fill buffer" );
690 p_sys->stat.i_seek_time += i_end - i_start;
691 p_sys->stat.i_seek_count++;
696 /* Read enought data */
697 while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
699 if( AStreamRefillBlock( s ) )
701 msg_Err( s, "can't read enough data in seek" );
704 while( p_sys->block.p_current &&
705 p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
707 p_sys->i_pos += p_sys->block.p_current->i_buffer;
708 p_sys->block.p_current = p_sys->block.p_current->p_next;
712 p_sys->block.i_offset = i_pos - p_sys->i_pos;
713 p_sys->i_pos = i_pos;
722 static int AStreamRefillBlock( stream_t *s )
724 stream_sys_t *p_sys = s->p_sys;
725 access_t *p_access = p_sys->p_access;
726 int64_t i_start, i_stop;
730 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
731 p_sys->block.p_first != p_sys->block.p_current )
733 block_t *b = p_sys->block.p_first;
735 p_sys->block.i_start += b->i_buffer;
736 p_sys->block.i_size -= b->i_buffer;
737 p_sys->block.p_first = b->p_next;
741 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
742 p_sys->block.p_current == p_sys->block.p_first &&
743 p_sys->block.p_current->p_next ) /* At least 2 packets */
745 /* Enough data, don't read more */
749 /* Now read a new block */
758 if( ( b = p_access->pf_block( p_access ) ) )
761 if( p_access->info.b_eof )
764 msleep( STREAM_DATA_WAIT );
768 /* Append the block */
769 p_sys->block.i_size += b->i_buffer;
770 *p_sys->block.pp_last = b;
771 p_sys->block.pp_last = &b->p_next;
774 if( p_sys->block.p_current == NULL )
775 p_sys->block.p_current = b;
778 p_sys->stat.i_bytes += b->i_buffer;
779 p_sys->stat.i_read_time += i_stop - i_start;
780 p_sys->stat.i_read_count++;
786 /****************************************************************************
788 ****************************************************************************/
789 static int AStreamRefillStream( stream_t *s );
791 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
793 stream_sys_t *p_sys = s->p_sys;
794 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
796 uint8_t *p_data = (uint8_t *)p_read;
799 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
802 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
805 msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
806 " offset=%d end="I64Fd,
807 i_read, p_sys->i_pos, p_sys->stream.i_tk,
808 tk->i_start, p_sys->stream.i_offset, tk->i_end );
811 while( i_data < i_read )
813 int i_off = (tk->i_start + p_sys->stream.i_offset) %
814 STREAM_CACHE_TRACK_SIZE;
816 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
817 STREAM_CACHE_TRACK_SIZE - i_off );
818 int i_copy = __MIN( i_current, i_read - i_data );
820 if( i_copy <= 0 ) break; /* EOF */
823 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
826 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
830 p_sys->stream.i_offset += i_copy;
833 p_sys->i_pos += i_copy;
836 p_sys->stream.i_used += i_copy;
837 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
838 p_sys->stream.i_used >= p_sys->stream.i_read_size )
840 if( AStreamRefillStream( s ) )
843 if( tk->i_start >= tk->i_end ) break;
851 static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read )
853 stream_sys_t *p_sys = s->p_sys;
854 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
857 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
860 msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
861 "start="I64Fd" offset=%d end="I64Fd,
862 i_read, p_sys->i_pos, p_sys->stream.i_tk,
863 tk->i_start, p_sys->stream.i_offset, tk->i_end );
866 /* Avoid problem, but that should *never* happen */
867 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
868 i_read = STREAM_CACHE_TRACK_SIZE / 2;
870 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
872 if( p_sys->stream.i_used <= 1 )
874 /* Be sure we will read something */
875 p_sys->stream.i_used += i_read - (tk->i_end - tk->i_start - p_sys->stream.i_offset);
877 if( AStreamRefillStream( s ) )
881 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
882 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
884 /* Now, direct pointer or a copy ? */
885 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
886 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
888 *pp_peek = &tk->p_buffer[i_off];
892 if( p_sys->i_peek < i_read )
894 if( p_sys->p_peek ) free( p_sys->p_peek );
895 p_sys->i_peek = i_read;
896 p_sys->p_peek = malloc( i_read );
899 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
900 STREAM_CACHE_TRACK_SIZE - i_off );
901 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
902 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
904 *pp_peek = p_sys->p_peek;
908 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
910 stream_sys_t *p_sys = s->p_sys;
911 access_t *p_access = p_sys->p_access;
913 vlc_bool_t b_afastseek;
919 msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
920 "tk=%d start="I64Fd" offset=%d end="I64Fd,
921 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
922 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
923 p_sys->stream.i_offset,
924 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
928 /* Seek in our current track ? */
929 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
930 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
932 //msg_Dbg( s, "AStreamSeekStream: current track" );
933 p_sys->i_pos = i_pos;
934 p_sys->stream.i_offset = i_pos - p_sys->stream.tk[p_sys->stream.i_tk].i_start;
938 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
941 /* We can't do nothing */
945 /* Date the current track */
946 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
948 /* Try to reuse already read data */
949 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
951 stream_track_t *tk = &p_sys->stream.tk[i];
953 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
956 msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
957 " end="I64Fd, i, tk->i_start, tk->i_end );
959 /* Seek at the end of the buffer */
960 if( p_access->pf_seek( p_access, tk->i_end ) )
964 p_sys->i_pos = i_pos;
965 p_sys->stream.i_tk = i;
966 p_sys->stream.i_offset = i_pos - tk->i_start;
968 if( p_sys->stream.i_used < 1024 )
969 p_sys->stream.i_used = 1024;
971 if( AStreamRefillStream( s ) )
978 access2_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
979 /* FIXME compute seek cost (instead of static 'stupid' value) */
980 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
986 /* Search closest segment TODO */
987 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
989 stream_track_t *tk = &p_sys->stream.tk[i];
991 if( i_pos + i_maxth >= tk->i_start )
993 msg_Dbg( s, "good segment before current pos, TODO" );
995 if( i_pos - i_maxth <= tk->i_end )
997 msg_Dbg( s, "good segment after current pos, TODO" );
1002 /* Nothing good, seek and choose oldest segment */
1003 if( p_access->pf_seek( p_access, i_pos ) )
1004 return VLC_EGENERIC;
1005 p_sys->i_pos = i_pos;
1008 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1010 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1014 /* Reset the segment */
1015 p_sys->stream.i_tk = i_new;
1016 p_sys->stream.i_offset = 0;
1017 p_sys->stream.tk[i_new].i_start = i_pos;
1018 p_sys->stream.tk[i_new].i_end = i_pos;
1021 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1022 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1024 if( AStreamRefillStream( s ) )
1025 return VLC_EGENERIC;
1030 static int AStreamRefillStream( stream_t *s )
1032 stream_sys_t *p_sys = s->p_sys;
1033 access_t *p_access = p_sys->p_access;
1034 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1036 /* We read but won't increase i_start after initial start + offset */
1038 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1039 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1040 vlc_bool_t b_read = VLC_FALSE;
1041 int64_t i_start, i_stop;
1043 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1045 /* msg_Dbg( s, "AStreamRefillStream: toread=%d", i_toread ); */
1048 while( i_toread > 0 )
1050 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1054 return VLC_EGENERIC;
1056 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1057 i_read = p_access->pf_read( p_access, &tk->p_buffer[i_off], i_read );
1059 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1062 msleep( STREAM_DATA_WAIT );
1065 else if( i_read == 0 )
1068 return VLC_EGENERIC;
1074 tk->i_end += i_read;
1076 /* Windows of STREAM_CACHE_TRACK_SIZE */
1077 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1079 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1081 tk->i_start += i_invalid;
1082 p_sys->stream.i_offset -= i_invalid;
1086 p_sys->stream.i_used -= i_read;
1088 p_sys->stat.i_bytes += i_read;
1089 p_sys->stat.i_read_count++;
1093 p_sys->stat.i_read_time += i_stop - i_start;
1098 static void AStreamPrebufferStream( stream_t *s )
1100 stream_sys_t *p_sys = s->p_sys;
1101 access_t *p_access = p_sys->p_access;
1103 int64_t i_first = 0;
1105 int64_t i_prebuffer = (s->p_sys->p_access->info.i_title > 1 ||
1106 s->p_sys->p_access->info.i_seekpoint > 1) ? STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3;
1108 msg_Dbg( s, "pre buffering" );
1112 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1114 int64_t i_date = mdate();
1117 if( s->b_die || tk->i_end >= i_prebuffer ||
1118 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1123 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1124 p_sys->stat.i_read_time = i_date - i_start;
1125 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
1126 (p_sys->stat.i_read_time+1);
1128 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
1130 p_sys->stat.i_bytes,
1131 p_sys->stat.i_read_time / I64C(1000000),
1132 i_byterate / 1024 );
1137 i_read = __MIN( p_sys->stream.i_read_size,
1138 STREAM_CACHE_TRACK_SIZE - tk->i_end );
1139 i_read = p_access->pf_read( p_access, &tk->p_buffer[tk->i_end],
1143 msleep( STREAM_DATA_WAIT );
1146 else if( i_read == 0 )
1155 msg_Dbg( s, "received first data for our buffer");
1158 tk->i_end += i_read;
1160 p_sys->stat.i_read_count++;
1165 /****************************************************************************
1167 ****************************************************************************/
1169 * Read from the stream untill first newline.
1170 * \param s Stream handle to read from
1171 * \return A null-terminated string. This must be freed,
1173 #define STREAM_PROBE_LINE 2048
1174 #define STREAM_LINE_MAX (2048*100)
1175 char *stream_ReadLine( stream_t *s )
1177 char *p_line = NULL;
1178 int i_line = 0, i_read = 0;
1180 while( i_read < STREAM_LINE_MAX )
1186 /* Probe new data */
1187 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1188 if( i_data <= 0 ) break; /* No more data */
1190 /* Check if there is an EOL */
1191 if( ( psz_eol = memchr( p_data, '\n', i_data ) ) )
1193 i_data = (psz_eol - (char *)p_data) + 1;
1194 p_line = realloc( p_line, i_line + i_data + 1 );
1195 i_data = stream_Read( s, &p_line[i_line], i_data );
1196 if( i_data <= 0 ) break; /* Hmmm */
1197 i_line += (i_data - 1);
1200 /* We have our line */
1204 /* Read data (+1 for easy \0 append) */
1205 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + 1 );
1206 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1207 if( i_data <= 0 ) break; /* Hmmm */
1212 /* Remove trailing LF/CR */
1213 while( i_line > 0 && ( p_line[i_line-1] == '\r' ||
1214 p_line[i_line-1] == '\n') ) i_line--;
1218 p_line[i_line] = '\0';
1222 /* We failed to read any data, probably EOF */
1223 if( p_line ) free( p_line );