InputStream.cxx 3.51 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "InputStream.hxx"
21
#include "Handler.hxx"
22
#include "tag/Tag.hxx"
23
#include "util/ASCII.hxx"
24

25
#include <cassert>
26 27
#include <stdexcept>

28
InputStream::~InputStream() noexcept = default;
29

30 31
void
InputStream::Check()
32 33 34
{
}

35
void
36
InputStream::Update() noexcept
37 38 39
{
}

40
void
41
InputStream::SetReady() noexcept
42 43 44 45 46
{
	assert(!ready);

	ready = true;

47
	InvokeOnReady();
48 49
}

50 51 52 53 54 55 56
/**
 * Is seeking on resources behind this URI "expensive"?  For example,
 * seeking in a HTTP file requires opening a new connection with a new
 * HTTP request.
 */
gcc_pure
static bool
57
ExpensiveSeeking(const char *uri) noexcept
58
{
59
	return StringStartsWithCaseASCII(uri, "http://") ||
60 61
		StringStartsWithCaseASCII(uri, "tidal://") ||
		StringStartsWithCaseASCII(uri, "qobuz://") ||
62
		StringStartsWithCaseASCII(uri, "https://");
63 64
}

65
bool
66
InputStream::CheapSeeking() const noexcept
67
{
68
	return IsSeekable() && !ExpensiveSeeking(uri.c_str());
69 70
}

71
//[[noreturn]]
72
void
Rosen Penev's avatar
Rosen Penev committed
73
InputStream::Seek(std::unique_lock<Mutex> &, [[maybe_unused]] offset_type new_offset)
Avuton Olrich's avatar
Avuton Olrich committed
74
{
75
	throw std::runtime_error("Seeking is not implemented");
76 77
}

78 79
void
InputStream::LockSeek(offset_type _offset)
80
{
81 82
	std::unique_lock<Mutex> lock(mutex);
	Seek(lock, _offset);
83 84
}

85 86
void
InputStream::LockSkip(offset_type _offset)
87
{
88 89
	std::unique_lock<Mutex> lock(mutex);
	Skip(lock, _offset);
90 91
}

92
std::unique_ptr<Tag>
93
InputStream::ReadTag() noexcept
94
{
95
	return nullptr;
96 97
}

98
std::unique_ptr<Tag>
99
InputStream::LockReadTag() noexcept
100
{
101
	const std::lock_guard<Mutex> protect(mutex);
102
	return ReadTag();
103 104 105
}

bool
106
InputStream::IsAvailable() const noexcept
107
{
108
	return true;
109 110
}

111
size_t
112
InputStream::LockRead(void *ptr, size_t _size)
113
{
114 115
#if !CLANG_CHECK_VERSION(3,6)
	/* disabled on clang due to -Wtautological-pointer-compare */
116
	assert(ptr != nullptr);
117
#endif
118
	assert(_size > 0);
119

120 121
	std::unique_lock<Mutex> lock(mutex);
	return Read(lock, ptr, _size);
122 123
}

124
void
125
InputStream::ReadFull(std::unique_lock<Mutex> &lock, void *_ptr, size_t _size)
126
{
Max Kellermann's avatar
Max Kellermann committed
127
	auto *ptr = (uint8_t *)_ptr;
128 129 130

	size_t nbytes_total = 0;
	while (_size > 0) {
131
		size_t nbytes = Read(lock, ptr + nbytes_total, _size);
132
		if (nbytes == 0)
133
			throw std::runtime_error("Unexpected end of file");
134 135 136 137 138 139

		nbytes_total += nbytes;
		_size -= nbytes;
	}
}

140 141
void
InputStream::LockReadFull(void *ptr, size_t _size)
142 143 144 145 146 147 148
{
#if !CLANG_CHECK_VERSION(3,6)
	/* disabled on clang due to -Wtautological-pointer-compare */
	assert(ptr != nullptr);
#endif
	assert(_size > 0);

149 150
	std::unique_lock<Mutex> lock(mutex);
	ReadFull(lock, ptr, _size);
151 152
}

153
bool
154
InputStream::LockIsEOF() const noexcept
Avuton Olrich's avatar
Avuton Olrich committed
155
{
156
	const std::lock_guard<Mutex> protect(mutex);
157
	return IsEOF();
158
}
159 160 161 162 163 164 165 166 167 168 169 170 171 172

void
InputStream::InvokeOnReady() noexcept
{
	if (handler != nullptr)
		handler->OnInputStreamReady();
}

void
InputStream::InvokeOnAvailable() noexcept
{
	if (handler != nullptr)
		handler->OnInputStreamAvailable();
}