]> git.sesse.net Git - vlc/blobdiff - modules/access/alsa.c
Use var_InheritString for --decklink-video-connection.
[vlc] / modules / access / alsa.c
index 1c86dc899907cd652185b94192d7e939e5d98c90..5f766dfdc537c6a1cc389355214db1420157148e 100644 (file)
 #include <vlc_access.h>
 #include <vlc_demux.h>
 #include <vlc_input.h>
-#include <vlc_vout.h>
 
-#include <ctype.h>
-#include <fcntl.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
@@ -80,26 +77,32 @@ static void DemuxClose( vlc_object_t * );
     "Caching value for Alsa captures. This " \
     "value should be set in milliseconds." )
 
+#define HELP_TEXT N_( \
+    "Use alsa:// to open the default audio input. If multiple audio " \
+    "inputs are available, they will be listed in the vlc debug output. " \
+    "To select hw:0,1 , use alsa://hw:0,1 ." )
+
 #define ALSA_DEFAULT "hw"
 #define CFG_PREFIX "alsa-"
 
-vlc_module_begin();
-    set_shortname( N_("Alsa") );
-    set_description( N_("Alsa audio capture input") );
-    set_category( CAT_INPUT );
-    set_subcategory( SUBCAT_INPUT_ACCESS );
+vlc_module_begin()
+    set_shortname( N_("ALSA") )
+    set_description( N_("ALSA audio capture input") )
+    set_category( CAT_INPUT )
+    set_subcategory( SUBCAT_INPUT_ACCESS )
+    set_help( HELP_TEXT )
 
-    add_shortcut( "alsa" );
-    set_capability( "access_demux", 10 );
-    set_callbacks( DemuxOpen, DemuxClose );
+    add_shortcut( "alsa" )
+    set_capability( "access_demux", 10 )
+    set_callbacks( DemuxOpen, DemuxClose )
 
     add_bool( CFG_PREFIX "stereo", true, NULL, STEREO_TEXT, STEREO_LONGTEXT,
-                true );
+                true )
     add_integer( CFG_PREFIX "samplerate", 48000, NULL, SAMPLERATE_TEXT,
-                SAMPLERATE_LONGTEXT, true );
+                SAMPLERATE_LONGTEXT, true )
     add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
-                CACHING_TEXT, CACHING_LONGTEXT, true );
-vlc_module_end();
+                CACHING_TEXT, CACHING_LONGTEXT, true )
+vlc_module_end()
 
 /*****************************************************************************
  * Access: local prototypes
@@ -111,44 +114,114 @@ static int Demux( demux_t * );
 
 static block_t* GrabAudio( demux_t *p_demux );
 
-static int OpenAudioDev( vlc_object_t *, demux_sys_t * );
-static bool ProbeAudioDevAlsa( vlc_object_t *, const char *psz_device );
+static int OpenAudioDev( demux_t *, const char * );
+static bool ProbeAudioDevAlsa( demux_t *, const char * );
+static char *ListAvailableDevices( demux_t *, bool b_probe );
 
 struct demux_sys_t
 {
-    const char *psz_device;  /* Alsa device from MRL */
-    int  i_fd_audio;
-
     /* Audio */
-    int i_pts;
+    int i_cache;
     unsigned int i_sample_rate;
     bool b_stereo;
-    size_t i_audio_max_frame_size;
-    block_t *p_block_audio;
-    es_out_id_t *p_es_audio;
-
-    int i_audio_method;
+    size_t i_max_frame_size;
+    block_t *p_block;
+    es_out_id_t *p_es;
 
     /* ALSA Audio */
     snd_pcm_t *p_alsa_pcm;
     size_t i_alsa_frame_size;
     int i_alsa_chunk_size;
+
+    int64_t i_next_demux_date; /* Used to handle alsa:// as input-slave properly */
 };
 
