]> git.sesse.net Git - vlc/blobdiff - modules/access/cdda/access.c
signed/unsigned mismatch fix.
[vlc] / modules / access / cdda / access.c
index 23c725bfcc0752816ee0a0ba1bba6f49abb9c655..84f81a6cb26fb1f79c1860a1e4a2f3e5819d244c 100644 (file)
@@ -2,7 +2,7 @@
  * cddax.c : CD digital audio input module for vlc using libcdio
  *****************************************************************************
  * Copyright (C) 2000,2003 VideoLAN
- * $Id: access.c,v 1.2 2003/11/26 03:40:31 rocky Exp $
+ * $Id: access.c,v 1.16 2003/12/14 23:40:36 rocky Exp $
  *
  * Authors: Rocky Bernstein <rocky@panix.com> 
  *          Laurent Aimar <fenrir@via.ecp.fr>
@@ -31,7 +31,7 @@
 
 #include <vlc/vlc.h>
 #include <vlc/intf.h>
-#include <vlc/input.h>
+
 #include <sys/types.h>
 #include <cdio/cdio.h>
 #include <cdio/cd_types.h>
@@ -51,6 +51,8 @@
 #define CDDA_BLOCKS_ONCE 1
 #define CDDA_DATA_ONCE   (CDDA_BLOCKS_ONCE * CDIO_CD_FRAMESIZE_RAW)
 
+#define CDDA_MRL_PREFIX "cddax://"
+
 /* FIXME: This variable is a hack. Would be nice to eliminate. */
 static input_thread_t *p_cdda_input = NULL;
 
@@ -62,28 +64,14 @@ static void CDDASeek         ( input_thread_t *, off_t );
 static int  CDDASetArea      ( input_thread_t *, input_area_t * );
 static int  CDDASetProgram   ( input_thread_t *, pgrm_descriptor_t * );
 
+static int  CDDAFixupPlayList( input_thread_t *p_input, 
+                             cdda_data_t *p_cdda, const char *psz_source, 
+                             bool play_single_track);
+
 /****************************************************************************
  * Private functions
  ****************************************************************************/
 
-int
-E_(DebugCallback)   ( vlc_object_t *p_this, const char *psz_name,
-                     vlc_value_t oldval, vlc_value_t val, void *p_data )
-{
-  cdda_data_t *p_cdda;
-
-  if (NULL == p_cdda_input) return VLC_EGENERIC;
-  
-  p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
-
-  if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
-    msg_Dbg( p_cdda_input, "Old debug (x%0x) %d, new debug (x%0x) %d", 
-             p_cdda->i_debug, p_cdda->i_debug, val.i_int, val.i_int);
-  }
-  p_cdda->i_debug = val.i_int;
-  return VLC_SUCCESS;
-}
-
 /* process messages that originate from libcdio. */
 static void
 cdio_log_handler (cdio_log_level_t level, const char message[])
@@ -111,166 +99,56 @@ cdio_log_handler (cdio_log_level_t level, const char message[])
 }
 
 
