1 /*****************************************************************************
2 * cdda.c : CD digital audio input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2003-2006, 2008-2009 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * 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 */
101 /* Current position */
102 int i_sector; /* Current Sector */
103 int *p_sectors; /* Track sectors */
105 /* Wave header for the output data */
106 WAVEHEADER waveheader;
114 static block_t *Block( access_t * );
115 static int Seek( access_t *, uint64_t );
116 static int Control( access_t *, int, va_list );
118 static int GetTracks( access_t *p_access, input_item_t *p_current );
121 static cddb_disc_t *GetCDDBInfo( access_t *p_access, int i_titles, int *p_sectors );
124 /*****************************************************************************
126 *****************************************************************************/
127 static int Open( vlc_object_t *p_this )
129 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';
150 access_sys_t *p_sys = calloc( 1, sizeof (*p_sys) );
151 if( unlikely(p_sys == NULL) )
156 p_access->p_sys = p_sys;
159 vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_name );
161 msg_Warn( p_access, "could not open %s", psz_name );
169 p_sys->vcddev = vcddev;
171 /* Do we play a single track ? */
172 p_sys->i_track = var_InheritInteger( p_access, "cdda-track" ) - 1;
174 if( p_sys->i_track < 0 )
176 /* We only do separate items if the whole disc is requested */
177 input_thread_t *p_input = access_GetParentInput( p_access );
182 input_item_t *p_current = input_GetItem( p_input );
184 i_ret = GetTracks( p_access, p_current );
186 vlc_object_release( p_input );
193 /* Build a WAV header for the output data */
194 memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
195 SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
196 SetWLE( &p_sys->waveheader.BitsPerSample, 16);
197 p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
198 p_sys->waveheader.Length = 0; /* we just don't know */
199 p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
200 p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
201 SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
202 SetWLE( &p_sys->waveheader.Modus, 2);
203 SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
204 SetWLE( &p_sys->waveheader.BytesPerSample,
205 2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
206 SetDWLE( &p_sys->waveheader.BytesPerSec,
207 2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
208 p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
209 p_sys->waveheader.DataLength = 0; /* we just don't know */
211 p_sys->i_first_sector = var_InheritInteger( p_access,
212 "cdda-first-sector" );
213 p_sys->i_last_sector = var_InheritInteger( p_access,
214 "cdda-last-sector" );
215 /* Tracknumber in MRL */
216 if( p_sys->i_first_sector < 0 || p_sys->i_last_sector < 0 )
218 const int i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
219 p_sys->vcddev, &p_sys->p_sectors );
220 if( p_sys->i_track >= i_titles )
222 msg_Err( p_access, "invalid track number" );
225 p_sys->i_first_sector = p_sys->p_sectors[p_sys->i_track];
226 p_sys->i_last_sector = p_sys->p_sectors[p_sys->i_track+1];
229 p_sys->i_sector = p_sys->i_first_sector;
230 p_sys->size = (p_sys->i_last_sector - p_sys->i_first_sector)
231 * (int64_t)CDDA_DATA_SIZE;
234 /* Set up p_access */
235 access_InitFields( p_access );
236 ACCESS_SET_CALLBACKS( NULL, Block, Control, Seek );
240 free( p_sys->p_sectors );
241 ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
246 /*****************************************************************************
248 *****************************************************************************/
249 static void Close( vlc_object_t *p_this )
251 access_t *p_access = (access_t *)p_this;
252 access_sys_t *p_sys = p_access->p_sys;
254 free( p_sys->p_sectors );
255 ioctl_Close( p_this, p_sys->vcddev );
259 /*****************************************************************************
260 * Block: read data (CDDA_DATA_ONCE)
261 *****************************************************************************/
262 static block_t *Block( access_t *p_access )
264 access_sys_t *p_sys = p_access->p_sys;
265 int i_blocks = CDDA_BLOCKS_ONCE;
268 if( p_sys->i_track < 0 ) p_access->info.b_eof = true;
270 /* Check end of file */
271 if( p_access->info.b_eof ) return NULL;
273 if( !p_sys->b_header )
275 /* Return only the header */
276 p_block = block_Alloc( sizeof( WAVEHEADER ) );
277 memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
278 p_sys->b_header = true;
282 if( p_sys->i_sector >= p_sys->i_last_sector )
284 p_access->info.b_eof = true;
288 /* Don't read too far */
289 if( p_sys->i_sector + i_blocks >= p_sys->i_last_sector )
290 i_blocks = p_sys->i_last_sector - p_sys->i_sector;
292 /* Do the actual reading */
293 if( !( p_block = block_Alloc( i_blocks * CDDA_DATA_SIZE ) ) )
295 msg_Err( p_access, "cannot get a new block of size: %i",
296 i_blocks * CDDA_DATA_SIZE );
300 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
301 p_sys->i_sector, p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
303 msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
304 block_Release( p_block );
306 /* Try to skip one sector (in case of bad sectors) */
308 p_access->info.i_pos += CDDA_DATA_SIZE;
312 /* Update a few values */
313 p_sys->i_sector += i_blocks;
314 p_access->info.i_pos += p_block->i_buffer;
319 /****************************************************************************
321 ****************************************************************************/
322 static int Seek( access_t *p_access, uint64_t i_pos )
324 access_sys_t *p_sys = p_access->p_sys;
326 /* Next sector to read */
327 p_sys->i_sector = p_sys->i_first_sector + i_pos / CDDA_DATA_SIZE;
328 assert( p_sys->i_sector >= 0 );
329 p_access->info.i_pos = i_pos;
334 /*****************************************************************************
336 *****************************************************************************/
337 static int Control( access_t *p_access, int i_query, va_list args )
341 case ACCESS_CAN_SEEK:
342 case ACCESS_CAN_FASTSEEK:
343 case ACCESS_CAN_PAUSE:
344 case ACCESS_CAN_CONTROL_PACE:
345 *va_arg( args, bool* ) = true;
347 case ACCESS_GET_SIZE:
348 *va_arg( args, uint64_t * ) = p_access->p_sys->size;
350 case ACCESS_GET_PTS_DELAY:
351 *va_arg( args, int64_t * ) =
352 INT64_C(1000) * var_InheritInteger( p_access, "disc-caching" );
355 case ACCESS_SET_PAUSE_STATE:
364 static int GetTracks( access_t *p_access, input_item_t *p_current )
366 access_sys_t *p_sys = p_access->p_sys;
368 const int i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
369 p_sys->vcddev, &p_sys->p_sectors );
373 msg_Err( p_access, "unable to count tracks" );
374 else if( i_titles <= 0 )
375 msg_Err( p_access, "no audio tracks found" );
376 return VLC_EGENERIC;;
380 input_item_SetName( p_current, "Audio CD" );
382 const char *psz_album = NULL;
383 const char *psz_year = NULL;
384 const char *psz_genre = NULL;
385 const char *psz_artist = NULL;
386 const char *psz_description = NULL;
388 /* Return true if the given string is not NULL and not empty */
389 #define NONEMPTY( psz ) ( (psz) && *(psz) )
390 /* If the given string is NULL or empty, fill it by the return value of 'code' */
391 #define ON_EMPTY( psz, code ) do { if( !NONEMPTY( psz) ) { (psz) = code; } } while(0)
393 /* Retreive CDDB information */
395 char psz_year_buffer[4+1];
396 msg_Dbg( p_access, "fetching infos with CDDB" );
397 cddb_disc_t *p_disc = GetCDDBInfo( p_access, i_titles, p_sys->p_sectors );
400 msg_Dbg( p_access, "Disc ID: %08x", cddb_disc_get_discid( p_disc ) );
401 psz_album = cddb_disc_get_title( p_disc );
402 psz_genre = cddb_disc_get_genre( p_disc );
405 const unsigned i_year = cddb_disc_get_year( p_disc );
408 psz_year = psz_year_buffer;
409 snprintf( psz_year_buffer, sizeof(psz_year_buffer), "%u", i_year );
412 /* Set artist only if unique */
413 for( int i = 0; i < i_titles; i++ )
415 cddb_track_t *t = cddb_disc_get_track( p_disc, i );
418 const char *psz_track_artist = cddb_track_get_artist( t );
419 if( psz_artist && psz_track_artist &&
420 strcmp( psz_artist, psz_track_artist ) )
425 psz_artist = psz_track_artist;
429 msg_Dbg( p_access, "GetCDDBInfo failed" );
433 vlc_meta_t **pp_cd_text;
436 if( ioctl_GetCdText( VLC_OBJECT(p_access), p_sys->vcddev, &pp_cd_text, &i_cd_text ) )
438 msg_Dbg( p_access, "CD-TEXT information missing" );
443 /* Retrieve CD-TEXT information but prefer CDDB */
444 if( i_cd_text > 0 && pp_cd_text[0] )
446 const vlc_meta_t *p_disc = pp_cd_text[0];
447 ON_EMPTY( psz_album, vlc_meta_Get( p_disc, vlc_meta_Album ) );
448 ON_EMPTY( psz_genre, vlc_meta_Get( p_disc, vlc_meta_Genre ) );
449 ON_EMPTY( psz_artist, vlc_meta_Get( p_disc, vlc_meta_Artist ) );
450 ON_EMPTY( psz_description, vlc_meta_Get( p_disc, vlc_meta_Description ) );
453 if( NONEMPTY( psz_album ) )
455 input_item_SetName( p_current, psz_album );
456 input_item_SetAlbum( p_current, psz_album );
459 if( NONEMPTY( psz_genre ) )
460 input_item_SetGenre( p_current, psz_genre );
462 if( NONEMPTY( psz_artist ) )
463 input_item_SetArtist( p_current, psz_artist );
465 if( NONEMPTY( psz_year ) )
466 input_item_SetDate( p_current, psz_year );
468 if( NONEMPTY( psz_description ) )
469 input_item_SetDescription( p_current, psz_description );
471 const mtime_t i_duration = (int64_t)( p_sys->p_sectors[i_titles] - p_sys->p_sectors[0] ) *
472 CDDA_DATA_SIZE * 1000000 / 44100 / 2 / 2;
473 input_item_SetDuration( p_current, i_duration );
475 input_item_node_t *p_root = input_item_node_Create( p_current );
477 /* Build title table */
478 for( int i = 0; i < i_titles; i++ )
480 char *psz_uri, *psz_opt, *psz_name;
482 msg_Dbg( p_access, "track[%d] start=%d", i, p_sys->p_sectors[i] );
484 if( asprintf( &psz_uri, "cdda://%s", p_access->psz_location ) == -1 )
487 /* Define a "default name" */
488 if( asprintf( &psz_name, _("Audio CD - Track %02i"), (i+1) ) == -1 )
491 /* Create playlist items */
492 const mtime_t i_duration = (int64_t)( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
493 CDDA_DATA_SIZE * 1000000 / 44100 / 2 / 2;
495 input_item_t *p_item = input_item_NewWithType( psz_uri, psz_name, 0,
496 NULL, 0, i_duration, ITEM_TYPE_DISC );
497 if( likely(psz_name != psz_uri) )
501 if( unlikely(p_item == NULL) )
504 input_item_CopyOptions( p_current, p_item );
506 if( likely(asprintf( &psz_opt, "cdda-track=%i", i+1 ) != -1) )
508 input_item_AddOption( p_item, psz_opt, VLC_INPUT_OPTION_TRUSTED );
511 if( likely(asprintf( &psz_opt, "cdda-first-sector=%i",
512 p_sys->p_sectors[i] ) != -1) )
514 input_item_AddOption( p_item, psz_opt, VLC_INPUT_OPTION_TRUSTED );
517 if( likely(asprintf( &psz_opt, "cdda-last-sector=%i",
518 p_sys->p_sectors[i+1] ) != -1) )
520 input_item_AddOption( p_item, psz_opt, VLC_INPUT_OPTION_TRUSTED );
524 const char *psz_track_title = NULL;
525 const char *psz_track_artist = NULL;
526 const char *psz_track_genre = NULL;
527 const char *psz_track_description = NULL;
530 /* Retreive CDDB information */
533 cddb_track_t *t = cddb_disc_get_track( p_disc, i );
536 psz_track_title = cddb_track_get_title( t );
537 psz_track_artist = cddb_track_get_artist( t );
542 /* Retreive CD-TEXT information but prefer CDDB */
543 if( i+1 < i_cd_text && pp_cd_text[i+1] )
545 const vlc_meta_t *t = pp_cd_text[i+1];
547 ON_EMPTY( psz_track_title, vlc_meta_Get( t, vlc_meta_Title ) );
548 ON_EMPTY( psz_track_artist, vlc_meta_Get( t, vlc_meta_Artist ) );
549 ON_EMPTY( psz_track_genre, vlc_meta_Get( t, vlc_meta_Genre ) );
550 ON_EMPTY( psz_track_description, vlc_meta_Get( t, vlc_meta_Description ) );
554 ON_EMPTY( psz_track_artist, psz_artist );
555 ON_EMPTY( psz_track_genre, psz_genre );
556 ON_EMPTY( psz_track_description, psz_description );
559 if( NONEMPTY( psz_track_title ) )
561 input_item_SetName( p_item, psz_track_title );
562 input_item_SetTitle( p_item, psz_track_title );
565 if( NONEMPTY( psz_track_artist ) )
566 input_item_SetArtist( p_item, psz_track_artist );
568 if( NONEMPTY( psz_track_genre ) )
569 input_item_SetGenre( p_item, psz_track_genre );
571 if( NONEMPTY( psz_track_description ) )
572 input_item_SetDescription( p_item, psz_track_description );
574 if( NONEMPTY( psz_album ) )
575 input_item_SetAlbum( p_item, psz_album );
577 if( NONEMPTY( psz_year ) )
578 input_item_SetDate( p_item, psz_year );
581 snprintf( psz_num, sizeof(psz_num), "%d", 1+i );
582 input_item_SetTrackNum( p_item, psz_num );
584 input_item_node_AppendItem( p_root, p_item );
585 vlc_gc_decref( p_item );
590 input_item_node_PostAndDelete( p_root );
593 for( int i = 0; i < i_cd_text; i++ )
595 vlc_meta_t *p_meta = pp_cd_text[i];
598 vlc_meta_Delete( p_meta );
604 cddb_disc_destroy( p_disc );
610 static cddb_disc_t *GetCDDBInfo( access_t *p_access, int i_titles, int *p_sectors )
612 if( var_InheritInteger( p_access, "album-art" ) != ALBUM_ART_ALL &&
613 ! var_InheritBool( p_access, "metadata-network-access" ) )
615 msg_Dbg( p_access, "Album art policy set to manual; no automatic fetching" );
620 cddb_conn_t *p_cddb = cddb_new();
623 msg_Warn( p_access, "unable to use CDDB" );
629 cddb_http_enable( p_cddb );
631 char *psz_tmp = var_InheritString( p_access, "cddb-server" );
634 cddb_set_server_name( p_cddb, psz_tmp );
638 cddb_set_server_port( p_cddb, var_InheritInteger( p_access, "cddb-port" ) );
640 cddb_set_email_address( p_cddb, "vlc@videolan.org" );
642 cddb_set_http_path_query( p_cddb, "/~cddb/cddb.cgi" );
643 cddb_set_http_path_submit( p_cddb, "/~cddb/submit.cgi" );
647 char *psz_temp = config_GetUserDir( VLC_CACHE_DIR );
649 if( asprintf( &psz_cachedir, "%s" DIR_SEP "cddb", psz_temp ) > 0 ) {
650 cddb_cache_enable( p_cddb );
651 cddb_cache_set_dir( p_cddb, psz_cachedir );
652 free( psz_cachedir );
656 cddb_set_timeout( p_cddb, 10 );
659 cddb_disc_t *p_disc = cddb_disc_new();
662 msg_Err( p_access, "unable to create CDDB disc structure." );
666 int64_t i_length = 2000000; /* PreGap */
667 for( int i = 0; i < i_titles; i++ )
669 cddb_track_t *t = cddb_track_new();
670 cddb_track_set_frame_offset( t, p_sectors[i] + 150 ); /* Pregap offset */
672 cddb_disc_add_track( p_disc, t );
673 const int64_t i_size = ( p_sectors[i+1] - p_sectors[i] ) *
674 (int64_t)CDDA_DATA_SIZE;
675 i_length += INT64_C(1000000) * i_size / 44100 / 4 ;
677 msg_Dbg( p_access, "Track %i offset: %i", i, p_sectors[i] + 150 );
680 msg_Dbg( p_access, "Total length: %i", (int)(i_length/1000000) );
681 cddb_disc_set_length( p_disc, (int)(i_length/1000000) );
683 if( !cddb_disc_calc_discid( p_disc ) )
685 msg_Err( p_access, "CDDB disc ID calculation failed" );
689 const int i_matches = cddb_query( p_cddb, p_disc );
692 msg_Warn( p_access, "CDDB error: %s", cddb_error_str(errno) );
695 else if( i_matches == 0 )
697 msg_Dbg( p_access, "Couldn't find any matches in CDDB." );
700 else if( i_matches > 1 )
701 msg_Warn( p_access, "found %d matches in CDDB. Using first one.", i_matches );
703 cddb_read( p_cddb, p_disc );
705 cddb_destroy( p_cddb);
710 cddb_disc_destroy( p_disc );
711 cddb_destroy( p_cddb );
714 #endif /*HAVE_LIBCDDB*/