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