2012-02-12 15:20:38 +01:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 The Music Player Daemon Project
|
2012-02-12 15:20:38 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-01-26 01:18:12 +01:00
|
|
|
#include "TagFile.hxx"
|
2016-02-26 13:18:32 +01:00
|
|
|
#include "tag/Generic.hxx"
|
2017-02-08 08:26:58 +01:00
|
|
|
#include "tag/Handler.hxx"
|
|
|
|
#include "tag/Builder.hxx"
|
2013-10-26 15:14:54 +02:00
|
|
|
#include "fs/Path.hxx"
|
2014-01-24 00:02:24 +01:00
|
|
|
#include "decoder/DecoderList.hxx"
|
|
|
|
#include "decoder/DecoderPlugin.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "input/InputStream.hxx"
|
2014-10-02 21:43:40 +02:00
|
|
|
#include "input/LocalOpen.hxx"
|
2012-02-12 15:20:38 +01:00
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2012-02-12 15:20:38 +01:00
|
|
|
|
2013-12-29 16:13:11 +01:00
|
|
|
class TagFileScan {
|
|
|
|
const Path path_fs;
|
|
|
|
const char *const suffix;
|
|
|
|
|
2018-07-05 19:07:05 +02:00
|
|
|
TagHandler &handler;
|
2013-12-29 16:13:11 +01:00
|
|
|
|
|
|
|
Mutex mutex;
|
2016-02-21 08:03:32 +01:00
|
|
|
InputStreamPtr is;
|
2013-12-29 16:13:11 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
TagFileScan(Path _path_fs, const char *_suffix,
|
2018-07-05 19:07:05 +02:00
|
|
|
TagHandler &_handler) noexcept
|
2013-12-29 16:13:11 +01:00
|
|
|
:path_fs(_path_fs), suffix(_suffix),
|
2018-07-05 19:07:05 +02:00
|
|
|
handler(_handler),
|
2013-12-29 16:13:11 +01:00
|
|
|
is(nullptr) {}
|
|
|
|
|
2018-01-21 11:42:04 +01:00
|
|
|
bool ScanFile(const DecoderPlugin &plugin) noexcept {
|
2018-07-05 19:07:05 +02:00
|
|
|
return plugin.ScanFile(path_fs, handler);
|
2013-12-29 16:13:11 +01:00
|
|
|
}
|
|
|
|
|
2019-05-21 23:24:44 +02:00
|
|
|
bool ScanStream(const DecoderPlugin &plugin) {
|
2013-12-29 16:13:11 +01:00
|
|
|
if (plugin.scan_stream == nullptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* open the InputStream (if not already open) */
|
|
|
|
if (is == nullptr) {
|
2019-05-21 23:24:44 +02:00
|
|
|
is = OpenLocalInputStream(path_fs, mutex);
|
2016-09-09 18:47:42 +02:00
|
|
|
} else {
|
2019-05-21 23:24:44 +02:00
|
|
|
is->LockRewind();
|
2016-09-09 18:47:42 +02:00
|
|
|
}
|
2013-12-29 16:13:11 +01:00
|
|
|
|
|
|
|
/* now try the stream_tag() method */
|
2018-07-05 19:07:05 +02:00
|
|
|
return plugin.ScanStream(*is, handler);
|
2013-12-29 16:13:11 +01:00
|
|
|
}
|
|
|
|
|
2019-05-21 23:24:44 +02:00
|
|
|
bool Scan(const DecoderPlugin &plugin) {
|
2013-12-29 16:13:11 +01:00
|
|
|
return plugin.SupportsSuffix(suffix) &&
|
|
|
|
(ScanFile(plugin) || ScanStream(plugin));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-12 15:20:38 +01:00
|
|
|
bool
|
2019-05-21 23:24:44 +02:00
|
|
|
ScanFileTagsNoGeneric(Path path_fs, TagHandler &handler)
|
2012-02-12 15:20:38 +01:00
|
|
|
{
|
2013-10-26 15:14:54 +02:00
|
|
|
assert(!path_fs.IsNull());
|
2012-02-12 15:20:38 +01:00
|
|
|
|
|
|
|
/* check if there's a suffix and a plugin */
|
|
|
|
|
2015-03-05 08:57:25 +01:00
|
|
|
const auto *suffix = path_fs.GetSuffix();
|
2013-10-19 18:19:03 +02:00
|
|
|
if (suffix == nullptr)
|
2012-02-12 15:20:38 +01:00
|
|
|
return false;
|
|
|
|
|
2015-03-05 08:57:25 +01:00
|
|
|
const auto suffix_utf8 = Path::FromFS(suffix).ToUTF8();
|
|
|
|
|
2018-07-05 19:07:05 +02:00
|
|
|
TagFileScan tfs(path_fs, suffix_utf8.c_str(), handler);
|
2013-12-29 16:13:11 +01:00
|
|
|
return decoder_plugins_try([&](const DecoderPlugin &plugin){
|
|
|
|
return tfs.Scan(plugin);
|
|
|
|
});
|
2012-02-12 15:20:38 +01:00
|
|
|
}
|
2016-02-26 13:18:32 +01:00
|
|
|
|
|
|
|
bool
|
2018-07-06 22:46:03 +02:00
|
|
|
ScanFileTagsWithGeneric(Path path, TagBuilder &builder,
|
2019-05-21 23:24:44 +02:00
|
|
|
AudioFormat *audio_format)
|
2016-02-26 13:18:32 +01:00
|
|
|
{
|
2018-07-06 22:46:03 +02:00
|
|
|
FullTagHandler h(builder, audio_format);
|
2018-07-05 19:07:05 +02:00
|
|
|
|
2018-07-06 22:33:35 +02:00
|
|
|
if (!ScanFileTagsNoGeneric(path, h))
|
2016-02-26 13:18:32 +01:00
|
|
|
return false;
|
|
|
|
|
2017-11-10 19:24:33 +01:00
|
|
|
if (builder.empty())
|
2018-07-05 19:07:05 +02:00
|
|
|
ScanGenericTags(path, h);
|
2016-02-26 13:18:32 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|