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 )
509 msg_Err( s, "Hey, what are you thinking ?"
510 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
513 return access2_vaControl( p_access, i_int, args );
516 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
524 /****************************************************************************
526 ****************************************************************************/
527 static void AStreamPrebufferBlock( stream_t *s )
529 stream_sys_t *p_sys = s->p_sys;
534 msg_Dbg( s, "pre buffering" );
538 int64_t i_date = mdate();
542 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
543 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
548 p_sys->stat.i_bytes = p_sys->block.i_size;
549 p_sys->stat.i_read_time = i_date - i_start;
550 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
551 (p_sys->stat.i_read_time + 1);
553 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
556 p_sys->stat.i_read_time / I64C(1000000),
562 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
566 msleep( STREAM_DATA_WAIT );
573 msg_Dbg( s, "received first data for our buffer");
576 /* Append the block */
577 p_sys->block.i_size += b->i_buffer;
578 *p_sys->block.pp_last = b;
579 p_sys->block.pp_last = &b->p_next;
581 p_sys->stat.i_read_count++;
584 p_sys->block.p_current = p_sys->block.p_first;
587 static int AStreamRefillBlock( stream_t *s );
589 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
591 stream_sys_t *p_sys = s->p_sys;
593 uint8_t *p_data= (uint8_t*)p_read;
597 if( p_sys->block.p_current == NULL )
602 /* seek within this stream if possible, else use plain old read and discard */
603 stream_sys_t *p_sys = s->p_sys;
604 access_t *p_access = p_sys->p_access;
606 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
608 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
611 while( i_data < i_read )
614 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
615 int i_copy = __MIN( i_current, i_read - i_data);
621 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
627 p_sys->block.i_offset += i_copy;
628 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
630 /* Current block is now empty, switch to next */
631 if( p_sys->block.p_current )
633 p_sys->block.i_offset = 0;
634 p_sys->block.p_current = p_sys->block.p_current->p_next;
637 if( AStreamRefillBlock( s ) )
644 p_sys->i_pos += i_data;
648 static int AStreamPeekBlock( stream_t *s, uint8_t **pp_peek, int i_read )
650 stream_sys_t *p_sys = s->p_sys;
656 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
658 /* We can directly give a pointer over our buffer */
659 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
661 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
665 /* We need to create a local copy */
666 if( p_sys->i_peek < i_read )
669 free( p_sys->p_peek );
670 p_sys->i_peek = i_read;
671 p_sys->p_peek = malloc( p_sys->i_peek );
674 /* Fill enough data */
675 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
678 block_t **pp_last = p_sys->block.pp_last;
680 if( AStreamRefillBlock( s ) )
683 /* Our buffer are probably filled enough, don't try anymore */
684 if( pp_last == p_sys->block.pp_last )
688 /* Copy what we have */
689 b = p_sys->block.p_current;
690 i_offset = p_sys->block.i_offset;
691 p_data = p_sys->p_peek;
693 while( b && i_data < i_read )
695 int i_current = b->i_buffer - i_offset;
696 int i_copy = __MIN( i_current, i_read - i_data );
698 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
703 if( i_offset >= b->i_buffer )
710 *pp_peek = p_sys->p_peek;
714 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
716 stream_sys_t *p_sys = s->p_sys;
717 access_t *p_access = p_sys->p_access;
718 int64_t i_offset = i_pos - p_sys->block.i_start;
721 /* We already have thoses data, just update p_current/i_offset */
722 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
724 block_t *b = p_sys->block.p_first;
727 while( i_current + b->i_buffer < i_offset )
729 i_current += b->i_buffer;
733 p_sys->block.p_current = b;
734 p_sys->block.i_offset = i_offset - i_current;
736 p_sys->i_pos = i_pos;
741 /* We may need to seek or to read data */
745 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
749 msg_Err( s, "backward seek impossible (access non seekable)" );
757 vlc_bool_t b_aseek, b_aseekfast;
759 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
760 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
765 msg_Warn( s, I64Fd" bytes need to be skipped "
766 "(access non seekable)",
767 i_offset - p_sys->block.i_size );
771 int64_t i_skip = i_offset - p_sys->block.i_size;
773 /* Avg bytes per packets */
774 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
775 /* TODO compute a seek cost instead of fixed threshold */
776 int i_th = b_aseekfast ? 1 : 5;
778 if( i_skip <= i_th * i_avg &&
779 i_skip < STREAM_CACHE_SIZE )
784 msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
785 b_seek, i_th*i_avg, i_skip );
791 int64_t i_start, i_end;
792 /* Do the access seek */
794 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
798 block_ChainRelease( p_sys->block.p_first );
801 p_sys->block.i_start = p_sys->i_pos = i_pos;
802 p_sys->block.i_offset = 0;
803 p_sys->block.p_current = NULL;
804 p_sys->block.i_size = 0;
805 p_sys->block.p_first = NULL;
806 p_sys->block.pp_last = &p_sys->block.p_first;
809 if( AStreamRefillBlock( s ) )
811 msg_Err( s, "cannot re fill buffer" );
815 p_sys->stat.i_seek_time += i_end - i_start;
816 p_sys->stat.i_seek_count++;
821 /* Read enought data */
822 while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
824 if( AStreamRefillBlock( s ) )
826 msg_Err( s, "can't read enough data in seek" );
829 while( p_sys->block.p_current &&
830 p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
832 p_sys->i_pos += p_sys->block.p_current->i_buffer;
833 p_sys->block.p_current = p_sys->block.p_current->p_next;
837 p_sys->block.i_offset = i_pos - p_sys->i_pos;
838 p_sys->i_pos = i_pos;
847 static int AStreamRefillBlock( stream_t *s )
849 stream_sys_t *p_sys = s->p_sys;
850 int64_t i_start, i_stop;
854 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
855 p_sys->block.p_first != p_sys->block.p_current )
857 block_t *b = p_sys->block.p_first;
859 p_sys->block.i_start += b->i_buffer;
860 p_sys->block.i_size -= b->i_buffer;
861 p_sys->block.p_first = b->p_next;
865 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
866 p_sys->block.p_current == p_sys->block.p_first &&
867 p_sys->block.p_current->p_next ) /* At least 2 packets */
869 /* Enough data, don't read more */
873 /* Now read a new block */
879 if( s->b_die ) return VLC_EGENERIC;
883 if( ( b = AReadBlock( s, &b_eof ) ) ) break;
885 if( b_eof ) return VLC_EGENERIC;
887 msleep( STREAM_DATA_WAIT );
891 /* Append the block */
892 p_sys->block.i_size += b->i_buffer;
893 *p_sys->block.pp_last = b;
894 p_sys->block.pp_last = &b->p_next;
897 if( p_sys->block.p_current == NULL )
898 p_sys->block.p_current = b;
901 p_sys->stat.i_bytes += b->i_buffer;
902 p_sys->stat.i_read_time += i_stop - i_start;
903 p_sys->stat.i_read_count++;
909 /****************************************************************************
911 ****************************************************************************/
912 static int AStreamRefillStream( stream_t *s );
914 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
916 stream_sys_t *p_sys = s->p_sys;
917 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
919 uint8_t *p_data = (uint8_t *)p_read;
922 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
926 /* seek within this stream if possible, else use plain old read and discard */
927 stream_sys_t *p_sys = s->p_sys;
928 access_t *p_access = p_sys->p_access;
930 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
932 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
936 msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
937 " offset=%d end="I64Fd,
938 i_read, p_sys->i_pos, p_sys->stream.i_tk,
939 tk->i_start, p_sys->stream.i_offset, tk->i_end );
942 while( i_data < i_read )
944 int i_off = (tk->i_start + p_sys->stream.i_offset) %
945 STREAM_CACHE_TRACK_SIZE;
947 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
948 STREAM_CACHE_TRACK_SIZE - i_off );
949 int i_copy = __MIN( i_current, i_read - i_data );
951 if( i_copy <= 0 ) break; /* EOF */
954 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
957 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
961 p_sys->stream.i_offset += i_copy;
964 p_sys->i_pos += i_copy;
967 p_sys->stream.i_used += i_copy;
968 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
969 p_sys->stream.i_used >= p_sys->stream.i_read_size )
971 if( AStreamRefillStream( s ) )
974 if( tk->i_start >= tk->i_end ) break;
982 static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read )
984 stream_sys_t *p_sys = s->p_sys;
985 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
988 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
991 msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
992 "start="I64Fd" offset=%d end="I64Fd,
993 i_read, p_sys->i_pos, p_sys->stream.i_tk,
994 tk->i_start, p_sys->stream.i_offset, tk->i_end );
997 /* Avoid problem, but that should *never* happen */
998 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
999 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1001 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1003 if( p_sys->stream.i_used <= 1 )
1005 /* Be sure we will read something */
1006 p_sys->stream.i_used += i_read - (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1008 if( AStreamRefillStream( s ) )
1012 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1013 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1015 /* Now, direct pointer or a copy ? */
1016 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1017 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1019 *pp_peek = &tk->p_buffer[i_off];
1023 if( p_sys->i_peek < i_read )
1025 if( p_sys->p_peek ) free( p_sys->p_peek );
1026 p_sys->i_peek = i_read;
1027 p_sys->p_peek = malloc( i_read );
1030 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1031 STREAM_CACHE_TRACK_SIZE - i_off );
1032 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1033 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1035 *pp_peek = p_sys->p_peek;
1039 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1041 stream_sys_t *p_sys = s->p_sys;
1042 access_t *p_access = p_sys->p_access;
1044 vlc_bool_t b_afastseek;
1050 msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
1051 "tk=%d start="I64Fd" offset=%d end="I64Fd,
1052 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1053 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1054 p_sys->stream.i_offset,
1055 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1059 /* Seek in our current track ? */
1060 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1061 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1063 //msg_Dbg( s, "AStreamSeekStream: current track" );
1064 p_sys->i_pos = i_pos;
1065 p_sys->stream.i_offset = i_pos - p_sys->stream.tk[p_sys->stream.i_tk].i_start;
1069 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1072 /* We can't do nothing */
1073 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1074 return VLC_EGENERIC;
1077 /* Date the current track */
1078 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1080 /* Try to reuse already read data */
1081 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1083 stream_track_t *tk = &p_sys->stream.tk[i];
1085 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1088 msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
1089 " end="I64Fd, i, tk->i_start, tk->i_end );
1091 /* Seek at the end of the buffer */
1092 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1095 p_sys->i_pos = i_pos;
1096 p_sys->stream.i_tk = i;
1097 p_sys->stream.i_offset = i_pos - tk->i_start;
1099 if( p_sys->stream.i_used < 1024 )
1100 p_sys->stream.i_used = 1024;
1102 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1103 return VLC_EGENERIC;
1109 access2_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1110 /* FIXME compute seek cost (instead of static 'stupid' value) */
1111 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1117 /* Search closest segment TODO */
1118 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1120 stream_track_t *tk = &p_sys->stream.tk[i];
1122 if( i_pos + i_maxth >= tk->i_start )
1124 msg_Dbg( s, "good segment before current pos, TODO" );
1126 if( i_pos - i_maxth <= tk->i_end )
1128 msg_Dbg( s, "good segment after current pos, TODO" );
1133 /* Nothing good, seek and choose oldest segment */
1134 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1135 p_sys->i_pos = i_pos;
1138 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1140 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1144 /* Reset the segment */
1145 p_sys->stream.i_tk = i_new;
1146 p_sys->stream.i_offset = 0;
1147 p_sys->stream.tk[i_new].i_start = i_pos;
1148 p_sys->stream.tk[i_new].i_end = i_pos;
1151 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1152 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1154 if( AStreamRefillStream( s ) )
1155 return VLC_EGENERIC;
1160 static int AStreamRefillStream( stream_t *s )
1162 stream_sys_t *p_sys = s->p_sys;
1163 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1165 /* We read but won't increase i_start after initial start + offset */
1167 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1168 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1169 vlc_bool_t b_read = VLC_FALSE;
1170 int64_t i_start, i_stop;
1172 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1174 /* msg_Dbg( s, "AStreamRefillStream: toread=%d", i_toread ); */
1177 while( i_toread > 0 )
1179 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1183 return VLC_EGENERIC;
1185 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1186 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1188 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1191 msleep( STREAM_DATA_WAIT );
1194 else if( i_read == 0 )
1196 if( !b_read ) return VLC_EGENERIC;
1202 tk->i_end += i_read;
1204 /* Windows of STREAM_CACHE_TRACK_SIZE */
1205 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1207 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1209 tk->i_start += i_invalid;
1210 p_sys->stream.i_offset -= i_invalid;
1214 p_sys->stream.i_used -= i_read;
1216 p_sys->stat.i_bytes += i_read;
1217 p_sys->stat.i_read_count++;
1221 p_sys->stat.i_read_time += i_stop - i_start;
1226 static void AStreamPrebufferStream( stream_t *s )
1228 stream_sys_t *p_sys = s->p_sys;
1229 access_t *p_access = p_sys->p_access;
1231 int64_t i_first = 0;
1233 int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1234 ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1235 STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1237 msg_Dbg( s, "pre buffering" );
1241 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1243 int64_t i_date = mdate();
1246 if( s->b_die || tk->i_end >= i_prebuffer ||
1247 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1252 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1253 p_sys->stat.i_read_time = i_date - i_start;
1254 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
1255 (p_sys->stat.i_read_time+1);
1257 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
1259 p_sys->stat.i_bytes,
1260 p_sys->stat.i_read_time / I64C(1000000),
1261 i_byterate / 1024 );
1266 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1267 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1268 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1271 msleep( STREAM_DATA_WAIT );
1274 else if( i_read == 0 )
1283 msg_Dbg( s, "received first data for our buffer");
1286 tk->i_end += i_read;
1288 p_sys->stat.i_read_count++;
1293 /****************************************************************************
1295 ****************************************************************************/
1297 * Read from the stream untill first newline.
1298 * \param s Stream handle to read from
1299 * \return A null-terminated string. This must be freed,
1301 #define STREAM_PROBE_LINE 2048
1302 #define STREAM_LINE_MAX (2048*100)
1303 char *stream_ReadLine( stream_t *s )
1305 char *p_line = NULL;
1306 int i_line = 0, i_read = 0;
1308 while( i_read < STREAM_LINE_MAX )
1314 /* Probe new data */
1315 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1316 if( i_data <= 0 ) break; /* No more data */
1318 /* Check if there is an EOL */
1319 if( ( psz_eol = memchr( p_data, '\n', i_data ) ) )
1321 i_data = (psz_eol - (char *)p_data) + 1;
1322 p_line = realloc( p_line, i_line + i_data + 1 );
1323 i_data = stream_Read( s, &p_line[i_line], i_data );
1324 if( i_data <= 0 ) break; /* Hmmm */
1325 i_line += (i_data - 1);
1328 /* We have our line */
1332 /* Read data (+1 for easy \0 append) */
1333 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + 1 );
1334 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1335 if( i_data <= 0 ) break; /* Hmmm */
1340 /* Remove trailing LF/CR */
1341 while( i_line > 0 && ( p_line[i_line-1] == '\r' ||
1342 p_line[i_line-1] == '\n') ) i_line--;
1346 p_line[i_line] = '\0';
1350 /* We failed to read any data, probably EOF */
1351 if( p_line ) free( p_line );
1355 /****************************************************************************
1356 * Access reading/seeking wrappers to handle concatenated streams.
1357 ****************************************************************************/
1358 static int AReadStream( stream_t *s, void *p_read, int i_read )
1360 stream_sys_t *p_sys = s->p_sys;
1361 access_t *p_access = p_sys->p_access;
1362 int i_read_orig = i_read;
1364 if( !p_sys->i_list )
1366 i_read = p_access->pf_read( p_access, p_read, i_read );
1370 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1373 /* If we reached an EOF then switch to the next stream in the list */
1374 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1376 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1377 access_t *p_list_access;
1379 msg_Dbg( s, "opening input `%s'", psz_name );
1381 p_list_access = access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1383 if( !p_list_access ) return 0;
1385 if( p_sys->p_list_access != p_access )
1386 access2_Delete( p_sys->p_list_access );
1388 p_sys->p_list_access = p_list_access;
1390 /* We have to read some data */
1391 return AReadStream( s, p_read, i_read_orig );
1397 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof )
1399 stream_sys_t *p_sys = s->p_sys;
1400 access_t *p_access = p_sys->p_access;
1404 if( !p_sys->i_list )
1406 p_block = p_access->pf_block( p_access );
1407 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1411 p_block = p_sys->p_list_access->pf_block( p_access );
1412 b_eof = p_sys->p_list_access->info.b_eof;
1413 if( pb_eof ) *pb_eof = b_eof;
1415 /* If we reached an EOF then switch to the next stream in the list */
1416 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1418 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1419 access_t *p_list_access;
1421 msg_Dbg( s, "opening input `%s'", psz_name );
1423 p_list_access = access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1425 if( !p_list_access ) return 0;
1427 if( p_sys->p_list_access != p_access )
1428 access2_Delete( p_sys->p_list_access );
1430 p_sys->p_list_access = p_list_access;
1432 /* We have to read some data */
1433 return AReadBlock( s, pb_eof );
1439 static int ASeek( stream_t *s, int64_t i_pos )
1441 stream_sys_t *p_sys = s->p_sys;
1442 access_t *p_access = p_sys->p_access;
1444 /* Check which stream we need to access */
1450 access_t *p_list_access = 0;
1452 for( i = 0; i < p_sys->i_list - 1; i++ )
1454 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1455 i_size += p_sys->list[i]->i_size;
1457 psz_name = p_sys->list[i]->psz_path;
1459 if( i != p_sys->i_list_index )
1460 msg_Dbg( s, "opening input `%s'", psz_name );
1462 if( i != p_sys->i_list_index && i != 0 )
1465 access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1467 else if( i != p_sys->i_list_index )
1469 p_list_access = p_access;
1474 if( p_sys->p_list_access != p_access )
1475 access2_Delete( p_sys->p_list_access );
1477 p_sys->p_list_access = p_list_access;
1480 p_sys->i_list_index = i;
1481 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1485 return p_access->pf_seek( p_access, i_pos );