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 | INPUT_UPDATE_SIZE;
204 p_access->info.i_title++;
205 p_access->info.i_size =
206 p_cdda->p_title[p_access->info.i_title]->i_size;
207 p_access->info.i_pos = 0;
211 /* Possibly adjust i_blocks so we don't read past the end of a track. */
212 if( p_cdda->i_lsn + i_blocks >=
213 cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_track+1) )
215 i_blocks = cdio_get_track_lsn( p_cdda->p_cdio, p_cdda->i_track+1 )
219 /* Do the actual reading */
220 p_block = block_New( p_access, i_blocks * CDIO_CD_FRAMESIZE_RAW );
223 msg_Err( p_access, "Cannot get a new block of size: %i",
224 i_blocks * CDIO_CD_FRAMESIZE_RAW );
228 if( cdio_read_audio_sectors( p_cdda->p_cdio, p_block->p_buffer,
229 p_cdda->i_lsn, i_blocks) != 0 )
231 msg_Err( p_access, "could not read sector %lu",
232 (long unsigned int) p_cdda->i_lsn );
233 block_Release( p_block );
235 /* If we had problems above, assume the problem is with
236 the first sector of the read and set to skip it. In
237 the future libcdio may have cdparanoia support.
240 p_access->info.i_pos += CDIO_CD_FRAMESIZE_RAW;
244 p_cdda->i_lsn += i_blocks;
245 p_access->info.i_pos += p_block->i_buffer;
250 /****************************************************************************
251 * CDDASeek - change position for subsequent reads. For example, this
252 * can happen if the user moves a position slider bar in a GUI.
253 ****************************************************************************/
254 static int CDDASeek( access_t * p_access, int64_t i_pos )
256 cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
258 p_cdda->i_lsn = cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_track)
259 + (i_pos / CDIO_CD_FRAMESIZE_RAW);
260 p_access->info.i_pos = i_pos;
262 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_SEEK),
263 "lsn %lu, offset: %lld",
264 (long unsigned int) p_cdda->i_lsn, i_pos );
268 /****************************************************************************
270 ****************************************************************************/
272 /*****************************************************************************
273 * Open: open cdda device or image file and initialize structures
274 * for subsequent operations.
275 *****************************************************************************/
276 int E_(CDDAOpen)( vlc_object_t *p_this )
278 access_t *p_access = (access_t*)p_this;
279 char * psz_source = NULL;
280 cdda_data_t *p_cdda = NULL;
283 vlc_bool_t b_single_track = false;
284 int i_rc = VLC_EGENERIC;
286 p_access->p_sys = NULL;
288 /* Set where to log errors messages from libcdio. */
289 p_cdda_input = p_access;
291 /* parse the options passed in command line : */
293 if( p_access->psz_path && *p_access->psz_path )
295 char *psz_parser = psz_source = strdup( p_access->psz_path );
297 while( *psz_parser && *psz_parser != '@' )
302 if( *psz_parser == '@' )
308 if ('T' == *psz_parser || 't' == *psz_parser )
311 i_track = (int)strtol( psz_parser, NULL, 10 );
312 i_track = i_track ? i_track : 1;
313 b_single_track = true;
317 if (!psz_source || !*psz_source)
319 /* No device/track given. Continue only when this plugin was
321 if( !p_this->b_force ) return VLC_EGENERIC;
323 psz_source = var_CreateGetString( p_this, "cd-audio" );
325 if( !psz_source || !*psz_source )
327 /* Scan for a CD-ROM drive with a CD-DA in it. */
329 cdio_get_devices_with_cap(NULL, CDIO_FS_AUDIO, false);
331 if (NULL == cd_drives || NULL == cd_drives[0] )
334 "libcdio couldn't find something with a CD-DA in it" );
335 if (cd_drives) cdio_free_device_list(cd_drives);
339 psz_source = strdup(cd_drives[0]);
340 cdio_free_device_list(cd_drives);
344 cdio_log_set_handler ( cdio_log_handler );
347 if( !(p_cdio = cdio_open( psz_source, DRIVER_UNKNOWN )) )
349 msg_Warn( p_access, "could not open %s", psz_source );
350 if (psz_source) free( psz_source );
354 p_cdda = malloc( sizeof(cdda_data_t) );
357 msg_Err( p_access, "out of memory" );
361 memset( p_cdda, 0, sizeof(cdda_data_t) );
364 cddb_log_set_handler ( cddb_log_handler );
365 p_cdda->cddb.disc = NULL;
366 p_cdda->b_cddb_enabled =
367 config_GetInt( p_access, MODULE_STRING "-cddb-enabled" );
370 p_cdda->b_cdtext_enabled =
371 config_GetInt( p_access, MODULE_STRING "-cdtext-enabled" );
373 p_cdda->b_cdtext_prefer =
374 config_GetInt( p_access, MODULE_STRING "-cdtext-prefer" );
376 p_cdda->psz_source = strdup(psz_source);
377 p_cdda->b_header = VLC_FALSE;
378 p_cdda->p_cdio = p_cdio;
379 p_cdda->i_tracks = 0;
380 p_cdda->i_titles = 0;
381 p_cdda->i_track = i_track;
382 p_cdda->i_debug = config_GetInt(p_this, MODULE_STRING "-debug");
383 p_cdda->i_blocks_per_read
384 = config_GetInt(p_this, MODULE_STRING "-blocks-per-read");
386 p_cdda->p_input = vlc_object_find( p_access, VLC_OBJECT_INPUT,
389 if (0 == p_cdda->i_blocks_per_read)
390 p_cdda->i_blocks_per_read = DEFAULT_BLOCKS_PER_READ;
392 if ( p_cdda->i_blocks_per_read < MIN_BLOCKS_PER_READ
393 || p_cdda->i_blocks_per_read > MAX_BLOCKS_PER_READ )
395 msg_Warn( p_cdda_input,
396 "Number of blocks (%d) has to be between %d and %d. "
398 p_cdda->i_blocks_per_read,
399 MIN_BLOCKS_PER_READ, MAX_BLOCKS_PER_READ,
400 DEFAULT_BLOCKS_PER_READ );
401 p_cdda->i_blocks_per_read = DEFAULT_BLOCKS_PER_READ;
404 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "%s", psz_source );
406 /* Set up p_access */
407 p_access->pf_read = NULL;
408 p_access->pf_block = CDDAReadBlocks;
409 p_access->pf_control = CDDAControl;
410 p_access->pf_seek = CDDASeek;
412 p_access->info.i_size = 0;
414 p_access->info.i_update = 0;
415 p_access->info.b_eof = VLC_FALSE;
416 p_access->info.i_title = 0;
417 p_access->info.i_seekpoint = 0;
419 p_access->p_sys = (access_sys_t *) p_cdda;
421 /* We read the Table Of Content information */
422 i_rc = CDDAInit( p_access, p_cdda );
423 if ( VLC_SUCCESS != i_rc ) goto error;
425 CDDAFixupPlaylist( p_access, p_cdda, b_single_track );
427 /* Build a WAV header to put in front of the output data.
428 This gets sent back in the Block (read) routine.
430 memset( &p_cdda->waveheader, 0, sizeof(WAVEHEADER) );
431 SetWLE( &p_cdda->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
432 SetWLE( &p_cdda->waveheader.BitsPerSample, 16);
433 p_cdda->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
434 p_cdda->waveheader.Length = 0; /* we just don't know */
435 p_cdda->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
436 p_cdda->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
437 SetDWLE( &p_cdda->waveheader.SubChunkLength, 16);
438 SetWLE( &p_cdda->waveheader.Modus, 2);
439 SetDWLE( &p_cdda->waveheader.SampleFreq, CDDA_FREQUENCY_SAMPLE);
440 SetWLE( &p_cdda->waveheader.BytesPerSample,
441 2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
442 SetDWLE( &p_cdda->waveheader.BytesPerSec,
443 2*16/8 /*BytesPerSample*/ * CDDA_FREQUENCY_SAMPLE );
444 p_cdda->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
445 p_cdda->waveheader.DataLength = 0; /* we just don't know */
448 var_Create( p_access, MODULE_STRING "-caching",
449 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
450 vlc_object_release( p_cdda->p_input );
454 cdio_destroy( p_cdda->p_cdio );
455 if( psz_source) free( psz_source );
457 if ( p_cdda->p_input )
458 vlc_object_release( p_cdda->p_input );
466 /*****************************************************************************
467 * CDDAClose: closes cdda and frees any resources associded with it.
468 *****************************************************************************/
469 void E_(CDDAClose)( vlc_object_t *p_this )
471 access_t *p_access = (access_t *) p_this;
472 cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
475 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
477 /* Remove playlist titles */
478 for( i = 0; i < p_cdda->i_titles; i++ )
480 vlc_input_title_Delete( p_cdda->p_title[i] );
483 cdio_destroy( p_cdda->p_cdio );
485 cdio_log_set_handler (uninit_log_handler);
488 cddb_log_set_handler ((cddb_log_handler_t) uninit_log_handler);
489 if (p_cdda->b_cddb_enabled)
490 cddb_disc_destroy(p_cdda->cddb.disc);
493 if (p_cdda->psz_mcn) free( p_cdda->psz_mcn );
494 if (p_cdda->psz_source) free( p_cdda->psz_source );
499 /*****************************************************************************
500 * Control: The front-end or vlc engine calls here to ether get
501 * information such as meta information or plugin capabilities or to
502 * issue miscellaneous "set" requests.
503 *****************************************************************************/
504 static int CDDAControl( access_t *p_access, int i_query, va_list args )
506 cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
510 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_EVENT),
511 "query %d", i_query );
515 /* Pass back a copy of meta information that was gathered when we
516 during the Open/Initialize call.
518 case ACCESS_GET_META:
520 vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
521 if ( p_cdda->p_meta )
523 *pp_meta = vlc_meta_Duplicate( p_cdda->p_meta );
524 dbg_print( INPUT_DBG_META, "%s", "Meta copied" );
528 msg_Warn( p_access, "tried to copy NULL meta info" );
533 case ACCESS_CAN_SEEK:
534 case ACCESS_CAN_FASTSEEK:
535 case ACCESS_CAN_PAUSE:
536 case ACCESS_CAN_CONTROL_PACE:
538 vlc_bool_t *pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
546 pi_int = (int*)va_arg( args, int * );
547 *pi_int = p_cdda-> i_blocks_per_read * CDIO_CD_FRAMESIZE_RAW;
551 case ACCESS_GET_PTS_DELAY:
553 int64_t *pi_64 = (int64_t*)va_arg( args, int64_t * );
554 *pi_64 = var_GetInteger( p_access, MODULE_STRING "-caching" )
555 * MILLISECONDS_PER_SEC;
560 case ACCESS_SET_PAUSE_STATE:
563 case ACCESS_GET_TITLE_INFO:
565 unsigned int psz_mrl_max = strlen(CDDA_MRL_PREFIX)
566 + strlen(p_cdda->psz_source) + 1;
567 input_title_t ***ppp_title =
568 (input_title_t***)va_arg( args, input_title_t*** );
569 char *psz_mrl = malloc( psz_mrl_max );
571 pi_int = (int*)va_arg( args, int* );
572 *((int*)va_arg( args, int* )) = 1; /* Title offset */
574 dbg_print ( INPUT_DBG_EVENT,
575 "GET TITLE: i_tracks %d, i_tracks %d",
576 p_cdda->i_tracks, p_cdda->i_tracks );
578 if( psz_mrl == NULL ) {
579 msg_Warn( p_access, "out of memory" );
581 snprintf(psz_mrl, psz_mrl_max, "%s%s",
582 CDDA_MRL_PREFIX, p_cdda->psz_source);
583 CDDAMetaInfo( p_access, -1, psz_mrl );
587 /* Duplicate title info */
588 if( p_cdda->i_titles == 0 )
590 *pi_int = 0; ppp_title = NULL;
593 *pi_int = p_cdda->i_titles;
594 *ppp_title = malloc(sizeof( input_title_t **) * p_cdda->i_titles );
596 if (!*ppp_title) return VLC_ENOMEM;
598 for( i = 0; i < p_cdda->i_titles; i++ )
600 if ( p_cdda->p_title[i] ) {
602 vlc_input_title_Duplicate( p_cdda->p_title[i] );
608 case ACCESS_SET_TITLE:
610 i = (int)va_arg( args, int );
612 dbg_print( INPUT_DBG_EVENT, "set title %d", i );
613 if( i != p_access->info.i_title )
616 p_access->info.i_update |=
617 INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE;
618 p_access->info.i_title = i;
619 p_access->info.i_size = p_cdda->p_title[i]->i_size;
620 p_access->info.i_pos = 0;
622 /* Next sector to read */
624 cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_first_track+i);
629 case ACCESS_SET_SEEKPOINT:
630 case ACCESS_SET_PRIVATE_ID_STATE:
634 msg_Warn( p_access, "unimplemented query in control" );
641 /*****************************************************************************
644 Initialize information pertaining to the CD: the number of tracks,
645 first track number, LSNs for each track and the leadout. The leadout
646 information is stored after the last track. The LSN array is
647 0-origin, same as p_access->info. Add first_track to get what track
648 number this is on the CD. Note: libcdio uses the real track number.
650 On input we assume p_cdda->p_cdio and p_cdda->i_track have been set.
652 We return the VLC-type status, e.g. VLC_SUCCESS, VLC_ENOMEM, etc.
653 *****************************************************************************/
654 static int CDDAInit( access_t *p_access, cdda_data_t *p_cdda )
656 discmode_t discmode = CDIO_DISC_MODE_NO_INFO;
658 p_cdda->i_tracks = cdio_get_num_tracks(p_cdda->p_cdio);
659 p_cdda->i_first_track = cdio_get_first_track_num(p_cdda->p_cdio);
661 discmode = cdio_get_discmode(p_cdda->p_cdio);
663 case CDIO_DISC_MODE_CD_DA:
664 case CDIO_DISC_MODE_CD_MIXED:
665 /* These are possible for CD-DA */
668 /* These are not possible for CD-DA */
670 "Disc seems not to be CD-DA. libcdio reports it is %s",
671 discmode2str[discmode]
676 /* Set reading start LSN. */
677 p_cdda->i_lsn = cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_track);