Added path information in stream_t.
[vlc.git] / src / input / stream_demux.c
1 /*****************************************************************************
2  * stream_demux.c
3  *****************************************************************************
4  * Copyright (C) 1999-2008 the VideoLAN team
5  * $Id$
6  *
7  * Author: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "demux.h"
29 #include <libvlc.h>
30 #include <vlc_codec.h>
31
32 /****************************************************************************
33  * stream_Demux*: create a demuxer for an outpout stream (allow demuxer chain)
34  ****************************************************************************/
35 struct stream_sys_t
36 {
37     /* Data buffer */
38     block_fifo_t *p_fifo;
39     block_t      *p_block;
40
41     int64_t     i_pos;
42
43     /* Demuxer */
44     char        *psz_name;
45     es_out_t    *out;
46     demux_t     *p_demux;
47
48 };
49
50 static int  DStreamRead   ( stream_t *, void *p_read, unsigned int i_read );
51 static int  DStreamPeek   ( stream_t *, const uint8_t **pp_peek, unsigned int i_peek );
52 static int  DStreamControl( stream_t *, int i_query, va_list );
53 static void DStreamDelete ( stream_t * );
54 static void* DStreamThread ( vlc_object_t * );
55
56
57 stream_t *__stream_DemuxNew( vlc_object_t *p_obj, const char *psz_demux,
58                              es_out_t *out )
59 {
60     /* We create a stream reader, and launch a thread */
61     stream_t     *s;
62     stream_sys_t *p_sys;
63
64     s = stream_CommonNew( p_obj );
65     if( s == NULL )
66         return NULL;
67     s->psz_path  = strdup(""); /* N/A */
68     s->pf_read   = DStreamRead;
69     s->pf_peek   = DStreamPeek;
70     s->pf_control= DStreamControl;
71     s->pf_destroy= DStreamDelete;
72
73     s->p_sys = p_sys = malloc( sizeof( *p_sys) );
74     if( !s->psz_path || !s->p_sys )
75     {
76         stream_CommonDelete( s );
77         return NULL;
78     }
79
80     p_sys->i_pos = 0;
81     p_sys->out = out;
82     p_sys->p_demux = NULL;
83     p_sys->p_block = NULL;
84     p_sys->psz_name = strdup( psz_demux );
85
86     /* decoder fifo */
87     if( ( p_sys->p_fifo = block_FifoNew() ) == NULL )
88     {
89         stream_CommonDelete( s );
90         free( p_sys->psz_name );
91         free( p_sys );
92         return NULL;
93     }
94
95     if( vlc_thread_create( s, "stream out", DStreamThread,
96                            VLC_THREAD_PRIORITY_INPUT ) )
97     {
98         stream_CommonDelete( s );
99         free( p_sys->psz_name );
100         free( p_sys );
101         return NULL;
102     }
103
104     return s;
105 }
106
107 void stream_DemuxSend( stream_t *s, block_t *p_block )
108 {
109     stream_sys_t *p_sys = s->p_sys;
110     if( p_block )
111         block_FifoPut( p_sys->p_fifo, p_block );
112 }
113
114 static void DStreamDelete( stream_t *s )
115 {
116     stream_sys_t *p_sys = s->p_sys;
117     block_t *p_empty;
118
119     vlc_object_kill( s );
120     if( p_sys->p_demux )
121         vlc_object_kill( p_sys->p_demux );
122     p_empty = block_New( s, 1 ); p_empty->i_buffer = 0;
123     block_FifoPut( p_sys->p_fifo, p_empty );
124     vlc_thread_join( s );
125
126     if( p_sys->p_demux )
127         demux_Delete( p_sys->p_demux );
128     if( p_sys->p_block )
129         block_Release( p_sys->p_block );
130
131     block_FifoRelease( p_sys->p_fifo );
132     free( p_sys->psz_name );
133     free( p_sys );
134
135     stream_CommonDelete( s );
136 }
137
138
139 static int DStreamRead( stream_t *s, void *p_read, unsigned int i_read )
140 {
141     stream_sys_t *p_sys = s->p_sys;
142     uint8_t *p_out = p_read;
143     int i_out = 0;
144
145     //msg_Dbg( s, "DStreamRead: wanted %d bytes", i_read );
146
147     while( !s->b_die && !s->b_error && i_read )
148     {
149         block_t *p_block = p_sys->p_block;
150         int i_copy;
151
152         if( !p_block )
153         {
154             p_block = block_FifoGet( p_sys->p_fifo );
155             if( !p_block ) s->b_error = 1;
156             p_sys->p_block = p_block;
157         }
158
159         if( p_block && i_read )
160         {
161             i_copy = __MIN( i_read, p_block->i_buffer );
162             if( p_out && i_copy ) memcpy( p_out, p_block->p_buffer, i_copy );
163             i_read -= i_copy;
164             i_out += i_copy;
165             p_block->i_buffer -= i_copy;
166             p_block->p_buffer += i_copy;
167
168             if( !p_block->i_buffer )
169             {
170                 block_Release( p_block );
171                 p_sys->p_block = NULL;
172             }
173         }
174     }
175
176     p_sys->i_pos += i_out;
177     return i_out;
178 }
179
180 static int DStreamPeek( stream_t *s, const uint8_t **pp_peek, unsigned int i_peek )
181 {
182     stream_sys_t *p_sys = s->p_sys;
183     block_t **pp_block = &p_sys->p_block;
184     int i_out = 0;
185     *pp_peek = 0;
186
187     //msg_Dbg( s, "DStreamPeek: wanted %d bytes", i_peek );
188
189     while( !s->b_die && !s->b_error && i_peek )
190     {
191         int i_copy;
192
193         if( !*pp_block )
194         {
195             *pp_block = block_FifoGet( p_sys->p_fifo );
196             if( !*pp_block ) s->b_error = 1;
197         }
198
199         if( *pp_block && i_peek )
200         {
201             i_copy = __MIN( i_peek, (*pp_block)->i_buffer );
202             i_peek -= i_copy;
203             i_out += i_copy;
204
205             if( i_peek ) pp_block = &(*pp_block)->p_next;
206         }
207     }
208
209     if( p_sys->p_block )
210     {
211         p_sys->p_block = block_ChainGather( p_sys->p_block );
212         *pp_peek = p_sys->p_block->p_buffer;
213     }
214
215     return i_out;
216 }
217
218 static int DStreamControl( stream_t *s, int i_query, va_list args )
219 {
220     stream_sys_t *p_sys = s->p_sys;
221     int64_t    *p_i64;
222     bool *p_b;
223
224     switch( i_query )
225     {
226         case STREAM_GET_SIZE:
227             p_i64 = (int64_t*) va_arg( args, int64_t * );
228             *p_i64 = 0;
229             return VLC_SUCCESS;
230
231         case STREAM_CAN_SEEK:
232             p_b = (bool*) va_arg( args, bool * );
233             *p_b = false;
234             return VLC_SUCCESS;
235
236         case STREAM_CAN_FASTSEEK:
237             p_b = (bool*) va_arg( args, bool * );
238             *p_b = false;
239             return VLC_SUCCESS;
240
241         case STREAM_GET_POSITION:
242             p_i64 = (int64_t*) va_arg( args, int64_t * );
243             *p_i64 = p_sys->i_pos;
244             return VLC_SUCCESS;
245
246         case STREAM_SET_POSITION:
247         {
248             int64_t i64 = (int64_t)va_arg( args, int64_t );
249             int i_skip;
250             if( i64 < p_sys->i_pos ) return VLC_EGENERIC;
251             i_skip = i64 - p_sys->i_pos;
252
253             while( i_skip > 0 )
254             {
255                 int i_read = DStreamRead( s, NULL, (long)i_skip );
256                 if( i_read <= 0 ) return VLC_EGENERIC;
257                 i_skip -= i_read;
258             }
259             return VLC_SUCCESS;
260         }
261
262         case STREAM_CONTROL_ACCESS:
263         case STREAM_GET_CONTENT_TYPE:
264         case STREAM_SET_RECORD_STATE:
265             return VLC_EGENERIC;
266
267         default:
268             msg_Err( s, "invalid DStreamControl query=0x%x", i_query );
269             return VLC_EGENERIC;
270     }
271 }
272
273 static void* DStreamThread( vlc_object_t* p_this )
274 {
275     stream_t *s = (stream_t *)p_this;
276     stream_sys_t *p_sys = s->p_sys;
277     demux_t *p_demux;
278     int canc = vlc_savecancel();
279
280     /* Create the demuxer */
281     if( !(p_demux = demux_New( s, "", p_sys->psz_name, "", s, p_sys->out,
282                                false )) )
283     {
284         return NULL;
285     }
286
287     p_sys->p_demux = p_demux;
288
289     /* Main loop */
290     while( !s->b_die && !p_demux->b_die )
291     {
292         if( demux_Demux( p_demux ) <= 0 ) break;
293     }
294
295     vlc_restorecancel( canc );
296     vlc_object_kill( p_demux );
297     return NULL;
298 }
299