]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/alsa.c
PulseAudio: remove the base volume hack
[vlc] / modules / audio_output / alsa.c
index 45d4945fbd03dcecf0c14921a02eb13b24c3c96b..1aafeb646fda6aa1531e6cbaa0e1701d260e3c61 100644 (file)
@@ -44,14 +44,16 @@ struct aout_sys_t
 {
     snd_pcm_t *pcm;
     unsigned rate; /**< Sample rate */
+    vlc_fourcc_t format; /**< Sample format */
     uint8_t chans_table[AOUT_CHAN_MAX]; /**< Channels order table */
     uint8_t chans_to_reorder; /**< Number of channels to reorder */
-    uint8_t bits; /**< Bits per sample per channel */
+
     bool soft_mute;
     float soft_gain;
+    char *device;
 };
 
-#include "volume.h"
+#include "audio_output/volume.h"
 
 #define A52_FRAME_NB 1536
 
@@ -143,24 +145,6 @@ static void DumpDeviceStatus (vlc_object_t *obj, snd_pcm_t *pcm)
 }
 #define DumpDeviceStatus(o, p) DumpDeviceStatus(VLC_OBJECT(o), p)
 
-static unsigned SetupChannelsUnknown (vlc_object_t *obj,
-                                      uint16_t *restrict mask)
-{
-    uint16_t map = var_InheritInteger (obj, "alsa-audio-channels");
-    uint16_t chans = *mask & map;
-
-    if (unlikely(chans == 0)) /* WTH? */
-        chans = AOUT_CHANS_STEREO;
-
-    if (popcount (chans) < popcount (*mask))
-        msg_Dbg (obj, "downmixing from %u to %u channels",
-                 popcount (*mask), popcount (chans));
-    else
-        msg_Dbg (obj, "keeping %u channels", popcount (chans));
-    *mask = chans;
-    return 0;
-}
-
 #if (SND_LIB_VERSION >= 0x01001B)
 static const uint16_t vlc_chans[] = {
     [SND_CHMAP_MONO] = AOUT_CHAN_CENTER,
@@ -205,37 +189,38 @@ static int Map2Mask (vlc_object_t *obj, const snd_pcm_chmap_t *restrict map)
  * Compares a fixed ALSA channels map with the VLC channels order.
  */
 static unsigned SetupChannelsFixed(const snd_pcm_chmap_t *restrict map,
-                                uint16_t *restrict mask, uint8_t *restrict tab)
+                               uint16_t *restrict maskp, uint8_t *restrict tab)
 {
     uint32_t chans_out[AOUT_CHAN_MAX];
+    uint16_t mask = 0;
 
     for (unsigned i = 0; i < map->channels; i++)
     {
         uint_fast16_t vlc_chan = vlc_chans[map->pos[i]];
 
         chans_out[i] = vlc_chan;
-        *mask |= vlc_chan;
+        mask |= vlc_chan;
     }
 
-    return aout_CheckChannelReorder(NULL, chans_out, *mask, tab);
+    *maskp = mask;
+    return aout_CheckChannelReorder(NULL, chans_out, mask, tab);
 }
 
 /**
  * Negotiate channels mapping.
  */
 static unsigned SetupChannels (vlc_object_t *obj, snd_pcm_t *pcm,
-                                uint16_t *restrict mask, uint8_t *restrict tab)
+                               uint16_t *restrict mask, uint8_t *restrict tab)
 {
     snd_pcm_chmap_query_t **maps = snd_pcm_query_chmaps (pcm);
-    if (tab == NULL)
-    {   /* Fallback to manual configuration */
+    if (maps == NULL)
+    {   /* Fallback to default order if unknown */
         msg_Dbg(obj, "channels map not provided");
-        return SetupChannelsUnknown (obj, mask);
+        return 0;
     }
 
     /* Find most appropriate available channels map */
-    unsigned best_offset;
-    unsigned best_score = 0;
+    unsigned best_offset, best_score = 0, to_reorder = 0;
 
     for (snd_pcm_chmap_query_t *const *p = maps; *p != NULL; p++)
     {
@@ -256,7 +241,8 @@ static unsigned SetupChannels (vlc_object_t *obj, snd_pcm_t *pcm,
         if (chans == -1)
             continue;
 
-        unsigned score = popcount (chans & *mask);
+        unsigned score = (popcount (chans & *mask) << 8)
+                       | (255 - popcount (chans));
         if (score > best_score)
         {
             best_offset = p - maps;
@@ -267,26 +253,24 @@ static unsigned SetupChannels (vlc_object_t *obj, snd_pcm_t *pcm,
     if (best_score == 0)
     {
         msg_Err (obj, "cannot find supported channels map");
-        snd_pcm_free_chmaps (maps);
-        return SetupChannelsUnknown (obj, mask);
+        goto out;
     }
 
     const snd_pcm_chmap_t *map = &maps[best_offset]->map;
     msg_Dbg (obj, "using channels map %u, type %u, %u channel(s)", best_offset,
-             maps[best_offset]->type, best_score);
+             maps[best_offset]->type, map->channels);
 
     /* Setup channels map */
-    unsigned to_reorder = SetupChannelsFixed(map, mask, tab);
+    to_reorder = SetupChannelsFixed(map, mask, tab);
 
     /* TODO: avoid reordering for PAIRED and VAR types */
     //snd_pcm_set_chmap (pcm, ...)
-
+out:
     snd_pcm_free_chmaps (maps);
     return to_reorder;
 }
 #else /* (SND_LIB_VERSION < 0x01001B) */
-# define SetupChannels(obj, pcm, mask, tab) \
-         SetupChannelsUnknown(obj, mask)
+# define SetupChannels(obj, pcm, mask, tab) (0)
 #endif
 
 static int TimeGet (audio_output_t *aout, mtime_t *);
@@ -299,61 +283,22 @@ static void Flush (audio_output_t *, bool);
 static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
 {
     aout_sys_t *sys = aout->sys;
-
-    /* Get device name */
-    char *device = var_InheritString (aout, "alsa-audio-device");
-    if (unlikely(device == NULL))
-        return VLC_ENOMEM;
-
     snd_pcm_format_t pcm_format; /* ALSA sample format */
     bool spdif = false;
 
     switch (fmt->i_format)
     {
-        case VLC_CODEC_F64B:
-            pcm_format = SND_PCM_FORMAT_FLOAT64_BE;
-            break;
-        case VLC_CODEC_F64L:
-            pcm_format = SND_PCM_FORMAT_FLOAT64_LE;
-            break;
-        case VLC_CODEC_F32B:
-            pcm_format = SND_PCM_FORMAT_FLOAT_BE;
+        case VLC_CODEC_FL64:
+            pcm_format = SND_PCM_FORMAT_FLOAT64;
             break;
-        case VLC_CODEC_F32L:
-            pcm_format = SND_PCM_FORMAT_FLOAT_LE;
+        case VLC_CODEC_FL32:
+            pcm_format = SND_PCM_FORMAT_FLOAT;
             break;
-        case VLC_CODEC_S32B:
-            pcm_format = SND_PCM_FORMAT_S32_BE;
+        case VLC_CODEC_S32N:
+            pcm_format = SND_PCM_FORMAT_S32;
             break;
-        case VLC_CODEC_S32L:
-            pcm_format = SND_PCM_FORMAT_S32_LE;
-            break;
-        case VLC_CODEC_S24B:
-            pcm_format = SND_PCM_FORMAT_S24_3BE;
-            break;
-        case VLC_CODEC_S24L:
-            pcm_format = SND_PCM_FORMAT_S24_3LE;
-            break;
-        case VLC_CODEC_U24B:
-            pcm_format = SND_PCM_FORMAT_U24_3BE;
-            break;
-        case VLC_CODEC_U24L:
-            pcm_format = SND_PCM_FORMAT_U24_3LE;
-            break;
-        case VLC_CODEC_S16B:
-            pcm_format = SND_PCM_FORMAT_S16_BE;
-            break;
-        case VLC_CODEC_S16L:
-            pcm_format = SND_PCM_FORMAT_S16_LE;
-            break;
-        case VLC_CODEC_U16B:
-            pcm_format = SND_PCM_FORMAT_U16_BE;
-            break;
-        case VLC_CODEC_U16L:
-            pcm_format = SND_PCM_FORMAT_U16_LE;
-            break;
-        case VLC_CODEC_S8:
-            pcm_format = SND_PCM_FORMAT_S8;
+        case VLC_CODEC_S16N:
+            pcm_format = SND_PCM_FORMAT_S16;
             break;
         case VLC_CODEC_U8:
             pcm_format = SND_PCM_FORMAT_U8;
@@ -379,10 +324,10 @@ static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
             }
     }
 
-    /* Choose the IEC device for S/PDIF output:
-       if the device is overridden by the user then it will be the one.
-       Otherwise we compute the default device based on the output format. */
-    if (spdif && !strcmp (device, "default"))
+    const char *device = sys->device;
+    char *devbuf = NULL;
+    /* Choose the IEC device for S/PDIF output */
+    if (spdif)
     {
         unsigned aes3;
 
@@ -400,13 +345,28 @@ static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
                 break;
         }
 
-        free (device);
-        if (asprintf (&device,
-                      "iec958:AES0=0x%x,AES1=0x%x,AES2=0x%x,AES3=0x%x",
+        char *opt = NULL;
+        if (!strcmp (device, "default"))
+            device = "iec958"; /* TODO: hdmi */
+        else
+        {
+            opt = strchr(device, ':');
+            if (opt && opt[1] == '\0') {
+                /* if device is terminated by : but there's no options,
+                 * remove ':', we'll add it back in the format string. */
+                *opt = '\0';
+                opt = NULL;
+            }
+        }
+
+        if (asprintf (&devbuf,
+                      "%s%cAES0=0x%x,AES1=0x%x,AES2=0x%x,AES3=0x%x", device,
+                      opt ? ',' : ':',
                       IEC958_AES0_CON_EMPHASIS_NONE | IEC958_AES0_NONAUDIO,
                       IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER,
                       0, aes3) == -1)
             return VLC_ENOMEM;
+        device = devbuf;
     }
 
     /* Open the device */
@@ -415,22 +375,20 @@ static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
     const int mode = SND_PCM_NO_AUTO_RESAMPLE;
 
     int val = snd_pcm_open (&pcm, device, SND_PCM_STREAM_PLAYBACK, mode);
+    free (devbuf);
     if (val != 0)
     {
-        msg_Err (aout, "cannot open ALSA device \"%s\": %s", device,
+        msg_Err (aout, "cannot open ALSA device \"%s\": %s", sys->device,
                  snd_strerror (val));
         dialog_Fatal (aout, _("Audio output failed"),
                       _("The audio device \"%s\" could not be used:\n%s."),
-                      device, snd_strerror (val));
-        free (device);
+                      sys->device, snd_strerror (val));
         return VLC_EGENERIC;
     }
     sys->pcm = pcm;
 
     /* Print some potentially useful debug */
-    msg_Dbg (aout, "using ALSA device: %s", device);
-    aout_DeviceReport (aout, device);
-    free (device);
+    msg_Dbg (aout, "using ALSA device: %s", sys->device);
     DumpDevice (VLC_OBJECT(aout), pcm);
 
     /* Get Initial hardware parameters */
@@ -494,13 +452,19 @@ static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
     unsigned channels;
     if (!spdif)
     {
-        sys->chans_to_reorder = SetupChannels (VLC_OBJECT(aout), pcm,
-                                  &fmt->i_physical_channels, sys->chans_table);
-        channels = popcount (fmt->i_physical_channels);
+        uint16_t map = var_InheritInteger (aout, "alsa-audio-channels");
+
+        sys->chans_to_reorder = SetupChannels (VLC_OBJECT(aout), pcm, &map,
+                                               sys->chans_table);
+        fmt->i_physical_channels = map;
+        fmt->i_original_channels = map;
+        channels = popcount (map);
     }
     else
+    {
+        sys->chans_to_reorder = 0;
         channels = 2;
-    fmt->i_original_channels = fmt->i_physical_channels;
+    }
 
     /* By default, ALSA plug will pad missing channels with zeroes, which is
      * usually fine. However, it will also discard extraneous channels, which
@@ -523,6 +487,15 @@ static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
     }
     sys->rate = fmt->i_rate;
 
+#if 1 /* work-around for period-long latency outputs (e.g. PulseAudio): */
+    param = AOUT_MIN_PREPARE_TIME;
+    val = snd_pcm_hw_params_set_period_time_near (pcm, hw, &param, NULL);
+    if (val)
+    {
+        msg_Err (aout, "cannot set period: %s", snd_strerror (val));
+        goto error;
+    }
+#endif
     /* Set buffer size */
     param = AOUT_MAX_ADVANCE_TIME;
     val = snd_pcm_hw_params_set_buffer_time_near (pcm, hw, &param, NULL);
@@ -540,15 +513,13 @@ static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
     }
     else
         param /= 2;
-#else /* work-around for period-long latency outputs (e.g. PulseAudio): */
-    param = AOUT_MIN_PREPARE_TIME;
-#endif
     val = snd_pcm_hw_params_set_period_time_near (pcm, hw, &param, NULL);
     if (val)
     {
         msg_Err (aout, "cannot set period: %s", snd_strerror (val));
         goto error;
     }
+#endif
 
     /* Commit hardware parameters */
     val = snd_pcm_hw_params (pcm, hw);
@@ -602,11 +573,7 @@ static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
         fmt->i_bytes_per_frame = AOUT_SPDIF_SIZE;
         fmt->i_frame_length = A52_FRAME_NB;
     }
-    else
-    {
-        aout_FormatPrepare (fmt);
-        sys->bits = fmt->i_bitspersample;
-    }
+    sys->format = fmt->i_format;
 
     aout->time_get = TimeGet;
     aout->play = Play;
@@ -623,7 +590,6 @@ static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
 
 error:
     snd_pcm_close (pcm);
-    free (device);
     return VLC_EGENERIC;
 }
 
@@ -651,7 +617,7 @@ static void Play (audio_output_t *aout, block_t *block)
 
     if (sys->chans_to_reorder != 0)
         aout_ChannelReorder(block->p_buffer, block->i_buffer,
-                           sys->chans_to_reorder, sys->chans_table, sys->bits);
+                           sys->chans_to_reorder, sys->chans_table, sys->format);
 
     snd_pcm_t *pcm = sys->pcm;
 
@@ -781,17 +747,17 @@ static int EnumDevices(vlc_object_t *obj, char const *varname,
     return n;
 }
 
-static int DevicesEnum (audio_output_t *aout, char ***idp, char ***namep)
-{
-    return EnumDevices (VLC_OBJECT(aout), NULL, idp, namep);
-}
-
 static int DeviceSelect (audio_output_t *aout, const char *id)
 {
-    if (!var_Type (aout, "alsa-audio-device"))
-        var_Create (aout, "alsa-audio-device", VLC_VAR_STRING);
-    var_SetString (aout, "alsa-audio-device", id);
+    aout_sys_t *sys = aout->sys;
+
+    char *device = strdup (id ? id : "default");
+    if (unlikely(device == NULL))
+        return -1;
 
+    free (sys->device);
+    sys->device = device;
+    aout_DeviceReport (aout, device);
     aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
     return 0;
 }
@@ -803,13 +769,36 @@ static int Open(vlc_object_t *obj)
 
     if (unlikely(sys == NULL))
         return VLC_ENOMEM;
+    sys->device = var_InheritString (aout, "alsa-audio-device");
+    if (unlikely(sys->device == NULL))
+        goto error;
+
     aout->sys = sys;
     aout->start = Start;
     aout->stop = Stop;
     aout_SoftVolumeInit (aout);
-    aout->device_enum = DevicesEnum;
     aout->device_select = DeviceSelect;
+    aout_DeviceReport (aout, sys->device);
+
+    /* ALSA does not support hot-plug events so list devices at startup */
+    char **ids, **names;
+    int count = EnumDevices (VLC_OBJECT(aout), NULL, &ids, &names);
+    if (count >= 0)
+    {
+        for (int i = 0; i < count; i++)
+        {
+            aout_HotplugReport (aout, ids[i], names[i]);
+            free (names[i]);
+            free (ids[i]);
+        }
+        free (names);
+        free (ids);
+    }
+
     return VLC_SUCCESS;
+error:
+    free (sys);
+    return VLC_ENOMEM;
 }
 
 static void Close(vlc_object_t *obj)
@@ -817,5 +806,6 @@ static void Close(vlc_object_t *obj)
     audio_output_t *aout = (audio_output_t *)obj;
     aout_sys_t *sys = aout->sys;
 
-    free(sys);
+    free (sys->device);
+    free (sys);
 }