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