output/alsa: add "allowed_formats" setting

Allows defining a list of supported audio formats, and allows
switching on and off DoP with certain formats.

This is a first rough draft.  The setting syntax and its semantics may
still be redesigned.
This commit is contained in:
Max Kellermann 2017-10-26 09:06:40 +02:00
parent 967d81b782
commit 44c60567dd
5 changed files with 190 additions and 8 deletions

View File

@ -258,6 +258,7 @@ UPNP_SOURCES = \
ALSA_SOURCES = \ ALSA_SOURCES = \
src/lib/alsa/Version.cxx src/lib/alsa/Version.hxx \ src/lib/alsa/Version.cxx src/lib/alsa/Version.hxx \
src/lib/alsa/AllowedFormat.cxx src/lib/alsa/AllowedFormat.hxx \
src/lib/alsa/HwSetup.cxx src/lib/alsa/HwSetup.hxx \ src/lib/alsa/HwSetup.cxx src/lib/alsa/HwSetup.hxx \
src/lib/alsa/Format.hxx \ src/lib/alsa/Format.hxx \
src/lib/alsa/PeriodBuffer.hxx \ src/lib/alsa/PeriodBuffer.hxx \

View File

@ -3457,6 +3457,28 @@ run</programlisting>
option at your own risk. option at your own risk.
</entry> </entry>
</row> </row>
<row>
<entry>
<varname>allowed_formats</varname>
<parameter>F1 F2 ...</parameter>
</entry>
<entry>
<para>
Specifies a list of allowed audio formats, separated
by a space. All items may contain asterisks as a
wild card, and may be followed by
"<parameter>=dop</parameter>" to enable DoP (DSD
over PCM) for this particular format. The first
matching format is used, and if none matches, MPD
chooses the best fallback of this list.
</para>
<para>
Example: "<parameter>96000:16:* 192000:24:*
dsd64:*=dop *:dsd:*</parameter>".
</para>
</entry>
</row>
</tbody> </tbody>
</tgroup> </tgroup>
</informaltable> </informaltable>

View File