-/*****************************************************************************
- * Open: open cdda
- *****************************************************************************/
-int 
-E_(Open)( vlc_object_t *p_this )
+#ifdef HAVE_LIBCDDB
+/*! This routine is called by libcddb routines on error. 
+   Setup is done by init_input_plugin.
+*/
+static void 
+cddb_log_handler (cddb_log_level_t level, const char message[])
 {
-    input_thread_t *        p_input = (input_thread_t *)p_this;
-    char *                  psz_orig;
-    char *                  psz_parser;
-    char *                  psz_source;
-    cdda_data_t *           p_cdda;
-    int                     i;
-    int                     i_title = 1;
-    cddev_t                 *p_cddev;
-
-    /* Set where to log errors messages from libcdio. */
-    p_cdda_input = (input_thread_t *)p_this;
-
-    /* parse the options passed in command line : */
-    psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
-
-    if( !psz_orig )
-    {
-        return( -1 );
-    }
-
-    while( *psz_parser && *psz_parser != '@' )
-    {
-        psz_parser++;
-    }
-
-    if( *psz_parser == '@' )
-    {
-        /* Found options */
-        *psz_parser = '\0';
-        ++psz_parser;
-
-        if ('T' == *psz_parser || 't' == *psz_parser ) 
-            ++psz_parser;
-          
-        i_title = (int)strtol( psz_parser, NULL, 10 );
-        i_title = i_title ? i_title : 1;
-    }
-
-    if( !*psz_source ) {
-      /* No source specified, so figure it out. */
-      if( !p_input->psz_access ) {
-        free( psz_orig );
-        return -1;
-      }
-      psz_source = config_GetPsz( p_input, MODULE_STRING "-device" );
-      
-      if( !psz_source || 0==strlen(psz_source) ) {
-        /* Scan for a CD-ROM drive with a CD-DA in it. */
-        char **cd_drives = 
-          cdio_get_devices_with_cap(NULL,  CDIO_FS_AUDIO, false);
-        if (NULL == cd_drives) return -1;
-        if (cd_drives[0] == NULL) {
-          cdio_free_device_list(cd_drives);
-          return -1;
-        }
-        psz_source = strdup(cd_drives[0]);
-        cdio_free_device_list(cd_drives);
-      }
-    }
-
-    /* Open CDDA */
-    cdio_log_set_handler ( cdio_log_handler );
-
-    if( !(p_cddev = ioctl_Open( p_this, psz_source )) )
-    {
-        msg_Warn( p_input, "could not open %s", psz_source );
-        free( psz_source );
-        return -1;
-    }
-    free( psz_source );
-
-    p_cdda = malloc( sizeof(cdda_data_t) );
-    if( p_cdda == NULL )
-    {
-        msg_Err( p_input, "out of memory" );
-        free( psz_source );
-        return -1;
-    }
-
-    p_cdda->p_cddev        = p_cddev;
-    p_cdda->i_debug        = config_GetInt( p_this, MODULE_STRING "-debug" );
-    p_input->p_access_data = (void *)p_cdda;
-
-    dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "%s", psz_source );
-
-    p_input->i_mtu = CDDA_DATA_ONCE;
-
-    /* We read the Table Of Content information */
-    p_cdda->i_nb_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_input),
-                              p_cdda->p_cddev->cdio, &p_cdda->p_sectors );
-    if( p_cdda->i_nb_tracks < 0 )
-        msg_Err( p_input, "unable to count tracks" );
-    else if( p_cdda->i_nb_tracks <= 0 )
-        msg_Err( p_input, "no audio tracks found" );
-
-    if( p_cdda->i_nb_tracks <= 1)
-    {
-        ioctl_Close( p_cdda->p_cddev );
-        free( p_cdda );
-        return -1;
-    }
-
-    if( i_title >= p_cdda->i_nb_tracks || i_title < 1 )
-        i_title = 1;
-
-    /* Set stream and area data */
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-
-    /* Initialize ES structures */
-    input_InitStream( p_input, 0 );
-
-    /* cdda input method */
-    p_input->stream.i_method = INPUT_METHOD_CDDA;
-
-    p_input->stream.b_pace_control = 1;
-    p_input->stream.b_seekable = 1;
-    p_input->stream.i_mux_rate = 44100 * 4 / 50;
-
-#define area p_input->stream.pp_areas
-    for( i = 1 ; i <= p_cdda->i_nb_tracks ; i++ )
-    {
-        input_AddArea( p_input, i, 1 );
-
-        /* Absolute start offset and size */
-        area[i]->i_start =
-            (off_t)p_cdda->p_sectors[i-1] * (off_t)CDIO_CD_FRAMESIZE_RAW;
-        area[i]->i_size =
-            (off_t)(p_cdda->p_sectors[i] - p_cdda->p_sectors[i-1])
-            * (off_t)CDIO_CD_FRAMESIZE_RAW;
-    }
-#undef area
-
-    CDDAPlay( p_input, i_title);
-
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
-
-    if( !p_input->psz_demux || !*p_input->psz_demux )
-    {
-        p_input->psz_demux = "cdda";
-    }
-
-    p_input->pf_read = CDDARead;
-    p_input->pf_seek = CDDASeek;
-    p_input->pf_set_area = CDDASetArea;
-    p_input->pf_set_program = CDDASetProgram;
-
-    /* Update default_pts to a suitable value for cdda access */
-    p_input->i_pts_delay = config_GetInt( p_input, 
-                                         MODULE_STRING "-caching" ) * 1000;
+  cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
+  switch (level) {
+  case CDDB_LOG_DEBUG:
+  case CDDB_LOG_INFO:
+    if (!(p_cdda->i_debug & INPUT_DBG_CDDB)) return;
+    /* Fall through if to warn case */
+  default:
+    cdio_log_handler (level, message);
+  }
+}
+#endif /*HAVE_LIBCDDB*/
 
-    p_cdda->p_intf = intf_Create( p_input, "cddax" );
-    intf_RunThread( p_cdda->p_intf );
 
