Init.cxx 8.75 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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 "Filtered.hxx"
21 22
#include "Registry.hxx"
#include "Domain.hxx"
23
#include "OutputAPI.hxx"
24
#include "Defaults.hxx"
25
#include "pcm/AudioParser.hxx"
Max Kellermann's avatar
Max Kellermann committed
26 27 28
#include "mixer/MixerList.hxx"
#include "mixer/MixerType.hxx"
#include "mixer/MixerControl.hxx"
29
#include "filter/LoadChain.hxx"
30
#include "filter/Prepared.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
#include "util/RuntimeError.hxx"
38
#include "util/StringAPI.hxx"
39
#include "util/StringFormat.hxx"
40
#include "Log.hxx"
41

42
#include <cassert>
43 44
#include <stdexcept>

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
FilteredAudioOutput::FilteredAudioOutput(const char *_plugin_name,
					 std::unique_ptr<AudioOutput> &&_output,
54
					 const ConfigBlock &block,
55
					 const AudioOutputDefaults &defaults,
56
					 FilterFactory *filter_factory)
57
	:plugin_name(_plugin_name), output(std::move(_output))
58
{
59
	Configure(block, defaults, filter_factory);
60 61
}

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

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

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

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

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

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

	/* fall back to the global "mixer_type" setting (also
	   deprecated) */
103
	return defaults.mixer_type;
104 105
}

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

116
	switch (mixer_type) {
117
	case MixerType::NONE:
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
			       const AudioOutputDefaults &defaults,
151
			       FilterFactory *filter_factory)
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 (defaults.normalize) {
179
		filter_chain_append(*prepared_filter, "normalize",
180
				    autoconvert_filter_new(normalize_filter_prepare()));
181 182
	}

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

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

209 210
	const auto mixer_type = audio_output_mixer_type(block, defaults);

211
	/* create the replay_gain filter */
212

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

216
	if (!StringIsEqual(replay_gain_handler, "none")) {
217 218 219 220 221 222
		/* when using software volume, we lose quality by
		   invoking PcmVolume::Apply() twice; to avoid losing
		   too much precision, we allow the ReplayGainFilter
		   to convert 16 bit to 24 bit */
		const bool allow_convert = mixer_type == MixerType::SOFTWARE;

223
		prepared_replay_gain_filter =
224
			NewReplayGainFilter(replay_gain_config, allow_convert);
225
		assert(prepared_replay_gain_filter != nullptr);
226

227
		prepared_other_replay_gain_filter =
228
			NewReplayGainFilter(replay_gain_config, allow_convert);
229
		assert(prepared_other_replay_gain_filter != nullptr);
230 231 232 233
	}

	/* set up the mixer */

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

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

248
	if (StringIsEqual(replay_gain_handler, "mixer")) {
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 (!StringIsEqual(replay_gain_handler, "software") &&
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
	filter_chain_append(*prepared_filter, "convert",
263
			    convert_filter.Set(convert_filter_prepare()));
264
}
265

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

276
	if (!block.IsNull()) {
277
		const char *p;
278

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

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

290
		plugin = audio_output_detect();
291

292 293 294
		FormatNotice(output_domain,
			     "Successfully detected a %s audio device",
			     plugin->name);
295 296
	}

297 298
	std::unique_ptr<AudioOutput> ao(ao_plugin_init(event_loop, *plugin,
						       block));
299
	assert(ao != nullptr);
300

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