-static int FindMainDevice( vlc_object_t *p_this, demux_sys_t *p_sys )
+static int FindMainDevice( demux_t *p_demux, const char *psz_device )
 {
-    msg_Dbg( p_this, "opening device '%s'", p_sys->psz_device );
-    if( ProbeAudioDevAlsa( p_this, p_sys->psz_device ) )
+    if( psz_device )
     {
-        msg_Dbg( p_this, "'%s' is an audio device", p_sys->psz_device );
-        p_sys->i_fd_audio = OpenAudioDev( p_this, p_sys );
+        msg_Dbg( p_demux, "opening device '%s'", psz_device );
+        if( ProbeAudioDevAlsa( p_demux, psz_device ) )
+        {
+            msg_Dbg( p_demux, "'%s' is an audio device", psz_device );
+            OpenAudioDev( p_demux, psz_device );
+        }
+    }
+    else if( ProbeAudioDevAlsa( p_demux, ALSA_DEFAULT ) )
+    {
+        msg_Dbg( p_demux, "'%s' is an audio device", ALSA_DEFAULT );
+        OpenAudioDev( p_demux, ALSA_DEFAULT );
+    }
+    else if( ( psz_device = ListAvailableDevices( p_demux, true ) ) )
+    {
+        msg_Dbg( p_demux, "'%s' is an audio device", psz_device );
+        OpenAudioDev( p_demux, psz_device );
+        free( (char *)psz_device );
     }
 
-    if( p_sys->i_fd_audio < 0 )
+    if( p_demux->p_sys->p_alsa_pcm == NULL )
         return VLC_EGENERIC;
     return VLC_SUCCESS;
 }
 
+static char *ListAvailableDevices( demux_t *p_demux, bool b_probe )
+{
+    snd_ctl_card_info_t *p_info = NULL;
+    snd_ctl_card_info_alloca( &p_info );
+
+    snd_pcm_info_t *p_pcminfo = NULL;
+    snd_pcm_info_alloca( &p_pcminfo );
+
+    if( !b_probe )
+        msg_Dbg( p_demux, "Available alsa capture devices:" );
+    int i_card = -1;
+    while( !snd_card_next( &i_card ) && i_card >= 0 )
+    {
+        char psz_devname[10];
+        snprintf( psz_devname, 10, "hw:%d", i_card );
+
+        snd_ctl_t *p_ctl = NULL;
+        if( snd_ctl_open( &p_ctl, psz_devname, 0 ) < 0 ) continue;
+
+        snd_ctl_card_info( p_ctl, p_info );
+        if( !b_probe )
+            msg_Dbg( p_demux, "  %s (%s)",
+                     snd_ctl_card_info_get_id( p_info ),
+                     snd_ctl_card_info_get_name( p_info ) );
+
+        int i_dev = -1;
+        while( !snd_ctl_pcm_next_device( p_ctl, &i_dev ) && i_dev >= 0 )
+        {
+            snd_pcm_info_set_device( p_pcminfo, i_dev );
+            snd_pcm_info_set_subdevice( p_pcminfo, 0 );
+            snd_pcm_info_set_stream( p_pcminfo, SND_PCM_STREAM_CAPTURE );
+            if( snd_ctl_pcm_info( p_ctl, p_pcminfo ) < 0 ) continue;
+
+            if( !b_probe )
+                msg_Dbg( p_demux, "    hw:%d,%d : %s (%s)", i_card, i_dev,
+                         snd_pcm_info_get_id( p_pcminfo ),
+                         snd_pcm_info_get_name( p_pcminfo ) );
+            else
+            {
+                char *psz_device;
+                if( asprintf( &psz_device, "hw:%d,%d", i_card, i_dev ) > 0 )
+                {
+                    if( ProbeAudioDevAlsa( p_demux, psz_device ) )
+                    {
+                        snd_ctl_close( p_ctl );
+                        return psz_device;
+                    }
+                    else
+                        free( psz_device );
+                }
+            }
+        }
+
+        snd_ctl_close( p_ctl );
+    }
+    return NULL;
+}
+
 /*****************************************************************************
  * DemuxOpen: opens alsa device, access_demux callback
  *****************************************************************************
@@ -174,22 +247,23 @@ static int DemuxOpen( vlc_object_t *p_this )
     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
     if( p_sys == NULL ) return VLC_ENOMEM;
 
-    p_sys->i_sample_rate = var_CreateGetInteger( p_demux, CFG_PREFIX "samplerate" );
-    p_sys->b_stereo = var_CreateGetBool( p_demux, CFG_PREFIX "stereo" );
-    p_sys->i_pts = var_CreateGetInteger( p_demux, CFG_PREFIX "caching" );
-    p_sys->psz_device = NULL;
-    p_sys->i_fd_audio = -1;
-    p_sys->p_es_audio = NULL;
-    p_sys->p_block_audio = NULL;
+    p_sys->i_sample_rate = var_InheritInteger( p_demux, CFG_PREFIX "samplerate" );
+    p_sys->b_stereo = var_InheritBool( p_demux, CFG_PREFIX "stereo" );
+    p_sys->i_cache = var_InheritInteger( p_demux, CFG_PREFIX "caching" );
+    p_sys->p_es = NULL;
+    p_sys->p_block = NULL;
+    p_sys->i_next_demux_date = -1;
 
-    if( p_demux->psz_path && *p_demux->psz_path )
-        p_sys->psz_device = p_demux->psz_path;
+    const char *psz_device = NULL;
+    if( p_demux->psz_location && *p_demux->psz_location )
+        psz_device = p_demux->psz_location;
     else
-        p_sys->psz_device = ALSA_DEFAULT;
-    msg_Err( p_this, "Device is %s", p_sys->psz_device );
+        ListAvailableDevices( p_demux, false );
 
-    if( FindMainDevice( p_this, p_sys ) != VLC_SUCCESS )
+    if( FindMainDevice( p_demux, psz_device ) != VLC_SUCCESS )
     {
+        if( p_demux->psz_location && *p_demux->psz_location )
+            ListAvailableDevices( p_demux, false );
         DemuxClose( p_this );
         return VLC_EGENERIC;
     }
@@ -208,11 +282,10 @@ static void DemuxClose( vlc_object_t *p_this )
     if( p_sys->p_alsa_pcm )
     {
         snd_pcm_close( p_sys->p_alsa_pcm );
-        p_sys->i_fd_audio = -1;
     }
-    if( p_sys->i_fd_audio >= 0 ) close( p_sys->i_fd_audio );
 
-    if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
+    if( p_sys->p_block ) block_Release( p_sys->p_block );
+
     free( p_sys );
 }
 
@@ -222,8 +295,6 @@ static void DemuxClose( vlc_object_t *p_this )
 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
-    bool *pb;
-    int64_t *pi64;
 
     switch( i_query )
     {
@@ -232,18 +303,19 @@ static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
         case DEMUX_CAN_SEEK:
         case DEMUX_SET_PAUSE_STATE:
         case DEMUX_CAN_CONTROL_PACE:
-            pb = (bool*)va_arg( args, bool * );
-            *pb = false;
+            *va_arg( args, bool * ) = false;
             return VLC_SUCCESS;
 
         case DEMUX_GET_PTS_DELAY:
-            pi64 = (int64_t*)va_arg( args, int64_t * );
-            *pi64 = (int64_t)p_sys->i_pts * 1000;
+            *va_arg( args, int64_t * ) = (int64_t)p_sys->i_cache * 1000;
             return VLC_SUCCESS;
 
         case DEMUX_GET_TIME:
-            pi64 = (int64_t*)va_arg( args, int64_t * );
-            *pi64 = mdate();
+            *va_arg( args, int64_t * ) = mdate();
+            return VLC_SUCCESS;
+
+        case DEMUX_SET_NEXT_DEMUX_TIME:
+            p_sys->i_next_demux_date = va_arg( args, int64_t );
             return VLC_SUCCESS;
 
         /* TODO implement others */
