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"
35 * - tune the 2 methods
36 * - compute cost for seek
37 * - improve stream mode seeking with closest segments
43 * One linked list of data read
45 * More complex scheme using mutliple track to avoid seeking
48 /* How many track we have, currently only used for stream mode */
50 # define STREAM_CACHE_TRACK 1
51 /* Max size of our cache 128Ko per track */
52 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
54 # define STREAM_CACHE_TRACK 3
55 /* Max size of our cache 4Mo per track */
56 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
59 /* How many data we try to prebuffer */
60 #define STREAM_CACHE_PREBUFFER_SIZE (32767)
61 /* Maximum time we take to pre-buffer */
62 #define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
64 /* Method1: Simple, for pf_block.
65 * We get blocks and put them in the linked list.
66 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
68 #define STREAM_DATA_WAIT 40000 /* Time between before a pf_block retry */
70 /* Method2: A bit more complex, for pf_read
71 * - We use ring buffers, only one if unseekable, all if seekable
72 * - Upon seek date current ring, then search if one ring match the pos,
73 * yes: switch to it, seek the access to match the end of the ring
74 * no: search the ring with i_end the closer to i_pos,
75 * if close enough, read data and use this ring
76 * else use the oldest ring, seek and use it.
78 * TODO: - with access non seekable: use all space available for only one ring, but
79 * we have to support seekable/non-seekable switch on the fly.
80 * - compute a good value for i_read_size
83 #define STREAM_READ_ATONCE 32767
84 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
108 vlc_bool_t b_block; /* Block method (1) or stream */
110 int64_t i_pos; /* Current reading offset */
112 /* Method 1: pf_block */
115 int64_t i_start; /* Offset of block for p_first */
116 int i_offset; /* Offset for data in p_current */
117 block_t *p_current; /* Current block */
119 int i_size; /* Total amount of data in the list */
125 /* Method 2: for pf_read */
128 int i_offset; /* Buffer offset in the current track */
129 int i_tk; /* Current track */
130 stream_track_t tk[STREAM_CACHE_TRACK];
136 int i_used; /* Used since last read */
141 /* Peek temporary buffer */
145 /* Stat for both method */
148 vlc_bool_t b_fastseek; /* From access */
150 /* Stat about reading data */
151 int64_t i_read_count;
155 /* Stat about seek */
163 access_entry_t **list;
165 access_t *p_list_access;
167 /* Preparse mode ? */
172 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read );
173 static int AStreamPeekBlock( stream_t *s, uint8_t **p_peek, int i_read );
174 static int AStreamSeekBlock( stream_t *s, int64_t i_pos );
175 static void AStreamPrebufferBlock( stream_t *s );
176 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof );
179 static int AStreamReadStream( stream_t *s, void *p_read, int i_read );
180 static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read );
181 static int AStreamSeekStream( stream_t *s, int64_t i_pos );
182 static void AStreamPrebufferStream( stream_t *s );
183 static int AReadStream( stream_t *s, void *p_read, int i_read );
186 static int AStreamControl( stream_t *s, int i_query, va_list );
187 static int ASeek( stream_t *s, int64_t i_pos );
190 /****************************************************************************
191 * stream_AccessNew: create a stream from a access
192 ****************************************************************************/
193 stream_t *stream_AccessNew( access_t *p_access, vlc_bool_t b_quick )
195 stream_t *s = vlc_object_create( p_access, VLC_OBJECT_STREAM );
199 if( !s ) return NULL;
201 /* Attach it now, needed for b_die */
202 vlc_object_attach( s, p_access );
205 s->pf_read = NULL; /* Set up later */
207 s->pf_control= AStreamControl;
209 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
212 p_sys->p_access = p_access;
213 p_sys->b_block = p_access->pf_block ? VLC_TRUE : VLC_FALSE;
214 p_sys->i_pos = p_access->info.i_pos;
217 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
218 p_sys->stat.i_bytes = 0;
219 p_sys->stat.i_read_time = 0;
220 p_sys->stat.i_read_count = 0;
221 p_sys->stat.i_seek_count = 0;
222 p_sys->stat.i_seek_time = 0;
226 p_sys->i_list_index = 0;
227 p_sys->p_list_access = 0;
229 p_sys->b_quick = b_quick;
231 /* Get the additional list of inputs if any (for concatenation) */
232 if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
234 access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
235 char *psz_name, *psz_parser = psz_name = psz_list;
237 p_sys->p_list_access = p_access;
238 p_entry->i_size = p_access->info.i_size;
239 p_entry->psz_path = strdup( p_access->psz_path );
240 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
241 msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
242 p_entry->psz_path, p_access->info.i_size );
244 while( psz_name && *psz_name )
246 psz_parser = strchr( psz_name, ',' );
247 if( psz_parser ) *psz_parser = 0;
249 psz_name = strdup( psz_name );
252 access_t *p_tmp = access2_New( p_access, p_access->psz_access,
257 psz_name = psz_parser;
258 if( psz_name ) psz_name++;
262 msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
263 psz_name, p_tmp->info.i_size );
265 p_entry = malloc( sizeof(access_entry_t) );
266 p_entry->i_size = p_tmp->info.i_size;
267 p_entry->psz_path = psz_name;
268 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
270 access2_Delete( p_tmp );
273 psz_name = psz_parser;
274 if( psz_name ) psz_name++;
277 if( psz_list ) free( psz_list );
281 p_sys->p_peek = NULL;
285 s->pf_read = AStreamReadBlock;
286 s->pf_peek = AStreamPeekBlock;
288 /* Init all fields of p_sys->block */
289 p_sys->block.i_start = p_sys->i_pos;
290 p_sys->block.i_offset = 0;
291 p_sys->block.p_current = NULL;
292 p_sys->block.i_size = 0;
293 p_sys->block.p_first = NULL;
294 p_sys->block.pp_last = &p_sys->block.p_first;
296 /* Do the prebuffering */
297 AStreamPrebufferBlock( s );
299 if( p_sys->block.i_size <= 0 )
301 msg_Err( s, "cannot pre fill buffer" );
309 s->pf_read = AStreamReadStream;
310 s->pf_peek = AStreamPeekStream;
312 /* Allocate/Setup our tracks */
313 p_sys->stream.i_offset = 0;
314 p_sys->stream.i_tk = 0;
315 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
316 p_sys->stream.i_used = 0;
317 access2_Control( p_access, ACCESS_GET_MTU,
318 &p_sys->stream.i_read_size );
319 if( p_sys->stream.i_read_size <= 0 )
320 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
321 else if( p_sys->stream.i_read_size <= 256 )
322 p_sys->stream.i_read_size = 256;
324 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
326 p_sys->stream.tk[i].i_date = 0;
327 p_sys->stream.tk[i].i_start = p_sys->i_pos;
328 p_sys->stream.tk[i].i_end = p_sys->i_pos;
329 p_sys->stream.tk[i].p_buffer=
330 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
333 /* Do the prebuffering */
334 AStreamPrebufferStream( s );
336 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
338 msg_Err( s, "cannot pre fill buffer" );
352 free( p_sys->stream.p_buffer );
355 vlc_object_detach( s );
356 vlc_object_destroy( s );
360 /****************************************************************************
361 * stream_AccessDelete:
362 ****************************************************************************/
363 void stream_AccessDelete( stream_t *s )
365 stream_sys_t *p_sys = s->p_sys;
367 vlc_object_detach( s );
369 if( p_sys->b_block ) block_ChainRelease( p_sys->block.p_first );
370 else free( p_sys->stream.p_buffer );
372 if( p_sys->p_peek ) free( p_sys->p_peek );
374 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
375 access2_Delete( p_sys->p_list_access );
377 while( p_sys->i_list-- )
379 free( p_sys->list[p_sys->i_list]->psz_path );
380 free( p_sys->list[p_sys->i_list] );
381 if( !p_sys->i_list ) free( p_sys->list );
385 vlc_object_destroy( s );
388 /****************************************************************************
389 * stream_AccessReset:
390 ****************************************************************************/
391 void stream_AccessReset( stream_t *s )
393 stream_sys_t *p_sys = s->p_sys;
395 p_sys->i_pos = p_sys->p_access->info.i_pos;
399 block_ChainRelease( p_sys->block.p_first );
401 /* Init all fields of p_sys->block */
402 p_sys->block.i_start = p_sys->i_pos;
403 p_sys->block.i_offset = 0;
404 p_sys->block.p_current = NULL;
405 p_sys->block.i_size = 0;
406 p_sys->block.p_first = NULL;
407 p_sys->block.pp_last = &p_sys->block.p_first;
409 /* Do the prebuffering */
410 AStreamPrebufferBlock( s );
416 /* Setup our tracks */
417 p_sys->stream.i_offset = 0;
418 p_sys->stream.i_tk = 0;
419 p_sys->stream.i_used = 0;
421 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
423 p_sys->stream.tk[i].i_date = 0;
424 p_sys->stream.tk[i].i_start = p_sys->i_pos;
425 p_sys->stream.tk[i].i_end = p_sys->i_pos;
428 /* Do the prebuffering */
429 AStreamPrebufferStream( s );
433 /****************************************************************************
434 * stream_AccessUpdate:
435 ****************************************************************************/
436 void stream_AccessUpdate( stream_t *s )
438 stream_sys_t *p_sys = s->p_sys;
440 p_sys->i_pos = p_sys->p_access->info.i_pos;
445 for( i = 0; i < p_sys->i_list_index; i++ )
447 p_sys->i_pos += p_sys->list[i]->i_size;
452 /****************************************************************************
454 ****************************************************************************/
455 static int AStreamControl( stream_t *s, int i_query, va_list args )
457 stream_sys_t *p_sys = s->p_sys;
458 access_t *p_access = p_sys->p_access;
461 int64_t *pi_64, i_64;
466 case STREAM_GET_SIZE:
467 pi_64 = (int64_t*)va_arg( args, int64_t * );
468 if( s->p_sys->i_list )
472 for( i = 0; i < s->p_sys->i_list; i++ )
473 *pi_64 += s->p_sys->list[i]->i_size;
476 *pi_64 = p_access->info.i_size;
479 case STREAM_CAN_SEEK:
480 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
481 access2_Control( p_access, ACCESS_CAN_SEEK, p_bool );
484 case STREAM_CAN_FASTSEEK:
485 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
486 access2_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
489 case STREAM_GET_POSITION:
490 pi_64 = (int64_t*)va_arg( args, int64_t * );
491 *pi_64 = p_sys->i_pos;
494 case STREAM_SET_POSITION:
495 i_64 = (int64_t)va_arg( args, int64_t );
497 return AStreamSeekBlock( s, i_64 );
499 return AStreamSeekStream( s, i_64 );
504 case STREAM_CONTROL_ACCESS:
505 i_int = (int) va_arg( args, int );
506 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
507 i_int != ACCESS_SET_PRIVATE_ID_CA &&
508 i_int != ACCESS_GET_PRIVATE_ID_STATE )
510 msg_Err( s, "Hey, what are you thinking ?"
511 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
514 return access2_vaControl( p_access, i_int, args );
517 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
525 /****************************************************************************
527 ****************************************************************************/
528 static void AStreamPrebufferBlock( stream_t *s )
530 stream_sys_t *p_sys = s->p_sys;
535 msg_Dbg( s, "pre buffering" );
539 int64_t i_date = mdate();
543 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
544 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
549 p_sys->stat.i_bytes = p_sys->block.i_size;
550 p_sys->stat.i_read_time = i_date - i_start;
551 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
552 (p_sys->stat.i_read_time + 1);
554 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
557 p_sys->stat.i_read_time / I64C(1000000),
563 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
567 msleep( STREAM_DATA_WAIT );
574 msg_Dbg( s, "received first data for our buffer");
577 /* Append the block */
578 p_sys->block.i_size += b->i_buffer;
579 *p_sys->block.pp_last = b;
580 p_sys->block.pp_last = &b->p_next;
582 p_sys->stat.i_read_count++;
585 p_sys->block.p_current = p_sys->block.p_first;
588 static int AStreamRefillBlock( stream_t *s );
590 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
592 stream_sys_t *p_sys = s->p_sys;
594 uint8_t *p_data= (uint8_t*)p_read;
598 if( p_sys->block.p_current == NULL )
603 /* seek within this stream if possible, else use plain old read and discard */
604 stream_sys_t *p_sys = s->p_sys;
605 access_t *p_access = p_sys->p_access;
607 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
609 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
612 while( i_data < i_read )
615 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
616 int i_copy = __MIN( i_current, i_read - i_data);
622 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
628 p_sys->block.i_offset += i_copy;
629 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
631 /* Current block is now empty, switch to next */
632 if( p_sys->block.p_current )
634 p_sys->block.i_offset = 0;
635 p_sys->block.p_current = p_sys->block.p_current->p_next;
638 if( AStreamRefillBlock( s ) )
645 p_sys->i_pos += i_data;
649 static int AStreamPeekBlock( stream_t *s, uint8_t **pp_peek, int i_read )
651 stream_sys_t *p_sys = s->p_sys;
657 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
659 /* We can directly give a pointer over our buffer */
660 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
662 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
666 /* We need to create a local copy */
667 if( p_sys->i_peek < i_read )
670 free( p_sys->p_peek );
671 p_sys->i_peek = i_read;
672 p_sys->p_peek = malloc( p_sys->i_peek );
675 /* Fill enough data */
676 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
679 block_t **pp_last = p_sys->block.pp_last;
681 if( AStreamRefillBlock( s ) )
684 /* Our buffer are probably filled enough, don't try anymore */
685 if( pp_last == p_sys->block.pp_last )
689 /* Copy what we have */
690 b = p_sys->block.p_current;
691 i_offset = p_sys->block.i_offset;
692 p_data = p_sys->p_peek;
694 while( b && i_data < i_read )
696 int i_current = b->i_buffer - i_offset;
697 int i_copy = __MIN( i_current, i_read - i_data );
699 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
704 if( i_offset >= b->i_buffer )
711 *pp_peek = p_sys->p_peek;
715 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
717 stream_sys_t *p_sys = s->p_sys;
718 access_t *p_access = p_sys->p_access;
719 int64_t i_offset = i_pos - p_sys->block.i_start;
722 /* We already have thoses data, just update p_current/i_offset */
723 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
725 block_t *b = p_sys->block.p_first;
728 while( i_current + b->i_buffer < i_offset )
730 i_current += b->i_buffer;
734 p_sys->block.p_current = b;
735 p_sys->block.i_offset = i_offset - i_current;
737 p_sys->i_pos = i_pos;
742 /* We may need to seek or to read data */
746 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
750 msg_Err( s, "backward seek impossible (access non seekable)" );
758 vlc_bool_t b_aseek, b_aseekfast;
760 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
761 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
766 msg_Warn( s, I64Fd" bytes need to be skipped "
767 "(access non seekable)",
768 i_offset - p_sys->block.i_size );
772 int64_t i_skip = i_offset - p_sys->block.i_size;
774 /* Avg bytes per packets */
775 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
776 /* TODO compute a seek cost instead of fixed threshold */
777 int i_th = b_aseekfast ? 1 : 5;
779 if( i_skip <= i_th * i_avg &&
780 i_skip < STREAM_CACHE_SIZE )
785 msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
786 b_seek, i_th*i_avg, i_skip );
792 int64_t i_start, i_end;
793 /* Do the access seek */
795 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
799 block_ChainRelease( p_sys->block.p_first );
802 p_sys->block.i_start = p_sys->i_pos = i_pos;
803 p_sys->block.i_offset = 0;
804 p_sys->block.p_current = NULL;
805 p_sys->block.i_size = 0;
806 p_sys->block.p_first = NULL;
807 p_sys->block.pp_last = &p_sys->block.p_first;
810 if( AStreamRefillBlock( s ) )
812 msg_Err( s, "cannot re fill buffer" );
816 p_sys->stat.i_seek_time += i_end - i_start;
817 p_sys->stat.i_seek_count++;
822 /* Read enought data */
823 while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
825 if( AStreamRefillBlock( s ) )
827 msg_Err( s, "can't read enough data in seek" );
830 while( p_sys->block.p_current &&
831 p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
833 p_sys->i_pos += p_sys->block.p_current->i_buffer;
834 p_sys->block.p_current = p_sys->block.p_current->p_next;
838 p_sys->block.i_offset = i_pos - p_sys->i_pos;
839 p_sys->i_pos = i_pos;
848 static int AStreamRefillBlock( stream_t *s )
850 stream_sys_t *p_sys = s->p_sys;
851 int64_t i_start, i_stop;
855 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
856 p_sys->block.p_first != p_sys->block.p_current )
858 block_t *b = p_sys->block.p_first;
860 p_sys->block.i_start += b->i_buffer;
861 p_sys->block.i_size -= b->i_buffer;
862 p_sys->block.p_first = b->p_next;
866 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
867 p_sys->block.p_current == p_sys->block.p_first &&
868 p_sys->block.p_current->p_next ) /* At least 2 packets */
870 /* Enough data, don't read more */
874 /* Now read a new block */
880 if( s->b_die ) return VLC_EGENERIC;
884 if( ( b = AReadBlock( s, &b_eof ) ) ) break;
886 if( b_eof ) return VLC_EGENERIC;
888 msleep( STREAM_DATA_WAIT );
892 /* Append the block */
893 p_sys->block.i_size += b->i_buffer;
894 *p_sys->block.pp_last = b;
895 p_sys->block.pp_last = &b->p_next;
898 if( p_sys->block.p_current == NULL )
899 p_sys->block.p_current = b;
902 p_sys->stat.i_bytes += b->i_buffer;
903 p_sys->stat.i_read_time += i_stop - i_start;
904 p_sys->stat.i_read_count++;
910 /****************************************************************************
912 ****************************************************************************/
913 static int AStreamRefillStream( stream_t *s );
915 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
917 stream_sys_t *p_sys = s->p_sys;
918 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
920 uint8_t *p_data = (uint8_t *)p_read;
923 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
927 /* seek within this stream if possible, else use plain old read and discard */
928 stream_sys_t *p_sys = s->p_sys;
929 access_t *p_access = p_sys->p_access;
931 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
933 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
937 msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
938 " offset=%d end="I64Fd,
939 i_read, p_sys->i_pos, p_sys->stream.i_tk,
940 tk->i_start, p_sys->stream.i_offset, tk->i_end );
943 while( i_data < i_read )
945 int i_off = (tk->i_start + p_sys->stream.i_offset) %
946 STREAM_CACHE_TRACK_SIZE;
948 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
949 STREAM_CACHE_TRACK_SIZE - i_off );
950 int i_copy = __MIN( i_current, i_read - i_data );
952 if( i_copy <= 0 ) break; /* EOF */
955 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
958 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
962 p_sys->stream.i_offset += i_copy;
965 p_sys->i_pos += i_copy;
968 p_sys->stream.i_used += i_copy;
969 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
970 p_sys->stream.i_used >= p_sys->stream.i_read_size )
972 if( AStreamRefillStream( s ) )
975 if( tk->i_start >= tk->i_end ) break;
983 static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read )
985 stream_sys_t *p_sys = s->p_sys;
986 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
989 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
992 msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
993 "start="I64Fd" offset=%d end="I64Fd,
994 i_read, p_sys->i_pos, p_sys->stream.i_tk,
995 tk->i_start, p_sys->stream.i_offset, tk->i_end );
998 /* Avoid problem, but that should *never* happen */
999 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1000 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1002 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1004 if( p_sys->stream.i_used <= 1 )
1006 /* Be sure we will read something */
1007 p_sys->stream.i_used += i_read - (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1009 if( AStreamRefillStream( s ) )
1013 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1014 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1016 /* Now, direct pointer or a copy ? */
1017 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1018 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1020 *pp_peek = &tk->p_buffer[i_off];
1024 if( p_sys->i_peek < i_read )
1026 if( p_sys->p_peek ) free( p_sys->p_peek );
1027 p_sys->i_peek = i_read;
1028 p_sys->p_peek = malloc( i_read );
1031 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1032 STREAM_CACHE_TRACK_SIZE - i_off );
1033 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1034 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1036 *pp_peek = p_sys->p_peek;
1040 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1042 stream_sys_t *p_sys = s->p_sys;
1043 access_t *p_access = p_sys->p_access;
1045 vlc_bool_t b_afastseek;
1051 msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
1052 "tk=%d start="I64Fd" offset=%d end="I64Fd,
1053 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1054 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1055 p_sys->stream.i_offset,
1056 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1060 /* Seek in our current track ? */
1061 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1062 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1064 //msg_Dbg( s, "AStreamSeekStream: current track" );
1065 p_sys->i_pos = i_pos;
1066 p_sys->stream.i_offset = i_pos - p_sys->stream.tk[p_sys->stream.i_tk].i_start;
1070 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1073 /* We can't do nothing */
1074 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1075 return VLC_EGENERIC;
1078 /* Date the current track */
1079 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1081 /* Try to reuse already read data */
1082 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1084 stream_track_t *tk = &p_sys->stream.tk[i];
1086 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1089 msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
1090 " end="I64Fd, i, tk->i_start, tk->i_end );
1092 /* Seek at the end of the buffer */
1093 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1096 p_sys->i_pos = i_pos;
1097 p_sys->stream.i_tk = i;
1098 p_sys->stream.i_offset = i_pos - tk->i_start;
1100 if( p_sys->stream.i_used < 1024 )
1101 p_sys->stream.i_used = 1024;
1103 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1104 return VLC_EGENERIC;
1110 access2_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1111 /* FIXME compute seek cost (instead of static 'stupid' value) */
1112 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1118 /* Search closest segment TODO */
1119 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1121 stream_track_t *tk = &p_sys->stream.tk[i];
1123 if( i_pos + i_maxth >= tk->i_start )
1125 msg_Dbg( s, "good segment before current pos, TODO" );
1127 if( i_pos - i_maxth <= tk->i_end )
1129 msg_Dbg( s, "good segment after current pos, TODO" );
1134 /* Nothing good, seek and choose oldest segment */
1135 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1136 p_sys->i_pos = i_pos;
1139 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1141 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1145 /* Reset the segment */
1146 p_sys->stream.i_tk = i_new;
1147 p_sys->stream.i_offset = 0;
1148 p_sys->stream.tk[i_new].i_start = i_pos;
1149 p_sys->stream.tk[i_new].i_end = i_pos;
1152 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1153 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1155 if( AStreamRefillStream( s ) )
1156 return VLC_EGENERIC;
1161 static int AStreamRefillStream( stream_t *s )
1163 stream_sys_t *p_sys = s->p_sys;
1164 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1166 /* We read but won't increase i_start after initial start + offset */
1168 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1169 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1170 vlc_bool_t b_read = VLC_FALSE;
1171 int64_t i_start, i_stop;
1173 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1175 /* msg_Dbg( s, "AStreamRefillStream: toread=%d", i_toread ); */
1178 while( i_toread > 0 )
1180 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1184 return VLC_EGENERIC;
1186 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1187 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1189 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1192 msleep( STREAM_DATA_WAIT );
1195 else if( i_read == 0 )
1197 if( !b_read ) return VLC_EGENERIC;
1203 tk->i_end += i_read;
1205 /* Windows of STREAM_CACHE_TRACK_SIZE */
1206 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1208 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1210 tk->i_start += i_invalid;
1211 p_sys->stream.i_offset -= i_invalid;
1215 p_sys->stream.i_used -= i_read;
1217 p_sys->stat.i_bytes += i_read;
1218 p_sys->stat.i_read_count++;
1222 p_sys->stat.i_read_time += i_stop - i_start;
1227 static void AStreamPrebufferStream( stream_t *s )
1229 stream_sys_t *p_sys = s->p_sys;
1230 access_t *p_access = p_sys->p_access;
1232 int64_t i_first = 0;
1234 int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1235 ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1236 STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1238 msg_Dbg( s, "pre buffering" );
1242 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1244 int64_t i_date = mdate();
1247 if( s->b_die || tk->i_end >= i_prebuffer ||
1248 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1253 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1254 p_sys->stat.i_read_time = i_date - i_start;
1255 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
1256 (p_sys->stat.i_read_time+1);
1258 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
1260 p_sys->stat.i_bytes,
1261 p_sys->stat.i_read_time / I64C(1000000),
1262 i_byterate / 1024 );
1267 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1268 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1269 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1272 msleep( STREAM_DATA_WAIT );
1275 else if( i_read == 0 )
1284 msg_Dbg( s, "received first data for our buffer");
1287 tk->i_end += i_read;
1289 p_sys->stat.i_read_count++;
1294 /****************************************************************************
1296 ****************************************************************************/
1298 * Read from the stream untill first newline.
1299 * \param s Stream handle to read from
1300 * \return A null-terminated string. This must be freed,
1302 #define STREAM_PROBE_LINE 2048
1303 #define STREAM_LINE_MAX (2048*100)
1304 char *stream_ReadLine( stream_t *s )
1306 char *p_line = NULL;
1307 int i_line = 0, i_read = 0;
1309 while( i_read < STREAM_LINE_MAX )
1315 /* Probe new data */
1316 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1317 if( i_data <= 0 ) break; /* No more data */
1319 /* Check if there is an EOL */
1320 if( ( psz_eol = memchr( p_data, '\n', i_data ) ) )
1322 i_data = (psz_eol - (char *)p_data) + 1;
1323 p_line = realloc( p_line, i_line + i_data + 1 );
1324 i_data = stream_Read( s, &p_line[i_line], i_data );
1325 if( i_data <= 0 ) break; /* Hmmm */
1326 i_line += (i_data - 1);
1329 /* We have our line */
1333 /* Read data (+1 for easy \0 append) */
1334 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + 1 );
1335 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1336 if( i_data <= 0 ) break; /* Hmmm */
1341 /* Remove trailing LF/CR */
1342 while( i_line > 0 && ( p_line[i_line-1] == '\r' ||
1343 p_line[i_line-1] == '\n') ) i_line--;
1347 p_line[i_line] = '\0';
1351 /* We failed to read any data, probably EOF */
1352 if( p_line ) free( p_line );
1356 /****************************************************************************
1357 * Access reading/seeking wrappers to handle concatenated streams.
1358 ****************************************************************************/
1359 static int AReadStream( stream_t *s, void *p_read, int i_read )
1361 stream_sys_t *p_sys = s->p_sys;
1362 access_t *p_access = p_sys->p_access;
1363 int i_read_orig = i_read;
1365 if( !p_sys->i_list )
1367 i_read = p_access->pf_read( p_access, p_read, i_read );
1371 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1374 /* If we reached an EOF then switch to the next stream in the list */
1375 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1377 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1378 access_t *p_list_access;
1380 msg_Dbg( s, "opening input `%s'", psz_name );
1382 p_list_access = access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1384 if( !p_list_access ) return 0;
1386 if( p_sys->p_list_access != p_access )
1387 access2_Delete( p_sys->p_list_access );
1389 p_sys->p_list_access = p_list_access;
1391 /* We have to read some data */
1392 return AReadStream( s, p_read, i_read_orig );
1398 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof )
1400 stream_sys_t *p_sys = s->p_sys;
1401 access_t *p_access = p_sys->p_access;
1405 if( !p_sys->i_list )
1407 p_block = p_access->pf_block( p_access );
1408 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1412 p_block = p_sys->p_list_access->pf_block( p_access );
1413 b_eof = p_sys->p_list_access->info.b_eof;
1414 if( pb_eof ) *pb_eof = b_eof;
1416 /* If we reached an EOF then switch to the next stream in the list */
1417 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1419 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1420 access_t *p_list_access;
1422 msg_Dbg( s, "opening input `%s'", psz_name );
1424 p_list_access = access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1426 if( !p_list_access ) return 0;
1428 if( p_sys->p_list_access != p_access )
1429 access2_Delete( p_sys->p_list_access );
1431 p_sys->p_list_access = p_list_access;
1433 /* We have to read some data */
1434 return AReadBlock( s, pb_eof );
1440 static int ASeek( stream_t *s, int64_t i_pos )
1442 stream_sys_t *p_sys = s->p_sys;
1443 access_t *p_access = p_sys->p_access;
1445 /* Check which stream we need to access */
1451 access_t *p_list_access = 0;
1453 for( i = 0; i < p_sys->i_list - 1; i++ )
1455 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1456 i_size += p_sys->list[i]->i_size;
1458 psz_name = p_sys->list[i]->psz_path;
1460 if( i != p_sys->i_list_index )
1461 msg_Dbg( s, "opening input `%s'", psz_name );
1463 if( i != p_sys->i_list_index && i != 0 )
1466 access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1468 else if( i != p_sys->i_list_index )
1470 p_list_access = p_access;
1475 if( p_sys->p_list_access != p_access )
1476 access2_Delete( p_sys->p_list_access );
1478 p_sys->p_list_access = p_list_access;
1481 p_sys->i_list_index = i;
1482 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1486 return p_access->pf_seek( p_access, i_pos );