]> git.sesse.net Git - vlc/blobdiff - src/audio_output/output.c
differentiate mp1v from mp2v and default mpgv to mp2v
[vlc] / src / audio_output / output.c
index fb1eed591f379eae3958e06d3772a8a7104ade47..18fee051641406e65e4e5aac0aacb41e7d31e1b0 100644 (file)
 /*****************************************************************************
  * output.c : internal management of output streams for the audio output
  *****************************************************************************
- * Copyright (C) 2002-2004 the VideoLAN team
+ * Copyright (C) 2002-2004 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-/*****************************************************************************
- * Preamble
- *****************************************************************************/
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 
+#include <stdlib.h>
+#include <assert.h>
+
 #include <vlc_common.h>
 #include <vlc_aout.h>
-#include <vlc_aout_intf.h>
-#include <vlc_cpu.h>
 #include <vlc_modules.h>
 
 #include "libvlc.h"
 #include "aout_internal.h"
 
-/*****************************************************************************
- * aout_OutputNew : allocate a new output and rework the filter pipeline
- *****************************************************************************
- * This function is entered with the mixer lock.
- *****************************************************************************/
-int aout_OutputNew( audio_output_t * p_aout,
-                    const audio_sample_format_t * p_format )
+static const char unset_str[1] = ""; /* Non-NULL constant string pointer */
+
+struct aout_dev
+{
+    aout_dev_t *next;
+    char *name;
+    char id[1];
+};
+
+
+/* Local functions */
+static void aout_OutputAssertLocked (audio_output_t *aout)
+{
+    aout_owner_t *owner = aout_owner (aout);
+
+    vlc_assert_locked (&owner->lock);
+}
+
+static void aout_Destructor( vlc_object_t * p_this );
+
+static int var_Copy (vlc_object_t *src, const char *name, vlc_value_t prev,
+                     vlc_value_t value, void *data)
 {
-    vlc_assert_locked( &p_aout->lock );
-    p_aout->format = *p_format;
+    vlc_object_t *dst = data;
 
-    /* Retrieve user defaults. */
-    int i_rate = var_InheritInteger( p_aout, "aout-rate" );
-    if ( i_rate != 0 )
-        p_aout->format.i_rate = i_rate;
-    aout_FormatPrepare( &p_aout->format );
+    (void) src; (void) prev;
+    return var_Set (dst, name, value);
+}
 
-    /* Find the best output plug-in. */
-    p_aout->module = module_need( p_aout, "audio output", "$aout", false );
-    if ( p_aout->module == NULL )
+/**
+ * Supply or update the current custom ("hardware") volume.
+ * @note This only makes sense after calling aout_VolumeHardInit().
+ * @param volume current custom volume
+ *
+ * @warning The caller (i.e. the audio output plug-in) is responsible for
+ * interlocking and synchronizing call to this function and to the
+ * audio_output_t.volume_set callback. This ensures that VLC gets correct
+ * volume information (possibly with a latency).
+ */
+static void aout_VolumeNotify (audio_output_t *aout, float volume)
+{
+    var_SetFloat (aout, "volume", volume);
+}
+
+static void aout_MuteNotify (audio_output_t *aout, bool mute)
+{
+    var_SetBool (aout, "mute", mute);
+}
+
+static void aout_PolicyNotify (audio_output_t *aout, bool cork)
+{
+    (cork ? var_IncInteger : var_DecInteger) (aout->p_parent, "corks");
+}
+
+static void aout_DeviceNotify (audio_output_t *aout, const char *id)
+{
+    var_SetString (aout, "device", (id != NULL) ? id : "");
+}
+
+static void aout_HotplugNotify (audio_output_t *aout,
+                                const char *id, const char *name)
+{
+    aout_owner_t *owner = aout_owner (aout);
+    aout_dev_t *dev, **pp = &owner->dev.list;
+
+    vlc_mutex_lock (&owner->dev.lock);
+    while ((dev = *pp) != NULL)
     {
-        msg_Err( p_aout, "no suitable audio output module" );
-        return -1;
+        if (!strcmp (id, dev->id))
+            break;
+        pp = &dev->next;
     }
 
-    if ( var_Type( p_aout, "audio-channels" ) ==
-             (VLC_VAR_INTEGER | VLC_VAR_HASCHOICE) )
+    if (name != NULL)
     {
-        /* The user may have selected a different channels configuration. */
-        switch( var_InheritInteger( p_aout, "audio-channels" ) )
+        if (dev == NULL) /* Added device */
         {
-            case AOUT_VAR_CHAN_RSTEREO:
-                p_aout->format.i_original_channels |= AOUT_CHAN_REVERSESTEREO;
-                break;
-            case AOUT_VAR_CHAN_STEREO:
-                p_aout->format.i_original_channels =
-                                              AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
-                break;
-            case AOUT_VAR_CHAN_LEFT:
-                p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
-                break;
-            case AOUT_VAR_CHAN_RIGHT:
-                p_aout->format.i_original_channels = AOUT_CHAN_RIGHT;
-                break;
-            case AOUT_VAR_CHAN_DOLBYS:
-                p_aout->format.i_original_channels =
-                      AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_DOLBYSTEREO;
-                break;
+            dev = malloc (sizeof (*dev) + strlen (id));
+            if (unlikely(dev == NULL))
+                goto out;
+            dev->next = NULL;
+            strcpy (dev->id, id);
+            *pp = dev;
+            owner->dev.count++;
         }
+        else /* Modified device */
+            free (dev->name);
+        dev->name = strdup (name);
     }
-    else if ( p_aout->format.i_physical_channels == AOUT_CHAN_CENTER
-              && (p_aout->format.i_original_channels
-                   & AOUT_CHAN_PHYSMASK) == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) )
+    else
     {
-        vlc_value_t val, text;
-
-        /* Mono - create the audio-channels variable. */
-        var_Create( p_aout, "audio-channels",
-                    VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
-        text.psz_string = _("Audio Channels");
-        var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );
-
-        val.i_int = AOUT_VAR_CHAN_STEREO; text.psz_string = _("Stereo");
-        var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
-        val.i_int = AOUT_VAR_CHAN_LEFT; text.psz_string = _("Left");
-        var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
-        val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
-        var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
-        if ( p_aout->format.i_original_channels & AOUT_CHAN_DUALMONO )
+        if (dev != NULL) /* Removed device */
         {
-            /* Go directly to the left channel. */
-            p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
-            var_SetInteger( p_aout, "audio-channels", AOUT_VAR_CHAN_LEFT );
+            owner->dev.count--;
+            *pp = dev->next;
+            free (dev->name);
+            free (dev);
         }
-        var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
-                         NULL );
     }
