1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
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>
28 #include "input_internal.h"
30 /*****************************************************************************
32 * if s is NULL then load a access_demux
33 *****************************************************************************/
34 demux_t *__demux2_New( vlc_object_t *p_obj,
35 char *psz_access, char *psz_demux, char *psz_path,
36 stream_t *s, es_out_t *out )
38 demux_t *p_demux = vlc_object_create( p_obj, VLC_OBJECT_DEMUX );
47 p_demux->psz_access = strdup( psz_access );
48 p_demux->psz_demux = strdup( psz_demux );
49 p_demux->psz_path = strdup( psz_path );
51 msg_Dbg( p_obj, "demux2_New: access='%s' demux='%s' path='%s'",
52 p_demux->psz_access, p_demux->psz_demux, p_demux->psz_path );
57 p_demux->pf_demux = NULL;
58 p_demux->pf_control = NULL;
59 p_demux->p_sys = NULL;
60 p_demux->info.i_update = 0;
61 p_demux->info.i_title = 0;
62 p_demux->info.i_seekpoint = 0;
65 psz_module = p_demux->psz_demux;
67 psz_module = p_demux->psz_access;
69 if( s && *psz_module == '\0' && strrchr( p_demux->psz_path, '.' ) )
71 /* XXX: add only file without any problem here and with strong detection.
72 * - no .mp3, .a52, ... (aac is added as it works only by file ext anyway
73 * - wav can't be added 'cause of a52 and dts in them as raw audio
75 static struct { char *ext; char *demux; } exttodemux[] =
79 { "asf", "asf" }, { "wmv", "asf" }, { "wma", "asf" },
85 { "mkv", "mkv" }, { "mka", "mkv" }, { "mks", "mkv" },
86 { "mp4", "mp4" }, { "m4a", "mp4" }, { "mov", "mp4" }, { "moov", "mp4" },
87 { "mod", "mod" }, { "xm", "mod" },
89 { "ogg", "ogg" }, { "ogm", "ogg" },
95 char *psz_ext = strrchr( p_demux->psz_path, '.' ) + 1;
98 for( i = 0; exttodemux[i].ext != NULL; i++ )
100 if( !strcasecmp( psz_ext, exttodemux[i].ext ) )
102 psz_module = exttodemux[i].demux;
108 /* Before module_Need (for var_Create...) */
109 vlc_object_attach( p_demux, p_obj );
114 module_Need( p_demux, "demux2", psz_module,
115 !strcmp( psz_module, p_demux->psz_demux ) ? VLC_TRUE : VLC_FALSE );
120 module_Need( p_demux, "access_demux", psz_module,
121 !strcmp( psz_module, p_demux->psz_access ) ? VLC_TRUE : VLC_FALSE );
124 if( p_demux->p_module == NULL )
126 vlc_object_detach( p_demux );
127 free( p_demux->psz_path );
128 free( p_demux->psz_demux );
129 free( p_demux->psz_access );
130 vlc_object_destroy( p_demux );
137 /*****************************************************************************
139 *****************************************************************************/
140 void demux2_Delete( demux_t *p_demux )
142 module_Unneed( p_demux, p_demux->p_module );
143 vlc_object_detach( p_demux );
145 free( p_demux->psz_path );
146 free( p_demux->psz_demux );
147 free( p_demux->psz_access );
149 vlc_object_destroy( p_demux );
152 /*****************************************************************************
153 * demux2_vaControlHelper:
154 *****************************************************************************/
155 int demux2_vaControlHelper( stream_t *s,
156 int64_t i_start, int64_t i_end,
157 int i_bitrate, int i_align,
158 int i_query, va_list args )
164 if( i_end < 0 ) i_end = stream_Size( s );
165 if( i_start < 0 ) i_start = 0;
166 if( i_align <= 0 ) i_align = 1;
167 i_tell = stream_Tell( s );
171 case DEMUX_GET_LENGTH:
172 pi64 = (int64_t*)va_arg( args, int64_t * );
173 if( i_bitrate > 0 && i_end > i_start )
175 *pi64 = I64C(8000000) * (i_end - i_start) / i_bitrate;
181 pi64 = (int64_t*)va_arg( args, int64_t * );
182 if( i_bitrate > 0 && i_end > i_start )
184 *pi64 = I64C(8000000) * (i_tell - i_start) / i_bitrate;
189 case DEMUX_GET_POSITION:
190 pf = (double*)va_arg( args, double * );
191 if( i_start < i_end )
193 *pf = (double)( i_tell - i_start ) /
194 (double)( i_end - i_start );
200 case DEMUX_SET_POSITION:
201 f = (double)va_arg( args, double );
202 if( i_start < i_end && f >= 0.0 && f <= 1.0 )
204 int64_t i_block = (f * ( i_end - i_start )) / i_align;
206 if( stream_Seek( s, i_start + i_block * i_align ) )
215 i64 = (int64_t)va_arg( args, int64_t );
216 if( i_bitrate > 0 && i64 >= 0 )
218 int64_t i_block = i64 * i_bitrate / I64C(8000000) / i_align;
219 if( stream_Seek( s, i_start + i_block * i_align ) )
232 msg_Err( s, "unknown query in demux_vaControlDefault" );
237 /****************************************************************************
238 * stream_Demux*: create a demuxer for an outpout stream (allow demuxer chain)
239 ****************************************************************************/
256 static int DStreamRead ( stream_t *, void *p_read, int i_read );
257 static int DStreamPeek ( stream_t *, uint8_t **pp_peek, int i_peek );
258 static int DStreamControl( stream_t *, int i_query, va_list );
259 static int DStreamThread ( stream_t * );
262 stream_t *__stream_DemuxNew( vlc_object_t *p_obj, char *psz_demux, es_out_t *out )
264 /* We create a stream reader, and launch a thread */
266 d_stream_sys_t *p_sys;
268 if( psz_demux == NULL || *psz_demux == '\0' )
273 s = vlc_object_create( p_obj, VLC_OBJECT_STREAM );
275 s->pf_read = DStreamRead;
276 s->pf_peek = DStreamPeek;
277 s->pf_control= DStreamControl;
279 s->p_sys = malloc( sizeof( d_stream_sys_t) );
280 p_sys = (d_stream_sys_t*)s->p_sys;
282 vlc_mutex_init( s, &p_sys->lock );
284 p_sys->i_buffer_size = 1000000;
285 p_sys->p_buffer = malloc( p_sys->i_buffer_size );
287 p_sys->psz_name = strdup( psz_demux );
289 p_sys->p_demux = NULL;
291 if( vlc_thread_create( s, "stream out", DStreamThread, VLC_THREAD_PRIORITY_INPUT, VLC_FALSE ) )
293 vlc_mutex_destroy( &p_sys->lock );
294 vlc_object_destroy( s );
302 void stream_DemuxSend( stream_t *s, block_t *p_block )
304 d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
306 if( p_block->i_buffer > 0 )
308 vlc_mutex_lock( &p_sys->lock );
309 /* Realloc if needed */
310 if( p_sys->i_buffer + p_block->i_buffer > p_sys->i_buffer_size )
312 if( p_sys->i_buffer_size > 5000000 )
314 vlc_mutex_unlock( &p_sys->lock );
315 msg_Err( s, "stream_DemuxSend: buffer size > 5000000" );
316 block_Release( p_block );
319 /* I know, it's more than needed but that's perfect */
320 p_sys->i_buffer_size += p_block->i_buffer;
321 /* FIXME won't work with PEEK -> segfault */
322 p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
323 msg_Dbg( s, "stream_DemuxSend: realloc to %d", p_sys->i_buffer_size );
327 memcpy( &p_sys->p_buffer[p_sys->i_buffer], p_block->p_buffer, p_block->i_buffer );
328 p_sys->i_buffer += p_block->i_buffer;
330 vlc_mutex_unlock( &p_sys->lock );
333 block_Release( p_block );
336 void stream_DemuxDelete( stream_t *s )
338 d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
342 vlc_mutex_lock( &p_sys->lock );
345 p_sys->p_demux->b_die = VLC_TRUE;
347 vlc_mutex_unlock( &p_sys->lock );
349 vlc_thread_join( s );
353 demux2_Delete( p_sys->p_demux );
355 vlc_mutex_destroy( &p_sys->lock );
356 free( p_sys->psz_name );
357 free( p_sys->p_buffer );
359 vlc_object_destroy( s );
363 static int DStreamRead ( stream_t *s, void *p_read, int i_read )
365 d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
368 //msg_Dbg( s, "DStreamRead: wanted %d bytes", i_read );
371 vlc_mutex_lock( &p_sys->lock );
372 //msg_Dbg( s, "DStreamRead: buffer %d", p_sys->i_buffer );
373 if( p_sys->i_buffer >= i_read || s->b_die )
377 vlc_mutex_unlock( &p_sys->lock );
381 //msg_Dbg( s, "DStreamRead: read %d buffer %d", i_read, p_sys->i_buffer );
383 i_copy = __MIN( i_read, p_sys->i_buffer );
388 memcpy( p_read, p_sys->p_buffer, i_copy );
390 p_sys->i_buffer -= i_copy;
391 p_sys->i_pos += i_copy;
393 if( p_sys->i_buffer > 0 )
395 memmove( p_sys->p_buffer, &p_sys->p_buffer[i_copy], p_sys->i_buffer );
399 vlc_mutex_unlock( &p_sys->lock );
403 static int DStreamPeek ( stream_t *s, uint8_t **pp_peek, int i_peek )
405 d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
408 //msg_Dbg( s, "DStreamPeek: wanted %d bytes", i_peek );
411 vlc_mutex_lock( &p_sys->lock );
412 //msg_Dbg( s, "DStreamPeek: buffer %d", p_sys->i_buffer );
413 if( p_sys->i_buffer >= i_peek || s->b_die )
417 vlc_mutex_unlock( &p_sys->lock );
420 *pp_peek = p_sys->p_buffer;
421 i_copy = __MIN( i_peek, p_sys->i_buffer );
423 vlc_mutex_unlock( &p_sys->lock );
428 static int DStreamControl( stream_t *s, int i_query, va_list args )
430 d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
436 case STREAM_GET_SIZE:
437 p_i64 = (int64_t*) va_arg( args, int64_t * );
441 case STREAM_CAN_SEEK:
442 p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
446 case STREAM_CAN_FASTSEEK:
447 p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
451 case STREAM_GET_POSITION:
452 p_i64 = (int64_t*) va_arg( args, int64_t * );
453 *p_i64 = p_sys->i_pos;
456 case STREAM_SET_POSITION:
460 p_int = (int*) va_arg( args, int * );
465 msg_Err( s, "invalid DStreamControl query=0x%x", i_query );
470 static int DStreamThread ( stream_t *s )
472 d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
475 /* Create the demuxer */
477 if( ( p_demux = demux2_New( s, "", p_sys->psz_name, "", s, p_sys->out ) ) == NULL )
482 vlc_mutex_lock( &p_sys->lock );
483 p_sys->p_demux = p_demux;
484 vlc_mutex_unlock( &p_sys->lock );
487 while( !s->b_die && !p_demux->b_die )
489 if( p_demux->pf_demux( p_demux ) <= 0 )
494 p_demux->b_die = VLC_TRUE;