IcyInputStream.cxx 2.49 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "IcyInputStream.hxx"
22
#include "IcyMetaDataParser.hxx"
23 24
#include "tag/Tag.hxx"

25
IcyInputStream::IcyInputStream(InputStreamPtr _input,
26
			       std::shared_ptr<IcyMetaDataParser> _parser) noexcept
27
	:ProxyInputStream(std::move(_input)), parser(std::move(_parser))
28 29 30
{
}

31
IcyInputStream::~IcyInputStream() noexcept = default;
32

33 34 35 36 37 38 39 40
inline bool
IcyInputStream::IsEnabled() const noexcept
{
	assert(parser);

	return parser->IsDefined();
}

41
void
42
IcyInputStream::Update() noexcept
43 44 45 46 47 48 49
{
	ProxyInputStream::Update();

	if (IsEnabled())
		offset = override_offset;
}

50
std::unique_ptr<Tag>
51 52
IcyInputStream::ReadTag()
{
53
	auto new_input_tag = ProxyInputStream::ReadTag();
54 55 56
	if (!IsEnabled())
		return new_input_tag;

57
	const bool had_new_input_tag = !!new_input_tag;
58
	if (new_input_tag != nullptr)
59
		input_tag = std::move(new_input_tag);
60

61
	auto new_icy_tag = parser->ReadTag();
62 63 64
	const bool had_new_icy_tag = !!new_icy_tag;
	if (new_icy_tag != nullptr)
		icy_tag = std::move(new_icy_tag);
65

66
	if (!had_new_input_tag && !had_new_icy_tag)
67 68 69 70 71 72 73 74
		/* no change */
		return nullptr;

	if (input_tag == nullptr && icy_tag == nullptr)
		/* no tag */
		return nullptr;

	if (input_tag == nullptr)
75
		return std::make_unique<Tag>(*icy_tag);
76 77

	if (icy_tag == nullptr)
78
		return std::make_unique<Tag>(*input_tag);
79

80
	return Tag::Merge(*input_tag, *icy_tag);
81 82 83
}

size_t
84
IcyInputStream::Read(void *ptr, size_t read_size)
85 86
{
	if (!IsEnabled())
87
		return ProxyInputStream::Read(ptr, read_size);
88 89

	while (true) {
90
		size_t nbytes = ProxyInputStream::Read(ptr, read_size);
91 92 93
		if (nbytes == 0)
			return 0;

94
		size_t result = parser->ParseInPlace(ptr, nbytes);
95 96 97 98 99 100 101
		if (result > 0) {
			override_offset += result;
			offset = override_offset;
			return result;
		}
	}
}