]> git.sesse.net Git - vlc/blob - modules/access/cdda.c
Don't crash
[vlc] / modules / access / cdda.c
1 /*****************************************************************************
2  * cdda.c : CD digital audio input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include "codecs.h"
34 #include "vcd/cdrom.h"
35
36 #include <vlc_playlist.h>
37
38 /*****************************************************************************
39  * Module descriptior
40  *****************************************************************************/
41 static int  Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
43
44 #define CACHING_TEXT N_("Caching value in ms")
45 #define CACHING_LONGTEXT N_( \
46     "Allows you to modify the default caching value for cdda streams. This " \
47     "value should be set in milliseconds units." )
48
49 vlc_module_begin();
50     set_shortname( _("Audio CD"));
51     set_description( _("Audio CD input") );
52     set_capability( "access2", 10 );
53     set_category( CAT_INPUT );
54     set_subcategory( SUBCAT_INPUT_ACCESS );
55     set_callbacks( Open, Close );
56
57     add_usage_hint( N_("[cdda:][device][@[track]]") );
58     add_integer( "cdda-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
59                  CACHING_LONGTEXT, VLC_TRUE );
60     add_bool( "cdda-separate-tracks", VLC_TRUE, NULL, NULL, NULL, VLC_TRUE );
61     add_integer( "cdda-track", -1 , NULL, NULL, NULL, VLC_TRUE );
62     add_shortcut( "cdda" );
63     add_shortcut( "cddasimple" );
64 vlc_module_end();
65
66
67 /* how many blocks VCDRead will read in each loop */
68 #define CDDA_BLOCKS_ONCE 20
69 #define CDDA_DATA_ONCE   (CDDA_BLOCKS_ONCE * CDDA_DATA_SIZE)
70
71 /*****************************************************************************
72  * Access: local prototypes
73  *****************************************************************************/
74 struct access_sys_t
75 {
76     vcddev_t    *vcddev;                            /* vcd device descriptor */
77
78     /* Title infos */
79     int           i_titles;
80     input_title_t *title[99];         /* No more that 99 track in a cd-audio */
81
82     /* Current position */
83     int         i_sector;                                  /* Current Sector */
84     int *       p_sectors;                                  /* Track sectors */
85
86     /* Wave header for the output data */
87     WAVEHEADER  waveheader;
88     vlc_bool_t  b_header;
89
90     vlc_bool_t  b_separate_items;
91     vlc_bool_t  b_single_track;
92     int         i_track;
93 };
94
95 static block_t *Block( access_t * );
96 static int      Seek( access_t *, int64_t );
97 static int      Control( access_t *, int, va_list );
98
99 static int GetTracks( access_t *p_access, vlc_bool_t b_separate,
100                       playlist_t *p_playlist, playlist_item_t *p_parent );
101
102 /*****************************************************************************
103  * Open: open cdda
104  *****************************************************************************/
105 static int Open( vlc_object_t *p_this )
106 {
107     access_t     *p_access = (access_t*)p_this;
108     access_sys_t *p_sys;
109
110     vcddev_t *vcddev;
111     char *psz_name;
112
113     vlc_bool_t b_separate_requested;
114     vlc_bool_t b_play = VLC_FALSE;
115     input_thread_t *p_input;
116     playlist_item_t *p_item = NULL;
117     playlist_t *p_playlist  = NULL;
118     int i_ret;
119     int i_track;
120
121     if( !p_access->psz_path || !*p_access->psz_path )
122     {
123         /* Only when selected */
124         if( !p_this->b_force ) return VLC_EGENERIC;
125
126         psz_name = var_CreateGetString( p_this, "cd-audio" );
127         if( !psz_name || !*psz_name )
128         {
129             if( psz_name ) free( psz_name );
130             return VLC_EGENERIC;
131         }
132     }
133     else psz_name = strdup( p_access->psz_path );
134
135     /* Open CDDA */
136     if( (vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_name )) == NULL )
137     {
138         msg_Warn( p_access, "could not open %s", psz_name );
139         free( psz_name );
140         return VLC_EGENERIC;
141     }
142     free( psz_name );
143
144     /* Set up p_access */
145     p_access->pf_read = NULL;
146     p_access->pf_block = Block;
147     p_access->pf_control = Control;
148     p_access->pf_seek = Seek;
149     p_access->info.i_update = 0;
150     p_access->info.i_size = 0;
151     p_access->info.i_pos = 0;
152     p_access->info.b_eof = VLC_FALSE;
153     p_access->info.i_title = 0;
154     p_access->info.i_seekpoint = 0;
155     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
156     memset( p_sys, 0, sizeof( access_sys_t ) );
157     p_sys->vcddev = vcddev;
158     p_sys->b_separate_items = VLC_FALSE;
159     p_sys->b_single_track = VLC_FALSE;
160     p_sys->b_header = VLC_FALSE;
161
162     b_separate_requested = var_CreateGetBool( p_access,
163                                               "cdda-separate-tracks" );
164
165     /* We only do separate items if the whole disc is requested -
166      *  Dirty hack we access some private data ! */
167     p_input = (input_thread_t *)( p_access->p_parent );
168     /* Do we play a single track ? */
169     i_track = var_CreateGetInteger( p_access, "cdda-track" );
170     if( b_separate_requested && p_input->input.i_title_start == -1 &&
171         i_track <= 0 )
172     {
173         p_sys->b_separate_items = VLC_TRUE;
174     }
175     if( i_track > 0 )
176     {
177         p_sys->b_single_track = VLC_TRUE;
178         p_sys->i_track = i_track - 1;
179     }
180
181     msg_Dbg( p_access, "Separate items : %i - Single track : %i",
182                         p_sys->b_separate_items, p_sys->b_single_track );
183
184     if( p_sys->b_separate_items )
185     {
186         p_playlist = (playlist_t *) vlc_object_find( p_access,
187                                        VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
188
189         if( !p_playlist ) return VLC_EGENERIC;
190
191         /* Let's check if we need to play */
192         if( &p_playlist->status.p_item->input ==
193              ((input_thread_t *)p_access->p_parent)->input.p_item )
194         {
195             p_item = p_playlist->status.p_item;
196             b_play = VLC_TRUE;
197             msg_Dbg( p_access, "starting Audio CD playback");
198         }
199         else
200         {
201             input_item_t *p_current = ( (input_thread_t*)p_access->p_parent)->
202                                          input.p_item;
203             p_item = playlist_LockItemGetByInput( p_playlist, p_current );
204             msg_Dbg( p_access, "not starting Audio CD playback");
205
206             if( !p_item )
207             {
208                 msg_Dbg( p_playlist, "unable to find item in playlist");
209                return -1;
210             }
211             b_play = VLC_FALSE;
212         }
213     }
214
215     /* We read the Table Of Content information */
216     if( 1 )
217     {
218         i_ret = GetTracks( p_access, p_sys->b_separate_items,
219                            p_playlist, p_item );
220         if( i_ret < 0 )
221         {
222             goto error;
223         }
224     }
225
226     /* Build a WAV header for the output data */
227     memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
228     SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
229     SetWLE( &p_sys->waveheader.BitsPerSample, 16);
230     p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
231     p_sys->waveheader.Length = 0;                     /* we just don't know */
232     p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
233     p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
234     SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
235     SetWLE( &p_sys->waveheader.Modus, 2);
236     SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
237     SetWLE( &p_sys->waveheader.BytesPerSample,
238             2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
239     SetDWLE( &p_sys->waveheader.BytesPerSec,
240              2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
241     p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
242     p_sys->waveheader.DataLength = 0;                 /* we just don't know */
243
244     /* Only update META if we are in old mode */
245     if( !p_sys->b_separate_items && !p_sys->b_single_track )
246     {
247         p_access->info.i_update |= INPUT_UPDATE_META;
248     }
249
250     /* Position on the right sector and update size */
251     if( p_sys->b_single_track )
252     {
253         p_sys->i_sector = p_sys->p_sectors[ p_sys->i_track ];
254         p_access->info.i_size =
255             ( p_sys->p_sectors[p_sys->i_track+1] -
256               p_sys->p_sectors[p_sys->i_track] ) *
257                         (int64_t)CDDA_DATA_SIZE;
258     }
259
260     /* PTS delay */
261     var_Create( p_access, "cdda-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
262
263     if( b_play )
264     {
265         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
266                           p_playlist->status.i_view, p_playlist->status.p_item,
267                           NULL );
268     }
269     return VLC_SUCCESS;
270
271 error:
272     ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
273     free( p_sys );
274     return VLC_EGENERIC;
275 }
276
277 /*****************************************************************************
278  * Close: closes cdda
279  *****************************************************************************/
280 static void Close( vlc_object_t *p_this )
281 {
282     access_t     *p_access = (access_t *)p_this;
283     access_sys_t *p_sys = p_access->p_sys;
284     int          i;
285
286     for( i = 0; i < p_sys->i_titles; i++ )
287     {
288         vlc_input_title_Delete( p_sys->title[i] );
289     }
290
291     ioctl_Close( p_this, p_sys->vcddev );
292     free( p_sys );
293 }
294
295 /*****************************************************************************
296  * Block: read data (CDDA_DATA_ONCE)
297  *****************************************************************************/
298 static block_t *Block( access_t *p_access )
299 {
300     access_sys_t *p_sys = p_access->p_sys;
301     int i_blocks = CDDA_BLOCKS_ONCE;
302     block_t *p_block;
303
304     if( p_sys->b_separate_items ) p_access->info.b_eof = VLC_TRUE;
305
306     /* Check end of file */
307     if( p_access->info.b_eof ) return NULL;
308
309     if( !p_sys->b_header )
310     {
311         /* Return only the header */
312         p_block = block_New( p_access, sizeof( WAVEHEADER ) );
313         memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
314         p_sys->b_header = VLC_TRUE;
315         return p_block;
316     }
317
318     /* Check end of title - Single track */
319     if( p_sys->b_single_track )
320     {
321         if( p_sys->i_sector >= p_sys->p_sectors[p_sys->i_track + 1] )
322         {
323             p_access->info.b_eof = VLC_TRUE;
324             return NULL;
325         }
326         /* Don't read too far */
327         if( p_sys->i_sector + i_blocks >=
328             p_sys->p_sectors[p_sys->i_track +1] )
329         {
330             i_blocks = p_sys->p_sectors[p_sys->i_track+1] - p_sys->i_sector;
331         }
332     }
333     else
334     {
335         /* Check end of title - Normal */
336         while( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 1] )
337         {
338             if( p_access->info.i_title + 1 >= p_sys->i_titles )
339             {
340                 p_access->info.b_eof = VLC_TRUE;
341                 return NULL;
342             }
343
344             p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SIZE |
345                                        INPUT_UPDATE_META;
346             p_access->info.i_title++;
347             p_access->info.i_size =
348                         p_sys->title[p_access->info.i_title]->i_size;
349             p_access->info.i_pos = 0;
350         }
351         /* Don't read too far */
352         if( p_sys->i_sector + i_blocks >=                                                       p_sys->p_sectors[p_access->info.i_title + 1] )
353         {
354             i_blocks = p_sys->p_sectors[ p_access->info.i_title + 1] -
355                          p_sys->i_sector;
356         }
357     }
358
359     /* Do the actual reading */
360     if( !( p_block = block_New( p_access, i_blocks * CDDA_DATA_SIZE ) ) )
361     {
362         msg_Err( p_access, "cannot get a new block of size: %i",
363                  i_blocks * CDDA_DATA_SIZE );
364         return NULL;
365     }
366
367     if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
368             p_sys->i_sector, p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
369     {
370         msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
371         block_Release( p_block );
372
373         /* Try to skip one sector (in case of bad sectors) */
374         p_sys->i_sector++;
375         p_access->info.i_pos += CDDA_DATA_SIZE;
376         return NULL;
377     }
378
379     /* Update a few values */
380     p_sys->i_sector += i_blocks;
381     p_access->info.i_pos += p_block->i_buffer;
382
383     return p_block;
384 }
385
386 /****************************************************************************
387  * Seek
388  ****************************************************************************/
389 static int Seek( access_t *p_access, int64_t i_pos )
390 {
391     access_sys_t *p_sys = p_access->p_sys;
392
393     /* Next sector to read */
394     p_sys->i_sector = p_sys->p_sectors[
395                                         (p_sys->b_single_track ?
396                                          p_sys->i_track :
397                                          p_access->info.i_title + 1)
398                                       ] +
399                                 i_pos / CDDA_DATA_SIZE;
400     p_access->info.i_pos = i_pos;
401
402     return VLC_SUCCESS;
403 }
404
405 /*****************************************************************************
406  * Control:
407  *****************************************************************************/
408 static int Control( access_t *p_access, int i_query, va_list args )
409 {
410     access_sys_t *p_sys = p_access->p_sys;
411     vlc_bool_t   *pb_bool;
412     int          *pi_int;
413     int64_t      *pi_64;
414     input_title_t ***ppp_title;
415     int i;
416     char         *psz_title;
417     vlc_meta_t  **pp_meta;
418
419     switch( i_query )
420     {
421         /* */
422         case ACCESS_CAN_SEEK:
423         case ACCESS_CAN_FASTSEEK:
424         case ACCESS_CAN_PAUSE:
425         case ACCESS_CAN_CONTROL_PACE:
426             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
427             *pb_bool = VLC_TRUE;
428             break;
429
430         /* */
431         case ACCESS_GET_MTU:
432             pi_int = (int*)va_arg( args, int * );
433             *pi_int = CDDA_DATA_ONCE;
434             break;
435
436         case ACCESS_GET_PTS_DELAY:
437             pi_64 = (int64_t*)va_arg( args, int64_t * );
438             *pi_64 = var_GetInteger( p_access, "cdda-caching" ) * 1000;
439             break;
440
441         /* */
442         case ACCESS_SET_PAUSE_STATE:
443             break;
444
445         case ACCESS_GET_TITLE_INFO:
446             if( p_sys->b_single_track )
447                 return VLC_EGENERIC;
448             ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
449             pi_int    = (int*)va_arg( args, int* );
450             *((int*)va_arg( args, int* )) = 1; /* Title offset */
451
452             /* Duplicate title infos */
453             *pi_int = p_sys->i_titles;
454             *ppp_title = malloc(sizeof( input_title_t **) * p_sys->i_titles );
455             for( i = 0; i < p_sys->i_titles; i++ )
456             {
457                 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
458             }
459             break;
460
461         case ACCESS_SET_TITLE:
462             if( p_sys->b_single_track ) return VLC_EGENERIC;
463             i = (int)va_arg( args, int );
464             if( i != p_access->info.i_title )
465             {
466                 /* Update info */
467                 p_access->info.i_update |=
468                     INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE|INPUT_UPDATE_META;
469                 p_access->info.i_title = i;
470                 p_access->info.i_size = p_sys->title[i]->i_size;
471                 p_access->info.i_pos = 0;
472
473                 /* Next sector to read */
474                 p_sys->i_sector = p_sys->p_sectors[i];
475             }
476             break;
477
478         case ACCESS_GET_META:
479              if( p_sys->b_single_track ) return VLC_EGENERIC;
480              psz_title = malloc( strlen( _("Audio CD - Track ") ) + 5 );
481              snprintf( psz_title, 100, _("Audio CD - Track %i" ),
482                                         p_access->info.i_title+1 );
483              pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
484              *pp_meta = vlc_meta_New();
485              vlc_meta_Add( *pp_meta, VLC_META_TITLE, psz_title );
486              free( psz_title );
487              break;
488
489         case ACCESS_SET_SEEKPOINT:
490         case ACCESS_SET_PRIVATE_ID_STATE:
491             return VLC_EGENERIC;
492
493         default:
494             msg_Warn( p_access, "unimplemented query in control" );
495             return VLC_EGENERIC;
496
497     }
498     return VLC_SUCCESS;
499 }
500
501 static int GetTracks( access_t *p_access, vlc_bool_t b_separate,
502                       playlist_t *p_playlist, playlist_item_t *p_parent )
503 {
504     access_sys_t *p_sys = p_access->p_sys;
505     int i;
506     playlist_item_t *p_item;
507     p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
508                                           p_sys->vcddev, &p_sys->p_sectors );
509     if( p_sys->i_titles < 0 )
510     {
511         msg_Err( p_access, "unable to count tracks" );
512         return VLC_EGENERIC;;
513     }
514     else if( p_sys->i_titles <= 0 )
515     {
516         msg_Err( p_access, "no audio tracks found" );
517         return VLC_EGENERIC;
518     }
519
520     if( b_separate )
521     {
522         if( p_parent->i_children == -1 )
523         {
524             playlist_LockItemToNode( p_playlist, p_parent );
525         }
526     }
527
528     /* Build title table */
529     for( i = 0; i < p_sys->i_titles; i++ )
530     {
531         msg_Dbg( p_access, "track[%d] start=%d", i, p_sys->p_sectors[i] );
532         if( !b_separate )
533         {
534             input_title_t *t = p_sys->title[i] = vlc_input_title_New();
535
536             asprintf( &t->psz_name, _("Track %i"), i + 1 );
537             t->i_size = ( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
538                         (int64_t)CDDA_DATA_SIZE;
539
540             t->i_length = I64C(1000000) * t->i_size / 44100 / 4;
541         }
542         else
543         {
544             char *psz_uri;
545             int i_path_len = p_access->psz_path ? strlen( p_access->psz_path )
546                                                 : 0;
547             char *psz_name = malloc( strlen( _("Audio CD - Track ") ) + 5 );
548             char *psz_opt = (char*)malloc( 14 );
549
550             psz_uri = (char*)malloc( i_path_len + 13 );
551             snprintf( psz_uri, i_path_len + 13, "cdda://%s",
552                                 p_access->psz_path ? p_access->psz_path : "" );
553             snprintf( psz_name, 100, _("Audio CD - Track %i" ),
554                                      (i+1) );
555             snprintf( psz_opt, 14, "cdda-track=%i", i+1 );
556             /* Create playlist items */
557             p_item = playlist_ItemNewWithType( VLC_OBJECT( p_playlist ),
558                                  psz_uri, psz_name, ITEM_TYPE_DISC );
559             playlist_ItemAddOption( p_item, psz_opt );
560             playlist_NodeAddItem( p_playlist, p_item,
561                                   p_parent->pp_parents[0]->i_view,
562                                   p_parent, PLAYLIST_APPEND, PLAYLIST_END );
563             free( psz_uri ); free( psz_opt );
564         }
565     }
566
567     p_sys->i_sector = p_sys->p_sectors[0];
568     if( p_sys->b_separate_items )
569     {
570         p_sys->i_titles = 0;
571     }
572
573    return VLC_SUCCESS;
574 }