PlaylistRegistry.cxx 7.21 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * 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.
 *
 * 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.
 */

20
#include "config.h"
21
#include "PlaylistRegistry.hxx"
22
#include "PlaylistPlugin.hxx"
23 24 25 26 27 28 29 30 31 32
#include "plugins/ExtM3uPlaylistPlugin.hxx"
#include "plugins/M3uPlaylistPlugin.hxx"
#include "plugins/XspfPlaylistPlugin.hxx"
#include "plugins/DespotifyPlaylistPlugin.hxx"
#include "plugins/SoundCloudPlaylistPlugin.hxx"
#include "plugins/PlsPlaylistPlugin.hxx"
#include "plugins/AsxPlaylistPlugin.hxx"
#include "plugins/RssPlaylistPlugin.hxx"
#include "plugins/CuePlaylistPlugin.hxx"
#include "plugins/EmbeddedCuePlaylistPlugin.hxx"
Max Kellermann's avatar
Max Kellermann committed
33
#include "input/InputStream.hxx"
Max Kellermann's avatar
Max Kellermann committed
34
#include "util/UriUtil.hxx"
35
#include "util/StringUtil.hxx"
36
#include "util/Error.hxx"
37
#include "util/Macros.hxx"
38 39
#include "config/ConfigGlobal.hxx"
#include "config/ConfigData.hxx"
40
#include "Log.hxx"
41 42

#include <assert.h>
43
#include <string.h>
44

45
const struct playlist_plugin *const playlist_plugins[] = {
46
	&extm3u_playlist_plugin,
47
	&m3u_playlist_plugin,
48 49
#ifdef HAVE_GLIB
	// TODO: enable without GLib
Qball Cow's avatar
Qball Cow committed
50
	&pls_playlist_plugin,
51
#endif
52 53
#ifdef HAVE_EXPAT
	&xspf_playlist_plugin,
54
	&asx_playlist_plugin,
55
	&rss_playlist_plugin,
56
#endif
57 58 59
#ifdef ENABLE_DESPOTIFY
	&despotify_playlist_plugin,
#endif
60 61
#ifdef ENABLE_SOUNDCLOUD
	&soundcloud_playlist_plugin,
62 63
#endif
	&cue_playlist_plugin,
64
	&embcue_playlist_plugin,
65
	nullptr
66 67
};

68 69 70
static constexpr unsigned n_playlist_plugins =
	ARRAY_SIZE(playlist_plugins) - 1;

71
/** which plugins have been initialized successfully? */
72
static bool playlist_plugins_enabled[n_playlist_plugins];
73

74 75 76 77
#define playlist_plugins_for_each_enabled(plugin) \
	playlist_plugins_for_each(plugin) \
		if (playlist_plugins_enabled[playlist_plugin_iterator - playlist_plugins])

78 79 80
void
playlist_list_global_init(void)
{
81 82
	const config_param empty;

83
	for (unsigned i = 0; playlist_plugins[i] != nullptr; ++i) {
84 85
		const struct playlist_plugin *plugin = playlist_plugins[i];
		const struct config_param *param =
86 87
			config_find_block(CONF_PLAYLIST_PLUGIN, "name",
					  plugin->name);
88 89 90
		if (param == nullptr)
			param = &empty;
		else if (!param->GetBlockValue("enabled", true))
91 92 93
			/* the plugin is disabled in mpd.conf */
			continue;

94
		playlist_plugins_enabled[i] =
95
			playlist_plugin_init(playlist_plugins[i], *param);
96
	}
97 98 99 100 101
}

void
playlist_list_global_finish(void)
{
102 103
	playlist_plugins_for_each_enabled(plugin)
		playlist_plugin_finish(plugin);
104 105
}

106
static SongEnumerator *
107
playlist_list_open_uri_scheme(const char *uri, Mutex &mutex, Cond &cond,
108
			      bool *tried)
109
{
110
	SongEnumerator *playlist = nullptr;
111

112
	assert(uri != nullptr);
113

114 115
	const auto scheme = uri_get_scheme(uri);
	if (scheme.empty())
116
		return nullptr;
117

118
	for (unsigned i = 0; playlist_plugins[i] != nullptr; ++i) {
119 120
		const struct playlist_plugin *plugin = playlist_plugins[i];

121 122
		assert(!tried[i]);

123 124
		if (playlist_plugins_enabled[i] && plugin->open_uri != nullptr &&
		    plugin->schemes != nullptr &&
125
		    string_array_contains(plugin->schemes, scheme.c_str())) {
126 127
			playlist = playlist_plugin_open_uri(plugin, uri,
							    mutex, cond);
128
			if (playlist != nullptr)
129
				break;
130 131

			tried[i] = true;
132 133 134 135 136 137
		}
	}

	return playlist;
}

