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"
43 * - tune the 2 methods (block/stream)
44 * - compute cost for seek
45 * - improve stream mode seeking with closest segments
47 * - Maybe remove (block/stream) in favour of immediate
52 * One linked list of data read
54 * More complex scheme using mutliple track to avoid seeking
55 * - using directly the access (only indirection for peeking).
56 * This method is known to introduce much less latency.
57 * It should probably defaulted (instead of the stream method (2)).
60 /* How many tracks we have, currently only used for stream mode */
61 #ifdef OPTIMIZE_MEMORY
62 # define STREAM_CACHE_TRACK 1
63 /* Max size of our cache 128Ko per track */
64 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
66 # define STREAM_CACHE_TRACK 3
67 /* Max size of our cache 4Mo per track */
68 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
71 /* How many data we try to prebuffer */
72 #define STREAM_CACHE_PREBUFFER_SIZE (32767)
73 /* Maximum time we take to pre-buffer */
74 #define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
76 /* Method1: Simple, for pf_block.
77 * We get blocks and put them in the linked list.
78 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
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;
308 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
311 vlc_object_release( s );
315 /* Attach it now, needed for b_die */
316 vlc_object_attach( s, p_access );
318 s->pf_read = NULL; /* Set up later */
320 s->pf_control = AStreamControl;
321 s->pf_destroy = AStreamDestroy;
323 /* UTF16 and UTF32 text file conversion */
325 s->b_little_endian = false;
326 s->conv = (vlc_iconv_t)(-1);
329 p_sys->p_access = p_access;
330 if( p_access->pf_block )
331 p_sys->method = STREAM_METHOD_BLOCK;
332 else if( var_CreateGetBool( s, "use-stream-immediate" ) )
333 p_sys->method = STREAM_METHOD_IMMEDIATE;
335 p_sys->method = STREAM_METHOD_STREAM;
337 p_sys->record.b_active = false;
339 p_sys->i_pos = p_access->info.i_pos;
342 access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
343 p_sys->stat.i_bytes = 0;
344 p_sys->stat.i_read_time = 0;
345 p_sys->stat.i_read_count = 0;
346 p_sys->stat.i_seek_count = 0;
347 p_sys->stat.i_seek_time = 0;
351 p_sys->i_list_index = 0;
352 p_sys->p_list_access = 0;
354 p_sys->b_quick = b_quick;
356 /* Get the additional list of inputs if any (for concatenation) */
357 if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
359 access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
360 if( p_entry == NULL )
362 char *psz_name, *psz_parser = psz_name = psz_list;
364 p_sys->p_list_access = p_access;
365 p_entry->i_size = p_access->info.i_size;
366 p_entry->psz_path = strdup( p_access->psz_path );
367 if( p_entry->psz_path == NULL )
372 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
373 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
374 p_entry->psz_path, p_access->info.i_size );
376 while( psz_name && *psz_name )
378 psz_parser = strchr( psz_name, ',' );
379 if( psz_parser ) *psz_parser = 0;
381 psz_name = strdup( psz_name );
384 access_t *p_tmp = access_New( p_access, p_access->psz_access,
389 psz_name = psz_parser;
390 if( psz_name ) psz_name++;
394 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
395 psz_name, p_tmp->info.i_size );
397 p_entry = malloc( sizeof(access_entry_t) );
398 if( p_entry == NULL )
400 p_entry->i_size = p_tmp->info.i_size;
401 p_entry->psz_path = psz_name;
402 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
404 access_Delete( p_tmp );
407 psz_name = psz_parser;
408 if( psz_name ) psz_name++;
411 FREENULL( psz_list );
415 p_sys->p_peek = NULL;
417 if( p_sys->method == STREAM_METHOD_BLOCK )
419 msg_Dbg( s, "Using AStream*Block" );
420 s->pf_read = AStreamReadBlock;
421 s->pf_peek = AStreamPeekBlock;
423 /* Init all fields of p_sys->block */
424 p_sys->block.i_start = p_sys->i_pos;
425 p_sys->block.i_offset = 0;
426 p_sys->block.p_current = NULL;
427 p_sys->block.i_size = 0;
428 p_sys->block.p_first = NULL;
429 p_sys->block.pp_last = &p_sys->block.p_first;
431 /* Do the prebuffering */
432 AStreamPrebufferBlock( s );
434 if( p_sys->block.i_size <= 0 )
436 msg_Err( s, "cannot pre fill buffer" );
440 else if( p_sys->method == STREAM_METHOD_IMMEDIATE )
442 msg_Dbg( s, "Using AStream*Immediate" );
444 s->pf_read = AStreamReadImmediate;
445 s->pf_peek = AStreamPeekImmediate;
447 /* Allocate/Setup our tracks (useful to peek)*/
448 p_sys->immediate.i_end = 0;
449 p_sys->immediate.p_buffer = malloc( STREAM_CACHE_SIZE );
451 msg_Dbg( s, "p_buffer %p-%p",
452 p_sys->immediate.p_buffer,
453 &p_sys->immediate.p_buffer[STREAM_CACHE_SIZE] );
455 if( p_sys->immediate.p_buffer == NULL )
457 msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
466 assert( p_sys->method == STREAM_METHOD_STREAM );
468 msg_Dbg( s, "Using AStream*Stream" );
470 s->pf_read = AStreamReadStream;
471 s->pf_peek = AStreamPeekStream;
473 /* Allocate/Setup our tracks */
474 p_sys->stream.i_offset = 0;
475 p_sys->stream.i_tk = 0;
476 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
477 if( p_sys->stream.p_buffer == NULL )
479 msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
483 p_sys->stream.i_used = 0;
484 access_Control( p_access, ACCESS_GET_MTU,
485 &p_sys->stream.i_read_size );
486 if( p_sys->stream.i_read_size <= 0 )
487 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
488 else if( p_sys->stream.i_read_size <= 256 )
489 p_sys->stream.i_read_size = 256;
491 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
493 p_sys->stream.tk[i].i_date = 0;
494 p_sys->stream.tk[i].i_start = p_sys->i_pos;
495 p_sys->stream.tk[i].i_end = p_sys->i_pos;
496 p_sys->stream.tk[i].p_buffer=
497 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
500 /* Do the prebuffering */
501 AStreamPrebufferStream( s );
503 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
505 msg_Err( s, "cannot pre fill buffer" );
513 if( p_sys->method == STREAM_METHOD_BLOCK )
519 free( p_sys->stream.p_buffer );
521 while( p_sys->i_list > 0 )
522 free( p_sys->list[--(p_sys->i_list)] );
526 vlc_object_detach( s );
527 vlc_object_release( s );
531 /****************************************************************************
533 ****************************************************************************/
534 static void AStreamDestroy( stream_t *s )
536 stream_sys_t *p_sys = s->p_sys;
538 vlc_object_detach( s );
540 if( p_sys->record.b_active )
541 ARecordSetState( s, false, NULL );
543 if( p_sys->method == STREAM_METHOD_BLOCK )
544 block_ChainRelease( p_sys->block.p_first );
545 else if( p_sys->method == STREAM_METHOD_IMMEDIATE )
546 free( p_sys->immediate.p_buffer );
548 free( p_sys->stream.p_buffer );
550 free( p_sys->p_peek );
552 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
553 access_Delete( p_sys->p_list_access );
555 while( p_sys->i_list-- )
557 free( p_sys->list[p_sys->i_list]->psz_path );
558 free( p_sys->list[p_sys->i_list] );
564 vlc_object_release( s );
567 static void UStreamDestroy( stream_t *s )
569 access_t *p_access = (access_t *)s->p_parent;
571 access_Delete( p_access );
574 /****************************************************************************
575 * stream_AccessReset:
576 ****************************************************************************/
577 void stream_AccessReset( stream_t *s )
579 stream_sys_t *p_sys = s->p_sys;
581 p_sys->i_pos = p_sys->p_access->info.i_pos;
583 if( p_sys->method == STREAM_METHOD_BLOCK )
585 block_ChainRelease( p_sys->block.p_first );
587 /* Init all fields of p_sys->block */
588 p_sys->block.i_start = p_sys->i_pos;
589 p_sys->block.i_offset = 0;
590 p_sys->block.p_current = NULL;
591 p_sys->block.i_size = 0;
592 p_sys->block.p_first = NULL;
593 p_sys->block.pp_last = &p_sys->block.p_first;
595 /* Do the prebuffering */
596 AStreamPrebufferBlock( s );
598 else if( p_sys->method == STREAM_METHOD_IMMEDIATE )
600 stream_buffer_empty( s, stream_buffered_size( s ) );
606 assert( p_sys->method == STREAM_METHOD_STREAM );
608 /* Setup our tracks */
609 p_sys->stream.i_offset = 0;
610 p_sys->stream.i_tk = 0;
611 p_sys->stream.i_used = 0;
613 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
615 p_sys->stream.tk[i].i_date = 0;
616 p_sys->stream.tk[i].i_start = p_sys->i_pos;
617 p_sys->stream.tk[i].i_end = p_sys->i_pos;
620 /* Do the prebuffering */
621 AStreamPrebufferStream( s );
625 /****************************************************************************
626 * stream_AccessUpdate:
627 ****************************************************************************/
628 void stream_AccessUpdate( stream_t *s )
630 stream_sys_t *p_sys = s->p_sys;
632 p_sys->i_pos = p_sys->p_access->info.i_pos;
637 for( i = 0; i < p_sys->i_list_index; i++ )
639 p_sys->i_pos += p_sys->list[i]->i_size;
644 /****************************************************************************
646 ****************************************************************************/
647 static int AStreamControl( stream_t *s, int i_query, va_list args )
649 stream_sys_t *p_sys = s->p_sys;
650 access_t *p_access = p_sys->p_access;
654 const char *psz_string;
655 int64_t *pi_64, i_64;
660 case STREAM_GET_SIZE:
661 pi_64 = (int64_t*)va_arg( args, int64_t * );
662 if( s->p_sys->i_list )
666 for( i = 0; i < s->p_sys->i_list; i++ )
667 *pi_64 += s->p_sys->list[i]->i_size;
670 *pi_64 = p_access->info.i_size;
673 case STREAM_CAN_SEEK:
674 p_bool = (bool*)va_arg( args, bool * );
675 access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
678 case STREAM_CAN_FASTSEEK:
679 p_bool = (bool*)va_arg( args, bool * );
680 access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
683 case STREAM_GET_POSITION:
684 pi_64 = (int64_t*)va_arg( args, int64_t * );
685 *pi_64 = p_sys->i_pos;
688 case STREAM_SET_POSITION:
689 i_64 = (int64_t)va_arg( args, int64_t );
690 switch( p_sys->method )
692 case STREAM_METHOD_BLOCK:
693 return AStreamSeekBlock( s, i_64 );
694 case STREAM_METHOD_IMMEDIATE:
695 return AStreamSeekImmediate( s, i_64 );
696 case STREAM_METHOD_STREAM:
697 return AStreamSeekStream( s, i_64 );
706 case STREAM_CONTROL_ACCESS:
707 i_int = (int) va_arg( args, int );
708 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
709 i_int != ACCESS_SET_PRIVATE_ID_CA &&
710 i_int != ACCESS_GET_PRIVATE_ID_STATE )
712 msg_Err( s, "Hey, what are you thinking ?"
713 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
716 return access_vaControl( p_access, i_int, args );
718 case STREAM_GET_CONTENT_TYPE:
719 return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
720 va_arg( args, char ** ) );
721 case STREAM_SET_RECORD_STATE:
722 b_bool = (bool)va_arg( args, int );
725 psz_string = (const char*)va_arg( args, const char* );
726 return ARecordSetState( s, b_bool, psz_string );
729 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
735 /****************************************************************************
736 * ARecord*: record stream functions
737 ****************************************************************************/
738 static int ARecordStart( stream_t *s, const char *psz_extension )
740 stream_sys_t *p_sys = s->p_sys;
747 psz_extension = "dat";
750 char *psz_path = var_CreateGetString( s, "input-record-path" );
751 if( !psz_path || *psz_path == '\0' )
754 psz_path = strdup( config_GetHomeDir() );
761 * TODO allow prefix configuration */
762 psz_file = input_CreateFilename( VLC_OBJECT(s), psz_path, INPUT_RECORD_PREFIX, psz_extension );
769 f = utf8_fopen( psz_file, "wb" );
775 msg_Dbg( s, "Recording into %s", psz_file );
780 p_sys->record.b_active = true;
781 p_sys->record.b_error = false;
784 static int ARecordStop( stream_t *s )
786 stream_sys_t *p_sys = s->p_sys;
788 assert( p_sys->record.b_active );
790 msg_Dbg( s, "Recording completed" );
791 fclose( p_sys->record.f );
792 p_sys->record.b_active = false;
796 static int ARecordSetState( stream_t *s, bool b_record, const char *psz_extension )
798 stream_sys_t *p_sys = s->p_sys;
800 if( !!p_sys->record.b_active == !!b_record )
804 return ARecordStart( s, psz_extension );
806 return ARecordStop( s );
808 static void ARecordWrite( stream_t *s, const uint8_t *p_buffer, size_t i_buffer )
810 stream_sys_t *p_sys = s->p_sys;
812 assert( p_sys->record.b_active );
816 const bool b_previous_error = p_sys->record.b_error;
817 const size_t i_written = fwrite( p_buffer, 1, i_buffer, p_sys->record.f );
819 p_sys->record.b_error = i_written != i_buffer;
821 /* TODO maybe a intf_UserError or something like that ? */
822 if( p_sys->record.b_error && !b_previous_error )
823 msg_Err( s, "Failed to record data (begin)" );
824 else if( !p_sys->record.b_error && b_previous_error )
825 msg_Err( s, "Failed to record data (end)" );
829 /****************************************************************************
831 ****************************************************************************/
832 static void AStreamPrebufferBlock( stream_t *s )
834 stream_sys_t *p_sys = s->p_sys;
835 access_t *p_access = p_sys->p_access;
840 msg_Dbg( s, "pre buffering" );
844 int64_t i_date = mdate();
848 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
849 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
854 p_sys->stat.i_bytes = p_sys->block.i_size;
855 p_sys->stat.i_read_time = i_date - i_start;
856 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
857 (p_sys->stat.i_read_time + 1);
859 msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
860 "%"PRId64" kbytes/s",
862 p_sys->stat.i_read_time / INT64_C(1000000),
868 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
877 /* Append the block */
878 p_sys->block.i_size += b->i_buffer;
879 *p_sys->block.pp_last = b;
880 p_sys->block.pp_last = &b->p_next;
882 p_sys->stat.i_read_count++;
886 if( p_access->info.b_prebuffered )
888 /* Access has already prebufferred - update stats and exit */
889 p_sys->stat.i_bytes = p_sys->block.i_size;
890 p_sys->stat.i_read_time = mdate() - i_start;
897 msg_Dbg( s, "received first data for our buffer");
902 p_sys->block.p_current = p_sys->block.p_first;
905 static int AStreamRefillBlock( stream_t *s );
907 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
909 stream_sys_t *p_sys = s->p_sys;
911 uint8_t *p_data = p_read;
912 uint8_t *p_record = p_data;
913 unsigned int i_data = 0;
916 if( p_sys->block.p_current == NULL )
919 if( p_sys->record.b_active && !p_data )
920 p_record = p_data = malloc( i_read );
924 /* seek within this stream if possible, else use plain old read and discard */
925 stream_sys_t *p_sys = s->p_sys;
926 access_t *p_access = p_sys->p_access;
928 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
930 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
933 while( i_data < i_read )
936 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
937 unsigned int i_copy = __MIN( (unsigned int)__MAX(i_current,0), i_read - i_data);
943 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
949 p_sys->block.i_offset += i_copy;
950 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
952 /* Current block is now empty, switch to next */
953 if( p_sys->block.p_current )
955 p_sys->block.i_offset = 0;
956 p_sys->block.p_current = p_sys->block.p_current->p_next;
958 /*Get a new block if needed */
959 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
966 if( p_sys->record.b_active )
968 if( i_data > 0 && p_record != NULL)
969 ARecordWrite( s, p_record, i_data );
974 p_sys->i_pos += i_data;
978 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
980 stream_sys_t *p_sys = s->p_sys;
982 unsigned int i_data = 0;
984 unsigned int i_offset;
986 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
988 /* We can directly give a pointer over our buffer */
989 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
991 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
995 /* We need to create a local copy */
996 if( p_sys->i_peek < i_read )
998 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1004 p_sys->i_peek = i_read;
1007 /* Fill enough data */
1008 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
1011 block_t **pp_last = p_sys->block.pp_last;
1013 if( AStreamRefillBlock( s ) ) break;
1015 /* Our buffer are probably filled enough, don't try anymore */
1016 if( pp_last == p_sys->block.pp_last ) break;
1019 /* Copy what we have */
1020 b = p_sys->block.p_current;
1021 i_offset = p_sys->block.i_offset;
1022 p_data = p_sys->p_peek;
1024 while( b && i_data < i_read )
1026 unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
1027 int i_copy = __MIN( i_current, i_read - i_data );
1029 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
1034 if( i_offset >= b->i_buffer )
1041 *pp_peek = p_sys->p_peek;
1045 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
1047 stream_sys_t *p_sys = s->p_sys;
1048 access_t *p_access = p_sys->p_access;
1049 int64_t i_offset = i_pos - p_sys->block.i_start;
1052 /* We already have thoses data, just update p_current/i_offset */
1053 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
1055 block_t *b = p_sys->block.p_first;
1058 while( i_current + b->i_buffer < i_offset )
1060 i_current += b->i_buffer;
1064 p_sys->block.p_current = b;
1065 p_sys->block.i_offset = i_offset - i_current;
1067 p_sys->i_pos = i_pos;
1072 /* We may need to seek or to read data */
1076 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1080 msg_Err( s, "backward seeking impossible (access not seekable)" );
1081 return VLC_EGENERIC;
1088 bool b_aseek, b_aseekfast;
1090 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1091 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
1096 msg_Warn( s, "%"PRId64" bytes need to be skipped "
1097 "(access non seekable)",
1098 i_offset - p_sys->block.i_size );
1102 int64_t i_skip = i_offset - p_sys->block.i_size;
1104 /* Avg bytes per packets */
1105 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
1106 /* TODO compute a seek cost instead of fixed threshold */
1107 int i_th = b_aseekfast ? 1 : 5;
1109 if( i_skip <= i_th * i_avg &&
1110 i_skip < STREAM_CACHE_SIZE )
1115 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
1116 b_seek, i_th*i_avg, i_skip );
1122 int64_t i_start, i_end;
1123 /* Do the access seek */
1125 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1129 block_ChainRelease( p_sys->block.p_first );
1132 p_sys->block.i_start = p_sys->i_pos = i_pos;
1133 p_sys->block.i_offset = 0;
1134 p_sys->block.p_current = NULL;
1135 p_sys->block.i_size = 0;
1136 p_sys->block.p_first = NULL;
1137 p_sys->block.pp_last = &p_sys->block.p_first;
1139 /* Refill a block */
1140 if( AStreamRefillBlock( s ) )
1141 return VLC_EGENERIC;
1144 p_sys->stat.i_seek_time += i_end - i_start;
1145 p_sys->stat.i_seek_count++;
1152 /* Read and skip enough data */
1153 if( AStreamRefillBlock( s ) )
1154 return VLC_EGENERIC;
1156 while( p_sys->block.p_current &&
1157 p_sys->i_pos + p_sys->block.p_current->i_buffer - p_sys->block.i_offset < i_pos )
1159 p_sys->i_pos += p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
1160 p_sys->block.p_current = p_sys->block.p_current->p_next;
1161 p_sys->block.i_offset = 0;
1164 while( p_sys->block.i_start + p_sys->block.i_size < i_pos );
1166 p_sys->block.i_offset = i_pos - p_sys->i_pos;
1167 p_sys->i_pos = i_pos;
1172 return VLC_EGENERIC;
1175 static int AStreamRefillBlock( stream_t *s )
1177 stream_sys_t *p_sys = s->p_sys;
1178 int64_t i_start, i_stop;
1182 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1183 p_sys->block.p_first != p_sys->block.p_current )
1185 block_t *b = p_sys->block.p_first;
1187 p_sys->block.i_start += b->i_buffer;
1188 p_sys->block.i_size -= b->i_buffer;
1189 p_sys->block.p_first = b->p_next;
1193 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1194 p_sys->block.p_current == p_sys->block.p_first &&
1195 p_sys->block.p_current->p_next ) /* At least 2 packets */
1197 /* Enough data, don't read more */
1201 /* Now read a new block */
1208 return VLC_EGENERIC;
1211 if( ( b = AReadBlock( s, &b_eof ) ) )
1214 return VLC_EGENERIC;
1221 /* Append the block */
1222 p_sys->block.i_size += b->i_buffer;
1223 *p_sys->block.pp_last = b;
1224 p_sys->block.pp_last = &b->p_next;
1227 if( p_sys->block.p_current == NULL )
1228 p_sys->block.p_current = b;
1231 p_sys->stat.i_bytes += b->i_buffer;
1232 p_sys->stat.i_read_time += i_stop - i_start;
1233 p_sys->stat.i_read_count++;
1242 /****************************************************************************
1244 ****************************************************************************/
1245 static int AStreamRefillStream( stream_t *s );
1247 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read )
1249 stream_sys_t *p_sys = s->p_sys;
1250 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1252 uint8_t *p_data = (uint8_t *)p_read;
1253 uint8_t *p_record = p_data;
1254 unsigned int i_data = 0;
1256 if( tk->i_start >= tk->i_end )
1259 if( p_sys->record.b_active && !p_data )
1260 p_record = p_data = malloc( i_read );
1262 if( p_data == NULL )
1264 /* seek within this stream if possible, else use plain old read and discard */
1265 stream_sys_t *p_sys = s->p_sys;
1266 access_t *p_access = p_sys->p_access;
1268 /* seeking after EOF is not what we want */
1269 if( !( p_access->info.b_eof ) )
1272 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1274 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1279 msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1280 " offset=%d end=%"PRId64,
1281 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1282 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1285 while( i_data < i_read )
1287 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1288 STREAM_CACHE_TRACK_SIZE;
1289 unsigned int i_current =
1290 __MAX(0,__MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1291 STREAM_CACHE_TRACK_SIZE - i_off ));
1292 int i_copy = __MIN( i_current, i_read - i_data );
1294 if( i_copy <= 0 ) break; /* EOF */
1297 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1300 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1304 p_sys->stream.i_offset += i_copy;
1306 /* Update pos now */
1307 p_sys->i_pos += i_copy;
1310 p_sys->stream.i_used += i_copy;
1311 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1312 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1314 if( AStreamRefillStream( s ) )
1317 if( tk->i_start >= tk->i_end ) break;
1322 if( p_sys->record.b_active )
1324 if( i_data > 0 && p_record != NULL)
1325 ARecordWrite( s, p_record, i_data );
1333 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1335 stream_sys_t *p_sys = s->p_sys;
1336 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1339 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1342 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1343 "start=%"PRId64" offset=%d end=%"PRId64,
1344 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1345 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1348 /* Avoid problem, but that should *never* happen */
1349 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1350 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1352 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1354 if( p_sys->stream.i_used <= 1 )
1356 /* Be sure we will read something */
1357 p_sys->stream.i_used += i_read -
1358 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1360 if( AStreamRefillStream( s ) ) break;
1363 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1364 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1366 /* Now, direct pointer or a copy ? */
1367 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1368 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1370 *pp_peek = &tk->p_buffer[i_off];
1374 if( p_sys->i_peek < i_read )
1376 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1377 if( !p_sys->p_peek )
1382 p_sys->i_peek = i_read;
1385 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1386 STREAM_CACHE_TRACK_SIZE - i_off );
1387 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1388 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1390 *pp_peek = p_sys->p_peek;
1394 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1396 stream_sys_t *p_sys = s->p_sys;
1397 access_t *p_access = p_sys->p_access;
1405 msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1406 " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1407 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1408 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1409 p_sys->stream.i_offset,
1410 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1414 /* Seek in our current track ? */
1415 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1416 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1418 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1420 msg_Dbg( s, "AStreamSeekStream: current track" );
1422 p_sys->i_pos = i_pos;
1423 p_sys->stream.i_offset = i_pos - tk->i_start;
1425 /* If there is not enough data left in the track, refill */
1426 /* \todo How to get a correct value for
1427 * - refilling threshold
1428 * - how much to refill
1430 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1431 p_sys->stream.i_read_size )
1433 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1435 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1436 AStreamRefillStream( s );
1442 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1445 /* We can't do nothing */
1446 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1447 return VLC_EGENERIC;
1450 /* Date the current track */
1451 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1453 /* Try to reuse already read data */
1454 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1456 stream_track_t *tk = &p_sys->stream.tk[i];
1458 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1461 msg_Dbg( s, "AStreamSeekStream: reusing %d start=%"PRId64
1462 " end=%"PRId64, i, tk->i_start, tk->i_end );
1465 /* Seek at the end of the buffer */
1466 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1469 p_sys->i_pos = i_pos;
1470 p_sys->stream.i_tk = i;
1471 p_sys->stream.i_offset = i_pos - tk->i_start;
1473 if( p_sys->stream.i_used < 1024 )
1474 p_sys->stream.i_used = 1024;
1476 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1477 return VLC_EGENERIC;
1483 access_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1484 /* FIXME compute seek cost (instead of static 'stupid' value) */
1485 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1491 /* Search closest segment TODO */
1492 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1494 stream_track_t *tk = &p_sys->stream.tk[i];
1496 if( i_pos + i_maxth >= tk->i_start )
1498 msg_Dbg( s, "good segment before current pos, TODO" );
1500 if( i_pos - i_maxth <= tk->i_end )
1502 msg_Dbg( s, "good segment after current pos, TODO" );
1507 /* Nothing good, seek and choose oldest segment */
1508 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1509 p_sys->i_pos = i_pos;
1512 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1514 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1518 /* Reset the segment */
1519 p_sys->stream.i_tk = i_new;
1520 p_sys->stream.i_offset = 0;
1521 p_sys->stream.tk[i_new].i_start = i_pos;
1522 p_sys->stream.tk[i_new].i_end = i_pos;
1525 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1526 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1528 if( AStreamRefillStream( s ) )
1529 return VLC_EGENERIC;
1534 static int AStreamRefillStream( stream_t *s )
1536 stream_sys_t *p_sys = s->p_sys;
1537 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1539 /* We read but won't increase i_start after initial start + offset */
1541 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1542 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1543 bool b_read = false;
1544 int64_t i_start, i_stop;
1546 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1549 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1550 p_sys->stream.i_used, i_toread );
1554 while( i_toread > 0 )
1556 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1560 return VLC_EGENERIC;
1562 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1563 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1565 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1570 else if( i_read == 0 )
1573 return VLC_EGENERIC;
1579 tk->i_end += i_read;
1581 /* Windows of STREAM_CACHE_TRACK_SIZE */
1582 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1584 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1586 tk->i_start += i_invalid;
1587 p_sys->stream.i_offset -= i_invalid;
1591 p_sys->stream.i_used -= i_read;
1593 p_sys->stat.i_bytes += i_read;
1594 p_sys->stat.i_read_count++;
1598 p_sys->stat.i_read_time += i_stop - i_start;
1603 static void AStreamPrebufferStream( stream_t *s )
1605 stream_sys_t *p_sys = s->p_sys;
1606 access_t *p_access = p_sys->p_access;
1608 int64_t i_first = 0;
1610 int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1611 ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1612 STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1614 msg_Dbg( s, "pre-buffering..." );
1618 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1620 int64_t i_date = mdate();
1623 if( s->b_die || tk->i_end >= i_prebuffer ||
1624 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1629 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1630 p_sys->stat.i_read_time = i_date - i_start;
1631 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1632 (p_sys->stat.i_read_time+1);
1634 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1635 "%"PRId64" kbytes/s",
1636 p_sys->stat.i_bytes,
1637 p_sys->stat.i_read_time / INT64_C(1000000),
1638 i_byterate / 1024 );
1643 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1644 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1645 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1648 else if( i_read == 0 )
1654 msg_Dbg( s, "received first data for our buffer");
1657 tk->i_end += i_read;
1659 p_sys->stat.i_read_count++;
1663 /****************************************************************************
1665 ****************************************************************************/
1667 static int AStreamReadImmediate( stream_t *s, void *p_read, unsigned int i_read )
1669 stream_sys_t *p_sys = s->p_sys;
1670 uint8_t *p_data= (uint8_t*)p_read;
1671 uint8_t *p_record = p_data;
1672 unsigned int i_data;
1675 msg_Dbg( s, "AStreamReadImmediate p_read=%p i_read=%d",
1679 if( p_sys->record.b_active && !p_data )
1680 p_record = p_data = malloc( i_read );
1682 /* First, check if we already have some data in the buffer,
1683 * that we could copy directly */
1684 i_data = __MIN( stream_buffered_size( s ), i_read );
1688 msg_Dbg( s, "AStreamReadImmediate: copy %u from %p", i_data, stream_buffer( s ) );
1691 assert( i_data <= STREAM_CACHE_SIZE );
1695 memcpy( p_data, stream_buffer( s ), i_data );
1700 /* Now that we've read our buffer we don't need its i_copy bytes */
1701 stream_buffer_empty( s, i_data );
1703 /* Now check if we have still to really read some data */
1704 while( i_data < i_read )
1706 const unsigned int i_to_read = i_read - i_data;
1711 i_result = AReadStream( s, p_data, i_to_read );
1715 void *p_dummy = malloc( i_to_read );
1720 i_result = AReadStream( s, p_dummy, i_to_read );
1727 p_sys->i_pos += i_data;
1733 if( p_sys->record.b_active )
1735 if( i_data > 0 && p_record != NULL)
1736 ARecordWrite( s, p_record, i_data );
1743 static int AStreamPeekImmediate( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1746 msg_Dbg( s, "AStreamPeekImmediate: %d size=%"PRId64,
1747 i_read, size_buffered_size( s ) );
1750 /* Avoid problem, but that shouldn't happen
1751 * FIXME yes it can */
1752 if( i_read > STREAM_CACHE_SIZE / 2 )
1753 i_read = STREAM_CACHE_SIZE / 2;
1755 int i_to_read = i_read - stream_buffered_size( s );
1759 msg_Dbg( s, "AStreamPeekImmediate: Reading %d",
1762 i_to_read = AReadStream( s, stream_buffer( s ) + stream_buffered_size( s ),
1766 stream_buffer_fill( s, i_to_read );
1769 *pp_peek = stream_buffer( s );
1771 return __MIN( stream_buffered_size( s ), i_read );
1774 static int AStreamSeekImmediate( stream_t *s, int64_t i_pos )
1776 stream_sys_t *p_sys = s->p_sys;
1777 access_t *p_access = p_sys->p_access;
1781 msg_Dbg( s, "AStreamSeekImmediate to %"PRId64" pos=%"PRId64
1782 i_pos, p_sys->i_pos );
1785 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1788 /* We can't do nothing */
1789 msg_Dbg( s, "AStreamSeekImmediate: can't seek" );
1790 return VLC_EGENERIC;
1793 /* Just reset our buffer */
1794 stream_buffer_empty( s, stream_buffered_size( s ) );
1796 if( ASeek( s, i_pos ) )
1797 return VLC_EGENERIC;
1802 /****************************************************************************
1804 ****************************************************************************/
1806 * Read from the stream untill first newline.
1807 * \param s Stream handle to read from
1808 * \return A pointer to the allocated output string. You need to free this when you are done.
1810 #define STREAM_PROBE_LINE 2048
1811 #define STREAM_LINE_MAX (2048*100)
1812 char *stream_ReadLine( stream_t *s )
1814 char *p_line = NULL;
1815 int i_line = 0, i_read = 0;
1817 while( i_read < STREAM_LINE_MAX )
1820 const uint8_t *p_data;
1824 /* Probe new data */
1825 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1826 if( i_data <= 0 ) break; /* No more data */
1829 i_pos = stream_Tell( s );
1830 if( i_pos == 0 && i_data >= 3 )
1833 const char *psz_encoding = NULL;
1835 if( !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
1837 psz_encoding = "UTF-8";
1840 else if( !memcmp( p_data, "\xFF\xFE", 2 ) )
1842 psz_encoding = "UTF-16LE";
1843 s->b_little_endian = true;
1844 s->i_char_width = 2;
1847 else if( !memcmp( p_data, "\xFE\xFF", 2 ) )
1849 psz_encoding = "UTF-16BE";
1850 s->i_char_width = 2;
1854 /* Seek past the BOM */
1857 stream_Seek( s, i_bom_size );
1858 p_data += i_bom_size;
1859 i_data -= i_bom_size;
1862 /* Open the converter if we need it */
1863 if( psz_encoding != NULL )
1865 input_thread_t *p_input;
1866 msg_Dbg( s, "%s BOM detected", psz_encoding );
1867 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1868 if( s->i_char_width > 1 )
1870 s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1871 if( s->conv == (vlc_iconv_t)-1 )
1873 msg_Err( s, "iconv_open failed" );
1876 if( p_input != NULL)
1878 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1879 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1880 vlc_object_release( p_input );
1885 if( i_data % s->i_char_width )
1887 /* keep i_char_width boundary */
1888 i_data = i_data - ( i_data % s->i_char_width );
1889 msg_Warn( s, "the read is not i_char_width compatible");
1895 /* Check if there is an EOL */
1896 if( s->i_char_width == 1 )
1898 /* UTF-8: 0A <LF> */
1899 psz_eol = memchr( p_data, '\n', i_data );
1903 const uint8_t *p = p_data;
1904 const uint8_t *p_last = p + i_data - s->i_char_width;
1906 if( s->i_char_width == 2 )
1908 if( s->b_little_endian == true)
1910 /* UTF-16LE: 0A 00 <LF> */
1911 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1916 /* UTF-16BE: 00 0A <LF> */
1917 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1928 psz_eol = (char *)p + ( s->i_char_width - 1 );
1934 i_data = (psz_eol - (char *)p_data) + 1;
1935 p_line = realloc( p_line, i_line + i_data + s->i_char_width ); /* add \0 */
1938 i_data = stream_Read( s, &p_line[i_line], i_data );
1939 if( i_data <= 0 ) break; /* Hmmm */
1940 i_line += i_data - s->i_char_width; /* skip \n */;
1943 /* We have our line */
1947 /* Read data (+1 for easy \0 append) */
1948 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->i_char_width );
1951 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1952 if( i_data <= 0 ) break; /* Hmmm */
1960 for( j = 0; j < s->i_char_width; j++ )
1962 p_line[i_line + j] = '\0';
1964 i_line += s->i_char_width; /* the added \0 */
1965 if( s->i_char_width > 1 )
1967 size_t i_in = 0, i_out = 0;
1968 const char * p_in = NULL;
1969 char * p_out = NULL;
1970 char * psz_new_line = NULL;
1973 psz_new_line = malloc( i_line );
1974 if( psz_new_line == NULL )
1976 i_in = i_out = (size_t)i_line;
1978 p_out = psz_new_line;
1980 if( vlc_iconv( s->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1982 msg_Err( s, "iconv failed" );
1983 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1986 p_line = psz_new_line;
1987 i_line = (size_t)i_line - i_out; /* does not include \0 */
1990 /* Remove trailing LF/CR */
1991 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1992 p_line[i_line-2] == '\n') ) i_line--;
1994 /* Make sure the \0 is there */
1995 p_line[i_line-1] = '\0';
2002 /* We failed to read any data, probably EOF */
2004 if( s->conv != (vlc_iconv_t)(-1) ) vlc_iconv_close( s->conv );
2008 /****************************************************************************
2009 * Access reading/seeking wrappers to handle concatenated streams.
2010 ****************************************************************************/
2011 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read )
2013 stream_sys_t *p_sys = s->p_sys;
2014 access_t *p_access = p_sys->p_access;
2015 input_thread_t *p_input = NULL;
2016 int i_read_orig = i_read;
2019 if( s->p_parent && s->p_parent->p_parent &&
2020 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
2021 p_input = (input_thread_t *)s->p_parent->p_parent;
2023 if( !p_sys->i_list )
2025 i_read = p_access->pf_read( p_access, p_read, i_read );
2026 if( p_access->b_die )
2027 vlc_object_kill( s );
2030 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2031 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
2033 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2034 (float)i_total, NULL );
2035 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2036 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2041 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
2043 if( p_access->b_die )
2044 vlc_object_kill( s );
2046 /* If we reached an EOF then switch to the next stream in the list */
2047 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
2049 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
2050 access_t *p_list_access;
2052 msg_Dbg( s, "opening input `%s'", psz_name );
2054 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
2056 if( !p_list_access ) return 0;
2058 if( p_sys->p_list_access != p_access )
2059 access_Delete( p_sys->p_list_access );
2061 p_sys->p_list_access = p_list_access;
2063 /* We have to read some data */
2064 return AReadStream( s, p_read, i_read_orig );
2067 /* Update read bytes in input */
2070 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2071 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
2072 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2073 (float)i_total, NULL );
2074 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2075 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2080 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
2082 stream_sys_t *p_sys = s->p_sys;
2083 access_t *p_access = p_sys->p_access;
2084 input_thread_t *p_input = NULL;
2089 if( s->p_parent && s->p_parent->p_parent &&
2090 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
2091 p_input = (input_thread_t *)s->p_parent->p_parent;
2093 if( !p_sys->i_list )
2095 p_block = p_access->pf_block( p_access );
2096 if( p_access->b_die )
2097 vlc_object_kill( s );
2098 if( pb_eof ) *pb_eof = p_access->info.b_eof;
2099 if( p_input && p_block && libvlc_stats (p_access) )
2101 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2102 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
2103 p_block->i_buffer, &i_total );
2104 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2105 (float)i_total, NULL );
2106 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2107 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2112 p_block = p_sys->p_list_access->pf_block( p_sys->p_list_access );
2113 if( p_access->b_die )
2114 vlc_object_kill( s );
2115 b_eof = p_sys->p_list_access->info.b_eof;
2116 if( pb_eof ) *pb_eof = b_eof;
2118 /* If we reached an EOF then switch to the next stream in the list */
2119 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
2121 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
2122 access_t *p_list_access;
2124 msg_Dbg( s, "opening input `%s'", psz_name );
2126 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
2128 if( !p_list_access ) return 0;
2130 if( p_sys->p_list_access != p_access )
2131 access_Delete( p_sys->p_list_access );
2133 p_sys->p_list_access = p_list_access;
2135 /* We have to read some data */
2136 return AReadBlock( s, pb_eof );
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,
2149 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2155 static int ASeek( stream_t *s, int64_t i_pos )
2157 stream_sys_t *p_sys = s->p_sys;
2158 access_t *p_access = p_sys->p_access;
2160 /* Check which stream we need to access */
2166 access_t *p_list_access = 0;
2168 for( i = 0; i < p_sys->i_list - 1; i++ )
2170 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
2171 i_size += p_sys->list[i]->i_size;
2173 psz_name = p_sys->list[i]->psz_path;
2175 if( i != p_sys->i_list_index )
2176 msg_Dbg( s, "opening input `%s'", psz_name );
2178 if( i != p_sys->i_list_index && i != 0 )
2181 access_New( s, p_access->psz_access, "", psz_name );
2183 else if( i != p_sys->i_list_index )
2185 p_list_access = p_access;
2190 if( p_sys->p_list_access != p_access )
2191 access_Delete( p_sys->p_list_access );
2193 p_sys->p_list_access = p_list_access;
2196 p_sys->i_list_index = i;
2197 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
2201 return p_access->pf_seek( p_access, i_pos );
2206 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
2207 * "p_read" is NULL then data are skipped instead of read. The return
2208 * value is the real numbers of bytes read/skip. If this value is less
2209 * than i_read that means that it's the end of the stream.
2211 int stream_Read( stream_t *s, void *p_read, int i_read )
2213 return s->pf_read( s, p_read, i_read );
2217 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
2218 * \return The real numbers of valid bytes, if it's less
2219 * or equal to 0, *pp_peek is invalid.
2220 * \note pp_peek is a pointer to internal buffer and it will be invalid as
2221 * soons as other stream_* functions are called.
2222 * \note Due to input limitation, it could be less than i_peek without meaning
2223 * the end of the stream (but only when you have i_peek >=
2224 * p_input->i_bufsize)
2226 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
2228 return s->pf_peek( s, pp_peek, i_peek );
2232 * Use to control the "stream_t *". Look at #stream_query_e for
2233 * possible "i_query" value and format arguments. Return VLC_SUCCESS
2234 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
2236 int stream_vaControl( stream_t *s, int i_query, va_list args )
2238 return s->pf_control( s, i_query, args );
2244 void stream_Delete( stream_t *s )
2249 int stream_Control( stream_t *s, int i_query, ... )
2255 return VLC_EGENERIC;
2257 va_start( args, i_query );
2258 i_result = s->pf_control( s, i_query, args );
2264 * Read "i_size" bytes and store them in a block_t.
2265 * It always read i_size bytes unless you are at the end of the stream
2266 * where it return what is available.
2268 block_t *stream_Block( stream_t *s, int i_size )
2270 if( i_size <= 0 ) return NULL;
2272 /* emulate block read */
2273 block_t *p_bk = block_New( s, i_size );
2276 int i_read = stream_Read( s, p_bk->p_buffer, i_size );
2279 p_bk->i_buffer = i_read;
2282 block_Release( p_bk );