-    return 0;
+/*! This routine is when xine is not fully set up (before full initialization)
+   or is not around (before finalization). 
+*/
+static void 
+uninit_log_handler (cdio_log_level_t level, const char message[])
+{
+  cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
+  switch (level) {
+  case CDIO_LOG_DEBUG:
+  case CDIO_LOG_INFO:
+    if (!(p_cdda->i_debug & (INPUT_DBG_CDIO|INPUT_DBG_CDDB)))
+      return;
+    /* Fall through if to warn case */
+  case CDIO_LOG_WARN:
+    fprintf(stderr, "WARN: %s\n", message);
+    break;
+  case CDIO_LOG_ERROR:
+    fprintf(stderr, "ERROR: %s\n", message);
+    break;
+  case CDIO_LOG_ASSERT:
+    fprintf(stderr, "ASSERT ERROR: %s\n", message);
+    break;
+  default:
+    fprintf(stderr, "UNKNOWN ERROR: %s\n%s %d\n",
+            message, 
+            _("The above message had unknown cdio log level"), 
+            level);
+  }
+  
+  /* gl_default_cdio_log_handler (level, message); */
 }
 
 /*****************************************************************************
@@ -289,21 +167,6 @@ CDDAPlay( input_thread_t *p_input, int i_track )
   return VLC_TRUE;
 }
 
-/*****************************************************************************
- * CDDAClose: closes cdda
- *****************************************************************************/
-void 
-E_(Close)( vlc_object_t *p_this )
-{
-    input_thread_t *   p_input = (input_thread_t *)p_this;
-    cdda_data_t *p_cdda = (cdda_data_t *)p_input->p_access_data;
-
-    dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
-    ioctl_Close( p_cdda->p_cddev );
-    free( p_cdda );
-    p_cdda_input = NULL;
-}
-
 /*****************************************************************************
  * CDDARead: reads from the CDDA into PES packets.
  *****************************************************************************
@@ -344,7 +207,7 @@ static int CDDARead( input_thread_t * p_input, byte_t * p_buffer,
            dbg_print( (INPUT_DBG_LSN|INPUT_DBG_CALL), 
                       "end of track, cur: %u", p_cdda->i_sector );
 
-            if ( p_cdda->i_track >= p_cdda->i_nb_tracks - 1 )
+            /*???? if ( p_cdda->i_track >= p_cdda->i_nb_tracks - 1 )*/
                 return 0; /* EOF */
 
             vlc_mutex_lock( &p_input->stream.stream_lock );
@@ -443,103 +306,687 @@ static void CDDASeek( input_thread_t * p_input, off_t i_off )
 
 }
 
-/*****************************************************************************
- * Demux: local prototypes
- *****************************************************************************/
-struct demux_sys_t
+#define meta_info_add_str(title, str) \
+  if ( str ) {                                                         \
+    dbg_print( INPUT_DBG_META, "field %s: %s\n", title, str);  \
+    input_AddInfo( p_cat, _(title), "%s", str );                       \
+  }
+
+
+static void InformationCreate( input_thread_t *p_input  )
 {
-    es_out_id_t *p_es;
-    mtime_t     i_pts;
-};
+  cdda_data_t *p_cdda = (cdda_data_t *) p_input->p_access_data;
+  input_info_category_t *p_cat;
+  
+  p_cat = input_InfoCategory( p_input, "General" );
+
+#ifdef HAVE_LIBCDDB
+  if (p_cdda->i_cddb_enabled) {
+    
+    meta_info_add_str( "Title", p_cdda->cddb.disc->title );
+    meta_info_add_str( "Artist", p_cdda->cddb.disc->artist );
+    meta_info_add_str( "Genre", p_cdda->cddb.disc->genre );
+    meta_info_add_str( "Extended Data", p_cdda->cddb.disc->ext_data );
+    {
+      char year[5];
+      if (p_cdda->cddb.disc->year != 0) {
+       snprintf(year, 5, "%d", p_cdda->cddb.disc->year);
+       meta_info_add_str( "Year", year );
+      }
+      if ( p_cdda->cddb.disc->discid ) {
+       input_AddInfo( p_cat, _("CDDB Disc ID"), "%x", 
+                      p_cdda->cddb.disc->discid );
+      }
+      
+      if ( p_cdda->cddb.disc->category != CDDB_CAT_INVALID ) {
+       input_AddInfo( p_cat, _("CDDB Disc Category"), "%s", 
+                      CDDB_CATEGORY[p_cdda->cddb.disc->category] );
+      }
+      
+    }
+  }
 
-static int  Demux     ( input_thread_t * p_input );
+#endif /*HAVE_LIBCDDB*/
 
