2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-09-08 04:16:08 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2004-09-08 04:16:08 +02:00
|
|
|
*/
|
|
|
|
|
2013-04-17 00:56:09 +02:00
|
|
|
#include "AoOutputPlugin.hxx"
|
2014-01-23 23:49:50 +01:00
|
|
|
#include "../OutputAPI.hxx"
|
2018-01-02 12:11:02 +01:00
|
|
|
#include "thread/SafeSingleton.hxx"
|
2016-11-02 12:36:13 +01:00
|
|
|
#include "system/Error.hxx"
|
2014-12-03 21:38:06 +01:00
|
|
|
#include "util/DivideString.hxx"
|
2020-04-03 16:46:48 +02:00
|
|
|
#include "util/IterableSplitString.hxx"
|
2016-11-02 12:36:13 +01:00
|
|
|
#include "util/RuntimeError.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Domain.hxx"
|
2019-09-26 14:44:48 +02:00
|
|
|
#include "util/StringAPI.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2004-09-08 04:16:08 +02:00
|
|
|
|
|
|
|
#include <ao/ao.h>
|
2008-11-25 16:10:01 +01:00
|
|
|
|
2011-02-13 02:37:28 +01:00
|
|
|
/* An ao_sample_format, with all fields set to zero: */
|
2013-04-17 00:56:09 +02:00
|
|
|
static ao_sample_format OUR_AO_FORMAT_INITIALIZER;
|
2011-02-13 02:37:28 +01:00
|
|
|
|
2018-01-02 12:11:02 +01:00
|
|
|
class AoInit {
|
|
|
|
public:
|
|
|
|
AoInit() {
|
|
|
|
ao_initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
~AoInit() noexcept {
|
|
|
|
ao_shutdown();
|
|
|
|
}
|
|
|
|
};
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2018-01-02 12:11:02 +01:00
|
|
|
class AoOutput final : AudioOutput, SafeSingleton<AoInit> {
|
2016-11-02 12:36:13 +01:00
|
|
|
const size_t write_size;
|
2009-02-25 19:08:49 +01:00
|
|
|
int driver;
|
2016-11-02 12:36:13 +01:00
|
|
|
ao_option *options = nullptr;
|
2006-07-20 18:02:40 +02:00
|
|
|
ao_device *device;
|
2013-04-17 00:56:09 +02:00
|
|
|
|
2018-01-02 17:33:33 +01:00
|
|
|
size_t frame_size;
|
|
|
|
|
2020-02-01 13:59:55 +01:00
|
|
|
explicit AoOutput(const ConfigBlock &block);
|
2020-02-01 04:37:53 +01:00
|
|
|
~AoOutput() override;
|
2017-01-25 10:33:27 +01:00
|
|
|
|
2017-01-25 10:46:09 +01:00
|
|
|
public:
|
2017-08-09 16:58:44 +02:00
|
|
|
static AudioOutput *Create(EventLoop &, const ConfigBlock &block) {
|
2017-01-25 10:33:27 +01:00
|
|
|
return new AoOutput(block);
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void Open(AudioFormat &audio_format) override;
|
|
|
|
void Close() noexcept override;
|
2017-01-25 10:33:27 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
size_t Play(const void *chunk, size_t size) override;
|
2013-04-17 00:56:09 +02:00
|
|
|
};
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain ao_output_domain("ao_output");
|
2009-02-26 22:04:59 +01:00
|
|
|
|
2016-11-02 12:36:13 +01:00
|
|
|
|
|
|
|
static std::system_error
|
|
|
|
MakeAoError()
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2016-11-02 12:36:13 +01:00
|
|
|
const char *error = "Unknown libao failure";
|
2008-11-25 16:17:48 +01:00
|
|
|
|
|
|
|
switch (errno) {
|
|
|
|
case AO_ENODRIVER:
|
|
|
|
error = "No such libao driver";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AO_ENOTLIVE:
|
|
|
|
error = "This driver is not a libao live device";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AO_EBADOPTION:
|
|
|
|
error = "Invalid libao option";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AO_EOPENDEVICE:
|
|
|
|
error = "Cannot open the libao device";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AO_EFAIL:
|
|
|
|
error = "Generic libao failure";
|
|
|
|
break;
|
2004-10-20 18:16:50 +02:00
|
|
|
}
|
2008-11-25 16:17:48 +01:00
|
|
|
|
2016-11-02 12:36:13 +01:00
|
|
|
return MakeErrno(errno, error);
|
2004-10-20 18:16:50 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 12:36:13 +01:00
|
|
|
AoOutput::AoOutput(const ConfigBlock &block)
|
2017-08-09 16:58:44 +02:00
|
|
|
:AudioOutput(0),
|
2020-02-02 23:31:45 +01:00
|
|
|
write_size(block.GetPositiveValue("write_size", 1024U))
|
2004-10-28 07:14:55 +02:00
|
|
|
{
|
2016-11-02 12:36:13 +01:00
|
|
|
const char *value = block.GetBlockValue("driver", "default");
|
2019-09-26 14:44:48 +02:00
|
|
|
if (StringIsEqual(value, "default"))
|
2013-04-17 00:56:09 +02:00
|
|
|
driver = ao_default_driver_id();
|
2009-02-26 22:04:59 +01:00
|
|
|
else
|
2013-04-17 00:56:09 +02:00
|
|
|
driver = ao_driver_id(value);
|
2009-02-26 22:04:59 +01:00
|
|
|
|
2016-11-02 12:36:13 +01:00
|
|
|
if (driver < 0)
|
|
|
|
throw FormatRuntimeError("\"%s\" is not a valid ao driver",
|
|
|
|
value);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2013-04-17 00:56:09 +02:00
|
|
|
ao_info *ai = ao_driver_info(driver);
|
2016-11-02 12:36:13 +01:00
|
|
|
if (ai == nullptr)
|
|
|
|
throw std::runtime_error("problems getting driver info");
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(ao_output_domain, "using ao driver \"%s\" for \"%s\"\n",
|
2015-01-21 22:13:44 +01:00
|
|
|
ai->short_name, block.GetBlockValue("name", nullptr));
|
2004-11-02 19:31:54 +01:00
|
|
|
|
2015-01-21 22:13:44 +01:00
|
|
|
value = block.GetBlockValue("options", nullptr);
|
2013-04-17 00:56:09 +02:00
|
|
|
if (value != nullptr) {
|
2020-04-03 16:46:48 +02:00
|
|
|
for (StringView i : IterableSplitString(value, ';')) {
|
|
|
|
i.Strip();
|
2009-01-03 13:20:12 +01:00
|
|
|
|
2020-04-03 16:46:48 +02:00
|
|
|
auto s = i.Split('=');
|
|
|
|
if (s.first.empty() || s.second.IsNull())
|
|
|
|
throw FormatRuntimeError("problems parsing option \"%.*s\"",
|
|
|
|
int(i.size), i.data);
|
2009-01-03 13:20:12 +01:00
|
|
|
|
2020-04-03 16:46:48 +02:00
|
|
|
const std::string n(s.first), v(s.second);
|
|
|
|
ao_append_option(&options, n.c_str(), v.c_str());
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
2013-04-17 00:56:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 10:33:27 +01:00
|
|
|
AoOutput::~AoOutput()
|
2013-04-17 00:56:09 +02:00
|
|
|
{
|
2017-01-25 10:33:27 +01:00
|
|
|
ao_free_options(options);
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
|
|
|
|
2017-01-25 10:33:27 +01:00
|
|
|
void
|
|
|
|
AoOutput::Open(AudioFormat &audio_format)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2011-02-13 02:37:28 +01:00
|
|
|
ao_sample_format format = OUR_AO_FORMAT_INITIALIZER;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
switch (audio_format.format) {
|
|
|
|
case SampleFormat::S8:
|
2009-11-10 17:11:34 +01:00
|
|
|
format.bits = 8;
|
|
|
|
break;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S16:
|
2009-11-10 17:11:34 +01:00
|
|
|
format.bits = 16;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* support for 24 bit samples in libao is currently
|
|
|
|
dubious, and until we have sorted that out,
|
|
|
|
convert everything to 16 bit */
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.format = SampleFormat::S16;
|
2009-11-10 17:11:34 +01:00
|
|
|
format.bits = 16;
|
|
|
|
break;
|
|
|
|
}
|
2008-12-09 07:39:24 +01:00
|
|
|
|
2018-01-02 17:33:33 +01:00
|
|
|
frame_size = audio_format.GetFrameSize();
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
format.rate = audio_format.sample_rate;
|
2004-10-20 18:05:13 +02:00
|
|
|
format.byte_format = AO_FMT_NATIVE;
|
2013-08-03 21:00:50 +02:00
|
|
|
format.channels = audio_format.channels;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2017-01-25 10:33:27 +01:00
|
|
|
device = ao_open_live(driver, &format, options);
|
|
|
|
if (device == nullptr)
|
2016-11-02 12:36:13 +01:00
|
|
|
throw MakeAoError();
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
|
|
|
|
2017-01-25 10:33:27 +01:00
|
|
|
void
|
2017-08-09 16:58:44 +02:00
|
|
|
AoOutput::Close() noexcept
|
2004-10-20 18:05:13 +02:00
|
|
|
{
|
2017-01-25 10:33:27 +01:00
|
|
|
ao_close(device);
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2017-01-25 10:33:27 +01:00
|
|
|
size_t
|
|
|
|
AoOutput::Play(const void *chunk, size_t size)
|
|
|
|
{
|
2018-01-02 17:33:33 +01:00
|
|
|
assert(size % frame_size == 0);
|
|
|
|
|
|
|
|
if (size > write_size) {
|
|
|
|
/* round down to a multiple of the frame size */
|
|
|
|
size = (write_size / frame_size) * frame_size;
|
|
|
|
|
|
|
|
if (size < frame_size)
|
|
|
|
/* no matter how small "write_size" was
|
|
|
|
configured, we must pass at least one frame
|
|
|
|
to libao */
|
|
|
|
size = frame_size;
|
|
|
|
}
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2017-01-25 10:44:19 +01:00
|
|
|
/* For whatever reason, libao wants a non-const pointer.
|
|
|
|
Let's hope it does not write to the buffer, and use the
|
|
|
|
union deconst hack to * work around this API misdesign. */
|
|
|
|
char *data = const_cast<char *>((const char *)chunk);
|
|
|
|
|
2017-01-25 10:33:27 +01:00
|
|
|
if (ao_play(device, data, size) == 0)
|
2016-11-02 12:36:13 +01:00
|
|
|
throw MakeAoError();
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2009-02-23 09:29:56 +01:00
|
|
|
return size;
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:22:27 +01:00
|
|
|
const struct AudioOutputPlugin ao_output_plugin = {
|
2013-04-17 00:56:09 +02:00
|
|
|
"ao",
|
|
|
|
nullptr,
|
2017-08-09 16:58:44 +02:00
|
|
|
&AoOutput::Create,
|
2013-04-17 00:56:09 +02:00
|
|
|
nullptr,
|
2004-09-08 04:16:08 +02:00
|
|
|
};
|