Split stream_demux from input/demux.c
[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 typedef struct
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 } d_stream_sys_t;
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     d_stream_sys_t *p_sys;
63
64     s = stream_CommonNew( p_obj );
65     if( s == NULL )
66         return NULL;
67     s->pf_read   = DStreamRead;
68     s->pf_peek   = DStreamPeek;
69     s->pf_control= DStreamControl;
70     s->pf_destroy= DStreamDelete;
71
72     s->p_sys = malloc( sizeof( d_stream_sys_t) );
73     if( s->p_sys == NULL )
74     {
75         stream_CommonDelete( s );
76         return NULL;
77     }
78     p_sys = (d_stream_sys_t*)s->p_sys;
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, false ) )
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     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
110     if( p_block ) block_FifoPut( p_sys->p_fifo, p_block );
111 }
112
113 static void DStreamDelete( stream_t *s )
114 {
115     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
116     block_t *p_empty;
117
118     vlc_object_kill( s );
119     if( p_sys->p_demux )
120         vlc_object_kill( p_sys->p_demux );
121     p_empty = block_New( s, 1 ); p_empty->i_buffer = 0;
122     block_FifoPut( p_sys->p_fifo, p_empty );
123     vlc_thread_join( s );
124
125     if( p_sys->p_demux ) demux_Delete( p_sys->p_demux );
126     if( p_sys->p_block ) block_Release( p_sys->p_block );
127
128     block_FifoRelease( p_sys->p_fifo );
129     free( p_sys->psz_name );
130     free( p_sys );
131
132     stream_CommonDelete( s );
133 }
134
135
136 static int DStreamRead( stream_t *s, void *p_read, unsigned int i_read )
137 {
138     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
139     uint8_t *p_out = p_read;
140     int i_out = 0;
141
142     //msg_Dbg( s, "DStreamRead: wanted %d bytes", i_read );
143
144     while( !s->b_die && !s->b_error && i_read )
145     {
146         block_t *p_block = p_sys->p_block;
147         int i_copy;
148
149         if( !p_block )
150         {
151             p_block = block_FifoGet( p_sys->p_fifo );
152             if( !p_block ) s->b_error = 1;
153             p_sys->p_block = p_block;
154         }
155
156         if( p_block && i_read )
157         {
158             i_copy = __MIN( i_read, p_block->i_buffer );
159             if( p_out && i_copy ) memcpy( p_out, p_block->p_buffer, i_copy );
160             i_read -= i_copy;
161             i_out += i_copy;
162             p_block->i_buffer -= i_copy;
163             p_block->p_buffer += i_copy;
164
165             if( !p_block->i_buffer )
166             {
167                 block_Release( p_block );
168                 p_sys->p_block = NULL;
169             }
170         }
171     }
172
173     p_sys->i_pos += i_out;
174     return i_out;
175 }
176
177 static int DStreamPeek( stream_t *s, const uint8_t **pp_peek, unsigned int i_peek )
178 {
179     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
180     block_t **pp_block = &p_sys->p_block;
181     int i_out = 0;
182     *pp_peek = 0;
183
184     //msg_Dbg( s, "DStreamPeek: wanted %d bytes", i_peek );
185
186     while( !s->b_die && !s->b_error && i_peek )
187     {
188         int i_copy;
189
190         if( !*pp_block )
191         {
192             *pp_block = block_FifoGet( p_sys->p_fifo );
193             if( !*pp_block ) s->b_error = 1;
194         }
195
196         if( *pp_block && i_peek )
197         {
198             i_copy = __MIN( i_peek, (*pp_block)->i_buffer );
199             i_peek -= i_copy;
200             i_out += i_copy;
201
202             if( i_peek ) pp_block = &(*pp_block)->p_next;
203         }
204     }
205
206     if( p_sys->p_block )
207     {
208         p_sys->p_block = block_ChainGather( p_sys->p_block );
209         *pp_peek = p_sys->p_block->p_buffer;
210     }
211
212     return i_out;
213 }
214
215 static int DStreamControl( stream_t *s, int i_query, va_list args )
216 {
217     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
218     int64_t    *p_i64;
219     bool *p_b;
220     int        *p_int;
221
222     switch( i_query )
223     {
224         case STREAM_GET_SIZE:
225             p_i64 = (int64_t*) va_arg( args, int64_t * );
226             *p_i64 = 0;
227             return VLC_SUCCESS;
228
229         case STREAM_CAN_SEEK:
230             p_b = (bool*) va_arg( args, bool * );
231             *p_b = false;
232             return VLC_SUCCESS;
233
234         case STREAM_CAN_FASTSEEK:
235             p_b = (bool*) va_arg( args, bool * );
236             *p_b = false;
237             return VLC_SUCCESS;
238
239         case STREAM_GET_POSITION:
240             p_i64 = (int64_t*) va_arg( args, int64_t * );
241             *p_i64 = p_sys->i_pos;
242             return VLC_SUCCESS;
243
244         case STREAM_SET_POSITION:
245         {
246             int64_t i64 = (int64_t)va_arg( args, int64_t );
247             int i_skip;
248             if( i64 < p_sys->i_pos ) return VLC_EGENERIC;
249             i_skip = i64 - p_sys->i_pos;
250
251             while( i_skip > 0 )
252             {
253                 int i_read = DStreamRead( s, NULL, (long)i_skip );
254                 if( i_read <= 0 ) return VLC_EGENERIC;
255                 i_skip -= i_read;
256             }
257             return VLC_SUCCESS;
258         }
259
260         case STREAM_GET_MTU:
261             p_int = (int*) va_arg( args, int * );
262             *p_int = 0;
263             return VLC_SUCCESS;
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( vlc_object_t* p_this )
277 {
278     stream_t *s = (stream_t *)p_this;
279     d_stream_sys_t *p_sys = (d_stream_sys_t*)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, "", p_sys->psz_name, "", s, p_sys->out,
285                                false )) )
286     {
287         return NULL;
288     }
289
290     p_sys->p_demux = p_demux;
291
292     /* Main loop */
293     while( !s->b_die && !p_demux->b_die )
294     {
295         if( demux_Demux( p_demux ) <= 0 ) break;
296     }
297
298     vlc_restorecancel( canc );
299     vlc_object_kill( p_demux );
300     return NULL;
301 }
302