-/****************************************************************************
- * DemuxOpen:
- ****************************************************************************/
-int  
-E_(DemuxOpen)    ( vlc_object_t * p_this)
+  {
+    track_t i_track = p_cdda->i_nb_tracks;
+    char psz_buffer[MSTRTIME_MAX_SIZE];
+    mtime_t i_duration = 
+      (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[0]) 
+      / CDIO_CD_FRAMES_PER_SEC;
+
+    dbg_print( INPUT_DBG_META, "Duration %ld", (long int) i_duration );
+    input_AddInfo( p_cat, _("Duration"), "%s", 
+                  secstotimestr( psz_buffer, i_duration ) );
+  }
+}
+
+
+#ifdef HAVE_LIBCDDB
+
+#define free_and_dup(var, val) \
+  if (var) free(var);         \
+  if (val) var=strdup(val);           
+  
+
+static void
+GetCDDBInfo( const input_thread_t *p_input, cdda_data_t *p_cdda )
 {
-    input_thread_t *p_input = (input_thread_t *)p_this;
-    demux_sys_t    *p_sys;
 
-    es_format_t    fmt;
+  dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
 
-    if( p_input->stream.i_method != INPUT_METHOD_CDDA )
-    {
-        return VLC_EGENERIC;
+  if (config_GetInt( p_input, MODULE_STRING "-cddb-enabled" )) {
+    int i, i_matches;
+    cddb_conn_t  *conn = cddb_new();
+    const CdIo *cdio = p_cdda->p_cddev->cdio;
+    
+    
+    cddb_log_set_handler (uninit_log_handler);
+
+    if (!conn) {
+      msg_Warn( p_input, "unable to initialize libcddb" );
+      goto cddb_destroy;
+    }
+    
+    cddb_set_email_address( conn, 
+                           config_GetPsz( p_input, 
+                                          MODULE_STRING "-cddb-email") );
+    
+    cddb_set_server_name( conn, 
+                         config_GetPsz( p_input, 
+                                        MODULE_STRING "-cddb-server") );
+
+    cddb_set_server_port(conn, 
+                         config_GetInt( p_input, 
+                                        MODULE_STRING "-cddb-port") );
+
+    /* Set the location of the local CDDB cache directory.
+       The default location of this directory is */
+
+    if (!config_GetInt( p_input, MODULE_STRING "-cddb-enable-cache" )) 
+      cddb_cache_disable(conn);
+
+    cddb_cache_set_dir(conn, 
+                      config_GetPsz( p_input, 
+                                     MODULE_STRING "-cddb-cachedir") );
+
+    cddb_set_timeout(conn, 
+                    config_GetInt( p_input, MODULE_STRING "-cddb-timeout") );
+
+
+    if (config_GetInt( p_input, MODULE_STRING "-cddb-httpd" )) {
+      cddb_http_enable(conn);
+    } else
+      cddb_http_disable(conn);
+    
+    p_cdda->cddb.disc = cddb_disc_new();
+    if (!p_cdda->cddb.disc) {
+      msg_Err( p_input, "Unable to create CDDB disc structure." );
+      goto cddb_end;
     }
 
-    p_input->pf_demux  = Demux;
-    p_input->pf_rewind = NULL;
-    p_input->pf_demux_control = demux_vaControlDefault;
-    p_input->p_demux_data = p_sys = malloc( sizeof( es_descriptor_t ) );
-    p_sys->i_pts = 0;
+    for(i = 1; i <= p_cdda->i_nb_tracks; i++) {
+      cddb_track_t *t = cddb_track_new(); 
+      t->frame_offset = cdio_get_track_lba(cdio, i);
+      cddb_disc_add_track(p_cdda->cddb.disc, t);
+    }
+    
+    p_cdda->cddb.disc->length = 
+      cdio_get_track_lba(cdio, CDIO_CDROM_LEADOUT_TRACK) 
+      / CDIO_CD_FRAMES_PER_SEC;
 
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-    if( input_InitStream( p_input, 0 ) == -1)
+
+    if (!cddb_disc_calc_discid(p_cdda->cddb.disc)) {
+      msg_Err( p_input, "CDDB disc calc failed" );
+      goto cddb_destroy;
+    }
+
+    i_matches = cddb_query(conn, p_cdda->cddb.disc);
+    if (i_matches > 0) {
+      if (i_matches > 1)
+       msg_Warn( p_input, "Found %d matches in CDDB. Using first one.", 
+                 i_matches);
+      cddb_read(conn, p_cdda->cddb.disc);
+
+      if (p_cdda->i_debug & INPUT_DBG_CDDB) 
+       cddb_disc_print(p_cdda->cddb.disc);
+
+    } else {
+      msg_Warn( p_input, "CDDB error: %s", cddb_error_str(errno));
+    }
+
+  cddb_destroy:
+    cddb_destroy(conn);
+  }
+  cddb_end: ;
+}
+#endif /*HAVE_LIBCDDB*/
+
+#define add_format_str_info(val)                       \
+  {                                                    \
+    const char *str = val;                             \
+    unsigned int len;                                  \
+    if (val != NULL) {                                 \
+      len=strlen(str);                                 \
+      if (len != 0) {                                  \
+       strncat(tp, str, TEMP_STR_LEN-(tp-temp_str));   \
+       tp += len;                                      \
+      }                                                        \
+      saw_control_prefix = false;                      \
+    }                                                  \
+  }
+
+#define add_format_num_info(val, fmt)                  \
+  {                                                    \
+    char num_str[10];                                  \
+    unsigned int len;                                   \
+    sprintf(num_str, fmt, val);                                \
+    len=strlen(num_str);                                \
+    if (len != 0) {                                    \
+      strncat(tp, num_str, TEMP_STR_LEN-(tp-temp_str));        \
+      tp += len;                                       \
+    }                                                  \
+    saw_control_prefix = false;                                \
+  }
+
+/*!
+   Take a format string and expand escape sequences, that is sequences that
+   begin with %, with information from the current CD. 
+   The expanded string is returned. Here is a list of escape sequences:
+
+   %a : The album artist **
+   %A : The album information **
+   %C : Category **
+   %I : CDDB disk ID **
+   %G : Genre **
+   %M : The current MRL
+   %m : The CD-DA Media Catalog Number (MCN)
+   %n : The number of tracks on the CD
+   %p : The artist/performer/composer in the track **
+   %T : The track number **
+   %s : Number of seconds in this track
+   %t : The name **
+   %Y : The year 19xx or 20xx **
+   %% : a %
+*/
+static char *
+CDDAFormatStr(const input_thread_t *p_input, cdda_data_t *p_cdda,
+             const char format_str[], const char *mrl, int i_track)
+{
+#define TEMP_STR_SIZE 256
+#define TEMP_STR_LEN (TEMP_STR_SIZE-1)
+  static char    temp_str[TEMP_STR_SIZE];
+  size_t i;
+  char * tp = temp_str;
+  bool saw_control_prefix = false;
+  size_t format_len = strlen(format_str);
+
+  bzero(temp_str, TEMP_STR_SIZE);
+
+  for (i=0; i<format_len; i++) {
+
+    if (!saw_control_prefix && format_str[i] != '%') {
+      *tp++ = format_str[i];
+      saw_control_prefix = false;
+      continue;
+    }
+
+    switch(format_str[i]) {
+    case '%':
+      if (saw_control_prefix) {
+       *tp++ = '%';
+      }
+      saw_control_prefix = !saw_control_prefix;
+      break;
+#ifdef HAVE_LIBCDDB      
+    case 'a':
+      if (!p_cdda->i_cddb_enabled) goto not_special;
+      add_format_str_info(p_cdda->cddb.disc->artist);
+      break;
+    case 'A':
+      if (!p_cdda->i_cddb_enabled) goto not_special;
+      add_format_str_info(p_cdda->cddb.disc->title);
+      break;
+    case 'C':
+      if (!p_cdda->i_cddb_enabled) goto not_special;
+      add_format_str_info(CDDB_CATEGORY[p_cdda->cddb.disc->category]);
+      break;
+    case 'G':
+      if (!p_cdda->i_cddb_enabled) goto not_special;
+      add_format_str_info(p_cdda->cddb.disc->genre);
+      break;
+    case 'I':
+      if (!p_cdda->i_cddb_enabled) goto not_special;
+      add_format_num_info(p_cdda->cddb.disc->discid, "%x");
+      break;
+    case 'Y':
+      if (!p_cdda->i_cddb_enabled) goto not_special;
+      add_format_num_info(p_cdda->cddb.disc->year, "%5d");
+      break;
+    case 't':
+      if (p_cdda->i_cddb_enabled) {
+       cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc, 
+                                           i_track-1);
+       if (t != NULL && t->title != NULL) 
+         add_format_str_info(t->title);
+      } else goto not_special;
+      break;
+    case 'p':
+      if (p_cdda->i_cddb_enabled) {
+       cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc, 
+                                           i_track-1);
+       if (t != NULL && t->artist != NULL) 
+         add_format_str_info(t->artist);
+      } else goto not_special;
+      break;
+#endif
+
+    case 'M':
+      add_format_str_info(mrl);
+      break;
+
+#if FINISHED
+    case 'm':
+      add_format_str_info(p_cdda->mcn);
+      break;
+#endif
+
+    case 'n':
+      add_format_num_info(p_cdda->i_nb_tracks, "%d");
+      break;
+
+    case 's':
+      if (p_cdda->i_cddb_enabled) {
+       char psz_buffer[MSTRTIME_MAX_SIZE];
+       mtime_t i_duration = 
+         (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[i_track-1]) 
+         / CDIO_CD_FRAMES_PER_SEC;
+       add_format_str_info(secstotimestr( psz_buffer, i_duration ) );
+      } else goto not_special;
+      break;
+
+    case 'T':
+      add_format_num_info(i_track, "%02d");
+      break;
+#ifdef HAVE_LIBCDDB      
+    not_special:
+#endif
+    default:
+      *tp++ = '%'; 
+      *tp++ = format_str[i];
+      saw_control_prefix = false;
+    }
+  }
+  return strdup(temp_str);
+}
+
+static void
+CDDACreatePlayListItem(const input_thread_t *p_input, cdda_data_t *p_cdda, 
+                      playlist_t *p_playlist, unsigned int i_track, 
+                      char *psz_mrl, int psz_mrl_max, 
+                      const char *psz_source, int playlist_operation, 
+                      int i_pos)
+{
+  mtime_t i_duration = 
+    (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[i_track-1]) 
+    / CDIO_CD_FRAMES_PER_SEC;
+  char *p_author;
+  char *p_title;
+  char *config_varname = MODULE_STRING "-title-format";
+
+#ifdef HAVE_LIBCDDB
+  if (p_cdda->i_cddb_enabled) {
+    config_varname = MODULE_STRING "-cddb-title-format";
+  }
+#endif
+
+  snprintf(psz_mrl, psz_mrl_max, "%s%s@T%u", 
+          CDDA_MRL_PREFIX, psz_source, i_track);
+
+  p_title = CDDAFormatStr(p_input, p_cdda,
+                         config_GetPsz( p_input, config_varname ), 
+                         psz_mrl, i_track);
+
+  dbg_print( INPUT_DBG_META, "mrl: %s, title: %s, duration, %ld, pos %d",
+            psz_mrl, p_title, (long int) i_duration, i_pos );
+
+  playlist_AddExt( p_playlist, psz_mrl, p_title, i_duration * 1000000, 
+                  0, 0, playlist_operation, i_pos );
+
+  p_author = 
+    CDDAFormatStr( p_input, p_cdda,
+                  config_GetPsz( p_input, MODULE_STRING "-author-format" ),
+                  psz_mrl, i_track );
+
+  /* FIXME: This is horrible, but until the playlist interface is fixed up
+     something like this has to be done for the "Author" field.
+   */
+  if( i_pos == PLAYLIST_END ) i_pos = p_playlist->i_size - 1;
+  free(p_playlist->pp_items[i_pos]->psz_author);
+  p_playlist->pp_items[i_pos]->psz_author = strdup(p_author);
+
+}
+
+static int
+CDDAFixupPlayList( input_thread_t *p_input, cdda_data_t *p_cdda, 
+                 const char *psz_source, bool play_single_track) 
+{
+  int i;
+  playlist_t * p_playlist;
+  char       * psz_mrl;
+  unsigned int psz_mrl_max = strlen(CDDA_MRL_PREFIX) + strlen(psz_source) + 
+    strlen("@T") + strlen("100") + 1;
+
+#ifdef HAVE_LIBCDDB
+  p_cdda->i_cddb_enabled = 
+    config_GetInt( p_input, MODULE_STRING "-cddb-enabled" );
+#endif
+  
+  if (play_single_track && !p_cdda->i_cddb_enabled) return 0;
+
+  psz_mrl = malloc( psz_mrl_max );
+
+  if( psz_mrl == NULL )
     {
-        vlc_mutex_unlock( &p_input->stream.stream_lock );
-        msg_Err( p_input, "cannot init stream" );
-        free( p_sys );
-        return VLC_EGENERIC;
+      msg_Warn( p_input, "out of memory" );
+      return -1;
     }
-    p_input->stream.i_mux_rate = 4 * 44100 / 50;
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
 
-    es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'a', 'r', 'a', 'w' ) );
-    fmt.audio.i_channels = 2;
-    fmt.audio.i_rate = 44100;
-    fmt.audio.i_bitspersample = 16;
-    fmt.audio.i_blockalign = 4;
-    fmt.i_bitrate = 4 * 44100 * 8;
+  p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
+                                              FIND_ANYWHERE );
+  if( !p_playlist )
+    {
+      msg_Warn( p_input, "can't find playlist" );
+      free(psz_mrl);
+      return -1;
+    }
+
+#ifdef HAVE_LIBCDDB
+  if (p_cdda->i_cddb_enabled)
+    GetCDDBInfo(p_input, p_cdda);
+  else 
+    p_cdda->cddb.disc = NULL;
+#endif
+
+  InformationCreate(p_input);
+  
+  if (play_single_track) {
+    /* May fill out more information when the playlist user interface becomes
+       more mature.
+     */
+    CDDACreatePlayListItem(p_input, p_cdda, p_playlist, p_cdda->i_track+1, 
+                          psz_mrl, psz_mrl_max, psz_source, PLAYLIST_REPLACE, 
+                          p_playlist->i_index);
+  } else {
+  
+    playlist_Delete( p_playlist, p_playlist->i_index);
+
+    for( i = 1 ; i <= p_cdda->i_nb_tracks ; i++ )
+      {
+       CDDACreatePlayListItem(p_input, p_cdda, p_playlist, i, psz_mrl, 
+                              psz_mrl_max, psz_source, PLAYLIST_APPEND, 
+                              PLAYLIST_END);
 
-    p_sys->p_es =  es_out_Add( p_input->p_es_out, &fmt );
+      }
+
+    playlist_Command( p_playlist, PLAYLIST_GOTO, 0 );
 
-    return VLC_SUCCESS;
+  }
+    
+  vlc_object_release( p_playlist );
+  free(psz_mrl);
+  return 0;
 }
 
 /****************************************************************************
- * DemuxClose:
+ * Public functions
  ****************************************************************************/
