]> git.sesse.net Git - vlc/blobdiff - modules/access/cdda/access.c
Fix crash
[vlc] / modules / access / cdda / access.c
index 23c725bfcc0752816ee0a0ba1bba6f49abb9c655..45cc329aa37791bd0522886df76694dc4051a847 100644 (file)
@@ -1,10 +1,10 @@
 /*****************************************************************************
  * 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 $
+ * Copyright (C) 2000, 2003, 2004 VideoLAN
+ * $Id$
  *
- * Authors: Rocky Bernstein <rocky@panix.com> 
+ * Authors: Rocky Bernstein <rocky@panix.com>
  *          Laurent Aimar <fenrir@via.ecp.fr>
  *          Gildas Bazin <gbazin@netcourrier.com>
  *
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdio.h>
-#include <stdlib.h>
+#include "callback.h"      /* FIXME - reorganize callback.h, cdda.h better */
+#include "cdda.h"          /* private structures. Also #includes vlc things */
+#include "info.h"          /* headers for meta info retrieval */
+#include <vlc_playlist.h>  /* Has to come *after* cdda.h */
+#include "vlc_keys.h"
 
-#include <vlc/vlc.h>
-#include <vlc/intf.h>
-#include <vlc/input.h>
-#include <sys/types.h>
 #include <cdio/cdio.h>
+#include <cdio/logging.h>
 #include <cdio/cd_types.h>
 
-#include "codecs.h"
-#include "vlc_keys.h"
+#include <stdio.h>
 
-#ifdef HAVE_UNISTD_H
-#   include <unistd.h>
+/* #ifdef variables below are defined via config.h via #include vlc above. */
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
 #endif
 
-#include <string.h>
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
 
-#include "cdda.h"
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
 
-/* how many blocks Open will read in each loop */
-#define CDDA_BLOCKS_ONCE 1
-#define CDDA_DATA_ONCE   (CDDA_BLOCKS_ONCE * CDIO_CD_FRAMESIZE_RAW)
+#ifdef HAVE_UNISTD_H
+#   include <unistd.h>
+#endif
 
 /* FIXME: This variable is a hack. Would be nice to eliminate. */
-static input_thread_t *p_cdda_input = NULL;
+access_t *p_cdda_input = NULL;
 
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-static int  CDDARead         ( input_thread_t *, byte_t *, size_t );
-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 block_t *CDDAReadBlocks( access_t * p_access );
+static int      CDDASeek( access_t * p_access, int64_t i_pos );
+static int      CDDAControl( access_t *p_access, int i_query,
+                             va_list args );
+
+static int      CDDAInit( access_t *p_access, cdda_data_t *p_cdda ) ;
+
 
 /****************************************************************************
  * 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[])
 {
-  cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
+  cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
+
+  if( p_cdda == NULL )
+      return;
+
   switch (level) {
   case CDIO_LOG_DEBUG:
   case CDIO_LOG_INFO:
-    if (p_cdda->i_debug & INPUT_DBG_CDIO) 
+    if (p_cdda->i_debug & INPUT_DBG_CDIO)
       msg_Dbg( p_cdda_input, message);
     break;
   case CDIO_LOG_WARN:
@@ -104,442 +97,569 @@ cdio_log_handler (cdio_log_level_t level, const char message[])
     break;
   default:
     msg_Warn( p_cdda_input, message,
-            _("The above message had unknown vcdimager log level"), 
+            "The above message had unknown cdio log level",
             level);
   }
   return;
 }
 
 
-/*****************************************************************************
- * 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 )
+    cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
+    switch (level)
     {
-        return( -1 );
-    }
+        case CDDB_LOG_DEBUG:
+        case CDDB_LOG_INFO:
+        if (!(p_cdda->i_debug & INPUT_DBG_CDDB)) return;
 
-    while( *psz_parser && *psz_parser != '@' )
-    {
-        psz_parser++;
+        /* Fall through if to warn case */
+        default:
+            cdio_log_handler (level, message);
     }
