1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_strings.h>
33 #include <vlc_memory.h>
40 #include "input_internal.h"
42 // #define STREAM_DEBUG 1
45 * - tune the 2 methods (block/stream)
46 * - compute cost for seek
47 * - improve stream mode seeking with closest segments
53 * One linked list of data read
55 * More complex scheme using mutliple track to avoid seeking
56 * - using directly the access (only indirection for peeking).
57 * This method is known to introduce much less latency.
58 * It should probably defaulted (instead of the stream method (2)).
61 /* How many tracks we have, currently only used for stream mode */
62 #ifdef OPTIMIZE_MEMORY
63 # define STREAM_CACHE_TRACK 1
64 /* Max size of our cache 128Ko per track */
65 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
67 # define STREAM_CACHE_TRACK 3
68 /* Max size of our cache 4Mo per track */
69 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
72 /* How many data we try to prebuffer
73 * XXX it should be small to avoid useless latency but big enough for
74 * efficient demux probing */
75 #define STREAM_CACHE_PREBUFFER_SIZE (128)
77 /* Method1: Simple, for pf_block.
78 * We get blocks and put them in the linked list.
79 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
82 /* Method2: A bit more complex, for pf_read
83 * - We use ring buffers, only one if unseekable, all if seekable
84 * - Upon seek date current ring, then search if one ring match the pos,
85 * yes: switch to it, seek the access to match the end of the ring
86 * no: search the ring with i_end the closer to i_pos,
87 * if close enough, read data and use this ring
88 * else use the oldest ring, seek and use it.
90 * TODO: - with access non seekable: use all space available for only one ring, but
91 * we have to support seekable/non-seekable switch on the fly.
92 * - compute a good value for i_read_size
95 #define STREAM_READ_ATONCE 1024
96 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
120 } stream_read_method_t
;
126 stream_read_method_t method
; /* method to use */
128 uint64_t i_pos
; /* Current reading offset */
130 /* Method 1: pf_block */
133 uint64_t i_start
; /* Offset of block for p_first */
134 uint64_t i_offset
; /* Offset for data in p_current */
135 block_t
*p_current
; /* Current block */
137 uint64_t i_size
; /* Total amount of data in the list */
143 /* Method 2: for pf_read */
146 unsigned i_offset
; /* Buffer offset in the current track */
147 int i_tk
; /* Current track */
148 stream_track_t tk
[STREAM_CACHE_TRACK
];
154 unsigned i_used
; /* Used since last read */
155 unsigned i_read_size
;
159 /* Peek temporary buffer */
163 /* Stat for both method */
166 bool b_fastseek
; /* From access */
168 /* Stat about reading data */
169 uint64_t i_read_count
;
171 uint64_t i_read_time
;
173 /* Stat about seek */
174 unsigned i_seek_count
;
175 uint64_t i_seek_time
;
181 access_entry_t
**list
;
183 access_t
*p_list_access
;
187 static int AStreamReadBlock( stream_t
*s
, void *p_read
, unsigned int i_read
);
188 static int AStreamPeekBlock( stream_t
*s
, const uint8_t **p_peek
, unsigned int i_read
);
189 static int AStreamSeekBlock( stream_t
*s
, uint64_t i_pos
);
190 static void AStreamPrebufferBlock( stream_t
*s
);
191 static block_t
*AReadBlock( stream_t
*s
, bool *pb_eof
);
194 static int AStreamReadStream( stream_t
*s
, void *p_read
, unsigned int i_read
);
195 static int AStreamPeekStream( stream_t
*s
, const uint8_t **pp_peek
, unsigned int i_read
);
196 static int AStreamSeekStream( stream_t
*s
, uint64_t i_pos
);
197 static void AStreamPrebufferStream( stream_t
*s
);
198 static int AReadStream( stream_t
*s
, void *p_read
, unsigned int i_read
);
201 static int AStreamControl( stream_t
*s
, int i_query
, va_list );
202 static void AStreamDestroy( stream_t
*s
);
203 static int ASeek( stream_t
*s
, uint64_t i_pos
);
205 /****************************************************************************
206 * stream_CommonNew: create an empty stream structure
207 ****************************************************************************/
208 stream_t
*stream_CommonNew( vlc_object_t
*p_obj
)
210 stream_t
*s
= (stream_t
*)vlc_custom_create( p_obj
, sizeof(*s
), "stream" );
215 s
->p_text
= malloc( sizeof(*s
->p_text
) );
218 vlc_object_release( s
);
222 /* UTF16 and UTF32 text file conversion */
223 s
->p_text
->conv
= (vlc_iconv_t
)(-1);
224 s
->p_text
->i_char_width
= 1;
225 s
->p_text
->b_little_endian
= false;
230 void stream_CommonDelete( stream_t
*s
)
234 if( s
->p_text
->conv
!= (vlc_iconv_t
)(-1) )
235 vlc_iconv_close( s
->p_text
->conv
);
238 free( s
->psz_access
);
240 vlc_object_release( s
);
244 /****************************************************************************
245 * stream_UrlNew: create a stream from a access
246 ****************************************************************************/
247 stream_t
*stream_UrlNew( vlc_object_t
*p_parent
, const char *psz_url
)
249 const char *psz_access
, *psz_demux
, *psz_path
, *psz_anchor
;
255 char psz_dup
[strlen( psz_url
) + 1];
256 strcpy( psz_dup
, psz_url
);
257 input_SplitMRL( &psz_access
, &psz_demux
, &psz_path
, &psz_anchor
, psz_dup
);
259 /* Now try a real access */
260 p_access
= access_New( p_parent
, NULL
, psz_access
, psz_demux
, psz_path
);
261 if( p_access
== NULL
)
263 msg_Err( p_parent
, "no suitable access module for `%s'", psz_url
);
267 return stream_AccessNew( p_access
, NULL
);
270 stream_t
*stream_AccessNew( access_t
*p_access
, char **ppsz_list
)
272 stream_t
*s
= stream_CommonNew( VLC_OBJECT(p_access
) );
278 s
->p_input
= p_access
->p_input
;
279 s
->psz_access
= strdup( p_access
->psz_access
);
280 s
->psz_path
= strdup( p_access
->psz_location
);
281 s
->p_sys
= p_sys
= malloc( sizeof( *p_sys
) );
282 if( !s
->psz_access
|| !s
->psz_path
|| !s
->p_sys
)
284 stream_CommonDelete( s
);
288 s
->pf_read
= NULL
; /* Set up later */
290 s
->pf_control
= AStreamControl
;
291 s
->pf_destroy
= AStreamDestroy
;
294 p_sys
->p_access
= p_access
;
295 if( p_access
->pf_block
)
296 p_sys
->method
= STREAM_METHOD_BLOCK
;
298 p_sys
->method
= STREAM_METHOD_STREAM
;
300 p_sys
->i_pos
= p_access
->info
.i_pos
;
303 access_Control( p_access
, ACCESS_CAN_FASTSEEK
, &p_sys
->stat
.b_fastseek
);
304 p_sys
->stat
.i_bytes
= 0;
305 p_sys
->stat
.i_read_time
= 0;
306 p_sys
->stat
.i_read_count
= 0;
307 p_sys
->stat
.i_seek_count
= 0;
308 p_sys
->stat
.i_seek_time
= 0;
310 TAB_INIT( p_sys
->i_list
, p_sys
->list
);
311 p_sys
->i_list_index
= 0;
312 p_sys
->p_list_access
= NULL
;
314 /* Get the additional list of inputs if any (for concatenation) */
315 if( ppsz_list
&& ppsz_list
[0] )
317 access_entry_t
*p_entry
= malloc( sizeof(*p_entry
) );
321 p_entry
->i_size
= p_access
->info
.i_size
;
322 p_entry
->psz_path
= strdup( p_access
->psz_location
);
323 if( !p_entry
->psz_path
)
328 p_sys
->p_list_access
= p_access
;
329 TAB_APPEND( p_sys
->i_list
, p_sys
->list
, p_entry
);
330 msg_Dbg( p_access
, "adding file `%s', (%"PRId64
" bytes)",
331 p_entry
->psz_path
, p_access
->info
.i_size
);
333 for( int i
= 0; ppsz_list
[i
] != NULL
; i
++ )
335 char *psz_name
= strdup( ppsz_list
[i
] );
340 access_t
*p_tmp
= access_New( p_access
, p_access
->p_input
,
341 p_access
->psz_access
, "", psz_name
);
345 msg_Dbg( p_access
, "adding file `%s', (%"PRId64
" bytes)",
346 psz_name
, p_tmp
->info
.i_size
);
348 p_entry
= malloc( sizeof(*p_entry
) );
351 p_entry
->i_size
= p_tmp
->info
.i_size
;
352 p_entry
->psz_path
= psz_name
;
353 TAB_APPEND( p_sys
->i_list
, p_sys
->list
, p_entry
);
355 access_Delete( p_tmp
);
361 p_sys
->p_peek
= NULL
;
363 if( p_sys
->method
== STREAM_METHOD_BLOCK
)
365 msg_Dbg( s
, "Using block method for AStream*" );
366 s
->pf_read
= AStreamReadBlock
;
367 s
->pf_peek
= AStreamPeekBlock
;
369 /* Init all fields of p_sys->block */
370 p_sys
->block
.i_start
= p_sys
->i_pos
;
371 p_sys
->block
.i_offset
= 0;
372 p_sys
->block
.p_current
= NULL
;
373 p_sys
->block
.i_size
= 0;
374 p_sys
->block
.p_first
= NULL
;
375 p_sys
->block
.pp_last
= &p_sys
->block
.p_first
;
377 /* Do the prebuffering */
378 AStreamPrebufferBlock( s
);
380 if( p_sys
->block
.i_size
<= 0 )
382 msg_Err( s
, "cannot pre fill buffer" );
390 assert( p_sys
->method
== STREAM_METHOD_STREAM
);
392 msg_Dbg( s
, "Using stream method for AStream*" );
394 s
->pf_read
= AStreamReadStream
;
395 s
->pf_peek
= AStreamPeekStream
;
397 /* Allocate/Setup our tracks */
398 p_sys
->stream
.i_offset
= 0;
399 p_sys
->stream
.i_tk
= 0;
400 p_sys
->stream
.p_buffer
= malloc( STREAM_CACHE_SIZE
);
401 if( p_sys
->stream
.p_buffer
== NULL
)
403 p_sys
->stream
.i_used
= 0;
404 p_sys
->stream
.i_read_size
= STREAM_READ_ATONCE
;
405 #if STREAM_READ_ATONCE < 256
406 # error "Invalid STREAM_READ_ATONCE value"
409 for( i
= 0; i
< STREAM_CACHE_TRACK
; i
++ )
411 p_sys
->stream
.tk
[i
].i_date
= 0;
412 p_sys
->stream
.tk
[i
].i_start
= p_sys
->i_pos
;
413 p_sys
->stream
.tk
[i
].i_end
= p_sys
->i_pos
;
414 p_sys
->stream
.tk
[i
].p_buffer
=
415 &p_sys
->stream
.p_buffer
[i
* STREAM_CACHE_TRACK_SIZE
];
418 /* Do the prebuffering */
419 AStreamPrebufferStream( s
);
421 if( p_sys
->stream
.tk
[p_sys
->stream
.i_tk
].i_end
<= 0 )
423 msg_Err( s
, "cannot pre fill buffer" );
431 if( p_sys
->method
== STREAM_METHOD_BLOCK
)
437 free( p_sys
->stream
.p_buffer
);
439 while( p_sys
->i_list
> 0 )
440 free( p_sys
->list
[--(p_sys
->i_list
)] );
443 stream_CommonDelete( s
);
444 access_Delete( p_access
);
448 /****************************************************************************
450 ****************************************************************************/
451 static void AStreamDestroy( stream_t
*s
)
453 stream_sys_t
*p_sys
= s
->p_sys
;
455 if( p_sys
->method
== STREAM_METHOD_BLOCK
)
456 block_ChainRelease( p_sys
->block
.p_first
);
458 free( p_sys
->stream
.p_buffer
);
460 free( p_sys
->p_peek
);
462 if( p_sys
->p_list_access
&& p_sys
->p_list_access
!= p_sys
->p_access
)
463 access_Delete( p_sys
->p_list_access
);
465 while( p_sys
->i_list
-- )
467 free( p_sys
->list
[p_sys
->i_list
]->psz_path
);
468 free( p_sys
->list
[p_sys
->i_list
] );
472 stream_CommonDelete( s
);
473 access_Delete( p_sys
->p_access
);
477 /****************************************************************************
478 * AStreamControlReset:
479 ****************************************************************************/
480 static void AStreamControlReset( stream_t
*s
)
482 stream_sys_t
*p_sys
= s
->p_sys
;
484 p_sys
->i_pos
= p_sys
->p_access
->info
.i_pos
;
486 if( p_sys
->method
== STREAM_METHOD_BLOCK
)
488 block_ChainRelease( p_sys
->block
.p_first
);
490 /* Init all fields of p_sys->block */
491 p_sys
->block
.i_start
= p_sys
->i_pos
;
492 p_sys
->block
.i_offset
= 0;
493 p_sys
->block
.p_current
= NULL
;
494 p_sys
->block
.i_size
= 0;
495 p_sys
->block
.p_first
= NULL
;
496 p_sys
->block
.pp_last
= &p_sys
->block
.p_first
;
498 /* Do the prebuffering */
499 AStreamPrebufferBlock( s
);
505 assert( p_sys
->method
== STREAM_METHOD_STREAM
);
507 /* Setup our tracks */
508 p_sys
->stream
.i_offset
= 0;
509 p_sys
->stream
.i_tk
= 0;
510 p_sys
->stream
.i_used
= 0;
512 for( i
= 0; i
< STREAM_CACHE_TRACK
; i
++ )
514 p_sys
->stream
.tk
[i
].i_date
= 0;
515 p_sys
->stream
.tk
[i
].i_start
= p_sys
->i_pos
;
516 p_sys
->stream
.tk
[i
].i_end
= p_sys
->i_pos
;
519 /* Do the prebuffering */
520 AStreamPrebufferStream( s
);
524 /****************************************************************************
525 * AStreamControlUpdate:
526 ****************************************************************************/
527 static void AStreamControlUpdate( stream_t
*s
)
529 stream_sys_t
*p_sys
= s
->p_sys
;
531 p_sys
->i_pos
= p_sys
->p_access
->info
.i_pos
;
536 for( i
= 0; i
< p_sys
->i_list_index
; i
++ )
538 p_sys
->i_pos
+= p_sys
->list
[i
]->i_size
;
543 /****************************************************************************
545 ****************************************************************************/
546 static int AStreamControl( stream_t
*s
, int i_query
, va_list args
)
548 stream_sys_t
*p_sys
= s
->p_sys
;
549 access_t
*p_access
= p_sys
->p_access
;
551 uint64_t *pi_64
, i_64
;
555 case STREAM_GET_SIZE
:
556 pi_64
= va_arg( args
, uint64_t * );
557 if( s
->p_sys
->i_list
)
561 for( i
= 0; i
< s
->p_sys
->i_list
; i
++ )
562 *pi_64
+= s
->p_sys
->list
[i
]->i_size
;
565 *pi_64
= p_access
->info
.i_size
;
568 case STREAM_CAN_SEEK
:
569 return access_vaControl( p_access
, ACCESS_CAN_SEEK
, args
);
570 case STREAM_CAN_FASTSEEK
:
571 return access_vaControl( p_access
, ACCESS_CAN_FASTSEEK
, args
);
572 case STREAM_CAN_PAUSE
:
573 return access_vaControl( p_access
, ACCESS_CAN_PAUSE
, args
);
574 case STREAM_CAN_CONTROL_PACE
:
575 return access_vaControl( p_access
, ACCESS_CAN_CONTROL_PACE
, args
);
577 case STREAM_GET_POSITION
:
578 pi_64
= va_arg( args
, uint64_t * );
579 *pi_64
= p_sys
->i_pos
;
582 case STREAM_SET_POSITION
:
583 i_64
= va_arg( args
, uint64_t );
584 switch( p_sys
->method
)
586 case STREAM_METHOD_BLOCK
:
587 return AStreamSeekBlock( s
, i_64
);
588 case STREAM_METHOD_STREAM
:
589 return AStreamSeekStream( s
, i_64
);
595 case STREAM_CONTROL_ACCESS
:
597 int i_int
= (int) va_arg( args
, int );
598 if( i_int
!= ACCESS_SET_PRIVATE_ID_STATE
&&
599 i_int
!= ACCESS_SET_PRIVATE_ID_CA
&&
600 i_int
!= ACCESS_GET_PRIVATE_ID_STATE
)
602 msg_Err( s
, "Hey, what are you thinking ?"
603 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
606 return access_vaControl( p_access
, i_int
, args
);
609 case STREAM_UPDATE_SIZE
:
610 AStreamControlUpdate( s
);
613 case STREAM_GET_TITLE_INFO
:
614 return access_vaControl( p_access
, ACCESS_GET_TITLE_INFO
, args
);
615 case STREAM_GET_META
:
616 return access_vaControl( p_access
, ACCESS_GET_META
, args
);
617 case STREAM_GET_CONTENT_TYPE
:
618 return access_vaControl( p_access
, ACCESS_GET_CONTENT_TYPE
, args
);
619 case STREAM_GET_SIGNAL
:
620 return access_vaControl( p_access
, ACCESS_GET_SIGNAL
, args
);
622 case STREAM_SET_PAUSE_STATE
:
623 return access_vaControl( p_access
, ACCESS_SET_PAUSE_STATE
, args
);
624 case STREAM_SET_TITLE
:
626 int ret
= access_vaControl( p_access
, ACCESS_SET_TITLE
, args
);
627 if( ret
== VLC_SUCCESS
)
628 AStreamControlReset( s
);
631 case STREAM_SET_SEEKPOINT
:
633 int ret
= access_vaControl( p_access
, ACCESS_SET_SEEKPOINT
, args
);
634 if( ret
== VLC_SUCCESS
)
635 AStreamControlReset( s
);
639 case STREAM_SET_RECORD_STATE
:
641 msg_Err( s
, "invalid stream_vaControl query=0x%x", i_query
);
647 /****************************************************************************
649 ****************************************************************************/
650 static void AStreamPrebufferBlock( stream_t
*s
)
652 stream_sys_t
*p_sys
= s
->p_sys
;
657 msg_Dbg( s
, "starting pre-buffering" );
661 const int64_t i_date
= mdate();
665 if( !vlc_object_alive(s
) || p_sys
->block
.i_size
> STREAM_CACHE_PREBUFFER_SIZE
)
670 p_sys
->stat
.i_bytes
= p_sys
->block
.i_size
;
671 p_sys
->stat
.i_read_time
= i_date
- i_start
;
672 i_byterate
= ( INT64_C(1000000) * p_sys
->stat
.i_bytes
) /
673 (p_sys
->stat
.i_read_time
+ 1);
675 msg_Dbg( s
, "prebuffering done %"PRId64
" bytes in %"PRId64
"s - "
678 p_sys
->stat
.i_read_time
/ INT64_C(1000000),
684 if( ( b
= AReadBlock( s
, &b_eof
) ) == NULL
)
693 /* Append the block */
694 p_sys
->block
.i_size
+= b
->i_buffer
;
695 *p_sys
->block
.pp_last
= b
;
696 p_sys
->block
.pp_last
= &b
->p_next
;
698 p_sys
->stat
.i_read_count
++;
705 msg_Dbg( s
, "received first data after %d ms",
706 (int)((i_first
-i_start
)/1000) );
710 p_sys
->block
.p_current
= p_sys
->block
.p_first
;
713 static int AStreamRefillBlock( stream_t
*s
);
715 static int AStreamReadBlock( stream_t
*s
, void *p_read
, unsigned int i_read
)
717 stream_sys_t
*p_sys
= s
->p_sys
;
719 uint8_t *p_data
= p_read
;
720 unsigned int i_data
= 0;
723 if( p_sys
->block
.p_current
== NULL
)
728 /* seek within this stream if possible, else use plain old read and discard */
729 stream_sys_t
*p_sys
= s
->p_sys
;
730 access_t
*p_access
= p_sys
->p_access
;
732 access_Control( p_access
, ACCESS_CAN_SEEK
, &b_aseek
);
734 return AStreamSeekBlock( s
, p_sys
->i_pos
+ i_read
) ? 0 : i_read
;
737 while( i_data
< i_read
)
740 p_sys
->block
.p_current
->i_buffer
- p_sys
->block
.i_offset
;
741 unsigned int i_copy
= VLC_CLIP( (unsigned int)i_current
, 0, i_read
- i_data
);
747 &p_sys
->block
.p_current
->p_buffer
[p_sys
->block
.i_offset
],
753 p_sys
->block
.i_offset
+= i_copy
;
754 if( p_sys
->block
.i_offset
>= p_sys
->block
.p_current
->i_buffer
)
756 /* Current block is now empty, switch to next */
757 if( p_sys
->block
.p_current
)
759 p_sys
->block
.i_offset
= 0;
760 p_sys
->block
.p_current
= p_sys
->block
.p_current
->p_next
;
762 /*Get a new block if needed */
763 if( !p_sys
->block
.p_current
&& AStreamRefillBlock( s
) )
770 p_sys
->i_pos
+= i_data
;
774 static int AStreamPeekBlock( stream_t
*s
, const uint8_t **pp_peek
, unsigned int i_read
)
776 stream_sys_t
*p_sys
= s
->p_sys
;
778 unsigned int i_data
= 0;
780 unsigned int i_offset
;
782 if( p_sys
->block
.p_current
== NULL
) return 0; /* EOF */
784 /* We can directly give a pointer over our buffer */
785 if( i_read
<= p_sys
->block
.p_current
->i_buffer
- p_sys
->block
.i_offset
)
787 *pp_peek
= &p_sys
->block
.p_current
->p_buffer
[p_sys
->block
.i_offset
];
791 /* We need to create a local copy */
792 if( p_sys
->i_peek
< i_read
)
794 p_sys
->p_peek
= realloc_or_free( p_sys
->p_peek
, i_read
);
800 p_sys
->i_peek
= i_read
;
803 /* Fill enough data */
804 while( p_sys
->block
.i_size
- (p_sys
->i_pos
- p_sys
->block
.i_start
)
807 block_t
**pp_last
= p_sys
->block
.pp_last
;
809 if( AStreamRefillBlock( s
) ) break;
811 /* Our buffer are probably filled enough, don't try anymore */
812 if( pp_last
== p_sys
->block
.pp_last
) break;
815 /* Copy what we have */
816 b
= p_sys
->block
.p_current
;
817 i_offset
= p_sys
->block
.i_offset
;
818 p_data
= p_sys
->p_peek
;
820 while( b
&& i_data
< i_read
)
822 unsigned int i_current
= __MAX(b
->i_buffer
- i_offset
,0);
823 int i_copy
= __MIN( i_current
, i_read
- i_data
);
825 memcpy( p_data
, &b
->p_buffer
[i_offset
], i_copy
);
830 if( i_offset
>= b
->i_buffer
)
837 *pp_peek
= p_sys
->p_peek
;
841 static int AStreamSeekBlock( stream_t
*s
, uint64_t i_pos
)
843 stream_sys_t
*p_sys
= s
->p_sys
;
844 access_t
*p_access
= p_sys
->p_access
;
845 int64_t i_offset
= i_pos
- p_sys
->block
.i_start
;
848 /* We already have thoses data, just update p_current/i_offset */
849 if( i_offset
>= 0 && (uint64_t)i_offset
< p_sys
->block
.i_size
)
851 block_t
*b
= p_sys
->block
.p_first
;
854 while( i_current
+ b
->i_buffer
< (uint64_t)i_offset
)
856 i_current
+= b
->i_buffer
;
860 p_sys
->block
.p_current
= b
;
861 p_sys
->block
.i_offset
= i_offset
- i_current
;
863 p_sys
->i_pos
= i_pos
;
868 /* We may need to seek or to read data */
872 access_Control( p_access
, ACCESS_CAN_SEEK
, &b_aseek
);
876 msg_Err( s
, "backward seeking impossible (access not seekable)" );
884 bool b_aseek
, b_aseekfast
;
886 access_Control( p_access
, ACCESS_CAN_SEEK
, &b_aseek
);
887 access_Control( p_access
, ACCESS_CAN_FASTSEEK
, &b_aseekfast
);
892 msg_Warn( s
, "%"PRId64
" bytes need to be skipped "
893 "(access non seekable)",
894 i_offset
- p_sys
->block
.i_size
);
898 int64_t i_skip
= i_offset
- p_sys
->block
.i_size
;
900 /* Avg bytes per packets */
901 int i_avg
= p_sys
->stat
.i_bytes
/ p_sys
->stat
.i_read_count
;
902 /* TODO compute a seek cost instead of fixed threshold */
903 int i_th
= b_aseekfast
? 1 : 5;
905 if( i_skip
<= i_th
* i_avg
&&
906 i_skip
< STREAM_CACHE_SIZE
)
911 msg_Dbg( s
, "b_seek=%d th*avg=%d skip=%"PRId64
,
912 b_seek
, i_th
*i_avg
, i_skip
);
918 int64_t i_start
, i_end
;
919 /* Do the access seek */
921 if( ASeek( s
, i_pos
) ) return VLC_EGENERIC
;
925 block_ChainRelease( p_sys
->block
.p_first
);
928 p_sys
->block
.i_start
= p_sys
->i_pos
= i_pos
;
929 p_sys
->block
.i_offset
= 0;
930 p_sys
->block
.p_current
= NULL
;
931 p_sys
->block
.i_size
= 0;
932 p_sys
->block
.p_first
= NULL
;
933 p_sys
->block
.pp_last
= &p_sys
->block
.p_first
;
936 if( AStreamRefillBlock( s
) )
940 p_sys
->stat
.i_seek_time
+= i_end
- i_start
;
941 p_sys
->stat
.i_seek_count
++;
948 while( p_sys
->block
.p_current
&&
949 p_sys
->i_pos
+ p_sys
->block
.p_current
->i_buffer
- p_sys
->block
.i_offset
<= i_pos
)
951 p_sys
->i_pos
+= p_sys
->block
.p_current
->i_buffer
- p_sys
->block
.i_offset
;
952 p_sys
->block
.p_current
= p_sys
->block
.p_current
->p_next
;
953 p_sys
->block
.i_offset
= 0;
955 if( !p_sys
->block
.p_current
&& AStreamRefillBlock( s
) )
957 if( p_sys
->i_pos
!= i_pos
)
961 while( p_sys
->block
.i_start
+ p_sys
->block
.i_size
< i_pos
);
963 p_sys
->block
.i_offset
+= i_pos
- p_sys
->i_pos
;
964 p_sys
->i_pos
= i_pos
;
972 static int AStreamRefillBlock( stream_t
*s
)
974 stream_sys_t
*p_sys
= s
->p_sys
;
978 while( p_sys
->block
.i_size
>= STREAM_CACHE_SIZE
&&
979 p_sys
->block
.p_first
!= p_sys
->block
.p_current
)
981 block_t
*b
= p_sys
->block
.p_first
;
983 p_sys
->block
.i_start
+= b
->i_buffer
;
984 p_sys
->block
.i_size
-= b
->i_buffer
;
985 p_sys
->block
.p_first
= b
->p_next
;
989 if( p_sys
->block
.i_size
>= STREAM_CACHE_SIZE
&&
990 p_sys
->block
.p_current
== p_sys
->block
.p_first
&&
991 p_sys
->block
.p_current
->p_next
) /* At least 2 packets */
993 /* Enough data, don't read more */
997 /* Now read a new block */
998 const int64_t i_start
= mdate();
1003 if( !vlc_object_alive(s
) )
1004 return VLC_EGENERIC
;
1007 if( ( b
= AReadBlock( s
, &b_eof
) ) )
1010 return VLC_EGENERIC
;
1013 p_sys
->stat
.i_read_time
+= mdate() - i_start
;
1016 /* Append the block */
1017 p_sys
->block
.i_size
+= b
->i_buffer
;
1018 *p_sys
->block
.pp_last
= b
;
1019 p_sys
->block
.pp_last
= &b
->p_next
;
1022 if( p_sys
->block
.p_current
== NULL
)
1023 p_sys
->block
.p_current
= b
;
1026 p_sys
->stat
.i_bytes
+= b
->i_buffer
;
1027 p_sys
->stat
.i_read_count
++;
1035 /****************************************************************************
1037 ****************************************************************************/
1038 static int AStreamRefillStream( stream_t
*s
);
1039 static int AStreamReadNoSeekStream( stream_t
*s
, void *p_read
, unsigned int i_read
);
1041 static int AStreamReadStream( stream_t
*s
, void *p_read
, unsigned int i_read
)
1043 stream_sys_t
*p_sys
= s
->p_sys
;
1047 const uint64_t i_pos_wanted
= p_sys
->i_pos
+ i_read
;
1049 if( AStreamSeekStream( s
, i_pos_wanted
) )
1051 if( p_sys
->i_pos
!= i_pos_wanted
)
1056 return AStreamReadNoSeekStream( s
, p_read
, i_read
);
1059 static int AStreamPeekStream( stream_t
*s
, const uint8_t **pp_peek
, unsigned int i_read
)
1061 stream_sys_t
*p_sys
= s
->p_sys
;
1062 stream_track_t
*tk
= &p_sys
->stream
.tk
[p_sys
->stream
.i_tk
];
1065 if( tk
->i_start
>= tk
->i_end
) return 0; /* EOF */
1068 msg_Dbg( s
, "AStreamPeekStream: %d pos=%"PRId64
" tk=%d "
1069 "start=%"PRId64
" offset=%d end=%"PRId64
,
1070 i_read
, p_sys
->i_pos
, p_sys
->stream
.i_tk
,
1071 tk
->i_start
, p_sys
->stream
.i_offset
, tk
->i_end
);
1074 /* Avoid problem, but that should *never* happen */
1075 if( i_read
> STREAM_CACHE_TRACK_SIZE
/ 2 )
1076 i_read
= STREAM_CACHE_TRACK_SIZE
/ 2;
1078 while( tk
->i_end
< tk
->i_start
+ p_sys
->stream
.i_offset
+ i_read
)
1080 if( p_sys
->stream
.i_used
<= 1 )
1082 /* Be sure we will read something */
1083 p_sys
->stream
.i_used
+= tk
->i_start
+ p_sys
->stream
.i_offset
+ i_read
- tk
->i_end
;
1085 if( AStreamRefillStream( s
) ) break;
1088 if( tk
->i_end
< tk
->i_start
+ p_sys
->stream
.i_offset
+ i_read
)
1090 i_read
= tk
->i_end
- tk
->i_start
- p_sys
->stream
.i_offset
;
1094 /* Now, direct pointer or a copy ? */
1095 i_off
= (tk
->i_start
+ p_sys
->stream
.i_offset
) % STREAM_CACHE_TRACK_SIZE
;
1096 if( i_off
+ i_read
<= STREAM_CACHE_TRACK_SIZE
)
1098 *pp_peek
= &tk
->p_buffer
[i_off
];
1102 if( p_sys
->i_peek
< i_read
)
1104 p_sys
->p_peek
= realloc_or_free( p_sys
->p_peek
, i_read
);
1105 if( !p_sys
->p_peek
)
1110 p_sys
->i_peek
= i_read
;
1113 memcpy( p_sys
->p_peek
, &tk
->p_buffer
[i_off
],
1114 STREAM_CACHE_TRACK_SIZE
- i_off
);
1115 memcpy( &p_sys
->p_peek
[STREAM_CACHE_TRACK_SIZE
- i_off
],
1116 &tk
->p_buffer
[0], i_read
- (STREAM_CACHE_TRACK_SIZE
- i_off
) );
1118 *pp_peek
= p_sys
->p_peek
;
1122 static int AStreamSeekStream( stream_t
*s
, uint64_t i_pos
)
1124 stream_sys_t
*p_sys
= s
->p_sys
;
1126 stream_track_t
*p_current
= &p_sys
->stream
.tk
[p_sys
->stream
.i_tk
];
1127 access_t
*p_access
= p_sys
->p_access
;
1129 if( p_current
->i_start
>= p_current
->i_end
&& i_pos
>= p_current
->i_end
)
1133 msg_Dbg( s
, "AStreamSeekStream: to %"PRId64
" pos=%"PRId64
1134 " tk=%d start=%"PRId64
" offset=%d end=%"PRId64
,
1135 i_pos
, p_sys
->i_pos
, p_sys
->stream
.i_tk
,
1137 p_sys
->stream
.i_offset
,
1142 access_Control( p_access
, ACCESS_CAN_SEEK
, &b_aseek
);
1143 if( !b_aseek
&& i_pos
< p_current
->i_start
)
1145 msg_Warn( s
, "AStreamSeekStream: can't seek" );
1146 return VLC_EGENERIC
;
1150 access_Control( p_access
, ACCESS_CAN_FASTSEEK
, &b_afastseek
);
1152 /* FIXME compute seek cost (instead of static 'stupid' value) */
1153 uint64_t i_skip_threshold
;
1155 i_skip_threshold
= b_afastseek
? 128 : 3*p_sys
->stream
.i_read_size
;
1157 i_skip_threshold
= INT64_MAX
;
1159 /* Date the current track */
1160 p_current
->i_date
= mdate();
1162 /* Search a new track slot */
1163 stream_track_t
*tk
= NULL
;
1166 /* Prefer the current track */
1167 if( p_current
->i_start
<= i_pos
&& i_pos
<= p_current
->i_end
+ i_skip_threshold
)
1170 i_tk_idx
= p_sys
->stream
.i_tk
;
1174 /* Try to maximize already read data */
1175 for( int i
= 0; i
< STREAM_CACHE_TRACK
; i
++ )
1177 stream_track_t
*t
= &p_sys
->stream
.tk
[i
];
1179 if( t
->i_start
> i_pos
|| i_pos
> t
->i_end
)
1182 if( !tk
|| tk
->i_end
< t
->i_end
)
1191 /* Use the oldest unused */
1192 for( int i
= 0; i
< STREAM_CACHE_TRACK
; i
++ )
1194 stream_track_t
*t
= &p_sys
->stream
.tk
[i
];
1196 if( !tk
|| tk
->i_date
> t
->i_date
)
1203 assert( i_tk_idx
>= 0 && i_tk_idx
< STREAM_CACHE_TRACK
);
1205 if( tk
!= p_current
)
1206 i_skip_threshold
= 0;
1207 if( tk
->i_start
<= i_pos
&& i_pos
<= tk
->i_end
+ i_skip_threshold
)
1210 msg_Err( s
, "AStreamSeekStream: reusing %d start=%"PRId64
1211 " end=%"PRId64
"(%s)",
1212 i_tk_idx
, tk
->i_start
, tk
->i_end
,
1213 tk
!= p_current
? "seek" : i_pos
> tk
->i_end
? "skip" : "noseek" );
1215 if( tk
!= p_current
)
1219 /* Seek at the end of the buffer
1220 * TODO it is stupid to seek now, it would be better to delay it
1222 if( ASeek( s
, tk
->i_end
) )
1223 return VLC_EGENERIC
;
1225 else if( i_pos
> tk
->i_end
)
1227 uint64_t i_skip
= i_pos
- tk
->i_end
;
1230 const int i_read_max
= __MIN( 10 * STREAM_READ_ATONCE
, i_skip
);
1231 if( AStreamReadNoSeekStream( s
, NULL
, i_read_max
) != i_read_max
)
1232 return VLC_EGENERIC
;
1233 i_skip
-= i_read_max
;
1240 msg_Err( s
, "AStreamSeekStream: hard seek" );
1242 /* Nothing good, seek and choose oldest segment */
1243 if( ASeek( s
, i_pos
) )
1244 return VLC_EGENERIC
;
1246 tk
->i_start
= i_pos
;
1249 p_sys
->stream
.i_offset
= i_pos
- tk
->i_start
;
1250 p_sys
->stream
.i_tk
= i_tk_idx
;
1251 p_sys
->i_pos
= i_pos
;
1253 /* If there is not enough data left in the track, refill */
1254 /* TODO How to get a correct value for
1255 * - refilling threshold
1256 * - how much to refill
1258 if( tk
->i_end
< tk
->i_start
+ p_sys
->stream
.i_offset
+ p_sys
->stream
.i_read_size
)
1260 if( p_sys
->stream
.i_used
< STREAM_READ_ATONCE
/ 2 )
1261 p_sys
->stream
.i_used
= STREAM_READ_ATONCE
/ 2;
1263 if( AStreamRefillStream( s
) && i_pos
>= tk
->i_end
)
1264 return VLC_EGENERIC
;
1269 static int AStreamReadNoSeekStream( stream_t
*s
, void *p_read
, unsigned int i_read
)
1271 stream_sys_t
*p_sys
= s
->p_sys
;
1272 stream_track_t
*tk
= &p_sys
->stream
.tk
[p_sys
->stream
.i_tk
];
1274 uint8_t *p_data
= (uint8_t *)p_read
;
1275 unsigned int i_data
= 0;
1277 if( tk
->i_start
>= tk
->i_end
)
1281 msg_Dbg( s
, "AStreamReadStream: %d pos=%"PRId64
" tk=%d start=%"PRId64
1282 " offset=%d end=%"PRId64
,
1283 i_read
, p_sys
->i_pos
, p_sys
->stream
.i_tk
,
1284 tk
->i_start
, p_sys
->stream
.i_offset
, tk
->i_end
);
1287 while( i_data
< i_read
)
1289 unsigned i_off
= (tk
->i_start
+ p_sys
->stream
.i_offset
) % STREAM_CACHE_TRACK_SIZE
;
1290 unsigned int i_current
=
1291 __MIN( tk
->i_end
- tk
->i_start
- p_sys
->stream
.i_offset
,
1292 STREAM_CACHE_TRACK_SIZE
- i_off
);
1293 int i_copy
= __MIN( i_current
, i_read
- i_data
);
1295 if( i_copy
<= 0 ) break; /* EOF */
1298 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1301 memcpy( p_data
, &tk
->p_buffer
[i_off
], i_copy
);
1305 p_sys
->stream
.i_offset
+= i_copy
;
1307 /* Update pos now */
1308 p_sys
->i_pos
+= i_copy
;
1311 p_sys
->stream
.i_used
+= i_copy
;
1313 if( tk
->i_end
+ i_data
<= tk
->i_start
+ p_sys
->stream
.i_offset
+ i_read
)
1315 const unsigned i_read_requested
= VLC_CLIP( i_read
- i_data
,
1316 STREAM_READ_ATONCE
/ 2,
1317 STREAM_READ_ATONCE
* 10 );
1319 if( p_sys
->stream
.i_used
< i_read_requested
)
1320 p_sys
->stream
.i_used
= i_read_requested
;
1322 if( AStreamRefillStream( s
) )
1325 if( tk
->i_start
>= tk
->i_end
) break;
1334 static int AStreamRefillStream( stream_t
*s
)
1336 stream_sys_t
*p_sys
= s
->p_sys
;
1337 stream_track_t
*tk
= &p_sys
->stream
.tk
[p_sys
->stream
.i_tk
];
1339 /* We read but won't increase i_start after initial start + offset */
1341 __MIN( p_sys
->stream
.i_used
, STREAM_CACHE_TRACK_SIZE
-
1342 (tk
->i_end
- tk
->i_start
- p_sys
->stream
.i_offset
) );
1343 bool b_read
= false;
1344 int64_t i_start
, i_stop
;
1346 if( i_toread
<= 0 ) return VLC_EGENERIC
; /* EOF */
1349 msg_Dbg( s
, "AStreamRefillStream: used=%d toread=%d",
1350 p_sys
->stream
.i_used
, i_toread
);
1354 while( i_toread
> 0 )
1356 int i_off
= tk
->i_end
% STREAM_CACHE_TRACK_SIZE
;
1359 if( !vlc_object_alive(s
) )
1360 return VLC_EGENERIC
;
1362 i_read
= __MIN( i_toread
, STREAM_CACHE_TRACK_SIZE
- i_off
);
1363 i_read
= AReadStream( s
, &tk
->p_buffer
[i_off
], i_read
);
1365 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1370 else if( i_read
== 0 )
1373 return VLC_EGENERIC
;
1379 tk
->i_end
+= i_read
;
1381 /* Windows of STREAM_CACHE_TRACK_SIZE */
1382 if( tk
->i_start
+ STREAM_CACHE_TRACK_SIZE
< tk
->i_end
)
1384 unsigned i_invalid
= tk
->i_end
- tk
->i_start
- STREAM_CACHE_TRACK_SIZE
;
1386 tk
->i_start
+= i_invalid
;
1387 p_sys
->stream
.i_offset
-= i_invalid
;
1391 p_sys
->stream
.i_used
-= i_read
;
1393 p_sys
->stat
.i_bytes
+= i_read
;
1394 p_sys
->stat
.i_read_count
++;
1398 p_sys
->stat
.i_read_time
+= i_stop
- i_start
;
1403 static void AStreamPrebufferStream( stream_t
*s
)
1405 stream_sys_t
*p_sys
= s
->p_sys
;
1407 int64_t i_first
= 0;
1410 msg_Dbg( s
, "starting pre-buffering" );
1414 stream_track_t
*tk
= &p_sys
->stream
.tk
[p_sys
->stream
.i_tk
];
1416 int64_t i_date
= mdate();
1418 int i_buffered
= tk
->i_end
- tk
->i_start
;
1420 if( !vlc_object_alive(s
) || i_buffered
>= STREAM_CACHE_PREBUFFER_SIZE
)
1425 p_sys
->stat
.i_bytes
= i_buffered
;
1426 p_sys
->stat
.i_read_time
= i_date
- i_start
;
1427 i_byterate
= ( INT64_C(1000000) * p_sys
->stat
.i_bytes
) /
1428 (p_sys
->stat
.i_read_time
+1);
1430 msg_Dbg( s
, "pre-buffering done %"PRId64
" bytes in %"PRId64
"s - "
1432 p_sys
->stat
.i_bytes
,
1433 p_sys
->stat
.i_read_time
/ INT64_C(1000000),
1434 i_byterate
/ 1024 );
1439 i_read
= STREAM_CACHE_TRACK_SIZE
- i_buffered
;
1440 i_read
= __MIN( (int)p_sys
->stream
.i_read_size
, i_read
);
1441 i_read
= AReadStream( s
, &tk
->p_buffer
[i_buffered
], i_read
);
1444 else if( i_read
== 0 )
1450 msg_Dbg( s
, "received first data after %d ms",
1451 (int)((i_first
-i_start
)/1000) );
1454 tk
->i_end
+= i_read
;
1456 p_sys
->stat
.i_read_count
++;
1460 /****************************************************************************
1462 ****************************************************************************/
1464 * Read from the stream untill first newline.
1465 * \param s Stream handle to read from
1466 * \return A pointer to the allocated output string. You need to free this when you are done.
1468 #define STREAM_PROBE_LINE 2048
1469 #define STREAM_LINE_MAX (2048*100)
1470 char *stream_ReadLine( stream_t
*s
)
1472 char *p_line
= NULL
;
1473 int i_line
= 0, i_read
= 0;
1475 while( i_read
< STREAM_LINE_MAX
)
1478 const uint8_t *p_data
;
1482 /* Probe new data */
1483 i_data
= stream_Peek( s
, &p_data
, STREAM_PROBE_LINE
);
1484 if( i_data
<= 0 ) break; /* No more data */
1487 i_pos
= stream_Tell( s
);
1488 if( i_pos
== 0 && i_data
>= 2 )
1490 const char *psz_encoding
= NULL
;
1492 if( !memcmp( p_data
, "\xFF\xFE", 2 ) )
1494 psz_encoding
= "UTF-16LE";
1495 s
->p_text
->b_little_endian
= true;
1497 else if( !memcmp( p_data
, "\xFE\xFF", 2 ) )
1499 psz_encoding
= "UTF-16BE";
1502 /* Open the converter if we need it */
1503 if( psz_encoding
!= NULL
)
1505 msg_Dbg( s
, "UTF-16 BOM detected" );
1506 s
->p_text
->i_char_width
= 2;
1507 s
->p_text
->conv
= vlc_iconv_open( "UTF-8", psz_encoding
);
1508 if( s
->p_text
->conv
== (vlc_iconv_t
)-1 )
1509 msg_Err( s
, "iconv_open failed" );
1513 if( i_data
% s
->p_text
->i_char_width
)
1515 /* keep i_char_width boundary */
1516 i_data
= i_data
- ( i_data
% s
->p_text
->i_char_width
);
1517 msg_Warn( s
, "the read is not i_char_width compatible");
1523 /* Check if there is an EOL */
1524 if( s
->p_text
->i_char_width
== 1 )
1526 /* UTF-8: 0A <LF> */
1527 psz_eol
= memchr( p_data
, '\n', i_data
);
1528 if( psz_eol
== NULL
)
1529 /* UTF-8: 0D <CR> */
1530 psz_eol
= memchr( p_data
, '\r', i_data
);
1534 const uint8_t *p_last
= p_data
+ i_data
- s
->p_text
->i_char_width
;
1535 uint16_t eol
= s
->p_text
->b_little_endian
? 0x0A00 : 0x00A0;
1537 assert( s
->p_text
->i_char_width
== 2 );
1539 /* UTF-16: 000A <LF> */
1540 for( const uint8_t *p
= p_data
; p
<= p_last
; p
+= 2 )
1542 if( U16_AT( p
) == eol
)
1544 psz_eol
= (char *)p
+ 1;
1549 if( psz_eol
== NULL
)
1550 { /* UTF-16: 000D <CR> */
1551 eol
= s
->p_text
->b_little_endian
? 0x0D00 : 0x00D0;
1552 for( const uint8_t *p
= p_data
; p
<= p_last
; p
+= 2 )
1554 if( U16_AT( p
) == eol
)
1556 psz_eol
= (char *)p
+ 1;
1565 i_data
= (psz_eol
- (char *)p_data
) + 1;
1566 p_line
= realloc_or_free( p_line
,
1567 i_line
+ i_data
+ s
->p_text
->i_char_width
); /* add \0 */
1570 i_data
= stream_Read( s
, &p_line
[i_line
], i_data
);
1571 if( i_data
<= 0 ) break; /* Hmmm */
1572 i_line
+= i_data
- s
->p_text
->i_char_width
; /* skip \n */;
1575 /* We have our line */
1579 /* Read data (+1 for easy \0 append) */
1580 p_line
= realloc_or_free( p_line
,
1581 i_line
+ STREAM_PROBE_LINE
+ s
->p_text
->i_char_width
);
1584 i_data
= stream_Read( s
, &p_line
[i_line
], STREAM_PROBE_LINE
);
1585 if( i_data
<= 0 ) break; /* Hmmm */
1593 for( j
= 0; j
< s
->p_text
->i_char_width
; j
++ )
1595 p_line
[i_line
+ j
] = '\0';
1597 i_line
+= s
->p_text
->i_char_width
; /* the added \0 */
1598 if( s
->p_text
->i_char_width
> 1 )
1601 size_t i_in
= 0, i_out
= 0;
1602 const char * p_in
= NULL
;
1603 char * p_out
= NULL
;
1604 char * psz_new_line
= NULL
;
1607 /* UTF-8 needs at most 150% of the buffer as many as UTF-16 */
1608 i_new_line
= i_line
* 3 / 2;
1609 psz_new_line
= malloc( i_new_line
);
1610 if( psz_new_line
== NULL
)
1612 i_in
= (size_t)i_line
;
1613 i_out
= (size_t)i_new_line
;
1615 p_out
= psz_new_line
;
1617 if( vlc_iconv( s
->p_text
->conv
, &p_in
, &i_in
, &p_out
, &i_out
) == (size_t)-1 )
1619 msg_Err( s
, "iconv failed" );
1620 msg_Dbg( s
, "original: %d, in %d, out %d", i_line
, (int)i_in
, (int)i_out
);
1623 p_line
= psz_new_line
;
1624 i_line
= (size_t)i_new_line
- i_out
; /* does not include \0 */
1627 /* Remove trailing LF/CR */
1628 while( i_line
>= 2 && ( p_line
[i_line
-2] == '\r' ||
1629 p_line
[i_line
-2] == '\n') ) i_line
--;
1631 /* Make sure the \0 is there */
1632 p_line
[i_line
-1] = '\0';
1638 /* We failed to read any data, probably EOF */
1642 if( s
->p_text
->conv
!= (vlc_iconv_t
)(-1) )
1643 vlc_iconv_close( s
->p_text
->conv
);
1644 s
->p_text
->conv
= (vlc_iconv_t
)(-1);
1648 /****************************************************************************
1649 * Access reading/seeking wrappers to handle concatenated streams.
1650 ****************************************************************************/
1651 static int AReadStream( stream_t
*s
, void *p_read
, unsigned int i_read
)
1653 stream_sys_t
*p_sys
= s
->p_sys
;
1654 access_t
*p_access
= p_sys
->p_access
;
1655 input_thread_t
*p_input
= s
->p_input
;
1656 int i_read_orig
= i_read
;
1658 if( !p_sys
->i_list
)
1660 i_read
= p_access
->pf_read( p_access
, p_read
, i_read
);
1665 vlc_mutex_lock( &p_input
->p
->counters
.counters_lock
);
1666 stats_Update( p_input
->p
->counters
.p_read_bytes
, i_read
, &total
);
1667 stats_Update( p_input
->p
->counters
.p_input_bitrate
, total
, NULL
);
1668 stats_Update( p_input
->p
->counters
.p_read_packets
, 1, NULL
);
1669 vlc_mutex_unlock( &p_input
->p
->counters
.counters_lock
);
1674 i_read
= p_sys
->p_list_access
->pf_read( p_sys
->p_list_access
, p_read
,
1677 /* If we reached an EOF then switch to the next stream in the list */
1678 if( i_read
== 0 && p_sys
->i_list_index
+ 1 < p_sys
->i_list
)
1680 char *psz_name
= p_sys
->list
[++p_sys
->i_list_index
]->psz_path
;
1681 access_t
*p_list_access
;
1683 msg_Dbg( s
, "opening input `%s'", psz_name
);
1685 p_list_access
= access_New( s
, s
->p_input
, p_access
->psz_access
, "", psz_name
);
1687 if( !p_list_access
) return 0;
1689 if( p_sys
->p_list_access
!= p_access
)
1690 access_Delete( p_sys
->p_list_access
);
1692 p_sys
->p_list_access
= p_list_access
;
1694 /* We have to read some data */
1695 return AReadStream( s
, p_read
, i_read_orig
);
1698 /* Update read bytes in input */
1703 vlc_mutex_lock( &p_input
->p
->counters
.counters_lock
);
1704 stats_Update( p_input
->p
->counters
.p_read_bytes
, i_read
, &total
);
1705 stats_Update( p_input
->p
->counters
.p_input_bitrate
, total
, NULL
);
1706 stats_Update( p_input
->p
->counters
.p_read_packets
, 1, NULL
);
1707 vlc_mutex_unlock( &p_input
->p
->counters
.counters_lock
);
1712 static block_t
*AReadBlock( stream_t
*s
, bool *pb_eof
)
1714 stream_sys_t
*p_sys
= s
->p_sys
;
1715 access_t
*p_access
= p_sys
->p_access
;
1716 input_thread_t
*p_input
= s
->p_input
;
1720 if( !p_sys
->i_list
)
1722 p_block
= p_access
->pf_block( p_access
);
1723 if( pb_eof
) *pb_eof
= p_access
->info
.b_eof
;
1724 if( p_input
&& p_block
&& libvlc_stats (p_access
) )
1728 vlc_mutex_lock( &p_input
->p
->counters
.counters_lock
);
1729 stats_Update( p_input
->p
->counters
.p_read_bytes
,
1730 p_block
->i_buffer
, &total
);
1731 stats_Update( p_input
->p
->counters
.p_input_bitrate
,
1733 stats_Update( p_input
->p
->counters
.p_read_packets
, 1, NULL
);
1734 vlc_mutex_unlock( &p_input
->p
->counters
.counters_lock
);
1739 p_block
= p_sys
->p_list_access
->pf_block( p_sys
->p_list_access
);
1740 b_eof
= p_sys
->p_list_access
->info
.b_eof
;
1741 if( pb_eof
) *pb_eof
= b_eof
;
1743 /* If we reached an EOF then switch to the next stream in the list */
1744 if( !p_block
&& b_eof
&& p_sys
->i_list_index
+ 1 < p_sys
->i_list
)
1746 char *psz_name
= p_sys
->list
[++p_sys
->i_list_index
]->psz_path
;
1747 access_t
*p_list_access
;
1749 msg_Dbg( s
, "opening input `%s'", psz_name
);
1751 p_list_access
= access_New( s
, s
->p_input
, p_access
->psz_access
, "", psz_name
);
1753 if( !p_list_access
) return 0;
1755 if( p_sys
->p_list_access
!= p_access
)
1756 access_Delete( p_sys
->p_list_access
);
1758 p_sys
->p_list_access
= p_list_access
;
1760 /* We have to read some data */
1761 return AReadBlock( s
, pb_eof
);
1769 vlc_mutex_lock( &p_input
->p
->counters
.counters_lock
);
1770 stats_Update( p_input
->p
->counters
.p_read_bytes
,
1771 p_block
->i_buffer
, &total
);
1772 stats_Update( p_input
->p
->counters
.p_input_bitrate
, total
, NULL
);
1773 stats_Update( p_input
->p
->counters
.p_read_packets
, 1 , NULL
);
1774 vlc_mutex_unlock( &p_input
->p
->counters
.counters_lock
);
1780 static int ASeek( stream_t
*s
, uint64_t i_pos
)
1782 stream_sys_t
*p_sys
= s
->p_sys
;
1783 access_t
*p_access
= p_sys
->p_access
;
1785 /* Check which stream we need to access */
1791 access_t
*p_list_access
= 0;
1793 for( i
= 0; i
< p_sys
->i_list
- 1; i
++ )
1795 if( i_pos
< p_sys
->list
[i
]->i_size
+ i_size
) break;
1796 i_size
+= p_sys
->list
[i
]->i_size
;
1798 psz_name
= p_sys
->list
[i
]->psz_path
;
1800 if( i
!= p_sys
->i_list_index
)
1801 msg_Dbg( s
, "opening input `%s'", psz_name
);
1803 if( i
!= p_sys
->i_list_index
&& i
!= 0 )
1806 access_New( s
, s
->p_input
, p_access
->psz_access
, "", psz_name
);
1808 else if( i
!= p_sys
->i_list_index
)
1810 p_list_access
= p_access
;
1815 if( p_sys
->p_list_access
!= p_access
)
1816 access_Delete( p_sys
->p_list_access
);
1818 p_sys
->p_list_access
= p_list_access
;
1821 p_sys
->i_list_index
= i
;
1822 return p_sys
->p_list_access
->pf_seek( p_sys
->p_list_access
,
1826 return p_access
->pf_seek( p_access
, i_pos
);
1831 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
1832 * "p_read" is NULL then data are skipped instead of read.
1833 * \return The real number of bytes read/skip. If this value is less
1834 * than i_read that means that it's the end of the stream.
1835 * \note stream_Read increments the stream position, and when p_read is NULL,
1836 * this is its only task.
1838 int stream_Read( stream_t
*s
, void *p_read
, int i_read
)
1840 return s
->pf_read( s
, p_read
, i_read
);
1844 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
1845 * \return The real number of valid bytes. If it's less
1846 * or equal to 0, *pp_peek is invalid.
1847 * \note pp_peek is a pointer to internal buffer and it will be invalid as
1848 * soons as other stream_* functions are called.
1849 * \note Contrary to stream_Read, stream_Peek doesn't modify the stream
1850 * position, and doesn't necessarily involve copying of data. It's mainly
1851 * used by the modules to quickly probe the (head of the) stream.
1852 * \note Due to input limitation, the return value could be less than i_peek
1853 * without meaning the end of the stream (but only when you have i_peek >=
1854 * p_input->i_bufsize)
1856 int stream_Peek( stream_t
*s
, const uint8_t **pp_peek
, int i_peek
)
1858 return s
->pf_peek( s
, pp_peek
, i_peek
);
1862 * Use to control the "stream_t *". Look at #stream_query_e for
1863 * possible "i_query" value and format arguments. Return VLC_SUCCESS
1864 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
1866 int stream_vaControl( stream_t
*s
, int i_query
, va_list args
)
1868 return s
->pf_control( s
, i_query
, args
);
1874 void stream_Delete( stream_t
*s
)
1879 int stream_Control( stream_t
*s
, int i_query
, ... )
1885 return VLC_EGENERIC
;
1887 va_start( args
, i_query
);
1888 i_result
= s
->pf_control( s
, i_query
, args
);
1894 * Read "i_size" bytes and store them in a block_t.
1895 * It always read i_size bytes unless you are at the end of the stream
1896 * where it return what is available.
1898 block_t
*stream_Block( stream_t
*s
, int i_size
)
1900 if( i_size
<= 0 ) return NULL
;
1902 /* emulate block read */
1903 block_t
*p_bk
= block_Alloc( i_size
);
1906 int i_read
= stream_Read( s
, p_bk
->p_buffer
, i_size
);
1909 p_bk
->i_buffer
= i_read
;
1912 block_Release( p_bk
);
1918 * Read the remaining of the data if there is less than i_max_size bytes, otherwise
1921 * The stream position is unknown after the call.
1923 block_t
*stream_BlockRemaining( stream_t
*s
, int i_max_size
)
1925 int i_allocate
= __MIN(1000000, i_max_size
);
1926 int64_t i_size
= stream_Size( s
);
1929 int64_t i_position
= stream_Tell( s
);
1930 if( i_position
+ i_max_size
< i_size
)
1932 msg_Err( s
, "Remaining stream size is greater than %d bytes",
1936 i_allocate
= i_size
- i_position
;
1938 if( i_allocate
<= 0 )
1941 block_t
*p_block
= block_Alloc( i_allocate
);
1945 int i_read
= stream_Read( s
, &p_block
->p_buffer
[i_index
],
1946 p_block
->i_buffer
- i_index
);
1950 i_max_size
-= i_read
;
1951 if( i_max_size
<= 0 )
1953 p_block
= block_Realloc( p_block
, 0, p_block
->i_buffer
+
1954 __MIN(1000000, i_max_size
) );
1957 p_block
->i_buffer
= i_index
;