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

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

31
struct Tag;
32
class InputStreamHandler;
33

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

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

44
public:
45 46 47 48 49 50 51 52
	/**
	 * 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.
	 */
53
	Mutex &mutex;
54

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

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

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

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

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

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

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

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

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

109 110 111
	InputStream(const InputStream &) = delete;
	InputStream &operator=(const InputStream &) = delete;

112 113 114 115
	/**
	 * Opens a new input stream.  You may not access it until the "ready"
	 * flag is set.
	 *
116 117
	 * Throws std::runtime_error on error.
	 *
118 119 120
	 * @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
121
	 * this object changes; may be nullptr if the caller doesn't want to get
122
	 * notifications
123
	 * @return an #InputStream object on success
124
	 */
125
	static InputStreamPtr Open(const char *uri, Mutex &mutex);
126

127 128 129 130
	/**
	 * Just like Open(), but waits for the stream to become ready.
	 * It is a wrapper for Open(), WaitReady() and Check().
	 */
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	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
	bool IsReady() const {
		return ready;
	}

184
	[[gnu::pure]]
185
	bool HasMimeType() const noexcept {
186 187 188 189 190
		assert(ready);

		return !mime.empty();
	}

191
	[[gnu::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
	[[gnu::nonnull]]
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
	[[gnu::pure]]
216
	bool KnownSize() const noexcept {
217 218
		assert(ready);

219
		return size != UNKNOWN_SIZE;
220 221
	}

222
	[[gnu::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
	[[gnu::pure]]
237
	offset_type GetOffset() const noexcept {
238 239 240 241 242
		assert(ready);

		return offset;
	}

243
	[[gnu::pure]]
244
	offset_type GetRest() const noexcept {
245
		assert(ready);
246
		assert(KnownSize());
247 248 249 250

		return size - offset;
	}

251
	[[gnu::pure]]
252
	bool IsSeekable() const noexcept {
253 254 255 256 257 258 259 260
		assert(ready);

		return seekable;
	}

	/**
	 * Determines whether seeking is cheap.  This is true for local files.
	 */
261
	[[gnu::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
	/**
	 * Returns true if the stream has reached end-of-file.
	 *
	 * The caller must lock the mutex.
	 */
313
	[[gnu::pure]]
314
	virtual bool IsEOF() const noexcept = 0;
315 316 317 318 319

	/**
	 * Wrapper for IsEOF() which locks and unlocks the mutex; the
	 * caller must not be holding it already.
	 */
320
	[[gnu::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

	/**
	 * 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.
	 */
346
	[[gnu::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
	 * @param ptr the buffer to read into
	 * @param size the maximum number of bytes to read
	 * @return the number of bytes read
	 */
363
	[[gnu::nonnull]]
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
	[[gnu::nonnull]]
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
	 * @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.
	 */
387
	[[gnu::nonnull]]
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
	[[gnu::nonnull]]
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