TagStream.cxx 2.69 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 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 32 33 34 35 36 37 38

#include <assert.h>

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

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

50 51
	UriSuffixBuffer suffix_buffer;
	const char *const suffix = uri_get_suffix(is.GetURI(), suffix_buffer);
52
	const char *mime = is.GetMimeType();
53 54 55 56

	if (suffix == nullptr && mime == nullptr)
		return false;

57 58 59 60
	std::string mime_base;
	if (mime != nullptr)
		mime = (mime_base = GetMimeTypeBase(mime)).c_str();

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

			return CheckDecoderPlugin(plugin, suffix, mime) &&
69
				plugin.ScanStream(is, handler);
70 71 72 73
		});
}

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

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

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

88
	FullTagHandler h(builder, audio_format);
89 90

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

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

	return true;
}

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

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