1 /*****************************************************************************
2 * cddax.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 *****************************************************************************/
33 #include <vlc/input.h>
35 #include <sys/types.h>
36 #include <cdio/cdio.h>
37 #include <cdio/cd_types.h>
54 /* how many blocks Open will read in each loop */
55 #define CDDA_BLOCKS_ONCE 20
56 #define CDDA_DATA_ONCE (CDDA_BLOCKS_ONCE * CDIO_CD_FRAMESIZE_RAW)
58 #define CDDA_MRL_PREFIX "cddax://"
60 /* FIXME: This variable is a hack. Would be nice to eliminate. */
61 static access_t *p_cdda_input = NULL;
63 /*****************************************************************************
65 *****************************************************************************/
66 static block_t *CDDABlock( access_t * p_access );
67 static int CDDASeek( access_t * p_access, int64_t i_pos );
68 static int CDDAControl( access_t *p_access, int i_query,
70 static void CDDAMetaInfo( access_t *p_access );
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;
84 if (p_cdda->i_debug & INPUT_DBG_CDIO)
85 msg_Dbg( p_cdda_input, message);
88 msg_Warn( p_cdda_input, message);
92 msg_Err( p_cdda_input, message);
95 msg_Warn( p_cdda_input, message,
96 _("The above message had unknown cdio log level"),
104 /*! This routine is called by libcddb routines on error.
105 Setup is done by init_input_plugin.
108 cddb_log_handler (cddb_log_level_t level, const char message[])
110 cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
114 if (!(p_cdda->i_debug & INPUT_DBG_CDDB)) return;
115 /* Fall through if to warn case */
117 cdio_log_handler (level, message);
120 #endif /*HAVE_LIBCDDB*/
123 /*! This routine is when xine is not fully set up (before full initialization)
124 or is not around (before finalization).
127 uninit_log_handler (cdio_log_level_t level, const char message[])
129 cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
133 if (!(p_cdda->i_debug & (INPUT_DBG_CDIO|INPUT_DBG_CDDB)))
135 /* Fall through if to warn case */
137 fprintf(stderr, "WARN: %s\n", message);
140 fprintf(stderr, "ERROR: %s\n", message);
142 case CDIO_LOG_ASSERT:
143 fprintf(stderr, "ASSERT ERROR: %s\n", message);
146 fprintf(stderr, "UNKNOWN ERROR: %s\n%s %d\n",
148 _("The above message had unknown cdio log level"),
152 /* gl_default_cdio_log_handler (level, message); */
155 /*****************************************************************************
156 * CDDARead: reads CDDA_BLOCKS_ONCE from the CD-DA and returns an
157 * allocated pointer to the data. NULL is returned if no data read. It
158 * is also possible if we haven't read a RIFF header in which case one
159 * that we creaded during Open/Initialization is returned.
160 *****************************************************************************/
162 CDDABlock( access_t * p_access )
165 cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
166 int i_blocks = CDDA_BLOCKS_ONCE;
168 dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_LSN), "called %d",
171 /* Check end of file */
172 if( p_access->info.b_eof ) return NULL;
174 if( !p_cdda->b_header )
176 /* Return only the dummy RIFF header we created in Open/Init */
177 p_block = block_New( p_access, sizeof( WAVEHEADER ) );
178 memcpy( p_block->p_buffer, &p_cdda->waveheader, sizeof(WAVEHEADER) );
179 p_cdda->b_header = VLC_TRUE;
183 /* Check end of track */
184 while( p_cdda->i_sector >= p_cdda->p_sectors[p_access->info.i_title + 1] )
186 if( p_access->info.i_title + 1 >= p_cdda->i_tracks )
188 p_access->info.b_eof = VLC_TRUE;
192 p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SIZE;
193 p_access->info.i_title++;
194 p_access->info.i_size =
195 p_cdda->p_title[p_access->info.i_title]->i_size;
196 p_access->info.i_pos = 0;
199 /* Don't read after the end of a title */
200 if( p_cdda->i_sector + i_blocks >=
201 p_cdda->p_sectors[p_access->info.i_title + 1] )
203 i_blocks = p_cdda->p_sectors[p_access->info.i_title + 1 ] -
207 /* Do the actual reading */
208 p_block = block_New( p_access, i_blocks * CDIO_CD_FRAMESIZE_RAW );
211 msg_Err( p_access, "cannot get a new block of size: %i",
212 i_blocks * CDIO_CD_FRAMESIZE_RAW );
216 if( cdio_read_audio_sectors( p_cdda->p_cddev->cdio, p_block->p_buffer,
217 p_cdda->i_sector, i_blocks) != 0 )
219 msg_Err( p_access, "could not read sector %lu",
220 (long unsigned int) p_cdda->i_sector );
221 block_Release( p_block );
223 /* If we had problems above, assume the problem is with
224 the first sector of the read and set to skip it. In
225 the future libcdio may have cdparanoia support.
228 p_access->info.i_pos += CDIO_CD_FRAMESIZE_RAW;
232 p_cdda->i_sector += i_blocks;
233 p_access->info.i_pos += p_block->i_buffer;
238 /****************************************************************************
239 * CDDASeek - change position for subsequent reads. For example, this
240 * can happen if the user moves a position slider bar in a GUI.
241 ****************************************************************************/
243 CDDASeek( access_t * p_access, int64_t i_pos )
245 cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
247 p_cdda->i_sector = p_cdda->p_sectors[p_access->info.i_title]
248 + i_pos / CDIO_CD_FRAMESIZE_RAW;
249 p_access->info.i_pos = i_pos;
251 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_SEEK),
252 "sector %lu, offset: %lld",
253 (long unsigned int) p_cdda->i_sector, i_pos );
259 #define free_and_dup(var, val) \
260 if (var) free(var); \
261 if (val) var=strdup(val);
265 GetCDDBInfo( access_t *p_access, cdda_data_t *p_cdda )
268 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
270 if (config_GetInt( p_access, MODULE_STRING "-cddb-enabled" )) {
272 cddb_conn_t *conn = cddb_new();
273 const CdIo *p_cdio = p_cdda->p_cddev->cdio;
276 cddb_log_set_handler (uninit_log_handler);
279 msg_Warn( p_access, "unable to initialize libcddb" );
283 cddb_set_email_address( conn,
284 config_GetPsz( p_access,
285 MODULE_STRING "-cddb-email") );
287 cddb_set_server_name( conn,
288 config_GetPsz( p_access,
289 MODULE_STRING "-cddb-server") );
291 cddb_set_server_port(conn,
292 config_GetInt( p_access,
293 MODULE_STRING "-cddb-port") );
295 /* Set the location of the local CDDB cache directory.
296 The default location of this directory is */
298 if (!config_GetInt( p_access, MODULE_STRING "-cddb-enable-cache" ))
299 cddb_cache_disable(conn);
301 cddb_cache_set_dir(conn,
302 config_GetPsz( p_access,
303 MODULE_STRING "-cddb-cachedir") );
305 cddb_set_timeout(conn,
306 config_GetInt( p_access, MODULE_STRING "-cddb-timeout") );
309 if (config_GetInt( p_access, MODULE_STRING "-cddb-httpd" )) {
310 cddb_http_enable(conn);
312 cddb_http_disable(conn);
314 p_cdda->cddb.disc = cddb_disc_new();
315 if (!p_cdda->cddb.disc) {
316 msg_Err( p_access, "Unable to create CDDB disc structure." );
320 p_cdda->psz_mcn = cdio_get_mcn(p_cdio);
322 for(i = 1; i <= p_cdda->i_tracks; i++) {
323 cddb_track_t *t = cddb_track_new();
324 t->frame_offset = cdio_get_track_lba(p_cdio, i);
325 cddb_disc_add_track(p_cdda->cddb.disc, t);
328 p_cdda->cddb.disc->length =
329 cdio_get_track_lba(p_cdio, CDIO_CDROM_LEADOUT_TRACK)
330 / CDIO_CD_FRAMES_PER_SEC;
332 if (!cddb_disc_calc_discid(p_cdda->cddb.disc)) {
333 msg_Err( p_access, "CDDB disc calc failed" );
337 i_matches = cddb_query(conn, p_cdda->cddb.disc);
340 msg_Warn( p_access, "Found %d matches in CDDB. Using first one.",
342 cddb_read(conn, p_cdda->cddb.disc);
344 if (p_cdda->i_debug & INPUT_DBG_CDDB)
345 cddb_disc_print(p_cdda->cddb.disc);
348 msg_Warn( p_access, "CDDB error: %s", cddb_error_str(errno));
356 #endif /*HAVE_LIBCDDB*/
358 #define add_meta_val(FIELD, VLC_META, VAL) \
359 if ( p_cdda->p_meta ) { \
360 vlc_meta_Add( p_cdda->p_meta, VLC_META, VAL ); \
361 dbg_print( INPUT_DBG_META, "field %s: %s\n", VLC_META, VAL ); \
364 #define add_cddb_meta(FIELD, VLC_META) \
365 add_meta_val(FIELD, VLC_META, p_cdda->cddb.disc->FIELD);
367 #define add_cddb_meta_fmt(FIELD, FORMAT_SPEC, VLC_META) \
370 snprintf( psz_buf, sizeof(psz_buf)-1, FORMAT_SPEC, \
371 p_cdda->cddb.disc->FIELD ); \
372 psz_buf[sizeof(psz_buf)-1] = '\0'; \
373 add_meta_val(FIELD, VLC_META, psz_buf); \
377 Gets and saves CDDA Meta Information. In the Control routine,
378 we handle Meta Information requests and basically copy what we've
381 static void CDDAMetaInfo( access_t *p_access )
383 cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
386 if ( p_cdda && p_cdda->i_cddb_enabled ) {
388 GetCDDBInfo(p_access, p_cdda);
390 if ( p_cdda->cddb.disc ) {
392 p_cdda->p_meta = vlc_meta_New();
394 add_cddb_meta(title, VLC_META_CDDB_TITLE);
395 add_cddb_meta(artist, VLC_META_CDDB_ARTIST);
396 add_cddb_meta(genre, VLC_META_CDDB_GENRE);
397 add_cddb_meta(ext_data, VLC_META_CDDB_EXT_DATA);
399 add_cddb_meta_fmt(year, "%d", VLC_META_CDDB_YEAR);
400 add_cddb_meta_fmt(discid, "%x", VLC_META_CDDB_DISCID);
404 #endif /*HAVE_LIBCDDB*/
407 #if UPDATE_TRACK_INFORMATION_FINISHED
409 track_t i_track = p_cdda->i_tracks;
410 char psz_buffer[MSTRTIME_MAX_SIZE];
412 (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[0])
413 / CDIO_CD_FRAMES_PER_SEC;
415 dbg_print( INPUT_DBG_META, "Duration %ld", (long int) i_duration );
416 input_Control( p_access, INPUT_ADD_INFO, _("General"), _("Duration"), "%s",
417 secstotimestr( psz_buffer, i_duration ) );
419 for( i_track = 0 ; i_track < p_cdda->i_tracks ; i_track++ ) {
420 char track_str[TITLE_MAX];
422 (p_cdda->p_sectors[i_track+1] - p_cdda->p_sectors[i_track])
423 / CDIO_CD_FRAMES_PER_SEC;
424 snprintf(track_str, TITLE_MAX, "%s %02d", _("Track"), i_track+1);
425 input_Control( p_access, INPUT_ADD_INFO, track_str, _("Duration"), "%s",
426 secstotimestr( psz_buffer, i_duration ) );
429 if (p_cdda->i_cddb_enabled) {
430 cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
433 if ( t->artist != NULL && strlen(t->artist) ) {
434 input_Control( p_access, INPUT_ADD_INFO, track_str,
435 _("Artist"), "%s", t->artist );
437 if ( t->title != NULL && strlen(t->title) ) {
438 input_Control( p_access, INPUT_ADD_INFO, track_str,
439 _("Title"), "%s", t->title );
441 if ( t->ext_data != NULL && strlen(t->ext_data) ) {
442 input_Control( p_access, INPUT_ADD_INFO, track_str,
443 _("Extended Data"), "%s", t->ext_data );
447 #endif /*HAVE_LIBCDDB*/
450 #endif /* UPDATE_TRACK_INFORMATION_FINISHED */
453 #define add_format_str_info(val) \
455 const char *str = val; \
460 strncat(tp, str, TEMP_STR_LEN-(tp-temp_str)); \
463 saw_control_prefix = false; \
467 #define add_format_num_info(val, fmt) \
471 sprintf(num_str, fmt, val); \
472 len=strlen(num_str); \
474 strncat(tp, num_str, TEMP_STR_LEN-(tp-temp_str)); \
477 saw_control_prefix = false; \
481 Take a format string and expand escape sequences, that is sequences that
482 begin with %, with information from the current CD.
483 The expanded string is returned. Here is a list of escape sequences:
485 %a : The album artist **
486 %A : The album information **
491 %m : The CD-DA Media Catalog Number (MCN)
492 %n : The number of tracks on the CD
493 %p : The artist/performer/composer in the track **
494 %T : The track number **
495 %s : Number of seconds in this track
497 %Y : The year 19xx or 20xx **
501 CDDAFormatStr( const access_t *p_access, cdda_data_t *p_cdda,
502 const char format_str[], const char *mrl, int i_track)
504 #define TEMP_STR_SIZE 256
505 #define TEMP_STR_LEN (TEMP_STR_SIZE-1)
506 static char temp_str[TEMP_STR_SIZE];
508 char * tp = temp_str;
509 bool saw_control_prefix = false;
510 size_t format_len = strlen(format_str);
512 memset(temp_str, 0, TEMP_STR_SIZE);
514 for (i=0; i<format_len; i++) {
516 if (!saw_control_prefix && format_str[i] != '%') {
517 *tp++ = format_str[i];
518 saw_control_prefix = false;
522 switch(format_str[i]) {
524 if (saw_control_prefix) {
527 saw_control_prefix = !saw_control_prefix;
531 if (!p_cdda->i_cddb_enabled) goto not_special;
532 if (p_cdda->cddb.disc)
533 add_format_str_info(p_cdda->cddb.disc->artist);
536 if (!p_cdda->i_cddb_enabled) goto not_special;
537 if (p_cdda->cddb.disc)
538 add_format_str_info(p_cdda->cddb.disc->title);
541 if (!p_cdda->i_cddb_enabled) goto not_special;
542 if (p_cdda->cddb.disc)
543 add_format_str_info(CDDB_CATEGORY[p_cdda->cddb.disc->category]);
546 if (!p_cdda->i_cddb_enabled) goto not_special;
547 if (p_cdda->cddb.disc)
548 add_format_str_info(p_cdda->cddb.disc->genre);
551 if (!p_cdda->i_cddb_enabled) goto not_special;
552 if (p_cdda->cddb.disc)
553 add_format_num_info(p_cdda->cddb.disc->discid, "%x");
556 if (!p_cdda->i_cddb_enabled) goto not_special;
557 if (p_cdda->cddb.disc)
558 add_format_num_info(p_cdda->cddb.disc->year, "%5d");
561 if (p_cdda && p_cdda->i_cddb_enabled && p_cdda->cddb.disc) {
562 cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
564 if (t != NULL && t->title != NULL)
565 add_format_str_info(t->title);
566 } else goto not_special;
569 if (p_cdda->i_cddb_enabled && p_cdda->cddb.disc) {
570 cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
572 if (t != NULL && t->artist != NULL)
573 add_format_str_info(t->artist);
574 } else goto not_special;
577 if (p_cdda->i_cddb_enabled && p_cdda->cddb.disc) {
578 cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
580 if (t != NULL && t->ext_data != NULL)
581 add_format_str_info(t->ext_data);
582 } else goto not_special;
587 add_format_str_info(mrl);
591 add_format_str_info(p_cdda->psz_mcn);
595 add_format_num_info(p_cdda->i_tracks, "%d");
599 if (p_cdda->i_cddb_enabled) {
600 char psz_buffer[MSTRTIME_MAX_SIZE];
602 (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[i_track-1])
603 / CDIO_CD_FRAMES_PER_SEC;
604 add_format_str_info(secstotimestr( psz_buffer, i_duration ) );
605 } else goto not_special;
609 add_format_num_info(i_track, "%02d");
616 *tp++ = format_str[i];
617 saw_control_prefix = false;
620 return strdup(temp_str);
623 #ifdef TRACK_META_INFORMATION_FINISHED
625 CDDACreatePlayListItem(const input_thread_t *p_access, cdda_data_t *p_cdda,
626 playlist_t *p_playlist, unsigned int i_track,
627 char *psz_mrl, int psz_mrl_max,
628 const char *psz_source, int playlist_operation,
632 (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[i_track-1])
633 * (1000000 / CDIO_CD_FRAMES_PER_SEC) ;
636 char *config_varname = MODULE_STRING "-title-format";
637 playlist_item_t *p_item;
640 if (p_cdda->i_cddb_enabled) {
641 config_varname = MODULE_STRING "-cddb-title-format";
643 #endif /*HAVE_LIBCDDB*/
645 snprintf(psz_mrl, psz_mrl_max, "%s%s@T%u",
646 CDDA_MRL_PREFIX, psz_source, i_track);
648 p_title = CDDAFormatStr(p_access, p_cdda,
649 config_GetPsz( p_access, config_varname ),
652 dbg_print( INPUT_DBG_META, "mrl: %s, title: %s, duration, %ld, pos %d",
653 psz_mrl, p_title, (long int) i_duration / 1000000 , i_pos );
654 playlist_AddExt( p_playlist, psz_mrl, p_title, playlist_operation,
655 i_pos, i_duration , NULL, 0);
657 if( i_pos == PLAYLIST_END ) i_pos = p_playlist->i_size - 1;
659 vlc_mutex_lock( &p_playlist->object_lock );
660 p_item = playlist_ItemGetByPos( p_playlist, i_pos );
661 vlc_mutex_unlock( &p_playlist->object_lock );
665 vlc_mutex_lock( &p_item->input.lock );
668 CDDAFormatStr( p_access, p_cdda,
669 config_GetPsz( p_access, MODULE_STRING "-author-format" ),
672 playlist_ItemAddInfo( p_item , _("General"),_("Author"), p_author);
675 if (p_cdda->i_cddb_enabled) {
676 const char *psz_general_cat = _("General");
678 playlist_ItemAddInfo( p_item, psz_general_cat, _("Album"),
679 "%s", p_cdda->cddb.disc->title);
680 playlist_ItemAddInfo( p_item, psz_general_cat, _("Disc Artist(s)"),
681 "%s", p_cdda->cddb.disc->artist);
682 playlist_ItemAddInfo( p_item, psz_general_cat,
683 _("CDDB Disc Category"),
684 "%s", CDDB_CATEGORY[p_cdda->cddb.disc->category]);
685 playlist_ItemAddInfo( p_item, psz_general_cat, _("Genre"),
686 "%s", p_cdda->cddb.disc->genre);
687 if ( p_cdda->cddb.disc->discid ) {
688 playlist_ItemAddInfo( p_item, psz_general_cat, _("CDDB Disc ID"),
689 "%x", p_cdda->cddb.disc->discid );
691 if (p_cdda->cddb.disc->year != 0) {
692 playlist_ItemAddInfo( p_item, psz_general_cat,
693 _("Year"), "%5d", p_cdda->cddb.disc->year );
696 if (p_cdda->i_cddb_enabled) {
697 cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
699 if (t != NULL && t->artist != NULL) {
700 playlist_ItemAddInfo( p_item, psz_general_cat,
701 _("Track Artist"), "%s", t->artist );
702 playlist_ItemAddInfo( p_item , psz_general_cat,
703 _("Track Title"), "%s", t->title );
708 #endif /*HAVE_LIBCDDB*/
710 vlc_mutex_unlock( &p_item->input.lock );
714 CDDAFixupPlayList( access_t *p_access, cdda_data_t *p_cdda,
715 const char *psz_source, track_t i_track)
719 unsigned int psz_mrl_max = strlen(CDDA_MRL_PREFIX) + strlen(psz_source) +
720 strlen("@T") + strlen("100") + 1;
722 psz_mrl = malloc( psz_mrl_max );
724 if( psz_mrl == NULL )
726 msg_Warn( p_access, "out of memory" );
730 CDDAMetaInfo(p_access);
732 for( i = 1 ; i <= p_cdda->i_tracks ; i++ )
734 input_title_t *t = p_cdda->p_sectors[i-1] = vlc_input_title_New();
736 asprintf( &t->psz_name, _("Track %i"), i );
737 t->i_size = ( p_sys->p_sectors[i] - p_sys->p_sectors[i-1] ) *
738 (int64_t)CDIO_CD_FRAMESIZE_RAW;
740 t->i_length = I64C(1000000) * t->i_size / 44100 / 4;
741 CDDACreatePlayListItem(p_access, p_cdda, i, psz_mrl,
742 psz_mrl_max, psz_source, PLAYLIST_APPEND,
748 #endif /* TRACK_META_INFORMATION_FINISHED*/
750 /****************************************************************************
752 ****************************************************************************/
754 E_(CDDADebugCB) ( vlc_object_t *p_this, const char *psz_name,
755 vlc_value_t oldval, vlc_value_t val, void *p_data )
759 if (NULL == p_cdda_input) return VLC_EGENERIC;
761 p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
763 if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
764 msg_Dbg( p_cdda_input, "Old debug (x%0x) %d, new debug (x%0x) %d",
765 p_cdda->i_debug, p_cdda->i_debug, val.i_int, val.i_int);
767 p_cdda->i_debug = val.i_int;
772 E_(CDDBEnabledCB) ( vlc_object_t *p_this, const char *psz_name,
773 vlc_value_t oldval, vlc_value_t val, void *p_data )
777 if (NULL == p_cdda_input) return VLC_EGENERIC;
779 p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
782 if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
783 msg_Dbg( p_cdda_input, "Old CDDB Enabled (x%0x) %d, new (x%0x) %d",
784 p_cdda->i_cddb_enabled, p_cdda->i_cddb_enabled,
785 val.i_int, val.i_int);
787 p_cdda->i_cddb_enabled = val.i_int;
792 /*****************************************************************************
793 * Open: open cdda device or image file and initialize structures
794 * for subsequent operations.
795 *****************************************************************************/
797 E_(CDDAOpen)( vlc_object_t *p_this )
799 access_t *p_access = (access_t*)p_this;
801 cdda_data_t * p_cdda;
805 /* Set where to log errors messages from libcdio. */
806 p_cdda_input = p_access;
808 if( !p_access->psz_path || !*p_access->psz_path )
810 /* Only when selected */
811 if( !p_this->b_force ) return VLC_EGENERIC;
813 psz_source = var_CreateGetString( p_this, "cd-audio" );
815 if( !psz_source || !*psz_source ) {
816 /* Scan for a CD-ROM drive with a CD-DA in it. */
818 cdio_get_devices_with_cap(NULL, CDIO_FS_AUDIO, false);
819 if (NULL == cd_drives) return -1;
820 if (cd_drives[0] == NULL) {
821 cdio_free_device_list(cd_drives);
824 psz_source = strdup(cd_drives[0]);
825 cdio_free_device_list(cd_drives);
829 psz_source = strdup( p_access->psz_path );
831 cdio_log_set_handler ( cdio_log_handler );
834 if( !(p_cddev = ioctl_Open( p_this, psz_source )) )
836 msg_Warn( p_access, "could not open %s", psz_source );
840 p_cdda = malloc( sizeof(cdda_data_t) );
843 msg_Err( p_access, "out of memory" );
849 cddb_log_set_handler ( cddb_log_handler );
850 p_cdda->cddb.disc = NULL;
851 p_cdda->i_cddb_enabled =
852 config_GetInt( p_access, MODULE_STRING "-cddb-enabled" );
855 p_cdda->b_header = VLC_FALSE;
856 p_cdda->p_cddev = p_cddev;
857 p_cdda->i_debug = config_GetInt( p_this, MODULE_STRING "-debug" );
859 printf("+++debug: %d\n", p_cdda->i_debug);
861 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "%s", psz_source );
863 /* We read the Table Of Content information */
864 p_cdda->i_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_access),
865 p_cdda->p_cddev->cdio, &p_cdda->p_sectors );
866 if( p_cdda->i_tracks < 0 ) {
867 msg_Err( p_access, "unable to count tracks" );
869 } else if( p_cdda->i_tracks <= 0 ) {
870 msg_Err( p_access, "no audio tracks found" );
874 /* Set up p_access */
875 p_access->pf_read = NULL;
876 p_access->pf_block = CDDABlock;
877 p_access->pf_control = CDDAControl;
878 p_access->pf_seek = CDDASeek;
880 p_access->info.i_update = 0;
881 p_access->info.i_size = 0;
882 p_access->info.i_pos = 0;
883 p_access->info.b_eof = VLC_FALSE;
884 p_access->info.i_title = 0;
885 p_access->info.i_seekpoint = 0;
887 p_access->p_sys = (access_sys_t *) p_cdda;
889 CDDAMetaInfo(p_access);
892 char *psz_config_title_fmt = MODULE_STRING "-title-format";
893 const char *psz_title_fmt =
894 config_GetPsz( p_access, psz_config_title_fmt );
897 if (p_cdda->i_cddb_enabled) {
898 psz_config_title_fmt = MODULE_STRING "-cddb-title-format";
900 #endif /*HAVE_LIBCDDB*/
902 for( i = 1 ; i <= p_cdda->i_tracks ; i++ )
905 input_title_t *t = p_cdda->p_title[i-1] = vlc_input_title_New();
908 asprintf(&psz_mrl, "%s%s@T%u", CDDA_MRL_PREFIX, psz_source, i);
910 psz_title = CDDAFormatStr(p_access, p_cdda, psz_title_fmt,
913 t->psz_name = strdup(psz_title);
915 t->i_size = ( p_cdda->p_sectors[i] - p_cdda->p_sectors[i-1] ) *
916 (int64_t)CDIO_CD_FRAMESIZE_RAW;
918 t->i_length = I64C(1000000) * t->i_size / 44100 / 4;
920 dbg_print((INPUT_DBG_MRL), "track %d, start=%d, mrl %s",
921 i, p_cdda->p_sectors[i-1], psz_mrl );
927 /* Build a WAV header to put in front of the output data.
928 This gets sent back in the Block (read) routine.
930 memset( &p_cdda->waveheader, 0, sizeof(WAVEHEADER) );
931 SetWLE( &p_cdda->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
932 SetWLE( &p_cdda->waveheader.BitsPerSample, 16);
933 p_cdda->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
934 p_cdda->waveheader.Length = 0; /* we just don't know */
935 p_cdda->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
936 p_cdda->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
937 SetDWLE( &p_cdda->waveheader.SubChunkLength, 16);
938 SetWLE( &p_cdda->waveheader.Modus, 2);
939 SetDWLE( &p_cdda->waveheader.SampleFreq, 44100);
940 SetWLE( &p_cdda->waveheader.BytesPerSample,
941 2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
942 SetDWLE( &p_cdda->waveheader.BytesPerSec,
943 2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
944 p_cdda->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
945 p_cdda->waveheader.DataLength = 0; /* we just don't know */
948 var_Create( p_access, MODULE_STRING "-caching",
949 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
953 ioctl_Close( p_cdda->p_cddev );
961 /*****************************************************************************
962 * CDDAClose: closes cdda and frees any resources associded with it.
963 *****************************************************************************/
965 E_(CDDAClose)( vlc_object_t *p_this )
967 access_t *p_access = (access_t *) p_this;
968 cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
971 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
973 /* Remove playlist titles */
974 for( i = 0; i < p_cdda->i_tracks; i++ )
976 vlc_input_title_Delete( p_cdda->p_title[i] );
979 ioctl_Close( p_cdda->p_cddev );
981 cdio_log_set_handler (uninit_log_handler);
984 cddb_log_set_handler (uninit_log_handler);
985 if (p_cdda->i_cddb_enabled)
986 cddb_disc_destroy(p_cdda->cddb.disc);
989 free( p_cdda->p_sectors );
990 if (p_cdda->psz_mcn) free( p_cdda->psz_mcn );
995 /*****************************************************************************
996 * Control: The front-end or vlc engine calls here to ether get
997 * information such as meta information or plugin capabilities or to
998 * issue miscellaneous "set" requests.
999 *****************************************************************************/
1000 static int CDDAControl( access_t *p_access, int i_query, va_list args )
1002 cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
1003 vlc_bool_t *pb_bool;
1007 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_EVENT),
1008 "query %d", i_query );
1012 /* Pass back a copy of meta information that was gathered when we
1013 during the Open/Initialize call.
1015 case ACCESS_GET_META:
1017 vlc_meta_t **pp_meta;
1018 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1019 dbg_print( INPUT_DBG_META, "Meta copied");
1020 *pp_meta = vlc_meta_Duplicate( p_cdda->p_meta );
1023 return VLC_EGENERIC;
1025 case ACCESS_CAN_SEEK:
1026 case ACCESS_CAN_FASTSEEK:
1027 case ACCESS_CAN_PAUSE:
1028 case ACCESS_CAN_CONTROL_PACE:
1029 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1030 *pb_bool = VLC_TRUE;
1034 case ACCESS_GET_MTU:
1035 pi_int = (int*)va_arg( args, int * );
1036 *pi_int = CDDA_DATA_ONCE;
1039 case ACCESS_GET_PTS_DELAY:
1042 pi_64 = (int64_t*)va_arg( args, int64_t * );
1043 *pi_64 = 1000 * var_GetInteger( p_access,
1044 MODULE_STRING "-caching" );
1049 case ACCESS_SET_PAUSE_STATE:
1052 case ACCESS_GET_TITLE_INFO:
1053 { input_title_t ***ppp_title;
1054 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1055 pi_int = (int*)va_arg( args, int* );
1056 *((int*)va_arg( args, int* )) = 1; /* Title offset */
1058 /* Duplicate track info */
1059 *pi_int = p_cdda->i_tracks;
1060 *ppp_title = malloc(sizeof( input_title_t **) * p_cdda->i_tracks );
1061 for( i = 0; i < p_cdda->i_tracks; i++ )
1064 vlc_input_title_Duplicate( p_cdda->p_title[i] );
1069 case ACCESS_SET_TITLE:
1070 i = (int)va_arg( args, int );
1071 if( i != p_access->info.i_title )
1074 p_access->info.i_update |=
1075 INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE;
1076 p_access->info.i_title = i;
1077 p_access->info.i_size = p_cdda->p_title[i]->i_size;
1078 p_access->info.i_pos = 0;
1080 /* Next sector to read */
1081 p_cdda->i_sector = p_cdda->p_sectors[i];
1085 case ACCESS_SET_SEEKPOINT:
1086 case ACCESS_SET_PRIVATE_ID_STATE:
1087 return VLC_EGENERIC;
1089 msg_Warn( p_access, "unimplemented query in control" );
1090 return VLC_EGENERIC;