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 *****************************************************************************/
28 #include <vlc_common.h>
32 #include "input_internal.h"
37 * - tune the 2 methods (block/stream)
38 * - compute cost for seek
39 * - improve stream mode seeking with closest segments
41 * - Maybe remove (block/stream) in favour of immediate
46 * One linked list of data read
48 * More complex scheme using mutliple track to avoid seeking
49 * - using directly the access (only indirection for peeking).
50 * This method is known to introduce much less latency.
51 * It should probably defaulted (instead of the stream method (2)).
54 /* How many tracks we have, currently only used for stream mode */
55 #ifdef OPTIMIZE_MEMORY
56 # define STREAM_CACHE_TRACK 1
57 /* Max size of our cache 128Ko per track */
58 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
60 # define STREAM_CACHE_TRACK 3
61 /* Max size of our cache 4Mo per track */
62 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
65 /* How many data we try to prebuffer */
66 #define STREAM_CACHE_PREBUFFER_SIZE (32767)
67 /* Maximum time we take to pre-buffer */
68 #define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
70 /* Method1: Simple, for pf_block.
71 * We get blocks and put them in the linked list.
72 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
74 #define STREAM_DATA_WAIT 40000 /* Time between before a pf_block retry */
76 /* Method2: A bit more complex, for pf_read
77 * - We use ring buffers, only one if unseekable, all if seekable
78 * - Upon seek date current ring, then search if one ring match the pos,
79 * yes: switch to it, seek the access to match the end of the ring
80 * no: search the ring with i_end the closer to i_pos,
81 * if close enough, read data and use this ring
82 * else use the oldest ring, seek and use it.
84 * TODO: - with access non seekable: use all space available for only one ring, but
85 * we have to support seekable/non-seekable switch on the fly.
86 * - compute a good value for i_read_size
89 #define STREAM_READ_ATONCE 32767
90 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
110 typedef enum stream_read_method_t
115 } stream_read_method_t;
121 stream_read_method_t method; /* method to use */
123 int64_t i_pos; /* Current reading offset */
125 /* Method 1: pf_block */
128 int64_t i_start; /* Offset of block for p_first */
129 int i_offset; /* Offset for data in p_current */
130 block_t *p_current; /* Current block */
132 int i_size; /* Total amount of data in the list */
138 /* Method 2: for pf_read */
141 int i_offset; /* Buffer offset in the current track */
142 int i_tk; /* Current track */
143 stream_track_t tk[STREAM_CACHE_TRACK];
149 int i_used; /* Used since last read */
154 /* Method 3: for pf_read */
161 /* Peek temporary buffer */
165 /* Stat for both method */
168 bool b_fastseek; /* From access */
170 /* Stat about reading data */
171 int64_t i_read_count;
175 /* Stat about seek */
183 access_entry_t **list;
185 access_t *p_list_access;
187 /* Preparse mode ? */
192 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read );
193 static int AStreamPeekBlock( stream_t *s, const uint8_t **p_peek, int i_read );
194 static int AStreamSeekBlock( stream_t *s, int64_t i_pos );
195 static void AStreamPrebufferBlock( stream_t *s );
196 static block_t *AReadBlock( stream_t *s, bool *pb_eof );
199 static int AStreamReadStream( stream_t *s, void *p_read, int i_read );
200 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, int i_read );
201 static int AStreamSeekStream( stream_t *s, int64_t i_pos );
202 static void AStreamPrebufferStream( stream_t *s );
203 static int AReadStream( stream_t *s, void *p_read, int i_read );
206 static int AStreamReadImmediate( stream_t *s, void *p_read, int i_read );
207 static int AStreamPeekImmediate( stream_t *s, const uint8_t **pp_peek, int i_read );
208 static int AStreamSeekImmediate( stream_t *s, int64_t i_pos );
211 static int AStreamControl( stream_t *s, int i_query, va_list );
212 static void AStreamDestroy( stream_t *s );
213 static void UStreamDestroy( stream_t *s );
214 static int ASeek( stream_t *s, int64_t i_pos );
216 /****************************************************************************
218 ****************************************************************************/
220 static inline int64_t stream_buffered_size( stream_t *s )
222 return s->p_sys->immediate.i_end;
225 static inline void stream_buffer_empty( stream_t *s, int length )
227 length = __MAX( stream_buffered_size( s ), length );
230 memmove( s->p_sys->immediate.p_buffer,
231 s->p_sys->immediate.p_buffer + length,
232 stream_buffered_size( s ) - length );
234 s->p_sys->immediate.i_end -= length;
237 static inline void stream_buffer_fill( stream_t *s, int length )
239 s->p_sys->immediate.i_end += length;
242 static inline uint8_t * stream_buffer( stream_t *s )
244 return s->p_sys->immediate.p_buffer;
247 /****************************************************************************
248 * stream_UrlNew: create a stream from a access
249 ****************************************************************************/
250 stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
252 const char *psz_access, *psz_demux;
257 if( !psz_url ) return 0;
259 char psz_dup[strlen (psz_url) + 1];
260 strcpy (psz_dup, psz_url);;
261 MRLSplit( psz_dup, &psz_access, &psz_demux, &psz_path );
263 /* Now try a real access */
264 p_access = access_New( p_parent, psz_access, psz_demux, psz_path );
266 if( p_access == NULL )
268 msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
272 if( !( p_res = stream_AccessNew( p_access, true ) ) )
274 access_Delete( p_access );
278 p_res->pf_destroy = UStreamDestroy;
282 stream_t *stream_AccessNew( access_t *p_access, bool b_quick )
284 stream_t *s = vlc_stream_create( VLC_OBJECT(p_access) );
286 char *psz_list = NULL;
288 if( !s ) return NULL;
290 /* Attach it now, needed for b_die */
291 vlc_object_attach( s, p_access );
293 s->pf_read = NULL; /* Set up later */
295 s->pf_control = AStreamControl;
296 s->pf_destroy = AStreamDestroy;
298 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
301 msg_Err( s, "Out of memory when allocating stream_sys_t" );
305 /* UTF16 and UTF32 text file conversion */
307 s->b_little_endian = false;
308 s->conv = (vlc_iconv_t)(-1);
311 p_sys->p_access = p_access;
312 if( p_access->pf_block )
313 p_sys->method = Block;
314 else if (var_CreateGetBool( s, "use-stream-immediate"))
315 p_sys->method = Immediate;
317 p_sys->method = Stream;
319 p_sys->i_pos = p_access->info.i_pos;
322 access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
323 p_sys->stat.i_bytes = 0;
324 p_sys->stat.i_read_time = 0;
325 p_sys->stat.i_read_count = 0;
326 p_sys->stat.i_seek_count = 0;
327 p_sys->stat.i_seek_time = 0;
331 p_sys->i_list_index = 0;
332 p_sys->p_list_access = 0;
334 p_sys->b_quick = b_quick;
336 /* Get the additional list of inputs if any (for concatenation) */
337 if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
339 access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
340 if( p_entry == NULL )
342 msg_Err( s, "Out of memory when allocating access_entry_t" );
345 char *psz_name, *psz_parser = psz_name = psz_list;
347 p_sys->p_list_access = p_access;
348 p_entry->i_size = p_access->info.i_size;
349 p_entry->psz_path = strdup( p_access->psz_path );
350 if( p_entry->psz_path == NULL )
352 msg_Err( s, "Out of memory when duplicating p_access->psz_path" );
356 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
357 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
358 p_entry->psz_path, p_access->info.i_size );
360 while( psz_name && *psz_name )
362 psz_parser = strchr( psz_name, ',' );
363 if( psz_parser ) *psz_parser = 0;
365 psz_name = strdup( psz_name );
368 access_t *p_tmp = access_New( p_access, p_access->psz_access,
373 psz_name = psz_parser;
374 if( psz_name ) psz_name++;
378 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
379 psz_name, p_tmp->info.i_size );
381 p_entry = malloc( sizeof(access_entry_t) );
382 if( p_entry == NULL )
384 msg_Err( p_access, "Out of memory when allocating access_entry_t" );
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 == 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" );
427 else if (p_sys->method == Immediate)
429 msg_Dbg( s, "Using AStream*Immediate" );
431 s->pf_read = AStreamReadImmediate;
432 s->pf_peek = AStreamPeekImmediate;
434 /* Allocate/Setup our tracks (useful to peek)*/
435 p_sys->immediate.i_end = 0;
436 p_sys->immediate.p_buffer = malloc( STREAM_CACHE_SIZE );
438 msg_Dbg( s, "p_buffer %p-%p", p_sys->immediate.p_buffer,
439 p_sys->immediate.p_buffer + STREAM_CACHE_SIZE );
441 if( p_sys->immediate.p_buffer == NULL )
443 msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
448 else /* ( p_sys->method == Stream ) */
452 msg_Dbg( s, "Using AStream*Stream" );
454 s->pf_read = AStreamReadStream;
455 s->pf_peek = AStreamPeekStream;
457 /* Allocate/Setup our tracks */
458 p_sys->stream.i_offset = 0;
459 p_sys->stream.i_tk = 0;
460 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
461 if( p_sys->stream.p_buffer == NULL )
463 msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
467 p_sys->stream.i_used = 0;
468 access_Control( p_access, ACCESS_GET_MTU,
469 &p_sys->stream.i_read_size );
470 if( p_sys->stream.i_read_size <= 0 )
471 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
472 else if( p_sys->stream.i_read_size <= 256 )
473 p_sys->stream.i_read_size = 256;
475 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
477 p_sys->stream.tk[i].i_date = 0;
478 p_sys->stream.tk[i].i_start = p_sys->i_pos;
479 p_sys->stream.tk[i].i_end = p_sys->i_pos;
480 p_sys->stream.tk[i].p_buffer=
481 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
484 /* Do the prebuffering */
485 AStreamPrebufferStream( s );
487 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
489 msg_Err( s, "cannot pre fill buffer" );
497 if( p_sys->method == Block )
503 free( p_sys->stream.p_buffer );
505 while( p_sys->i_list > 0 )
506 free( p_sys->list[--(p_sys->i_list)] );
510 vlc_object_detach( s );
511 vlc_object_release( s );
515 /****************************************************************************
517 ****************************************************************************/
518 static void AStreamDestroy( stream_t *s )
520 stream_sys_t *p_sys = s->p_sys;
522 vlc_object_detach( s );
524 if( p_sys->method == Block ) block_ChainRelease( p_sys->block.p_first );
525 else free( p_sys->stream.p_buffer );
527 free( p_sys->p_peek );
529 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
530 access_Delete( p_sys->p_list_access );
532 while( p_sys->i_list-- )
534 free( p_sys->list[p_sys->i_list]->psz_path );
535 free( p_sys->list[p_sys->i_list] );
541 vlc_object_release( s );
544 static void UStreamDestroy( stream_t *s )
546 access_t *p_access = (access_t*)vlc_object_find( s, VLC_OBJECT_ACCESS, FIND_PARENT );
548 vlc_object_release( p_access );
549 access_Delete( p_access );
552 /****************************************************************************
553 * stream_AccessReset:
554 ****************************************************************************/
555 void stream_AccessReset( stream_t *s )
557 stream_sys_t *p_sys = s->p_sys;
559 p_sys->i_pos = p_sys->p_access->info.i_pos;
561 if( p_sys->method == Block )
563 block_ChainRelease( p_sys->block.p_first );
565 /* Init all fields of p_sys->block */
566 p_sys->block.i_start = p_sys->i_pos;
567 p_sys->block.i_offset = 0;
568 p_sys->block.p_current = NULL;
569 p_sys->block.i_size = 0;
570 p_sys->block.p_first = NULL;
571 p_sys->block.pp_last = &p_sys->block.p_first;
573 /* Do the prebuffering */
574 AStreamPrebufferBlock( s );
576 else if( p_sys->method == Immediate )
578 stream_buffer_empty( s, stream_buffered_size( s ) );
580 else /* ( p_sys->method == Stream ) */
584 /* Setup our tracks */
585 p_sys->stream.i_offset = 0;
586 p_sys->stream.i_tk = 0;
587 p_sys->stream.i_used = 0;
589 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
591 p_sys->stream.tk[i].i_date = 0;
592 p_sys->stream.tk[i].i_start = p_sys->i_pos;
593 p_sys->stream.tk[i].i_end = p_sys->i_pos;
596 /* Do the prebuffering */
597 AStreamPrebufferStream( s );
601 /****************************************************************************
602 * stream_AccessUpdate:
603 ****************************************************************************/
604 void stream_AccessUpdate( stream_t *s )
606 stream_sys_t *p_sys = s->p_sys;
608 p_sys->i_pos = p_sys->p_access->info.i_pos;
613 for( i = 0; i < p_sys->i_list_index; i++ )
615 p_sys->i_pos += p_sys->list[i]->i_size;
620 /****************************************************************************
622 ****************************************************************************/
623 static int AStreamControl( stream_t *s, int i_query, va_list args )
625 stream_sys_t *p_sys = s->p_sys;
626 access_t *p_access = p_sys->p_access;
629 int64_t *pi_64, i_64;
634 case STREAM_GET_SIZE:
635 pi_64 = (int64_t*)va_arg( args, int64_t * );
636 if( s->p_sys->i_list )
640 for( i = 0; i < s->p_sys->i_list; i++ )
641 *pi_64 += s->p_sys->list[i]->i_size;
644 *pi_64 = p_access->info.i_size;
647 case STREAM_CAN_SEEK:
648 p_bool = (bool*)va_arg( args, bool * );
649 access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
652 case STREAM_CAN_FASTSEEK:
653 p_bool = (bool*)va_arg( args, bool * );
654 access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
657 case STREAM_GET_POSITION:
658 pi_64 = (int64_t*)va_arg( args, int64_t * );
659 *pi_64 = p_sys->i_pos;
662 case STREAM_SET_POSITION:
663 i_64 = (int64_t)va_arg( args, int64_t );
664 if( p_sys->method == Block )
665 return AStreamSeekBlock( s, i_64 );
666 else if( p_sys->method == Immediate )
667 return AStreamSeekImmediate( s, i_64 );
668 else /* ( p_sys->method == Stream ) */
669 return AStreamSeekStream( s, i_64 );
674 case STREAM_CONTROL_ACCESS:
675 i_int = (int) va_arg( args, int );
676 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
677 i_int != ACCESS_SET_PRIVATE_ID_CA &&
678 i_int != ACCESS_GET_PRIVATE_ID_STATE )
680 msg_Err( s, "Hey, what are you thinking ?"
681 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
684 return access_vaControl( p_access, i_int, args );
686 case STREAM_GET_CONTENT_TYPE:
687 return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
688 va_arg( args, char ** ) );
691 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
699 /****************************************************************************
701 ****************************************************************************/
702 static void AStreamPrebufferBlock( stream_t *s )
704 stream_sys_t *p_sys = s->p_sys;
705 access_t *p_access = p_sys->p_access;
710 msg_Dbg( s, "pre buffering" );
714 int64_t i_date = mdate();
718 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
719 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
724 p_sys->stat.i_bytes = p_sys->block.i_size;
725 p_sys->stat.i_read_time = i_date - i_start;
726 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
727 (p_sys->stat.i_read_time + 1);
729 msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
730 "%"PRId64" kbytes/s",
732 p_sys->stat.i_read_time / INT64_C(1000000),
738 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
742 msleep( STREAM_DATA_WAIT );
748 /* Append the block */
749 p_sys->block.i_size += b->i_buffer;
750 *p_sys->block.pp_last = b;
751 p_sys->block.pp_last = &b->p_next;
753 p_sys->stat.i_read_count++;
757 if( p_access->info.b_prebuffered )
759 /* Access has already prebufferred - update stats and exit */
760 p_sys->stat.i_bytes = p_sys->block.i_size;
761 p_sys->stat.i_read_time = mdate() - i_start;
768 msg_Dbg( s, "received first data for our buffer");
773 p_sys->block.p_current = p_sys->block.p_first;
776 static int AStreamRefillBlock( stream_t *s );
778 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
780 stream_sys_t *p_sys = s->p_sys;
782 uint8_t *p_data= (uint8_t*)p_read;
786 if( p_sys->block.p_current == NULL )
791 /* seek within this stream if possible, else use plain old read and discard */
792 stream_sys_t *p_sys = s->p_sys;
793 access_t *p_access = p_sys->p_access;
795 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
797 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
800 while( i_data < i_read )
803 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
804 int i_copy = __MIN( i_current, i_read - i_data);
810 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
816 p_sys->block.i_offset += i_copy;
817 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
819 /* Current block is now empty, switch to next */
820 if( p_sys->block.p_current )
822 p_sys->block.i_offset = 0;
823 p_sys->block.p_current = p_sys->block.p_current->p_next;
825 /*Get a new block if needed */
826 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
833 p_sys->i_pos += i_data;
837 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, int i_read )
839 stream_sys_t *p_sys = s->p_sys;
845 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
847 /* We can directly give a pointer over our buffer */
848 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
850 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
854 /* We need to create a local copy */
855 if( p_sys->i_peek < i_read )
857 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
863 p_sys->i_peek = i_read;
866 /* Fill enough data */
867 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
870 block_t **pp_last = p_sys->block.pp_last;
872 if( AStreamRefillBlock( s ) ) break;
874 /* Our buffer are probably filled enough, don't try anymore */
875 if( pp_last == p_sys->block.pp_last ) break;
878 /* Copy what we have */
879 b = p_sys->block.p_current;
880 i_offset = p_sys->block.i_offset;
881 p_data = p_sys->p_peek;
883 while( b && i_data < i_read )
885 int i_current = b->i_buffer - i_offset;
886 int i_copy = __MIN( i_current, i_read - i_data );
888 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
893 if( i_offset >= b->i_buffer )
900 *pp_peek = p_sys->p_peek;
904 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
906 stream_sys_t *p_sys = s->p_sys;
907 access_t *p_access = p_sys->p_access;
908 int64_t i_offset = i_pos - p_sys->block.i_start;
911 /* We already have thoses data, just update p_current/i_offset */
912 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
914 block_t *b = p_sys->block.p_first;
917 while( i_current + b->i_buffer < i_offset )
919 i_current += b->i_buffer;
923 p_sys->block.p_current = b;
924 p_sys->block.i_offset = i_offset - i_current;
926 p_sys->i_pos = i_pos;
931 /* We may need to seek or to read data */
935 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
939 msg_Err( s, "backward seeking impossible (access not seekable)" );
947 bool b_aseek, b_aseekfast;
949 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
950 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
955 msg_Warn( s, "%"PRId64" bytes need to be skipped "
956 "(access non seekable)",
957 i_offset - p_sys->block.i_size );
961 int64_t i_skip = i_offset - p_sys->block.i_size;
963 /* Avg bytes per packets */
964 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
965 /* TODO compute a seek cost instead of fixed threshold */
966 int i_th = b_aseekfast ? 1 : 5;
968 if( i_skip <= i_th * i_avg &&
969 i_skip < STREAM_CACHE_SIZE )
974 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
975 b_seek, i_th*i_avg, i_skip );
981 int64_t i_start, i_end;
982 /* Do the access seek */
984 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
988 block_ChainRelease( p_sys->block.p_first );
991 p_sys->block.i_start = p_sys->i_pos = i_pos;
992 p_sys->block.i_offset = 0;
993 p_sys->block.p_current = NULL;
994 p_sys->block.i_size = 0;
995 p_sys->block.p_first = NULL;
996 p_sys->block.pp_last = &p_sys->block.p_first;
999 if( AStreamRefillBlock( s ) )
1000 return VLC_EGENERIC;
1003 p_sys->stat.i_seek_time += i_end - i_start;
1004 p_sys->stat.i_seek_count++;
1009 /* Read enough data */
1010 while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
1012 if( AStreamRefillBlock( s ) )
1013 return VLC_EGENERIC;
1015 while( p_sys->block.p_current &&
1016 p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
1018 p_sys->i_pos += p_sys->block.p_current->i_buffer;
1019 p_sys->block.p_current = p_sys->block.p_current->p_next;
1023 p_sys->block.i_offset = i_pos - p_sys->i_pos;
1024 p_sys->i_pos = i_pos;
1029 return VLC_EGENERIC;
1032 static int AStreamRefillBlock( stream_t *s )
1034 stream_sys_t *p_sys = s->p_sys;
1035 int64_t i_start, i_stop;
1039 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1040 p_sys->block.p_first != p_sys->block.p_current )
1042 block_t *b = p_sys->block.p_first;
1044 p_sys->block.i_start += b->i_buffer;
1045 p_sys->block.i_size -= b->i_buffer;
1046 p_sys->block.p_first = b->p_next;
1050 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1051 p_sys->block.p_current == p_sys->block.p_first &&
1052 p_sys->block.p_current->p_next ) /* At least 2 packets */
1054 /* Enough data, don't read more */
1058 /* Now read a new block */
1064 if( s->b_die ) return VLC_EGENERIC;
1068 if( ( b = AReadBlock( s, &b_eof ) ) ) break;
1070 if( b_eof ) return VLC_EGENERIC;
1072 msleep( STREAM_DATA_WAIT );
1079 /* Append the block */
1080 p_sys->block.i_size += b->i_buffer;
1081 *p_sys->block.pp_last = b;
1082 p_sys->block.pp_last = &b->p_next;
1085 if( p_sys->block.p_current == NULL )
1086 p_sys->block.p_current = b;
1089 p_sys->stat.i_bytes += b->i_buffer;
1090 p_sys->stat.i_read_time += i_stop - i_start;
1091 p_sys->stat.i_read_count++;
1100 /****************************************************************************
1102 ****************************************************************************/
1103 static int AStreamRefillStream( stream_t *s );
1105 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
1107 stream_sys_t *p_sys = s->p_sys;
1108 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1110 uint8_t *p_data = (uint8_t *)p_read;
1113 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1115 if( p_read == NULL )
1117 /* seek within this stream if possible, else use plain old read and discard */
1118 stream_sys_t *p_sys = s->p_sys;
1119 access_t *p_access = p_sys->p_access;
1121 /* seeking after EOF is not what we want */
1122 if( !( p_access->info.b_eof ) )
1125 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1127 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1132 msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1133 " offset=%d end=%"PRId64,
1134 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1135 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1138 while( i_data < i_read )
1140 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1141 STREAM_CACHE_TRACK_SIZE;
1143 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1144 STREAM_CACHE_TRACK_SIZE - i_off );
1145 int i_copy = __MIN( i_current, i_read - i_data );
1147 if( i_copy <= 0 ) break; /* EOF */
1150 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1153 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1157 p_sys->stream.i_offset += i_copy;
1159 /* Update pos now */
1160 p_sys->i_pos += i_copy;
1163 p_sys->stream.i_used += i_copy;
1164 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1165 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1167 if( AStreamRefillStream( s ) )
1170 if( tk->i_start >= tk->i_end ) break;
1178 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, int i_read )
1180 stream_sys_t *p_sys = s->p_sys;
1181 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1184 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1187 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1188 "start=%"PRId64" offset=%d end=%"PRId64,
1189 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1190 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1193 /* Avoid problem, but that should *never* happen */
1194 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1195 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1197 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1199 if( p_sys->stream.i_used <= 1 )
1201 /* Be sure we will read something */
1202 p_sys->stream.i_used += i_read -
1203 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1205 if( AStreamRefillStream( s ) ) break;
1208 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1209 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1211 /* Now, direct pointer or a copy ? */
1212 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1213 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1215 *pp_peek = &tk->p_buffer[i_off];
1219 if( p_sys->i_peek < i_read )
1221 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1222 if( !p_sys->p_peek )
1227 p_sys->i_peek = i_read;
1230 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1231 STREAM_CACHE_TRACK_SIZE - i_off );
1232 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1233 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1235 *pp_peek = p_sys->p_peek;
1239 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1241 stream_sys_t *p_sys = s->p_sys;
1242 access_t *p_access = p_sys->p_access;
1250 msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1251 " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1252 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1253 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1254 p_sys->stream.i_offset,
1255 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1259 /* Seek in our current track ? */
1260 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1261 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1263 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1265 msg_Dbg( s, "AStreamSeekStream: current track" );
1267 p_sys->i_pos = i_pos;
1268 p_sys->stream.i_offset = i_pos - tk->i_start;
1270 /* If there is not enough data left in the track, refill */
1271 /* \todo How to get a correct value for
1272 * - refilling threshold
1273 * - how much to refill
1275 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1276 p_sys->stream.i_read_size )
1278 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1280 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1281 AStreamRefillStream( s );
1287 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1290 /* We can't do nothing */
1291 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1292 return VLC_EGENERIC;
1295 /* Date the current track */
1296 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1298 /* Try to reuse already read data */
1299 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1301 stream_track_t *tk = &p_sys->stream.tk[i];
1303 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1306 msg_Dbg( s, "AStreamSeekStream: reusing %d start=%"PRId64
1307 " end=%"PRId64, i, tk->i_start, tk->i_end );
1310 /* Seek at the end of the buffer */
1311 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1314 p_sys->i_pos = i_pos;
1315 p_sys->stream.i_tk = i;
1316 p_sys->stream.i_offset = i_pos - tk->i_start;
1318 if( p_sys->stream.i_used < 1024 )
1319 p_sys->stream.i_used = 1024;
1321 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1322 return VLC_EGENERIC;
1328 access_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1329 /* FIXME compute seek cost (instead of static 'stupid' value) */
1330 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1336 /* Search closest segment TODO */
1337 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1339 stream_track_t *tk = &p_sys->stream.tk[i];
1341 if( i_pos + i_maxth >= tk->i_start )
1343 msg_Dbg( s, "good segment before current pos, TODO" );
1345 if( i_pos - i_maxth <= tk->i_end )
1347 msg_Dbg( s, "good segment after current pos, TODO" );
1352 /* Nothing good, seek and choose oldest segment */
1353 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1354 p_sys->i_pos = i_pos;
1357 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1359 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1363 /* Reset the segment */
1364 p_sys->stream.i_tk = i_new;
1365 p_sys->stream.i_offset = 0;
1366 p_sys->stream.tk[i_new].i_start = i_pos;
1367 p_sys->stream.tk[i_new].i_end = i_pos;
1370 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1371 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1373 if( AStreamRefillStream( s ) )
1374 return VLC_EGENERIC;
1379 static int AStreamRefillStream( stream_t *s )
1381 stream_sys_t *p_sys = s->p_sys;
1382 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1384 /* We read but won't increase i_start after initial start + offset */
1386 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1387 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1388 bool b_read = false;
1389 int64_t i_start, i_stop;
1391 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1394 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1395 p_sys->stream.i_used, i_toread );
1399 while( i_toread > 0 )
1401 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1405 return VLC_EGENERIC;
1407 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1408 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1410 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1413 msleep( STREAM_DATA_WAIT );
1416 else if( i_read == 0 )
1418 if( !b_read ) return VLC_EGENERIC;
1424 tk->i_end += i_read;
1426 /* Windows of STREAM_CACHE_TRACK_SIZE */
1427 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1429 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1431 tk->i_start += i_invalid;
1432 p_sys->stream.i_offset -= i_invalid;
1436 p_sys->stream.i_used -= i_read;
1438 p_sys->stat.i_bytes += i_read;
1439 p_sys->stat.i_read_count++;
1443 p_sys->stat.i_read_time += i_stop - i_start;
1448 static void AStreamPrebufferStream( stream_t *s )
1450 stream_sys_t *p_sys = s->p_sys;
1451 access_t *p_access = p_sys->p_access;
1453 int64_t i_first = 0;
1455 int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1456 ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1457 STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1459 msg_Dbg( s, "pre-buffering..." );
1463 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1465 int64_t i_date = mdate();
1468 if( s->b_die || tk->i_end >= i_prebuffer ||
1469 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1474 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1475 p_sys->stat.i_read_time = i_date - i_start;
1476 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1477 (p_sys->stat.i_read_time+1);
1479 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1480 "%"PRId64" kbytes/s",
1481 p_sys->stat.i_bytes,
1482 p_sys->stat.i_read_time / INT64_C(1000000),
1483 i_byterate / 1024 );
1488 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1489 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1490 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1493 msleep( STREAM_DATA_WAIT );
1496 else if( i_read == 0 )
1505 msg_Dbg( s, "received first data for our buffer");
1508 tk->i_end += i_read;
1510 p_sys->stat.i_read_count++;
1514 /****************************************************************************
1516 ****************************************************************************/
1518 static int AStreamReadImmediate( stream_t *s, void *p_read, int i_read )
1520 stream_sys_t *p_sys = s->p_sys;
1523 msg_Dbg( s, "AStreamReadImmediate p_read=%p i_read=%d",
1527 if( p_read == NULL )
1529 /* seek within this stream if possible, else use plain old read and discard */
1530 stream_sys_t *p_sys = s->p_sys;
1531 access_t *p_access = p_sys->p_access;
1533 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1535 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1538 /* First, check if we already have some data in the buffer,
1539 * that we could copy directly */
1540 int i_copy = __MIN( stream_buffered_size( s ), i_read );
1544 msg_Dbg( s, "AStreamReadImmediate: copy %d from %p", i_copy, stream_buffer( s ) );
1547 assert( i_copy <= STREAM_CACHE_SIZE );
1551 memcpy( p_read, stream_buffer( s ), i_copy );
1552 p_read = (uint8_t *)p_read + i_copy;
1556 /* Now that we've read our buffer we don't need its i_copy bytes */
1557 stream_buffer_empty( s, i_copy );
1559 /* Now check if we have still to really read some data */
1560 int i_to_read = i_read - i_copy;
1563 i_to_read = AReadStream( s, p_read, i_to_read );
1566 p_sys->i_pos += i_to_read;
1568 return i_to_read + i_copy;
1571 static int AStreamPeekImmediate( stream_t *s, const uint8_t **pp_peek, int i_read )
1574 msg_Dbg( s, "AStreamPeekImmediate: %d size=%"PRId64,
1575 i_read, size_buffered_size( s ) );
1578 /* Avoid problem, but that shouldn't happen */
1579 if( i_read > STREAM_CACHE_SIZE / 2 )
1580 i_read = STREAM_CACHE_SIZE / 2;
1582 int i_to_read = i_read - stream_buffered_size( s );
1586 msg_Dbg( s, "AStreamPeekImmediate: Reading %d",
1589 i_to_read = AReadStream( s, stream_buffer( s ) + stream_buffered_size( s ),
1593 stream_buffer_fill( s, i_to_read );
1596 *pp_peek = stream_buffer( s );
1598 return __MIN(stream_buffered_size( s ), i_read);
1601 static int AStreamSeekImmediate( stream_t *s, int64_t i_pos )
1603 stream_sys_t *p_sys = s->p_sys;
1604 access_t *p_access = p_sys->p_access;
1608 msg_Dbg( s, "AStreamSeekImmediate to %"PRId64" pos=%"PRId64
1609 i_pos, p_sys->i_pos );
1612 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1615 /* We can't do nothing */
1616 msg_Dbg( s, "AStreamSeekImmediate: can't seek" );
1617 return VLC_EGENERIC;
1620 /* Just reset our buffer */
1621 stream_buffer_empty( s, stream_buffered_size( s ) );
1623 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1628 /****************************************************************************
1630 ****************************************************************************/
1632 * Read from the stream untill first newline.
1633 * \param s Stream handle to read from
1634 * \return A pointer to the allocated output string. You need to free this when you are done.
1636 #define STREAM_PROBE_LINE 2048
1637 #define STREAM_LINE_MAX (2048*100)
1638 char * stream_ReadLine( stream_t *s )
1640 char *p_line = NULL;
1641 int i_line = 0, i_read = 0;
1643 while( i_read < STREAM_LINE_MAX )
1646 const uint8_t *p_data;
1650 /* Probe new data */
1651 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1652 if( i_data <= 0 ) break; /* No more data */
1655 i_pos = stream_Tell( s );
1656 if( i_pos == 0 && i_data > 4 )
1659 char *psz_encoding = NULL;
1661 if( p_data[0] == 0xEF && p_data[1] == 0xBB && p_data[2] == 0xBF )
1663 psz_encoding = strdup( "UTF-8" );
1666 else if( p_data[0] == 0x00 && p_data[1] == 0x00 )
1668 if( p_data[2] == 0xFE && p_data[3] == 0xFF )
1670 psz_encoding = strdup( "UTF-32BE" );
1671 s->i_char_width = 4;
1675 else if( p_data[0] == 0xFF && p_data[1] == 0xFE )
1677 if( p_data[2] == 0x00 && p_data[3] == 0x00 )
1679 psz_encoding = strdup( "UTF-32LE" );
1680 s->i_char_width = 4;
1681 s->b_little_endian = true;
1686 psz_encoding = strdup( "UTF-16LE" );
1687 s->b_little_endian = true;
1688 s->i_char_width = 2;
1692 else if( p_data[0] == 0xFE && p_data[1] == 0xFF )
1694 psz_encoding = strdup( "UTF-16BE" );
1695 s->i_char_width = 2;
1699 /* Seek past the BOM */
1702 stream_Seek( s, i_bom_size );
1703 p_data += i_bom_size;
1704 i_data -= i_bom_size;
1707 /* Open the converter if we need it */
1708 if( psz_encoding != NULL )
1710 input_thread_t *p_input;
1711 msg_Dbg( s, "%s BOM detected", psz_encoding );
1712 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1713 if( s->i_char_width > 1 )
1715 s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1716 if( s->conv == (vlc_iconv_t)-1 )
1718 msg_Err( s, "iconv_open failed" );
1721 if( p_input != NULL)
1723 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1724 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1725 vlc_object_release( p_input );
1727 free( psz_encoding );
1731 if( i_data % s->i_char_width )
1733 /* keep i_char_width boundary */
1734 i_data = i_data - ( i_data % s->i_char_width );
1735 msg_Warn( s, "the read is not i_char_width compatible");
1741 /* Check if there is an EOL */
1742 if( s->i_char_width == 1 )
1744 /* UTF-8: 0A <LF> */
1745 psz_eol = memchr( p_data, '\n', i_data );
1749 const uint8_t *p = p_data;
1750 const uint8_t *p_last = p + i_data - s->i_char_width;
1752 if( s->i_char_width == 2 )
1754 if( s->b_little_endian == true)
1756 /* UTF-16LE: 0A 00 <LF> */
1757 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1762 /* UTF-16BE: 00 0A <LF> */
1763 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1767 else if( s->i_char_width == 4 )
1769 if( s->b_little_endian == true)
1771 /* UTF-32LE: 0A 00 00 00 <LF> */
1772 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ||
1773 p[2] != 0x00 || p[3] != 0x00 ) )
1778 /* UTF-32BE: 00 00 00 0A <LF> */
1779 while( p <= p_last && ( p[3] != 0x0A || p[2] != 0x00 ||
1780 p[1] != 0x00 || p[0] != 0x00 ) )
1791 psz_eol = (char *)p + ( s->i_char_width - 1 );
1797 i_data = (psz_eol - (char *)p_data) + 1;
1798 p_line = realloc( p_line, i_line + i_data + s->i_char_width ); /* add \0 */
1801 msg_Err( s, "Out of memory when reallocating p_line" );
1804 i_data = stream_Read( s, &p_line[i_line], i_data );
1805 if( i_data <= 0 ) break; /* Hmmm */
1806 i_line += i_data - s->i_char_width; /* skip \n */;
1809 /* We have our line */
1813 /* Read data (+1 for easy \0 append) */
1814 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->i_char_width );
1817 msg_Err( s, "Out of memory when reallocating p_line" );
1820 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1821 if( i_data <= 0 ) break; /* Hmmm */
1829 for( j = 0; j < s->i_char_width; j++ )
1831 p_line[i_line + j] = '\0';
1833 i_line += s->i_char_width; /* the added \0 */
1834 if( s->i_char_width > 1 )
1836 size_t i_in = 0, i_out = 0;
1837 const char * p_in = NULL;
1838 char * p_out = NULL;
1839 char * psz_new_line = NULL;
1842 psz_new_line = malloc( i_line );
1843 if( psz_new_line == NULL )
1845 msg_Err( s, "Out of memory when allocating psz_new_line" );
1848 i_in = i_out = (size_t)i_line;
1850 p_out = psz_new_line;
1852 if( vlc_iconv( s->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1854 msg_Err( s, "iconv failed" );
1855 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1858 p_line = psz_new_line;
1859 i_line = (size_t)i_line - i_out; /* does not include \0 */
1862 /* Remove trailing LF/CR */
1863 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1864 p_line[i_line-2] == '\n') ) i_line--;
1866 /* Make sure the \0 is there */
1867 p_line[i_line-1] = '\0';
1874 /* We failed to read any data, probably EOF */
1876 if( s->conv != (vlc_iconv_t)(-1) ) vlc_iconv_close( s->conv );
1880 /****************************************************************************
1881 * Access reading/seeking wrappers to handle concatenated streams.
1882 ****************************************************************************/
1883 static int AReadStream( stream_t *s, void *p_read, int i_read )
1885 stream_sys_t *p_sys = s->p_sys;
1886 access_t *p_access = p_sys->p_access;
1887 input_thread_t *p_input = NULL;
1888 int i_read_orig = i_read;
1891 if( s->p_parent && s->p_parent->p_parent &&
1892 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1893 p_input = (input_thread_t *)s->p_parent->p_parent;
1895 if( !p_sys->i_list )
1897 i_read = p_access->pf_read( p_access, p_read, i_read );
1900 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1901 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
1903 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1904 (float)i_total, NULL );
1905 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1906 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1911 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1914 /* If we reached an EOF then switch to the next stream in the list */
1915 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1917 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1918 access_t *p_list_access;
1920 msg_Dbg( s, "opening input `%s'", psz_name );
1922 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1924 if( !p_list_access ) return 0;
1926 if( p_sys->p_list_access != p_access )
1927 access_Delete( p_sys->p_list_access );
1929 p_sys->p_list_access = p_list_access;
1931 /* We have to read some data */
1932 return AReadStream( s, p_read, i_read_orig );
1935 /* Update read bytes in input */
1938 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1939 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
1940 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1941 (float)i_total, NULL );
1942 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1943 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1948 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
1950 stream_sys_t *p_sys = s->p_sys;
1951 access_t *p_access = p_sys->p_access;
1952 input_thread_t *p_input = NULL;
1957 if( s->p_parent && s->p_parent->p_parent &&
1958 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1959 p_input = (input_thread_t *)s->p_parent->p_parent;
1961 if( !p_sys->i_list )
1963 p_block = p_access->pf_block( p_access );
1964 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1965 if( p_input && p_block && libvlc_stats (p_access) )
1967 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1968 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1969 p_block->i_buffer, &i_total );
1970 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1971 (float)i_total, NULL );
1972 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1973 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1978 p_block = p_sys->p_list_access->pf_block( p_access );
1979 b_eof = p_sys->p_list_access->info.b_eof;
1980 if( pb_eof ) *pb_eof = b_eof;
1982 /* If we reached an EOF then switch to the next stream in the list */
1983 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1985 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1986 access_t *p_list_access;
1988 msg_Dbg( s, "opening input `%s'", psz_name );
1990 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1992 if( !p_list_access ) return 0;
1994 if( p_sys->p_list_access != p_access )
1995 access_Delete( p_sys->p_list_access );
1997 p_sys->p_list_access = p_list_access;
1999 /* We have to read some data */
2000 return AReadBlock( s, pb_eof );
2006 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2007 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
2008 p_block->i_buffer, &i_total );
2009 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2010 (float)i_total, NULL );
2011 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
2013 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2019 static int ASeek( stream_t *s, int64_t i_pos )
2021 stream_sys_t *p_sys = s->p_sys;
2022 access_t *p_access = p_sys->p_access;
2024 /* Check which stream we need to access */
2030 access_t *p_list_access = 0;
2032 for( i = 0; i < p_sys->i_list - 1; i++ )
2034 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
2035 i_size += p_sys->list[i]->i_size;
2037 psz_name = p_sys->list[i]->psz_path;
2039 if( i != p_sys->i_list_index )
2040 msg_Dbg( s, "opening input `%s'", psz_name );
2042 if( i != p_sys->i_list_index && i != 0 )
2045 access_New( s, p_access->psz_access, "", psz_name );
2047 else if( i != p_sys->i_list_index )
2049 p_list_access = p_access;
2054 if( p_sys->p_list_access != p_access )
2055 access_Delete( p_sys->p_list_access );
2057 p_sys->p_list_access = p_list_access;
2060 p_sys->i_list_index = i;
2061 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
2065 return p_access->pf_seek( p_access, i_pos );
2070 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
2071 * "p_read" is NULL then data are skipped instead of read. The return
2072 * value is the real numbers of bytes read/skip. If this value is less
2073 * than i_read that means that it's the end of the stream.
2075 int stream_Read( stream_t *s, void *p_read, int i_read )
2077 return s->pf_read( s, p_read, i_read );
2081 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
2082 * \return The real numbers of valid bytes, if it's less
2083 * or equal to 0, *pp_peek is invalid.
2084 * \note pp_peek is a pointer to internal buffer and it will be invalid as
2085 * soons as other stream_* functions are called.
2086 * \note Due to input limitation, it could be less than i_peek without meaning
2087 * the end of the stream (but only when you have i_peek >=
2088 * p_input->i_bufsize)
2090 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
2092 return s->pf_peek( s, pp_peek, i_peek );
2096 * Use to control the "stream_t *". Look at #stream_query_e for
2097 * possible "i_query" value and format arguments. Return VLC_SUCCESS
2098 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
2100 int stream_vaControl( stream_t *s, int i_query, va_list args )
2102 return s->pf_control( s, i_query, args );
2108 void stream_Delete( stream_t *s )
2113 int stream_Control( stream_t *s, int i_query, ... )
2119 return VLC_EGENERIC;
2121 va_start( args, i_query );
2122 i_result = s->pf_control( s, i_query, args );
2128 * Read "i_size" bytes and store them in a block_t.
2129 * It always read i_size bytes unless you are at the end of the stream
2130 * where it return what is available.
2132 block_t *stream_Block( stream_t *s, int i_size )
2134 if( i_size <= 0 ) return NULL;
2136 /* emulate block read */
2137 block_t *p_bk = block_New( s, i_size );
2140 p_bk->i_buffer = stream_Read( s, p_bk->p_buffer, i_size );
2141 if( p_bk->i_buffer > 0 )
2146 if( p_bk ) block_Release( p_bk );