AoOutputPlugin.cxx 4.88 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "Log.hxx"
29 30

#include <ao/ao.h>
31

Max Kellermann's avatar
Max Kellermann committed
32 33
#include <string.h>

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

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

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

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

54 55
	size_t frame_size;

56
	AoOutput(const ConfigBlock &block);
57 58
	~AoOutput();

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

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

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

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

72 73 74

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

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

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

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

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

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

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

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

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

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

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

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

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

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

166 167
	frame_size = audio_format.GetFrameSize();

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

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

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

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

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

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

207
	return size;
208 209
}

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