1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: stream.c,v 1.5 2003/09/08 00:36:26 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 ****************************************************************************/
43 /** pointer to the input thread */
44 input_thread_t *p_input;
49 * Create a "stream_t *" from an "input_thread_t *".
51 stream_t *stream_OpenInput( input_thread_t *p_input )
55 s = vlc_object_create( p_input, sizeof( stream_t ) );
65 * Destroy a previously created "stream_t *" instance.
67 void stream_Release( stream_t *s )
69 vlc_object_destroy( s );
73 * Similar to #stream_Control(), but takes a va_list and not variable arguments.
75 int stream_vaControl( stream_t *s, int i_query, va_list args )
83 p_i64 = (int64_t*) va_arg( args, int64_t * );
85 vlc_mutex_lock( &s->p_input->stream.stream_lock );
86 *p_i64 = s->p_input->stream.p_selected_area->i_size;
87 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
91 p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
93 vlc_mutex_lock( &s->p_input->stream.stream_lock );
94 *p_b = s->p_input->stream.b_seekable;
95 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
98 case STREAM_CAN_FASTSEEK:
99 p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
101 vlc_mutex_lock( &s->p_input->stream.stream_lock );
102 *p_b = s->p_input->stream.b_seekable && s->p_input->stream.i_method == INPUT_METHOD_FILE;
103 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
106 case STREAM_GET_POSITION:
107 p_i64 = (int64_t*) va_arg( args, int64_t * );
109 vlc_mutex_lock( &s->p_input->stream.stream_lock );
110 *p_i64 = s->p_input->stream.p_selected_area->i_tell;
111 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
114 case STREAM_SET_POSITION:
115 i64 = (int64_t) va_arg( args, int64_t );
117 vlc_mutex_lock( &s->p_input->stream.stream_lock );
119 ( s->p_input->stream.p_selected_area->i_size > 0 &&
120 s->p_input->stream.p_selected_area->i_size < i64 ) )
122 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
123 msg_Warn( s, "seek out of bound" );
126 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
128 if( i64 == s->p_input->stream.p_selected_area->i_tell )
133 if( s->p_input->stream.b_seekable &&
134 ( s->p_input->stream.i_method == INPUT_METHOD_FILE ||
135 i64 - s->p_input->stream.p_selected_area->i_tell < 0 ||
136 i64 - s->p_input->stream.p_selected_area->i_tell > 4096 ) )
138 input_AccessReinit( s->p_input );
139 s->p_input->pf_seek( s->p_input, i64 );
143 if( i64 - s->p_input->stream.p_selected_area->i_tell > 0 )
145 data_packet_t *p_data;
146 int i_skip = i64 - s->p_input->stream.p_selected_area->i_tell;
150 msg_Warn( s, "will skip %d bytes, slow", i_skip );
157 i_read = input_SplitBuffer( s->p_input, &p_data,
158 __MIN( (int)s->p_input->i_bufsize, i_skip ) );
165 input_DeletePacket( s->p_input->p_method_data, p_data );
166 if( i_read == 0 && i_skip > 0 )
176 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
182 * Use to control the "stream_t *". Look at #stream_query_e for
183 * possible "i_query" value and format arguments. Return VLC_SUCCESS
184 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
186 int stream_Control( stream_t *s, int i_query, ... )
191 va_start( args, i_query );
192 i_result = stream_vaControl( s, i_query, args );
199 * Try to read "i_read" bytes into a buffer pointed by "p_read". If
200 * "p_read" is NULL then data are skipped instead of read. The return
201 * value is the real numbers of bytes read/skip. If this value is less
202 * than i_read that means that it's the end of the stream.
204 int stream_Read( stream_t *s, void *p_data, int i_data )
206 uint8_t *p = (uint8_t*)p_data;
207 data_packet_t *p_packet;
211 if( p_data == NULL && i_data > 0 )
215 stream_Control( s, STREAM_GET_POSITION, &i_pos );
218 if( stream_Control( s, STREAM_SET_POSITION, i_pos ) )
225 while( i_data > 0 && !s->p_input->b_die )
229 i_count = input_SplitBuffer( s->p_input, &p_packet,
230 __MIN( i_data, (int)s->p_input->i_bufsize ) );
238 memcpy( p, p_packet->p_payload_start, i_count );
242 input_DeletePacket( s->p_input->p_method_data, p_packet );
252 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
253 * The return value is the real numbers of valid bytes, if it's less
254 * or equal to 0, *pp_peek is invalid. XXX: it's a pointer to
255 * internal buffer and it will be invalid as soons as other stream_*
256 * functions are called. be 0 (then *pp_peek isn't valid). XXX: due
257 * to input limitation, it could be less than i_peek without meaning
258 * the end of the stream (but only when you have i_peek >=
259 * p_input->i_bufsize)
261 int stream_Peek( stream_t *s, uint8_t **pp_peek, int i_data )
263 return input_Peek( s->p_input, pp_peek, i_data );
267 * Read "i_size" bytes and store them in a pes_packet_t. Only fields
268 * p_first, p_last, i_nb_data, and i_pes_size are set. (Of course,
269 * you need to fill i_dts, i_pts, ... ) If only less than "i_size"
270 * bytes are available NULL is returned.
272 pes_packet_t *stream_PesPacket( stream_t *s, int i_data )
275 data_packet_t *p_packet;
278 if( !(p_pes = input_NewPES( s->p_input->p_method_data ) ) )
287 input_NewPacket( s->p_input->p_method_data, 0 );
288 p_pes->i_nb_data = 1;
296 i_read = input_SplitBuffer( s->p_input, &p_packet,
297 __MIN( i_data, (int)s->p_input->i_bufsize ) );
300 /* should occur only with EOF and max allocation reached
301 * it safer to return an error */
303 input_DeletePES( s->p_input->p_method_data, p_pes );
307 if( p_pes->p_first == NULL )
309 p_pes->p_first = p_packet;
313 p_pes->p_last->p_next = p_packet;
315 p_pes->p_last = p_packet;
317 p_pes->i_pes_size += i_read;
325 * Read i_size into a data_packet_t. If b_force is not set, fewer bytes can
326 * be returned. You should always set b_force, unless you know what you are
329 data_packet_t *stream_DataPacket( stream_t *s, int i_size, vlc_bool_t b_force )
336 p_pk = input_NewPacket( s->p_input->p_method_data, 0 );
339 p_pk->p_payload_end = p_pk->p_payload_start;
344 i_read = input_SplitBuffer( s->p_input, &p_pk, i_size );
350 /* Should be really rare, near 0 */
351 if( i_read < i_size && b_force )
353 data_packet_t *p_old = p_pk;
354 int i_missing = i_size - i_read;
356 p_pk = input_NewPacket( s->p_input->p_method_data, i_size );
359 input_DeletePacket( s->p_input->p_method_data, p_old );
362 p_pk->p_payload_end = p_pk->p_payload_start + i_size;
363 memcpy( p_pk->p_payload_start, p_old->p_payload_start, i_read );
364 input_DeletePacket( s->p_input->p_method_data, p_old );
366 if( stream_Read( s, &p_pk->p_payload_start[i_read], i_missing ) < i_missing )
368 input_DeletePacket( s->p_input->p_method_data, p_pk );