1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2008 VLC authors and VideoLAN
7 * Author: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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 ****************************************************************************/
58 static int DStreamRead ( stream_t *, void *p_read, unsigned int i_read );
59 static int DStreamPeek ( stream_t *, const uint8_t **pp_peek, unsigned int i_peek );
60 static int DStreamControl( stream_t *, int i_query, va_list );
61 static void DStreamDelete ( stream_t * );
62 static void* DStreamThread ( void * );
65 stream_t *stream_DemuxNew( demux_t *p_demux, const char *psz_demux, es_out_t *out )
67 vlc_object_t *p_obj = VLC_OBJECT(p_demux);
68 /* We create a stream reader, and launch a thread */
72 s = stream_CommonNew( p_obj );
75 s->p_input = p_demux->p_input;
76 s->psz_path = strdup(""); /* N/A */
77 s->pf_read = DStreamRead;
78 s->pf_peek = DStreamPeek;
79 s->pf_control= DStreamControl;
80 s->pf_destroy= DStreamDelete;
82 s->p_sys = p_sys = malloc( sizeof( *p_sys) );
83 if( !s->psz_path || !s->p_sys )
85 stream_CommonDelete( s );
91 p_sys->p_block = NULL;
92 p_sys->psz_name = strdup( psz_demux );
93 p_sys->stats.position = 0.;
94 p_sys->stats.length = 0;
95 p_sys->stats.time = 0;
98 if( ( p_sys->p_fifo = block_FifoNew() ) == NULL )
100 stream_CommonDelete( s );
101 free( p_sys->psz_name );
106 vlc_mutex_init( &p_sys->lock );
108 if( vlc_clone( &p_sys->thread, DStreamThread, s, VLC_THREAD_PRIORITY_INPUT ) )
110 vlc_mutex_destroy( &p_sys->lock );
111 block_FifoRelease( p_sys->p_fifo );
112 stream_CommonDelete( s );
113 free( p_sys->psz_name );
121 void stream_DemuxSend( stream_t *s, block_t *p_block )
123 stream_sys_t *p_sys = s->p_sys;
124 block_FifoPut( p_sys->p_fifo, p_block );
127 int stream_DemuxControlVa( stream_t *s, int query, va_list args )
129 stream_sys_t *sys = s->p_sys;
133 case DEMUX_GET_POSITION:
134 vlc_mutex_lock( &sys->lock );
135 *va_arg( args, double * ) = sys->stats.position;
136 vlc_mutex_unlock( &sys->lock );
138 case DEMUX_GET_LENGTH:
139 vlc_mutex_lock( &sys->lock );
140 *va_arg( args, int64_t * ) = sys->stats.length;
141 vlc_mutex_unlock( &sys->lock );
144 vlc_mutex_lock( &sys->lock );
145 *va_arg( args, int64_t * ) = sys->stats.time;
146 vlc_mutex_unlock( &sys->lock );
154 static void DStreamDelete( stream_t *s )
156 stream_sys_t *p_sys = s->p_sys;
159 vlc_object_kill( s );
160 p_empty = block_Alloc( 0 );
161 block_FifoPut( p_sys->p_fifo, p_empty );
162 vlc_join( p_sys->thread, NULL );
163 vlc_mutex_destroy( &p_sys->lock );
166 block_Release( p_sys->p_block );
168 block_FifoRelease( p_sys->p_fifo );
169 free( p_sys->psz_name );
171 stream_CommonDelete( s );
175 static int DStreamRead( stream_t *s, void *p_read, unsigned int i_read )
177 stream_sys_t *p_sys = s->p_sys;
178 uint8_t *p_out = p_read;
181 //msg_Dbg( s, "DStreamRead: wanted %d bytes", i_read );
183 while( vlc_object_alive( s ) && !s->b_error && i_read )
185 block_t *p_block = p_sys->p_block;
190 p_block = block_FifoGet( p_sys->p_fifo );
191 if( !p_block ) s->b_error = 1;
192 p_sys->p_block = p_block;
195 if( p_block && i_read )
197 i_copy = __MIN( i_read, p_block->i_buffer );
198 if( p_out && i_copy ) memcpy( p_out, p_block->p_buffer, i_copy );
202 p_block->i_buffer -= i_copy;
203 p_block->p_buffer += i_copy;
205 if( !p_block->i_buffer )
207 block_Release( p_block );
208 p_sys->p_block = NULL;
213 p_sys->i_pos += i_out;
217 static int DStreamPeek( stream_t *s, const uint8_t **pp_peek, unsigned int i_peek )
219 stream_sys_t *p_sys = s->p_sys;
220 block_t **pp_block = &p_sys->p_block;
224 //msg_Dbg( s, "DStreamPeek: wanted %d bytes", i_peek );
226 while( vlc_object_alive( s ) && !s->b_error && i_peek )
232 *pp_block = block_FifoGet( p_sys->p_fifo );
233 if( !*pp_block ) s->b_error = 1;
236 if( *pp_block && i_peek )
238 i_copy = __MIN( i_peek, (*pp_block)->i_buffer );
242 if( i_peek ) pp_block = &(*pp_block)->p_next;
248 p_sys->p_block = block_ChainGather( p_sys->p_block );
249 *pp_peek = p_sys->p_block->p_buffer;
255 static int DStreamControl( stream_t *s, int i_query, va_list args )
257 stream_sys_t *p_sys = s->p_sys;
263 case STREAM_GET_SIZE:
264 p_i64 = va_arg( args, uint64_t * );
268 case STREAM_CAN_SEEK:
269 p_b = (bool*) va_arg( args, bool * );
273 case STREAM_CAN_FASTSEEK:
274 p_b = (bool*) va_arg( args, bool * );
278 case STREAM_GET_POSITION:
279 p_i64 = va_arg( args, uint64_t * );
280 *p_i64 = p_sys->i_pos;
283 case STREAM_SET_POSITION:
285 uint64_t i64 = va_arg( args, uint64_t );
286 if( i64 < p_sys->i_pos )
289 uint64_t i_skip = i64 - p_sys->i_pos;
292 int i_read = DStreamRead( s, NULL, __MIN(i_skip, INT_MAX) );
300 case STREAM_CONTROL_ACCESS:
301 case STREAM_GET_CONTENT_TYPE:
302 case STREAM_SET_RECORD_STATE:
306 msg_Err( s, "invalid DStreamControl query=0x%x", i_query );
311 static void* DStreamThread( void *obj )
313 stream_t *s = (stream_t *)obj;
314 stream_sys_t *p_sys = s->p_sys;
317 /* Create the demuxer */
318 p_demux = demux_New( s, s->p_input, "", p_sys->psz_name, "", s, p_sys->out,
320 if( p_demux == NULL )
323 /* stream_Demux cannot apply DVB filters.
324 * Get all programs and let the E/S output sort them out. */
325 demux_Control( p_demux, DEMUX_SET_GROUP, -1, NULL );
328 mtime_t next_update = 0;
329 while( vlc_object_alive( s ) )
331 if( p_demux->info.i_update || mdate() >= next_update )
334 int64_t newlen, newtime;
336 if( demux_Control( p_demux, DEMUX_GET_POSITION, &newpos ) )
338 if( demux_Control( p_demux, DEMUX_GET_LENGTH, &newlen ) )
340 if( demux_Control( p_demux, DEMUX_GET_TIME, &newtime ) )
343 vlc_mutex_lock( &p_sys->lock );
344 p_sys->stats.position = newpos;
345 p_sys->stats.length = newlen;
346 p_sys->stats.time = newtime;
347 vlc_mutex_unlock( &p_sys->lock );
349 p_demux->info.i_update = 0;
350 next_update = mdate() + (CLOCK_FREQ / 4);
353 if( demux_Demux( p_demux ) <= 0 )
357 demux_Delete( p_demux );