]> 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 aa135793a03ac8fa07394fae0d5ac7d6f6e11dda..18fee051641406e65e4e5aac0aacb41e7d31e1b0 100644 (file)
  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-/*****************************************************************************
- * Preamble
- *****************************************************************************/
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 
-#include <math.h>
-
+#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_owner_t *owner = aout_owner (p_aout);
+    aout_dev_t *next;
+    char *name;
+    char id[1];
+};
 
-    aout_assert_locked( p_aout );
-    p_aout->format = *p_format;
 
-    aout_FormatPrepare( &p_aout->format );
+/* Local functions */
+static void aout_OutputAssertLocked (audio_output_t *aout)
+{
+    aout_owner_t *owner = aout_owner (aout);
 
-    /* Find the best output plug-in. */
-    owner->module = module_need (p_aout, "audio output", "$aout", false);
-    if (owner->module == NULL)
+    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_object_t *dst = data;
+
+    (void) src; (void) prev;
+    return var_Set (dst, name, value);
+}
+
+/**
+ * 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_TriggerCallback( p_aout, "intf-change" );
 
-    aout_FormatPrepare( &p_aout->format );
-    aout_FormatPrint( p_aout, "output", &p_aout->format );
+    var_Create (aout, "equalizer-preamp", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
+    var_Create (aout, "equalizer-bands", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
 
-    /* Choose the mixer format. */
-    owner->mixer_format = p_aout->format;
-    if (!AOUT_FMT_LINEAR(&p_aout->format))
-        owner->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 )
-        owner->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 || !AOUT_FMT_LINEAR(p_format))
-        owner->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. */
-        owner->mixer_format.i_format = VLC_CODEC_S16N;
-
-    aout_FormatPrepare (&owner->mixer_format);
-    aout_FormatPrint (p_aout, "mixer", &owner->mixer_format);
-
-    /* Create filters. */
-    owner->nb_filters = 0;
-    if (aout_FiltersCreatePipeline (p_aout, owner->filters,
-                                    &owner->nb_filters, &owner->mixer_format,
-                                    &p_aout->format) < 0)
-    {
-        msg_Err( p_aout, "couldn't create audio output pipeline" );
-        module_unneed (p_aout, owner->module);
-        owner->module = NULL;
-        return -1;
-    }
-    return 0;
+    return aout;
 }
 
 /**
- * Destroys the audio output plug-in instance.
+ * Deinitializes an audio output module and destroys an audio output object.
  */
-void aout_OutputDelete (audio_output_t *aout)
+void aout_Destroy (audio_output_t *aout)
 {
     aout_owner_t *owner = aout_owner (aout);
 
-    aout_assert_locked (aout);
-
-    if (owner->module == NULL)
-        return;
-
+    aout_OutputLock (aout);
     module_unneed (aout, owner->module);
-    /* Clear callbacks */
-    aout->pf_play = aout_DecDeleteBuffer; /* gruik */
-    aout->pf_pause = NULL;
-    aout->pf_flush = NULL;
-    aout_VolumeNoneInit (aout);
-    owner->module = NULL;
-    aout_FiltersDestroyPipeline (owner->filters, owner->nb_filters);
+    /* 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);
 }
 
 /**
- * Plays a decoded audio buffer.
+ * Destroys the audio output lock used (asynchronously) by interface functions.
  */
-void aout_OutputPlay (audio_output_t *aout, block_t *block)
+static void aout_Destructor (vlc_object_t *obj)
 {
+    audio_output_t *aout = (audio_output_t *)obj;
     aout_owner_t *owner = aout_owner (aout);
 
-    aout_assert_locked (aout);
-
-    aout_FiltersPlay (owner->filters, owner->nb_filters, &block);
-    if (block == NULL)
-        return;
-    if (block->i_buffer == 0)
+    vlc_mutex_destroy (&owner->dev.lock);
+    for (aout_dev_t *dev = owner->dev.list, *next; dev != NULL; dev = next)
     {
-        block_Release (block);
-        return;
+        next = dev->next;
+        free (dev->name);
+        free (dev);
     }
 
-    aout->pf_play (aout, block);
+    assert (owner->req.device == unset_str);
+    vlc_mutex_destroy (&owner->req.lock);
+    vlc_mutex_destroy (&owner->lock);
 }
 
 /**
- * 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.
+ * Starts an audio output stream.
+ * \param fmt audio output stream format [IN/OUT]
+ * \warning The caller must hold the audio output lock.
  */
-void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
+int aout_OutputNew (audio_output_t *aout, audio_sample_format_t *restrict fmt)
 {
-    aout_assert_locked( aout );
-    if( aout->pf_pause != NULL )
-        aout->pf_pause( aout, pause, date );
-}
+    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);
+    }
 