@@ -261,24 +333,47 @@ static int Demux( demux_t *p_demux )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
 
-    struct pollfd fd;
-    fd.fd = p_sys->i_fd_audio;
-    fd.events = POLLIN|POLLPRI;
-    fd.revents = 0;
+    block_t *p_block = NULL;
 
-    /* Wait for data */
-    if( poll( &fd, 1, 500 ) ) /* Timeout after 0.5 seconds since I don't know if pf_demux can be blocking. */
+    do
     {
-        if( fd.revents & (POLLIN|POLLPRI) )
+        if( p_block )
         {
-            block_t *p_block = GrabAudio( p_demux );
-            if( p_block )
+            es_out_Send( p_demux->out, p_sys->p_es, p_block );
+            p_block = NULL;
+        }
+
+        /* Wait for data */
+        int i_wait = snd_pcm_wait( p_sys->p_alsa_pcm, 10 ); /* See poll() comment in oss.c */
+        switch( i_wait )
+        {
+            case 1:
+            {
+                p_block = GrabAudio( p_demux );
+                if( p_block )
+                    es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
+            }
+
+            /* FIXME: this is a copy paste from below. Shouldn't be needed
+             * twice. */
+            case -EPIPE:
+                /* xrun */
+                snd_pcm_prepare( p_sys->p_alsa_pcm );
+                break;
+            case -ESTRPIPE:
             {
-                es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
-                es_out_Send( p_demux->out, p_sys->p_es_audio, p_block );
+                /* suspend */
+                int i_resume = snd_pcm_resume( p_sys->p_alsa_pcm );
+                if( i_resume < 0 && i_resume != -EAGAIN ) snd_pcm_prepare( p_sys->p_alsa_pcm );
+                break;
             }
+            /* </FIXME> */
         }
-    }
+    } while( p_block && p_sys->i_next_demux_date > 0 &&
+             p_block->i_pts < p_sys->i_next_demux_date );
+
+    if( p_block )
+        es_out_Send( p_demux->out, p_sys->p_es, p_block );
 
     return 1;
 }