@ -0,0 +1,68 @@
/*
* Copyright 2003-2017 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "AllowedFormat.hxx"
#include "AudioParser.hxx"
#include "util/IterableSplitString.hxx"
#include <stdexcept>
namespace Alsa {
AllowedFormat::AllowedFormat(StringView s)
{
#ifdef ENABLE_DSD
const StringView dop_tail("=dop");
if (s.EndsWith(dop_tail)) {
dop = true;
s.size -= dop_tail.size;
} else
dop = false;
#endif
char buffer[64];
if (s.size >= sizeof(buffer))
throw std::runtime_error("Failed to parse audio format");
memcpy(buffer, s.data, s.size);
buffer[s.size] = 0;
format = ParseAudioFormat(buffer, true);
#ifdef ENABLE_DSD
if (dop && format.format != SampleFormat::DSD)
throw std::runtime_error("DoP works only with DSD");
#endif
}
std::forward_list<AllowedFormat>
AllowedFormat::ParseList(StringView s)
{
std::forward_list<AllowedFormat> list;
auto tail = list.before_begin();
for (const auto i : IterableSplitString(s, ' '))
if (!i.empty())
tail = list.emplace_after(tail, i);
return list;
}
} // namespace Alsa

View File

@ -0,0 +1,45 @@
/*
* Copyright 2003-2017 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_ALSA_ALLOWED_FORMAT_HXX
#define MPD_ALSA_ALLOWED_FORMAT_HXX
#include "check.h"
#include "AudioFormat.hxx"
#include <forward_list>
struct StringView;
namespace Alsa {
struct AllowedFormat {
AudioFormat format;
#ifdef ENABLE_DSD
bool dop;
#endif
explicit AllowedFormat(StringView s);
static std::forward_list<AllowedFormat> ParseList(StringView s);
};
} // namespace Alsa
#endif

View File

@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include "AlsaOutputPlugin.hxx" #include "AlsaOutputPlugin.hxx"
#include "lib/alsa/AllowedFormat.hxx"
#include "lib/alsa/HwSetup.hxx" #include "lib/alsa/HwSetup.hxx"
#include "lib/alsa/NonBlock.hxx" #include "lib/alsa/NonBlock.hxx"
#include "lib/alsa/PeriodBuffer.hxx" #include "lib/alsa/PeriodBuffer.hxx"
@ -32,6 +33,7 @@
#include "util/RuntimeError.hxx" #include "util/RuntimeError.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "util/ConstBuffer.hxx" #include "util/ConstBuffer.hxx"
#include "util/StringView.hxx"
#include "event/MultiSocketMonitor.hxx" #include "event/MultiSocketMonitor.hxx"
#include "event/DeferredMonitor.hxx" #include "event/DeferredMonitor.hxx"
#include "event/Call.hxx" #include "event/Call.hxx"
@ -42,6 +44,7 @@
#include <boost/lockfree/spsc_queue.hpp> #include <boost/lockfree/spsc_queue.hpp>
#include <string> #include <string>
#include <forward_list>
static const char default_device[] = "default"; static const char default_device[] = "default";
@ -64,7 +67,7 @@ class AlsaOutput final
* *
* @see http://dsd-guide.com/dop-open-standard * @see http://dsd-guide.com/dop-open-standard
*/ */
const bool dop; const bool dop_setting;
#endif #endif
/** libasound's buffer_time setting (in microseconds) */ /** libasound's buffer_time setting (in microseconds) */
@ -76,6 +79,8 @@ class AlsaOutput final
/** the mode flags passed to snd_pcm_open */ /** the mode flags passed to snd_pcm_open */
int mode = 0; int mode = 0;
std::forward_list<Alsa::AllowedFormat> allowed_formats;
/** the libasound PCM device handle */ /** the libasound PCM device handle */
snd_pcm_t *pcm; snd_pcm_t *pcm;
@ -202,7 +207,11 @@ private:
PcmExport::Params &params); PcmExport::Params &params);
#endif #endif
void SetupOrDop(AudioFormat &audio_format, PcmExport::Params &params); void SetupOrDop(AudioFormat &audio_format, PcmExport::Params &params
#ifdef ENABLE_DSD
, bool dop
#endif
);
/** /**
* Activate the output by registering the sockets in the * Activate the output by registering the sockets in the
@ -302,7 +311,7 @@ AlsaOutput::AlsaOutput(EventLoop &loop, const ConfigBlock &block)
MultiSocketMonitor(loop), DeferredMonitor(loop), MultiSocketMonitor(loop), DeferredMonitor(loop),
device(block.GetBlockValue("device", "")), device(block.GetBlockValue("device", "")),
#ifdef ENABLE_DSD #ifdef ENABLE_DSD
dop(block.GetBlockValue("dop", false) || dop_setting(block.GetBlockValue("dop", false) ||
/* legacy name from MPD 0.18 and older: */ /* legacy name from MPD 0.18 and older: */
block.GetBlockValue("dsd_usb", false)), block.GetBlockValue("dsd_usb", false)),
#endif #endif
@ -324,6 +333,11 @@ AlsaOutput::AlsaOutput(EventLoop &loop, const ConfigBlock &block)
if (!block.GetBlockValue("auto_format", true)) if (!block.GetBlockValue("auto_format", true))
mode |= SND_PCM_NO_AUTO_FORMAT; mode |= SND_PCM_NO_AUTO_FORMAT;
#endif #endif
const char *allowed_formats_string =
block.GetBlockValue("allowed_formats", nullptr);
if (allowed_formats_string != nullptr)
allowed_formats = Alsa::AllowedFormat::ParseList(allowed_formats_string);
} }
void void
@ -430,7 +444,6 @@ inline void
AlsaOutput::SetupDop(const AudioFormat audio_format, AlsaOutput::SetupDop(const AudioFormat audio_format,
PcmExport::Params &params) PcmExport::Params &params)
{ {
assert(dop);
assert(audio_format.format == SampleFormat::DSD); assert(audio_format.format == SampleFormat::DSD);
/* pass 24 bit to AlsaSetup() */ /* pass 24 bit to AlsaSetup() */
@ -462,7 +475,11 @@ AlsaOutput::SetupDop(const AudioFormat audio_format,
#endif #endif
inline void inline void
AlsaOutput::SetupOrDop(AudioFormat &audio_format, PcmExport::Params &params) AlsaOutput::SetupOrDop(AudioFormat &audio_format, PcmExport::Params &params
#ifdef ENABLE_DSD
, bool dop
#endif
)
{ {
#ifdef ENABLE_DSD #ifdef ENABLE_DSD
std::exception_ptr dop_error; std::exception_ptr dop_error;
@ -506,9 +523,34 @@ MaybeDmix(snd_pcm_t *pcm) noexcept
return MaybeDmix(snd_pcm_type(pcm)); return MaybeDmix(snd_pcm_type(pcm));
} }
static const Alsa::AllowedFormat &
BestMatch(const std::forward_list<Alsa::AllowedFormat> &haystack,
const AudioFormat &needle)
{
assert(!haystack.empty());
for (const auto &i : haystack)
if (needle.MatchMask(i.format))
return i;
return haystack.front();
}
void void
AlsaOutput::Open(AudioFormat &audio_format) AlsaOutput::Open(AudioFormat &audio_format)
{ {
#ifdef ENABLE_DSD
bool dop = dop_setting;
#endif
if (!allowed_formats.empty()) {
const auto &a = BestMatch(allowed_formats, audio_format);
audio_format.ApplyMask(a.format);
#ifdef ENABLE_DSD
dop = a.dop;
#endif
}
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)
@ -523,7 +565,11 @@ AlsaOutput::Open(AudioFormat &audio_format)
params.alsa_channel_order = true; params.alsa_channel_order = true;
try { try {
SetupOrDop(audio_format, params); SetupOrDop(audio_format, params
#ifdef ENABLE_DSD
, dop
#endif
);
} catch (...) { } catch (...) {
snd_pcm_close(pcm); snd_pcm_close(pcm);
std::throw_with_nested(FormatRuntimeError("Error opening ALSA device \"%s\"", std::throw_with_nested(FormatRuntimeError("Error opening ALSA device \"%s\"",