2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2019-04-05 12:38:49 +02:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "FingerprintCommands.hxx"
|
|
|
|
#include "Request.hxx"
|
|
|
|
#include "LocateUri.hxx"
|
|
|
|
#include "lib/chromaprint/DecoderClient.hxx"
|
|
|
|
#include "decoder/DecoderAPI.hxx"
|
|
|
|
#include "decoder/DecoderList.hxx"
|
|
|
|
#include "storage/StorageInterface.hxx"
|
|
|
|
#include "client/Client.hxx"
|
|
|
|
#include "client/Response.hxx"
|
|
|
|
#include "client/ThreadBackgroundCommand.hxx"
|
|
|
|
#include "input/InputStream.hxx"
|
|
|
|
#include "input/LocalOpen.hxx"
|
|
|
|
#include "input/Handler.hxx"
|
|
|
|
#include "thread/Mutex.hxx"
|
|
|
|
#include "thread/Cond.hxx"
|
|
|
|
#include "system/Error.hxx"
|
|
|
|
#include "util/MimeType.hxx"
|
2019-08-09 15:54:13 +02:00
|
|
|
#include "util/UriExtract.hxx"
|
2019-04-05 12:38:49 +02:00
|
|
|
|
2021-05-21 20:35:29 +02:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2019-04-05 12:38:49 +02:00
|
|
|
class GetChromaprintCommand final
|
|
|
|
: public ThreadBackgroundCommand, ChromaprintDecoderClient, InputStreamHandler
|
|
|
|
{
|
|
|
|
Mutex mutex;
|
|
|
|
Cond cond;
|
|
|
|
|
|
|
|
const std::string uri;
|
|
|
|
const AllocatedPath path;
|
|
|
|
|
|
|
|
bool cancel = false;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GetChromaprintCommand(Client &_client, std::string &&_uri,
|
|
|
|
AllocatedPath &&_path) noexcept
|
|
|
|
:ThreadBackgroundCommand(_client),
|
|
|
|
uri(std::move(_uri)), path(std::move(_path))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void Run() override;
|
|
|
|
|
|
|
|
void SendResponse(Response &r) noexcept override {
|
2021-05-21 20:35:29 +02:00
|
|
|
r.Fmt(FMT_STRING("chromaprint: {}\n"),
|
|
|
|
GetFingerprint());
|
2019-04-05 12:38:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CancelThread() noexcept override {
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> lock(mutex);
|
2019-04-05 12:38:49 +02:00
|
|
|
cancel = true;
|
2019-04-25 18:33:09 +02:00
|
|
|
cond.notify_one();
|
2019-04-05 12:38:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void DecodeStream(InputStream &is, const DecoderPlugin &plugin);
|
2020-11-04 20:39:06 +01:00
|
|
|
bool DecodeStream(InputStream &is, std::string_view suffix,
|
2019-04-05 12:38:49 +02:00
|
|
|
const DecoderPlugin &plugin);
|
|
|
|
void DecodeStream(InputStream &is);
|
2020-11-04 20:39:06 +01:00
|
|
|
bool DecodeContainer(std::string_view suffix, const DecoderPlugin &plugin);
|
|
|
|
bool DecodeContainer(std::string_view suffix);
|
|
|
|
bool DecodeFile(std::string_view suffix, InputStream &is,
|
2019-04-05 12:38:49 +02:00
|
|
|
const DecoderPlugin &plugin);
|
|
|
|
void DecodeFile();
|
|
|
|
|
|
|
|
/* virtual methods from class DecoderClient */
|
|
|
|
InputStreamPtr OpenUri(const char *uri) override;
|
2019-07-05 08:35:39 +02:00
|
|
|
size_t Read(InputStream &is,
|
|
|
|
void *buffer, size_t length) noexcept override;
|
2019-04-05 12:38:49 +02:00
|
|
|
|
|
|
|
/* virtual methods from class InputStreamHandler */
|
|
|
|
void OnInputStreamReady() noexcept override {
|
2019-04-25 18:33:09 +02:00
|
|
|
cond.notify_one();
|
2019-04-05 12:38:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnInputStreamAvailable() noexcept override {
|
2019-04-25 18:33:09 +02:00
|
|
|
cond.notify_one();
|
2019-04-05 12:38:49 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline void
|
|
|
|
GetChromaprintCommand::DecodeStream(InputStream &input_stream,
|
|
|
|
const DecoderPlugin &plugin)
|
|
|
|
{
|
|
|
|
assert(plugin.stream_decode != nullptr);
|
|
|
|
assert(input_stream.IsReady());
|
|
|
|
|
|
|
|
if (cancel)
|
|
|
|
throw StopDecoder();
|
|
|
|
|
|
|
|
/* rewind the stream, so each plugin gets a fresh start */
|
|
|
|
try {
|
2019-05-07 19:09:13 +02:00
|
|
|
input_stream.LockRewind();
|
2019-04-05 12:38:49 +02:00
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin.StreamDecode(*this, input_stream);
|
|
|
|
}
|
|
|
|
|
2023-03-06 15:57:36 +01:00
|
|
|
[[gnu::pure]]
|
2019-04-05 12:38:49 +02:00
|
|
|
static bool
|
|
|
|
decoder_check_plugin_mime(const DecoderPlugin &plugin,
|
|
|
|
const InputStream &is) noexcept
|
|
|
|
{
|
|
|
|
assert(plugin.stream_decode != nullptr);
|
|
|
|
|
|
|
|
const char *mime_type = is.GetMimeType();
|
|
|
|
return mime_type != nullptr &&
|
2020-11-04 20:29:25 +01:00
|
|
|
plugin.SupportsMimeType(GetMimeTypeBase(mime_type));
|
2019-04-05 12:38:49 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 15:57:36 +01:00
|
|
|
[[gnu::pure]]
|
2019-04-05 12:38:49 +02:00
|
|
|
static bool
|
|
|
|
decoder_check_plugin_suffix(const DecoderPlugin &plugin,
|
2020-11-04 20:39:06 +01:00
|
|
|
std::string_view suffix) noexcept
|
2019-04-05 12:38:49 +02:00
|
|
|
{
|
|
|
|
assert(plugin.stream_decode != nullptr);
|
|
|
|
|
2020-11-04 20:39:06 +01:00
|
|
|
return !suffix.empty() && plugin.SupportsSuffix(suffix);
|
2019-04-05 12:38:49 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 15:57:36 +01:00
|
|
|
[[gnu::pure]]
|
2019-04-05 12:38:49 +02:00
|
|
|
static bool
|
|
|
|
decoder_check_plugin(const DecoderPlugin &plugin, const InputStream &is,
|
2020-11-04 20:39:06 +01:00
|
|
|
std::string_view suffix) noexcept
|
2019-04-05 12:38:49 +02:00
|
|
|
{
|
|
|
|
return plugin.stream_decode != nullptr &&
|
|
|
|
(decoder_check_plugin_mime(plugin, is) ||
|
|
|
|
decoder_check_plugin_suffix(plugin, suffix));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
GetChromaprintCommand::DecodeStream(InputStream &is,
|
2020-11-04 20:39:06 +01:00
|
|
|
std::string_view suffix,
|
2019-04-05 12:38:49 +02:00
|
|
|
const DecoderPlugin &plugin)
|
|
|
|
{
|
|
|
|
if (!decoder_check_plugin(plugin, is, suffix))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ChromaprintDecoderClient::Reset();
|
|
|
|
|
|
|
|
DecodeStream(is, plugin);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
GetChromaprintCommand::DecodeStream(InputStream &is)
|
|
|
|
{
|
2020-11-04 21:10:58 +01:00
|
|
|
const auto suffix = uri_get_suffix(uri);
|
2019-04-05 12:38:49 +02:00
|
|
|
|
|
|
|
decoder_plugins_try([this, &is, suffix](const DecoderPlugin &plugin){
|
|
|
|
return DecodeStream(is, suffix, plugin);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2020-11-04 20:39:06 +01:00
|
|
|
GetChromaprintCommand::DecodeContainer(std::string_view suffix,
|
2019-04-05 12:38:49 +02:00
|
|
|
const DecoderPlugin &plugin)
|
|
|
|
{
|
|
|
|
if (plugin.container_scan == nullptr ||
|
|
|
|
plugin.file_decode == nullptr ||
|
|
|
|
!plugin.SupportsSuffix(suffix))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ChromaprintDecoderClient::Reset();
|
|
|
|
|
|
|
|
plugin.FileDecode(*this, path);
|
|
|
|
return IsReady();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2020-11-04 20:39:06 +01:00
|
|
|
GetChromaprintCommand::DecodeContainer(std::string_view suffix)
|
2019-04-05 12:38:49 +02:00
|
|
|
{
|
|
|
|
return decoder_plugins_try([this, suffix](const DecoderPlugin &plugin){
|
|
|
|
return DecodeContainer(suffix, plugin);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2020-11-04 20:39:06 +01:00
|
|
|
GetChromaprintCommand::DecodeFile(std::string_view suffix, InputStream &is,
|
2019-04-05 12:38:49 +02:00
|
|
|
const DecoderPlugin &plugin)
|
|
|
|
{
|
|
|
|
if (!plugin.SupportsSuffix(suffix))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
{
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2019-04-05 12:38:49 +02:00
|
|
|
if (cancel)
|
|
|
|
throw StopDecoder();
|
|
|
|
}
|
|
|
|
|
|
|
|
ChromaprintDecoderClient::Reset();
|
|
|
|
|
|
|
|
if (plugin.file_decode != nullptr) {
|
|
|
|
plugin.FileDecode(*this, path);
|
|
|
|
return IsReady();
|
|
|
|
} else if (plugin.stream_decode != nullptr) {
|
|
|
|
plugin.StreamDecode(*this, is);
|
|
|
|
return IsReady();
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
GetChromaprintCommand::DecodeFile()
|
|
|
|
{
|
2021-10-28 15:04:30 +02:00
|
|
|
const char *_suffix = PathTraitsUTF8::GetFilenameSuffix(uri.c_str());
|
|
|
|
if (_suffix == nullptr)
|
2019-04-05 12:38:49 +02:00
|
|
|
return;
|
|
|
|
|
2021-10-28 15:04:30 +02:00
|
|
|
const std::string_view suffix{_suffix};
|
|
|
|
|
2019-04-05 12:38:49 +02:00
|
|
|
InputStreamPtr input_stream;
|
|
|
|
|
|
|
|
try {
|
|
|
|
input_stream = OpenLocalInputStream(path, mutex);
|
|
|
|
} catch (const std::system_error &e) {
|
|
|
|
if (IsPathNotFound(e) &&
|
|
|
|
/* ENOTDIR means this may be a path inside a
|
|
|
|
"container" file */
|
|
|
|
DecodeContainer(suffix))
|
|
|
|
return;
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(input_stream);
|
|
|
|
|
|
|
|
auto &is = *input_stream;
|
|
|
|
decoder_plugins_try([this, suffix, &is](const DecoderPlugin &plugin){
|
|
|
|
return DecodeFile(suffix, is, plugin);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GetChromaprintCommand::Run()
|
|
|
|
try {
|
|
|
|
if (!path.IsNull())
|
|
|
|
DecodeFile();
|
|
|
|
else
|
|
|
|
DecodeStream(*OpenUri(uri.c_str()));
|
|
|
|
|
|
|
|
ChromaprintDecoderClient::Finish();
|
|
|
|
} catch (StopDecoder) {
|
|
|
|
}
|
|
|
|
|
|
|
|
InputStreamPtr
|
|
|
|
GetChromaprintCommand::OpenUri(const char *uri2)
|
|
|
|
{
|
|
|
|
if (cancel)
|
|
|
|
throw StopDecoder();
|
|
|
|
|
|
|
|
auto is = InputStream::Open(uri2, mutex);
|
|
|
|
is->SetHandler(this);
|
|
|
|
|
2019-04-25 18:53:38 +02:00
|
|
|
std::unique_lock<Mutex> lock(mutex);
|
2019-04-05 12:38:49 +02:00
|
|
|
while (true) {
|
|
|
|
if (cancel)
|
|
|
|
throw StopDecoder();
|
|
|
|
|
|
|
|
is->Update();
|
|
|
|
if (is->IsReady()) {
|
|
|
|
is->Check();
|
|
|
|
return is;
|
|
|
|
}
|
|
|
|
|
2019-04-25 18:53:38 +02:00
|
|
|
cond.wait(lock);
|
2019-04-05 12:38:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2019-07-05 08:35:39 +02:00
|
|
|
GetChromaprintCommand::Read(InputStream &is,
|
|
|
|
void *buffer, size_t length) noexcept
|
2019-04-05 12:38:49 +02:00
|
|
|
{
|
|
|
|
/* overriding ChromaprintDecoderClient's implementation to
|
|
|
|
make it cancellable */
|
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
return 0;
|
|
|
|
|
2019-04-25 18:53:38 +02:00
|
|
|
std::unique_lock<Mutex> lock(mutex);
|
2019-04-05 12:38:49 +02:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (cancel)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (is.IsAvailable())
|
|
|
|
break;
|
|
|
|
|
2019-04-25 18:53:38 +02:00
|
|
|
cond.wait(lock);
|
2019-04-05 12:38:49 +02:00
|
|
|
}
|
|
|
|
|
2019-07-05 08:36:14 +02:00
|
|
|
try {
|
|
|
|
return is.Read(lock, buffer, length);
|
|
|
|
} catch (...) {
|
|
|
|
ChromaprintDecoderClient::error = std::current_exception();
|
|
|
|
return 0;
|
|
|
|
}
|
2019-04-05 12:38:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CommandResult
|
|
|
|
handle_getfingerprint(Client &client, Request args, Response &)
|
|
|
|
{
|
|
|
|
const char *_uri = args.front();
|
|
|
|
|
2019-04-18 11:48:22 +02:00
|
|
|
auto lu = LocateUri(UriPluginKind::INPUT, _uri, &client
|
2019-04-05 12:38:49 +02:00
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
, nullptr
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
|
|
|
|
std::string uri = lu.canonical_uri;
|
|
|
|
|
|
|
|
switch (lu.type) {
|
|
|
|
case LocatedUri::Type::ABSOLUTE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LocatedUri::Type::PATH:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LocatedUri::Type::RELATIVE:
|
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
{
|
|
|
|
const auto *storage = client.GetStorage();
|
|
|
|
if (storage == nullptr)
|
|
|
|
throw ProtocolError(ACK_ERROR_NO_EXIST, "No database");
|
|
|
|
|
|
|
|
lu.path = storage->MapFS(lu.canonical_uri);
|
|
|
|
if (lu.path.IsNull()) {
|
|
|
|
uri = storage->MapUTF8(lu.canonical_uri);
|
2022-08-09 11:43:33 +02:00
|
|
|
if (!uri_has_scheme(uri))
|
2019-04-05 12:38:49 +02:00
|
|
|
throw ProtocolError(ACK_ERROR_NO_EXIST, "No such song");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
throw ProtocolError(ACK_ERROR_NO_EXIST, "No database");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auto cmd = std::make_unique<GetChromaprintCommand>(client,
|
|
|
|
std::move(uri),
|
|
|
|
std::move(lu.path));
|
|
|
|
cmd->Start();
|
|
|
|
client.SetBackgroundCommand(std::move(cmd));
|
|
|
|
return CommandResult::BACKGROUND;
|
|
|
|
}
|