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 ) );
241 /* UTF16 and UTF32 text file conversion */
243 s->b_little_endian = VLC_FALSE;
246 p_sys->p_access = p_access;
247 p_sys->b_block = p_access->pf_block ? VLC_TRUE : VLC_FALSE;
248 p_sys->i_pos = p_access->info.i_pos;
251 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
252 p_sys->stat.i_bytes = 0;
253 p_sys->stat.i_read_time = 0;
254 p_sys->stat.i_read_count = 0;
255 p_sys->stat.i_seek_count = 0;
256 p_sys->stat.i_seek_time = 0;
260 p_sys->i_list_index = 0;
261 p_sys->p_list_access = 0;
263 p_sys->b_quick = b_quick;
265 /* Get the additional list of inputs if any (for concatenation) */
266 if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
268 access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
269 char *psz_name, *psz_parser = psz_name = psz_list;
271 p_sys->p_list_access = p_access;
272 p_entry->i_size = p_access->info.i_size;
273 p_entry->psz_path = strdup( p_access->psz_path );
274 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
275 msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
276 p_entry->psz_path, p_access->info.i_size );
278 while( psz_name && *psz_name )
280 psz_parser = strchr( psz_name, ',' );
281 if( psz_parser ) *psz_parser = 0;
283 psz_name = strdup( psz_name );
286 access_t *p_tmp = access2_New( p_access, p_access->psz_access,
291 psz_name = psz_parser;
292 if( psz_name ) psz_name++;
296 msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
297 psz_name, p_tmp->info.i_size );
299 p_entry = malloc( sizeof(access_entry_t) );
300 p_entry->i_size = p_tmp->info.i_size;
301 p_entry->psz_path = psz_name;
302 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
304 access2_Delete( p_tmp );
307 psz_name = psz_parser;
308 if( psz_name ) psz_name++;
311 if( psz_list ) free( psz_list );
315 p_sys->p_peek = NULL;
319 s->pf_read = AStreamReadBlock;
320 s->pf_peek = AStreamPeekBlock;
322 /* Init all fields of p_sys->block */
323 p_sys->block.i_start = p_sys->i_pos;
324 p_sys->block.i_offset = 0;
325 p_sys->block.p_current = NULL;
326 p_sys->block.i_size = 0;
327 p_sys->block.p_first = NULL;
328 p_sys->block.pp_last = &p_sys->block.p_first;
330 /* Do the prebuffering */
331 AStreamPrebufferBlock( s );
333 if( p_sys->block.i_size <= 0 )
335 msg_Err( s, "cannot pre fill buffer" );
343 s->pf_read = AStreamReadStream;
344 s->pf_peek = AStreamPeekStream;
346 /* Allocate/Setup our tracks */
347 p_sys->stream.i_offset = 0;
348 p_sys->stream.i_tk = 0;
349 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
350 p_sys->stream.i_used = 0;
351 access2_Control( p_access, ACCESS_GET_MTU,
352 &p_sys->stream.i_read_size );
353 if( p_sys->stream.i_read_size <= 0 )
354 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
355 else if( p_sys->stream.i_read_size <= 256 )
356 p_sys->stream.i_read_size = 256;
358 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
360 p_sys->stream.tk[i].i_date = 0;
361 p_sys->stream.tk[i].i_start = p_sys->i_pos;
362 p_sys->stream.tk[i].i_end = p_sys->i_pos;
363 p_sys->stream.tk[i].p_buffer=
364 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
367 /* Do the prebuffering */
368 AStreamPrebufferStream( s );
370 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
372 msg_Err( s, "cannot pre fill buffer" );
386 free( p_sys->stream.p_buffer );
389 vlc_object_detach( s );
390 vlc_object_destroy( s );
394 /****************************************************************************
396 ****************************************************************************/
397 static void AStreamDestroy( stream_t *s )
399 stream_sys_t *p_sys = s->p_sys;
401 vlc_object_detach( s );
403 if( p_sys->b_block ) block_ChainRelease( p_sys->block.p_first );
404 else free( p_sys->stream.p_buffer );
406 if( p_sys->p_peek ) free( p_sys->p_peek );
408 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
409 access2_Delete( p_sys->p_list_access );
411 while( p_sys->i_list-- )
413 free( p_sys->list[p_sys->i_list]->psz_path );
414 free( p_sys->list[p_sys->i_list] );
415 if( !p_sys->i_list ) free( p_sys->list );
419 vlc_object_destroy( s );
422 static void UStreamDestroy( stream_t *s )
424 access_t *p_access = (access_t*)vlc_object_find( s, VLC_OBJECT_ACCESS, FIND_PARENT );
426 vlc_object_release( p_access );
427 access2_Delete( p_access );
430 /****************************************************************************
431 * stream_AccessReset:
432 ****************************************************************************/
433 void stream_AccessReset( stream_t *s )
435 stream_sys_t *p_sys = s->p_sys;
437 p_sys->i_pos = p_sys->p_access->info.i_pos;
441 block_ChainRelease( p_sys->block.p_first );
443 /* Init all fields of p_sys->block */
444 p_sys->block.i_start = p_sys->i_pos;
445 p_sys->block.i_offset = 0;
446 p_sys->block.p_current = NULL;
447 p_sys->block.i_size = 0;
448 p_sys->block.p_first = NULL;
449 p_sys->block.pp_last = &p_sys->block.p_first;
451 /* Do the prebuffering */
452 AStreamPrebufferBlock( s );
458 /* Setup our tracks */
459 p_sys->stream.i_offset = 0;
460 p_sys->stream.i_tk = 0;
461 p_sys->stream.i_used = 0;
463 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
465 p_sys->stream.tk[i].i_date = 0;
466 p_sys->stream.tk[i].i_start = p_sys->i_pos;
467 p_sys->stream.tk[i].i_end = p_sys->i_pos;
470 /* Do the prebuffering */
471 AStreamPrebufferStream( s );
475 /****************************************************************************
476 * stream_AccessUpdate:
477 ****************************************************************************/
478 void stream_AccessUpdate( stream_t *s )
480 stream_sys_t *p_sys = s->p_sys;
482 p_sys->i_pos = p_sys->p_access->info.i_pos;
487 for( i = 0; i < p_sys->i_list_index; i++ )
489 p_sys->i_pos += p_sys->list[i]->i_size;
494 /****************************************************************************
496 ****************************************************************************/
497 static int AStreamControl( stream_t *s, int i_query, va_list args )
499 stream_sys_t *p_sys = s->p_sys;
500 access_t *p_access = p_sys->p_access;
503 int64_t *pi_64, i_64;
508 case STREAM_GET_SIZE:
509 pi_64 = (int64_t*)va_arg( args, int64_t * );
510 if( s->p_sys->i_list )
514 for( i = 0; i < s->p_sys->i_list; i++ )
515 *pi_64 += s->p_sys->list[i]->i_size;
518 *pi_64 = p_access->info.i_size;
521 case STREAM_CAN_SEEK:
522 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
523 access2_Control( p_access, ACCESS_CAN_SEEK, p_bool );
526 case STREAM_CAN_FASTSEEK:
527 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
528 access2_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
531 case STREAM_GET_POSITION:
532 pi_64 = (int64_t*)va_arg( args, int64_t * );
533 *pi_64 = p_sys->i_pos;
536 case STREAM_SET_POSITION:
537 i_64 = (int64_t)va_arg( args, int64_t );
539 return AStreamSeekBlock( s, i_64 );
541 return AStreamSeekStream( s, i_64 );
546 case STREAM_CONTROL_ACCESS:
547 i_int = (int) va_arg( args, int );
548 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
549 i_int != ACCESS_SET_PRIVATE_ID_CA &&
550 i_int != ACCESS_GET_PRIVATE_ID_STATE )
552 msg_Err( s, "Hey, what are you thinking ?"
553 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
556 return access2_vaControl( p_access, i_int, args );
559 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
567 /****************************************************************************
569 ****************************************************************************/
570 static void AStreamPrebufferBlock( stream_t *s )
572 stream_sys_t *p_sys = s->p_sys;
573 access_t *p_access = p_sys->p_access;
578 msg_Dbg( s, "pre buffering" );
582 int64_t i_date = mdate();
586 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
587 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
592 p_sys->stat.i_bytes = p_sys->block.i_size;
593 p_sys->stat.i_read_time = i_date - i_start;
594 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
595 (p_sys->stat.i_read_time + 1);
597 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
600 p_sys->stat.i_read_time / I64C(1000000),
606 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
610 msleep( STREAM_DATA_WAIT );
616 /* Append the block */
617 p_sys->block.i_size += b->i_buffer;
618 *p_sys->block.pp_last = b;
619 p_sys->block.pp_last = &b->p_next;
621 p_sys->stat.i_read_count++;
625 if( p_access->info.b_prebuffered )
627 /* Access has already prebufferred - update stats and exit */
628 p_sys->stat.i_bytes = p_sys->block.i_size;
629 p_sys->stat.i_read_time = mdate() - i_start;
636 msg_Dbg( s, "received first data for our buffer");
641 p_sys->block.p_current = p_sys->block.p_first;
644 static int AStreamRefillBlock( stream_t *s );
646 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
648 stream_sys_t *p_sys = s->p_sys;
650 uint8_t *p_data= (uint8_t*)p_read;
654 if( p_sys->block.p_current == NULL )
659 /* seek within this stream if possible, else use plain old read and discard */
660 stream_sys_t *p_sys = s->p_sys;
661 access_t *p_access = p_sys->p_access;
663 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
665 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
668 while( i_data < i_read )
671 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
672 int i_copy = __MIN( i_current, i_read - i_data);
678 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
684 p_sys->block.i_offset += i_copy;
685 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
687 /* Current block is now empty, switch to next */
688 if( p_sys->block.p_current )
690 p_sys->block.i_offset = 0;
691 p_sys->block.p_current = p_sys->block.p_current->p_next;
694 if( AStreamRefillBlock( s ) )
701 p_sys->i_pos += i_data;
705 static int AStreamPeekBlock( stream_t *s, uint8_t **pp_peek, int i_read )
707 stream_sys_t *p_sys = s->p_sys;
713 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
715 /* We can directly give a pointer over our buffer */
716 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
718 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
722 /* We need to create a local copy */
723 if( p_sys->i_peek < i_read )
725 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
731 p_sys->i_peek = i_read;
734 /* Fill enough data */
735 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
738 block_t **pp_last = p_sys->block.pp_last;
740 if( AStreamRefillBlock( s ) ) break;
742 /* Our buffer are probably filled enough, don't try anymore */
743 if( pp_last == p_sys->block.pp_last ) break;
746 /* Copy what we have */
747 b = p_sys->block.p_current;
748 i_offset = p_sys->block.i_offset;
749 p_data = p_sys->p_peek;
751 while( b && i_data < i_read )
753 int i_current = b->i_buffer - i_offset;
754 int i_copy = __MIN( i_current, i_read - i_data );
756 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
761 if( i_offset >= b->i_buffer )
768 *pp_peek = p_sys->p_peek;
772 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
774 stream_sys_t *p_sys = s->p_sys;
775 access_t *p_access = p_sys->p_access;
776 int64_t i_offset = i_pos - p_sys->block.i_start;
779 /* We already have thoses data, just update p_current/i_offset */
780 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
782 block_t *b = p_sys->block.p_first;
785 while( i_current + b->i_buffer < i_offset )
787 i_current += b->i_buffer;
791 p_sys->block.p_current = b;
792 p_sys->block.i_offset = i_offset - i_current;
794 p_sys->i_pos = i_pos;
799 /* We may need to seek or to read data */
803 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
807 msg_Err( s, "backward seek impossible (access non seekable)" );
815 vlc_bool_t b_aseek, b_aseekfast;
817 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
818 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
823 msg_Warn( s, I64Fd" bytes need to be skipped "
824 "(access non seekable)",
825 i_offset - p_sys->block.i_size );
829 int64_t i_skip = i_offset - p_sys->block.i_size;
831 /* Avg bytes per packets */
832 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
833 /* TODO compute a seek cost instead of fixed threshold */
834 int i_th = b_aseekfast ? 1 : 5;
836 if( i_skip <= i_th * i_avg &&
837 i_skip < STREAM_CACHE_SIZE )
842 msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
843 b_seek, i_th*i_avg, i_skip );
849 int64_t i_start, i_end;
850 /* Do the access seek */
852 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
856 block_ChainRelease( p_sys->block.p_first );
859 p_sys->block.i_start = p_sys->i_pos = i_pos;
860 p_sys->block.i_offset = 0;
861 p_sys->block.p_current = NULL;
862 p_sys->block.i_size = 0;
863 p_sys->block.p_first = NULL;
864 p_sys->block.pp_last = &p_sys->block.p_first;
867 if( AStreamRefillBlock( s ) )
869 msg_Err( s, "cannot re fill buffer" );
873 p_sys->stat.i_seek_time += i_end - i_start;
874 p_sys->stat.i_seek_count++;
879 /* Read enought data */
880 while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
882 if( AStreamRefillBlock( s ) )
884 msg_Err( s, "can't read enough data in seek" );
887 while( p_sys->block.p_current &&
888 p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
890 p_sys->i_pos += p_sys->block.p_current->i_buffer;
891 p_sys->block.p_current = p_sys->block.p_current->p_next;
895 p_sys->block.i_offset = i_pos - p_sys->i_pos;
896 p_sys->i_pos = i_pos;
905 static int AStreamRefillBlock( stream_t *s )
907 stream_sys_t *p_sys = s->p_sys;
908 int64_t i_start, i_stop;
912 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
913 p_sys->block.p_first != p_sys->block.p_current )
915 block_t *b = p_sys->block.p_first;
917 p_sys->block.i_start += b->i_buffer;
918 p_sys->block.i_size -= b->i_buffer;
919 p_sys->block.p_first = b->p_next;
923 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
924 p_sys->block.p_current == p_sys->block.p_first &&
925 p_sys->block.p_current->p_next ) /* At least 2 packets */
927 /* Enough data, don't read more */
931 /* Now read a new block */
937 if( s->b_die ) return VLC_EGENERIC;
941 if( ( b = AReadBlock( s, &b_eof ) ) ) break;
943 if( b_eof ) return VLC_EGENERIC;
945 msleep( STREAM_DATA_WAIT );
952 /* Append the block */
953 p_sys->block.i_size += b->i_buffer;
954 *p_sys->block.pp_last = b;
955 p_sys->block.pp_last = &b->p_next;
958 if( p_sys->block.p_current == NULL )
959 p_sys->block.p_current = b;
962 p_sys->stat.i_bytes += b->i_buffer;
963 p_sys->stat.i_read_time += i_stop - i_start;
964 p_sys->stat.i_read_count++;
973 /****************************************************************************
975 ****************************************************************************/
976 static int AStreamRefillStream( stream_t *s );
978 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
980 stream_sys_t *p_sys = s->p_sys;
981 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
983 uint8_t *p_data = (uint8_t *)p_read;
986 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
990 /* seek within this stream if possible, else use plain old read and discard */
991 stream_sys_t *p_sys = s->p_sys;
992 access_t *p_access = p_sys->p_access;
994 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
996 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1000 msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
1001 " offset=%d end="I64Fd,
1002 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1003 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1006 while( i_data < i_read )
1008 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1009 STREAM_CACHE_TRACK_SIZE;
1011 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1012 STREAM_CACHE_TRACK_SIZE - i_off );
1013 int i_copy = __MIN( i_current, i_read - i_data );
1015 if( i_copy <= 0 ) break; /* EOF */
1018 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1021 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1025 p_sys->stream.i_offset += i_copy;
1027 /* Update pos now */
1028 p_sys->i_pos += i_copy;
1031 p_sys->stream.i_used += i_copy;
1032 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1033 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1035 if( AStreamRefillStream( s ) )
1038 if( tk->i_start >= tk->i_end ) break;
1046 static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read )
1048 stream_sys_t *p_sys = s->p_sys;
1049 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1052 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1055 msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
1056 "start="I64Fd" offset=%d end="I64Fd,
1057 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1058 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1061 /* Avoid problem, but that should *never* happen */
1062 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1063 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1065 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1067 if( p_sys->stream.i_used <= 1 )
1069 /* Be sure we will read something */
1070 p_sys->stream.i_used += i_read -
1071 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1073 if( AStreamRefillStream( s ) ) break;
1076 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1077 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1079 /* Now, direct pointer or a copy ? */
1080 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1081 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1083 *pp_peek = &tk->p_buffer[i_off];
1087 if( p_sys->i_peek < i_read )
1089 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1090 if( !p_sys->p_peek )
1095 p_sys->i_peek = i_read;
1098 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1099 STREAM_CACHE_TRACK_SIZE - i_off );
1100 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1101 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1103 *pp_peek = p_sys->p_peek;
1107 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1109 stream_sys_t *p_sys = s->p_sys;
1110 access_t *p_access = p_sys->p_access;
1112 vlc_bool_t b_afastseek;
1118 msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
1119 "tk=%d start="I64Fd" offset=%d end="I64Fd,
1120 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1121 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1122 p_sys->stream.i_offset,
1123 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1127 /* Seek in our current track ? */
1128 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1129 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1131 //msg_Dbg( s, "AStreamSeekStream: current track" );
1132 p_sys->i_pos = i_pos;
1133 p_sys->stream.i_offset = i_pos - p_sys->stream.tk[p_sys->stream.i_tk].i_start;
1137 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1140 /* We can't do nothing */
1141 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1142 return VLC_EGENERIC;
1145 /* Date the current track */
1146 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1148 /* Try to reuse already read data */
1149 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1151 stream_track_t *tk = &p_sys->stream.tk[i];
1153 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1156 msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
1157 " end="I64Fd, i, tk->i_start, tk->i_end );
1159 /* Seek at the end of the buffer */
1160 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1163 p_sys->i_pos = i_pos;
1164 p_sys->stream.i_tk = i;
1165 p_sys->stream.i_offset = i_pos - tk->i_start;
1167 if( p_sys->stream.i_used < 1024 )
1168 p_sys->stream.i_used = 1024;
1170 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1171 return VLC_EGENERIC;
1177 access2_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1178 /* FIXME compute seek cost (instead of static 'stupid' value) */
1179 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1185 /* Search closest segment TODO */
1186 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1188 stream_track_t *tk = &p_sys->stream.tk[i];
1190 if( i_pos + i_maxth >= tk->i_start )
1192 msg_Dbg( s, "good segment before current pos, TODO" );
1194 if( i_pos - i_maxth <= tk->i_end )
1196 msg_Dbg( s, "good segment after current pos, TODO" );
1201 /* Nothing good, seek and choose oldest segment */
1202 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1203 p_sys->i_pos = i_pos;
1206 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1208 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1212 /* Reset the segment */
1213 p_sys->stream.i_tk = i_new;
1214 p_sys->stream.i_offset = 0;
1215 p_sys->stream.tk[i_new].i_start = i_pos;
1216 p_sys->stream.tk[i_new].i_end = i_pos;
1219 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1220 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1222 if( AStreamRefillStream( s ) )
1223 return VLC_EGENERIC;
1228 static int AStreamRefillStream( stream_t *s )
1230 stream_sys_t *p_sys = s->p_sys;
1231 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1233 /* We read but won't increase i_start after initial start + offset */
1235 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1236 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1237 vlc_bool_t b_read = VLC_FALSE;
1238 int64_t i_start, i_stop;
1240 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1242 /* msg_Dbg( s, "AStreamRefillStream: toread=%d", i_toread ); */
1245 while( i_toread > 0 )
1247 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1251 return VLC_EGENERIC;
1253 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1254 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1256 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1259 msleep( STREAM_DATA_WAIT );
1262 else if( i_read == 0 )
1264 if( !b_read ) return VLC_EGENERIC;
1270 tk->i_end += i_read;
1272 /* Windows of STREAM_CACHE_TRACK_SIZE */
1273 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1275 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1277 tk->i_start += i_invalid;
1278 p_sys->stream.i_offset -= i_invalid;
1282 p_sys->stream.i_used -= i_read;
1284 p_sys->stat.i_bytes += i_read;
1285 p_sys->stat.i_read_count++;
1289 p_sys->stat.i_read_time += i_stop - i_start;
1294 static void AStreamPrebufferStream( stream_t *s )
1296 stream_sys_t *p_sys = s->p_sys;
1297 access_t *p_access = p_sys->p_access;
1299 int64_t i_first = 0;
1301 int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1302 ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1303 STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1305 msg_Dbg( s, "pre buffering" );
1309 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1311 int64_t i_date = mdate();
1314 if( s->b_die || tk->i_end >= i_prebuffer ||
1315 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1320 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1321 p_sys->stat.i_read_time = i_date - i_start;
1322 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
1323 (p_sys->stat.i_read_time+1);
1325 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
1327 p_sys->stat.i_bytes,
1328 p_sys->stat.i_read_time / I64C(1000000),
1329 i_byterate / 1024 );
1334 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1335 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1336 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1339 msleep( STREAM_DATA_WAIT );
1342 else if( i_read == 0 )
1351 msg_Dbg( s, "received first data for our buffer");
1354 tk->i_end += i_read;
1356 p_sys->stat.i_read_count++;
1361 /****************************************************************************
1363 ****************************************************************************/
1365 * Read from the stream untill first newline.
1366 * \param s Stream handle to read from
1367 * \return A pointer to the allocated output string. You need to free this when you are done.
1369 #define STREAM_PROBE_LINE 2048
1370 #define STREAM_LINE_MAX (2048*100)
1371 char * stream_ReadLine( stream_t *s )
1373 char *p_line = NULL;
1374 int i_line = 0, i_read = 0;
1376 while( i_read < STREAM_LINE_MAX )
1383 /* Probe new data */
1384 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1385 if( i_data <= 0 ) break; /* No more data */
1388 i_pos = stream_Tell( s );
1389 if( i_pos == 0 && i_data > 4 )
1392 char *psz_encoding = NULL;
1394 if( p_data[0] == 0xEF && p_data[1] == 0xBB && p_data[2] == 0xBF )
1396 psz_encoding = strdup( "UTF-8" );
1399 else if( p_data[0] == 0x00 && p_data[1] == 0x00 )
1401 if( p_data[2] == 0xFE && p_data[3] == 0xFF )
1403 psz_encoding = strdup( "UTF-32BE" );
1404 s->i_char_width = 4;
1408 else if( p_data[0] == 0xFF && p_data[1] == 0xFE )
1410 if( p_data[2] == 0x00 && p_data[3] == 0x00 )
1412 psz_encoding = strdup( "UTF-32LE" );
1413 s->i_char_width = 4;
1414 s->b_little_endian = VLC_TRUE;
1419 psz_encoding = strdup( "UTF-16LE" );
1420 s->b_little_endian = VLC_TRUE;
1421 s->i_char_width = 2;
1425 else if( p_data[0] == 0xFE && p_data[1] == 0xFF )
1427 psz_encoding = strdup( "UTF-16BE" );
1428 s->i_char_width = 2;
1432 /* Seek past the BOM */
1435 stream_Seek( s, i_bom_size );
1436 p_data += i_bom_size;
1437 i_data -= i_bom_size;
1440 /* Open the converter if we need it */
1441 if( psz_encoding != NULL )
1443 msg_Dbg( s, "%s BOM detected", psz_encoding );
1444 if( s->i_char_width > 1 )
1446 s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1447 if( s->conv == (vlc_iconv_t)-1 )
1449 msg_Err( s, "iconv_open failed" );
1452 if( psz_encoding ) free( psz_encoding );
1456 if( i_data % s->i_char_width )
1458 msg_Warn( s, "the read is not i_char_width compatible");
1461 /* Check if there is an EOL */
1462 if( ( psz_eol = memchr( p_data, '\n', i_data ) ) )
1464 if( s->b_little_endian == VLC_TRUE && s->i_char_width > 1 )
1466 psz_eol += ( s->i_char_width - 1 );
1468 i_data = (psz_eol - (char *)p_data) + 1;
1469 p_line = realloc( p_line, i_line + i_data + s->i_char_width ); /* add \0 */
1470 i_data = stream_Read( s, &p_line[i_line], i_data );
1471 if( i_data <= 0 ) break; /* Hmmm */
1472 i_line += i_data - s->i_char_width; /* skip \n */;
1475 /* We have our line */
1479 /* Read data (+1 for easy \0 append) */
1480 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->i_char_width );
1481 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1482 if( i_data <= 0 ) break; /* Hmmm */
1490 for( j = 0; j < s->i_char_width; j++ )
1492 p_line[i_line + j] = '\0';
1494 i_line += s->i_char_width; /* the added \0 */
1495 if( s->i_char_width > 1 )
1497 size_t i_in = 0, i_out = 0;
1499 char * p_out = NULL;
1500 char * psz_new_line = NULL;
1503 psz_new_line = malloc( i_line );
1505 i_in = i_out = (size_t)i_line;
1507 p_out = psz_new_line;
1509 if( vlc_iconv( s->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1511 msg_Err( s, "iconv failed" );
1512 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1514 if( p_line ) free( p_line );
1515 p_line = psz_new_line;
1516 i_line = (size_t)i_line - i_out; /* does not include \0 */
1519 /* Remove trailing LF/CR */
1520 while( i_line > 0 && ( p_line[i_line-2] == '\r' ||
1521 p_line[i_line-2] == '\n') ) i_line--;
1523 /* Make sure the \0 is there */
1524 p_line[i_line-1] = '\0';
1529 /* We failed to read any data, probably EOF */
1530 if( p_line ) free( p_line );
1531 vlc_iconv_close( s->conv );
1535 /****************************************************************************
1536 * Access reading/seeking wrappers to handle concatenated streams.
1537 ****************************************************************************/
1538 static int AReadStream( stream_t *s, void *p_read, int i_read )
1540 stream_sys_t *p_sys = s->p_sys;
1541 access_t *p_access = p_sys->p_access;
1542 int i_read_orig = i_read;
1544 if( !p_sys->i_list )
1546 i_read = p_access->pf_read( p_access, p_read, i_read );
1550 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1553 /* If we reached an EOF then switch to the next stream in the list */
1554 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1556 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1557 access_t *p_list_access;
1559 msg_Dbg( s, "opening input `%s'", psz_name );
1561 p_list_access = access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1563 if( !p_list_access ) return 0;
1565 if( p_sys->p_list_access != p_access )
1566 access2_Delete( p_sys->p_list_access );
1568 p_sys->p_list_access = p_list_access;
1570 /* We have to read some data */
1571 return AReadStream( s, p_read, i_read_orig );
1577 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof )
1579 stream_sys_t *p_sys = s->p_sys;
1580 access_t *p_access = p_sys->p_access;
1584 if( !p_sys->i_list )
1586 p_block = p_access->pf_block( p_access );
1587 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1591 p_block = p_sys->p_list_access->pf_block( p_access );
1592 b_eof = p_sys->p_list_access->info.b_eof;
1593 if( pb_eof ) *pb_eof = b_eof;
1595 /* If we reached an EOF then switch to the next stream in the list */
1596 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1598 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1599 access_t *p_list_access;
1601 msg_Dbg( s, "opening input `%s'", psz_name );
1603 p_list_access = access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1605 if( !p_list_access ) return 0;
1607 if( p_sys->p_list_access != p_access )
1608 access2_Delete( p_sys->p_list_access );
1610 p_sys->p_list_access = p_list_access;
1612 /* We have to read some data */
1613 return AReadBlock( s, pb_eof );
1619 static int ASeek( stream_t *s, int64_t i_pos )
1621 stream_sys_t *p_sys = s->p_sys;
1622 access_t *p_access = p_sys->p_access;
1624 /* Check which stream we need to access */
1630 access_t *p_list_access = 0;
1632 for( i = 0; i < p_sys->i_list - 1; i++ )
1634 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1635 i_size += p_sys->list[i]->i_size;
1637 psz_name = p_sys->list[i]->psz_path;
1639 if( i != p_sys->i_list_index )
1640 msg_Dbg( s, "opening input `%s'", psz_name );
1642 if( i != p_sys->i_list_index && i != 0 )
1645 access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1647 else if( i != p_sys->i_list_index )
1649 p_list_access = p_access;
1654 if( p_sys->p_list_access != p_access )
1655 access2_Delete( p_sys->p_list_access );
1657 p_sys->p_list_access = p_list_access;
1660 p_sys->i_list_index = i;
1661 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1665 return p_access->pf_seek( p_access, i_pos );