InputStream.cxx 3.5 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
void
Rosen Penev's avatar
Rosen Penev committed
72
InputStream::Seek(std::unique_lock<Mutex> &, [[maybe_unused]] offset_type new_offset)
Avuton Olrich's avatar
Avuton Olrich committed
73
{
74
	throw std::runtime_error("Seeking is not implemented");
75 76
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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