Init.cxx 8.29 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/RuntimeError.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 53
AudioOutput::AudioOutput(const AudioOutputPlugin &_plugin,
			 const ConfigBlock &block)
54
	:plugin(_plugin)
55
{
56 57 58 59
	assert(plugin.finish != nullptr);
	assert(plugin.open != nullptr);
	assert(plugin.close != nullptr);
	assert(plugin.play != nullptr);
60

61
	Configure(block);
62 63
}

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

69
	audio_output_plugins_for_each(plugin) {
70
		if (plugin->test_default_device == nullptr)
71 72
			continue;

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

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

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

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

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

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

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

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

	case MixerType::NULL_:
		return mixer_new(event_loop, null_mixer_plugin, ao, listener,
131
				 block);
132

133
	case MixerType::HARDWARE:
134 135
		if (plugin == nullptr)
			return nullptr;
136

137
		return mixer_new(event_loop, *plugin, ao, listener,
138
				 block);
139

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

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

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

155 156
void
AudioOutput::Configure(const ConfigBlock &block)
157
{
158 159
	if (!block.IsNull()) {
		name = block.GetBlockValue(AUDIO_OUTPUT_NAME);
160 161
		if (name == nullptr)
			throw std::runtime_error("Missing \"name\" configuration");
162

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

171
		config_audio_format.Clear();
172 173
	}

174 175 176
	tags = block.GetBlockValue("tags", true);
	always_on = block.GetBlockValue("always_on", false);
	enabled = block.GetBlockValue("enabled", true);
177

178 179
	/* set up the filter chain */

180 181
	prepared_filter = filter_chain_new();
	assert(prepared_filter != nullptr);
182

183 184
	/* create the normalization filter (if configured) */

185
	if (config_get_bool(ConfigOption::VOLUME_NORMALIZATION, false)) {
186
		auto *normalize_filter =
187
			filter_new(&normalize_filter_plugin, ConfigBlock());
188
		assert(normalize_filter != nullptr);
189

190
		filter_chain_append(*prepared_filter, "normalize",
191 192 193
				    autoconvert_filter_new(normalize_filter));
	}

194 195 196 197 198 199 200 201
	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,
202
			    "Failed to initialize filter chain for '%s'",
203
			    name);
204
	}
205 206
}

207
static void
208 209 210
audio_output_setup(EventLoop &event_loop,
		   const ReplayGainConfig &replay_gain_config,
		   AudioOutput &ao,
211
		   MixerListener &mixer_listener,
212
		   const ConfigBlock &block)
213 214 215
{

	/* create the replay_gain filter */
216

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

	if (strcmp(replay_gain_handler, "none") != 0) {
221 222
		ao.prepared_replay_gain_filter =
			NewReplayGainFilter(replay_gain_config);
223
		assert(ao.prepared_replay_gain_filter != nullptr);
224

225 226
		ao.prepared_other_replay_gain_filter =
			NewReplayGainFilter(replay_gain_config);
227
		assert(ao.prepared_other_replay_gain_filter != nullptr);
228
	} else {
229 230
		ao.prepared_replay_gain_filter = nullptr;
		ao.prepared_other_replay_gain_filter = nullptr;
231 232 233 234
	}

	/* set up the mixer */

235 236 237 238 239 240 241
	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,
242
			    "Failed to initialize hardware mixer for '%s'",
243
			    ao.name);
244
	}
245

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

	if (strcmp(replay_gain_handler, "mixer") == 0) {
249
		if (ao.mixer != nullptr)
250
			replay_gain_filter_set_mixer(*ao.prepared_replay_gain_filter,
251
						     ao.mixer, 100);
252
		else
253
			FormatError(output_domain,
254
				    "No such mixer for output '%s'", ao.name);
255
	} else if (strcmp(replay_gain_handler, "software") != 0 &&
256
		   ao.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
	auto *f = filter_new(&convert_filter_plugin, ConfigBlock());
263
	assert(f != nullptr);
264

265 266
	filter_chain_append(*ao.prepared_filter, "convert",
			    ao.convert_filter.Set(f));
267
}
268

269
AudioOutput *
270 271 272
audio_output_new(EventLoop &event_loop,
		 const ReplayGainConfig &replay_gain_config,
		 const ConfigBlock &block,
273
		 MixerListener &mixer_listener,
274
		 AudioOutputClient &client)
275
{
276
	const AudioOutputPlugin *plugin;
277

278
	if (!block.IsNull()) {
279
		const char *p;
280

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

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

292
		plugin = audio_output_detect();
293

294 295 296
		FormatDefault(output_domain,
			      "Successfully detected a %s audio device",
			      plugin->name);
297 298
	}

299 300
	AudioOutput *ao = ao_plugin_init(plugin, block);
	assert(ao != nullptr);
301

302
	try {
303 304
		audio_output_setup(event_loop, replay_gain_config,
				   *ao, mixer_listener, block);
305
	} catch (...) {
306
		ao_plugin_finish(ao);
307
		throw;
308 309
	}

310
	ao->client = &client;
311
	return ao;
312
}