1 /*****************************************************************************
2 * cddax.c : CD digital audio input module for vlc using libcdio
3 *****************************************************************************
4 * Copyright (C) 2000,2003 VideoLAN
5 * $Id: access.c,v 1.24 2004/02/11 18:08:05 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 /****************************************************************************
77 ****************************************************************************/
79 /* process messages that originate from libcdio. */
81 cdio_log_handler (cdio_log_level_t level, const char message[])
83 cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
87 if (p_cdda->i_debug & INPUT_DBG_CDIO)
88 msg_Dbg( p_cdda_input, message);
91 msg_Warn( p_cdda_input, message);
95 msg_Err( p_cdda_input, message);
98 msg_Warn( p_cdda_input, message,
99 _("The above message had unknown vcdimager log level"),
107 /*! This routine is called by libcddb routines on error.
108 Setup is done by init_input_plugin.
111 cddb_log_handler (cddb_log_level_t level, const char message[])
113 cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
117 if (!(p_cdda->i_debug & INPUT_DBG_CDDB)) return;
118 /* Fall through if to warn case */
120 cdio_log_handler (level, message);
123 #endif /*HAVE_LIBCDDB*/
126 /*! This routine is when xine is not fully set up (before full initialization)
127 or is not around (before finalization).
130 uninit_log_handler (cdio_log_level_t level, const char message[])
132 cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
136 if (!(p_cdda->i_debug & (INPUT_DBG_CDIO|INPUT_DBG_CDDB)))
138 /* Fall through if to warn case */
140 fprintf(stderr, "WARN: %s\n", message);
143 fprintf(stderr, "ERROR: %s\n", message);
145 case CDIO_LOG_ASSERT:
146 fprintf(stderr, "ASSERT ERROR: %s\n", message);
149 fprintf(stderr, "UNKNOWN ERROR: %s\n%s %d\n",
151 _("The above message had unknown cdio log level"),
155 /* gl_default_cdio_log_handler (level, message); */
158 /*****************************************************************************
159 * CDDAPlay: Arrange things so we play the specified track.
160 * VLC_TRUE is returned if there was no error.
161 *****************************************************************************/
163 CDDAPlay( input_thread_t *p_input, int i_track )
165 cdda_data_t *p_cdda = (cdda_data_t *) p_input->p_access_data;
167 if( i_track >= p_cdda->i_nb_tracks || i_track < 1 )
170 CDDASetArea( p_input, p_input->stream.pp_areas[i_track] );
174 /*****************************************************************************
175 * CDDARead: reads from the CDDA into PES packets.
176 *****************************************************************************
177 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
179 *****************************************************************************/
180 static int CDDARead( input_thread_t * p_input, byte_t * p_buffer,
183 cdda_data_t * p_cdda;
188 p_cdda = (cdda_data_t *)p_input->p_access_data;
190 /* Compute the number of blocks we have to read */
191 i_blocks = i_len / CDIO_CD_FRAMESIZE_RAW;
194 if( !p_cdda->i_header_pos )
196 p_cdda->i_header_pos = sizeof(WAVEHEADER);
197 i_blocks = (i_len - sizeof(WAVEHEADER)) / CDIO_CD_FRAMESIZE_RAW;
198 memcpy( p_buffer, &p_cdda->waveheader, sizeof(WAVEHEADER) );
199 p_buffer += sizeof(WAVEHEADER);
200 i_read += sizeof(WAVEHEADER);
203 for( i_index = 0; i_index < i_blocks; i_index++ )
206 if( cdio_read_audio_sector( p_cdda->p_cddev->cdio, p_buffer,
207 p_cdda->i_sector) != 0 )
209 msg_Err( p_input, "could not read sector %d", p_cdda->i_sector );
214 if( p_cdda->i_sector == p_cdda->p_sectors[p_cdda->i_track + 1] )
216 input_area_t *p_area;
218 dbg_print( (INPUT_DBG_LSN|INPUT_DBG_CALL),
219 "end of track, cur: %u", p_cdda->i_sector );
221 /*???? if ( p_cdda->i_track >= p_cdda->i_nb_tracks - 1 )*/
224 vlc_mutex_lock( &p_input->stream.stream_lock );
225 p_area = p_input->stream.pp_areas[
226 p_input->stream.p_selected_area->i_id + 1 ];
229 CDDASetArea( p_input, p_area );
230 vlc_mutex_unlock( &p_input->stream.stream_lock );
232 i_read += CDIO_CD_FRAMESIZE_RAW;
233 p_buffer += CDIO_CD_FRAMESIZE_RAW;
236 if ( i_len % CDIO_CD_FRAMESIZE_RAW ) /* this should not happen */
238 msg_Err( p_input, "must read full sectors" );
244 /*****************************************************************************
245 * CDDASetProgram: Does nothing since a CDDA is mono_program
246 *****************************************************************************/
247 static int CDDASetProgram( input_thread_t * p_input,
248 pgrm_descriptor_t * p_program)
250 cdda_data_t * p_cdda= (cdda_data_t *) p_input->p_access_data;
251 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
255 /*****************************************************************************
256 * CDDASetArea: initialize input data for title x.
257 * It should be called for each user navigation request.
258 ****************************************************************************/
259 static int CDDASetArea( input_thread_t * p_input, input_area_t * p_area )
261 cdda_data_t *p_cdda = (cdda_data_t*) p_input->p_access_data;
264 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "");
266 /* we can't use the interface slider until initilization is complete */
267 p_input->stream.b_seekable = 0;
269 if( p_area != p_input->stream.p_selected_area )
271 /* Change the default area */
272 p_input->stream.p_selected_area = p_area;
274 /* Change the current track */
275 p_cdda->i_track = p_area->i_id - 1;
276 p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track];
278 /* Update the navigation variables without triggering a callback */
279 val.i_int = p_area->i_id;
280 var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
283 p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track];
285 p_input->stream.p_selected_area->i_tell =
286 (off_t)p_cdda->i_sector * (off_t)CDIO_CD_FRAMESIZE_RAW
287 - p_input->stream.p_selected_area->i_start;
289 /* warn interface that something has changed */
290 p_input->stream.b_seekable = 1;
291 p_input->stream.b_changed = 1;
296 /****************************************************************************
298 ****************************************************************************/
299 static void CDDASeek( input_thread_t * p_input, off_t i_off )
301 cdda_data_t * p_cdda;
303 p_cdda = (cdda_data_t *) p_input->p_access_data;
305 p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track]
306 + i_off / (off_t)CDIO_CD_FRAMESIZE_RAW;
308 vlc_mutex_lock( &p_input->stream.stream_lock );
309 p_input->stream.p_selected_area->i_tell =
310 (off_t)p_cdda->i_sector * (off_t)CDIO_CD_FRAMESIZE_RAW
311 - p_input->stream.p_selected_area->i_start;
313 vlc_mutex_unlock( &p_input->stream.stream_lock );
315 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_SEEK),
316 "sector %ud, offset: %lld, i_tell: %lld", p_cdda->i_sector, i_off,
317 p_input->stream.p_selected_area->i_tell );
321 #define meta_info_add_str(title, str) \
323 dbg_print( INPUT_DBG_META, "field %s: %s\n", title, str); \
324 input_AddInfo( p_cat, _(title), "%s", str ); \
325 vlc_mutex_lock( &p_playlist->object_lock ); \
326 p_item = playlist_ItemGetByPos( p_playlist, -1 ); \
327 vlc_mutex_unlock( &p_playlist->object_lock ); \
328 vlc_mutex_lock( &p_item->lock ); \
329 playlist_ItemAddInfo( p_item, p_cat->psz_name, \
330 _(title), "%s" , str ); \
331 vlc_mutex_unlock( &p_item->lock ); \
335 static void InformationCreate( input_thread_t *p_input )
337 cdda_data_t *p_cdda = (cdda_data_t *) p_input->p_access_data;
338 playlist_t *p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
340 input_info_category_t *p_cat;
341 playlist_item_t *p_item;
343 p_cat = input_InfoCategory( p_input, "General" );
347 if (p_cdda->i_cddb_enabled) {
349 meta_info_add_str( "Title", p_cdda->cddb.disc->title );
350 meta_info_add_str( "Artist", p_cdda->cddb.disc->artist );
351 meta_info_add_str( "Genre", p_cdda->cddb.disc->genre );
352 meta_info_add_str( "Extended Data", p_cdda->cddb.disc->ext_data );
355 if (p_cdda->cddb.disc->year != 0) {
356 snprintf(year, 5, "%d", p_cdda->cddb.disc->year);
357 meta_info_add_str( "Year", year );
359 if ( p_cdda->cddb.disc->discid ) {
360 input_AddInfo( p_cat, _("CDDB Disc ID"), "%x",
361 p_cdda->cddb.disc->discid );
364 if ( p_cdda->cddb.disc->category != CDDB_CAT_INVALID ) {
365 input_AddInfo( p_cat, _("CDDB Disc Category"), "%s",
366 CDDB_CATEGORY[p_cdda->cddb.disc->category] );
372 #endif /*HAVE_LIBCDDB*/
376 track_t i_track = p_cdda->i_nb_tracks;
377 char psz_buffer[MSTRTIME_MAX_SIZE];
379 (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[0])
380 / CDIO_CD_FRAMES_PER_SEC;
382 dbg_print( INPUT_DBG_META, "Duration %ld", (long int) i_duration );
383 input_AddInfo( p_cat, _("Duration"), "%s",
384 secstotimestr( psz_buffer, i_duration ) );
386 for( i_track = 0 ; i_track < p_cdda->i_nb_tracks ; i_track++ ) {
387 char track_str[TITLE_MAX];
389 (p_cdda->p_sectors[i_track+1] - p_cdda->p_sectors[i_track])
390 / CDIO_CD_FRAMES_PER_SEC;
391 snprintf(track_str, TITLE_MAX, "%s %02d", _("Track"), i_track+1);
392 p_cat = input_InfoCategory( p_input, track_str );
393 input_AddInfo( p_cat, _("Duration"), "%s",
394 secstotimestr( psz_buffer, i_duration ) );
397 if (p_cdda->i_cddb_enabled) {
398 cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
400 if (t != NULL && t->artist != NULL) {
401 input_AddInfo( p_cat, _("Artist"), "%s", t->artist );
402 input_AddInfo( p_cat, _("Title"), "%s", t->title );
409 if( p_playlist ) vlc_object_release( p_playlist );
415 #define free_and_dup(var, val) \
416 if (var) free(var); \
417 if (val) var=strdup(val);
421 GetCDDBInfo( const input_thread_t *p_input, cdda_data_t *p_cdda )
424 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
426 if (config_GetInt( p_input, MODULE_STRING "-cddb-enabled" )) {
428 cddb_conn_t *conn = cddb_new();
429 const CdIo *cdio = p_cdda->p_cddev->cdio;
432 cddb_log_set_handler (uninit_log_handler);
435 msg_Warn( p_input, "unable to initialize libcddb" );
439 cddb_set_email_address( conn,
440 config_GetPsz( p_input,
441 MODULE_STRING "-cddb-email") );
443 cddb_set_server_name( conn,
444 config_GetPsz( p_input,
445 MODULE_STRING "-cddb-server") );
447 cddb_set_server_port(conn,
448 config_GetInt( p_input,
449 MODULE_STRING "-cddb-port") );
451 /* Set the location of the local CDDB cache directory.
452 The default location of this directory is */
454 if (!config_GetInt( p_input, MODULE_STRING "-cddb-enable-cache" ))
455 cddb_cache_disable(conn);
457 cddb_cache_set_dir(conn,
458 config_GetPsz( p_input,
459 MODULE_STRING "-cddb-cachedir") );
461 cddb_set_timeout(conn,
462 config_GetInt( p_input, MODULE_STRING "-cddb-timeout") );
465 if (config_GetInt( p_input, MODULE_STRING "-cddb-httpd" )) {
466 cddb_http_enable(conn);
468 cddb_http_disable(conn);
470 p_cdda->cddb.disc = cddb_disc_new();
471 if (!p_cdda->cddb.disc) {
472 msg_Err( p_input, "Unable to create CDDB disc structure." );
476 for(i = 1; i <= p_cdda->i_nb_tracks; i++) {
477 cddb_track_t *t = cddb_track_new();
478 t->frame_offset = cdio_get_track_lba(cdio, i);
479 cddb_disc_add_track(p_cdda->cddb.disc, t);
482 p_cdda->cddb.disc->length =
483 cdio_get_track_lba(cdio, CDIO_CDROM_LEADOUT_TRACK)
484 / CDIO_CD_FRAMES_PER_SEC;
487 if (!cddb_disc_calc_discid(p_cdda->cddb.disc)) {
488 msg_Err( p_input, "CDDB disc calc failed" );
492 i_matches = cddb_query(conn, p_cdda->cddb.disc);
495 msg_Warn( p_input, "Found %d matches in CDDB. Using first one.",
497 cddb_read(conn, p_cdda->cddb.disc);
499 if (p_cdda->i_debug & INPUT_DBG_CDDB)
500 cddb_disc_print(p_cdda->cddb.disc);
503 msg_Warn( p_input, "CDDB error: %s", cddb_error_str(errno));
511 #endif /*HAVE_LIBCDDB*/
513 #define add_format_str_info(val) \
515 const char *str = val; \
520 strncat(tp, str, TEMP_STR_LEN-(tp-temp_str)); \
523 saw_control_prefix = false; \
527 #define add_format_num_info(val, fmt) \
531 sprintf(num_str, fmt, val); \
532 len=strlen(num_str); \
534 strncat(tp, num_str, TEMP_STR_LEN-(tp-temp_str)); \
537 saw_control_prefix = false; \
541 Take a format string and expand escape sequences, that is sequences that
542 begin with %, with information from the current CD.
543 The expanded string is returned. Here is a list of escape sequences:
545 %a : The album artist **
546 %A : The album information **
551 %m : The CD-DA Media Catalog Number (MCN)
552 %n : The number of tracks on the CD
553 %p : The artist/performer/composer in the track **
554 %T : The track number **
555 %s : Number of seconds in this track
557 %Y : The year 19xx or 20xx **
561 CDDAFormatStr(const input_thread_t *p_input, cdda_data_t *p_cdda,
562 const char format_str[], const char *mrl, int i_track)
564 #define TEMP_STR_SIZE 256
565 #define TEMP_STR_LEN (TEMP_STR_SIZE-1)
566 static char temp_str[TEMP_STR_SIZE];
568 char * tp = temp_str;
569 bool saw_control_prefix = false;
570 size_t format_len = strlen(format_str);
572 bzero(temp_str, TEMP_STR_SIZE);
574 for (i=0; i<format_len; i++) {
576 if (!saw_control_prefix && format_str[i] != '%') {
577 *tp++ = format_str[i];
578 saw_control_prefix = false;
582 switch(format_str[i]) {
584 if (saw_control_prefix) {
587 saw_control_prefix = !saw_control_prefix;
591 if (!p_cdda->i_cddb_enabled) goto not_special;
592 add_format_str_info(p_cdda->cddb.disc->artist);
595 if (!p_cdda->i_cddb_enabled) goto not_special;
596 add_format_str_info(p_cdda->cddb.disc->title);
599 if (!p_cdda->i_cddb_enabled) goto not_special;
600 add_format_str_info(CDDB_CATEGORY[p_cdda->cddb.disc->category]);
603 if (!p_cdda->i_cddb_enabled) goto not_special;
604 add_format_str_info(p_cdda->cddb.disc->genre);
607 if (!p_cdda->i_cddb_enabled) goto not_special;
608 add_format_num_info(p_cdda->cddb.disc->discid, "%x");
611 if (!p_cdda->i_cddb_enabled) goto not_special;
612 add_format_num_info(p_cdda->cddb.disc->year, "%5d");
615 if (p_cdda->i_cddb_enabled) {
616 cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
618 if (t != NULL && t->title != NULL)
619 add_format_str_info(t->title);
620 } else goto not_special;
623 if (p_cdda->i_cddb_enabled) {
624 cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
626 if (t != NULL && t->artist != NULL)
627 add_format_str_info(t->artist);
628 } else goto not_special;
633 add_format_str_info(mrl);
638 add_format_str_info(p_cdda->mcn);
643 add_format_num_info(p_cdda->i_nb_tracks, "%d");
648 if (p_cdda->i_cddb_enabled) {
649 char psz_buffer[MSTRTIME_MAX_SIZE];
651 (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[i_track-1])
652 / CDIO_CD_FRAMES_PER_SEC;
653 add_format_str_info(secstotimestr( psz_buffer, i_duration ) );
654 } else goto not_special;
659 add_format_num_info(i_track, "%02d");
666 *tp++ = format_str[i];
667 saw_control_prefix = false;
670 return strdup(temp_str);
674 CDDACreatePlayListItem(const input_thread_t *p_input, cdda_data_t *p_cdda,
675 playlist_t *p_playlist, unsigned int i_track,
676 char *psz_mrl, int psz_mrl_max,
677 const char *psz_source, int playlist_operation,
681 (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[i_track-1])
682 * (1000000 / CDIO_CD_FRAMES_PER_SEC) ;
685 char *config_varname = MODULE_STRING "-title-format";
686 playlist_item_t *p_item;
689 if (p_cdda->i_cddb_enabled) {
690 config_varname = MODULE_STRING "-cddb-title-format";
692 #endif /*HAVE_LIBCDDB*/
694 snprintf(psz_mrl, psz_mrl_max, "%s%s@T%u",
695 CDDA_MRL_PREFIX, psz_source, i_track);
697 p_title = CDDAFormatStr(p_input, p_cdda,
698 config_GetPsz( p_input, config_varname ),
701 dbg_print( INPUT_DBG_META, "mrl: %s, title: %s, duration, %ld, pos %d",
702 psz_mrl, p_title, (long int) i_duration / 1000000 , i_pos );
703 playlist_AddExt( p_playlist, psz_mrl, p_title, playlist_operation,
704 i_pos, i_duration , NULL, 0);
706 if( i_pos == PLAYLIST_END ) i_pos = p_playlist->i_size - 1;
708 vlc_mutex_lock( &p_playlist->object_lock );
709 p_item = playlist_ItemGetByPos( p_playlist, i_pos );
710 vlc_mutex_unlock( &p_playlist->object_lock );
714 vlc_mutex_lock( &p_item->lock );
717 CDDAFormatStr( p_input, p_cdda,
718 config_GetPsz( p_input, MODULE_STRING "-author-format" ),
721 playlist_ItemAddInfo( p_item , _("General"),_("Author"), p_author);
724 if (p_cdda->i_cddb_enabled) {
725 const char *psz_general_cat = _("General");
727 playlist_ItemAddInfo( p_item, psz_general_cat, _("Album"),
728 "%s", p_cdda->cddb.disc->title);
729 playlist_ItemAddInfo( p_item, psz_general_cat, _("Disc Artist(s)"),
730 "%s", p_cdda->cddb.disc->artist);
731 playlist_ItemAddInfo( p_item, psz_general_cat,
732 _("CDDB Disc Category"),
733 "%s", CDDB_CATEGORY[p_cdda->cddb.disc->category]);
734 playlist_ItemAddInfo( p_item, psz_general_cat, _("Genre"),
735 "%s", p_cdda->cddb.disc->genre);
736 if ( p_cdda->cddb.disc->discid ) {
737 playlist_ItemAddInfo( p_item, psz_general_cat, _("CDDB Disc ID"),
738 "%x", p_cdda->cddb.disc->discid );
740 if (p_cdda->cddb.disc->year != 0) {
741 playlist_ItemAddInfo( p_item, psz_general_cat,
742 _("Year"), "%5d", p_cdda->cddb.disc->year );
745 if (p_cdda->i_cddb_enabled) {
746 cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
748 if (t != NULL && t->artist != NULL) {
749 playlist_ItemAddInfo( p_item, psz_general_cat,
750 _("Track Artist"), "%s", t->artist );
751 playlist_ItemAddInfo( p_item , psz_general_cat,
752 _("Track Title"), "%s", t->title );
757 #endif /*HAVE_LIBCDDB*/
759 vlc_mutex_unlock( &p_item->lock );
763 CDDAFixupPlayList( input_thread_t *p_input, cdda_data_t *p_cdda,
764 const char *psz_source, bool play_single_track)
767 playlist_t * p_playlist;
769 unsigned int psz_mrl_max = strlen(CDDA_MRL_PREFIX) + strlen(psz_source) +
770 strlen("@T") + strlen("100") + 1;
773 p_cdda->i_cddb_enabled =
774 config_GetInt( p_input, MODULE_STRING "-cddb-enabled" );
775 if( play_single_track && !p_cdda->i_cddb_enabled ) return 0;
777 if( play_single_track ) return 0;
781 psz_mrl = malloc( psz_mrl_max );
783 if( psz_mrl == NULL )
785 msg_Warn( p_input, "out of memory" );
789 p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
793 msg_Warn( p_input, "can't find playlist" );
799 if (p_cdda->i_cddb_enabled)
800 GetCDDBInfo(p_input, p_cdda);
802 p_cdda->cddb.disc = NULL;
805 InformationCreate(p_input);
807 if (play_single_track) {
808 /* May fill out more information when the playlist user interface becomes
811 CDDACreatePlayListItem(p_input, p_cdda, p_playlist, p_cdda->i_track+1,
812 psz_mrl, psz_mrl_max, psz_source, PLAYLIST_REPLACE,
813 p_playlist->i_index);
816 playlist_Delete( p_playlist, p_playlist->i_index);
818 for( i = 1 ; i <= p_cdda->i_nb_tracks ; i++ )
820 CDDACreatePlayListItem(p_input, p_cdda, p_playlist, i, psz_mrl,
821 psz_mrl_max, psz_source, PLAYLIST_APPEND,
826 playlist_Command( p_playlist, PLAYLIST_GOTO, 0 );
830 vlc_object_release( p_playlist );
835 /****************************************************************************
837 ****************************************************************************/
839 E_(DebugCB) ( vlc_object_t *p_this, const char *psz_name,
840 vlc_value_t oldval, vlc_value_t val, void *p_data )
844 if (NULL == p_cdda_input) return VLC_EGENERIC;
846 p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
848 if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
849 msg_Dbg( p_cdda_input, "Old debug (x%0x) %d, new debug (x%0x) %d",
850 p_cdda->i_debug, p_cdda->i_debug, val.i_int, val.i_int);
852 p_cdda->i_debug = val.i_int;
857 E_(CDDBEnabledCB) ( vlc_object_t *p_this, const char *psz_name,
858 vlc_value_t oldval, vlc_value_t val, void *p_data )
862 if (NULL == p_cdda_input) return VLC_EGENERIC;
864 p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
867 if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
868 msg_Dbg( p_cdda_input, "Old CDDB Enabled (x%0x) %d, new (x%0x) %d",
869 p_cdda->i_cddb_enabled, p_cdda->i_cddb_enabled,
870 val.i_int, val.i_int);
872 p_cdda->i_cddb_enabled = val.i_int;
878 #if PLAYLIST_INTERFACE_IS_FIXED
880 E_(TitleFormatCB) ( vlc_object_t *p_this, const char *psz_name,
881 vlc_value_t oldval, vlc_value_t val, void *p_data )
885 if (NULL == p_cdda_input) return VLC_EGENERIC;
887 p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
889 if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
890 msg_Dbg( p_cdda_input, "Old CDDB Enabled (%s), new (%s)",
891 oldval.psz_string, val.psz_string);
898 /*****************************************************************************
900 *****************************************************************************/
902 E_(Open)( vlc_object_t *p_this )
904 input_thread_t * p_input = (input_thread_t *)p_this;
908 cdda_data_t * p_cdda;
913 bool play_single_track = false;
915 /* Set where to log errors messages from libcdio. */
916 p_cdda_input = (input_thread_t *)p_this;
918 /* parse the options passed in command line : */
919 psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
926 while( *psz_parser && *psz_parser != '@' )
931 if( *psz_parser == '@' )
937 if ('T' == *psz_parser || 't' == *psz_parser )
940 i_track = (int)strtol( psz_parser, NULL, 10 );
941 i_track = i_track ? i_track : 1;
942 play_single_track = true;
946 /* No source specified, so figure it out. */
947 if( !p_input->psz_access ) {
951 psz_source = config_GetPsz( p_input, "cd-audio" );
953 if( !psz_source || 0==strlen(psz_source) ) {
954 /* Scan for a CD-ROM drive with a CD-DA in it. */
956 cdio_get_devices_with_cap(NULL, CDIO_FS_AUDIO, false);
957 if (NULL == cd_drives) return -1;
958 if (cd_drives[0] == NULL) {
959 cdio_free_device_list(cd_drives);
962 psz_source = strdup(cd_drives[0]);
963 cdio_free_device_list(cd_drives);
968 cdio_log_set_handler ( cdio_log_handler );
970 cddb_log_set_handler ( cddb_log_handler );
973 if( !(p_cddev = ioctl_Open( p_this, psz_source )) )
975 msg_Warn( p_input, "could not open %s", psz_source );
980 p_cdda = malloc( sizeof(cdda_data_t) );
983 msg_Err( p_input, "out of memory" );
988 p_cdda->p_cddev = p_cddev;
989 p_cdda->i_debug = config_GetInt( p_this, MODULE_STRING "-debug" );
990 p_input->p_access_data = (void *)p_cdda;
992 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "%s", psz_source );
994 p_input->i_mtu = CDDA_DATA_ONCE;
996 /* We read the Table Of Content information */
997 p_cdda->i_nb_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_input),
998 p_cdda->p_cddev->cdio, &p_cdda->p_sectors );
999 if( p_cdda->i_nb_tracks < 0 )
1000 msg_Err( p_input, "unable to count tracks" );
1001 else if( p_cdda->i_nb_tracks <= 0 )
1002 msg_Err( p_input, "no audio tracks found" );
1004 if( p_cdda->i_nb_tracks <= 1)
1006 ioctl_Close( p_cdda->p_cddev );
1012 if( i_track >= p_cdda->i_nb_tracks || i_track < 1 )
1015 /* Set stream and area data */
1016 vlc_mutex_lock( &p_input->stream.stream_lock );
1018 /* Initialize ES structures */
1019 input_InitStream( p_input, 0 );
1021 /* cdda input method */
1022 p_input->stream.i_method = INPUT_METHOD_CDDA;
1024 p_input->stream.b_pace_control = 1;
1025 p_input->stream.b_seekable = 1;
1026 p_input->stream.i_mux_rate = 44100 * 4 / 50;
1028 #define area p_input->stream.pp_areas
1029 for( i = 1 ; i <= p_cdda->i_nb_tracks ; i++ )
1031 input_AddArea( p_input, i, 1 );
1033 /* Absolute start offset and size */
1035 (off_t)p_cdda->p_sectors[i-1] * (off_t)CDIO_CD_FRAMESIZE_RAW;
1037 (off_t)(p_cdda->p_sectors[i] - p_cdda->p_sectors[i-1])
1038 * (off_t)CDIO_CD_FRAMESIZE_RAW;
1042 CDDAPlay( p_input, i_track);
1044 CDDAFixupPlayList(p_input, p_cdda, psz_source, play_single_track);
1046 vlc_mutex_unlock( &p_input->stream.stream_lock );
1048 p_input->pf_read = CDDARead;
1049 p_input->pf_seek = CDDASeek;
1050 p_input->pf_set_area = CDDASetArea;
1051 p_input->pf_set_program = CDDASetProgram;
1053 /* Update default_pts to a suitable value for cdda access */
1054 var_Create( p_input, MODULE_STRING "-caching",
1055 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
1056 var_Get( p_input, MODULE_STRING "-caching", &val );
1057 p_input->i_pts_delay = val.i_int * 1000;
1061 /* Build a WAV header for the output data */
1062 memset( &p_cdda->waveheader, 0, sizeof(WAVEHEADER) );
1063 SetWLE( &p_cdda->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
1064 SetWLE( &p_cdda->waveheader.BitsPerSample, 16);
1065 p_cdda->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
1066 p_cdda->waveheader.Length = 0; /* we just don't know */
1067 p_cdda->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
1068 p_cdda->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
1069 SetDWLE( &p_cdda->waveheader.SubChunkLength, 16);
1070 SetWLE( &p_cdda->waveheader.Modus, 2);
1071 SetDWLE( &p_cdda->waveheader.SampleFreq, 44100);
1072 SetWLE( &p_cdda->waveheader.BytesPerSample,
1073 2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
1074 SetDWLE( &p_cdda->waveheader.BytesPerSec,
1075 16 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
1076 p_cdda->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
1077 p_cdda->waveheader.DataLength = 0; /* we just don't know */
1078 p_cdda->i_header_pos = 0;
1083 /*****************************************************************************
1084 * CDDAClose: closes cdda
1085 *****************************************************************************/
1087 E_(Close)( vlc_object_t *p_this )
1089 input_thread_t * p_input = (input_thread_t *)p_this;
1090 cdda_data_t *p_cdda = (cdda_data_t *)p_input->p_access_data;
1092 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
1093 ioctl_Close( p_cdda->p_cddev );
1095 cdio_log_set_handler (uninit_log_handler);
1098 cddb_log_set_handler (uninit_log_handler);
1099 if (p_cdda->i_cddb_enabled)
1100 cddb_disc_destroy(p_cdda->cddb.disc);
1104 p_cdda_input = NULL;