1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
5 * $Id: stream.c,v 1.13 2004/01/25 17:16:06 zorglub 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>
31 /****************************************************************************
33 * XXX for now it's just a wrapper
35 ****************************************************************************/
44 /** pointer to the input thread */
45 input_thread_t *p_input;
50 * Create a "stream_t *" from an "input_thread_t *".
52 stream_t *stream_OpenInput( input_thread_t *p_input )
56 s = vlc_object_create( p_input, sizeof( stream_t ) );
66 * Destroy a previously created "stream_t *" instance.
68 void stream_Release( stream_t *s )
70 vlc_object_destroy( s );
74 * Similar to #stream_Control(), but takes a va_list and not variable
77 int stream_vaControl( stream_t *s, int i_query, va_list args )
86 p_i64 = (int64_t*) va_arg( args, int64_t * );
88 vlc_mutex_lock( &s->p_input->stream.stream_lock );
89 *p_i64 = s->p_input->stream.p_selected_area->i_size;
90 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
94 p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
96 vlc_mutex_lock( &s->p_input->stream.stream_lock );
97 *p_b = s->p_input->stream.b_seekable;
98 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
101 case STREAM_CAN_FASTSEEK:
102 p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
104 vlc_mutex_lock( &s->p_input->stream.stream_lock );
105 *p_b = s->p_input->stream.b_seekable &&
106 s->p_input->stream.i_method == INPUT_METHOD_FILE;
107 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
110 case STREAM_GET_POSITION:
111 p_i64 = (int64_t*) va_arg( args, int64_t * );
113 vlc_mutex_lock( &s->p_input->stream.stream_lock );
114 *p_i64 = s->p_input->stream.p_selected_area->i_tell;
115 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
118 case STREAM_SET_POSITION:
120 i64 = (int64_t) va_arg( args, int64_t );
123 vlc_mutex_lock( &s->p_input->stream.stream_lock );
125 ( s->p_input->stream.p_selected_area->i_size > 0 &&
126 s->p_input->stream.p_selected_area->i_size < i64 ) )
128 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
129 msg_Warn( s, "seek out of bound" );
133 i_skip = i64 - s->p_input->stream.p_selected_area->i_tell;
137 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
141 if( i_skip > 0 && i_skip < s->p_input->p_last_data -
142 s->p_input->p_current_data - 1 )
144 /* We can skip without reading/seeking */
145 s->p_input->p_current_data += i_skip;
146 s->p_input->stream.p_selected_area->i_tell = i64;
147 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
150 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
152 if( s->p_input->stream.b_seekable &&
153 ( s->p_input->stream.i_method == INPUT_METHOD_FILE ||
154 i_skip < 0 || i_skip >= ( s->p_input->i_mtu > 0 ?
155 s->p_input->i_mtu : 4096 ) ) )
157 input_AccessReinit( s->p_input );
158 s->p_input->pf_seek( s->p_input, i64 );
164 data_packet_t *p_data;
168 msg_Warn( s, "will skip "I64Fd" bytes, slow", i_skip );
175 i_read = input_SplitBuffer( s->p_input, &p_data,
176 __MIN( (int)s->p_input->i_bufsize, i_skip ) );
183 input_DeletePacket( s->p_input->p_method_data, p_data );
184 if( i_read == 0 && i_skip > 0 )
194 p_int = (int*) va_arg( args, int * );
195 *p_int = s->p_input->i_mtu;
199 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
205 * Use to control the "stream_t *". Look at #stream_query_e for
206 * possible "i_query" value and format arguments. Return VLC_SUCCESS
207 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
209 int stream_Control( stream_t *s, int i_query, ... )
214 va_start( args, i_query );
215 i_result = stream_vaControl( s, i_query, args );
222 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
223 * "p_read" is NULL then data are skipped instead of read. The return
224 * value is the real numbers of bytes read/skip. If this value is less
225 * than i_read that means that it's the end of the stream.
227 int stream_Read( stream_t *s, void *p_data, int i_data )
229 uint8_t *p = (uint8_t*)p_data;
230 data_packet_t *p_packet;
234 if( p_data == NULL && i_data > 0 )
238 stream_Control( s, STREAM_GET_POSITION, &i_pos );
241 if( stream_Control( s, STREAM_SET_POSITION, i_pos ) )
248 while( i_data > 0 && !s->p_input->b_die )
252 i_count = input_SplitBuffer( s->p_input, &p_packet,
253 __MIN( i_data, (int)s->p_input->i_bufsize ) );
258 input_DeletePacket( s->p_input->p_method_data, p_packet );
265 memcpy( p, p_packet->p_payload_start, i_count );
269 input_DeletePacket( s->p_input->p_method_data, p_packet );
279 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
280 * \return The real numbers of valid bytes, if it's less
281 * or equal to 0, *pp_peek is invalid.
282 * \note pp_peek is a pointer to internal buffer and it will be invalid as
283 * soons as other stream_* functions are called.
284 * \note Due to input limitation, it could be less than i_peek without meaning
285 * the end of the stream (but only when you have i_peek >=
286 * p_input->i_bufsize)
288 int stream_Peek( stream_t *s, uint8_t **pp_peek, int i_peek )
290 return input_Peek( s->p_input, pp_peek, i_peek );
294 * Read "i_size" bytes and store them in a block_t. If less than "i_size"
295 * bytes are available then return what is left and if nothing is availble,
298 block_t *stream_Block( stream_t *s, int i_size )
302 if( i_size <= 0 ) return NULL;
303 if( !(p_block = block_New( s->p_input, i_size ) ) ) return NULL;
305 p_block->i_buffer = stream_Read( s, p_block->p_buffer, i_size );
306 if( !p_block->i_buffer )
308 block_Release( p_block );
316 * Read from the stream untill first newline.
317 * \param s Stream handle to read from
318 * \return A null-terminated string. This must be freed,
320 char *stream_ReadLine( stream_t *s )
326 i_data = stream_Peek( s, &p_data, MAX_LINE );
327 msg_Dbg( s->p_input, "i_data %d", i_data );
328 while( i < i_data && p_data[i] != '\n' )
338 p_line = malloc( i + 1 );
341 msg_Err( s->p_input, "out of memory" );
344 i = stream_Read( s, p_line, i + 1 );
345 p_line[ i - 1 ] = '\0';
346 msg_Dbg( s->p_input, "found %d chars long line", i );