@@ -290,12 +385,11 @@ static int Demux( demux_t *p_demux )
 static block_t* GrabAudio( demux_t *p_demux )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
-    int i_read = 0, i_correct;
+    int i_read, i_correct;
     block_t *p_block;
 
-    printf("%s %d\n",__func__,__LINE__);
-    if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
-    else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
+    if( p_sys->p_block ) p_block = p_sys->p_block;
+    else p_block = block_New( p_demux, p_sys->i_max_frame_size );
 
     if( !p_block )
     {
@@ -303,7 +397,7 @@ static block_t* GrabAudio( demux_t *p_demux )
         return 0;
     }
 
-    p_sys->p_block_audio = p_block;
+    p_sys->p_block = p_block;
 
     /* ALSA */
     i_read = snd_pcm_readi( p_sys->p_alsa_pcm, p_block->p_buffer, p_sys->i_alsa_chunk_size );
@@ -337,7 +431,7 @@ static block_t* GrabAudio( demux_t *p_demux )
     if( i_read <= 0 ) return 0;
 
     p_block->i_buffer = i_read;
-    p_sys->p_block_audio = 0;
+    p_sys->p_block = 0;
 
     /* Correct the date because of kernel buffering */
     i_correct = i_read;
@@ -348,11 +442,11 @@ static block_t* GrabAudio( demux_t *p_demux )
     {
         size_t i_correction_delta = delay * p_sys->i_alsa_frame_size;
         /* Test for overrun */
-        if( i_correction_delta > p_sys->i_audio_max_frame_size )
+        if( i_correction_delta > p_sys->i_max_frame_size )
         {
             msg_Warn( p_demux, "ALSA read overrun (%zu > %zu)",
-                      i_correction_delta, p_sys->i_audio_max_frame_size );
-            i_correction_delta = p_sys->i_audio_max_frame_size;
+                      i_correction_delta, p_sys->i_max_frame_size );
+            i_correction_delta = p_sys->i_max_frame_size;
             snd_pcm_prepare( p_sys->p_alsa_pcm );
         }
         i_correct += i_correction_delta;
@@ -375,9 +469,9 @@ static block_t* GrabAudio( demux_t *p_demux )
 /*****************************************************************************
  * OpenAudioDev: open and set up the audio device and probe for capabilities
  *****************************************************************************/
