1 /*****************************************************************************
2 * cdda.c : CD digital audio input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2003 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #include <vlc/input.h>
34 #include "vcd/cdrom.h"
36 #include <vlc_playlist.h>
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
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." )
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 );
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" );
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)
71 /*****************************************************************************
72 * Access: local prototypes
73 *****************************************************************************/
76 vcddev_t *vcddev; /* vcd device descriptor */
80 input_title_t *title[99]; /* No more that 99 track in a cd-audio */
82 /* Current position */
83 int i_sector; /* Current Sector */
84 int * p_sectors; /* Track sectors */
86 /* Wave header for the output data */
87 WAVEHEADER waveheader;
90 vlc_bool_t b_separate_items;
91 vlc_bool_t b_single_track;
95 static block_t *Block( access_t * );
96 static int Seek( access_t *, int64_t );
97 static int Control( access_t *, int, va_list );
99 static int GetTracks( access_t *p_access, vlc_bool_t b_separate,
100 playlist_t *p_playlist, playlist_item_t *p_parent );
102 /*****************************************************************************
104 *****************************************************************************/
105 static int Open( vlc_object_t *p_this )
107 access_t *p_access = (access_t*)p_this;
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;
121 if( !p_access->psz_path || !*p_access->psz_path )
123 /* Only when selected */
124 if( !p_this->b_force ) return VLC_EGENERIC;
126 psz_name = var_CreateGetString( p_this, "cd-audio" );
127 if( !psz_name || !*psz_name )
129 if( psz_name ) free( psz_name );
133 else psz_name = strdup( p_access->psz_path );
136 if( (vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_name )) == NULL )
138 msg_Warn( p_access, "could not open %s", psz_name );
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;
162 b_separate_requested = var_CreateGetBool( p_access,
163 "cdda-separate-tracks" );
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 &&
173 p_sys->b_separate_items = VLC_TRUE;
177 p_sys->b_single_track = VLC_TRUE;
178 p_sys->i_track = i_track - 1;
181 msg_Dbg( p_access, "Separate items : %i - Single track : %i",
182 p_sys->b_separate_items, p_sys->b_single_track );
184 if( p_sys->b_separate_items )
186 p_playlist = (playlist_t *) vlc_object_find( p_access,
187 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
189 if( !p_playlist ) return VLC_EGENERIC;
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 )
195 p_item = p_playlist->status.p_item;
197 msg_Dbg( p_access, "starting Audio CD playback");
201 input_item_t *p_current = ( (input_thread_t*)p_access->p_parent)->
203 p_item = playlist_LockItemGetByInput( p_playlist, p_current );
204 msg_Dbg( p_access, "not starting Audio CD playback");
208 msg_Dbg( p_playlist, "unable to find item in playlist");
215 /* We read the Table Of Content information */
218 i_ret = GetTracks( p_access, p_sys->b_separate_items,
219 p_playlist, p_item );
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 */
244 /* Only update META if we are in old mode */
245 if( !p_sys->b_separate_items && !p_sys->b_single_track )
247 p_access->info.i_update |= INPUT_UPDATE_META;
250 /* Position on the right sector and update size */
251 if( p_sys->b_single_track )
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;
261 var_Create( p_access, "cdda-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
265 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
266 p_playlist->status.i_view, p_playlist->status.p_item,
270 if( p_playlist ) vlc_object_release( p_playlist );
275 ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
277 if( p_playlist ) vlc_object_release( p_playlist );
281 /*****************************************************************************
283 *****************************************************************************/
284 static void Close( vlc_object_t *p_this )
286 access_t *p_access = (access_t *)p_this;
287 access_sys_t *p_sys = p_access->p_sys;
290 for( i = 0; i < p_sys->i_titles; i++ )
292 vlc_input_title_Delete( p_sys->title[i] );
295 ioctl_Close( p_this, p_sys->vcddev );
299 /*****************************************************************************
300 * Block: read data (CDDA_DATA_ONCE)
301 *****************************************************************************/
302 static block_t *Block( access_t *p_access )
304 access_sys_t *p_sys = p_access->p_sys;
305 int i_blocks = CDDA_BLOCKS_ONCE;
308 if( p_sys->b_separate_items ) p_access->info.b_eof = VLC_TRUE;
310 /* Check end of file */
311 if( p_access->info.b_eof ) return NULL;
313 if( !p_sys->b_header )
315 /* Return only the header */
316 p_block = block_New( p_access, sizeof( WAVEHEADER ) );
317 memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
318 p_sys->b_header = VLC_TRUE;
322 /* Check end of title - Single track */
323 if( p_sys->b_single_track )
325 if( p_sys->i_sector >= p_sys->p_sectors[p_sys->i_track + 1] )
327 p_access->info.b_eof = VLC_TRUE;
330 /* Don't read too far */
331 if( p_sys->i_sector + i_blocks >=
332 p_sys->p_sectors[p_sys->i_track +1] )
334 i_blocks = p_sys->p_sectors[p_sys->i_track+1] - p_sys->i_sector;
339 /* Check end of title - Normal */
340 while( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 1] )
342 if( p_access->info.i_title + 1 >= p_sys->i_titles )
344 p_access->info.b_eof = VLC_TRUE;
348 p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SIZE |
350 p_access->info.i_title++;
351 p_access->info.i_size =
352 p_sys->title[p_access->info.i_title]->i_size;
353 p_access->info.i_pos = 0;
355 /* Don't read too far */
356 if( p_sys->i_sector + i_blocks >= p_sys->p_sectors[p_access->info.i_title + 1] )
358 i_blocks = p_sys->p_sectors[ p_access->info.i_title + 1] -
363 /* Do the actual reading */
364 if( !( p_block = block_New( p_access, i_blocks * CDDA_DATA_SIZE ) ) )
366 msg_Err( p_access, "cannot get a new block of size: %i",
367 i_blocks * CDDA_DATA_SIZE );
371 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
372 p_sys->i_sector, p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
374 msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
375 block_Release( p_block );
377 /* Try to skip one sector (in case of bad sectors) */
379 p_access->info.i_pos += CDDA_DATA_SIZE;
383 /* Update a few values */
384 p_sys->i_sector += i_blocks;
385 p_access->info.i_pos += p_block->i_buffer;
390 /****************************************************************************
392 ****************************************************************************/
393 static int Seek( access_t *p_access, int64_t i_pos )
395 access_sys_t *p_sys = p_access->p_sys;
397 /* Next sector to read */
398 p_sys->i_sector = p_sys->p_sectors[
399 (p_sys->b_single_track ?
401 p_access->info.i_title + 1)
403 i_pos / CDDA_DATA_SIZE;
404 p_access->info.i_pos = i_pos;
409 /*****************************************************************************
411 *****************************************************************************/
412 static int Control( access_t *p_access, int i_query, va_list args )
414 access_sys_t *p_sys = p_access->p_sys;
418 input_title_t ***ppp_title;
421 vlc_meta_t **pp_meta;
426 case ACCESS_CAN_SEEK:
427 case ACCESS_CAN_FASTSEEK:
428 case ACCESS_CAN_PAUSE:
429 case ACCESS_CAN_CONTROL_PACE:
430 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
436 pi_int = (int*)va_arg( args, int * );
437 *pi_int = CDDA_DATA_ONCE;
440 case ACCESS_GET_PTS_DELAY:
441 pi_64 = (int64_t*)va_arg( args, int64_t * );
442 *pi_64 = var_GetInteger( p_access, "cdda-caching" ) * 1000;
446 case ACCESS_SET_PAUSE_STATE:
449 case ACCESS_GET_TITLE_INFO:
450 if( p_sys->b_single_track )
452 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
453 pi_int = (int*)va_arg( args, int* );
454 *((int*)va_arg( args, int* )) = 1; /* Title offset */
456 /* Duplicate title infos */
457 *pi_int = p_sys->i_titles;
458 *ppp_title = malloc(sizeof( input_title_t **) * p_sys->i_titles );
459 for( i = 0; i < p_sys->i_titles; i++ )
461 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
465 case ACCESS_SET_TITLE:
466 if( p_sys->b_single_track ) return VLC_EGENERIC;
467 i = (int)va_arg( args, int );
468 if( i != p_access->info.i_title )
471 p_access->info.i_update |=
472 INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE|INPUT_UPDATE_META;
473 p_access->info.i_title = i;
474 p_access->info.i_size = p_sys->title[i]->i_size;
475 p_access->info.i_pos = 0;
477 /* Next sector to read */
478 p_sys->i_sector = p_sys->p_sectors[i];
482 case ACCESS_GET_META:
483 if( p_sys->b_single_track ) return VLC_EGENERIC;
484 psz_title = malloc( strlen( _("Audio CD - Track ") ) + 5 );
485 snprintf( psz_title, 100, _("Audio CD - Track %i" ),
486 p_access->info.i_title+1 );
487 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
488 *pp_meta = vlc_meta_New();
489 vlc_meta_Add( *pp_meta, VLC_META_TITLE, psz_title );
493 case ACCESS_SET_SEEKPOINT:
494 case ACCESS_SET_PRIVATE_ID_STATE:
498 msg_Warn( p_access, "unimplemented query in control" );
505 static int GetTracks( access_t *p_access, vlc_bool_t b_separate,
506 playlist_t *p_playlist, playlist_item_t *p_parent )
508 access_sys_t *p_sys = p_access->p_sys;
510 playlist_item_t *p_item;
511 p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
512 p_sys->vcddev, &p_sys->p_sectors );
513 if( p_sys->i_titles < 0 )
515 msg_Err( p_access, "unable to count tracks" );
516 return VLC_EGENERIC;;
518 else if( p_sys->i_titles <= 0 )
520 msg_Err( p_access, "no audio tracks found" );
526 if( p_parent->i_children == -1 )
528 playlist_LockItemToNode( p_playlist, p_parent );
532 /* Build title table */
533 for( i = 0; i < p_sys->i_titles; i++ )
535 msg_Dbg( p_access, "track[%d] start=%d", i, p_sys->p_sectors[i] );
538 input_title_t *t = p_sys->title[i] = vlc_input_title_New();
540 asprintf( &t->psz_name, _("Track %i"), i + 1 );
541 t->i_size = ( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
542 (int64_t)CDDA_DATA_SIZE;
544 t->i_length = I64C(1000000) * t->i_size / 44100 / 4;
549 int i_path_len = p_access->psz_path ? strlen( p_access->psz_path )
551 char *psz_name = malloc( strlen( _("Audio CD - Track ") ) + 5 );
552 char *psz_opt = (char*)malloc( 14 );
554 psz_uri = (char*)malloc( i_path_len + 13 );
555 snprintf( psz_uri, i_path_len + 13, "cdda://%s",
556 p_access->psz_path ? p_access->psz_path : "" );
557 snprintf( psz_name, 100, _("Audio CD - Track %i" ),
559 snprintf( psz_opt, 14, "cdda-track=%i", i+1 );
560 /* Create playlist items */
561 p_item = playlist_ItemNewWithType( VLC_OBJECT( p_playlist ),
562 psz_uri, psz_name, ITEM_TYPE_DISC );
563 playlist_ItemAddOption( p_item, psz_opt );
564 playlist_NodeAddItem( p_playlist, p_item,
565 p_parent->pp_parents[0]->i_view,
566 p_parent, PLAYLIST_APPEND, PLAYLIST_END );
567 free( psz_uri ); free( psz_opt );
571 p_sys->i_sector = p_sys->p_sectors[0];
572 if( p_sys->b_separate_items )