* String review, round one
[vlc.git] / src / input / stream.c
1 /*****************************************************************************
2  * stream.c
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id: stream.c,v 1.13 2004/01/25 17:16:06 zorglub 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         {
120             i64 = (int64_t) va_arg( args, int64_t );
121             int64_t i_skip;
122
123             vlc_mutex_lock( &s->p_input->stream.stream_lock );
124             if( i64 < 0 ||
125                 ( s->p_input->stream.p_selected_area->i_size > 0 &&
126                   s->p_input->stream.p_selected_area->i_size < i64 ) )
127             {
128                 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
129                 msg_Warn( s, "seek out of bound" );
130                 return VLC_EGENERIC;
131             }
132
133             i_skip = i64 - s->p_input->stream.p_selected_area->i_tell;
134
135             if( i_skip == 0 )
136             {
137                 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
138                 return VLC_SUCCESS;
139             }
140
141             if( i_skip > 0 && i_skip < s->p_input->p_last_data -
142                             s->p_input->p_current_data - 1 )
143             {
144                 /* We can skip without reading/seeking */
145                 s->p_input->p_current_data += i_skip;
146                 s->p_input->stream.p_selected_area->i_tell = i64;
147                 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
148                 return VLC_SUCCESS;
149             }
150             vlc_mutex_unlock( &s->p_input->stream.stream_lock );
151
152             if( s->p_input->stream.b_seekable &&
153                 ( s->p_input->stream.i_method == INPUT_METHOD_FILE ||
154                   i_skip < 0 || i_skip >= ( s->p_input->i_mtu > 0 ?
155                                             s->p_input->i_mtu : 4096 ) ) )
156             {
157                 input_AccessReinit( s->p_input );
158                 s->p_input->pf_seek( s->p_input, i64 );
159                 return VLC_SUCCESS;
160             }
161
162             if( i_skip > 0 )
163             {
164                 data_packet_t *p_data;
165
166                 if( i_skip > 1000 )
167                 {
168                     msg_Warn( s, "will skip "I64Fd" bytes, slow", i_skip );
169                 }
170
171                 while( i_skip > 0 )
172                 {
173                     int i_read;
174
175                     i_read = input_SplitBuffer( s->p_input, &p_data,
176                                  __MIN( (int)s->p_input->i_bufsize, i_skip ) );
177                     if( i_read < 0 )
178                     {
179                         return VLC_EGENERIC;
180                     }
181                     i_skip -= i_read;
182
183                     input_DeletePacket( s->p_input->p_method_data, p_data );
184                     if( i_read == 0 && i_skip > 0 )
185                     {
186                         return VLC_EGENERIC;
187                     }
188                 }
189             }
190             return VLC_SUCCESS;
191         }
192
193         case STREAM_GET_MTU:
194             p_int = (int*) va_arg( args, int * );
195             *p_int = s->p_input->i_mtu;
196             return VLC_SUCCESS;
197
198         default:
199             msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
200             return VLC_EGENERIC;
201     }
202 }
203
204 /**
205  * Use to control the "stream_t *". Look at #stream_query_e for
206  * possible "i_query" value and format arguments.  Return VLC_SUCCESS
207  * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
208  */
209 int stream_Control( stream_t *s, int i_query, ... )
210 {
211     va_list args;
212     int     i_result;
213
214     va_start( args, i_query );
215     i_result = stream_vaControl( s, i_query, args );
216     va_end( args );
217
218     return i_result;
219 }
220
221 /**
222  * Try to read "i_read" bytes into a buffer pointed by "p_read".  If
223  * "p_read" is NULL then data are skipped instead of read.  The return
224  * value is the real numbers of bytes read/skip. If this value is less
225  * than i_read that means that it's the end of the stream.
226  */
227 int stream_Read( stream_t *s, void *p_data, int i_data )
228 {
229     uint8_t       *p = (uint8_t*)p_data;
230     data_packet_t *p_packet;
231
232     int i_read = 0;
233
234     if( p_data == NULL && i_data > 0 )
235     {
236         int64_t i_pos;
237
238         stream_Control( s, STREAM_GET_POSITION, &i_pos );
239
240         i_pos += i_data;
241         if( stream_Control( s, STREAM_SET_POSITION, i_pos ) )
242         {
243             return 0;
244         }
245         return i_data;
246     }
247
248     while( i_data > 0 && !s->p_input->b_die )
249     {
250         int i_count;
251
252         i_count = input_SplitBuffer( s->p_input, &p_packet,
253                       __MIN( i_data, (int)s->p_input->i_bufsize ) );
254
255         if( i_count <= 0 )
256         {
257             if( i_count == 0 )
258                 input_DeletePacket( s->p_input->p_method_data, p_packet );
259
260             return i_read;
261         }
262
263         if( p )
264         {
265             memcpy( p, p_packet->p_payload_start, i_count );
266             p += i_count;
267         }
268
269         input_DeletePacket( s->p_input->p_method_data, p_packet );
270
271         i_data -= i_count;
272         i_read += i_count;
273     }
274
275     return i_read;
276 }
277
278 /**
279  * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
280  * \return The real numbers of valid bytes, if it's less
281  * or equal to 0, *pp_peek is invalid.
282  * \note pp_peek is a pointer to internal buffer and it will be invalid as
283  * soons as other stream_* functions are called.
284  * \note Due to input limitation, it could be less than i_peek without meaning
285  * the end of the stream (but only when you have i_peek >=
286  * p_input->i_bufsize)
287  */
288 int stream_Peek( stream_t *s, uint8_t **pp_peek, int i_peek )
289 {
290     return input_Peek( s->p_input, pp_peek, i_peek );
291 }
292
293 /**
294  * Read "i_size" bytes and store them in a block_t. If less than "i_size"
295  * bytes are available then return what is left and if nothing is availble,
296  * return NULL.
297  */
298 block_t *stream_Block( stream_t *s, int i_size )
299 {
300     block_t *p_block;
301
302     if( i_size <= 0 ) return NULL;
303     if( !(p_block = block_New( s->p_input, i_size ) ) ) return NULL;
304
305     p_block->i_buffer = stream_Read( s, p_block->p_buffer, i_size );
306     if( !p_block->i_buffer )
307     {
308         block_Release( p_block );
309         p_block = NULL;
310     }
311
312     return p_block;
313 }
314
315 /**
316  * Read from the stream untill first newline.
317  * \param s Stream handle to read from
318  * \return A null-terminated string. This must be freed,
319  */
320 char *stream_ReadLine( stream_t *s )
321 {
322     uint8_t *p_data;
323     char    *p_line;
324     int      i_data;
325     int      i = 0;
326     i_data = stream_Peek( s, &p_data, MAX_LINE );
327     msg_Dbg( s->p_input, "i_data %d", i_data );
328     while( i < i_data && p_data[i] != '\n' )
329     {
330         i++;
331     }
332     if( i_data <= 0 )
333     {
334         return NULL;
335     }
336     else
337     {
338         p_line = malloc( i + 1 );
339         if( p_line == NULL )
340         {
341             msg_Err( s->p_input, "out of memory" );
342             return NULL;
343         }
344         i = stream_Read( s, p_line, i + 1 );
345         p_line[ i - 1 ] = '\0';
346         msg_Dbg( s->p_input, "found %d chars long line", i );
347         return p_line;
348     }
349 }