mpd/src/playlist/PlaylistRegistry.cxx

290 lines
7.4 KiB
C++
Raw Normal View History

/*
2017-01-03 20:48:59 +01:00
* Copyright 2003-2017 The Music Player Daemon Project
* 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.
*/
#include "config.h"
2013-01-26 01:04:02 +01:00
#include "PlaylistRegistry.hxx"
2013-01-27 17:38:09 +01:00
#include "PlaylistPlugin.hxx"
#include "SongEnumerator.hxx"
2014-01-23 23:30:12 +01:00
#include "plugins/ExtM3uPlaylistPlugin.hxx"
#include "plugins/M3uPlaylistPlugin.hxx"
#include "plugins/XspfPlaylistPlugin.hxx"
#include "plugins/SoundCloudPlaylistPlugin.hxx"
#include "plugins/PlsPlaylistPlugin.hxx"
#include "plugins/AsxPlaylistPlugin.hxx"
#include "plugins/RssPlaylistPlugin.hxx"
#include "plugins/FlacPlaylistPlugin.hxx"
2014-01-23 23:30:12 +01:00
#include "plugins/CuePlaylistPlugin.hxx"
#include "plugins/EmbeddedCuePlaylistPlugin.hxx"
2014-01-24 16:18:21 +01:00
#include "input/InputStream.hxx"
#include "util/MimeType.hxx"
2013-04-08 23:30:21 +02:00
#include "util/UriUtil.hxx"
2013-04-09 01:08:20 +02:00
#include "util/StringUtil.hxx"
#include "util/Macros.hxx"
#include "config/Data.hxx"
#include "config/Block.hxx"
#include <assert.h>
#include <string.h>
2012-06-12 22:12:06 +02:00
const struct playlist_plugin *const playlist_plugins[] = {
&extm3u_playlist_plugin,
&m3u_playlist_plugin,
2009-10-15 00:08:06 +02:00
&pls_playlist_plugin,
#ifdef ENABLE_EXPAT
&xspf_playlist_plugin,
&asx_playlist_plugin,
&rss_playlist_plugin,
#endif
#ifdef ENABLE_SOUNDCLOUD
&soundcloud_playlist_plugin,
#endif
#ifdef ENABLE_FLAC
&flac_playlist_plugin,
#endif
#ifdef ENABLE_CUE
&cue_playlist_plugin,
&embcue_playlist_plugin,
#endif
2013-10-02 08:13:28 +02:00
nullptr
};
static constexpr unsigned n_playlist_plugins =
ARRAY_SIZE(playlist_plugins) - 1;
/** which plugins have been initialized successfully? */
static bool playlist_plugins_enabled[n_playlist_plugins];
2012-06-12 22:12:06 +02:00
#define playlist_plugins_for_each_enabled(plugin) \
playlist_plugins_for_each(plugin) \
if (playlist_plugins_enabled[playlist_plugin_iterator - playlist_plugins])
void
playlist_list_global_init(const ConfigData &config)
{
const ConfigBlock empty;
2013-10-02 08:13:28 +02:00
for (unsigned i = 0; playlist_plugins[i] != nullptr; ++i) {
const struct playlist_plugin *plugin = playlist_plugins[i];
const auto *param =
config.FindBlock(ConfigBlockOption::PLAYLIST_PLUGIN,
"name", plugin->name);
if (param == nullptr)
param = &empty;
else if (!param->GetBlockValue("enabled", true))
/* the plugin is disabled in mpd.conf */
continue;
if (param != nullptr)
param->SetUsed();
playlist_plugins_enabled[i] =
playlist_plugin_init(playlist_plugins[i], *param);
}
}
void
2018-01-21 11:24:17 +01:00
playlist_list_global_finish() noexcept
{
2012-06-12 22:12:06 +02:00
playlist_plugins_for_each_enabled(plugin)
playlist_plugin_finish(plugin);
}
static std::unique_ptr<SongEnumerator>
playlist_list_open_uri_scheme(const char *uri, Mutex &mutex,
bool *tried)
{
2013-10-02 08:13:28 +02:00
assert(uri != nullptr);
const auto scheme = uri_get_scheme(uri);
if (scheme.empty())
2013-10-02 08:13:28 +02:00
return nullptr;
2013-10-02 08:13:28 +02:00
for (unsigned i = 0; playlist_plugins[i] != nullptr; ++i) {
const struct playlist_plugin *plugin = playlist_plugins[i];
assert(!tried[i]);
2013-10-02 08:13:28 +02:00
if (playlist_plugins_enabled[i] && plugin->open_uri != nullptr &&
plugin->schemes != nullptr &&
2016-06-10 22:08:13 +02:00
StringArrayContainsCase(plugin->schemes, scheme.c_str())) {
auto playlist = plugin->open_uri(uri, mutex);
if (playlist)
return playlist;
tried[i] = true;
}
}
return nullptr;
}
static std::unique_ptr<SongEnumerator>
playlist_list_open_uri_suffix(const char *uri, Mutex &mutex,
const bool *tried)
{
2013-10-02 08:13:28 +02:00
assert(uri != nullptr);
UriSuffixBuffer suffix_buffer;
const char *const suffix = uri_get_suffix(uri, suffix_buffer);
2013-10-02 08:13:28 +02:00
if (suffix == nullptr)
return nullptr;
2013-10-02 08:13:28 +02:00
for (unsigned i = 0; playlist_plugins[i] != nullptr; ++i) {
const struct playlist_plugin *plugin = playlist_plugins[i];
if (playlist_plugins_enabled[i] && !tried[i] &&
2013-10-02 08:13:28 +02:00
plugin->open_uri != nullptr && plugin->suffixes != nullptr &&
2016-06-10 22:08:13 +02:00
StringArrayContainsCase(plugin->suffixes, suffix)) {
auto playlist = plugin->open_uri(uri, mutex);
2013-10-02 08:13:28 +02:00
if (playlist != nullptr)
return playlist;
}
}
return nullptr;
}
std::unique_ptr<SongEnumerator>
playlist_list_open_uri(const char *uri, Mutex &mutex)
{
/** this array tracks which plugins have already been tried by
playlist_list_open_uri_scheme() */
bool tried[n_playlist_plugins];
2013-10-02 08:13:28 +02:00
assert(uri != nullptr);
memset(tried, false, sizeof(tried));
auto playlist = playlist_list_open_uri_scheme(uri, mutex, tried);
2013-10-02 08:13:28 +02:00
if (playlist == nullptr)
playlist = playlist_list_open_uri_suffix(uri, mutex,
tried);
return playlist;
}
static std::unique_ptr<SongEnumerator>
playlist_list_open_stream_mime2(InputStreamPtr &&is, const char *mime)
{
2013-10-02 08:13:28 +02:00
assert(mime != nullptr);
2012-06-12 22:12:06 +02:00
playlist_plugins_for_each_enabled(plugin) {
2013-10-02 08:13:28 +02:00
if (plugin->open_stream != nullptr &&
plugin->mime_types != nullptr &&
2016-06-10 22:08:13 +02:00
StringArrayContainsCase(plugin->mime_types, mime)) {
/* rewind the stream, so each plugin gets a
fresh start */
try {
is->LockRewind();
} catch (...) {
}
auto playlist = plugin->open_stream(std::move(is));
2013-10-02 08:13:28 +02:00
if (playlist != nullptr)
return playlist;
}
}
2013-10-02 08:13:28 +02:00
return nullptr;
}
static std::unique_ptr<SongEnumerator>
playlist_list_open_stream_mime(InputStreamPtr &&is, const char *full_mime)
{
2013-10-02 08:13:28 +02:00
assert(full_mime != nullptr);
const char *semicolon = strchr(full_mime, ';');
2013-10-02 08:13:28 +02:00
if (semicolon == nullptr)
return playlist_list_open_stream_mime2(std::move(is),
full_mime);
if (semicolon == full_mime)
2013-10-02 08:13:28 +02:00
return nullptr;
/* probe only the portion before the semicolon*/
const std::string mime(full_mime, semicolon);
return playlist_list_open_stream_mime2(std::move(is), mime.c_str());
}
std::unique_ptr<SongEnumerator>
playlist_list_open_stream_suffix(InputStreamPtr &&is, const char *suffix)
{
2013-10-02 08:13:28 +02:00
assert(suffix != nullptr);
2012-06-12 22:12:06 +02:00
playlist_plugins_for_each_enabled(plugin) {
2013-10-02 08:13:28 +02:00
if (plugin->open_stream != nullptr &&
plugin->suffixes != nullptr &&
2016-06-10 22:08:13 +02:00
StringArrayContainsCase(plugin->suffixes, suffix)) {
/* rewind the stream, so each plugin gets a
fresh start */
try {
is->LockRewind();
} catch (...) {
}
auto playlist = plugin->open_stream(std::move(is));
2013-10-02 08:13:28 +02:00
if (playlist != nullptr)
return playlist;
}
}
2013-10-02 08:13:28 +02:00
return nullptr;
}
std::unique_ptr<SongEnumerator>
playlist_list_open_stream(InputStreamPtr &&is, const char *uri)
{
assert(is->IsReady());
const char *const mime = is->GetMimeType();
2013-10-02 08:13:28 +02:00
if (mime != nullptr) {
auto playlist = playlist_list_open_stream_mime(std::move(is),
GetMimeTypeBase(mime).c_str());
2013-10-02 08:13:28 +02:00
if (playlist != nullptr)
return playlist;
}
UriSuffixBuffer suffix_buffer;
const char *suffix = uri != nullptr
? uri_get_suffix(uri, suffix_buffer)
: nullptr;
2013-10-02 08:13:28 +02:00
if (suffix != nullptr) {
auto playlist = playlist_list_open_stream_suffix(std::move(is),
suffix);
2013-10-02 08:13:28 +02:00
if (playlist != nullptr)
return playlist;
}
2013-10-02 08:13:28 +02:00
return nullptr;
}
bool
2018-01-21 11:24:17 +01:00
playlist_suffix_supported(const char *suffix) noexcept
{
2013-10-02 08:13:28 +02:00
assert(suffix != nullptr);
2012-06-12 22:12:06 +02:00
playlist_plugins_for_each_enabled(plugin) {
2013-10-02 08:13:28 +02:00
if (plugin->suffixes != nullptr &&
2016-06-10 22:08:13 +02:00
StringArrayContainsCase(plugin->suffixes, suffix))
return true;
}
return false;
}