InputStream.cxx 3.4 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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
 */

Max Kellermann's avatar
Max Kellermann committed
20
#include "config.h"
21
#include "InputStream.hxx"
22
#include "Handler.hxx"
23
#include "tag/Tag.hxx"
24
#include "util/StringCompare.hxx"
25

26 27
#include <stdexcept>

28
#include <assert.h>
29

30
InputStream::~InputStream() noexcept
31 32 33
{
}

34 35
void
InputStream::Check()
36 37 38
{
}

39
void
40
InputStream::Update() noexcept
41 42 43
{
}

44
void
45
InputStream::SetReady() noexcept
46 47 48 49 50
{
	assert(!ready);

	ready = true;

51
	InvokeOnReady();
52 53
}

54 55 56 57 58 59 60
/**
 * 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
61
ExpensiveSeeking(const char *uri) noexcept
62
{
63
	return StringStartsWith(uri, "http://") ||
64 65
		StringStartsWith(uri, "tidal://") ||
		StringStartsWith(uri, "qobuz://") ||
66
		StringStartsWith(uri, "https://");
67 68
}

69
bool
70
InputStream::CheapSeeking() const noexcept
71
{
72
	return IsSeekable() && !ExpensiveSeeking(uri.c_str());
73 74
}

75 76
void
InputStream::Seek(gcc_unused offset_type new_offset)
Avuton Olrich's avatar
Avuton Olrich committed
77
{
78
	throw std::runtime_error("Seeking is not implemented");
79 80
}

81 82
void
InputStream::LockSeek(offset_type _offset)
83
{
84
	const std::lock_guard<Mutex> protect(mutex);
85
	Seek(_offset);
86 87
}

88 89
void
InputStream::LockSkip(offset_type _offset)
90
{
91
	const std::lock_guard<Mutex> protect(mutex);
92
	Skip(_offset);
93 94
}

95
std::unique_ptr<Tag>
96
InputStream::ReadTag()
97
{
98
	return nullptr;
99 100
}

101
std::unique_ptr<Tag>
102
InputStream::LockReadTag()
103
{
104
	const std::lock_guard<Mutex> protect(mutex);
105
	return ReadTag();
106 107 108
}

bool
109
InputStream::IsAvailable() noexcept
110
{
111
	return true;
112 113
}

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

123
	const std::lock_guard<Mutex> protect(mutex);
124
	return Read(ptr, _size);
125 126
}

127 128
void
InputStream::ReadFull(void *_ptr, size_t _size)
129 130 131 132 133
{
	uint8_t *ptr = (uint8_t *)_ptr;

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

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

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

152
	const std::lock_guard<Mutex> protect(mutex);
153
	ReadFull(ptr, _size);
154 155
}

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

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

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