-/**
- * 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)
- */
-void aout_OutputFlush( audio_output_t *aout, bool wait )
-{
-    aout_assert_locked( aout );
+    if (aout->start (aout, fmt))
+    {
+        msg_Err (aout, "module not functional");
+        return -1;
+    }
 
-    if( aout->pf_flush != NULL )
-        aout->pf_flush( aout, wait );
-}
+    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);
+    }
 
-/*** Volume handling ***/
+    /* 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);
+        }
+    }
 
-/**
- * Dummy volume setter. This is the default volume setter.
- */
-static int aout_VolumeNoneSet (audio_output_t *aout, float volume, bool mute)
-{
-    (void)aout; (void)volume; (void)mute;
-    return -1;
+    aout_FormatPrepare (fmt);
+    aout_FormatPrint (aout, "output", fmt);
+    return 0;
 }
 
 /**
- * Configures the dummy volume setter.
- * @note Audio output plugins for which volume is irrelevant
- * should call this function during activation.
+ * 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_VolumeNoneInit (audio_output_t *aout)
+void aout_OutputDelete (audio_output_t *aout)
 {
-    /* aout_New() -safely- calls this function without the lock, before any
-     * other thread knows of this audio output instance.
-    aout_assert_locked (aout); */
-    aout->pf_volume_set = aout_VolumeNoneSet;
-    var_Destroy (aout, "volume");
-    var_Destroy (aout, "mute");
+    aout_OutputAssertLocked (aout);
+
+    var_DelCallback (aout, "stereo-mode", aout_ChannelsRestart, NULL);
+    if (aout->stop != NULL)
+        aout->stop (aout);
 }
 
-/**
- * Volume setter for software volume.
- */
-static int aout_VolumeSoftSet (audio_output_t *aout, float volume, bool mute)
+int aout_OutputTimeGet (audio_output_t *aout, mtime_t *delay)
 {
-    aout_owner_t *owner = aout_owner (aout);
-
-    aout_assert_locked (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.;
+    aout_OutputAssertLocked (aout);
 
-    owner->volume.multiplier = volume;
-    return 0;
+    if (aout->time_get == NULL)
+        return -1;
+    return aout->time_get (aout, delay);
 }
 
 /**
- * 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.
+ * 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_VolumeSoftInit (audio_output_t *aout)
+void aout_OutputPlay (audio_output_t *aout, block_t *block)
 {
-    audio_volume_t volume = var_InheritInteger (aout, "volume");
-    bool mute = var_InheritBool (aout, "mute");
+    aout_OutputAssertLocked (aout);
+    aout->play (aout, block);
+}
 
-    aout_assert_locked (aout);
-    aout->pf_volume_set = aout_VolumeSoftSet;
-    aout_VolumeSoftSet (aout, volume / (float)AOUT_VOLUME_DEFAULT, mute);
+static void PauseDefault (audio_output_t *aout, bool pause, mtime_t date)
+{
+    if (pause)
+        aout_OutputFlush (aout, false);
+    (void) date;
 }
 
 /**
- * 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
+ * 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_VolumeHardInit (audio_output_t *aout, aout_volume_cb setter)
+void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
 {
-    aout_assert_locked (aout);
-    aout->pf_volume_set = setter;
-    var_Create (aout, "volume", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
-    var_Create (aout, "mute", VLC_VAR_BOOL|VLC_VAR_DOINHERIT);
+    aout_OutputAssertLocked (aout);
+    ((aout->pause != NULL) ? aout->pause : PauseDefault) (aout, pause, date);
 }
 
 /**
- * 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
- *
- * @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.pf_volume_set callback. This ensures that VLC gets correct
- * volume information (possibly with a latency).
+ * 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_VolumeHardSet (audio_output_t *aout, float volume, bool mute)
+void aout_OutputFlush( audio_output_t *aout, bool wait )
 {
-    audio_volume_t vol = lroundf (volume * (float)AOUT_VOLUME_DEFAULT);
-
-    /* We cannot acquire the volume lock as this gets called from the audio
-     * output plug-in (it would cause a lock inversion). */
-    var_SetInteger (aout, "volume", vol);
-    var_SetBool (aout, "mute", mute);
-    var_TriggerCallback (aout, "intf-change");
+    aout_OutputAssertLocked( aout );
+    aout->flush (aout, wait);
 }
 
