]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/alsa.c
auhal: prepare SPDIF transition from the packet API (refs #8083)
[vlc] / modules / audio_output / alsa.c
index 1d66f36cfecfc0e7cdbd549770982bd8e587b25b..240dcdd170032a2ca04b812a757a32f53b7a2177 100644 (file)
@@ -44,11 +44,13 @@ 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"
@@ -58,7 +60,6 @@ struct aout_sys_t
 static int Open (vlc_object_t *);
 static void Close (vlc_object_t *);
 static int EnumDevices (vlc_object_t *, char const *, char ***, char ***);
-static void GetDevices (vlc_object_t *, const char *);
 
 #define AUDIO_DEV_TEXT N_("Audio output device")
 #define AUDIO_DEV_LONGTEXT N_("Audio output device (using ALSA syntax).")
@@ -144,17 +145,6 @@ static void DumpDeviceStatus (vlc_object_t *obj, snd_pcm_t *pcm)
 }
 #define DumpDeviceStatus(o, p) DumpDeviceStatus(VLC_OBJECT(o), p)
 
-static int DeviceChanged (vlc_object_t *obj, const char *varname,
-                          vlc_value_t prev, vlc_value_t cur, void *data)
-{
-    aout_ChannelsRestart (obj, varname, prev, cur, data);
-
-    if (!var_Type (obj, "alsa-audio-device"))
-        var_Create (obj, "alsa-audio-device", VLC_VAR_STRING);
-    var_SetString (obj, "alsa-audio-device", cur.psz_string);
-    return VLC_SUCCESS;
-}
-
 static unsigned SetupChannelsUnknown (vlc_object_t *obj,
                                       uint16_t *restrict mask)
 {
@@ -311,61 +301,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;
-            break;
-        case VLC_CODEC_F32L:
-            pcm_format = SND_PCM_FORMAT_FLOAT_LE;
-            break;
-        case VLC_CODEC_S32B:
-            pcm_format = SND_PCM_FORMAT_S32_BE;
-            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;
+        case VLC_CODEC_FL64:
+            pcm_format = SND_PCM_FORMAT_FLOAT64;
             break;
-        case VLC_CODEC_U16B:
-            pcm_format = SND_PCM_FORMAT_U16_BE;
+        case VLC_CODEC_FL32:
+            pcm_format = SND_PCM_FORMAT_FLOAT;
             break;
-        case VLC_CODEC_U16L:
-            pcm_format = SND_PCM_FORMAT_U16_LE;
+        case VLC_CODEC_S32N:
+            pcm_format = SND_PCM_FORMAT_S32;
             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;
@@ -391,9 +342,9 @@ 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. */
+    const char *device = sys->device;
+    char *devbuf = NULL;
+    /* Choose the IEC device for S/PDIF output */
     if (spdif && !strcmp (device, "default"))
     {
         unsigned aes3;
@@ -412,13 +363,13 @@ static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
                 break;
         }
 
-        free (device);
-        if (asprintf (&device,
+        if (asprintf (&devbuf,
                       "iec958:AES0=0x%x,AES1=0x%x,AES2=0x%x,AES3=0x%x",
                       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 */
@@ -427,20 +378,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);
+    msg_Dbg (aout, "using ALSA device: %s", sys->device);
     DumpDevice (VLC_OBJECT(aout), pcm);
 
     /* Get Initial hardware parameters */
@@ -612,11 +563,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;
@@ -628,26 +575,11 @@ static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
         msg_Warn (aout, "device cannot be paused");
     }
     aout->flush = Flush;
-
-    /* Setup audio-device choices */
-    {
-        vlc_value_t text;
-
-        var_Create (aout, "audio-device", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
-        text.psz_string = _("Audio Device");
-        var_Change (aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL);
-
-        GetDevices (VLC_OBJECT(aout), device);
-    }
-    var_AddCallback (aout, "audio-device", DeviceChanged, NULL);
-
-    free (device);
     aout_SoftVolumeStart (aout);
     return 0;
 
 error:
     snd_pcm_close (pcm);
-    free (device);
     return VLC_EGENERIC;
 }
 
@@ -675,7 +607,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;
 
@@ -758,9 +690,6 @@ static void Stop (audio_output_t *aout)
     aout_sys_t *sys = aout->sys;
     snd_pcm_t *pcm = sys->pcm;
 
-    var_DelCallback (aout, "audio-device", DeviceChanged, NULL);
-    var_Destroy (aout, "audio-device");
-
     snd_pcm_drop (pcm);
     snd_pcm_close (pcm);
 }
@@ -769,21 +698,16 @@ static void Stop (audio_output_t *aout)
  * Enumerates ALSA output devices.
  */
 static int EnumDevices(vlc_object_t *obj, char const *varname,
-                       char ***restrict vp, char ***restrict tp)
+                       char ***restrict idp, char ***restrict namep)
 {
-    unsigned n = 0;
-
-    char **names = xmalloc(sizeof (*names));
-    char **descs = xmalloc(sizeof (*names));
-    names[0] = strdup ("default");
-    descs[0] = strdup (N_("Default"));
-    n++;
-    if (unlikely(names[0] == NULL || descs[0] == NULL))
-        abort();
-
     void **hints;
+
+    msg_Dbg (obj, "Available ALSA PCM devices:");
     if (snd_device_name_hint(-1, "pcm", &hints) < 0)
-        return n;
+        return -1;
+
+    char **ids = NULL, **names = NULL;
+    unsigned n = 0;
 
     for (size_t i = 0; hints[i] != NULL; i++)
     {
@@ -794,81 +718,43 @@ static int EnumDevices(vlc_object_t *obj, char const *varname,
             continue;
 
         char *desc = snd_device_name_get_hint(hint, "DESC");
-        if (desc == NULL)
-        {
-            free (name);
-            continue;
-        }
+        if (desc != NULL)
+            for (char *lf = strchr(desc, '\n'); lf; lf = strchr(lf, '\n'))
+                 *lf = ' ';
+        msg_Dbg (obj, "%s (%s)", (desc != NULL) ? desc : name, name);
 
+        ids = xrealloc (ids, (n + 1) * sizeof (*ids));
         names = xrealloc (names, (n + 1) * sizeof (*names));
-        descs = xrealloc (descs, (n + 1) * sizeof (*descs));
-        names[n] = name;
-        descs[n] = desc;
+        ids[n] = name;
+        names[n] = desc;
         n++;
     }
-    snd_device_name_free_hint(hints);
 
-    (void) obj; (void) varname;
-    *vp = names;
-    *tp = descs;
+    snd_device_name_free_hint(hints);
+    *idp = ids;
+    *namep = names;
+    (void) varname;
     return n;
 }
 
-
-static void GetDevices(vlc_object_t *obj, const char *prefs_dev)
+static int DevicesEnum (audio_output_t *aout, char ***idp, char ***namep)
 {
-    void **hints;
-
-    msg_Dbg(obj, "Available ALSA PCM devices:");
-    if (snd_device_name_hint(-1, "pcm", &hints) < 0)
-        return;
-
-    vlc_value_t val, text;
-    bool hinted_default = false;
-    bool hinted_prefs = !strcmp (prefs_dev, "default");
-
-    for (size_t i = 0; hints[i] != NULL; i++)
-    {
-        void *hint = hints[i];
-
-        char *name = snd_device_name_get_hint(hint, "NAME");
-        if (unlikely(name == NULL))
-            continue;
-
-        char *desc = snd_device_name_get_hint(hint, "DESC");
-        if (desc != NULL)
-            for (char *lf = strchr(desc, '\n'); lf; lf = strchr(lf, '\n'))
-                 *lf = ' ';
-        msg_Dbg(obj, "%s (%s)", (desc != NULL) ? desc : name, name);
-
-        if (!strcmp (name, "default"))
-            hinted_default = true;
-        if (!strcmp (name, prefs_dev))
-            hinted_prefs = true;
-
-        val.psz_string = name;
-        text.psz_string = desc;
-        var_Change(obj, "audio-device", VLC_VAR_ADDCHOICE, &val, &text);
-        free(desc);
-        free(name);
-    }
+    return EnumDevices (VLC_OBJECT(aout), NULL, idp, namep);
+}
 
-    snd_device_name_free_hint(hints);
+static int DeviceSelect (audio_output_t *aout, const char *id)
+{
+    aout_sys_t *sys = aout->sys;
 
-    if (!hinted_default)
-    {
-        val.psz_string = (char *)"default";
-        text.psz_string = (char *)N_("Default");
-        var_Change(obj, "audio-device", VLC_VAR_ADDCHOICE, &val, &text);
-    }
+    char *device = strdup (id ? id : "default");
+    if (unlikely(device == NULL))
+        return -1;
 
-    val.psz_string = (char *)prefs_dev;
-    if (!hinted_prefs)
-    {
-        text.psz_string = (char *)N_("VLC preferences");
-        var_Change(obj, "audio-device", VLC_VAR_ADDCHOICE, &val, &text);
-    }
-    var_Change(obj, "audio-device", VLC_VAR_SETVALUE, &val, NULL);
+    free (sys->device);
+    sys->device = device;
+    aout_DeviceReport (aout, device);
+    aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
+    return 0;
 }
 
 static int Open(vlc_object_t *obj)
@@ -878,11 +764,21 @@ 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);
     return VLC_SUCCESS;
+error:
+    free (sys);
+    return VLC_ENOMEM;
 }
 
 static void Close(vlc_object_t *obj)
@@ -890,5 +786,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);
 }