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;
134 if( !p_access->psz_filepath || !*p_access->psz_filepath )
136 /* Only when selected */
137 if( !p_access->psz_access || !*p_access->psz_access )
140 psz_name = var_InheritString( p_this, "cd-audio" );
144 else psz_name = ToLocaleDup( p_access->psz_filepath );
146 #if defined( _WIN32 ) || defined( __OS2__ )
147 if( psz_name[0] && psz_name[1] == ':' &&
148 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
150 /* Set up p_access */
151 STANDARD_BLOCK_ACCESS_INIT
154 if( (vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_name ) ) == NULL )
156 msg_Warn( p_access, "could not open %s", psz_name );
162 p_sys->vcddev = vcddev;
164 /* Do we play a single track ? */
165 p_sys->i_track = var_InheritInteger( p_access, "cdda-track" ) - 1;
167 if( p_sys->i_track < 0 )
169 /* We only do separate items if the whole disc is requested */
170 input_thread_t *p_input = access_GetParentInput( p_access );
175 input_item_t *p_current = input_GetItem( p_input );
177 i_ret = GetTracks( p_access, p_current );
179 vlc_object_release( p_input );
186 /* Build a WAV header for the output data */
187 memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
188 SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
189 SetWLE( &p_sys->waveheader.BitsPerSample, 16);
190 p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
191 p_sys->waveheader.Length = 0; /* we just don't know */
192 p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
193 p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
194 SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
195 SetWLE( &p_sys->waveheader.Modus, 2);
196 SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
197 SetWLE( &p_sys->waveheader.BytesPerSample,
198 2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
199 SetDWLE( &p_sys->waveheader.BytesPerSec,
200 2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
201 p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
202 p_sys->waveheader.DataLength = 0; /* we just don't know */
204 p_sys->i_first_sector = var_InheritInteger( p_access,
205 "cdda-first-sector" );
206 p_sys->i_last_sector = var_InheritInteger( p_access,
207 "cdda-last-sector" );
208 /* Tracknumber in MRL */
209 if( p_sys->i_first_sector < 0 || p_sys->i_last_sector < 0 )
211 const int i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
212 p_sys->vcddev, &p_sys->p_sectors );
213 if( p_sys->i_track >= i_titles )
215 msg_Err( p_access, "invalid track number" );
218 p_sys->i_first_sector = p_sys->p_sectors[p_sys->i_track];
219 p_sys->i_last_sector = p_sys->p_sectors[p_sys->i_track+1];
222 p_sys->i_sector = p_sys->i_first_sector;
223 p_sys->size = (p_sys->i_last_sector - p_sys->i_first_sector)
224 * (int64_t)CDDA_DATA_SIZE;
230 free( p_sys->p_sectors );
231 ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
236 /*****************************************************************************
238 *****************************************************************************/
239 static void Close( vlc_object_t *p_this )
241 access_t *p_access = (access_t *)p_this;
242 access_sys_t *p_sys = p_access->p_sys;
244 free( p_sys->p_sectors );
245 ioctl_Close( p_this, p_sys->vcddev );
249 /*****************************************************************************
250 * Block: read data (CDDA_DATA_ONCE)
251 *****************************************************************************/
252 static block_t *Block( access_t *p_access )
254 access_sys_t *p_sys = p_access->p_sys;
255 int i_blocks = CDDA_BLOCKS_ONCE;
258 if( p_sys->i_track < 0 ) p_access->info.b_eof = true;
260 /* Check end of file */
261 if( p_access->info.b_eof ) return NULL;
263 if( !p_sys->b_header )
265 /* Return only the header */
266 p_block = block_Alloc( sizeof( WAVEHEADER ) );
267 memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
268 p_sys->b_header = true;
272 if( p_sys->i_sector >= p_sys->i_last_sector )
274 p_access->info.b_eof = true;
278 /* Don't read too far */
279 if( p_sys->i_sector + i_blocks >= p_sys->i_last_sector )
280 i_blocks = p_sys->i_last_sector - p_sys->i_sector;
282 /* Do the actual reading */
283 if( !( p_block = block_Alloc( i_blocks * CDDA_DATA_SIZE ) ) )
285 msg_Err( p_access, "cannot get a new block of size: %i",
286 i_blocks * CDDA_DATA_SIZE );
290 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
291 p_sys->i_sector, p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
293 msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
294 block_Release( p_block );
296 /* Try to skip one sector (in case of bad sectors) */
298 p_access->info.i_pos += CDDA_DATA_SIZE;
302 /* Update a few values */
303 p_sys->i_sector += i_blocks;
304 p_access->info.i_pos += p_block->i_buffer;
309 /****************************************************************************
311 ****************************************************************************/
312 static int Seek( access_t *p_access, uint64_t i_pos )
314 access_sys_t *p_sys = p_access->p_sys;
316 /* Next sector to read */
317 p_sys->i_sector = p_sys->i_first_sector + i_pos / CDDA_DATA_SIZE;
318 assert( p_sys->i_sector >= 0 );
319 p_access->info.i_pos = i_pos;
324 /*****************************************************************************
326 *****************************************************************************/
327 static int Control( access_t *p_access, int i_query, va_list args )
331 case ACCESS_CAN_SEEK:
332 case ACCESS_CAN_FASTSEEK:
333 case ACCESS_CAN_PAUSE:
334 case ACCESS_CAN_CONTROL_PACE:
335 *va_arg( args, bool* ) = true;
337 case ACCESS_GET_SIZE:
338 *va_arg( args, uint64_t * ) = p_access->p_sys->size;
340 case ACCESS_GET_PTS_DELAY:
341 *va_arg( args, int64_t * ) =
342 INT64_C(1000) * var_InheritInteger( p_access, "disc-caching" );
345 case ACCESS_SET_PAUSE_STATE:
354 static int GetTracks( access_t *p_access, input_item_t *p_current )
356 access_sys_t *p_sys = p_access->p_sys;
358 const int i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
359 p_sys->vcddev, &p_sys->p_sectors );
363 msg_Err( p_access, "unable to count tracks" );
364 else if( i_titles <= 0 )
365 msg_Err( p_access, "no audio tracks found" );
366 return VLC_EGENERIC;;
370 input_item_SetName( p_current, "Audio CD" );
372 const char *psz_album = NULL;
373 const char *psz_year = NULL;
374 const char *psz_genre = NULL;
375 const char *psz_artist = NULL;
376 const char *psz_description = NULL;
378 /* Return true if the given string is not NULL and not empty */
379 #define NONEMPTY( psz ) ( (psz) && *(psz) )
380 /* If the given string is NULL or empty, fill it by the return value of 'code' */
381 #define ON_EMPTY( psz, code ) do { if( !NONEMPTY( psz) ) { (psz) = code; } } while(0)
383 /* Retreive CDDB information */
385 char psz_year_buffer[4+1];
386 msg_Dbg( p_access, "fetching infos with CDDB" );
387 cddb_disc_t *p_disc = GetCDDBInfo( p_access, i_titles, p_sys->p_sectors );
390 psz_album = cddb_disc_get_title( p_disc );
391 psz_genre = cddb_disc_get_genre( p_disc );
394 const unsigned i_year = cddb_disc_get_year( p_disc );
397 psz_year = psz_year_buffer;
398 snprintf( psz_year_buffer, sizeof(psz_year_buffer), "%u", i_year );
401 /* Set artist only if unique */
402 for( int i = 0; i < i_titles; i++ )
404 cddb_track_t *t = cddb_disc_get_track( p_disc, i );
407 const char *psz_track_artist = cddb_track_get_artist( t );
408 if( psz_artist && psz_track_artist &&
409 strcmp( psz_artist, psz_track_artist ) )
414 psz_artist = psz_track_artist;
418 msg_Dbg( p_access, "GetCDDBInfo failed" );
422 vlc_meta_t **pp_cd_text;
425 if( ioctl_GetCdText( VLC_OBJECT(p_access), p_sys->vcddev, &pp_cd_text, &i_cd_text ) )
427 msg_Dbg( p_access, "CD-TEXT information missing" );
432 /* Retrieve CD-TEXT information but prefer CDDB */
433 if( i_cd_text > 0 && pp_cd_text[0] )
435 const vlc_meta_t *p_disc = pp_cd_text[0];
436 ON_EMPTY( psz_album, vlc_meta_Get( p_disc, vlc_meta_Album ) );
437 ON_EMPTY( psz_genre, vlc_meta_Get( p_disc, vlc_meta_Genre ) );
438 ON_EMPTY( psz_artist, vlc_meta_Get( p_disc, vlc_meta_Artist ) );
439 ON_EMPTY( psz_description, vlc_meta_Get( p_disc, vlc_meta_Description ) );
442 if( NONEMPTY( psz_album ) )
444 input_item_SetName( p_current, psz_album );
445 input_item_SetAlbum( p_current, psz_album );
448 if( NONEMPTY( psz_genre ) )
449 input_item_SetGenre( p_current, psz_genre );
451 if( NONEMPTY( psz_artist ) )
452 input_item_SetArtist( p_current, psz_artist );
454 if( NONEMPTY( psz_year ) )
455 input_item_SetDate( p_current, psz_year );
457 if( NONEMPTY( psz_description ) )
458 input_item_SetDescription( p_current, psz_description );
460 const mtime_t i_duration = (int64_t)( p_sys->p_sectors[i_titles] - p_sys->p_sectors[0] ) *
461 CDDA_DATA_SIZE * 1000000 / 44100 / 2 / 2;
462 input_item_SetDuration( p_current, i_duration );
464 input_item_node_t *p_root = input_item_node_Create( p_current );
466 /* Build title table */
467 for( int i = 0; i < i_titles; i++ )
469 input_item_t *p_input_item;
471 char *psz_uri, *psz_opt, *psz_first, *psz_last;
474 msg_Dbg( p_access, "track[%d] start=%d", i, p_sys->p_sectors[i] );
477 if( asprintf( &psz_uri, "cdda://%s", p_access->psz_location ) == -1 )
479 if( asprintf( &psz_opt, "cdda-track=%i", i+1 ) == -1 )
481 if( asprintf( &psz_first, "cdda-first-sector=%i",p_sys->p_sectors[i] ) == -1 )
483 if( asprintf( &psz_last, "cdda-last-sector=%i", p_sys->p_sectors[i+1] ) == -1 )
486 /* Define a "default name" */
487 if( asprintf( &psz_name, _("Audio CD - Track %02i"), (i+1) ) == -1 )
490 /* Create playlist items */
491 const mtime_t i_duration = (int64_t)( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
492 CDDA_DATA_SIZE * 1000000 / 44100 / 2 / 2;
493 p_input_item = input_item_NewWithType( psz_uri, psz_name, 0, NULL, 0,
494 i_duration, ITEM_TYPE_DISC );
495 input_item_CopyOptions( p_current, p_input_item );
496 input_item_AddOption( p_input_item, psz_first, VLC_INPUT_OPTION_TRUSTED );
497 input_item_AddOption( p_input_item, psz_last, VLC_INPUT_OPTION_TRUSTED );
498 input_item_AddOption( p_input_item, psz_opt, VLC_INPUT_OPTION_TRUSTED );
500 const char *psz_track_title = NULL;
501 const char *psz_track_artist = NULL;
502 const char *psz_track_genre = NULL;
503 const char *psz_track_description = NULL;
506 /* Retreive CDDB information */
509 cddb_track_t *t = cddb_disc_get_track( p_disc, i );
512 psz_track_title = cddb_track_get_title( t );
513 psz_track_artist = cddb_track_get_artist( t );
518 /* Retreive CD-TEXT information but prefer CDDB */
519 if( i+1 < i_cd_text && pp_cd_text[i+1] )
521 const vlc_meta_t *t = pp_cd_text[i+1];
523 ON_EMPTY( psz_track_title, vlc_meta_Get( t, vlc_meta_Title ) );
524 ON_EMPTY( psz_track_artist, vlc_meta_Get( t, vlc_meta_Artist ) );
525 ON_EMPTY( psz_track_genre, vlc_meta_Get( t, vlc_meta_Genre ) );
526 ON_EMPTY( psz_track_description, vlc_meta_Get( t, vlc_meta_Description ) );
530 ON_EMPTY( psz_track_artist, psz_artist );
531 ON_EMPTY( psz_track_genre, psz_genre );
532 ON_EMPTY( psz_track_description, psz_description );
535 if( NONEMPTY( psz_track_title ) )
537 input_item_SetName( p_input_item, psz_track_title );
538 input_item_SetTitle( p_input_item, psz_track_title );
541 if( NONEMPTY( psz_track_artist ) )
542 input_item_SetArtist( p_input_item, psz_track_artist );
544 if( NONEMPTY( psz_track_genre ) )
545 input_item_SetGenre( p_input_item, psz_track_genre );
547 if( NONEMPTY( psz_track_description ) )
548 input_item_SetDescription( p_input_item, psz_track_description );
550 if( NONEMPTY( psz_album ) )
551 input_item_SetAlbum( p_input_item, psz_album );
553 if( NONEMPTY( psz_year ) )
554 input_item_SetDate( p_input_item, psz_year );
557 snprintf( psz_num, sizeof(psz_num), "%d", 1+i );
558 input_item_SetTrackNum( p_input_item, psz_num );
560 input_item_node_AppendItem( p_root, p_input_item );
561 vlc_gc_decref( p_input_item );
562 free( psz_uri ); free( psz_opt ); free( psz_name );
563 free( psz_first ); free( psz_last );
568 input_item_node_PostAndDelete( p_root );
571 for( int i = 0; i < i_cd_text; i++ )
573 vlc_meta_t *p_meta = pp_cd_text[i];
576 vlc_meta_Delete( p_meta );
582 cddb_disc_destroy( p_disc );
588 static cddb_disc_t *GetCDDBInfo( access_t *p_access, int i_titles, int *p_sectors )
590 if( var_InheritInteger( p_access, "album-art" ) != ALBUM_ART_ALL &&
591 ! var_InheritBool( p_access, "metadata-network-access" ) )
593 msg_Dbg( p_access, "Album art policy set to manual; no automatic fetching" );
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*/