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 *****************************************************************************/
30 #include <vlc_codec.h>
32 /****************************************************************************
33 * stream_Demux*: create a demuxer for an outpout stream (allow demuxer chain)
34 ****************************************************************************/
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 * );
57 stream_t *__stream_DemuxNew( vlc_object_t *p_obj, const char *psz_demux,
60 /* We create a stream reader, and launch a thread */
64 s = stream_CommonNew( p_obj );
67 s->pf_read = DStreamRead;
68 s->pf_peek = DStreamPeek;
69 s->pf_control= DStreamControl;
70 s->pf_destroy= DStreamDelete;
72 s->p_sys = p_sys = malloc( sizeof( *p_sys) );
73 if( s->p_sys == NULL )
75 stream_CommonDelete( s );
81 p_sys->p_demux = NULL;
82 p_sys->p_block = NULL;
83 p_sys->psz_name = strdup( psz_demux );
86 if( ( p_sys->p_fifo = block_FifoNew() ) == NULL )
88 stream_CommonDelete( s );
89 free( p_sys->psz_name );
94 if( vlc_thread_create( s, "stream out", DStreamThread,
95 VLC_THREAD_PRIORITY_INPUT, false ) )
97 stream_CommonDelete( s );
98 free( p_sys->psz_name );
106 void stream_DemuxSend( stream_t *s, block_t *p_block )
108 stream_sys_t *p_sys = s->p_sys;
110 block_FifoPut( p_sys->p_fifo, p_block );
113 static void DStreamDelete( stream_t *s )
115 stream_sys_t *p_sys = s->p_sys;
118 vlc_object_kill( s );
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 );
126 demux_Delete( p_sys->p_demux );
128 block_Release( p_sys->p_block );
130 block_FifoRelease( p_sys->p_fifo );
131 free( p_sys->psz_name );
134 stream_CommonDelete( s );
138 static int DStreamRead( stream_t *s, void *p_read, unsigned int i_read )
140 stream_sys_t *p_sys = s->p_sys;
141 uint8_t *p_out = p_read;
144 //msg_Dbg( s, "DStreamRead: wanted %d bytes", i_read );
146 while( !s->b_die && !s->b_error && i_read )
148 block_t *p_block = p_sys->p_block;
153 p_block = block_FifoGet( p_sys->p_fifo );
154 if( !p_block ) s->b_error = 1;
155 p_sys->p_block = p_block;
158 if( p_block && i_read )
160 i_copy = __MIN( i_read, p_block->i_buffer );
161 if( p_out && i_copy ) memcpy( p_out, p_block->p_buffer, i_copy );
164 p_block->i_buffer -= i_copy;
165 p_block->p_buffer += i_copy;
167 if( !p_block->i_buffer )
169 block_Release( p_block );
170 p_sys->p_block = NULL;
175 p_sys->i_pos += i_out;
179 static int DStreamPeek( stream_t *s, const uint8_t **pp_peek, unsigned int i_peek )
181 stream_sys_t *p_sys = s->p_sys;
182 block_t **pp_block = &p_sys->p_block;
186 //msg_Dbg( s, "DStreamPeek: wanted %d bytes", i_peek );
188 while( !s->b_die && !s->b_error && i_peek )
194 *pp_block = block_FifoGet( p_sys->p_fifo );
195 if( !*pp_block ) s->b_error = 1;
198 if( *pp_block && i_peek )
200 i_copy = __MIN( i_peek, (*pp_block)->i_buffer );
204 if( i_peek ) pp_block = &(*pp_block)->p_next;
210 p_sys->p_block = block_ChainGather( p_sys->p_block );
211 *pp_peek = p_sys->p_block->p_buffer;
217 static int DStreamControl( stream_t *s, int i_query, va_list args )
219 stream_sys_t *p_sys = s->p_sys;
226 case STREAM_GET_SIZE:
227 p_i64 = (int64_t*) va_arg( args, int64_t * );
231 case STREAM_CAN_SEEK:
232 p_b = (bool*) va_arg( args, bool * );
236 case STREAM_CAN_FASTSEEK:
237 p_b = (bool*) va_arg( args, bool * );
241 case STREAM_GET_POSITION:
242 p_i64 = (int64_t*) va_arg( args, int64_t * );
243 *p_i64 = p_sys->i_pos;
246 case STREAM_SET_POSITION:
248 int64_t i64 = (int64_t)va_arg( args, int64_t );
250 if( i64 < p_sys->i_pos ) return VLC_EGENERIC;
251 i_skip = i64 - p_sys->i_pos;
255 int i_read = DStreamRead( s, NULL, (long)i_skip );
256 if( i_read <= 0 ) return VLC_EGENERIC;
263 p_int = (int*) va_arg( args, int * );
267 case STREAM_CONTROL_ACCESS:
268 case STREAM_GET_CONTENT_TYPE:
269 case STREAM_SET_RECORD_STATE:
273 msg_Err( s, "invalid DStreamControl query=0x%x", i_query );
278 static void* DStreamThread( vlc_object_t* p_this )
280 stream_t *s = (stream_t *)p_this;
281 stream_sys_t *p_sys = s->p_sys;
283 int canc = vlc_savecancel();
285 /* Create the demuxer */
286 if( !(p_demux = demux_New( s, "", p_sys->psz_name, "", s, p_sys->out,
292 p_sys->p_demux = p_demux;
295 while( !s->b_die && !p_demux->b_die )
297 if( demux_Demux( p_demux ) <= 0 ) break;
300 vlc_restorecancel( canc );
301 vlc_object_kill( p_demux );