+}
+#endif /*HAVE_LIBCDDB*/
 
-    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);
-      }
+/*! 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 = NULL;
+
+    if (p_cdda_input)
+        p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
+
+     switch (level)
+     {
+        case CDIO_LOG_DEBUG:
+        case CDIO_LOG_INFO:
+        if (!p_cdda || !(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);
     }
 
-    /* Open CDDA */
-    cdio_log_set_handler ( cdio_log_handler );
+    /* gl_default_cdio_log_handler (level, message); */
+}
 
-    if( !(p_cddev = ioctl_Open( p_this, psz_source )) )
-    {
-        msg_Warn( p_input, "could not open %s", psz_source );
-        free( psz_source );
-        return -1;
+/*****************************************************************************
+ * CDDAReadBlocks: reads a group of blocks from the CD-DA and returns
+ * an allocated pointer to the data. NULL is returned if no data
+ * read. It is also possible if we haven't read a RIFF header in which
+ * case one that we creaded during Open/Initialization is returned.
+ *****************************************************************************/
+static block_t * CDDAReadBlocks( access_t * p_access )
+{
+    block_t     *p_block;
+    cdda_data_t *p_cdda   = (cdda_data_t *) p_access->p_sys;
+    int          i_blocks = p_cdda->i_blocks_per_read;
+
+    dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_LSN), "called %d",
+              p_cdda->i_lsn);
+
+    /* Check end of file */
+    if( p_access->info.b_eof ) return NULL;
+
+    if( !p_cdda->b_header )
+      {
+        /* Return only the dummy RIFF header we created in Open/Init */
+        p_block = block_New( p_access, sizeof( WAVEHEADER ) );
+        memcpy( p_block->p_buffer, &p_cdda->waveheader, sizeof(WAVEHEADER) );
+        p_cdda->b_header = VLC_TRUE;
+        return p_block;
     }
-    free( psz_source );
 
-    p_cdda = malloc( sizeof(cdda_data_t) );
-    if( p_cdda == NULL )
+    /* Check end of track */
+    while( p_cdda->i_lsn >= p_cdda->lsn[p_cdda->i_track+1] )
     {
-        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 );
+        if( p_cdda->i_track >= p_cdda->i_first_track + p_cdda->i_titles - 1 )
+        {
+            p_access->info.b_eof = VLC_TRUE;
+            return NULL;
+        }
 
-    p_input->i_mtu = CDDA_DATA_ONCE;
+        p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SIZE;
+        p_access->info.i_title++;
+        p_access->info.i_size =
+                  p_cdda->p_title[p_access->info.i_title]->i_size;
+        p_access->info.i_pos = 0;
+        p_cdda->i_track++;
+    }
 
-    /* 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)
+    /* Possibly adjust i_blocks so we don't read past the end of a track. */
+    if( p_cdda->i_lsn + i_blocks >= p_cdda->lsn[p_cdda->i_track+1] )
     {
-        ioctl_Close( p_cdda->p_cddev );
-        free( p_cdda );
-        return -1;
+        i_blocks = p_cdda->lsn[p_cdda->i_track+1 ] - p_cdda->i_lsn;
     }
 
-    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++ )
+    /* Do the actual reading */
+    p_block = block_New( p_access, i_blocks * CDIO_CD_FRAMESIZE_RAW );
+    if( !p_block)
     {
-        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;
+      msg_Err( p_access, "Cannot get a new block of size: %i",
+               i_blocks * CDIO_CD_FRAMESIZE_RAW );
+      return NULL;
     }
-#undef area
-
-    CDDAPlay( p_input, i_title);
-
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
 