138
static SongEnumerator *
139
playlist_list_open_uri_suffix(const char *uri, Mutex &mutex, Cond &cond,
140
			      const bool *tried)
141 142
{
	const char *suffix;
143
	SongEnumerator *playlist = nullptr;
144

145
	assert(uri != nullptr);
146

147
	suffix = uri_get_suffix(uri);
148 149
	if (suffix == nullptr)
		return nullptr;
150

151
	for (unsigned i = 0; playlist_plugins[i] != nullptr; ++i) {
152 153 154
		const struct playlist_plugin *plugin = playlist_plugins[i];

		if (playlist_plugins_enabled[i] && !tried[i] &&
155
		    plugin->open_uri != nullptr && plugin->suffixes != nullptr &&
156
		    string_array_contains(plugin->suffixes, suffix)) {
157 158
			playlist = playlist_plugin_open_uri(plugin, uri,
							    mutex, cond);
159
			if (playlist != nullptr)
160 161 162 163 164 165 166
				break;
		}
	}

	return playlist;
}

167
SongEnumerator *
168
playlist_list_open_uri(const char *uri, Mutex &mutex, Cond &cond)
169 170 171
{
	/** this array tracks which plugins have already been tried by
	    playlist_list_open_uri_scheme() */
172
	bool tried[n_playlist_plugins];
173

174
	assert(uri != nullptr);
175 176 177

	memset(tried, false, sizeof(tried));

178
	auto playlist = playlist_list_open_uri_scheme(uri, mutex, cond, tried);
179
	if (playlist == nullptr)
180 181
		playlist = playlist_list_open_uri_suffix(uri, mutex, cond,
							 tried);
182 183 184 185

	return playlist;
}

186
static SongEnumerator *
187
playlist_list_open_stream_mime2(InputStream &is, const char *mime)
188
{
189
	assert(mime != nullptr);
190

191
	playlist_plugins_for_each_enabled(plugin) {
192 193
		if (plugin->open_stream != nullptr &&
		    plugin->mime_types != nullptr &&
194
		    string_array_contains(plugin->mime_types, mime)) {
195 196
			/* rewind the stream, so each plugin gets a
			   fresh start */
197
			is.Rewind(IgnoreError());
198

199 200
			auto playlist = playlist_plugin_open_stream(plugin,
								    is);
201
			if (playlist != nullptr)
202 203 204 205
				return playlist;
		}
	}

206
	return nullptr;
207 208
}

209
static SongEnumerator *
210
playlist_list_open_stream_mime(InputStream &is, const char *full_mime)
211
{
212
	assert(full_mime != nullptr);
213

214
	const char *semicolon = strchr(full_mime, ';');
215
	if (semicolon == nullptr)
216
		return playlist_list_open_stream_mime2(is, full_mime);
217

218
	if (semicolon == full_mime)
219
		return nullptr;
220 221

	/* probe only the portion before the semicolon*/
222 223
	const std::string mime(full_mime, semicolon);
	return playlist_list_open_stream_mime2(is, mime.c_str());
224 225
}

226
SongEnumerator *
227
playlist_list_open_stream_suffix(InputStream &is, const char *suffix)
228
{
229
	assert(suffix != nullptr);
230

231
	playlist_plugins_for_each_enabled(plugin) {
232 233
		if (plugin->open_stream != nullptr &&
		    plugin->suffixes != nullptr &&
234
		    string_array_contains(plugin->suffixes, suffix)) {
235 236
			/* rewind the stream, so each plugin gets a
			   fresh start */
237
			is.Rewind(IgnoreError());
238

239
			auto playlist = playlist_plugin_open_stream(plugin, is);
240
			if (playlist != nullptr)
241 242 243 244
				return playlist;
		}
	}

245
	return nullptr;
246 247
}

248
SongEnumerator *
249
playlist_list_open_stream(InputStream &is, const char *uri)
250
{
251
	assert(is.IsReady());
252

253
	const char *const mime = is.GetMimeType();
254
	if (mime != nullptr) {
255
		auto playlist = playlist_list_open_stream_mime(is, mime);
256
		if (playlist != nullptr)
257 258 259
			return playlist;
	}

260
	const char *suffix = uri != nullptr ? uri_get_suffix(uri) : nullptr;
261
	if (suffix != nullptr) {
262
		auto playlist = playlist_list_open_stream_suffix(is, suffix);
263
		if (playlist != nullptr)
264 265 266
			return playlist;
	}

267
	return nullptr;
268
}
269

270
bool
271 272
playlist_suffix_supported(const char *suffix)
{
273
	assert(suffix != nullptr);
274

275
	playlist_plugins_for_each_enabled(plugin) {
276
		if (plugin->suffixes != nullptr &&
277
		    string_array_contains(plugin->suffixes, suffix))
278 279 280 281 282
			return true;
	}

	return false;
}