]> git.sesse.net Git - vlc/blob - src/input/demux.c
Reduce the global verbosity
[vlc] / src / input / demux.c
1 /*****************************************************************************
2  * demux.c
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id$
6  *
7  * Author: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include <stdlib.h>
25 #include <vlc/vlc.h>
26 #include <vlc/input.h>
27
28 #include "input_internal.h"
29
30 /*****************************************************************************
31  * demux2_New:
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, vlc_bool_t b_quick )
37 {
38     demux_t *p_demux = vlc_object_create( p_obj, VLC_OBJECT_DEMUX );
39     char *psz_module;
40
41     if( p_demux == NULL )
42     {
43         return NULL;
44     }
45
46     /* Parse URL */
47     p_demux->psz_access = strdup( psz_access );
48     p_demux->psz_demux  = strdup( psz_demux );
49     p_demux->psz_path   = strdup( psz_path );
50
51     /* Take into account "demux" to be able to do :demux=dump */
52     if( *p_demux->psz_demux == '\0' )
53     {
54         free( p_demux->psz_demux );
55         p_demux->psz_demux = var_GetString( p_obj, "demux" );
56     }
57
58     if( !b_quick )
59     {
60         msg_Dbg( p_obj, "creating demux: access='%s' demux='%s' path='%s'",
61                  p_demux->psz_access, p_demux->psz_demux, p_demux->psz_path );
62     }
63
64     p_demux->s          = s;
65     p_demux->out        = out;
66
67     p_demux->pf_demux   = NULL;
68     p_demux->pf_control = NULL;
69     p_demux->p_sys      = NULL;
70     p_demux->info.i_update = 0;
71     p_demux->info.i_title  = 0;
72     p_demux->info.i_seekpoint = 0;
73
74     if( s )
75         psz_module = p_demux->psz_demux;
76     else
77         psz_module = p_demux->psz_access;
78
79     if( s && *psz_module == '\0' && strrchr( p_demux->psz_path, '.' ) )
80     {
81         /* XXX: add only file without any problem here and with strong detection.
82          *  - no .mp3, .a52, ... (aac is added as it works only by file ext anyway
83          *  - wav can't be added 'cause of a52 and dts in them as raw audio
84          */
85          static struct { char *ext; char *demux; } exttodemux[] =
86          {
87             { "aac",  "aac" },
88             { "aiff", "aiff" },
89             { "asf",  "asf" }, { "wmv",  "asf" }, { "wma",  "asf" },
90             { "avi",  "avi" },
91             { "au",   "au" },
92             { "flac", "flac" },
93             { "dv",   "dv" },
94             { "m3u",  "m3u" },
95             { "mkv",  "mkv" }, { "mka",  "mkv" }, { "mks",  "mkv" },
96             { "mp4",  "mp4" }, { "m4a",  "mp4" }, { "mov",  "mp4" }, { "moov", "mp4" },
97             { "mod",  "mod" }, { "xm",   "mod" },
98             { "nsv",  "nsv" },
99             { "ogg",  "ogg" }, { "ogm",  "ogg" },
100             { "pva",  "pva" },
101             { "rm",   "rm" },
102             { NULL,  NULL },
103         };
104         /* Here, we don't mind if it does not work, it must be quick */
105         static struct { char *ext; char *demux; } exttodemux_quick[] = 
106         {
107             { "mp3", "mpga" },
108             { "ogg", "ogg" },
109             { "wma", "asf" },
110             { NULL, NULL }
111         };
112
113         char *psz_ext = strrchr( p_demux->psz_path, '.' ) + 1;
114         int  i;
115
116         if( !b_quick )
117         {
118             for( i = 0; exttodemux[i].ext != NULL; i++ )
119             {
120                 if( !strcasecmp( psz_ext, exttodemux[i].ext ) )
121                 {
122                     psz_module = exttodemux[i].demux;
123                     break;
124                 }
125             }
126         }
127         else
128         {
129             for( i = 0; exttodemux_quick[i].ext != NULL; i++ )
130             {
131                 if( !strcasecmp( psz_ext, exttodemux_quick[i].ext ) )
132                 {
133                     psz_module = exttodemux_quick[i].demux;
134                     break;
135                 }
136             }
137
138         }
139     }
140
141     /* Before module_Need (for var_Create...) */
142     vlc_object_attach( p_demux, p_obj );
143
144     if( s )
145     {
146         p_demux->p_module =
147             module_Need( p_demux, "demux2", psz_module,
148                          !strcmp( psz_module, p_demux->psz_demux ) ?
149                          VLC_TRUE : VLC_FALSE );
150     }
151     else
152     {
153         p_demux->p_module =
154             module_Need( p_demux, "access_demux", psz_module,
155                          !strcmp( psz_module, p_demux->psz_access ) ?
156                          VLC_TRUE : VLC_FALSE );
157     }
158
159     if( p_demux->p_module == NULL )
160     {
161         vlc_object_detach( p_demux );
162         free( p_demux->psz_path );
163         free( p_demux->psz_demux );
164         free( p_demux->psz_access );
165         vlc_object_destroy( p_demux );
166         return NULL;
167     }
168
169     return p_demux;
170 }
171
172 /*****************************************************************************
173  * demux2_Delete:
174  *****************************************************************************/
175 void demux2_Delete( demux_t *p_demux )
176 {
177     module_Unneed( p_demux, p_demux->p_module );
178     vlc_object_detach( p_demux );
179
180     free( p_demux->psz_path );
181     free( p_demux->psz_demux );
182     free( p_demux->psz_access );
183
184     vlc_object_destroy( p_demux );
185 }
186
187 /*****************************************************************************
188  * demux2_vaControlHelper:
189  *****************************************************************************/
190 int demux2_vaControlHelper( stream_t *s,
191                             int64_t i_start, int64_t i_end,
192                             int i_bitrate, int i_align,
193                             int i_query, va_list args )
194 {
195     int64_t i_tell;
196     double  f, *pf;
197     int64_t i64, *pi64;
198
199     if( i_end < 0 )    i_end   = stream_Size( s );
200     if( i_start < 0 )  i_start = 0;
201     if( i_align <= 0 ) i_align = 1;
202     i_tell = stream_Tell( s );
203
204     switch( i_query )
205     {
206         case DEMUX_GET_LENGTH:
207             pi64 = (int64_t*)va_arg( args, int64_t * );
208             if( i_bitrate > 0 && i_end > i_start )
209             {
210                 *pi64 = I64C(8000000) * (i_end - i_start) / i_bitrate;
211                 return VLC_SUCCESS;
212             }
213             return VLC_EGENERIC;
214
215         case DEMUX_GET_TIME:
216             pi64 = (int64_t*)va_arg( args, int64_t * );
217             if( i_bitrate > 0 && i_end > i_start )
218             {
219                 *pi64 = I64C(8000000) * (i_tell - i_start) / i_bitrate;
220                 return VLC_SUCCESS;
221             }
222             return VLC_EGENERIC;
223
224         case DEMUX_GET_POSITION:
225             pf = (double*)va_arg( args, double * );
226             if( i_start < i_end )
227             {
228                 *pf = (double)( i_tell - i_start ) /
229                       (double)( i_end  - i_start );
230                 return VLC_SUCCESS;
231             }
232             return VLC_EGENERIC;
233
234
235         case DEMUX_SET_POSITION:
236             f = (double)va_arg( args, double );
237             if( i_start < i_end && f >= 0.0 && f <= 1.0 )
238             {
239                 int64_t i_block = (f * ( i_end - i_start )) / i_align;
240
241                 if( stream_Seek( s, i_start + i_block * i_align ) )
242                 {
243                     return VLC_EGENERIC;
244                 }
245                 return VLC_SUCCESS;
246             }
247             return VLC_EGENERIC;
248
249         case DEMUX_SET_TIME:
250             i64 = (int64_t)va_arg( args, int64_t );
251             if( i_bitrate > 0 && i64 >= 0 )
252             {
253                 int64_t i_block = i64 * i_bitrate / I64C(8000000) / i_align;
254                 if( stream_Seek( s, i_start + i_block * i_align ) )
255                 {
256                     return VLC_EGENERIC;
257                 }
258                 return VLC_SUCCESS;
259             }
260             return VLC_EGENERIC;
261
262         case DEMUX_GET_FPS:
263         case DEMUX_GET_META:
264         case DEMUX_SET_NEXT_DEMUX_TIME:
265         case DEMUX_GET_TITLE_INFO:
266         case DEMUX_SET_GROUP:
267             return VLC_EGENERIC;
268
269         default:
270             msg_Err( s, "unknown query in demux_vaControlDefault" );
271             return VLC_EGENERIC;
272     }
273 }
274
275 /****************************************************************************
276  * stream_Demux*: create a demuxer for an outpout stream (allow demuxer chain)
277  ****************************************************************************/
278 typedef struct
279 {
280     /* Data buffer */
281     block_fifo_t *p_fifo;
282     block_t      *p_block;
283
284     int64_t     i_pos;
285
286     /* Demuxer */
287     char        *psz_name;
288     es_out_t    *out;
289     demux_t     *p_demux;
290
291 } d_stream_sys_t;
292
293 static int DStreamRead   ( stream_t *, void *p_read, int i_read );
294 static int DStreamPeek   ( stream_t *, uint8_t **pp_peek, int i_peek );
295 static int DStreamControl( stream_t *, int i_query, va_list );
296 static int DStreamThread ( stream_t * );
297
298
299 stream_t *__stream_DemuxNew( vlc_object_t *p_obj, char *psz_demux,
300                              es_out_t *out )
301 {
302     /* We create a stream reader, and launch a thread */
303     stream_t       *s;
304     d_stream_sys_t *p_sys;
305
306     if( psz_demux == NULL || *psz_demux == '\0' ) return NULL;
307
308     s = vlc_object_create( p_obj, VLC_OBJECT_STREAM );
309     s->pf_block  = NULL;
310     s->pf_read   = DStreamRead;
311     s->pf_peek   = DStreamPeek;
312     s->pf_control= DStreamControl;
313
314     s->p_sys = malloc( sizeof( d_stream_sys_t) );
315     p_sys = (d_stream_sys_t*)s->p_sys;
316
317     p_sys->i_pos = 0;
318     p_sys->out = out;
319     p_sys->p_demux = NULL;
320     p_sys->p_block = NULL;
321     p_sys->psz_name = strdup( psz_demux );
322
323     /* decoder fifo */
324     if( ( p_sys->p_fifo = block_FifoNew( s ) ) == NULL )
325     {
326         msg_Err( s, "out of memory" );
327         vlc_object_destroy( s );
328         free( p_sys );
329         return NULL;
330     }
331
332     if( vlc_thread_create( s, "stream out", DStreamThread,
333                            VLC_THREAD_PRIORITY_INPUT, VLC_FALSE ) )
334     {
335         vlc_object_destroy( s );
336         free( p_sys );
337         return NULL;
338     }
339
340     return s;
341 }
342
343 void stream_DemuxSend( stream_t *s, block_t *p_block )
344 {
345     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
346     if( p_block ) block_FifoPut( p_sys->p_fifo, p_block );
347 }
348
349 void stream_DemuxDelete( stream_t *s )
350 {
351     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
352     block_t *p_empty;
353
354     s->b_die = VLC_TRUE;
355     if( p_sys->p_demux ) p_sys->p_demux->b_die = VLC_TRUE;
356     p_empty = block_New( s, 1 ); p_empty->i_buffer = 0;
357     block_FifoPut( p_sys->p_fifo, p_empty );
358     vlc_thread_join( s );
359
360     if( p_sys->p_demux ) demux2_Delete( p_sys->p_demux );
361     if( p_sys->p_block ) block_Release( p_sys->p_block );
362
363     block_FifoRelease( p_sys->p_fifo );
364     free( p_sys->psz_name );
365     free( p_sys );
366
367     vlc_object_destroy( s );
368 }
369
370
371 static int DStreamRead( stream_t *s, void *p_read, int i_read )
372 {
373     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
374     uint8_t *p_out = p_read;
375     int i_out = 0;
376
377     //msg_Dbg( s, "DStreamRead: wanted %d bytes", i_read );
378
379     while( !s->b_die && !s->b_error && i_read )
380     {
381         block_t *p_block = p_sys->p_block;
382         int i_copy;
383
384         if( !p_block )
385         {
386             p_block = block_FifoGet( p_sys->p_fifo );
387             if( !p_block ) s->b_error = 1;
388             p_sys->p_block = p_block;
389         }
390
391         if( p_block && i_read )
392         {
393             i_copy = __MIN( i_read, p_block->i_buffer );
394             if( p_out && i_copy ) memcpy( p_out, p_block->p_buffer, i_copy );
395             i_read -= i_copy;
396             i_out += i_copy;
397             p_block->i_buffer -= i_copy;
398             p_block->p_buffer += i_copy;
399
400             if( !p_block->i_buffer )
401             {
402                 block_Release( p_block );
403                 p_sys->p_block = NULL;
404             }
405         }
406     }
407
408     p_sys->i_pos += i_out;
409     return i_out;
410 }
411
412 static int DStreamPeek( stream_t *s, uint8_t **pp_peek, int i_peek )
413 {
414     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
415     block_t **pp_block = &p_sys->p_block;
416     int i_out = 0;
417     *pp_peek = 0;
418
419     //msg_Dbg( s, "DStreamPeek: wanted %d bytes", i_peek );
420
421     while( !s->b_die && !s->b_error && i_peek )
422     {
423         int i_copy;
424
425         if( !*pp_block )
426         {
427             *pp_block = block_FifoGet( p_sys->p_fifo );
428             if( !*pp_block ) s->b_error = 1;
429         }
430
431         if( *pp_block && i_peek )
432         {
433             i_copy = __MIN( i_peek, (*pp_block)->i_buffer );
434             i_peek -= i_copy;
435             i_out += i_copy;
436
437             if( i_peek ) pp_block = &(*pp_block)->p_next;
438         }
439     }
440
441     if( p_sys->p_block )
442     {
443         p_sys->p_block = block_ChainGather( p_sys->p_block );
444         *pp_peek = p_sys->p_block->p_buffer;
445     }
446
447     return i_out;
448 }
449
450 static int DStreamControl( stream_t *s, int i_query, va_list args )
451 {
452     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
453     int64_t    *p_i64;
454     vlc_bool_t *p_b;
455     int        *p_int;
456
457     switch( i_query )
458     {
459         case STREAM_GET_SIZE:
460             p_i64 = (int64_t*) va_arg( args, int64_t * );
461             *p_i64 = 0;
462             return VLC_SUCCESS;
463
464         case STREAM_CAN_SEEK:
465             p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
466             *p_b = VLC_FALSE;
467             return VLC_SUCCESS;
468
469         case STREAM_CAN_FASTSEEK:
470             p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
471             *p_b = VLC_FALSE;
472             return VLC_SUCCESS;
473
474         case STREAM_GET_POSITION:
475             p_i64 = (int64_t*) va_arg( args, int64_t * );
476             *p_i64 = p_sys->i_pos;
477             return VLC_SUCCESS;
478
479         case STREAM_SET_POSITION:
480         {
481             int64_t i64 = (int64_t)va_arg( args, int64_t );
482             int i_skip;
483             if( i64 < p_sys->i_pos ) return VLC_EGENERIC;
484             i_skip = i64 - p_sys->i_pos;
485
486             while( i_skip > 0 )
487             {
488                 int i_read = DStreamRead( s, NULL, i_skip );
489                 if( i_read <= 0 ) return VLC_EGENERIC;
490                 i_skip -= i_read;
491             }
492             return VLC_SUCCESS;
493         }
494
495         case STREAM_GET_MTU:
496             p_int = (int*) va_arg( args, int * );
497             *p_int = 0;
498             return VLC_SUCCESS;
499
500         case STREAM_CONTROL_ACCESS:
501             return VLC_EGENERIC;
502
503         default:
504             msg_Err( s, "invalid DStreamControl query=0x%x", i_query );
505             return VLC_EGENERIC;
506     }
507 }
508
509 static int DStreamThread( stream_t *s )
510 {
511     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
512     demux_t *p_demux;
513
514     /* Create the demuxer */
515     if( !(p_demux = demux2_New( s, "", p_sys->psz_name, "", s, p_sys->out,
516                                VLC_FALSE )) )
517     {
518         return VLC_EGENERIC;
519     }
520
521     p_sys->p_demux = p_demux;
522
523     /* Main loop */
524     while( !s->b_die && !p_demux->b_die )
525     {
526         if( p_demux->pf_demux( p_demux ) <= 0 ) break;
527     }
528
529     p_demux->b_die = VLC_TRUE;
530     return VLC_SUCCESS;
531 }