InputStream.hxx 9.43 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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.
 */

#ifndef MPD_INPUT_STREAM_HXX
#define MPD_INPUT_STREAM_HXX

23
#include "Offset.hxx"
24
#include "Ptr.hxx"
25
#include "thread/Mutex.hxx"
26
#include "util/Compiler.h"
27

28
#include <cassert>
29
#include <memory>
30
#include <string>
31

32
struct Tag;
33
class InputStreamHandler;
34

35 36
class InputStream {
public:
37
	typedef ::offset_type offset_type;
38

39
private:
40
	/**
41
	 * The absolute URI which was used to open this stream.
42
	 */
43
	const std::string uri;
44

45
public:
46 47 48 49 50 51 52 53
	/**
	 * A mutex that protects the mutable attributes of this object
	 * and its implementation.  It must be locked before calling
	 * any of the public methods.
	 *
	 * This object is allocated by the client, and the client is
	 * responsible for freeing it.
	 */
54
	Mutex &mutex;
55

56
private:
57
	/**
58 59
	 * An (optional) object which gets receives events from this
	 * #InputStream.
60 61 62 63
	 *
	 * This object is allocated by the client, and the client is
	 * responsible for freeing it.
	 */
64
	InputStreamHandler *handler = nullptr;
65

66
protected:
67 68 69 70
	/**
	 * indicates whether the stream is ready for reading and
	 * whether the other attributes in this struct are valid
	 */
71
	bool ready = false;
72 73 74 75

	/**
	 * if true, then the stream is fully seekable
	 */
76
	bool seekable = false;
77

78
	static constexpr offset_type UNKNOWN_SIZE = ~offset_type(0);
79

80
	/**
81
	 * the size of the resource, or #UNKNOWN_SIZE if unknown
82
	 */
83
	offset_type size = UNKNOWN_SIZE;
84 85 86 87

	/**
	 * the current offset within the stream
	 */
88
	offset_type offset = 0;
89

90
private:
91
	/**
92
	 * the MIME content type of the resource, or empty if unknown.
93
	 */
94
	std::string mime;
95

96
public:
97
	InputStream(const char *_uri, Mutex &_mutex) noexcept
98
		:uri(_uri),
99
		 mutex(_mutex) {
100
		assert(_uri != nullptr);
101
	}
102

103 104 105 106 107
	/**
	 * Close the input stream and free resources.
	 *
	 * The caller must not lock the mutex.
	 */
108
	virtual ~InputStream() noexcept;
109

110 111 112 113
	/**
	 * Opens a new input stream.  You may not access it until the "ready"
	 * flag is set.
	 *
114 115
	 * Throws std::runtime_error on error.
	 *
116 117 118
	 * @param mutex a mutex that is used to protect this object; must be
	 * locked before calling any of the public methods
	 * @param cond a cond that gets signalled when the state of
119
	 * this object changes; may be nullptr if the caller doesn't want to get
120
	 * notifications
121
	 * @return an #InputStream object on success
122
	 */
123 124
	gcc_nonnull(1)
	static InputStreamPtr Open(const char *uri, Mutex &mutex);
125

126 127 128 129
	/**
	 * Just like Open(), but waits for the stream to become ready.
	 * It is a wrapper for Open(), WaitReady() and Check().
	 */
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	gcc_nonnull(1)
	static InputStreamPtr OpenReady(const char *uri, Mutex &mutex);

	/**
	 * Install a new handler.
	 *
	 * The caller must lock the mutex.
	 */
	void SetHandler(InputStreamHandler *new_handler) noexcept {
		handler = new_handler;
	}

	/**
	 * Install a new handler and return the old one.
	 *
	 * The caller must lock the mutex.
	 */
	InputStreamHandler *ExchangeHandler(InputStreamHandler *new_handler) noexcept {
		return std::exchange(handler, new_handler);
	}
150

151 152 153 154 155
	/**
	 * The absolute URI which was used to open this stream.
	 *
	 * No lock necessary for this method.
	 */
156
	const char *GetURI() const noexcept {
157 158 159
		return uri.c_str();
	}

160 161
	/**
	 * Check for errors that may have occurred in the I/O thread.
162
	 * Throws std::runtime_error on error.
163
	 */
164
	virtual void Check();
165 166 167 168 169

