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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
26 #include <vlc/input.h>
29 #include "input_internal.h"
34 * - tune the 2 methods
35 * - compute cost for seek
36 * - improve stream mode seeking with closest segments
42 * One linked list of data read
44 * More complex scheme using mutliple track to avoid seeking
47 /* How many tracks we have, currently only used for stream mode */
48 #ifdef OPTIMIZE_MEMORY
49 # define STREAM_CACHE_TRACK 1
50 /* Max size of our cache 128Ko per track */
51 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
53 # define STREAM_CACHE_TRACK 3
54 /* Max size of our cache 4Mo per track */
55 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
58 /* How many data we try to prebuffer */
59 #define STREAM_CACHE_PREBUFFER_SIZE (32767)
60 /* Maximum time we take to pre-buffer */
61 #define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
63 /* Method1: Simple, for pf_block.
64 * We get blocks and put them in the linked list.
65 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
67 #define STREAM_DATA_WAIT 40000 /* Time between before a pf_block retry */
69 /* Method2: A bit more complex, for pf_read
70 * - We use ring buffers, only one if unseekable, all if seekable
71 * - Upon seek date current ring, then search if one ring match the pos,
72 * yes: switch to it, seek the access to match the end of the ring
73 * no: search the ring with i_end the closer to i_pos,
74 * if close enough, read data and use this ring
75 * else use the oldest ring, seek and use it.
77 * TODO: - with access non seekable: use all space available for only one ring, but
78 * we have to support seekable/non-seekable switch on the fly.
79 * - compute a good value for i_read_size
82 #define STREAM_READ_ATONCE 32767
83 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
107 vlc_bool_t b_block; /* Block method (1) or stream */
109 int64_t i_pos; /* Current reading offset */
111 /* Method 1: pf_block */
114 int64_t i_start; /* Offset of block for p_first */
115 int i_offset; /* Offset for data in p_current */
116 block_t *p_current; /* Current block */
118 int i_size; /* Total amount of data in the list */
124 /* Method 2: for pf_read */
127 int i_offset; /* Buffer offset in the current track */
128 int i_tk; /* Current track */
129 stream_track_t tk[STREAM_CACHE_TRACK];
135 int i_used; /* Used since last read */
140 /* Peek temporary buffer */
144 /* Stat for both method */
147 vlc_bool_t b_fastseek; /* From access */
149 /* Stat about reading data */
150 int64_t i_read_count;
154 /* Stat about seek */
162 access_entry_t **list;
164 access_t *p_list_access;
166 /* Preparse mode ? */
171 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read );
172 static int AStreamPeekBlock( stream_t *s, uint8_t **p_peek, int i_read );
173 static int AStreamSeekBlock( stream_t *s, int64_t i_pos );
174 static void AStreamPrebufferBlock( stream_t *s );
175 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof );
178 static int AStreamReadStream( stream_t *s, void *p_read, int i_read );
179 static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read );
180 static int AStreamSeekStream( stream_t *s, int64_t i_pos );
181 static void AStreamPrebufferStream( stream_t *s );
182 static int AReadStream( stream_t *s, void *p_read, int i_read );
185 static int AStreamControl( stream_t *s, int i_query, va_list );
186 static void AStreamDestroy( stream_t *s );
187 static void UStreamDestroy( stream_t *s );
188 static int ASeek( stream_t *s, int64_t i_pos );
191 /****************************************************************************
192 * stream_UrlNew: create a stream from a access
193 ****************************************************************************/
194 stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
196 const char *psz_access, *psz_demux;
201 if( !psz_url ) return 0;
203 char psz_dup[strlen (psz_url) + 1];
204 strcpy (psz_dup, psz_url);;
205 MRLSplit( p_parent, psz_dup, &psz_access, &psz_demux, &psz_path );
207 /* Now try a real access */
208 p_access = access2_New( p_parent, psz_access, psz_demux, psz_path, 0 );
210 if( p_access == NULL )
212 msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
216 if( !( p_res = stream_AccessNew( p_access, VLC_TRUE ) ) )
218 access2_Delete( p_access );
222 p_res->pf_destroy = UStreamDestroy;
226 stream_t *stream_AccessNew( access_t *p_access, vlc_bool_t b_quick )
228 stream_t *s = vlc_object_create( p_access, VLC_OBJECT_STREAM );
232 if( !s ) return NULL;
234 /* Attach it now, needed for b_die */
235 vlc_object_attach( s, p_access );
238 s->pf_read = NULL; /* Set up later */
240 s->pf_control = AStreamControl;
241 s->pf_destroy = AStreamDestroy;
243 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
245 /* UTF16 and UTF32 text file conversion */
247 s->b_little_endian = VLC_FALSE;
248 s->conv = (vlc_iconv_t)(-1);
251 p_sys->p_access = p_access;
252 p_sys->b_block = p_access->pf_block ? VLC_TRUE : VLC_FALSE;
253 p_sys->i_pos = p_access->info.i_pos;
256 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
257 p_sys->stat.i_bytes = 0;
258 p_sys->stat.i_read_time = 0;
259 p_sys->stat.i_read_count = 0;
260 p_sys->stat.i_seek_count = 0;
261 p_sys->stat.i_seek_time = 0;
265 p_sys->i_list_index = 0;
266 p_sys->p_list_access = 0;
268 p_sys->b_quick = b_quick;
270 /* Get the additional list of inputs if any (for concatenation) */
271 if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
273 access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
274 char *psz_name, *psz_parser = psz_name = psz_list;
276 p_sys->p_list_access = p_access;
277 p_entry->i_size = p_access->info.i_size;
278 p_entry->psz_path = strdup( p_access->psz_path );
279 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
280 msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
281 p_entry->psz_path, p_access->info.i_size );
283 while( psz_name && *psz_name )
285 psz_parser = strchr( psz_name, ',' );
286 if( psz_parser ) *psz_parser = 0;
288 psz_name = strdup( psz_name );
291 access_t *p_tmp = access2_New( p_access, p_access->psz_access,
296 psz_name = psz_parser;
297 if( psz_name ) psz_name++;
301 msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
302 psz_name, p_tmp->info.i_size );
304 p_entry = malloc( sizeof(access_entry_t) );
305 p_entry->i_size = p_tmp->info.i_size;
306 p_entry->psz_path = psz_name;
307 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
309 access2_Delete( p_tmp );
312 psz_name = psz_parser;
313 if( psz_name ) psz_name++;
316 if( psz_list ) free( psz_list );
320 p_sys->p_peek = NULL;
324 s->pf_read = AStreamReadBlock;
325 s->pf_peek = AStreamPeekBlock;
327 /* Init all fields of p_sys->block */
328 p_sys->block.i_start = p_sys->i_pos;
329 p_sys->block.i_offset = 0;
330 p_sys->block.p_current = NULL;
331 p_sys->block.i_size = 0;
332 p_sys->block.p_first = NULL;
333 p_sys->block.pp_last = &p_sys->block.p_first;
335 /* Do the prebuffering */
336 AStreamPrebufferBlock( s );
338 if( p_sys->block.i_size <= 0 )
340 msg_Err( s, "cannot pre fill buffer" );
348 s->pf_read = AStreamReadStream;
349 s->pf_peek = AStreamPeekStream;
351 /* Allocate/Setup our tracks */
352 p_sys->stream.i_offset = 0;
353 p_sys->stream.i_tk = 0;
354 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
355 p_sys->stream.i_used = 0;
356 access2_Control( p_access, ACCESS_GET_MTU,
357 &p_sys->stream.i_read_size );
358 if( p_sys->stream.i_read_size <= 0 )
359 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
360 else if( p_sys->stream.i_read_size <= 256 )
361 p_sys->stream.i_read_size = 256;
363 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
365 p_sys->stream.tk[i].i_date = 0;
366 p_sys->stream.tk[i].i_start = p_sys->i_pos;
367 p_sys->stream.tk[i].i_end = p_sys->i_pos;
368 p_sys->stream.tk[i].p_buffer=
369 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
372 /* Do the prebuffering */
373 AStreamPrebufferStream( s );
375 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
377 msg_Err( s, "cannot pre fill buffer" );
391 free( p_sys->stream.p_buffer );
394 vlc_object_detach( s );
395 vlc_object_destroy( s );
399 /****************************************************************************
401 ****************************************************************************/
402 static void AStreamDestroy( stream_t *s )
404 stream_sys_t *p_sys = s->p_sys;
406 vlc_object_detach( s );
408 if( p_sys->b_block ) block_ChainRelease( p_sys->block.p_first );
409 else free( p_sys->stream.p_buffer );
411 if( p_sys->p_peek ) free( p_sys->p_peek );
413 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
414 access2_Delete( p_sys->p_list_access );
416 while( p_sys->i_list-- )
418 free( p_sys->list[p_sys->i_list]->psz_path );
419 free( p_sys->list[p_sys->i_list] );
420 if( !p_sys->i_list ) free( p_sys->list );
424 vlc_object_destroy( s );
427 static void UStreamDestroy( stream_t *s )
429 access_t *p_access = (access_t*)vlc_object_find( s, VLC_OBJECT_ACCESS, FIND_PARENT );
431 vlc_object_release( p_access );
432 access2_Delete( p_access );
435 /****************************************************************************
436 * stream_AccessReset:
437 ****************************************************************************/
438 void stream_AccessReset( stream_t *s )
440 stream_sys_t *p_sys = s->p_sys;
442 p_sys->i_pos = p_sys->p_access->info.i_pos;
446 block_ChainRelease( p_sys->block.p_first );
448 /* Init all fields of p_sys->block */
449 p_sys->block.i_start = p_sys->i_pos;
450 p_sys->block.i_offset = 0;
451 p_sys->block.p_current = NULL;
452 p_sys->block.i_size = 0;
453 p_sys->block.p_first = NULL;
454 p_sys->block.pp_last = &p_sys->block.p_first;
456 /* Do the prebuffering */
457 AStreamPrebufferBlock( s );
463 /* Setup our tracks */
464 p_sys->stream.i_offset = 0;
465 p_sys->stream.i_tk = 0;
466 p_sys->stream.i_used = 0;
468 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
470 p_sys->stream.tk[i].i_date = 0;
471 p_sys->stream.tk[i].i_start = p_sys->i_pos;
472 p_sys->stream.tk[i].i_end = p_sys->i_pos;
475 /* Do the prebuffering */
476 AStreamPrebufferStream( s );
480 /****************************************************************************
481 * stream_AccessUpdate:
482 ****************************************************************************/
483 void stream_AccessUpdate( stream_t *s )
485 stream_sys_t *p_sys = s->p_sys;
487 p_sys->i_pos = p_sys->p_access->info.i_pos;
492 for( i = 0; i < p_sys->i_list_index; i++ )
494 p_sys->i_pos += p_sys->list[i]->i_size;
499 /****************************************************************************
501 ****************************************************************************/
502 static int AStreamControl( stream_t *s, int i_query, va_list args )
504 stream_sys_t *p_sys = s->p_sys;
505 access_t *p_access = p_sys->p_access;
508 int64_t *pi_64, i_64;
513 case STREAM_GET_SIZE:
514 pi_64 = (int64_t*)va_arg( args, int64_t * );
515 if( s->p_sys->i_list )
519 for( i = 0; i < s->p_sys->i_list; i++ )
520 *pi_64 += s->p_sys->list[i]->i_size;
523 *pi_64 = p_access->info.i_size;
526 case STREAM_CAN_SEEK:
527 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
528 access2_Control( p_access, ACCESS_CAN_SEEK, p_bool );
531 case STREAM_CAN_FASTSEEK:
532 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
533 access2_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
536 case STREAM_GET_POSITION:
537 pi_64 = (int64_t*)va_arg( args, int64_t * );
538 *pi_64 = p_sys->i_pos;
541 case STREAM_SET_POSITION:
542 i_64 = (int64_t)va_arg( args, int64_t );
544 return AStreamSeekBlock( s, i_64 );
546 return AStreamSeekStream( s, i_64 );
551 case STREAM_CONTROL_ACCESS:
552 i_int = (int) va_arg( args, int );
553 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
554 i_int != ACCESS_SET_PRIVATE_ID_CA &&
555 i_int != ACCESS_GET_PRIVATE_ID_STATE )
557 msg_Err( s, "Hey, what are you thinking ?"
558 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
561 return access2_vaControl( p_access, i_int, args );
564 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
572 /****************************************************************************
574 ****************************************************************************/
575 static void AStreamPrebufferBlock( stream_t *s )
577 stream_sys_t *p_sys = s->p_sys;
578 access_t *p_access = p_sys->p_access;
583 msg_Dbg( s, "pre buffering" );
587 int64_t i_date = mdate();
591 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
592 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
597 p_sys->stat.i_bytes = p_sys->block.i_size;
598 p_sys->stat.i_read_time = i_date - i_start;
599 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
600 (p_sys->stat.i_read_time + 1);
602 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
605 p_sys->stat.i_read_time / I64C(1000000),
611 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
615 msleep( STREAM_DATA_WAIT );
621 /* Append the block */
622 p_sys->block.i_size += b->i_buffer;
623 *p_sys->block.pp_last = b;
624 p_sys->block.pp_last = &b->p_next;
626 p_sys->stat.i_read_count++;
630 if( p_access->info.b_prebuffered )
632 /* Access has already prebufferred - update stats and exit */
633 p_sys->stat.i_bytes = p_sys->block.i_size;
634 p_sys->stat.i_read_time = mdate() - i_start;
641 msg_Dbg( s, "received first data for our buffer");
646 p_sys->block.p_current = p_sys->block.p_first;
649 static int AStreamRefillBlock( stream_t *s );
651 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
653 stream_sys_t *p_sys = s->p_sys;
655 uint8_t *p_data= (uint8_t*)p_read;
659 if( p_sys->block.p_current == NULL )
664 /* seek within this stream if possible, else use plain old read and discard */
665 stream_sys_t *p_sys = s->p_sys;
666 access_t *p_access = p_sys->p_access;
668 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
670 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
673 while( i_data < i_read )
676 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
677 int i_copy = __MIN( i_current, i_read - i_data);
683 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
689 p_sys->block.i_offset += i_copy;
690 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
692 /* Current block is now empty, switch to next */
693 if( p_sys->block.p_current )
695 p_sys->block.i_offset = 0;
696 p_sys->block.p_current = p_sys->block.p_current->p_next;
699 if( AStreamRefillBlock( s ) )
706 p_sys->i_pos += i_data;
710 static int AStreamPeekBlock( stream_t *s, uint8_t **pp_peek, int i_read )
712 stream_sys_t *p_sys = s->p_sys;
718 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
720 /* We can directly give a pointer over our buffer */
721 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
723 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
727 /* We need to create a local copy */
728 if( p_sys->i_peek < i_read )
730 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
736 p_sys->i_peek = i_read;
739 /* Fill enough data */
740 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
743 block_t **pp_last = p_sys->block.pp_last;
745 if( AStreamRefillBlock( s ) ) break;
747 /* Our buffer are probably filled enough, don't try anymore */
748 if( pp_last == p_sys->block.pp_last ) break;
751 /* Copy what we have */
752 b = p_sys->block.p_current;
753 i_offset = p_sys->block.i_offset;
754 p_data = p_sys->p_peek;
756 while( b && i_data < i_read )
758 int i_current = b->i_buffer - i_offset;
759 int i_copy = __MIN( i_current, i_read - i_data );
761 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
766 if( i_offset >= b->i_buffer )
773 *pp_peek = p_sys->p_peek;
777 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
779 stream_sys_t *p_sys = s->p_sys;
780 access_t *p_access = p_sys->p_access;
781 int64_t i_offset = i_pos - p_sys->block.i_start;
784 /* We already have thoses data, just update p_current/i_offset */
785 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
787 block_t *b = p_sys->block.p_first;
790 while( i_current + b->i_buffer < i_offset )
792 i_current += b->i_buffer;
796 p_sys->block.p_current = b;
797 p_sys->block.i_offset = i_offset - i_current;
799 p_sys->i_pos = i_pos;
804 /* We may need to seek or to read data */
808 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
812 msg_Err( s, "backward seeking impossible (access not seekable)" );
820 vlc_bool_t b_aseek, b_aseekfast;
822 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
823 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
828 msg_Warn( s, I64Fd" bytes need to be skipped "
829 "(access non seekable)",
830 i_offset - p_sys->block.i_size );
834 int64_t i_skip = i_offset - p_sys->block.i_size;
836 /* Avg bytes per packets */
837 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
838 /* TODO compute a seek cost instead of fixed threshold */
839 int i_th = b_aseekfast ? 1 : 5;
841 if( i_skip <= i_th * i_avg &&
842 i_skip < STREAM_CACHE_SIZE )
847 msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
848 b_seek, i_th*i_avg, i_skip );
854 int64_t i_start, i_end;
855 /* Do the access seek */
857 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
861 block_ChainRelease( p_sys->block.p_first );
864 p_sys->block.i_start = p_sys->i_pos = i_pos;
865 p_sys->block.i_offset = 0;
866 p_sys->block.p_current = NULL;
867 p_sys->block.i_size = 0;
868 p_sys->block.p_first = NULL;
869 p_sys->block.pp_last = &p_sys->block.p_first;
872 if( AStreamRefillBlock( s ) )
874 msg_Err( s, "cannot re fill buffer" );
878 p_sys->stat.i_seek_time += i_end - i_start;
879 p_sys->stat.i_seek_count++;
884 /* Read enough data */
885 while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
887 if( AStreamRefillBlock( s ) )
889 msg_Err( s, "can't read enough data in seek" );
892 while( p_sys->block.p_current &&
893 p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
895 p_sys->i_pos += p_sys->block.p_current->i_buffer;
896 p_sys->block.p_current = p_sys->block.p_current->p_next;
900 p_sys->block.i_offset = i_pos - p_sys->i_pos;
901 p_sys->i_pos = i_pos;
910 static int AStreamRefillBlock( stream_t *s )
912 stream_sys_t *p_sys = s->p_sys;
913 int64_t i_start, i_stop;
917 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
918 p_sys->block.p_first != p_sys->block.p_current )
920 block_t *b = p_sys->block.p_first;
922 p_sys->block.i_start += b->i_buffer;
923 p_sys->block.i_size -= b->i_buffer;
924 p_sys->block.p_first = b->p_next;
928 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
929 p_sys->block.p_current == p_sys->block.p_first &&
930 p_sys->block.p_current->p_next ) /* At least 2 packets */
932 /* Enough data, don't read more */
936 /* Now read a new block */
942 if( s->b_die ) return VLC_EGENERIC;
946 if( ( b = AReadBlock( s, &b_eof ) ) ) break;
948 if( b_eof ) return VLC_EGENERIC;
950 msleep( STREAM_DATA_WAIT );
957 /* Append the block */
958 p_sys->block.i_size += b->i_buffer;
959 *p_sys->block.pp_last = b;
960 p_sys->block.pp_last = &b->p_next;
963 if( p_sys->block.p_current == NULL )
964 p_sys->block.p_current = b;
967 p_sys->stat.i_bytes += b->i_buffer;
968 p_sys->stat.i_read_time += i_stop - i_start;
969 p_sys->stat.i_read_count++;
978 /****************************************************************************
980 ****************************************************************************/
981 static int AStreamRefillStream( stream_t *s );
983 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
985 stream_sys_t *p_sys = s->p_sys;
986 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
988 uint8_t *p_data = (uint8_t *)p_read;
991 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
995 /* seek within this stream if possible, else use plain old read and discard */
996 stream_sys_t *p_sys = s->p_sys;
997 access_t *p_access = p_sys->p_access;
999 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1001 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1005 msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
1006 " offset=%d end="I64Fd,
1007 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1008 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1011 while( i_data < i_read )
1013 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1014 STREAM_CACHE_TRACK_SIZE;
1016 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1017 STREAM_CACHE_TRACK_SIZE - i_off );
1018 int i_copy = __MIN( i_current, i_read - i_data );
1020 if( i_copy <= 0 ) break; /* EOF */
1023 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1026 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1030 p_sys->stream.i_offset += i_copy;
1032 /* Update pos now */
1033 p_sys->i_pos += i_copy;
1036 p_sys->stream.i_used += i_copy;
1037 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1038 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1040 if( AStreamRefillStream( s ) )
1043 if( tk->i_start >= tk->i_end ) break;
1051 static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read )
1053 stream_sys_t *p_sys = s->p_sys;
1054 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1057 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1060 msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
1061 "start="I64Fd" offset=%d end="I64Fd,
1062 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1063 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1066 /* Avoid problem, but that should *never* happen */
1067 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1068 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1070 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1072 if( p_sys->stream.i_used <= 1 )
1074 /* Be sure we will read something */
1075 p_sys->stream.i_used += i_read -
1076 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1078 if( AStreamRefillStream( s ) ) break;
1081 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1082 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1084 /* Now, direct pointer or a copy ? */
1085 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1086 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1088 *pp_peek = &tk->p_buffer[i_off];
1092 if( p_sys->i_peek < i_read )
1094 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1095 if( !p_sys->p_peek )
1100 p_sys->i_peek = i_read;
1103 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1104 STREAM_CACHE_TRACK_SIZE - i_off );
1105 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1106 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1108 *pp_peek = p_sys->p_peek;
1112 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1114 stream_sys_t *p_sys = s->p_sys;
1115 access_t *p_access = p_sys->p_access;
1117 vlc_bool_t b_afastseek;
1123 msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
1124 " tk=%d start="I64Fd" offset=%d end="I64Fd,
1125 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1126 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1127 p_sys->stream.i_offset,
1128 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1132 /* Seek in our current track ? */
1133 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1134 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1136 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1138 msg_Dbg( s, "AStreamSeekStream: current track" );
1140 p_sys->i_pos = i_pos;
1141 p_sys->stream.i_offset = i_pos - tk->i_start;
1143 /* If there is not enough data left in the track, refill */
1144 /* \todo How to get a correct value for
1145 * - refilling threshold
1146 * - how much to refill
1148 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1149 p_sys->stream.i_read_size )
1151 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1153 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1154 AStreamRefillStream( s );
1160 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1163 /* We can't do nothing */
1164 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1165 return VLC_EGENERIC;
1168 /* Date the current track */
1169 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1171 /* Try to reuse already read data */
1172 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1174 stream_track_t *tk = &p_sys->stream.tk[i];
1176 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1179 msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
1180 " end="I64Fd, i, tk->i_start, tk->i_end );
1183 /* Seek at the end of the buffer */
1184 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1187 p_sys->i_pos = i_pos;
1188 p_sys->stream.i_tk = i;
1189 p_sys->stream.i_offset = i_pos - tk->i_start;
1191 if( p_sys->stream.i_used < 1024 )
1192 p_sys->stream.i_used = 1024;
1194 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1195 return VLC_EGENERIC;
1201 access2_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1202 /* FIXME compute seek cost (instead of static 'stupid' value) */
1203 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1209 /* Search closest segment TODO */
1210 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1212 stream_track_t *tk = &p_sys->stream.tk[i];
1214 if( i_pos + i_maxth >= tk->i_start )
1216 msg_Dbg( s, "good segment before current pos, TODO" );
1218 if( i_pos - i_maxth <= tk->i_end )
1220 msg_Dbg( s, "good segment after current pos, TODO" );
1225 /* Nothing good, seek and choose oldest segment */
1226 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1227 p_sys->i_pos = i_pos;
1230 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1232 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1236 /* Reset the segment */
1237 p_sys->stream.i_tk = i_new;
1238 p_sys->stream.i_offset = 0;
1239 p_sys->stream.tk[i_new].i_start = i_pos;
1240 p_sys->stream.tk[i_new].i_end = i_pos;
1243 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1244 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1246 if( AStreamRefillStream( s ) )
1247 return VLC_EGENERIC;
1252 static int AStreamRefillStream( stream_t *s )
1254 stream_sys_t *p_sys = s->p_sys;
1255 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1257 /* We read but won't increase i_start after initial start + offset */
1259 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1260 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1261 vlc_bool_t b_read = VLC_FALSE;
1262 int64_t i_start, i_stop;
1264 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1267 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1268 p_sys->stream.i_used, i_toread );
1272 while( i_toread > 0 )
1274 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1278 return VLC_EGENERIC;
1280 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1281 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1283 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1286 msleep( STREAM_DATA_WAIT );
1289 else if( i_read == 0 )
1291 if( !b_read ) return VLC_EGENERIC;
1297 tk->i_end += i_read;
1299 /* Windows of STREAM_CACHE_TRACK_SIZE */
1300 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1302 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1304 tk->i_start += i_invalid;
1305 p_sys->stream.i_offset -= i_invalid;
1309 p_sys->stream.i_used -= i_read;
1311 p_sys->stat.i_bytes += i_read;
1312 p_sys->stat.i_read_count++;
1316 p_sys->stat.i_read_time += i_stop - i_start;
1321 static void AStreamPrebufferStream( stream_t *s )
1323 stream_sys_t *p_sys = s->p_sys;
1324 access_t *p_access = p_sys->p_access;
1326 int64_t i_first = 0;
1328 int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1329 ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1330 STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1332 msg_Dbg( s, "pre-buffering..." );
1336 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1338 int64_t i_date = mdate();
1341 if( s->b_die || tk->i_end >= i_prebuffer ||
1342 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1347 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1348 p_sys->stat.i_read_time = i_date - i_start;
1349 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
1350 (p_sys->stat.i_read_time+1);
1352 msg_Dbg( s, "pre-buffering done "I64Fd" bytes in "I64Fd"s - "
1354 p_sys->stat.i_bytes,
1355 p_sys->stat.i_read_time / I64C(1000000),
1356 i_byterate / 1024 );
1361 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1362 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1363 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1366 msleep( STREAM_DATA_WAIT );
1369 else if( i_read == 0 )
1378 msg_Dbg( s, "received first data for our buffer");
1381 tk->i_end += i_read;
1383 p_sys->stat.i_read_count++;
1388 /****************************************************************************
1390 ****************************************************************************/
1392 * Read from the stream untill first newline.
1393 * \param s Stream handle to read from
1394 * \return A pointer to the allocated output string. You need to free this when you are done.
1396 #define STREAM_PROBE_LINE 2048
1397 #define STREAM_LINE_MAX (2048*100)
1398 char * stream_ReadLine( stream_t *s )
1400 char *p_line = NULL;
1401 int i_line = 0, i_read = 0;
1403 while( i_read < STREAM_LINE_MAX )
1410 /* Probe new data */
1411 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1412 if( i_data <= 0 ) break; /* No more data */
1415 i_pos = stream_Tell( s );
1416 if( i_pos == 0 && i_data > 4 )
1419 char *psz_encoding = NULL;
1421 if( p_data[0] == 0xEF && p_data[1] == 0xBB && p_data[2] == 0xBF )
1423 psz_encoding = strdup( "UTF-8" );
1426 else if( p_data[0] == 0x00 && p_data[1] == 0x00 )
1428 if( p_data[2] == 0xFE && p_data[3] == 0xFF )
1430 psz_encoding = strdup( "UTF-32BE" );
1431 s->i_char_width = 4;
1435 else if( p_data[0] == 0xFF && p_data[1] == 0xFE )
1437 if( p_data[2] == 0x00 && p_data[3] == 0x00 )
1439 psz_encoding = strdup( "UTF-32LE" );
1440 s->i_char_width = 4;
1441 s->b_little_endian = VLC_TRUE;
1446 psz_encoding = strdup( "UTF-16LE" );
1447 s->b_little_endian = VLC_TRUE;
1448 s->i_char_width = 2;
1452 else if( p_data[0] == 0xFE && p_data[1] == 0xFF )
1454 psz_encoding = strdup( "UTF-16BE" );
1455 s->i_char_width = 2;
1459 /* Seek past the BOM */
1462 stream_Seek( s, i_bom_size );
1463 p_data += i_bom_size;
1464 i_data -= i_bom_size;
1467 /* Open the converter if we need it */
1468 if( psz_encoding != NULL )
1470 input_thread_t *p_input;
1471 msg_Dbg( s, "%s BOM detected", psz_encoding );
1472 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1473 if( s->i_char_width > 1 )
1475 s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1476 if( s->conv == (vlc_iconv_t)-1 )
1478 msg_Err( s, "iconv_open failed" );
1481 if( p_input != NULL)
1483 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1484 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1485 vlc_object_release( p_input );
1487 if( psz_encoding ) free( psz_encoding );
1491 if( i_data % s->i_char_width )
1493 msg_Warn( s, "the read is not i_char_width compatible");
1496 /* Check if there is an EOL */
1497 if( ( psz_eol = memchr( p_data, '\n', i_data ) ) )
1499 if( s->b_little_endian == VLC_TRUE && s->i_char_width > 1 )
1501 psz_eol += ( s->i_char_width - 1 );
1503 i_data = (psz_eol - (char *)p_data) + 1;
1504 p_line = realloc( p_line, i_line + i_data + s->i_char_width ); /* add \0 */
1505 i_data = stream_Read( s, &p_line[i_line], i_data );
1506 if( i_data <= 0 ) break; /* Hmmm */
1507 i_line += i_data - s->i_char_width; /* skip \n */;
1510 /* We have our line */
1514 /* Read data (+1 for easy \0 append) */
1515 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->i_char_width );
1516 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1517 if( i_data <= 0 ) break; /* Hmmm */
1525 for( j = 0; j < s->i_char_width; j++ )
1527 p_line[i_line + j] = '\0';
1529 i_line += s->i_char_width; /* the added \0 */
1530 if( s->i_char_width > 1 )
1532 size_t i_in = 0, i_out = 0;
1533 const char * p_in = NULL;
1534 char * p_out = NULL;
1535 char * psz_new_line = NULL;
1538 psz_new_line = malloc( i_line );
1540 i_in = i_out = (size_t)i_line;
1542 p_out = psz_new_line;
1544 if( vlc_iconv( s->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1546 msg_Err( s, "iconv failed" );
1547 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1549 if( p_line ) free( p_line );
1550 p_line = psz_new_line;
1551 i_line = (size_t)i_line - i_out; /* does not include \0 */
1554 /* Remove trailing LF/CR */
1555 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1556 p_line[i_line-2] == '\n') ) i_line--;
1558 /* Make sure the \0 is there */
1559 p_line[i_line-1] = '\0';
1564 /* We failed to read any data, probably EOF */
1565 if( p_line ) free( p_line );
1566 if( s->conv != (vlc_iconv_t)(-1) ) vlc_iconv_close( s->conv );
1570 /****************************************************************************
1571 * Access reading/seeking wrappers to handle concatenated streams.
1572 ****************************************************************************/
1573 static int AReadStream( stream_t *s, void *p_read, int i_read )
1575 stream_sys_t *p_sys = s->p_sys;
1576 access_t *p_access = p_sys->p_access;
1577 input_thread_t *p_input = NULL;
1578 int i_read_orig = i_read;
1581 if( s->p_parent && s->p_parent->p_parent &&
1582 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1583 p_input = (input_thread_t *)s->p_parent->p_parent;
1585 if( !p_sys->i_list )
1587 i_read = p_access->pf_read( p_access, p_read, i_read );
1590 vlc_object_yield( p_input );
1591 vlc_mutex_lock( &p_input->counters.counters_lock );
1592 stats_UpdateInteger( s, p_input->counters.p_read_bytes, i_read,
1594 stats_UpdateFloat( s, p_input->counters.p_input_bitrate,
1595 (float)i_total, NULL );
1596 stats_UpdateInteger( s, p_input->counters.p_read_packets, 1, NULL );
1597 vlc_mutex_unlock( &p_input->counters.counters_lock );
1598 vlc_object_release( p_input );
1603 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1606 /* If we reached an EOF then switch to the next stream in the list */
1607 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1609 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1610 access_t *p_list_access;
1612 msg_Dbg( s, "opening input `%s'", psz_name );
1614 p_list_access = access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1616 if( !p_list_access ) return 0;
1618 if( p_sys->p_list_access != p_access )
1619 access2_Delete( p_sys->p_list_access );
1621 p_sys->p_list_access = p_list_access;
1623 /* We have to read some data */
1624 return AReadStream( s, p_read, i_read_orig );
1627 /* Update read bytes in input */
1630 vlc_object_yield( p_input );
1631 vlc_mutex_lock( &p_input->counters.counters_lock );
1632 stats_UpdateInteger( s, p_input->counters.p_read_bytes, i_read, &i_total );
1633 stats_UpdateFloat( s, p_input->counters.p_input_bitrate,
1634 (float)i_total, NULL );
1635 stats_UpdateInteger( s, p_input->counters.p_read_packets, 1, NULL );
1636 vlc_mutex_unlock( &p_input->counters.counters_lock );
1637 vlc_object_release( p_input );
1642 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof )
1644 stream_sys_t *p_sys = s->p_sys;
1645 access_t *p_access = p_sys->p_access;
1646 input_thread_t *p_input = NULL;
1651 if( s->p_parent && s->p_parent->p_parent &&
1652 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1653 p_input = (input_thread_t *)s->p_parent->p_parent;
1655 if( !p_sys->i_list )
1657 p_block = p_access->pf_block( p_access );
1658 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1659 if( p_input && p_block && p_access->p_libvlc->b_stats )
1661 vlc_object_yield( p_input );
1662 vlc_mutex_lock( &p_input->counters.counters_lock );
1663 stats_UpdateInteger( s, p_input->counters.p_read_bytes,
1664 p_block->i_buffer, &i_total );
1665 stats_UpdateFloat( s, p_input->counters.p_input_bitrate,
1666 (float)i_total, NULL );
1667 stats_UpdateInteger( s, p_input->counters.p_read_packets, 1, NULL );
1668 vlc_mutex_unlock( &p_input->counters.counters_lock );
1669 vlc_object_release( p_input );
1674 p_block = p_sys->p_list_access->pf_block( p_access );
1675 b_eof = p_sys->p_list_access->info.b_eof;
1676 if( pb_eof ) *pb_eof = b_eof;
1678 /* If we reached an EOF then switch to the next stream in the list */
1679 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1681 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1682 access_t *p_list_access;
1684 msg_Dbg( s, "opening input `%s'", psz_name );
1686 p_list_access = access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1688 if( !p_list_access ) return 0;
1690 if( p_sys->p_list_access != p_access )
1691 access2_Delete( p_sys->p_list_access );
1693 p_sys->p_list_access = p_list_access;
1695 /* We have to read some data */
1696 return AReadBlock( s, pb_eof );
1702 vlc_object_yield( p_input );
1703 vlc_mutex_lock( &p_input->counters.counters_lock );
1704 stats_UpdateInteger( s, p_input->counters.p_read_bytes,
1705 p_block->i_buffer, &i_total );
1706 stats_UpdateFloat( s, p_input->counters.p_input_bitrate,
1707 (float)i_total, NULL );
1708 stats_UpdateInteger( s, p_input->counters.p_read_packets,
1710 vlc_mutex_unlock( &p_input->counters.counters_lock );
1711 vlc_object_release( p_input );
1717 static int ASeek( stream_t *s, int64_t i_pos )
1719 stream_sys_t *p_sys = s->p_sys;
1720 access_t *p_access = p_sys->p_access;
1722 /* Check which stream we need to access */
1728 access_t *p_list_access = 0;
1730 for( i = 0; i < p_sys->i_list - 1; i++ )
1732 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1733 i_size += p_sys->list[i]->i_size;
1735 psz_name = p_sys->list[i]->psz_path;
1737 if( i != p_sys->i_list_index )
1738 msg_Dbg( s, "opening input `%s'", psz_name );
1740 if( i != p_sys->i_list_index && i != 0 )
1743 access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1745 else if( i != p_sys->i_list_index )
1747 p_list_access = p_access;
1752 if( p_sys->p_list_access != p_access )
1753 access2_Delete( p_sys->p_list_access );
1755 p_sys->p_list_access = p_list_access;
1758 p_sys->i_list_index = i;
1759 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1763 return p_access->pf_seek( p_access, i_pos );