-    if( !p_input->psz_demux || !*p_input->psz_demux )
+    if( cdio_read_audio_sectors( p_cdda->p_cdio, p_block->p_buffer,
+                                 p_cdda->i_lsn, i_blocks) != 0 )
     {
-        p_input->psz_demux = "cdda";
+        msg_Err( p_access, "could not read sector %lu",
+                 (long unsigned int) p_cdda->i_lsn );
+        block_Release( p_block );
+
+        /* If we had problems above, assume the problem is with
+           the first sector of the read and set to skip it.  In
+           the future libcdio may have cdparanoia support.
+        */
+        p_cdda->i_lsn++;
+        p_access->info.i_pos += CDIO_CD_FRAMESIZE_RAW;
+        return NULL;
     }
 
-    p_input->pf_read = CDDARead;
-    p_input->pf_seek = CDDASeek;
-    p_input->pf_set_area = CDDASetArea;
-    p_input->pf_set_program = CDDASetProgram;
+    p_cdda->i_lsn        += i_blocks;
+    p_access->info.i_pos += p_block->i_buffer;
 
-    /* Update default_pts to a suitable value for cdda access */
-    p_input->i_pts_delay = config_GetInt( p_input, 
-                                         MODULE_STRING "-caching" ) * 1000;
-
-    p_cdda->p_intf = intf_Create( p_input, "cddax" );
-    intf_RunThread( p_cdda->p_intf );
-
-    return 0;
+    return p_block;
 }
 
-/*****************************************************************************
- * CDDAPlay: Arrange things so we play the specified track.
- * VLC_TRUE is returned if there was no error.
- *****************************************************************************/
-vlc_bool_t
-CDDAPlay( input_thread_t *p_input, int i_track )
+/****************************************************************************
+ * CDDASeek - change position for subsequent reads. For example, this
+ * can happen if the user moves a position slider bar in a GUI.
+ ****************************************************************************/
+static int CDDASeek( access_t * p_access, int64_t i_pos )
 {
-  cdda_data_t *p_cdda = (cdda_data_t *) p_input->p_access_data;
+    cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
 
-  if( i_track >= p_cdda->i_nb_tracks || i_track < 1 )
-    return VLC_FALSE;
+    p_cdda->i_lsn = p_cdda->lsn[p_cdda->i_track]
+                  + (i_pos / CDIO_CD_FRAMESIZE_RAW);
+    p_access->info.i_pos = i_pos;
 
-  CDDASetArea( p_input, p_input->stream.pp_areas[i_track] );
-  return VLC_TRUE;
+    dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_SEEK),
+               "lsn %lu, offset: %lld",
+               (long unsigned int) p_cdda->i_lsn, i_pos );
+    return VLC_SUCCESS;
 }
 
-/*****************************************************************************
- * 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;
-}
+/****************************************************************************
+ * Public functions
+ ****************************************************************************/
 
 /*****************************************************************************
- * CDDARead: reads from the CDDA into PES packets.
- *****************************************************************************
- * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
- * bytes.
+ * Open: open cdda device or image file and initialize structures
+ *       for subsequent operations.
  *****************************************************************************/
