1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
5 * $Id: demux.c,v 1.13 2004/03/03 20:39:53 gbazin Exp $
7 * Author: Laurent Aimar <fenrir@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
26 #include <vlc/input.h>
30 int demux_vaControl( input_thread_t *p_input, int i_query, va_list args )
32 if( p_input->pf_demux_control )
34 return p_input->pf_demux_control( p_input, i_query, args );
39 int demux_Control( input_thread_t *p_input, int i_query, ... )
44 va_start( args, i_query );
45 i_result = demux_vaControl( p_input, i_query, args );
51 static void SeekOffset( input_thread_t *p_input, int64_t i_pos );
53 int demux_vaControlDefault( input_thread_t *p_input, int i_query,
60 vlc_mutex_lock( &p_input->stream.stream_lock );
63 case DEMUX_GET_POSITION:
64 pf = (double*)va_arg( args, double * );
65 if( p_input->stream.p_selected_area->i_size <= 0 )
71 *pf = (double)p_input->stream.p_selected_area->i_tell /
72 (double)p_input->stream.p_selected_area->i_size;
77 case DEMUX_SET_POSITION:
78 f = (double)va_arg( args, double );
79 if( p_input->stream.b_seekable && p_input->pf_seek != NULL &&
80 f >= 0.0 && f <= 1.0 )
82 SeekOffset( p_input, (int64_t)(f *
83 (double)p_input->stream.p_selected_area->i_size) );
93 pi64 = (int64_t*)va_arg( args, int64_t * );
94 if( p_input->stream.i_mux_rate > 0 )
96 *pi64 = (int64_t)1000000 *
97 ( p_input->stream.p_selected_area->i_tell / 50 ) /
98 p_input->stream.i_mux_rate;
104 i_ret = VLC_EGENERIC;
109 i64 = (int64_t)va_arg( args, int64_t );
110 if( p_input->stream.i_mux_rate > 0 &&
111 p_input->stream.b_seekable &&
112 p_input->pf_seek != NULL && i64 >= 0 )
114 SeekOffset( p_input, i64 * 50 *
115 (int64_t)p_input->stream.i_mux_rate /
121 i_ret = VLC_EGENERIC;
125 case DEMUX_GET_LENGTH:
126 pi64 = (int64_t*)va_arg( args, int64_t * );
127 if( p_input->stream.i_mux_rate > 0 )
129 *pi64 = (int64_t)1000000 *
130 ( p_input->stream.p_selected_area->i_size / 50 ) /
131 p_input->stream.i_mux_rate;
137 i_ret = VLC_EGENERIC;
141 i_ret = VLC_EGENERIC;
144 i_ret = VLC_EGENERIC;
148 msg_Err( p_input, "unknown query in demux_vaControlDefault" );
149 i_ret = VLC_EGENERIC;
152 vlc_mutex_unlock( &p_input->stream.stream_lock );
157 static void SeekOffset( input_thread_t *p_input, int64_t i_pos )
159 /* Reinitialize buffer manager. */
160 input_AccessReinit( p_input );
162 vlc_mutex_unlock( &p_input->stream.stream_lock );
163 p_input->pf_seek( p_input, i_pos );
164 vlc_mutex_lock( &p_input->stream.stream_lock );
168 /*****************************************************************************
170 *****************************************************************************/
171 demux_t *__demux2_New( vlc_object_t *p_obj,
172 char *psz_mrl, stream_t *s, es_out_t *out )
174 demux_t *p_demux = vlc_object_create( p_obj, sizeof( demux_t ) );
176 char *psz_dup = strdup( psz_mrl ? psz_mrl : "" );
177 char *psz = strchr( psz_dup, ':' );
179 if( p_demux == NULL )
186 p_demux->psz_access = NULL;
187 p_demux->psz_demux = NULL;
188 p_demux->psz_path = NULL;
194 if( psz[0] == '/' && psz[1] == '/' )
198 p_demux->psz_path = strdup( psz );
200 psz = strchr( psz_dup, '/' );
204 p_demux->psz_access = strdup( psz_dup );
205 p_demux->psz_demux = strdup( psz );
210 p_demux->psz_path = strdup( psz_mrl );
215 if( p_demux->psz_access == NULL )
217 p_demux->psz_access = strdup( "" );
219 if( p_demux->psz_demux == NULL )
221 p_demux->psz_demux = strdup( "" );
223 if( p_demux->psz_path == NULL )
225 p_demux->psz_path = strdup( "" );
227 msg_Dbg( p_obj, "demux2_New: '%s' -> access='%s' demux='%s' path='%s'",
229 p_demux->psz_access, p_demux->psz_demux, p_demux->psz_path );
234 p_demux->pf_demux = NULL;
235 p_demux->pf_control = NULL;
236 p_demux->p_sys = NULL;
238 /* Before module_Need (for var_Create...) */
239 vlc_object_attach( p_demux, p_obj );
242 module_Need( p_demux, "demux2", p_demux->psz_demux, VLC_TRUE );
243 if( p_demux->p_module == NULL )
245 vlc_object_detach( p_demux );
246 free( p_demux->psz_path );
247 free( p_demux->psz_demux );
248 free( p_demux->psz_access );
249 vlc_object_destroy( p_demux );
256 /*****************************************************************************
258 *****************************************************************************/
259 void demux2_Delete( demux_t *p_demux )
261 module_Unneed( p_demux, p_demux->p_module );
262 vlc_object_detach( p_demux );
264 free( p_demux->psz_path );
265 free( p_demux->psz_demux );
266 free( p_demux->psz_access );
268 vlc_object_destroy( p_demux );
271 /*****************************************************************************
272 * demux2_vaControlHelper:
273 *****************************************************************************/
274 int demux2_vaControlHelper( stream_t *s,
275 int64_t i_start, int64_t i_end,
276 int i_bitrate, int i_align,
277 int i_query, va_list args )
283 if( i_end < 0 ) i_end = stream_Size( s );
284 if( i_start < 0 ) i_start = 0;
285 if( i_align <= 0 ) i_align = 1;
286 i_tell = stream_Tell( s );
290 case DEMUX_GET_LENGTH:
291 pi64 = (int64_t*)va_arg( args, int64_t * );
292 if( i_bitrate > 0 && i_end > i_start )
294 *pi64 = I64C(8000000) * (i_end - i_start) / i_bitrate;
300 pi64 = (int64_t*)va_arg( args, int64_t * );
301 if( i_bitrate > 0 && i_end > i_start )
303 *pi64 = I64C(8000000) * (i_tell - i_start) / i_bitrate;
308 case DEMUX_GET_POSITION:
309 pf = (double*)va_arg( args, double * );
310 if( i_start < i_end )
312 *pf = (double)( i_tell - i_start ) /
313 (double)( i_end - i_start );
319 case DEMUX_SET_POSITION:
320 f = (double)va_arg( args, double );
321 if( i_start < i_end && f >= 0.0 && f <= 1.0 )
323 int64_t i_block = (f * ( i_end - i_start )) / i_align;
325 if( stream_Seek( s, i_start + i_block * i_align ) )
334 i64 = (int64_t)va_arg( args, int64_t );
335 if( i_bitrate > 0 && i64 >= 0 )
337 int64_t i_block = i64 * i_bitrate / I64C(8000000) / i_align;
338 if( stream_Seek( s, i_start + i_block * i_align ) )
351 msg_Err( s, "unknown query in demux_vaControlDefault" );