1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
5 * $Id: stream.c,v 1.11 2004/01/16 11:12:16 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>
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:
119 i64 = (int64_t) va_arg( args, int64_t );
121 vlc_mutex_lock( &s->p_input->stream.stream_lock );
123 ( s->p_input->stream.p_selected_area->i_size > 0 &&
124 s->p_input->stream.p_selected_area->i_size < i64 ) )
126 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
127 msg_Warn( s, "seek out of bound" );
130 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
132 if( i64 == s->p_input->stream.p_selected_area->i_tell )
137 if( s->p_input->stream.b_seekable &&
138 ( s->p_input->stream.i_method == INPUT_METHOD_FILE ||
139 i64 - s->p_input->stream.p_selected_area->i_tell < 0 ||
140 i64 - s->p_input->stream.p_selected_area->i_tell > 4096 ) )
142 input_AccessReinit( s->p_input );
143 s->p_input->pf_seek( s->p_input, i64 );
147 if( i64 - s->p_input->stream.p_selected_area->i_tell > 0 )
149 data_packet_t *p_data;
150 int i_skip = i64 - s->p_input->stream.p_selected_area->i_tell;
154 msg_Warn( s, "will skip %d bytes, slow", i_skip );
161 i_read = input_SplitBuffer( s->p_input, &p_data,
162 __MIN( (int)s->p_input->i_bufsize, i_skip ) );
169 input_DeletePacket( s->p_input->p_method_data, p_data );
170 if( i_read == 0 && i_skip > 0 )
179 p_int = (int*) va_arg( args, int * );
180 *p_int = s->p_input->i_mtu;
184 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
190 * Use to control the "stream_t *". Look at #stream_query_e for
191 * possible "i_query" value and format arguments. Return VLC_SUCCESS
192 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
194 int stream_Control( stream_t *s, int i_query, ... )
199 va_start( args, i_query );
200 i_result = stream_vaControl( s, i_query, args );
207 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
208 * "p_read" is NULL then data are skipped instead of read. The return
209 * value is the real numbers of bytes read/skip. If this value is less
210 * than i_read that means that it's the end of the stream.
212 int stream_Read( stream_t *s, void *p_data, int i_data )
214 uint8_t *p = (uint8_t*)p_data;
215 data_packet_t *p_packet;
219 if( p_data == NULL && i_data > 0 )
223 stream_Control( s, STREAM_GET_POSITION, &i_pos );
226 if( stream_Control( s, STREAM_SET_POSITION, i_pos ) )
233 while( i_data > 0 && !s->p_input->b_die )
237 i_count = input_SplitBuffer( s->p_input, &p_packet,
238 __MIN( i_data, (int)s->p_input->i_bufsize ) );
243 input_DeletePacket( s->p_input->p_method_data, p_packet );
250 memcpy( p, p_packet->p_payload_start, i_count );
254 input_DeletePacket( s->p_input->p_method_data, p_packet );
264 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
265 * \return The real numbers of valid bytes, if it's less
266 * or equal to 0, *pp_peek is invalid.
267 * \note pp_peek is a pointer to internal buffer and it will be invalid as
268 * soons as other stream_* functions are called.
269 * \note Due to input limitation, it could be less than i_peek without meaning
270 * the end of the stream (but only when you have i_peek >=
271 * p_input->i_bufsize)
273 int stream_Peek( stream_t *s, uint8_t **pp_peek, int i_peek )
275 return input_Peek( s->p_input, pp_peek, i_peek );
279 * Read "i_size" bytes and store them in a block_t. If less than "i_size"
280 * bytes are available then return what is left and if nothing is availble,
283 block_t *stream_Block( stream_t *s, int i_size )
287 if( i_size <= 0 ) return NULL;
288 if( !(p_block = block_New( s->p_input, i_size ) ) ) return NULL;
290 p_block->i_buffer = stream_Read( s, p_block->p_buffer, i_size );
291 if( !p_block->i_buffer )
293 block_Release( p_block );
301 * Read from the stream untill first newline.
302 * \param s Stream handle to read from
303 * \return A null-terminated string. This must be freed,
305 char *stream_ReadLine( stream_t *s )
311 i_data = stream_Peek( s, &p_data, MAX_LINE );
312 msg_Dbg( s->p_input, "i_data %d", i_data );
313 while( i < i_data && p_data[i] != '\n' )
323 p_line = malloc( i + 1 );
326 msg_Err( s->p_input, "Out of memory" );
329 i = stream_Read( s, p_line, i + 1 );
330 p_line[ i - 1 ] = '\0';
331 msg_Dbg( s->p_input, "found %d chars long line", i );
337 * Read "i_size" bytes and store them in a pes_packet_t. Only fields
338 * p_first, p_last, i_nb_data, and i_pes_size are set. (Of course,
339 * you need to fill i_dts, i_pts, ... ) If only less than "i_size"
340 * bytes are available NULL is returned.
342 pes_packet_t *stream_PesPacket( stream_t *s, int i_data )
345 data_packet_t *p_packet;
348 if( !(p_pes = input_NewPES( s->p_input->p_method_data ) ) )
356 p_pes->p_last = input_NewPacket( s->p_input->p_method_data, 0 );
357 p_pes->i_nb_data = 1;
365 i_read = input_SplitBuffer( s->p_input, &p_packet,
366 __MIN( i_data, (int)s->p_input->i_bufsize ) );
369 /* should occur only with EOF and max allocation reached
370 * it safer to return an error */
372 input_DeletePES( s->p_input->p_method_data, p_pes );
376 if( p_pes->p_first == NULL )
378 p_pes->p_first = p_packet;
382 p_pes->p_last->p_next = p_packet;
384 p_pes->p_last = p_packet;
386 p_pes->i_pes_size += i_read;
394 * Read i_size into a data_packet_t. If b_force is not set, fewer bytes can
395 * be returned. You should always set b_force, unless you know what you are
398 data_packet_t *stream_DataPacket( stream_t *s, int i_size, vlc_bool_t b_force )
405 p_pk = input_NewPacket( s->p_input->p_method_data, 0 );
408 p_pk->p_payload_end = p_pk->p_payload_start;
413 i_read = input_SplitBuffer( s->p_input, &p_pk, i_size );
419 /* Should be really rare, near 0 */
420 if( i_read < i_size && b_force )
422 data_packet_t *p_old = p_pk;
423 int i_missing = i_size - i_read;
425 p_pk = input_NewPacket( s->p_input->p_method_data, i_size );
428 input_DeletePacket( s->p_input->p_method_data, p_old );
431 p_pk->p_payload_end = p_pk->p_payload_start + i_size;
432 memcpy( p_pk->p_payload_start, p_old->p_payload_start, i_read );
433 input_DeletePacket( s->p_input->p_method_data, p_old );
435 if( stream_Read( s, &p_pk->p_payload_start[i_read], i_missing )
438 input_DeletePacket( s->p_input->p_method_data, p_pk );