InputStream.cxx 3.11 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 "thread/Cond.hxx"
23
#include "util/StringCompare.hxx"
24

25 26
#include <stdexcept>

27
#include <assert.h>
28

29 30 31 32
InputStream::~InputStream()
{
}

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

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

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

	ready = true;
	cond.broadcast();
}

52
void
53
InputStream::WaitReady()
54 55
{
	while (true) {
56 57
		Update();
		if (ready)
58 59
			break;

60
		cond.wait(mutex);
61 62 63 64
	}
}

void
65
InputStream::LockWaitReady()
66
{
67 68
	const ScopeLock protect(mutex);
	WaitReady();
69 70
}

71 72 73 74 75 76 77 78 79
/**
 * 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
ExpensiveSeeking(const char *uri)
{
80 81
	return StringStartsWith(uri, "http://") ||
		StringStartsWith(uri, "https://");
82 83
}

84
bool
85
InputStream::CheapSeeking() const
86
{
87
	return IsSeekable() && !ExpensiveSeeking(uri.c_str());
88 89
}

90 91
void
InputStream::Seek(gcc_unused offset_type new_offset)
Avuton Olrich's avatar
Avuton Olrich committed
92
{
93
	throw std::runtime_error("Seeking is not implemented");
94 95
}

96 97
void
InputStream::LockSeek(offset_type _offset)
98
{
99
	const ScopeLock protect(mutex);
100
	Seek(_offset);
101 102
}

103 104
void
InputStream::LockSkip(offset_type _offset)
105 106
{
	const ScopeLock protect(mutex);
107
	Skip(_offset);
108 109
}

Max Kellermann's avatar
Max Kellermann committed
110
Tag *
111
InputStream::ReadTag()
112
{
113
	return nullptr;
114 115
}

Max Kellermann's avatar
Max Kellermann committed
116
Tag *
117
InputStream::LockReadTag()
118
{
119 120
	const ScopeLock protect(mutex);
	return ReadTag();
121 122 123
}

bool
124
InputStream::IsAvailable()
125
{
126
	return true;
127 128
}

129
size_t
130
InputStream::LockRead(void *ptr, size_t _size)
131
{
132 133
#if !CLANG_CHECK_VERSION(3,6)
	/* disabled on clang due to -Wtautological-pointer-compare */
134
	assert(ptr != nullptr);
135
#endif
136
	assert(_size > 0);
137

138
	const ScopeLock protect(mutex);
139
	return Read(ptr, _size);
140 141
}

142 143
void
InputStream::ReadFull(void *_ptr, size_t _size)
144 145 146 147 148
{
	uint8_t *ptr = (uint8_t *)_ptr;

	size_t nbytes_total = 0;
	while (_size > 0) {
149
		size_t nbytes = Read(ptr + nbytes_total, _size);
150
		if (nbytes == 0)
151
			throw std::runtime_error("Unexpected end of file");
152 153 154 155 156 157

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

158 159
void
InputStream::LockReadFull(void *ptr, size_t _size)
160 161 162 163 164 165 166 167
{
#if !CLANG_CHECK_VERSION(3,6)
	/* disabled on clang due to -Wtautological-pointer-compare */
	assert(ptr != nullptr);
#endif
	assert(_size > 0);

	const ScopeLock protect(mutex);
168
	ReadFull(ptr, _size);
169 170
}

171
bool
172
InputStream::LockIsEOF()
Avuton Olrich's avatar
Avuton Olrich committed
173
{
174 175
	const ScopeLock protect(mutex);
	return IsEOF();
176
}