AoOutputPlugin.cxx 4.56 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 "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

Max Kellermann's avatar
Max Kellermann committed
37
static unsigned ao_output_ref;
38

39
class AoOutput final : AudioOutput {
40
	const size_t write_size;
Max Kellermann's avatar
Max Kellermann committed
41
	int driver;
42
	ao_option *options = nullptr;
Avuton Olrich's avatar
Avuton Olrich committed
43
	ao_device *device;
44

45
	AoOutput(const ConfigBlock &block);
46 47
	~AoOutput();

48
public:
49
	static AudioOutput *Create(EventLoop &, const ConfigBlock &block) {
50 51 52
		return new AoOutput(block);
	}

53 54
	void Open(AudioFormat &audio_format) override;
	void Close() noexcept override;
55

56
	size_t Play(const void *chunk, size_t size) override;
57
};
58

59
static constexpr Domain ao_output_domain("ao_output");
60

61 62 63

static std::system_error
MakeAoError()
Avuton Olrich's avatar
Avuton Olrich committed
64
{
65
	const char *error = "Unknown libao failure";
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

	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;
87
	}
88

89
	return MakeErrno(errno, error);
90 91
}

92
AoOutput::AoOutput(const ConfigBlock &block)
93
	:AudioOutput(0),
94
	 write_size(block.GetBlockValue("write_size", 1024u))
95
{
Max Kellermann's avatar
Max Kellermann committed
96
	if (ao_output_ref == 0) {
97 98
		ao_initialize();
	}
Max Kellermann's avatar
Max Kellermann committed
99
	ao_output_ref++;
100

101
	const char *value = block.GetBlockValue("driver", "default");
102
	if (0 == strcmp(value, "default"))
103
		driver = ao_default_driver_id();
104
	else
105
		driver = ao_driver_id(value);
106

107 108 109
	if (driver < 0)
		throw FormatRuntimeError("\"%s\" is not a valid ao driver",
					 value);
Avuton Olrich's avatar
Avuton Olrich committed
110

111
	ao_info *ai = ao_driver_info(driver);
112 113
	if (ai == nullptr)
		throw std::runtime_error("problems getting driver info");
114

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

118
	value = block.GetBlockValue("options", nullptr);
119
	if (value != nullptr) {
120
		for (const auto &i : SplitString(value, ';')) {
121
			const DivideString ss(i.c_str(), '=', true);
122

123 124
			if (!ss.IsDefined())
				throw FormatRuntimeError("problems parsing options \"%s\"",
125
					     i.c_str());
126

127
			ao_append_option(&options, ss.GetFirst(), ss.GetSecond());
128
		}
129 130 131
	}
}

132
AoOutput::~AoOutput()
133
{
134
	ao_free_options(options);
135

Max Kellermann's avatar
Max Kellermann committed
136
	ao_output_ref--;
137

Max Kellermann's avatar
Max Kellermann committed
138
	if (ao_output_ref == 0)
Avuton Olrich's avatar
Avuton Olrich committed
139
		ao_shutdown();
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
	format.rate = audio_format.sample_rate;
166
	format.byte_format = AO_FMT_NATIVE;
167
	format.channels = audio_format.channels;
168

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

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

180 181 182 183 184
size_t
AoOutput::Play(const void *chunk, size_t size)
{
	if (size > write_size)
		size = write_size;
185

186 187 188 189 190
	/* 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);

191
	if (ao_play(device, data, size) == 0)
192
		throw MakeAoError();
193

194
	return size;
195 196
}

197
const struct AudioOutputPlugin ao_output_plugin = {
198 199
	"ao",
	nullptr,
200
	&AoOutput::Create,
201
	nullptr,
202
};