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 ) );
302 /* UTF16 and UTF32 text file conversion */
304 s->b_little_endian = false;
305 s->conv = (vlc_iconv_t)(-1);
308 p_sys->p_access = p_access;
309 if( p_access->pf_block )
310 p_sys->method = Block;
311 else if (var_CreateGetBool( s, "use-stream-immediate"))
312 p_sys->method = Immediate;
314 p_sys->method = Stream;
316 p_sys->i_pos = p_access->info.i_pos;
319 access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
320 p_sys->stat.i_bytes = 0;
321 p_sys->stat.i_read_time = 0;
322 p_sys->stat.i_read_count = 0;
323 p_sys->stat.i_seek_count = 0;
324 p_sys->stat.i_seek_time = 0;
328 p_sys->i_list_index = 0;
329 p_sys->p_list_access = 0;
331 p_sys->b_quick = b_quick;
333 /* Get the additional list of inputs if any (for concatenation) */
334 if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
336 access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
337 if( p_entry == NULL )
339 char *psz_name, *psz_parser = psz_name = psz_list;
341 p_sys->p_list_access = p_access;
342 p_entry->i_size = p_access->info.i_size;
343 p_entry->psz_path = strdup( p_access->psz_path );
344 if( p_entry->psz_path == NULL )
349 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
350 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
351 p_entry->psz_path, p_access->info.i_size );
353 while( psz_name && *psz_name )
355 psz_parser = strchr( psz_name, ',' );
356 if( psz_parser ) *psz_parser = 0;
358 psz_name = strdup( psz_name );
361 access_t *p_tmp = access_New( p_access, p_access->psz_access,
366 psz_name = psz_parser;
367 if( psz_name ) psz_name++;
371 msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
372 psz_name, p_tmp->info.i_size );
374 p_entry = malloc( sizeof(access_entry_t) );
375 if( p_entry == NULL )
377 p_entry->i_size = p_tmp->info.i_size;
378 p_entry->psz_path = psz_name;
379 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
381 access_Delete( p_tmp );
384 psz_name = psz_parser;
385 if( psz_name ) psz_name++;
388 FREENULL( psz_list );
392 p_sys->p_peek = NULL;
394 if( p_sys->method == Block )
396 msg_Dbg( s, "Using AStream*Block" );
397 s->pf_read = AStreamReadBlock;
398 s->pf_peek = AStreamPeekBlock;
400 /* Init all fields of p_sys->block */
401 p_sys->block.i_start = p_sys->i_pos;
402 p_sys->block.i_offset = 0;
403 p_sys->block.p_current = NULL;
404 p_sys->block.i_size = 0;
405 p_sys->block.p_first = NULL;
406 p_sys->block.pp_last = &p_sys->block.p_first;
408 /* Do the prebuffering */
409 AStreamPrebufferBlock( s );
411 if( p_sys->block.i_size <= 0 )
413 msg_Err( s, "cannot pre fill buffer" );
417 else if (p_sys->method == Immediate)
419 msg_Dbg( s, "Using AStream*Immediate" );
421 s->pf_read = AStreamReadImmediate;
422 s->pf_peek = AStreamPeekImmediate;
424 /* Allocate/Setup our tracks (useful to peek)*/
425 p_sys->immediate.i_end = 0;
426 p_sys->immediate.p_buffer = malloc( STREAM_CACHE_SIZE );
428 msg_Dbg( s, "p_buffer %p-%p", p_sys->immediate.p_buffer,
429 p_sys->immediate.p_buffer + STREAM_CACHE_SIZE );
431 if( p_sys->immediate.p_buffer == NULL )
433 msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
438 else /* ( p_sys->method == Stream ) */
442 msg_Dbg( s, "Using AStream*Stream" );
444 s->pf_read = AStreamReadStream;
445 s->pf_peek = AStreamPeekStream;
447 /* Allocate/Setup our tracks */
448 p_sys->stream.i_offset = 0;
449 p_sys->stream.i_tk = 0;
450 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
451 if( p_sys->stream.p_buffer == NULL )
453 msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
457 p_sys->stream.i_used = 0;
458 access_Control( p_access, ACCESS_GET_MTU,
459 &p_sys->stream.i_read_size );
460 if( p_sys->stream.i_read_size <= 0 )
461 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
462 else if( p_sys->stream.i_read_size <= 256 )
463 p_sys->stream.i_read_size = 256;
465 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
467 p_sys->stream.tk[i].i_date = 0;
468 p_sys->stream.tk[i].i_start = p_sys->i_pos;
469 p_sys->stream.tk[i].i_end = p_sys->i_pos;
470 p_sys->stream.tk[i].p_buffer=
471 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
474 /* Do the prebuffering */
475 AStreamPrebufferStream( s );
477 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
479 msg_Err( s, "cannot pre fill buffer" );
487 if( p_sys->method == Block )
493 free( p_sys->stream.p_buffer );
495 while( p_sys->i_list > 0 )
496 free( p_sys->list[--(p_sys->i_list)] );
500 vlc_object_detach( s );
501 vlc_object_release( s );
505 /****************************************************************************
507 ****************************************************************************/
508 static void AStreamDestroy( stream_t *s )
510 stream_sys_t *p_sys = s->p_sys;
512 vlc_object_detach( s );
514 if( p_sys->method == Block ) block_ChainRelease( p_sys->block.p_first );
515 else free( p_sys->stream.p_buffer );
517 free( p_sys->p_peek );
519 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
520 access_Delete( p_sys->p_list_access );
522 while( p_sys->i_list-- )
524 free( p_sys->list[p_sys->i_list]->psz_path );
525 free( p_sys->list[p_sys->i_list] );
531 vlc_object_release( s );
534 static void UStreamDestroy( stream_t *s )
536 access_t *p_access = (access_t *)s->p_parent;
538 vlc_object_release( p_access );
539 access_Delete( p_access );
542 /****************************************************************************
543 * stream_AccessReset:
544 ****************************************************************************/
545 void stream_AccessReset( stream_t *s )
547 stream_sys_t *p_sys = s->p_sys;
549 p_sys->i_pos = p_sys->p_access->info.i_pos;
551 if( p_sys->method == Block )
553 block_ChainRelease( p_sys->block.p_first );
555 /* Init all fields of p_sys->block */
556 p_sys->block.i_start = p_sys->i_pos;
557 p_sys->block.i_offset = 0;
558 p_sys->block.p_current = NULL;
559 p_sys->block.i_size = 0;
560 p_sys->block.p_first = NULL;
561 p_sys->block.pp_last = &p_sys->block.p_first;
563 /* Do the prebuffering */
564 AStreamPrebufferBlock( s );
566 else if( p_sys->method == Immediate )
568 stream_buffer_empty( s, stream_buffered_size( s ) );
570 else /* ( p_sys->method == Stream ) */
574 /* Setup our tracks */
575 p_sys->stream.i_offset = 0;
576 p_sys->stream.i_tk = 0;
577 p_sys->stream.i_used = 0;
579 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
581 p_sys->stream.tk[i].i_date = 0;
582 p_sys->stream.tk[i].i_start = p_sys->i_pos;
583 p_sys->stream.tk[i].i_end = p_sys->i_pos;
586 /* Do the prebuffering */
587 AStreamPrebufferStream( s );
591 /****************************************************************************
592 * stream_AccessUpdate:
593 ****************************************************************************/
594 void stream_AccessUpdate( stream_t *s )
596 stream_sys_t *p_sys = s->p_sys;
598 p_sys->i_pos = p_sys->p_access->info.i_pos;
603 for( i = 0; i < p_sys->i_list_index; i++ )
605 p_sys->i_pos += p_sys->list[i]->i_size;
610 /****************************************************************************
612 ****************************************************************************/
613 static int AStreamControl( stream_t *s, int i_query, va_list args )
615 stream_sys_t *p_sys = s->p_sys;
616 access_t *p_access = p_sys->p_access;
619 int64_t *pi_64, i_64;
624 case STREAM_GET_SIZE:
625 pi_64 = (int64_t*)va_arg( args, int64_t * );
626 if( s->p_sys->i_list )
630 for( i = 0; i < s->p_sys->i_list; i++ )
631 *pi_64 += s->p_sys->list[i]->i_size;
634 *pi_64 = p_access->info.i_size;
637 case STREAM_CAN_SEEK:
638 p_bool = (bool*)va_arg( args, bool * );
639 access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
642 case STREAM_CAN_FASTSEEK:
643 p_bool = (bool*)va_arg( args, bool * );
644 access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
647 case STREAM_GET_POSITION:
648 pi_64 = (int64_t*)va_arg( args, int64_t * );
649 *pi_64 = p_sys->i_pos;
652 case STREAM_SET_POSITION:
653 i_64 = (int64_t)va_arg( args, int64_t );
654 if( p_sys->method == Block )
655 return AStreamSeekBlock( s, i_64 );
656 else if( p_sys->method == Immediate )
657 return AStreamSeekImmediate( s, i_64 );
658 else /* ( p_sys->method == Stream ) */
659 return AStreamSeekStream( s, i_64 );
664 case STREAM_CONTROL_ACCESS:
665 i_int = (int) va_arg( args, int );
666 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
667 i_int != ACCESS_SET_PRIVATE_ID_CA &&
668 i_int != ACCESS_GET_PRIVATE_ID_STATE )
670 msg_Err( s, "Hey, what are you thinking ?"
671 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
674 return access_vaControl( p_access, i_int, args );
676 case STREAM_GET_CONTENT_TYPE:
677 return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
678 va_arg( args, char ** ) );
681 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
689 /****************************************************************************
691 ****************************************************************************/
692 static void AStreamPrebufferBlock( stream_t *s )
694 stream_sys_t *p_sys = s->p_sys;
695 access_t *p_access = p_sys->p_access;
700 msg_Dbg( s, "pre buffering" );
704 int64_t i_date = mdate();
708 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
709 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
714 p_sys->stat.i_bytes = p_sys->block.i_size;
715 p_sys->stat.i_read_time = i_date - i_start;
716 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
717 (p_sys->stat.i_read_time + 1);
719 msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
720 "%"PRId64" kbytes/s",
722 p_sys->stat.i_read_time / INT64_C(1000000),
728 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
732 msleep( STREAM_DATA_WAIT );
738 /* Append the block */
739 p_sys->block.i_size += b->i_buffer;
740 *p_sys->block.pp_last = b;
741 p_sys->block.pp_last = &b->p_next;
743 p_sys->stat.i_read_count++;
747 if( p_access->info.b_prebuffered )
749 /* Access has already prebufferred - update stats and exit */
750 p_sys->stat.i_bytes = p_sys->block.i_size;
751 p_sys->stat.i_read_time = mdate() - i_start;
758 msg_Dbg( s, "received first data for our buffer");
763 p_sys->block.p_current = p_sys->block.p_first;
766 static int AStreamRefillBlock( stream_t *s );
768 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
770 stream_sys_t *p_sys = s->p_sys;
772 uint8_t *p_data= (uint8_t*)p_read;
776 if( p_sys->block.p_current == NULL )
781 /* seek within this stream if possible, else use plain old read and discard */
782 stream_sys_t *p_sys = s->p_sys;
783 access_t *p_access = p_sys->p_access;
785 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
787 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
790 while( i_data < i_read )
793 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
794 int i_copy = __MIN( i_current, i_read - i_data);
800 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
806 p_sys->block.i_offset += i_copy;
807 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
809 /* Current block is now empty, switch to next */
810 if( p_sys->block.p_current )
812 p_sys->block.i_offset = 0;
813 p_sys->block.p_current = p_sys->block.p_current->p_next;
815 /*Get a new block if needed */
816 if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
823 p_sys->i_pos += i_data;
827 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, int i_read )
829 stream_sys_t *p_sys = s->p_sys;
835 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
837 /* We can directly give a pointer over our buffer */
838 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
840 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
844 /* We need to create a local copy */
845 if( p_sys->i_peek < i_read )
847 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
853 p_sys->i_peek = i_read;
856 /* Fill enough data */
857 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
860 block_t **pp_last = p_sys->block.pp_last;
862 if( AStreamRefillBlock( s ) ) break;
864 /* Our buffer are probably filled enough, don't try anymore */
865 if( pp_last == p_sys->block.pp_last ) break;
868 /* Copy what we have */
869 b = p_sys->block.p_current;
870 i_offset = p_sys->block.i_offset;
871 p_data = p_sys->p_peek;
873 while( b && i_data < i_read )
875 int i_current = b->i_buffer - i_offset;
876 int i_copy = __MIN( i_current, i_read - i_data );
878 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
883 if( i_offset >= b->i_buffer )
890 *pp_peek = p_sys->p_peek;
894 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
896 stream_sys_t *p_sys = s->p_sys;
897 access_t *p_access = p_sys->p_access;
898 int64_t i_offset = i_pos - p_sys->block.i_start;
901 /* We already have thoses data, just update p_current/i_offset */
902 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
904 block_t *b = p_sys->block.p_first;
907 while( i_current + b->i_buffer < i_offset )
909 i_current += b->i_buffer;
913 p_sys->block.p_current = b;
914 p_sys->block.i_offset = i_offset - i_current;
916 p_sys->i_pos = i_pos;
921 /* We may need to seek or to read data */
925 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
929 msg_Err( s, "backward seeking impossible (access not seekable)" );
937 bool b_aseek, b_aseekfast;
939 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
940 access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
945 msg_Warn( s, "%"PRId64" bytes need to be skipped "
946 "(access non seekable)",
947 i_offset - p_sys->block.i_size );
951 int64_t i_skip = i_offset - p_sys->block.i_size;
953 /* Avg bytes per packets */
954 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
955 /* TODO compute a seek cost instead of fixed threshold */
956 int i_th = b_aseekfast ? 1 : 5;
958 if( i_skip <= i_th * i_avg &&
959 i_skip < STREAM_CACHE_SIZE )
964 msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
965 b_seek, i_th*i_avg, i_skip );
971 int64_t i_start, i_end;
972 /* Do the access seek */
974 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
978 block_ChainRelease( p_sys->block.p_first );
981 p_sys->block.i_start = p_sys->i_pos = i_pos;
982 p_sys->block.i_offset = 0;
983 p_sys->block.p_current = NULL;
984 p_sys->block.i_size = 0;
985 p_sys->block.p_first = NULL;
986 p_sys->block.pp_last = &p_sys->block.p_first;
989 if( AStreamRefillBlock( s ) )
993 p_sys->stat.i_seek_time += i_end - i_start;
994 p_sys->stat.i_seek_count++;
999 /* Read enough data */
1000 while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
1002 if( AStreamRefillBlock( s ) )
1003 return VLC_EGENERIC;
1005 while( p_sys->block.p_current &&
1006 p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
1008 p_sys->i_pos += p_sys->block.p_current->i_buffer;
1009 p_sys->block.p_current = p_sys->block.p_current->p_next;
1013 p_sys->block.i_offset = i_pos - p_sys->i_pos;
1014 p_sys->i_pos = i_pos;
1019 return VLC_EGENERIC;
1022 static int AStreamRefillBlock( stream_t *s )
1024 stream_sys_t *p_sys = s->p_sys;
1025 int64_t i_start, i_stop;
1029 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1030 p_sys->block.p_first != p_sys->block.p_current )
1032 block_t *b = p_sys->block.p_first;
1034 p_sys->block.i_start += b->i_buffer;
1035 p_sys->block.i_size -= b->i_buffer;
1036 p_sys->block.p_first = b->p_next;
1040 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
1041 p_sys->block.p_current == p_sys->block.p_first &&
1042 p_sys->block.p_current->p_next ) /* At least 2 packets */
1044 /* Enough data, don't read more */
1048 /* Now read a new block */
1054 if( s->b_die ) return VLC_EGENERIC;
1058 if( ( b = AReadBlock( s, &b_eof ) ) ) break;
1060 if( b_eof ) return VLC_EGENERIC;
1062 msleep( STREAM_DATA_WAIT );
1069 /* Append the block */
1070 p_sys->block.i_size += b->i_buffer;
1071 *p_sys->block.pp_last = b;
1072 p_sys->block.pp_last = &b->p_next;
1075 if( p_sys->block.p_current == NULL )
1076 p_sys->block.p_current = b;
1079 p_sys->stat.i_bytes += b->i_buffer;
1080 p_sys->stat.i_read_time += i_stop - i_start;
1081 p_sys->stat.i_read_count++;
1090 /****************************************************************************
1092 ****************************************************************************/
1093 static int AStreamRefillStream( stream_t *s );
1095 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
1097 stream_sys_t *p_sys = s->p_sys;
1098 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1100 uint8_t *p_data = (uint8_t *)p_read;
1103 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1105 if( p_read == NULL )
1107 /* seek within this stream if possible, else use plain old read and discard */
1108 stream_sys_t *p_sys = s->p_sys;
1109 access_t *p_access = p_sys->p_access;
1111 /* seeking after EOF is not what we want */
1112 if( !( p_access->info.b_eof ) )
1115 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1117 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1122 msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
1123 " offset=%d end=%"PRId64,
1124 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1125 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1128 while( i_data < i_read )
1130 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1131 STREAM_CACHE_TRACK_SIZE;
1133 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1134 STREAM_CACHE_TRACK_SIZE - i_off );
1135 int i_copy = __MIN( i_current, i_read - i_data );
1137 if( i_copy <= 0 ) break; /* EOF */
1140 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1143 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1147 p_sys->stream.i_offset += i_copy;
1149 /* Update pos now */
1150 p_sys->i_pos += i_copy;
1153 p_sys->stream.i_used += i_copy;
1154 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1155 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1157 if( AStreamRefillStream( s ) )
1160 if( tk->i_start >= tk->i_end ) break;
1168 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, int i_read )
1170 stream_sys_t *p_sys = s->p_sys;
1171 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1174 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1177 msg_Dbg( s, "AStreamPeekStream: %d pos=%"PRId64" tk=%d "
1178 "start=%"PRId64" offset=%d end=%"PRId64,
1179 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1180 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1183 /* Avoid problem, but that should *never* happen */
1184 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1185 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1187 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1189 if( p_sys->stream.i_used <= 1 )
1191 /* Be sure we will read something */
1192 p_sys->stream.i_used += i_read -
1193 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1195 if( AStreamRefillStream( s ) ) break;
1198 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1199 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1201 /* Now, direct pointer or a copy ? */
1202 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1203 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1205 *pp_peek = &tk->p_buffer[i_off];
1209 if( p_sys->i_peek < i_read )
1211 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1212 if( !p_sys->p_peek )
1217 p_sys->i_peek = i_read;
1220 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1221 STREAM_CACHE_TRACK_SIZE - i_off );
1222 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1223 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1225 *pp_peek = p_sys->p_peek;
1229 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1231 stream_sys_t *p_sys = s->p_sys;
1232 access_t *p_access = p_sys->p_access;
1240 msg_Dbg( s, "AStreamSeekStream: to %"PRId64" pos=%"PRId64
1241 " tk=%d start=%"PRId64" offset=%d end=%"PRId64,
1242 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1243 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1244 p_sys->stream.i_offset,
1245 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1249 /* Seek in our current track ? */
1250 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1251 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1253 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1255 msg_Dbg( s, "AStreamSeekStream: current track" );
1257 p_sys->i_pos = i_pos;
1258 p_sys->stream.i_offset = i_pos - tk->i_start;
1260 /* If there is not enough data left in the track, refill */
1261 /* \todo How to get a correct value for
1262 * - refilling threshold
1263 * - how much to refill
1265 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1266 p_sys->stream.i_read_size )
1268 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1270 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1271 AStreamRefillStream( s );
1277 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1280 /* We can't do nothing */
1281 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1282 return VLC_EGENERIC;
1285 /* Date the current track */
1286 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1288 /* Try to reuse already read data */
1289 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1291 stream_track_t *tk = &p_sys->stream.tk[i];
1293 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1296 msg_Dbg( s, "AStreamSeekStream: reusing %d start=%"PRId64
1297 " end=%"PRId64, i, tk->i_start, tk->i_end );
1300 /* Seek at the end of the buffer */
1301 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1304 p_sys->i_pos = i_pos;
1305 p_sys->stream.i_tk = i;
1306 p_sys->stream.i_offset = i_pos - tk->i_start;
1308 if( p_sys->stream.i_used < 1024 )
1309 p_sys->stream.i_used = 1024;
1311 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1312 return VLC_EGENERIC;
1318 access_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1319 /* FIXME compute seek cost (instead of static 'stupid' value) */
1320 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1326 /* Search closest segment TODO */
1327 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1329 stream_track_t *tk = &p_sys->stream.tk[i];
1331 if( i_pos + i_maxth >= tk->i_start )
1333 msg_Dbg( s, "good segment before current pos, TODO" );
1335 if( i_pos - i_maxth <= tk->i_end )
1337 msg_Dbg( s, "good segment after current pos, TODO" );
1342 /* Nothing good, seek and choose oldest segment */
1343 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1344 p_sys->i_pos = i_pos;
1347 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1349 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1353 /* Reset the segment */
1354 p_sys->stream.i_tk = i_new;
1355 p_sys->stream.i_offset = 0;
1356 p_sys->stream.tk[i_new].i_start = i_pos;
1357 p_sys->stream.tk[i_new].i_end = i_pos;
1360 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1361 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1363 if( AStreamRefillStream( s ) )
1364 return VLC_EGENERIC;
1369 static int AStreamRefillStream( stream_t *s )
1371 stream_sys_t *p_sys = s->p_sys;
1372 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1374 /* We read but won't increase i_start after initial start + offset */
1376 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1377 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1378 bool b_read = false;
1379 int64_t i_start, i_stop;
1381 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1384 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1385 p_sys->stream.i_used, i_toread );
1389 while( i_toread > 0 )
1391 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1395 return VLC_EGENERIC;
1397 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1398 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1400 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1403 msleep( STREAM_DATA_WAIT );
1406 else if( i_read == 0 )
1408 if( !b_read ) return VLC_EGENERIC;
1414 tk->i_end += i_read;
1416 /* Windows of STREAM_CACHE_TRACK_SIZE */
1417 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1419 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1421 tk->i_start += i_invalid;
1422 p_sys->stream.i_offset -= i_invalid;
1426 p_sys->stream.i_used -= i_read;
1428 p_sys->stat.i_bytes += i_read;
1429 p_sys->stat.i_read_count++;
1433 p_sys->stat.i_read_time += i_stop - i_start;
1438 static void AStreamPrebufferStream( stream_t *s )
1440 stream_sys_t *p_sys = s->p_sys;
1441 access_t *p_access = p_sys->p_access;
1443 int64_t i_first = 0;
1445 int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1446 ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1447 STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1449 msg_Dbg( s, "pre-buffering..." );
1453 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1455 int64_t i_date = mdate();
1458 if( s->b_die || tk->i_end >= i_prebuffer ||
1459 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1464 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1465 p_sys->stat.i_read_time = i_date - i_start;
1466 i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
1467 (p_sys->stat.i_read_time+1);
1469 msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
1470 "%"PRId64" kbytes/s",
1471 p_sys->stat.i_bytes,
1472 p_sys->stat.i_read_time / INT64_C(1000000),
1473 i_byterate / 1024 );
1478 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1479 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1480 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1483 msleep( STREAM_DATA_WAIT );
1486 else if( i_read == 0 )
1495 msg_Dbg( s, "received first data for our buffer");
1498 tk->i_end += i_read;
1500 p_sys->stat.i_read_count++;
1504 /****************************************************************************
1506 ****************************************************************************/
1508 static int AStreamReadImmediate( stream_t *s, void *p_read, int i_read )
1510 stream_sys_t *p_sys = s->p_sys;
1513 msg_Dbg( s, "AStreamReadImmediate p_read=%p i_read=%d",
1517 /* First, check if we already have some data in the buffer,
1518 * that we could copy directly */
1519 int i_copy = __MIN( stream_buffered_size( s ), i_read );
1523 msg_Dbg( s, "AStreamReadImmediate: copy %d from %p", i_copy, stream_buffer( s ) );
1526 assert( i_copy <= STREAM_CACHE_SIZE );
1530 memcpy( p_read, stream_buffer( s ), i_copy );
1531 p_read = (uint8_t *)p_read + i_copy;
1535 /* Now that we've read our buffer we don't need its i_copy bytes */
1536 stream_buffer_empty( s, i_copy );
1538 /* Now check if we have still to really read some data */
1539 int i_to_read = i_read - i_copy;
1543 i_to_read = AReadStream( s, p_read, i_to_read );
1546 void * dummy = malloc(i_to_read);
1547 i_to_read = AReadStream( s, dummy, i_to_read );
1552 p_sys->i_pos += i_to_read;
1554 return i_to_read + i_copy;
1557 static int AStreamPeekImmediate( stream_t *s, const uint8_t **pp_peek, int i_read )
1560 msg_Dbg( s, "AStreamPeekImmediate: %d size=%"PRId64,
1561 i_read, size_buffered_size( s ) );
1564 /* Avoid problem, but that shouldn't happen */
1565 if( i_read > STREAM_CACHE_SIZE / 2 )
1566 i_read = STREAM_CACHE_SIZE / 2;
1568 int i_to_read = i_read - stream_buffered_size( s );
1572 msg_Dbg( s, "AStreamPeekImmediate: Reading %d",
1575 i_to_read = AReadStream( s, stream_buffer( s ) + stream_buffered_size( s ),
1579 stream_buffer_fill( s, i_to_read );
1582 *pp_peek = stream_buffer( s );
1584 return __MIN(stream_buffered_size( s ), i_read);
1587 static int AStreamSeekImmediate( stream_t *s, int64_t i_pos )
1589 stream_sys_t *p_sys = s->p_sys;
1590 access_t *p_access = p_sys->p_access;
1594 msg_Dbg( s, "AStreamSeekImmediate to %"PRId64" pos=%"PRId64
1595 i_pos, p_sys->i_pos );
1598 access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1601 /* We can't do nothing */
1602 msg_Dbg( s, "AStreamSeekImmediate: can't seek" );
1603 return VLC_EGENERIC;
1606 /* Just reset our buffer */
1607 stream_buffer_empty( s, stream_buffered_size( s ) );
1609 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1614 /****************************************************************************
1616 ****************************************************************************/
1618 * Read from the stream untill first newline.
1619 * \param s Stream handle to read from
1620 * \return A pointer to the allocated output string. You need to free this when you are done.
1622 #define STREAM_PROBE_LINE 2048
1623 #define STREAM_LINE_MAX (2048*100)
1624 char * stream_ReadLine( stream_t *s )
1626 char *p_line = NULL;
1627 int i_line = 0, i_read = 0;
1629 while( i_read < STREAM_LINE_MAX )
1632 const uint8_t *p_data;
1636 /* Probe new data */
1637 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1638 if( i_data <= 0 ) break; /* No more data */
1641 i_pos = stream_Tell( s );
1642 if( i_pos == 0 && i_data > 4 )
1645 char *psz_encoding = NULL;
1647 if( p_data[0] == 0xEF && p_data[1] == 0xBB && p_data[2] == 0xBF )
1649 psz_encoding = strdup( "UTF-8" );
1652 else if( p_data[0] == 0x00 && p_data[1] == 0x00 )
1654 if( p_data[2] == 0xFE && p_data[3] == 0xFF )
1656 psz_encoding = strdup( "UTF-32BE" );
1657 s->i_char_width = 4;
1661 else if( p_data[0] == 0xFF && p_data[1] == 0xFE )
1663 if( p_data[2] == 0x00 && p_data[3] == 0x00 )
1665 psz_encoding = strdup( "UTF-32LE" );
1666 s->i_char_width = 4;
1667 s->b_little_endian = true;
1672 psz_encoding = strdup( "UTF-16LE" );
1673 s->b_little_endian = true;
1674 s->i_char_width = 2;
1678 else if( p_data[0] == 0xFE && p_data[1] == 0xFF )
1680 psz_encoding = strdup( "UTF-16BE" );
1681 s->i_char_width = 2;
1685 /* Seek past the BOM */
1688 stream_Seek( s, i_bom_size );
1689 p_data += i_bom_size;
1690 i_data -= i_bom_size;
1693 /* Open the converter if we need it */
1694 if( psz_encoding != NULL )
1696 input_thread_t *p_input;
1697 msg_Dbg( s, "%s BOM detected", psz_encoding );
1698 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1699 if( s->i_char_width > 1 )
1701 s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1702 if( s->conv == (vlc_iconv_t)-1 )
1704 msg_Err( s, "iconv_open failed" );
1707 if( p_input != NULL)
1709 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1710 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1711 vlc_object_release( p_input );
1713 free( psz_encoding );
1717 if( i_data % s->i_char_width )
1719 /* keep i_char_width boundary */
1720 i_data = i_data - ( i_data % s->i_char_width );
1721 msg_Warn( s, "the read is not i_char_width compatible");
1727 /* Check if there is an EOL */
1728 if( s->i_char_width == 1 )
1730 /* UTF-8: 0A <LF> */
1731 psz_eol = memchr( p_data, '\n', i_data );
1735 const uint8_t *p = p_data;
1736 const uint8_t *p_last = p + i_data - s->i_char_width;
1738 if( s->i_char_width == 2 )
1740 if( s->b_little_endian == true)
1742 /* UTF-16LE: 0A 00 <LF> */
1743 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1748 /* UTF-16BE: 00 0A <LF> */
1749 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1753 else if( s->i_char_width == 4 )
1755 if( s->b_little_endian == true)
1757 /* UTF-32LE: 0A 00 00 00 <LF> */
1758 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ||
1759 p[2] != 0x00 || p[3] != 0x00 ) )
1764 /* UTF-32BE: 00 00 00 0A <LF> */
1765 while( p <= p_last && ( p[3] != 0x0A || p[2] != 0x00 ||
1766 p[1] != 0x00 || p[0] != 0x00 ) )
1777 psz_eol = (char *)p + ( s->i_char_width - 1 );
1783 i_data = (psz_eol - (char *)p_data) + 1;
1784 p_line = realloc( p_line, i_line + i_data + s->i_char_width ); /* add \0 */
1787 i_data = stream_Read( s, &p_line[i_line], i_data );
1788 if( i_data <= 0 ) break; /* Hmmm */
1789 i_line += i_data - s->i_char_width; /* skip \n */;
1792 /* We have our line */
1796 /* Read data (+1 for easy \0 append) */
1797 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->i_char_width );
1800 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1801 if( i_data <= 0 ) break; /* Hmmm */
1809 for( j = 0; j < s->i_char_width; j++ )
1811 p_line[i_line + j] = '\0';
1813 i_line += s->i_char_width; /* the added \0 */
1814 if( s->i_char_width > 1 )
1816 size_t i_in = 0, i_out = 0;
1817 const char * p_in = NULL;
1818 char * p_out = NULL;
1819 char * psz_new_line = NULL;
1822 psz_new_line = malloc( i_line );
1823 if( psz_new_line == NULL )
1825 i_in = i_out = (size_t)i_line;
1827 p_out = psz_new_line;
1829 if( vlc_iconv( s->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1831 msg_Err( s, "iconv failed" );
1832 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1835 p_line = psz_new_line;
1836 i_line = (size_t)i_line - i_out; /* does not include \0 */
1839 /* Remove trailing LF/CR */
1840 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1841 p_line[i_line-2] == '\n') ) i_line--;
1843 /* Make sure the \0 is there */
1844 p_line[i_line-1] = '\0';
1851 /* We failed to read any data, probably EOF */
1853 if( s->conv != (vlc_iconv_t)(-1) ) vlc_iconv_close( s->conv );
1857 /****************************************************************************
1858 * Access reading/seeking wrappers to handle concatenated streams.
1859 ****************************************************************************/
1860 static int AReadStream( stream_t *s, void *p_read, int i_read )
1862 stream_sys_t *p_sys = s->p_sys;
1863 access_t *p_access = p_sys->p_access;
1864 input_thread_t *p_input = NULL;
1865 int i_read_orig = i_read;
1868 if( s->p_parent && s->p_parent->p_parent &&
1869 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1870 p_input = (input_thread_t *)s->p_parent->p_parent;
1872 if( !p_sys->i_list )
1874 i_read = p_access->pf_read( p_access, p_read, i_read );
1875 if( p_access->b_die )
1876 vlc_object_kill( s );
1879 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1880 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
1882 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1883 (float)i_total, NULL );
1884 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1885 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1890 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1892 if( p_access->b_die )
1893 vlc_object_kill( s );
1895 /* If we reached an EOF then switch to the next stream in the list */
1896 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1898 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1899 access_t *p_list_access;
1901 msg_Dbg( s, "opening input `%s'", psz_name );
1903 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1905 if( !p_list_access ) return 0;
1907 if( p_sys->p_list_access != p_access )
1908 access_Delete( p_sys->p_list_access );
1910 p_sys->p_list_access = p_list_access;
1912 /* We have to read some data */
1913 return AReadStream( s, p_read, i_read_orig );
1916 /* Update read bytes in input */
1919 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1920 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
1921 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1922 (float)i_total, NULL );
1923 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1924 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1929 static block_t *AReadBlock( stream_t *s, bool *pb_eof )
1931 stream_sys_t *p_sys = s->p_sys;
1932 access_t *p_access = p_sys->p_access;
1933 input_thread_t *p_input = NULL;
1938 if( s->p_parent && s->p_parent->p_parent &&
1939 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1940 p_input = (input_thread_t *)s->p_parent->p_parent;
1942 if( !p_sys->i_list )
1944 p_block = p_access->pf_block( p_access );
1945 if( p_access->b_die )
1946 vlc_object_kill( s );
1947 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1948 if( p_input && p_block && libvlc_stats (p_access) )
1950 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1951 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1952 p_block->i_buffer, &i_total );
1953 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1954 (float)i_total, NULL );
1955 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1956 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1961 p_block = p_sys->p_list_access->pf_block( p_access );
1962 if( p_access->b_die )
1963 vlc_object_kill( s );
1964 b_eof = p_sys->p_list_access->info.b_eof;
1965 if( pb_eof ) *pb_eof = b_eof;
1967 /* If we reached an EOF then switch to the next stream in the list */
1968 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1970 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1971 access_t *p_list_access;
1973 msg_Dbg( s, "opening input `%s'", psz_name );
1975 p_list_access = access_New( s, p_access->psz_access, "", psz_name );
1977 if( !p_list_access ) return 0;
1979 if( p_sys->p_list_access != p_access )
1980 access_Delete( p_sys->p_list_access );
1982 p_sys->p_list_access = p_list_access;
1984 /* We have to read some data */
1985 return AReadBlock( s, pb_eof );
1991 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1992 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1993 p_block->i_buffer, &i_total );
1994 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1995 (float)i_total, NULL );
1996 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
1998 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
2004 static int ASeek( stream_t *s, int64_t i_pos )
2006 stream_sys_t *p_sys = s->p_sys;
2007 access_t *p_access = p_sys->p_access;
2009 /* Check which stream we need to access */
2015 access_t *p_list_access = 0;
2017 for( i = 0; i < p_sys->i_list - 1; i++ )
2019 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
2020 i_size += p_sys->list[i]->i_size;
2022 psz_name = p_sys->list[i]->psz_path;
2024 if( i != p_sys->i_list_index )
2025 msg_Dbg( s, "opening input `%s'", psz_name );
2027 if( i != p_sys->i_list_index && i != 0 )
2030 access_New( s, p_access->psz_access, "", psz_name );
2032 else if( i != p_sys->i_list_index )
2034 p_list_access = p_access;
2039 if( p_sys->p_list_access != p_access )
2040 access_Delete( p_sys->p_list_access );
2042 p_sys->p_list_access = p_list_access;
2045 p_sys->i_list_index = i;
2046 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
2050 return p_access->pf_seek( p_access, i_pos );
2055 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
2056 * "p_read" is NULL then data are skipped instead of read. The return
2057 * value is the real numbers of bytes read/skip. If this value is less
2058 * than i_read that means that it's the end of the stream.
2060 int stream_Read( stream_t *s, void *p_read, int i_read )
2062 return s->pf_read( s, p_read, i_read );
2066 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
2067 * \return The real numbers of valid bytes, if it's less
2068 * or equal to 0, *pp_peek is invalid.
2069 * \note pp_peek is a pointer to internal buffer and it will be invalid as
2070 * soons as other stream_* functions are called.
2071 * \note Due to input limitation, it could be less than i_peek without meaning
2072 * the end of the stream (but only when you have i_peek >=
2073 * p_input->i_bufsize)
2075 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
2077 return s->pf_peek( s, pp_peek, i_peek );
2081 * Use to control the "stream_t *". Look at #stream_query_e for
2082 * possible "i_query" value and format arguments. Return VLC_SUCCESS
2083 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
2085 int stream_vaControl( stream_t *s, int i_query, va_list args )
2087 return s->pf_control( s, i_query, args );
2093 void stream_Delete( stream_t *s )
2098 int stream_Control( stream_t *s, int i_query, ... )
2104 return VLC_EGENERIC;
2106 va_start( args, i_query );
2107 i_result = s->pf_control( s, i_query, args );
2113 * Read "i_size" bytes and store them in a block_t.
2114 * It always read i_size bytes unless you are at the end of the stream
2115 * where it return what is available.
2117 block_t *stream_Block( stream_t *s, int i_size )
2119 if( i_size <= 0 ) return NULL;
2121 /* emulate block read */
2122 block_t *p_bk = block_New( s, i_size );
2125 p_bk->i_buffer = stream_Read( s, p_bk->p_buffer, i_size );
2126 if( p_bk->i_buffer > 0 )
2131 if( p_bk ) block_Release( p_bk );