TagStream.cxx 2.85 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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 "config.h"
#include "TagStream.hxx"
22
#include "tag/Generic.hxx"
23 24
#include "tag/Handler.hxx"
#include "tag/Builder.hxx"
25
#include "util/MimeType.hxx"
26
#include "util/UriUtil.hxx"
27 28
#include "decoder/DecoderList.hxx"
#include "decoder/DecoderPlugin.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "input/InputStream.hxx"
30 31
#include "thread/Mutex.hxx"

32
#include <exception>
33

34 35 36 37 38 39 40 41
#include <assert.h>

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

bool
49
tag_stream_scan(InputStream &is, TagHandler &handler) noexcept
50
{
51
	assert(is.IsReady());
52

53 54
	UriSuffixBuffer suffix_buffer;
	const char *const suffix = uri_get_suffix(is.GetURI(), suffix_buffer);
55
	const char *mime = is.GetMimeType();
56 57 58 59

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

60 61 62 63
	std::string mime_base;
	if (mime != nullptr)
		mime = (mime_base = GetMimeTypeBase(mime)).c_str();

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

			return CheckDecoderPlugin(plugin, suffix, mime) &&
72
				plugin.ScanStream(is, handler);
73 74 75 76
		});
}

bool
77
tag_stream_scan(const char *uri, TagHandler &handler) noexcept
78
try {
79 80
	Mutex mutex;

81
	auto is = InputStream::OpenReady(uri, mutex);
82
	return tag_stream_scan(*is, handler);
83 84
} catch (const std::exception &e) {
	return false;
85
}
86 87

bool
88 89
tag_stream_scan(InputStream &is, TagBuilder &builder,
		AudioFormat *audio_format) noexcept
90 91 92
{
	assert(is.IsReady());

93
	FullTagHandler h(builder, audio_format);
94 95

	if (!tag_stream_scan(is, h))
96 97
		return false;

98
	if (builder.empty())
99
		ScanGenericTags(is, h);
100 101 102 103 104

	return true;
}

bool
105 106
tag_stream_scan(const char *uri, TagBuilder &builder,
		AudioFormat *audio_format) noexcept
107
try {
108 109
	Mutex mutex;

110
	auto is = InputStream::OpenReady(uri, mutex);
111
	return tag_stream_scan(*is, builder, audio_format);
112 113
} catch (const std::exception &e) {
	return false;
114
}