-static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys )
+static int OpenAudioDevAlsa( demux_t *p_demux, const char *psz_device )
 {
-    const char *psz_device = p_sys->psz_device;
+    demux_sys_t *p_sys = p_demux->p_sys;
     p_sys->p_alsa_pcm = NULL;
     snd_pcm_hw_params_t *p_hw_params = NULL;
     snd_pcm_uframes_t buffer_size;
@@ -389,14 +483,14 @@ static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys )
     if( ( i_err = snd_pcm_open( &p_sys->p_alsa_pcm, psz_device,
         SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0)
     {
-        msg_Err( p_this, "Cannot open ALSA audio device %s (%s)",
+        msg_Err( p_demux, "Cannot open ALSA audio device %s (%s)",
                  psz_device, snd_strerror( i_err ) );
         goto adev_fail;
     }
 
     if( ( i_err = snd_pcm_nonblock( p_sys->p_alsa_pcm, 1 ) ) < 0)
     {
-        msg_Err( p_this, "Cannot set ALSA nonblock (%s)",
+        msg_Err( p_demux, "Cannot set ALSA nonblock (%s)",
                  snd_strerror( i_err ) );
         goto adev_fail;
     }
@@ -405,7 +499,7 @@ static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys )
 
     if( ( i_err = snd_pcm_hw_params_malloc( &p_hw_params ) ) < 0 )
     {
-        msg_Err( p_this,
+        msg_Err( p_demux,
                  "ALSA: cannot allocate hardware parameter structure (%s)",
                  snd_strerror( i_err ) );
         goto adev_fail;
@@ -413,7 +507,7 @@ static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys )
 
     if( ( i_err = snd_pcm_hw_params_any( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
     {
-        msg_Err( p_this,
+        msg_Err( p_demux,
                 "ALSA: cannot initialize hardware parameter structure (%s)",
                  snd_strerror( i_err ) );
         goto adev_fail;
@@ -422,7 +516,7 @@ static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys )
     /* Set Interleaved access */
     if( ( i_err = snd_pcm_hw_params_set_access( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
     {
-        msg_Err( p_this, "ALSA: cannot set access type (%s)",
+        msg_Err( p_demux, "ALSA: cannot set access type (%s)",
                  snd_strerror( i_err ) );
         goto adev_fail;
     }
@@ -430,20 +524,16 @@ static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys )
     /* Set 16 bit little endian */
     if( ( i_err = snd_pcm_hw_params_set_format( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_FORMAT_S16_LE ) ) < 0 )
     {
-        msg_Err( p_this, "ALSA: cannot set sample format (%s)",
+        msg_Err( p_demux, "ALSA: cannot set sample format (%s)",
                  snd_strerror( i_err ) );
         goto adev_fail;
     }
 
     /* Set sample rate */
-#ifdef HAVE_ALSA_NEW_API
     i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, &p_sys->i_sample_rate, NULL );
-#else
-    i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, p_sys->i_sample_rate, NULL );
-#endif
     if( i_err < 0 )
     {
-        msg_Err( p_this, "ALSA: cannot set sample rate (%s)",
+        msg_Err( p_demux, "ALSA: cannot set sample rate (%s)",
                  snd_strerror( i_err ) );
         goto adev_fail;
     }
@@ -453,13 +543,13 @@ static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys )
     if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
     {
         channels = ( channels==1 ) ? 2 : 1;
-        msg_Warn( p_this, "ALSA: cannot set channel count (%s). "
+        msg_Warn( p_demux, "ALSA: cannot set channel count (%s). "
                   "Trying with channels=%d",
                   snd_strerror( i_err ),
                   channels );
         if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
         {
-            msg_Err( p_this, "ALSA: cannot set channel count (%s)",
+            msg_Err( p_demux, "ALSA: cannot set channel count (%s)",
                      snd_strerror( i_err ) );
             goto adev_fail;
         }
@@ -470,7 +560,7 @@ static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys )
     unsigned int buffer_time;
     if( ( i_err = snd_pcm_hw_params_get_buffer_time_max(p_hw_params, &buffer_time, 0) ) < 0 )
     {
-        msg_Err( p_this, "ALSA: cannot get buffer time max (%s)",
+        msg_Err( p_demux, "ALSA: cannot get buffer time max (%s)",
                  snd_strerror( i_err ) );
         goto adev_fail;
     }
@@ -478,27 +568,19 @@ static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys )
 
     /* Set period time */
     unsigned int period_time = buffer_time / 4;
-#ifdef HAVE_ALSA_NEW_API
     i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, &period_time, 0 );
-#else
-    i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, period_time, 0 );
-#endif
     if( i_err < 0 )
     {
-        msg_Err( p_this, "ALSA: cannot set period time (%s)",
+        msg_Err( p_demux, "ALSA: cannot set period time (%s)",
                  snd_strerror( i_err ) );
         goto adev_fail;
     }
 
     /* Set buffer time */
-#ifdef HAVE_ALSA_NEW_API
     i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, &buffer_time, 0 );
-#else
-    i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, buffer_time, 0 );
-#endif
     if( i_err < 0 )
     {
-        msg_Err( p_this, "ALSA: cannot set buffer time (%s)",
+        msg_Err( p_demux, "ALSA: cannot set buffer time (%s)",
                  snd_strerror( i_err ) );
         goto adev_fail;
     }
@@ -506,7 +588,7 @@ static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys )
     /* Apply new hardware parameters */
     if( ( i_err = snd_pcm_hw_params( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
     {
-        msg_Err( p_this, "ALSA: cannot set hw parameters (%s)",
+        msg_Err( p_demux, "ALSA: cannot set hw parameters (%s)",
                  snd_strerror( i_err ) );
         goto adev_fail;
     }
@@ -516,7 +598,7 @@ static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys )
     snd_pcm_hw_params_get_buffer_size( p_hw_params, &buffer_size );
     if( chunk_size == buffer_size )
     {
-        msg_Err( p_this,
+        msg_Err( p_demux,
                  "ALSA: period cannot equal buffer size (%lu == %lu)",
                  chunk_size, buffer_size);
         goto adev_fail;
@@ -527,7 +609,7 @@ static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys )
 
     p_sys->i_alsa_chunk_size = chunk_size;
     p_sys->i_alsa_frame_size = bits_per_frame / 8;
-    p_sys->i_audio_max_frame_size = chunk_size * bits_per_frame / 8;
+    p_sys->i_max_frame_size = chunk_size * bits_per_frame / 8;
 
     snd_pcm_hw_params_free( p_hw_params );
     p_hw_params = NULL;
@@ -535,36 +617,34 @@ static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys )
     /* Prep device */
     if( ( i_err = snd_pcm_prepare( p_sys->p_alsa_pcm ) ) < 0 )
     {
-        msg_Err( p_this,
+        msg_Err( p_demux,
                  "ALSA: cannot prepare audio interface for use (%s)",
                  snd_strerror( i_err ) );
         goto adev_fail;
     }
 
-    if( !p_sys->psz_device )
-        p_sys->psz_device = strdup( ALSA_DEFAULT );
+    snd_pcm_start( p_sys->p_alsa_pcm );
 
-    /* Return a fake handle so other tests work */
-    return 1;
+    return VLC_SUCCESS;
 
  adev_fail:
 
     if( p_hw_params ) snd_pcm_hw_params_free( p_hw_params );
     if( p_sys->p_alsa_pcm ) snd_pcm_close( p_sys->p_alsa_pcm );
+    p_sys->p_alsa_pcm = NULL;
 
-    return -1;
+    return VLC_EGENERIC;
 
 }
 
-static int OpenAudioDev( vlc_object_t *p_this, demux_sys_t *p_sys )
+static int OpenAudioDev( demux_t *p_demux, const char *psz_device )
 {
-    int i_fd  = OpenAudioDevAlsa( p_this, p_sys );
-
-    if( i_fd < 0 )
-        return i_fd;
+    demux_sys_t *p_sys = p_demux->p_sys;
+    if( OpenAudioDevAlsa( p_demux, psz_device ) != VLC_SUCCESS )
+        return VLC_EGENERIC;
 
-    msg_Dbg( p_this, "opened adev=`%s' %s %dHz",
-             p_sys->psz_device, p_sys->b_stereo ? "stereo" : "mono",
+    msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
+             psz_device, p_sys->b_stereo ? "stereo" : "mono",
              p_sys->i_sample_rate );
 
     es_format_t fmt;
@@ -576,26 +656,25 @@ static int OpenAudioDev( vlc_object_t *p_this, demux_sys_t *p_sys )
     fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
     fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
 
-    msg_Dbg( p_this, "new audio es %d channels %dHz",
+    msg_Dbg( p_demux, "new audio es %d channels %dHz",
              fmt.audio.i_channels, fmt.audio.i_rate );
 
-    demux_t *p_demux = (demux_t *)p_this;
-    p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
+    p_sys->p_es = es_out_Add( p_demux->out, &fmt );
 
-    return i_fd;
+    return VLC_SUCCESS;
 }
 
 /*****************************************************************************
  * ProbeAudioDevAlsa: probe audio for capabilities
  *****************************************************************************/
-static bool ProbeAudioDevAlsa( vlc_object_t *p_this, const char *psz_device )
+static bool ProbeAudioDevAlsa( demux_t *p_demux, const char *psz_device )
 {
     int i_err;
     snd_pcm_t *p_alsa_pcm;
 
     if( ( i_err = snd_pcm_open( &p_alsa_pcm, psz_device, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0 )
     {
-        msg_Err( p_this, "cannot open device %s for ALSA audio (%s)", psz_device, snd_strerror( i_err ) );
+        msg_Err( p_demux, "cannot open device %s for ALSA audio (%s)", psz_device, snd_strerror( i_err ) );
         return false;
     }