Init.cxx 8.24 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 22 23
#include "Internal.hxx"
#include "Registry.hxx"
#include "Domain.hxx"
24
#include "OutputAPI.hxx"
25
#include "filter/FilterConfig.hxx"
26
#include "AudioParser.hxx"
Max Kellermann's avatar
Max Kellermann committed
27 28 29 30
#include "mixer/MixerList.hxx"
#include "mixer/MixerType.hxx"
#include "mixer/MixerControl.hxx"
#include "mixer/plugins/SoftwareMixerPlugin.hxx"
31 32 33 34 35
#include "filter/FilterPlugin.hxx"
#include "filter/FilterRegistry.hxx"
#include "filter/plugins/AutoConvertFilterPlugin.hxx"
#include "filter/plugins/ReplayGainFilterPlugin.hxx"
#include "filter/plugins/ChainFilterPlugin.hxx"
36 37
#include "config/ConfigError.hxx"
#include "config/ConfigGlobal.hxx"
38
#include "config/Block.hxx"
39
#include "util/RuntimeError.hxx"
40
#include "Log.hxx"
41

42 43
#include <stdexcept>

44
#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
45
#include <string.h>
46

47 48 49
#define AUDIO_OUTPUT_TYPE	"type"
#define AUDIO_OUTPUT_NAME	"name"
#define AUDIO_OUTPUT_FORMAT	"format"
50
#define AUDIO_FILTERS		"filters"
51

52 53
AudioOutput::AudioOutput(const AudioOutputPlugin &_plugin,
			 const ConfigBlock &block)
54 55
	:plugin(_plugin),
	 thread(BIND_THIS_METHOD(Task))
56
{
57 58 59 60
	assert(plugin.finish != nullptr);
	assert(plugin.open != nullptr);
	assert(plugin.close != nullptr);
	assert(plugin.play != nullptr);
61

62
	Configure(block);
63 64
}

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