-static int CDDARead( input_thread_t * p_input, byte_t * p_buffer,
-                     size_t i_len )
+int E_(CDDAOpen)( vlc_object_t *p_this )
 {
-    cdda_data_t *           p_cdda;
-    int                     i_blocks;
-    int                     i_index;
-    int                     i_read;
-
-    p_cdda = (cdda_data_t *)p_input->p_access_data;
+    access_t    *p_access = (access_t*)p_this;
+    char *      psz_source = NULL;
+    cdda_data_t *p_cdda    = NULL;
+    CdIo        *p_cdio;
+    track_t     i_track = 1;
+    vlc_bool_t  b_single_track = false;
+    int         i_rc = VLC_EGENERIC;
 
-    i_read = 0;
+    p_access->p_sys = NULL;
 
-    /* Compute the number of blocks we have to read */
+    /* Set where to log errors messages from libcdio. */
+    p_cdda_input = p_access;
 
-    i_blocks = i_len / CDIO_CD_FRAMESIZE_RAW;
+    /* parse the options passed in command line : */
 
-    for ( i_index = 0; i_index < i_blocks; i_index++ )
+    if( p_access->psz_path && *p_access->psz_path )
     {
+        char *psz_parser = psz_source = strdup( p_access->psz_path );
 
-      if (cdio_read_audio_sector(p_cdda->p_cddev->cdio, p_buffer, 
-                                 p_cdda->i_sector) != 0)
+        while( *psz_parser && *psz_parser != '@' )
         {
-          msg_Err( p_input, "could not read sector %d", p_cdda->i_sector );
-          return -1;
+            psz_parser++;
         }
 
-        p_cdda->i_sector ++;
-        if ( p_cdda->i_sector == p_cdda->p_sectors[p_cdda->i_track + 1] )
+        if( *psz_parser == '@' )
         {
-            input_area_t *p_area;
+            /* Found options */
+            *psz_parser = '\0';
+            ++psz_parser;
 
-           dbg_print( (INPUT_DBG_LSN|INPUT_DBG_CALL), 
-                      "end of track, cur: %u", p_cdda->i_sector );
+            if ('T' == *psz_parser || 't' == *psz_parser )
+            ++psz_parser;
+
+            i_track = (int)strtol( psz_parser, NULL, 10 );
+            i_track = i_track ? i_track : 1;
+            b_single_track = true;
+        }
+    }
 
-            if ( p_cdda->i_track >= p_cdda->i_nb_tracks - 1 )
-                return 0; /* EOF */
+    if (!psz_source || !*psz_source)
+    {
+        /* No device/track given. Continue only when this plugin was
+           selected */
+        if( !p_this->b_force ) return VLC_EGENERIC;
 
-            vlc_mutex_lock( &p_input->stream.stream_lock );
-            p_area = p_input->stream.pp_areas[
-                    p_input->stream.p_selected_area->i_id + 1 ];
+        psz_source = var_CreateGetString( p_this, "cd-audio" );
 
-            p_area->i_part = 1;
-            CDDASetArea( p_input, p_area );
-            vlc_mutex_unlock( &p_input->stream.stream_lock );
+        if( !psz_source || !*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 || NULL == cd_drives[0] )
+            {
+                msg_Err( p_access,
+                     "libcdio couldn't find something with a CD-DA in it" );
+                if (cd_drives) cdio_free_device_list(cd_drives);
+                return VLC_EGENERIC;
+            }
+
+            psz_source = strdup(cd_drives[0]);
+            cdio_free_device_list(cd_drives);
         }
-        i_read += CDIO_CD_FRAMESIZE_RAW;
     }
 
-    if ( i_len % CDIO_CD_FRAMESIZE_RAW ) /* this should not happen */
+    cdio_log_set_handler ( cdio_log_handler );
+
+    /* Open CDDA */
+    if( !(p_cdio = cdio_open( psz_source, DRIVER_UNKNOWN )) )
     {
-        msg_Err( p_input, "must read full sectors" );
+        msg_Warn( p_access, "could not open %s", psz_source );
+        goto error2;
     }
 
-    return i_read;
-}
+    p_cdda = malloc( sizeof(cdda_data_t) );
+    if( p_cdda == NULL )
+    {
+        msg_Err( p_access, "out of memory" );
+        free( psz_source );
+        return VLC_ENOMEM;
+    }
+    memset( p_cdda, 0, sizeof(cdda_data_t) );
 
-/*****************************************************************************
- * CDDASetProgram: Does nothing since a CDDA is mono_program
- *****************************************************************************/
-static int CDDASetProgram( input_thread_t * p_input,
-                           pgrm_descriptor_t * p_program)
-{
-    cdda_data_t * p_cdda= (cdda_data_t *) p_input->p_access_data;
-    dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
-    return 0;
-}
+#ifdef HAVE_LIBCDDB
+    cddb_log_set_handler ( cddb_log_handler );
+    p_cdda->cddb.disc = NULL;
+    p_cdda->b_cddb_enabled =
+      config_GetInt( p_access, MODULE_STRING "-cddb-enabled" );
+#endif
 
