TagStream.cxx 2.83 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
 * 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
#include "util/UriUtil.hxx"
26 27
#include "decoder/DecoderList.hxx"
#include "decoder/DecoderPlugin.hxx"
Max Kellermann's avatar
Max Kellermann committed
28
#include "input/InputStream.hxx"
29 30
#include "thread/Mutex.hxx"

31
#include <exception>
32

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

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

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

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

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

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

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

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

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

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

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

92
	FullTagHandler h(builder, audio_format);
93 94

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

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

	return true;
}

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

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