Init.cxx 8.58 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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/Error.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
AudioOutput::AudioOutput(const AudioOutputPlugin &_plugin)
53
	:plugin(_plugin)
54
{
55 56 57 58
	assert(plugin.finish != nullptr);
	assert(plugin.open != nullptr);
	assert(plugin.close != nullptr);
	assert(plugin.play != nullptr);
59 60
}

61
static const AudioOutputPlugin *
62
audio_output_detect(Error &error)
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
	error.Set(output_domain, "Unable to detect an audio device");
78
	return nullptr;
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
gcc_pure
89
static MixerType
90
audio_output_mixer_type(const ConfigBlock &block)
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 mixer_type_parse(config_get_string(ConfigOption::MIXER_TYPE,
104
						  "hardware"));
105 106
}

107
static PreparedFilter *
108 109
CreateVolumeFilter()
{
110
	return filter_new(&volume_filter_plugin, ConfigBlock());
111 112
}

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

122
	switch (audio_output_mixer_type(block)) {
123 124
	case MixerType::NONE:
	case MixerType::UNKNOWN:
125
		return nullptr;
126 127 128

	case MixerType::NULL_:
		return mixer_new(event_loop, null_mixer_plugin, ao, listener,
129
				 block);
130

131
	case MixerType::HARDWARE:
132 133
		if (plugin == nullptr)
			return nullptr;
134

135
		return mixer_new(event_loop, *plugin, ao, listener,
136
				 block);
137

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

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

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

153
bool
154
AudioOutput::Configure(const ConfigBlock &block, Error &error)
155
{
156 157
	if (!block.IsNull()) {
		name = block.GetBlockValue(AUDIO_OUTPUT_NAME);
158
		if (name == nullptr) {
159 160
			error.Set(config_domain,
				  "Missing \"name\" configuration");
161 162 163
			return false;
		}

164
		const char *p = block.GetBlockValue(AUDIO_OUTPUT_FORMAT);
165
		if (p != nullptr) {
166
			bool success =
167
				audio_format_parse(config_audio_format,
168
						   p, true, error);
169 170
			if (!success)
				return false;
171
		} else
172
			config_audio_format.Clear();
173
	} else {
174
		name = "default detected output";
175

176
		config_audio_format.Clear();
177 178
	}

179 180 181
	tags = block.GetBlockValue("tags", true);
	always_on = block.GetBlockValue("always_on", false);
	enabled = block.GetBlockValue("enabled", true);
182

183 184
	/* set up the filter chain */

185 186
	prepared_filter = filter_chain_new();
	assert(prepared_filter != nullptr);
187

188 189
	/* create the normalization filter (if configured) */

190
	if (config_get_bool(ConfigOption::VOLUME_NORMALIZATION, false)) {
191
		auto *normalize_filter =
192
			filter_new(&normalize_filter_plugin, ConfigBlock());
193
		assert(normalize_filter != nullptr);
194

195
		filter_chain_append(*prepared_filter, "normalize",
196 197 198
				    autoconvert_filter_new(normalize_filter));
	}

199 200 201 202 203 204 205 206
	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,
207
			    "Failed to initialize filter chain for '%s'",
208
			    name);
209
	}
210

211 212 213 214 215 216
	/* done */

	return true;
}

static bool
217
audio_output_setup(EventLoop &event_loop, AudioOutput &ao,
218
		   MixerListener &mixer_listener,
219
		   const ConfigBlock &block,
220
		   Error &error)
221 222 223
{

	/* create the replay_gain filter */
224

225
	const char *replay_gain_handler =
226
		block.GetBlockValue("replay_gain_handler", "software");
227 228

	if (strcmp(replay_gain_handler, "none") != 0) {
229
		ao.prepared_replay_gain_filter = filter_new(&replay_gain_filter_plugin,
230
							    block);
231
		assert(ao.prepared_replay_gain_filter != nullptr);
232

233
		ao.replay_gain_serial = 0;
234

235
		ao.prepared_other_replay_gain_filter = filter_new(&replay_gain_filter_plugin,
236
								  block);
237
		assert(ao.prepared_other_replay_gain_filter != nullptr);
238

239
		ao.other_replay_gain_serial = 0;
240
	} else {
241 242
		ao.prepared_replay_gain_filter = nullptr;
		ao.prepared_other_replay_gain_filter = nullptr;
243 244 245 246
	}

	/* set up the mixer */

247 248 249 250 251 252 253
	try {
		ao.mixer = audio_output_load_mixer(event_loop, ao, block,
						   ao.plugin.mixer_plugin,
						   *ao.prepared_filter,
						   mixer_listener);
	} catch (const std::runtime_error &e) {
		FormatError(e,
254
			    "Failed to initialize hardware mixer for '%s'",
255
			    ao.name);
256
	}
257

258 259 260
	/* use the hardware mixer for replay gain? */

	if (strcmp(replay_gain_handler, "mixer") == 0) {
261
		if (ao.mixer != nullptr)
262
			replay_gain_filter_set_mixer(ao.prepared_replay_gain_filter,
263
						     ao.mixer, 100);
264
		else
265
			FormatError(output_domain,
266
				    "No such mixer for output '%s'", ao.name);
267
	} else if (strcmp(replay_gain_handler, "software") != 0 &&
268
		   ao.prepared_replay_gain_filter != nullptr) {
269 270
		error.Set(config_domain,
			  "Invalid \"replay_gain_handler\" value");
271 272 273
		return false;
	}

274 275
	/* the "convert" filter must be the last one in the chain */

276
	auto *f = filter_new(&convert_filter_plugin, ConfigBlock());
277
	assert(f != nullptr);
278

279 280
	filter_chain_append(*ao.prepared_filter, "convert",
			    ao.convert_filter.Set(f));
281

282 283
	return true;
}
284

285
AudioOutput *
286
audio_output_new(EventLoop &event_loop, const ConfigBlock &block,
287
		 MixerListener &mixer_listener,
288
		 PlayerControl &pc,
289
		 Error &error)
290
{
291
	const AudioOutputPlugin *plugin;
292

293
	if (!block.IsNull()) {
294
		const char *p;
295

296
		p = block.GetBlockValue(AUDIO_OUTPUT_TYPE);
297
		if (p == nullptr) {
298 299
			error.Set(config_domain,
				  "Missing \"type\" configuration");
300
			return nullptr;
301
		}
302

303
		plugin = AudioOutputPlugin_get(p);
304
		if (plugin == nullptr) {
305 306
			error.Format(config_domain,
				     "No such audio output plugin: %s", p);
307
			return nullptr;
308 309
		}
	} else {
310
		LogWarning(output_domain,
311
			   "No 'AudioOutput' defined in config file");
312

313
		plugin = audio_output_detect(error);
314
		if (plugin == nullptr)
315
			return nullptr;
316

317 318 319
		FormatDefault(output_domain,
			      "Successfully detected a %s audio device",
			      plugin->name);
320 321
	}

322
	AudioOutput *ao = ao_plugin_init(plugin, block, error);
323 324
	if (ao == nullptr)
		return nullptr;
325

326
	if (!audio_output_setup(event_loop, *ao, mixer_listener,
327
				block, error)) {
328
		ao_plugin_finish(ao);
329
		return nullptr;
330 331
	}

332
	ao->player_control = &pc;
333
	return ao;
334
}