]> git.sesse.net Git - vlc/blob - src/input/stream.c
* all: added STREAM_GET_MTU and stream_MTU.
[vlc] / src / input / stream.c
1 /*****************************************************************************
2  * stream.c
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id: stream.c,v 1.11 2004/01/16 11:12:16 fenrir 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 #define MAX_LINE 1024
31 /****************************************************************************
32  * stream_* :
33  * XXX for now it's just a wrapper
34  *
35  ****************************************************************************/
36
37 /**
38  * Handle to a stream.
39  */
40 struct stream_t
41 {
42     VLC_COMMON_MEMBERS
43
44     /** pointer to the input thread */
45     input_thread_t *p_input;
46
47 };
48
49 /**
50  * Create a "stream_t *" from an "input_thread_t *".
51  */
52 stream_t *stream_OpenInput( input_thread_t *p_input )
53 {
54     stream_t *s;
55
56     s = vlc_object_create( p_input, sizeof( stream_t ) );
57     if( s )
58     {
59         s->p_input = p_input;
60     }
61
62     return s;
63 }
64
65 /**
66  * Destroy a previously created "stream_t *" instance.
67  */
68 void stream_Release( stream_t *s )
69 {
70     vlc_object_destroy( s );
71 }
72
73 /**
74  * Similar to #stream_Control(), but takes a va_list and not variable
75  * arguments.
76  */
77 int stream_vaControl( stream_t *s, int i_query, va_list args )
78 {
79     vlc_bool_t *p_b;
80     int64_t    *p_i64, i64;
81     int        *p_int;
82
83     switch( i_query )
84     {
85         case STREAM_GET_SIZE:
86             p_i64 = (int64_t*) va_arg( args, int64_t * );
87
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 );
91             return VLC_SUCCESS;
92
93         case STREAM_CAN_SEEK:
94             p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
95
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 );
99             return VLC_SUCCESS;
100
101         case STREAM_CAN_FASTSEEK:
102             p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
103
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 );
108             return VLC_SUCCESS;
109
110         case STREAM_GET_POSITION:
111             p_i64 = (int64_t*) va_arg( args, int64_t * );
112
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 );
116             return VLC_SUCCESS;
117
118         case STREAM_SET_POSITION:
119             i64 = (int64_t) va_arg( args, int64_t );
120
121             vlc_mutex_lock( &s->p_input->stream.stream_lock );
122             if( i64 < 0 ||
123                 ( s->p_input->stream.p_selected_area->i_size > 0 &&
124                   s->p_input->stream.p_selected_area->i_size < i64 ) )
125             {
126                 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
127                 msg_Warn( s, "seek out of bound" );
128                 return VLC_EGENERIC;
129             }
130             vlc_mutex_unlock( &s->p_input->stream.stream_lock );
131
132             if( i64 == s->p_input->stream.p_selected_area->i_tell )
133             {
134                 return VLC_SUCCESS;
135             }
136
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 ) )
141             {
142                 input_AccessReinit( s->p_input );
143                 s->p_input->pf_seek( s->p_input, i64 );
144                 return VLC_SUCCESS;
145             }
146
147             if( i64 - s->p_input->stream.p_selected_area->i_tell > 0 )
148             {
149                 data_packet_t *p_data;
150                 int i_skip = i64 - s->p_input->stream.p_selected_area->i_tell;
151
152                 if( i_skip > 1000 )
153                 {
154                     msg_Warn( s, "will skip %d bytes, slow", i_skip );
155                 }
156
157                 while (i_skip > 0 )
158                 {
159                     int i_read;
160
161                     i_read = input_SplitBuffer( s->p_input, &p_data,
162                                  __MIN( (int)s->p_input->i_bufsize, i_skip ) );
163                     if( i_read < 0 )
164                     {
165                         return VLC_EGENERIC;
166                     }
167                     i_skip -= i_read;
168
169                     input_DeletePacket( s->p_input->p_method_data, p_data );
170                     if( i_read == 0 && i_skip > 0 )
171                     {
172                         return VLC_EGENERIC;
173                     }
174                 }
175             }
176             return VLC_SUCCESS;
177
178         case STREAM_GET_MTU:
179             p_int = (int*) va_arg( args, int * );
180             *p_int = s->p_input->i_mtu;
181             return VLC_SUCCESS;
182
183         default:
184             msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
185             return VLC_EGENERIC;
186     }
187 }
188
189 /**
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
193  */
194 int stream_Control( stream_t *s, int i_query, ... )
195 {
196     va_list args;
197     int     i_result;
198
199     va_start( args, i_query );
200     i_result = stream_vaControl( s, i_query, args );
201     va_end( args );
202
203     return i_result;
204 }
205
206 /**
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.
211  */
212 int stream_Read( stream_t *s, void *p_data, int i_data )
213 {
214     uint8_t       *p = (uint8_t*)p_data;
215     data_packet_t *p_packet;
216
217     int i_read = 0;
218
219     if( p_data == NULL && i_data > 0 )
220     {
221         int64_t i_pos;
222
223         stream_Control( s, STREAM_GET_POSITION, &i_pos );
224
225         i_pos += i_data;
226         if( stream_Control( s, STREAM_SET_POSITION, i_pos ) )
227         {
228             return 0;
229         }
230         return i_data;
231     }
232
233     while( i_data > 0 && !s->p_input->b_die )
234     {
235         int i_count;
236
237         i_count = input_SplitBuffer( s->p_input, &p_packet,
238                       __MIN( i_data, (int)s->p_input->i_bufsize ) );
239
240         if( i_count <= 0 )
241         {
242             if( i_count == 0 )
243                 input_DeletePacket( s->p_input->p_method_data, p_packet );
244
245             return i_read;
246         }
247
248         if( p )
249         {
250             memcpy( p, p_packet->p_payload_start, i_count );
251             p += i_count;
252         }
253
254         input_DeletePacket( s->p_input->p_method_data, p_packet );
255
256         i_data -= i_count;
257         i_read += i_count;
258     }
259
260     return i_read;
261 }
262
263 /**
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)
272  */
273 int stream_Peek( stream_t *s, uint8_t **pp_peek, int i_peek )
274 {
275     return input_Peek( s->p_input, pp_peek, i_peek );
276 }
277
278 /**
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,
281  * return NULL.
282  */
283 block_t *stream_Block( stream_t *s, int i_size )
284 {
285     block_t *p_block;
286
287     if( i_size <= 0 ) return NULL;
288     if( !(p_block = block_New( s->p_input, i_size ) ) ) return NULL;
289
290     p_block->i_buffer = stream_Read( s, p_block->p_buffer, i_size );
291     if( !p_block->i_buffer )
292     {
293         block_Release( p_block );
294         p_block = NULL;
295     }
296
297     return p_block;
298 }
299
300 /**
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,
304  */
305 char *stream_ReadLine( stream_t *s )
306 {
307     uint8_t *p_data;
308     char    *p_line;
309     int      i_data;
310     int      i = 0;
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' )
314     {
315         i++;
316     }
317     if( i_data <= 0 )
318     {
319         return NULL;
320     }
321     else
322     {
323         p_line = malloc( i + 1 );
324         if( p_line == NULL )
325         {
326             msg_Err( s->p_input, "Out of memory" );
327             return NULL;
328         }
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 );
332         return p_line;
333     }
334 }
335
336 /**
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.
341  */
342 pes_packet_t *stream_PesPacket( stream_t *s, int i_data )
343 {
344     pes_packet_t  *p_pes;
345     data_packet_t *p_packet;
346
347
348     if( !(p_pes = input_NewPES( s->p_input->p_method_data ) ) )
349     {
350         return NULL;
351     }
352
353     if( i_data <= 0 )
354     {
355         p_pes->p_first =
356             p_pes->p_last = input_NewPacket( s->p_input->p_method_data, 0 );
357         p_pes->i_nb_data = 1;
358         return p_pes;
359     }
360
361     while( i_data > 0 )
362     {
363         int i_read;
364
365         i_read = input_SplitBuffer( s->p_input, &p_packet,
366                      __MIN( i_data, (int)s->p_input->i_bufsize ) );
367         if( i_read <= 0 )
368         {
369             /* should occur only with EOF and max allocation reached 
370              * it safer to  return an error */
371             /* free pes */
372             input_DeletePES( s->p_input->p_method_data, p_pes );
373             return NULL;
374         }
375
376         if( p_pes->p_first == NULL )
377         {
378             p_pes->p_first = p_packet;
379         }
380         else
381         {
382             p_pes->p_last->p_next = p_packet;
383         }
384         p_pes->p_last = p_packet;
385         p_pes->i_nb_data++;
386         p_pes->i_pes_size += i_read;
387         i_data -= i_read;
388     }
389
390     return p_pes;
391 }
392
393 /**
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
396  * doing.
397  */
398 data_packet_t *stream_DataPacket( stream_t *s, int i_size, vlc_bool_t b_force )
399 {
400     data_packet_t *p_pk;
401     int           i_read;
402
403     if( i_size <= 0 )
404     {
405         p_pk = input_NewPacket( s->p_input->p_method_data, 0 );
406         if( p_pk )
407         {
408             p_pk->p_payload_end = p_pk->p_payload_start;
409         }
410         return p_pk;
411     }
412
413     i_read = input_SplitBuffer( s->p_input, &p_pk, i_size );
414     if( i_read <= 0 )
415     {
416         return NULL;
417     }
418
419     /* Should be really rare, near 0 */
420     if( i_read < i_size && b_force )
421     {
422         data_packet_t *p_old = p_pk;
423         int           i_missing = i_size - i_read;
424
425         p_pk = input_NewPacket( s->p_input->p_method_data, i_size );
426         if( p_pk == NULL )
427         {
428             input_DeletePacket( s->p_input->p_method_data, p_old );
429             return NULL;
430         }
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 );
434
435         if( stream_Read( s, &p_pk->p_payload_start[i_read], i_missing )
436             < i_missing )
437         {
438             input_DeletePacket( s->p_input->p_method_data, p_pk );
439             return NULL;
440         }
441     }
442
443     return p_pk;
444 }