1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2008 the VideoLAN team
7 * Author: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
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.
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.
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 *****************************************************************************/
31 #include <vlc_codec.h>
33 /****************************************************************************
34 * stream_Demux*: create a demuxer for an outpout stream (allow demuxer chain)
35 ****************************************************************************/
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 * );
58 stream_t *stream_DemuxNew( demux_t *p_demux, const char *psz_demux, es_out_t *out )
60 vlc_object_t *p_obj = VLC_OBJECT(p_demux);
61 /* We create a stream reader, and launch a thread */
65 s = stream_CommonNew( p_obj );
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;
75 s->p_sys = p_sys = malloc( sizeof( *p_sys) );
76 if( !s->psz_path || !s->p_sys )
78 stream_CommonDelete( s );
84 p_sys->p_demux = NULL;
85 p_sys->p_block = NULL;
86 p_sys->psz_name = strdup( psz_demux );
89 if( ( p_sys->p_fifo = block_FifoNew() ) == NULL )
91 stream_CommonDelete( s );
92 free( p_sys->psz_name );
97 vlc_object_attach( s, p_obj );
99 if( vlc_thread_create( s, "stream out", DStreamThread,
100 VLC_THREAD_PRIORITY_INPUT ) )
102 vlc_object_detach( s );
103 stream_CommonDelete( s );
104 free( p_sys->psz_name );
112 void stream_DemuxSend( stream_t *s, block_t *p_block )
114 stream_sys_t *p_sys = s->p_sys;
116 block_FifoPut( p_sys->p_fifo, p_block );
119 static void DStreamDelete( stream_t *s )
121 stream_sys_t *p_sys = s->p_sys;
124 vlc_object_kill( s );
126 vlc_object_kill( p_sys->p_demux );
127 p_empty = block_New( s, 1 ); p_empty->i_buffer = 0;
128 block_FifoPut( p_sys->p_fifo, p_empty );
129 vlc_thread_join( s );
132 demux_Delete( p_sys->p_demux );
134 block_Release( p_sys->p_block );
136 block_FifoRelease( p_sys->p_fifo );
137 free( p_sys->psz_name );
139 vlc_object_detach( s );
140 stream_CommonDelete( s );
144 static int DStreamRead( stream_t *s, void *p_read, unsigned int i_read )
146 stream_sys_t *p_sys = s->p_sys;
147 uint8_t *p_out = p_read;
150 //msg_Dbg( s, "DStreamRead: wanted %d bytes", i_read );
152 while( !s->b_die && !s->b_error && i_read )
154 block_t *p_block = p_sys->p_block;
159 p_block = block_FifoGet( p_sys->p_fifo );
160 if( !p_block ) s->b_error = 1;
161 p_sys->p_block = p_block;
164 if( p_block && i_read )
166 i_copy = __MIN( i_read, p_block->i_buffer );
167 if( p_out && i_copy ) memcpy( p_out, p_block->p_buffer, i_copy );
171 p_block->i_buffer -= i_copy;
172 p_block->p_buffer += i_copy;
174 if( !p_block->i_buffer )
176 block_Release( p_block );
177 p_sys->p_block = NULL;
182 p_sys->i_pos += i_out;
186 static int DStreamPeek( stream_t *s, const uint8_t **pp_peek, unsigned int i_peek )
188 stream_sys_t *p_sys = s->p_sys;
189 block_t **pp_block = &p_sys->p_block;
193 //msg_Dbg( s, "DStreamPeek: wanted %d bytes", i_peek );
195 while( !s->b_die && !s->b_error && i_peek )
201 *pp_block = block_FifoGet( p_sys->p_fifo );
202 if( !*pp_block ) s->b_error = 1;
205 if( *pp_block && i_peek )
207 i_copy = __MIN( i_peek, (*pp_block)->i_buffer );
211 if( i_peek ) pp_block = &(*pp_block)->p_next;
217 p_sys->p_block = block_ChainGather( p_sys->p_block );
218 *pp_peek = p_sys->p_block->p_buffer;
224 static int DStreamControl( stream_t *s, int i_query, va_list args )
226 stream_sys_t *p_sys = s->p_sys;
232 case STREAM_GET_SIZE:
233 p_i64 = va_arg( args, uint64_t * );
237 case STREAM_CAN_SEEK:
238 p_b = (bool*) va_arg( args, bool * );
242 case STREAM_CAN_FASTSEEK:
243 p_b = (bool*) va_arg( args, bool * );
247 case STREAM_GET_POSITION:
248 p_i64 = va_arg( args, uint64_t * );
249 *p_i64 = p_sys->i_pos;
252 case STREAM_SET_POSITION:
254 uint64_t i64 = va_arg( args, uint64_t );
255 if( i64 < p_sys->i_pos )
258 uint64_t i_skip = i64 - p_sys->i_pos;
261 int i_read = DStreamRead( s, NULL, __MIN(i_skip, INT_MAX) );
269 case STREAM_CONTROL_ACCESS:
270 case STREAM_GET_CONTENT_TYPE:
271 case STREAM_SET_RECORD_STATE:
275 msg_Err( s, "invalid DStreamControl query=0x%x", i_query );
280 static void* DStreamThread( vlc_object_t* p_this )
282 stream_t *s = (stream_t *)p_this;
283 stream_sys_t *p_sys = s->p_sys;
285 int canc = vlc_savecancel();
287 /* Create the demuxer */
288 if( !(p_demux = demux_New( s, s->p_input, "", p_sys->psz_name, "", s, p_sys->out,
294 /* stream_Demux cannot apply DVB filters.
295 * Get all programs and let the E/S output sort them out. */
296 demux_Control( p_demux, DEMUX_SET_GROUP, -1, NULL );
297 p_sys->p_demux = p_demux;
300 while( !s->b_die && !p_demux->b_die )
302 if( demux_Demux( p_demux ) <= 0 ) break;
305 vlc_restorecancel( canc );
306 vlc_object_kill( p_demux );