]> git.sesse.net Git - vlc/blob - src/input/demux.c
libvlccore: push threads cancellation down vlc_thread_create
[vlc] / src / input / demux.c
1 /*****************************************************************************
2  * demux.c
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29
30 #include "input_internal.h"
31
32 static bool SkipID3Tag( demux_t * );
33 static bool SkipAPETag( demux_t *p_demux );
34
35 /*****************************************************************************
36  * demux_New:
37  *  if s is NULL then load a access_demux
38  *****************************************************************************/
39 demux_t *__demux_New( vlc_object_t *p_obj,
40                        const char *psz_access, const char *psz_demux,
41                        const char *psz_path,
42                        stream_t *s, es_out_t *out, bool b_quick )
43 {
44     static const char typename[] = "demux";
45     demux_t *p_demux = vlc_custom_create( p_obj, sizeof( *p_demux ),
46                                           VLC_OBJECT_GENERIC, typename );
47     const char *psz_module;
48
49     if( p_demux == NULL ) return NULL;
50
51     /* Parse URL */
52     p_demux->psz_access = strdup( psz_access );
53     p_demux->psz_demux  = strdup( psz_demux );
54     p_demux->psz_path   = strdup( psz_path );
55
56     /* Take into account "demux" to be able to do :demux=dump */
57     if( p_demux->psz_demux && *p_demux->psz_demux == '\0' )
58     {
59         free( p_demux->psz_demux );
60         p_demux->psz_demux = var_GetNonEmptyString( p_obj, "demux" );
61         if( p_demux->psz_demux == NULL )
62             p_demux->psz_demux = strdup( "" );
63     }
64
65     if( !b_quick )
66     {
67         msg_Dbg( p_obj, "creating demux: access='%s' demux='%s' path='%s'",
68                  p_demux->psz_access, p_demux->psz_demux, p_demux->psz_path );
69     }
70
71     p_demux->s          = s;
72     p_demux->out        = out;
73
74     p_demux->pf_demux   = NULL;
75     p_demux->pf_control = NULL;
76     p_demux->p_sys      = NULL;
77     p_demux->info.i_update = 0;
78     p_demux->info.i_title  = 0;
79     p_demux->info.i_seekpoint = 0;
80
81     if( s ) psz_module = p_demux->psz_demux;
82     else psz_module = p_demux->psz_access;
83
84     if( s && *psz_module == '\0' && strrchr( p_demux->psz_path, '.' ) )
85     {
86        /* XXX: add only file without any problem here and with strong detection.
87         *  - no .mp3, .a52, ... (aac is added as it works only by file ext
88         *     anyway
89         *  - wav can't be added 'cause of a52 and dts in them as raw audio
90          */
91          static const struct { char ext[5]; char demux[9]; } exttodemux[] =
92          {
93             { "aac",  "aac" },
94             { "aiff", "aiff" },
95             { "asf",  "asf" }, { "wmv",  "asf" }, { "wma",  "asf" },
96             { "avi",  "avi" },
97             { "au",   "au" },
98             { "flac", "flac" },
99             { "dv",   "dv" },
100             { "m3u",  "playlist" },
101             { "mkv",  "mkv" }, { "mka",  "mkv" }, { "mks",  "mkv" },
102             { "mp4",  "mp4" }, { "m4a",  "mp4" }, { "mov",  "mp4" }, { "moov", "mp4" },
103             { "mod",  "mod" }, { "xm",   "mod" },
104             { "nsv",  "nsv" },
105             { "ogg",  "ogg" }, { "ogm",  "ogg" },
106             { "pva",  "pva" },
107             { "rm",   "rm" },
108             { "m4v",  "m4v" },
109             { "h264",  "h264" },
110             { "",  "" },
111         };
112         /* Here, we don't mind if it does not work, it must be quick */
113         static const struct { char ext[4]; char demux[5]; } exttodemux_quick[] =
114         {
115             { "mp3", "mpga" },
116             { "ogg", "ogg" },
117             { "wma", "asf" },
118             { "", "" }
119         };
120
121         const char *psz_ext = strrchr( p_demux->psz_path, '.' ) + 1;
122         int  i;
123
124         if( !b_quick )
125         {
126             for( i = 0; exttodemux[i].ext[0]; i++ )
127             {
128                 if( !strcasecmp( psz_ext, exttodemux[i].ext ) )
129                 {
130                     psz_module = exttodemux[i].demux;
131                     break;
132                 }
133             }
134         }
135         else
136         {
137             for( i = 0; exttodemux_quick[i].ext[0]; i++ )
138             {
139                 if( !strcasecmp( psz_ext, exttodemux_quick[i].ext ) )
140                 {
141                     psz_module = exttodemux_quick[i].demux;
142                     break;
143                 }
144             }
145
146         }
147     }
148
149     /* Before module_Need (for var_Create...) */
150     vlc_object_attach( p_demux, p_obj );
151
152     if( s )
153     {
154         /* ID3/APE tags will mess-up demuxer probing so we skip it here.
155          * ID3/APE parsers will called later on in the demuxer to access the
156          * skipped info. */
157         if( !SkipID3Tag( p_demux ) )
158             SkipAPETag( p_demux );
159
160         p_demux->p_module =
161             module_Need( p_demux, "demux", psz_module,
162                          !strcmp( psz_module, p_demux->psz_demux ) ?
163                          true : false );
164     }
165     else
166     {
167         p_demux->p_module =
168             module_Need( p_demux, "access_demux", psz_module,
169                          !strcmp( psz_module, p_demux->psz_access ) ?
170                          true : false );
171     }
172
173     if( p_demux->p_module == NULL )
174     {
175         vlc_object_detach( p_demux );
176         free( p_demux->psz_path );
177         free( p_demux->psz_demux );
178         free( p_demux->psz_access );
179         vlc_object_release( p_demux );
180         return NULL;
181     }
182
183     return p_demux;
184 }
185
186 /*****************************************************************************
187  * demux_Delete:
188  *****************************************************************************/
189 void demux_Delete( demux_t *p_demux )
190 {
191     module_Unneed( p_demux, p_demux->p_module );
192     vlc_object_detach( p_demux );
193
194     free( p_demux->psz_path );
195     free( p_demux->psz_demux );
196     free( p_demux->psz_access );
197
198     vlc_object_release( p_demux );
199 }
200
201 /*****************************************************************************
202  * demux_vaControlHelper:
203  *****************************************************************************/
204 int demux_vaControlHelper( stream_t *s,
205                             int64_t i_start, int64_t i_end,
206                             int i_bitrate, int i_align,
207                             int i_query, va_list args )
208 {
209     int64_t i_tell;
210     double  f, *pf;
211     int64_t i64, *pi64;
212
213     if( i_end < 0 )    i_end   = stream_Size( s );
214     if( i_start < 0 )  i_start = 0;
215     if( i_align <= 0 ) i_align = 1;
216     i_tell = stream_Tell( s );
217
218     switch( i_query )
219     {
220         case DEMUX_GET_LENGTH:
221             pi64 = (int64_t*)va_arg( args, int64_t * );
222             if( i_bitrate > 0 && i_end > i_start )
223             {
224                 *pi64 = INT64_C(8000000) * (i_end - i_start) / i_bitrate;
225                 return VLC_SUCCESS;
226             }
227             return VLC_EGENERIC;
228
229         case DEMUX_GET_TIME:
230             pi64 = (int64_t*)va_arg( args, int64_t * );
231             if( i_bitrate > 0 && i_end > i_start )
232             {
233                 *pi64 = INT64_C(8000000) * (i_tell - i_start) / i_bitrate;
234                 return VLC_SUCCESS;
235             }
236             return VLC_EGENERIC;
237
238         case DEMUX_GET_POSITION:
239             pf = (double*)va_arg( args, double * );
240             if( i_start < i_end )
241             {
242                 *pf = (double)( i_tell - i_start ) /
243                       (double)( i_end  - i_start );
244                 return VLC_SUCCESS;
245             }
246             return VLC_EGENERIC;
247
248
249         case DEMUX_SET_POSITION:
250             f = (double)va_arg( args, double );
251             if( i_start < i_end && f >= 0.0 && f <= 1.0 )
252             {
253                 int64_t i_block = (f * ( i_end - i_start )) / i_align;
254
255                 if( stream_Seek( s, i_start + i_block * i_align ) )
256                 {
257                     return VLC_EGENERIC;
258                 }
259                 return VLC_SUCCESS;
260             }
261             return VLC_EGENERIC;
262
263         case DEMUX_SET_TIME:
264             i64 = (int64_t)va_arg( args, int64_t );
265             if( i_bitrate > 0 && i64 >= 0 )
266             {
267                 int64_t i_block = i64 * i_bitrate / INT64_C(8000000) / i_align;
268                 if( stream_Seek( s, i_start + i_block * i_align ) )
269                 {
270                     return VLC_EGENERIC;
271                 }
272                 return VLC_SUCCESS;
273             }
274             return VLC_EGENERIC;
275
276         case DEMUX_GET_FPS:
277         case DEMUX_GET_META:
278         case DEMUX_HAS_UNSUPPORTED_META:
279         case DEMUX_SET_NEXT_DEMUX_TIME:
280         case DEMUX_GET_TITLE_INFO:
281         case DEMUX_SET_GROUP:
282         case DEMUX_GET_ATTACHMENTS:
283         case DEMUX_CAN_RECORD:
284         case DEMUX_SET_RECORD_STATE:
285             return VLC_EGENERIC;
286
287         default:
288             msg_Err( s, "unknown query in demux_vaControlDefault" );
289             return VLC_EGENERIC;
290     }
291 }
292
293 /****************************************************************************
294  * stream_Demux*: create a demuxer for an outpout stream (allow demuxer chain)
295  ****************************************************************************/
296 typedef struct
297 {
298     /* Data buffer */
299     block_fifo_t *p_fifo;
300     block_t      *p_block;
301
302     int64_t     i_pos;
303
304     /* Demuxer */
305     char        *psz_name;
306     es_out_t    *out;
307     demux_t     *p_demux;
308
309 } d_stream_sys_t;
310
311 static int DStreamRead   ( stream_t *, void *p_read, unsigned int i_read );
312 static int DStreamPeek   ( stream_t *, const uint8_t **pp_peek, unsigned int i_peek );
313 static int DStreamControl( stream_t *, int i_query, va_list );
314 static void* DStreamThread ( vlc_object_t * );
315
316
317 stream_t *__stream_DemuxNew( vlc_object_t *p_obj, const char *psz_demux,
318                              es_out_t *out )
319 {
320     /* We create a stream reader, and launch a thread */
321     stream_t       *s;
322     d_stream_sys_t *p_sys;
323
324     if( psz_demux == NULL || *psz_demux == '\0' ) return NULL;
325
326     s = vlc_stream_create( p_obj );
327     if( s == NULL )
328         return NULL;
329     s->pf_read   = DStreamRead;
330     s->pf_peek   = DStreamPeek;
331     s->pf_control= DStreamControl;
332
333     s->i_char_width = 1;
334     s->b_little_endian = false;
335
336     s->p_sys = malloc( sizeof( d_stream_sys_t) );
337     if( s->p_sys == NULL )
338     {
339         vlc_object_release( s );
340         return NULL;
341     }
342     p_sys = (d_stream_sys_t*)s->p_sys;
343
344     p_sys->i_pos = 0;
345     p_sys->out = out;
346     p_sys->p_demux = NULL;
347     p_sys->p_block = NULL;
348     p_sys->psz_name = strdup( psz_demux );
349
350     /* decoder fifo */
351     if( ( p_sys->p_fifo = block_FifoNew() ) == NULL )
352     {
353         vlc_object_release( s );
354         free( p_sys->psz_name );
355         free( p_sys );
356         return NULL;
357     }
358
359     if( vlc_thread_create( s, "stream out", DStreamThread,
360                            VLC_THREAD_PRIORITY_INPUT, false ) )
361     {
362         vlc_object_release( s );
363         free( p_sys->psz_name );
364         free( p_sys );
365         return NULL;
366     }
367
368     return s;
369 }
370
371 void stream_DemuxSend( stream_t *s, block_t *p_block )
372 {
373     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
374     if( p_block ) block_FifoPut( p_sys->p_fifo, p_block );
375 }
376
377 void stream_DemuxDelete( stream_t *s )
378 {
379     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
380     block_t *p_empty;
381
382     vlc_object_kill( s );
383     if( p_sys->p_demux )
384         vlc_object_kill( p_sys->p_demux );
385     p_empty = block_New( s, 1 ); p_empty->i_buffer = 0;
386     block_FifoPut( p_sys->p_fifo, p_empty );
387     vlc_thread_join( s );
388
389     if( p_sys->p_demux ) demux_Delete( p_sys->p_demux );
390     if( p_sys->p_block ) block_Release( p_sys->p_block );
391
392     block_FifoRelease( p_sys->p_fifo );
393     free( p_sys->psz_name );
394     free( p_sys );
395
396     vlc_object_release( s );
397 }
398
399
400 static int DStreamRead( stream_t *s, void *p_read, unsigned int i_read )
401 {
402     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
403     uint8_t *p_out = p_read;
404     int i_out = 0;
405
406     //msg_Dbg( s, "DStreamRead: wanted %d bytes", i_read );
407
408     while( !s->b_die && !s->b_error && i_read )
409     {
410         block_t *p_block = p_sys->p_block;
411         int i_copy;
412
413         if( !p_block )
414         {
415             p_block = block_FifoGet( p_sys->p_fifo );
416             if( !p_block ) s->b_error = 1;
417             p_sys->p_block = p_block;
418         }
419
420         if( p_block && i_read )
421         {
422             i_copy = __MIN( i_read, p_block->i_buffer );
423             if( p_out && i_copy ) memcpy( p_out, p_block->p_buffer, i_copy );
424             i_read -= i_copy;
425             i_out += i_copy;
426             p_block->i_buffer -= i_copy;
427             p_block->p_buffer += i_copy;
428
429             if( !p_block->i_buffer )
430             {
431                 block_Release( p_block );
432                 p_sys->p_block = NULL;
433             }
434         }
435     }
436
437     p_sys->i_pos += i_out;
438     return i_out;
439 }
440
441 static int DStreamPeek( stream_t *s, const uint8_t **pp_peek, unsigned int i_peek )
442 {
443     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
444     block_t **pp_block = &p_sys->p_block;
445     int i_out = 0;
446     *pp_peek = 0;
447
448     //msg_Dbg( s, "DStreamPeek: wanted %d bytes", i_peek );
449
450     while( !s->b_die && !s->b_error && i_peek )
451     {
452         int i_copy;
453
454         if( !*pp_block )
455         {
456             *pp_block = block_FifoGet( p_sys->p_fifo );
457             if( !*pp_block ) s->b_error = 1;
458         }
459
460         if( *pp_block && i_peek )
461         {
462             i_copy = __MIN( i_peek, (*pp_block)->i_buffer );
463             i_peek -= i_copy;
464             i_out += i_copy;
465
466             if( i_peek ) pp_block = &(*pp_block)->p_next;
467         }
468     }
469
470     if( p_sys->p_block )
471     {
472         p_sys->p_block = block_ChainGather( p_sys->p_block );
473         *pp_peek = p_sys->p_block->p_buffer;
474     }
475
476     return i_out;
477 }
478
479 static int DStreamControl( stream_t *s, int i_query, va_list args )
480 {
481     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
482     int64_t    *p_i64;
483     bool *p_b;
484     int        *p_int;
485
486     switch( i_query )
487     {
488         case STREAM_GET_SIZE:
489             p_i64 = (int64_t*) va_arg( args, int64_t * );
490             *p_i64 = 0;
491             return VLC_SUCCESS;
492
493         case STREAM_CAN_SEEK:
494             p_b = (bool*) va_arg( args, bool * );
495             *p_b = false;
496             return VLC_SUCCESS;
497
498         case STREAM_CAN_FASTSEEK:
499             p_b = (bool*) va_arg( args, bool * );
500             *p_b = false;
501             return VLC_SUCCESS;
502
503         case STREAM_GET_POSITION:
504             p_i64 = (int64_t*) va_arg( args, int64_t * );
505             *p_i64 = p_sys->i_pos;
506             return VLC_SUCCESS;
507
508         case STREAM_SET_POSITION:
509         {
510             int64_t i64 = (int64_t)va_arg( args, int64_t );
511             int i_skip;
512             if( i64 < p_sys->i_pos ) return VLC_EGENERIC;
513             i_skip = i64 - p_sys->i_pos;
514
515             while( i_skip > 0 )
516             {
517                 int i_read = DStreamRead( s, NULL, (long)i_skip );
518                 if( i_read <= 0 ) return VLC_EGENERIC;
519                 i_skip -= i_read;
520             }
521             return VLC_SUCCESS;
522         }
523
524         case STREAM_GET_MTU:
525             p_int = (int*) va_arg( args, int * );
526             *p_int = 0;
527             return VLC_SUCCESS;
528
529         case STREAM_CONTROL_ACCESS:
530         case STREAM_GET_CONTENT_TYPE:
531         case STREAM_SET_RECORD_STATE:
532             return VLC_EGENERIC;
533
534         default:
535             msg_Err( s, "invalid DStreamControl query=0x%x", i_query );
536             return VLC_EGENERIC;
537     }
538 }
539
540 static void* DStreamThread( vlc_object_t* p_this )
541 {
542     stream_t *s = (stream_t *)p_this;
543     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
544     demux_t *p_demux;
545     int canc = vlc_savecancel ();
546
547     /* Create the demuxer */
548     if( !(p_demux = demux_New( s, "", p_sys->psz_name, "", s, p_sys->out,
549                                false )) )
550     {
551         return NULL;
552     }
553
554     p_sys->p_demux = p_demux;
555
556     /* Main loop */
557     while( !s->b_die && !p_demux->b_die )
558     {
559         if( p_demux->pf_demux( p_demux ) <= 0 ) break;
560     }
561
562     vlc_restorecancel (canc);
563     vlc_object_kill( p_demux );
564     return NULL;
565 }
566
567 /****************************************************************************
568  * Utility functions
569  ****************************************************************************/
570 static bool SkipID3Tag( demux_t *p_demux )
571 {
572     const uint8_t *p_peek;
573     uint8_t version, revision;
574     int i_size;
575     int b_footer;
576
577     if( !p_demux->s )
578         return false;
579
580     /* Get 10 byte id3 header */
581     if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
582         return false;
583
584     if( memcmp( p_peek, "ID3", 3 ) )
585         return false;
586
587     version = p_peek[3];
588     revision = p_peek[4];
589     b_footer = p_peek[5] & 0x10;
590     i_size = (p_peek[6]<<21) + (p_peek[7]<<14) + (p_peek[8]<<7) + p_peek[9];
591
592     if( b_footer ) i_size += 10;
593     i_size += 10;
594
595     /* Skip the entire tag */
596     stream_Read( p_demux->s, NULL, i_size );
597
598     msg_Dbg( p_demux, "ID3v2.%d revision %d tag found, skipping %d bytes",
599              version, revision, i_size );
600     return true;
601 }
602 static bool SkipAPETag( demux_t *p_demux )
603 {
604     const uint8_t *p_peek;
605     int i_version;
606     int i_size;
607     uint32_t flags;
608
609     if( !p_demux->s )
610         return false;
611
612     /* Get 32 byte ape header */
613     if( stream_Peek( p_demux->s, &p_peek, 32 ) < 32 )
614         return false;
615
616     if( memcmp( p_peek, "APETAGEX", 8 ) )
617         return false;
618
619     i_version = GetDWLE( &p_peek[8] );
620     flags = GetDWLE( &p_peek[8+4+4] );
621     if( ( i_version != 1000 && i_version != 2000 ) || !( flags & (1<<29) ) )
622         return false;
623
624     i_size = GetDWLE( &p_peek[8+4] ) + ( (flags&(1<<30)) ? 32 : 0 );
625
626     /* Skip the entire tag */
627     stream_Read( p_demux->s, NULL, i_size );
628
629     msg_Dbg( p_demux, "AP2 v%d tag found, skipping %d bytes",
630              i_version/1000, i_size );
631     return true;
632 }
633