-
-/*** Packet-oriented audio output support ***/
-
-static inline aout_packet_t *aout_packet (audio_output_t *aout)
+static int aout_OutputVolumeSet (audio_output_t *aout, float vol)
 {
-    return (aout_packet_t *)(aout->sys);
+    aout_OutputAssertLocked (aout);
+    return (aout->volume_set != NULL) ? aout->volume_set (aout, vol) : -1;
 }
 
-void aout_PacketInit (audio_output_t *aout, aout_packet_t *p, unsigned samples)
+static int aout_OutputMuteSet (audio_output_t *aout, bool mute)
 {
-    assert (p == aout_packet (aout));
-
-    vlc_mutex_init (&p->lock);
-    aout_FifoInit (aout, &p->partial, aout->format.i_rate);
-    aout_FifoInit (aout, &p->fifo, aout->format.i_rate);
-    p->pause_date = VLC_TS_INVALID;
-    p->time_report = VLC_TS_INVALID;
-    p->samples = samples;
-    p->starving = true;
+    aout_OutputAssertLocked (aout);
+    return (aout->mute_set != NULL) ? aout->mute_set (aout, mute) : -1;
 }
 
-void aout_PacketDestroy (audio_output_t *aout)
+static int aout_OutputDeviceSet (audio_output_t *aout, const char *id)
 {
-    aout_packet_t *p = aout_packet (aout);
-
-    aout_FifoDestroy (&p->partial);
-    aout_FifoDestroy (&p->fifo);
-    vlc_mutex_destroy (&p->lock);
+    aout_OutputAssertLocked (aout);
+    return (aout->device_select != NULL) ? aout->device_select (aout, id) : -1;
 }
 
-static block_t *aout_OutputSlice (audio_output_t *);
-
-void aout_PacketPlay (audio_output_t *aout, block_t *block)
+void aout_OutputLock (audio_output_t *aout)
 {
-    aout_packet_t *p = aout_packet (aout);
-    mtime_t time_report;
+    aout_owner_t *owner = aout_owner (aout);
 
-    vlc_mutex_lock (&p->lock);
-    aout_FifoPush (&p->partial, block);
-    while ((block = aout_OutputSlice (aout)) != NULL)
-        aout_FifoPush (&p->fifo, block);
+    vlc_mutex_lock (&owner->lock);
+}
 
-    time_report = p->time_report;
-    p->time_report = VLC_TS_INVALID;
-    vlc_mutex_unlock (&p->lock);
+static int aout_OutputTryLock (audio_output_t *aout)
+{
+    aout_owner_t *owner = aout_owner (aout);
 
-    if (time_report != VLC_TS_INVALID)
-        aout_TimeReport (aout, mdate () - time_report);
+    return vlc_mutex_trylock (&owner->lock);
 }
 
-void aout_PacketPause (audio_output_t *aout, bool pause, mtime_t date)
+void aout_OutputUnlock (audio_output_t *aout)
 {
-    aout_packet_t *p = aout_packet (aout);
+    aout_owner_t *owner = aout_owner (aout);
 
-    if (pause)
+    vlc_assert_locked (&owner->lock);
+    vlc_mutex_lock (&owner->req.lock);
+
+    if (owner->req.device != unset_str)
     {
-        assert (p->pause_date == VLC_TS_INVALID);
-        p->pause_date = date;
+        aout_OutputDeviceSet (aout, owner->req.device);
+        free (owner->req.device);
+        owner->req.device = (char *)unset_str;
     }
-    else
-    {
-        assert (p->pause_date != VLC_TS_INVALID);
 
-        mtime_t duration = date - p->pause_date;
+    if (owner->req.volume >= 0.f)
+    {
+        aout_OutputVolumeSet (aout, owner->req.volume);
+        owner->req.volume = -1.f;
+    }
 
-        p->pause_date = VLC_TS_INVALID;
-        vlc_mutex_lock (&p->lock);
-        aout_FifoMoveDates (&p->partial, duration);
-        aout_FifoMoveDates (&p->fifo, duration);
-        vlc_mutex_unlock (&p->lock);
+    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);
 }
 
