1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: stream.c,v 1.1 2003/08/01 00:00:12 fenrir Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
26 #include <vlc/input.h>
30 /****************************************************************************
32 * XXX for now it's just a wrapper
34 ****************************************************************************/
40 input_thread_t *p_input;
44 stream_t *stream_OpenInput( input_thread_t *p_input )
48 s = vlc_object_create( p_input, sizeof( stream_t ) );
57 void stream_Release( stream_t *s )
59 vlc_object_destroy( s );
62 int stream_vaControl( stream_t *s, int i_query, va_list args )
70 p_i64 = (int64_t*) va_arg( args, int64_t * );
72 vlc_mutex_lock( &s->p_input->stream.stream_lock );
73 *p_i64 = s->p_input->stream.p_selected_area->i_size;
74 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
78 p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
80 vlc_mutex_lock( &s->p_input->stream.stream_lock );
81 *p_b = s->p_input->stream.b_seekable;
82 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
85 case STREAM_CAN_FASTSEEK:
86 p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
88 vlc_mutex_lock( &s->p_input->stream.stream_lock );
89 *p_b = s->p_input->stream.b_seekable && s->p_input->stream.i_method == INPUT_METHOD_FILE;
90 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
93 case STREAM_GET_POSITION:
94 p_i64 = (int64_t*) va_arg( args, int64_t * );
96 vlc_mutex_lock( &s->p_input->stream.stream_lock );
97 *p_i64 = s->p_input->stream.p_selected_area->i_tell;
98 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
101 case STREAM_SET_POSITION:
102 i64 = (int64_t) va_arg( args, int64_t );
104 vlc_mutex_lock( &s->p_input->stream.stream_lock );
106 ( s->p_input->stream.p_selected_area->i_size > 0 &&
107 s->p_input->stream.p_selected_area->i_size < i64 ) )
109 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
110 msg_Err( s, "seek out of bound" );
113 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
115 if( i64 == s->p_input->stream.p_selected_area->i_tell )
120 if( s->p_input->stream.b_seekable &&
121 ( s->p_input->stream.i_method == INPUT_METHOD_FILE ||
122 i64 - s->p_input->stream.p_selected_area->i_tell < 0 ||
123 i64 - s->p_input->stream.p_selected_area->i_tell > 1024 ) )
125 input_AccessReinit( s->p_input );
126 s->p_input->pf_seek( s->p_input, i64 );
130 if( i64 - s->p_input->stream.p_selected_area->i_tell > 0 )
132 data_packet_t *p_data;
133 int i_skip = i64 - s->p_input->stream.p_selected_area->i_tell;
135 msg_Warn( s, "will skip %d bytes, slow", i_skip );
141 i_read = input_SplitBuffer( s->p_input, &p_data, __MIN( 4096, i_skip ) );
148 input_DeletePacket( s->p_input->p_method_data, p_data );
149 if( i_read == 0 && i_skip > 0 )
159 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
164 int stream_Control( stream_t *s, int i_query, ... )
169 va_start( args, i_query );
170 i_result = stream_vaControl( s, i_query, args );
176 int stream_Read( stream_t *s, void *p_data, int i_data )
178 uint8_t *p = (uint8_t*)p_data;
179 data_packet_t *p_packet;
183 if( p_data == NULL && i_data > 0 )
187 stream_Control( s, STREAM_GET_POSITION, &i_pos );
190 if( stream_Control( s, STREAM_SET_POSITION, i_pos ) )
197 while( i_data > 0 && !s->p_input->b_die )
201 i_count = input_SplitBuffer( s->p_input, &p_packet, __MIN( i_data, 4096 ) );
209 memcpy( p, p_packet->p_payload_start, i_count );
213 input_DeletePacket( s->p_input->p_method_data, p_packet );
222 int stream_Peek( stream_t *s, uint8_t **pp_peek, int i_data )
224 return input_Peek( s->p_input, pp_peek, i_data );
228 pes_packet_t *stream_PesPacket( stream_t *s, int i_data )
231 data_packet_t *p_packet;
234 if( !(p_pes = input_NewPES( s->p_input->p_method_data ) ) )
243 input_NewPacket( s->p_input->p_method_data, 0 );
244 p_pes->i_nb_data = 1;
252 i_read = input_SplitBuffer( s->p_input, &p_packet, __MIN( i_data, 4096 ) );
255 /* should occur only with EOF and max allocation reached
256 * it safer to return an error */
258 input_DeletePES( s->p_input->p_method_data, p_pes );
262 if( p_pes->p_first == NULL )
264 p_pes->p_first = p_packet;
268 p_pes->p_last->p_next = p_packet;
270 p_pes->p_last = p_packet;
272 p_pes->i_pes_size += i_read;