Init.cxx 8.79 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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),
Nix's avatar
Nix committed
52
	 mixer(nullptr),
53
	 enabled(true), really_enabled(false),
54 55 56 57 58 59 60 61
	 open(false),
	 pause(false),
	 allow_play(true),
	 in_playback_loop(false),
	 woken_for_play(false),
	 filter(nullptr),
	 replay_gain_filter(nullptr),
	 other_replay_gain_filter(nullptr),
62
	 command(Command::NONE)
63
{
64 65 66 67
	assert(plugin.finish != nullptr);
	assert(plugin.open != nullptr);
	assert(plugin.close != nullptr);
	assert(plugin.play != nullptr);
68 69
}

70
static const AudioOutputPlugin *
71
audio_output_detect(Error &error)
72
{
73
	LogDefault(output_domain, "Attempt to detect audio output device");
74

75
	audio_output_plugins_for_each(plugin) {
76
		if (plugin->test_default_device == nullptr)
77 78
			continue;

79 80 81
		FormatDefault(output_domain,
			      "Attempting to detect a %s audio device",
			      plugin->name);
82 83 84 85
		if (ao_plugin_test_default_device(plugin))
			return plugin;
	}

86
	error.Set(output_domain, "Unable to detect an audio device");
87
	return nullptr;
88 89
}

90 91 92 93 94 95 96
/**
 * 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.
 */
97
gcc_pure
98
static MixerType
99
audio_output_mixer_type(const ConfigBlock &block)
100 101
{
	/* read the local "mixer_type" setting */
102
	const char *p = block.GetBlockValue("mixer_type");
103
	if (p != nullptr)
104 105 106
		return mixer_type_parse(p);

	/* try the local "mixer_enabled" setting next (deprecated) */
107
	if (!block.GetBlockValue("mixer_enabled", true))
108
		return MixerType::NONE;
109 110 111

	/* fall back to the global "mixer_type" setting (also
	   deprecated) */
112
	return mixer_type_parse(config_get_string(ConfigOption::MIXER_TYPE,
113
						  "hardware"));
114 115
}

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

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

	case MixerType::NULL_:
		return mixer_new(event_loop, null_mixer_plugin, ao, listener,
133
				 block, error);
134

135
	case MixerType::HARDWARE:
136 137
		if (plugin == nullptr)
			return nullptr;
138

139
		return mixer_new(event_loop, *plugin, ao, listener,
140
				 block, error);
141

142
	case MixerType::SOFTWARE:
143
		mixer = mixer_new(event_loop, software_mixer_plugin, ao,
144
				  listener,
145
				  ConfigBlock(),
146
				  IgnoreError());
147
		assert(mixer != nullptr);
148

149
		filter_chain_append(filter_chain, "software_mixer",
150 151 152 153 154
				    software_mixer_get_filter(mixer));
		return mixer;
	}

	assert(false);
155
	gcc_unreachable();
156 157
}

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

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

181
		config_audio_format.Clear();
182 183
	}

184 185 186
	tags = block.GetBlockValue("tags", true);
	always_on = block.GetBlockValue("always_on", false);
	enabled = block.GetBlockValue("enabled", true);
187

188 189
	/* set up the filter chain */

190 191
	filter = filter_chain_new();
	assert(filter != nullptr);
192

193 194
	/* create the normalization filter (if configured) */

195
	if (config_get_bool(ConfigOption::VOLUME_NORMALIZATION, false)) {
196
		Filter *normalize_filter =
197
			filter_new(&normalize_filter_plugin, ConfigBlock(),
198
				   IgnoreError());
199
		assert(normalize_filter != nullptr);
200

201
		filter_chain_append(*filter, "normalize",
202 203 204
				    autoconvert_filter_new(normalize_filter));
	}

205
	Error filter_error;
206
	filter_chain_parse(*filter,
207
			   block.GetBlockValue(AUDIO_FILTERS, ""),
208
			   filter_error);
209

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

217 218 219 220 221 222
	/* done */

	return true;
}

static bool
223
audio_output_setup(EventLoop &event_loop, AudioOutput &ao,
224
		   MixerListener &mixer_listener,
225
		   const ConfigBlock &block,
226
		   Error &error)
227 228 229
{

	/* create the replay_gain filter */
230

231
	const char *replay_gain_handler =
232
		block.GetBlockValue("replay_gain_handler", "software");
233 234

	if (strcmp(replay_gain_handler, "none") != 0) {
235
		ao.replay_gain_filter = filter_new(&replay_gain_filter_plugin,
236
						   block, IgnoreError());
237
		assert(ao.replay_gain_filter != nullptr);
238

239
		ao.replay_gain_serial = 0;
240

241
		ao.other_replay_gain_filter = filter_new(&replay_gain_filter_plugin,
242
							 block,
243 244
							 IgnoreError());
		assert(ao.other_replay_gain_filter != nullptr);
245

246
		ao.other_replay_gain_serial = 0;
247
	} else {
248 249
		ao.replay_gain_filter = nullptr;
		ao.other_replay_gain_filter = nullptr;
250 251 252 253
	}

	/* set up the mixer */

254
	Error mixer_error;
255
	ao.mixer = audio_output_load_mixer(event_loop, ao, block,
256
					   ao.plugin.mixer_plugin,
257 258 259
					   *ao.filter,
					   mixer_listener,
					   mixer_error);
260
	if (ao.mixer == nullptr && mixer_error.IsDefined())
261 262
		FormatError(mixer_error,
			    "Failed to initialize hardware mixer for '%s'",
263
			    ao.name);
264

265 266 267
	/* use the hardware mixer for replay gain? */

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

281 282
	/* the "convert" filter must be the last one in the chain */

283
	ao.convert_filter = filter_new(&convert_filter_plugin, ConfigBlock(),
284
					IgnoreError());
285
	assert(ao.convert_filter != nullptr);
286

287
	filter_chain_append(*ao.filter, "convert", ao.convert_filter);
288

289 290
	return true;
}
291

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

300
	if (!block.IsNull()) {
301
		const char *p;
302

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

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

320
		plugin = audio_output_detect(error);
321
		if (plugin == nullptr)
322
			return nullptr;
323

324 325 326
		FormatDefault(output_domain,
			      "Successfully detected a %s audio device",
			      plugin->name);
327 328
	}

329
	AudioOutput *ao = ao_plugin_init(plugin, block, error);
330 331
	if (ao == nullptr)
		return nullptr;
332

333
	if (!audio_output_setup(event_loop, *ao, mixer_listener,
334
				block, error)) {
335
		ao_plugin_finish(ao);
336
		return nullptr;
337 338
	}

339
	ao->player_control = &pc;
340
	return ao;
341
}