-void aout_PacketFlush (audio_output_t *aout, bool drain)
+/**
+ * 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.
+ */
+float aout_VolumeGet (audio_output_t *aout)
 {
-    aout_packet_t *p = aout_packet (aout);
-
-    vlc_mutex_lock (&p->lock);
-    aout_FifoReset (&p->partial);
-    aout_FifoReset (&p->fifo);
-    vlc_mutex_unlock (&p->lock);
-
-    (void) drain; /* TODO */
+    return var_GetFloat (aout, "volume");
 }
 
-
 /**
- * Rearranges audio blocks in correct number of samples.
- * @note (FIXME) This is left here for historical reasons. It belongs in the
- * output code. Besides, this operation should be avoided if possible.
+ * Sets the volume of the audio output stream.
+ * \note The mute status is not changed.
+ * \return 0 on success, -1 on failure (TODO).
  */
-static block_t *aout_OutputSlice (audio_output_t *p_aout)
+int aout_VolumeSet (audio_output_t *aout, float vol)
 {
-    aout_packet_t *p = aout_packet (p_aout);
-    aout_fifo_t *p_fifo = &p->partial;
-    const unsigned samples = p->samples;
-    assert( samples > 0 );
-
-    vlc_assert_locked( &p->lock );
-
-    /* Retrieve the date of the next buffer. */
-    date_t exact_start_date = p->fifo.end_date;
-    mtime_t start_date = date_Get( &exact_start_date );
-
-    /* See if we have enough data to prepare a new buffer for the audio output. */
-    aout_buffer_t *p_buffer = p_fifo->p_first;
-    if( p_buffer == NULL )
-        return NULL;
-
-    /* Find the earliest start date available. */
-    if ( start_date == VLC_TS_INVALID )
-    {
-        start_date = p_buffer->i_pts;
-        date_Set( &exact_start_date, start_date );
-    }
-    /* Compute the end date for the new buffer. */
-    mtime_t end_date = date_Increment( &exact_start_date, samples );
-
-    /* Check that we have enough samples (TODO merge with next loop). */
-    for( unsigned available = 0; available < samples; )
-    {
-        p_buffer = p_buffer->p_next;
-        if( p_buffer == NULL )
-            return NULL;
-
-        available += p_buffer->i_nb_samples;
-    }
-
-    if( AOUT_FMT_LINEAR( &p_aout->format ) )
-    {
-        const unsigned framesize = p_aout->format.i_bytes_per_frame;
-        /* Build packet with adequate number of samples */
-        unsigned needed = samples * framesize;
+    aout_owner_t *owner = aout_owner (aout);
 
-        p_buffer = block_Alloc( needed );
-        if( unlikely(p_buffer == NULL) )
-            /* XXX: should free input buffers */
-            return NULL;
-        p_buffer->i_nb_samples = samples;
+    assert (vol >= 0.f);
+    vlc_mutex_lock (&owner->req.lock);
+    owner->req.volume = vol;
+    vlc_mutex_unlock (&owner->req.lock);
 
-        for( uint8_t *p_out = p_buffer->p_buffer; needed > 0; )
-        {
-            aout_buffer_t *p_inbuf = p_fifo->p_first;
-            if( unlikely(p_inbuf == NULL) )
-            {
-                msg_Err( p_aout, "packetization error" );
-                vlc_memset( p_out, 0, needed );
-                break;
-            }
+    if (aout_OutputTryLock (aout) == 0)
+        aout_OutputUnlock (aout);
+    return 0;
+}
 
-            const uint8_t *p_in = p_inbuf->p_buffer;
-            size_t avail = p_inbuf->i_nb_samples * framesize;
-            if( avail > needed )
-            {
-                vlc_memcpy( p_out, p_in, needed );
-                p_fifo->p_first->p_buffer += needed;
-                p_fifo->p_first->i_buffer -= needed;
-                needed /= framesize;
-                p_fifo->p_first->i_nb_samples -= needed;
-
-                mtime_t t = needed * CLOCK_FREQ / p_aout->format.i_rate;
-                p_fifo->p_first->i_pts += t;
-                p_fifo->p_first->i_length -= t;
-                break;
-            }
+/**
+ * Gets the audio output stream mute flag.
+ * \return 0 if not muted, 1 if muted, -1 if undefined.
+ */
+int aout_MuteGet (audio_output_t *aout)
+{
+    return var_InheritBool (aout, "mute");
+}
 
-            vlc_memcpy( p_out, p_in, avail );
-            needed -= avail;
-            p_out += avail;
-            /* Next buffer */
-            aout_BufferFree( aout_FifoPop( p_fifo ) );
-        }
-    }
-    else
-        p_buffer = aout_FifoPop( p_fifo );
+/**
+ * Sets the audio output stream mute flag.
+ * \return 0 on success, -1 on failure (TODO).
+ */
+int aout_MuteSet (audio_output_t *aout, bool mute)
+{
+    aout_owner_t *owner = aout_owner (aout);
 
-    p_buffer->i_pts = start_date;
-    p_buffer->i_length = end_date - start_date;
+    vlc_mutex_lock (&owner->req.lock);
+    owner->req.mute = mute;
+    vlc_mutex_unlock (&owner->req.lock);
 
-    return p_buffer;
+    if (aout_OutputTryLock (aout) == 0)
+        aout_OutputUnlock (aout);
+    return 0;
 }
 
 /**
- * Dequeues the next audio packet (a.k.a. audio fragment).
- * The audio output plugin must first call aout_PacketPlay() to queue the
- * decoded audio samples. Typically, audio_output_t.pf_play is set to, or calls
- * aout_PacketPlay().
- * @note This function is considered legacy. Please do not use this function in
- * new audio output plugins.
- * @param p_aout audio output instance
- * @param start_date expected PTS of the audio packet
+ * 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.
  */