-/*****************************************************************************
- * CDDASetArea: initialize input data for title x.
- * It should be called for each user navigation request.
- ****************************************************************************/
-static int CDDASetArea( input_thread_t * p_input, input_area_t * p_area )
-{
-    cdda_data_t *p_cdda = (cdda_data_t*) p_input->p_access_data;
-    vlc_value_t val;
+    p_cdda->b_cdtext_enabled =
+      config_GetInt( p_access, MODULE_STRING "-cdtext-enabled" );
 
-    dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "");
+    p_cdda->b_cdtext_prefer =
+      config_GetInt( p_access, MODULE_STRING "-cdtext-prefer" );
 
-    /* we can't use the interface slider until initilization is complete */
-    p_input->stream.b_seekable = 0;
+    p_cdda->b_header = VLC_FALSE;
+    p_cdda->p_cdio   = p_cdio;
+    p_cdda->i_titles = 0;
+    p_cdda->i_track  = i_track;
+    p_cdda->i_debug  = config_GetInt(p_this, MODULE_STRING "-debug");
+    p_cdda->i_blocks_per_read
+                     = config_GetInt(p_this, MODULE_STRING "-blocks-per-read");
 
-    if( p_area != p_input->stream.p_selected_area )
-    {
-        /* Change the default area */
-        p_input->stream.p_selected_area = p_area;
+    p_cdda->p_input  = vlc_object_find( p_access, VLC_OBJECT_INPUT,
+                                        FIND_PARENT );
 
-        /* Change the current track */
-        p_cdda->i_track = p_area->i_id - 1;
-        p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track];
+    if (0 == p_cdda->i_blocks_per_read)
+        p_cdda->i_blocks_per_read = DEFAULT_BLOCKS_PER_READ;
 
-        /* Update the navigation variables without triggering a callback */
-        val.i_int = p_area->i_id;
-        var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
+    if ( p_cdda->i_blocks_per_read < MIN_BLOCKS_PER_READ
+         || p_cdda->i_blocks_per_read > MAX_BLOCKS_PER_READ )
+    {
+        msg_Warn( p_cdda_input,
+                "Number of blocks (%d) has to be between %d and %d. "
+                "Using %d.",
+                p_cdda->i_blocks_per_read,
+                MIN_BLOCKS_PER_READ, MAX_BLOCKS_PER_READ,
+                DEFAULT_BLOCKS_PER_READ );
+        p_cdda->i_blocks_per_read = DEFAULT_BLOCKS_PER_READ;
     }
 
-    p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track];
+    dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "%s", psz_source );
+
+    /* Set up p_access */
+    p_access->pf_read    = NULL;
+    p_access->pf_block   = CDDAReadBlocks;
+    p_access->pf_control = CDDAControl;
+    p_access->pf_seek    = CDDASeek;
+
+    p_access->info.i_size      = 0;
 
-    p_input->stream.p_selected_area->i_tell =
-        (off_t)p_cdda->i_sector * (off_t)CDIO_CD_FRAMESIZE_RAW
-         - p_input->stream.p_selected_area->i_start;
+    p_access->info.i_update    = 0;
+    p_access->info.b_eof       = VLC_FALSE;
+    p_access->info.i_title     = 0;
+    p_access->info.i_seekpoint = 0;
 
