output/alsa: migrate from class Error to C++ exceptions

This commit is contained in:
Max Kellermann 2016-11-03 17:28:34 +01:00
parent 93a14a93f9
commit 6341be9cdf
1 changed files with 91 additions and 116 deletions

View File

@ -23,10 +23,9 @@
#include "../Wrapper.hxx" #include "../Wrapper.hxx"
#include "mixer/MixerList.hxx" #include "mixer/MixerList.hxx"
#include "pcm/PcmExport.hxx" #include "pcm/PcmExport.hxx"
#include "config/ConfigError.hxx"
#include "system/ByteOrder.hxx" #include "system/ByteOrder.hxx"
#include "util/Manual.hxx" #include "util/Manual.hxx"
#include "util/Error.hxx" #include "util/RuntimeError.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "util/ConstBuffer.hxx" #include "util/ConstBuffer.hxx"
#include "Log.hxx" #include "Log.hxx"
@ -148,13 +147,11 @@ struct AlsaOutput {
private: private:
#ifdef ENABLE_DSD #ifdef ENABLE_DSD
bool SetupDop(AudioFormat audio_format, void SetupDop(AudioFormat audio_format,
PcmExport::Params &params, PcmExport::Params &params);
Error &error);
#endif #endif
bool SetupOrDop(AudioFormat &audio_format, PcmExport::Params &params, void SetupOrDop(AudioFormat &audio_format, PcmExport::Params &params);
Error &error);
int Recover(int err); int Recover(int err);
@ -469,15 +466,16 @@ AlsaSetupFormat(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
/** /**
* Set up the snd_pcm_t object which was opened by the caller. Set up * Set up the snd_pcm_t object which was opened by the caller. Set up
* the configured settings and the audio format. * the configured settings and the audio format.
*
* Throws #std::runtime_error on error.
*/ */
static bool static void
AlsaSetup(AlsaOutput *ad, AudioFormat &audio_format, AlsaSetup(AlsaOutput *ad, AudioFormat &audio_format,
PcmExport::Params &params, Error &error) PcmExport::Params &params)
{ {
unsigned int sample_rate = audio_format.sample_rate; unsigned int sample_rate = audio_format.sample_rate;
unsigned int channels = audio_format.channels; unsigned int channels = audio_format.channels;
int err; int err;
const char *cmd = nullptr;
unsigned retry = MPD_ALSA_RETRY_NR; unsigned retry = MPD_ALSA_RETRY_NR;
unsigned int period_time, period_time_ro; unsigned int period_time, period_time_ro;
unsigned int buffer_time; unsigned int buffer_time;
@ -487,25 +485,22 @@ configure_hw:
/* configure HW params */ /* configure HW params */
snd_pcm_hw_params_t *hwparams; snd_pcm_hw_params_t *hwparams;
snd_pcm_hw_params_alloca(&hwparams); snd_pcm_hw_params_alloca(&hwparams);
cmd = "snd_pcm_hw_params_any";
err = snd_pcm_hw_params_any(ad->pcm, hwparams); err = snd_pcm_hw_params_any(ad->pcm, hwparams);
if (err < 0) if (err < 0)
goto error; throw FormatRuntimeError("snd_pcm_hw_params_any() failed: %s",
snd_strerror(-err));
cmd = "snd_pcm_hw_params_set_access";
err = snd_pcm_hw_params_set_access(ad->pcm, hwparams, err = snd_pcm_hw_params_set_access(ad->pcm, hwparams,
SND_PCM_ACCESS_RW_INTERLEAVED); SND_PCM_ACCESS_RW_INTERLEAVED);
if (err < 0) if (err < 0)
goto error; throw FormatRuntimeError("snd_pcm_hw_params_set_access() failed: %s",
snd_strerror(-err));
err = AlsaSetupFormat(ad->pcm, hwparams, audio_format, params); err = AlsaSetupFormat(ad->pcm, hwparams, audio_format, params);
if (err < 0) { if (err < 0)
error.Format(alsa_output_domain, err, throw FormatRuntimeError("Failed to configure format %s: %s",
"Failed to configure format %s: %s", sample_format_to_string(audio_format.format),
sample_format_to_string(audio_format.format), snd_strerror(-err));
snd_strerror(-err));
return false;
}
snd_pcm_format_t format; snd_pcm_format_t format;
if (snd_pcm_hw_params_get_format(hwparams, &format) == 0) if (snd_pcm_hw_params_get_format(hwparams, &format) == 0)
@ -515,31 +510,23 @@ configure_hw:
err = snd_pcm_hw_params_set_channels_near(ad->pcm, hwparams, err = snd_pcm_hw_params_set_channels_near(ad->pcm, hwparams,
&channels); &channels);
if (err < 0) { if (err < 0)
error.Format(alsa_output_domain, err, throw FormatRuntimeError("Failed to configure %i channels: %s",
"Failed to configure %i channels: %s", (int)audio_format.channels,
(int)audio_format.channels, snd_strerror(-err));
snd_strerror(-err));
return false;
}
audio_format.channels = (int8_t)channels; audio_format.channels = (int8_t)channels;
err = snd_pcm_hw_params_set_rate_near(ad->pcm, hwparams, err = snd_pcm_hw_params_set_rate_near(ad->pcm, hwparams,
&sample_rate, nullptr); &sample_rate, nullptr);
if (err < 0) { if (err < 0)
error.Format(alsa_output_domain, err, throw FormatRuntimeError("Failed to configure sample rate %u Hz: %s",
"Failed to configure sample rate %u Hz: %s", audio_format.sample_rate,
audio_format.sample_rate, snd_strerror(-err));
snd_strerror(-err));
return false;
}
if (sample_rate == 0) { if (sample_rate == 0)
error.Format(alsa_output_domain, err, throw FormatRuntimeError("Failed to configure sample rate %u Hz",
"Failed to configure sample rate %u Hz", audio_format.sample_rate);
audio_format.sample_rate);
return false;
}
audio_format.sample_rate = sample_rate; audio_format.sample_rate = sample_rate;
@ -565,11 +552,11 @@ configure_hw:
if (ad->buffer_time > 0) { if (ad->buffer_time > 0) {
buffer_time = ad->buffer_time; buffer_time = ad->buffer_time;
cmd = "snd_pcm_hw_params_set_buffer_time_near";
err = snd_pcm_hw_params_set_buffer_time_near(ad->pcm, hwparams, err = snd_pcm_hw_params_set_buffer_time_near(ad->pcm, hwparams,
&buffer_time, nullptr); &buffer_time, nullptr);
if (err < 0) if (err < 0)
goto error; throw FormatRuntimeError("snd_pcm_hw_params_set_buffer_time_near() failed: %s",
snd_strerror(-err));
} else { } else {
err = snd_pcm_hw_params_get_buffer_time(hwparams, &buffer_time, err = snd_pcm_hw_params_get_buffer_time(hwparams, &buffer_time,
nullptr); nullptr);
@ -587,63 +574,63 @@ configure_hw:
if (period_time_ro > 0) { if (period_time_ro > 0) {
period_time = period_time_ro; period_time = period_time_ro;
cmd = "snd_pcm_hw_params_set_period_time_near";
err = snd_pcm_hw_params_set_period_time_near(ad->pcm, hwparams, err = snd_pcm_hw_params_set_period_time_near(ad->pcm, hwparams,
&period_time, nullptr); &period_time, nullptr);
if (err < 0) if (err < 0)
goto error; throw FormatRuntimeError("snd_pcm_hw_params_set_period_time_near() failed: %s",
snd_strerror(-err));
} }
cmd = "snd_pcm_hw_params";
err = snd_pcm_hw_params(ad->pcm, hwparams); err = snd_pcm_hw_params(ad->pcm, hwparams);
if (err == -EPIPE && --retry > 0 && period_time_ro > 0) { if (err == -EPIPE && --retry > 0 && period_time_ro > 0) {
period_time_ro = period_time_ro >> 1; period_time_ro = period_time_ro >> 1;
goto configure_hw; goto configure_hw;
} else if (err < 0) } else if (err < 0)
goto error; throw FormatRuntimeError("snd_pcm_hw_params() failed: %s",
snd_strerror(-err));
if (retry != MPD_ALSA_RETRY_NR) if (retry != MPD_ALSA_RETRY_NR)
FormatDebug(alsa_output_domain, FormatDebug(alsa_output_domain,
"ALSA period_time set to %d", period_time); "ALSA period_time set to %d", period_time);
snd_pcm_uframes_t alsa_buffer_size; snd_pcm_uframes_t alsa_buffer_size;
cmd = "snd_pcm_hw_params_get_buffer_size";
err = snd_pcm_hw_params_get_buffer_size(hwparams, &alsa_buffer_size); err = snd_pcm_hw_params_get_buffer_size(hwparams, &alsa_buffer_size);
if (err < 0) if (err < 0)
goto error; throw FormatRuntimeError("snd_pcm_hw_params_get_buffer_size() failed: %s",
snd_strerror(-err));
snd_pcm_uframes_t alsa_period_size; snd_pcm_uframes_t alsa_period_size;
cmd = "snd_pcm_hw_params_get_period_size";
err = snd_pcm_hw_params_get_period_size(hwparams, &alsa_period_size, err = snd_pcm_hw_params_get_period_size(hwparams, &alsa_period_size,
nullptr); nullptr);
if (err < 0) if (err < 0)
goto error; throw FormatRuntimeError("snd_pcm_hw_params_get_period_size() failed: %s",
snd_strerror(-err));
/* configure SW params */ /* configure SW params */
snd_pcm_sw_params_t *swparams; snd_pcm_sw_params_t *swparams;
snd_pcm_sw_params_alloca(&swparams); snd_pcm_sw_params_alloca(&swparams);
cmd = "snd_pcm_sw_params_current";
err = snd_pcm_sw_params_current(ad->pcm, swparams); err = snd_pcm_sw_params_current(ad->pcm, swparams);
if (err < 0) if (err < 0)
goto error; throw FormatRuntimeError("snd_pcm_sw_params_current() failed: %s",
snd_strerror(-err));
cmd = "snd_pcm_sw_params_set_start_threshold";
err = snd_pcm_sw_params_set_start_threshold(ad->pcm, swparams, err = snd_pcm_sw_params_set_start_threshold(ad->pcm, swparams,
alsa_buffer_size - alsa_buffer_size -
alsa_period_size); alsa_period_size);
if (err < 0) if (err < 0)
goto error; throw FormatRuntimeError("snd_pcm_sw_params_set_start_threshold() failed: %s",
snd_strerror(-err));
cmd = "snd_pcm_sw_params_set_avail_min";
err = snd_pcm_sw_params_set_avail_min(ad->pcm, swparams, err = snd_pcm_sw_params_set_avail_min(ad->pcm, swparams,
alsa_period_size); alsa_period_size);
if (err < 0) if (err < 0)
goto error; throw FormatRuntimeError("snd_pcm_sw_params_set_avail_min() failed: %s",
snd_strerror(-err));
cmd = "snd_pcm_sw_params";
err = snd_pcm_sw_params(ad->pcm, swparams); err = snd_pcm_sw_params(ad->pcm, swparams);
if (err < 0) if (err < 0)
goto error; throw FormatRuntimeError("snd_pcm_sw_params() failed: %s",
snd_strerror(-err));
FormatDebug(alsa_output_domain, "buffer_size=%u period_size=%u", FormatDebug(alsa_output_domain, "buffer_size=%u period_size=%u",
(unsigned)alsa_buffer_size, (unsigned)alsa_period_size); (unsigned)alsa_buffer_size, (unsigned)alsa_period_size);
@ -664,21 +651,13 @@ configure_hw:
snd_pcm_format_set_silence(format, ad->silence, snd_pcm_format_set_silence(format, ad->silence,
alsa_period_size * channels); alsa_period_size * channels);
return true;
error:
error.Format(alsa_output_domain, err,
"%s failed: %s",
cmd, snd_strerror(-err));
return false;
} }
#ifdef ENABLE_DSD #ifdef ENABLE_DSD
inline bool inline void
AlsaOutput::SetupDop(const AudioFormat audio_format, AlsaOutput::SetupDop(const AudioFormat audio_format,
PcmExport::Params &params, PcmExport::Params &params)
Error &error)
{ {
assert(dop); assert(dop);
assert(audio_format.format == SampleFormat::DSD); assert(audio_format.format == SampleFormat::DSD);
@ -691,8 +670,7 @@ AlsaOutput::SetupDop(const AudioFormat audio_format,
const AudioFormat check = dop_format; const AudioFormat check = dop_format;
if (!AlsaSetup(this, dop_format, params, error)) AlsaSetup(this, dop_format, params);
return false;
/* if the device allows only 32 bit, shift all DoP /* if the device allows only 32 bit, shift all DoP
samples left by 8 bit and leave the lower 8 bit cleared; samples left by 8 bit and leave the lower 8 bit cleared;
@ -706,54 +684,52 @@ AlsaOutput::SetupDop(const AudioFormat audio_format,
if (dop_format != check) { if (dop_format != check) {
/* no bit-perfect playback, which is required /* no bit-perfect playback, which is required
for DSD over USB */ for DSD over USB */
error.Set(alsa_output_domain,
"Failed to configure DSD-over-PCM");
delete[] silence; delete[] silence;
return false; throw std::runtime_error("Failed to configure DSD-over-PCM");
} }
return true;
} }
#endif #endif
inline bool inline void
AlsaOutput::SetupOrDop(AudioFormat &audio_format, PcmExport::Params &params, AlsaOutput::SetupOrDop(AudioFormat &audio_format, PcmExport::Params &params)
Error &error)
{ {
#ifdef ENABLE_DSD #ifdef ENABLE_DSD
Error dop_error; std::exception_ptr dop_error;
if (dop && audio_format.format == SampleFormat::DSD && if (dop && audio_format.format == SampleFormat::DSD) {
SetupDop(audio_format, params, dop_error)) { try {
params.dop = true; SetupDop(audio_format, params);
return true; params.dop = true;
return;
} catch (...) {
dop_error = std::current_exception();
}
}
try {
#endif
AlsaSetup(this, audio_format, params);
#ifdef ENABLE_DSD
} catch (...) {
if (dop_error)
/* if DoP was attempted, prefer returning the
original DoP error instead of the fallback
error */
std::rethrow_exception(dop_error);
else
throw;
} }
#endif #endif
if (AlsaSetup(this, audio_format, params, error))
return true;
#ifdef ENABLE_DSD
if (dop_error.IsDefined())
/* if DoP was attempted, prefer returning the original
DoP error instead of the fallback error */
error = std::move(dop_error);
#endif
return false;
} }
inline bool inline bool
AlsaOutput::Open(AudioFormat &audio_format, Error &error) AlsaOutput::Open(AudioFormat &audio_format, gcc_unused Error &error)
{ {
int err = snd_pcm_open(&pcm, GetDevice(), int err = snd_pcm_open(&pcm, GetDevice(),
SND_PCM_STREAM_PLAYBACK, mode); SND_PCM_STREAM_PLAYBACK, mode);
if (err < 0) { if (err < 0)
error.Format(alsa_output_domain, err, throw FormatRuntimeError("Failed to open ALSA device \"%s\": %s",
"Failed to open ALSA device \"%s\": %s", GetDevice(), snd_strerror(err));
GetDevice(), snd_strerror(err));
return false;
}
FormatDebug(alsa_output_domain, "opened %s type=%s", FormatDebug(alsa_output_domain, "opened %s type=%s",
snd_pcm_name(pcm), snd_pcm_name(pcm),
@ -762,11 +738,12 @@ AlsaOutput::Open(AudioFormat &audio_format, Error &error)
PcmExport::Params params; PcmExport::Params params;
params.alsa_channel_order = true; params.alsa_channel_order = true;
if (!SetupOrDop(audio_format, params, error)) { try {
SetupOrDop(audio_format, params);
} catch (...) {
snd_pcm_close(pcm); snd_pcm_close(pcm);
error.FormatPrefix("Error opening ALSA device \"%s\": ", std::throw_with_nested(FormatRuntimeError("Error opening ALSA device \"%s\"",
GetDevice()); GetDevice()));
return false;
} }
pcm_export->Open(audio_format.format, pcm_export->Open(audio_format.format,
@ -858,7 +835,7 @@ AlsaOutput::Close()
} }
inline size_t inline size_t
AlsaOutput::Play(const void *chunk, size_t size, Error &error) AlsaOutput::Play(const void *chunk, size_t size, gcc_unused Error &error)
{ {
assert(size > 0); assert(size > 0);
assert(size % in_frame_size == 0); assert(size % in_frame_size == 0);
@ -867,10 +844,9 @@ AlsaOutput::Play(const void *chunk, size_t size, Error &error)
must_prepare = false; must_prepare = false;
int err = snd_pcm_prepare(pcm); int err = snd_pcm_prepare(pcm);
if (err < 0) { if (err < 0)
error.Set(alsa_output_domain, err, snd_strerror(-err)); throw FormatRuntimeError("snd_pcm_prepare() failed: %s",
return 0; snd_strerror(-err));
}
} }
const auto e = pcm_export->Export({chunk, size}); const auto e = pcm_export->Export({chunk, size});
@ -902,10 +878,9 @@ AlsaOutput::Play(const void *chunk, size_t size, Error &error)
} }
if (ret < 0 && ret != -EAGAIN && ret != -EINTR && if (ret < 0 && ret != -EAGAIN && ret != -EINTR &&
Recover(ret) < 0) { Recover(ret) < 0)
error.Set(alsa_output_domain, ret, snd_strerror(-ret)); throw FormatRuntimeError("snd_pcm_writei() failed: %s",
return 0; snd_strerror(-ret));
}
} }
} }