-block_t *aout_PacketNext (audio_output_t *p_aout, mtime_t start_date)
+char *aout_DeviceGet (audio_output_t *aout)
 {
-    aout_packet_t *p = aout_packet (p_aout);
-    aout_fifo_t *p_fifo = &p->fifo;
-    block_t *p_buffer;
-    const bool b_can_sleek = !AOUT_FMT_LINEAR(&p_aout->format);
-    const mtime_t now = mdate ();
-    const mtime_t threshold =
-        (b_can_sleek ? start_date : now) - AOUT_MAX_PTS_DELAY;
+    return var_GetNonEmptyString (aout, "device");
+}
 
-    vlc_mutex_lock( &p->lock );
-    if( p->pause_date != VLC_TS_INVALID )
-        goto out; /* paused: do not dequeue buffers */
+/**
+ * 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_owner_t *owner = aout_owner (aout);
 
-    for (;;)
+    char *dev = NULL;
+    if (id != NULL)
     {
-        p_buffer = p_fifo->p_first;
-        if (p_buffer == NULL)
-            goto out; /* nothing to play */
-
-        if (p_buffer->i_pts >= threshold)
-            break;
-
-        /* 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. */
-        msg_Dbg (p_aout, "audio output is too slow (%"PRId64" us): "
-                 " trashing %"PRId64" us", threshold - p_buffer->i_pts,
-                 p_buffer->i_length);
-        block_Release (aout_FifoPop (p_fifo));
+        dev = strdup (id);
+        if (unlikely(dev == NULL))
+            return -1;
     }
 
-    mtime_t delta = start_date - p_buffer->i_pts;
-    /* This assumes that all buffers have the same duration. This is true
-     * since aout_PacketPlay() (aout_OutputSlice()) is used. */
-    if (0 >= delta + p_buffer->i_length)
-    {
-        if (!p->starving)
-        {
-            msg_Dbg (p_aout, "audio output is starving (%"PRId64"), "
-                     "playing silence", delta);
-            p->starving = true;
-        }
-        goto out; /* nothing to play _yet_ */
-    }
+    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);
 
-    p->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_ADVANCE || AOUT_MAX_PTS_DELAY < delta))
+/**
+ * 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)
     {
-        msg_Warn (p_aout, "audio output out of sync, "
-                          "adjusting dates (%"PRId64" us)", delta);
-        aout_FifoMoveDates (&p->partial, delta);
-        aout_FifoMoveDates (p_fifo, delta);
-        p->time_report = delta;
+        *(tabid++) = xstrdup (dev->id);
+        *(tabname++) = xstrdup (dev->name);
     }
-    vlc_mutex_unlock( &p->lock );
-    return p_buffer;
-out:
-    vlc_mutex_unlock( &p->lock );
-    return NULL;
+    vlc_mutex_unlock (&owner->dev.lock);
+
+    return count;
 }