-    /* warn interface that something has changed */
-    p_input->stream.b_seekable = 1;
-    p_input->stream.b_changed = 1;
+    p_access->p_sys     = (access_sys_t *) p_cdda;
+
+    /* We read the Table Of Content information */
+    i_rc = CDDAInit( p_access, p_cdda );
+    if ( VLC_SUCCESS != i_rc ) goto error;
+
+    CDDAFixupPlaylist( p_access, p_cdda, psz_source, b_single_track );
+
+    /* Build a WAV header to put in front of the output data.
+       This gets sent back in the Block (read) routine.
+     */
+    memset( &p_cdda->waveheader, 0, sizeof(WAVEHEADER) );
+    SetWLE( &p_cdda->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
+    SetWLE( &p_cdda->waveheader.BitsPerSample, 16);
+    p_cdda->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
+    p_cdda->waveheader.Length = 0;                     /* we just don't know */
+    p_cdda->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
+    p_cdda->waveheader.SubChunkID  = VLC_FOURCC('f', 'm', 't', ' ');
+    SetDWLE( &p_cdda->waveheader.SubChunkLength, 16);
+    SetWLE( &p_cdda->waveheader.Modus, 2);
+    SetDWLE( &p_cdda->waveheader.SampleFreq, CDDA_FREQUENCY_SAMPLE);
+    SetWLE( &p_cdda->waveheader.BytesPerSample,
+            2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
+    SetDWLE( &p_cdda->waveheader.BytesPerSec,
+             2*16/8 /*BytesPerSample*/ * CDDA_FREQUENCY_SAMPLE );
+    p_cdda->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
+    p_cdda->waveheader.DataLength  = 0;    /* we just don't know */
+
+    /* PTS delay */
+    var_Create( p_access, MODULE_STRING "-caching",
+                VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
+    vlc_object_release( p_cdda->p_input );
+    return VLC_SUCCESS;
+
+ error:
+    cdio_destroy( p_cdda->p_cdio );
+    free( p_cdda );
+ error2:
+    free( psz_source );
+    if( p_cdda && p_cdda->p_input )
+    {
+        vlc_object_release( p_cdda->p_input );
+    }
+    return i_rc;
 
-    return 0;
 }
 
-/****************************************************************************
- * CDDASeek
- ****************************************************************************/
-static void CDDASeek( input_thread_t * p_input, off_t i_off )
+/*****************************************************************************
+ * CDDAClose: closes cdda and frees any resources associded with it.
+ *****************************************************************************/
+void E_(CDDAClose)( vlc_object_t *p_this )
 {
-    cdda_data_t * p_cdda;
+    access_t    *p_access = (access_t *) p_this;
+    cdda_data_t *p_cdda   = (cdda_data_t *) p_access->p_sys;
+    track_t      i;
 
-    p_cdda = (cdda_data_t *) p_input->p_access_data;
+    dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
 
-    p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track]
-                       + i_off / (off_t)CDIO_CD_FRAMESIZE_RAW;
+    /* Remove playlist titles */
+    for( i = 0; i < p_cdda->i_titles; i++ )
+    {
+        vlc_input_title_Delete( p_cdda->p_title[i] );
+    }
 
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-    p_input->stream.p_selected_area->i_tell =
-        (off_t)p_cdda->i_sector * (off_t)CDIO_CD_FRAMESIZE_RAW
-         - p_input->stream.p_selected_area->i_start;
+    cdio_destroy( p_cdda->p_cdio );
 
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
+    cdio_log_set_handler (uninit_log_handler);
 
-    dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_SEEK),
-    "sector %ud, offset: %lld, i_tell: %lld",  p_cdda->i_sector, i_off, 
-               p_input->stream.p_selected_area->i_tell );
+#ifdef HAVE_LIBCDDB
+    cddb_log_set_handler (uninit_log_handler);
+    if (p_cdda->b_cddb_enabled)
+      cddb_disc_destroy(p_cdda->cddb.disc);
+#endif
 
+    if (p_cdda->psz_mcn) free( p_cdda->psz_mcn );
+    free( p_cdda );
+    p_cdda_input = NULL;
 }
 
 /*****************************************************************************
- * Demux: local prototypes
+ * Control: The front-end or vlc engine calls here to ether get
+ * information such as meta information or plugin capabilities or to
+ * issue miscellaneous "set" requests.
  *****************************************************************************/
-struct demux_sys_t
+static int CDDAControl( access_t *p_access, int i_query, va_list args )
 {
-    es_out_id_t *p_es;
-    mtime_t     i_pts;
-};
+    cdda_data_t  *p_cdda = (cdda_data_t *) p_access->p_sys;
+    int          *pi_int;
+    int i;
 
