DecoderList.cxx 4.63 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20
#include "config.h"
21
#include "DecoderList.hxx"
22
#include "DecoderPlugin.hxx"
23 24
#include "PluginUnavailable.hxx"
#include "Log.hxx"
25
#include "config/Data.hxx"
26
#include "config/Block.hxx"
27 28 29 30
#include "plugins/AudiofileDecoderPlugin.hxx"
#include "plugins/PcmDecoderPlugin.hxx"
#include "plugins/DsdiffDecoderPlugin.hxx"
#include "plugins/DsfDecoderPlugin.hxx"
31
#include "plugins/HybridDsdDecoderPlugin.hxx"
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include "plugins/FlacDecoderPlugin.h"
#include "plugins/OpusDecoderPlugin.h"
#include "plugins/VorbisDecoderPlugin.h"
#include "plugins/AdPlugDecoderPlugin.h"
#include "plugins/WavpackDecoderPlugin.hxx"
#include "plugins/FfmpegDecoderPlugin.hxx"
#include "plugins/GmeDecoderPlugin.hxx"
#include "plugins/FaadDecoderPlugin.hxx"
#include "plugins/MadDecoderPlugin.hxx"
#include "plugins/SndfileDecoderPlugin.hxx"
#include "plugins/Mpg123DecoderPlugin.hxx"
#include "plugins/WildmidiDecoderPlugin.hxx"
#include "plugins/MikmodDecoderPlugin.hxx"
#include "plugins/ModplugDecoderPlugin.hxx"
#include "plugins/MpcdecDecoderPlugin.hxx"
#include "plugins/FluidsynthDecoderPlugin.hxx"
#include "plugins/SidplayDecoderPlugin.hxx"
49
#include "util/Macros.hxx"
50
#include "util/RuntimeError.hxx"
51

52 53
#include <string.h>

54
const struct DecoderPlugin *const decoder_plugins[] = {
55
#ifdef ENABLE_MAD
56
	&mad_decoder_plugin,
57
#endif
58
#ifdef ENABLE_MPG123
59 60
	&mpg123_decoder_plugin,
#endif
61
#ifdef ENABLE_VORBIS_DECODER
Max Kellermann's avatar
Max Kellermann committed
62
	&vorbis_decoder_plugin,
63
#endif
64
#ifdef ENABLE_FLAC
Max Kellermann's avatar
Max Kellermann committed
65 66
	&oggflac_decoder_plugin,
	&flac_decoder_plugin,
67
#endif
68
#ifdef ENABLE_OPUS
69 70
	&opus_decoder_plugin,
#endif
71 72 73
#ifdef ENABLE_SNDFILE
	&sndfile_decoder_plugin,
#endif
74
#ifdef ENABLE_AUDIOFILE
75
	&audiofile_decoder_plugin,
76
#endif
77
#ifdef ENABLE_DSD
78
	&dsdiff_decoder_plugin,
79
	&dsf_decoder_plugin,
80
	&hybrid_dsd_decoder_plugin,
81
#endif
82
#ifdef ENABLE_FAAD
83
	&faad_decoder_plugin,
84
#endif
85
#ifdef ENABLE_MPCDEC
86
	&mpcdec_decoder_plugin,
87
#endif
88
#ifdef ENABLE_WAVPACK
89
	&wavpack_decoder_plugin,
90
#endif
91
#ifdef ENABLE_MODPLUG
92
	&modplug_decoder_plugin,
93
#endif
94
#ifdef ENABLE_LIBMIKMOD
95
	&mikmod_decoder_plugin,
96
#endif
97 98 99
#ifdef ENABLE_SIDPLAY
	&sidplay_decoder_plugin,
#endif
100 101 102
#ifdef ENABLE_WILDMIDI
	&wildmidi_decoder_plugin,
#endif
103 104 105
#ifdef ENABLE_FLUIDSYNTH
	&fluidsynth_decoder_plugin,
#endif
106
#ifdef ENABLE_ADPLUG
107 108
	&adplug_decoder_plugin,
#endif
109
#ifdef ENABLE_FFMPEG
110
	&ffmpeg_decoder_plugin,
111
#endif
112
#ifdef ENABLE_GME
113
	&gme_decoder_plugin,
114
#endif
115
	&pcm_decoder_plugin,
116
	nullptr
117
};
118

119 120
static constexpr unsigned num_decoder_plugins =
	ARRAY_SIZE(decoder_plugins) - 1;
121

122
/** which plugins have been initialized successfully? */
123
bool decoder_plugins_enabled[num_decoder_plugins];
124

125
const struct DecoderPlugin *
126
decoder_plugin_from_name(const char *name) noexcept
Avuton Olrich's avatar
Avuton Olrich committed
127
{
128
	return decoder_plugins_find([=](const DecoderPlugin &plugin){
129 130
			return strcmp(plugin.name, name) == 0;
		});
131 132
}

133 134
void
decoder_plugin_init_all(const ConfigData &config)
Avuton Olrich's avatar
Avuton Olrich committed
135
{
136
	ConfigBlock empty;
137

138
	for (unsigned i = 0; decoder_plugins[i] != nullptr; ++i) {
139
		const DecoderPlugin &plugin = *decoder_plugins[i];
140
		const auto *param =
141 142
			config.FindBlock(ConfigBlockOption::DECODER, "plugin",
					 plugin.name);
143

144 145 146
		if (param == nullptr)
			param = &empty;
		else if (!param->GetBlockValue("enabled", true))
147 148 149
			/* the plugin is disabled in mpd.conf */
			continue;

150 151 152
		if (param != nullptr)
			param->SetUsed();

153 154 155
		try {
			if (plugin.Init(*param))
				decoder_plugins_enabled[i] = true;
156 157 158 159
		} catch (const PluginUnavailable &e) {
			FormatError(e,
				    "Decoder plugin '%s' is unavailable",
				    plugin.name);
160 161 162 163
		} catch (...) {
			std::throw_with_nested(FormatRuntimeError("Failed to initialize decoder plugin '%s'",
								  plugin.name));
		}
164
	}
165 166
}

167 168
void
decoder_plugin_deinit_all() noexcept
Avuton Olrich's avatar
Avuton Olrich committed
169
{
170 171 172
	decoder_plugins_for_each_enabled([=](const DecoderPlugin &plugin){
			plugin.Finish();
		});
173
}
174 175

bool
176
decoder_plugins_supports_suffix(const char *suffix) noexcept
177 178 179 180 181
{
	return decoder_plugins_try([suffix](const DecoderPlugin &plugin){
			return plugin.SupportsSuffix(suffix);
		});
}