-void 
-E_(DemuxClose)( vlc_object_t * p_this)
+int
+E_(DebugCB)   ( vlc_object_t *p_this, const char *psz_name,
+               vlc_value_t oldval, vlc_value_t val, void *p_data )
 {
-    input_thread_t *p_input = (input_thread_t*)p_this;
-    demux_sys_t    *p_sys = (demux_sys_t*)p_input->p_demux_data;
+  cdda_data_t *p_cdda;
 
-    free( p_sys );
-    return;
+  if (NULL == p_cdda_input) return VLC_EGENERIC;
+  
+  p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
+
+  if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
+    msg_Dbg( p_cdda_input, "Old debug (x%0x) %d, new debug (x%0x) %d", 
+             p_cdda->i_debug, p_cdda->i_debug, val.i_int, val.i_int);
+  }
+  p_cdda->i_debug = val.i_int;
+  return VLC_SUCCESS;
 }
 
-/****************************************************************************
- * Demux:
- ****************************************************************************/
-static int  Demux( input_thread_t * p_input )
+int
+E_(CDDBEnabledCB)   ( vlc_object_t *p_this, const char *psz_name,
+                     vlc_value_t oldval, vlc_value_t val, void *p_data )
 {
-    demux_sys_t    *p_sys = (demux_sys_t*)p_input->p_demux_data;
-    block_t        *p_block;
+  cdda_data_t *p_cdda;
 
+  if (NULL == p_cdda_input) return VLC_EGENERIC;
+  
+  p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
+
+  if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
+    msg_Dbg( p_cdda_input, "Old CDDB Enabled (x%0x) %d, new (x%0x) %d", 
+             p_cdda->i_cddb_enabled, p_cdda->i_cddb_enabled, 
+            val.i_int, val.i_int);
+  }
+  p_cdda->i_cddb_enabled = val.i_int;
+  return VLC_SUCCESS;
+}
+
+/*FIXME*/
+#if PLAYLIST_INTERFACE_IS_FIXED
+int
+E_(TitleFormatCB)   ( vlc_object_t *p_this, const char *psz_name,
+                     vlc_value_t oldval, vlc_value_t val, void *p_data )
+{
+  cdda_data_t *p_cdda;
+
+  if (NULL == p_cdda_input) return VLC_EGENERIC;
+  
+  p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
+
+  if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
+    msg_Dbg( p_cdda_input, "Old CDDB Enabled (%s), new (%s)", 
+             oldval.psz_string, val.psz_string);
+  }
+  ????
+  return VLC_SUCCESS;
+}
+#endif
+
+/*****************************************************************************
+ * Open: open cdda
+ *****************************************************************************/
+int 
+E_(Open)( vlc_object_t *p_this )
+{
+    input_thread_t *        p_input = (input_thread_t *)p_this;
+    char *                  psz_orig;
+    char *                  psz_parser;
+    char *                  psz_source;
+    cdda_data_t *           p_cdda;
+    int                     i;
+    int                     i_track = 1;
+    cddev_t                 *p_cddev;
+    bool                    play_single_track = false;
 
-    input_ClockManageRef( p_input,
-                          p_input->stream.p_selected_program,
-                          p_sys->i_pts );
+    /* Set where to log errors messages from libcdio. */
+    p_cdda_input = (input_thread_t *)p_this;
+
+    /* parse the options passed in command line : */
+    psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
 
-    if( ( p_block = stream_Block( p_input->s, CDIO_CD_FRAMESIZE_RAW ) ) == NULL )
+    if( !psz_orig )
     {
-        /* eof */
-        return 0;
+        return( -1 );
     }
-    p_block->i_dts =
-    p_block->i_pts = input_ClockGetTS( p_input,
-                                       p_input->stream.p_selected_program,
-                                       p_sys->i_pts );
-    p_block->i_length = (mtime_t)90000 * (mtime_t)p_block->i_buffer/44100/4;
 
-    p_sys->i_pts += p_block->i_length;
+    while( *psz_parser && *psz_parser != '@' )
+    {
+        psz_parser++;
+    }
 
-    es_out_Send( p_input->p_es_out, p_sys->p_es, p_block );
+    if( *psz_parser == '@' )
+    {
+        /* Found options */
+        *psz_parser = '\0';
+        ++psz_parser;
 
-    return 1;
+        if ('T' == *psz_parser || 't' == *psz_parser ) 
+            ++psz_parser;
+          
+        i_track = (int)strtol( psz_parser, NULL, 10 );
+        i_track = i_track ? i_track : 1;
+       play_single_track = true;
+    }
+
+    if( !*psz_source ) {
+      /* No source specified, so figure it out. */
+      if( !p_input->psz_access ) {
+        free( psz_orig );
+        return -1;
+      }
+      psz_source = config_GetPsz( p_input, "cd-audio" );
+      
+      if( !psz_source || 0==strlen(psz_source) ) {
+        /* Scan for a CD-ROM drive with a CD-DA in it. */
+        char **cd_drives = 
+          cdio_get_devices_with_cap(NULL,  CDIO_FS_AUDIO, false);
+        if (NULL == cd_drives) return -1;
+        if (cd_drives[0] == NULL) {
+          cdio_free_device_list(cd_drives);
+          return -1;
+        }
+        psz_source = strdup(cd_drives[0]);
+        cdio_free_device_list(cd_drives);
+      }
+    }
+
+    /* Open CDDA */
+    cdio_log_set_handler ( cdio_log_handler );
+#ifdef HAVE_LIBCDDB
+    cddb_log_set_handler ( cddb_log_handler );
+#endif
+
+    if( !(p_cddev = ioctl_Open( p_this, psz_source )) )
+    {
+        msg_Warn( p_input, "could not open %s", psz_source );
+        free( psz_source );
+        return -1;
+    }
+
+    p_cdda = malloc( sizeof(cdda_data_t) );
+    if( p_cdda == NULL )
+    {
+        msg_Err( p_input, "out of memory" );
+        free( psz_source );
+        return -1;
+    }
+
+    p_cdda->p_cddev        = p_cddev;
+    p_cdda->i_debug        = config_GetInt( p_this, MODULE_STRING "-debug" );
+    p_input->p_access_data = (void *)p_cdda;
+
+    dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "%s", psz_source );
+
+    p_input->i_mtu = CDDA_DATA_ONCE;
+
+    /* We read the Table Of Content information */
+    p_cdda->i_nb_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_input),
+                              p_cdda->p_cddev->cdio, &p_cdda->p_sectors );
+    if( p_cdda->i_nb_tracks < 0 )
+        msg_Err( p_input, "unable to count tracks" );
+    else if( p_cdda->i_nb_tracks <= 0 )
+        msg_Err( p_input, "no audio tracks found" );
+
+    if( p_cdda->i_nb_tracks <= 1)
+    {
+        ioctl_Close( p_cdda->p_cddev );
+        free( p_cdda );
+       free( psz_source );
+        return -1;
+    }
+
+    if( i_track >= p_cdda->i_nb_tracks || i_track < 1 )
+        i_track = 1;
+
+    /* Set stream and area data */
+    vlc_mutex_lock( &p_input->stream.stream_lock );
+
+    /* Initialize ES structures */
+    input_InitStream( p_input, 0 );
+
+    /* cdda input method */
+    p_input->stream.i_method = INPUT_METHOD_CDDA;
+
+    p_input->stream.b_pace_control = 1;
+    p_input->stream.b_seekable = 1;
+    p_input->stream.i_mux_rate = 44100 * 4 / 50;
+
+#define area p_input->stream.pp_areas
+    for( i = 1 ; i <= p_cdda->i_nb_tracks ; i++ )
+    {
+        input_AddArea( p_input, i, 1 );
+
+        /* Absolute start offset and size */
+        area[i]->i_start =
+            (off_t)p_cdda->p_sectors[i-1] * (off_t)CDIO_CD_FRAMESIZE_RAW;
+        area[i]->i_size =
+            (off_t)(p_cdda->p_sectors[i] - p_cdda->p_sectors[i-1])
+            * (off_t)CDIO_CD_FRAMESIZE_RAW;
+    }
+#undef area
+
+    CDDAPlay( p_input, i_track);
+
+    CDDAFixupPlayList(p_input, p_cdda, psz_source, play_single_track);
+
+    vlc_mutex_unlock( &p_input->stream.stream_lock );
+
+    if( !p_input->psz_demux || !*p_input->psz_demux )
+    {
+        p_input->psz_demux = "cdda";
+    }
+
+    p_input->pf_read = CDDARead;
+    p_input->pf_seek = CDDASeek;
+    p_input->pf_set_area = CDDASetArea;
+    p_input->pf_set_program = CDDASetProgram;
+
+    /* Update default_pts to a suitable value for cdda access */
+    p_input->i_pts_delay = config_GetInt( p_input, 
+                                         MODULE_STRING "-caching" ) * 1000;
+
+    free( psz_source );
+
+    return 0;
 }
 
+/*****************************************************************************
+ * CDDAClose: closes cdda
+ *****************************************************************************/
+void 
+E_(Close)( vlc_object_t *p_this )
+{
+    input_thread_t *   p_input = (input_thread_t *)p_this;
+    cdda_data_t *p_cdda = (cdda_data_t *)p_input->p_access_data;
+
+    dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
+    ioctl_Close( p_cdda->p_cddev );
+
+    cdio_log_set_handler (uninit_log_handler);
+
+#ifdef HAVE_LIBCDDB
+    cddb_log_set_handler (uninit_log_handler);
+    if (p_cdda->i_cddb_enabled)
+      cddb_disc_destroy(p_cdda->cddb.disc);
+#endif
+
+    free( p_cdda );
+    p_cdda_input = NULL;
+}