1 /*****************************************************************************
2 * cddax.c : CD digital audio input module for vlc using libcdio
3 *****************************************************************************
4 * Copyright (C) 2000, 2003, 2004 VideoLAN
5 * $Id: access.c,v 1.27 2004/02/22 21:32:42 gbazin Exp $
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 *****************************************************************************/
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 input_thread_t *p_cdda_input = NULL;
63 /*****************************************************************************
65 *****************************************************************************/
66 static int CDDARead ( input_thread_t *, byte_t *, size_t );
67 static void CDDASeek ( input_thread_t *, off_t );
68 static int CDDASetArea ( input_thread_t *, input_area_t * );
69 static int CDDASetProgram ( input_thread_t *, pgrm_descriptor_t * );
71 static int CDDAFixupPlayList( input_thread_t *p_input,
72 cdda_data_t *p_cdda, const char *psz_source,
73 bool play_single_track);
75 static void CDDAUpdateVar( input_thread_t *p_input, int i_entry, int i_action,
76 const char *p_varname, char *p_label,
77 const char *p_debug_label );
80 /****************************************************************************
82 ****************************************************************************/
84 /* process messages that originate from libcdio. */
86 cdio_log_handler (cdio_log_level_t level, const char message[])
88 cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
92 if (p_cdda->i_debug & INPUT_DBG_CDIO)
93 msg_Dbg( p_cdda_input, message);
96 msg_Warn( p_cdda_input, message);
100 msg_Err( p_cdda_input, message);
103 msg_Warn( p_cdda_input, message,
104 _("The above message had unknown vcdimager log level"),
112 /*! This routine is called by libcddb routines on error.
113 Setup is done by init_input_plugin.
116 cddb_log_handler (cddb_log_level_t level, const char message[])
118 cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
122 if (!(p_cdda->i_debug & INPUT_DBG_CDDB)) return;
123 /* Fall through if to warn case */
125 cdio_log_handler (level, message);
128 #endif /*HAVE_LIBCDDB*/
131 /*! This routine is when xine is not fully set up (before full initialization)
132 or is not around (before finalization).
135 uninit_log_handler (cdio_log_level_t level, const char message[])
137 cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
141 if (!(p_cdda->i_debug & (INPUT_DBG_CDIO|INPUT_DBG_CDDB)))
143 /* Fall through if to warn case */
145 fprintf(stderr, "WARN: %s\n", message);
148 fprintf(stderr, "ERROR: %s\n", message);
150 case CDIO_LOG_ASSERT:
151 fprintf(stderr, "ASSERT ERROR: %s\n", message);
154 fprintf(stderr, "UNKNOWN ERROR: %s\n%s %d\n",
156 _("The above message had unknown cdio log level"),
160 /* gl_default_cdio_log_handler (level, message); */
163 /*****************************************************************************
164 * CDDAPlay: Arrange things so we play the specified track.
165 * VLC_TRUE is returned if there was no error.
166 *****************************************************************************/
168 CDDAPlay( input_thread_t *p_input, int i_track )
170 cdda_data_t *p_cdda = (cdda_data_t *) p_input->p_access_data;
172 if( i_track > p_cdda->i_nb_tracks || i_track < 1 )
175 CDDASetArea( p_input, p_input->stream.pp_areas[i_track] );
179 /*****************************************************************************
180 * CDDARead: reads from the CDDA into PES packets.
181 *****************************************************************************
182 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
184 *****************************************************************************/
185 static int CDDARead( input_thread_t * p_input, byte_t * p_buffer,
188 cdda_data_t * p_cdda;
193 p_cdda = (cdda_data_t *)p_input->p_access_data;
195 /* Compute the number of blocks we have to read */
196 i_blocks = i_len / CDIO_CD_FRAMESIZE_RAW;
199 if( !p_cdda->i_header_pos )
201 p_cdda->i_header_pos = sizeof(WAVEHEADER);
202 i_blocks = (i_len - sizeof(WAVEHEADER)) / CDIO_CD_FRAMESIZE_RAW;
203 memcpy( p_buffer, &p_cdda->waveheader, sizeof(WAVEHEADER) );
204 p_buffer += sizeof(WAVEHEADER);
205 i_read += sizeof(WAVEHEADER);
208 for( i_index = 0; i_index < i_blocks; i_index++ )
211 if( cdio_read_audio_sector( p_cdda->p_cddev->cdio, p_buffer,
212 p_cdda->i_sector) != 0 )
214 msg_Err( p_input, "could not read sector %d", p_cdda->i_sector );
219 if( p_cdda->i_sector == p_cdda->p_sectors[p_cdda->i_track + 1] )
221 input_area_t *p_area;
223 dbg_print( (INPUT_DBG_LSN|INPUT_DBG_CALL),
224 "end of track, cur: %u", p_cdda->i_sector );
226 /*???? if ( p_cdda->i_track >= p_cdda->i_nb_tracks - 1 )*/
229 vlc_mutex_lock( &p_input->stream.stream_lock );
230 p_area = p_input->stream.pp_areas[
231 p_input->stream.p_selected_area->i_id + 1 ];
234 CDDASetArea( p_input, p_area );
235 vlc_mutex_unlock( &p_input->stream.stream_lock );
237 i_read += CDIO_CD_FRAMESIZE_RAW;
238 p_buffer += CDIO_CD_FRAMESIZE_RAW;
241 if ( i_len % CDIO_CD_FRAMESIZE_RAW ) /* this should not happen */
243 msg_Err( p_input, "must read full sectors" );
249 /*****************************************************************************
250 * CDDASetProgram: Does nothing since a CDDA is mono_program
251 *****************************************************************************/
252 static int CDDASetProgram( input_thread_t * p_input,
253 pgrm_descriptor_t * p_program)
255 cdda_data_t * p_cdda= (cdda_data_t *) p_input->p_access_data;
256 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
260 /*****************************************************************************
261 * CDDASetArea: initialize input data for title x.
262 * It should be called for each user navigation request.
263 ****************************************************************************/
264 static int CDDASetArea( input_thread_t * p_input, input_area_t * p_area )
266 cdda_data_t *p_cdda = (cdda_data_t*) p_input->p_access_data;
270 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "");
272 text.psz_string = _("Track");
273 var_Change( p_input, "title", VLC_VAR_SETTEXT, &text, NULL );
275 /* we can't use the interface slider until initilization is complete */
276 p_input->stream.b_seekable = 0;
278 if( p_area != p_input->stream.p_selected_area )
280 /* Change the default area */
281 p_input->stream.p_selected_area = p_area;
283 /* Change the current track */
284 p_cdda->i_track = p_area->i_id - 1;
285 p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track];
287 /* Update the navigation variables without triggering a callback */
288 val.i_int = p_area->i_id;
289 var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
292 p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track];
294 p_input->stream.p_selected_area->i_tell =
295 (off_t)p_cdda->i_sector * (off_t)CDIO_CD_FRAMESIZE_RAW
296 - p_input->stream.p_selected_area->i_start;
298 /* warn interface that something has changed */
299 p_input->stream.b_seekable = 1;
300 p_input->stream.b_changed = 1;
305 /****************************************************************************
307 ****************************************************************************/
308 static void CDDASeek( input_thread_t * p_input, off_t i_off )
310 cdda_data_t * p_cdda;
312 p_cdda = (cdda_data_t *) p_input->p_access_data;
314 p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track]
315 + i_off / (off_t)CDIO_CD_FRAMESIZE_RAW;
317 vlc_mutex_lock( &p_input->stream.stream_lock );
318 p_input->stream.p_selected_area->i_tell =
319 (off_t)p_cdda->i_sector * (off_t)CDIO_CD_FRAMESIZE_RAW
320 - p_input->stream.p_selected_area->i_start;
322 vlc_mutex_unlock( &p_input->stream.stream_lock );
324 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_SEEK),
325 "sector %ud, offset: %lld, i_tell: %lld", p_cdda->i_sector, i_off,
326 p_input->stream.p_selected_area->i_tell );
330 /****************************************************************************
331 Update the "varname" variable to i_num without triggering a callback.
332 ****************************************************************************/
334 CDDAUpdateVar( input_thread_t *p_input, int i_num, int i_action,
335 const char *p_varname, char *p_label,
336 const char *p_debug_label)
342 text.psz_string = p_label;
343 var_Change( p_input, p_varname, VLC_VAR_SETTEXT, &text, NULL );
345 var_Change( p_input, p_varname, i_action, &val, NULL );
349 #define meta_info_add_str(title, str) \
351 dbg_print( INPUT_DBG_META, "field %s: %s\n", title, str); \
352 input_AddInfo( p_cat, _(title), "%s", str ); \
353 vlc_mutex_lock( &p_playlist->object_lock ); \
354 p_item = playlist_ItemGetByPos( p_playlist, -1 ); \
355 vlc_mutex_unlock( &p_playlist->object_lock ); \
356 vlc_mutex_lock( &p_item->lock ); \
357 playlist_ItemAddInfo( p_item, p_cat->psz_name, \
358 _(title), "%s" , str ); \
359 vlc_mutex_unlock( &p_item->lock ); \
363 static void InformationCreate( input_thread_t *p_input )
365 cdda_data_t *p_cdda = (cdda_data_t *) p_input->p_access_data;
366 playlist_t *p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
368 input_info_category_t *p_cat;
369 playlist_item_t *p_item;
371 p_cat = input_InfoCategory( p_input, "General" );
375 if (p_cdda->i_cddb_enabled) {
377 meta_info_add_str( "Title", p_cdda->cddb.disc->title );
378 meta_info_add_str( "Artist", p_cdda->cddb.disc->artist );
379 meta_info_add_str( "Genre", p_cdda->cddb.disc->genre );
380 meta_info_add_str( "Extended Data", p_cdda->cddb.disc->ext_data );
383 if (p_cdda->cddb.disc->year != 0) {
384 snprintf(year, 5, "%d", p_cdda->cddb.disc->year);
385 meta_info_add_str( "Year", year );
387 if ( p_cdda->cddb.disc->discid ) {
388 input_AddInfo( p_cat, _("CDDB Disc ID"), "%x",
389 p_cdda->cddb.disc->discid );
392 if ( p_cdda->cddb.disc->category != CDDB_CAT_INVALID ) {
393 input_AddInfo( p_cat, _("CDDB Disc Category"), "%s",
394 CDDB_CATEGORY[p_cdda->cddb.disc->category] );
400 #endif /*HAVE_LIBCDDB*/
404 track_t i_track = p_cdda->i_nb_tracks;
405 char psz_buffer[MSTRTIME_MAX_SIZE];
407 (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[0])
408 / CDIO_CD_FRAMES_PER_SEC;
410 dbg_print( INPUT_DBG_META, "Duration %ld", (long int) i_duration );
411 input_AddInfo( p_cat, _("Duration"), "%s",
412 secstotimestr( psz_buffer, i_duration ) );
414 for( i_track = 0 ; i_track < p_cdda->i_nb_tracks ; i_track++ ) {
415 char track_str[TITLE_MAX];
417 (p_cdda->p_sectors[i_track+1] - p_cdda->p_sectors[i_track])
418 / CDIO_CD_FRAMES_PER_SEC;
419 snprintf(track_str, TITLE_MAX, "%s %02d", _("Track"), i_track+1);
420 p_cat = input_InfoCategory( p_input, track_str );
421 input_AddInfo( p_cat, _("Duration"), "%s",
422 secstotimestr( psz_buffer, i_duration ) );
425 if (p_cdda->i_cddb_enabled) {
426 cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
428 if (t != NULL && t->artist != NULL) {
429 input_AddInfo( p_cat, _("Artist"), "%s", t->artist );
430 input_AddInfo( p_cat, _("Title"), "%s", t->title );
437 if( p_playlist ) vlc_object_release( p_playlist );
443 #define free_and_dup(var, val) \
444 if (var) free(var); \
445 if (val) var=strdup(val);
449 GetCDDBInfo( const input_thread_t *p_input, cdda_data_t *p_cdda )
452 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
454 if (config_GetInt( p_input, MODULE_STRING "-cddb-enabled" )) {
456 cddb_conn_t *conn = cddb_new();
457 const CdIo *cdio = p_cdda->p_cddev->cdio;
460 cddb_log_set_handler (uninit_log_handler);
463 msg_Warn( p_input, "unable to initialize libcddb" );
467 cddb_set_email_address( conn,
468 config_GetPsz( p_input,
469 MODULE_STRING "-cddb-email") );
471 cddb_set_server_name( conn,
472 config_GetPsz( p_input,
473 MODULE_STRING "-cddb-server") );
475 cddb_set_server_port(conn,
476 config_GetInt( p_input,
477 MODULE_STRING "-cddb-port") );
479 /* Set the location of the local CDDB cache directory.
480 The default location of this directory is */
482 if (!config_GetInt( p_input, MODULE_STRING "-cddb-enable-cache" ))
483 cddb_cache_disable(conn);
485 cddb_cache_set_dir(conn,
486 config_GetPsz( p_input,
487 MODULE_STRING "-cddb-cachedir") );
489 cddb_set_timeout(conn,
490 config_GetInt( p_input, MODULE_STRING "-cddb-timeout") );
493 if (config_GetInt( p_input, MODULE_STRING "-cddb-httpd" )) {
494 cddb_http_enable(conn);
496 cddb_http_disable(conn);
498 p_cdda->cddb.disc = cddb_disc_new();
499 if (!p_cdda->cddb.disc) {
500 msg_Err( p_input, "Unable to create CDDB disc structure." );
504 for(i = 1; i <= p_cdda->i_nb_tracks; i++) {
505 cddb_track_t *t = cddb_track_new();
506 t->frame_offset = cdio_get_track_lba(cdio, i);
507 cddb_disc_add_track(p_cdda->cddb.disc, t);
510 p_cdda->cddb.disc->length =
511 cdio_get_track_lba(cdio, CDIO_CDROM_LEADOUT_TRACK)
512 / CDIO_CD_FRAMES_PER_SEC;
515 if (!cddb_disc_calc_discid(p_cdda->cddb.disc)) {
516 msg_Err( p_input, "CDDB disc calc failed" );
520 i_matches = cddb_query(conn, p_cdda->cddb.disc);
523 msg_Warn( p_input, "Found %d matches in CDDB. Using first one.",
525 cddb_read(conn, p_cdda->cddb.disc);
527 if (p_cdda->i_debug & INPUT_DBG_CDDB)
528 cddb_disc_print(p_cdda->cddb.disc);
531 msg_Warn( p_input, "CDDB error: %s", cddb_error_str(errno));
539 #endif /*HAVE_LIBCDDB*/
541 #define add_format_str_info(val) \
543 const char *str = val; \
548 strncat(tp, str, TEMP_STR_LEN-(tp-temp_str)); \
551 saw_control_prefix = false; \
555 #define add_format_num_info(val, fmt) \
559 sprintf(num_str, fmt, val); \
560 len=strlen(num_str); \
562 strncat(tp, num_str, TEMP_STR_LEN-(tp-temp_str)); \
565 saw_control_prefix = false; \
569 Take a format string and expand escape sequences, that is sequences that
570 begin with %, with information from the current CD.
571 The expanded string is returned. Here is a list of escape sequences:
573 %a : The album artist **
574 %A : The album information **
579 %m : The CD-DA Media Catalog Number (MCN)
580 %n : The number of tracks on the CD
581 %p : The artist/performer/composer in the track **
582 %T : The track number **
583 %s : Number of seconds in this track
585 %Y : The year 19xx or 20xx **
589 CDDAFormatStr(const input_thread_t *p_input, cdda_data_t *p_cdda,
590 const char format_str[], const char *mrl, int i_track)
592 #define TEMP_STR_SIZE 256
593 #define TEMP_STR_LEN (TEMP_STR_SIZE-1)
594 static char temp_str[TEMP_STR_SIZE];
596 char * tp = temp_str;
597 bool saw_control_prefix = false;
598 size_t format_len = strlen(format_str);
600 bzero(temp_str, TEMP_STR_SIZE);
602 for (i=0; i<format_len; i++) {
604 if (!saw_control_prefix && format_str[i] != '%') {
605 *tp++ = format_str[i];
606 saw_control_prefix = false;
610 switch(format_str[i]) {
612 if (saw_control_prefix) {
615 saw_control_prefix = !saw_control_prefix;
619 if (!p_cdda->i_cddb_enabled) goto not_special;
620 add_format_str_info(p_cdda->cddb.disc->artist);
623 if (!p_cdda->i_cddb_enabled) goto not_special;
624 add_format_str_info(p_cdda->cddb.disc->title);
627 if (!p_cdda->i_cddb_enabled) goto not_special;
628 add_format_str_info(CDDB_CATEGORY[p_cdda->cddb.disc->category]);
631 if (!p_cdda->i_cddb_enabled) goto not_special;
632 add_format_str_info(p_cdda->cddb.disc->genre);
635 if (!p_cdda->i_cddb_enabled) goto not_special;
636 add_format_num_info(p_cdda->cddb.disc->discid, "%x");
639 if (!p_cdda->i_cddb_enabled) goto not_special;
640 add_format_num_info(p_cdda->cddb.disc->year, "%5d");
643 if (p_cdda->i_cddb_enabled) {
644 cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
646 if (t != NULL && t->title != NULL)
647 add_format_str_info(t->title);
648 } else goto not_special;
651 if (p_cdda->i_cddb_enabled) {
652 cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
654 if (t != NULL && t->artist != NULL)
655 add_format_str_info(t->artist);
656 } else goto not_special;
661 add_format_str_info(mrl);
666 add_format_str_info(p_cdda->mcn);
671 add_format_num_info(p_cdda->i_nb_tracks, "%d");
676 if (p_cdda->i_cddb_enabled) {
677 char psz_buffer[MSTRTIME_MAX_SIZE];
679 (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[i_track-1])
680 / CDIO_CD_FRAMES_PER_SEC;
681 add_format_str_info(secstotimestr( psz_buffer, i_duration ) );
682 } else goto not_special;
687 add_format_num_info(i_track, "%02d");
694 *tp++ = format_str[i];
695 saw_control_prefix = false;
698 return strdup(temp_str);
702 CDDACreatePlayListItem(const input_thread_t *p_input, cdda_data_t *p_cdda,
703 playlist_t *p_playlist, unsigned int i_track,
704 char *psz_mrl, int psz_mrl_max,
705 const char *psz_source, int playlist_operation,
709 (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[i_track-1])
710 * (1000000 / CDIO_CD_FRAMES_PER_SEC) ;
713 char *config_varname = MODULE_STRING "-title-format";
714 playlist_item_t *p_item;
717 if (p_cdda->i_cddb_enabled) {
718 config_varname = MODULE_STRING "-cddb-title-format";
720 #endif /*HAVE_LIBCDDB*/
722 snprintf(psz_mrl, psz_mrl_max, "%s%s@T%u",
723 CDDA_MRL_PREFIX, psz_source, i_track);
725 p_title = CDDAFormatStr(p_input, p_cdda,
726 config_GetPsz( p_input, config_varname ),
729 dbg_print( INPUT_DBG_META, "mrl: %s, title: %s, duration, %ld, pos %d",
730 psz_mrl, p_title, (long int) i_duration / 1000000 , i_pos );
731 playlist_AddExt( p_playlist, psz_mrl, p_title, playlist_operation,
732 i_pos, i_duration , NULL, 0);
734 if( i_pos == PLAYLIST_END ) i_pos = p_playlist->i_size - 1;
736 vlc_mutex_lock( &p_playlist->object_lock );
737 p_item = playlist_ItemGetByPos( p_playlist, i_pos );
738 vlc_mutex_unlock( &p_playlist->object_lock );
742 vlc_mutex_lock( &p_item->lock );
745 CDDAFormatStr( p_input, p_cdda,
746 config_GetPsz( p_input, MODULE_STRING "-author-format" ),
749 playlist_ItemAddInfo( p_item , _("General"),_("Author"), p_author);
752 if (p_cdda->i_cddb_enabled) {
753 const char *psz_general_cat = _("General");
755 playlist_ItemAddInfo( p_item, psz_general_cat, _("Album"),
756 "%s", p_cdda->cddb.disc->title);
757 playlist_ItemAddInfo( p_item, psz_general_cat, _("Disc Artist(s)"),
758 "%s", p_cdda->cddb.disc->artist);
759 playlist_ItemAddInfo( p_item, psz_general_cat,
760 _("CDDB Disc Category"),
761 "%s", CDDB_CATEGORY[p_cdda->cddb.disc->category]);
762 playlist_ItemAddInfo( p_item, psz_general_cat, _("Genre"),
763 "%s", p_cdda->cddb.disc->genre);
764 if ( p_cdda->cddb.disc->discid ) {
765 playlist_ItemAddInfo( p_item, psz_general_cat, _("CDDB Disc ID"),
766 "%x", p_cdda->cddb.disc->discid );
768 if (p_cdda->cddb.disc->year != 0) {
769 playlist_ItemAddInfo( p_item, psz_general_cat,
770 _("Year"), "%5d", p_cdda->cddb.disc->year );
773 if (p_cdda->i_cddb_enabled) {
774 cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
776 if (t != NULL && t->artist != NULL) {
777 playlist_ItemAddInfo( p_item, psz_general_cat,
778 _("Track Artist"), "%s", t->artist );
779 playlist_ItemAddInfo( p_item , psz_general_cat,
780 _("Track Title"), "%s", t->title );
785 #endif /*HAVE_LIBCDDB*/
787 vlc_mutex_unlock( &p_item->lock );
791 CDDAFixupPlayList( input_thread_t *p_input, cdda_data_t *p_cdda,
792 const char *psz_source, bool play_single_track)
795 playlist_t * p_playlist;
797 unsigned int psz_mrl_max = strlen(CDDA_MRL_PREFIX) + strlen(psz_source) +
798 strlen("@T") + strlen("100") + 1;
801 p_cdda->i_cddb_enabled =
802 config_GetInt( p_input, MODULE_STRING "-cddb-enabled" );
803 if( play_single_track && !p_cdda->i_cddb_enabled ) return 0;
805 if( play_single_track ) return 0;
809 psz_mrl = malloc( psz_mrl_max );
811 if( psz_mrl == NULL )
813 msg_Warn( p_input, "out of memory" );
817 p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
821 msg_Warn( p_input, "can't find playlist" );
827 if (p_cdda->i_cddb_enabled)
828 GetCDDBInfo(p_input, p_cdda);
830 p_cdda->cddb.disc = NULL;
833 InformationCreate(p_input);
835 if (play_single_track) {
836 /* May fill out more information when the playlist user interface becomes
839 CDDACreatePlayListItem(p_input, p_cdda, p_playlist, p_cdda->i_track+1,
840 psz_mrl, psz_mrl_max, psz_source, PLAYLIST_REPLACE,
841 p_playlist->i_index);
844 playlist_Delete( p_playlist, p_playlist->i_index);
846 for( i = 1 ; i <= p_cdda->i_nb_tracks ; i++ )
848 CDDACreatePlayListItem(p_input, p_cdda, p_playlist, i, psz_mrl,
849 psz_mrl_max, psz_source, PLAYLIST_APPEND,
853 playlist_Command( p_playlist, PLAYLIST_GOTO, 0 );
857 vlc_object_release( p_playlist );
862 /****************************************************************************
864 ****************************************************************************/
866 E_(DebugCB) ( vlc_object_t *p_this, const char *psz_name,
867 vlc_value_t oldval, vlc_value_t val, void *p_data )
871 if (NULL == p_cdda_input) return VLC_EGENERIC;
873 p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
875 if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
876 msg_Dbg( p_cdda_input, "Old debug (x%0x) %d, new debug (x%0x) %d",
877 p_cdda->i_debug, p_cdda->i_debug, val.i_int, val.i_int);
879 p_cdda->i_debug = val.i_int;
884 E_(CDDBEnabledCB) ( vlc_object_t *p_this, const char *psz_name,
885 vlc_value_t oldval, vlc_value_t val, void *p_data )
889 if (NULL == p_cdda_input) return VLC_EGENERIC;
891 p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
894 if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
895 msg_Dbg( p_cdda_input, "Old CDDB Enabled (x%0x) %d, new (x%0x) %d",
896 p_cdda->i_cddb_enabled, p_cdda->i_cddb_enabled,
897 val.i_int, val.i_int);
899 p_cdda->i_cddb_enabled = val.i_int;
905 #if PLAYLIST_INTERFACE_IS_FIXED
907 E_(TitleFormatCB) ( vlc_object_t *p_this, const char *psz_name,
908 vlc_value_t oldval, vlc_value_t val, void *p_data )
912 if (NULL == p_cdda_input) return VLC_EGENERIC;
914 p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
916 if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
917 msg_Dbg( p_cdda_input, "Old CDDB Enabled (%s), new (%s)",
918 oldval.psz_string, val.psz_string);
925 /*****************************************************************************
927 *****************************************************************************/
929 E_(Open)( vlc_object_t *p_this )
931 input_thread_t * p_input = (input_thread_t *)p_this;
935 cdda_data_t * p_cdda;
940 bool play_single_track = false;
942 /* Set where to log errors messages from libcdio. */
943 p_cdda_input = (input_thread_t *)p_this;
945 /* parse the options passed in command line : */
946 psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
953 while( *psz_parser && *psz_parser != '@' )
958 if( *psz_parser == '@' )
964 if ('T' == *psz_parser || 't' == *psz_parser )
967 i_track = (int)strtol( psz_parser, NULL, 10 );
968 i_track = i_track ? i_track : 1;
969 play_single_track = true;
973 /* No source specified, so figure it out. */
974 if( !p_input->psz_access ) {
978 psz_source = config_GetPsz( p_input, "cd-audio" );
980 if( !psz_source || 0==strlen(psz_source) ) {
981 /* Scan for a CD-ROM drive with a CD-DA in it. */
983 cdio_get_devices_with_cap(NULL, CDIO_FS_AUDIO, false);
984 if (NULL == cd_drives) return -1;
985 if (cd_drives[0] == NULL) {
986 cdio_free_device_list(cd_drives);
989 psz_source = strdup(cd_drives[0]);
990 cdio_free_device_list(cd_drives);
995 cdio_log_set_handler ( cdio_log_handler );
997 cddb_log_set_handler ( cddb_log_handler );
1000 if( !(p_cddev = ioctl_Open( p_this, psz_source )) )
1002 msg_Warn( p_input, "could not open %s", psz_source );
1007 p_cdda = malloc( sizeof(cdda_data_t) );
1008 if( p_cdda == NULL )
1010 msg_Err( p_input, "out of memory" );
1015 p_cdda->p_cddev = p_cddev;
1016 p_cdda->i_debug = config_GetInt( p_this, MODULE_STRING "-debug" );
1017 p_input->p_access_data = (void *)p_cdda;
1019 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "%s", psz_source );
1021 p_input->i_mtu = CDDA_DATA_ONCE;
1023 /* We read the Table Of Content information */
1024 p_cdda->i_nb_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_input),
1025 p_cdda->p_cddev->cdio, &p_cdda->p_sectors );
1026 if( p_cdda->i_nb_tracks < 0 )
1027 msg_Err( p_input, "unable to count tracks" );
1028 else if( p_cdda->i_nb_tracks <= 0 )
1029 msg_Err( p_input, "no audio tracks found" );
1031 if( p_cdda->i_nb_tracks <= 0 )
1033 ioctl_Close( p_cdda->p_cddev );
1039 if( i_track > p_cdda->i_nb_tracks || i_track < 1 )
1042 /* Set stream and area data */
1043 vlc_mutex_lock( &p_input->stream.stream_lock );
1045 /* Initialize ES structures */
1046 input_InitStream( p_input, 0 );
1048 /* cdda input method */
1049 p_input->stream.i_method = INPUT_METHOD_CDDA;
1051 p_input->stream.b_pace_control = 1;
1052 p_input->stream.b_seekable = 1;
1053 p_input->stream.i_mux_rate = 44100 * 4 / 50;
1055 #define area p_input->stream.pp_areas
1056 for( i = 1 ; i <= p_cdda->i_nb_tracks ; i++ )
1058 input_AddArea( p_input, i, 1 );
1060 /* Absolute start offset and size */
1062 (off_t)p_cdda->p_sectors[i-1] * (off_t)CDIO_CD_FRAMESIZE_RAW;
1064 (off_t)(p_cdda->p_sectors[i] - p_cdda->p_sectors[i-1])
1065 * (off_t)CDIO_CD_FRAMESIZE_RAW;
1069 CDDAPlay( p_input, i_track);
1070 vlc_mutex_unlock( &p_input->stream.stream_lock );
1072 CDDAFixupPlayList(p_input, p_cdda, psz_source, play_single_track);
1074 p_input->pf_read = CDDARead;
1075 p_input->pf_seek = CDDASeek;
1076 p_input->pf_set_area = CDDASetArea;
1077 p_input->pf_set_program = CDDASetProgram;
1079 /* Update default_pts to a suitable value for cdda access */
1080 var_Create( p_input, MODULE_STRING "-caching",
1081 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
1082 var_Get( p_input, MODULE_STRING "-caching", &val );
1083 p_input->i_pts_delay = val.i_int * 1000;
1087 /* Build a WAV header for the output data */
1088 memset( &p_cdda->waveheader, 0, sizeof(WAVEHEADER) );
1089 SetWLE( &p_cdda->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
1090 SetWLE( &p_cdda->waveheader.BitsPerSample, 16);
1091 p_cdda->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
1092 p_cdda->waveheader.Length = 0; /* we just don't know */
1093 p_cdda->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
1094 p_cdda->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
1095 SetDWLE( &p_cdda->waveheader.SubChunkLength, 16);
1096 SetWLE( &p_cdda->waveheader.Modus, 2);
1097 SetDWLE( &p_cdda->waveheader.SampleFreq, 44100);
1098 SetWLE( &p_cdda->waveheader.BytesPerSample,
1099 2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
1100 SetDWLE( &p_cdda->waveheader.BytesPerSec,
1101 16 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
1102 p_cdda->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
1103 p_cdda->waveheader.DataLength = 0; /* we just don't know */
1104 p_cdda->i_header_pos = 0;
1109 /*****************************************************************************
1110 * CDDAClose: closes cdda
1111 *****************************************************************************/
1113 E_(Close)( vlc_object_t *p_this )
1115 input_thread_t * p_input = (input_thread_t *)p_this;
1116 cdda_data_t *p_cdda = (cdda_data_t *)p_input->p_access_data;
1118 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
1119 ioctl_Close( p_cdda->p_cddev );
1121 cdio_log_set_handler (uninit_log_handler);
1124 cddb_log_set_handler (uninit_log_handler);
1125 if (p_cdda->i_cddb_enabled)
1126 cddb_disc_destroy(p_cdda->cddb.disc);
1129 free( p_cdda->p_sectors );
1131 p_cdda_input = NULL;