Init.cxx 8.63 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 "util/Error.hxx"
39
#include "Log.hxx"
40

41
#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
42
#include <string.h>
43

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

49
AudioOutput::AudioOutput(const AudioOutputPlugin &_plugin)
50
	:plugin(_plugin),
51
	 enabled(true), really_enabled(false),
52 53 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),
	 command(AO_COMMAND_NONE)
{
62 63 64 65
	assert(plugin.finish != nullptr);
	assert(plugin.open != nullptr);
	assert(plugin.close != nullptr);
	assert(plugin.play != nullptr);
66 67
}

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

73
	audio_output_plugins_for_each(plugin) {
74
		if (plugin->test_default_device == nullptr)
75 76
			continue;

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

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

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

	/* try the local "mixer_enabled" setting next (deprecated) */
105
	if (!param.GetBlockValue("mixer_enabled", true))
106 107 108 109
		return MIXER_TYPE_NONE;

	/* fall back to the global "mixer_type" setting (also
	   deprecated) */
110 111
	return mixer_type_parse(config_get_string(CONF_MIXER_TYPE,
						  "hardware"));
112 113
}

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

124 125 126
	switch (audio_output_mixer_type(param)) {
	case MIXER_TYPE_NONE:
	case MIXER_TYPE_UNKNOWN:
127
		return nullptr;
128

129
	case MIXER_TYPE_HARDWARE:
130 131
		if (plugin == nullptr)
			return nullptr;
132

133 134
		return mixer_new(event_loop, *plugin, ao, listener,
				 param, error);
135 136

	case MIXER_TYPE_SOFTWARE:
137
		mixer = mixer_new(event_loop, software_mixer_plugin, ao,
138
				  listener,
139
				  config_param(),
140
				  IgnoreError());
141
		assert(mixer != nullptr);
142

143
		filter_chain_append(filter_chain, "software_mixer",
144 145 146 147 148
				    software_mixer_get_filter(mixer));
		return mixer;
	}

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

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

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

175
		config_audio_format.Clear();
176 177
	}

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

182 183
	/* set up the filter chain */

184 185
	filter = filter_chain_new();
	assert(filter != nullptr);
186

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

189
	if (config_get_bool(CONF_VOLUME_NORMALIZATION, false)) {
190
		Filter *normalize_filter =
191
			filter_new(&normalize_filter_plugin, config_param(),
192
				   IgnoreError());
193
		assert(normalize_filter != nullptr);
194

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

199
	Error filter_error;
200
	filter_chain_parse(*filter,
201
			   param.GetBlockValue(AUDIO_FILTERS, ""),
202
			   filter_error);
203

204 205
	// 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)
206
	if (filter_error.IsDefined())
207 208
		FormatError(filter_error,
			    "Failed to initialize filter chain for '%s'",
209
			    name);
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 config_param &param,
220
		   Error &error)
221 222 223
{

	/* create the replay_gain filter */
224

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

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

233
		ao.replay_gain_serial = 0;
234

235 236 237 238
		ao.other_replay_gain_filter = filter_new(&replay_gain_filter_plugin,
							 param,
							 IgnoreError());
		assert(ao.other_replay_gain_filter != nullptr);
239

240
		ao.other_replay_gain_serial = 0;
241
	} else {
242 243
		ao.replay_gain_filter = nullptr;
		ao.other_replay_gain_filter = nullptr;
244 245 246 247
	}

	/* set up the mixer */

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

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

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

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

277
	ao.convert_filter = filter_new(&convert_filter_plugin, config_param(),
278
					IgnoreError());
279
	assert(ao.convert_filter != nullptr);
280

281
	filter_chain_append(*ao.filter, "convert", ao.convert_filter);
282

283 284
	return true;
}
285

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

294
	if (!param.IsNull()) {
295
		const char *p;
296

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

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

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

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

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

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

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