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 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_strings.h>
33 #include <vlc_charset.h>
37 #include "input_internal.h"
42 * - tune the 2 methods (block/stream)
43 * - compute cost for seek
44 * - improve stream mode seeking with closest segments
46 * - Maybe remove (block/stream) in favour of immediate
51 * One linked list of data read
53 * More complex scheme using mutliple track to avoid seeking
54 * - using directly the access (only indirection for peeking).
55 * This method is known to introduce much less latency.
56 * It should probably defaulted (instead of the stream method (2)).
59 /* How many tracks we have, currently only used for stream mode */
60 #ifdef OPTIMIZE_MEMORY
61 # define STREAM_CACHE_TRACK 1
62 /* Max size of our cache 128Ko per track */
63 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
65 # define STREAM_CACHE_TRACK 3
66 /* Max size of our cache 4Mo per track */
67 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
70 /* How many data we try to prebuffer */
71 #define STREAM_CACHE_PREBUFFER_SIZE (32767)
72 /* Maximum time we take to pre-buffer */
73 #define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
75 /* Method1: Simple, for pf_block.
76 * We get blocks and put them in the linked list.
77 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
79 #define STREAM_DATA_WAIT 40000 /* Time between before a pf_block retry */
81 /* Method2: A bit more complex, for pf_read
82 * - We use ring buffers, only one if unseekable, all if seekable
83 * - Upon seek date current ring, then search if one ring match the pos,
84 * yes: switch to it, seek the access to match the end of the ring
85 * no: search the ring with i_end the closer to i_pos,
86 * if close enough, read data and use this ring
87 * else use the oldest ring, seek and use it.
89 * TODO: - with access non seekable: use all space available for only one ring, but
90 * we have to support seekable/non-seekable switch on the fly.
91 * - compute a good value for i_read_size
94 #define STREAM_READ_ATONCE 32767
95 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
115 typedef enum stream_read_method_t
117 STREAM_METHOD_IMMEDIATE,
120 } stream_read_method_t;
126 stream_read_method_t method; /* method to use */
128 int64_t i_pos; /* Current reading offset */
130 /* Method 1: pf_block */
133 int64_t i_start; /* Offset of block for p_first */
134 int64_t i_offset; /* Offset for data in p_current */
135 block_t *p_current; /* Current block */
137 int i_size; /* Total amount of data in the list */
143 /* Method 2: for pf_read */
146 int i_offset; /* Buffer offset in the current track */
147 int i_tk; /* Current track */
148 stream_track_t tk[STREAM_CACHE_TRACK];
154 int i_used; /* Used since last read */
159 /* Method 3: for pf_read */
166 /* Peek temporary buffer */
170 /* Stat for both method */
173 bool b_fastseek; /* From access */
175 /* Stat about reading data */
176 int64_t i_read_count;
180 /* Stat about seek */
188 access_entry_t **list;
190 access_t *p_list_access;
192 /* Preparse mode ? */
200 FILE *f; /* TODO it could be replaced by access_output_t one day */
206 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read );
207 static int AStreamPeekBlock( stream_t *s, const uint8_t **p_peek, unsigned int i_read );
208 static int AStreamSeekBlock( stream_t *s, int64_t i_pos );
209 static void AStreamPrebufferBlock( stream_t *s );
210 static block_t *AReadBlock( stream_t *s, bool *pb_eof );
213 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read );
214 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read );
215 static int AStreamSeekStream( stream_t *s, int64_t i_pos );
216 static void AStreamPrebufferStream( stream_t *s );
217 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read );
220 static int AStreamReadImmediate( stream_t *s, void *p_read, unsigned int i_read );
221 static int AStreamPeekImmediate( stream_t *s, const uint8_t **pp_peek, unsigned int i_read );
222 static int AStreamSeekImmediate( stream_t *s, int64_t i_pos );
225 static int AStreamControl( stream_t *s, int i_query, va_list );
226 static void AStreamDestroy( stream_t *s );
227 static void UStreamDestroy( stream_t *s );
228 static int ASeek( stream_t *s, int64_t i_pos );
229 static int ARecordSetState( stream_t *s, bool b_record, const char *psz_extension );
230 static void ARecordWrite( stream_t *s, const uint8_t *p_buffer, size_t i_buffer );
232 /****************************************************************************
234 ****************************************************************************/
236 static inline int64_t stream_buffered_size( stream_t *s )
238 return s->p_sys->immediate.i_end;
241 static inline void stream_buffer_empty( stream_t *s, int length )
243 length = __MAX( stream_buffered_size( s ), length );
246 memmove( s->p_sys->immediate.p_buffer,
247 s->p_sys->immediate.p_buffer + length,
248 stream_buffered_size( s ) - length );
250 s->p_sys->immediate.i_end -= length;
253 static inline void stream_buffer_fill( stream_t *s, int length )
255 s->p_sys->immediate.i_end += length;
258 static inline uint8_t * stream_buffer( stream_t *s )
260 return s->p_sys->immediate.p_buffer;
263 /****************************************************************************
264 * stream_UrlNew: create a stream from a access
265 ****************************************************************************/
266 stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
268 const char *psz_access, *psz_demux;
276 char psz_dup[strlen( psz_url ) + 1];
277 strcpy( psz_dup, psz_url );
278 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
280 /* Now try a real access */
281 p_access = access_New( p_parent, psz_access, psz_demux, psz_path );
283 if( p_access == NULL )
285 msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
289 if( !( p_res = stream_AccessNew( p_access, true ) ) )
291 access_Delete( p_access );
295 p_res->pf_destroy = UStreamDestroy;
299 stream_t *stream_AccessNew( access_t *p_access, bool b_quick )
301 stream_t *s = vlc_stream_create( VLC_OBJECT(p_access) );
303 char *psz_list = NULL;
305 if( !s ) return NULL;
307 /* Attach it now, needed for b_die */
308 vlc_object_attach( s, p_access );
310 s->pf_read = NULL; /* Set up later */
312 s->pf_control = AStreamControl;
313 s->pf_destroy = AStreamDestroy;
315 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
319 /* UTF16 and UTF32 text file conversion */
321 s->b_little_endian = false;
322 s->conv = (vlc_iconv_t)(-1);
325 p_sys->p_access = p_access;
326 if( p_access->pf_block )
327 p_sys->method = STREAM_METHOD_BLOCK;
328 else if( var_CreateGetBool( s, "use-stream-immediate" ) )
329 p_sys->method = STREAM_METHOD_IMMEDIATE;
331 p_sys->method = STREAM_METHOD_STREAM;
333 p_sys->record.b_active = false;
335 p_sys->i_pos = p_access->info.i_pos;
338 access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
339 p_sys->stat.i_bytes = 0;
340 p_sys->stat.i_read_time = 0;
341 p_sys->stat.i_read_count = 0;
342 p_sys->stat.i_seek_count = 0;
343 p_sys->stat.i_seek_time = 0;
347 p_sys->i_list_index = 0;
348 p_sys->p_list_access = 0;
350 p_sys->b_quick = b_quick;
352 /* Get the additional list of inputs if any (for concatenation) */
353 if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
355 access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
356 if( p_entry == NULL )
358 char *psz_name, *psz_parser = psz_name = psz_list;
360 p_sys->p_list_access = p_access;
361 p_entry->i_size = p_access->info.i_size;
362 p_entry->psz_path = strdup( p_access->psz_path );
363 if( p_entry->psz_path == NULL )
368 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
369 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
370 p_entry->psz_path, p_access->info.i_size );
372 while( psz_name && *psz_name )
374 psz_parser = strchr( psz_name, ',' );
375 if( psz_parser ) *psz_parser = 0;
377 psz_name = strdup( psz_name );
380 access_t *p_tmp = access_New( p_access, p_access->psz_access,
385 psz_name = psz_parser;
386 if( psz_name ) psz_name++;
390 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
391 psz_name, p_tmp->info.i_size );
393 p_entry = malloc( sizeof(access_entry_t) );
394 if( p_entry == NULL )
396 p_entry->i_size = p_tmp->info.i_size;
397 p_entry->psz_path = psz_name;
398 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
400 access_Delete( p_tmp );
403 psz_name = psz_parser;
404 if( psz_name ) psz_name++;
407 FREENULL( psz_list );
411 p_sys->p_peek = NULL;
413 if( p_sys->method == STREAM_METHOD_BLOCK )
415 msg_Dbg( s, "Using AStream*Block" );
416 s->pf_read = AStreamReadBlock;
417 s->pf_peek = AStreamPeekBlock;
419 /* Init all fields of p_sys->block */
420 p_sys->block.i_start = p_sys->i_pos;
421 p_sys->block.i_offset = 0;
422 p_sys->block.p_current = NULL;
423 p_sys->block.i_size = 0;
424 p_sys->block.p_first = NULL;
425 p_sys->block.pp_last = &p_sys->block.p_first;
427 /* Do the prebuffering */
428 AStreamPrebufferBlock( s );
430 if( p_sys->block.i_size <= 0 )
432 msg_Err( s, "cannot pre fill buffer" );
436 else if( p_sys->method == STREAM_METHOD_IMMEDIATE )
438 msg_Dbg( s, "Using AStream*Immediate" );
440 s->pf_read = AStreamReadImmediate;
441 s->pf_peek = AStreamPeekImmediate;
443 /* Allocate/Setup our tracks (useful to peek)*/
444 p_sys->immediate.i_end = 0;
445 p_sys->immediate.p_buffer = malloc( STREAM_CACHE_SIZE );
447 msg_Dbg( s, "p_buffer %p-%p",
448 p_sys->immediate.p_buffer,
449 &p_sys->immediate.p_buffer[STREAM_CACHE_SIZE] );
451 if( p_sys->immediate.p_buffer == NULL )
453 msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
462 assert( p_sys->method == STREAM_METHOD_STREAM );
464 msg_Dbg( s, "Using AStream*Stream" );
466 s->pf_read = AStreamReadStream;
467 s->pf_peek = AStreamPeekStream;
469 /* Allocate/Setup our tracks */
470 p_sys->stream.i_offset = 0;
471 p_sys->stream.i_tk = 0;
472 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
473 if( p_sys->stream.p_buffer == NULL )
475 msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
479 p_sys->stream.i_used = 0;
480 access_Control( p_access, ACCESS_GET_MTU,
481 &p_sys->stream.i_read_size );
482 if( p_sys->stream.i_read_size <= 0 )
483 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
484 else if( p_sys->stream.i_read_size <= 256 )
485 p_sys->stream.i_read_size = 256;
487 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
489 p_sys->stream.tk[i].i_date = 0;
490 p_sys->stream.tk[i].i_start = p_sys->i_pos;
491 p_sys->stream.tk[i].i_end = p_sys->i_pos;
492 p_sys->stream.tk[i].p_buffer=
493 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
496 /* Do the prebuffering */
497 AStreamPrebufferStream( s );
499 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
501 msg_Err( s, "cannot pre fill buffer" );
509 if( p_sys->method == STREAM_METHOD_BLOCK )
515 free( p_sys->stream.p_buffer );
517 while( p_sys->i_list > 0 )
518 free( p_sys->list[--(p_sys->i_list)] );
522 vlc_object_detach( s );
523 vlc_object_release( s );
527 /****************************************************************************
529 ****************************************************************************/
530 static void AStreamDestroy( stream_t *s )
532 stream_sys_t *p_sys = s->p_sys;
534 vlc_object_detach( s );
536 if( p_sys->record.b_active )
537 ARecordSetState( s, false, NULL );
539 if( p_sys->method == STREAM_METHOD_BLOCK )
540 block_ChainRelease( p_sys->block.p_first );
541 else if( p_sys->method == STREAM_METHOD_IMMEDIATE )
542 free( p_sys->immediate.p_buffer );
544 free( p_sys->stream.p_buffer );
546 free( p_sys->p_peek );
548 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
549 access_Delete( p_sys->p_list_access );
551 while( p_sys->i_list-- )
553 free( p_sys->list[p_sys->i_list]->psz_path );
554 free( p_sys->list[p_sys->i_list] );
560 vlc_object_release( s );
563 static void UStreamDestroy( stream_t *s )
565 access_t *p_access = (access_t *)s->p_parent;
567 access_Delete( p_access );
570 /****************************************************************************
571 * stream_AccessReset:
572 ****************************************************************************/
573 void stream_AccessReset( stream_t *s )
575 stream_sys_t *p_sys = s->p_sys;
577 p_sys->i_pos = p_sys->p_access->info.i_pos;
579 if( p_sys->method == STREAM_METHOD_BLOCK )
581 block_ChainRelease( p_sys->block.p_first );
583 /* Init all fields of p_sys->block */
584 p_sys->block.i_start = p_sys->i_pos;
585 p_sys->block.i_offset = 0;
586 p_sys->block.p_current = NULL;
587 p_sys->block.i_size = 0;
588 p_sys->block.p_first = NULL;
589 p_sys->block.pp_last = &p_sys->block.p_first;
591 /* Do the prebuffering */
592 AStreamPrebufferBlock( s );
594 else if( p_sys->method == STREAM_METHOD_IMMEDIATE )
596 stream_buffer_empty( s, stream_buffered_size( s ) );
602 assert( p_sys->method == STREAM_METHOD_STREAM );
604 /* Setup our tracks */
605 p_sys->stream.i_offset = 0;
606 p_sys->stream.i_tk = 0;
607 p_sys->stream.i_used = 0;
609 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
611 p_sys->stream.tk[i].i_date = 0;
612 p_sys->stream.tk[i].i_start = p_sys->i_pos;
613 p_sys->stream.tk[i].i_end = p_sys->i_pos;
616 /* Do the prebuffering */
617 AStreamPrebufferStream( s );
621 /****************************************************************************
622 * stream_AccessUpdate:
623 ****************************************************************************/
624 void stream_AccessUpdate( stream_t *s )
626 stream_sys_t *p_sys = s->p_sys;
628 p_sys->i_pos = p_sys->p_access->info.i_pos;
633 for( i = 0; i < p_sys->i_list_index; i++ )
635 p_sys->i_pos += p_sys->list[i]->i_size;
640 /****************************************************************************
642 ****************************************************************************/
643 static int AStreamControl( stream_t *s, int i_query, va_list args )
645 stream_sys_t *p_sys = s->p_sys;
646 access_t *p_access = p_sys->p_access;
650 const char *psz_string;
651 int64_t *pi_64, i_64;
656 case STREAM_GET_SIZE:
657 pi_64 = (int64_t*)va_arg( args, int64_t * );
658 if( s->p_sys->i_list )
662 for( i = 0; i < s->p_sys->i_list; i++ )
663 *pi_64 += s->p_sys->list[i]->i_size;
666 *pi_64 = p_access->info.i_size;
669 case STREAM_CAN_SEEK:
670 p_bool = (bool*)va_arg( args, bool * );
671 access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
674 case STREAM_CAN_FASTSEEK:
675 p_bool = (bool*)va_arg( args, bool * );
676 access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
679 case STREAM_GET_POSITION:
680 pi_64 = (int64_t*)va_arg( args, int64_t * );
681 *pi_64 = p_sys->i_pos;
684 case STREAM_SET_POSITION:
685 i_64 = (int64_t)va_arg( args, int64_t );
686 switch( p_sys->method )
688 case STREAM_METHOD_BLOCK:
689 return AStreamSeekBlock( s, i_64 );
690 case STREAM_METHOD_IMMEDIATE:
691 return AStreamSeekImmediate( s, i_64 );
692 case STREAM_METHOD_STREAM:
693 return AStreamSeekStream( s, i_64 );
702 case STREAM_CONTROL_ACCESS:
703 i_int = (int) va_arg( args, int );
704 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
705 i_int != ACCESS_SET_PRIVATE_ID_CA &&
706 i_int != ACCESS_GET_PRIVATE_ID_STATE )
708 msg_Err( s, "Hey, what are you thinking ?"
709 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
712 return access_vaControl( p_access, i_int, args );
714 case STREAM_GET_CONTENT_TYPE:
715 return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
716 va_arg( args, char ** ) );
717 case STREAM_SET_RECORD_STATE:
718 b_bool = (bool)va_arg( args, int );
721 psz_string = (const char*)va_arg( args, const char* );
722 return ARecordSetState( s, b_bool, psz_string );
725 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
731 /****************************************************************************
732 * ARecord*: record stream functions
733 ****************************************************************************/
734 static int ARecordStart( stream_t *s, const char *psz_extension )
736 stream_sys_t *p_sys = s->p_sys;
743 psz_extension = "dat";
746 char *psz_path = var_CreateGetString( s, "input-record-path" );
747 if( !psz_path || *psz_path == '\0' )
750 psz_path = strdup( config_GetHomeDir() );
757 * TODO allow prefix configuration */
758 psz_file = input_CreateFilename( VLC_OBJECT(s), psz_path, INPUT_RECORD_PREFIX, psz_extension );
765 f = utf8_fopen( psz_file, "wb" );
771 msg_Dbg( s, "Recording into %s", psz_file );
776 p_sys->record.b_active = true;
777 p_sys->record.b_error = false;
780 static int ARecordStop( stream_t *s )
782 stream_sys_t *p_sys = s->p_sys;
784 assert( p_sys->record.b_active );
786 msg_Dbg( s, "Recording completed" );
787 fclose( p_sys->record.f );
788 p_sys->record.b_active = false;
792 static int ARecordSetState( stream_t *s, bool b_record, const char *psz_extension )
794 stream_sys_t *p_sys = s->p_sys;
796 if( !!p_sys->record.b_active == !!b_record )
800 return ARecordStart( s, psz_extension );
802 return ARecordStop( s );
804 static void ARecordWrite( stream_t *s, const uint8_t *p_buffer, size_t i_buffer )
806 stream_sys_t *p_sys = s->p_sys;
808 assert( p_sys->record.b_active );
812 const bool b_previous_error = p_sys->record.b_error;
813 const size_t i_written = fwrite( p_buffer, 1, i_buffer, p_sys->record.f );
815 p_sys->record.b_error = i_written != i_buffer;
817 /* TODO maybe a intf_UserError or something like that ? */
818 if( p_sys->record.b_error && !b_previous_error )
819 msg_Err( s, "Failed to record data (begin)" );
820 else if( !p_sys->record.b_error && b_previous_error )
821 msg_Err( s, "Failed to record data (end)" );
825 /****************************************************************************
827 ****************************************************************************/
828 static void AStreamPrebufferBlock( stream_t *s )
830 stream_sys_t *p_sys = s->p_sys;
831 access_t *p_access = p_sys->p_access;
836 msg_Dbg( s, "pre buffering" );
840 int64_t i_date = mdate();
844 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
845 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
850 p_sys->stat.i_bytes = p_sys->block.i_size;
851 p_sys->stat.i_read_time = i_date - i_start;
852 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
853 (p_sys->stat.i_read_time + 1);
855 msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
856 "%"PRId64" kbytes/s",
858 p_sys->stat.i_read_time / INT64_C(1000000),
864 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
868 msleep( STREAM_DATA_WAIT );
874 /* Append the block */
875 p_sys->block.i_size += b->i_buffer;
876 *p_sys->block.pp_last = b;
877 p_sys->block.pp_last = &b->p_next;
879 p_sys->stat.i_read_count++;
883 if( p_access->info.b_prebuffered )
885 /* Access has already prebufferred - update stats and exit */
886 p_sys->stat.i_bytes = p_sys->block.i_size;
887 p_sys->stat.i_read_time = mdate() - i_start;
894 msg_Dbg( s, "received first data for our buffer");
899 p_sys->block.p_current = p_sys->block.p_first;
902 static int AStreamRefillBlock( stream_t *s );
904 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
906 stream_sys_t *p_sys = s->p_sys;
908 uint8_t *p_data = p_read;
909 uint8_t *p_record = p_data;
910 unsigned int i_data = 0;
913 if( p_sys->block.p_current == NULL )
916 if( p_sys->record.b_active && !p_data )
917 p_record = p_data = malloc( i_read );
921 /* seek within this stream if possible, else use plain old read and discard */
922 stream_sys_t *p_sys = s->p_sys;
923 access_t *p_access = p_sys->p_access;
925 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
927 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
930 while( i_data < i_read )
933 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
934 unsigned int i_copy = __MIN( (unsigned int)__MAX(i_current,0), i_read - i_data);
940 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
946 p_sys->block.i_offset += i_copy;
947 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
949 /* Current block is now empty, switch to next */
950 if( p_sys->block.p_current )
952 p_sys->block.i_offset = 0;
953 p_sys->block.p_current = p_sys->block.p_current->p_next;
955 /*Get a new block if needed */
956 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
963 if( p_sys->record.b_active )
965 if( i_data > 0 && p_record != NULL)
966 ARecordWrite( s, p_record, i_data );
971 p_sys->i_pos += i_data;
975 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
977 stream_sys_t *p_sys = s->p_sys;
979 unsigned int i_data = 0;
981 unsigned int i_offset;
983 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
985 /* We can directly give a pointer over our buffer */
986 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
988 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
992 /* We need to create a local copy */
993 if( p_sys->i_peek < i_read )
995 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1001 p_sys->i_peek = i_read;
1004 /* Fill enough data */
1005 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
1008 block_t **pp_last = p_sys->block.pp_last;
1010 if( AStreamRefillBlock( s ) ) break;
1012 /* Our buffer are probably filled enough, don't try anymore */
1013 if( pp_last == p_sys->block.pp_last ) break;
1016 /* Copy what we have */
1017 b = p_sys->block.p_current;
1018 i_offset = p_sys->block.i_offset;
1019 p_data = p_sys->p_peek;
1021 while( b && i_data < i_read )
1023 unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
1024 int i_copy = __MIN( i_current, i_read - i_data );
1026 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
1031 if( i_offset >= b->i_buffer )
1038 *pp_peek = p_sys->p_peek;
1042 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
1044 stream_sys_t *p_sys = s->p_sys;
1045 access_t *p_access = p_sys->p_access;
1046 int64_t i_offset = i_pos - p_sys->block.i_start;
1049 /* We already have thoses data, just update p_current/i_offset */
1050 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
1052 block_t *b = p_sys->block.p_first;
1055 while( i_current + b->i_buffer < i_offset )
1057 i_current += b->i_buffer;
1061 p_sys->block.p_current = b;
1062 p_sys->block.i_offset = i_offset - i_current;
1064 p_sys->i_pos = i_pos;
1069 /* We may need to seek or to read data */
1073 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1077 msg_Err( s, "backward seeking impossible (access not seekable)" );
1078 return VLC_EGENERIC;
1085 bool b_aseek, b_aseekfast;
1087 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1088 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
1093 msg_Warn( s, "%"PRId64" bytes need to be skipped "
1094 "(access non seekable)",
1095 i_offset - p_sys->block.i_size );
1099 int64_t i_skip = i_offset - p_sys->block.i_size;
1101 /* Avg bytes per packets */
1102 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
1103 /* TODO compute a seek cost instead of fixed threshold */
1104 int i_th = b_aseekfast ? 1 : 5;
1106 if( i_skip <= i_th * i_avg &&
1107 i_skip < STREAM_CACHE_SIZE )
1112 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
1113 b_seek, i_th*i_avg, i_skip );
1119 int64_t i_start, i_end;
1120 /* Do the access seek */
1122 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1126 block_ChainRelease( p_sys->block.p_first );
1129 p_sys->block.i_start = p_sys->i_pos = i_pos;
1130 p_sys->block.i_offset = 0;
1131 p_sys->block.p_current = NULL;
1132 p_sys->block.i_size = 0;
1133 p_sys->block.p_first = NULL;
1134 p_sys->block.pp_last = &p_sys->block.p_first;
1136 /* Refill a block */
1137 if( AStreamRefillBlock( s ) )
1138 return VLC_EGENERIC;
1141 p_sys->stat.i_seek_time += i_end - i_start;
1142 p_sys->stat.i_seek_count++;
1149 /* Read and skip enough data */
1150 if( AStreamRefillBlock( s ) )
1151 return VLC_EGENERIC;
1153 while( p_sys->block.p_current &&
1154 p_sys->i_pos + p_sys->block.p_current->i_buffer - p_sys->block.i_offset < i_pos )
1156 p_sys->i_pos += p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
1157 p_sys->block.p_current = p_sys->block.p_current->p_next;
1158 p_sys->block.i_offset = 0;
1161 while( p_sys->block.i_start + p_sys->block.i_size < i_pos );
1163 p_sys->block.i_offset = i_pos - p_sys->i_pos;
1164 p_sys->i_pos = i_pos;
1169 return VLC_EGENERIC;
1172 static int AStreamRefillBlock( stream_t *s )
1174 stream_sys_t *p_sys = s->p_sys;
1175 int64_t i_start, i_stop;
1179 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1180 p_sys->block.p_first != p_sys->block.p_current )
1182 block_t *b = p_sys->block.p_first;
1184 p_sys->block.i_start += b->i_buffer;
1185 p_sys->block.i_size -= b->i_buffer;
1186 p_sys->block.p_first = b->p_next;
1190 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1191 p_sys->block.p_current == p_sys->block.p_first &&
1192 p_sys->block.p_current->p_next ) /* At least 2 packets */
1194 /* Enough data, don't read more */
1198 /* Now read a new block */
1204 if( s->b_die ) return VLC_EGENERIC;
1208 if( ( b = AReadBlock( s, &b_eof ) ) ) break;
1210 if( b_eof ) return VLC_EGENERIC;
1212 msleep( STREAM_DATA_WAIT );
1219 /* Append the block */
1220 p_sys->block.i_size += b->i_buffer;
1221 *p_sys->block.pp_last = b;
1222 p_sys->block.pp_last = &b->p_next;
1225 if( p_sys->block.p_current == NULL )
1226 p_sys->block.p_current = b;
1229 p_sys->stat.i_bytes += b->i_buffer;
1230 p_sys->stat.i_read_time += i_stop - i_start;
1231 p_sys->stat.i_read_count++;
1240 /****************************************************************************
1242 ****************************************************************************/
1243 static int AStreamRefillStream( stream_t *s );
1245 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read )
1247 stream_sys_t *p_sys = s->p_sys;
1248 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1250 uint8_t *p_data = (uint8_t *)p_read;
1251 uint8_t *p_record = p_data;
1252 unsigned int i_data = 0;
1254 if( tk->i_start >= tk->i_end )
1257 if( p_sys->record.b_active && !p_data )
1258 p_record = p_data = malloc( i_read );
1260 if( p_data == NULL )
1262 /* seek within this stream if possible, else use plain old read and discard */
1263 stream_sys_t *p_sys = s->p_sys;
1264 access_t *p_access = p_sys->p_access;
1266 /* seeking after EOF is not what we want */
1267 if( !( p_access->info.b_eof ) )
1270 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1272 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1277 msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1278 " offset=%d end=%"PRId64,
1279 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1280 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1283 while( i_data < i_read )
1285 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1286 STREAM_CACHE_TRACK_SIZE;
1287 unsigned int i_current =
1288 __MAX(0,__MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1289 STREAM_CACHE_TRACK_SIZE - i_off ));
1290 int i_copy = __MIN( i_current, i_read - i_data );
1292 if( i_copy <= 0 ) break; /* EOF */
1295 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1298 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1302 p_sys->stream.i_offset += i_copy;
1304 /* Update pos now */
1305 p_sys->i_pos += i_copy;
1308 p_sys->stream.i_used += i_copy;
1309 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1310 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1312 if( AStreamRefillStream( s ) )
1315 if( tk->i_start >= tk->i_end ) break;
1320 if( p_sys->record.b_active )
1322 if( i_data > 0 && p_record != NULL)
1323 ARecordWrite( s, p_record, i_data );
1331 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1333 stream_sys_t *p_sys = s->p_sys;
1334 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1337 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1340 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1341 "start=%"PRId64" offset=%d end=%"PRId64,
1342 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1343 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1346 /* Avoid problem, but that should *never* happen */
1347 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1348 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1350 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1352 if( p_sys->stream.i_used <= 1 )
1354 /* Be sure we will read something */
1355 p_sys->stream.i_used += i_read -
1356 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1358 if( AStreamRefillStream( s ) ) break;
1361 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1362 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1364 /* Now, direct pointer or a copy ? */
1365 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1366 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1368 *pp_peek = &tk->p_buffer[i_off];
1372 if( p_sys->i_peek < i_read )
1374 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1375 if( !p_sys->p_peek )
1380 p_sys->i_peek = i_read;
1383 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1384 STREAM_CACHE_TRACK_SIZE - i_off );
1385 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1386 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1388 *pp_peek = p_sys->p_peek;
1392 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1394 stream_sys_t *p_sys = s->p_sys;
1395 access_t *p_access = p_sys->p_access;
1403 msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1404 " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1405 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1406 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1407 p_sys->stream.i_offset,
1408 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1412 /* Seek in our current track ? */
1413 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1414 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1416 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1418 msg_Dbg( s, "AStreamSeekStream: current track" );
1420 p_sys->i_pos = i_pos;
1421 p_sys->stream.i_offset = i_pos - tk->i_start;
1423 /* If there is not enough data left in the track, refill */
1424 /* \todo How to get a correct value for
1425 * - refilling threshold
1426 * - how much to refill
1428 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1429 p_sys->stream.i_read_size )
1431 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1433 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1434 AStreamRefillStream( s );
1440 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1443 /* We can't do nothing */
1444 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1445 return VLC_EGENERIC;
1448 /* Date the current track */
1449 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1451 /* Try to reuse already read data */
1452 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1454 stream_track_t *tk = &p_sys->stream.tk[i];
1456 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1459 msg_Dbg( s, "AStreamSeekStream: reusing %d start=%"PRId64
1460 " end=%"PRId64, i, tk->i_start, tk->i_end );
1463 /* Seek at the end of the buffer */
1464 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1467 p_sys->i_pos = i_pos;
1468 p_sys->stream.i_tk = i;
1469 p_sys->stream.i_offset = i_pos - tk->i_start;
1471 if( p_sys->stream.i_used < 1024 )
1472 p_sys->stream.i_used = 1024;
1474 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1475 return VLC_EGENERIC;
1481 access_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1482 /* FIXME compute seek cost (instead of static 'stupid' value) */
1483 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1489 /* Search closest segment TODO */
1490 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1492 stream_track_t *tk = &p_sys->stream.tk[i];
1494 if( i_pos + i_maxth >= tk->i_start )
1496 msg_Dbg( s, "good segment before current pos, TODO" );
1498 if( i_pos - i_maxth <= tk->i_end )
1500 msg_Dbg( s, "good segment after current pos, TODO" );
1505 /* Nothing good, seek and choose oldest segment */
1506 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1507 p_sys->i_pos = i_pos;
1510 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1512 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1516 /* Reset the segment */
1517 p_sys->stream.i_tk = i_new;
1518 p_sys->stream.i_offset = 0;
1519 p_sys->stream.tk[i_new].i_start = i_pos;
1520 p_sys->stream.tk[i_new].i_end = i_pos;
1523 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1524 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1526 if( AStreamRefillStream( s ) )
1527 return VLC_EGENERIC;
1532 static int AStreamRefillStream( stream_t *s )
1534 stream_sys_t *p_sys = s->p_sys;
1535 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1537 /* We read but won't increase i_start after initial start + offset */
1539 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1540 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1541 bool b_read = false;
1542 int64_t i_start, i_stop;
1544 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1547 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1548 p_sys->stream.i_used, i_toread );
1552 while( i_toread > 0 )
1554 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1558 return VLC_EGENERIC;
1560 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1561 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1563 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1566 msleep( STREAM_DATA_WAIT );
1569 else if( i_read == 0 )
1571 if( !b_read ) return VLC_EGENERIC;
1577 tk->i_end += i_read;
1579 /* Windows of STREAM_CACHE_TRACK_SIZE */
1580 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1582 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1584 tk->i_start += i_invalid;
1585 p_sys->stream.i_offset -= i_invalid;
1589 p_sys->stream.i_used -= i_read;
1591 p_sys->stat.i_bytes += i_read;
1592 p_sys->stat.i_read_count++;
1596 p_sys->stat.i_read_time += i_stop - i_start;
1601 static void AStreamPrebufferStream( stream_t *s )
1603 stream_sys_t *p_sys = s->p_sys;
1604 access_t *p_access = p_sys->p_access;
1606 int64_t i_first = 0;
1608 int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1609 ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1610 STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1612 msg_Dbg( s, "pre-buffering..." );
1616 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1618 int64_t i_date = mdate();
1621 if( s->b_die || tk->i_end >= i_prebuffer ||
1622 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1627 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1628 p_sys->stat.i_read_time = i_date - i_start;
1629 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1630 (p_sys->stat.i_read_time+1);
1632 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1633 "%"PRId64" kbytes/s",
1634 p_sys->stat.i_bytes,
1635 p_sys->stat.i_read_time / INT64_C(1000000),
1636 i_byterate / 1024 );
1641 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1642 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1643 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1646 msleep( STREAM_DATA_WAIT );
1649 else if( i_read == 0 )
1658 msg_Dbg( s, "received first data for our buffer");
1661 tk->i_end += i_read;
1663 p_sys->stat.i_read_count++;
1667 /****************************************************************************
1669 ****************************************************************************/
1671 static int AStreamReadImmediate( stream_t *s, void *p_read, unsigned int i_read )
1673 stream_sys_t *p_sys = s->p_sys;
1674 uint8_t *p_data= (uint8_t*)p_read;
1675 uint8_t *p_record = p_data;
1676 unsigned int i_data;
1679 msg_Dbg( s, "AStreamReadImmediate p_read=%p i_read=%d",
1683 if( p_sys->record.b_active && !p_data )
1684 p_record = p_data = malloc( i_read );
1686 /* First, check if we already have some data in the buffer,
1687 * that we could copy directly */
1688 i_data = __MIN( stream_buffered_size( s ), i_read );
1692 msg_Dbg( s, "AStreamReadImmediate: copy %u from %p", i_data, stream_buffer( s ) );
1695 assert( i_data <= STREAM_CACHE_SIZE );
1699 memcpy( p_data, stream_buffer( s ), i_data );
1704 /* Now that we've read our buffer we don't need its i_copy bytes */
1705 stream_buffer_empty( s, i_data );
1707 /* Now check if we have still to really read some data */
1708 while( i_data < i_read )
1710 const unsigned int i_to_read = i_read - i_data;
1715 i_result = AReadStream( s, p_data, i_to_read );
1719 void *p_dummy = malloc( i_to_read );
1724 i_result = AReadStream( s, p_dummy, i_to_read );
1731 p_sys->i_pos += i_data;
1737 if( p_sys->record.b_active )
1739 if( i_data > 0 && p_record != NULL)
1740 ARecordWrite( s, p_record, i_data );
1747 static int AStreamPeekImmediate( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1750 msg_Dbg( s, "AStreamPeekImmediate: %d size=%"PRId64,
1751 i_read, size_buffered_size( s ) );
1754 /* Avoid problem, but that shouldn't happen
1755 * FIXME yes it can */
1756 if( i_read > STREAM_CACHE_SIZE / 2 )
1757 i_read = STREAM_CACHE_SIZE / 2;
1759 int i_to_read = i_read - stream_buffered_size( s );
1763 msg_Dbg( s, "AStreamPeekImmediate: Reading %d",
1766 i_to_read = AReadStream( s, stream_buffer( s ) + stream_buffered_size( s ),
1770 stream_buffer_fill( s, i_to_read );
1773 *pp_peek = stream_buffer( s );
1775 return __MIN( stream_buffered_size( s ), i_read );
1778 static int AStreamSeekImmediate( stream_t *s, int64_t i_pos )
1780 stream_sys_t *p_sys = s->p_sys;
1781 access_t *p_access = p_sys->p_access;
1785 msg_Dbg( s, "AStreamSeekImmediate to %"PRId64" pos=%"PRId64
1786 i_pos, p_sys->i_pos );
1789 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1792 /* We can't do nothing */
1793 msg_Dbg( s, "AStreamSeekImmediate: can't seek" );
1794 return VLC_EGENERIC;
1797 /* Just reset our buffer */
1798 stream_buffer_empty( s, stream_buffered_size( s ) );
1800 if( ASeek( s, i_pos ) )
1801 return VLC_EGENERIC;
1806 /****************************************************************************
1808 ****************************************************************************/
1810 * Read from the stream untill first newline.
1811 * \param s Stream handle to read from
1812 * \return A pointer to the allocated output string. You need to free this when you are done.
1814 #define STREAM_PROBE_LINE 2048
1815 #define STREAM_LINE_MAX (2048*100)
1816 char * stream_ReadLine( stream_t *s )
1818 char *p_line = NULL;
1819 int i_line = 0, i_read = 0;
1821 while( i_read < STREAM_LINE_MAX )
1824 const uint8_t *p_data;
1828 /* Probe new data */
1829 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1830 if( i_data <= 0 ) break; /* No more data */
1833 i_pos = stream_Tell( s );
1834 if( i_pos == 0 && i_data > 4 )
1837 char *psz_encoding = NULL;
1839 if( p_data[0] == 0xEF && p_data[1] == 0xBB && p_data[2] == 0xBF )
1841 psz_encoding = strdup( "UTF-8" );
1844 else if( p_data[0] == 0x00 && p_data[1] == 0x00 )
1846 if( p_data[2] == 0xFE && p_data[3] == 0xFF )
1848 psz_encoding = strdup( "UTF-32BE" );
1849 s->i_char_width = 4;
1853 else if( p_data[0] == 0xFF && p_data[1] == 0xFE )
1855 if( p_data[2] == 0x00 && p_data[3] == 0x00 )
1857 psz_encoding = strdup( "UTF-32LE" );
1858 s->i_char_width = 4;
1859 s->b_little_endian = true;
1864 psz_encoding = strdup( "UTF-16LE" );
1865 s->b_little_endian = true;
1866 s->i_char_width = 2;
1870 else if( p_data[0] == 0xFE && p_data[1] == 0xFF )
1872 psz_encoding = strdup( "UTF-16BE" );
1873 s->i_char_width = 2;
1877 /* Seek past the BOM */
1880 stream_Seek( s, i_bom_size );
1881 p_data += i_bom_size;
1882 i_data -= i_bom_size;
1885 /* Open the converter if we need it */
1886 if( psz_encoding != NULL )
1888 input_thread_t *p_input;
1889 msg_Dbg( s, "%s BOM detected", psz_encoding );
1890 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1891 if( s->i_char_width > 1 )
1893 s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1894 if( s->conv == (vlc_iconv_t)-1 )
1896 msg_Err( s, "iconv_open failed" );
1899 if( p_input != NULL)
1901 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1902 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1903 vlc_object_release( p_input );
1905 free( psz_encoding );
1909 if( i_data % s->i_char_width )
1911 /* keep i_char_width boundary */
1912 i_data = i_data - ( i_data % s->i_char_width );
1913 msg_Warn( s, "the read is not i_char_width compatible");
1919 /* Check if there is an EOL */
1920 if( s->i_char_width == 1 )
1922 /* UTF-8: 0A <LF> */
1923 psz_eol = memchr( p_data, '\n', i_data );
1927 const uint8_t *p = p_data;
1928 const uint8_t *p_last = p + i_data - s->i_char_width;
1930 if( s->i_char_width == 2 )
1932 if( s->b_little_endian == true)
1934 /* UTF-16LE: 0A 00 <LF> */
1935 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1940 /* UTF-16BE: 00 0A <LF> */
1941 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1945 else if( s->i_char_width == 4 )
1947 if( s->b_little_endian == true)
1949 /* UTF-32LE: 0A 00 00 00 <LF> */
1950 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ||
1951 p[2] != 0x00 || p[3] != 0x00 ) )
1956 /* UTF-32BE: 00 00 00 0A <LF> */
1957 while( p <= p_last && ( p[3] != 0x0A || p[2] != 0x00 ||
1958 p[1] != 0x00 || p[0] != 0x00 ) )
1969 psz_eol = (char *)p + ( s->i_char_width - 1 );
1975 i_data = (psz_eol - (char *)p_data) + 1;
1976 p_line = realloc( p_line, i_line + i_data + s->i_char_width ); /* add \0 */
1979 i_data = stream_Read( s, &p_line[i_line], i_data );
1980 if( i_data <= 0 ) break; /* Hmmm */
1981 i_line += i_data - s->i_char_width; /* skip \n */;
1984 /* We have our line */
1988 /* Read data (+1 for easy \0 append) */
1989 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->i_char_width );
1992 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1993 if( i_data <= 0 ) break; /* Hmmm */
2001 for( j = 0; j < s->i_char_width; j++ )
2003 p_line[i_line + j] = '\0';
2005 i_line += s->i_char_width; /* the added \0 */
2006 if( s->i_char_width > 1 )
2008 size_t i_in = 0, i_out = 0;
2009 const char * p_in = NULL;
2010 char * p_out = NULL;
2011 char * psz_new_line = NULL;
2014 psz_new_line = malloc( i_line );
2015 if( psz_new_line == NULL )
2017 i_in = i_out = (size_t)i_line;
2019 p_out = psz_new_line;
2021 if( vlc_iconv( s->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
2023 msg_Err( s, "iconv failed" );
2024 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
2027 p_line = psz_new_line;
2028 i_line = (size_t)i_line - i_out; /* does not include \0 */
2031 /* Remove trailing LF/CR */
2032 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
2033 p_line[i_line-2] == '\n') ) i_line--;
2035 /* Make sure the \0 is there */
2036 p_line[i_line-1] = '\0';
2043 /* We failed to read any data, probably EOF */
2045 if( s->conv != (vlc_iconv_t)(-1) ) vlc_iconv_close( s->conv );
2049 /****************************************************************************
2050 * Access reading/seeking wrappers to handle concatenated streams.
2051 ****************************************************************************/
2052 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read )
2054 stream_sys_t *p_sys = s->p_sys;
2055 access_t *p_access = p_sys->p_access;
2056 input_thread_t *p_input = NULL;
2057 int i_read_orig = i_read;
2060 if( s->p_parent && s->p_parent->p_parent &&
2061 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
2062 p_input = (input_thread_t *)s->p_parent->p_parent;
2064 if( !p_sys->i_list )
2066 i_read = p_access->pf_read( p_access, p_read, i_read );
2067 if( p_access->b_die )
2068 vlc_object_kill( s );
2071 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2072 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
2074 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2075 (float)i_total, NULL );
2076 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2077 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2082 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
2084 if( p_access->b_die )
2085 vlc_object_kill( s );
2087 /* If we reached an EOF then switch to the next stream in the list */
2088 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
2090 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
2091 access_t *p_list_access;
2093 msg_Dbg( s, "opening input `%s'", psz_name );
2095 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
2097 if( !p_list_access ) return 0;
2099 if( p_sys->p_list_access != p_access )
2100 access_Delete( p_sys->p_list_access );
2102 p_sys->p_list_access = p_list_access;
2104 /* We have to read some data */
2105 return AReadStream( s, p_read, i_read_orig );
2108 /* Update read bytes in input */
2111 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2112 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
2113 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2114 (float)i_total, NULL );
2115 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2116 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2121 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
2123 stream_sys_t *p_sys = s->p_sys;
2124 access_t *p_access = p_sys->p_access;
2125 input_thread_t *p_input = NULL;
2130 if( s->p_parent && s->p_parent->p_parent &&
2131 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
2132 p_input = (input_thread_t *)s->p_parent->p_parent;
2134 if( !p_sys->i_list )
2136 p_block = p_access->pf_block( p_access );
2137 if( p_access->b_die )
2138 vlc_object_kill( s );
2139 if( pb_eof ) *pb_eof = p_access->info.b_eof;
2140 if( p_input && p_block && libvlc_stats (p_access) )
2142 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2143 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
2144 p_block->i_buffer, &i_total );
2145 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2146 (float)i_total, NULL );
2147 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2148 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2153 p_block = p_sys->p_list_access->pf_block( p_access );
2154 if( p_access->b_die )
2155 vlc_object_kill( s );
2156 b_eof = p_sys->p_list_access->info.b_eof;
2157 if( pb_eof ) *pb_eof = b_eof;
2159 /* If we reached an EOF then switch to the next stream in the list */
2160 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
2162 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
2163 access_t *p_list_access;
2165 msg_Dbg( s, "opening input `%s'", psz_name );
2167 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
2169 if( !p_list_access ) return 0;
2171 if( p_sys->p_list_access != p_access )
2172 access_Delete( p_sys->p_list_access );
2174 p_sys->p_list_access = p_list_access;
2176 /* We have to read some data */
2177 return AReadBlock( s, pb_eof );
2183 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2184 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
2185 p_block->i_buffer, &i_total );
2186 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2187 (float)i_total, NULL );
2188 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
2190 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2196 static int ASeek( stream_t *s, int64_t i_pos )
2198 stream_sys_t *p_sys = s->p_sys;
2199 access_t *p_access = p_sys->p_access;
2201 /* Check which stream we need to access */
2207 access_t *p_list_access = 0;
2209 for( i = 0; i < p_sys->i_list - 1; i++ )
2211 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
2212 i_size += p_sys->list[i]->i_size;
2214 psz_name = p_sys->list[i]->psz_path;
2216 if( i != p_sys->i_list_index )
2217 msg_Dbg( s, "opening input `%s'", psz_name );
2219 if( i != p_sys->i_list_index && i != 0 )
2222 access_New( s, p_access->psz_access, "", psz_name );
2224 else if( i != p_sys->i_list_index )
2226 p_list_access = p_access;
2231 if( p_sys->p_list_access != p_access )
2232 access_Delete( p_sys->p_list_access );
2234 p_sys->p_list_access = p_list_access;
2237 p_sys->i_list_index = i;
2238 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
2242 return p_access->pf_seek( p_access, i_pos );
2247 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
2248 * "p_read" is NULL then data are skipped instead of read. The return
2249 * value is the real numbers of bytes read/skip. If this value is less
2250 * than i_read that means that it's the end of the stream.
2252 int stream_Read( stream_t *s, void *p_read, int i_read )
2254 return s->pf_read( s, p_read, i_read );
2258 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
2259 * \return The real numbers of valid bytes, if it's less
2260 * or equal to 0, *pp_peek is invalid.
2261 * \note pp_peek is a pointer to internal buffer and it will be invalid as
2262 * soons as other stream_* functions are called.
2263 * \note Due to input limitation, it could be less than i_peek without meaning
2264 * the end of the stream (but only when you have i_peek >=
2265 * p_input->i_bufsize)
2267 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
2269 return s->pf_peek( s, pp_peek, i_peek );
2273 * Use to control the "stream_t *". Look at #stream_query_e for
2274 * possible "i_query" value and format arguments. Return VLC_SUCCESS
2275 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
2277 int stream_vaControl( stream_t *s, int i_query, va_list args )
2279 return s->pf_control( s, i_query, args );
2285 void stream_Delete( stream_t *s )
2290 int stream_Control( stream_t *s, int i_query, ... )
2296 return VLC_EGENERIC;
2298 va_start( args, i_query );
2299 i_result = s->pf_control( s, i_query, args );
2305 * Read "i_size" bytes and store them in a block_t.
2306 * It always read i_size bytes unless you are at the end of the stream
2307 * where it return what is available.
2309 block_t *stream_Block( stream_t *s, int i_size )
2311 if( i_size <= 0 ) return NULL;
2313 /* emulate block read */
2314 block_t *p_bk = block_New( s, i_size );
2317 int i_read = stream_Read( s, p_bk->p_buffer, i_size );
2320 p_bk->i_buffer = i_read;
2323 block_Release( p_bk );