	/**
	 * Update the public attributes.  Call before accessing attributes
	 * such as "ready" or "offset".
	 */
170
	virtual void Update() noexcept;
171

172
	void SetReady() noexcept;
173

174
	/**
175 176
	 * Return whether the stream is ready for reading and whether
	 * the other attributes in this struct are valid.
177 178 179
	 *
	 * The caller must lock the mutex.
	 */
180 181 182 183 184
	bool IsReady() const {
		return ready;
	}

	gcc_pure
185
	bool HasMimeType() const noexcept {
186 187 188 189 190
		assert(ready);

		return !mime.empty();
	}

191
	gcc_pure
192
	const char *GetMimeType() const noexcept {
193 194 195 196 197
		assert(ready);

		return mime.empty() ? nullptr : mime.c_str();
	}

198
	void ClearMimeType() noexcept {
199 200 201
		mime.clear();
	}

202
	gcc_nonnull_all
203
	void SetMimeType(const char *_mime) noexcept {
204 205 206 207 208
		assert(!ready);

		mime = _mime;
	}

209
	void SetMimeType(std::string &&_mime) noexcept {
210 211 212 213 214
		assert(!ready);

		mime = std::move(_mime);
	}

215
	gcc_pure
216
	bool KnownSize() const noexcept {
217 218
		assert(ready);

219
		return size != UNKNOWN_SIZE;
220 221
	}

222
	gcc_pure
223
	offset_type GetSize() const noexcept {
224
		assert(ready);
225
		assert(KnownSize());
226 227 228 229

		return size;
	}

230
	void AddOffset(offset_type delta) noexcept {
231 232 233 234 235
		assert(ready);

		offset += delta;
	}

236
	gcc_pure
237
	offset_type GetOffset() const noexcept {
238 239 240 241 242
		assert(ready);

		return offset;
	}

243
	gcc_pure
244
	offset_type GetRest() const noexcept {
245
		assert(ready);
246
		assert(KnownSize());
247 248 249 250

		return size - offset;
	}

251
	gcc_pure
252
	bool IsSeekable() const noexcept {
253 254 255 256 257 258 259 260 261
		assert(ready);

		return seekable;
	}

	/**
	 * Determines whether seeking is cheap.  This is true for local files.
	 */
	gcc_pure
262
	bool CheapSeeking() const noexcept;
263 264 265 266 267 268 269

	/**
	 * Seeks to the specified position in the stream.  This will most
	 * likely fail if the "seekable" flag is false.
	 *
	 * The caller must lock the mutex.
	 *
270 271
	 * Throws std::runtime_error on error.
	 *
272 273
	 * @param lock the locked mutex; may be used to wait on
	 * condition variables
274 275
	 * @param offset the relative offset
	 */
276
	virtual void Seek(std::unique_lock<Mutex> &lock, offset_type offset);
277 278 279 280 281

	/**
	 * Wrapper for Seek() which locks and unlocks the mutex; the
	 * caller must not be holding it already.
	 */
282
	void LockSeek(offset_type offset);
283

284 285
	/**
	 * Rewind to the beginning of the stream.  This is a wrapper
286
	 * for Seek(0, error).
287
	 */
288
	void Rewind(std::unique_lock<Mutex> &lock) {
289 290
		if (offset > 0)
			Seek(lock, 0);
291 292
	}

293
	void LockRewind() {
294 295
		std::unique_lock<Mutex> lock(mutex);
		Rewind(lock);
296
	}
297

298 299 300
	/**
	 * Skip input bytes.
	 */
301 302 303
	void Skip(std::unique_lock<Mutex> &lock,
		  offset_type _offset) {
		Seek(lock, GetOffset() + _offset);
304 305
	}

306
	void LockSkip(offset_type _offset);
307

308 309 310 311 312 313
	/**
	 * Returns true if the stream has reached end-of-file.
	 *
	 * The caller must lock the mutex.
	 */
	gcc_pure
314
	virtual bool IsEOF() const noexcept = 0;
315 316 317 318 319 320

