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->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.i_view, p_playlist->status.p_item,
297 if( p_playlist ) vlc_object_release( p_playlist );
302 ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
304 if( p_playlist ) vlc_object_release( p_playlist );
308 /*****************************************************************************
310 *****************************************************************************/
311 static void Close( vlc_object_t *p_this )
313 access_t *p_access = (access_t *)p_this;
314 access_sys_t *p_sys = p_access->p_sys;
317 for( i = 0; i < p_sys->i_titles; i++ )
319 vlc_input_title_Delete( p_sys->title[i] );
322 ioctl_Close( p_this, p_sys->vcddev );
326 /*****************************************************************************
327 * Block: read data (CDDA_DATA_ONCE)
328 *****************************************************************************/
329 static block_t *Block( access_t *p_access )
331 access_sys_t *p_sys = p_access->p_sys;
332 int i_blocks = CDDA_BLOCKS_ONCE;
335 if( p_sys->b_separate_items ) p_access->info.b_eof = VLC_TRUE;
337 /* Check end of file */
338 if( p_access->info.b_eof ) return NULL;
340 if( !p_sys->b_header )
342 /* Return only the header */
343 p_block = block_New( p_access, sizeof( WAVEHEADER ) );
344 memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
345 p_sys->b_header = VLC_TRUE;
349 /* Check end of title - Single track */
350 if( p_sys->b_single_track )
352 if( p_sys->i_sector >= p_sys->p_sectors[p_sys->i_track + 1] )
354 p_access->info.b_eof = VLC_TRUE;
357 /* Don't read too far */
358 if( p_sys->i_sector + i_blocks >=
359 p_sys->p_sectors[p_sys->i_track +1] )
361 i_blocks = p_sys->p_sectors[p_sys->i_track+1] - p_sys->i_sector;
366 /* Check end of title - Normal */
367 while( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 1] )
369 if( p_access->info.i_title + 1 >= p_sys->i_titles )
371 p_access->info.b_eof = VLC_TRUE;
375 p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SIZE |
377 p_access->info.i_title++;
378 p_access->info.i_size =
379 p_sys->title[p_access->info.i_title]->i_size;
380 p_access->info.i_pos = 0;
382 /* Don't read too far */
383 if( p_sys->i_sector + i_blocks >= p_sys->p_sectors[p_access->info.i_title + 1] )
385 i_blocks = p_sys->p_sectors[ p_access->info.i_title + 1] -
390 /* Do the actual reading */
391 if( !( p_block = block_New( p_access, i_blocks * CDDA_DATA_SIZE ) ) )
393 msg_Err( p_access, "cannot get a new block of size: %i",
394 i_blocks * CDDA_DATA_SIZE );
398 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
399 p_sys->i_sector, p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
401 msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
402 block_Release( p_block );
404 /* Try to skip one sector (in case of bad sectors) */
406 p_access->info.i_pos += CDDA_DATA_SIZE;
410 /* Update a few values */
411 p_sys->i_sector += i_blocks;
412 p_access->info.i_pos += p_block->i_buffer;
417 /****************************************************************************
419 ****************************************************************************/
420 static int Seek( access_t *p_access, int64_t i_pos )
422 access_sys_t *p_sys = p_access->p_sys;
424 /* Next sector to read */
425 p_sys->i_sector = p_sys->p_sectors[
426 (p_sys->b_single_track ?
428 p_access->info.i_title + 1)
430 i_pos / CDDA_DATA_SIZE;
431 p_access->info.i_pos = i_pos;
436 /*****************************************************************************
438 *****************************************************************************/
439 static int Control( access_t *p_access, int i_query, va_list args )
441 access_sys_t *p_sys = p_access->p_sys;
445 input_title_t ***ppp_title;
448 vlc_meta_t **pp_meta;
453 case ACCESS_CAN_SEEK:
454 case ACCESS_CAN_FASTSEEK:
455 case ACCESS_CAN_PAUSE:
456 case ACCESS_CAN_CONTROL_PACE:
457 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
463 pi_int = (int*)va_arg( args, int * );
464 *pi_int = CDDA_DATA_ONCE;
467 case ACCESS_GET_PTS_DELAY:
468 pi_64 = (int64_t*)va_arg( args, int64_t * );
469 *pi_64 = var_GetInteger( p_access, "cdda-caching" ) * 1000;
473 case ACCESS_SET_PAUSE_STATE:
476 case ACCESS_GET_TITLE_INFO:
477 if( p_sys->b_single_track )
479 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
480 pi_int = (int*)va_arg( args, int* );
481 *((int*)va_arg( args, int* )) = 1; /* Title offset */
483 /* Duplicate title infos */
484 *pi_int = p_sys->i_titles;
485 *ppp_title = malloc(sizeof( input_title_t **) * p_sys->i_titles );
486 for( i = 0; i < p_sys->i_titles; i++ )
488 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
492 case ACCESS_SET_TITLE:
493 if( p_sys->b_single_track ) return VLC_EGENERIC;
494 i = (int)va_arg( args, int );
495 if( i != p_access->info.i_title )
498 p_access->info.i_update |=
499 INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE|INPUT_UPDATE_META;
500 p_access->info.i_title = i;
501 p_access->info.i_size = p_sys->title[i]->i_size;
502 p_access->info.i_pos = 0;
504 /* Next sector to read */
505 p_sys->i_sector = p_sys->p_sectors[i];
509 case ACCESS_GET_META:
510 if( p_sys->b_single_track ) return VLC_EGENERIC;
511 psz_title = malloc( strlen( _("Audio CD - Track ") ) + 5 );
512 snprintf( psz_title, 100, _("Audio CD - Track %i" ),
513 p_access->info.i_title+1 );
514 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
515 *pp_meta = vlc_meta_New();
516 vlc_meta_Add( *pp_meta, _(VLC_META_TITLE), psz_title );
520 case ACCESS_SET_SEEKPOINT:
521 case ACCESS_SET_PRIVATE_ID_STATE:
525 msg_Warn( p_access, "unimplemented query in control" );
532 static int GetTracks( access_t *p_access, vlc_bool_t b_separate,
533 playlist_t *p_playlist, playlist_item_t *p_parent )
535 access_sys_t *p_sys = p_access->p_sys;
537 playlist_item_t *p_item;
539 p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
540 p_sys->vcddev, &p_sys->p_sectors );
541 if( p_sys->i_titles < 0 )
543 msg_Err( p_access, "unable to count tracks" );
544 return VLC_EGENERIC;;
546 else if( p_sys->i_titles <= 0 )
548 msg_Err( p_access, "no audio tracks found" );
554 if( p_parent->i_children == -1 )
556 playlist_LockItemToNode( p_playlist, p_parent );
558 psz_name = strdup( "Audio CD" );
559 vlc_mutex_lock( &p_playlist->object_lock );
560 playlist_ItemSetName( p_parent, psz_name );
561 vlc_mutex_unlock( &p_playlist->object_lock );
562 var_SetInteger( p_playlist, "item-change",
563 p_parent->input.i_id );
567 GetCDDBInfo( p_access, p_sys->i_titles, p_sys->p_sectors );
570 if( cddb_disc_get_title( p_sys->p_disc ) )
572 asprintf( &psz_name, "%s", cddb_disc_get_title( p_sys->p_disc )
574 vlc_mutex_lock( &p_playlist->object_lock );
575 playlist_ItemSetName( p_parent, psz_name );
576 vlc_mutex_unlock( &p_playlist->object_lock );
577 var_SetInteger( p_playlist, "item-change",
578 p_parent->input.i_id );
585 /* Build title table */
586 for( i = 0; i < p_sys->i_titles; i++ )
588 msg_Dbg( p_access, "track[%d] start=%d", i, p_sys->p_sectors[i] );
591 input_title_t *t = p_sys->title[i] = vlc_input_title_New();
593 asprintf( &t->psz_name, _("Track %i"), i + 1 );
594 t->i_size = ( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
595 (int64_t)CDDA_DATA_SIZE;
597 t->i_length = I64C(1000000) * t->i_size / 44100 / 4;
602 int i_path_len = p_access->psz_path ? strlen( p_access->psz_path )
606 psz_name = malloc( strlen( _("Audio CD - Track ") ) + 5 );
607 psz_opt = malloc( strlen( "cdda-track=" ) + 3 );
609 psz_uri = (char*)malloc( i_path_len + 13 );
610 snprintf( psz_uri, i_path_len + 13, "cdda://%s",
611 p_access->psz_path ? p_access->psz_path : "" );
612 sprintf( psz_opt, "cdda-track=%i", i+1 );
614 /* Define a "default name" */
615 sprintf( psz_name, _("Audio CD - Track %i"), (i+1) );
617 /* Create playlist items */
618 p_item = playlist_ItemNewWithType( VLC_OBJECT( p_playlist ),
619 psz_uri, psz_name, ITEM_TYPE_DISC );
620 playlist_ItemAddOption( p_item, psz_opt );
622 /* If we have CDDB info, change the name */
626 cddb_track_t *t = cddb_disc_get_track( p_sys->p_disc, i );
629 if( cddb_track_get_title( t ) != NULL )
631 vlc_input_item_AddInfo( &p_item->input,
632 _(VLC_META_INFO_CAT),
634 cddb_track_get_title( t ) );
635 if( p_item->input.psz_name )
636 free( p_item->input.psz_name );
637 asprintf( &p_item->input.psz_name, "%s",
638 cddb_track_get_title( t ) );
640 psz_result = cddb_track_get_artist( t );
643 vlc_input_item_AddInfo( &p_item->input,
644 _(VLC_META_INFO_CAT),
645 _(VLC_META_ARTIST), psz_result );
650 playlist_NodeAddItem( p_playlist, p_item,
651 p_parent->pp_parents[0]->i_view,
652 p_parent, PLAYLIST_APPEND, PLAYLIST_END );
653 free( psz_uri ); free( psz_opt ); free( psz_name );
657 p_sys->i_sector = p_sys->p_sectors[0];
658 if( p_sys->b_separate_items )
667 static void GetCDDBInfo( access_t *p_access, int i_titles, int *p_sectors )
670 int64_t i_length = 0, i_size = 0;
671 cddb_conn_t *p_cddb = cddb_new();
673 // cddb_log_set_handler( CDDBLogHandler );
677 msg_Warn( p_access, "unable to use CDDB" );
681 cddb_set_email_address( p_cddb, "vlc@videolan.org" );
682 cddb_set_server_name( p_cddb, config_GetPsz( p_access, "cddb-server" ) );
683 cddb_set_server_port( p_cddb, config_GetInt( p_access, "cddb-port" ) );
686 cddb_cache_disable( p_cddb );
688 // cddb_cache_set_dir( p_cddb,
689 // config_GetPsz( p_access,
690 // MODULE_STRING "-cddb-cachedir") );
692 cddb_set_timeout( p_cddb, 10 );
695 cddb_http_disable( p_cddb);
697 p_access->p_sys->p_disc = cddb_disc_new();
699 if(! p_access->p_sys->p_disc )
701 msg_Err( p_access, "unable to create CDDB disc structure." );
705 for(i = 0; i < i_titles ; i++ )
707 cddb_track_t *t = cddb_track_new();
708 cddb_track_set_frame_offset(t, p_sectors[i] );
709 cddb_disc_add_track( p_access->p_sys->p_disc, t );
710 i_size = ( p_sectors[i+1] - p_sectors[i] ) *
711 (int64_t)CDDA_DATA_SIZE;
712 i_length += I64C(1000000) * i_size / 44100 / 4 ;
715 cddb_disc_set_length( p_access->p_sys->p_disc, (int)(i_length/1000000) );
717 if (!cddb_disc_calc_discid(p_access->p_sys->p_disc ))
719 msg_Err( p_access, "CDDB disc ID calculation failed" );
723 i_matches = cddb_query( p_cddb, p_access->p_sys->p_disc);
728 msg_Warn( p_access, "found %d matches in CDDB. Using first one.",
730 cddb_read( p_cddb, p_access->p_sys->p_disc );
732 // cddb_disc_print( p_access->p_sys->p_disc );
736 msg_Warn( p_access, "CDDB error: %s", cddb_error_str(errno));
740 cddb_destroy( p_cddb);
744 #endif /*HAVE_LIBCDDB*/