output/alsa: move AlsaSetupHw() to lib/alsa/HwSetup.cxx
This commit is contained in:
@@ -19,14 +19,13 @@
|
||||
|
||||
#include "config.h"
|
||||
#include "AlsaOutputPlugin.hxx"
|
||||
#include "lib/alsa/Format.hxx"
|
||||
#include "lib/alsa/HwSetup.hxx"
|
||||
#include "lib/alsa/NonBlock.hxx"
|
||||
#include "lib/alsa/PeriodBuffer.hxx"
|
||||
#include "lib/alsa/Version.hxx"
|
||||
#include "../OutputAPI.hxx"
|
||||
#include "mixer/MixerList.hxx"
|
||||
#include "pcm/PcmExport.hxx"
|
||||
#include "system/ByteOrder.hxx"
|
||||
#include "thread/Mutex.hxx"
|
||||
#include "thread/Cond.hxx"
|
||||
#include "util/Manual.hxx"
|
||||
@@ -357,272 +356,6 @@ alsa_test_default_device()
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to configure the specified sample format. On failure,
|
||||
* fall back to the packed version.
|
||||
*/
|
||||
static int
|
||||
AlsaTryFormatOrPacked(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
|
||||
snd_pcm_format_t fmt, PcmExport::Params ¶ms)
|
||||
{
|
||||
int err = snd_pcm_hw_params_set_format(pcm, hwparams, fmt);
|
||||
if (err == 0)
|
||||
params.pack24 = false;
|
||||
|
||||
if (err != -EINVAL)
|
||||
return err;
|
||||
|
||||
fmt = PackAlsaPcmFormat(fmt);
|
||||
if (fmt == SND_PCM_FORMAT_UNKNOWN)
|
||||
return -EINVAL;
|
||||
|
||||
err = snd_pcm_hw_params_set_format(pcm, hwparams, fmt);
|
||||
if (err == 0)
|
||||
params.pack24 = true;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to configure the specified sample format, and tries the
|
||||
* reversed host byte order if was not supported.
|
||||
*/
|
||||
static int
|
||||
AlsaTryFormatOrByteSwap(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
|
||||
snd_pcm_format_t fmt,
|
||||
PcmExport::Params ¶ms)
|
||||
{
|
||||
int err = AlsaTryFormatOrPacked(pcm, hwparams, fmt, params);
|
||||
if (err == 0)
|
||||
params.reverse_endian = false;
|
||||
|
||||
if (err != -EINVAL)
|
||||
return err;
|
||||
|
||||
fmt = ByteSwapAlsaPcmFormat(fmt);
|
||||
if (fmt == SND_PCM_FORMAT_UNKNOWN)
|
||||
return -EINVAL;
|
||||
|
||||
err = AlsaTryFormatOrPacked(pcm, hwparams, fmt, params);
|
||||
if (err == 0)
|
||||
params.reverse_endian = true;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to configure the specified sample format. On DSD_U8
|
||||
* failure, attempt to switch to DSD_U32 or DSD_U16.
|
||||
*/
|
||||
static int
|
||||
AlsaTryFormatDsd(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
|
||||
snd_pcm_format_t fmt, PcmExport::Params ¶ms)
|
||||
{
|
||||
int err = AlsaTryFormatOrByteSwap(pcm, hwparams, fmt, params);
|
||||
|
||||
#if defined(ENABLE_DSD) && defined(HAVE_ALSA_DSD_U32)
|
||||
if (err == 0) {
|
||||
params.dsd_u16 = false;
|
||||
params.dsd_u32 = false;
|
||||
}
|
||||
|
||||
if (err == -EINVAL && fmt == SND_PCM_FORMAT_DSD_U8) {
|
||||
/* attempt to switch to DSD_U32 */
|
||||
fmt = IsLittleEndian()
|
||||
? SND_PCM_FORMAT_DSD_U32_LE
|
||||
: SND_PCM_FORMAT_DSD_U32_BE;
|
||||
err = AlsaTryFormatOrByteSwap(pcm, hwparams, fmt, params);
|
||||
if (err == 0)
|
||||
params.dsd_u32 = true;
|
||||
else
|
||||
fmt = SND_PCM_FORMAT_DSD_U8;
|
||||
}
|
||||
|
||||
if (err == -EINVAL && fmt == SND_PCM_FORMAT_DSD_U8) {
|
||||
/* attempt to switch to DSD_U16 */
|
||||
fmt = IsLittleEndian()
|
||||
? SND_PCM_FORMAT_DSD_U16_LE
|
||||
: SND_PCM_FORMAT_DSD_U16_BE;
|
||||
err = AlsaTryFormatOrByteSwap(pcm, hwparams, fmt, params);
|
||||
if (err == 0)
|
||||
params.dsd_u16 = true;
|
||||
else
|
||||
fmt = SND_PCM_FORMAT_DSD_U8;
|
||||
}
|
||||
#endif
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static int
|
||||
AlsaTryFormat(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
|
||||
SampleFormat sample_format,
|
||||
PcmExport::Params ¶ms)
|
||||
{
|
||||
snd_pcm_format_t alsa_format = ToAlsaPcmFormat(sample_format);
|
||||
if (alsa_format == SND_PCM_FORMAT_UNKNOWN)
|
||||
return -EINVAL;
|
||||
|
||||
return AlsaTryFormatDsd(pcm, hwparams, alsa_format, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure a sample format, and probe other formats if that fails.
|
||||
*/
|
||||
static int
|
||||
AlsaSetupSampleFormat(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
|
||||
SampleFormat &sample_format,
|
||||
PcmExport::Params ¶ms)
|
||||
{
|
||||
/* try the input format first */
|
||||
|
||||
int err = AlsaTryFormat(pcm, hwparams, sample_format, params);
|
||||
|
||||
/* if unsupported by the hardware, try other formats */
|
||||
|
||||
static constexpr SampleFormat probe_formats[] = {
|
||||
SampleFormat::S24_P32,
|
||||
SampleFormat::S32,
|
||||
SampleFormat::S16,
|
||||
SampleFormat::S8,
|
||||
SampleFormat::UNDEFINED,
|
||||
};
|
||||
|
||||
for (unsigned i = 0;
|
||||
err == -EINVAL && probe_formats[i] != SampleFormat::UNDEFINED;
|
||||
++i) {
|
||||
const SampleFormat mpd_format = probe_formats[i];
|
||||
if (mpd_format == sample_format)
|
||||
continue;
|
||||
|
||||
err = AlsaTryFormat(pcm, hwparams, mpd_format, params);
|
||||
if (err == 0)
|
||||
sample_format = mpd_format;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for snd_pcm_hw_params().
|
||||
*
|
||||
* @param buffer_time the configured buffer time, or 0 if not configured
|
||||
* @param period_time the configured period time, or 0 if not configured
|
||||
* @param audio_format an #AudioFormat to be configured (or modified)
|
||||
* by this function
|
||||
* @param params to be modified by this function
|
||||
*/
|
||||
static void
|
||||
AlsaSetupHw(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
|
||||
unsigned buffer_time, unsigned period_time,
|
||||
AudioFormat &audio_format, PcmExport::Params ¶ms)
|
||||
{
|
||||
int err;
|
||||
unsigned int period_time_ro = period_time;
|
||||
|
||||
/* configure HW params */
|
||||
err = snd_pcm_hw_params_any(pcm, hwparams);
|
||||
if (err < 0)
|
||||
throw FormatRuntimeError("snd_pcm_hw_params_any() failed: %s",
|
||||
snd_strerror(-err));
|
||||
|
||||
err = snd_pcm_hw_params_set_access(pcm, hwparams,
|
||||
SND_PCM_ACCESS_RW_INTERLEAVED);
|
||||
if (err < 0)
|
||||
throw FormatRuntimeError("snd_pcm_hw_params_set_access() failed: %s",
|
||||
snd_strerror(-err));
|
||||
|
||||
err = AlsaSetupSampleFormat(pcm, hwparams,
|
||||
audio_format.format, params);
|
||||
if (err < 0)
|
||||
throw FormatRuntimeError("Failed to configure format %s: %s",
|
||||
sample_format_to_string(audio_format.format),
|
||||
snd_strerror(-err));
|
||||
|
||||
unsigned int channels = audio_format.channels;
|
||||
err = snd_pcm_hw_params_set_channels_near(pcm, hwparams,
|
||||
&channels);
|
||||
if (err < 0)
|
||||
throw FormatRuntimeError("Failed to configure %i channels: %s",
|
||||
(int)audio_format.channels,
|
||||
snd_strerror(-err));
|
||||
|
||||
audio_format.channels = (int8_t)channels;
|
||||
|
||||
const unsigned requested_sample_rate =
|
||||
params.CalcOutputSampleRate(audio_format.sample_rate);
|
||||
unsigned output_sample_rate = requested_sample_rate;
|
||||
|
||||
err = snd_pcm_hw_params_set_rate_near(pcm, hwparams,
|
||||
&output_sample_rate, nullptr);
|
||||
if (err < 0)
|
||||
throw FormatRuntimeError("Failed to configure sample rate %u Hz: %s",
|
||||
requested_sample_rate,
|
||||
snd_strerror(-err));
|
||||
|
||||
if (output_sample_rate == 0)
|
||||
throw FormatRuntimeError("Failed to configure sample rate %u Hz",
|
||||
audio_format.sample_rate);
|
||||
|
||||
if (output_sample_rate != requested_sample_rate)
|
||||
audio_format.sample_rate = params.CalcInputSampleRate(output_sample_rate);
|
||||
|
||||
snd_pcm_uframes_t buffer_size_min, buffer_size_max;
|
||||
snd_pcm_hw_params_get_buffer_size_min(hwparams, &buffer_size_min);
|
||||
snd_pcm_hw_params_get_buffer_size_max(hwparams, &buffer_size_max);
|
||||
unsigned buffer_time_min, buffer_time_max;
|
||||
snd_pcm_hw_params_get_buffer_time_min(hwparams, &buffer_time_min, 0);
|
||||
snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time_max, 0);
|
||||
FormatDebug(alsa_output_domain, "buffer: size=%u..%u time=%u..%u",
|
||||
(unsigned)buffer_size_min, (unsigned)buffer_size_max,
|
||||
buffer_time_min, buffer_time_max);
|
||||
|
||||
snd_pcm_uframes_t period_size_min, period_size_max;
|
||||
snd_pcm_hw_params_get_period_size_min(hwparams, &period_size_min, 0);
|
||||
snd_pcm_hw_params_get_period_size_max(hwparams, &period_size_max, 0);
|
||||
unsigned period_time_min, period_time_max;
|
||||
snd_pcm_hw_params_get_period_time_min(hwparams, &period_time_min, 0);
|
||||
snd_pcm_hw_params_get_period_time_max(hwparams, &period_time_max, 0);
|
||||
FormatDebug(alsa_output_domain, "period: size=%u..%u time=%u..%u",
|
||||
(unsigned)period_size_min, (unsigned)period_size_max,
|
||||
period_time_min, period_time_max);
|
||||
|
||||
if (buffer_time > 0) {
|
||||
err = snd_pcm_hw_params_set_buffer_time_near(pcm, hwparams,
|
||||
&buffer_time, nullptr);
|
||||
if (err < 0)
|
||||
throw FormatRuntimeError("snd_pcm_hw_params_set_buffer_time_near() failed: %s",
|
||||
snd_strerror(-err));
|
||||
} else {
|
||||
err = snd_pcm_hw_params_get_buffer_time(hwparams, &buffer_time,
|
||||
nullptr);
|
||||
if (err < 0)
|
||||
buffer_time = 0;
|
||||
}
|
||||
|
||||
if (period_time_ro == 0 && buffer_time >= 10000) {
|
||||
period_time_ro = period_time = buffer_time / 4;
|
||||
|
||||
FormatDebug(alsa_output_domain,
|
||||
"default period_time = buffer_time/4 = %u/4 = %u",
|
||||
buffer_time, period_time);
|
||||
}
|
||||
|
||||
if (period_time_ro > 0) {
|
||||
period_time = period_time_ro;
|
||||
err = snd_pcm_hw_params_set_period_time_near(pcm, hwparams,
|
||||
&period_time, nullptr);
|
||||
if (err < 0)
|
||||
throw FormatRuntimeError("snd_pcm_hw_params_set_period_time_near() failed: %s",
|
||||
snd_strerror(-err));
|
||||
}
|
||||
|
||||
err = snd_pcm_hw_params(pcm, hwparams);
|
||||
if (err < 0)
|
||||
throw FormatRuntimeError("snd_pcm_hw_params() failed: %s",
|
||||
snd_strerror(-err));
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for snd_pcm_sw_params().
|
||||
*/
|
||||
@@ -662,9 +395,9 @@ AlsaOutput::Setup(AudioFormat &audio_format,
|
||||
snd_pcm_hw_params_t *hwparams;
|
||||
snd_pcm_hw_params_alloca(&hwparams);
|
||||
|
||||
AlsaSetupHw(pcm, hwparams,
|
||||
buffer_time, period_time,
|
||||
audio_format, params);
|
||||
Alsa::SetupHw(pcm, hwparams,
|
||||
buffer_time, period_time,
|
||||
audio_format, params);
|
||||
|
||||
snd_pcm_format_t format;
|
||||
if (snd_pcm_hw_params_get_format(hwparams, &format) == 0)
|
||||
|
Reference in New Issue
Block a user