	/**
	 * Wrapper for IsEOF() which locks and unlocks the mutex; the
	 * caller must not be holding it already.
	 */
	gcc_pure
321
	bool LockIsEOF() const noexcept;
322 323 324 325 326 327

	/**
	 * Reads the tag from the stream.
	 *
	 * The caller must lock the mutex.
	 *
328 329
	 * @return a tag object or nullptr if the tag has not changed
	 * since the last call
330
	 */
331
	virtual std::unique_ptr<Tag> ReadTag() noexcept;
332 333 334 335 336

	/**
	 * Wrapper for ReadTag() which locks and unlocks the mutex;
	 * the caller must not be holding it already.
	 */
337
	std::unique_ptr<Tag> LockReadTag() noexcept;
338 339 340 341 342 343 344 345 346

	/**
	 * Returns true if the next read operation will not block: either data
	 * is available, or end-of-stream has been reached, or an error has
	 * occurred.
	 *
	 * The caller must lock the mutex.
	 */
	gcc_pure
347
	virtual bool IsAvailable() const noexcept;
348 349 350 351 352 353 354

	/**
	 * Reads data from the stream into the caller-supplied buffer.
	 * Returns 0 on error or eof (check with IsEOF()).
	 *
	 * The caller must lock the mutex.
	 *
355 356
	 * Throws std::runtime_error on error.
	 *
357 358
	 * @param lock the locked mutex; may be used to wait on
	 * condition variables
359 360 361 362 363
	 * @param ptr the buffer to read into
	 * @param size the maximum number of bytes to read
	 * @return the number of bytes read
	 */
	gcc_nonnull_all
364 365
	virtual size_t Read(std::unique_lock<Mutex> &lock,
			    void *ptr, size_t size) = 0;
366 367 368 369

	/**
	 * Wrapper for Read() which locks and unlocks the mutex;
	 * the caller must not be holding it already.
370 371
	 *
	 * Throws std::runtime_error on error.
372 373
	 */
	gcc_nonnull_all
374
	size_t LockRead(void *ptr, size_t size);
375 376 377 378 379 380

	/**
	 * Reads the whole data from the stream into the caller-supplied buffer.
	 *
	 * The caller must lock the mutex.
	 *
381 382
	 * Throws std::runtime_error on error.
	 *
383 384 385 386 387
	 * @param ptr the buffer to read into
	 * @param size the number of bytes to read
	 * @return true if the whole data was read, false otherwise.
	 */
	gcc_nonnull_all
388
	void ReadFull(std::unique_lock<Mutex> &lock, void *ptr, size_t size);
389 390 391 392

	/**
	 * Wrapper for ReadFull() which locks and unlocks the mutex;
	 * the caller must not be holding it already.
393 394
	 *
	 * Throws std::runtime_error on error.
395 396
	 */
	gcc_nonnull_all
397
	void LockReadFull(void *ptr, size_t size);
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421

protected:
	void InvokeOnReady() noexcept;
	void InvokeOnAvailable() noexcept;
};

/**
 * Install an #InputStreamHandler during the scope in which this
 * variable lives, and restore the old handler afterwards.
 */
class ScopeExchangeInputStreamHandler {
	InputStream &is;
	InputStreamHandler *const old_handler;

public:
	ScopeExchangeInputStreamHandler(InputStream &_is,
					InputStreamHandler *new_handler) noexcept
		:is(_is), old_handler(is.ExchangeHandler(new_handler)) {}

	ScopeExchangeInputStreamHandler(const ScopeExchangeInputStreamHandler &) = delete;

	~ScopeExchangeInputStreamHandler() noexcept {
		is.SetHandler(old_handler);
	}
422
};
423 424

#endif