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);
808 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
813 p_sys->block.i_offset += i_copy;
814 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
816 /* Current block is now empty, switch to next */
817 if( p_sys->block.p_current )
819 p_sys->block.i_offset = 0;
820 p_sys->block.p_current = p_sys->block.p_current->p_next;
822 /*Get a new block if needed */
823 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
830 p_sys->i_pos += i_data;
834 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, int i_read )
836 stream_sys_t *p_sys = s->p_sys;
842 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
844 /* We can directly give a pointer over our buffer */
845 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
847 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
851 /* We need to create a local copy */
852 if( p_sys->i_peek < i_read )
854 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
860 p_sys->i_peek = i_read;
863 /* Fill enough data */
864 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
867 block_t **pp_last = p_sys->block.pp_last;
869 if( AStreamRefillBlock( s ) ) break;
871 /* Our buffer are probably filled enough, don't try anymore */
872 if( pp_last == p_sys->block.pp_last ) break;
875 /* Copy what we have */
876 b = p_sys->block.p_current;
877 i_offset = p_sys->block.i_offset;
878 p_data = p_sys->p_peek;
880 while( b && i_data < i_read )
882 int i_current = b->i_buffer - i_offset;
883 int i_copy = __MIN( i_current, i_read - i_data );
885 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
890 if( i_offset >= b->i_buffer )
897 *pp_peek = p_sys->p_peek;
901 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
903 stream_sys_t *p_sys = s->p_sys;
904 access_t *p_access = p_sys->p_access;
905 int64_t i_offset = i_pos - p_sys->block.i_start;
908 /* We already have thoses data, just update p_current/i_offset */
909 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
911 block_t *b = p_sys->block.p_first;
914 while( i_current + b->i_buffer < i_offset )
916 i_current += b->i_buffer;
920 p_sys->block.p_current = b;
921 p_sys->block.i_offset = i_offset - i_current;
923 p_sys->i_pos = i_pos;
928 /* We may need to seek or to read data */
932 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
936 msg_Err( s, "backward seeking impossible (access not seekable)" );
944 bool b_aseek, b_aseekfast;
946 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
947 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
952 msg_Warn( s, "%"PRId64" bytes need to be skipped "
953 "(access non seekable)",
954 i_offset - p_sys->block.i_size );
958 int64_t i_skip = i_offset - p_sys->block.i_size;
960 /* Avg bytes per packets */
961 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
962 /* TODO compute a seek cost instead of fixed threshold */
963 int i_th = b_aseekfast ? 1 : 5;
965 if( i_skip <= i_th * i_avg &&
966 i_skip < STREAM_CACHE_SIZE )
971 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
972 b_seek, i_th*i_avg, i_skip );
978 int64_t i_start, i_end;
979 /* Do the access seek */
981 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
985 block_ChainRelease( p_sys->block.p_first );
988 p_sys->block.i_start = p_sys->i_pos = i_pos;
989 p_sys->block.i_offset = 0;
990 p_sys->block.p_current = NULL;
991 p_sys->block.i_size = 0;
992 p_sys->block.p_first = NULL;
993 p_sys->block.pp_last = &p_sys->block.p_first;
996 if( AStreamRefillBlock( s ) )
1000 p_sys->stat.i_seek_time += i_end - i_start;
1001 p_sys->stat.i_seek_count++;
1006 /* Read enough data */
1007 while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
1009 if( AStreamRefillBlock( s ) )
1010 return VLC_EGENERIC;
1012 while( p_sys->block.p_current &&
1013 p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
1015 p_sys->i_pos += p_sys->block.p_current->i_buffer;
1016 p_sys->block.p_current = p_sys->block.p_current->p_next;
1020 p_sys->block.i_offset = i_pos - p_sys->i_pos;
1021 p_sys->i_pos = i_pos;
1026 return VLC_EGENERIC;
1029 static int AStreamRefillBlock( stream_t *s )
1031 stream_sys_t *p_sys = s->p_sys;
1032 int64_t i_start, i_stop;
1036 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1037 p_sys->block.p_first != p_sys->block.p_current )
1039 block_t *b = p_sys->block.p_first;
1041 p_sys->block.i_start += b->i_buffer;
1042 p_sys->block.i_size -= b->i_buffer;
1043 p_sys->block.p_first = b->p_next;
1047 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1048 p_sys->block.p_current == p_sys->block.p_first &&
1049 p_sys->block.p_current->p_next ) /* At least 2 packets */
1051 /* Enough data, don't read more */
1055 /* Now read a new block */
1061 if( s->b_die ) return VLC_EGENERIC;
1065 if( ( b = AReadBlock( s, &b_eof ) ) ) break;
1067 if( b_eof ) return VLC_EGENERIC;
1069 msleep( STREAM_DATA_WAIT );
1076 /* Append the block */
1077 p_sys->block.i_size += b->i_buffer;
1078 *p_sys->block.pp_last = b;
1079 p_sys->block.pp_last = &b->p_next;
1082 if( p_sys->block.p_current == NULL )
1083 p_sys->block.p_current = b;
1086 p_sys->stat.i_bytes += b->i_buffer;
1087 p_sys->stat.i_read_time += i_stop - i_start;
1088 p_sys->stat.i_read_count++;
1097 /****************************************************************************
1099 ****************************************************************************/
1100 static int AStreamRefillStream( stream_t *s );
1102 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
1104 stream_sys_t *p_sys = s->p_sys;
1105 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1107 uint8_t *p_data = (uint8_t *)p_read;
1110 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1112 if( p_read == NULL )
1114 /* seek within this stream if possible, else use plain old read and discard */
1115 stream_sys_t *p_sys = s->p_sys;
1116 access_t *p_access = p_sys->p_access;
1118 /* seeking after EOF is not what we want */
1119 if( !( p_access->info.b_eof ) )
1122 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1124 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1129 msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1130 " offset=%d end=%"PRId64,
1131 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1132 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1135 while( i_data < i_read )
1137 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1138 STREAM_CACHE_TRACK_SIZE;
1140 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1141 STREAM_CACHE_TRACK_SIZE - i_off );
1142 int i_copy = __MIN( i_current, i_read - i_data );
1144 if( i_copy <= 0 ) break; /* EOF */
1147 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1148 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1151 p_sys->stream.i_offset += i_copy;
1153 /* Update pos now */
1154 p_sys->i_pos += i_copy;
1157 p_sys->stream.i_used += i_copy;
1158 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1159 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1161 if( AStreamRefillStream( s ) )
1164 if( tk->i_start >= tk->i_end ) break;
1172 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, int i_read )
1174 stream_sys_t *p_sys = s->p_sys;
1175 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1178 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1181 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1182 "start=%"PRId64" offset=%d end=%"PRId64,
1183 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1184 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1187 /* Avoid problem, but that should *never* happen */
1188 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1189 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1191 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1193 if( p_sys->stream.i_used <= 1 )
1195 /* Be sure we will read something */
1196 p_sys->stream.i_used += i_read -
1197 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1199 if( AStreamRefillStream( s ) ) break;
1202 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1203 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1205 /* Now, direct pointer or a copy ? */
1206 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1207 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1209 *pp_peek = &tk->p_buffer[i_off];
1213 if( p_sys->i_peek < i_read )
1215 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1216 if( !p_sys->p_peek )
1221 p_sys->i_peek = i_read;
1224 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1225 STREAM_CACHE_TRACK_SIZE - i_off );
1226 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1227 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1229 *pp_peek = p_sys->p_peek;
1233 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1235 stream_sys_t *p_sys = s->p_sys;
1236 access_t *p_access = p_sys->p_access;
1244 msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1245 " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1246 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1247 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1248 p_sys->stream.i_offset,
1249 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1253 /* Seek in our current track ? */
1254 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1255 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1257 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1259 msg_Dbg( s, "AStreamSeekStream: current track" );
1261 p_sys->i_pos = i_pos;
1262 p_sys->stream.i_offset = i_pos - tk->i_start;
1264 /* If there is not enough data left in the track, refill */
1265 /* \todo How to get a correct value for
1266 * - refilling threshold
1267 * - how much to refill
1269 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1270 p_sys->stream.i_read_size )
1272 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1274 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1275 AStreamRefillStream( s );
1281 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1284 /* We can't do nothing */
1285 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1286 return VLC_EGENERIC;
1289 /* Date the current track */
1290 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1292 /* Try to reuse already read data */
1293 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1295 stream_track_t *tk = &p_sys->stream.tk[i];
1297 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1300 msg_Dbg( s, "AStreamSeekStream: reusing %d start=%"PRId64
1301 " end=%"PRId64, i, tk->i_start, tk->i_end );
1304 /* Seek at the end of the buffer */
1305 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1308 p_sys->i_pos = i_pos;
1309 p_sys->stream.i_tk = i;
1310 p_sys->stream.i_offset = i_pos - tk->i_start;
1312 if( p_sys->stream.i_used < 1024 )
1313 p_sys->stream.i_used = 1024;
1315 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1316 return VLC_EGENERIC;
1322 access_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1323 /* FIXME compute seek cost (instead of static 'stupid' value) */
1324 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1330 /* Search closest segment TODO */
1331 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1333 stream_track_t *tk = &p_sys->stream.tk[i];
1335 if( i_pos + i_maxth >= tk->i_start )
1337 msg_Dbg( s, "good segment before current pos, TODO" );
1339 if( i_pos - i_maxth <= tk->i_end )
1341 msg_Dbg( s, "good segment after current pos, TODO" );
1346 /* Nothing good, seek and choose oldest segment */
1347 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1348 p_sys->i_pos = i_pos;
1351 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1353 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1357 /* Reset the segment */
1358 p_sys->stream.i_tk = i_new;
1359 p_sys->stream.i_offset = 0;
1360 p_sys->stream.tk[i_new].i_start = i_pos;
1361 p_sys->stream.tk[i_new].i_end = i_pos;
1364 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1365 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1367 if( AStreamRefillStream( s ) )
1368 return VLC_EGENERIC;
1373 static int AStreamRefillStream( stream_t *s )
1375 stream_sys_t *p_sys = s->p_sys;
1376 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1378 /* We read but won't increase i_start after initial start + offset */
1380 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1381 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1382 bool b_read = false;
1383 int64_t i_start, i_stop;
1385 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1388 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1389 p_sys->stream.i_used, i_toread );
1393 while( i_toread > 0 )
1395 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1399 return VLC_EGENERIC;
1401 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1402 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1404 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1407 msleep( STREAM_DATA_WAIT );
1410 else if( i_read == 0 )
1412 if( !b_read ) return VLC_EGENERIC;
1418 tk->i_end += i_read;
1420 /* Windows of STREAM_CACHE_TRACK_SIZE */
1421 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1423 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1425 tk->i_start += i_invalid;
1426 p_sys->stream.i_offset -= i_invalid;
1430 p_sys->stream.i_used -= i_read;
1432 p_sys->stat.i_bytes += i_read;
1433 p_sys->stat.i_read_count++;
1437 p_sys->stat.i_read_time += i_stop - i_start;
1442 static void AStreamPrebufferStream( stream_t *s )
1444 stream_sys_t *p_sys = s->p_sys;
1445 access_t *p_access = p_sys->p_access;
1447 int64_t i_first = 0;
1449 int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1450 ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1451 STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1453 msg_Dbg( s, "pre-buffering..." );
1457 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1459 int64_t i_date = mdate();
1462 if( s->b_die || tk->i_end >= i_prebuffer ||
1463 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1468 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1469 p_sys->stat.i_read_time = i_date - i_start;
1470 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1471 (p_sys->stat.i_read_time+1);
1473 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1474 "%"PRId64" kbytes/s",
1475 p_sys->stat.i_bytes,
1476 p_sys->stat.i_read_time / INT64_C(1000000),
1477 i_byterate / 1024 );
1482 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1483 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1484 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1487 msleep( STREAM_DATA_WAIT );
1490 else if( i_read == 0 )
1499 msg_Dbg( s, "received first data for our buffer");
1502 tk->i_end += i_read;
1504 p_sys->stat.i_read_count++;
1508 /****************************************************************************
1510 ****************************************************************************/
1512 static int AStreamReadImmediate( stream_t *s, void *p_read, int i_read )
1514 stream_sys_t *p_sys = s->p_sys;
1517 msg_Dbg( s, "AStreamReadImmediate p_read=%p i_read=%d",
1521 if( p_read == NULL )
1523 /* seek within this stream if possible, else use plain old read and discard */
1524 stream_sys_t *p_sys = s->p_sys;
1525 access_t *p_access = p_sys->p_access;
1527 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1529 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1532 /* First, check if we already have some data in the buffer,
1533 * that we could copy directly */
1534 int i_copy = __MIN( stream_buffered_size( s ), i_read );
1538 msg_Dbg( s, "AStreamReadImmediate: copy %d from %p", i_copy, stream_buffer( s ) );
1541 assert( i_copy <= STREAM_CACHE_SIZE );
1545 memcpy( p_read, stream_buffer( s ), i_copy );
1546 p_read = (uint8_t *)p_read + i_copy;
1550 /* Now that we've read our buffer we don't need its i_copy bytes */
1551 stream_buffer_empty( s, i_copy );
1553 /* Now check if we have still to really read some data */
1554 int i_to_read = i_read - i_copy;
1557 i_to_read = AReadStream( s, p_read, i_to_read );
1560 p_sys->i_pos += i_to_read;
1562 return i_to_read + i_copy;
1565 static int AStreamPeekImmediate( stream_t *s, const uint8_t **pp_peek, int i_read )
1568 msg_Dbg( s, "AStreamPeekImmediate: %d size=%"PRId64,
1569 i_read, size_buffered_size( s ) );
1572 /* Avoid problem, but that shouldn't happen */
1573 if( i_read > STREAM_CACHE_SIZE / 2 )
1574 i_read = STREAM_CACHE_SIZE / 2;
1576 int i_to_read = i_read - stream_buffered_size( s );
1580 msg_Dbg( s, "AStreamPeekImmediate: Reading %d",
1583 i_to_read = AReadStream( s, stream_buffer( s ) + stream_buffered_size( s ),
1587 stream_buffer_fill( s, i_to_read );
1590 *pp_peek = stream_buffer( s );
1592 return __MIN(stream_buffered_size( s ), i_read);
1595 static int AStreamSeekImmediate( stream_t *s, int64_t i_pos )
1597 stream_sys_t *p_sys = s->p_sys;
1598 access_t *p_access = p_sys->p_access;
1602 msg_Dbg( s, "AStreamSeekImmediate to %"PRId64" pos=%"PRId64
1603 i_pos, p_sys->i_pos );
1606 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1609 /* We can't do nothing */
1610 msg_Dbg( s, "AStreamSeekImmediate: can't seek" );
1611 return VLC_EGENERIC;
1614 /* Just reset our buffer */
1615 stream_buffer_empty( s, stream_buffered_size( s ) );
1617 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1622 /****************************************************************************
1624 ****************************************************************************/
1626 * Read from the stream untill first newline.
1627 * \param s Stream handle to read from
1628 * \return A pointer to the allocated output string. You need to free this when you are done.
1630 #define STREAM_PROBE_LINE 2048
1631 #define STREAM_LINE_MAX (2048*100)
1632 char * stream_ReadLine( stream_t *s )
1634 char *p_line = NULL;
1635 int i_line = 0, i_read = 0;
1637 while( i_read < STREAM_LINE_MAX )
1640 const uint8_t *p_data;
1644 /* Probe new data */
1645 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1646 if( i_data <= 0 ) break; /* No more data */
1649 i_pos = stream_Tell( s );
1650 if( i_pos == 0 && i_data > 4 )
1653 char *psz_encoding = NULL;
1655 if( p_data[0] == 0xEF && p_data[1] == 0xBB && p_data[2] == 0xBF )
1657 psz_encoding = strdup( "UTF-8" );
1660 else if( p_data[0] == 0x00 && p_data[1] == 0x00 )
1662 if( p_data[2] == 0xFE && p_data[3] == 0xFF )
1664 psz_encoding = strdup( "UTF-32BE" );
1665 s->i_char_width = 4;
1669 else if( p_data[0] == 0xFF && p_data[1] == 0xFE )
1671 if( p_data[2] == 0x00 && p_data[3] == 0x00 )
1673 psz_encoding = strdup( "UTF-32LE" );
1674 s->i_char_width = 4;
1675 s->b_little_endian = true;
1680 psz_encoding = strdup( "UTF-16LE" );
1681 s->b_little_endian = true;
1682 s->i_char_width = 2;
1686 else if( p_data[0] == 0xFE && p_data[1] == 0xFF )
1688 psz_encoding = strdup( "UTF-16BE" );
1689 s->i_char_width = 2;
1693 /* Seek past the BOM */
1696 stream_Seek( s, i_bom_size );
1697 p_data += i_bom_size;
1698 i_data -= i_bom_size;
1701 /* Open the converter if we need it */
1702 if( psz_encoding != NULL )
1704 input_thread_t *p_input;
1705 msg_Dbg( s, "%s BOM detected", psz_encoding );
1706 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1707 if( s->i_char_width > 1 )
1709 s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1710 if( s->conv == (vlc_iconv_t)-1 )
1712 msg_Err( s, "iconv_open failed" );
1715 if( p_input != NULL)
1717 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1718 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1719 vlc_object_release( p_input );
1721 free( psz_encoding );
1725 if( i_data % s->i_char_width )
1727 /* keep i_char_width boundary */
1728 i_data = i_data - ( i_data % s->i_char_width );
1729 msg_Warn( s, "the read is not i_char_width compatible");
1735 /* Check if there is an EOL */
1736 if( s->i_char_width == 1 )
1738 /* UTF-8: 0A <LF> */
1739 psz_eol = memchr( p_data, '\n', i_data );
1743 const uint8_t *p = p_data;
1744 const uint8_t *p_last = p + i_data - s->i_char_width;
1746 if( s->i_char_width == 2 )
1748 if( s->b_little_endian == true)
1750 /* UTF-16LE: 0A 00 <LF> */
1751 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1756 /* UTF-16BE: 00 0A <LF> */
1757 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1761 else if( s->i_char_width == 4 )
1763 if( s->b_little_endian == true)
1765 /* UTF-32LE: 0A 00 00 00 <LF> */
1766 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ||
1767 p[2] != 0x00 || p[3] != 0x00 ) )
1772 /* UTF-32BE: 00 00 00 0A <LF> */
1773 while( p <= p_last && ( p[3] != 0x0A || p[2] != 0x00 ||
1774 p[1] != 0x00 || p[0] != 0x00 ) )
1785 psz_eol = (char *)p + ( s->i_char_width - 1 );
1791 i_data = (psz_eol - (char *)p_data) + 1;
1792 p_line = realloc( p_line, i_line + i_data + s->i_char_width ); /* add \0 */
1795 msg_Err( s, "Out of memory when reallocating p_line" );
1798 i_data = stream_Read( s, &p_line[i_line], i_data );
1799 if( i_data <= 0 ) break; /* Hmmm */
1800 i_line += i_data - s->i_char_width; /* skip \n */;
1803 /* We have our line */
1807 /* Read data (+1 for easy \0 append) */
1808 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->i_char_width );
1811 msg_Err( s, "Out of memory when reallocating p_line" );
1814 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1815 if( i_data <= 0 ) break; /* Hmmm */
1823 for( j = 0; j < s->i_char_width; j++ )
1825 p_line[i_line + j] = '\0';
1827 i_line += s->i_char_width; /* the added \0 */
1828 if( s->i_char_width > 1 )
1830 size_t i_in = 0, i_out = 0;
1831 const char * p_in = NULL;
1832 char * p_out = NULL;
1833 char * psz_new_line = NULL;
1836 psz_new_line = malloc( i_line );
1837 if( psz_new_line == NULL )
1839 msg_Err( s, "Out of memory when allocating psz_new_line" );
1842 i_in = i_out = (size_t)i_line;
1844 p_out = psz_new_line;
1846 if( vlc_iconv( s->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1848 msg_Err( s, "iconv failed" );
1849 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1852 p_line = psz_new_line;
1853 i_line = (size_t)i_line - i_out; /* does not include \0 */
1856 /* Remove trailing LF/CR */
1857 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1858 p_line[i_line-2] == '\n') ) i_line--;
1860 /* Make sure the \0 is there */
1861 p_line[i_line-1] = '\0';
1868 /* We failed to read any data, probably EOF */
1870 if( s->conv != (vlc_iconv_t)(-1) ) vlc_iconv_close( s->conv );
1874 /****************************************************************************
1875 * Access reading/seeking wrappers to handle concatenated streams.
1876 ****************************************************************************/
1877 static int AReadStream( stream_t *s, void *p_read, int i_read )
1879 stream_sys_t *p_sys = s->p_sys;
1880 access_t *p_access = p_sys->p_access;
1881 input_thread_t *p_input = NULL;
1882 int i_read_orig = i_read;
1885 if( s->p_parent && s->p_parent->p_parent &&
1886 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1887 p_input = (input_thread_t *)s->p_parent->p_parent;
1889 if( !p_sys->i_list )
1891 i_read = p_access->pf_read( p_access, p_read, i_read );
1894 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1895 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
1897 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1898 (float)i_total, NULL );
1899 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1900 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1905 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1908 /* If we reached an EOF then switch to the next stream in the list */
1909 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1911 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1912 access_t *p_list_access;
1914 msg_Dbg( s, "opening input `%s'", psz_name );
1916 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1918 if( !p_list_access ) return 0;
1920 if( p_sys->p_list_access != p_access )
1921 access_Delete( p_sys->p_list_access );
1923 p_sys->p_list_access = p_list_access;
1925 /* We have to read some data */
1926 return AReadStream( s, p_read, i_read_orig );
1929 /* Update read bytes in input */
1932 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1933 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
1934 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1935 (float)i_total, NULL );
1936 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1937 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1942 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
1944 stream_sys_t *p_sys = s->p_sys;
1945 access_t *p_access = p_sys->p_access;
1946 input_thread_t *p_input = NULL;
1951 if( s->p_parent && s->p_parent->p_parent &&
1952 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1953 p_input = (input_thread_t *)s->p_parent->p_parent;
1955 if( !p_sys->i_list )
1957 p_block = p_access->pf_block( p_access );
1958 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1959 if( p_input && p_block && libvlc_stats (p_access) )
1961 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1962 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1963 p_block->i_buffer, &i_total );
1964 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1965 (float)i_total, NULL );
1966 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1967 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1972 p_block = p_sys->p_list_access->pf_block( p_access );
1973 b_eof = p_sys->p_list_access->info.b_eof;
1974 if( pb_eof ) *pb_eof = b_eof;
1976 /* If we reached an EOF then switch to the next stream in the list */
1977 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1979 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1980 access_t *p_list_access;
1982 msg_Dbg( s, "opening input `%s'", psz_name );
1984 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1986 if( !p_list_access ) return 0;
1988 if( p_sys->p_list_access != p_access )
1989 access_Delete( p_sys->p_list_access );
1991 p_sys->p_list_access = p_list_access;
1993 /* We have to read some data */
1994 return AReadBlock( s, pb_eof );
2000 vlc_mutex_lock( &p_input->p->counters.counters_lock );
2001 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
2002 p_block->i_buffer, &i_total );
2003 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
2004 (float)i_total, NULL );
2005 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
2007 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2013 static int ASeek( stream_t *s, int64_t i_pos )
2015 stream_sys_t *p_sys = s->p_sys;
2016 access_t *p_access = p_sys->p_access;
2018 /* Check which stream we need to access */
2024 access_t *p_list_access = 0;
2026 for( i = 0; i < p_sys->i_list - 1; i++ )
2028 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
2029 i_size += p_sys->list[i]->i_size;
2031 psz_name = p_sys->list[i]->psz_path;
2033 if( i != p_sys->i_list_index )
2034 msg_Dbg( s, "opening input `%s'", psz_name );
2036 if( i != p_sys->i_list_index && i != 0 )
2039 access_New( s, p_access->psz_access, "", psz_name );
2041 else if( i != p_sys->i_list_index )
2043 p_list_access = p_access;
2048 if( p_sys->p_list_access != p_access )
2049 access_Delete( p_sys->p_list_access );
2051 p_sys->p_list_access = p_list_access;
2054 p_sys->i_list_index = i;
2055 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
2059 return p_access->pf_seek( p_access, i_pos );
2064 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
2065 * "p_read" is NULL then data are skipped instead of read. The return
2066 * value is the real numbers of bytes read/skip. If this value is less
2067 * than i_read that means that it's the end of the stream.
2069 int stream_Read( stream_t *s, void *p_read, int i_read )
2073 return s->pf_read( s, p_read, i_read );
2077 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
2078 * \return The real numbers of valid bytes, if it's less
2079 * or equal to 0, *pp_peek is invalid.
2080 * \note pp_peek is a pointer to internal buffer and it will be invalid as
2081 * soons as other stream_* functions are called.
2082 * \note Due to input limitation, it could be less than i_peek without meaning
2083 * the end of the stream (but only when you have i_peek >=
2084 * p_input->i_bufsize)
2086 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
2088 return s->pf_peek( s, pp_peek, i_peek );
2092 * Use to control the "stream_t *". Look at #stream_query_e for
2093 * possible "i_query" value and format arguments. Return VLC_SUCCESS
2094 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
2096 int stream_vaControl( stream_t *s, int i_query, va_list args )
2098 return s->pf_control( s, i_query, args );
2104 void stream_Delete( stream_t *s )
2109 int stream_Control( stream_t *s, int i_query, ... )
2115 return VLC_EGENERIC;
2117 va_start( args, i_query );
2118 i_result = s->pf_control( s, i_query, args );
2124 * Read "i_size" bytes and store them in a block_t.
2125 * It always read i_size bytes unless you are at the end of the stream
2126 * where it return what is available.
2128 block_t *stream_Block( stream_t *s, int i_size )
2130 if( i_size <= 0 ) return NULL;
2132 /* emulate block read */
2133 block_t *p_bk = block_New( s, i_size );
2136 p_bk->i_buffer = stream_Read( s, p_bk->p_buffer, i_size );
2137 if( p_bk->i_buffer > 0 )
2142 if( p_bk ) block_Release( p_bk );