Init.cxx 8.27 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 "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
#include "filter/plugins/AutoConvertFilterPlugin.hxx"
32
#include "filter/plugins/ConvertFilterPlugin.hxx"
33 34
#include "filter/plugins/ReplayGainFilterPlugin.hxx"
#include "filter/plugins/ChainFilterPlugin.hxx"
35
#include "filter/plugins/VolumeFilterPlugin.hxx"
36
#include "filter/plugins/NormalizeFilterPlugin.hxx"
37 38
#include "config/ConfigError.hxx"
#include "config/ConfigGlobal.hxx"
39
#include "config/Block.hxx"
40
#include "util/RuntimeError.hxx"
41
#include "Log.hxx"
42

43 44
#include <stdexcept>

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

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

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

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

66
	audio_output_plugins_for_each(plugin) {
67
		if (plugin->test_default_device == nullptr)
68 69
			continue;

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

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

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

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

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

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

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

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

125
	case MixerType::HARDWARE:
126 127
		if (plugin == nullptr)
			return nullptr;
128

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

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

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

	assert(false);
145
	gcc_unreachable();
146 147
}

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

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

164
		config_audio_format.Clear();
165 166
	}

167 168 169
	{
		char buffer[64];
		snprintf(buffer, sizeof(buffer), "\"%s\" (%s)",
170
			 name, plugin_name);
171 172 173
		log_name = buffer;
	}

174 175
	/* set up the filter chain */

176 177
	prepared_filter = filter_chain_new();
	assert(prepared_filter != nullptr);
178

179 180
	/* create the normalization filter (if configured) */

181
	if (config_get_bool(ConfigOption::VOLUME_NORMALIZATION, false)) {
182
		filter_chain_append(*prepared_filter, "normalize",
183
				    autoconvert_filter_new(normalize_filter_prepare()));
184 185
	}

186 187 188 189 190 191 192 193
	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,
194
			    "Failed to initialize filter chain for '%s'",
195
			    name);
196
	}
197 198
}

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

	/* create the replay_gain filter */
211

212
	const char *replay_gain_handler =
213
		block.GetBlockValue("replay_gain_handler", "software");
214 215

	if (strcmp(replay_gain_handler, "none") != 0) {
216
		prepared_replay_gain_filter =
217
			NewReplayGainFilter(replay_gain_config);
218
		assert(prepared_replay_gain_filter != nullptr);
219

220
		prepared_other_replay_gain_filter =
221
			NewReplayGainFilter(replay_gain_config);
222
		assert(prepared_other_replay_gain_filter != nullptr);
223
	} else {
224 225
		prepared_replay_gain_filter = nullptr;
		prepared_other_replay_gain_filter = nullptr;
226 227 228 229
	}

	/* set up the mixer */

230
	try {
231
		mixer = audio_output_load_mixer(event_loop, *this, block,
232
						mixer_plugin,
233 234
						*prepared_filter,
						mixer_listener);
235 236
	} catch (const std::runtime_error &e) {
		FormatError(e,
237
			    "Failed to initialize hardware mixer for '%s'",
238
			    name);
239
	}
240

241 242 243
	/* use the hardware mixer for replay gain? */

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

255 256
	/* the "convert" filter must be the last one in the chain */

257
	filter_chain_append(*prepared_filter, "convert",
258
			    convert_filter.Set(convert_filter_prepare()));
259
}
260

261
FilteredAudioOutput *
262 263 264
audio_output_new(EventLoop &event_loop,
		 const ReplayGainConfig &replay_gain_config,
		 const ConfigBlock &block,
265
		 MixerListener &mixer_listener)
266
{
267
	const AudioOutputPlugin *plugin;
268

269
	if (!block.IsNull()) {
270
		const char *p;
271

272
		p = block.GetBlockValue(AUDIO_OUTPUT_TYPE);
273 274
		if (p == nullptr)
			throw std::runtime_error("Missing \"type\" configuration");
275

276
		plugin = AudioOutputPlugin_get(p);
277 278
		if (plugin == nullptr)
			throw FormatRuntimeError("No such audio output plugin: %s", p);
279
	} else {
280
		LogWarning(output_domain,
281
			   "No 'AudioOutput' defined in config file");
282

283
		plugin = audio_output_detect();
284

285 286 287
		FormatDefault(output_domain,
			      "Successfully detected a %s audio device",
			      plugin->name);
288 289
	}

290 291
	std::unique_ptr<AudioOutput> ao(ao_plugin_init(event_loop, *plugin,
						       block));
292
	assert(ao != nullptr);
293

294
	auto *f = new FilteredAudioOutput(plugin->name, std::move(ao), block);
295

296 297
	try {
		f->Setup(event_loop, replay_gain_config,
298
			 plugin->mixer_plugin,
299 300 301 302 303 304 305
			 mixer_listener, block);
	} catch (...) {
		delete f;
		throw;
	}

	return f;
306
}