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 *****************************************************************************/
30 #include "input_internal.h"
35 * - tune the 2 methods
36 * - compute cost for seek
37 * - improve stream mode seeking with closest segments
43 * One linked list of data read
45 * More complex scheme using mutliple track to avoid seeking
48 /* How many tracks we have, currently only used for stream mode */
49 #ifdef OPTIMIZE_MEMORY
50 # define STREAM_CACHE_TRACK 1
51 /* Max size of our cache 128Ko per track */
52 # define STREAM_CACHE_SIZE (STREAM_CACHE_TRACK*1024*128)
54 # define STREAM_CACHE_TRACK 3
55 /* Max size of our cache 4Mo per track */
56 # define STREAM_CACHE_SIZE (4*STREAM_CACHE_TRACK*1024*1024)
59 /* How many data we try to prebuffer */
60 #define STREAM_CACHE_PREBUFFER_SIZE (32767)
61 /* Maximum time we take to pre-buffer */
62 #define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
64 /* Method1: Simple, for pf_block.
65 * We get blocks and put them in the linked list.
66 * We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
68 #define STREAM_DATA_WAIT 40000 /* Time between before a pf_block retry */
70 /* Method2: A bit more complex, for pf_read
71 * - We use ring buffers, only one if unseekable, all if seekable
72 * - Upon seek date current ring, then search if one ring match the pos,
73 * yes: switch to it, seek the access to match the end of the ring
74 * no: search the ring with i_end the closer to i_pos,
75 * if close enough, read data and use this ring
76 * else use the oldest ring, seek and use it.
78 * TODO: - with access non seekable: use all space available for only one ring, but
79 * we have to support seekable/non-seekable switch on the fly.
80 * - compute a good value for i_read_size
83 #define STREAM_READ_ATONCE 32767
84 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
108 vlc_bool_t b_block; /* Block method (1) or stream */
110 int64_t i_pos; /* Current reading offset */
112 /* Method 1: pf_block */
115 int64_t i_start; /* Offset of block for p_first */
116 int i_offset; /* Offset for data in p_current */
117 block_t *p_current; /* Current block */
119 int i_size; /* Total amount of data in the list */
125 /* Method 2: for pf_read */
128 int i_offset; /* Buffer offset in the current track */
129 int i_tk; /* Current track */
130 stream_track_t tk[STREAM_CACHE_TRACK];
136 int i_used; /* Used since last read */
141 /* Peek temporary buffer */
145 /* Stat for both method */
148 vlc_bool_t b_fastseek; /* From access */
150 /* Stat about reading data */
151 int64_t i_read_count;
155 /* Stat about seek */
163 access_entry_t **list;
165 access_t *p_list_access;
167 /* Preparse mode ? */
172 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read );
173 static int AStreamPeekBlock( stream_t *s, const uint8_t **p_peek, int i_read );
174 static int AStreamSeekBlock( stream_t *s, int64_t i_pos );
175 static void AStreamPrebufferBlock( stream_t *s );
176 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof );
179 static int AStreamReadStream( stream_t *s, void *p_read, int i_read );
180 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, int i_read );
181 static int AStreamSeekStream( stream_t *s, int64_t i_pos );
182 static void AStreamPrebufferStream( stream_t *s );
183 static int AReadStream( stream_t *s, void *p_read, int i_read );
186 static int AStreamControl( stream_t *s, int i_query, va_list );
187 static void AStreamDestroy( stream_t *s );
188 static void UStreamDestroy( stream_t *s );
189 static int ASeek( stream_t *s, int64_t i_pos );
192 /****************************************************************************
193 * stream_UrlNew: create a stream from a access
194 ****************************************************************************/
195 stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
197 const char *psz_access, *psz_demux;
202 if( !psz_url ) return 0;
204 char psz_dup[strlen (psz_url) + 1];
205 strcpy (psz_dup, psz_url);;
206 MRLSplit( p_parent, psz_dup, &psz_access, &psz_demux, &psz_path );
208 /* Now try a real access */
209 p_access = access2_New( p_parent, psz_access, psz_demux, psz_path, 0 );
211 if( p_access == NULL )
213 msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
217 if( !( p_res = stream_AccessNew( p_access, VLC_TRUE ) ) )
219 access2_Delete( p_access );
223 p_res->pf_destroy = UStreamDestroy;
227 stream_t *stream_AccessNew( access_t *p_access, vlc_bool_t b_quick )
229 stream_t *s = vlc_stream_create( VLC_OBJECT(p_access) );
233 if( !s ) return NULL;
235 /* Attach it now, needed for b_die */
236 vlc_object_attach( s, p_access );
238 s->pf_read = NULL; /* Set up later */
240 s->pf_control = AStreamControl;
241 s->pf_destroy = AStreamDestroy;
243 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
245 /* UTF16 and UTF32 text file conversion */
247 s->b_little_endian = VLC_FALSE;
248 s->conv = (vlc_iconv_t)(-1);
251 p_sys->p_access = p_access;
252 p_sys->b_block = p_access->pf_block ? VLC_TRUE : VLC_FALSE;
253 p_sys->i_pos = p_access->info.i_pos;
256 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
257 p_sys->stat.i_bytes = 0;
258 p_sys->stat.i_read_time = 0;
259 p_sys->stat.i_read_count = 0;
260 p_sys->stat.i_seek_count = 0;
261 p_sys->stat.i_seek_time = 0;
265 p_sys->i_list_index = 0;
266 p_sys->p_list_access = 0;
268 p_sys->b_quick = b_quick;
270 /* Get the additional list of inputs if any (for concatenation) */
271 if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
273 access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
274 char *psz_name, *psz_parser = psz_name = psz_list;
276 p_sys->p_list_access = p_access;
277 p_entry->i_size = p_access->info.i_size;
278 p_entry->psz_path = strdup( p_access->psz_path );
279 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
280 msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
281 p_entry->psz_path, p_access->info.i_size );
283 while( psz_name && *psz_name )
285 psz_parser = strchr( psz_name, ',' );
286 if( psz_parser ) *psz_parser = 0;
288 psz_name = strdup( psz_name );
291 access_t *p_tmp = access2_New( p_access, p_access->psz_access,
296 psz_name = psz_parser;
297 if( psz_name ) psz_name++;
301 msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
302 psz_name, p_tmp->info.i_size );
304 p_entry = malloc( sizeof(access_entry_t) );
305 p_entry->i_size = p_tmp->info.i_size;
306 p_entry->psz_path = psz_name;
307 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
309 access2_Delete( p_tmp );
312 psz_name = psz_parser;
313 if( psz_name ) psz_name++;
316 if( psz_list ) free( psz_list );
320 p_sys->p_peek = NULL;
324 s->pf_read = AStreamReadBlock;
325 s->pf_peek = AStreamPeekBlock;
327 /* Init all fields of p_sys->block */
328 p_sys->block.i_start = p_sys->i_pos;
329 p_sys->block.i_offset = 0;
330 p_sys->block.p_current = NULL;
331 p_sys->block.i_size = 0;
332 p_sys->block.p_first = NULL;
333 p_sys->block.pp_last = &p_sys->block.p_first;
335 /* Do the prebuffering */
336 AStreamPrebufferBlock( s );
338 if( p_sys->block.i_size <= 0 )
340 msg_Err( s, "cannot pre fill buffer" );
348 s->pf_read = AStreamReadStream;
349 s->pf_peek = AStreamPeekStream;
351 /* Allocate/Setup our tracks */
352 p_sys->stream.i_offset = 0;
353 p_sys->stream.i_tk = 0;
354 p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
355 p_sys->stream.i_used = 0;
356 access2_Control( p_access, ACCESS_GET_MTU,
357 &p_sys->stream.i_read_size );
358 if( p_sys->stream.i_read_size <= 0 )
359 p_sys->stream.i_read_size = STREAM_READ_ATONCE;
360 else if( p_sys->stream.i_read_size <= 256 )
361 p_sys->stream.i_read_size = 256;
363 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
365 p_sys->stream.tk[i].i_date = 0;
366 p_sys->stream.tk[i].i_start = p_sys->i_pos;
367 p_sys->stream.tk[i].i_end = p_sys->i_pos;
368 p_sys->stream.tk[i].p_buffer=
369 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
372 /* Do the prebuffering */
373 AStreamPrebufferStream( s );
375 if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
377 msg_Err( s, "cannot pre fill buffer" );
391 free( p_sys->stream.p_buffer );
394 vlc_object_detach( s );
395 vlc_object_destroy( s );
399 /****************************************************************************
401 ****************************************************************************/
402 static void AStreamDestroy( stream_t *s )
404 stream_sys_t *p_sys = s->p_sys;
406 vlc_object_detach( s );
408 if( p_sys->b_block ) block_ChainRelease( p_sys->block.p_first );
409 else free( p_sys->stream.p_buffer );
411 if( p_sys->p_peek ) free( p_sys->p_peek );
413 if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
414 access2_Delete( p_sys->p_list_access );
416 while( p_sys->i_list-- )
418 free( p_sys->list[p_sys->i_list]->psz_path );
419 free( p_sys->list[p_sys->i_list] );
420 if( !p_sys->i_list ) free( p_sys->list );
424 vlc_object_destroy( s );
427 static void UStreamDestroy( stream_t *s )
429 access_t *p_access = (access_t*)vlc_object_find( s, VLC_OBJECT_ACCESS, FIND_PARENT );
431 vlc_object_release( p_access );
432 access2_Delete( p_access );
435 /****************************************************************************
436 * stream_AccessReset:
437 ****************************************************************************/
438 void stream_AccessReset( stream_t *s )
440 stream_sys_t *p_sys = s->p_sys;
442 p_sys->i_pos = p_sys->p_access->info.i_pos;
446 block_ChainRelease( p_sys->block.p_first );
448 /* Init all fields of p_sys->block */
449 p_sys->block.i_start = p_sys->i_pos;
450 p_sys->block.i_offset = 0;
451 p_sys->block.p_current = NULL;
452 p_sys->block.i_size = 0;
453 p_sys->block.p_first = NULL;
454 p_sys->block.pp_last = &p_sys->block.p_first;
456 /* Do the prebuffering */
457 AStreamPrebufferBlock( s );
463 /* Setup our tracks */
464 p_sys->stream.i_offset = 0;
465 p_sys->stream.i_tk = 0;
466 p_sys->stream.i_used = 0;
468 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
470 p_sys->stream.tk[i].i_date = 0;
471 p_sys->stream.tk[i].i_start = p_sys->i_pos;
472 p_sys->stream.tk[i].i_end = p_sys->i_pos;
475 /* Do the prebuffering */
476 AStreamPrebufferStream( s );
480 /****************************************************************************
481 * stream_AccessUpdate:
482 ****************************************************************************/
483 void stream_AccessUpdate( stream_t *s )
485 stream_sys_t *p_sys = s->p_sys;
487 p_sys->i_pos = p_sys->p_access->info.i_pos;
492 for( i = 0; i < p_sys->i_list_index; i++ )
494 p_sys->i_pos += p_sys->list[i]->i_size;
499 /****************************************************************************
501 ****************************************************************************/
502 static int AStreamControl( stream_t *s, int i_query, va_list args )
504 stream_sys_t *p_sys = s->p_sys;
505 access_t *p_access = p_sys->p_access;
508 int64_t *pi_64, i_64;
513 case STREAM_GET_SIZE:
514 pi_64 = (int64_t*)va_arg( args, int64_t * );
515 if( s->p_sys->i_list )
519 for( i = 0; i < s->p_sys->i_list; i++ )
520 *pi_64 += s->p_sys->list[i]->i_size;
523 *pi_64 = p_access->info.i_size;
526 case STREAM_CAN_SEEK:
527 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
528 access2_Control( p_access, ACCESS_CAN_SEEK, p_bool );
531 case STREAM_CAN_FASTSEEK:
532 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
533 access2_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
536 case STREAM_GET_POSITION:
537 pi_64 = (int64_t*)va_arg( args, int64_t * );
538 *pi_64 = p_sys->i_pos;
541 case STREAM_SET_POSITION:
542 i_64 = (int64_t)va_arg( args, int64_t );
544 return AStreamSeekBlock( s, i_64 );
546 return AStreamSeekStream( s, i_64 );
551 case STREAM_CONTROL_ACCESS:
552 i_int = (int) va_arg( args, int );
553 if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
554 i_int != ACCESS_SET_PRIVATE_ID_CA &&
555 i_int != ACCESS_GET_PRIVATE_ID_STATE )
557 msg_Err( s, "Hey, what are you thinking ?"
558 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
561 return access2_vaControl( p_access, i_int, args );
563 case STREAM_GET_CONTENT_TYPE:
564 return access2_Control( p_access, ACCESS_GET_CONTENT_TYPE,
565 va_arg( args, char ** ) );
568 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
576 /****************************************************************************
578 ****************************************************************************/
579 static void AStreamPrebufferBlock( stream_t *s )
581 stream_sys_t *p_sys = s->p_sys;
582 access_t *p_access = p_sys->p_access;
587 msg_Dbg( s, "pre buffering" );
591 int64_t i_date = mdate();
595 if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
596 ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
601 p_sys->stat.i_bytes = p_sys->block.i_size;
602 p_sys->stat.i_read_time = i_date - i_start;
603 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
604 (p_sys->stat.i_read_time + 1);
606 msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
609 p_sys->stat.i_read_time / I64C(1000000),
615 if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
619 msleep( STREAM_DATA_WAIT );
625 /* Append the block */
626 p_sys->block.i_size += b->i_buffer;
627 *p_sys->block.pp_last = b;
628 p_sys->block.pp_last = &b->p_next;
630 p_sys->stat.i_read_count++;
634 if( p_access->info.b_prebuffered )
636 /* Access has already prebufferred - update stats and exit */
637 p_sys->stat.i_bytes = p_sys->block.i_size;
638 p_sys->stat.i_read_time = mdate() - i_start;
645 msg_Dbg( s, "received first data for our buffer");
650 p_sys->block.p_current = p_sys->block.p_first;
653 static int AStreamRefillBlock( stream_t *s );
655 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
657 stream_sys_t *p_sys = s->p_sys;
659 uint8_t *p_data= (uint8_t*)p_read;
663 if( p_sys->block.p_current == NULL )
668 /* seek within this stream if possible, else use plain old read and discard */
669 stream_sys_t *p_sys = s->p_sys;
670 access_t *p_access = p_sys->p_access;
672 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
674 return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
677 while( i_data < i_read )
680 p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
681 int i_copy = __MIN( i_current, i_read - i_data);
687 &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
693 p_sys->block.i_offset += i_copy;
694 if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
696 /* Current block is now empty, switch to next */
697 if( p_sys->block.p_current )
699 p_sys->block.i_offset = 0;
700 p_sys->block.p_current = p_sys->block.p_current->p_next;
703 if( AStreamRefillBlock( s ) )
710 p_sys->i_pos += i_data;
714 static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, int i_read )
716 stream_sys_t *p_sys = s->p_sys;
722 if( p_sys->block.p_current == NULL ) return 0; /* EOF */
724 /* We can directly give a pointer over our buffer */
725 if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
727 *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
731 /* We need to create a local copy */
732 if( p_sys->i_peek < i_read )
734 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
740 p_sys->i_peek = i_read;
743 /* Fill enough data */
744 while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
747 block_t **pp_last = p_sys->block.pp_last;
749 if( AStreamRefillBlock( s ) ) break;
751 /* Our buffer are probably filled enough, don't try anymore */
752 if( pp_last == p_sys->block.pp_last ) break;
755 /* Copy what we have */
756 b = p_sys->block.p_current;
757 i_offset = p_sys->block.i_offset;
758 p_data = p_sys->p_peek;
760 while( b && i_data < i_read )
762 int i_current = b->i_buffer - i_offset;
763 int i_copy = __MIN( i_current, i_read - i_data );
765 memcpy( p_data, &b->p_buffer[i_offset], i_copy );
770 if( i_offset >= b->i_buffer )
777 *pp_peek = p_sys->p_peek;
781 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
783 stream_sys_t *p_sys = s->p_sys;
784 access_t *p_access = p_sys->p_access;
785 int64_t i_offset = i_pos - p_sys->block.i_start;
788 /* We already have thoses data, just update p_current/i_offset */
789 if( i_offset >= 0 && i_offset < p_sys->block.i_size )
791 block_t *b = p_sys->block.p_first;
794 while( i_current + b->i_buffer < i_offset )
796 i_current += b->i_buffer;
800 p_sys->block.p_current = b;
801 p_sys->block.i_offset = i_offset - i_current;
803 p_sys->i_pos = i_pos;
808 /* We may need to seek or to read data */
812 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
816 msg_Err( s, "backward seeking impossible (access not seekable)" );
824 vlc_bool_t b_aseek, b_aseekfast;
826 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
827 access2_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
832 msg_Warn( s, I64Fd" bytes need to be skipped "
833 "(access non seekable)",
834 i_offset - p_sys->block.i_size );
838 int64_t i_skip = i_offset - p_sys->block.i_size;
840 /* Avg bytes per packets */
841 int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
842 /* TODO compute a seek cost instead of fixed threshold */
843 int i_th = b_aseekfast ? 1 : 5;
845 if( i_skip <= i_th * i_avg &&
846 i_skip < STREAM_CACHE_SIZE )
851 msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
852 b_seek, i_th*i_avg, i_skip );
858 int64_t i_start, i_end;
859 /* Do the access seek */
861 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
865 block_ChainRelease( p_sys->block.p_first );
868 p_sys->block.i_start = p_sys->i_pos = i_pos;
869 p_sys->block.i_offset = 0;
870 p_sys->block.p_current = NULL;
871 p_sys->block.i_size = 0;
872 p_sys->block.p_first = NULL;
873 p_sys->block.pp_last = &p_sys->block.p_first;
876 if( AStreamRefillBlock( s ) )
878 msg_Err( s, "cannot pre fill buffer" );
882 p_sys->stat.i_seek_time += i_end - i_start;
883 p_sys->stat.i_seek_count++;
888 /* Read enough data */
889 while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
891 if( AStreamRefillBlock( s ) )
893 msg_Err( s, "can't read enough data in seek" );
896 while( p_sys->block.p_current &&
897 p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
899 p_sys->i_pos += p_sys->block.p_current->i_buffer;
900 p_sys->block.p_current = p_sys->block.p_current->p_next;
904 p_sys->block.i_offset = i_pos - p_sys->i_pos;
905 p_sys->i_pos = i_pos;
914 static int AStreamRefillBlock( stream_t *s )
916 stream_sys_t *p_sys = s->p_sys;
917 int64_t i_start, i_stop;
921 while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
922 p_sys->block.p_first != p_sys->block.p_current )
924 block_t *b = p_sys->block.p_first;
926 p_sys->block.i_start += b->i_buffer;
927 p_sys->block.i_size -= b->i_buffer;
928 p_sys->block.p_first = b->p_next;
932 if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
933 p_sys->block.p_current == p_sys->block.p_first &&
934 p_sys->block.p_current->p_next ) /* At least 2 packets */
936 /* Enough data, don't read more */
940 /* Now read a new block */
946 if( s->b_die ) return VLC_EGENERIC;
950 if( ( b = AReadBlock( s, &b_eof ) ) ) break;
952 if( b_eof ) return VLC_EGENERIC;
954 msleep( STREAM_DATA_WAIT );
961 /* Append the block */
962 p_sys->block.i_size += b->i_buffer;
963 *p_sys->block.pp_last = b;
964 p_sys->block.pp_last = &b->p_next;
967 if( p_sys->block.p_current == NULL )
968 p_sys->block.p_current = b;
971 p_sys->stat.i_bytes += b->i_buffer;
972 p_sys->stat.i_read_time += i_stop - i_start;
973 p_sys->stat.i_read_count++;
982 /****************************************************************************
984 ****************************************************************************/
985 static int AStreamRefillStream( stream_t *s );
987 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
989 stream_sys_t *p_sys = s->p_sys;
990 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
992 uint8_t *p_data = (uint8_t *)p_read;
995 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
999 /* seek within this stream if possible, else use plain old read and discard */
1000 stream_sys_t *p_sys = s->p_sys;
1001 access_t *p_access = p_sys->p_access;
1003 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1005 return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
1009 msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
1010 " offset=%d end="I64Fd,
1011 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1012 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1015 while( i_data < i_read )
1017 int i_off = (tk->i_start + p_sys->stream.i_offset) %
1018 STREAM_CACHE_TRACK_SIZE;
1020 __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1021 STREAM_CACHE_TRACK_SIZE - i_off );
1022 int i_copy = __MIN( i_current, i_read - i_data );
1024 if( i_copy <= 0 ) break; /* EOF */
1027 /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1030 memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1034 p_sys->stream.i_offset += i_copy;
1036 /* Update pos now */
1037 p_sys->i_pos += i_copy;
1040 p_sys->stream.i_used += i_copy;
1041 if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1042 p_sys->stream.i_used >= p_sys->stream.i_read_size )
1044 if( AStreamRefillStream( s ) )
1047 if( tk->i_start >= tk->i_end ) break;
1055 static int AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, int i_read )
1057 stream_sys_t *p_sys = s->p_sys;
1058 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1061 if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1064 msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
1065 "start="I64Fd" offset=%d end="I64Fd,
1066 i_read, p_sys->i_pos, p_sys->stream.i_tk,
1067 tk->i_start, p_sys->stream.i_offset, tk->i_end );
1070 /* Avoid problem, but that should *never* happen */
1071 if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1072 i_read = STREAM_CACHE_TRACK_SIZE / 2;
1074 while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1076 if( p_sys->stream.i_used <= 1 )
1078 /* Be sure we will read something */
1079 p_sys->stream.i_used += i_read -
1080 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1082 if( AStreamRefillStream( s ) ) break;
1085 if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1086 i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1088 /* Now, direct pointer or a copy ? */
1089 i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1090 if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1092 *pp_peek = &tk->p_buffer[i_off];
1096 if( p_sys->i_peek < i_read )
1098 p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1099 if( !p_sys->p_peek )
1104 p_sys->i_peek = i_read;
1107 memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1108 STREAM_CACHE_TRACK_SIZE - i_off );
1109 memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1110 &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1112 *pp_peek = p_sys->p_peek;
1116 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1118 stream_sys_t *p_sys = s->p_sys;
1119 access_t *p_access = p_sys->p_access;
1121 vlc_bool_t b_afastseek;
1127 msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
1128 " tk=%d start="I64Fd" offset=%d end="I64Fd,
1129 i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1130 p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1131 p_sys->stream.i_offset,
1132 p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1136 /* Seek in our current track ? */
1137 if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1138 i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1140 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1142 msg_Dbg( s, "AStreamSeekStream: current track" );
1144 p_sys->i_pos = i_pos;
1145 p_sys->stream.i_offset = i_pos - tk->i_start;
1147 /* If there is not enough data left in the track, refill */
1148 /* \todo How to get a correct value for
1149 * - refilling threshold
1150 * - how much to refill
1152 if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
1153 p_sys->stream.i_read_size )
1155 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1157 p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
1158 AStreamRefillStream( s );
1164 access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1167 /* We can't do nothing */
1168 msg_Dbg( s, "AStreamSeekStream: can't seek" );
1169 return VLC_EGENERIC;
1172 /* Date the current track */
1173 p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1175 /* Try to reuse already read data */
1176 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1178 stream_track_t *tk = &p_sys->stream.tk[i];
1180 if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1183 msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
1184 " end="I64Fd, i, tk->i_start, tk->i_end );
1187 /* Seek at the end of the buffer */
1188 if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1191 p_sys->i_pos = i_pos;
1192 p_sys->stream.i_tk = i;
1193 p_sys->stream.i_offset = i_pos - tk->i_start;
1195 if( p_sys->stream.i_used < 1024 )
1196 p_sys->stream.i_used = 1024;
1198 if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1199 return VLC_EGENERIC;
1205 access2_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1206 /* FIXME compute seek cost (instead of static 'stupid' value) */
1207 i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1213 /* Search closest segment TODO */
1214 for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1216 stream_track_t *tk = &p_sys->stream.tk[i];
1218 if( i_pos + i_maxth >= tk->i_start )
1220 msg_Dbg( s, "good segment before current pos, TODO" );
1222 if( i_pos - i_maxth <= tk->i_end )
1224 msg_Dbg( s, "good segment after current pos, TODO" );
1229 /* Nothing good, seek and choose oldest segment */
1230 if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1231 p_sys->i_pos = i_pos;
1234 for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1236 if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1240 /* Reset the segment */
1241 p_sys->stream.i_tk = i_new;
1242 p_sys->stream.i_offset = 0;
1243 p_sys->stream.tk[i_new].i_start = i_pos;
1244 p_sys->stream.tk[i_new].i_end = i_pos;
1247 if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1248 p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1250 if( AStreamRefillStream( s ) )
1251 return VLC_EGENERIC;
1256 static int AStreamRefillStream( stream_t *s )
1258 stream_sys_t *p_sys = s->p_sys;
1259 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1261 /* We read but won't increase i_start after initial start + offset */
1263 __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1264 (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1265 vlc_bool_t b_read = VLC_FALSE;
1266 int64_t i_start, i_stop;
1268 if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1271 msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
1272 p_sys->stream.i_used, i_toread );
1276 while( i_toread > 0 )
1278 int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1282 return VLC_EGENERIC;
1284 i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1285 i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1287 /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1290 msleep( STREAM_DATA_WAIT );
1293 else if( i_read == 0 )
1295 if( !b_read ) return VLC_EGENERIC;
1301 tk->i_end += i_read;
1303 /* Windows of STREAM_CACHE_TRACK_SIZE */
1304 if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1306 int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1308 tk->i_start += i_invalid;
1309 p_sys->stream.i_offset -= i_invalid;
1313 p_sys->stream.i_used -= i_read;
1315 p_sys->stat.i_bytes += i_read;
1316 p_sys->stat.i_read_count++;
1320 p_sys->stat.i_read_time += i_stop - i_start;
1325 static void AStreamPrebufferStream( stream_t *s )
1327 stream_sys_t *p_sys = s->p_sys;
1328 access_t *p_access = p_sys->p_access;
1330 int64_t i_first = 0;
1332 int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1333 ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1334 STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1336 msg_Dbg( s, "pre-buffering..." );
1340 stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1342 int64_t i_date = mdate();
1345 if( s->b_die || tk->i_end >= i_prebuffer ||
1346 (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1351 p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1352 p_sys->stat.i_read_time = i_date - i_start;
1353 i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
1354 (p_sys->stat.i_read_time+1);
1356 msg_Dbg( s, "pre-buffering done "I64Fd" bytes in "I64Fd"s - "
1358 p_sys->stat.i_bytes,
1359 p_sys->stat.i_read_time / I64C(1000000),
1360 i_byterate / 1024 );
1365 i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1366 i_read = __MIN( p_sys->stream.i_read_size, i_read );
1367 i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1370 msleep( STREAM_DATA_WAIT );
1373 else if( i_read == 0 )
1382 msg_Dbg( s, "received first data for our buffer");
1385 tk->i_end += i_read;
1387 p_sys->stat.i_read_count++;
1392 /****************************************************************************
1394 ****************************************************************************/
1396 * Read from the stream untill first newline.
1397 * \param s Stream handle to read from
1398 * \return A pointer to the allocated output string. You need to free this when you are done.
1400 #define STREAM_PROBE_LINE 2048
1401 #define STREAM_LINE_MAX (2048*100)
1402 char * stream_ReadLine( stream_t *s )
1404 char *p_line = NULL;
1405 int i_line = 0, i_read = 0;
1407 while( i_read < STREAM_LINE_MAX )
1410 const uint8_t *p_data;
1414 /* Probe new data */
1415 i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1416 if( i_data <= 0 ) break; /* No more data */
1419 i_pos = stream_Tell( s );
1420 if( i_pos == 0 && i_data > 4 )
1423 char *psz_encoding = NULL;
1425 if( p_data[0] == 0xEF && p_data[1] == 0xBB && p_data[2] == 0xBF )
1427 psz_encoding = strdup( "UTF-8" );
1430 else if( p_data[0] == 0x00 && p_data[1] == 0x00 )
1432 if( p_data[2] == 0xFE && p_data[3] == 0xFF )
1434 psz_encoding = strdup( "UTF-32BE" );
1435 s->i_char_width = 4;
1439 else if( p_data[0] == 0xFF && p_data[1] == 0xFE )
1441 if( p_data[2] == 0x00 && p_data[3] == 0x00 )
1443 psz_encoding = strdup( "UTF-32LE" );
1444 s->i_char_width = 4;
1445 s->b_little_endian = VLC_TRUE;
1450 psz_encoding = strdup( "UTF-16LE" );
1451 s->b_little_endian = VLC_TRUE;
1452 s->i_char_width = 2;
1456 else if( p_data[0] == 0xFE && p_data[1] == 0xFF )
1458 psz_encoding = strdup( "UTF-16BE" );
1459 s->i_char_width = 2;
1463 /* Seek past the BOM */
1466 stream_Seek( s, i_bom_size );
1467 p_data += i_bom_size;
1468 i_data -= i_bom_size;
1471 /* Open the converter if we need it */
1472 if( psz_encoding != NULL )
1474 input_thread_t *p_input;
1475 msg_Dbg( s, "%s BOM detected", psz_encoding );
1476 p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
1477 if( s->i_char_width > 1 )
1479 s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1480 if( s->conv == (vlc_iconv_t)-1 )
1482 msg_Err( s, "iconv_open failed" );
1485 if( p_input != NULL)
1487 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1488 var_SetString( p_input, "subsdec-encoding", "UTF-8" );
1489 vlc_object_release( p_input );
1491 if( psz_encoding ) free( psz_encoding );
1495 if( i_data % s->i_char_width )
1497 /* keep i_char_width boundary */
1498 i_data = i_data - ( i_data % s->i_char_width );
1499 msg_Warn( s, "the read is not i_char_width compatible");
1505 /* Check if there is an EOL */
1506 if( s->i_char_width == 1 )
1508 /* UTF-8: 0A <LF> */
1509 psz_eol = memchr( p_data, '\n', i_data );
1513 const uint8_t *p = p_data;
1514 const uint8_t *p_last = p + i_data - s->i_char_width;
1516 if( s->i_char_width == 2 )
1518 if( s->b_little_endian == VLC_TRUE)
1520 /* UTF-16LE: 0A 00 <LF> */
1521 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ) )
1526 /* UTF-16BE: 00 0A <LF> */
1527 while( p <= p_last && ( p[1] != 0x0A || p[0] != 0x00 ) )
1531 else if( s->i_char_width == 4 )
1533 if( s->b_little_endian == VLC_TRUE)
1535 /* UTF-32LE: 0A 00 00 00 <LF> */
1536 while( p <= p_last && ( p[0] != 0x0A || p[1] != 0x00 ||
1537 p[2] != 0x00 || p[3] != 0x00 ) )
1542 /* UTF-32BE: 00 00 00 0A <LF> */
1543 while( p <= p_last && ( p[3] != 0x0A || p[2] != 0x00 ||
1544 p[1] != 0x00 || p[0] != 0x00 ) )
1555 psz_eol = (char *)p + ( s->i_char_width - 1 );
1561 i_data = (psz_eol - (char *)p_data) + 1;
1562 p_line = realloc( p_line, i_line + i_data + s->i_char_width ); /* add \0 */
1563 i_data = stream_Read( s, &p_line[i_line], i_data );
1564 if( i_data <= 0 ) break; /* Hmmm */
1565 i_line += i_data - s->i_char_width; /* skip \n */;
1568 /* We have our line */
1572 /* Read data (+1 for easy \0 append) */
1573 p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->i_char_width );
1574 i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1575 if( i_data <= 0 ) break; /* Hmmm */
1583 for( j = 0; j < s->i_char_width; j++ )
1585 p_line[i_line + j] = '\0';
1587 i_line += s->i_char_width; /* the added \0 */
1588 if( s->i_char_width > 1 )
1590 size_t i_in = 0, i_out = 0;
1591 const char * p_in = NULL;
1592 char * p_out = NULL;
1593 char * psz_new_line = NULL;
1596 psz_new_line = malloc( i_line );
1598 i_in = i_out = (size_t)i_line;
1600 p_out = psz_new_line;
1602 if( vlc_iconv( s->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1604 msg_Err( s, "iconv failed" );
1605 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1607 if( p_line ) free( p_line );
1608 p_line = psz_new_line;
1609 i_line = (size_t)i_line - i_out; /* does not include \0 */
1612 /* Remove trailing LF/CR */
1613 while( i_line >= 2 && ( p_line[i_line-2] == '\r' ||
1614 p_line[i_line-2] == '\n') ) i_line--;
1616 /* Make sure the \0 is there */
1617 p_line[i_line-1] = '\0';
1622 /* We failed to read any data, probably EOF */
1623 if( p_line ) free( p_line );
1624 if( s->conv != (vlc_iconv_t)(-1) ) vlc_iconv_close( s->conv );
1628 /****************************************************************************
1629 * Access reading/seeking wrappers to handle concatenated streams.
1630 ****************************************************************************/
1631 static int AReadStream( stream_t *s, void *p_read, int i_read )
1633 stream_sys_t *p_sys = s->p_sys;
1634 access_t *p_access = p_sys->p_access;
1635 input_thread_t *p_input = NULL;
1636 int i_read_orig = i_read;
1639 if( s->p_parent && s->p_parent->p_parent &&
1640 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1641 p_input = (input_thread_t *)s->p_parent->p_parent;
1643 if( !p_sys->i_list )
1645 i_read = p_access->pf_read( p_access, p_read, i_read );
1648 vlc_object_yield( p_input );
1649 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1650 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read,
1652 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1653 (float)i_total, NULL );
1654 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1655 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1656 vlc_object_release( p_input );
1661 i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1664 /* If we reached an EOF then switch to the next stream in the list */
1665 if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1667 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1668 access_t *p_list_access;
1670 msg_Dbg( s, "opening input `%s'", psz_name );
1672 p_list_access = access2_New( s, p_access->psz_access, "", psz_name, 0 );
1674 if( !p_list_access ) return 0;
1676 if( p_sys->p_list_access != p_access )
1677 access2_Delete( p_sys->p_list_access );
1679 p_sys->p_list_access = p_list_access;
1681 /* We have to read some data */
1682 return AReadStream( s, p_read, i_read_orig );
1685 /* Update read bytes in input */
1688 vlc_object_yield( p_input );
1689 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1690 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes, i_read, &i_total );
1691 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1692 (float)i_total, NULL );
1693 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1694 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1695 vlc_object_release( p_input );
1700 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof )
1702 stream_sys_t *p_sys = s->p_sys;
1703 access_t *p_access = p_sys->p_access;
1704 input_thread_t *p_input = NULL;
1709 if( s->p_parent && s->p_parent->p_parent &&
1710 s->p_parent->p_parent->i_object_type == VLC_OBJECT_INPUT )
1711 p_input = (input_thread_t *)s->p_parent->p_parent;
1713 if( !p_sys->i_list )
1715 p_block = p_access->pf_block( p_access );
1716 if( pb_eof ) *pb_eof = p_access->info.b_eof;
1717 if( p_input && p_block && p_access->p_libvlc->b_stats )
1719 vlc_object_yield( p_input );
1720 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1721 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1722 p_block->i_buffer, &i_total );
1723 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1724 (float)i_total, NULL );
1725 stats_UpdateInteger( s, p_input->p->counters.p_read_packets, 1, NULL );
1726 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1727 vlc_object_release( p_input );
1732 p_block = p_sys->p_list_access->pf_block( p_access );
1733 b_eof = p_sys->p_list_access->info.b_eof;
1734 if( pb_eof ) *pb_eof = b_eof;
1736 /* If we reached an EOF then switch to the next stream in the list */
1737 if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1739 char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1740 access_t *p_list_access;
1742 msg_Dbg( s, "opening input `%s'", psz_name );
1744 p_list_access = access2_New( s, p_access->psz_access, "", psz_name, 0 );
1746 if( !p_list_access ) return 0;
1748 if( p_sys->p_list_access != p_access )
1749 access2_Delete( p_sys->p_list_access );
1751 p_sys->p_list_access = p_list_access;
1753 /* We have to read some data */
1754 return AReadBlock( s, pb_eof );
1760 vlc_object_yield( p_input );
1761 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1762 stats_UpdateInteger( s, p_input->p->counters.p_read_bytes,
1763 p_block->i_buffer, &i_total );
1764 stats_UpdateFloat( s, p_input->p->counters.p_input_bitrate,
1765 (float)i_total, NULL );
1766 stats_UpdateInteger( s, p_input->p->counters.p_read_packets,
1768 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1769 vlc_object_release( p_input );
1775 static int ASeek( stream_t *s, int64_t i_pos )
1777 stream_sys_t *p_sys = s->p_sys;
1778 access_t *p_access = p_sys->p_access;
1780 /* Check which stream we need to access */
1786 access_t *p_list_access = 0;
1788 for( i = 0; i < p_sys->i_list - 1; i++ )
1790 if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1791 i_size += p_sys->list[i]->i_size;
1793 psz_name = p_sys->list[i]->psz_path;
1795 if( i != p_sys->i_list_index )
1796 msg_Dbg( s, "opening input `%s'", psz_name );
1798 if( i != p_sys->i_list_index && i != 0 )
1801 access2_New( s, p_access->psz_access, "", psz_name, 0 );
1803 else if( i != p_sys->i_list_index )
1805 p_list_access = p_access;
1810 if( p_sys->p_list_access != p_access )
1811 access2_Delete( p_sys->p_list_access );
1813 p_sys->p_list_access = p_list_access;
1816 p_sys->i_list_index = i;
1817 return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1821 return p_access->pf_seek( p_access, i_pos );
1826 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
1827 * "p_read" is NULL then data are skipped instead of read. The return
1828 * value is the real numbers of bytes read/skip. If this value is less
1829 * than i_read that means that it's the end of the stream.
1831 int stream_Read( stream_t *s, void *p_read, int i_read )
1833 return s->pf_read( s, p_read, i_read );
1837 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
1838 * \return The real numbers of valid bytes, if it's less
1839 * or equal to 0, *pp_peek is invalid.
1840 * \note pp_peek is a pointer to internal buffer and it will be invalid as
1841 * soons as other stream_* functions are called.
1842 * \note Due to input limitation, it could be less than i_peek without meaning
1843 * the end of the stream (but only when you have i_peek >=
1844 * p_input->i_bufsize)
1846 int stream_Peek( stream_t *s, const uint8_t **pp_peek, int i_peek )
1848 return s->pf_peek( s, pp_peek, i_peek );
1852 * Use to control the "stream_t *". Look at #stream_query_e for
1853 * possible "i_query" value and format arguments. Return VLC_SUCCESS
1854 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
1856 int stream_vaControl( stream_t *s, int i_query, va_list args )
1858 return s->pf_control( s, i_query, args );
1864 void stream_Delete( stream_t *s )
1869 int stream_Control( stream_t *s, int i_query, ... )
1875 return VLC_EGENERIC;
1877 va_start( args, i_query );
1878 i_result = s->pf_control( s, i_query, args );
1884 * Read "i_size" bytes and store them in a block_t.
1885 * It always read i_size bytes unless you are at the end of the stream
1886 * where it return what is available.
1888 block_t *stream_Block( stream_t *s, int i_size )
1890 if( i_size <= 0 ) return NULL;
1892 /* emulate block read */
1893 block_t *p_bk = block_New( s, i_size );
1896 p_bk->i_buffer = stream_Read( s, p_bk->p_buffer, i_size );
1897 if( p_bk->i_buffer > 0 )
1902 if( p_bk ) block_Release( p_bk );