1 /*****************************************************************************
2 * cdda.c : CD digital audio input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2003-2006, 2008-2009 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 *****************************************************************************/
27 * - Improve CDDB support (non-blocking, ...)
28 * - Fix tracknumber in MRL
31 /*****************************************************************************
33 *****************************************************************************/
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_input.h>
43 #include <vlc_access.h>
45 #include <vlc_charset.h> /* ToLocaleDup */
47 #include <vlc_codecs.h> /* For WAVEHEADER */
48 #include "vcd/cdrom.h" /* For CDDA_DATA_SIZE */
51 #include <cddb/cddb.h>
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
62 set_shortname( N_("Audio CD") )
63 set_description( N_("Audio CD input") )
64 set_capability( "access", 10 )
65 set_category( CAT_INPUT )
66 set_subcategory( SUBCAT_INPUT_ACCESS )
67 set_callbacks( Open, Close )
69 add_usage_hint( N_("[cdda:][device][@[track]]") )
70 add_integer( "cdda-track", 0 , NULL, NULL, true )
72 add_integer( "cdda-first-sector", -1, NULL, NULL, true )
74 add_integer( "cdda-last-sector", -1, NULL, NULL, true )
78 add_string( "cddb-server", "freedb.videolan.org", N_( "CDDB Server" ),
79 N_( "Address of the CDDB server to use." ), true )
80 add_integer( "cddb-port", 80, N_( "CDDB port" ),
81 N_( "CDDB Server port to use." ), true )
82 change_integer_range( 1, 65535 )
85 add_shortcut( "cdda", "cddasimple" )
89 /* how many blocks VCDRead will read in each loop */
90 #define CDDA_BLOCKS_ONCE 20
91 #define CDDA_DATA_ONCE (CDDA_BLOCKS_ONCE * CDDA_DATA_SIZE)
93 /*****************************************************************************
94 * Access: local prototypes
95 *****************************************************************************/
98 vcddev_t *vcddev; /* vcd device descriptor */
100 /* Current position */
101 int i_sector; /* Current Sector */
102 int *p_sectors; /* Track sectors */
104 /* Wave header for the output data */
105 WAVEHEADER waveheader;
113 static block_t *Block( access_t * );
114 static int Seek( access_t *, uint64_t );
115 static int Control( access_t *, int, va_list );
117 static int GetTracks( access_t *p_access, input_item_t *p_current );
120 static cddb_disc_t *GetCDDBInfo( access_t *p_access, int i_titles, int *p_sectors );
123 /*****************************************************************************
125 *****************************************************************************/
126 static int Open( vlc_object_t *p_this )
128 access_t *p_access = (access_t*)p_this;
133 if( !p_access->psz_filepath || !*p_access->psz_filepath )
135 /* Only when selected */
136 if( !p_access->psz_access || !*p_access->psz_access )
139 psz_name = var_InheritString( p_this, "cd-audio" );
143 else psz_name = ToLocaleDup( p_access->psz_filepath );
145 #if defined( WIN32 ) || defined( __OS2__ )
146 if( psz_name[0] && psz_name[1] == ':' &&
147 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
151 if( (vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_name ) ) == NULL )
153 msg_Warn( p_access, "could not open %s", psz_name );
159 /* Set up p_access */
160 STANDARD_BLOCK_ACCESS_INIT
161 p_sys->vcddev = vcddev;
163 /* Do we play a single track ? */
164 p_sys->i_track = var_InheritInteger( p_access, "cdda-track" ) - 1;
166 if( p_sys->i_track < 0 )
168 /* We only do separate items if the whole disc is requested */
169 input_thread_t *p_input = access_GetParentInput( p_access );
174 input_item_t *p_current = input_GetItem( p_input );
176 i_ret = GetTracks( p_access, p_current );
178 vlc_object_release( p_input );
185 /* Build a WAV header for the output data */
186 memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
187 SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
188 SetWLE( &p_sys->waveheader.BitsPerSample, 16);
189 p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
190 p_sys->waveheader.Length = 0; /* we just don't know */
191 p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
192 p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
193 SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
194 SetWLE( &p_sys->waveheader.Modus, 2);
195 SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
196 SetWLE( &p_sys->waveheader.BytesPerSample,
197 2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
198 SetDWLE( &p_sys->waveheader.BytesPerSec,
199 2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
200 p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
201 p_sys->waveheader.DataLength = 0; /* we just don't know */
203 p_sys->i_first_sector = var_InheritInteger( p_access,
204 "cdda-first-sector" );
205 p_sys->i_last_sector = var_InheritInteger( p_access,
206 "cdda-last-sector" );
207 /* Tracknumber in MRL */
208 if( p_sys->i_first_sector < 0 || p_sys->i_last_sector < 0 )
210 const int i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
211 p_sys->vcddev, &p_sys->p_sectors );
212 if( p_sys->i_track >= i_titles )
214 msg_Err( p_access, "invalid track number" );
217 p_sys->i_first_sector = p_sys->p_sectors[p_sys->i_track];
218 p_sys->i_last_sector = p_sys->p_sectors[p_sys->i_track+1];
221 p_sys->i_sector = p_sys->i_first_sector;
222 p_access->info.i_size = (p_sys->i_last_sector - p_sys->i_first_sector)
223 * (int64_t)CDDA_DATA_SIZE;
229 free( p_sys->p_sectors );
230 ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
235 /*****************************************************************************
237 *****************************************************************************/
238 static void Close( vlc_object_t *p_this )
240 access_t *p_access = (access_t *)p_this;
241 access_sys_t *p_sys = p_access->p_sys;
243 free( p_sys->p_sectors );
244 ioctl_Close( p_this, p_sys->vcddev );
248 /*****************************************************************************
249 * Block: read data (CDDA_DATA_ONCE)
250 *****************************************************************************/
251 static block_t *Block( access_t *p_access )
253 access_sys_t *p_sys = p_access->p_sys;
254 int i_blocks = CDDA_BLOCKS_ONCE;
257 if( p_sys->i_track < 0 ) p_access->info.b_eof = true;
259 /* Check end of file */
260 if( p_access->info.b_eof ) return NULL;
262 if( !p_sys->b_header )
264 /* Return only the header */
265 p_block = block_New( p_access, sizeof( WAVEHEADER ) );
266 memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
267 p_sys->b_header = true;
271 if( p_sys->i_sector >= p_sys->i_last_sector )
273 p_access->info.b_eof = true;
277 /* Don't read too far */
278 if( p_sys->i_sector + i_blocks >= p_sys->i_last_sector )
279 i_blocks = p_sys->i_last_sector - p_sys->i_sector;
281 /* Do the actual reading */
282 if( !( p_block = block_New( p_access, i_blocks * CDDA_DATA_SIZE ) ) )
284 msg_Err( p_access, "cannot get a new block of size: %i",
285 i_blocks * CDDA_DATA_SIZE );
289 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
290 p_sys->i_sector, p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
292 msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
293 block_Release( p_block );
295 /* Try to skip one sector (in case of bad sectors) */
297 p_access->info.i_pos += CDDA_DATA_SIZE;
301 /* Update a few values */
302 p_sys->i_sector += i_blocks;
303 p_access->info.i_pos += p_block->i_buffer;
308 /****************************************************************************
310 ****************************************************************************/
311 static int Seek( access_t *p_access, uint64_t i_pos )
313 access_sys_t *p_sys = p_access->p_sys;
315 /* Next sector to read */
316 p_sys->i_sector = p_sys->i_first_sector + i_pos / CDDA_DATA_SIZE;
317 assert( p_sys->i_sector >= 0 );
318 p_access->info.i_pos = i_pos;
323 /*****************************************************************************
325 *****************************************************************************/
326 static int Control( access_t *p_access, int i_query, va_list args )
330 case ACCESS_CAN_SEEK:
331 case ACCESS_CAN_FASTSEEK:
332 case ACCESS_CAN_PAUSE:
333 case ACCESS_CAN_CONTROL_PACE:
334 *va_arg( args, bool* ) = true;
337 case ACCESS_GET_PTS_DELAY:
338 *va_arg( args, int64_t * ) =
339 INT64_C(1000) * var_InheritInteger( p_access, "disc-caching" );
342 case ACCESS_SET_PAUSE_STATE:
345 case ACCESS_GET_TITLE_INFO:
346 case ACCESS_SET_TITLE:
347 case ACCESS_GET_META:
348 case ACCESS_SET_SEEKPOINT:
349 case ACCESS_SET_PRIVATE_ID_STATE:
350 case ACCESS_GET_CONTENT_TYPE:
354 msg_Warn( p_access, "unimplemented query in control" );
360 static int GetTracks( access_t *p_access, input_item_t *p_current )
362 access_sys_t *p_sys = p_access->p_sys;
364 const int i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
365 p_sys->vcddev, &p_sys->p_sectors );
369 msg_Err( p_access, "unable to count tracks" );
370 else if( i_titles <= 0 )
371 msg_Err( p_access, "no audio tracks found" );
372 return VLC_EGENERIC;;
376 input_item_SetName( p_current, "Audio CD" );
378 const char *psz_album = NULL;
379 const char *psz_year = NULL;
380 const char *psz_genre = NULL;
381 const char *psz_artist = NULL;
382 const char *psz_description = NULL;
384 /* Return true if the given string is not NULL and not empty */
385 #define NONEMPTY( psz ) ( (psz) && *(psz) )
386 /* If the given string is NULL or empty, fill it by the return value of 'code' */
387 #define ON_EMPTY( psz, code ) do { if( !NONEMPTY( psz) ) { (psz) = code; } } while(0)
389 /* Retreive CDDB information */
391 char psz_year_buffer[4+1];
392 msg_Dbg( p_access, "fetching infos with CDDB" );
393 cddb_disc_t *p_disc = GetCDDBInfo( p_access, i_titles, p_sys->p_sectors );
396 psz_album = cddb_disc_get_title( p_disc );
397 psz_genre = cddb_disc_get_genre( p_disc );
400 const unsigned i_year = cddb_disc_get_year( p_disc );
403 psz_year = psz_year_buffer;
404 snprintf( psz_year_buffer, sizeof(psz_year_buffer), "%u", i_year );
407 /* Set artist only if unique */
408 for( int i = 0; i < i_titles; i++ )
410 cddb_track_t *t = cddb_disc_get_track( p_disc, i );
413 const char *psz_track_artist = cddb_track_get_artist( t );
414 if( psz_artist && psz_track_artist &&
415 strcmp( psz_artist, psz_track_artist ) )
420 psz_artist = psz_track_artist;
426 vlc_meta_t **pp_cd_text;
429 if( ioctl_GetCdText( VLC_OBJECT(p_access), p_sys->vcddev, &pp_cd_text, &i_cd_text ) )
431 msg_Dbg( p_access, "CD-TEXT information missing" );
436 /* Retrieve CD-TEXT information but prefer CDDB */
437 if( i_cd_text > 0 && pp_cd_text[0] )
439 const vlc_meta_t *p_disc = pp_cd_text[0];
440 ON_EMPTY( psz_album, vlc_meta_Get( p_disc, vlc_meta_Album ) );
441 ON_EMPTY( psz_genre, vlc_meta_Get( p_disc, vlc_meta_Genre ) );
442 ON_EMPTY( psz_artist, vlc_meta_Get( p_disc, vlc_meta_Artist ) );
443 ON_EMPTY( psz_description, vlc_meta_Get( p_disc, vlc_meta_Description ) );
446 if( NONEMPTY( psz_album ) )
448 input_item_SetName( p_current, psz_album );
449 input_item_SetAlbum( p_current, psz_album );
452 if( NONEMPTY( psz_genre ) )
453 input_item_SetGenre( p_current, psz_genre );
455 if( NONEMPTY( psz_artist ) )
456 input_item_SetArtist( p_current, psz_artist );
458 if( NONEMPTY( psz_year ) )
459 input_item_SetDate( p_current, psz_year );
461 if( NONEMPTY( psz_description ) )
462 input_item_SetDescription( p_current, psz_description );
464 const mtime_t i_duration = (int64_t)( p_sys->p_sectors[i_titles] - p_sys->p_sectors[0] ) *
465 CDDA_DATA_SIZE * 1000000 / 44100 / 2 / 2;
466 input_item_SetDuration( p_current, i_duration );
468 input_item_node_t *p_root = input_item_node_Create( p_current );
470 /* Build title table */
471 for( int i = 0; i < i_titles; i++ )
473 input_item_t *p_input_item;
475 char *psz_uri, *psz_opt, *psz_first, *psz_last;
478 msg_Dbg( p_access, "track[%d] start=%d", i, p_sys->p_sectors[i] );
481 if( asprintf( &psz_uri, "cdda://%s", p_access->psz_location ) == -1 )
483 if( asprintf( &psz_opt, "cdda-track=%i", i+1 ) == -1 )
485 if( asprintf( &psz_first, "cdda-first-sector=%i",p_sys->p_sectors[i] ) == -1 )
487 if( asprintf( &psz_last, "cdda-last-sector=%i", p_sys->p_sectors[i+1] ) == -1 )
490 /* Define a "default name" */
491 if( asprintf( &psz_name, _("Audio CD - Track %02i"), (i+1) ) == -1 )
494 /* Create playlist items */
495 const mtime_t i_duration = (int64_t)( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
496 CDDA_DATA_SIZE * 1000000 / 44100 / 2 / 2;
497 p_input_item = input_item_NewWithType( psz_uri, psz_name, 0, NULL, 0,
498 i_duration, ITEM_TYPE_DISC );
499 input_item_CopyOptions( p_current, p_input_item );
500 input_item_AddOption( p_input_item, psz_first, VLC_INPUT_OPTION_TRUSTED );
501 input_item_AddOption( p_input_item, psz_last, VLC_INPUT_OPTION_TRUSTED );
502 input_item_AddOption( p_input_item, psz_opt, VLC_INPUT_OPTION_TRUSTED );
504 const char *psz_track_title = NULL;
505 const char *psz_track_artist = NULL;
506 const char *psz_track_genre = NULL;
507 const char *psz_track_description = NULL;
510 /* Retreive CDDB information */
513 cddb_track_t *t = cddb_disc_get_track( p_disc, i );
516 psz_track_title = cddb_track_get_title( t );
517 psz_track_artist = cddb_track_get_artist( t );
522 /* Retreive CD-TEXT information but prefer CDDB */
523 if( i+1 < i_cd_text && pp_cd_text[i+1] )
525 const vlc_meta_t *t = pp_cd_text[i+1];
527 ON_EMPTY( psz_track_title, vlc_meta_Get( t, vlc_meta_Title ) );
528 ON_EMPTY( psz_track_artist, vlc_meta_Get( t, vlc_meta_Artist ) );
529 ON_EMPTY( psz_track_genre, vlc_meta_Get( t, vlc_meta_Genre ) );
530 ON_EMPTY( psz_track_description, vlc_meta_Get( t, vlc_meta_Description ) );
534 ON_EMPTY( psz_track_artist, psz_artist );
535 ON_EMPTY( psz_track_genre, psz_genre );
536 ON_EMPTY( psz_track_description, psz_description );
539 if( NONEMPTY( psz_track_title ) )
541 input_item_SetName( p_input_item, psz_track_title );
542 input_item_SetTitle( p_input_item, psz_track_title );
545 if( NONEMPTY( psz_track_artist ) )
546 input_item_SetArtist( p_input_item, psz_track_artist );
548 if( NONEMPTY( psz_track_genre ) )
549 input_item_SetGenre( p_input_item, psz_track_genre );
551 if( NONEMPTY( psz_track_description ) )
552 input_item_SetDescription( p_input_item, psz_track_description );
554 if( NONEMPTY( psz_album ) )
555 input_item_SetAlbum( p_input_item, psz_album );
557 if( NONEMPTY( psz_year ) )
558 input_item_SetDate( p_input_item, psz_year );
561 snprintf( psz_num, sizeof(psz_num), "%d", 1+i );
562 input_item_SetTrackNum( p_input_item, psz_num );
564 input_item_node_AppendItem( p_root, p_input_item );
565 vlc_gc_decref( p_input_item );
566 free( psz_uri ); free( psz_opt ); free( psz_name );
567 free( psz_first ); free( psz_last );
572 input_item_node_PostAndDelete( p_root );
575 for( int i = 0; i < i_cd_text; i++ )
577 vlc_meta_t *p_meta = pp_cd_text[i];
580 vlc_meta_Delete( p_meta );
586 cddb_disc_destroy( p_disc );
592 static cddb_disc_t *GetCDDBInfo( access_t *p_access, int i_titles, int *p_sectors )
594 if( var_InheritInteger( p_access, "album-art" ) == ALBUM_ART_WHEN_ASKED )
598 cddb_conn_t *p_cddb = cddb_new();
601 msg_Warn( p_access, "unable to use CDDB" );
607 cddb_http_enable( p_cddb );
609 char *psz_tmp = var_InheritString( p_access, "cddb-server" );
612 cddb_set_server_name( p_cddb, psz_tmp );
616 cddb_set_server_port( p_cddb, var_InheritInteger( p_access, "cddb-port" ) );
618 cddb_set_email_address( p_cddb, "vlc@videolan.org" );
620 cddb_set_http_path_query( p_cddb, "/~cddb/cddb.cgi" );
621 cddb_set_http_path_submit( p_cddb, "/~cddb/submit.cgi" );
625 char *psz_temp = config_GetUserDir( VLC_CACHE_DIR );
627 if( asprintf( &psz_cachedir, "%s" DIR_SEP "cddb", psz_temp ) > 0 ) {
628 cddb_cache_enable( p_cddb );
629 cddb_cache_set_dir( p_cddb, psz_cachedir );
630 free( psz_cachedir );
634 cddb_set_timeout( p_cddb, 10 );
637 cddb_disc_t *p_disc = cddb_disc_new();
640 msg_Err( p_access, "unable to create CDDB disc structure." );
644 int64_t i_length = 0;
645 for( int i = 0; i < i_titles; i++ )
647 cddb_track_t *t = cddb_track_new();
648 cddb_track_set_frame_offset( t, p_sectors[i] );
649 cddb_disc_add_track( p_disc, t );
650 const int64_t i_size = ( p_sectors[i+1] - p_sectors[i] ) *
651 (int64_t)CDDA_DATA_SIZE;
652 i_length += INT64_C(1000000) * i_size / 44100 / 4 ;
655 cddb_disc_set_length( p_disc, (int)(i_length/1000000) );
657 if( !cddb_disc_calc_discid( p_disc ) )
659 msg_Err( p_access, "CDDB disc ID calculation failed" );
663 const int i_matches = cddb_query( p_cddb, p_disc );
666 msg_Warn( p_access, "CDDB error: %s", cddb_error_str(errno) );
669 else if( i_matches == 0 )
671 msg_Dbg( p_access, "Couldn't find any matches in CDDB." );
674 else if( i_matches > 1 )
675 msg_Warn( p_access, "found %d matches in CDDB. Using first one.", i_matches );
677 cddb_read( p_cddb, p_disc );
679 cddb_destroy( p_cddb);
684 cddb_disc_destroy( p_disc );
685 cddb_destroy( p_cddb );
688 #endif /*HAVE_LIBCDDB*/