]> git.sesse.net Git - vlc/blob - src/input/stream.c
2f888fcd52f82efe21c2975d5957db20438a9475
[vlc] / src / input / stream.c
1 /*****************************************************************************
2  * stream.c
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: stream.c,v 1.8 2004/01/03 00:23:04 gbazin Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include <stdlib.h>
25 #include <vlc/vlc.h>
26 #include <vlc/input.h>
27
28 #include "ninput.h"
29
30 /****************************************************************************
31  * stream_* :
32  * XXX for now it's just a wrapper
33  *
34  ****************************************************************************/
35
36 /**
37  * Handle to a stream.
38  */
39 struct stream_t
40 {
41     VLC_COMMON_MEMBERS
42
43     /** pointer to the input thread */
44     input_thread_t *p_input;
45
46 };
47
48 /**
49  * Create a "stream_t *" from an "input_thread_t *".
50  */
51 stream_t *stream_OpenInput( input_thread_t *p_input )
52 {
53     stream_t *s;
54
55     s = vlc_object_create( p_input, sizeof( stream_t ) );
56     if( s )
57     {
58         s->p_input = p_input;
59     }
60
61     return s;
62 }
63
64 /**
65  * Destroy a previously created "stream_t *" instance.
66  */
67 void stream_Release( stream_t *s )
68 {
69     vlc_object_destroy( s );
70 }
71
72 /**
73  * Similar to #stream_Control(), but takes a va_list and not variable
74  * arguments.
75  */
76 int stream_vaControl( stream_t *s, int i_query, va_list args )
77 {
78     vlc_bool_t *p_b;
79     int64_t    *p_i64, i64;
80
81     switch( i_query )
82     {
83         case STREAM_GET_SIZE:
84             p_i64 = (int64_t*) va_arg( args, int64_t * );
85
86             vlc_mutex_lock( &s->p_input->stream.stream_lock );
87             *p_i64 = s->p_input->stream.p_selected_area->i_size;
88             vlc_mutex_unlock( &s->p_input->stream.stream_lock );
89             return VLC_SUCCESS;
90
91         case STREAM_CAN_SEEK:
92             p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
93
94             vlc_mutex_lock( &s->p_input->stream.stream_lock );
95             *p_b = s->p_input->stream.b_seekable;
96             vlc_mutex_unlock( &s->p_input->stream.stream_lock );
97             return VLC_SUCCESS;
98
99         case STREAM_CAN_FASTSEEK:
100             p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
101
102             vlc_mutex_lock( &s->p_input->stream.stream_lock );
103             *p_b = s->p_input->stream.b_seekable &&
104                    s->p_input->stream.i_method == INPUT_METHOD_FILE;
105             vlc_mutex_unlock( &s->p_input->stream.stream_lock );
106             return VLC_SUCCESS;
107
108         case STREAM_GET_POSITION:
109             p_i64 = (int64_t*) va_arg( args, int64_t * );
110
111             vlc_mutex_lock( &s->p_input->stream.stream_lock );
112             *p_i64 = s->p_input->stream.p_selected_area->i_tell;
113             vlc_mutex_unlock( &s->p_input->stream.stream_lock );
114             return VLC_SUCCESS;
115
116         case STREAM_SET_POSITION:
117             i64 = (int64_t) va_arg( args, int64_t );
118
119             vlc_mutex_lock( &s->p_input->stream.stream_lock );
120             if( i64 < 0 ||
121                 ( s->p_input->stream.p_selected_area->i_size > 0 &&
122                   s->p_input->stream.p_selected_area->i_size < i64 ) )
123             {
124                 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
125                 msg_Warn( s, "seek out of bound" );
126                 return VLC_EGENERIC;
127             }
128             vlc_mutex_unlock( &s->p_input->stream.stream_lock );
129
130             if( i64 == s->p_input->stream.p_selected_area->i_tell )
131             {
132                 return VLC_SUCCESS;
133             }
134
135             if( s->p_input->stream.b_seekable &&
136                 ( s->p_input->stream.i_method == INPUT_METHOD_FILE ||
137                   i64 - s->p_input->stream.p_selected_area->i_tell < 0 ||
138                   i64 - s->p_input->stream.p_selected_area->i_tell > 4096 ) )
139             {
140                 input_AccessReinit( s->p_input );
141                 s->p_input->pf_seek( s->p_input, i64 );
142                 return VLC_SUCCESS;
143             }
144
145             if( i64 - s->p_input->stream.p_selected_area->i_tell > 0 )
146             {
147                 data_packet_t *p_data;
148                 int i_skip = i64 - s->p_input->stream.p_selected_area->i_tell;
149
150                 if( i_skip > 1000 )
151                 {
152                     msg_Warn( s, "will skip %d bytes, slow", i_skip );
153                 }
154
155                 while (i_skip > 0 )
156                 {
157                     int i_read;
158
159                     i_read = input_SplitBuffer( s->p_input, &p_data,
160                                  __MIN( (int)s->p_input->i_bufsize, i_skip ) );
161                     if( i_read < 0 )
162                     {
163                         return VLC_EGENERIC;
164                     }
165                     i_skip -= i_read;
166
167                     input_DeletePacket( s->p_input->p_method_data, p_data );
168                     if( i_read == 0 && i_skip > 0 )
169                     {
170                         return VLC_EGENERIC;
171                     }
172                 }
173             }
174
175             return VLC_SUCCESS;
176
177         default:
178             msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
179             return VLC_EGENERIC;
180     }
181 }
182
183 /**
184  * Use to control the "stream_t *". Look at #stream_query_e for
185  * possible "i_query" value and format arguments.  Return VLC_SUCCESS
186  * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
187  */
188 int stream_Control( stream_t *s, int i_query, ... )
189 {
190     va_list args;
191     int     i_result;
192
193     va_start( args, i_query );
194     i_result = stream_vaControl( s, i_query, args );
195     va_end( args );
196
197     return i_result;
198 }
199
200 /**
201  * Try to read "i_read" bytes into a buffer pointed by "p_read".  If
202  * "p_read" is NULL then data are skipped instead of read.  The return
203  * value is the real numbers of bytes read/skip. If this value is less
204  * than i_read that means that it's the end of the stream.
205  */
206 int stream_Read( stream_t *s, void *p_data, int i_data )
207 {
208     uint8_t       *p = (uint8_t*)p_data;
209     data_packet_t *p_packet;
210
211     int i_read = 0;
212
213     if( p_data == NULL && i_data > 0 )
214     {
215         int64_t i_pos;
216
217         stream_Control( s, STREAM_GET_POSITION, &i_pos );
218
219         i_pos += i_data;
220         if( stream_Control( s, STREAM_SET_POSITION, i_pos ) )
221         {
222             return 0;
223         }
224         return i_data;
225     }
226
227     while( i_data > 0 && !s->p_input->b_die )
228     {
229         int i_count;
230
231         i_count = input_SplitBuffer( s->p_input, &p_packet,
232                       __MIN( i_data, (int)s->p_input->i_bufsize ) );
233
234         if( i_count <= 0 )
235         {
236             if( i_count == 0 )
237                 input_DeletePacket( s->p_input->p_method_data, p_packet );
238
239             return i_read;
240         }
241
242         if( p )
243         {
244             memcpy( p, p_packet->p_payload_start, i_count );
245             p += i_count;
246         }
247
248         input_DeletePacket( s->p_input->p_method_data, p_packet );
249
250         i_data -= i_count;
251         i_read += i_count;
252     }
253
254     return i_read;
255 }
256
257 /**
258  * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
259  * The return value is the real numbers of valid bytes, if it's less
260  * or equal to 0, *pp_peek is invalid.  XXX: it's a pointer to
261  * internal buffer and it will be invalid as soons as other stream_*
262  * functions are called.  be 0 (then *pp_peek isn't valid).  XXX: due
263  * to input limitation, it could be less than i_peek without meaning
264  * the end of the stream (but only when you have i_peek >=
265  * p_input->i_bufsize)
266  */
267 int stream_Peek( stream_t *s, uint8_t **pp_peek, int i_data )
268 {
269     return input_Peek( s->p_input, pp_peek, i_data );
270 }
271
272 /**
273  * Read "i_size" bytes and store them in a block_t. If less than "i_size"
274  * bytes are available then return what is left and if nothing is availble,
275  * return NULL.
276  */
277 block_t *stream_Block( stream_t *s, int i_size )
278 {
279     block_t *p_block;
280
281     if( i_size <= 0 ) return NULL;
282     if( !(p_block = block_New( s->p_input, i_size ) ) ) return NULL;
283
284     p_block->i_buffer = stream_Read( s, p_block->p_buffer, i_size );
285     if( !p_block->i_buffer )
286     {
287         block_Release( p_block );
288         p_block = NULL;
289     }
290
291     return p_block;
292 }
293
294 /**
295  * Read "i_size" bytes and store them in a pes_packet_t.  Only fields
296  * p_first, p_last, i_nb_data, and i_pes_size are set.  (Of course,
297  * you need to fill i_dts, i_pts, ... ) If only less than "i_size"
298  * bytes are available NULL is returned.
299  */
300 pes_packet_t *stream_PesPacket( stream_t *s, int i_data )
301 {
302     pes_packet_t  *p_pes;
303     data_packet_t *p_packet;
304
305
306     if( !(p_pes = input_NewPES( s->p_input->p_method_data ) ) )
307     {
308         return NULL;
309     }
310
311     if( i_data <= 0 )
312     {
313         p_pes->p_first =
314             p_pes->p_last = input_NewPacket( s->p_input->p_method_data, 0 );
315         p_pes->i_nb_data = 1;
316         return p_pes;
317     }
318
319     while( i_data > 0 )
320     {
321         int i_read;
322
323         i_read = input_SplitBuffer( s->p_input, &p_packet,
324                      __MIN( i_data, (int)s->p_input->i_bufsize ) );
325         if( i_read <= 0 )
326         {
327             /* should occur only with EOF and max allocation reached 
328              * it safer to  return an error */
329             /* free pes */
330             input_DeletePES( s->p_input->p_method_data, p_pes );
331             return NULL;
332         }
333
334         if( p_pes->p_first == NULL )
335         {
336             p_pes->p_first = p_packet;
337         }
338         else
339         {
340             p_pes->p_last->p_next = p_packet;
341         }
342         p_pes->p_last = p_packet;
343         p_pes->i_nb_data++;
344         p_pes->i_pes_size += i_read;
345         i_data -= i_read;
346     }
347
348     return p_pes;
349 }
350
351 /**
352  * Read i_size into a data_packet_t. If b_force is not set, fewer bytes can
353  * be returned. You should always set b_force, unless you know what you are
354  * doing.
355  */
356 data_packet_t *stream_DataPacket( stream_t *s, int i_size, vlc_bool_t b_force )
357 {
358     data_packet_t *p_pk;
359     int           i_read;
360
361     if( i_size <= 0 )
362     {
363         p_pk = input_NewPacket( s->p_input->p_method_data, 0 );
364         if( p_pk )
365         {
366             p_pk->p_payload_end = p_pk->p_payload_start;
367         }
368         return p_pk;
369     }
370
371     i_read = input_SplitBuffer( s->p_input, &p_pk, i_size );
372     if( i_read <= 0 )
373     {
374         return NULL;
375     }
376
377     /* Should be really rare, near 0 */
378     if( i_read < i_size && b_force )
379     {
380         data_packet_t *p_old = p_pk;
381         int           i_missing = i_size - i_read;
382
383         p_pk = input_NewPacket( s->p_input->p_method_data, i_size );
384         if( p_pk == NULL )
385         {
386             input_DeletePacket( s->p_input->p_method_data, p_old );
387             return NULL;
388         }
389         p_pk->p_payload_end = p_pk->p_payload_start + i_size;
390         memcpy( p_pk->p_payload_start, p_old->p_payload_start, i_read );
391         input_DeletePacket( s->p_input->p_method_data, p_old );
392
393         if( stream_Read( s, &p_pk->p_payload_start[i_read], i_missing )
394             < i_missing )
395         {
396             input_DeletePacket( s->p_input->p_method_data, p_pk );
397             return NULL;
398         }
399     }
400
401     return p_pk;
402 }