TagStream.cxx 2.64 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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 "TagStream.hxx"
21
#include "tag/Generic.hxx"
22 23
#include "tag/Handler.hxx"
#include "tag/Builder.hxx"
24
#include "util/MimeType.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 "thread/Mutex.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "util/UriExtract.hxx"
30

31
#include <cassert>
32 33 34 35 36 37 38

/**
 * Does the #DecoderPlugin support either the suffix or the MIME type?
 */
gcc_pure
static bool
CheckDecoderPlugin(const DecoderPlugin &plugin,
39
		   std::string_view suffix, std::string_view mime) noexcept
40
{
41
	return (!mime.empty() && plugin.SupportsMimeType(mime)) ||
42
		(!suffix.empty() && plugin.SupportsSuffix(suffix));
43 44 45
}

bool
46
tag_stream_scan(InputStream &is, TagHandler &handler)
47
{
48
	assert(is.IsReady());
49

50
	const auto suffix = uri_get_suffix(is.GetURI());
51
	const char *full_mime = is.GetMimeType();
52

53
	if (suffix.empty() && full_mime == nullptr)
54 55
		return false;

56 57 58
	std::string_view mime_base{};
	if (full_mime != nullptr)
		mime_base = GetMimeTypeBase(full_mime);
59

60
	return decoder_plugins_try([suffix, mime_base, &is,
61
				    &handler](const DecoderPlugin &plugin){
62 63
			try {
				is.LockRewind();
64
			} catch (...) {
65
			}
66

67
			return CheckDecoderPlugin(plugin, suffix, mime_base) &&
68
				plugin.ScanStream(is, handler);
69 70 71 72
		});
}

bool
73 74
tag_stream_scan(const char *uri, TagHandler &handler)
{
75 76
	Mutex mutex;

77
	auto is = InputStream::OpenReady(uri, mutex);
78
	return tag_stream_scan(*is, handler);
79
}
80 81

bool
82
tag_stream_scan(InputStream &is, TagBuilder &builder,
83
		AudioFormat *audio_format)
84 85 86
{
	assert(is.IsReady());

87
	FullTagHandler h(builder, audio_format);
88 89

	if (!tag_stream_scan(is, h))
90 91
		return false;

92
	if (builder.empty())
93
		ScanGenericTags(is, h);
94 95 96 97 98

	return true;
}

bool
99
tag_stream_scan(const char *uri, TagBuilder &builder,
100 101
		AudioFormat *audio_format)
{
102 103
	Mutex mutex;

104
	auto is = InputStream::OpenReady(uri, mutex);
105
	return tag_stream_scan(*is, builder, audio_format);
106
}