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 * XXX it should be small to avoid useless latency but big enough for
76 * efficient demux probing */
77 #define STREAM_CACHE_PREBUFFER_SIZE (128)
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 1024
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 if( p_sys->immediate.p_buffer == NULL )
466 msg_Dbg( s, "p_buffer %p-%p",
467 p_sys->immediate.p_buffer,
468 &p_sys->immediate.p_buffer[STREAM_CACHE_SIZE] );
474 assert( p_sys->method == STREAM_METHOD_STREAM );
476 msg_Dbg( s, "Using AStream*Stream" );
478 s->pf_read = AStreamReadStream;
479 s->pf_peek = AStreamPeekStream;
481 /* Allocate/Setup our tracks */
482 p_sys->stream.i_offset = 0;
483 p_sys->stream.i_tk = 0;
484 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
485 if( p_sys->stream.p_buffer == NULL )
487 p_sys->stream.i_used = 0;
488 access_Control( p_access, ACCESS_GET_MTU,
489 &p_sys->stream.i_read_size );
490 if( p_sys->stream.i_read_size <= 0 )
491 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
492 else if( p_sys->stream.i_read_size <= 256 )
493 p_sys->stream.i_read_size = 256;
495 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
497 p_sys->stream.tk[i].i_date = 0;
498 p_sys->stream.tk[i].i_start = p_sys->i_pos;
499 p_sys->stream.tk[i].i_end = p_sys->i_pos;
500 p_sys->stream.tk[i].p_buffer=
501 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
504 /* Do the prebuffering */
505 AStreamPrebufferStream( s );
507 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
509 msg_Err( s, "cannot pre fill buffer" );
517 if( p_sys->method == STREAM_METHOD_BLOCK )
523 free( p_sys->stream.p_buffer );
525 while( p_sys->i_list > 0 )
526 free( p_sys->list[--(p_sys->i_list)] );
530 vlc_object_detach( s );
531 vlc_object_release( s );
535 /****************************************************************************
537 ****************************************************************************/
538 static void AStreamDestroy( stream_t *s )
540 stream_sys_t *p_sys = s->p_sys;
542 vlc_object_detach( s );
544 if( p_sys->record.b_active )
545 ARecordSetState( s, false, NULL );
547 if( p_sys->method == STREAM_METHOD_BLOCK )
548 block_ChainRelease( p_sys->block.p_first );
549 else if( p_sys->method == STREAM_METHOD_IMMEDIATE )
550 free( p_sys->immediate.p_buffer );
552 free( p_sys->stream.p_buffer );
554 free( p_sys->p_peek );
556 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
557 access_Delete( p_sys->p_list_access );
559 while( p_sys->i_list-- )
561 free( p_sys->list[p_sys->i_list]->psz_path );
562 free( p_sys->list[p_sys->i_list] );
568 vlc_object_release( s );
571 static void UStreamDestroy( stream_t *s )
573 access_t *p_access = (access_t *)s->p_parent;
575 access_Delete( p_access );
578 /****************************************************************************
579 * stream_AccessReset:
580 ****************************************************************************/
581 void stream_AccessReset( stream_t *s )
583 stream_sys_t *p_sys = s->p_sys;
585 p_sys->i_pos = p_sys->p_access->info.i_pos;
587 if( p_sys->method == STREAM_METHOD_BLOCK )
589 block_ChainRelease( p_sys->block.p_first );
591 /* Init all fields of p_sys->block */
592 p_sys->block.i_start = p_sys->i_pos;
593 p_sys->block.i_offset = 0;
594 p_sys->block.p_current = NULL;
595 p_sys->block.i_size = 0;
596 p_sys->block.p_first = NULL;
597 p_sys->block.pp_last = &p_sys->block.p_first;
599 /* Do the prebuffering */
600 AStreamPrebufferBlock( s );
602 else if( p_sys->method == STREAM_METHOD_IMMEDIATE )
604 stream_buffer_empty( s, stream_buffered_size( s ) );
610 assert( p_sys->method == STREAM_METHOD_STREAM );
612 /* Setup our tracks */
613 p_sys->stream.i_offset = 0;
614 p_sys->stream.i_tk = 0;
615 p_sys->stream.i_used = 0;
617 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
619 p_sys->stream.tk[i].i_date = 0;
620 p_sys->stream.tk[i].i_start = p_sys->i_pos;
621 p_sys->stream.tk[i].i_end = p_sys->i_pos;
624 /* Do the prebuffering */
625 AStreamPrebufferStream( s );
629 /****************************************************************************
630 * stream_AccessUpdate:
631 ****************************************************************************/
632 void stream_AccessUpdate( stream_t *s )
634 stream_sys_t *p_sys = s->p_sys;
636 p_sys->i_pos = p_sys->p_access->info.i_pos;
641 for( i = 0; i < p_sys->i_list_index; i++ )
643 p_sys->i_pos += p_sys->list[i]->i_size;
648 /****************************************************************************
650 ****************************************************************************/
651 static int AStreamControl( stream_t *s, int i_query, va_list args )
653 stream_sys_t *p_sys = s->p_sys;
654 access_t *p_access = p_sys->p_access;
658 const char *psz_string;
659 int64_t *pi_64, i_64;
664 case STREAM_GET_SIZE:
665 pi_64 = (int64_t*)va_arg( args, int64_t * );
666 if( s->p_sys->i_list )
670 for( i = 0; i < s->p_sys->i_list; i++ )
671 *pi_64 += s->p_sys->list[i]->i_size;
674 *pi_64 = p_access->info.i_size;
677 case STREAM_CAN_SEEK:
678 p_bool = (bool*)va_arg( args, bool * );
679 access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
682 case STREAM_CAN_FASTSEEK:
683 p_bool = (bool*)va_arg( args, bool * );
684 access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
687 case STREAM_GET_POSITION:
688 pi_64 = (int64_t*)va_arg( args, int64_t * );
689 *pi_64 = p_sys->i_pos;
692 case STREAM_SET_POSITION:
693 i_64 = (int64_t)va_arg( args, int64_t );
694 switch( p_sys->method )
696 case STREAM_METHOD_BLOCK:
697 return AStreamSeekBlock( s, i_64 );
698 case STREAM_METHOD_IMMEDIATE:
699 return AStreamSeekImmediate( s, i_64 );
700 case STREAM_METHOD_STREAM:
701 return AStreamSeekStream( s, i_64 );
710 case STREAM_CONTROL_ACCESS:
711 i_int = (int) va_arg( args, int );
712 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
713 i_int != ACCESS_SET_PRIVATE_ID_CA &&
714 i_int != ACCESS_GET_PRIVATE_ID_STATE )
716 msg_Err( s, "Hey, what are you thinking ?"
717 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
720 return access_vaControl( p_access, i_int, args );
722 case STREAM_GET_CONTENT_TYPE:
723 return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
724 va_arg( args, char ** ) );
725 case STREAM_SET_RECORD_STATE:
726 b_bool = (bool)va_arg( args, int );
729 psz_string = (const char*)va_arg( args, const char* );
730 return ARecordSetState( s, b_bool, psz_string );
733 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
739 /****************************************************************************
740 * ARecord*: record stream functions
741 ****************************************************************************/
742 static int ARecordStart( stream_t *s, const char *psz_extension )
744 stream_sys_t *p_sys = s->p_sys;
751 psz_extension = "dat";
754 char *psz_path = var_CreateGetString( s, "input-record-path" );
755 if( !psz_path || *psz_path == '\0' )
758 psz_path = strdup( config_GetHomeDir() );
765 * TODO allow prefix configuration */
766 psz_file = input_CreateFilename( VLC_OBJECT(s), psz_path, INPUT_RECORD_PREFIX, psz_extension );
773 f = utf8_fopen( psz_file, "wb" );
779 msg_Dbg( s, "Recording into %s", psz_file );
784 p_sys->record.b_active = true;
785 p_sys->record.b_error = false;
788 static int ARecordStop( stream_t *s )
790 stream_sys_t *p_sys = s->p_sys;
792 assert( p_sys->record.b_active );
794 msg_Dbg( s, "Recording completed" );
795 fclose( p_sys->record.f );
796 p_sys->record.b_active = false;
800 static int ARecordSetState( stream_t *s, bool b_record, const char *psz_extension )
802 stream_sys_t *p_sys = s->p_sys;
804 if( !!p_sys->record.b_active == !!b_record )
808 return ARecordStart( s, psz_extension );
810 return ARecordStop( s );
812 static void ARecordWrite( stream_t *s, const uint8_t *p_buffer, size_t i_buffer )
814 stream_sys_t *p_sys = s->p_sys;
816 assert( p_sys->record.b_active );
820 const bool b_previous_error = p_sys->record.b_error;
821 const size_t i_written = fwrite( p_buffer, 1, i_buffer, p_sys->record.f );
823 p_sys->record.b_error = i_written != i_buffer;
825 /* TODO maybe a intf_UserError or something like that ? */
826 if( p_sys->record.b_error && !b_previous_error )
827 msg_Err( s, "Failed to record data (begin)" );
828 else if( !p_sys->record.b_error && b_previous_error )
829 msg_Err( s, "Failed to record data (end)" );
833 /****************************************************************************
835 ****************************************************************************/
836 static void AStreamPrebufferBlock( stream_t *s )
838 stream_sys_t *p_sys = s->p_sys;
839 access_t *p_access = p_sys->p_access;
844 msg_Dbg( s, "pre buffering" );
848 const int64_t i_date = mdate();
852 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE )
857 p_sys->stat.i_bytes = p_sys->block.i_size;
858 p_sys->stat.i_read_time = i_date - i_start;
859 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
860 (p_sys->stat.i_read_time + 1);
862 msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
863 "%"PRId64" kbytes/s",
865 p_sys->stat.i_read_time / INT64_C(1000000),
871 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
880 /* Append the block */
881 p_sys->block.i_size += b->i_buffer;
882 *p_sys->block.pp_last = b;
883 p_sys->block.pp_last = &b->p_next;
885 p_sys->stat.i_read_count++;
892 msg_Dbg( s, "received first data after %d ms",
893 (int)((i_first-i_start)/1000) );
897 p_sys->block.p_current = p_sys->block.p_first;
900 static int AStreamRefillBlock( stream_t *s );
902 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
904 stream_sys_t *p_sys = s->p_sys;
906 uint8_t *p_data = p_read;
907 uint8_t *p_record = p_data;
908 unsigned int i_data = 0;
911 if( p_sys->block.p_current == NULL )
914 if( p_sys->record.b_active && !p_data )
915 p_record = p_data = malloc( i_read );
919 /* seek within this stream if possible, else use plain old read and discard */
920 stream_sys_t *p_sys = s->p_sys;
921 access_t *p_access = p_sys->p_access;
923 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
925 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
928 while( i_data < i_read )
931 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
932 unsigned int i_copy = __MIN( (unsigned int)__MAX(i_current,0), i_read - i_data);
938 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
944 p_sys->block.i_offset += i_copy;
945 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
947 /* Current block is now empty, switch to next */
948 if( p_sys->block.p_current )
950 p_sys->block.i_offset = 0;
951 p_sys->block.p_current = p_sys->block.p_current->p_next;
953 /*Get a new block if needed */
954 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
961 if( p_sys->record.b_active )
963 if( i_data > 0 && p_record != NULL)
964 ARecordWrite( s, p_record, i_data );
969 p_sys->i_pos += i_data;
973 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
975 stream_sys_t *p_sys = s->p_sys;
977 unsigned int i_data = 0;
979 unsigned int i_offset;
981 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
983 /* We can directly give a pointer over our buffer */
984 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
986 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
990 /* We need to create a local copy */
991 if( p_sys->i_peek < i_read )
993 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
999 p_sys->i_peek = i_read;
1002 /* Fill enough data */
1003 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
1006 block_t **pp_last = p_sys->block.pp_last;
1008 if( AStreamRefillBlock( s ) ) break;
1010 /* Our buffer are probably filled enough, don't try anymore */
1011 if( pp_last == p_sys->block.pp_last ) break;
1014 /* Copy what we have */
1015 b = p_sys->block.p_current;
1016 i_offset = p_sys->block.i_offset;
1017 p_data = p_sys->p_peek;
1019 while( b && i_data < i_read )
1021 unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
1022 int i_copy = __MIN( i_current, i_read - i_data );
1024 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
1029 if( i_offset >= b->i_buffer )
1036 *pp_peek = p_sys->p_peek;
1040 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
1042 stream_sys_t *p_sys = s->p_sys;
1043 access_t *p_access = p_sys->p_access;
1044 int64_t i_offset = i_pos - p_sys->block.i_start;
1047 /* We already have thoses data, just update p_current/i_offset */
1048 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
1050 block_t *b = p_sys->block.p_first;
1053 while( i_current + b->i_buffer < i_offset )
1055 i_current += b->i_buffer;
1059 p_sys->block.p_current = b;
1060 p_sys->block.i_offset = i_offset - i_current;
1062 p_sys->i_pos = i_pos;
1067 /* We may need to seek or to read data */
1071 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1075 msg_Err( s, "backward seeking impossible (access not seekable)" );
1076 return VLC_EGENERIC;
1083 bool b_aseek, b_aseekfast;
1085 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1086 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
1091 msg_Warn( s, "%"PRId64" bytes need to be skipped "
1092 "(access non seekable)",
1093 i_offset - p_sys->block.i_size );
1097 int64_t i_skip = i_offset - p_sys->block.i_size;
1099 /* Avg bytes per packets */
1100 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
1101 /* TODO compute a seek cost instead of fixed threshold */
1102 int i_th = b_aseekfast ? 1 : 5;
1104 if( i_skip <= i_th * i_avg &&
1105 i_skip < STREAM_CACHE_SIZE )
1110 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
1111 b_seek, i_th*i_avg, i_skip );
1117 int64_t i_start, i_end;
1118 /* Do the access seek */
1120 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1124 block_ChainRelease( p_sys->block.p_first );
1127 p_sys->block.i_start = p_sys->i_pos = i_pos;
1128 p_sys->block.i_offset = 0;
1129 p_sys->block.p_current = NULL;
1130 p_sys->block.i_size = 0;
1131 p_sys->block.p_first = NULL;
1132 p_sys->block.pp_last = &p_sys->block.p_first;
1134 /* Refill a block */
1135 if( AStreamRefillBlock( s ) )
1136 return VLC_EGENERIC;
1139 p_sys->stat.i_seek_time += i_end - i_start;
1140 p_sys->stat.i_seek_count++;
1147 /* Read and skip enough data */
1148 if( AStreamRefillBlock( s ) )
1149 return VLC_EGENERIC;
1151 while( p_sys->block.p_current &&
1152 p_sys->i_pos + p_sys->block.p_current->i_buffer - p_sys->block.i_offset < i_pos )
1154 p_sys->i_pos += p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
1155 p_sys->block.p_current = p_sys->block.p_current->p_next;
1156 p_sys->block.i_offset = 0;
1159 while( p_sys->block.i_start + p_sys->block.i_size < i_pos );
1161 p_sys->block.i_offset = i_pos - p_sys->i_pos;
1162 p_sys->i_pos = i_pos;
1167 return VLC_EGENERIC;
1170 static int AStreamRefillBlock( stream_t *s )
1172 stream_sys_t *p_sys = s->p_sys;
1173 int64_t i_start, i_stop;
1177 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1178 p_sys->block.p_first != p_sys->block.p_current )
1180 block_t *b = p_sys->block.p_first;
1182 p_sys->block.i_start += b->i_buffer;
1183 p_sys->block.i_size -= b->i_buffer;
1184 p_sys->block.p_first = b->p_next;
1188 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1189 p_sys->block.p_current == p_sys->block.p_first &&
1190 p_sys->block.p_current->p_next ) /* At least 2 packets */
1192 /* Enough data, don't read more */
1196 /* Now read a new block */
1203 return VLC_EGENERIC;
1206 if( ( b = AReadBlock( s, &b_eof ) ) )
1209 return VLC_EGENERIC;
1216 /* Append the block */
1217 p_sys->block.i_size += b->i_buffer;
1218 *p_sys->block.pp_last = b;
1219 p_sys->block.pp_last = &b->p_next;
1222 if( p_sys->block.p_current == NULL )
1223 p_sys->block.p_current = b;
1226 p_sys->stat.i_bytes += b->i_buffer;
1227 p_sys->stat.i_read_time += i_stop - i_start;
1228 p_sys->stat.i_read_count++;
1237 /****************************************************************************
1239 ****************************************************************************/
1240 static int AStreamRefillStream( stream_t *s );
1242 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read )
1244 stream_sys_t *p_sys = s->p_sys;
1245 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1247 uint8_t *p_data = (uint8_t *)p_read;
1248 uint8_t *p_record = p_data;
1249 unsigned int i_data = 0;
1251 if( tk->i_start >= tk->i_end )
1254 if( p_sys->record.b_active && !p_data )
1255 p_record = p_data = malloc( i_read );
1257 if( p_data == NULL )
1259 /* seek within this stream if possible, else use plain old read and discard */
1260 stream_sys_t *p_sys = s->p_sys;
1261 access_t *p_access = p_sys->p_access;
1263 /* seeking after EOF is not what we want */
1264 if( !( p_access->info.b_eof ) )
1267 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1269 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1274 msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1275 " offset=%d end=%"PRId64,
1276 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1277 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1280 while( i_data < i_read )
1282 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1283 STREAM_CACHE_TRACK_SIZE;
1284 unsigned int i_current =
1285 __MAX(0,__MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1286 STREAM_CACHE_TRACK_SIZE - i_off ));
1287 int i_copy = __MIN( i_current, i_read - i_data );
1289 if( i_copy <= 0 ) break; /* EOF */
1292 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1295 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1299 p_sys->stream.i_offset += i_copy;
1301 /* Update pos now */
1302 p_sys->i_pos += i_copy;
1305 p_sys->stream.i_used += i_copy;
1306 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1307 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1309 if( p_sys->stream.i_used < i_read - i_data )
1310 p_sys->stream.i_used = __MIN( i_read - i_data, STREAM_READ_ATONCE * 10 );
1312 if( AStreamRefillStream( s ) )
1315 if( tk->i_start >= tk->i_end ) break;
1320 if( p_sys->record.b_active )
1322 if( i_data > 0 && p_record != NULL)
1323 ARecordWrite( s, p_record, i_data );
1331 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1333 stream_sys_t *p_sys = s->p_sys;
1334 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1337 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1340 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1341 "start=%"PRId64" offset=%d end=%"PRId64,
1342 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1343 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1346 /* Avoid problem, but that should *never* happen */
1347 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1348 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1350 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1352 if( p_sys->stream.i_used <= 1 )
1354 /* Be sure we will read something */
1355 p_sys->stream.i_used += i_read -
1356 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1358 if( AStreamRefillStream( s ) ) break;
1361 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1362 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1364 /* Now, direct pointer or a copy ? */
1365 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1366 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1368 *pp_peek = &tk->p_buffer[i_off];
1372 if( p_sys->i_peek < i_read )
1374 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1375 if( !p_sys->p_peek )
1380 p_sys->i_peek = i_read;
1383 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1384 STREAM_CACHE_TRACK_SIZE - i_off );
1385 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1386 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1388 *pp_peek = p_sys->p_peek;
1392 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1394 stream_sys_t *p_sys = s->p_sys;
1395 access_t *p_access = p_sys->p_access;
1403 msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1404 " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1405 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1406 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1407 p_sys->stream.i_offset,
1408 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1412 /* Seek in our current track ? */
1413 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1414 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1416 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1418 msg_Dbg( s, "AStreamSeekStream: current track" );
1420 p_sys->i_pos = i_pos;
1421 p_sys->stream.i_offset = i_pos - tk->i_start;
1423 /* If there is not enough data left in the track, refill */
1424 /* \todo How to get a correct value for
1425 * - refilling threshold
1426 * - how much to refill
1428 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1429 p_sys->stream.i_read_size )
1431 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1433 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1434 AStreamRefillStream( s );
1440 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1443 /* We can't do nothing */
1444 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1445 return VLC_EGENERIC;
1448 /* Date the current track */
1449 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1451 /* Try to reuse already read data */
1452 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1454 stream_track_t *tk = &p_sys->stream.tk[i];
1456 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1459 msg_Dbg( s, "AStreamSeekStream: reusing %d start=%"PRId64
1460 " end=%"PRId64, i, tk->i_start, tk->i_end );
1463 /* Seek at the end of the buffer */
1464 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1467 p_sys->i_pos = i_pos;
1468 p_sys->stream.i_tk = i;
1469 p_sys->stream.i_offset = i_pos - tk->i_start;
1471 if( p_sys->stream.i_used < 1024 )
1472 p_sys->stream.i_used = 1024;
1474 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1475 return VLC_EGENERIC;
1481 access_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1482 /* FIXME compute seek cost (instead of static 'stupid' value) */
1483 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1489 /* Search closest segment TODO */
1490 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1492 stream_track_t *tk = &p_sys->stream.tk[i];
1494 if( i_pos + i_maxth >= tk->i_start )
1496 msg_Dbg( s, "good segment before current pos, TODO" );
1498 if( i_pos - i_maxth <= tk->i_end )
1500 msg_Dbg( s, "good segment after current pos, TODO" );
1505 /* Nothing good, seek and choose oldest segment */
1506 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1507 p_sys->i_pos = i_pos;
1510 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1512 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1516 /* Reset the segment */
1517 p_sys->stream.i_tk = i_new;
1518 p_sys->stream.i_offset = 0;
1519 p_sys->stream.tk[i_new].i_start = i_pos;
1520 p_sys->stream.tk[i_new].i_end = i_pos;
1523 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1524 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1526 if( AStreamRefillStream( s ) )
1527 return VLC_EGENERIC;
1532 static int AStreamRefillStream( stream_t *s )
1534 stream_sys_t *p_sys = s->p_sys;
1535 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1537 /* We read but won't increase i_start after initial start + offset */
1539 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1540 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1541 bool b_read = false;
1542 int64_t i_start, i_stop;
1544 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1546 //#ifdef STREAM_DEBUG
1547 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1548 p_sys->stream.i_used, i_toread );
1552 while( i_toread > 0 )
1554 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1558 return VLC_EGENERIC;
1560 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1561 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1563 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1568 else if( i_read == 0 )
1571 return VLC_EGENERIC;
1577 tk->i_end += i_read;
1579 /* Windows of STREAM_CACHE_TRACK_SIZE */
1580 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1582 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1584 tk->i_start += i_invalid;
1585 p_sys->stream.i_offset -= i_invalid;
1589 p_sys->stream.i_used -= i_read;
1591 p_sys->stat.i_bytes += i_read;
1592 p_sys->stat.i_read_count++;
1596 p_sys->stat.i_read_time += i_stop - i_start;
1601 static void AStreamPrebufferStream( stream_t *s )
1603 stream_sys_t *p_sys = s->p_sys;
1604 access_t *p_access = p_sys->p_access;
1606 int64_t i_first = 0;
1609 msg_Dbg( s, "pre buffering" );
1613 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1615 int64_t i_date = mdate();
1618 if( s->b_die || tk->i_end >= STREAM_CACHE_PREBUFFER_SIZE )
1623 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1624 p_sys->stat.i_read_time = i_date - i_start;
1625 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1626 (p_sys->stat.i_read_time+1);
1628 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1629 "%"PRId64" kbytes/s",
1630 p_sys->stat.i_bytes,
1631 p_sys->stat.i_read_time / INT64_C(1000000),
1632 i_byterate / 1024 );
1637 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1638 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1639 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1642 else if( i_read == 0 )
1648 msg_Dbg( s, "received first data after %d ms",
1649 (int)((i_first-i_start)/1000) );
1652 tk->i_end += i_read;
1654 p_sys->stat.i_read_count++;
1658 /****************************************************************************
1660 ****************************************************************************/
1662 static int AStreamReadImmediate( stream_t *s, void *p_read, unsigned int i_read )
1664 stream_sys_t *p_sys = s->p_sys;
1665 uint8_t *p_data= (uint8_t*)p_read;
1666 uint8_t *p_record = p_data;
1667 unsigned int i_data;
1670 msg_Dbg( s, "AStreamReadImmediate p_read=%p i_read=%d",
1674 if( p_sys->record.b_active && !p_data )
1675 p_record = p_data = malloc( i_read );
1677 /* First, check if we already have some data in the buffer,
1678 * that we could copy directly */
1679 i_data = __MIN( stream_buffered_size( s ), i_read );
1683 msg_Dbg( s, "AStreamReadImmediate: copy %u from %p", i_data, stream_buffer( s ) );
1686 assert( i_data <= STREAM_CACHE_SIZE );
1690 memcpy( p_data, stream_buffer( s ), i_data );
1695 /* Now that we've read our buffer we don't need its i_copy bytes */
1696 stream_buffer_empty( s, i_data );
1698 /* Now check if we have still to really read some data */
1699 while( i_data < i_read )
1701 const unsigned int i_to_read = i_read - i_data;
1706 i_result = AReadStream( s, p_data, i_to_read );
1710 void *p_dummy = malloc( i_to_read );
1715 i_result = AReadStream( s, p_dummy, i_to_read );
1722 p_sys->i_pos += i_data;
1728 if( p_sys->record.b_active )
1730 if( i_data > 0 && p_record != NULL)
1731 ARecordWrite( s, p_record, i_data );
1738 static int AStreamPeekImmediate( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1741 msg_Dbg( s, "AStreamPeekImmediate: %d size=%"PRId64,
1742 i_read, size_buffered_size( s ) );
1745 /* Avoid problem, but that shouldn't happen
1746 * FIXME yes it can */
1747 if( i_read > STREAM_CACHE_SIZE / 2 )
1748 i_read = STREAM_CACHE_SIZE / 2;
1750 int i_to_read = i_read - stream_buffered_size( s );
1754 msg_Dbg( s, "AStreamPeekImmediate: Reading %d",
1757 i_to_read = AReadStream( s, stream_buffer( s ) + stream_buffered_size( s ),
1761 stream_buffer_fill( s, i_to_read );
1764 *pp_peek = stream_buffer( s );
1766 return __MIN( stream_buffered_size( s ), i_read );
1769 static int AStreamSeekImmediate( stream_t *s, int64_t i_pos )
1771 stream_sys_t *p_sys = s->p_sys;
1772 access_t *p_access = p_sys->p_access;
1776 msg_Dbg( s, "AStreamSeekImmediate to %"PRId64" pos=%"PRId64
1777 i_pos, p_sys->i_pos );
1780 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1783 /* We can't do nothing */
1784 msg_Dbg( s, "AStreamSeekImmediate: can't seek" );
1785 return VLC_EGENERIC;
1788 /* Just reset our buffer */
1789 stream_buffer_empty( s, stream_buffered_size( s ) );
1791 if( ASeek( s, i_pos ) )
1792 return VLC_EGENERIC;
1797 /****************************************************************************
1799 ****************************************************************************/
1801 * Read from the stream untill first newline.
1802 * \param s Stream handle to read from
1803 * \return A pointer to the allocated output string. You need to free this when you are done.
1805 #define STREAM_PROBE_LINE 2048
1806 #define STREAM_LINE_MAX (2048*100)
1807 char *stream_ReadLine( stream_t *s )
1809 char *p_line = NULL;
1810 int i_line = 0, i_read = 0;
1812 while( i_read < STREAM_LINE_MAX )
1815 const uint8_t *p_data;
1819 /* Probe new data */
1820 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1821 if( i_data <= 0 ) break; /* No more data */
1824 i_pos = stream_Tell( s );
1825 if( i_pos == 0 && i_data >= 3 )
1828 const char *psz_encoding = NULL;
1830 if( !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
1832 psz_encoding = "UTF-8";
1835 else if( !memcmp( p_data, "\xFF\xFE", 2 ) )
1837 psz_encoding = "UTF-16LE";
1838 s->b_little_endian = true;
1839 s->i_char_width = 2;
1842 else if( !memcmp( p_data, "\xFE\xFF", 2 ) )
1844 psz_encoding = "UTF-16BE";
1845 s->i_char_width = 2;
1849 /* Seek past the BOM */
1852 stream_Seek( s, i_bom_size );
1853 p_data += i_bom_size;
1854 i_data -= i_bom_size;
1857 /* Open the converter if we need it */
1858 if( psz_encoding != NULL )
1860 input_thread_t *p_input;
1861 msg_Dbg( s, "%s BOM detected", psz_encoding );
1862 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1863 if( s->i_char_width > 1 )
1865 s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1866 if( s->conv == (vlc_iconv_t)-1 )
1868 msg_Err( s, "iconv_open failed" );
1871 if( p_input != NULL)
1873 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1874 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1875 vlc_object_release( p_input );
1880 if( i_data % s->i_char_width )
1882 /* keep i_char_width boundary */
1883 i_data = i_data - ( i_data % s->i_char_width );
1884 msg_Warn( s, "the read is not i_char_width compatible");
1890 /* Check if there is an EOL */
1891 if( s->i_char_width == 1 )
1893 /* UTF-8: 0A <LF> */
1894 psz_eol = memchr( p_data, '\n', i_data );
1898 const uint8_t *p = p_data;
1899 const uint8_t *p_last = p + i_data - s->i_char_width;
1901 if( s->i_char_width == 2 )
1903 if( s->b_little_endian == true)
1905 /* UTF-16LE: 0A 00 <LF> */
1906 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1911 /* UTF-16BE: 00 0A <LF> */
1912 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1923 psz_eol = (char *)p + ( s->i_char_width - 1 );
1929 i_data = (psz_eol - (char *)p_data) + 1;
1930 p_line = realloc( p_line, i_line + i_data + s->i_char_width ); /* add \0 */
1933 i_data = stream_Read( s, &p_line[i_line], i_data );
1934 if( i_data <= 0 ) break; /* Hmmm */
1935 i_line += i_data - s->i_char_width; /* skip \n */;
1938 /* We have our line */
1942 /* Read data (+1 for easy \0 append) */
1943 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->i_char_width );
1946 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1947 if( i_data <= 0 ) break; /* Hmmm */
1955 for( j = 0; j < s->i_char_width; j++ )
1957 p_line[i_line + j] = '\0';
1959 i_line += s->i_char_width; /* the added \0 */
1960 if( s->i_char_width > 1 )
1962 size_t i_in = 0, i_out = 0;
1963 const char * p_in = NULL;
1964 char * p_out = NULL;
1965 char * psz_new_line = NULL;
1968 psz_new_line = malloc( i_line );
1969 if( psz_new_line == NULL )
1971 i_in = i_out = (size_t)i_line;
1973 p_out = psz_new_line;
1975 if( vlc_iconv( s->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1977 msg_Err( s, "iconv failed" );
1978 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1981 p_line = psz_new_line;
1982 i_line = (size_t)i_line - i_out; /* does not include \0 */
1985 /* Remove trailing LF/CR */
1986 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1987 p_line[i_line-2] == '\n') ) i_line--;
1989 /* Make sure the \0 is there */
1990 p_line[i_line-1] = '\0';
1997 /* We failed to read any data, probably EOF */
1999 if( s->conv != (vlc_iconv_t)(-1) ) vlc_iconv_close( s->conv );
2003 /****************************************************************************
2004 * Access reading/seeking wrappers to handle concatenated streams.
2005 ****************************************************************************/
2006 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read )
2008 stream_sys_t *p_sys = s->p_sys;
2009 access_t *p_access = p_sys->p_access;
2010 input_thread_t *p_input = NULL;
2011 int i_read_orig = i_read;
2014 if( s->p_parent && s->p_parent->p_parent &&
2015 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
2016 p_input = (input_thread_t *)s->p_parent->p_parent;
2018 if( !p_sys->i_list )
2020 i_read = p_access->pf_read( p_access, p_read, i_read );
2021 if( p_access->b_die )
2022 vlc_object_kill( s );
2025 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2026 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
2028 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2029 (float)i_total, NULL );
2030 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2031 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2036 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
2038 if( p_access->b_die )
2039 vlc_object_kill( s );
2041 /* If we reached an EOF then switch to the next stream in the list */
2042 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
2044 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
2045 access_t *p_list_access;
2047 msg_Dbg( s, "opening input `%s'", psz_name );
2049 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
2051 if( !p_list_access ) return 0;
2053 if( p_sys->p_list_access != p_access )
2054 access_Delete( p_sys->p_list_access );
2056 p_sys->p_list_access = p_list_access;
2058 /* We have to read some data */
2059 return AReadStream( s, p_read, i_read_orig );
2062 /* Update read bytes in input */
2065 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2066 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
2067 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2068 (float)i_total, NULL );
2069 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2070 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2075 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
2077 stream_sys_t *p_sys = s->p_sys;
2078 access_t *p_access = p_sys->p_access;
2079 input_thread_t *p_input = NULL;
2084 if( s->p_parent && s->p_parent->p_parent &&
2085 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
2086 p_input = (input_thread_t *)s->p_parent->p_parent;
2088 if( !p_sys->i_list )
2090 p_block = p_access->pf_block( p_access );
2091 if( p_access->b_die )
2092 vlc_object_kill( s );
2093 if( pb_eof ) *pb_eof = p_access->info.b_eof;
2094 if( p_input && p_block && libvlc_stats (p_access) )
2096 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2097 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
2098 p_block->i_buffer, &i_total );
2099 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2100 (float)i_total, NULL );
2101 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2102 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2107 p_block = p_sys->p_list_access->pf_block( p_sys->p_list_access );
2108 if( p_access->b_die )
2109 vlc_object_kill( s );
2110 b_eof = p_sys->p_list_access->info.b_eof;
2111 if( pb_eof ) *pb_eof = b_eof;
2113 /* If we reached an EOF then switch to the next stream in the list */
2114 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
2116 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
2117 access_t *p_list_access;
2119 msg_Dbg( s, "opening input `%s'", psz_name );
2121 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
2123 if( !p_list_access ) return 0;
2125 if( p_sys->p_list_access != p_access )
2126 access_Delete( p_sys->p_list_access );
2128 p_sys->p_list_access = p_list_access;
2130 /* We have to read some data */
2131 return AReadBlock( s, pb_eof );
2137 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2138 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
2139 p_block->i_buffer, &i_total );
2140 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2141 (float)i_total, NULL );
2142 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
2144 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2150 static int ASeek( stream_t *s, int64_t i_pos )
2152 stream_sys_t *p_sys = s->p_sys;
2153 access_t *p_access = p_sys->p_access;
2155 /* Check which stream we need to access */
2161 access_t *p_list_access = 0;
2163 for( i = 0; i < p_sys->i_list - 1; i++ )
2165 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
2166 i_size += p_sys->list[i]->i_size;
2168 psz_name = p_sys->list[i]->psz_path;
2170 if( i != p_sys->i_list_index )
2171 msg_Dbg( s, "opening input `%s'", psz_name );
2173 if( i != p_sys->i_list_index && i != 0 )
2176 access_New( s, p_access->psz_access, "", psz_name );
2178 else if( i != p_sys->i_list_index )
2180 p_list_access = p_access;
2185 if( p_sys->p_list_access != p_access )
2186 access_Delete( p_sys->p_list_access );
2188 p_sys->p_list_access = p_list_access;
2191 p_sys->i_list_index = i;
2192 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
2196 return p_access->pf_seek( p_access, i_pos );
2201 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
2202 * "p_read" is NULL then data are skipped instead of read. The return
2203 * value is the real numbers of bytes read/skip. If this value is less
2204 * than i_read that means that it's the end of the stream.
2206 int stream_Read( stream_t *s, void *p_read, int i_read )
2208 return s->pf_read( s, p_read, i_read );
2212 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
2213 * \return The real numbers of valid bytes, if it's less
2214 * or equal to 0, *pp_peek is invalid.
2215 * \note pp_peek is a pointer to internal buffer and it will be invalid as
2216 * soons as other stream_* functions are called.
2217 * \note Due to input limitation, it could be less than i_peek without meaning
2218 * the end of the stream (but only when you have i_peek >=
2219 * p_input->i_bufsize)
2221 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
2223 return s->pf_peek( s, pp_peek, i_peek );
2227 * Use to control the "stream_t *". Look at #stream_query_e for
2228 * possible "i_query" value and format arguments. Return VLC_SUCCESS
2229 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
2231 int stream_vaControl( stream_t *s, int i_query, va_list args )
2233 return s->pf_control( s, i_query, args );
2239 void stream_Delete( stream_t *s )
2244 int stream_Control( stream_t *s, int i_query, ... )
2250 return VLC_EGENERIC;
2252 va_start( args, i_query );
2253 i_result = s->pf_control( s, i_query, args );
2259 * Read "i_size" bytes and store them in a block_t.
2260 * It always read i_size bytes unless you are at the end of the stream
2261 * where it return what is available.
2263 block_t *stream_Block( stream_t *s, int i_size )
2265 if( i_size <= 0 ) return NULL;
2267 /* emulate block read */
2268 block_t *p_bk = block_New( s, i_size );
2271 int i_read = stream_Read( s, p_bk->p_buffer, i_size );
2274 p_bk->i_buffer = i_read;
2277 block_Release( p_bk );