-static int  Demux     ( input_thread_t * p_input );
+    dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_EVENT),
+               "query %d", i_query );
 
-/****************************************************************************
- * DemuxOpen:
- ****************************************************************************/
-int  
-E_(DemuxOpen)    ( vlc_object_t * p_this)
-{
-    input_thread_t *p_input = (input_thread_t *)p_this;
-    demux_sys_t    *p_sys;
+    switch( i_query )
+    {
+        /* Pass back a copy of meta information that was gathered when we
+           during the Open/Initialize call.
+         */
+        case ACCESS_GET_META:
+        {
+            vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
+            if ( p_cdda->p_meta )
+            {
+                *pp_meta = vlc_meta_Duplicate( p_cdda->p_meta );
+                dbg_print( INPUT_DBG_META, "%s", "Meta copied" );
+            }
+            else
+                msg_Warn( p_access, "tried to copy NULL meta info" );
+
+            return VLC_SUCCESS;
+        }
 
-    es_format_t    fmt;
+        case ACCESS_CAN_SEEK:
+        case ACCESS_CAN_FASTSEEK:
+        case ACCESS_CAN_PAUSE:
+        case ACCESS_CAN_CONTROL_PACE:
+        {
+            vlc_bool_t *pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
+            *pb_bool = VLC_TRUE;
+            return VLC_SUCCESS;;
+        }
 
-    if( p_input->stream.i_method != INPUT_METHOD_CDDA )
-    {
-        return VLC_EGENERIC;
-    }
+        /* */
+        case ACCESS_GET_MTU:
+        {
+            pi_int = (int*)va_arg( args, int * );
+            *pi_int = p_cdda-> i_blocks_per_read * CDIO_CD_FRAMESIZE_RAW;
+            break;
+        }
 
-    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;
+        case ACCESS_GET_PTS_DELAY:
+        {
+            int64_t *pi_64 = (int64_t*)va_arg( args, int64_t * );
+            *pi_64 = var_GetInteger( p_access, MODULE_STRING "-caching" )
+              * MILLISECONDS_PER_SEC;
+            break;
+        }
 
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-    if( input_InitStream( p_input, 0 ) == -1)
-    {
-        vlc_mutex_unlock( &p_input->stream.stream_lock );
-        msg_Err( p_input, "cannot init stream" );
-        free( p_sys );
-        return VLC_EGENERIC;
-    }
-    p_input->stream.i_mux_rate = 4 * 44100 / 50;
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
+        /* */
+        case ACCESS_SET_PAUSE_STATE:
+            break;
 
-    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;
+        case ACCESS_GET_TITLE_INFO:
+        {
+            input_title_t ***ppp_title;
+            ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
+            pi_int    = (int*)va_arg( args, int* );
+            *((int*)va_arg( args, int* )) = 1; /* Title offset */
+
+            /* Duplicate title info */
+            dbg_print ( INPUT_DBG_EVENT,
+                        "GET TITLE: i_tracks %d, i_titles %d",
+                        p_cdda->i_tracks, p_cdda->i_titles );
+            if( p_cdda->i_titles == 0 )
+            {
+                *pi_int = 0; ppp_title = NULL;
+                return VLC_SUCCESS;
+            }
+            *pi_int = p_cdda->i_titles;
+            *ppp_title = malloc(sizeof( input_title_t **) * p_cdda->i_titles );
+
+            if (!*ppp_title) return VLC_ENOMEM;
+
+            for( i = 0; i < p_cdda->i_titles; i++ )
+            {
+                if ( p_cdda->p_title[i] )
+                   (*ppp_title)[i] =
+                          vlc_input_title_Duplicate( p_cdda->p_title[i] );
+            }
+            break;
+        }
 
-    p_sys->p_es =  es_out_Add( p_input->p_es_out, &fmt );
+        case ACCESS_SET_TITLE:
+        {
+            i = (int)va_arg( args, int );
+            if( i != p_access->info.i_title )
+            {
+                /* Update info */
+                p_access->info.i_update |=
+                    INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE;
+                p_access->info.i_title = i;
+                p_access->info.i_size = p_cdda->p_title[i]->i_size;
+                p_access->info.i_pos = 0;
+
+                /* Next sector to read */
+                p_cdda->i_lsn = p_cdda->lsn[p_cdda->i_first_track+i];
+            }
+            break;
+        }
 
-    return VLC_SUCCESS;
-}
+        case ACCESS_SET_SEEKPOINT:
+        case ACCESS_SET_PRIVATE_ID_STATE:
+            return VLC_EGENERIC;
 
-/****************************************************************************
- * DemuxClose:
- ****************************************************************************/
-void 
-E_(DemuxClose)( vlc_object_t * p_this)
-{
-    input_thread_t *p_input = (input_thread_t*)p_this;
-    demux_sys_t    *p_sys = (demux_sys_t*)p_input->p_demux_data;
+        default:
+            msg_Warn( p_access, "unimplemented query in control" );
+            return VLC_EGENERIC;
 
-    free( p_sys );
-    return;
+    }
+    return VLC_SUCCESS;
 }
 
