Init.cxx 8.18 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 "Filtered.hxx"
22 23
#include "Registry.hxx"
#include "Domain.hxx"
24
#include "OutputAPI.hxx"
25
#include "AudioParser.hxx"
Max Kellermann's avatar
Max Kellermann committed
26 27 28 29
#include "mixer/MixerList.hxx"
#include "mixer/MixerType.hxx"
#include "mixer/MixerControl.hxx"
#include "mixer/plugins/SoftwareMixerPlugin.hxx"
30
#include "filter/LoadChain.hxx"
31
#include "filter/Prepared.hxx"
32
#include "filter/plugins/AutoConvertFilterPlugin.hxx"
33
#include "filter/plugins/ConvertFilterPlugin.hxx"
34 35
#include "filter/plugins/ReplayGainFilterPlugin.hxx"
#include "filter/plugins/ChainFilterPlugin.hxx"
36
#include "filter/plugins/VolumeFilterPlugin.hxx"
37
#include "filter/plugins/NormalizeFilterPlugin.hxx"
38 39
#include "config/Domain.hxx"
#include "config/Global.hxx"
40
#include "config/Block.hxx"
41
#include "util/RuntimeError.hxx"
42
#include "util/StringFormat.hxx"
43
#include "Log.hxx"
44

45 46
#include <stdexcept>

47
#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
48
#include <string.h>
49

50 51 52
#define AUDIO_OUTPUT_TYPE	"type"
#define AUDIO_OUTPUT_NAME	"name"
#define AUDIO_OUTPUT_FORMAT	"format"
53
#define AUDIO_FILTERS		"filters"
54

55 56
FilteredAudioOutput::FilteredAudioOutput(const char *_plugin_name,
					 std::unique_ptr<AudioOutput> &&_output,
57
					 const ConfigBlock &block)
58
	:plugin_name(_plugin_name), output(std::move(_output))
59
{
60
	Configure(block);
61 62
}

63
static const AudioOutputPlugin *
64
audio_output_detect()
65
{
66
	LogDefault(output_domain, "Attempt to detect audio output device");
67

68
	audio_output_plugins_for_each(plugin) {
69
		if (plugin->test_default_device == nullptr)
70 71
			continue;

72 73 74
		FormatDefault(output_domain,
			      "Attempting to detect a %s audio device",
			      plugin->name);
75 76 77 78
		if (ao_plugin_test_default_device(plugin))
			return plugin;
	}

79
	throw std::runtime_error("Unable to detect an audio device");
80 81
}

82 83 84 85 86 87 88
/**
 * Determines the mixer type which should be used for the specified
 * configuration block.
 *
 * This handles the deprecated options mixer_type (global) and
 * mixer_enabled, if the mixer_type setting is not configured.
 */
89
gcc_pure
90
static MixerType
91
audio_output_mixer_type(const ConfigBlock &block) noexcept
92 93
{
	/* read the local "mixer_type" setting */
94
	const char *p = block.GetBlockValue("mixer_type");
95
	if (p != nullptr)
96 97 98
		return mixer_type_parse(p);

	/* try the local "mixer_enabled" setting next (deprecated) */
99
	if (!block.GetBlockValue("mixer_enabled", true))
100
		return MixerType::NONE;
101 102 103

	/* fall back to the global "mixer_type" setting (also
	   deprecated) */
104
	return mixer_type_parse(config_get_string(ConfigOption::MIXER_TYPE,
105
						  "hardware"));
106 107
}

108
static Mixer *
109
audio_output_load_mixer(EventLoop &event_loop, FilteredAudioOutput &ao,
110
			const ConfigBlock &block,
111
			const MixerPlugin *plugin,
112
			PreparedFilter &filter_chain,
113
			MixerListener &listener)
114
{
115
	Mixer *mixer;
116

117
	switch (audio_output_mixer_type(block)) {
118 119
	case MixerType::NONE:
	case MixerType::UNKNOWN:
120
		return nullptr;
121 122

	case MixerType::NULL_:
123 124
		return mixer_new(event_loop, null_mixer_plugin,
				 *ao.output, listener,
125
				 block);
126

127
	case MixerType::HARDWARE:
128 129
		if (plugin == nullptr)
			return nullptr;
130

131 132
		return mixer_new(event_loop, *plugin,
				 *ao.output, listener,
133
				 block);
134

135
	case MixerType::SOFTWARE:
136 137
		mixer = mixer_new(event_loop, software_mixer_plugin,
				  *ao.output, listener,
138
				  ConfigBlock());
139
		assert(mixer != nullptr);
140

141
		filter_chain_append(filter_chain, "software_mixer",
142
				    ao.volume_filter.Set(volume_filter_prepare()));
143 144 145 146
		return mixer;
	}

	assert(false);
147
	gcc_unreachable();
148 149
}

