1 /*****************************************************************************
2 * access.c : CD digital audio input module for vlc using libcdio
3 *****************************************************************************
4 * Copyright (C) 2000, 2003, 2004 VideoLAN
7 * Authors: Rocky Bernstein <rocky@panix.com>
8 * Laurent Aimar <fenrir@via.ecp.fr>
9 * Gildas Bazin <gbazin@netcourrier.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include "callback.h" /* FIXME - reorganize callback.h, cdda.h better */
30 #include "cdda.h" /* private structures. Also #includes vlc things */
31 #include "info.h" /* headers for meta info retrieval */
32 #include <vlc_playlist.h> /* Has to come *after* cdda.h */
35 #include <cdio/cdio.h>
36 #include <cdio/logging.h>
37 #include <cdio/cd_types.h>
41 /* #ifdef variables below are defined via config.h via #include vlc above. */
46 #ifdef HAVE_SYS_TYPES_H
47 #include <sys/types.h>
58 /* FIXME: This variable is a hack. Would be nice to eliminate. */
59 access_t *p_cdda_input = NULL;
61 /*****************************************************************************
63 *****************************************************************************/
64 static block_t *CDDAReadBlocks( access_t * p_access );
65 static int CDDASeek( access_t * p_access, int64_t i_pos );
66 static int CDDAControl( access_t *p_access, int i_query,
69 static int CDDAInit( access_t *p_access, cdda_data_t *p_cdda ) ;
72 /****************************************************************************
74 ****************************************************************************/
76 /* process messages that originate from libcdio. */
78 cdio_log_handler (cdio_log_level_t level, const char message[])
80 cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
88 if (p_cdda->i_debug & INPUT_DBG_CDIO)
89 msg_Dbg( p_cdda_input, message);
92 msg_Warn( p_cdda_input, message);
96 msg_Err( p_cdda_input, message);
99 msg_Warn( p_cdda_input, message,
100 "The above message had unknown cdio log level",
108 /*! This routine is called by libcddb routines on error.
109 Setup is done by init_input_plugin.
112 cddb_log_handler (cddb_log_level_t level, const char message[])
114 cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
119 if (!(p_cdda->i_debug & INPUT_DBG_CDDB)) return;
121 /* Fall through if to warn case */
123 cdio_log_handler (level, message);
126 #endif /*HAVE_LIBCDDB*/
129 /*! This routine is when xine is not fully set up (before full initialization)
130 or is not around (before finalization).
133 uninit_log_handler (cdio_log_level_t level, const char message[])
135 cdda_data_t *p_cdda = NULL;
138 p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
144 if (!p_cdda || !(p_cdda->i_debug & (INPUT_DBG_CDIO|INPUT_DBG_CDDB)))
147 /* Fall through if to warn case */
149 fprintf(stderr, "WARN: %s\n", message);
152 fprintf(stderr, "ERROR: %s\n", message);
154 case CDIO_LOG_ASSERT:
155 fprintf(stderr, "ASSERT ERROR: %s\n", message);
158 fprintf(stderr, "UNKNOWN ERROR: %s\n%s %d\n", message,
159 "The above message had unknown cdio log level",
163 /* gl_default_cdio_log_handler (level, message); */
166 /*****************************************************************************
167 * CDDAReadBlocks: reads a group of blocks from the CD-DA and returns
168 * an allocated pointer to the data. NULL is returned if no data
169 * read. It is also possible if we haven't read a RIFF header in which
170 * case one that we creaded during Open/Initialization is returned.
171 *****************************************************************************/
172 static block_t * CDDAReadBlocks( access_t * p_access )
175 cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
176 int i_blocks = p_cdda->i_blocks_per_read;
178 dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_LSN), "called %d",
181 /* Check end of file */
182 if( p_access->info.b_eof ) return NULL;
184 if( !p_cdda->b_header )
186 /* Return only the dummy RIFF header we created in Open/Init */
187 p_block = block_New( p_access, sizeof( WAVEHEADER ) );
188 memcpy( p_block->p_buffer, &p_cdda->waveheader, sizeof(WAVEHEADER) );
189 p_cdda->b_header = VLC_TRUE;
193 /* Check end of track */
194 while( p_cdda->i_lsn >= cdio_get_track_lsn(p_cdda->p_cdio,
197 if( p_cdda->i_track >= p_cdda->i_first_track + p_cdda->i_titles - 1 )
199 p_access->info.b_eof = VLC_TRUE;
203 p_access->info.i_update |= INPUT_UPDATE_TITLE;
204 p_access->info.i_title++;
207 if ( p_cdda-> b_nav_mode ) {
208 char *psz_title = CDDAFormatTitle( p_access, p_cdda->i_track );
209 input_Control( p_cdda->p_input, INPUT_SET_NAME, psz_title );
212 p_access->info.i_size =
213 p_cdda->p_title[p_access->info.i_title]->i_size;
214 p_access->info.i_pos = 0;
215 p_access->info.i_update |= INPUT_UPDATE_SIZE;
219 /* Possibly adjust i_blocks so we don't read past the end of a track. */
220 if( p_cdda->i_lsn + i_blocks >=
221 cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_track+1) )
223 i_blocks = cdio_get_track_lsn( p_cdda->p_cdio, p_cdda->i_track+1 )
227 /* Do the actual reading */
228 p_block = block_New( p_access, i_blocks * CDIO_CD_FRAMESIZE_RAW );
231 msg_Err( p_access, "Cannot get a new block of size: %i",
232 i_blocks * CDIO_CD_FRAMESIZE_RAW );
236 if( cdio_read_audio_sectors( p_cdda->p_cdio, p_block->p_buffer,
237 p_cdda->i_lsn, i_blocks) != 0 )
239 msg_Err( p_access, "could not read sector %lu",
240 (long unsigned int) p_cdda->i_lsn );
241 block_Release( p_block );
243 /* If we had problems above, assume the problem is with
244 the first sector of the read and set to skip it. In
245 the future libcdio may have cdparanoia support.
248 p_access->info.i_pos += CDIO_CD_FRAMESIZE_RAW;
252 p_cdda->i_lsn += i_blocks;
253 p_access->info.i_pos += p_block->i_buffer;
258 /****************************************************************************
259 * CDDASeek - change position for subsequent reads. For example, this
260 * can happen if the user moves a position slider bar in a GUI.
261 ****************************************************************************/
262 static int CDDASeek( access_t * p_access, int64_t i_pos )
264 cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
266 p_cdda->i_lsn = (i_pos / CDIO_CD_FRAMESIZE_RAW);
268 if ( ! p_cdda->b_nav_mode )
269 p_cdda->i_lsn += cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_track);
271 /* Seeked backwards and we are doing disc mode. */
272 if ( p_cdda->b_nav_mode && p_access->info.i_pos > i_pos ) {
276 for( i_track = p_cdda->i_track;
278 p_cdda->i_lsn < cdio_get_track_lsn(p_cdda->p_cdio, i_track);
279 i_track--, p_access->info.i_title-- ) ;
281 p_cdda->i_track = i_track;
282 p_access->info.i_update |= INPUT_UPDATE_TITLE;
283 psz_title = CDDAFormatTitle( p_access, p_cdda->i_track );
284 input_Control( p_cdda->p_input, INPUT_SET_NAME,
290 p_access->info.i_pos = i_pos;
292 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_SEEK),
293 "lsn %lu, offset: %lld",
294 (long unsigned int) p_cdda->i_lsn, i_pos );
298 /****************************************************************************
300 ****************************************************************************/
302 /*****************************************************************************
303 * Open: open cdda device or image file and initialize structures
304 * for subsequent operations.
305 *****************************************************************************/
307 CDDAOpen( vlc_object_t *p_this )
309 access_t *p_access = (access_t*)p_this;
310 char * psz_source = NULL;
311 cdda_data_t *p_cdda = NULL;
314 vlc_bool_t b_single_track = false;
315 int i_rc = VLC_EGENERIC;
317 p_access->p_sys = NULL;
319 /* Set where to log errors messages from libcdio. */
320 p_cdda_input = p_access;
322 /* parse the options passed in command line : */
324 if( p_access->psz_path && *p_access->psz_path )
326 char *psz_parser = psz_source = strdup( p_access->psz_path );
328 while( *psz_parser && *psz_parser != '@' )
333 if( *psz_parser == '@' )
339 if ('T' == *psz_parser || 't' == *psz_parser )
342 i_track = (int)strtol( psz_parser, NULL, 10 );
343 i_track = i_track ? i_track : 1;
344 b_single_track = true;
348 if (!psz_source || !*psz_source)
350 /* No device/track given. Continue only when this plugin was
352 if( !p_this->b_force ) return VLC_EGENERIC;
354 psz_source = var_CreateGetString( p_this, "cd-audio" );
356 if( !psz_source || !*psz_source )
358 /* Scan for a CD-ROM drive with a CD-DA in it. */
360 cdio_get_devices_with_cap(NULL, CDIO_FS_AUDIO, false);
362 if (NULL == cd_drives || NULL == cd_drives[0] )
365 "libcdio couldn't find something with a CD-DA in it" );
366 if (cd_drives) cdio_free_device_list(cd_drives);
370 psz_source = strdup(cd_drives[0]);
371 cdio_free_device_list(cd_drives);
375 cdio_log_set_handler ( cdio_log_handler );
378 if( !(p_cdio = cdio_open( psz_source, DRIVER_UNKNOWN )) )
380 msg_Warn( p_access, "could not open %s", psz_source );
381 if (psz_source) free( psz_source );
385 p_cdda = malloc( sizeof(cdda_data_t) );
388 msg_Err( p_access, "out of memory" );
392 memset( p_cdda, 0, sizeof(cdda_data_t) );
395 cddb_log_set_handler ( cddb_log_handler );
396 p_cdda->cddb.disc = NULL;
397 p_cdda->b_cddb_enabled =
398 config_GetInt( p_access, MODULE_STRING "-cddb-enabled" );
401 p_cdda->b_cdtext_enabled =
402 config_GetInt( p_access, MODULE_STRING "-cdtext-enabled" );
404 p_cdda->b_cdtext_prefer =
405 config_GetInt( p_access, MODULE_STRING "-cdtext-prefer" );
407 p_cdda->psz_source = strdup(psz_source);
408 p_cdda->b_header = VLC_FALSE;
409 p_cdda->p_cdio = p_cdio;
410 p_cdda->i_tracks = 0;
411 p_cdda->i_titles = 0;
412 p_cdda->i_track = i_track;
413 p_cdda->i_debug = config_GetInt(p_this, MODULE_STRING
415 p_cdda->b_nav_mode = config_GetInt(p_this, MODULE_STRING
416 "-navigation-mode" );
417 p_cdda->i_blocks_per_read
418 = config_GetInt(p_this, MODULE_STRING "-blocks-per-read");
420 p_cdda->p_input = vlc_object_find( p_access, VLC_OBJECT_INPUT,
423 if (0 == p_cdda->i_blocks_per_read)
424 p_cdda->i_blocks_per_read = DEFAULT_BLOCKS_PER_READ;
426 if ( p_cdda->i_blocks_per_read < MIN_BLOCKS_PER_READ
427 || p_cdda->i_blocks_per_read > MAX_BLOCKS_PER_READ )
429 msg_Warn( p_cdda_input,
430 "Number of blocks (%d) has to be between %d and %d. "
432 p_cdda->i_blocks_per_read,
433 MIN_BLOCKS_PER_READ, MAX_BLOCKS_PER_READ,
434 DEFAULT_BLOCKS_PER_READ );
435 p_cdda->i_blocks_per_read = DEFAULT_BLOCKS_PER_READ;
438 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "%s", psz_source );
440 /* Set up p_access */
441 p_access->pf_read = NULL;
442 p_access->pf_block = CDDAReadBlocks;
443 p_access->pf_control = CDDAControl;
444 p_access->pf_seek = CDDASeek;
446 p_access->info.i_size = 0;
448 p_access->info.i_update = 0;
449 p_access->info.b_eof = VLC_FALSE;
450 p_access->info.i_title = 0;
451 p_access->info.i_seekpoint = 0;
453 p_access->p_sys = (access_sys_t *) p_cdda;
455 /* We read the Table Of Content information */
456 i_rc = CDDAInit( p_access, p_cdda );
457 if ( VLC_SUCCESS != i_rc ) goto error;
459 CDDAFixupPlaylist( p_access, p_cdda, b_single_track );
461 /* Build a WAV header to put in front of the output data.
462 This gets sent back in the Block (read) routine.
464 memset( &p_cdda->waveheader, 0, sizeof(WAVEHEADER) );
465 SetWLE( &p_cdda->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
466 SetWLE( &p_cdda->waveheader.BitsPerSample, 16);
467 p_cdda->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
468 p_cdda->waveheader.Length = 0; /* we just don't know */
469 p_cdda->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
470 p_cdda->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
471 SetDWLE( &p_cdda->waveheader.SubChunkLength, 16);
472 SetWLE( &p_cdda->waveheader.Modus, 2);
473 SetDWLE( &p_cdda->waveheader.SampleFreq, CDDA_FREQUENCY_SAMPLE);
474 SetWLE( &p_cdda->waveheader.BytesPerSample,
475 2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
476 SetDWLE( &p_cdda->waveheader.BytesPerSec,
477 2*16/8 /*BytesPerSample*/ * CDDA_FREQUENCY_SAMPLE );
478 p_cdda->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
479 p_cdda->waveheader.DataLength = 0; /* we just don't know */
482 var_Create( p_access, MODULE_STRING "-caching",
483 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
484 vlc_object_release( p_cdda->p_input );
488 cdio_destroy( p_cdda->p_cdio );
489 if( psz_source) free( psz_source );
491 if ( p_cdda->p_input )
492 vlc_object_release( p_cdda->p_input );
500 /*****************************************************************************
501 * CDDAClose: closes cdda and frees any resources associded with it.
502 *****************************************************************************/
504 CDDAClose (vlc_object_t *p_this )
506 access_t *p_access = (access_t *) p_this;
507 cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
510 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
512 /* Remove playlist titles */
513 for( i = 0; i < p_cdda->i_titles; i++ )
515 vlc_input_title_Delete( p_cdda->p_title[i] );
518 cdio_destroy( p_cdda->p_cdio );
520 cdio_log_set_handler (uninit_log_handler);
523 cddb_log_set_handler ((cddb_log_handler_t) uninit_log_handler);
524 if (p_cdda->b_cddb_enabled)
525 cddb_disc_destroy(p_cdda->cddb.disc);
528 if (p_cdda->psz_mcn) free( p_cdda->psz_mcn );
529 if (p_cdda->psz_source) free( p_cdda->psz_source );
534 /*****************************************************************************
535 * Control: The front-end or vlc engine calls here to ether get
536 * information such as meta information or plugin capabilities or to
537 * issue miscellaneous "set" requests.
538 *****************************************************************************/
539 static int CDDAControl( access_t *p_access, int i_query, va_list args )
541 cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
545 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_EVENT),
546 "query %d", i_query );
550 /* Pass back a copy of meta information that was gathered when we
551 during the Open/Initialize call.
553 case ACCESS_GET_META:
555 vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
556 if ( p_cdda->p_meta )
558 *pp_meta = vlc_meta_Duplicate( p_cdda->p_meta );
559 dbg_print( INPUT_DBG_META, "%s", "Meta copied" );
563 msg_Warn( p_access, "tried to copy NULL meta info" );
568 case ACCESS_CAN_SEEK:
569 case ACCESS_CAN_FASTSEEK:
570 case ACCESS_CAN_PAUSE:
571 case ACCESS_CAN_CONTROL_PACE:
573 vlc_bool_t *pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
581 pi_int = (int*)va_arg( args, int * );
582 *pi_int = p_cdda-> i_blocks_per_read * CDIO_CD_FRAMESIZE_RAW;
586 case ACCESS_GET_PTS_DELAY:
588 int64_t *pi_64 = (int64_t*)va_arg( args, int64_t * );
589 *pi_64 = var_GetInteger( p_access, MODULE_STRING "-caching" )
590 * MILLISECONDS_PER_SEC;
595 case ACCESS_SET_PAUSE_STATE:
598 case ACCESS_GET_TITLE_INFO:
600 input_title_t ***ppp_title =
601 (input_title_t***)va_arg( args, input_title_t*** );
603 pi_int = (int*)va_arg( args, int* );
604 *((int*)va_arg( args, int* )) = 1; /* Title offset */
606 dbg_print ( INPUT_DBG_EVENT,
607 "GET TITLE: i_tracks %d, i_tracks %d",
608 p_cdda->i_tracks, p_cdda->i_tracks );
610 CDDAMetaInfo( p_access, CDIO_INVALID_TRACK );
612 if ( p_cdda->b_nav_mode) {
614 CDDAFormatTitle( p_access, p_cdda->i_track );
615 input_Control( p_cdda->p_input, INPUT_SET_NAME,
620 /* Duplicate title info */
621 if( p_cdda->i_titles == 0 )
623 *pi_int = 0; ppp_title = NULL;
626 *pi_int = p_cdda->i_titles;
627 *ppp_title = malloc(sizeof( input_title_t **) * p_cdda->i_titles );
629 if (!*ppp_title) return VLC_ENOMEM;
631 for( i = 0; i < p_cdda->i_titles; i++ )
633 if ( p_cdda->p_title[i] ) {
635 vlc_input_title_Duplicate( p_cdda->p_title[i] );
641 case ACCESS_SET_TITLE:
643 i = (int)va_arg( args, int );
645 dbg_print( INPUT_DBG_EVENT, "set title %d", i );
646 if( i != p_access->info.i_title )
649 p_access->info.i_update |= INPUT_UPDATE_TITLE;
650 p_access->info.i_title = i;
651 if ( p_cdda->b_nav_mode) {
653 CDDAFormatTitle( p_access, i+1 );
654 input_Control( p_cdda->p_input, INPUT_SET_NAME,
657 p_cdda->i_track = i+1;
658 p_access->info.i_pos =
659 cdio_get_track_lsn( p_cdda->p_cdio, p_cdda->i_track )
660 * CDIO_CD_FRAMESIZE_RAW;
662 p_access->info.i_update |= INPUT_UPDATE_SIZE;
663 p_access->info.i_size = p_cdda->p_title[i]->i_size;
664 p_access->info.i_pos = 0;
667 /* Next sector to read */
669 cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_first_track+i);
674 case ACCESS_SET_SEEKPOINT:
675 case ACCESS_SET_PRIVATE_ID_STATE:
679 msg_Warn( p_access, "unimplemented query in control" );
686 /*****************************************************************************
689 Initialize information pertaining to the CD: the number of tracks,
690 first track number, LSNs for each track and the leadout. The leadout
691 information is stored after the last track. The LSN array is
692 0-origin, same as p_access->info. Add first_track to get what track
693 number this is on the CD. Note: libcdio uses the real track number.
695 On input we assume p_cdda->p_cdio and p_cdda->i_track have been set.
697 We return the VLC-type status, e.g. VLC_SUCCESS, VLC_ENOMEM, etc.
698 *****************************************************************************/
699 static int CDDAInit( access_t *p_access, cdda_data_t *p_cdda )
701 discmode_t discmode = CDIO_DISC_MODE_NO_INFO;
703 p_cdda->i_tracks = cdio_get_num_tracks(p_cdda->p_cdio);
704 p_cdda->i_first_track = cdio_get_first_track_num(p_cdda->p_cdio);
706 discmode = cdio_get_discmode(p_cdda->p_cdio);
708 case CDIO_DISC_MODE_CD_DA:
709 case CDIO_DISC_MODE_CD_MIXED:
710 /* These are possible for CD-DA */
713 /* These are not possible for CD-DA */
715 "Disc seems not to be CD-DA. libcdio reports it is %s",
716 discmode2str[discmode]
721 /* Set reading start LSN. */
722 p_cdda->i_lsn = cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_track);