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