include/ninput.c, src/input/stream.c:
[vlc.git] / src / input / stream.c
1 /*****************************************************************************
2  * stream.c
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: stream.c,v 1.2 2003/08/14 11:25:56 sigmunau 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 arguments.
74  */
75 int         stream_vaControl( stream_t *s, int i_query, va_list args )
76 {
77     vlc_bool_t *p_b;
78     int64_t    *p_i64, i64;
79
80     switch( i_query )
81     {
82         case STREAM_GET_SIZE:
83             p_i64 = (int64_t*) va_arg( args, int64_t * );
84
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 );
88             return VLC_SUCCESS;
89
90         case STREAM_CAN_SEEK:
91             p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
92
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 );
96             return VLC_SUCCESS;
97
98         case STREAM_CAN_FASTSEEK:
99             p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
100
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 );
104             return VLC_SUCCESS;
105
106         case STREAM_GET_POSITION:
107             p_i64 = (int64_t*) va_arg( args, int64_t * );
108
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 );
112             return VLC_SUCCESS;
113
114         case STREAM_SET_POSITION:
115             i64 = (int64_t) va_arg( args, int64_t );
116
117             vlc_mutex_lock( &s->p_input->stream.stream_lock );
118             if( i64 < 0 ||
119                 ( s->p_input->stream.p_selected_area->i_size > 0 &&
120                   s->p_input->stream.p_selected_area->i_size < i64 ) )
121             {
122                 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
123                 msg_Err( s, "seek out of bound" );
124                 return VLC_EGENERIC;
125             }
126             vlc_mutex_unlock( &s->p_input->stream.stream_lock );
127
128             if( i64 == s->p_input->stream.p_selected_area->i_tell )
129             {
130                 return VLC_SUCCESS;
131             }
132
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 > 1024 ) )
137             {
138                 input_AccessReinit( s->p_input );
139                 s->p_input->pf_seek( s->p_input, i64 );
140                 return VLC_SUCCESS;
141             }
142
143             if( i64 - s->p_input->stream.p_selected_area->i_tell > 0 )
144             {
145                 data_packet_t   *p_data;
146                 int             i_skip = i64 - s->p_input->stream.p_selected_area->i_tell;
147
148                 msg_Warn( s, "will skip %d bytes, slow", i_skip );
149
150                 while (i_skip > 0 )
151                 {
152                     int i_read;
153
154                     i_read = input_SplitBuffer( s->p_input, &p_data, __MIN( 4096, i_skip ) );
155                     if( i_read < 0 )
156                     {
157                         return VLC_EGENERIC;
158                     }
159                     i_skip -= i_read;
160
161                     input_DeletePacket( s->p_input->p_method_data, p_data );
162                     if( i_read == 0 && i_skip > 0 )
163                     {
164                         return VLC_EGENERIC;
165                     }
166                 }
167             }
168
169             return VLC_SUCCESS;
170
171         default:
172             msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
173             return VLC_EGENERIC;
174     }
175 }
176
177 /**
178  * Use to control the "stream_t *". Look at #stream_query_e for
179  * possible "i_query" value and format arguments.  Return VLC_SUCCESS
180  * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
181  */
182 int         stream_Control( stream_t *s, int i_query, ... )
183 {
184     va_list args;
185     int     i_result;
186
187     va_start( args, i_query );
188     i_result = stream_vaControl( s, i_query, args );
189     va_end( args );
190
191     return i_result;
192 }
193
194 /**
195  * Try to read "i_read" bytes into a buffer pointed by "p_read".  If
196  * "p_read" is NULL then data are skipped instead of read.  The return
197  * value is the real numbers of bytes read/skip. If this value is less
198  * than i_read that means that it's the end of the stream.
199  */
200 int         stream_Read( stream_t *s, void *p_data, int i_data )
201 {
202     uint8_t       *p = (uint8_t*)p_data;
203     data_packet_t *p_packet;
204
205     int i_read = 0;
206
207     if( p_data == NULL && i_data > 0 )
208     {
209         int64_t i_pos;
210
211         stream_Control( s, STREAM_GET_POSITION, &i_pos );
212
213         i_pos += i_data;
214         if( stream_Control( s, STREAM_SET_POSITION, i_pos ) )
215         {
216             return 0;
217         }
218         return i_data;
219     }
220
221     while( i_data > 0 && !s->p_input->b_die )
222     {
223         int i_count;
224
225         i_count = input_SplitBuffer( s->p_input, &p_packet, __MIN( i_data, 4096 ) );
226         if( i_count <= 0 )
227         {
228             return i_read;
229         }
230
231         if( p )
232         {
233             memcpy( p, p_packet->p_payload_start, i_count );
234             p += i_count;
235         }
236
237         input_DeletePacket( s->p_input->p_method_data, p_packet );
238
239         i_data -= i_count;
240         i_read += i_count;
241     }
242
243     return i_read;
244 }
245
246 /**
247  * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
248  * The return value is the real numbers of valid bytes, if it's less
249  * or equal to 0, *pp_peek is invalid.  XXX: it's a pointer to
250  * internal buffer and it will be invalid as soons as other stream_*
251  * functions are called.  be 0 (then *pp_peek isn't valid).  XXX: due
252  * to input limitation, it could be less than i_peek without meaning
253  * the end of the stream (but only when you have i_peek >=
254  * p_input->i_bufsize)
255  */
256 int         stream_Peek( stream_t *s, uint8_t **pp_peek, int i_data )
257 {
258     return input_Peek( s->p_input, pp_peek, i_data );
259 }
260
261 /**
262  * Read "i_size" bytes and store them in a pes_packet_t.  Only fields
263  * p_first, p_last, i_nb_data, and i_pes_size are set.  (Of course,
264  * you need to fill i_dts, i_pts, ... ) If only less than "i_size"
265  * bytes are available NULL is returned.
266  */
267 pes_packet_t    *stream_PesPacket( stream_t *s, int i_data )
268 {
269     pes_packet_t  *p_pes;
270     data_packet_t *p_packet;
271
272
273     if( !(p_pes = input_NewPES( s->p_input->p_method_data ) ) )
274     {
275         return NULL;
276     }
277
278     if( i_data <= 0 )
279     {
280         p_pes->p_first =
281             p_pes->p_last  =
282                 input_NewPacket( s->p_input->p_method_data, 0 );
283         p_pes->i_nb_data = 1;
284         return p_pes;
285     }
286
287     while( i_data > 0 )
288     {
289         int i_read;
290
291         i_read = input_SplitBuffer( s->p_input, &p_packet, __MIN( i_data, 4096 ) );
292         if( i_read <= 0 )
293         {
294             /* should occur only with EOF and max allocation reached 
295              * it safer to  return an error */
296             /* free pes */
297             input_DeletePES( s->p_input->p_method_data, p_pes );
298             return NULL;
299         }
300
301         if( p_pes->p_first == NULL )
302         {
303             p_pes->p_first = p_packet;
304         }
305         else
306         {
307             p_pes->p_last->p_next = p_packet;
308         }
309         p_pes->p_last = p_packet;
310         p_pes->i_nb_data++;
311         p_pes->i_pes_size += i_read;
312         i_data            -= i_read;
313     }
314
315     return p_pes;
316 }
317
318
319
320
321