Init.cxx 8.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 "Filtered.hxx"
22 23
#include "Registry.hxx"
#include "Domain.hxx"
24
#include "OutputAPI.hxx"
25
#include "Defaults.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/LoadChain.hxx"
32
#include "filter/Prepared.hxx"
33
#include "filter/plugins/AutoConvertFilterPlugin.hxx"
34
#include "filter/plugins/ConvertFilterPlugin.hxx"
35 36
#include "filter/plugins/ReplayGainFilterPlugin.hxx"
#include "filter/plugins/ChainFilterPlugin.hxx"
37
#include "filter/plugins/VolumeFilterPlugin.hxx"
38
#include "filter/plugins/NormalizeFilterPlugin.hxx"
39
#include "config/Domain.hxx"
40
#include "config/Option.hxx"
41
#include "config/Block.hxx"
42
#include "util/RuntimeError.hxx"
43
#include "util/StringFormat.hxx"
44
#include "Log.hxx"
45

46 47
#include <stdexcept>

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

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

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

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

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

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

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

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

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

	/* fall back to the global "mixer_type" setting (also
	   deprecated) */
107
	return defaults.mixer_type;
108 109
}

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

120
	switch (audio_output_mixer_type(block, defaults)) {
121
	case MixerType::NONE:
122
		return nullptr;
123 124

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

129
	case MixerType::HARDWARE:
130 131
		if (plugin == nullptr)
			return nullptr;
132

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

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

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

	assert(false);
149
	gcc_unreachable();
150 151
}

152
void
153
FilteredAudioOutput::Configure(const ConfigBlock &block,
154
			       const AudioOutputDefaults &defaults,
155
			       FilterFactory *filter_factory)
156
{
157 158
	if (!block.IsNull()) {
		name = block.GetBlockValue(AUDIO_OUTPUT_NAME);
159 160
		if (name == nullptr)
			throw std::runtime_error("Missing \"name\" configuration");
161

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

170
		config_audio_format.Clear();
171 172
	}

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

175 176
	/* set up the filter chain */

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

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

182
	if (defaults.normalize) {
183
		filter_chain_append(*prepared_filter, "normalize",
184
				    autoconvert_filter_new(normalize_filter_prepare()));
185 186
	}

187
	try {
188 189 190
		if (filter_factory != nullptr)
			filter_chain_parse(*prepared_filter, *filter_factory,
					   block.GetBlockValue(AUDIO_FILTERS, ""));
191
	} catch (...) {
192 193 194
		/* 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) */
195
		FormatError(std::current_exception(),
196
			    "Failed to initialize filter chain for '%s'",
197
			    name);
198
	}
199 200
}

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

	/* create the replay_gain filter */
214

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

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

223
		prepared_other_replay_gain_filter =
224
			NewReplayGainFilter(replay_gain_config);
225
		assert(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
						defaults,
233
						mixer_plugin,
234 235
						*prepared_filter,
						mixer_listener);
236 237
	} catch (...) {
		FormatError(std::current_exception(),
238
			    "Failed to initialize hardware mixer for '%s'",
239
			    name);
240
	}
241

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

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

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

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

262
std::unique_ptr<FilteredAudioOutput>
263 264 265
audio_output_new(EventLoop &event_loop,
		 const ReplayGainConfig &replay_gain_config,
		 const ConfigBlock &block,
266
		 const AudioOutputDefaults &defaults,
267
		 FilterFactory *filter_factory,
268
		 MixerListener &mixer_listener)
269
{
270
	const AudioOutputPlugin *plugin;
271

272
	if (!block.IsNull()) {
273
		const char *p;
274

275
		p = block.GetBlockValue(AUDIO_OUTPUT_TYPE);
276 277
		if (p == nullptr)
			throw std::runtime_error("Missing \"type\" configuration");
278

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

286
		plugin = audio_output_detect();
287

288 289 290
		FormatDefault(output_domain,
			      "Successfully detected a %s audio device",
			      plugin->name);
291 292
	}

293 294
	std::unique_ptr<AudioOutput> ao(ao_plugin_init(event_loop, *plugin,
						       block));
295
	assert(ao != nullptr);
296

297
	auto f = std::make_unique<FilteredAudioOutput>(plugin->name,
298
						       std::move(ao), block,
299
						       defaults,
300
						       filter_factory);
301 302
	f->Setup(event_loop, replay_gain_config,
		 plugin->mixer_plugin,
303
		 mixer_listener, block, defaults);
304
	return f;
305
}