-/****************************************************************************
- * Demux:
- ****************************************************************************/
-static int  Demux( input_thread_t * p_input )
-{
-    demux_sys_t    *p_sys = (demux_sys_t*)p_input->p_demux_data;
-    block_t        *p_block;
+/*****************************************************************************
+  CDDAInit:
 
+ Initialize information pertaining to the CD: the number of tracks,
+ first track number, LSNs for each track and the leadout. The leadout
+ information is stored after the last track. The LSN array is
+ 0-origin, same as p_access->info.  Add first_track to get what track
+ number this is on the CD. Note: libcdio uses the real track number.
 
-    input_ClockManageRef( p_input,
-                          p_input->stream.p_selected_program,
-                          p_sys->i_pts );
+ On input we assume p_cdda->p_cdio and p_cdda->i_track have been set.
 
-    if( ( p_block = stream_Block( p_input->s, CDIO_CD_FRAMESIZE_RAW ) ) == NULL )
-    {
-        /* eof */
-        return 0;
+ We return the VLC-type status, e.g. VLC_SUCCESS, VLC_ENOMEM, etc.
+ *****************************************************************************/
+static int CDDAInit( access_t *p_access, cdda_data_t *p_cdda )
+{
+    track_t i;
+    discmode_t  discmode = CDIO_DISC_MODE_NO_INFO;
+
+    p_cdda->i_tracks       = cdio_get_num_tracks(p_cdda->p_cdio);
+    p_cdda->i_first_track  = cdio_get_first_track_num(p_cdda->p_cdio);
+
+    discmode = cdio_get_discmode(p_cdda->p_cdio);
+    switch(discmode) {
+    case CDIO_DISC_MODE_CD_DA:
+    case CDIO_DISC_MODE_CD_MIXED:
+        /* These are possible for CD-DA */
+        break;
+    default:
+        /* These are not possible for CD-DA */
+        msg_Err( p_access,
+               "Disc seems not to be CD-DA. libcdio reports it is %s",
+               discmode2str[discmode]
+               );
+        return VLC_EGENERIC;
     }
-    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;
+    /* Fill the lsn array with the track/sector matches.
+       Note cdio_get_track_lsn when given num_tracks + 1 will return
+       the leadout LSN.
+     */
+    for( i = 0 ; i <= p_cdda->i_tracks ; i++ )
+    {
+        track_t i_track = p_cdda->i_first_track + i;
+        (p_cdda->lsn)[ i_track ] = cdio_get_track_lsn(p_cdda->p_cdio, i_track);
+    }
 
-    es_out_Send( p_input->p_es_out, p_sys->p_es, p_block );
+    /* Set reading start LSN. */
+    p_cdda->i_lsn = p_cdda->lsn[p_cdda->i_track];
 
-    return 1;
+    return VLC_SUCCESS;
 }
-