-    else if ( p_aout->format.i_physical_channels ==
-               (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)
-                && (p_aout->format.i_original_channels &
-                     (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
+out:
+    vlc_mutex_unlock (&owner->dev.lock);
+}
+
+static void aout_RestartNotify (audio_output_t *aout, unsigned mode)
+{
+    aout_RequestRestart (aout, mode);
+}
+
+static int aout_GainNotify (audio_output_t *aout, float gain)
+{
+    aout_owner_t *owner = aout_owner (aout);
+
+    aout_OutputAssertLocked (aout);
+    aout_volume_SetVolume (owner->volume, gain);
+    /* XXX: ideally, return -1 if format cannot be amplified */
+    return 0;
+}
+
+#undef aout_New
+/**
+ * Creates an audio output object and initializes an output module.
+ */
+audio_output_t *aout_New (vlc_object_t *parent)
+{
+    vlc_value_t val, text;
+
+    audio_output_t *aout = vlc_custom_create (parent, sizeof (aout_instance_t),
+                                              "audio output");
+    if (unlikely(aout == NULL))
+        return NULL;
+
+    aout_owner_t *owner = aout_owner (aout);
+
+    vlc_mutex_init (&owner->lock);
+    vlc_mutex_init (&owner->req.lock);
+    vlc_mutex_init (&owner->dev.lock);
+    owner->req.device = (char *)unset_str;
+    owner->req.volume = -1.f;
+    owner->req.mute = -1;
+
+    vlc_object_set_destructor (aout, aout_Destructor);
+
+    /* Audio output module callbacks */
+    var_Create (aout, "volume", VLC_VAR_FLOAT);
+    var_AddCallback (aout, "volume", var_Copy, parent);
+    var_Create (aout, "mute", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
+    var_AddCallback (aout, "mute", var_Copy, parent);
+    var_Create (aout, "device", VLC_VAR_STRING);
+
+    aout->event.volume_report = aout_VolumeNotify;
+    aout->event.mute_report = aout_MuteNotify;
+    aout->event.policy_report = aout_PolicyNotify;
+    aout->event.device_report = aout_DeviceNotify;
+    aout->event.hotplug_report = aout_HotplugNotify;
+    aout->event.gain_request = aout_GainNotify;
+    aout->event.restart_request = aout_RestartNotify;
+
+    /* Audio output module initialization */
+    aout->start = NULL;
+    aout->stop = NULL;
+    aout->volume_set = NULL;
+    aout->mute_set = NULL;
+    aout->device_select = NULL;
+    owner->module = module_need (aout, "audio output", "$aout", false);
+    if (owner->module == NULL)
     {
-        vlc_value_t val, text;
+        msg_Err (aout, "no suitable audio output module");
+        vlc_object_release (aout);
+        return NULL;
+    }
 
-        /* Stereo - create the audio-channels variable. */
-        var_Create( p_aout, "audio-channels",
-                    VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
-        text.psz_string = _("Audio Channels");
-        var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );
+    /*
+     * Persistent audio output variables
+     */
+    module_config_t *cfg;
+    char *str;
+
+    /* Visualizations */
+    var_Create (aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
+    text.psz_string = _("Visualizations");
+    var_Change (aout, "visual", VLC_VAR_SETTEXT, &text, NULL);
+    val.psz_string = (char *)"";
+    text.psz_string = _("Disable");
+    var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
+    val.psz_string = (char *)"spectrometer";
+    text.psz_string = _("Spectrometer");
+    var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
+    val.psz_string = (char *)"scope";
+    text.psz_string = _("Scope");
+    var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
+    val.psz_string = (char *)"spectrum";
+    text.psz_string = _("Spectrum");
+    var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
+    val.psz_string = (char *)"vuMeter";
+    text.psz_string = _("Vu meter");
+    var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
+    /* Look for goom plugin */
+    if (module_exists ("goom"))
+    {
+        val.psz_string = (char *)"goom";
+        text.psz_string = (char *)"Goom";
+        var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
+    }
+    /* Look for libprojectM plugin */
+    if (module_exists ("projectm"))
+    {
+        val.psz_string = (char *)"projectm";
+        text.psz_string = (char*)"projectM";
+        var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
+    }
+    /* Look for VSXu plugin */
+    if (module_exists ("vsxu"))
+    {
+        val.psz_string = (char *)"vsxu";
+        text.psz_string = (char*)"Vovoid VSXu";
+        var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
+    }
+    /* Look for glspectrum plugin */
+    if (module_exists ("glspectrum"))
+    {
+        val.psz_string = (char *)"glspectrum";
+        text.psz_string = (char*)"3D spectrum";
+        var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
+    }
+    str = var_GetNonEmptyString (aout, "effect-list");
+    if (str != NULL)
+    {
+        var_SetString (aout, "visual", str);
+        free (str);
+    }
 
-        if ( p_aout->format.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
-        {
-            val.i_int = AOUT_VAR_CHAN_DOLBYS;
-            text.psz_string = _("Dolby Surround");
-        }
-        else
+    /* Equalizer */
+    var_Create (aout, "equalizer", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
+    text.psz_string = _("Equalizer");
+    var_Change (aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL);
+    val.psz_string = (char*)"";
+    text.psz_string = _("Disable");
+    var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
+    cfg = config_FindConfig (VLC_OBJECT(aout), "equalizer-preset");
+    if (likely(cfg != NULL))
+        for (unsigned i = 0; i < cfg->list_count; i++)
         {
-            val.i_int = AOUT_VAR_CHAN_STEREO;
-            text.psz_string = _("Stereo");
+            val.psz_string = cfg->list.psz[i];
+            text.psz_string = vlc_gettext(cfg->list_text[i]);
+            var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
         }
-        var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
-        val.i_int = AOUT_VAR_CHAN_LEFT; text.psz_string = _("Left");
-        var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
-        val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
-        var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
-        val.i_int = AOUT_VAR_CHAN_RSTEREO; text.psz_string=_("Reverse stereo");
-        var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
-        if ( p_aout->format.i_original_channels & AOUT_CHAN_DUALMONO )
+
+    var_Create (aout, "audio-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
+    text.psz_string = _("Audio filters");
+    var_Change (aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL);
+
+
+    var_Create (aout, "audio-visual", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
+    text.psz_string = _("Audio visualizations");
+    var_Change (aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL);
+
+    /* Replay gain */
+    var_Create (aout, "audio-replay-gain-mode",
+                VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+    text.psz_string = _("Replay gain");
+    var_Change (aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL);
+    cfg = config_FindConfig (VLC_OBJECT(aout), "audio-replay-gain-mode");
+    if (likely(cfg != NULL))
+        for (unsigned i = 0; i < cfg->list_count; i++)
         {
-            /* Go directly to the left channel. */
-            p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
-            var_SetInteger( p_aout, "audio-channels", AOUT_VAR_CHAN_LEFT );
+            val.psz_string = cfg->list.psz[i];
+            text.psz_string = vlc_gettext(cfg->list_text[i]);
+            var_Change (aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
+                            &val, &text);
         }
-        var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
-                         NULL );
+
+    var_Create (aout, "equalizer-preamp", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
+    var_Create (aout, "equalizer-bands", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
+
+    return aout;
+}
+
+/**
+ * Deinitializes an audio output module and destroys an audio output object.
+ */
+void aout_Destroy (audio_output_t *aout)
+{
+    aout_owner_t *owner = aout_owner (aout);
+
+    aout_OutputLock (aout);
+    module_unneed (aout, owner->module);
+    /* Protect against late call from intf.c */
+    aout->volume_set = NULL;
+    aout->mute_set = NULL;
+    aout_OutputUnlock (aout);
+
+    var_DelCallback (aout, "mute", var_Copy, aout->p_parent);
+    var_SetFloat (aout, "volume", -1.f);
+    var_DelCallback (aout, "volume", var_Copy, aout->p_parent);
+    vlc_object_release (aout);
+}
+
+/**
+ * Destroys the audio output lock used (asynchronously) by interface functions.
+ */
+static void aout_Destructor (vlc_object_t *obj)
+{
+    audio_output_t *aout = (audio_output_t *)obj;
+    aout_owner_t *owner = aout_owner (aout);
+
+    vlc_mutex_destroy (&owner->dev.lock);
+    for (aout_dev_t *dev = owner->dev.list, *next; dev != NULL; dev = next)
+    {
+        next = dev->next;
+        free (dev->name);
+        free (dev);
     }
-    var_TriggerCallback( p_aout, "intf-change" );
 
-    aout_FormatPrepare( &p_aout->format );
+    assert (owner->req.device == unset_str);
+    vlc_mutex_destroy (&owner->req.lock);
+    vlc_mutex_destroy (&owner->lock);
+}
 
-    /* Prepare FIFO. */
-    aout_FifoInit( p_aout, &p_aout->fifo, p_aout->format.i_rate );
-    aout_FormatPrint( p_aout, "output", &p_aout->format );
+/**
+ * Starts an audio output stream.
+ * \param fmt audio output stream format [IN/OUT]
+ * \warning The caller must hold the audio output lock.
+ */
+int aout_OutputNew (audio_output_t *aout, audio_sample_format_t *restrict fmt)
+{
+    aout_OutputAssertLocked (aout);
+
+    /* Ideally, the audio filters would be created before the audio output,
+     * and the ideal audio format would be the output of the filters chain.
+     * But that scheme would not really play well with digital pass-through. */
+    if (AOUT_FMT_LINEAR(fmt))
+    {   /* Try to stay in integer domain if possible for no/slow FPU. */
+        fmt->i_format = (fmt->i_bitspersample > 16) ? VLC_CODEC_FL32
+                                                    : VLC_CODEC_S16N;
+        aout_FormatPrepare (fmt);
+    }
 
-    /* Choose the mixer format. */
-    p_aout->mixer_format = p_aout->format;
-    if ( AOUT_FMT_NON_LINEAR(&p_aout->format) )
-        p_aout->mixer_format.i_format = p_format->i_format;
-    else
-    /* Most audio filters can only deal with single-precision,
-     * so lets always use that when hardware supports floating point. */
-    if( HAVE_FPU )
-        p_aout->mixer_format.i_format = VLC_CODEC_FL32;
-    else
-    /* Otherwise, audio filters will not work. Use fixed-point if the input has
-     * more than 16-bits depth. */
-    if( p_format->i_bitspersample > 16 )
-        p_aout->mixer_format.i_format = VLC_CODEC_FI32;
-    else
-    /* Fallback to 16-bits. This avoids pointless conversion to and from
-     * 32-bits samples for the sole purpose of software mixing. */
-        p_aout->mixer_format.i_format = VLC_CODEC_S16N;
-
-    aout_FormatPrepare( &p_aout->mixer_format );
-    aout_FormatPrint( p_aout, "mixer", &p_aout->mixer_format );
-
-    /* Create filters. */
-    p_aout->i_nb_filters = 0;
-    if ( aout_FiltersCreatePipeline( p_aout, p_aout->pp_filters,
-                                     &p_aout->i_nb_filters,
-                                     &p_aout->mixer_format,
-                                     &p_aout->format ) < 0 )
+    if (aout->start (aout, fmt))
     {
-        msg_Err( p_aout, "couldn't create audio output pipeline" );
-        module_unneed( p_aout, p_aout->module );
-        p_aout->module = NULL;
+        msg_Err (aout, "module not functional");
         return -1;
     }
+
+    if (!var_Type (aout, "stereo-mode"))
+    {
+        var_Create (aout, "stereo-mode",
+                    VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT);
+
+        vlc_value_t txt;
+        txt.psz_string = _("Stereo audio mode");
+        var_Change (aout, "stereo-mode", VLC_VAR_SETTEXT, &txt, NULL);
+    }
+
+    /* The user may have selected a different channels configuration. */
+    var_AddCallback (aout, "stereo-mode", aout_ChannelsRestart, NULL);
+    switch (var_GetInteger (aout, "stereo-mode"))
+    {
+        case AOUT_VAR_CHAN_RSTEREO:
+            fmt->i_original_channels |= AOUT_CHAN_REVERSESTEREO;
+            break;
+        case AOUT_VAR_CHAN_STEREO:
+            fmt->i_original_channels = AOUT_CHANS_STEREO;
+            break;
+        case AOUT_VAR_CHAN_LEFT:
+            fmt->i_original_channels = AOUT_CHAN_LEFT;
+            break;
+        case AOUT_VAR_CHAN_RIGHT:
+            fmt->i_original_channels = AOUT_CHAN_RIGHT;
+            break;
+        case AOUT_VAR_CHAN_DOLBYS:
+            fmt->i_original_channels = AOUT_CHANS_STEREO|AOUT_CHAN_DOLBYSTEREO;
+            break;
+        default:
+        {
+            if ((fmt->i_original_channels & AOUT_CHAN_PHYSMASK)
+                                                          != AOUT_CHANS_STEREO)
+                 break;
+
+            vlc_value_t val, txt;
+            val.i_int = 0;
+            var_Change (aout, "stereo-mode", VLC_VAR_DELCHOICE, &val, NULL);
+            if (fmt->i_original_channels & AOUT_CHAN_DOLBYSTEREO)
+            {
+                val.i_int = AOUT_VAR_CHAN_DOLBYS;
+                txt.psz_string = _("Dolby Surround");
+            }
+            else
+            {
+                val.i_int = AOUT_VAR_CHAN_STEREO;
+                txt.psz_string = _("Stereo");
+            }
+            var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
+            var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &val, NULL);
+            val.i_int = AOUT_VAR_CHAN_LEFT;
+            txt.psz_string = _("Left");
+            var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
+            if (fmt->i_original_channels & AOUT_CHAN_DUALMONO)
+            {   /* Go directly to the left channel. */
+                fmt->i_original_channels = AOUT_CHAN_LEFT;
+                var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &val, NULL);
+            }
+            val.i_int = AOUT_VAR_CHAN_RIGHT;
+            txt.psz_string = _("Right");
+            var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
+            val.i_int = AOUT_VAR_CHAN_RSTEREO;
+            txt.psz_string = _("Reverse stereo");
+            var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
+        }
+    }
+
+    aout_FormatPrepare (fmt);
+    aout_FormatPrint (aout, "output", fmt);
     return 0;
 }
 
-/*****************************************************************************
- * aout_OutputDelete : delete the output
- *****************************************************************************
- * This function is entered with the mixer lock.
- *****************************************************************************/
-void aout_OutputDelete( audio_output_t * p_aout )
+/**
+ * Stops the audio output stream (undoes aout_OutputNew()).
+ * \note This can only be called after a succesful aout_OutputNew().
+ * \warning The caller must hold the audio output lock.
+ */
+void aout_OutputDelete (audio_output_t *aout)
 {
-    vlc_assert_locked( &p_aout->lock );
-
-    if( p_aout->module == NULL )
-        return;
+    aout_OutputAssertLocked (aout);
 
-    module_unneed( p_aout, p_aout->module );
-    aout_VolumeNoneInit( p_aout ); /* clear volume callback */
-    p_aout->module = NULL;
-    aout_FiltersDestroyPipeline( p_aout->pp_filters, p_aout->i_nb_filters );
-    aout_FifoDestroy( &p_aout->fifo );
+    var_DelCallback (aout, "stereo-mode", aout_ChannelsRestart, NULL);
+    if (aout->stop != NULL)
+        aout->stop (aout);
 }
 
-/*****************************************************************************
- * aout_OutputPlay : play a buffer
- *****************************************************************************
- * This function is entered with the mixer lock.
- *****************************************************************************/
-void aout_OutputPlay( audio_output_t * p_aout, aout_buffer_t * p_buffer )
+int aout_OutputTimeGet (audio_output_t *aout, mtime_t *delay)
 {
-    vlc_assert_locked( &p_aout->lock );
+    aout_OutputAssertLocked (aout);
 
-    aout_FiltersPlay( p_aout->pp_filters, p_aout->i_nb_filters, &p_buffer );
-    if( !p_buffer )
-        return;
-    if( p_buffer->i_buffer == 0 )
-    {
-        block_Release( p_buffer );
-        return;
-    }
+    if (aout->time_get == NULL)
+        return -1;
+    return aout->time_get (aout, delay);
+}
 
-    aout_FifoPush( &p_aout->fifo, p_buffer );
-    p_aout->pf_play( p_aout );
+/**
+ * Plays a decoded audio buffer.
+ * \note This can only be called after a succesful aout_OutputNew().
+ * \warning The caller must hold the audio output lock.
+ */
+void aout_OutputPlay (audio_output_t *aout, block_t *block)
+{
+    aout_OutputAssertLocked (aout);
+    aout->play (aout, block);
+}
+
+static void PauseDefault (audio_output_t *aout, bool pause, mtime_t date)
+{
+    if (pause)
+        aout_OutputFlush (aout, false);
+    (void) date;
 }
 
 /**
  * Notifies the audio output (if any) of pause/resume events.
  * This enables the output to expedite pause, instead of waiting for its
  * buffers to drain.
+ * \note This can only be called after a succesful aout_OutputNew().
+ * \warning The caller must hold the audio output lock.
  */
 void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
 {
-    vlc_assert_locked( &aout->lock );
+    aout_OutputAssertLocked (aout);
+    ((aout->pause != NULL) ? aout->pause : PauseDefault) (aout, pause, date);
+}
 
-    if( aout->pf_pause != NULL )
-        aout->pf_pause( aout, pause, date );
+/**
+ * Flushes or drains the audio output buffers.
+ * This enables the output to expedite seek and stop.
+ * \param wait if true, wait for buffer playback (i.e. drain),
+ *             if false, discard the buffers immediately (i.e. flush)
+ * \note This can only be called after a succesful aout_OutputNew().
+ * \warning The caller must hold the audio output lock.
+ */
+void aout_OutputFlush( audio_output_t *aout, bool wait )
+{
+    aout_OutputAssertLocked( aout );
+    aout->flush (aout, wait);
 }
 
+static int aout_OutputVolumeSet (audio_output_t *aout, float vol)
+{
+    aout_OutputAssertLocked (aout);
+    return (aout->volume_set != NULL) ? aout->volume_set (aout, vol) : -1;
+}
 
-/*** Volume handling ***/
+static int aout_OutputMuteSet (audio_output_t *aout, bool mute)
+{
+    aout_OutputAssertLocked (aout);
+    return (aout->mute_set != NULL) ? aout->mute_set (aout, mute) : -1;
+}
 
-/**
- * Dummy volume setter. This is the default volume setter.
- */
-static int aout_VolumeNoneSet (audio_output_t *aout, float volume, bool mute)
+static int aout_OutputDeviceSet (audio_output_t *aout, const char *id)
+{
+    aout_OutputAssertLocked (aout);
+    return (aout->device_select != NULL) ? aout->device_select (aout, id) : -1;
+}
+
+void aout_OutputLock (audio_output_t *aout)
 {
-    (void)aout; (void)volume; (void)mute;
-    return -1;
+    aout_owner_t *owner = aout_owner (aout);
+
+    vlc_mutex_lock (&owner->lock);
+}
+
+static int aout_OutputTryLock (audio_output_t *aout)
+{
+    aout_owner_t *owner = aout_owner (aout);
+
+    return vlc_mutex_trylock (&owner->lock);
+}
+
+void aout_OutputUnlock (audio_output_t *aout)
+{
+    aout_owner_t *owner = aout_owner (aout);
+
+    vlc_assert_locked (&owner->lock);
+    vlc_mutex_lock (&owner->req.lock);
+
+    if (owner->req.device != unset_str)
+    {
+        aout_OutputDeviceSet (aout, owner->req.device);
+        free (owner->req.device);
+        owner->req.device = (char *)unset_str;
+    }
+
+    if (owner->req.volume >= 0.f)
+    {
+        aout_OutputVolumeSet (aout, owner->req.volume);
+        owner->req.volume = -1.f;
+    }
+
+    if (owner->req.mute >= 0)
+    {
+        aout_OutputMuteSet (aout, owner->req.mute);
+        owner->req.mute = -1;
+    }
+
+    vlc_mutex_unlock (&owner->lock);
+    /* If another thread is blocked waiting for owner->req.lock at this point,
+     * this aout_OutputUnlock() call will not see and apply its change request.
+     * The other thread will need to apply the change request itself, which
+     * implies it is able to (try-)lock owner->lock. Therefore this thread must
+     * release owner->lock _before_ owner->req.lock. Do not reorder!!! */
+    vlc_mutex_unlock (&owner->req.lock);
 }
 
 /**
- * Configures the dummy volume setter.
- * @note Audio output plugins for which volume is irrelevant
- * should call this function during activation.
+ * Gets the volume of the audio output stream (independent of mute).
+ * \return Current audio volume (0. = silent, 1. = nominal),
+ * or a strictly negative value if undefined.
  */
-void aout_VolumeNoneInit (audio_output_t *aout)
+float aout_VolumeGet (audio_output_t *aout)
 {
-    /* aout_New() -safely- calls this function without the lock, before any
-     * other thread knows of this audio output instance.
-    vlc_assert_locked (&aout->lock); */
-    aout->pf_volume_set = aout_VolumeNoneSet;
+    return var_GetFloat (aout, "volume");
 }
 
 /**
- * Volume setter for software volume.
+ * Sets the volume of the audio output stream.
+ * \note The mute status is not changed.
+ * \return 0 on success, -1 on failure (TODO).
  */
-static int aout_VolumeSoftSet (audio_output_t *aout, float volume, bool mute)
+int aout_VolumeSet (audio_output_t *aout, float vol)
 {
-    vlc_assert_locked (&aout->lock);
+    aout_owner_t *owner = aout_owner (aout);
 
-    /* Cubic mapping from software volume to amplification factor.
-     * This provides a good tradeoff between low and high volume ranges.
-     *
-     * This code is only used for the VLC software mixer. If you change this
-     * formula, be sure to update the aout_VolumeHardInit()-based plugins also.
-     */
-    if (!mute)
-        volume = volume * volume * volume;
-    else
-        volume = 0.;
+    assert (vol >= 0.f);
+    vlc_mutex_lock (&owner->req.lock);
+    owner->req.volume = vol;
+    vlc_mutex_unlock (&owner->req.lock);
 
-    aout->mixer_multiplier = volume;
+    if (aout_OutputTryLock (aout) == 0)
+        aout_OutputUnlock (aout);
     return 0;
 }
 
 /**
- * Configures the volume setter for software mixing
- * and apply the default volume.
- * @note Audio output plugins that cannot apply the volume
- * should call this function during activation.
+ * Gets the audio output stream mute flag.
+ * \return 0 if not muted, 1 if muted, -1 if undefined.
  */
-void aout_VolumeSoftInit (audio_output_t *aout)
+int aout_MuteGet (audio_output_t *aout)
 {
-    audio_volume_t volume = var_InheritInteger (aout, "volume");
-    bool mute = var_InheritBool (aout, "mute");
-
-    vlc_assert_locked (&aout->lock);
-    aout->pf_volume_set = aout_VolumeSoftSet;
-    aout_VolumeSoftSet (aout, volume / (float)AOUT_VOLUME_DEFAULT, mute);
+    return var_InheritBool (aout, "mute");
 }
 
 /**
- * Configures a custom volume setter. This is used by audio outputs that can
- * control the hardware volume directly and/or emulate it internally.
- * @param setter volume setter callback
+ * Sets the audio output stream mute flag.
+ * \return 0 on success, -1 on failure (TODO).
  */
-void aout_VolumeHardInit (audio_output_t *aout, aout_volume_cb setter)
+int aout_MuteSet (audio_output_t *aout, bool mute)
 {
-    vlc_assert_locked (&aout->lock);
-    aout->pf_volume_set = setter;
+    aout_owner_t *owner = aout_owner (aout);
+
+    vlc_mutex_lock (&owner->req.lock);
+    owner->req.mute = mute;
+    vlc_mutex_unlock (&owner->req.lock);
+
+    if (aout_OutputTryLock (aout) == 0)
+        aout_OutputUnlock (aout);
+    return 0;
 }
 
 /**
- * Supply or update the current custom ("hardware") volume.
- * @note This only makes sense after calling aout_VolumeHardInit().
- * @param setter volume setter callback
- * @param volume current custom volume
- * @param mute current mute flag
- * @note Audio output plugins that cannot apply the volume
- * should call this function during activation.
+ * Gets the currently selected device.
+ * \return the selected device ID (caller must free() it)
+ *         NULL if no device is selected or in case of error.
  */
-void aout_VolumeHardSet (audio_output_t *aout, float volume, bool mute)
+char *aout_DeviceGet (audio_output_t *aout)
 {
-#warning FIXME
-    /* REVISIT: This is tricky. We cannot acquire the volume lock as this gets
-     * called from the audio output (it would cause a lock inversion).
-     * We also should not override the input manager volume, but only the
-     * volume of the current audio output... FIXME */
-    msg_Err (aout, "%s(%f, %u)", __func__, volume, (unsigned)mute);
+    return var_GetNonEmptyString (aout, "device");
 }
 
-
-/*** Buffer management ***/
-
-/*****************************************************************************
- * aout_OutputNextBuffer : give the audio output plug-in the right buffer
- *****************************************************************************
- * If b_can_sleek is 1, the aout core functions won't try to resample
- * new buffers to catch up - that is we suppose that the output plug-in can
- * compensate it by itself. S/PDIF outputs should always set b_can_sleek = 1.
- * This function is entered with no lock at all :-).
- *****************************************************************************/
-aout_buffer_t * aout_OutputNextBuffer( audio_output_t * p_aout,
-                                       mtime_t start_date,
-                                       bool b_can_sleek )
+/**
+ * Selects an audio output device.
+ * \param id device ID to select, or NULL for the default device
+ * \return zero on success, non-zero on error (TODO).
+ */
+int aout_DeviceSet (audio_output_t *aout, const char *id)
 {
-    aout_fifo_t *p_fifo = &p_aout->fifo;
-    aout_buffer_t * p_buffer;
-    mtime_t now = mdate();
-
-    aout_lock( p_aout );
+    aout_owner_t *owner = aout_owner (aout);
 
-    /* Drop the audio sample if the audio output is really late.
-     * In the case of b_can_sleek, we don't use a resampler so we need to be
-     * a lot more severe. */
-    while( ((p_buffer = p_fifo->p_first) != NULL)
-     && p_buffer->i_pts < (b_can_sleek ? start_date : now) - AOUT_MAX_PTS_DELAY )
+    char *dev = NULL;
+    if (id != NULL)
     {
-        msg_Dbg( p_aout, "audio output is too slow (%"PRId64"), "
-                 "trashing %"PRId64"us", now - p_buffer->i_pts,
-                 p_buffer->i_length );
-        aout_BufferFree( aout_FifoPop( p_fifo ) );
+        dev = strdup (id);
+        if (unlikely(dev == NULL))
+            return -1;
     }
 
-    if( p_buffer == NULL )
-    {
-#if 0 /* This is bad because the audio output might just be trying to fill
-       * in its internal buffers. And anyway, it's up to the audio output
-       * to deal with this kind of starvation. */
-
-        /* Set date to 0, to allow the mixer to send a new buffer ASAP */
-        aout_FifoReset( &p_aout->fifo );
-        if ( !p_aout->b_starving )
-            msg_Dbg( p_aout,
-                 "audio output is starving (no input), playing silence" );
-        p_aout->b_starving = true;
-#endif
-        goto out;
-    }
+    vlc_mutex_lock (&owner->req.lock);
+    if (owner->req.device != unset_str)
+        free (owner->req.device);
+    owner->req.device = dev;
+    vlc_mutex_unlock (&owner->req.lock);
 
-    mtime_t delta = start_date - p_buffer->i_pts;
-    /* Here we suppose that all buffers have the same duration - this is
-     * generally true, and anyway if it's wrong it won't be a disaster.
-     */
-    if ( 0 > delta + p_buffer->i_length )
-    {
-        if ( !p_aout->b_starving )
-            msg_Dbg( p_aout, "audio output is starving (%"PRId64"), "
-                     "playing silence", -delta );
-        p_aout->b_starving = true;
-        p_buffer = NULL;
-        goto out;
-    }
-
-    p_aout->b_starving = false;
-    p_buffer = aout_FifoPop( p_fifo );
+    if (aout_OutputTryLock (aout) == 0)
+        aout_OutputUnlock (aout);
+    return 0;
+}
 
-    if( !b_can_sleek
-     && ( delta > AOUT_MAX_PTS_DELAY || delta < -AOUT_MAX_PTS_ADVANCE ) )
+/**
+ * Enumerates possible audio output devices.
+ *
+ * The function will heap-allocate two tables of heap-allocated strings;
+ * the caller is responsible for freeing all strings and both tables.
+ *
+ * \param ids pointer to a table of device identifiers [OUT]
+ * \param names pointer to a table of device human-readable descriptions [OUT]
+ * \return the number of devices, or negative on error.
+ * \note In case of error, *ids and *names are undefined.
+ */
+int aout_DevicesList (audio_output_t *aout, char ***ids, char ***names)
+{
+    aout_owner_t *owner = aout_owner (aout);
+    char **tabid, **tabname;
+    unsigned count;
+
+    vlc_mutex_lock (&owner->dev.lock);
+    count = owner->dev.count;
+    tabid = xmalloc (sizeof (*tabid) * count);
+    tabname = xmalloc (sizeof (*tabname) * count);
+    *ids = tabid;
+    *names = tabname;
+    for (aout_dev_t *dev = owner->dev.list; dev != NULL; dev = dev->next)
     {
-        /* Try to compensate the drift by doing some resampling. */
-        msg_Warn( p_aout, "output date isn't PTS date, requesting "
-                  "resampling (%"PRId64")", delta );
-
-        aout_FifoMoveDates( &p_aout->p_input->fifo, delta );
-        aout_FifoMoveDates( p_fifo, delta );
+        *(tabid++) = xstrdup (dev->id);
+        *(tabname++) = xstrdup (dev->name);
     }
-out:
-    aout_unlock( p_aout );
-    return p_buffer;
+    vlc_mutex_unlock (&owner->dev.lock);
+
+    return count;
 }