1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
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 tracks we have, currently only used for stream mode */
45 #ifdef OPTIMIZE_MEMORY
46 # define STREAM_CACHE_TRACK 1
47 /* Max size of our cache 128Ko per track */
48 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
50 # define STREAM_CACHE_TRACK 3
51 /* Max size of our cache 4Mo per track */
52 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
55 /* How many data we try to prebuffer */
56 #define STREAM_CACHE_PREBUFFER_SIZE (32767)
57 /* Maximum time we take to pre-buffer */
58 #define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
60 /* Method1: Simple, for pf_block.
61 * We get blocks and put them in the linked list.
62 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
64 #define STREAM_DATA_WAIT 40000 /* Time between before a pf_block retry */
66 /* Method2: A bit more complex, for pf_read
67 * - We use ring buffers, only one if unseekable, all if seekable
68 * - Upon seek date current ring, then search if one ring match the pos,
69 * yes: switch to it, seek the access to match the end of the ring
70 * no: search the ring with i_end the closer to i_pos,
71 * if close enough, read data and use this ring
72 * else use the oldest ring, seek and use it.
74 * TODO: - with access non seekable: use all space available for only one ring, but
75 * we have to support seekable/non-seekable switch on the fly.
76 * - compute a good value for i_read_size
79 #define STREAM_READ_ATONCE 32767
80 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
104 vlc_bool_t b_block; /* Block method (1) or stream */
106 int64_t i_pos; /* Current reading offset */
108 /* Method 1: pf_block */
111 int64_t i_start; /* Offset of block for p_first */
112 int i_offset; /* Offset for data in p_current */
113 block_t *p_current; /* Current block */
115 int i_size; /* Total amount of data in the list */
121 /* Method 2: for pf_read */
124 int i_offset; /* Buffer offset in the current track */
125 int i_tk; /* Current track */
126 stream_track_t tk[STREAM_CACHE_TRACK];
132 int i_used; /* Used since last read */
137 /* Peek temporary buffer */
141 /* Stat for both method */
144 vlc_bool_t b_fastseek; /* From access */
146 /* Stat about reading data */
147 int64_t i_read_count;
151 /* Stat about seek */
159 access_entry_t **list;
161 access_t *p_list_access;
163 /* Preparse mode ? */
168 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read );
169 static int AStreamPeekBlock( stream_t *s, uint8_t **p_peek, int i_read );
170 static int AStreamSeekBlock( stream_t *s, int64_t i_pos );
171 static void AStreamPrebufferBlock( stream_t *s );
172 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof );
175 static int AStreamReadStream( stream_t *s, void *p_read, int i_read );
176 static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read );
177 static int AStreamSeekStream( stream_t *s, int64_t i_pos );
178 static void AStreamPrebufferStream( stream_t *s );
179 static int AReadStream( stream_t *s, void *p_read, int i_read );
182 static int AStreamControl( stream_t *s, int i_query, va_list );
183 static void AStreamDestroy( stream_t *s );
184 static void UStreamDestroy( stream_t *s );
185 static int ASeek( stream_t *s, int64_t i_pos );
188 /****************************************************************************
189 * stream_UrlNew: create a stream from a access
190 ****************************************************************************/
191 stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
193 char *psz_access, *psz_demux, *psz_path, *psz_dup;
197 if( !psz_url ) return 0;
199 psz_dup = strdup( psz_url );
200 MRLSplit( p_parent, psz_dup, &psz_access, &psz_demux, &psz_path );
202 /* Now try a real access */
203 p_access = access2_New( p_parent, psz_access, psz_demux, psz_path, 0 );
206 if( p_access == NULL )
208 msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
212 if( !( p_res = stream_AccessNew( p_access, VLC_TRUE ) ) )
214 access2_Delete( p_access );
218 p_res->pf_destroy = UStreamDestroy;
222 stream_t *stream_AccessNew( access_t *p_access, vlc_bool_t b_quick )
224 stream_t *s = vlc_object_create( p_access, VLC_OBJECT_STREAM );
228 if( !s ) return NULL;
230 /* Attach it now, needed for b_die */
231 vlc_object_attach( s, p_access );
234 s->pf_read = NULL; /* Set up later */
236 s->pf_control= AStreamControl;
237 s->pf_destroy = AStreamDestroy;
239 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
242 p_sys->p_access = p_access;
243 p_sys->b_block = p_access->pf_block ? VLC_TRUE : VLC_FALSE;
244 p_sys->i_pos = p_access->info.i_pos;
247 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
248 p_sys->stat.i_bytes = 0;
249 p_sys->stat.i_read_time = 0;
250 p_sys->stat.i_read_count = 0;
251 p_sys->stat.i_seek_count = 0;
252 p_sys->stat.i_seek_time = 0;
256 p_sys->i_list_index = 0;
257 p_sys->p_list_access = 0;
259 p_sys->b_quick = b_quick;
261 /* Get the additional list of inputs if any (for concatenation) */
262 if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
264 access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
265 char *psz_name, *psz_parser = psz_name = psz_list;
267 p_sys->p_list_access = p_access;
268 p_entry->i_size = p_access->info.i_size;
269 p_entry->psz_path = strdup( p_access->psz_path );
270 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
271 msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
272 p_entry->psz_path, p_access->info.i_size );
274 while( psz_name && *psz_name )
276 psz_parser = strchr( psz_name, ',' );
277 if( psz_parser ) *psz_parser = 0;
279 psz_name = strdup( psz_name );
282 access_t *p_tmp = access2_New( p_access, p_access->psz_access,
287 psz_name = psz_parser;
288 if( psz_name ) psz_name++;
292 msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
293 psz_name, p_tmp->info.i_size );
295 p_entry = malloc( sizeof(access_entry_t) );
296 p_entry->i_size = p_tmp->info.i_size;
297 p_entry->psz_path = psz_name;
298 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
300 access2_Delete( p_tmp );
303 psz_name = psz_parser;
304 if( psz_name ) psz_name++;
307 if( psz_list ) free( psz_list );
311 p_sys->p_peek = NULL;
315 s->pf_read = AStreamReadBlock;
316 s->pf_peek = AStreamPeekBlock;
318 /* Init all fields of p_sys->block */
319 p_sys->block.i_start = p_sys->i_pos;
320 p_sys->block.i_offset = 0;
321 p_sys->block.p_current = NULL;
322 p_sys->block.i_size = 0;
323 p_sys->block.p_first = NULL;
324 p_sys->block.pp_last = &p_sys->block.p_first;
326 /* Do the prebuffering */
327 AStreamPrebufferBlock( s );
329 if( p_sys->block.i_size <= 0 )
331 msg_Err( s, "cannot pre fill buffer" );
339 s->pf_read = AStreamReadStream;
340 s->pf_peek = AStreamPeekStream;
342 /* Allocate/Setup our tracks */
343 p_sys->stream.i_offset = 0;
344 p_sys->stream.i_tk = 0;
345 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
346 p_sys->stream.i_used = 0;
347 access2_Control( p_access, ACCESS_GET_MTU,
348 &p_sys->stream.i_read_size );
349 if( p_sys->stream.i_read_size <= 0 )
350 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
351 else if( p_sys->stream.i_read_size <= 256 )
352 p_sys->stream.i_read_size = 256;
354 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
356 p_sys->stream.tk[i].i_date = 0;
357 p_sys->stream.tk[i].i_start = p_sys->i_pos;
358 p_sys->stream.tk[i].i_end = p_sys->i_pos;
359 p_sys->stream.tk[i].p_buffer=
360 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
363 /* Do the prebuffering */
364 AStreamPrebufferStream( s );
366 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
368 msg_Err( s, "cannot pre fill buffer" );
382 free( p_sys->stream.p_buffer );
385 vlc_object_detach( s );
386 vlc_object_destroy( s );
390 /****************************************************************************
392 ****************************************************************************/
393 static void AStreamDestroy( stream_t *s )
395 stream_sys_t *p_sys = s->p_sys;
397 vlc_object_detach( s );
399 if( p_sys->b_block ) block_ChainRelease( p_sys->block.p_first );
400 else free( p_sys->stream.p_buffer );
402 if( p_sys->p_peek ) free( p_sys->p_peek );
404 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
405 access2_Delete( p_sys->p_list_access );
407 while( p_sys->i_list-- )
409 free( p_sys->list[p_sys->i_list]->psz_path );
410 free( p_sys->list[p_sys->i_list] );
411 if( !p_sys->i_list ) free( p_sys->list );
415 vlc_object_destroy( s );
418 static void UStreamDestroy( stream_t *s )
420 access_t *p_access = (access_t*)vlc_object_find( s, VLC_OBJECT_ACCESS, FIND_PARENT );
422 vlc_object_release( p_access );
423 access2_Delete( p_access );
426 /****************************************************************************
427 * stream_AccessReset:
428 ****************************************************************************/
429 void stream_AccessReset( stream_t *s )
431 stream_sys_t *p_sys = s->p_sys;
433 p_sys->i_pos = p_sys->p_access->info.i_pos;
437 block_ChainRelease( p_sys->block.p_first );
439 /* Init all fields of p_sys->block */
440 p_sys->block.i_start = p_sys->i_pos;
441 p_sys->block.i_offset = 0;
442 p_sys->block.p_current = NULL;
443 p_sys->block.i_size = 0;
444 p_sys->block.p_first = NULL;
445 p_sys->block.pp_last = &p_sys->block.p_first;
447 /* Do the prebuffering */
448 AStreamPrebufferBlock( s );
454 /* Setup our tracks */
455 p_sys->stream.i_offset = 0;
456 p_sys->stream.i_tk = 0;
457 p_sys->stream.i_used = 0;
459 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
461 p_sys->stream.tk[i].i_date = 0;
462 p_sys->stream.tk[i].i_start = p_sys->i_pos;
463 p_sys->stream.tk[i].i_end = p_sys->i_pos;
466 /* Do the prebuffering */
467 AStreamPrebufferStream( s );
471 /****************************************************************************
472 * stream_AccessUpdate:
473 ****************************************************************************/
474 void stream_AccessUpdate( stream_t *s )
476 stream_sys_t *p_sys = s->p_sys;
478 p_sys->i_pos = p_sys->p_access->info.i_pos;
483 for( i = 0; i < p_sys->i_list_index; i++ )
485 p_sys->i_pos += p_sys->list[i]->i_size;
490 /****************************************************************************
492 ****************************************************************************/
493 static int AStreamControl( stream_t *s, int i_query, va_list args )
495 stream_sys_t *p_sys = s->p_sys;
496 access_t *p_access = p_sys->p_access;
499 int64_t *pi_64, i_64;
504 case STREAM_GET_SIZE:
505 pi_64 = (int64_t*)va_arg( args, int64_t * );
506 if( s->p_sys->i_list )
510 for( i = 0; i < s->p_sys->i_list; i++ )
511 *pi_64 += s->p_sys->list[i]->i_size;
514 *pi_64 = p_access->info.i_size;
517 case STREAM_CAN_SEEK:
518 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
519 access2_Control( p_access, ACCESS_CAN_SEEK, p_bool );
522 case STREAM_CAN_FASTSEEK:
523 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
524 access2_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
527 case STREAM_GET_POSITION:
528 pi_64 = (int64_t*)va_arg( args, int64_t * );
529 *pi_64 = p_sys->i_pos;
532 case STREAM_SET_POSITION:
533 i_64 = (int64_t)va_arg( args, int64_t );
535 return AStreamSeekBlock( s, i_64 );
537 return AStreamSeekStream( s, i_64 );
542 case STREAM_CONTROL_ACCESS:
543 i_int = (int) va_arg( args, int );
544 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
545 i_int != ACCESS_SET_PRIVATE_ID_CA &&
546 i_int != ACCESS_GET_PRIVATE_ID_STATE )
548 msg_Err( s, "Hey, what are you thinking ?"
549 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
552 return access2_vaControl( p_access, i_int, args );
555 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
563 /****************************************************************************
565 ****************************************************************************/
566 static void AStreamPrebufferBlock( stream_t *s )
568 stream_sys_t *p_sys = s->p_sys;
573 msg_Dbg( s, "pre buffering" );
577 int64_t i_date = mdate();
581 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
582 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
587 p_sys->stat.i_bytes = p_sys->block.i_size;
588 p_sys->stat.i_read_time = i_date - i_start;
589 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
590 (p_sys->stat.i_read_time + 1);
592 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
595 p_sys->stat.i_read_time / I64C(1000000),
601 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
605 msleep( STREAM_DATA_WAIT );
612 msg_Dbg( s, "received first data for our buffer");
617 /* Append the block */
618 p_sys->block.i_size += b->i_buffer;
619 *p_sys->block.pp_last = b;
620 p_sys->block.pp_last = &b->p_next;
622 p_sys->stat.i_read_count++;
627 p_sys->block.p_current = p_sys->block.p_first;
630 static int AStreamRefillBlock( stream_t *s );
632 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
634 stream_sys_t *p_sys = s->p_sys;
636 uint8_t *p_data= (uint8_t*)p_read;
640 if( p_sys->block.p_current == NULL )
645 /* seek within this stream if possible, else use plain old read and discard */
646 stream_sys_t *p_sys = s->p_sys;
647 access_t *p_access = p_sys->p_access;
649 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
651 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
654 while( i_data < i_read )
657 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
658 int i_copy = __MIN( i_current, i_read - i_data);
664 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
670 p_sys->block.i_offset += i_copy;
671 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
673 /* Current block is now empty, switch to next */
674 if( p_sys->block.p_current )
676 p_sys->block.i_offset = 0;
677 p_sys->block.p_current = p_sys->block.p_current->p_next;
680 if( AStreamRefillBlock( s ) )
687 p_sys->i_pos += i_data;
691 static int AStreamPeekBlock( stream_t *s, uint8_t **pp_peek, int i_read )
693 stream_sys_t *p_sys = s->p_sys;
699 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
701 /* We can directly give a pointer over our buffer */
702 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
704 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
708 /* We need to create a local copy */
709 if( p_sys->i_peek < i_read )
711 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
717 p_sys->i_peek = i_read;
720 /* Fill enough data */
721 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
724 block_t **pp_last = p_sys->block.pp_last;
726 if( AStreamRefillBlock( s ) ) break;
728 /* Our buffer are probably filled enough, don't try anymore */
729 if( pp_last == p_sys->block.pp_last ) break;
732 /* Copy what we have */
733 b = p_sys->block.p_current;
734 i_offset = p_sys->block.i_offset;
735 p_data = p_sys->p_peek;
737 while( b && i_data < i_read )
739 int i_current = b->i_buffer - i_offset;
740 int i_copy = __MIN( i_current, i_read - i_data );
742 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
747 if( i_offset >= b->i_buffer )
754 *pp_peek = p_sys->p_peek;
758 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
760 stream_sys_t *p_sys = s->p_sys;
761 access_t *p_access = p_sys->p_access;
762 int64_t i_offset = i_pos - p_sys->block.i_start;
765 /* We already have thoses data, just update p_current/i_offset */
766 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
768 block_t *b = p_sys->block.p_first;
771 while( i_current + b->i_buffer < i_offset )
773 i_current += b->i_buffer;
777 p_sys->block.p_current = b;
778 p_sys->block.i_offset = i_offset - i_current;
780 p_sys->i_pos = i_pos;
785 /* We may need to seek or to read data */
789 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
793 msg_Err( s, "backward seek impossible (access non seekable)" );
801 vlc_bool_t b_aseek, b_aseekfast;
803 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
804 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
809 msg_Warn( s, I64Fd" bytes need to be skipped "
810 "(access non seekable)",
811 i_offset - p_sys->block.i_size );
815 int64_t i_skip = i_offset - p_sys->block.i_size;
817 /* Avg bytes per packets */
818 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
819 /* TODO compute a seek cost instead of fixed threshold */
820 int i_th = b_aseekfast ? 1 : 5;
822 if( i_skip <= i_th * i_avg &&
823 i_skip < STREAM_CACHE_SIZE )
828 msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
829 b_seek, i_th*i_avg, i_skip );
835 int64_t i_start, i_end;
836 /* Do the access seek */
838 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
842 block_ChainRelease( p_sys->block.p_first );
845 p_sys->block.i_start = p_sys->i_pos = i_pos;
846 p_sys->block.i_offset = 0;
847 p_sys->block.p_current = NULL;
848 p_sys->block.i_size = 0;
849 p_sys->block.p_first = NULL;
850 p_sys->block.pp_last = &p_sys->block.p_first;
853 if( AStreamRefillBlock( s ) )
855 msg_Err( s, "cannot re fill buffer" );
859 p_sys->stat.i_seek_time += i_end - i_start;
860 p_sys->stat.i_seek_count++;
865 /* Read enought data */
866 while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
868 if( AStreamRefillBlock( s ) )
870 msg_Err( s, "can't read enough data in seek" );
873 while( p_sys->block.p_current &&
874 p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
876 p_sys->i_pos += p_sys->block.p_current->i_buffer;
877 p_sys->block.p_current = p_sys->block.p_current->p_next;
881 p_sys->block.i_offset = i_pos - p_sys->i_pos;
882 p_sys->i_pos = i_pos;
891 static int AStreamRefillBlock( stream_t *s )
893 stream_sys_t *p_sys = s->p_sys;
894 int64_t i_start, i_stop;
898 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
899 p_sys->block.p_first != p_sys->block.p_current )
901 block_t *b = p_sys->block.p_first;
903 p_sys->block.i_start += b->i_buffer;
904 p_sys->block.i_size -= b->i_buffer;
905 p_sys->block.p_first = b->p_next;
909 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
910 p_sys->block.p_current == p_sys->block.p_first &&
911 p_sys->block.p_current->p_next ) /* At least 2 packets */
913 /* Enough data, don't read more */
917 /* Now read a new block */
923 if( s->b_die ) return VLC_EGENERIC;
927 if( ( b = AReadBlock( s, &b_eof ) ) ) break;
929 if( b_eof ) return VLC_EGENERIC;
931 msleep( STREAM_DATA_WAIT );
938 /* Append the block */
939 p_sys->block.i_size += b->i_buffer;
940 *p_sys->block.pp_last = b;
941 p_sys->block.pp_last = &b->p_next;
944 if( p_sys->block.p_current == NULL )
945 p_sys->block.p_current = b;
948 p_sys->stat.i_bytes += b->i_buffer;
949 p_sys->stat.i_read_time += i_stop - i_start;
950 p_sys->stat.i_read_count++;
959 /****************************************************************************
961 ****************************************************************************/
962 static int AStreamRefillStream( stream_t *s );
964 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
966 stream_sys_t *p_sys = s->p_sys;
967 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
969 uint8_t *p_data = (uint8_t *)p_read;
972 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
976 /* seek within this stream if possible, else use plain old read and discard */
977 stream_sys_t *p_sys = s->p_sys;
978 access_t *p_access = p_sys->p_access;
980 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
982 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
986 msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
987 " offset=%d end="I64Fd,
988 i_read, p_sys->i_pos, p_sys->stream.i_tk,
989 tk->i_start, p_sys->stream.i_offset, tk->i_end );
992 while( i_data < i_read )
994 int i_off = (tk->i_start + p_sys->stream.i_offset) %
995 STREAM_CACHE_TRACK_SIZE;
997 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
998 STREAM_CACHE_TRACK_SIZE - i_off );
999 int i_copy = __MIN( i_current, i_read - i_data );
1001 if( i_copy <= 0 ) break; /* EOF */
1004 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1007 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1011 p_sys->stream.i_offset += i_copy;
1013 /* Update pos now */
1014 p_sys->i_pos += i_copy;
1017 p_sys->stream.i_used += i_copy;
1018 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1019 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1021 if( AStreamRefillStream( s ) )
1024 if( tk->i_start >= tk->i_end ) break;
1032 static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read )
1034 stream_sys_t *p_sys = s->p_sys;
1035 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1038 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1041 msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
1042 "start="I64Fd" offset=%d end="I64Fd,
1043 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1044 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1047 /* Avoid problem, but that should *never* happen */
1048 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1049 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1051 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1053 if( p_sys->stream.i_used <= 1 )
1055 /* Be sure we will read something */
1056 p_sys->stream.i_used += i_read -
1057 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1059 if( AStreamRefillStream( s ) ) break;
1062 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1063 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1065 /* Now, direct pointer or a copy ? */
1066 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1067 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1069 *pp_peek = &tk->p_buffer[i_off];
1073 if( p_sys->i_peek < i_read )
1075 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1076 if( !p_sys->p_peek )
1081 p_sys->i_peek = i_read;
1084 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1085 STREAM_CACHE_TRACK_SIZE - i_off );
1086 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1087 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1089 *pp_peek = p_sys->p_peek;
1093 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1095 stream_sys_t *p_sys = s->p_sys;
1096 access_t *p_access = p_sys->p_access;
1098 vlc_bool_t b_afastseek;
1104 msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
1105 "tk=%d start="I64Fd" offset=%d end="I64Fd,
1106 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1107 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1108 p_sys->stream.i_offset,
1109 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1113 /* Seek in our current track ? */
1114 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1115 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1117 //msg_Dbg( s, "AStreamSeekStream: current track" );
1118 p_sys->i_pos = i_pos;
1119 p_sys->stream.i_offset = i_pos - p_sys->stream.tk[p_sys->stream.i_tk].i_start;
1123 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1126 /* We can't do nothing */
1127 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1128 return VLC_EGENERIC;
1131 /* Date the current track */
1132 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1134 /* Try to reuse already read data */
1135 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1137 stream_track_t *tk = &p_sys->stream.tk[i];
1139 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1142 msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
1143 " end="I64Fd, i, tk->i_start, tk->i_end );
1145 /* Seek at the end of the buffer */
1146 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1149 p_sys->i_pos = i_pos;
1150 p_sys->stream.i_tk = i;
1151 p_sys->stream.i_offset = i_pos - tk->i_start;
1153 if( p_sys->stream.i_used < 1024 )
1154 p_sys->stream.i_used = 1024;
1156 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1157 return VLC_EGENERIC;
1163 access2_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1164 /* FIXME compute seek cost (instead of static 'stupid' value) */
1165 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1171 /* Search closest segment TODO */
1172 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1174 stream_track_t *tk = &p_sys->stream.tk[i];
1176 if( i_pos + i_maxth >= tk->i_start )
1178 msg_Dbg( s, "good segment before current pos, TODO" );
1180 if( i_pos - i_maxth <= tk->i_end )
1182 msg_Dbg( s, "good segment after current pos, TODO" );
1187 /* Nothing good, seek and choose oldest segment */
1188 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1189 p_sys->i_pos = i_pos;
1192 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1194 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1198 /* Reset the segment */
1199 p_sys->stream.i_tk = i_new;
1200 p_sys->stream.i_offset = 0;
1201 p_sys->stream.tk[i_new].i_start = i_pos;
1202 p_sys->stream.tk[i_new].i_end = i_pos;
1205 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1206 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1208 if( AStreamRefillStream( s ) )
1209 return VLC_EGENERIC;
1214 static int AStreamRefillStream( stream_t *s )
1216 stream_sys_t *p_sys = s->p_sys;
1217 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1219 /* We read but won't increase i_start after initial start + offset */
1221 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1222 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1223 vlc_bool_t b_read = VLC_FALSE;
1224 int64_t i_start, i_stop;
1226 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1228 /* msg_Dbg( s, "AStreamRefillStream: toread=%d", i_toread ); */
1231 while( i_toread > 0 )
1233 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1237 return VLC_EGENERIC;
1239 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1240 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1242 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1245 msleep( STREAM_DATA_WAIT );
1248 else if( i_read == 0 )
1250 if( !b_read ) return VLC_EGENERIC;
1256 tk->i_end += i_read;
1258 /* Windows of STREAM_CACHE_TRACK_SIZE */
1259 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1261 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1263 tk->i_start += i_invalid;
1264 p_sys->stream.i_offset -= i_invalid;
1268 p_sys->stream.i_used -= i_read;
1270 p_sys->stat.i_bytes += i_read;
1271 p_sys->stat.i_read_count++;
1275 p_sys->stat.i_read_time += i_stop - i_start;
1280 static void AStreamPrebufferStream( stream_t *s )
1282 stream_sys_t *p_sys = s->p_sys;
1283 access_t *p_access = p_sys->p_access;
1285 int64_t i_first = 0;
1287 int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1288 ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1289 STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1291 msg_Dbg( s, "pre buffering" );
1295 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1297 int64_t i_date = mdate();
1300 if( s->b_die || tk->i_end >= i_prebuffer ||
1301 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1306 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1307 p_sys->stat.i_read_time = i_date - i_start;
1308 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
1309 (p_sys->stat.i_read_time+1);
1311 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
1313 p_sys->stat.i_bytes,
1314 p_sys->stat.i_read_time / I64C(1000000),
1315 i_byterate / 1024 );
1320 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1321 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1322 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1325 msleep( STREAM_DATA_WAIT );
1328 else if( i_read == 0 )
1337 msg_Dbg( s, "received first data for our buffer");
1340 tk->i_end += i_read;
1342 p_sys->stat.i_read_count++;
1347 /****************************************************************************
1349 ****************************************************************************/
1351 * Read from the stream untill first newline.
1352 * \param s Stream handle to read from
1353 * \return A null-terminated string. This must be freed,
1355 #define STREAM_PROBE_LINE 2048
1356 #define STREAM_LINE_MAX (2048*100)
1357 char *stream_ReadLine( stream_t *s )
1359 char *p_line = NULL;
1360 int i_line = 0, i_read = 0;
1362 while( i_read < STREAM_LINE_MAX )
1368 /* Probe new data */
1369 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1370 if( i_data <= 0 ) break; /* No more data */
1372 /* Check if there is an EOL */
1373 if( ( psz_eol = memchr( p_data, '\n', i_data ) ) )
1375 i_data = (psz_eol - (char *)p_data) + 1;
1376 p_line = realloc( p_line, i_line + i_data + 1 );
1377 i_data = stream_Read( s, &p_line[i_line], i_data );
1378 if( i_data <= 0 ) break; /* Hmmm */
1379 i_line += (i_data - 1);
1382 /* We have our line */
1386 /* Read data (+1 for easy \0 append) */
1387 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + 1 );
1388 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1389 if( i_data <= 0 ) break; /* Hmmm */
1394 /* Remove trailing LF/CR */
1395 while( i_line > 0 && ( p_line[i_line-1] == '\r' ||
1396 p_line[i_line-1] == '\n') ) i_line--;
1400 p_line[i_line] = '\0';
1404 /* We failed to read any data, probably EOF */
1405 if( p_line ) free( p_line );
1409 /****************************************************************************
1410 * Access reading/seeking wrappers to handle concatenated streams.
1411 ****************************************************************************/
1412 static int AReadStream( stream_t *s, void *p_read, int i_read )
1414 stream_sys_t *p_sys = s->p_sys;
1415 access_t *p_access = p_sys->p_access;
1416 int i_read_orig = i_read;
1418 if( !p_sys->i_list )
1420 i_read = p_access->pf_read( p_access, p_read, i_read );
1424 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1427 /* If we reached an EOF then switch to the next stream in the list */
1428 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1430 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1431 access_t *p_list_access;
1433 msg_Dbg( s, "opening input `%s'", psz_name );
1435 p_list_access = access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1437 if( !p_list_access ) return 0;
1439 if( p_sys->p_list_access != p_access )
1440 access2_Delete( p_sys->p_list_access );
1442 p_sys->p_list_access = p_list_access;
1444 /* We have to read some data */
1445 return AReadStream( s, p_read, i_read_orig );
1451 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof )
1453 stream_sys_t *p_sys = s->p_sys;
1454 access_t *p_access = p_sys->p_access;
1458 if( !p_sys->i_list )
1460 p_block = p_access->pf_block( p_access );
1461 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1465 p_block = p_sys->p_list_access->pf_block( p_access );
1466 b_eof = p_sys->p_list_access->info.b_eof;
1467 if( pb_eof ) *pb_eof = b_eof;
1469 /* If we reached an EOF then switch to the next stream in the list */
1470 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1472 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1473 access_t *p_list_access;
1475 msg_Dbg( s, "opening input `%s'", psz_name );
1477 p_list_access = access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1479 if( !p_list_access ) return 0;
1481 if( p_sys->p_list_access != p_access )
1482 access2_Delete( p_sys->p_list_access );
1484 p_sys->p_list_access = p_list_access;
1486 /* We have to read some data */
1487 return AReadBlock( s, pb_eof );
1493 static int ASeek( stream_t *s, int64_t i_pos )
1495 stream_sys_t *p_sys = s->p_sys;
1496 access_t *p_access = p_sys->p_access;
1498 /* Check which stream we need to access */
1504 access_t *p_list_access = 0;
1506 for( i = 0; i < p_sys->i_list - 1; i++ )
1508 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1509 i_size += p_sys->list[i]->i_size;
1511 psz_name = p_sys->list[i]->psz_path;
1513 if( i != p_sys->i_list_index )
1514 msg_Dbg( s, "opening input `%s'", psz_name );
1516 if( i != p_sys->i_list_index && i != 0 )
1519 access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1521 else if( i != p_sys->i_list_index )
1523 p_list_access = p_access;
1528 if( p_sys->p_list_access != p_access )
1529 access2_Delete( p_sys->p_list_access );
1531 p_sys->p_list_access = p_list_access;
1534 p_sys->i_list_index = i;
1535 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1539 return p_access->pf_seek( p_access, i_pos );