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
54 * One linked list of data read
56 * More complex scheme using mutliple track to avoid seeking
57 * - using directly the access (only indirection for peeking).
58 * This method is known to introduce much less latency.
59 * It should probably defaulted (instead of the stream method (2)).
62 /* How many tracks we have, currently only used for stream mode */
63 #ifdef OPTIMIZE_MEMORY
64 # define STREAM_CACHE_TRACK 1
65 /* Max size of our cache 128Ko per track */
66 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
68 # define STREAM_CACHE_TRACK 3
69 /* Max size of our cache 4Mo per track */
70 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
73 /* How many data we try to prebuffer
74 * XXX it should be small to avoid useless latency but big enough for
75 * efficient demux probing */
76 #define STREAM_CACHE_PREBUFFER_SIZE (128)
78 /* Method1: Simple, for pf_block.
79 * We get blocks and put them in the linked list.
80 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
83 /* Method2: A bit more complex, for pf_read
84 * - We use ring buffers, only one if unseekable, all if seekable
85 * - Upon seek date current ring, then search if one ring match the pos,
86 * yes: switch to it, seek the access to match the end of the ring
87 * no: search the ring with i_end the closer to i_pos,
88 * if close enough, read data and use this ring
89 * else use the oldest ring, seek and use it.
91 * TODO: - with access non seekable: use all space available for only one ring, but
92 * we have to support seekable/non-seekable switch on the fly.
93 * - compute a good value for i_read_size
96 #define STREAM_READ_ATONCE 1024
97 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
121 } stream_read_method_t;
127 stream_read_method_t method; /* method to use */
129 int64_t i_pos; /* Current reading offset */
131 /* Method 1: pf_block */
134 int64_t i_start; /* Offset of block for p_first */
135 int64_t i_offset; /* Offset for data in p_current */
136 block_t *p_current; /* Current block */
138 int i_size; /* Total amount of data in the list */
144 /* Method 2: for pf_read */
147 int i_offset; /* Buffer offset in the current track */
148 int i_tk; /* Current track */
149 stream_track_t tk[STREAM_CACHE_TRACK];
155 int i_used; /* Used since last read */
160 /* Peek temporary buffer */
164 /* Stat for both method */
167 bool b_fastseek; /* From access */
169 /* Stat about reading data */
170 int64_t i_read_count;
174 /* Stat about seek */
182 access_entry_t **list;
184 access_t *p_list_access;
186 /* Preparse mode ? */
194 FILE *f; /* TODO it could be replaced by access_output_t one day */
200 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read );
201 static int AStreamPeekBlock( stream_t *s, const uint8_t **p_peek, unsigned int i_read );
202 static int AStreamSeekBlock( stream_t *s, int64_t i_pos );
203 static void AStreamPrebufferBlock( stream_t *s );
204 static block_t *AReadBlock( stream_t *s, bool *pb_eof );
207 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read );
208 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read );
209 static int AStreamSeekStream( stream_t *s, int64_t i_pos );
210 static void AStreamPrebufferStream( stream_t *s );
211 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read );
214 static int AStreamControl( stream_t *s, int i_query, va_list );
215 static void AStreamDestroy( stream_t *s );
216 static void UStreamDestroy( stream_t *s );
217 static int ASeek( stream_t *s, int64_t i_pos );
218 static int ARecordSetState( stream_t *s, bool b_record, const char *psz_extension );
219 static void ARecordWrite( stream_t *s, const uint8_t *p_buffer, size_t i_buffer );
221 /****************************************************************************
222 * stream_CommonNew: create an empty stream structure
223 ****************************************************************************/
224 stream_t *stream_CommonNew( vlc_object_t *p_obj )
226 stream_t *s = (stream_t *)vlc_custom_create( p_obj, sizeof(*s),
227 VLC_OBJECT_GENERIC, "stream" );
232 s->p_text = malloc( sizeof(*s->p_text) );
235 vlc_object_release( s );
239 /* UTF16 and UTF32 text file conversion */
240 s->p_text->conv = (vlc_iconv_t)(-1);
241 s->p_text->i_char_width = 1;
242 s->p_text->b_little_endian = false;
246 void stream_CommonDelete( stream_t *s )
250 if( s->p_text->conv != (vlc_iconv_t)(-1) )
251 vlc_iconv_close( s->p_text->conv );
254 vlc_object_release( s );
257 /****************************************************************************
258 * stream_UrlNew: create a stream from a access
259 ****************************************************************************/
260 stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
262 const char *psz_access, *psz_demux;
270 char psz_dup[strlen( psz_url ) + 1];
271 strcpy( psz_dup, psz_url );
272 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
274 /* Now try a real access */
275 p_access = access_New( p_parent, psz_access, psz_demux, psz_path );
277 if( p_access == NULL )
279 msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
283 if( !( p_res = stream_AccessNew( p_access, true ) ) )
285 access_Delete( p_access );
289 p_res->pf_destroy = UStreamDestroy;
293 stream_t *stream_AccessNew( access_t *p_access, bool b_quick )
295 stream_t *s = stream_CommonNew( VLC_OBJECT(p_access) );
297 char *psz_list = NULL;
302 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
305 stream_CommonDelete( s );
309 /* Attach it now, needed for b_die */
310 vlc_object_attach( s, p_access );
312 s->pf_read = NULL; /* Set up later */
314 s->pf_control = AStreamControl;
315 s->pf_destroy = AStreamDestroy;
318 p_sys->p_access = p_access;
319 if( p_access->pf_block )
320 p_sys->method = STREAM_METHOD_BLOCK;
322 p_sys->method = STREAM_METHOD_STREAM;
324 p_sys->record.b_active = false;
326 p_sys->i_pos = p_access->info.i_pos;
329 access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
330 p_sys->stat.i_bytes = 0;
331 p_sys->stat.i_read_time = 0;
332 p_sys->stat.i_read_count = 0;
333 p_sys->stat.i_seek_count = 0;
334 p_sys->stat.i_seek_time = 0;
338 p_sys->i_list_index = 0;
339 p_sys->p_list_access = 0;
341 p_sys->b_quick = b_quick;
343 /* Get the additional list of inputs if any (for concatenation) */
344 if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
346 access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
347 if( p_entry == NULL )
349 char *psz_name, *psz_parser = psz_name = psz_list;
351 p_sys->p_list_access = p_access;
352 p_entry->i_size = p_access->info.i_size;
353 p_entry->psz_path = strdup( p_access->psz_path );
354 if( p_entry->psz_path == NULL )
359 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
360 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
361 p_entry->psz_path, p_access->info.i_size );
363 while( psz_name && *psz_name )
365 psz_parser = strchr( psz_name, ',' );
366 if( psz_parser ) *psz_parser = 0;
368 psz_name = strdup( psz_name );
371 access_t *p_tmp = access_New( p_access, p_access->psz_access,
376 psz_name = psz_parser;
377 if( psz_name ) psz_name++;
381 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
382 psz_name, p_tmp->info.i_size );
384 p_entry = malloc( sizeof(access_entry_t) );
385 if( p_entry == NULL )
387 p_entry->i_size = p_tmp->info.i_size;
388 p_entry->psz_path = psz_name;
389 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
391 access_Delete( p_tmp );
394 psz_name = psz_parser;
395 if( psz_name ) psz_name++;
398 FREENULL( psz_list );
402 p_sys->p_peek = NULL;
404 if( p_sys->method == STREAM_METHOD_BLOCK )
406 msg_Dbg( s, "Using AStream*Block" );
407 s->pf_read = AStreamReadBlock;
408 s->pf_peek = AStreamPeekBlock;
410 /* Init all fields of p_sys->block */
411 p_sys->block.i_start = p_sys->i_pos;
412 p_sys->block.i_offset = 0;
413 p_sys->block.p_current = NULL;
414 p_sys->block.i_size = 0;
415 p_sys->block.p_first = NULL;
416 p_sys->block.pp_last = &p_sys->block.p_first;
418 /* Do the prebuffering */
419 AStreamPrebufferBlock( s );
421 if( p_sys->block.i_size <= 0 )
423 msg_Err( s, "cannot pre fill buffer" );
431 assert( p_sys->method == STREAM_METHOD_STREAM );
433 msg_Dbg( s, "Using AStream*Stream" );
435 s->pf_read = AStreamReadStream;
436 s->pf_peek = AStreamPeekStream;
438 /* Allocate/Setup our tracks */
439 p_sys->stream.i_offset = 0;
440 p_sys->stream.i_tk = 0;
441 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
442 if( p_sys->stream.p_buffer == NULL )
444 p_sys->stream.i_used = 0;
445 access_Control( p_access, ACCESS_GET_MTU,
446 &p_sys->stream.i_read_size );
447 if( p_sys->stream.i_read_size <= 0 )
448 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
449 else if( p_sys->stream.i_read_size <= 256 )
450 p_sys->stream.i_read_size = 256;
452 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
454 p_sys->stream.tk[i].i_date = 0;
455 p_sys->stream.tk[i].i_start = p_sys->i_pos;
456 p_sys->stream.tk[i].i_end = p_sys->i_pos;
457 p_sys->stream.tk[i].p_buffer=
458 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
461 /* Do the prebuffering */
462 AStreamPrebufferStream( s );
464 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
466 msg_Err( s, "cannot pre fill buffer" );
474 if( p_sys->method == STREAM_METHOD_BLOCK )
480 free( p_sys->stream.p_buffer );
482 while( p_sys->i_list > 0 )
483 free( p_sys->list[--(p_sys->i_list)] );
487 vlc_object_detach( s );
488 stream_CommonDelete( s );
492 /****************************************************************************
494 ****************************************************************************/
495 static void AStreamDestroy( stream_t *s )
497 stream_sys_t *p_sys = s->p_sys;
499 vlc_object_detach( s );
501 if( p_sys->record.b_active )
502 ARecordSetState( s, false, NULL );
504 if( p_sys->method == STREAM_METHOD_BLOCK )
505 block_ChainRelease( p_sys->block.p_first );
507 free( p_sys->stream.p_buffer );
509 free( p_sys->p_peek );
511 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
512 access_Delete( p_sys->p_list_access );
514 while( p_sys->i_list-- )
516 free( p_sys->list[p_sys->i_list]->psz_path );
517 free( p_sys->list[p_sys->i_list] );
523 stream_CommonDelete( s );
526 static void UStreamDestroy( stream_t *s )
528 access_t *p_access = (access_t *)s->p_parent;
530 access_Delete( p_access );
533 /****************************************************************************
534 * AStreamControlReset:
535 ****************************************************************************/
536 static void AStreamControlReset( stream_t *s )
538 stream_sys_t *p_sys = s->p_sys;
540 p_sys->i_pos = p_sys->p_access->info.i_pos;
542 if( p_sys->method == STREAM_METHOD_BLOCK )
544 block_ChainRelease( p_sys->block.p_first );
546 /* Init all fields of p_sys->block */
547 p_sys->block.i_start = p_sys->i_pos;
548 p_sys->block.i_offset = 0;
549 p_sys->block.p_current = NULL;
550 p_sys->block.i_size = 0;
551 p_sys->block.p_first = NULL;
552 p_sys->block.pp_last = &p_sys->block.p_first;
554 /* Do the prebuffering */
555 AStreamPrebufferBlock( s );
561 assert( p_sys->method == STREAM_METHOD_STREAM );
563 /* Setup our tracks */
564 p_sys->stream.i_offset = 0;
565 p_sys->stream.i_tk = 0;
566 p_sys->stream.i_used = 0;
568 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
570 p_sys->stream.tk[i].i_date = 0;
571 p_sys->stream.tk[i].i_start = p_sys->i_pos;
572 p_sys->stream.tk[i].i_end = p_sys->i_pos;
575 /* Do the prebuffering */
576 AStreamPrebufferStream( s );
580 /****************************************************************************
581 * AStreamControlUpdate:
582 ****************************************************************************/
583 static void AStreamControlUpdate( stream_t *s )
585 stream_sys_t *p_sys = s->p_sys;
587 p_sys->i_pos = p_sys->p_access->info.i_pos;
592 for( i = 0; i < p_sys->i_list_index; i++ )
594 p_sys->i_pos += p_sys->list[i]->i_size;
599 /****************************************************************************
601 ****************************************************************************/
602 static int AStreamControl( stream_t *s, int i_query, va_list args )
604 stream_sys_t *p_sys = s->p_sys;
605 access_t *p_access = p_sys->p_access;
609 const char *psz_string;
610 int64_t *pi_64, i_64;
615 case STREAM_GET_SIZE:
616 pi_64 = (int64_t*)va_arg( args, int64_t * );
617 if( s->p_sys->i_list )
621 for( i = 0; i < s->p_sys->i_list; i++ )
622 *pi_64 += s->p_sys->list[i]->i_size;
625 *pi_64 = p_access->info.i_size;
628 case STREAM_CAN_SEEK:
629 p_bool = (bool*)va_arg( args, bool * );
630 access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
633 case STREAM_CAN_FASTSEEK:
634 p_bool = (bool*)va_arg( args, bool * );
635 access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
638 case STREAM_GET_POSITION:
639 pi_64 = (int64_t*)va_arg( args, int64_t * );
640 *pi_64 = p_sys->i_pos;
643 case STREAM_SET_POSITION:
644 i_64 = (int64_t)va_arg( args, int64_t );
645 switch( p_sys->method )
647 case STREAM_METHOD_BLOCK:
648 return AStreamSeekBlock( s, i_64 );
649 case STREAM_METHOD_STREAM:
650 return AStreamSeekStream( s, i_64 );
659 case STREAM_CONTROL_ACCESS:
661 i_int = (int) va_arg( args, int );
662 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
663 i_int != ACCESS_SET_PRIVATE_ID_CA &&
664 i_int != ACCESS_GET_PRIVATE_ID_STATE &&
665 i_int != ACCESS_SET_TITLE &&
666 i_int != ACCESS_SET_SEEKPOINT )
668 msg_Err( s, "Hey, what are you thinking ?"
669 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
672 int i_ret = access_vaControl( p_access, i_int, args );
673 if( i_int == ACCESS_SET_TITLE || i_int == ACCESS_SET_SEEKPOINT )
674 AStreamControlReset( s );
678 case STREAM_UPDATE_SIZE:
679 AStreamControlUpdate( s );
682 case STREAM_GET_CONTENT_TYPE:
683 return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
684 va_arg( args, char ** ) );
685 case STREAM_SET_RECORD_STATE:
686 b_bool = (bool)va_arg( args, int );
689 psz_string = (const char*)va_arg( args, const char* );
690 return ARecordSetState( s, b_bool, psz_string );
693 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
699 /****************************************************************************
700 * ARecord*: record stream functions
701 ****************************************************************************/
702 static int ARecordStart( stream_t *s, const char *psz_extension )
704 stream_sys_t *p_sys = s->p_sys;
711 psz_extension = "dat";
714 char *psz_path = var_CreateGetString( s, "input-record-path" );
715 if( !psz_path || *psz_path == '\0' )
718 psz_path = strdup( config_GetHomeDir() );
725 * TODO allow prefix configuration */
726 psz_file = input_CreateFilename( VLC_OBJECT(s), psz_path, INPUT_RECORD_PREFIX, psz_extension );
733 f = utf8_fopen( psz_file, "wb" );
739 msg_Dbg( s, "Recording into %s", psz_file );
744 p_sys->record.b_active = true;
745 p_sys->record.b_error = false;
748 static int ARecordStop( stream_t *s )
750 stream_sys_t *p_sys = s->p_sys;
752 assert( p_sys->record.b_active );
754 msg_Dbg( s, "Recording completed" );
755 fclose( p_sys->record.f );
756 p_sys->record.b_active = false;
760 static int ARecordSetState( stream_t *s, bool b_record, const char *psz_extension )
762 stream_sys_t *p_sys = s->p_sys;
764 if( !!p_sys->record.b_active == !!b_record )
768 return ARecordStart( s, psz_extension );
770 return ARecordStop( s );
772 static void ARecordWrite( stream_t *s, const uint8_t *p_buffer, size_t i_buffer )
774 stream_sys_t *p_sys = s->p_sys;
776 assert( p_sys->record.b_active );
780 const bool b_previous_error = p_sys->record.b_error;
781 const size_t i_written = fwrite( p_buffer, 1, i_buffer, p_sys->record.f );
783 p_sys->record.b_error = i_written != i_buffer;
785 /* TODO maybe a intf_UserError or something like that ? */
786 if( p_sys->record.b_error && !b_previous_error )
787 msg_Err( s, "Failed to record data (begin)" );
788 else if( !p_sys->record.b_error && b_previous_error )
789 msg_Err( s, "Failed to record data (end)" );
793 /****************************************************************************
795 ****************************************************************************/
796 static void AStreamPrebufferBlock( stream_t *s )
798 stream_sys_t *p_sys = s->p_sys;
799 access_t *p_access = p_sys->p_access;
804 msg_Dbg( s, "pre buffering" );
808 const int64_t i_date = mdate();
812 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE )
817 p_sys->stat.i_bytes = p_sys->block.i_size;
818 p_sys->stat.i_read_time = i_date - i_start;
819 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
820 (p_sys->stat.i_read_time + 1);
822 msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
823 "%"PRId64" kbytes/s",
825 p_sys->stat.i_read_time / INT64_C(1000000),
831 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
840 /* Append the block */
841 p_sys->block.i_size += b->i_buffer;
842 *p_sys->block.pp_last = b;
843 p_sys->block.pp_last = &b->p_next;
845 p_sys->stat.i_read_count++;
852 msg_Dbg( s, "received first data after %d ms",
853 (int)((i_first-i_start)/1000) );
857 p_sys->block.p_current = p_sys->block.p_first;
860 static int AStreamRefillBlock( stream_t *s );
862 static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
864 stream_sys_t *p_sys = s->p_sys;
866 uint8_t *p_data = p_read;
867 uint8_t *p_record = p_data;
868 unsigned int i_data = 0;
871 if( p_sys->block.p_current == NULL )
874 if( p_sys->record.b_active && !p_data )
875 p_record = p_data = malloc( i_read );
879 /* seek within this stream if possible, else use plain old read and discard */
880 stream_sys_t *p_sys = s->p_sys;
881 access_t *p_access = p_sys->p_access;
883 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
885 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
888 while( i_data < i_read )
891 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
892 unsigned int i_copy = __MIN( (unsigned int)__MAX(i_current,0), i_read - i_data);
898 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
904 p_sys->block.i_offset += i_copy;
905 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
907 /* Current block is now empty, switch to next */
908 if( p_sys->block.p_current )
910 p_sys->block.i_offset = 0;
911 p_sys->block.p_current = p_sys->block.p_current->p_next;
913 /*Get a new block if needed */
914 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
921 if( p_sys->record.b_active )
923 if( i_data > 0 && p_record != NULL)
924 ARecordWrite( s, p_record, i_data );
929 p_sys->i_pos += i_data;
933 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
935 stream_sys_t *p_sys = s->p_sys;
937 unsigned int i_data = 0;
939 unsigned int i_offset;
941 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
943 /* We can directly give a pointer over our buffer */
944 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
946 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
950 /* We need to create a local copy */
951 if( p_sys->i_peek < i_read )
953 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
959 p_sys->i_peek = i_read;
962 /* Fill enough data */
963 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
966 block_t **pp_last = p_sys->block.pp_last;
968 if( AStreamRefillBlock( s ) ) break;
970 /* Our buffer are probably filled enough, don't try anymore */
971 if( pp_last == p_sys->block.pp_last ) break;
974 /* Copy what we have */
975 b = p_sys->block.p_current;
976 i_offset = p_sys->block.i_offset;
977 p_data = p_sys->p_peek;
979 while( b && i_data < i_read )
981 unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
982 int i_copy = __MIN( i_current, i_read - i_data );
984 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
989 if( i_offset >= b->i_buffer )
996 *pp_peek = p_sys->p_peek;
1000 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
1002 stream_sys_t *p_sys = s->p_sys;
1003 access_t *p_access = p_sys->p_access;
1004 int64_t i_offset = i_pos - p_sys->block.i_start;
1007 /* We already have thoses data, just update p_current/i_offset */
1008 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
1010 block_t *b = p_sys->block.p_first;
1013 while( i_current + b->i_buffer < i_offset )
1015 i_current += b->i_buffer;
1019 p_sys->block.p_current = b;
1020 p_sys->block.i_offset = i_offset - i_current;
1022 p_sys->i_pos = i_pos;
1027 /* We may need to seek or to read data */
1031 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1035 msg_Err( s, "backward seeking impossible (access not seekable)" );
1036 return VLC_EGENERIC;
1043 bool b_aseek, b_aseekfast;
1045 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1046 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
1051 msg_Warn( s, "%"PRId64" bytes need to be skipped "
1052 "(access non seekable)",
1053 i_offset - p_sys->block.i_size );
1057 int64_t i_skip = i_offset - p_sys->block.i_size;
1059 /* Avg bytes per packets */
1060 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
1061 /* TODO compute a seek cost instead of fixed threshold */
1062 int i_th = b_aseekfast ? 1 : 5;
1064 if( i_skip <= i_th * i_avg &&
1065 i_skip < STREAM_CACHE_SIZE )
1070 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
1071 b_seek, i_th*i_avg, i_skip );
1077 int64_t i_start, i_end;
1078 /* Do the access seek */
1080 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1084 block_ChainRelease( p_sys->block.p_first );
1087 p_sys->block.i_start = p_sys->i_pos = i_pos;
1088 p_sys->block.i_offset = 0;
1089 p_sys->block.p_current = NULL;
1090 p_sys->block.i_size = 0;
1091 p_sys->block.p_first = NULL;
1092 p_sys->block.pp_last = &p_sys->block.p_first;
1094 /* Refill a block */
1095 if( AStreamRefillBlock( s ) )
1096 return VLC_EGENERIC;
1099 p_sys->stat.i_seek_time += i_end - i_start;
1100 p_sys->stat.i_seek_count++;
1107 /* Read and skip enough data */
1108 if( AStreamRefillBlock( s ) )
1109 return VLC_EGENERIC;
1111 while( p_sys->block.p_current &&
1112 p_sys->i_pos + p_sys->block.p_current->i_buffer - p_sys->block.i_offset < i_pos )
1114 p_sys->i_pos += p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
1115 p_sys->block.p_current = p_sys->block.p_current->p_next;
1116 p_sys->block.i_offset = 0;
1119 while( p_sys->block.i_start + p_sys->block.i_size < i_pos );
1121 p_sys->block.i_offset = i_pos - p_sys->i_pos;
1122 p_sys->i_pos = i_pos;
1127 return VLC_EGENERIC;
1130 static int AStreamRefillBlock( stream_t *s )
1132 stream_sys_t *p_sys = s->p_sys;
1133 int64_t i_start, i_stop;
1137 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1138 p_sys->block.p_first != p_sys->block.p_current )
1140 block_t *b = p_sys->block.p_first;
1142 p_sys->block.i_start += b->i_buffer;
1143 p_sys->block.i_size -= b->i_buffer;
1144 p_sys->block.p_first = b->p_next;
1148 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1149 p_sys->block.p_current == p_sys->block.p_first &&
1150 p_sys->block.p_current->p_next ) /* At least 2 packets */
1152 /* Enough data, don't read more */
1156 /* Now read a new block */
1163 return VLC_EGENERIC;
1166 if( ( b = AReadBlock( s, &b_eof ) ) )
1169 return VLC_EGENERIC;
1176 /* Append the block */
1177 p_sys->block.i_size += b->i_buffer;
1178 *p_sys->block.pp_last = b;
1179 p_sys->block.pp_last = &b->p_next;
1182 if( p_sys->block.p_current == NULL )
1183 p_sys->block.p_current = b;
1186 p_sys->stat.i_bytes += b->i_buffer;
1187 p_sys->stat.i_read_time += i_stop - i_start;
1188 p_sys->stat.i_read_count++;
1197 /****************************************************************************
1199 ****************************************************************************/
1200 static int AStreamRefillStream( stream_t *s );
1202 static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read )
1204 stream_sys_t *p_sys = s->p_sys;
1205 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1207 uint8_t *p_data = (uint8_t *)p_read;
1208 uint8_t *p_record = p_data;
1209 unsigned int i_data = 0;
1211 if( tk->i_start >= tk->i_end )
1214 if( p_sys->record.b_active && !p_data )
1215 p_record = p_data = malloc( i_read );
1217 if( p_data == NULL )
1219 /* seek within this stream if possible, else use plain old read and discard */
1220 stream_sys_t *p_sys = s->p_sys;
1221 access_t *p_access = p_sys->p_access;
1223 /* seeking after EOF is not what we want */
1224 if( !( p_access->info.b_eof ) )
1227 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1229 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1234 msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1235 " offset=%d end=%"PRId64,
1236 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1237 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1240 while( i_data < i_read )
1242 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1243 STREAM_CACHE_TRACK_SIZE;
1244 unsigned int i_current =
1245 __MAX(0,__MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1246 STREAM_CACHE_TRACK_SIZE - i_off ));
1247 int i_copy = __MIN( i_current, i_read - i_data );
1249 if( i_copy <= 0 ) break; /* EOF */
1252 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1255 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1259 p_sys->stream.i_offset += i_copy;
1261 /* Update pos now */
1262 p_sys->i_pos += i_copy;
1265 p_sys->stream.i_used += i_copy;
1266 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1267 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1269 if( p_sys->stream.i_used < i_read - i_data )
1270 p_sys->stream.i_used = __MIN( i_read - i_data, STREAM_READ_ATONCE * 10 );
1272 if( AStreamRefillStream( s ) )
1275 if( tk->i_start >= tk->i_end ) break;
1280 if( p_sys->record.b_active )
1282 if( i_data > 0 && p_record != NULL)
1283 ARecordWrite( s, p_record, i_data );
1291 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
1293 stream_sys_t *p_sys = s->p_sys;
1294 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1297 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1300 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1301 "start=%"PRId64" offset=%d end=%"PRId64,
1302 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1303 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1306 /* Avoid problem, but that should *never* happen */
1307 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1308 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1310 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1312 if( p_sys->stream.i_used <= 1 )
1314 /* Be sure we will read something */
1315 p_sys->stream.i_used += i_read -
1316 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1318 if( AStreamRefillStream( s ) ) break;
1321 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1322 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1324 /* Now, direct pointer or a copy ? */
1325 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1326 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1328 *pp_peek = &tk->p_buffer[i_off];
1332 if( p_sys->i_peek < i_read )
1334 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1335 if( !p_sys->p_peek )
1340 p_sys->i_peek = i_read;
1343 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1344 STREAM_CACHE_TRACK_SIZE - i_off );
1345 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1346 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1348 *pp_peek = p_sys->p_peek;
1352 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1354 stream_sys_t *p_sys = s->p_sys;
1355 access_t *p_access = p_sys->p_access;
1363 msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1364 " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1365 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1366 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1367 p_sys->stream.i_offset,
1368 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1372 /* Seek in our current track ? */
1373 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1374 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1376 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1378 msg_Dbg( s, "AStreamSeekStream: current track" );
1380 p_sys->i_pos = i_pos;
1381 p_sys->stream.i_offset = i_pos - tk->i_start;
1383 /* If there is not enough data left in the track, refill */
1384 /* \todo How to get a correct value for
1385 * - refilling threshold
1386 * - how much to refill
1388 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1389 p_sys->stream.i_read_size )
1391 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1393 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1394 AStreamRefillStream( s );
1400 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1403 /* We can't do nothing */
1404 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1405 return VLC_EGENERIC;
1408 /* Date the current track */
1409 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1411 /* Try to reuse already read data */
1412 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1414 stream_track_t *tk = &p_sys->stream.tk[i];
1416 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1419 msg_Dbg( s, "AStreamSeekStream: reusing %d start=%"PRId64
1420 " end=%"PRId64, i, tk->i_start, tk->i_end );
1423 /* Seek at the end of the buffer */
1424 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1427 p_sys->i_pos = i_pos;
1428 p_sys->stream.i_tk = i;
1429 p_sys->stream.i_offset = i_pos - tk->i_start;
1431 if( p_sys->stream.i_used < 1024 )
1432 p_sys->stream.i_used = 1024;
1434 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1435 return VLC_EGENERIC;
1441 access_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1442 /* FIXME compute seek cost (instead of static 'stupid' value) */
1443 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1449 /* Search closest segment TODO */
1450 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1452 stream_track_t *tk = &p_sys->stream.tk[i];
1454 if( i_pos + i_maxth >= tk->i_start )
1456 msg_Dbg( s, "good segment before current pos, TODO" );
1458 if( i_pos - i_maxth <= tk->i_end )
1460 msg_Dbg( s, "good segment after current pos, TODO" );
1465 /* Nothing good, seek and choose oldest segment */
1466 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1467 p_sys->i_pos = i_pos;
1470 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1472 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1476 /* Reset the segment */
1477 p_sys->stream.i_tk = i_new;
1478 p_sys->stream.i_offset = 0;
1479 p_sys->stream.tk[i_new].i_start = i_pos;
1480 p_sys->stream.tk[i_new].i_end = i_pos;
1483 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1484 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1486 if( AStreamRefillStream( s ) )
1487 return VLC_EGENERIC;
1492 static int AStreamRefillStream( stream_t *s )
1494 stream_sys_t *p_sys = s->p_sys;
1495 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1497 /* We read but won't increase i_start after initial start + offset */
1499 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1500 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1501 bool b_read = false;
1502 int64_t i_start, i_stop;
1504 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1507 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1508 p_sys->stream.i_used, i_toread );
1512 while( i_toread > 0 )
1514 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1518 return VLC_EGENERIC;
1520 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1521 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1523 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1528 else if( i_read == 0 )
1531 return VLC_EGENERIC;
1537 tk->i_end += i_read;
1539 /* Windows of STREAM_CACHE_TRACK_SIZE */
1540 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1542 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1544 tk->i_start += i_invalid;
1545 p_sys->stream.i_offset -= i_invalid;
1549 p_sys->stream.i_used -= i_read;
1551 p_sys->stat.i_bytes += i_read;
1552 p_sys->stat.i_read_count++;
1556 p_sys->stat.i_read_time += i_stop - i_start;
1561 static void AStreamPrebufferStream( stream_t *s )
1563 stream_sys_t *p_sys = s->p_sys;
1564 access_t *p_access = p_sys->p_access;
1566 int64_t i_first = 0;
1569 msg_Dbg( s, "pre buffering" );
1573 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1575 int64_t i_date = mdate();
1578 if( s->b_die || tk->i_end >= STREAM_CACHE_PREBUFFER_SIZE )
1583 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1584 p_sys->stat.i_read_time = i_date - i_start;
1585 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1586 (p_sys->stat.i_read_time+1);
1588 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1589 "%"PRId64" kbytes/s",
1590 p_sys->stat.i_bytes,
1591 p_sys->stat.i_read_time / INT64_C(1000000),
1592 i_byterate / 1024 );
1597 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1598 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1599 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1602 else if( i_read == 0 )
1608 msg_Dbg( s, "received first data after %d ms",
1609 (int)((i_first-i_start)/1000) );
1612 tk->i_end += i_read;
1614 p_sys->stat.i_read_count++;
1618 /****************************************************************************
1620 ****************************************************************************/
1622 * Read from the stream untill first newline.
1623 * \param s Stream handle to read from
1624 * \return A pointer to the allocated output string. You need to free this when you are done.
1626 #define STREAM_PROBE_LINE 2048
1627 #define STREAM_LINE_MAX (2048*100)
1628 char *stream_ReadLine( stream_t *s )
1630 char *p_line = NULL;
1631 int i_line = 0, i_read = 0;
1633 while( i_read < STREAM_LINE_MAX )
1636 const uint8_t *p_data;
1640 /* Probe new data */
1641 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1642 if( i_data <= 0 ) break; /* No more data */
1645 i_pos = stream_Tell( s );
1646 if( i_pos == 0 && i_data >= 3 )
1649 const char *psz_encoding = NULL;
1651 if( !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
1653 psz_encoding = "UTF-8";
1656 else if( !memcmp( p_data, "\xFF\xFE", 2 ) )
1658 psz_encoding = "UTF-16LE";
1659 s->p_text->b_little_endian = true;
1660 s->p_text->i_char_width = 2;
1663 else if( !memcmp( p_data, "\xFE\xFF", 2 ) )
1665 psz_encoding = "UTF-16BE";
1666 s->p_text->i_char_width = 2;
1670 /* Seek past the BOM */
1673 stream_Seek( s, i_bom_size );
1674 p_data += i_bom_size;
1675 i_data -= i_bom_size;
1678 /* Open the converter if we need it */
1679 if( psz_encoding != NULL )
1681 msg_Dbg( s, "%s BOM detected", psz_encoding );
1682 if( s->p_text->i_char_width > 1 )
1684 s->p_text->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1685 if( s->p_text->conv == (vlc_iconv_t)-1 )
1687 msg_Err( s, "iconv_open failed" );
1691 /* FIXME that's UGLY */
1692 input_thread_t *p_input;
1693 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1694 if( p_input != NULL)
1696 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1697 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1698 vlc_object_release( p_input );
1703 if( i_data % s->p_text->i_char_width )
1705 /* keep i_char_width boundary */
1706 i_data = i_data - ( i_data % s->p_text->i_char_width );
1707 msg_Warn( s, "the read is not i_char_width compatible");
1713 /* Check if there is an EOL */
1714 if( s->p_text->i_char_width == 1 )
1716 /* UTF-8: 0A <LF> */
1717 psz_eol = memchr( p_data, '\n', i_data );
1721 const uint8_t *p = p_data;
1722 const uint8_t *p_last = p + i_data - s->p_text->i_char_width;
1724 if( s->p_text->i_char_width == 2 )
1726 if( s->p_text->b_little_endian == true)
1728 /* UTF-16LE: 0A 00 <LF> */
1729 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1734 /* UTF-16BE: 00 0A <LF> */
1735 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1746 psz_eol = (char *)p + ( s->p_text->i_char_width - 1 );
1752 i_data = (psz_eol - (char *)p_data) + 1;
1753 p_line = realloc( p_line, i_line + i_data + s->p_text->i_char_width ); /* add \0 */
1756 i_data = stream_Read( s, &p_line[i_line], i_data );
1757 if( i_data <= 0 ) break; /* Hmmm */
1758 i_line += i_data - s->p_text->i_char_width; /* skip \n */;
1761 /* We have our line */
1765 /* Read data (+1 for easy \0 append) */
1766 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->p_text->i_char_width );
1769 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1770 if( i_data <= 0 ) break; /* Hmmm */
1778 for( j = 0; j < s->p_text->i_char_width; j++ )
1780 p_line[i_line + j] = '\0';
1782 i_line += s->p_text->i_char_width; /* the added \0 */
1783 if( s->p_text->i_char_width > 1 )
1785 size_t i_in = 0, i_out = 0;
1786 const char * p_in = NULL;
1787 char * p_out = NULL;
1788 char * psz_new_line = NULL;
1791 psz_new_line = malloc( i_line );
1792 if( psz_new_line == NULL )
1794 i_in = i_out = (size_t)i_line;
1796 p_out = psz_new_line;
1798 if( vlc_iconv( s->p_text->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1800 msg_Err( s, "iconv failed" );
1801 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1804 p_line = psz_new_line;
1805 i_line = (size_t)i_line - i_out; /* does not include \0 */
1808 /* Remove trailing LF/CR */
1809 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1810 p_line[i_line-2] == '\n') ) i_line--;
1812 /* Make sure the \0 is there */
1813 p_line[i_line-1] = '\0';
1819 /* We failed to read any data, probably EOF */
1823 if( s->p_text->conv != (vlc_iconv_t)(-1) )
1824 vlc_iconv_close( s->p_text->conv );
1825 s->p_text->conv = (vlc_iconv_t)(-1);
1829 /****************************************************************************
1830 * Access reading/seeking wrappers to handle concatenated streams.
1831 ****************************************************************************/
1832 static int AReadStream( stream_t *s, void *p_read, unsigned int i_read )
1834 stream_sys_t *p_sys = s->p_sys;
1835 access_t *p_access = p_sys->p_access;
1836 input_thread_t *p_input = NULL;
1837 int i_read_orig = i_read;
1840 if( s->p_parent && s->p_parent->p_parent &&
1841 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1842 p_input = (input_thread_t *)s->p_parent->p_parent;
1844 if( !p_sys->i_list )
1846 i_read = p_access->pf_read( p_access, p_read, i_read );
1847 if( p_access->b_die )
1848 vlc_object_kill( s );
1851 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1852 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
1854 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1855 (float)i_total, NULL );
1856 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1857 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1862 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1864 if( p_access->b_die )
1865 vlc_object_kill( s );
1867 /* If we reached an EOF then switch to the next stream in the list */
1868 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1870 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1871 access_t *p_list_access;
1873 msg_Dbg( s, "opening input `%s'", psz_name );
1875 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1877 if( !p_list_access ) return 0;
1879 if( p_sys->p_list_access != p_access )
1880 access_Delete( p_sys->p_list_access );
1882 p_sys->p_list_access = p_list_access;
1884 /* We have to read some data */
1885 return AReadStream( s, p_read, i_read_orig );
1888 /* Update read bytes in input */
1891 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1892 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
1893 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1894 (float)i_total, NULL );
1895 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1896 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1901 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
1903 stream_sys_t *p_sys = s->p_sys;
1904 access_t *p_access = p_sys->p_access;
1905 input_thread_t *p_input = NULL;
1910 if( s->p_parent && s->p_parent->p_parent &&
1911 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1912 p_input = (input_thread_t *)s->p_parent->p_parent;
1914 if( !p_sys->i_list )
1916 p_block = p_access->pf_block( p_access );
1917 if( p_access->b_die )
1918 vlc_object_kill( s );
1919 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1920 if( p_input && p_block && libvlc_stats (p_access) )
1922 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1923 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1924 p_block->i_buffer, &i_total );
1925 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1926 (float)i_total, NULL );
1927 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1928 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1933 p_block = p_sys->p_list_access->pf_block( p_sys->p_list_access );
1934 if( p_access->b_die )
1935 vlc_object_kill( s );
1936 b_eof = p_sys->p_list_access->info.b_eof;
1937 if( pb_eof ) *pb_eof = b_eof;
1939 /* If we reached an EOF then switch to the next stream in the list */
1940 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1942 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1943 access_t *p_list_access;
1945 msg_Dbg( s, "opening input `%s'", psz_name );
1947 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1949 if( !p_list_access ) return 0;
1951 if( p_sys->p_list_access != p_access )
1952 access_Delete( p_sys->p_list_access );
1954 p_sys->p_list_access = p_list_access;
1956 /* We have to read some data */
1957 return AReadBlock( s, pb_eof );
1963 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1964 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1965 p_block->i_buffer, &i_total );
1966 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1967 (float)i_total, NULL );
1968 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
1970 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1976 static int ASeek( stream_t *s, int64_t i_pos )
1978 stream_sys_t *p_sys = s->p_sys;
1979 access_t *p_access = p_sys->p_access;
1981 /* Check which stream we need to access */
1987 access_t *p_list_access = 0;
1989 for( i = 0; i < p_sys->i_list - 1; i++ )
1991 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1992 i_size += p_sys->list[i]->i_size;
1994 psz_name = p_sys->list[i]->psz_path;
1996 if( i != p_sys->i_list_index )
1997 msg_Dbg( s, "opening input `%s'", psz_name );
1999 if( i != p_sys->i_list_index && i != 0 )
2002 access_New( s, p_access->psz_access, "", psz_name );
2004 else if( i != p_sys->i_list_index )
2006 p_list_access = p_access;
2011 if( p_sys->p_list_access != p_access )
2012 access_Delete( p_sys->p_list_access );
2014 p_sys->p_list_access = p_list_access;
2017 p_sys->i_list_index = i;
2018 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
2022 return p_access->pf_seek( p_access, i_pos );
2027 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
2028 * "p_read" is NULL then data are skipped instead of read. The return
2029 * value is the real numbers of bytes read/skip. If this value is less
2030 * than i_read that means that it's the end of the stream.
2032 int stream_Read( stream_t *s, void *p_read, int i_read )
2034 return s->pf_read( s, p_read, i_read );
2038 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
2039 * \return The real numbers of valid bytes, if it's less
2040 * or equal to 0, *pp_peek is invalid.
2041 * \note pp_peek is a pointer to internal buffer and it will be invalid as
2042 * soons as other stream_* functions are called.
2043 * \note Due to input limitation, it could be less than i_peek without meaning
2044 * the end of the stream (but only when you have i_peek >=
2045 * p_input->i_bufsize)
2047 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
2049 return s->pf_peek( s, pp_peek, i_peek );
2053 * Use to control the "stream_t *". Look at #stream_query_e for
2054 * possible "i_query" value and format arguments. Return VLC_SUCCESS
2055 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
2057 int stream_vaControl( stream_t *s, int i_query, va_list args )
2059 return s->pf_control( s, i_query, args );
2065 void stream_Delete( stream_t *s )
2070 int stream_Control( stream_t *s, int i_query, ... )
2076 return VLC_EGENERIC;
2078 va_start( args, i_query );
2079 i_result = s->pf_control( s, i_query, args );
2085 * Read "i_size" bytes and store them in a block_t.
2086 * It always read i_size bytes unless you are at the end of the stream
2087 * where it return what is available.
2089 block_t *stream_Block( stream_t *s, int i_size )
2091 if( i_size <= 0 ) return NULL;
2093 /* emulate block read */
2094 block_t *p_bk = block_New( s, i_size );
2097 int i_read = stream_Read( s, p_bk->p_buffer, i_size );
2100 p_bk->i_buffer = i_read;
2103 block_Release( p_bk );