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>
39 #include <cddb/cddb.h>
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
52 #define CACHING_TEXT N_("Caching value in ms")
53 #define CACHING_LONGTEXT N_( \
54 "Default caching value for Audio CDs. This " \
55 "value should be set in milliseconds." )
58 set_shortname( _("Audio CD"));
59 set_description( _("Audio CD input") );
60 set_capability( "access2", 10 );
61 set_category( CAT_INPUT );
62 set_subcategory( SUBCAT_INPUT_ACCESS );
63 set_callbacks( Open, Close );
65 add_usage_hint( N_("[cdda:][device][@[track]]") );
66 add_integer( "cdda-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
67 CACHING_LONGTEXT, VLC_TRUE );
68 add_bool( "cdda-separate-tracks", VLC_TRUE, NULL, NULL, NULL, VLC_TRUE );
69 add_integer( "cdda-track", -1 , NULL, NULL, NULL, VLC_TRUE );
70 add_string( "cddb-server", "freedb.freedb.org", NULL,
71 N_( "CDDB Server" ), N_( "Address of the CDDB server to use." ),
73 add_integer( "cddb-port", 8880, NULL,
74 N_( "CDDB port" ), N_( "CDDB Server port to use." ),
76 add_shortcut( "cdda" );
77 add_shortcut( "cddasimple" );
81 /* how many blocks VCDRead will read in each loop */
82 #define CDDA_BLOCKS_ONCE 20
83 #define CDDA_DATA_ONCE (CDDA_BLOCKS_ONCE * CDDA_DATA_SIZE)
85 /*****************************************************************************
86 * Access: local prototypes
87 *****************************************************************************/
90 vcddev_t *vcddev; /* vcd device descriptor */
94 input_title_t *title[99]; /* No more that 99 track in a cd-audio */
96 /* Current position */
97 int i_sector; /* Current Sector */
98 int * p_sectors; /* Track sectors */
100 /* Wave header for the output data */
101 WAVEHEADER waveheader;
104 vlc_bool_t b_separate_items;
105 vlc_bool_t b_single_track;
113 static block_t *Block( access_t * );
114 static int Seek( access_t *, int64_t );
115 static int Control( access_t *, int, va_list );
117 static int GetTracks( access_t *p_access, vlc_bool_t b_separate,
118 playlist_t *p_playlist, playlist_item_t *p_parent );
121 static void 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;
135 vlc_bool_t b_separate_requested;
136 vlc_bool_t b_play = VLC_FALSE;
137 input_thread_t *p_input;
138 playlist_item_t *p_item = NULL;
139 playlist_t *p_playlist = NULL;
143 if( !p_access->psz_path || !*p_access->psz_path )
145 /* Only when selected */
146 if( !p_this->b_force ) return VLC_EGENERIC;
148 psz_name = var_CreateGetString( p_this, "cd-audio" );
149 if( !psz_name || !*psz_name )
151 if( psz_name ) free( psz_name );
155 else psz_name = strdup( p_access->psz_path );
158 if( psz_name[0] && psz_name[1] == ':' &&
159 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
163 if( (vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_name )) == NULL )
165 msg_Warn( p_access, "could not open %s", psz_name );
171 /* Set up p_access */
172 p_access->pf_read = NULL;
173 p_access->pf_block = Block;
174 p_access->pf_control = Control;
175 p_access->pf_seek = Seek;
176 p_access->info.i_update = 0;
177 p_access->info.i_size = 0;
178 p_access->info.i_pos = 0;
179 p_access->info.b_eof = VLC_FALSE;
180 p_access->info.i_title = 0;
181 p_access->info.i_seekpoint = 0;
182 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
183 memset( p_sys, 0, sizeof( access_sys_t ) );
184 p_sys->vcddev = vcddev;
185 p_sys->b_separate_items = VLC_FALSE;
186 p_sys->b_single_track = VLC_FALSE;
187 p_sys->b_header = VLC_FALSE;
189 b_separate_requested = var_CreateGetBool( p_access,
190 "cdda-separate-tracks" );
192 /* We only do separate items if the whole disc is requested -
193 * Dirty hack we access some private data ! */
194 p_input = (input_thread_t *)( p_access->p_parent );
195 /* Do we play a single track ? */
196 i_track = var_CreateGetInteger( p_access, "cdda-track" );
197 if( b_separate_requested && p_input->input.i_title_start == -1 &&
200 p_sys->b_separate_items = VLC_TRUE;
204 p_sys->b_single_track = VLC_TRUE;
205 p_sys->i_track = i_track - 1;
208 msg_Dbg( p_access, "separate items : %i - single track : %i",
209 p_sys->b_separate_items, p_sys->b_single_track );
211 if( p_sys->b_separate_items )
213 p_playlist = (playlist_t *) vlc_object_find( p_access,
214 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
216 if( !p_playlist ) return VLC_EGENERIC;
218 /* Let's check if we need to play */
219 if( p_playlist->status.p_item->p_input ==
220 ((input_thread_t *)p_access->p_parent)->input.p_item )
222 p_item = p_playlist->status.p_item;
224 msg_Dbg( p_access, "starting Audio CD playback");
228 input_item_t *p_current = ( (input_thread_t*)p_access->p_parent)->
230 p_item = playlist_LockItemGetByInput( p_playlist, p_current );
231 msg_Dbg( p_access, "not starting Audio CD playback");
235 msg_Dbg( p_playlist, "unable to find item in playlist");
242 /* We read the Table Of Content information */
245 i_ret = GetTracks( p_access, p_sys->b_separate_items,
246 p_playlist, p_item );
253 /* Build a WAV header for the output data */
254 memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
255 SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
256 SetWLE( &p_sys->waveheader.BitsPerSample, 16);
257 p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
258 p_sys->waveheader.Length = 0; /* we just don't know */
259 p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
260 p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
261 SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
262 SetWLE( &p_sys->waveheader.Modus, 2);
263 SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
264 SetWLE( &p_sys->waveheader.BytesPerSample,
265 2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
266 SetDWLE( &p_sys->waveheader.BytesPerSec,
267 2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
268 p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
269 p_sys->waveheader.DataLength = 0; /* we just don't know */
271 /* Only update META if we are in old mode */
272 if( !p_sys->b_separate_items && !p_sys->b_single_track )
274 p_access->info.i_update |= INPUT_UPDATE_META;
277 /* Position on the right sector and update size */
278 if( p_sys->b_single_track )
280 p_sys->i_sector = p_sys->p_sectors[ p_sys->i_track ];
281 p_access->info.i_size =
282 ( p_sys->p_sectors[p_sys->i_track+1] -
283 p_sys->p_sectors[p_sys->i_track] ) *
284 (int64_t)CDDA_DATA_SIZE;
288 var_Create( p_access, "cdda-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
292 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
293 p_playlist->status.p_item, NULL );
296 if( p_playlist ) vlc_object_release( p_playlist );
301 ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
303 if( p_playlist ) vlc_object_release( p_playlist );
307 /*****************************************************************************
309 *****************************************************************************/
310 static void Close( vlc_object_t *p_this )
312 access_t *p_access = (access_t *)p_this;
313 access_sys_t *p_sys = p_access->p_sys;
316 for( i = 0; i < p_sys->i_titles; i++ )
318 vlc_input_title_Delete( p_sys->title[i] );
321 ioctl_Close( p_this, p_sys->vcddev );
325 /*****************************************************************************
326 * Block: read data (CDDA_DATA_ONCE)
327 *****************************************************************************/
328 static block_t *Block( access_t *p_access )
330 access_sys_t *p_sys = p_access->p_sys;
331 int i_blocks = CDDA_BLOCKS_ONCE;
334 if( p_sys->b_separate_items ) p_access->info.b_eof = VLC_TRUE;
336 /* Check end of file */
337 if( p_access->info.b_eof ) return NULL;
339 if( !p_sys->b_header )
341 /* Return only the header */
342 p_block = block_New( p_access, sizeof( WAVEHEADER ) );
343 memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
344 p_sys->b_header = VLC_TRUE;
348 /* Check end of title - Single track */
349 if( p_sys->b_single_track )
351 if( p_sys->i_sector >= p_sys->p_sectors[p_sys->i_track + 1] )
353 p_access->info.b_eof = VLC_TRUE;
356 /* Don't read too far */
357 if( p_sys->i_sector + i_blocks >=
358 p_sys->p_sectors[p_sys->i_track +1] )
360 i_blocks = p_sys->p_sectors[p_sys->i_track+1] - p_sys->i_sector;
365 /* Check end of title - Normal */
366 while( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 1] )
368 if( p_access->info.i_title + 1 >= p_sys->i_titles )
370 p_access->info.b_eof = VLC_TRUE;
374 p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SIZE |
376 p_access->info.i_title++;
377 p_access->info.i_size =
378 p_sys->title[p_access->info.i_title]->i_size;
379 p_access->info.i_pos = 0;
381 /* Don't read too far */
382 if( p_sys->i_sector + i_blocks >= p_sys->p_sectors[p_access->info.i_title + 1] )
384 i_blocks = p_sys->p_sectors[ p_access->info.i_title + 1] -
389 /* Do the actual reading */
390 if( !( p_block = block_New( p_access, i_blocks * CDDA_DATA_SIZE ) ) )
392 msg_Err( p_access, "cannot get a new block of size: %i",
393 i_blocks * CDDA_DATA_SIZE );
397 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
398 p_sys->i_sector, p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
400 msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
401 block_Release( p_block );
403 /* Try to skip one sector (in case of bad sectors) */
405 p_access->info.i_pos += CDDA_DATA_SIZE;
409 /* Update a few values */
410 p_sys->i_sector += i_blocks;
411 p_access->info.i_pos += p_block->i_buffer;
416 /****************************************************************************
418 ****************************************************************************/
419 static int Seek( access_t *p_access, int64_t i_pos )
421 access_sys_t *p_sys = p_access->p_sys;
423 /* Next sector to read */
424 p_sys->i_sector = p_sys->p_sectors[
425 (p_sys->b_single_track ?
427 p_access->info.i_title + 1)
429 i_pos / CDDA_DATA_SIZE;
430 p_access->info.i_pos = i_pos;
435 /*****************************************************************************
437 *****************************************************************************/
438 static int Control( access_t *p_access, int i_query, va_list args )
440 access_sys_t *p_sys = p_access->p_sys;
444 input_title_t ***ppp_title;
452 case ACCESS_CAN_SEEK:
453 case ACCESS_CAN_FASTSEEK:
454 case ACCESS_CAN_PAUSE:
455 case ACCESS_CAN_CONTROL_PACE:
456 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
462 pi_int = (int*)va_arg( args, int * );
463 *pi_int = CDDA_DATA_ONCE;
466 case ACCESS_GET_PTS_DELAY:
467 pi_64 = (int64_t*)va_arg( args, int64_t * );
468 *pi_64 = var_GetInteger( p_access, "cdda-caching" ) * 1000;
472 case ACCESS_SET_PAUSE_STATE:
475 case ACCESS_GET_TITLE_INFO:
476 if( p_sys->b_single_track )
478 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
479 pi_int = (int*)va_arg( args, int* );
480 *((int*)va_arg( args, int* )) = 1; /* Title offset */
482 /* Duplicate title infos */
483 *pi_int = p_sys->i_titles;
484 *ppp_title = malloc(sizeof( input_title_t **) * p_sys->i_titles );
485 for( i = 0; i < p_sys->i_titles; i++ )
487 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
491 case ACCESS_SET_TITLE:
492 if( p_sys->b_single_track ) return VLC_EGENERIC;
493 i = (int)va_arg( args, int );
494 if( i != p_access->info.i_title )
497 p_access->info.i_update |=
498 INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE|INPUT_UPDATE_META;
499 p_access->info.i_title = i;
500 p_access->info.i_size = p_sys->title[i]->i_size;
501 p_access->info.i_pos = 0;
503 /* Next sector to read */
504 p_sys->i_sector = p_sys->p_sectors[i];
508 case ACCESS_GET_META:
509 if( p_sys->b_single_track ) return VLC_EGENERIC;
510 psz_title = malloc( strlen( _("Audio CD - Track ") ) + 5 );
511 snprintf( psz_title, 100, _("Audio CD - Track %i" ),
512 p_access->info.i_title+1 );
513 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
514 vlc_meta_SetTitle( p_meta, psz_title );
518 case ACCESS_SET_SEEKPOINT:
519 case ACCESS_SET_PRIVATE_ID_STATE:
523 msg_Warn( p_access, "unimplemented query in control" );
530 static int GetTracks( access_t *p_access, vlc_bool_t b_separate,
531 playlist_t *p_playlist, playlist_item_t *p_parent )
533 access_sys_t *p_sys = p_access->p_sys;
535 input_item_t *p_input_item;
537 p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
538 p_sys->vcddev, &p_sys->p_sectors );
539 if( p_sys->i_titles < 0 )
541 msg_Err( p_access, "unable to count tracks" );
542 return VLC_EGENERIC;;
544 else if( p_sys->i_titles <= 0 )
546 msg_Err( p_access, "no audio tracks found" );
552 if( p_parent->i_children == -1 )
554 playlist_LockItemToNode( p_playlist, p_parent );
556 psz_name = strdup( "Audio CD" );
557 vlc_mutex_lock( &p_playlist->object_lock );
558 playlist_ItemSetName( p_parent, psz_name );
559 vlc_mutex_unlock( &p_playlist->object_lock );
560 var_SetInteger( p_playlist, "item-change",
561 p_parent->p_input->i_id );
565 GetCDDBInfo( p_access, p_sys->i_titles, p_sys->p_sectors );
568 if( cddb_disc_get_title( p_sys->p_disc ) )
570 asprintf( &psz_name, "%s", cddb_disc_get_title( p_sys->p_disc )
572 vlc_mutex_lock( &p_playlist->object_lock );
573 playlist_ItemSetName( p_parent, psz_name );
574 vlc_mutex_unlock( &p_playlist->object_lock );
575 var_SetInteger( p_playlist, "item-change",
576 p_parent->p_input->i_id );
583 /* Build title table */
584 for( i = 0; i < p_sys->i_titles; i++ )
586 msg_Dbg( p_access, "track[%d] start=%d", i, p_sys->p_sectors[i] );
589 input_title_t *t = p_sys->title[i] = vlc_input_title_New();
591 asprintf( &t->psz_name, _("Track %i"), i + 1 );
592 t->i_size = ( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
593 (int64_t)CDDA_DATA_SIZE;
595 t->i_length = I64C(1000000) * t->i_size / 44100 / 4;
600 int i_path_len = p_access->psz_path ? strlen( p_access->psz_path )
604 psz_name = malloc( strlen( _("Audio CD - Track ") ) + 5 );
605 psz_opt = malloc( strlen( "cdda-track=" ) + 3 );
607 psz_uri = (char*)malloc( i_path_len + 13 );
608 snprintf( psz_uri, i_path_len + 13, "cdda://%s",
609 p_access->psz_path ? p_access->psz_path : "" );
610 sprintf( psz_opt, "cdda-track=%i", i+1 );
612 /* Define a "default name" */
613 sprintf( psz_name, _("Audio CD - Track %i"), (i+1) );
615 /* Create playlist items */
616 p_input_item = input_ItemNewWithType( VLC_OBJECT( p_playlist ),
617 psz_uri, psz_name, 0, NULL, -1,
619 vlc_input_item_AddOption( p_input_item, psz_opt );
621 /* If we have CDDB info, change the name */
625 cddb_track_t *t = cddb_disc_get_track( p_sys->p_disc, i );
628 if( cddb_track_get_title( t ) != NULL )
630 vlc_input_item_AddInfo( p_input_item,
631 _(VLC_META_INFO_CAT),
633 cddb_track_get_title( t ) );
634 if( p_input_item->psz_name )
635 free( p_input_item->psz_name );
636 asprintf( &p_input_item->psz_name, "%s",
637 cddb_track_get_title( t ) );
639 psz_result = cddb_track_get_artist( t );
642 vlc_input_item_AddInfo( p_input_item,
643 _(VLC_META_INFO_CAT),
644 _(VLC_META_ARTIST), psz_result );
649 playlist_BothAddInput( p_playlist, p_input_item, p_parent,
650 PLAYLIST_APPEND, PLAYLIST_END );
651 free( psz_uri ); free( psz_opt ); free( psz_name );
655 p_sys->i_sector = p_sys->p_sectors[0];
656 if( p_sys->b_separate_items )
665 static void GetCDDBInfo( access_t *p_access, int i_titles, int *p_sectors )
668 int64_t i_length = 0, i_size = 0;
669 cddb_conn_t *p_cddb = cddb_new();
671 // cddb_log_set_handler( CDDBLogHandler );
675 msg_Warn( p_access, "unable to use CDDB" );
679 cddb_set_email_address( p_cddb, "vlc@videolan.org" );
680 cddb_set_server_name( p_cddb, config_GetPsz( p_access, "cddb-server" ) );
681 cddb_set_server_port( p_cddb, config_GetInt( p_access, "cddb-port" ) );
684 cddb_cache_disable( p_cddb );
686 // cddb_cache_set_dir( p_cddb,
687 // config_GetPsz( p_access,
688 // MODULE_STRING "-cddb-cachedir") );
690 cddb_set_timeout( p_cddb, 10 );
693 cddb_http_disable( p_cddb);
695 p_access->p_sys->p_disc = cddb_disc_new();
697 if(! p_access->p_sys->p_disc )
699 msg_Err( p_access, "unable to create CDDB disc structure." );
703 for(i = 0; i < i_titles ; i++ )
705 cddb_track_t *t = cddb_track_new();
706 cddb_track_set_frame_offset(t, p_sectors[i] );
707 cddb_disc_add_track( p_access->p_sys->p_disc, t );
708 i_size = ( p_sectors[i+1] - p_sectors[i] ) *
709 (int64_t)CDDA_DATA_SIZE;
710 i_length += I64C(1000000) * i_size / 44100 / 4 ;
713 cddb_disc_set_length( p_access->p_sys->p_disc, (int)(i_length/1000000) );
715 if (!cddb_disc_calc_discid(p_access->p_sys->p_disc ))
717 msg_Err( p_access, "CDDB disc ID calculation failed" );
721 i_matches = cddb_query( p_cddb, p_access->p_sys->p_disc);
726 msg_Warn( p_access, "found %d matches in CDDB. Using first one.",
728 cddb_read( p_cddb, p_access->p_sys->p_disc );
730 // cddb_disc_print( p_access->p_sys->p_disc );
734 msg_Warn( p_access, "CDDB error: %s", cddb_error_str(errno));
738 cddb_destroy( p_cddb);
742 #endif /*HAVE_LIBCDDB*/