08ac2314f6d963420dcd72ab670c1f7dcf98f0ff
[vlc.git] / src / input / stream.c
1 /*****************************************************************************
2  * stream.c
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: stream.c,v 1.1 2003/08/01 00:00:12 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 /****************************************************************************
31  * stream_* :
32  * XXX for now it's just a wrapper
33  *
34  ****************************************************************************/
35
36 struct stream_t
37 {
38     VLC_COMMON_MEMBERS
39
40     input_thread_t *p_input;
41
42 };
43
44 stream_t    *stream_OpenInput( input_thread_t *p_input )
45 {
46     stream_t *s;
47
48     s = vlc_object_create( p_input, sizeof( stream_t ) );
49     if( s )
50     {
51         s->p_input = p_input;
52     }
53
54     return s;
55 }
56
57 void        stream_Release( stream_t *s )
58 {
59     vlc_object_destroy( s );
60 }
61
62 int         stream_vaControl( stream_t *s, int i_query, va_list args )
63 {
64     vlc_bool_t *p_b;
65     int64_t    *p_i64, i64;
66
67     switch( i_query )
68     {
69         case STREAM_GET_SIZE:
70             p_i64 = (int64_t*) va_arg( args, int64_t * );
71
72             vlc_mutex_lock( &s->p_input->stream.stream_lock );
73             *p_i64 = s->p_input->stream.p_selected_area->i_size;
74             vlc_mutex_unlock( &s->p_input->stream.stream_lock );
75             return VLC_SUCCESS;
76
77         case STREAM_CAN_SEEK:
78             p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
79
80             vlc_mutex_lock( &s->p_input->stream.stream_lock );
81             *p_b = s->p_input->stream.b_seekable;
82             vlc_mutex_unlock( &s->p_input->stream.stream_lock );
83             return VLC_SUCCESS;
84
85         case STREAM_CAN_FASTSEEK:
86             p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
87
88             vlc_mutex_lock( &s->p_input->stream.stream_lock );
89             *p_b = s->p_input->stream.b_seekable && s->p_input->stream.i_method == INPUT_METHOD_FILE;
90             vlc_mutex_unlock( &s->p_input->stream.stream_lock );
91             return VLC_SUCCESS;
92
93         case STREAM_GET_POSITION:
94             p_i64 = (int64_t*) va_arg( args, int64_t * );
95
96             vlc_mutex_lock( &s->p_input->stream.stream_lock );
97             *p_i64 = s->p_input->stream.p_selected_area->i_tell;
98             vlc_mutex_unlock( &s->p_input->stream.stream_lock );
99             return VLC_SUCCESS;
100
101         case STREAM_SET_POSITION:
102             i64 = (int64_t) va_arg( args, int64_t );
103
104             vlc_mutex_lock( &s->p_input->stream.stream_lock );
105             if( i64 < 0 ||
106                 ( s->p_input->stream.p_selected_area->i_size > 0 &&
107                   s->p_input->stream.p_selected_area->i_size < i64 ) )
108             {
109                 vlc_mutex_unlock( &s->p_input->stream.stream_lock );
110                 msg_Err( s, "seek out of bound" );
111                 return VLC_EGENERIC;
112             }
113             vlc_mutex_unlock( &s->p_input->stream.stream_lock );
114
115             if( i64 == s->p_input->stream.p_selected_area->i_tell )
116             {
117                 return VLC_SUCCESS;
118             }
119
120             if( s->p_input->stream.b_seekable &&
121                 ( s->p_input->stream.i_method == INPUT_METHOD_FILE ||
122                   i64 - s->p_input->stream.p_selected_area->i_tell < 0 ||
123                   i64 - s->p_input->stream.p_selected_area->i_tell > 1024 ) )
124             {
125                 input_AccessReinit( s->p_input );
126                 s->p_input->pf_seek( s->p_input, i64 );
127                 return VLC_SUCCESS;
128             }
129
130             if( i64 - s->p_input->stream.p_selected_area->i_tell > 0 )
131             {
132                 data_packet_t   *p_data;
133                 int             i_skip = i64 - s->p_input->stream.p_selected_area->i_tell;
134
135                 msg_Warn( s, "will skip %d bytes, slow", i_skip );
136
137                 while (i_skip > 0 )
138                 {
139                     int i_read;
140
141                     i_read = input_SplitBuffer( s->p_input, &p_data, __MIN( 4096, i_skip ) );
142                     if( i_read < 0 )
143                     {
144                         return VLC_EGENERIC;
145                     }
146                     i_skip -= i_read;
147
148                     input_DeletePacket( s->p_input->p_method_data, p_data );
149                     if( i_read == 0 && i_skip > 0 )
150                     {
151                         return VLC_EGENERIC;
152                     }
153                 }
154             }
155
156             return VLC_SUCCESS;
157
158         default:
159             msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
160             return VLC_EGENERIC;
161     }
162 }
163
164 int         stream_Control( stream_t *s, int i_query, ... )
165 {
166     va_list args;
167     int     i_result;
168
169     va_start( args, i_query );
170     i_result = stream_vaControl( s, i_query, args );
171     va_end( args );
172
173     return i_result;
174 }
175
176 int         stream_Read( stream_t *s, void *p_data, int i_data )
177 {
178     uint8_t       *p = (uint8_t*)p_data;
179     data_packet_t *p_packet;
180
181     int i_read = 0;
182
183     if( p_data == NULL && i_data > 0 )
184     {
185         int64_t i_pos;
186
187         stream_Control( s, STREAM_GET_POSITION, &i_pos );
188
189         i_pos += i_data;
190         if( stream_Control( s, STREAM_SET_POSITION, i_pos ) )
191         {
192             return 0;
193         }
194         return i_data;
195     }
196
197     while( i_data > 0 && !s->p_input->b_die )
198     {
199         int i_count;
200
201         i_count = input_SplitBuffer( s->p_input, &p_packet, __MIN( i_data, 4096 ) );
202         if( i_count <= 0 )
203         {
204             return i_read;
205         }
206
207         if( p )
208         {
209             memcpy( p, p_packet->p_payload_start, i_count );
210             p += i_count;
211         }
212
213         input_DeletePacket( s->p_input->p_method_data, p_packet );
214
215         i_data -= i_count;
216         i_read += i_count;
217     }
218
219     return i_read;
220 }
221
222 int         stream_Peek( stream_t *s, uint8_t **pp_peek, int i_data )
223 {
224     return input_Peek( s->p_input, pp_peek, i_data );
225 }
226
227
228 pes_packet_t    *stream_PesPacket( stream_t *s, int i_data )
229 {
230     pes_packet_t  *p_pes;
231     data_packet_t *p_packet;
232
233
234     if( !(p_pes = input_NewPES( s->p_input->p_method_data ) ) )
235     {
236         return NULL;
237     }
238
239     if( i_data <= 0 )
240     {
241         p_pes->p_first =
242             p_pes->p_last  =
243                 input_NewPacket( s->p_input->p_method_data, 0 );
244         p_pes->i_nb_data = 1;
245         return p_pes;
246     }
247
248     while( i_data > 0 )
249     {
250         int i_read;
251
252         i_read = input_SplitBuffer( s->p_input, &p_packet, __MIN( i_data, 4096 ) );
253         if( i_read <= 0 )
254         {
255             /* should occur only with EOF and max allocation reached 
256              * it safer to  return an error */
257             /* free pes */
258             input_DeletePES( s->p_input->p_method_data, p_pes );
259             return NULL;
260         }
261
262         if( p_pes->p_first == NULL )
263         {
264             p_pes->p_first = p_packet;
265         }
266         else
267         {
268             p_pes->p_last->p_next = p_packet;
269         }
270         p_pes->p_last = p_packet;
271         p_pes->i_nb_data++;
272         p_pes->i_pes_size += i_read;
273         i_data            -= i_read;
274     }
275
276     return p_pes;
277 }
278
279
280
281
282