1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_strings.h>
34 #include <vlc_charset.h>
41 #include "input_internal.h"
46 * - tune the 2 methods (block/stream)
47 * - compute cost for seek
48 * - improve stream mode seeking with closest segments
50 * - Maybe remove (block/stream) in favour of immediate
55 * One linked list of data read
57 * More complex scheme using mutliple track to avoid seeking
58 * - using directly the access (only indirection for peeking).
59 * This method is known to introduce much less latency.
60 * It should probably defaulted (instead of the stream method (2)).
63 /* How many tracks we have, currently only used for stream mode */
64 #ifdef OPTIMIZE_MEMORY
65 # define STREAM_CACHE_TRACK 1
66 /* Max size of our cache 128Ko per track */
67 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
69 # define STREAM_CACHE_TRACK 3
70 /* Max size of our cache 4Mo per track */
71 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
74 /* How many data we try to prebuffer */
75 #define STREAM_CACHE_PREBUFFER_SIZE (32767)
76 /* Maximum time we take to pre-buffer */
77 #define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
79 /* Method1: Simple, for pf_block.
80 * We get blocks and put them in the linked list.
81 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
84 /* Method2: A bit more complex, for pf_read
85 * - We use ring buffers, only one if unseekable, all if seekable
86 * - Upon seek date current ring, then search if one ring match the pos,
87 * yes: switch to it, seek the access to match the end of the ring
88 * no: search the ring with i_end the closer to i_pos,
89 * if close enough, read data and use this ring
90 * else use the oldest ring, seek and use it.
92 * TODO: - with access non seekable: use all space available for only one ring, but
93 * we have to support seekable/non-seekable switch on the fly.
94 * - compute a good value for i_read_size
97 #define STREAM_READ_ATONCE 32767
98 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
118 typedef enum stream_read_method_t
120 STREAM_METHOD_IMMEDIATE,
123 } stream_read_method_t;
129 stream_read_method_t method; /* method to use */
131 int64_t i_pos; /* Current reading offset */
133 /* Method 1: pf_block */
136 int64_t i_start; /* Offset of block for p_first */
137 int64_t i_offset; /* Offset for data in p_current */
138 block_t *p_current; /* Current block */
140 int i_size; /* Total amount of data in the list */
146 /* Method 2: for pf_read */
149 int i_offset; /* Buffer offset in the current track */
150 int i_tk; /* Current track */
151 stream_track_t tk[STREAM_CACHE_TRACK];
157 int i_used; /* Used since last read */
162 /* Method 3: for pf_read */
169 /* Peek temporary buffer */
173 /* Stat for both method */
176 bool b_fastseek; /* From access */
178 /* Stat about reading data */
179 int64_t i_read_count;
183 /* Stat about seek */
191 access_entry_t **list;
193 access_t *p_list_access;
195 /* Preparse mode ? */
203 FILE *f; /* TODO it could be replaced by access_output_t one day */
209 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read );
210 static int AStreamPeekBlock( stream_t *s, const uint8_t **p_peek, unsigned int i_read );
211 static int AStreamSeekBlock( stream_t *s, int64_t i_pos );
212 static void AStreamPrebufferBlock( stream_t *s );
213 static block_t *AReadBlock( stream_t *s, bool *pb_eof );
216 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read );
217 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read );
218 static int AStreamSeekStream( stream_t *s, int64_t i_pos );
219 static void AStreamPrebufferStream( stream_t *s );
220 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read );
223 static int AStreamReadImmediate( stream_t *s, void *p_read, unsigned int i_read );
224 static int AStreamPeekImmediate( stream_t *s, const uint8_t **pp_peek, unsigned int i_read );
225 static int AStreamSeekImmediate( stream_t *s, int64_t i_pos );
228 static int AStreamControl( stream_t *s, int i_query, va_list );
229 static void AStreamDestroy( stream_t *s );
230 static void UStreamDestroy( stream_t *s );
231 static int ASeek( stream_t *s, int64_t i_pos );
232 static int ARecordSetState( stream_t *s, bool b_record, const char *psz_extension );
233 static void ARecordWrite( stream_t *s, const uint8_t *p_buffer, size_t i_buffer );
235 /****************************************************************************
237 ****************************************************************************/
239 static inline int64_t stream_buffered_size( stream_t *s )
241 return s->p_sys->immediate.i_end;
244 static inline void stream_buffer_empty( stream_t *s, int length )
246 length = __MAX( stream_buffered_size( s ), length );
249 memmove( s->p_sys->immediate.p_buffer,
250 s->p_sys->immediate.p_buffer + length,
251 stream_buffered_size( s ) - length );
253 s->p_sys->immediate.i_end -= length;
256 static inline void stream_buffer_fill( stream_t *s, int length )
258 s->p_sys->immediate.i_end += length;
261 static inline uint8_t * stream_buffer( stream_t *s )
263 return s->p_sys->immediate.p_buffer;
266 /****************************************************************************
267 * stream_UrlNew: create a stream from a access
268 ****************************************************************************/
269 stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
271 const char *psz_access, *psz_demux;
279 char psz_dup[strlen( psz_url ) + 1];
280 strcpy( psz_dup, psz_url );
281 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
283 /* Now try a real access */
284 p_access = access_New( p_parent, psz_access, psz_demux, psz_path );
286 if( p_access == NULL )
288 msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
292 if( !( p_res = stream_AccessNew( p_access, true ) ) )
294 access_Delete( p_access );
298 p_res->pf_destroy = UStreamDestroy;
302 stream_t *stream_AccessNew( access_t *p_access, bool b_quick )
304 stream_t *s = vlc_stream_create( VLC_OBJECT(p_access) );
306 char *psz_list = NULL;
311 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
314 vlc_object_release( s );
318 /* Attach it now, needed for b_die */
319 vlc_object_attach( s, p_access );
321 s->pf_read = NULL; /* Set up later */
323 s->pf_control = AStreamControl;
324 s->pf_destroy = AStreamDestroy;
326 /* UTF16 and UTF32 text file conversion */
328 s->b_little_endian = false;
329 s->conv = (vlc_iconv_t)(-1);
332 p_sys->p_access = p_access;
333 if( p_access->pf_block )
334 p_sys->method = STREAM_METHOD_BLOCK;
335 else if( var_CreateGetBool( s, "use-stream-immediate" ) )
336 p_sys->method = STREAM_METHOD_IMMEDIATE;
338 p_sys->method = STREAM_METHOD_STREAM;
340 p_sys->record.b_active = false;
342 p_sys->i_pos = p_access->info.i_pos;
345 access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
346 p_sys->stat.i_bytes = 0;
347 p_sys->stat.i_read_time = 0;
348 p_sys->stat.i_read_count = 0;
349 p_sys->stat.i_seek_count = 0;
350 p_sys->stat.i_seek_time = 0;
354 p_sys->i_list_index = 0;
355 p_sys->p_list_access = 0;
357 p_sys->b_quick = b_quick;
359 /* Get the additional list of inputs if any (for concatenation) */
360 if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
362 access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
363 if( p_entry == NULL )
365 char *psz_name, *psz_parser = psz_name = psz_list;
367 p_sys->p_list_access = p_access;
368 p_entry->i_size = p_access->info.i_size;
369 p_entry->psz_path = strdup( p_access->psz_path );
370 if( p_entry->psz_path == NULL )
375 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
376 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
377 p_entry->psz_path, p_access->info.i_size );
379 while( psz_name && *psz_name )
381 psz_parser = strchr( psz_name, ',' );
382 if( psz_parser ) *psz_parser = 0;
384 psz_name = strdup( psz_name );
387 access_t *p_tmp = access_New( p_access, p_access->psz_access,
392 psz_name = psz_parser;
393 if( psz_name ) psz_name++;
397 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
398 psz_name, p_tmp->info.i_size );
400 p_entry = malloc( sizeof(access_entry_t) );
401 if( p_entry == NULL )
403 p_entry->i_size = p_tmp->info.i_size;
404 p_entry->psz_path = psz_name;
405 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
407 access_Delete( p_tmp );
410 psz_name = psz_parser;
411 if( psz_name ) psz_name++;
414 FREENULL( psz_list );
418 p_sys->p_peek = NULL;
420 if( p_sys->method == STREAM_METHOD_BLOCK )
422 msg_Dbg( s, "Using AStream*Block" );
423 s->pf_read = AStreamReadBlock;
424 s->pf_peek = AStreamPeekBlock;
426 /* Init all fields of p_sys->block */
427 p_sys->block.i_start = p_sys->i_pos;
428 p_sys->block.i_offset = 0;
429 p_sys->block.p_current = NULL;
430 p_sys->block.i_size = 0;
431 p_sys->block.p_first = NULL;
432 p_sys->block.pp_last = &p_sys->block.p_first;
434 /* Do the prebuffering */
435 AStreamPrebufferBlock( s );
437 if( p_sys->block.i_size <= 0 )
439 msg_Err( s, "cannot pre fill buffer" );
443 else if( p_sys->method == STREAM_METHOD_IMMEDIATE )
445 msg_Dbg( s, "Using AStream*Immediate" );
447 s->pf_read = AStreamReadImmediate;
448 s->pf_peek = AStreamPeekImmediate;
450 /* Allocate/Setup our tracks (useful to peek)*/
451 p_sys->immediate.i_end = 0;
452 p_sys->immediate.p_buffer = malloc( STREAM_CACHE_SIZE );
454 msg_Dbg( s, "p_buffer %p-%p",
455 p_sys->immediate.p_buffer,
456 &p_sys->immediate.p_buffer[STREAM_CACHE_SIZE] );
458 if( p_sys->immediate.p_buffer == NULL )
460 msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
469 assert( p_sys->method == STREAM_METHOD_STREAM );
471 msg_Dbg( s, "Using AStream*Stream" );
473 s->pf_read = AStreamReadStream;
474 s->pf_peek = AStreamPeekStream;
476 /* Allocate/Setup our tracks */
477 p_sys->stream.i_offset = 0;
478 p_sys->stream.i_tk = 0;
479 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
480 if( p_sys->stream.p_buffer == NULL )
482 msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
486 p_sys->stream.i_used = 0;
487 access_Control( p_access, ACCESS_GET_MTU,
488 &p_sys->stream.i_read_size );
489 if( p_sys->stream.i_read_size <= 0 )
490 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
491 else if( p_sys->stream.i_read_size <= 256 )
492 p_sys->stream.i_read_size = 256;
494 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
496 p_sys->stream.tk[i].i_date = 0;
497 p_sys->stream.tk[i].i_start = p_sys->i_pos;
498 p_sys->stream.tk[i].i_end = p_sys->i_pos;
499 p_sys->stream.tk[i].p_buffer=
500 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
503 /* Do the prebuffering */
504 AStreamPrebufferStream( s );
506 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
508 msg_Err( s, "cannot pre fill buffer" );
516 if( p_sys->method == STREAM_METHOD_BLOCK )
522 free( p_sys->stream.p_buffer );
524 while( p_sys->i_list > 0 )
525 free( p_sys->list[--(p_sys->i_list)] );
529 vlc_object_detach( s );
530 vlc_object_release( s );
534 /****************************************************************************
536 ****************************************************************************/
537 static void AStreamDestroy( stream_t *s )
539 stream_sys_t *p_sys = s->p_sys;
541 vlc_object_detach( s );
543 if( p_sys->record.b_active )
544 ARecordSetState( s, false, NULL );
546 if( p_sys->method == STREAM_METHOD_BLOCK )
547 block_ChainRelease( p_sys->block.p_first );
548 else if( p_sys->method == STREAM_METHOD_IMMEDIATE )
549 free( p_sys->immediate.p_buffer );
551 free( p_sys->stream.p_buffer );
553 free( p_sys->p_peek );
555 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
556 access_Delete( p_sys->p_list_access );
558 while( p_sys->i_list-- )
560 free( p_sys->list[p_sys->i_list]->psz_path );
561 free( p_sys->list[p_sys->i_list] );
567 vlc_object_release( s );
570 static void UStreamDestroy( stream_t *s )
572 access_t *p_access = (access_t *)s->p_parent;
574 access_Delete( p_access );
577 /****************************************************************************
578 * stream_AccessReset:
579 ****************************************************************************/
580 void stream_AccessReset( stream_t *s )
582 stream_sys_t *p_sys = s->p_sys;
584 p_sys->i_pos = p_sys->p_access->info.i_pos;
586 if( p_sys->method == STREAM_METHOD_BLOCK )
588 block_ChainRelease( p_sys->block.p_first );
590 /* Init all fields of p_sys->block */
591 p_sys->block.i_start = p_sys->i_pos;
592 p_sys->block.i_offset = 0;
593 p_sys->block.p_current = NULL;
594 p_sys->block.i_size = 0;
595 p_sys->block.p_first = NULL;
596 p_sys->block.pp_last = &p_sys->block.p_first;
598 /* Do the prebuffering */
599 AStreamPrebufferBlock( s );
601 else if( p_sys->method == STREAM_METHOD_IMMEDIATE )
603 stream_buffer_empty( s, stream_buffered_size( s ) );
609 assert( p_sys->method == STREAM_METHOD_STREAM );
611 /* Setup our tracks */
612 p_sys->stream.i_offset = 0;
613 p_sys->stream.i_tk = 0;
614 p_sys->stream.i_used = 0;
616 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
618 p_sys->stream.tk[i].i_date = 0;
619 p_sys->stream.tk[i].i_start = p_sys->i_pos;
620 p_sys->stream.tk[i].i_end = p_sys->i_pos;
623 /* Do the prebuffering */
624 AStreamPrebufferStream( s );
628 /****************************************************************************
629 * stream_AccessUpdate:
630 ****************************************************************************/
631 void stream_AccessUpdate( stream_t *s )
633 stream_sys_t *p_sys = s->p_sys;
635 p_sys->i_pos = p_sys->p_access->info.i_pos;
640 for( i = 0; i < p_sys->i_list_index; i++ )
642 p_sys->i_pos += p_sys->list[i]->i_size;
647 /****************************************************************************
649 ****************************************************************************/
650 static int AStreamControl( stream_t *s, int i_query, va_list args )
652 stream_sys_t *p_sys = s->p_sys;
653 access_t *p_access = p_sys->p_access;
657 const char *psz_string;
658 int64_t *pi_64, i_64;
663 case STREAM_GET_SIZE:
664 pi_64 = (int64_t*)va_arg( args, int64_t * );
665 if( s->p_sys->i_list )
669 for( i = 0; i < s->p_sys->i_list; i++ )
670 *pi_64 += s->p_sys->list[i]->i_size;
673 *pi_64 = p_access->info.i_size;
676 case STREAM_CAN_SEEK:
677 p_bool = (bool*)va_arg( args, bool * );
678 access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
681 case STREAM_CAN_FASTSEEK:
682 p_bool = (bool*)va_arg( args, bool * );
683 access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
686 case STREAM_GET_POSITION:
687 pi_64 = (int64_t*)va_arg( args, int64_t * );
688 *pi_64 = p_sys->i_pos;
691 case STREAM_SET_POSITION:
692 i_64 = (int64_t)va_arg( args, int64_t );
693 switch( p_sys->method )
695 case STREAM_METHOD_BLOCK:
696 return AStreamSeekBlock( s, i_64 );
697 case STREAM_METHOD_IMMEDIATE:
698 return AStreamSeekImmediate( s, i_64 );
699 case STREAM_METHOD_STREAM:
700 return AStreamSeekStream( s, i_64 );
709 case STREAM_CONTROL_ACCESS:
710 i_int = (int) va_arg( args, int );
711 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
712 i_int != ACCESS_SET_PRIVATE_ID_CA &&
713 i_int != ACCESS_GET_PRIVATE_ID_STATE )
715 msg_Err( s, "Hey, what are you thinking ?"
716 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
719 return access_vaControl( p_access, i_int, args );
721 case STREAM_GET_CONTENT_TYPE:
722 return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
723 va_arg( args, char ** ) );
724 case STREAM_SET_RECORD_STATE:
725 b_bool = (bool)va_arg( args, int );
728 psz_string = (const char*)va_arg( args, const char* );
729 return ARecordSetState( s, b_bool, psz_string );
732 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
738 /****************************************************************************
739 * ARecord*: record stream functions
740 ****************************************************************************/
741 static int ARecordStart( stream_t *s, const char *psz_extension )
743 stream_sys_t *p_sys = s->p_sys;
750 psz_extension = "dat";
753 char *psz_path = var_CreateGetString( s, "input-record-path" );
754 if( !psz_path || *psz_path == '\0' )
757 psz_path = strdup( config_GetHomeDir() );
764 * TODO allow prefix configuration */
765 psz_file = input_CreateFilename( VLC_OBJECT(s), psz_path, INPUT_RECORD_PREFIX, psz_extension );
772 f = utf8_fopen( psz_file, "wb" );
778 msg_Dbg( s, "Recording into %s", psz_file );
783 p_sys->record.b_active = true;
784 p_sys->record.b_error = false;
787 static int ARecordStop( stream_t *s )
789 stream_sys_t *p_sys = s->p_sys;
791 assert( p_sys->record.b_active );
793 msg_Dbg( s, "Recording completed" );
794 fclose( p_sys->record.f );
795 p_sys->record.b_active = false;
799 static int ARecordSetState( stream_t *s, bool b_record, const char *psz_extension )
801 stream_sys_t *p_sys = s->p_sys;
803 if( !!p_sys->record.b_active == !!b_record )
807 return ARecordStart( s, psz_extension );
809 return ARecordStop( s );
811 static void ARecordWrite( stream_t *s, const uint8_t *p_buffer, size_t i_buffer )
813 stream_sys_t *p_sys = s->p_sys;
815 assert( p_sys->record.b_active );
819 const bool b_previous_error = p_sys->record.b_error;
820 const size_t i_written = fwrite( p_buffer, 1, i_buffer, p_sys->record.f );
822 p_sys->record.b_error = i_written != i_buffer;
824 /* TODO maybe a intf_UserError or something like that ? */
825 if( p_sys->record.b_error && !b_previous_error )
826 msg_Err( s, "Failed to record data (begin)" );
827 else if( !p_sys->record.b_error && b_previous_error )
828 msg_Err( s, "Failed to record data (end)" );
832 /****************************************************************************
834 ****************************************************************************/
835 static void AStreamPrebufferBlock( stream_t *s )
837 stream_sys_t *p_sys = s->p_sys;
838 access_t *p_access = p_sys->p_access;
843 msg_Dbg( s, "pre buffering" );
847 int64_t i_date = mdate();
851 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
852 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
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++;
889 if( p_access->info.b_prebuffered )
891 /* Access has already prebufferred - update stats and exit */
892 p_sys->stat.i_bytes = p_sys->block.i_size;
893 p_sys->stat.i_read_time = mdate() - i_start;
900 msg_Dbg( s, "received first data for our buffer");
905 p_sys->block.p_current = p_sys->block.p_first;
908 static int AStreamRefillBlock( stream_t *s );
910 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
912 stream_sys_t *p_sys = s->p_sys;
914 uint8_t *p_data = p_read;
915 uint8_t *p_record = p_data;
916 unsigned int i_data = 0;
919 if( p_sys->block.p_current == NULL )
922 if( p_sys->record.b_active && !p_data )
923 p_record = p_data = malloc( i_read );
927 /* seek within this stream if possible, else use plain old read and discard */
928 stream_sys_t *p_sys = s->p_sys;
929 access_t *p_access = p_sys->p_access;
931 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
933 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
936 while( i_data < i_read )
939 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
940 unsigned int i_copy = __MIN( (unsigned int)__MAX(i_current,0), i_read - i_data);
946 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
952 p_sys->block.i_offset += i_copy;
953 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
955 /* Current block is now empty, switch to next */
956 if( p_sys->block.p_current )
958 p_sys->block.i_offset = 0;
959 p_sys->block.p_current = p_sys->block.p_current->p_next;
961 /*Get a new block if needed */
962 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
969 if( p_sys->record.b_active )
971 if( i_data > 0 && p_record != NULL)
972 ARecordWrite( s, p_record, i_data );
977 p_sys->i_pos += i_data;
981 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
983 stream_sys_t *p_sys = s->p_sys;
985 unsigned int i_data = 0;
987 unsigned int i_offset;
989 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
991 /* We can directly give a pointer over our buffer */
992 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
994 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
998 /* We need to create a local copy */
999 if( p_sys->i_peek < i_read )
1001 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1002 if( !p_sys->p_peek )
1007 p_sys->i_peek = i_read;
1010 /* Fill enough data */
1011 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
1014 block_t **pp_last = p_sys->block.pp_last;
1016 if( AStreamRefillBlock( s ) ) break;
1018 /* Our buffer are probably filled enough, don't try anymore */
1019 if( pp_last == p_sys->block.pp_last ) break;
1022 /* Copy what we have */
1023 b = p_sys->block.p_current;
1024 i_offset = p_sys->block.i_offset;
1025 p_data = p_sys->p_peek;
1027 while( b && i_data < i_read )
1029 unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
1030 int i_copy = __MIN( i_current, i_read - i_data );
1032 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
1037 if( i_offset >= b->i_buffer )
1044 *pp_peek = p_sys->p_peek;
1048 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
1050 stream_sys_t *p_sys = s->p_sys;
1051 access_t *p_access = p_sys->p_access;
1052 int64_t i_offset = i_pos - p_sys->block.i_start;
1055 /* We already have thoses data, just update p_current/i_offset */
1056 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
1058 block_t *b = p_sys->block.p_first;
1061 while( i_current + b->i_buffer < i_offset )
1063 i_current += b->i_buffer;
1067 p_sys->block.p_current = b;
1068 p_sys->block.i_offset = i_offset - i_current;
1070 p_sys->i_pos = i_pos;
1075 /* We may need to seek or to read data */
1079 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1083 msg_Err( s, "backward seeking impossible (access not seekable)" );
1084 return VLC_EGENERIC;
1091 bool b_aseek, b_aseekfast;
1093 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1094 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
1099 msg_Warn( s, "%"PRId64" bytes need to be skipped "
1100 "(access non seekable)",
1101 i_offset - p_sys->block.i_size );
1105 int64_t i_skip = i_offset - p_sys->block.i_size;
1107 /* Avg bytes per packets */
1108 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
1109 /* TODO compute a seek cost instead of fixed threshold */
1110 int i_th = b_aseekfast ? 1 : 5;
1112 if( i_skip <= i_th * i_avg &&
1113 i_skip < STREAM_CACHE_SIZE )
1118 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
1119 b_seek, i_th*i_avg, i_skip );
1125 int64_t i_start, i_end;
1126 /* Do the access seek */
1128 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1132 block_ChainRelease( p_sys->block.p_first );
1135 p_sys->block.i_start = p_sys->i_pos = i_pos;
1136 p_sys->block.i_offset = 0;
1137 p_sys->block.p_current = NULL;
1138 p_sys->block.i_size = 0;
1139 p_sys->block.p_first = NULL;
1140 p_sys->block.pp_last = &p_sys->block.p_first;
1142 /* Refill a block */
1143 if( AStreamRefillBlock( s ) )
1144 return VLC_EGENERIC;
1147 p_sys->stat.i_seek_time += i_end - i_start;
1148 p_sys->stat.i_seek_count++;
1155 /* Read and skip enough data */
1156 if( AStreamRefillBlock( s ) )
1157 return VLC_EGENERIC;
1159 while( p_sys->block.p_current &&
1160 p_sys->i_pos + p_sys->block.p_current->i_buffer - p_sys->block.i_offset < i_pos )
1162 p_sys->i_pos += p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
1163 p_sys->block.p_current = p_sys->block.p_current->p_next;
1164 p_sys->block.i_offset = 0;
1167 while( p_sys->block.i_start + p_sys->block.i_size < i_pos );
1169 p_sys->block.i_offset = i_pos - p_sys->i_pos;
1170 p_sys->i_pos = i_pos;
1175 return VLC_EGENERIC;
1178 static int AStreamRefillBlock( stream_t *s )
1180 stream_sys_t *p_sys = s->p_sys;
1181 int64_t i_start, i_stop;
1185 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1186 p_sys->block.p_first != p_sys->block.p_current )
1188 block_t *b = p_sys->block.p_first;
1190 p_sys->block.i_start += b->i_buffer;
1191 p_sys->block.i_size -= b->i_buffer;
1192 p_sys->block.p_first = b->p_next;
1196 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1197 p_sys->block.p_current == p_sys->block.p_first &&
1198 p_sys->block.p_current->p_next ) /* At least 2 packets */
1200 /* Enough data, don't read more */
1204 /* Now read a new block */
1211 return VLC_EGENERIC;
1214 if( ( b = AReadBlock( s, &b_eof ) ) )
1217 return VLC_EGENERIC;
1224 /* Append the block */
1225 p_sys->block.i_size += b->i_buffer;
1226 *p_sys->block.pp_last = b;
1227 p_sys->block.pp_last = &b->p_next;
1230 if( p_sys->block.p_current == NULL )
1231 p_sys->block.p_current = b;
1234 p_sys->stat.i_bytes += b->i_buffer;
1235 p_sys->stat.i_read_time += i_stop - i_start;
1236 p_sys->stat.i_read_count++;
1245 /****************************************************************************
1247 ****************************************************************************/
1248 static int AStreamRefillStream( stream_t *s );
1250 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read )
1252 stream_sys_t *p_sys = s->p_sys;
1253 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1255 uint8_t *p_data = (uint8_t *)p_read;
1256 uint8_t *p_record = p_data;
1257 unsigned int i_data = 0;
1259 if( tk->i_start >= tk->i_end )
1262 if( p_sys->record.b_active && !p_data )
1263 p_record = p_data = malloc( i_read );
1265 if( p_data == NULL )
1267 /* seek within this stream if possible, else use plain old read and discard */
1268 stream_sys_t *p_sys = s->p_sys;
1269 access_t *p_access = p_sys->p_access;
1271 /* seeking after EOF is not what we want */
1272 if( !( p_access->info.b_eof ) )
1275 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1277 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1282 msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1283 " offset=%d end=%"PRId64,
1284 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1285 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1288 while( i_data < i_read )
1290 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1291 STREAM_CACHE_TRACK_SIZE;
1292 unsigned int i_current =
1293 __MAX(0,__MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1294 STREAM_CACHE_TRACK_SIZE - i_off ));
1295 int i_copy = __MIN( i_current, i_read - i_data );
1297 if( i_copy <= 0 ) break; /* EOF */
1300 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1303 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1307 p_sys->stream.i_offset += i_copy;
1309 /* Update pos now */
1310 p_sys->i_pos += i_copy;
1313 p_sys->stream.i_used += i_copy;
1314 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1315 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1317 if( AStreamRefillStream( s ) )
1320 if( tk->i_start >= tk->i_end ) break;
1325 if( p_sys->record.b_active )
1327 if( i_data > 0 && p_record != NULL)
1328 ARecordWrite( s, p_record, i_data );
1336 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1338 stream_sys_t *p_sys = s->p_sys;
1339 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1342 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1345 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1346 "start=%"PRId64" offset=%d end=%"PRId64,
1347 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1348 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1351 /* Avoid problem, but that should *never* happen */
1352 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1353 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1355 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1357 if( p_sys->stream.i_used <= 1 )
1359 /* Be sure we will read something */
1360 p_sys->stream.i_used += i_read -
1361 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1363 if( AStreamRefillStream( s ) ) break;
1366 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1367 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1369 /* Now, direct pointer or a copy ? */
1370 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1371 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1373 *pp_peek = &tk->p_buffer[i_off];
1377 if( p_sys->i_peek < i_read )
1379 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1380 if( !p_sys->p_peek )
1385 p_sys->i_peek = i_read;
1388 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1389 STREAM_CACHE_TRACK_SIZE - i_off );
1390 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1391 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1393 *pp_peek = p_sys->p_peek;
1397 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1399 stream_sys_t *p_sys = s->p_sys;
1400 access_t *p_access = p_sys->p_access;
1408 msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1409 " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1410 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1411 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1412 p_sys->stream.i_offset,
1413 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1417 /* Seek in our current track ? */
1418 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1419 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1421 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1423 msg_Dbg( s, "AStreamSeekStream: current track" );
1425 p_sys->i_pos = i_pos;
1426 p_sys->stream.i_offset = i_pos - tk->i_start;
1428 /* If there is not enough data left in the track, refill */
1429 /* \todo How to get a correct value for
1430 * - refilling threshold
1431 * - how much to refill
1433 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1434 p_sys->stream.i_read_size )
1436 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1438 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1439 AStreamRefillStream( s );
1445 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1448 /* We can't do nothing */
1449 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1450 return VLC_EGENERIC;
1453 /* Date the current track */
1454 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1456 /* Try to reuse already read data */
1457 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1459 stream_track_t *tk = &p_sys->stream.tk[i];
1461 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1464 msg_Dbg( s, "AStreamSeekStream: reusing %d start=%"PRId64
1465 " end=%"PRId64, i, tk->i_start, tk->i_end );
1468 /* Seek at the end of the buffer */
1469 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1472 p_sys->i_pos = i_pos;
1473 p_sys->stream.i_tk = i;
1474 p_sys->stream.i_offset = i_pos - tk->i_start;
1476 if( p_sys->stream.i_used < 1024 )
1477 p_sys->stream.i_used = 1024;
1479 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1480 return VLC_EGENERIC;
1486 access_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1487 /* FIXME compute seek cost (instead of static 'stupid' value) */
1488 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1494 /* Search closest segment TODO */
1495 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1497 stream_track_t *tk = &p_sys->stream.tk[i];
1499 if( i_pos + i_maxth >= tk->i_start )
1501 msg_Dbg( s, "good segment before current pos, TODO" );
1503 if( i_pos - i_maxth <= tk->i_end )
1505 msg_Dbg( s, "good segment after current pos, TODO" );
1510 /* Nothing good, seek and choose oldest segment */
1511 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1512 p_sys->i_pos = i_pos;
1515 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1517 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1521 /* Reset the segment */
1522 p_sys->stream.i_tk = i_new;
1523 p_sys->stream.i_offset = 0;
1524 p_sys->stream.tk[i_new].i_start = i_pos;
1525 p_sys->stream.tk[i_new].i_end = i_pos;
1528 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1529 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1531 if( AStreamRefillStream( s ) )
1532 return VLC_EGENERIC;
1537 static int AStreamRefillStream( stream_t *s )
1539 stream_sys_t *p_sys = s->p_sys;
1540 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1542 /* We read but won't increase i_start after initial start + offset */
1544 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1545 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1546 bool b_read = false;
1547 int64_t i_start, i_stop;
1549 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1552 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1553 p_sys->stream.i_used, i_toread );
1557 while( i_toread > 0 )
1559 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1563 return VLC_EGENERIC;
1565 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1566 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1568 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1573 else if( i_read == 0 )
1576 return VLC_EGENERIC;
1582 tk->i_end += i_read;
1584 /* Windows of STREAM_CACHE_TRACK_SIZE */
1585 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1587 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1589 tk->i_start += i_invalid;
1590 p_sys->stream.i_offset -= i_invalid;
1594 p_sys->stream.i_used -= i_read;
1596 p_sys->stat.i_bytes += i_read;
1597 p_sys->stat.i_read_count++;
1601 p_sys->stat.i_read_time += i_stop - i_start;
1606 static void AStreamPrebufferStream( stream_t *s )
1608 stream_sys_t *p_sys = s->p_sys;
1609 access_t *p_access = p_sys->p_access;
1611 int64_t i_first = 0;
1613 int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1614 ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1615 STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1617 msg_Dbg( s, "pre-buffering..." );
1621 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1623 int64_t i_date = mdate();
1626 if( s->b_die || tk->i_end >= i_prebuffer ||
1627 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1632 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1633 p_sys->stat.i_read_time = i_date - i_start;
1634 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1635 (p_sys->stat.i_read_time+1);
1637 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1638 "%"PRId64" kbytes/s",
1639 p_sys->stat.i_bytes,
1640 p_sys->stat.i_read_time / INT64_C(1000000),
1641 i_byterate / 1024 );
1646 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1647 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1648 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1651 else if( i_read == 0 )
1657 msg_Dbg( s, "received first data for our buffer");
1660 tk->i_end += i_read;
1662 p_sys->stat.i_read_count++;
1666 /****************************************************************************
1668 ****************************************************************************/
1670 static int AStreamReadImmediate( stream_t *s, void *p_read, unsigned int i_read )
1672 stream_sys_t *p_sys = s->p_sys;
1673 uint8_t *p_data= (uint8_t*)p_read;
1674 uint8_t *p_record = p_data;
1675 unsigned int i_data;
1678 msg_Dbg( s, "AStreamReadImmediate p_read=%p i_read=%d",
1682 if( p_sys->record.b_active && !p_data )
1683 p_record = p_data = malloc( i_read );
1685 /* First, check if we already have some data in the buffer,
1686 * that we could copy directly */
1687 i_data = __MIN( stream_buffered_size( s ), i_read );
1691 msg_Dbg( s, "AStreamReadImmediate: copy %u from %p", i_data, stream_buffer( s ) );
1694 assert( i_data <= STREAM_CACHE_SIZE );
1698 memcpy( p_data, stream_buffer( s ), i_data );
1703 /* Now that we've read our buffer we don't need its i_copy bytes */
1704 stream_buffer_empty( s, i_data );
1706 /* Now check if we have still to really read some data */
1707 while( i_data < i_read )
1709 const unsigned int i_to_read = i_read - i_data;
1714 i_result = AReadStream( s, p_data, i_to_read );
1718 void *p_dummy = malloc( i_to_read );
1723 i_result = AReadStream( s, p_dummy, i_to_read );
1730 p_sys->i_pos += i_data;
1736 if( p_sys->record.b_active )
1738 if( i_data > 0 && p_record != NULL)
1739 ARecordWrite( s, p_record, i_data );
1746 static int AStreamPeekImmediate( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1749 msg_Dbg( s, "AStreamPeekImmediate: %d size=%"PRId64,
1750 i_read, size_buffered_size( s ) );
1753 /* Avoid problem, but that shouldn't happen
1754 * FIXME yes it can */
1755 if( i_read > STREAM_CACHE_SIZE / 2 )
1756 i_read = STREAM_CACHE_SIZE / 2;
1758 int i_to_read = i_read - stream_buffered_size( s );
1762 msg_Dbg( s, "AStreamPeekImmediate: Reading %d",
1765 i_to_read = AReadStream( s, stream_buffer( s ) + stream_buffered_size( s ),
1769 stream_buffer_fill( s, i_to_read );
1772 *pp_peek = stream_buffer( s );
1774 return __MIN( stream_buffered_size( s ), i_read );
1777 static int AStreamSeekImmediate( stream_t *s, int64_t i_pos )
1779 stream_sys_t *p_sys = s->p_sys;
1780 access_t *p_access = p_sys->p_access;
1784 msg_Dbg( s, "AStreamSeekImmediate to %"PRId64" pos=%"PRId64
1785 i_pos, p_sys->i_pos );
1788 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1791 /* We can't do nothing */
1792 msg_Dbg( s, "AStreamSeekImmediate: can't seek" );
1793 return VLC_EGENERIC;
1796 /* Just reset our buffer */
1797 stream_buffer_empty( s, stream_buffered_size( s ) );
1799 if( ASeek( s, i_pos ) )
1800 return VLC_EGENERIC;
1805 /****************************************************************************
1807 ****************************************************************************/
1809 * Read from the stream untill first newline.
1810 * \param s Stream handle to read from
1811 * \return A pointer to the allocated output string. You need to free this when you are done.
1813 #define STREAM_PROBE_LINE 2048
1814 #define STREAM_LINE_MAX (2048*100)
1815 char *stream_ReadLine( stream_t *s )
1817 char *p_line = NULL;
1818 int i_line = 0, i_read = 0;
1820 while( i_read < STREAM_LINE_MAX )
1823 const uint8_t *p_data;
1827 /* Probe new data */
1828 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1829 if( i_data <= 0 ) break; /* No more data */
1832 i_pos = stream_Tell( s );
1833 if( i_pos == 0 && i_data >= 3 )
1836 const char *psz_encoding = NULL;
1838 if( !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
1840 psz_encoding = "UTF-8";
1843 else if( !memcmp( p_data, "\xFF\xFE", 2 ) )
1845 psz_encoding = "UTF-16LE";
1846 s->b_little_endian = true;
1847 s->i_char_width = 2;
1850 else if( !memcmp( p_data, "\xFE\xFF", 2 ) )
1852 psz_encoding = "UTF-16BE";
1853 s->i_char_width = 2;
1857 /* Seek past the BOM */
1860 stream_Seek( s, i_bom_size );
1861 p_data += i_bom_size;
1862 i_data -= i_bom_size;
1865 /* Open the converter if we need it */
1866 if( psz_encoding != NULL )
1868 input_thread_t *p_input;
1869 msg_Dbg( s, "%s BOM detected", psz_encoding );
1870 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1871 if( s->i_char_width > 1 )
1873 s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1874 if( s->conv == (vlc_iconv_t)-1 )
1876 msg_Err( s, "iconv_open failed" );
1879 if( p_input != NULL)
1881 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1882 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1883 vlc_object_release( p_input );
1888 if( i_data % s->i_char_width )
1890 /* keep i_char_width boundary */
1891 i_data = i_data - ( i_data % s->i_char_width );
1892 msg_Warn( s, "the read is not i_char_width compatible");
1898 /* Check if there is an EOL */
1899 if( s->i_char_width == 1 )
1901 /* UTF-8: 0A <LF> */
1902 psz_eol = memchr( p_data, '\n', i_data );
1906 const uint8_t *p = p_data;
1907 const uint8_t *p_last = p + i_data - s->i_char_width;
1909 if( s->i_char_width == 2 )
1911 if( s->b_little_endian == true)
1913 /* UTF-16LE: 0A 00 <LF> */
1914 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1919 /* UTF-16BE: 00 0A <LF> */
1920 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1931 psz_eol = (char *)p + ( s->i_char_width - 1 );
1937 i_data = (psz_eol - (char *)p_data) + 1;
1938 p_line = realloc( p_line, i_line + i_data + s->i_char_width ); /* add \0 */
1941 i_data = stream_Read( s, &p_line[i_line], i_data );
1942 if( i_data <= 0 ) break; /* Hmmm */
1943 i_line += i_data - s->i_char_width; /* skip \n */;
1946 /* We have our line */
1950 /* Read data (+1 for easy \0 append) */
1951 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->i_char_width );
1954 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1955 if( i_data <= 0 ) break; /* Hmmm */
1963 for( j = 0; j < s->i_char_width; j++ )
1965 p_line[i_line + j] = '\0';
1967 i_line += s->i_char_width; /* the added \0 */
1968 if( s->i_char_width > 1 )
1970 size_t i_in = 0, i_out = 0;
1971 const char * p_in = NULL;
1972 char * p_out = NULL;
1973 char * psz_new_line = NULL;
1976 psz_new_line = malloc( i_line );
1977 if( psz_new_line == NULL )
1979 i_in = i_out = (size_t)i_line;
1981 p_out = psz_new_line;
1983 if( vlc_iconv( s->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1985 msg_Err( s, "iconv failed" );
1986 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1989 p_line = psz_new_line;
1990 i_line = (size_t)i_line - i_out; /* does not include \0 */
1993 /* Remove trailing LF/CR */
1994 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1995 p_line[i_line-2] == '\n') ) i_line--;
1997 /* Make sure the \0 is there */
1998 p_line[i_line-1] = '\0';
2005 /* We failed to read any data, probably EOF */
2007 if( s->conv != (vlc_iconv_t)(-1) ) vlc_iconv_close( s->conv );
2011 /****************************************************************************
2012 * Access reading/seeking wrappers to handle concatenated streams.
2013 ****************************************************************************/
2014 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read )
2016 stream_sys_t *p_sys = s->p_sys;
2017 access_t *p_access = p_sys->p_access;
2018 input_thread_t *p_input = NULL;
2019 int i_read_orig = i_read;
2022 if( s->p_parent && s->p_parent->p_parent &&
2023 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
2024 p_input = (input_thread_t *)s->p_parent->p_parent;
2026 if( !p_sys->i_list )
2028 i_read = p_access->pf_read( p_access, p_read, i_read );
2029 if( p_access->b_die )
2030 vlc_object_kill( s );
2033 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2034 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
2036 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2037 (float)i_total, NULL );
2038 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2039 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2044 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
2046 if( p_access->b_die )
2047 vlc_object_kill( s );
2049 /* If we reached an EOF then switch to the next stream in the list */
2050 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
2052 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
2053 access_t *p_list_access;
2055 msg_Dbg( s, "opening input `%s'", psz_name );
2057 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
2059 if( !p_list_access ) return 0;
2061 if( p_sys->p_list_access != p_access )
2062 access_Delete( p_sys->p_list_access );
2064 p_sys->p_list_access = p_list_access;
2066 /* We have to read some data */
2067 return AReadStream( s, p_read, i_read_orig );
2070 /* Update read bytes in input */
2073 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2074 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
2075 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2076 (float)i_total, NULL );
2077 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2078 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2083 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
2085 stream_sys_t *p_sys = s->p_sys;
2086 access_t *p_access = p_sys->p_access;
2087 input_thread_t *p_input = NULL;
2092 if( s->p_parent && s->p_parent->p_parent &&
2093 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
2094 p_input = (input_thread_t *)s->p_parent->p_parent;
2096 if( !p_sys->i_list )
2098 p_block = p_access->pf_block( p_access );
2099 if( p_access->b_die )
2100 vlc_object_kill( s );
2101 if( pb_eof ) *pb_eof = p_access->info.b_eof;
2102 if( p_input && p_block && libvlc_stats (p_access) )
2104 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2105 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
2106 p_block->i_buffer, &i_total );
2107 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2108 (float)i_total, NULL );
2109 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
2110 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2115 p_block = p_sys->p_list_access->pf_block( p_sys->p_list_access );
2116 if( p_access->b_die )
2117 vlc_object_kill( s );
2118 b_eof = p_sys->p_list_access->info.b_eof;
2119 if( pb_eof ) *pb_eof = b_eof;
2121 /* If we reached an EOF then switch to the next stream in the list */
2122 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
2124 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
2125 access_t *p_list_access;
2127 msg_Dbg( s, "opening input `%s'", psz_name );
2129 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
2131 if( !p_list_access ) return 0;
2133 if( p_sys->p_list_access != p_access )
2134 access_Delete( p_sys->p_list_access );
2136 p_sys->p_list_access = p_list_access;
2138 /* We have to read some data */
2139 return AReadBlock( s, pb_eof );
2145 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2146 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
2147 p_block->i_buffer, &i_total );
2148 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2149 (float)i_total, NULL );
2150 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
2152 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2158 static int ASeek( stream_t *s, int64_t i_pos )
2160 stream_sys_t *p_sys = s->p_sys;
2161 access_t *p_access = p_sys->p_access;
2163 /* Check which stream we need to access */
2169 access_t *p_list_access = 0;
2171 for( i = 0; i < p_sys->i_list - 1; i++ )
2173 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
2174 i_size += p_sys->list[i]->i_size;
2176 psz_name = p_sys->list[i]->psz_path;
2178 if( i != p_sys->i_list_index )
2179 msg_Dbg( s, "opening input `%s'", psz_name );
2181 if( i != p_sys->i_list_index && i != 0 )
2184 access_New( s, p_access->psz_access, "", psz_name );
2186 else if( i != p_sys->i_list_index )
2188 p_list_access = p_access;
2193 if( p_sys->p_list_access != p_access )
2194 access_Delete( p_sys->p_list_access );
2196 p_sys->p_list_access = p_list_access;
2199 p_sys->i_list_index = i;
2200 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
2204 return p_access->pf_seek( p_access, i_pos );
2209 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
2210 * "p_read" is NULL then data are skipped instead of read. The return
2211 * value is the real numbers of bytes read/skip. If this value is less
2212 * than i_read that means that it's the end of the stream.
2214 int stream_Read( stream_t *s, void *p_read, int i_read )
2216 return s->pf_read( s, p_read, i_read );
2220 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
2221 * \return The real numbers of valid bytes, if it's less
2222 * or equal to 0, *pp_peek is invalid.
2223 * \note pp_peek is a pointer to internal buffer and it will be invalid as
2224 * soons as other stream_* functions are called.
2225 * \note Due to input limitation, it could be less than i_peek without meaning
2226 * the end of the stream (but only when you have i_peek >=
2227 * p_input->i_bufsize)
2229 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
2231 return s->pf_peek( s, pp_peek, i_peek );
2235 * Use to control the "stream_t *". Look at #stream_query_e for
2236 * possible "i_query" value and format arguments. Return VLC_SUCCESS
2237 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
2239 int stream_vaControl( stream_t *s, int i_query, va_list args )
2241 return s->pf_control( s, i_query, args );
2247 void stream_Delete( stream_t *s )
2252 int stream_Control( stream_t *s, int i_query, ... )
2258 return VLC_EGENERIC;
2260 va_start( args, i_query );
2261 i_result = s->pf_control( s, i_query, args );
2267 * Read "i_size" bytes and store them in a block_t.
2268 * It always read i_size bytes unless you are at the end of the stream
2269 * where it return what is available.
2271 block_t *stream_Block( stream_t *s, int i_size )
2273 if( i_size <= 0 ) return NULL;
2275 /* emulate block read */
2276 block_t *p_bk = block_New( s, i_size );
2279 int i_read = stream_Read( s, p_bk->p_buffer, i_size );
2282 p_bk->i_buffer = i_read;
2285 block_Release( p_bk );