Init.cxx 8.78 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
#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
43
#include <string.h>
44

45 46 47
#define AUDIO_OUTPUT_TYPE	"type"
#define AUDIO_OUTPUT_NAME	"name"
#define AUDIO_OUTPUT_FORMAT	"format"
48
#define AUDIO_FILTERS		"filters"
49

50
AudioOutput::AudioOutput(const AudioOutputPlugin &_plugin)
51
	:plugin(_plugin)
52
{
53 54 55 56
	assert(plugin.finish != nullptr);
	assert(plugin.open != nullptr);
	assert(plugin.close != nullptr);
	assert(plugin.play != nullptr);
57 58
}

59
static const AudioOutputPlugin *
60
audio_output_detect(Error &error)
61
{
62
	LogDefault(output_domain, "Attempt to detect audio output device");
63

64
	audio_output_plugins_for_each(plugin) {
65
		if (plugin->test_default_device == nullptr)
66 67
			continue;

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

75
	error.Set(output_domain, "Unable to detect an audio device");
76
	return nullptr;
77 78
}

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

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

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

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

112
static Mixer *
113
audio_output_load_mixer(EventLoop &event_loop, AudioOutput &ao,
114
			const ConfigBlock &block,
115
			const MixerPlugin *plugin,
116
			PreparedFilter &filter_chain,
117
			MixerListener &listener,
118
			Error &error)
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, error);
130

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

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

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

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

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

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

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

177
		config_audio_format.Clear();
178 179
	}

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

184 185
	/* set up the filter chain */

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

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

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

197
		filter_chain_append(*prepared_filter, "normalize",
198 199 200
				    autoconvert_filter_new(normalize_filter));
	}

201
	Error filter_error;
202
	filter_chain_parse(*prepared_filter,
203
			   block.GetBlockValue(AUDIO_FILTERS, ""),
204
			   filter_error);
205

206 207
	// 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)
208
	if (filter_error.IsDefined())
209 210
		FormatError(filter_error,
			    "Failed to initialize filter chain for '%s'",
211
			    name);
212

213 214 215 216 217 218
	/* done */

	return true;
}

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

	/* create the replay_gain filter */
226

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

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

235
		ao.replay_gain_serial = 0;
236

237 238 239 240
		ao.prepared_other_replay_gain_filter = filter_new(&replay_gain_filter_plugin,
								  block,
								  IgnoreError());
		assert(ao.prepared_other_replay_gain_filter != nullptr);
241

242
		ao.other_replay_gain_serial = 0;
243
	} else {
244 245
		ao.prepared_replay_gain_filter = nullptr;
		ao.prepared_other_replay_gain_filter = nullptr;
246 247 248 249
	}

	/* set up the mixer */

250
	Error mixer_error;
251
	ao.mixer = audio_output_load_mixer(event_loop, ao, block,
252
					   ao.plugin.mixer_plugin,
253
					   *ao.prepared_filter,
254 255
					   mixer_listener,
					   mixer_error);
256
	if (ao.mixer == nullptr && mixer_error.IsDefined())
257 258
		FormatError(mixer_error,
			    "Failed to initialize hardware mixer for '%s'",
259
			    ao.name);
260

261 262 263
	/* use the hardware mixer for replay gain? */

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

277 278
	/* the "convert" filter must be the last one in the chain */

279 280 281
	auto *f = filter_new(&convert_filter_plugin, ConfigBlock(),
			     IgnoreError());
	assert(f != nullptr);
282

283 284
	filter_chain_append(*ao.prepared_filter, "convert",
			    ao.convert_filter.Set(f));
285

286 287
	return true;
}
288

289
AudioOutput *
290
audio_output_new(EventLoop &event_loop, const ConfigBlock &block,
291
		 MixerListener &mixer_listener,
292
		 PlayerControl &pc,
293
		 Error &error)
294
{
295
	const AudioOutputPlugin *plugin;
296

297
	if (!block.IsNull()) {
298
		const char *p;
299

300
		p = block.GetBlockValue(AUDIO_OUTPUT_TYPE);
301
		if (p == nullptr) {
302 303
			error.Set(config_domain,
				  "Missing \"type\" configuration");
304
			return nullptr;
305
		}
306

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

317
		plugin = audio_output_detect(error);
318
		if (plugin == nullptr)
319
			return nullptr;
320

321 322 323
		FormatDefault(output_domain,
			      "Successfully detected a %s audio device",
			      plugin->name);
324 325
	}

326
	AudioOutput *ao = ao_plugin_init(plugin, block, error);
327 328
	if (ao == nullptr)
		return nullptr;
329

330
	if (!audio_output_setup(event_loop, *ao, mixer_listener,
331
				block, error)) {
332
		ao_plugin_finish(ao);
333
		return nullptr;
334 335
	}

336
	ao->player_control = &pc;
337
	return ao;
338
}