AoOutputPlugin.cxx 4.91 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "AoOutputPlugin.hxx"
21
#include "../OutputAPI.hxx"
22
#include "thread/SafeSingleton.hxx"
23
#include "system/Error.hxx"
24
#include "util/DivideString.hxx"
25
#include "util/SplitString.hxx"
26
#include "util/RuntimeError.hxx"
27
#include "util/Domain.hxx"
28
#include "util/StringAPI.hxx"
29
#include "Log.hxx"
30 31

#include <ao/ao.h>
32

33
/* An ao_sample_format, with all fields set to zero: */
34
static ao_sample_format OUR_AO_FORMAT_INITIALIZER;
35

36 37 38 39 40 41 42 43 44 45
class AoInit {
public:
	AoInit() {
		ao_initialize();
	}

	~AoInit() noexcept {
		ao_shutdown();
	}
};
46

47
class AoOutput final : AudioOutput, SafeSingleton<AoInit> {
48
	const size_t write_size;
Max Kellermann's avatar
Max Kellermann committed
49
	int driver;
50
	ao_option *options = nullptr;
Avuton Olrich's avatar
Avuton Olrich committed
51
	ao_device *device;
52

53 54
	size_t frame_size;

Max Kellermann's avatar
Max Kellermann committed
55
	explicit AoOutput(const ConfigBlock &block);
56
	~AoOutput() override;
57

58
public:
59
	static AudioOutput *Create(EventLoop &, const ConfigBlock &block) {
60 61 62
		return new AoOutput(block);
	}

63 64
	void Open(AudioFormat &audio_format) override;
	void Close() noexcept override;
65

66
	size_t Play(const void *chunk, size_t size) override;
67
};
68

69
static constexpr Domain ao_output_domain("ao_output");
70

71 72 73

static std::system_error
MakeAoError()
Avuton Olrich's avatar
Avuton Olrich committed
74
{
75
	const char *error = "Unknown libao failure";
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

	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;
97
	}
98

99
	return MakeErrno(errno, error);
100 101
}

102
AoOutput::AoOutput(const ConfigBlock &block)
103
	:AudioOutput(0),
104
	 write_size(block.GetPositiveValue("write_size", 1024U))
105
{
106
	const char *value = block.GetBlockValue("driver", "default");
107
	if (StringIsEqual(value, "default"))
108
		driver = ao_default_driver_id();
109
	else
110
		driver = ao_driver_id(value);
111

112 113 114
	if (driver < 0)
		throw FormatRuntimeError("\"%s\" is not a valid ao driver",
					 value);
Avuton Olrich's avatar
Avuton Olrich committed
115

116
	ao_info *ai = ao_driver_info(driver);
117 118
	if (ai == nullptr)
		throw std::runtime_error("problems getting driver info");
119

120
	FormatDebug(ao_output_domain, "using ao driver \"%s\" for \"%s\"\n",
121
		    ai->short_name, block.GetBlockValue("name", nullptr));
122

123
	value = block.GetBlockValue("options", nullptr);
124
	if (value != nullptr) {
125
		for (const auto &i : SplitString(value, ';')) {
126
			const DivideString ss(i.c_str(), '=', true);
127

128 129
			if (!ss.IsDefined())
				throw FormatRuntimeError("problems parsing options \"%s\"",
130
					     i.c_str());
131

132
			ao_append_option(&options, ss.GetFirst(), ss.GetSecond());
133
		}
134 135 136
	}
}

137
AoOutput::~AoOutput()
138
{
139
	ao_free_options(options);
140 141
}

142 143
void
AoOutput::Open(AudioFormat &audio_format)
Avuton Olrich's avatar
Avuton Olrich committed
144
{
145
	ao_sample_format format = OUR_AO_FORMAT_INITIALIZER;
146

147 148
	switch (audio_format.format) {
	case SampleFormat::S8:
149 150 151
		format.bits = 8;
		break;

152
	case SampleFormat::S16:
153 154 155 156 157 158 159
		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 */
160
		audio_format.format = SampleFormat::S16;
161 162 163
		format.bits = 16;
		break;
	}
164

165 166
	frame_size = audio_format.GetFrameSize();

167
	format.rate = audio_format.sample_rate;
168
	format.byte_format = AO_FMT_NATIVE;
169
	format.channels = audio_format.channels;
170

171 172
	device = ao_open_live(driver, &format, options);
	if (device == nullptr)
173
		throw MakeAoError();
174 175
}

176
void
177
AoOutput::Close() noexcept
178
{
179 180
	ao_close(device);
}
Avuton Olrich's avatar
Avuton Olrich committed
181

182 183 184
size_t
AoOutput::Play(const void *chunk, size_t size)
{
185 186 187 188 189 190 191 192 193 194 195 196
	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;
	}
197

198 199 200 201 202
	/* 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);

203
	if (ao_play(device, data, size) == 0)
204
		throw MakeAoError();
205

206
	return size;
207 208
}

209
const struct AudioOutputPlugin ao_output_plugin = {
210 211
	"ao",
	nullptr,
212
	&AoOutput::Create,
213
	nullptr,
214
};