AoOutputPlugin.cxx 4.9 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "config.h"
21
#include "AoOutputPlugin.hxx"
22
#include "../OutputAPI.hxx"
23
#include "thread/SafeSingleton.hxx"
24
#include "system/Error.hxx"
25
#include "util/DivideString.hxx"
26
#include "util/SplitString.hxx"
27
#include "util/RuntimeError.hxx"
28
#include "util/Domain.hxx"
29
#include "Log.hxx"
30 31

#include <ao/ao.h>
32

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

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

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

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

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

55 56
	size_t frame_size;

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

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

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

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

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

73 74 75

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

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

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

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

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

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

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

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

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

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

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

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

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

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

167 168
	frame_size = audio_format.GetFrameSize();

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

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

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

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

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

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

208
	return size;
209 210
}

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