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_charset.h>
32 #include <vlc_strings.h>
37 #include "input_internal.h"
42 * - tune the 2 methods (block/stream)
43 * - compute cost for seek
44 * - improve stream mode seeking with closest segments
46 * - Maybe remove (block/stream) in favour of immediate
51 * One linked list of data read
53 * More complex scheme using mutliple track to avoid seeking
54 * - using directly the access (only indirection for peeking).
55 * This method is known to introduce much less latency.
56 * It should probably defaulted (instead of the stream method (2)).
59 /* How many tracks we have, currently only used for stream mode */
60 #ifdef OPTIMIZE_MEMORY
61 # define STREAM_CACHE_TRACK 1
62 /* Max size of our cache 128Ko per track */
63 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
65 # define STREAM_CACHE_TRACK 3
66 /* Max size of our cache 4Mo per track */
67 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
70 /* How many data we try to prebuffer */
71 #define STREAM_CACHE_PREBUFFER_SIZE (32767)
72 /* Maximum time we take to pre-buffer */
73 #define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
75 /* Method1: Simple, for pf_block.
76 * We get blocks and put them in the linked list.
77 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
79 #define STREAM_DATA_WAIT 40000 /* Time between before a pf_block retry */
81 /* Method2: A bit more complex, for pf_read
82 * - We use ring buffers, only one if unseekable, all if seekable
83 * - Upon seek date current ring, then search if one ring match the pos,
84 * yes: switch to it, seek the access to match the end of the ring
85 * no: search the ring with i_end the closer to i_pos,
86 * if close enough, read data and use this ring
87 * else use the oldest ring, seek and use it.
89 * TODO: - with access non seekable: use all space available for only one ring, but
90 * we have to support seekable/non-seekable switch on the fly.
91 * - compute a good value for i_read_size
94 #define STREAM_READ_ATONCE 32767
95 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
115 typedef enum stream_read_method_t
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 */
205 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read );
206 static int AStreamPeekBlock( stream_t *s, const uint8_t **p_peek, unsigned int i_read );
207 static int AStreamSeekBlock( stream_t *s, int64_t i_pos );
208 static void AStreamPrebufferBlock( stream_t *s );
209 static block_t *AReadBlock( stream_t *s, bool *pb_eof );
212 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read );
213 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read );
214 static int AStreamSeekStream( stream_t *s, int64_t i_pos );
215 static void AStreamPrebufferStream( stream_t *s );
216 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read );
219 static int AStreamReadImmediate( stream_t *s, void *p_read, unsigned int i_read );
220 static int AStreamPeekImmediate( stream_t *s, const uint8_t **pp_peek, unsigned int i_read );
221 static int AStreamSeekImmediate( stream_t *s, int64_t i_pos );
224 static int AStreamControl( stream_t *s, int i_query, va_list );
225 static void AStreamDestroy( stream_t *s );
226 static void UStreamDestroy( stream_t *s );
227 static int ASeek( stream_t *s, int64_t i_pos );
228 static int ARecordSetState( stream_t *s, bool b_record, const char *psz_extension );
229 static void ARecordWrite( stream_t *s, const uint8_t *p_buffer, size_t i_buffer );
231 /****************************************************************************
233 ****************************************************************************/
235 static inline int64_t stream_buffered_size( stream_t *s )
237 return s->p_sys->immediate.i_end;
240 static inline void stream_buffer_empty( stream_t *s, int length )
242 length = __MAX( stream_buffered_size( s ), length );
245 memmove( s->p_sys->immediate.p_buffer,
246 s->p_sys->immediate.p_buffer + length,
247 stream_buffered_size( s ) - length );
249 s->p_sys->immediate.i_end -= length;
252 static inline void stream_buffer_fill( stream_t *s, int length )
254 s->p_sys->immediate.i_end += length;
257 static inline uint8_t * stream_buffer( stream_t *s )
259 return s->p_sys->immediate.p_buffer;
262 /****************************************************************************
263 * stream_UrlNew: create a stream from a access
264 ****************************************************************************/
265 stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
267 const char *psz_access, *psz_demux;
275 char psz_dup[strlen( psz_url ) + 1];
276 strcpy( psz_dup, psz_url );
277 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
279 /* Now try a real access */
280 p_access = access_New( p_parent, psz_access, psz_demux, psz_path );
282 if( p_access == NULL )
284 msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
288 if( !( p_res = stream_AccessNew( p_access, true ) ) )
290 access_Delete( p_access );
294 p_res->pf_destroy = UStreamDestroy;
298 stream_t *stream_AccessNew( access_t *p_access, bool b_quick )
300 stream_t *s = vlc_stream_create( VLC_OBJECT(p_access) );
302 char *psz_list = NULL;
304 if( !s ) return NULL;
306 /* Attach it now, needed for b_die */
307 vlc_object_attach( s, p_access );
309 s->pf_read = NULL; /* Set up later */
311 s->pf_control = AStreamControl;
312 s->pf_destroy = AStreamDestroy;
314 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
318 /* UTF16 and UTF32 text file conversion */
320 s->b_little_endian = false;
321 s->conv = (vlc_iconv_t)(-1);
324 p_sys->p_access = p_access;
325 if( p_access->pf_block )
326 p_sys->method = Block;
327 else if (var_CreateGetBool( s, "use-stream-immediate"))
328 p_sys->method = Immediate;
330 p_sys->method = Stream;
332 p_sys->record.b_active = false;
334 p_sys->i_pos = p_access->info.i_pos;
337 access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
338 p_sys->stat.i_bytes = 0;
339 p_sys->stat.i_read_time = 0;
340 p_sys->stat.i_read_count = 0;
341 p_sys->stat.i_seek_count = 0;
342 p_sys->stat.i_seek_time = 0;
346 p_sys->i_list_index = 0;
347 p_sys->p_list_access = 0;
349 p_sys->b_quick = b_quick;
351 /* Get the additional list of inputs if any (for concatenation) */
352 if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
354 access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
355 if( p_entry == NULL )
357 char *psz_name, *psz_parser = psz_name = psz_list;
359 p_sys->p_list_access = p_access;
360 p_entry->i_size = p_access->info.i_size;
361 p_entry->psz_path = strdup( p_access->psz_path );
362 if( p_entry->psz_path == NULL )
367 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
368 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
369 p_entry->psz_path, p_access->info.i_size );
371 while( psz_name && *psz_name )
373 psz_parser = strchr( psz_name, ',' );
374 if( psz_parser ) *psz_parser = 0;
376 psz_name = strdup( psz_name );
379 access_t *p_tmp = access_New( p_access, p_access->psz_access,
384 psz_name = psz_parser;
385 if( psz_name ) psz_name++;
389 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
390 psz_name, p_tmp->info.i_size );
392 p_entry = malloc( sizeof(access_entry_t) );
393 if( p_entry == NULL )
395 p_entry->i_size = p_tmp->info.i_size;
396 p_entry->psz_path = psz_name;
397 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
399 access_Delete( p_tmp );
402 psz_name = psz_parser;
403 if( psz_name ) psz_name++;
406 FREENULL( psz_list );
410 p_sys->p_peek = NULL;
412 if( p_sys->method == Block )
414 msg_Dbg( s, "Using AStream*Block" );
415 s->pf_read = AStreamReadBlock;
416 s->pf_peek = AStreamPeekBlock;
418 /* Init all fields of p_sys->block */
419 p_sys->block.i_start = p_sys->i_pos;
420 p_sys->block.i_offset = 0;
421 p_sys->block.p_current = NULL;
422 p_sys->block.i_size = 0;
423 p_sys->block.p_first = NULL;
424 p_sys->block.pp_last = &p_sys->block.p_first;
426 /* Do the prebuffering */
427 AStreamPrebufferBlock( s );
429 if( p_sys->block.i_size <= 0 )
431 msg_Err( s, "cannot pre fill buffer" );
435 else if (p_sys->method == Immediate)
437 msg_Dbg( s, "Using AStream*Immediate" );
439 s->pf_read = AStreamReadImmediate;
440 s->pf_peek = AStreamPeekImmediate;
442 /* Allocate/Setup our tracks (useful to peek)*/
443 p_sys->immediate.i_end = 0;
444 p_sys->immediate.p_buffer = malloc( STREAM_CACHE_SIZE );
446 msg_Dbg( s, "p_buffer %p-%p", p_sys->immediate.p_buffer,
447 p_sys->immediate.p_buffer + STREAM_CACHE_SIZE );
449 if( p_sys->immediate.p_buffer == NULL )
451 msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
456 else /* ( p_sys->method == Stream ) */
460 msg_Dbg( s, "Using AStream*Stream" );
462 s->pf_read = AStreamReadStream;
463 s->pf_peek = AStreamPeekStream;
465 /* Allocate/Setup our tracks */
466 p_sys->stream.i_offset = 0;
467 p_sys->stream.i_tk = 0;
468 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
469 if( p_sys->stream.p_buffer == NULL )
471 msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
475 p_sys->stream.i_used = 0;
476 access_Control( p_access, ACCESS_GET_MTU,
477 &p_sys->stream.i_read_size );
478 if( p_sys->stream.i_read_size <= 0 )
479 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
480 else if( p_sys->stream.i_read_size <= 256 )
481 p_sys->stream.i_read_size = 256;
483 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
485 p_sys->stream.tk[i].i_date = 0;
486 p_sys->stream.tk[i].i_start = p_sys->i_pos;
487 p_sys->stream.tk[i].i_end = p_sys->i_pos;
488 p_sys->stream.tk[i].p_buffer=
489 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
492 /* Do the prebuffering */
493 AStreamPrebufferStream( s );
495 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
497 msg_Err( s, "cannot pre fill buffer" );
505 if( p_sys->method == Block )
511 free( p_sys->stream.p_buffer );
513 while( p_sys->i_list > 0 )
514 free( p_sys->list[--(p_sys->i_list)] );
518 vlc_object_detach( s );
519 vlc_object_release( s );
523 /****************************************************************************
525 ****************************************************************************/
526 static void AStreamDestroy( stream_t *s )
528 stream_sys_t *p_sys = s->p_sys;
530 vlc_object_detach( s );
532 if( p_sys->record.b_active )
533 ARecordSetState( s, false, NULL );
535 if( p_sys->method == Block )
536 block_ChainRelease( p_sys->block.p_first );
537 else if ( p_sys->method == Immediate )
538 free( p_sys->immediate.p_buffer );
540 free( p_sys->stream.p_buffer );
542 free( p_sys->p_peek );
544 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
545 access_Delete( p_sys->p_list_access );
547 while( p_sys->i_list-- )
549 free( p_sys->list[p_sys->i_list]->psz_path );
550 free( p_sys->list[p_sys->i_list] );
556 vlc_object_release( s );
559 static void UStreamDestroy( stream_t *s )
561 access_t *p_access = (access_t *)s->p_parent;
563 access_Delete( p_access );
566 /****************************************************************************
567 * stream_AccessReset:
568 ****************************************************************************/
569 void stream_AccessReset( stream_t *s )
571 stream_sys_t *p_sys = s->p_sys;
573 p_sys->i_pos = p_sys->p_access->info.i_pos;
575 if( p_sys->method == Block )
577 block_ChainRelease( p_sys->block.p_first );
579 /* Init all fields of p_sys->block */
580 p_sys->block.i_start = p_sys->i_pos;
581 p_sys->block.i_offset = 0;
582 p_sys->block.p_current = NULL;
583 p_sys->block.i_size = 0;
584 p_sys->block.p_first = NULL;
585 p_sys->block.pp_last = &p_sys->block.p_first;
587 /* Do the prebuffering */
588 AStreamPrebufferBlock( s );
590 else if( p_sys->method == Immediate )
592 stream_buffer_empty( s, stream_buffered_size( s ) );
594 else /* ( p_sys->method == Stream ) */
598 /* Setup our tracks */
599 p_sys->stream.i_offset = 0;
600 p_sys->stream.i_tk = 0;
601 p_sys->stream.i_used = 0;
603 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
605 p_sys->stream.tk[i].i_date = 0;
606 p_sys->stream.tk[i].i_start = p_sys->i_pos;
607 p_sys->stream.tk[i].i_end = p_sys->i_pos;
610 /* Do the prebuffering */
611 AStreamPrebufferStream( s );
615 /****************************************************************************
616 * stream_AccessUpdate:
617 ****************************************************************************/
618 void stream_AccessUpdate( stream_t *s )
620 stream_sys_t *p_sys = s->p_sys;
622 p_sys->i_pos = p_sys->p_access->info.i_pos;
627 for( i = 0; i < p_sys->i_list_index; i++ )
629 p_sys->i_pos += p_sys->list[i]->i_size;
634 /****************************************************************************
636 ****************************************************************************/
637 static int AStreamControl( stream_t *s, int i_query, va_list args )
639 stream_sys_t *p_sys = s->p_sys;
640 access_t *p_access = p_sys->p_access;
644 const char *psz_string;
645 int64_t *pi_64, i_64;
650 case STREAM_GET_SIZE:
651 pi_64 = (int64_t*)va_arg( args, int64_t * );
652 if( s->p_sys->i_list )
656 for( i = 0; i < s->p_sys->i_list; i++ )
657 *pi_64 += s->p_sys->list[i]->i_size;
660 *pi_64 = p_access->info.i_size;
663 case STREAM_CAN_SEEK:
664 p_bool = (bool*)va_arg( args, bool * );
665 access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
668 case STREAM_CAN_FASTSEEK:
669 p_bool = (bool*)va_arg( args, bool * );
670 access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
673 case STREAM_GET_POSITION:
674 pi_64 = (int64_t*)va_arg( args, int64_t * );
675 *pi_64 = p_sys->i_pos;
678 case STREAM_SET_POSITION:
679 i_64 = (int64_t)va_arg( args, int64_t );
680 if( p_sys->method == Block )
681 return AStreamSeekBlock( s, i_64 );
682 else if( p_sys->method == Immediate )
683 return AStreamSeekImmediate( s, i_64 );
684 else /* ( p_sys->method == Stream ) */
685 return AStreamSeekStream( s, i_64 );
690 case STREAM_CONTROL_ACCESS:
691 i_int = (int) va_arg( args, int );
692 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
693 i_int != ACCESS_SET_PRIVATE_ID_CA &&
694 i_int != ACCESS_GET_PRIVATE_ID_STATE )
696 msg_Err( s, "Hey, what are you thinking ?"
697 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
700 return access_vaControl( p_access, i_int, args );
702 case STREAM_GET_CONTENT_TYPE:
703 return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
704 va_arg( args, char ** ) );
705 case STREAM_SET_RECORD_STATE:
706 b_bool = (bool)va_arg( args, int );
709 psz_string = (const char*)va_arg( args, const char* );
710 return ARecordSetState( s, b_bool, psz_string );
713 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
719 /****************************************************************************
720 * ARecord*: record stream functions
721 ****************************************************************************/
723 /* TODO FIXME nearly the same logic that snapshot code */
724 static char *ARecordGetFileName( stream_t *s, const char *psz_path, const char *psz_prefix, const char *psz_extension )
729 path = utf8_opendir( psz_path );
734 const char *psz_prefix = "vlc-record-%Y-%m-%d-%H:%M:%S-$p"; // TODO allow conf ?
735 char *psz_tmp = str_format( s, psz_prefix );
739 filename_sanitize( psz_tmp );
740 if( asprintf( &psz_file, "%s"DIR_SEP"%s.%s",
741 psz_path, psz_tmp, psz_extension ) < 0 )
748 psz_file = str_format( s, psz_path );
749 path_sanitize( psz_file );
754 static int ARecordStart( stream_t *s, const char *psz_extension )
756 stream_sys_t *p_sys = s->p_sys;
764 psz_extension = "dat";
767 char *psz_path = var_CreateGetString( s, "input-record-path" );
768 if( !psz_path || *psz_path == '\0' )
771 psz_path = strdup( config_GetHomeDir() );
778 * TODO allow prefix configuration */
779 psz_file = ARecordGetFileName( s, psz_path, "vlc-record-%Y-%m-%d-%H:%M:%S-$p", psz_extension );
786 f = utf8_fopen( psz_file, "wb" );
792 msg_Dbg( s, "Recording into %s", psz_file );
797 p_sys->record.b_active = true;
800 static int ARecordStop( stream_t *s )
802 stream_sys_t *p_sys = s->p_sys;
804 assert( p_sys->record.b_active );
806 msg_Dbg( s, "Recording completed" );
807 fclose( p_sys->record.f );
808 p_sys->record.b_active = false;
812 static int ARecordSetState( stream_t *s, bool b_record, const char *psz_extension )
814 stream_sys_t *p_sys = s->p_sys;
816 if( !!p_sys->record.b_active == !!b_record )
820 return ARecordStart( s, psz_extension );
822 return ARecordStop( s );
824 static void ARecordWrite( stream_t *s, const uint8_t *p_buffer, size_t i_buffer )
826 stream_sys_t *p_sys = s->p_sys;
828 assert( p_sys->record.b_active );
831 fwrite( p_buffer, 1, i_buffer, p_sys->record.f );
834 /****************************************************************************
836 ****************************************************************************/
837 static void AStreamPrebufferBlock( stream_t *s )
839 stream_sys_t *p_sys = s->p_sys;
840 access_t *p_access = p_sys->p_access;
845 msg_Dbg( s, "pre buffering" );
849 int64_t i_date = mdate();
853 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
854 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
859 p_sys->stat.i_bytes = p_sys->block.i_size;
860 p_sys->stat.i_read_time = i_date - i_start;
861 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
862 (p_sys->stat.i_read_time + 1);
864 msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
865 "%"PRId64" kbytes/s",
867 p_sys->stat.i_read_time / INT64_C(1000000),
873 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
877 msleep( STREAM_DATA_WAIT );
883 /* Append the block */
884 p_sys->block.i_size += b->i_buffer;
885 *p_sys->block.pp_last = b;
886 p_sys->block.pp_last = &b->p_next;
888 p_sys->stat.i_read_count++;
892 if( p_access->info.b_prebuffered )
894 /* Access has already prebufferred - update stats and exit */
895 p_sys->stat.i_bytes = p_sys->block.i_size;
896 p_sys->stat.i_read_time = mdate() - i_start;
903 msg_Dbg( s, "received first data for our buffer");
908 p_sys->block.p_current = p_sys->block.p_first;
911 static int AStreamRefillBlock( stream_t *s );
913 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
915 stream_sys_t *p_sys = s->p_sys;
917 uint8_t *p_data= (uint8_t*)p_read;
918 unsigned int i_data = 0;
921 if( p_sys->block.p_current == NULL )
926 /* seek within this stream if possible, else use plain old read and discard */
927 stream_sys_t *p_sys = s->p_sys;
928 access_t *p_access = p_sys->p_access;
930 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
932 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
935 while( i_data < i_read )
938 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
939 unsigned int i_copy = __MIN( (unsigned int)__MAX(i_current,0), i_read - i_data);
945 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
951 p_sys->block.i_offset += i_copy;
952 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
954 /* Current block is now empty, switch to next */
955 if( p_sys->block.p_current )
957 p_sys->block.i_offset = 0;
958 p_sys->block.p_current = p_sys->block.p_current->p_next;
960 /*Get a new block if needed */
961 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
968 if( p_sys->record.b_active && i_data > 0 )
969 ARecordWrite( s, p_read, i_data );
971 p_sys->i_pos += i_data;
975 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
977 stream_sys_t *p_sys = s->p_sys;
979 unsigned int i_data = 0;
981 unsigned int i_offset;
983 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
985 /* We can directly give a pointer over our buffer */
986 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
988 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
992 /* We need to create a local copy */
993 if( p_sys->i_peek < i_read )
995 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1001 p_sys->i_peek = i_read;
1004 /* Fill enough data */
1005 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
1008 block_t **pp_last = p_sys->block.pp_last;
1010 if( AStreamRefillBlock( s ) ) break;
1012 /* Our buffer are probably filled enough, don't try anymore */
1013 if( pp_last == p_sys->block.pp_last ) break;
1016 /* Copy what we have */
1017 b = p_sys->block.p_current;
1018 i_offset = p_sys->block.i_offset;
1019 p_data = p_sys->p_peek;
1021 while( b && i_data < i_read )
1023 unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
1024 int i_copy = __MIN( i_current, i_read - i_data );
1026 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
1031 if( i_offset >= b->i_buffer )
1038 *pp_peek = p_sys->p_peek;
1042 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
1044 stream_sys_t *p_sys = s->p_sys;
1045 access_t *p_access = p_sys->p_access;
1046 int64_t i_offset = i_pos - p_sys->block.i_start;
1049 /* We already have thoses data, just update p_current/i_offset */
1050 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
1052 block_t *b = p_sys->block.p_first;
1055 while( i_current + b->i_buffer < i_offset )
1057 i_current += b->i_buffer;
1061 p_sys->block.p_current = b;
1062 p_sys->block.i_offset = i_offset - i_current;
1064 p_sys->i_pos = i_pos;
1069 /* We may need to seek or to read data */
1073 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1077 msg_Err( s, "backward seeking impossible (access not seekable)" );
1078 return VLC_EGENERIC;
1085 bool b_aseek, b_aseekfast;
1087 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1088 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
1093 msg_Warn( s, "%"PRId64" bytes need to be skipped "
1094 "(access non seekable)",
1095 i_offset - p_sys->block.i_size );
1099 int64_t i_skip = i_offset - p_sys->block.i_size;
1101 /* Avg bytes per packets */
1102 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
1103 /* TODO compute a seek cost instead of fixed threshold */
1104 int i_th = b_aseekfast ? 1 : 5;
1106 if( i_skip <= i_th * i_avg &&
1107 i_skip < STREAM_CACHE_SIZE )
1112 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
1113 b_seek, i_th*i_avg, i_skip );
1119 int64_t i_start, i_end;
1120 /* Do the access seek */
1122 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1126 block_ChainRelease( p_sys->block.p_first );
1129 p_sys->block.i_start = p_sys->i_pos = i_pos;
1130 p_sys->block.i_offset = 0;
1131 p_sys->block.p_current = NULL;
1132 p_sys->block.i_size = 0;
1133 p_sys->block.p_first = NULL;
1134 p_sys->block.pp_last = &p_sys->block.p_first;
1136 /* Refill a block */
1137 if( AStreamRefillBlock( s ) )
1138 return VLC_EGENERIC;
1141 p_sys->stat.i_seek_time += i_end - i_start;
1142 p_sys->stat.i_seek_count++;
1149 /* Read and skip enough data */
1150 if( AStreamRefillBlock( s ) )
1151 return VLC_EGENERIC;
1153 while( p_sys->block.p_current &&
1154 p_sys->i_pos + p_sys->block.p_current->i_buffer - p_sys->block.i_offset < i_pos )
1156 p_sys->i_pos += p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
1157 p_sys->block.p_current = p_sys->block.p_current->p_next;
1158 p_sys->block.i_offset = 0;
1161 while( p_sys->block.i_start + p_sys->block.i_size < i_pos );
1163 p_sys->block.i_offset = i_pos - p_sys->i_pos;
1164 p_sys->i_pos = i_pos;
1169 return VLC_EGENERIC;
1172 static int AStreamRefillBlock( stream_t *s )
1174 stream_sys_t *p_sys = s->p_sys;
1175 int64_t i_start, i_stop;
1179 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1180 p_sys->block.p_first != p_sys->block.p_current )
1182 block_t *b = p_sys->block.p_first;
1184 p_sys->block.i_start += b->i_buffer;
1185 p_sys->block.i_size -= b->i_buffer;
1186 p_sys->block.p_first = b->p_next;
1190 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1191 p_sys->block.p_current == p_sys->block.p_first &&
1192 p_sys->block.p_current->p_next ) /* At least 2 packets */
1194 /* Enough data, don't read more */
1198 /* Now read a new block */
1204 if( s->b_die ) return VLC_EGENERIC;
1208 if( ( b = AReadBlock( s, &b_eof ) ) ) break;
1210 if( b_eof ) return VLC_EGENERIC;
1212 msleep( STREAM_DATA_WAIT );
1219 /* Append the block */
1220 p_sys->block.i_size += b->i_buffer;
1221 *p_sys->block.pp_last = b;
1222 p_sys->block.pp_last = &b->p_next;
1225 if( p_sys->block.p_current == NULL )
1226 p_sys->block.p_current = b;
1229 p_sys->stat.i_bytes += b->i_buffer;
1230 p_sys->stat.i_read_time += i_stop - i_start;
1231 p_sys->stat.i_read_count++;
1240 /****************************************************************************
1242 ****************************************************************************/
1243 static int AStreamRefillStream( stream_t *s );
1245 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read )
1247 stream_sys_t *p_sys = s->p_sys;
1248 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1250 uint8_t *p_data = (uint8_t *)p_read;
1251 unsigned int i_data = 0;
1253 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1255 if( p_read == NULL )
1257 /* seek within this stream if possible, else use plain old read and discard */
1258 stream_sys_t *p_sys = s->p_sys;
1259 access_t *p_access = p_sys->p_access;
1261 /* seeking after EOF is not what we want */
1262 if( !( p_access->info.b_eof ) )
1265 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1267 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1272 msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1273 " offset=%d end=%"PRId64,
1274 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1275 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1278 while( i_data < i_read )
1280 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1281 STREAM_CACHE_TRACK_SIZE;
1282 unsigned int i_current =
1283 __MAX(0,__MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1284 STREAM_CACHE_TRACK_SIZE - i_off ));
1285 int i_copy = __MIN( i_current, i_read - i_data );
1287 if( i_copy <= 0 ) break; /* EOF */
1290 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1293 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1297 p_sys->stream.i_offset += i_copy;
1299 /* Update pos now */
1300 p_sys->i_pos += i_copy;
1303 p_sys->stream.i_used += i_copy;
1304 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1305 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1307 if( AStreamRefillStream( s ) )
1310 if( tk->i_start >= tk->i_end ) break;
1315 if( p_sys->record.b_active && i_data > 0 )
1316 ARecordWrite( s, p_read, i_data );
1321 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1323 stream_sys_t *p_sys = s->p_sys;
1324 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1327 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1330 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1331 "start=%"PRId64" offset=%d end=%"PRId64,
1332 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1333 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1336 /* Avoid problem, but that should *never* happen */
1337 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1338 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1340 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1342 if( p_sys->stream.i_used <= 1 )
1344 /* Be sure we will read something */
1345 p_sys->stream.i_used += i_read -
1346 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1348 if( AStreamRefillStream( s ) ) break;
1351 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1352 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1354 /* Now, direct pointer or a copy ? */
1355 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1356 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1358 *pp_peek = &tk->p_buffer[i_off];
1362 if( p_sys->i_peek < i_read )
1364 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1365 if( !p_sys->p_peek )
1370 p_sys->i_peek = i_read;
1373 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1374 STREAM_CACHE_TRACK_SIZE - i_off );
1375 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1376 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1378 *pp_peek = p_sys->p_peek;
1382 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1384 stream_sys_t *p_sys = s->p_sys;
1385 access_t *p_access = p_sys->p_access;
1393 msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1394 " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1395 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1396 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1397 p_sys->stream.i_offset,
1398 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1402 /* Seek in our current track ? */
1403 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1404 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1406 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1408 msg_Dbg( s, "AStreamSeekStream: current track" );
1410 p_sys->i_pos = i_pos;
1411 p_sys->stream.i_offset = i_pos - tk->i_start;
1413 /* If there is not enough data left in the track, refill */
1414 /* \todo How to get a correct value for
1415 * - refilling threshold
1416 * - how much to refill
1418 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1419 p_sys->stream.i_read_size )
1421 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1423 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1424 AStreamRefillStream( s );
1430 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1433 /* We can't do nothing */
1434 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1435 return VLC_EGENERIC;
1438 /* Date the current track */
1439 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1441 /* Try to reuse already read data */
1442 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1444 stream_track_t *tk = &p_sys->stream.tk[i];
1446 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1449 msg_Dbg( s, "AStreamSeekStream: reusing %d start=%"PRId64
1450 " end=%"PRId64, i, tk->i_start, tk->i_end );
1453 /* Seek at the end of the buffer */
1454 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1457 p_sys->i_pos = i_pos;
1458 p_sys->stream.i_tk = i;
1459 p_sys->stream.i_offset = i_pos - tk->i_start;
1461 if( p_sys->stream.i_used < 1024 )
1462 p_sys->stream.i_used = 1024;
1464 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1465 return VLC_EGENERIC;
1471 access_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1472 /* FIXME compute seek cost (instead of static 'stupid' value) */
1473 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1479 /* Search closest segment TODO */
1480 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1482 stream_track_t *tk = &p_sys->stream.tk[i];
1484 if( i_pos + i_maxth >= tk->i_start )
1486 msg_Dbg( s, "good segment before current pos, TODO" );
1488 if( i_pos - i_maxth <= tk->i_end )
1490 msg_Dbg( s, "good segment after current pos, TODO" );
1495 /* Nothing good, seek and choose oldest segment */
1496 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1497 p_sys->i_pos = i_pos;
1500 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1502 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1506 /* Reset the segment */
1507 p_sys->stream.i_tk = i_new;
1508 p_sys->stream.i_offset = 0;
1509 p_sys->stream.tk[i_new].i_start = i_pos;
1510 p_sys->stream.tk[i_new].i_end = i_pos;
1513 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1514 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1516 if( AStreamRefillStream( s ) )
1517 return VLC_EGENERIC;
1522 static int AStreamRefillStream( stream_t *s )
1524 stream_sys_t *p_sys = s->p_sys;
1525 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1527 /* We read but won't increase i_start after initial start + offset */
1529 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1530 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1531 bool b_read = false;
1532 int64_t i_start, i_stop;
1534 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1537 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1538 p_sys->stream.i_used, i_toread );
1542 while( i_toread > 0 )
1544 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1548 return VLC_EGENERIC;
1550 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1551 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1553 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1556 msleep( STREAM_DATA_WAIT );
1559 else if( i_read == 0 )
1561 if( !b_read ) return VLC_EGENERIC;
1567 tk->i_end += i_read;
1569 /* Windows of STREAM_CACHE_TRACK_SIZE */
1570 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1572 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1574 tk->i_start += i_invalid;
1575 p_sys->stream.i_offset -= i_invalid;
1579 p_sys->stream.i_used -= i_read;
1581 p_sys->stat.i_bytes += i_read;
1582 p_sys->stat.i_read_count++;
1586 p_sys->stat.i_read_time += i_stop - i_start;
1591 static void AStreamPrebufferStream( stream_t *s )
1593 stream_sys_t *p_sys = s->p_sys;
1594 access_t *p_access = p_sys->p_access;
1596 int64_t i_first = 0;
1598 int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1599 ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1600 STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1602 msg_Dbg( s, "pre-buffering..." );
1606 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1608 int64_t i_date = mdate();
1611 if( s->b_die || tk->i_end >= i_prebuffer ||
1612 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1617 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1618 p_sys->stat.i_read_time = i_date - i_start;
1619 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1620 (p_sys->stat.i_read_time+1);
1622 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1623 "%"PRId64" kbytes/s",
1624 p_sys->stat.i_bytes,
1625 p_sys->stat.i_read_time / INT64_C(1000000),
1626 i_byterate / 1024 );
1631 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1632 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1633 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1636 msleep( STREAM_DATA_WAIT );
1639 else if( i_read == 0 )
1648 msg_Dbg( s, "received first data for our buffer");
1651 tk->i_end += i_read;
1653 p_sys->stat.i_read_count++;
1657 /****************************************************************************
1659 ****************************************************************************/
1661 static int AStreamReadImmediate( stream_t *s, void *p_read, unsigned int i_read )
1663 stream_sys_t *p_sys = s->p_sys;
1666 msg_Dbg( s, "AStreamReadImmediate p_read=%p i_read=%d",
1670 /* First, check if we already have some data in the buffer,
1671 * that we could copy directly */
1672 int i_copy = __MIN( stream_buffered_size( s ), i_read );
1676 msg_Dbg( s, "AStreamReadImmediate: copy %d from %p", i_copy, stream_buffer( s ) );
1679 assert( i_copy <= STREAM_CACHE_SIZE );
1683 memcpy( p_read, stream_buffer( s ), i_copy );
1684 p_read = (uint8_t *)p_read + i_copy;
1688 /* Now that we've read our buffer we don't need its i_copy bytes */
1689 stream_buffer_empty( s, i_copy );
1691 /* Now check if we have still to really read some data */
1692 int i_to_read = i_read - i_copy;
1696 i_to_read = AReadStream( s, p_read, i_to_read );
1699 void * dummy = malloc(i_to_read);
1700 i_to_read = AReadStream( s, dummy, i_to_read );
1705 if( p_sys->record.b_active && i_copy > 0 )
1706 ARecordWrite( s, p_read, i_copy );
1708 p_sys->i_pos += i_to_read;
1710 return i_to_read + i_copy;
1713 static int AStreamPeekImmediate( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1716 msg_Dbg( s, "AStreamPeekImmediate: %d size=%"PRId64,
1717 i_read, size_buffered_size( s ) );
1720 /* Avoid problem, but that shouldn't happen */
1721 if( i_read > STREAM_CACHE_SIZE / 2 )
1722 i_read = STREAM_CACHE_SIZE / 2;
1724 int i_to_read = i_read - stream_buffered_size( s );
1728 msg_Dbg( s, "AStreamPeekImmediate: Reading %d",
1731 i_to_read = AReadStream( s, stream_buffer( s ) + stream_buffered_size( s ),
1735 stream_buffer_fill( s, i_to_read );
1738 *pp_peek = stream_buffer( s );
1740 return __MIN(stream_buffered_size( s ), i_read);
1743 static int AStreamSeekImmediate( stream_t *s, int64_t i_pos )
1745 stream_sys_t *p_sys = s->p_sys;
1746 access_t *p_access = p_sys->p_access;
1750 msg_Dbg( s, "AStreamSeekImmediate to %"PRId64" pos=%"PRId64
1751 i_pos, p_sys->i_pos );
1754 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1757 /* We can't do nothing */
1758 msg_Dbg( s, "AStreamSeekImmediate: can't seek" );
1759 return VLC_EGENERIC;
1762 /* Just reset our buffer */
1763 stream_buffer_empty( s, stream_buffered_size( s ) );
1765 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1770 /****************************************************************************
1772 ****************************************************************************/
1774 * Read from the stream untill first newline.
1775 * \param s Stream handle to read from
1776 * \return A pointer to the allocated output string. You need to free this when you are done.
1778 #define STREAM_PROBE_LINE 2048
1779 #define STREAM_LINE_MAX (2048*100)
1780 char * stream_ReadLine( stream_t *s )
1782 char *p_line = NULL;
1783 int i_line = 0, i_read = 0;
1785 while( i_read < STREAM_LINE_MAX )
1788 const uint8_t *p_data;
1792 /* Probe new data */
1793 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1794 if( i_data <= 0 ) break; /* No more data */
1797 i_pos = stream_Tell( s );
1798 if( i_pos == 0 && i_data > 4 )
1801 char *psz_encoding = NULL;
1803 if( p_data[0] == 0xEF && p_data[1] == 0xBB && p_data[2] == 0xBF )
1805 psz_encoding = strdup( "UTF-8" );
1808 else if( p_data[0] == 0x00 && p_data[1] == 0x00 )
1810 if( p_data[2] == 0xFE && p_data[3] == 0xFF )
1812 psz_encoding = strdup( "UTF-32BE" );
1813 s->i_char_width = 4;
1817 else if( p_data[0] == 0xFF && p_data[1] == 0xFE )
1819 if( p_data[2] == 0x00 && p_data[3] == 0x00 )
1821 psz_encoding = strdup( "UTF-32LE" );
1822 s->i_char_width = 4;
1823 s->b_little_endian = true;
1828 psz_encoding = strdup( "UTF-16LE" );
1829 s->b_little_endian = true;
1830 s->i_char_width = 2;
1834 else if( p_data[0] == 0xFE && p_data[1] == 0xFF )
1836 psz_encoding = strdup( "UTF-16BE" );
1837 s->i_char_width = 2;
1841 /* Seek past the BOM */
1844 stream_Seek( s, i_bom_size );
1845 p_data += i_bom_size;
1846 i_data -= i_bom_size;
1849 /* Open the converter if we need it */
1850 if( psz_encoding != NULL )
1852 input_thread_t *p_input;
1853 msg_Dbg( s, "%s BOM detected", psz_encoding );
1854 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1855 if( s->i_char_width > 1 )
1857 s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1858 if( s->conv == (vlc_iconv_t)-1 )
1860 msg_Err( s, "iconv_open failed" );
1863 if( p_input != NULL)
1865 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1866 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1867 vlc_object_release( p_input );
1869 free( psz_encoding );
1873 if( i_data % s->i_char_width )
1875 /* keep i_char_width boundary */
1876 i_data = i_data - ( i_data % s->i_char_width );
1877 msg_Warn( s, "the read is not i_char_width compatible");
1883 /* Check if there is an EOL */
1884 if( s->i_char_width == 1 )
1886 /* UTF-8: 0A <LF> */
1887 psz_eol = memchr( p_data, '\n', i_data );
1891 const uint8_t *p = p_data;
1892 const uint8_t *p_last = p + i_data - s->i_char_width;
1894 if( s->i_char_width == 2 )
1896 if( s->b_little_endian == true)
1898 /* UTF-16LE: 0A 00 <LF> */
1899 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1904 /* UTF-16BE: 00 0A <LF> */
1905 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1909 else if( s->i_char_width == 4 )
1911 if( s->b_little_endian == true)
1913 /* UTF-32LE: 0A 00 00 00 <LF> */
1914 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ||
1915 p[2] != 0x00 || p[3] != 0x00 ) )
1920 /* UTF-32BE: 00 00 00 0A <LF> */
1921 while( p <= p_last && ( p[3] != 0x0A || p[2] != 0x00 ||
1922 p[1] != 0x00 || p[0] != 0x00 ) )
1933 psz_eol = (char *)p + ( s->i_char_width - 1 );
1939 i_data = (psz_eol - (char *)p_data) + 1;
1940 p_line = realloc( p_line, i_line + i_data + s->i_char_width ); /* add \0 */
1943 i_data = stream_Read( s, &p_line[i_line], i_data );
1944 if( i_data <= 0 ) break; /* Hmmm */
1945 i_line += i_data - s->i_char_width; /* skip \n */;
1948 /* We have our line */
1952 /* Read data (+1 for easy \0 append) */
1953 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->i_char_width );
1956 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1957 if( i_data <= 0 ) break; /* Hmmm */
1965 for( j = 0; j < s->i_char_width; j++ )
1967 p_line[i_line + j] = '\0';
1969 i_line += s->i_char_width; /* the added \0 */
1970 if( s->i_char_width > 1 )
1972 size_t i_in = 0, i_out = 0;
1973 const char * p_in = NULL;
1974 char * p_out = NULL;
1975 char * psz_new_line = NULL;
1978 psz_new_line = malloc( i_line );
1979 if( psz_new_line == NULL )
1981 i_in = i_out = (size_t)i_line;
1983 p_out = psz_new_line;
1985 if( vlc_iconv( s->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1987 msg_Err( s, "iconv failed" );
1988 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1991 p_line = psz_new_line;
1992 i_line = (size_t)i_line - i_out; /* does not include \0 */
1995 /* Remove trailing LF/CR */
1996 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1997 p_line[i_line-2] == '\n') ) i_line--;
1999 /* Make sure the \0 is there */
2000 p_line[i_line-1] = '\0';
2007 /* We failed to read any data, probably EOF */
2009 if( s->conv != (vlc_iconv_t)(-1) ) vlc_iconv_close( s->conv );
2013 /****************************************************************************
2014 * Access reading/seeking wrappers to handle concatenated streams.
2015 ****************************************************************************/
2016 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read )
2018 stream_sys_t *p_sys = s->p_sys;
2019 access_t *p_access = p_sys->p_access;
2020 input_thread_t *p_input = NULL;
2021 int i_read_orig = i_read;
2024 if( s->p_parent && s->p_parent->p_parent &&
2025 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
2026 p_input = (input_thread_t *)s->p_parent->p_parent;
2028 if( !p_sys->i_list )
2030 i_read = p_access->pf_read( p_access, p_read, i_read );
2031 if( p_access->b_die )
2032 vlc_object_kill( s );
2035 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2036 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
2038 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2039 (float)i_total, NULL );
2040 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2041 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2046 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
2048 if( p_access->b_die )
2049 vlc_object_kill( s );
2051 /* If we reached an EOF then switch to the next stream in the list */
2052 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
2054 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
2055 access_t *p_list_access;
2057 msg_Dbg( s, "opening input `%s'", psz_name );
2059 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
2061 if( !p_list_access ) return 0;
2063 if( p_sys->p_list_access != p_access )
2064 access_Delete( p_sys->p_list_access );
2066 p_sys->p_list_access = p_list_access;
2068 /* We have to read some data */
2069 return AReadStream( s, p_read, i_read_orig );
2072 /* Update read bytes in input */
2075 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2076 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
2077 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2078 (float)i_total, NULL );
2079 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2080 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2085 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
2087 stream_sys_t *p_sys = s->p_sys;
2088 access_t *p_access = p_sys->p_access;
2089 input_thread_t *p_input = NULL;
2094 if( s->p_parent && s->p_parent->p_parent &&
2095 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
2096 p_input = (input_thread_t *)s->p_parent->p_parent;
2098 if( !p_sys->i_list )
2100 p_block = p_access->pf_block( p_access );
2101 if( p_access->b_die )
2102 vlc_object_kill( s );
2103 if( pb_eof ) *pb_eof = p_access->info.b_eof;
2104 if( p_input && p_block && libvlc_stats (p_access) )
2106 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2107 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
2108 p_block->i_buffer, &i_total );
2109 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2110 (float)i_total, NULL );
2111 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2112 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2117 p_block = p_sys->p_list_access->pf_block( p_access );
2118 if( p_access->b_die )
2119 vlc_object_kill( s );
2120 b_eof = p_sys->p_list_access->info.b_eof;
2121 if( pb_eof ) *pb_eof = b_eof;
2123 /* If we reached an EOF then switch to the next stream in the list */
2124 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
2126 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
2127 access_t *p_list_access;
2129 msg_Dbg( s, "opening input `%s'", psz_name );
2131 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
2133 if( !p_list_access ) return 0;
2135 if( p_sys->p_list_access != p_access )
2136 access_Delete( p_sys->p_list_access );
2138 p_sys->p_list_access = p_list_access;
2140 /* We have to read some data */
2141 return AReadBlock( s, pb_eof );
2147 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2148 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
2149 p_block->i_buffer, &i_total );
2150 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2151 (float)i_total, NULL );
2152 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
2154 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2160 static int ASeek( stream_t *s, int64_t i_pos )
2162 stream_sys_t *p_sys = s->p_sys;
2163 access_t *p_access = p_sys->p_access;
2165 /* Check which stream we need to access */
2171 access_t *p_list_access = 0;
2173 for( i = 0; i < p_sys->i_list - 1; i++ )
2175 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
2176 i_size += p_sys->list[i]->i_size;
2178 psz_name = p_sys->list[i]->psz_path;
2180 if( i != p_sys->i_list_index )
2181 msg_Dbg( s, "opening input `%s'", psz_name );
2183 if( i != p_sys->i_list_index && i != 0 )
2186 access_New( s, p_access->psz_access, "", psz_name );
2188 else if( i != p_sys->i_list_index )
2190 p_list_access = p_access;
2195 if( p_sys->p_list_access != p_access )
2196 access_Delete( p_sys->p_list_access );
2198 p_sys->p_list_access = p_list_access;
2201 p_sys->i_list_index = i;
2202 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
2206 return p_access->pf_seek( p_access, i_pos );
2211 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
2212 * "p_read" is NULL then data are skipped instead of read. The return
2213 * value is the real numbers of bytes read/skip. If this value is less
2214 * than i_read that means that it's the end of the stream.
2216 int stream_Read( stream_t *s, void *p_read, int i_read )
2218 return s->pf_read( s, p_read, i_read );
2222 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
2223 * \return The real numbers of valid bytes, if it's less
2224 * or equal to 0, *pp_peek is invalid.
2225 * \note pp_peek is a pointer to internal buffer and it will be invalid as
2226 * soons as other stream_* functions are called.
2227 * \note Due to input limitation, it could be less than i_peek without meaning
2228 * the end of the stream (but only when you have i_peek >=
2229 * p_input->i_bufsize)
2231 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
2233 return s->pf_peek( s, pp_peek, i_peek );
2237 * Use to control the "stream_t *". Look at #stream_query_e for
2238 * possible "i_query" value and format arguments. Return VLC_SUCCESS
2239 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
2241 int stream_vaControl( stream_t *s, int i_query, va_list args )
2243 return s->pf_control( s, i_query, args );
2249 void stream_Delete( stream_t *s )
2254 int stream_Control( stream_t *s, int i_query, ... )
2260 return VLC_EGENERIC;
2262 va_start( args, i_query );
2263 i_result = s->pf_control( s, i_query, args );
2269 * Read "i_size" bytes and store them in a block_t.
2270 * It always read i_size bytes unless you are at the end of the stream
2271 * where it return what is available.
2273 block_t *stream_Block( stream_t *s, int i_size )
2275 if( i_size <= 0 ) return NULL;
2277 /* emulate block read */
2278 block_t *p_bk = block_New( s, i_size );
2281 int i_read = stream_Read( s, p_bk->p_buffer, i_size );
2284 p_bk->i_buffer = i_read;
2287 block_Release( p_bk );