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.21 2004/01/06 04:57:34 rocky 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 1
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;
192 /* Compute the number of blocks we have to read */
194 i_blocks = i_len / CDIO_CD_FRAMESIZE_RAW;
196 for ( i_index = 0; i_index < i_blocks; i_index++ )
199 if (cdio_read_audio_sector(p_cdda->p_cddev->cdio, p_buffer,
200 p_cdda->i_sector) != 0)
202 msg_Err( p_input, "could not read sector %d", p_cdda->i_sector );
207 if ( p_cdda->i_sector == p_cdda->p_sectors[p_cdda->i_track + 1] )
209 input_area_t *p_area;
211 dbg_print( (INPUT_DBG_LSN|INPUT_DBG_CALL),
212 "end of track, cur: %u", p_cdda->i_sector );
214 /*???? if ( p_cdda->i_track >= p_cdda->i_nb_tracks - 1 )*/
217 vlc_mutex_lock( &p_input->stream.stream_lock );
218 p_area = p_input->stream.pp_areas[
219 p_input->stream.p_selected_area->i_id + 1 ];
222 CDDASetArea( p_input, p_area );
223 vlc_mutex_unlock( &p_input->stream.stream_lock );
225 i_read += CDIO_CD_FRAMESIZE_RAW;
228 if ( i_len % CDIO_CD_FRAMESIZE_RAW ) /* this should not happen */
230 msg_Err( p_input, "must read full sectors" );
236 /*****************************************************************************
237 * CDDASetProgram: Does nothing since a CDDA is mono_program
238 *****************************************************************************/
239 static int CDDASetProgram( input_thread_t * p_input,
240 pgrm_descriptor_t * p_program)
242 cdda_data_t * p_cdda= (cdda_data_t *) p_input->p_access_data;
243 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
247 /*****************************************************************************
248 * CDDASetArea: initialize input data for title x.
249 * It should be called for each user navigation request.
250 ****************************************************************************/
251 static int CDDASetArea( input_thread_t * p_input, input_area_t * p_area )
253 cdda_data_t *p_cdda = (cdda_data_t*) p_input->p_access_data;
256 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "");
258 /* we can't use the interface slider until initilization is complete */
259 p_input->stream.b_seekable = 0;
261 if( p_area != p_input->stream.p_selected_area )
263 /* Change the default area */
264 p_input->stream.p_selected_area = p_area;
266 /* Change the current track */
267 p_cdda->i_track = p_area->i_id - 1;
268 p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track];
270 /* Update the navigation variables without triggering a callback */
271 val.i_int = p_area->i_id;
272 var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
275 p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track];
277 p_input->stream.p_selected_area->i_tell =
278 (off_t)p_cdda->i_sector * (off_t)CDIO_CD_FRAMESIZE_RAW
279 - p_input->stream.p_selected_area->i_start;
281 /* warn interface that something has changed */
282 p_input->stream.b_seekable = 1;
283 p_input->stream.b_changed = 1;
288 /****************************************************************************
290 ****************************************************************************/
291 static void CDDASeek( input_thread_t * p_input, off_t i_off )
293 cdda_data_t * p_cdda;
295 p_cdda = (cdda_data_t *) p_input->p_access_data;
297 p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track]
298 + i_off / (off_t)CDIO_CD_FRAMESIZE_RAW;
300 vlc_mutex_lock( &p_input->stream.stream_lock );
301 p_input->stream.p_selected_area->i_tell =
302 (off_t)p_cdda->i_sector * (off_t)CDIO_CD_FRAMESIZE_RAW
303 - p_input->stream.p_selected_area->i_start;
305 vlc_mutex_unlock( &p_input->stream.stream_lock );
307 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_SEEK),
308 "sector %ud, offset: %lld, i_tell: %lld", p_cdda->i_sector, i_off,
309 p_input->stream.p_selected_area->i_tell );
313 #define meta_info_add_str(title, str) \
315 dbg_print( INPUT_DBG_META, "field %s: %s\n", title, str); \
316 input_AddInfo( p_cat, _(title), "%s", str ); \
317 playlist_AddInfo( p_playlist, -1, p_cat->psz_name, \
318 _(title), "%s" , str ); \
322 static void InformationCreate( input_thread_t *p_input )
324 cdda_data_t *p_cdda = (cdda_data_t *) p_input->p_access_data;
325 playlist_t *p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
327 input_info_category_t *p_cat;
329 p_cat = input_InfoCategory( p_input, "General" );
333 if (p_cdda->i_cddb_enabled) {
335 meta_info_add_str( "Title", p_cdda->cddb.disc->title );
336 meta_info_add_str( "Artist", p_cdda->cddb.disc->artist );
337 meta_info_add_str( "Genre", p_cdda->cddb.disc->genre );
338 meta_info_add_str( "Extended Data", p_cdda->cddb.disc->ext_data );
341 if (p_cdda->cddb.disc->year != 0) {
342 snprintf(year, 5, "%d", p_cdda->cddb.disc->year);
343 meta_info_add_str( "Year", year );
345 if ( p_cdda->cddb.disc->discid ) {
346 input_AddInfo( p_cat, _("CDDB Disc ID"), "%x",
347 p_cdda->cddb.disc->discid );
350 if ( p_cdda->cddb.disc->category != CDDB_CAT_INVALID ) {
351 input_AddInfo( p_cat, _("CDDB Disc Category"), "%s",
352 CDDB_CATEGORY[p_cdda->cddb.disc->category] );
358 #endif /*HAVE_LIBCDDB*/
361 track_t i_track = p_cdda->i_nb_tracks;
362 char psz_buffer[MSTRTIME_MAX_SIZE];
364 (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[0])
365 / CDIO_CD_FRAMES_PER_SEC;
367 dbg_print( INPUT_DBG_META, "Duration %ld", (long int) i_duration );
368 input_AddInfo( p_cat, _("Duration"), "%s",
369 secstotimestr( psz_buffer, i_duration ) );
371 if( p_playlist ) vlc_object_release( p_playlist );
377 #define free_and_dup(var, val) \
378 if (var) free(var); \
379 if (val) var=strdup(val);
383 GetCDDBInfo( const input_thread_t *p_input, cdda_data_t *p_cdda )
386 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
388 if (config_GetInt( p_input, MODULE_STRING "-cddb-enabled" )) {
390 cddb_conn_t *conn = cddb_new();
391 const CdIo *cdio = p_cdda->p_cddev->cdio;
394 cddb_log_set_handler (uninit_log_handler);
397 msg_Warn( p_input, "unable to initialize libcddb" );
401 cddb_set_email_address( conn,
402 config_GetPsz( p_input,
403 MODULE_STRING "-cddb-email") );
405 cddb_set_server_name( conn,
406 config_GetPsz( p_input,
407 MODULE_STRING "-cddb-server") );
409 cddb_set_server_port(conn,
410 config_GetInt( p_input,
411 MODULE_STRING "-cddb-port") );
413 /* Set the location of the local CDDB cache directory.
414 The default location of this directory is */
416 if (!config_GetInt( p_input, MODULE_STRING "-cddb-enable-cache" ))
417 cddb_cache_disable(conn);
419 cddb_cache_set_dir(conn,
420 config_GetPsz( p_input,
421 MODULE_STRING "-cddb-cachedir") );
423 cddb_set_timeout(conn,
424 config_GetInt( p_input, MODULE_STRING "-cddb-timeout") );
427 if (config_GetInt( p_input, MODULE_STRING "-cddb-httpd" )) {
428 cddb_http_enable(conn);
430 cddb_http_disable(conn);
432 p_cdda->cddb.disc = cddb_disc_new();
433 if (!p_cdda->cddb.disc) {
434 msg_Err( p_input, "Unable to create CDDB disc structure." );
438 for(i = 1; i <= p_cdda->i_nb_tracks; i++) {
439 cddb_track_t *t = cddb_track_new();
440 t->frame_offset = cdio_get_track_lba(cdio, i);
441 cddb_disc_add_track(p_cdda->cddb.disc, t);
444 p_cdda->cddb.disc->length =
445 cdio_get_track_lba(cdio, CDIO_CDROM_LEADOUT_TRACK)
446 / CDIO_CD_FRAMES_PER_SEC;
449 if (!cddb_disc_calc_discid(p_cdda->cddb.disc)) {
450 msg_Err( p_input, "CDDB disc calc failed" );
454 i_matches = cddb_query(conn, p_cdda->cddb.disc);
457 msg_Warn( p_input, "Found %d matches in CDDB. Using first one.",
459 cddb_read(conn, p_cdda->cddb.disc);
461 if (p_cdda->i_debug & INPUT_DBG_CDDB)
462 cddb_disc_print(p_cdda->cddb.disc);
465 msg_Warn( p_input, "CDDB error: %s", cddb_error_str(errno));
473 #endif /*HAVE_LIBCDDB*/
475 #define add_format_str_info(val) \
477 const char *str = val; \
482 strncat(tp, str, TEMP_STR_LEN-(tp-temp_str)); \
485 saw_control_prefix = false; \
489 #define add_format_num_info(val, fmt) \
493 sprintf(num_str, fmt, val); \
494 len=strlen(num_str); \
496 strncat(tp, num_str, TEMP_STR_LEN-(tp-temp_str)); \
499 saw_control_prefix = false; \
503 Take a format string and expand escape sequences, that is sequences that
504 begin with %, with information from the current CD.
505 The expanded string is returned. Here is a list of escape sequences:
507 %a : The album artist **
508 %A : The album information **
513 %m : The CD-DA Media Catalog Number (MCN)
514 %n : The number of tracks on the CD
515 %p : The artist/performer/composer in the track **
516 %T : The track number **
517 %s : Number of seconds in this track
519 %Y : The year 19xx or 20xx **
523 CDDAFormatStr(const input_thread_t *p_input, cdda_data_t *p_cdda,
524 const char format_str[], const char *mrl, int i_track)
526 #define TEMP_STR_SIZE 256
527 #define TEMP_STR_LEN (TEMP_STR_SIZE-1)
528 static char temp_str[TEMP_STR_SIZE];
530 char * tp = temp_str;
531 bool saw_control_prefix = false;
532 size_t format_len = strlen(format_str);
534 bzero(temp_str, TEMP_STR_SIZE);
536 for (i=0; i<format_len; i++) {
538 if (!saw_control_prefix && format_str[i] != '%') {
539 *tp++ = format_str[i];
540 saw_control_prefix = false;
544 switch(format_str[i]) {
546 if (saw_control_prefix) {
549 saw_control_prefix = !saw_control_prefix;
553 if (!p_cdda->i_cddb_enabled) goto not_special;
554 add_format_str_info(p_cdda->cddb.disc->artist);
557 if (!p_cdda->i_cddb_enabled) goto not_special;
558 add_format_str_info(p_cdda->cddb.disc->title);
561 if (!p_cdda->i_cddb_enabled) goto not_special;
562 add_format_str_info(CDDB_CATEGORY[p_cdda->cddb.disc->category]);
565 if (!p_cdda->i_cddb_enabled) goto not_special;
566 add_format_str_info(p_cdda->cddb.disc->genre);
569 if (!p_cdda->i_cddb_enabled) goto not_special;
570 add_format_num_info(p_cdda->cddb.disc->discid, "%x");
573 if (!p_cdda->i_cddb_enabled) goto not_special;
574 add_format_num_info(p_cdda->cddb.disc->year, "%5d");
577 if (p_cdda->i_cddb_enabled) {
578 cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
580 if (t != NULL && t->title != NULL)
581 add_format_str_info(t->title);
582 } else goto not_special;
585 if (p_cdda->i_cddb_enabled) {
586 cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
588 if (t != NULL && t->artist != NULL)
589 add_format_str_info(t->artist);
590 } else goto not_special;
595 add_format_str_info(mrl);
600 add_format_str_info(p_cdda->mcn);
605 add_format_num_info(p_cdda->i_nb_tracks, "%d");
610 if (p_cdda->i_cddb_enabled) {
611 char psz_buffer[MSTRTIME_MAX_SIZE];
613 (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[i_track-1])
614 / CDIO_CD_FRAMES_PER_SEC;
615 add_format_str_info(secstotimestr( psz_buffer, i_duration ) );
616 } else goto not_special;
621 add_format_num_info(i_track, "%02d");
628 *tp++ = format_str[i];
629 saw_control_prefix = false;
632 return strdup(temp_str);
636 CDDACreatePlayListItem(const input_thread_t *p_input, cdda_data_t *p_cdda,
637 playlist_t *p_playlist, unsigned int i_track,
638 char *psz_mrl, int psz_mrl_max,
639 const char *psz_source, int playlist_operation,
643 ((p_cdda->p_sectors[i_track] - p_cdda->p_sectors[i_track-1])
644 / CDIO_CD_FRAMES_PER_SEC) * 1000000;
647 char *config_varname = MODULE_STRING "-title-format";
650 if (p_cdda->i_cddb_enabled) {
651 config_varname = MODULE_STRING "-cddb-title-format";
655 snprintf(psz_mrl, psz_mrl_max, "%s%s@T%u",
656 CDDA_MRL_PREFIX, psz_source, i_track);
658 p_title = CDDAFormatStr(p_input, p_cdda,
659 config_GetPsz( p_input, config_varname ),
662 dbg_print( INPUT_DBG_META, "mrl: %s, title: %s, duration, %ld, pos %d",
663 psz_mrl, p_title, (long int) i_duration / 1000000 , i_pos );
664 playlist_AddWDuration( p_playlist, psz_mrl, p_title, playlist_operation,
668 CDDAFormatStr( p_input, p_cdda,
669 config_GetPsz( p_input, MODULE_STRING "-author-format" ),
672 if( i_pos == PLAYLIST_END ) i_pos = p_playlist->i_size - 1;
673 playlist_AddInfo( p_playlist, i_pos, _("General"),_("Author"),p_author);
678 CDDAFixupPlayList( input_thread_t *p_input, cdda_data_t *p_cdda,
679 const char *psz_source, bool play_single_track)
682 playlist_t * p_playlist;
684 unsigned int psz_mrl_max = strlen(CDDA_MRL_PREFIX) + strlen(psz_source) +
685 strlen("@T") + strlen("100") + 1;
688 p_cdda->i_cddb_enabled =
689 config_GetInt( p_input, MODULE_STRING "-cddb-enabled" );
690 if( play_single_track && !p_cdda->i_cddb_enabled ) return 0;
692 if( play_single_track ) return 0;
696 psz_mrl = malloc( psz_mrl_max );
698 if( psz_mrl == NULL )
700 msg_Warn( p_input, "out of memory" );
704 p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
708 msg_Warn( p_input, "can't find playlist" );
714 if (p_cdda->i_cddb_enabled)
715 GetCDDBInfo(p_input, p_cdda);
717 p_cdda->cddb.disc = NULL;
720 InformationCreate(p_input);
722 if (play_single_track) {
723 /* May fill out more information when the playlist user interface becomes
726 CDDACreatePlayListItem(p_input, p_cdda, p_playlist, p_cdda->i_track+1,
727 psz_mrl, psz_mrl_max, psz_source, PLAYLIST_REPLACE,
728 p_playlist->i_index);
731 playlist_Delete( p_playlist, p_playlist->i_index);
733 for( i = 1 ; i <= p_cdda->i_nb_tracks ; i++ )
735 CDDACreatePlayListItem(p_input, p_cdda, p_playlist, i, psz_mrl,
736 psz_mrl_max, psz_source, PLAYLIST_APPEND,
741 playlist_Command( p_playlist, PLAYLIST_GOTO, 0 );
745 vlc_object_release( p_playlist );
750 /****************************************************************************
752 ****************************************************************************/
754 E_(DebugCB) ( 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_access_data;
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_access_data;
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;
793 #if PLAYLIST_INTERFACE_IS_FIXED
795 E_(TitleFormatCB) ( vlc_object_t *p_this, const char *psz_name,
796 vlc_value_t oldval, vlc_value_t val, void *p_data )
800 if (NULL == p_cdda_input) return VLC_EGENERIC;
802 p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
804 if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
805 msg_Dbg( p_cdda_input, "Old CDDB Enabled (%s), new (%s)",
806 oldval.psz_string, val.psz_string);
813 /*****************************************************************************
815 *****************************************************************************/
817 E_(Open)( vlc_object_t *p_this )
819 input_thread_t * p_input = (input_thread_t *)p_this;
823 cdda_data_t * p_cdda;
827 bool play_single_track = false;
829 /* Set where to log errors messages from libcdio. */
830 p_cdda_input = (input_thread_t *)p_this;
832 /* parse the options passed in command line : */
833 psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
840 while( *psz_parser && *psz_parser != '@' )
845 if( *psz_parser == '@' )
851 if ('T' == *psz_parser || 't' == *psz_parser )
854 i_track = (int)strtol( psz_parser, NULL, 10 );
855 i_track = i_track ? i_track : 1;
856 play_single_track = true;
860 /* No source specified, so figure it out. */
861 if( !p_input->psz_access ) {
865 psz_source = config_GetPsz( p_input, "cd-audio" );
867 if( !psz_source || 0==strlen(psz_source) ) {
868 /* Scan for a CD-ROM drive with a CD-DA in it. */
870 cdio_get_devices_with_cap(NULL, CDIO_FS_AUDIO, false);
871 if (NULL == cd_drives) return -1;
872 if (cd_drives[0] == NULL) {
873 cdio_free_device_list(cd_drives);
876 psz_source = strdup(cd_drives[0]);
877 cdio_free_device_list(cd_drives);
882 cdio_log_set_handler ( cdio_log_handler );
884 cddb_log_set_handler ( cddb_log_handler );
887 if( !(p_cddev = ioctl_Open( p_this, psz_source )) )
889 msg_Warn( p_input, "could not open %s", psz_source );
894 p_cdda = malloc( sizeof(cdda_data_t) );
897 msg_Err( p_input, "out of memory" );
902 p_cdda->p_cddev = p_cddev;
903 p_cdda->i_debug = config_GetInt( p_this, MODULE_STRING "-debug" );
904 p_input->p_access_data = (void *)p_cdda;
906 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "%s", psz_source );
908 p_input->i_mtu = CDDA_DATA_ONCE;
910 /* We read the Table Of Content information */
911 p_cdda->i_nb_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_input),
912 p_cdda->p_cddev->cdio, &p_cdda->p_sectors );
913 if( p_cdda->i_nb_tracks < 0 )
914 msg_Err( p_input, "unable to count tracks" );
915 else if( p_cdda->i_nb_tracks <= 0 )
916 msg_Err( p_input, "no audio tracks found" );
918 if( p_cdda->i_nb_tracks <= 1)
920 ioctl_Close( p_cdda->p_cddev );
926 if( i_track >= p_cdda->i_nb_tracks || i_track < 1 )
929 /* Set stream and area data */
930 vlc_mutex_lock( &p_input->stream.stream_lock );
932 /* Initialize ES structures */
933 input_InitStream( p_input, 0 );
935 /* cdda input method */
936 p_input->stream.i_method = INPUT_METHOD_CDDA;
938 p_input->stream.b_pace_control = 1;
939 p_input->stream.b_seekable = 1;
940 p_input->stream.i_mux_rate = 44100 * 4 / 50;
942 #define area p_input->stream.pp_areas
943 for( i = 1 ; i <= p_cdda->i_nb_tracks ; i++ )
945 input_AddArea( p_input, i, 1 );
947 /* Absolute start offset and size */
949 (off_t)p_cdda->p_sectors[i-1] * (off_t)CDIO_CD_FRAMESIZE_RAW;
951 (off_t)(p_cdda->p_sectors[i] - p_cdda->p_sectors[i-1])
952 * (off_t)CDIO_CD_FRAMESIZE_RAW;
956 CDDAPlay( p_input, i_track);
958 CDDAFixupPlayList(p_input, p_cdda, psz_source, play_single_track);
960 vlc_mutex_unlock( &p_input->stream.stream_lock );
962 if( !p_input->psz_demux || !*p_input->psz_demux )
964 p_input->psz_demux = "cdda";
967 p_input->pf_read = CDDARead;
968 p_input->pf_seek = CDDASeek;
969 p_input->pf_set_area = CDDASetArea;
970 p_input->pf_set_program = CDDASetProgram;
972 /* Update default_pts to a suitable value for cdda access */
973 p_input->i_pts_delay = config_GetInt( p_input,
974 MODULE_STRING "-caching" ) * 1000;
981 /*****************************************************************************
982 * CDDAClose: closes cdda
983 *****************************************************************************/
985 E_(Close)( vlc_object_t *p_this )
987 input_thread_t * p_input = (input_thread_t *)p_this;
988 cdda_data_t *p_cdda = (cdda_data_t *)p_input->p_access_data;
990 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
991 ioctl_Close( p_cdda->p_cddev );
993 cdio_log_set_handler (uninit_log_handler);
996 cddb_log_set_handler (uninit_log_handler);
997 if (p_cdda->i_cddb_enabled)
998 cddb_disc_destroy(p_cdda->cddb.disc);
1002 p_cdda_input = NULL;