TagFile.cxx 2.61 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 "TagFile.hxx"
21
#include "tag/Generic.hxx"
22 23
#include "tag/Handler.hxx"
#include "tag/Builder.hxx"
24
#include "fs/Path.hxx"
25 26
#include "decoder/DecoderList.hxx"
#include "decoder/DecoderPlugin.hxx"
Max Kellermann's avatar
Max Kellermann committed
27
#include "input/InputStream.hxx"
28
#include "input/LocalOpen.hxx"
29

30
#include <cassert>
31

32 33 34 35
class TagFileScan {
	const Path path_fs;
	const char *const suffix;

36
	TagHandler &handler;
37 38

	Mutex mutex;
39
	InputStreamPtr is;
40 41 42

public:
	TagFileScan(Path _path_fs, const char *_suffix,
43
		    TagHandler &_handler) noexcept
44
		:path_fs(_path_fs), suffix(_suffix),
45
		 handler(_handler),
46 47
		 is(nullptr) {}

48
	bool ScanFile(const DecoderPlugin &plugin) noexcept {
49
		return plugin.ScanFile(path_fs, handler);
50 51
	}

52
	bool ScanStream(const DecoderPlugin &plugin) {
53 54 55 56 57
		if (plugin.scan_stream == nullptr)
			return false;

		/* open the InputStream (if not already open) */
		if (is == nullptr) {
58
			is = OpenLocalInputStream(path_fs, mutex);
59
		} else {
60
			is->LockRewind();
61
		}
62 63

		/* now try the stream_tag() method */
64
		return plugin.ScanStream(*is, handler);
65 66
	}

67
	bool Scan(const DecoderPlugin &plugin) {
68 69 70 71 72
		return plugin.SupportsSuffix(suffix) &&
			(ScanFile(plugin) || ScanStream(plugin));
	}
};

73
bool
74
ScanFileTagsNoGeneric(Path path_fs, TagHandler &handler)
75
{
76
	assert(!path_fs.IsNull());
77 78 79

	/* check if there's a suffix and a plugin */

80
	const auto *suffix = path_fs.GetSuffix();
81
	if (suffix == nullptr)
82 83
		return false;

84 85
	const auto suffix_utf8 = Path::FromFS(suffix).ToUTF8();

86
	TagFileScan tfs(path_fs, suffix_utf8.c_str(), handler);
87 88 89
	return decoder_plugins_try([&](const DecoderPlugin &plugin){
			return tfs.Scan(plugin);
		});
90
}
91 92

bool
93
ScanFileTagsWithGeneric(Path path, TagBuilder &builder,
94
			AudioFormat *audio_format)
95
{
96
	FullTagHandler h(builder, audio_format);
97

98
	if (!ScanFileTagsNoGeneric(path, h))
99 100
		return false;

101
	if (builder.empty())
102
		ScanGenericTags(path, h);
103 104 105

	return true;
}