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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_strings.h>
34 #include <vlc_charset.h>
41 #include "input_internal.h"
46 * - tune the 2 methods (block/stream)
47 * - compute cost for seek
48 * - improve stream mode seeking with closest segments
50 * - Maybe remove (block/stream) in favour of immediate
55 * One linked list of data read
57 * More complex scheme using mutliple track to avoid seeking
58 * - using directly the access (only indirection for peeking).
59 * This method is known to introduce much less latency.
60 * It should probably defaulted (instead of the stream method (2)).
63 /* How many tracks we have, currently only used for stream mode */
64 #ifdef OPTIMIZE_MEMORY
65 # define STREAM_CACHE_TRACK 1
66 /* Max size of our cache 128Ko per track */
67 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
69 # define STREAM_CACHE_TRACK 3
70 /* Max size of our cache 4Mo per track */
71 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
74 /* How many data we try to prebuffer */
75 #define STREAM_CACHE_PREBUFFER_SIZE (32767)
76 /* Maximum time we take to pre-buffer */
77 #define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
79 /* Method1: Simple, for pf_block.
80 * We get blocks and put them in the linked list.
81 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
84 /* Method2: A bit more complex, for pf_read
85 * - We use ring buffers, only one if unseekable, all if seekable
86 * - Upon seek date current ring, then search if one ring match the pos,
87 * yes: switch to it, seek the access to match the end of the ring
88 * no: search the ring with i_end the closer to i_pos,
89 * if close enough, read data and use this ring
90 * else use the oldest ring, seek and use it.
92 * TODO: - with access non seekable: use all space available for only one ring, but
93 * we have to support seekable/non-seekable switch on the fly.
94 * - compute a good value for i_read_size
97 #define STREAM_READ_ATONCE 32767
98 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
118 typedef enum stream_read_method_t
120 STREAM_METHOD_IMMEDIATE,
123 } stream_read_method_t;
129 stream_read_method_t method; /* method to use */
131 int64_t i_pos; /* Current reading offset */
133 /* Method 1: pf_block */
136 int64_t i_start; /* Offset of block for p_first */
137 int64_t i_offset; /* Offset for data in p_current */
138 block_t *p_current; /* Current block */
140 int i_size; /* Total amount of data in the list */
146 /* Method 2: for pf_read */
149 int i_offset; /* Buffer offset in the current track */
150 int i_tk; /* Current track */
151 stream_track_t tk[STREAM_CACHE_TRACK];
157 int i_used; /* Used since last read */
162 /* Method 3: for pf_read */
169 /* Peek temporary buffer */
173 /* Stat for both method */
176 bool b_fastseek; /* From access */
178 /* Stat about reading data */
179 int64_t i_read_count;
183 /* Stat about seek */
191 access_entry_t **list;
193 access_t *p_list_access;
195 /* Preparse mode ? */
203 FILE *f; /* TODO it could be replaced by access_output_t one day */
209 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read );
210 static int AStreamPeekBlock( stream_t *s, const uint8_t **p_peek, unsigned int i_read );
211 static int AStreamSeekBlock( stream_t *s, int64_t i_pos );
212 static void AStreamPrebufferBlock( stream_t *s );
213 static block_t *AReadBlock( stream_t *s, bool *pb_eof );
216 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read );
217 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read );
218 static int AStreamSeekStream( stream_t *s, int64_t i_pos );
219 static void AStreamPrebufferStream( stream_t *s );
220 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read );
223 static int AStreamReadImmediate( stream_t *s, void *p_read, unsigned int i_read );
224 static int AStreamPeekImmediate( stream_t *s, const uint8_t **pp_peek, unsigned int i_read );
225 static int AStreamSeekImmediate( stream_t *s, int64_t i_pos );
228 static int AStreamControl( stream_t *s, int i_query, va_list );
229 static void AStreamDestroy( stream_t *s );
230 static void UStreamDestroy( stream_t *s );
231 static int ASeek( stream_t *s, int64_t i_pos );
232 static int ARecordSetState( stream_t *s, bool b_record, const char *psz_extension );
233 static void ARecordWrite( stream_t *s, const uint8_t *p_buffer, size_t i_buffer );
235 /****************************************************************************
237 ****************************************************************************/
239 static inline int64_t stream_buffered_size( stream_t *s )
241 return s->p_sys->immediate.i_end;
244 static inline void stream_buffer_empty( stream_t *s, int length )
246 length = __MAX( stream_buffered_size( s ), length );
249 memmove( s->p_sys->immediate.p_buffer,
250 s->p_sys->immediate.p_buffer + length,
251 stream_buffered_size( s ) - length );
253 s->p_sys->immediate.i_end -= length;
256 static inline void stream_buffer_fill( stream_t *s, int length )
258 s->p_sys->immediate.i_end += length;
261 static inline uint8_t * stream_buffer( stream_t *s )
263 return s->p_sys->immediate.p_buffer;
266 /****************************************************************************
267 * stream_CommonNew: create an empty stream structure
268 ****************************************************************************/
269 stream_t *stream_CommonNew( vlc_object_t *p_obj )
271 return (stream_t *)vlc_custom_create( p_obj, sizeof(stream_t),
272 VLC_OBJECT_GENERIC, "stream" );
275 /****************************************************************************
276 * stream_UrlNew: create a stream from a access
277 ****************************************************************************/
278 stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
280 const char *psz_access, *psz_demux;
288 char psz_dup[strlen( psz_url ) + 1];
289 strcpy( psz_dup, psz_url );
290 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
292 /* Now try a real access */
293 p_access = access_New( p_parent, psz_access, psz_demux, psz_path );
295 if( p_access == NULL )
297 msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
301 if( !( p_res = stream_AccessNew( p_access, true ) ) )
303 access_Delete( p_access );
307 p_res->pf_destroy = UStreamDestroy;
311 stream_t *stream_AccessNew( access_t *p_access, bool b_quick )
313 stream_t *s = stream_CommonNew( VLC_OBJECT(p_access) );
315 char *psz_list = NULL;
320 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
323 vlc_object_release( s );
327 /* Attach it now, needed for b_die */
328 vlc_object_attach( s, p_access );
330 s->pf_read = NULL; /* Set up later */
332 s->pf_control = AStreamControl;
333 s->pf_destroy = AStreamDestroy;
335 /* UTF16 and UTF32 text file conversion */
337 s->b_little_endian = false;
338 s->conv = (vlc_iconv_t)(-1);
341 p_sys->p_access = p_access;
342 if( p_access->pf_block )
343 p_sys->method = STREAM_METHOD_BLOCK;
344 else if( var_CreateGetBool( s, "use-stream-immediate" ) )
345 p_sys->method = STREAM_METHOD_IMMEDIATE;
347 p_sys->method = STREAM_METHOD_STREAM;
349 p_sys->record.b_active = false;
351 p_sys->i_pos = p_access->info.i_pos;
354 access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
355 p_sys->stat.i_bytes = 0;
356 p_sys->stat.i_read_time = 0;
357 p_sys->stat.i_read_count = 0;
358 p_sys->stat.i_seek_count = 0;
359 p_sys->stat.i_seek_time = 0;
363 p_sys->i_list_index = 0;
364 p_sys->p_list_access = 0;
366 p_sys->b_quick = b_quick;
368 /* Get the additional list of inputs if any (for concatenation) */
369 if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
371 access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
372 if( p_entry == NULL )
374 char *psz_name, *psz_parser = psz_name = psz_list;
376 p_sys->p_list_access = p_access;
377 p_entry->i_size = p_access->info.i_size;
378 p_entry->psz_path = strdup( p_access->psz_path );
379 if( p_entry->psz_path == NULL )
384 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
385 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
386 p_entry->psz_path, p_access->info.i_size );
388 while( psz_name && *psz_name )
390 psz_parser = strchr( psz_name, ',' );
391 if( psz_parser ) *psz_parser = 0;
393 psz_name = strdup( psz_name );
396 access_t *p_tmp = access_New( p_access, p_access->psz_access,
401 psz_name = psz_parser;
402 if( psz_name ) psz_name++;
406 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
407 psz_name, p_tmp->info.i_size );
409 p_entry = malloc( sizeof(access_entry_t) );
410 if( p_entry == NULL )
412 p_entry->i_size = p_tmp->info.i_size;
413 p_entry->psz_path = psz_name;
414 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
416 access_Delete( p_tmp );
419 psz_name = psz_parser;
420 if( psz_name ) psz_name++;
423 FREENULL( psz_list );
427 p_sys->p_peek = NULL;
429 if( p_sys->method == STREAM_METHOD_BLOCK )
431 msg_Dbg( s, "Using AStream*Block" );
432 s->pf_read = AStreamReadBlock;
433 s->pf_peek = AStreamPeekBlock;
435 /* Init all fields of p_sys->block */
436 p_sys->block.i_start = p_sys->i_pos;
437 p_sys->block.i_offset = 0;
438 p_sys->block.p_current = NULL;
439 p_sys->block.i_size = 0;
440 p_sys->block.p_first = NULL;
441 p_sys->block.pp_last = &p_sys->block.p_first;
443 /* Do the prebuffering */
444 AStreamPrebufferBlock( s );
446 if( p_sys->block.i_size <= 0 )
448 msg_Err( s, "cannot pre fill buffer" );
452 else if( p_sys->method == STREAM_METHOD_IMMEDIATE )
454 msg_Dbg( s, "Using AStream*Immediate" );
456 s->pf_read = AStreamReadImmediate;
457 s->pf_peek = AStreamPeekImmediate;
459 /* Allocate/Setup our tracks (useful to peek)*/
460 p_sys->immediate.i_end = 0;
461 p_sys->immediate.p_buffer = malloc( STREAM_CACHE_SIZE );
463 msg_Dbg( s, "p_buffer %p-%p",
464 p_sys->immediate.p_buffer,
465 &p_sys->immediate.p_buffer[STREAM_CACHE_SIZE] );
467 if( p_sys->immediate.p_buffer == NULL )
469 msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
478 assert( p_sys->method == STREAM_METHOD_STREAM );
480 msg_Dbg( s, "Using AStream*Stream" );
482 s->pf_read = AStreamReadStream;
483 s->pf_peek = AStreamPeekStream;
485 /* Allocate/Setup our tracks */
486 p_sys->stream.i_offset = 0;
487 p_sys->stream.i_tk = 0;
488 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
489 if( p_sys->stream.p_buffer == NULL )
491 msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
495 p_sys->stream.i_used = 0;
496 access_Control( p_access, ACCESS_GET_MTU,
497 &p_sys->stream.i_read_size );
498 if( p_sys->stream.i_read_size <= 0 )
499 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
500 else if( p_sys->stream.i_read_size <= 256 )
501 p_sys->stream.i_read_size = 256;
503 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
505 p_sys->stream.tk[i].i_date = 0;
506 p_sys->stream.tk[i].i_start = p_sys->i_pos;
507 p_sys->stream.tk[i].i_end = p_sys->i_pos;
508 p_sys->stream.tk[i].p_buffer=
509 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
512 /* Do the prebuffering */
513 AStreamPrebufferStream( s );
515 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
517 msg_Err( s, "cannot pre fill buffer" );
525 if( p_sys->method == STREAM_METHOD_BLOCK )
531 free( p_sys->stream.p_buffer );
533 while( p_sys->i_list > 0 )
534 free( p_sys->list[--(p_sys->i_list)] );
538 vlc_object_detach( s );
539 vlc_object_release( s );
543 /****************************************************************************
545 ****************************************************************************/
546 static void AStreamDestroy( stream_t *s )
548 stream_sys_t *p_sys = s->p_sys;
550 vlc_object_detach( s );
552 if( p_sys->record.b_active )
553 ARecordSetState( s, false, NULL );
555 if( p_sys->method == STREAM_METHOD_BLOCK )
556 block_ChainRelease( p_sys->block.p_first );
557 else if( p_sys->method == STREAM_METHOD_IMMEDIATE )
558 free( p_sys->immediate.p_buffer );
560 free( p_sys->stream.p_buffer );
562 free( p_sys->p_peek );
564 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
565 access_Delete( p_sys->p_list_access );
567 while( p_sys->i_list-- )
569 free( p_sys->list[p_sys->i_list]->psz_path );
570 free( p_sys->list[p_sys->i_list] );
576 vlc_object_release( s );
579 static void UStreamDestroy( stream_t *s )
581 access_t *p_access = (access_t *)s->p_parent;
583 access_Delete( p_access );
586 /****************************************************************************
587 * stream_AccessReset:
588 ****************************************************************************/
589 void stream_AccessReset( stream_t *s )
591 stream_sys_t *p_sys = s->p_sys;
593 p_sys->i_pos = p_sys->p_access->info.i_pos;
595 if( p_sys->method == STREAM_METHOD_BLOCK )
597 block_ChainRelease( p_sys->block.p_first );
599 /* Init all fields of p_sys->block */
600 p_sys->block.i_start = p_sys->i_pos;
601 p_sys->block.i_offset = 0;
602 p_sys->block.p_current = NULL;
603 p_sys->block.i_size = 0;
604 p_sys->block.p_first = NULL;
605 p_sys->block.pp_last = &p_sys->block.p_first;
607 /* Do the prebuffering */
608 AStreamPrebufferBlock( s );
610 else if( p_sys->method == STREAM_METHOD_IMMEDIATE )
612 stream_buffer_empty( s, stream_buffered_size( s ) );
618 assert( p_sys->method == STREAM_METHOD_STREAM );
620 /* Setup our tracks */
621 p_sys->stream.i_offset = 0;
622 p_sys->stream.i_tk = 0;
623 p_sys->stream.i_used = 0;
625 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
627 p_sys->stream.tk[i].i_date = 0;
628 p_sys->stream.tk[i].i_start = p_sys->i_pos;
629 p_sys->stream.tk[i].i_end = p_sys->i_pos;
632 /* Do the prebuffering */
633 AStreamPrebufferStream( s );
637 /****************************************************************************
638 * stream_AccessUpdate:
639 ****************************************************************************/
640 void stream_AccessUpdate( stream_t *s )
642 stream_sys_t *p_sys = s->p_sys;
644 p_sys->i_pos = p_sys->p_access->info.i_pos;
649 for( i = 0; i < p_sys->i_list_index; i++ )
651 p_sys->i_pos += p_sys->list[i]->i_size;
656 /****************************************************************************
658 ****************************************************************************/
659 static int AStreamControl( stream_t *s, int i_query, va_list args )
661 stream_sys_t *p_sys = s->p_sys;
662 access_t *p_access = p_sys->p_access;
666 const char *psz_string;
667 int64_t *pi_64, i_64;
672 case STREAM_GET_SIZE:
673 pi_64 = (int64_t*)va_arg( args, int64_t * );
674 if( s->p_sys->i_list )
678 for( i = 0; i < s->p_sys->i_list; i++ )
679 *pi_64 += s->p_sys->list[i]->i_size;
682 *pi_64 = p_access->info.i_size;
685 case STREAM_CAN_SEEK:
686 p_bool = (bool*)va_arg( args, bool * );
687 access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
690 case STREAM_CAN_FASTSEEK:
691 p_bool = (bool*)va_arg( args, bool * );
692 access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
695 case STREAM_GET_POSITION:
696 pi_64 = (int64_t*)va_arg( args, int64_t * );
697 *pi_64 = p_sys->i_pos;
700 case STREAM_SET_POSITION:
701 i_64 = (int64_t)va_arg( args, int64_t );
702 switch( p_sys->method )
704 case STREAM_METHOD_BLOCK:
705 return AStreamSeekBlock( s, i_64 );
706 case STREAM_METHOD_IMMEDIATE:
707 return AStreamSeekImmediate( s, i_64 );
708 case STREAM_METHOD_STREAM:
709 return AStreamSeekStream( s, i_64 );
718 case STREAM_CONTROL_ACCESS:
719 i_int = (int) va_arg( args, int );
720 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
721 i_int != ACCESS_SET_PRIVATE_ID_CA &&
722 i_int != ACCESS_GET_PRIVATE_ID_STATE )
724 msg_Err( s, "Hey, what are you thinking ?"
725 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
728 return access_vaControl( p_access, i_int, args );
730 case STREAM_GET_CONTENT_TYPE:
731 return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
732 va_arg( args, char ** ) );
733 case STREAM_SET_RECORD_STATE:
734 b_bool = (bool)va_arg( args, int );
737 psz_string = (const char*)va_arg( args, const char* );
738 return ARecordSetState( s, b_bool, psz_string );
741 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
747 /****************************************************************************
748 * ARecord*: record stream functions
749 ****************************************************************************/
750 static int ARecordStart( stream_t *s, const char *psz_extension )
752 stream_sys_t *p_sys = s->p_sys;
759 psz_extension = "dat";
762 char *psz_path = var_CreateGetString( s, "input-record-path" );
763 if( !psz_path || *psz_path == '\0' )
766 psz_path = strdup( config_GetHomeDir() );
773 * TODO allow prefix configuration */
774 psz_file = input_CreateFilename( VLC_OBJECT(s), psz_path, INPUT_RECORD_PREFIX, psz_extension );
781 f = utf8_fopen( psz_file, "wb" );
787 msg_Dbg( s, "Recording into %s", psz_file );
792 p_sys->record.b_active = true;
793 p_sys->record.b_error = false;
796 static int ARecordStop( stream_t *s )
798 stream_sys_t *p_sys = s->p_sys;
800 assert( p_sys->record.b_active );
802 msg_Dbg( s, "Recording completed" );
803 fclose( p_sys->record.f );
804 p_sys->record.b_active = false;
808 static int ARecordSetState( stream_t *s, bool b_record, const char *psz_extension )
810 stream_sys_t *p_sys = s->p_sys;
812 if( !!p_sys->record.b_active == !!b_record )
816 return ARecordStart( s, psz_extension );
818 return ARecordStop( s );
820 static void ARecordWrite( stream_t *s, const uint8_t *p_buffer, size_t i_buffer )
822 stream_sys_t *p_sys = s->p_sys;
824 assert( p_sys->record.b_active );
828 const bool b_previous_error = p_sys->record.b_error;
829 const size_t i_written = fwrite( p_buffer, 1, i_buffer, p_sys->record.f );
831 p_sys->record.b_error = i_written != i_buffer;
833 /* TODO maybe a intf_UserError or something like that ? */
834 if( p_sys->record.b_error && !b_previous_error )
835 msg_Err( s, "Failed to record data (begin)" );
836 else if( !p_sys->record.b_error && b_previous_error )
837 msg_Err( s, "Failed to record data (end)" );
841 /****************************************************************************
843 ****************************************************************************/
844 static void AStreamPrebufferBlock( stream_t *s )
846 stream_sys_t *p_sys = s->p_sys;
847 access_t *p_access = p_sys->p_access;
852 msg_Dbg( s, "pre buffering" );
856 int64_t i_date = mdate();
860 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
861 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
866 p_sys->stat.i_bytes = p_sys->block.i_size;
867 p_sys->stat.i_read_time = i_date - i_start;
868 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
869 (p_sys->stat.i_read_time + 1);
871 msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
872 "%"PRId64" kbytes/s",
874 p_sys->stat.i_read_time / INT64_C(1000000),
880 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
889 /* Append the block */
890 p_sys->block.i_size += b->i_buffer;
891 *p_sys->block.pp_last = b;
892 p_sys->block.pp_last = &b->p_next;
894 p_sys->stat.i_read_count++;
898 if( p_access->info.b_prebuffered )
900 /* Access has already prebufferred - update stats and exit */
901 p_sys->stat.i_bytes = p_sys->block.i_size;
902 p_sys->stat.i_read_time = mdate() - i_start;
909 msg_Dbg( s, "received first data for our buffer");
914 p_sys->block.p_current = p_sys->block.p_first;
917 static int AStreamRefillBlock( stream_t *s );
919 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
921 stream_sys_t *p_sys = s->p_sys;
923 uint8_t *p_data = p_read;
924 uint8_t *p_record = p_data;
925 unsigned int i_data = 0;
928 if( p_sys->block.p_current == NULL )
931 if( p_sys->record.b_active && !p_data )
932 p_record = p_data = malloc( i_read );
936 /* seek within this stream if possible, else use plain old read and discard */
937 stream_sys_t *p_sys = s->p_sys;
938 access_t *p_access = p_sys->p_access;
940 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
942 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
945 while( i_data < i_read )
948 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
949 unsigned int i_copy = __MIN( (unsigned int)__MAX(i_current,0), i_read - i_data);
955 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
961 p_sys->block.i_offset += i_copy;
962 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
964 /* Current block is now empty, switch to next */
965 if( p_sys->block.p_current )
967 p_sys->block.i_offset = 0;
968 p_sys->block.p_current = p_sys->block.p_current->p_next;
970 /*Get a new block if needed */
971 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
978 if( p_sys->record.b_active )
980 if( i_data > 0 && p_record != NULL)
981 ARecordWrite( s, p_record, i_data );
986 p_sys->i_pos += i_data;
990 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
992 stream_sys_t *p_sys = s->p_sys;
994 unsigned int i_data = 0;
996 unsigned int i_offset;
998 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
1000 /* We can directly give a pointer over our buffer */
1001 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
1003 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
1007 /* We need to create a local copy */
1008 if( p_sys->i_peek < i_read )
1010 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1011 if( !p_sys->p_peek )
1016 p_sys->i_peek = i_read;
1019 /* Fill enough data */
1020 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
1023 block_t **pp_last = p_sys->block.pp_last;
1025 if( AStreamRefillBlock( s ) ) break;
1027 /* Our buffer are probably filled enough, don't try anymore */
1028 if( pp_last == p_sys->block.pp_last ) break;
1031 /* Copy what we have */
1032 b = p_sys->block.p_current;
1033 i_offset = p_sys->block.i_offset;
1034 p_data = p_sys->p_peek;
1036 while( b && i_data < i_read )
1038 unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
1039 int i_copy = __MIN( i_current, i_read - i_data );
1041 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
1046 if( i_offset >= b->i_buffer )
1053 *pp_peek = p_sys->p_peek;
1057 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
1059 stream_sys_t *p_sys = s->p_sys;
1060 access_t *p_access = p_sys->p_access;
1061 int64_t i_offset = i_pos - p_sys->block.i_start;
1064 /* We already have thoses data, just update p_current/i_offset */
1065 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
1067 block_t *b = p_sys->block.p_first;
1070 while( i_current + b->i_buffer < i_offset )
1072 i_current += b->i_buffer;
1076 p_sys->block.p_current = b;
1077 p_sys->block.i_offset = i_offset - i_current;
1079 p_sys->i_pos = i_pos;
1084 /* We may need to seek or to read data */
1088 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1092 msg_Err( s, "backward seeking impossible (access not seekable)" );
1093 return VLC_EGENERIC;
1100 bool b_aseek, b_aseekfast;
1102 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1103 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
1108 msg_Warn( s, "%"PRId64" bytes need to be skipped "
1109 "(access non seekable)",
1110 i_offset - p_sys->block.i_size );
1114 int64_t i_skip = i_offset - p_sys->block.i_size;
1116 /* Avg bytes per packets */
1117 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
1118 /* TODO compute a seek cost instead of fixed threshold */
1119 int i_th = b_aseekfast ? 1 : 5;
1121 if( i_skip <= i_th * i_avg &&
1122 i_skip < STREAM_CACHE_SIZE )
1127 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
1128 b_seek, i_th*i_avg, i_skip );
1134 int64_t i_start, i_end;
1135 /* Do the access seek */
1137 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1141 block_ChainRelease( p_sys->block.p_first );
1144 p_sys->block.i_start = p_sys->i_pos = i_pos;
1145 p_sys->block.i_offset = 0;
1146 p_sys->block.p_current = NULL;
1147 p_sys->block.i_size = 0;
1148 p_sys->block.p_first = NULL;
1149 p_sys->block.pp_last = &p_sys->block.p_first;
1151 /* Refill a block */
1152 if( AStreamRefillBlock( s ) )
1153 return VLC_EGENERIC;
1156 p_sys->stat.i_seek_time += i_end - i_start;
1157 p_sys->stat.i_seek_count++;
1164 /* Read and skip enough data */
1165 if( AStreamRefillBlock( s ) )
1166 return VLC_EGENERIC;
1168 while( p_sys->block.p_current &&
1169 p_sys->i_pos + p_sys->block.p_current->i_buffer - p_sys->block.i_offset < i_pos )
1171 p_sys->i_pos += p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
1172 p_sys->block.p_current = p_sys->block.p_current->p_next;
1173 p_sys->block.i_offset = 0;
1176 while( p_sys->block.i_start + p_sys->block.i_size < i_pos );
1178 p_sys->block.i_offset = i_pos - p_sys->i_pos;
1179 p_sys->i_pos = i_pos;
1184 return VLC_EGENERIC;
1187 static int AStreamRefillBlock( stream_t *s )
1189 stream_sys_t *p_sys = s->p_sys;
1190 int64_t i_start, i_stop;
1194 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1195 p_sys->block.p_first != p_sys->block.p_current )
1197 block_t *b = p_sys->block.p_first;
1199 p_sys->block.i_start += b->i_buffer;
1200 p_sys->block.i_size -= b->i_buffer;
1201 p_sys->block.p_first = b->p_next;
1205 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1206 p_sys->block.p_current == p_sys->block.p_first &&
1207 p_sys->block.p_current->p_next ) /* At least 2 packets */
1209 /* Enough data, don't read more */
1213 /* Now read a new block */
1220 return VLC_EGENERIC;
1223 if( ( b = AReadBlock( s, &b_eof ) ) )
1226 return VLC_EGENERIC;
1233 /* Append the block */
1234 p_sys->block.i_size += b->i_buffer;
1235 *p_sys->block.pp_last = b;
1236 p_sys->block.pp_last = &b->p_next;
1239 if( p_sys->block.p_current == NULL )
1240 p_sys->block.p_current = b;
1243 p_sys->stat.i_bytes += b->i_buffer;
1244 p_sys->stat.i_read_time += i_stop - i_start;
1245 p_sys->stat.i_read_count++;
1254 /****************************************************************************
1256 ****************************************************************************/
1257 static int AStreamRefillStream( stream_t *s );
1259 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read )
1261 stream_sys_t *p_sys = s->p_sys;
1262 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1264 uint8_t *p_data = (uint8_t *)p_read;
1265 uint8_t *p_record = p_data;
1266 unsigned int i_data = 0;
1268 if( tk->i_start >= tk->i_end )
1271 if( p_sys->record.b_active && !p_data )
1272 p_record = p_data = malloc( i_read );
1274 if( p_data == NULL )
1276 /* seek within this stream if possible, else use plain old read and discard */
1277 stream_sys_t *p_sys = s->p_sys;
1278 access_t *p_access = p_sys->p_access;
1280 /* seeking after EOF is not what we want */
1281 if( !( p_access->info.b_eof ) )
1284 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1286 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1291 msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1292 " offset=%d end=%"PRId64,
1293 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1294 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1297 while( i_data < i_read )
1299 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1300 STREAM_CACHE_TRACK_SIZE;
1301 unsigned int i_current =
1302 __MAX(0,__MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1303 STREAM_CACHE_TRACK_SIZE - i_off ));
1304 int i_copy = __MIN( i_current, i_read - i_data );
1306 if( i_copy <= 0 ) break; /* EOF */
1309 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1312 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1316 p_sys->stream.i_offset += i_copy;
1318 /* Update pos now */
1319 p_sys->i_pos += i_copy;
1322 p_sys->stream.i_used += i_copy;
1323 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1324 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1326 if( AStreamRefillStream( s ) )
1329 if( tk->i_start >= tk->i_end ) break;
1334 if( p_sys->record.b_active )
1336 if( i_data > 0 && p_record != NULL)
1337 ARecordWrite( s, p_record, i_data );
1345 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1347 stream_sys_t *p_sys = s->p_sys;
1348 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1351 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1354 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1355 "start=%"PRId64" offset=%d end=%"PRId64,
1356 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1357 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1360 /* Avoid problem, but that should *never* happen */
1361 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1362 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1364 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1366 if( p_sys->stream.i_used <= 1 )
1368 /* Be sure we will read something */
1369 p_sys->stream.i_used += i_read -
1370 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1372 if( AStreamRefillStream( s ) ) break;
1375 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1376 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1378 /* Now, direct pointer or a copy ? */
1379 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1380 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1382 *pp_peek = &tk->p_buffer[i_off];
1386 if( p_sys->i_peek < i_read )
1388 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1389 if( !p_sys->p_peek )
1394 p_sys->i_peek = i_read;
1397 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1398 STREAM_CACHE_TRACK_SIZE - i_off );
1399 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1400 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1402 *pp_peek = p_sys->p_peek;
1406 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1408 stream_sys_t *p_sys = s->p_sys;
1409 access_t *p_access = p_sys->p_access;
1417 msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1418 " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1419 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1420 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1421 p_sys->stream.i_offset,
1422 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1426 /* Seek in our current track ? */
1427 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1428 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1430 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1432 msg_Dbg( s, "AStreamSeekStream: current track" );
1434 p_sys->i_pos = i_pos;
1435 p_sys->stream.i_offset = i_pos - tk->i_start;
1437 /* If there is not enough data left in the track, refill */
1438 /* \todo How to get a correct value for
1439 * - refilling threshold
1440 * - how much to refill
1442 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1443 p_sys->stream.i_read_size )
1445 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1447 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1448 AStreamRefillStream( s );
1454 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1457 /* We can't do nothing */
1458 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1459 return VLC_EGENERIC;
1462 /* Date the current track */
1463 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1465 /* Try to reuse already read data */
1466 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1468 stream_track_t *tk = &p_sys->stream.tk[i];
1470 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1473 msg_Dbg( s, "AStreamSeekStream: reusing %d start=%"PRId64
1474 " end=%"PRId64, i, tk->i_start, tk->i_end );
1477 /* Seek at the end of the buffer */
1478 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1481 p_sys->i_pos = i_pos;
1482 p_sys->stream.i_tk = i;
1483 p_sys->stream.i_offset = i_pos - tk->i_start;
1485 if( p_sys->stream.i_used < 1024 )
1486 p_sys->stream.i_used = 1024;
1488 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1489 return VLC_EGENERIC;
1495 access_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1496 /* FIXME compute seek cost (instead of static 'stupid' value) */
1497 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1503 /* Search closest segment TODO */
1504 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1506 stream_track_t *tk = &p_sys->stream.tk[i];
1508 if( i_pos + i_maxth >= tk->i_start )
1510 msg_Dbg( s, "good segment before current pos, TODO" );
1512 if( i_pos - i_maxth <= tk->i_end )
1514 msg_Dbg( s, "good segment after current pos, TODO" );
1519 /* Nothing good, seek and choose oldest segment */
1520 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1521 p_sys->i_pos = i_pos;
1524 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1526 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1530 /* Reset the segment */
1531 p_sys->stream.i_tk = i_new;
1532 p_sys->stream.i_offset = 0;
1533 p_sys->stream.tk[i_new].i_start = i_pos;
1534 p_sys->stream.tk[i_new].i_end = i_pos;
1537 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1538 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1540 if( AStreamRefillStream( s ) )
1541 return VLC_EGENERIC;
1546 static int AStreamRefillStream( stream_t *s )
1548 stream_sys_t *p_sys = s->p_sys;
1549 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1551 /* We read but won't increase i_start after initial start + offset */
1553 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1554 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1555 bool b_read = false;
1556 int64_t i_start, i_stop;
1558 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1561 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1562 p_sys->stream.i_used, i_toread );
1566 while( i_toread > 0 )
1568 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1572 return VLC_EGENERIC;
1574 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1575 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1577 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1582 else if( i_read == 0 )
1585 return VLC_EGENERIC;
1591 tk->i_end += i_read;
1593 /* Windows of STREAM_CACHE_TRACK_SIZE */
1594 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1596 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1598 tk->i_start += i_invalid;
1599 p_sys->stream.i_offset -= i_invalid;
1603 p_sys->stream.i_used -= i_read;
1605 p_sys->stat.i_bytes += i_read;
1606 p_sys->stat.i_read_count++;
1610 p_sys->stat.i_read_time += i_stop - i_start;
1615 static void AStreamPrebufferStream( stream_t *s )
1617 stream_sys_t *p_sys = s->p_sys;
1618 access_t *p_access = p_sys->p_access;
1620 int64_t i_first = 0;
1622 int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1623 ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1624 STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1626 msg_Dbg( s, "pre-buffering..." );
1630 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1632 int64_t i_date = mdate();
1635 if( s->b_die || tk->i_end >= i_prebuffer ||
1636 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1641 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1642 p_sys->stat.i_read_time = i_date - i_start;
1643 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1644 (p_sys->stat.i_read_time+1);
1646 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1647 "%"PRId64" kbytes/s",
1648 p_sys->stat.i_bytes,
1649 p_sys->stat.i_read_time / INT64_C(1000000),
1650 i_byterate / 1024 );
1655 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1656 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1657 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1660 else if( i_read == 0 )
1666 msg_Dbg( s, "received first data for our buffer");
1669 tk->i_end += i_read;
1671 p_sys->stat.i_read_count++;
1675 /****************************************************************************
1677 ****************************************************************************/
1679 static int AStreamReadImmediate( stream_t *s, void *p_read, unsigned int i_read )
1681 stream_sys_t *p_sys = s->p_sys;
1682 uint8_t *p_data= (uint8_t*)p_read;
1683 uint8_t *p_record = p_data;
1684 unsigned int i_data;
1687 msg_Dbg( s, "AStreamReadImmediate p_read=%p i_read=%d",
1691 if( p_sys->record.b_active && !p_data )
1692 p_record = p_data = malloc( i_read );
1694 /* First, check if we already have some data in the buffer,
1695 * that we could copy directly */
1696 i_data = __MIN( stream_buffered_size( s ), i_read );
1700 msg_Dbg( s, "AStreamReadImmediate: copy %u from %p", i_data, stream_buffer( s ) );
1703 assert( i_data <= STREAM_CACHE_SIZE );
1707 memcpy( p_data, stream_buffer( s ), i_data );
1712 /* Now that we've read our buffer we don't need its i_copy bytes */
1713 stream_buffer_empty( s, i_data );
1715 /* Now check if we have still to really read some data */
1716 while( i_data < i_read )
1718 const unsigned int i_to_read = i_read - i_data;
1723 i_result = AReadStream( s, p_data, i_to_read );
1727 void *p_dummy = malloc( i_to_read );
1732 i_result = AReadStream( s, p_dummy, i_to_read );
1739 p_sys->i_pos += i_data;
1745 if( p_sys->record.b_active )
1747 if( i_data > 0 && p_record != NULL)
1748 ARecordWrite( s, p_record, i_data );
1755 static int AStreamPeekImmediate( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1758 msg_Dbg( s, "AStreamPeekImmediate: %d size=%"PRId64,
1759 i_read, size_buffered_size( s ) );
1762 /* Avoid problem, but that shouldn't happen
1763 * FIXME yes it can */
1764 if( i_read > STREAM_CACHE_SIZE / 2 )
1765 i_read = STREAM_CACHE_SIZE / 2;
1767 int i_to_read = i_read - stream_buffered_size( s );
1771 msg_Dbg( s, "AStreamPeekImmediate: Reading %d",
1774 i_to_read = AReadStream( s, stream_buffer( s ) + stream_buffered_size( s ),
1778 stream_buffer_fill( s, i_to_read );
1781 *pp_peek = stream_buffer( s );
1783 return __MIN( stream_buffered_size( s ), i_read );
1786 static int AStreamSeekImmediate( stream_t *s, int64_t i_pos )
1788 stream_sys_t *p_sys = s->p_sys;
1789 access_t *p_access = p_sys->p_access;
1793 msg_Dbg( s, "AStreamSeekImmediate to %"PRId64" pos=%"PRId64
1794 i_pos, p_sys->i_pos );
1797 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1800 /* We can't do nothing */
1801 msg_Dbg( s, "AStreamSeekImmediate: can't seek" );
1802 return VLC_EGENERIC;
1805 /* Just reset our buffer */
1806 stream_buffer_empty( s, stream_buffered_size( s ) );
1808 if( ASeek( s, i_pos ) )
1809 return VLC_EGENERIC;
1814 /****************************************************************************
1816 ****************************************************************************/
1818 * Read from the stream untill first newline.
1819 * \param s Stream handle to read from
1820 * \return A pointer to the allocated output string. You need to free this when you are done.
1822 #define STREAM_PROBE_LINE 2048
1823 #define STREAM_LINE_MAX (2048*100)
1824 char *stream_ReadLine( stream_t *s )
1826 char *p_line = NULL;
1827 int i_line = 0, i_read = 0;
1829 while( i_read < STREAM_LINE_MAX )
1832 const uint8_t *p_data;
1836 /* Probe new data */
1837 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1838 if( i_data <= 0 ) break; /* No more data */
1841 i_pos = stream_Tell( s );
1842 if( i_pos == 0 && i_data >= 3 )
1845 const char *psz_encoding = NULL;
1847 if( !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
1849 psz_encoding = "UTF-8";
1852 else if( !memcmp( p_data, "\xFF\xFE", 2 ) )
1854 psz_encoding = "UTF-16LE";
1855 s->b_little_endian = true;
1856 s->i_char_width = 2;
1859 else if( !memcmp( p_data, "\xFE\xFF", 2 ) )
1861 psz_encoding = "UTF-16BE";
1862 s->i_char_width = 2;
1866 /* Seek past the BOM */
1869 stream_Seek( s, i_bom_size );
1870 p_data += i_bom_size;
1871 i_data -= i_bom_size;
1874 /* Open the converter if we need it */
1875 if( psz_encoding != NULL )
1877 input_thread_t *p_input;
1878 msg_Dbg( s, "%s BOM detected", psz_encoding );
1879 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1880 if( s->i_char_width > 1 )
1882 s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1883 if( s->conv == (vlc_iconv_t)-1 )
1885 msg_Err( s, "iconv_open failed" );
1888 if( p_input != NULL)
1890 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1891 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1892 vlc_object_release( p_input );
1897 if( i_data % s->i_char_width )
1899 /* keep i_char_width boundary */
1900 i_data = i_data - ( i_data % s->i_char_width );
1901 msg_Warn( s, "the read is not i_char_width compatible");
1907 /* Check if there is an EOL */
1908 if( s->i_char_width == 1 )
1910 /* UTF-8: 0A <LF> */
1911 psz_eol = memchr( p_data, '\n', i_data );
1915 const uint8_t *p = p_data;
1916 const uint8_t *p_last = p + i_data - s->i_char_width;
1918 if( s->i_char_width == 2 )
1920 if( s->b_little_endian == true)
1922 /* UTF-16LE: 0A 00 <LF> */
1923 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1928 /* UTF-16BE: 00 0A <LF> */
1929 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1940 psz_eol = (char *)p + ( s->i_char_width - 1 );
1946 i_data = (psz_eol - (char *)p_data) + 1;
1947 p_line = realloc( p_line, i_line + i_data + s->i_char_width ); /* add \0 */
1950 i_data = stream_Read( s, &p_line[i_line], i_data );
1951 if( i_data <= 0 ) break; /* Hmmm */
1952 i_line += i_data - s->i_char_width; /* skip \n */;
1955 /* We have our line */
1959 /* Read data (+1 for easy \0 append) */
1960 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->i_char_width );
1963 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1964 if( i_data <= 0 ) break; /* Hmmm */
1972 for( j = 0; j < s->i_char_width; j++ )
1974 p_line[i_line + j] = '\0';
1976 i_line += s->i_char_width; /* the added \0 */
1977 if( s->i_char_width > 1 )
1979 size_t i_in = 0, i_out = 0;
1980 const char * p_in = NULL;
1981 char * p_out = NULL;
1982 char * psz_new_line = NULL;
1985 psz_new_line = malloc( i_line );
1986 if( psz_new_line == NULL )
1988 i_in = i_out = (size_t)i_line;
1990 p_out = psz_new_line;
1992 if( vlc_iconv( s->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1994 msg_Err( s, "iconv failed" );
1995 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1998 p_line = psz_new_line;
1999 i_line = (size_t)i_line - i_out; /* does not include \0 */
2002 /* Remove trailing LF/CR */
2003 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
2004 p_line[i_line-2] == '\n') ) i_line--;
2006 /* Make sure the \0 is there */
2007 p_line[i_line-1] = '\0';
2014 /* We failed to read any data, probably EOF */
2016 if( s->conv != (vlc_iconv_t)(-1) ) vlc_iconv_close( s->conv );
2020 /****************************************************************************
2021 * Access reading/seeking wrappers to handle concatenated streams.
2022 ****************************************************************************/
2023 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read )
2025 stream_sys_t *p_sys = s->p_sys;
2026 access_t *p_access = p_sys->p_access;
2027 input_thread_t *p_input = NULL;
2028 int i_read_orig = i_read;
2031 if( s->p_parent && s->p_parent->p_parent &&
2032 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
2033 p_input = (input_thread_t *)s->p_parent->p_parent;
2035 if( !p_sys->i_list )
2037 i_read = p_access->pf_read( p_access, p_read, i_read );
2038 if( p_access->b_die )
2039 vlc_object_kill( s );
2042 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2043 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
2045 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2046 (float)i_total, NULL );
2047 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2048 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2053 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
2055 if( p_access->b_die )
2056 vlc_object_kill( s );
2058 /* If we reached an EOF then switch to the next stream in the list */
2059 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
2061 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
2062 access_t *p_list_access;
2064 msg_Dbg( s, "opening input `%s'", psz_name );
2066 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
2068 if( !p_list_access ) return 0;
2070 if( p_sys->p_list_access != p_access )
2071 access_Delete( p_sys->p_list_access );
2073 p_sys->p_list_access = p_list_access;
2075 /* We have to read some data */
2076 return AReadStream( s, p_read, i_read_orig );
2079 /* Update read bytes in input */
2082 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2083 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
2084 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2085 (float)i_total, NULL );
2086 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2087 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2092 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
2094 stream_sys_t *p_sys = s->p_sys;
2095 access_t *p_access = p_sys->p_access;
2096 input_thread_t *p_input = NULL;
2101 if( s->p_parent && s->p_parent->p_parent &&
2102 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
2103 p_input = (input_thread_t *)s->p_parent->p_parent;
2105 if( !p_sys->i_list )
2107 p_block = p_access->pf_block( p_access );
2108 if( p_access->b_die )
2109 vlc_object_kill( s );
2110 if( pb_eof ) *pb_eof = p_access->info.b_eof;
2111 if( p_input && p_block && libvlc_stats (p_access) )
2113 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2114 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
2115 p_block->i_buffer, &i_total );
2116 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2117 (float)i_total, NULL );
2118 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2119 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2124 p_block = p_sys->p_list_access->pf_block( p_sys->p_list_access );
2125 if( p_access->b_die )
2126 vlc_object_kill( s );
2127 b_eof = p_sys->p_list_access->info.b_eof;
2128 if( pb_eof ) *pb_eof = b_eof;
2130 /* If we reached an EOF then switch to the next stream in the list */
2131 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
2133 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
2134 access_t *p_list_access;
2136 msg_Dbg( s, "opening input `%s'", psz_name );
2138 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
2140 if( !p_list_access ) return 0;
2142 if( p_sys->p_list_access != p_access )
2143 access_Delete( p_sys->p_list_access );
2145 p_sys->p_list_access = p_list_access;
2147 /* We have to read some data */
2148 return AReadBlock( s, pb_eof );
2154 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2155 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
2156 p_block->i_buffer, &i_total );
2157 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2158 (float)i_total, NULL );
2159 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
2161 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2167 static int ASeek( stream_t *s, int64_t i_pos )
2169 stream_sys_t *p_sys = s->p_sys;
2170 access_t *p_access = p_sys->p_access;
2172 /* Check which stream we need to access */
2178 access_t *p_list_access = 0;
2180 for( i = 0; i < p_sys->i_list - 1; i++ )
2182 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
2183 i_size += p_sys->list[i]->i_size;
2185 psz_name = p_sys->list[i]->psz_path;
2187 if( i != p_sys->i_list_index )
2188 msg_Dbg( s, "opening input `%s'", psz_name );
2190 if( i != p_sys->i_list_index && i != 0 )
2193 access_New( s, p_access->psz_access, "", psz_name );
2195 else if( i != p_sys->i_list_index )
2197 p_list_access = p_access;
2202 if( p_sys->p_list_access != p_access )
2203 access_Delete( p_sys->p_list_access );
2205 p_sys->p_list_access = p_list_access;
2208 p_sys->i_list_index = i;
2209 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
2213 return p_access->pf_seek( p_access, i_pos );
2218 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
2219 * "p_read" is NULL then data are skipped instead of read. The return
2220 * value is the real numbers of bytes read/skip. If this value is less
2221 * than i_read that means that it's the end of the stream.
2223 int stream_Read( stream_t *s, void *p_read, int i_read )
2225 return s->pf_read( s, p_read, i_read );
2229 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
2230 * \return The real numbers of valid bytes, if it's less
2231 * or equal to 0, *pp_peek is invalid.
2232 * \note pp_peek is a pointer to internal buffer and it will be invalid as
2233 * soons as other stream_* functions are called.
2234 * \note Due to input limitation, it could be less than i_peek without meaning
2235 * the end of the stream (but only when you have i_peek >=
2236 * p_input->i_bufsize)
2238 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
2240 return s->pf_peek( s, pp_peek, i_peek );
2244 * Use to control the "stream_t *". Look at #stream_query_e for
2245 * possible "i_query" value and format arguments. Return VLC_SUCCESS
2246 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
2248 int stream_vaControl( stream_t *s, int i_query, va_list args )
2250 return s->pf_control( s, i_query, args );
2256 void stream_Delete( stream_t *s )
2261 int stream_Control( stream_t *s, int i_query, ... )
2267 return VLC_EGENERIC;
2269 va_start( args, i_query );
2270 i_result = s->pf_control( s, i_query, args );
2276 * Read "i_size" bytes and store them in a block_t.
2277 * It always read i_size bytes unless you are at the end of the stream
2278 * where it return what is available.
2280 block_t *stream_Block( stream_t *s, int i_size )
2282 if( i_size <= 0 ) return NULL;
2284 /* emulate block read */
2285 block_t *p_bk = block_New( s, i_size );
2288 int i_read = stream_Read( s, p_bk->p_buffer, i_size );
2291 p_bk->i_buffer = i_read;
2294 block_Release( p_bk );