70
	audio_output_plugins_for_each(plugin) {
71
		if (plugin->test_default_device == nullptr)
72 73
			continue;

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

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

84 85 86 87 88 89 90
/**
 * 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.
 */
91
gcc_pure
92
static MixerType
93
audio_output_mixer_type(const ConfigBlock &block) noexcept
94 95
{
	/* read the local "mixer_type" setting */
96
	const char *p = block.GetBlockValue("mixer_type");
97
	if (p != nullptr)
98 99 100
		return mixer_type_parse(p);

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

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

110
static PreparedFilter *
111 112
CreateVolumeFilter()
{
113
	return filter_new(&volume_filter_plugin, ConfigBlock());
114 115
}

116
static Mixer *
117
audio_output_load_mixer(EventLoop &event_loop, AudioOutput &ao,
118
			const ConfigBlock &block,
119
			const MixerPlugin *plugin,
120
			PreparedFilter &filter_chain,
121
			MixerListener &listener)
122
{
123
	Mixer *mixer;
124

125
	switch (audio_output_mixer_type(block)) {
126 127
	case MixerType::NONE:
	case MixerType::UNKNOWN:
128
		return nullptr;
129 130 131

	case MixerType::NULL_:
		return mixer_new(event_loop, null_mixer_plugin, ao, listener,
132
				 block);
133

134
	case MixerType::HARDWARE:
135 136
		if (plugin == nullptr)
			return nullptr;
137

138
		return mixer_new(event_loop, *plugin, ao, listener,
139
				 block);
140

141
	case MixerType::SOFTWARE:
142
		mixer = mixer_new(event_loop, software_mixer_plugin, ao,
143
				  listener,
144
				  ConfigBlock());
145
		assert(mixer != nullptr);
146

147
		filter_chain_append(filter_chain, "software_mixer",
148
				    ao.volume_filter.Set(CreateVolumeFilter()));
149 150 151 152
		return mixer;
	}

	assert(false);
153
	gcc_unreachable();
154 155
}

156 157
void
AudioOutput::Configure(const ConfigBlock &block)
158
{
159 160
	if (!block.IsNull()) {
		name = block.GetBlockValue(AUDIO_OUTPUT_NAME);
161 162
		if (name == nullptr)
			throw std::runtime_error("Missing \"name\" configuration");
163

164
		const char *p = block.GetBlockValue(AUDIO_OUTPUT_FORMAT);
165 166 167
		if (p != nullptr)
			config_audio_format = ParseAudioFormat(p, true);
		else
168
			config_audio_format.Clear();
169
	} else {
170
		name = "default detected output";
171

172
		config_audio_format.Clear();
173 174
	}

175 176 177
	tags = block.GetBlockValue("tags", true);
	always_on = block.GetBlockValue("always_on", false);
	enabled = block.GetBlockValue("enabled", true);
178

179 180
	/* set up the filter chain */

181 182
	prepared_filter = filter_chain_new();
	assert(prepared_filter != nullptr);
183

184 185
	/* create the normalization filter (if configured) */

186
	if (config_get_bool(ConfigOption::VOLUME_NORMALIZATION, false)) {
187
		auto *normalize_filter =
188
			filter_new(&normalize_filter_plugin, ConfigBlock());
189
		assert(normalize_filter != nullptr);
190

191
		filter_chain_append(*prepared_filter, "normalize",
192 193 194
				    autoconvert_filter_new(normalize_filter));
	}

195 196 197 198 199 200 201 202
	try {
		filter_chain_parse(*prepared_filter,
				   block.GetBlockValue(AUDIO_FILTERS, ""));
	} catch (const std::runtime_error &e) {
		/* 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) */
		FormatError(e,
203
			    "Failed to initialize filter chain for '%s'",
204
			    name);
205
	}
206 207
}

208 209
inline void
AudioOutput::Setup(EventLoop &event_loop,
210
		   const ReplayGainConfig &replay_gain_config,
211
		   MixerListener &mixer_listener,
212
		   const ConfigBlock &block)
213 214 215
{

	/* create the replay_gain filter */
216

217
	const char *replay_gain_handler =
218
		block.GetBlockValue("replay_gain_handler", "software");
219 220

	if (strcmp(replay_gain_handler, "none") != 0) {
221
		prepared_replay_gain_filter =
222
			NewReplayGainFilter(replay_gain_config);
223
		assert(prepared_replay_gain_filter != nullptr);
224

225
		prepared_other_replay_gain_filter =
226
			NewReplayGainFilter(replay_gain_config);
227
		assert(prepared_other_replay_gain_filter != nullptr);
228
	} else {
229 230
		prepared_replay_gain_filter = nullptr;
		prepared_other_replay_gain_filter = nullptr;
231 232 233 234
	}

	/* set up the mixer */

235
	try {
236 237 238 239
		mixer = audio_output_load_mixer(event_loop, *this, block,
						plugin.mixer_plugin,
						*prepared_filter,
						mixer_listener);
240 241
	} catch (const std::runtime_error &e) {
		FormatError(e,
242
			    "Failed to initialize hardware mixer for '%s'",
243
			    name);
244
	}
245

246 247 248
	/* use the hardware mixer for replay gain? */

	if (strcmp(replay_gain_handler, "mixer") == 0) {
249 250 251
		if (mixer != nullptr)
			replay_gain_filter_set_mixer(*prepared_replay_gain_filter,
						     mixer, 100);
252
		else
253
			FormatError(output_domain,
254
				    "No such mixer for output '%s'", name);
255
	} else if (strcmp(replay_gain_handler, "software") != 0 &&
256
		   prepared_replay_gain_filter != nullptr) {
257
		throw std::runtime_error("Invalid \"replay_gain_handler\" value");
258 259
	}

260 261
	/* the "convert" filter must be the last one in the chain */

262
	auto *f = filter_new(&convert_filter_plugin, ConfigBlock());
263
	assert(f != nullptr);
264

265 266
	filter_chain_append(*prepared_filter, "convert",
			    convert_filter.Set(f));
267
}
268

269
AudioOutput *
270 271 272
audio_output_new(EventLoop &event_loop,
		 const ReplayGainConfig &replay_gain_config,
		 const ConfigBlock &block,
273
		 MixerListener &mixer_listener,
274
		 AudioOutputClient &client)
275
{
276
	const AudioOutputPlugin *plugin;
277

278
	if (!block.IsNull()) {
279
		const char *p;
280

281
		p = block.GetBlockValue(AUDIO_OUTPUT_TYPE);
282 283
		if (p == nullptr)
			throw std::runtime_error("Missing \"type\" configuration");
284

285
		plugin = AudioOutputPlugin_get(p);
286 287
		if (plugin == nullptr)
			throw FormatRuntimeError("No such audio output plugin: %s", p);
288
	} else {
289
		LogWarning(output_domain,
290
			   "No 'AudioOutput' defined in config file");
291

292
		plugin = audio_output_detect();
293

294 295 296
		FormatDefault(output_domain,
			      "Successfully detected a %s audio device",
			      plugin->name);
297 298
	}

299 300
	AudioOutput *ao = ao_plugin_init(plugin, block);
	assert(ao != nullptr);
301

302
	try {
303 304
		ao->Setup(event_loop, replay_gain_config,
			  mixer_listener, block);
305
	} catch (...) {
306
		ao_plugin_finish(ao);
307
		throw;
308 309
	}

310
	ao->client = &client;
311
	return ao;
312
}