150
void
151
FilteredAudioOutput::Configure(const ConfigBlock &block)
152
{
153 154
	if (!block.IsNull()) {
		name = block.GetBlockValue(AUDIO_OUTPUT_NAME);
155 156
		if (name == nullptr)
			throw std::runtime_error("Missing \"name\" configuration");
157

158
		const char *p = block.GetBlockValue(AUDIO_OUTPUT_FORMAT);
159 160 161
		if (p != nullptr)
			config_audio_format = ParseAudioFormat(p, true);
		else
162
			config_audio_format.Clear();
163
	} else {
164
		name = "default detected output";
165

166
		config_audio_format.Clear();
167 168
	}

169
	log_name = StringFormat<256>("\"%s\" (%s)", name, plugin_name);
170

171 172
	/* set up the filter chain */

173 174
	prepared_filter = filter_chain_new();
	assert(prepared_filter != nullptr);
175

176 177
	/* create the normalization filter (if configured) */

178
	if (config_get_bool(ConfigOption::VOLUME_NORMALIZATION, false)) {
179
		filter_chain_append(*prepared_filter, "normalize",
180
				    autoconvert_filter_new(normalize_filter_prepare()));
181 182
	}

183
	try {
184
		filter_chain_parse(*prepared_filter, GetGlobalConfig(),
185
				   block.GetBlockValue(AUDIO_FILTERS, ""));
186
	} catch (...) {
187 188 189
		/* It's not really fatal - Part of the filter chain
		   has been set up already and even an empty one will
		   work (if only with unexpected behaviour) */
190
		FormatError(std::current_exception(),
191
			    "Failed to initialize filter chain for '%s'",
192
			    name);
193
	}
194 195
}

196
inline void
197 198
FilteredAudioOutput::Setup(EventLoop &event_loop,
			   const ReplayGainConfig &replay_gain_config,
199
			   const MixerPlugin *mixer_plugin,
200 201
			   MixerListener &mixer_listener,
			   const ConfigBlock &block)
202
{
203
	if (output->GetNeedFullyDefinedAudioFormat() &&
204 205
	    !config_audio_format.IsFullyDefined())
		throw std::runtime_error("Need full audio format specification");
206 207

	/* create the replay_gain filter */
208

209
	const char *replay_gain_handler =
210
		block.GetBlockValue("replay_gain_handler", "software");
211 212

	if (strcmp(replay_gain_handler, "none") != 0) {
213
		prepared_replay_gain_filter =
214
			NewReplayGainFilter(replay_gain_config);
215
		assert(prepared_replay_gain_filter != nullptr);
216

217
		prepared_other_replay_gain_filter =
218
			NewReplayGainFilter(replay_gain_config);
219
		assert(prepared_other_replay_gain_filter != nullptr);
220 221 222 223
	}

	/* set up the mixer */

224
	try {
225
		mixer = audio_output_load_mixer(event_loop, *this, block,
226
						mixer_plugin,
227 228
						*prepared_filter,
						mixer_listener);
229 230
	} catch (...) {
		FormatError(std::current_exception(),
231
			    "Failed to initialize hardware mixer for '%s'",
232
			    name);
233
	}
234

235 236 237
	/* use the hardware mixer for replay gain? */

	if (strcmp(replay_gain_handler, "mixer") == 0) {
238 239 240
		if (mixer != nullptr)
			replay_gain_filter_set_mixer(*prepared_replay_gain_filter,
						     mixer, 100);
241
		else
242
			FormatError(output_domain,
243
				    "No such mixer for output '%s'", name);
244
	} else if (strcmp(replay_gain_handler, "software") != 0 &&
245
		   prepared_replay_gain_filter != nullptr) {
246
		throw std::runtime_error("Invalid \"replay_gain_handler\" value");
247 248
	}

249 250
	/* the "convert" filter must be the last one in the chain */

251
	filter_chain_append(*prepared_filter, "convert",
252
			    convert_filter.Set(convert_filter_prepare()));
253
}
254

255
std::unique_ptr<FilteredAudioOutput>
256 257 258
audio_output_new(EventLoop &event_loop,
		 const ReplayGainConfig &replay_gain_config,
		 const ConfigBlock &block,
259
		 MixerListener &mixer_listener)
260
{
261
	const AudioOutputPlugin *plugin;
262

263
	if (!block.IsNull()) {
264
		const char *p;
265

266
		p = block.GetBlockValue(AUDIO_OUTPUT_TYPE);
267 268
		if (p == nullptr)
			throw std::runtime_error("Missing \"type\" configuration");
269

270
		plugin = AudioOutputPlugin_get(p);
271 272
		if (plugin == nullptr)
			throw FormatRuntimeError("No such audio output plugin: %s", p);
273
	} else {
274
		LogWarning(output_domain,
275
			   "No 'audio_output' defined in config file");
276

277
		plugin = audio_output_detect();
278

279 280 281
		FormatDefault(output_domain,
			      "Successfully detected a %s audio device",
			      plugin->name);
282 283
	}

284 285
	std::unique_ptr<AudioOutput> ao(ao_plugin_init(event_loop, *plugin,
						       block));
286
	assert(ao != nullptr);
287

288 289 290 291 292
	auto f = std::make_unique<FilteredAudioOutput>(plugin->name,
						       std::move(ao), block);
	f->Setup(event_loop, replay_gain_config,
		 plugin->mixer_plugin,
		 mixer_listener, block);
293
	return f;
294
}