2014-05-02 22:31:02 +02:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2014-05-02 22:31:02 +02:00
|
|
|
* 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_ASYNC_INPUT_STREAM_HXX
|
|
|
|
#define MPD_ASYNC_INPUT_STREAM_HXX
|
|
|
|
|
|
|
|
#include "InputStream.hxx"
|
2017-09-21 22:40:25 +02:00
|
|
|
#include "event/DeferEvent.hxx"
|
2016-06-17 18:06:02 +02:00
|
|
|
#include "util/HugeAllocator.hxx"
|
2014-05-02 22:31:02 +02:00
|
|
|
#include "util/CircularBuffer.hxx"
|
|
|
|
|
2016-09-09 15:41:09 +02:00
|
|
|
#include <exception>
|
|
|
|
|
2014-05-02 22:31:02 +02:00
|
|
|
/**
|
|
|
|
* Helper class for moving asynchronous (non-blocking) InputStream
|
|
|
|
* implementations to the I/O thread. Data is being read into a ring
|
|
|
|
* buffer, and that buffer is then consumed by another thread using
|
|
|
|
* the regular #InputStream API.
|
|
|
|
*/
|
2016-06-17 18:30:45 +02:00
|
|
|
class AsyncInputStream : public InputStream {
|
2014-05-02 22:31:02 +02:00
|
|
|
enum class SeekState : uint8_t {
|
|
|
|
NONE, SCHEDULED, PENDING
|
|
|
|
};
|
|
|
|
|
2017-09-21 22:40:25 +02:00
|
|
|
DeferEvent deferred_resume;
|
|
|
|
DeferEvent deferred_seek;
|
2016-06-17 18:30:45 +02:00
|
|
|
|
2017-09-21 21:50:45 +02:00
|
|
|
HugeArray<uint8_t> allocation;
|
2016-06-17 18:06:02 +02:00
|
|
|
|
2014-05-02 22:31:02 +02:00
|
|
|
CircularBuffer<uint8_t> buffer;
|
|
|
|
const size_t resume_at;
|
|
|
|
|
2017-09-19 19:54:07 +02:00
|
|
|
bool open = true;
|
2014-05-02 22:31:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the connection currently paused? That happens when the
|
|
|
|
* buffer was getting too large. It will be unpaused when the
|
|
|
|
* buffer is below the threshold again.
|
|
|
|
*/
|
2017-09-19 19:54:07 +02:00
|
|
|
bool paused = false;
|
2014-05-02 22:31:02 +02:00
|
|
|
|
2017-09-19 19:54:07 +02:00
|
|
|
SeekState seek_state = SeekState::NONE;
|
2014-05-02 22:31:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The #Tag object ready to be requested via
|
|
|
|
* InputStream::ReadTag().
|
|
|
|
*/
|
2017-09-19 19:54:07 +02:00
|
|
|
Tag *tag = nullptr;
|
2014-05-02 22:31:02 +02:00
|
|
|
|
|
|
|
offset_type seek_offset;
|
|
|
|
|
|
|
|
protected:
|
2016-09-09 15:41:09 +02:00
|
|
|
std::exception_ptr postponed_exception;
|
|
|
|
|
2014-05-02 22:31:02 +02:00
|
|
|
public:
|
2017-01-25 23:12:29 +01:00
|
|
|
AsyncInputStream(EventLoop &event_loop, const char *_url,
|
2014-05-02 22:31:02 +02:00
|
|
|
Mutex &_mutex, Cond &_cond,
|
2016-06-17 18:06:02 +02:00
|
|
|
size_t _buffer_size,
|
2014-05-02 22:31:02 +02:00
|
|
|
size_t _resume_at);
|
|
|
|
|
|
|
|
virtual ~AsyncInputStream();
|
|
|
|
|
2017-01-25 23:15:52 +01:00
|
|
|
EventLoop &GetEventLoop() {
|
|
|
|
return deferred_resume.GetEventLoop();
|
|
|
|
}
|
|
|
|
|
2014-05-02 22:31:02 +02:00
|
|
|
/* virtual methods from InputStream */
|
2016-09-09 18:47:42 +02:00
|
|
|
void Check() final;
|
2017-05-08 14:44:49 +02:00
|
|
|
bool IsEOF() noexcept final;
|
2016-09-09 18:47:42 +02:00
|
|
|
void Seek(offset_type new_offset) final;
|
2014-05-02 22:31:02 +02:00
|
|
|
Tag *ReadTag() final;
|
2017-05-08 14:44:49 +02:00
|
|
|
bool IsAvailable() noexcept final;
|
2016-09-09 18:47:42 +02:00
|
|
|
size_t Read(void *ptr, size_t read_size) final;
|
2014-05-02 22:31:02 +02:00
|
|
|
|
|
|
|
protected:
|
2014-07-10 10:33:43 +02:00
|
|
|
/**
|
|
|
|
* Pass an tag from the I/O thread to the client thread.
|
|
|
|
*/
|
2017-06-03 21:33:44 +02:00
|
|
|
void SetTag(Tag *_tag) noexcept;
|
2014-05-02 22:31:02 +02:00
|
|
|
|
2017-06-03 21:33:44 +02:00
|
|
|
void ClearTag() noexcept {
|
2014-11-02 13:00:25 +01:00
|
|
|
SetTag(nullptr);
|
|
|
|
}
|
|
|
|
|
2017-06-03 21:33:44 +02:00
|
|
|
void Pause() noexcept;
|
2014-05-02 22:31:02 +02:00
|
|
|
|
2017-06-03 21:33:44 +02:00
|
|
|
bool IsPaused() const noexcept {
|
2014-09-02 20:02:56 +02:00
|
|
|
return paused;
|
|
|
|
}
|
|
|
|
|
2014-07-10 10:33:43 +02:00
|
|
|
/**
|
|
|
|
* Declare that the underlying stream was closed. We will
|
|
|
|
* continue feeding Read() calls from the buffer until it runs
|
|
|
|
* empty.
|
|
|
|
*/
|
2017-06-03 21:33:44 +02:00
|
|
|
void SetClosed() noexcept {
|
2014-05-02 22:31:02 +02:00
|
|
|
open = false;
|
|
|
|
}
|
|
|
|
|
2017-06-03 21:33:44 +02:00
|
|
|
bool IsBufferEmpty() const noexcept {
|
2017-11-10 19:24:33 +01:00
|
|
|
return buffer.empty();
|
2014-05-02 22:31:02 +02:00
|
|
|
}
|
|
|
|
|
2017-06-03 21:33:44 +02:00
|
|
|
bool IsBufferFull() const noexcept {
|
2014-06-17 02:32:10 +02:00
|
|
|
return buffer.IsFull();
|
|
|
|
}
|
|
|
|
|
2014-07-10 10:33:43 +02:00
|
|
|
/**
|
|
|
|
* Determine how many bytes can be added to the buffer.
|
|
|
|
*/
|
2014-05-02 22:31:02 +02:00
|
|
|
gcc_pure
|
2017-06-03 21:33:44 +02:00
|
|
|
size_t GetBufferSpace() const noexcept {
|
2014-05-02 22:31:02 +02:00
|
|
|
return buffer.GetSpace();
|
|
|
|
}
|
|
|
|
|
2017-06-03 21:33:44 +02:00
|
|
|
CircularBuffer<uint8_t>::Range PrepareWriteBuffer() noexcept {
|
2016-06-17 17:34:47 +02:00
|
|
|
return buffer.Write();
|
|
|
|
}
|
|
|
|
|
2017-06-03 21:33:44 +02:00
|
|
|
void CommitWriteBuffer(size_t nbytes) noexcept;
|
2016-06-17 17:34:47 +02:00
|
|
|
|
2014-07-10 10:33:43 +02:00
|
|
|
/**
|
|
|
|
* Append data to the buffer. The size must fit into the
|
|
|
|
* buffer; see GetBufferSpace().
|
|
|
|
*/
|
2017-06-03 21:33:44 +02:00
|
|
|
void AppendToBuffer(const void *data, size_t append_size) noexcept;
|
2014-05-02 22:31:02 +02:00
|
|
|
|
2014-07-10 10:33:43 +02:00
|
|
|
/**
|
|
|
|
* Implement code here that will resume the stream after it
|
|
|
|
* has been paused due to full input buffer.
|
|
|
|
*/
|
2014-05-02 22:31:02 +02:00
|
|
|
virtual void DoResume() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The actual Seek() implementation. This virtual method will
|
|
|
|
* be called from within the I/O thread. When the operation
|
|
|
|
* is finished, call SeekDone() to notify the caller.
|
|
|
|
*/
|
|
|
|
virtual void DoSeek(offset_type new_offset) = 0;
|
|
|
|
|
2017-06-03 21:33:44 +02:00
|
|
|
bool IsSeekPending() const noexcept {
|
2014-05-02 22:31:02 +02:00
|
|
|
return seek_state == SeekState::PENDING;
|
|
|
|
}
|
|
|
|
|
2014-07-10 10:33:43 +02:00
|
|
|
/**
|
|
|
|
* Call this after seeking has finished. It will notify the
|
|
|
|
* client thread.
|
|
|
|
*/
|
2017-06-03 21:33:44 +02:00
|
|
|
void SeekDone() noexcept;
|
2014-05-02 22:31:02 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
void Resume();
|
|
|
|
|
2017-09-21 22:40:25 +02:00
|
|
|
/* for DeferEvent */
|
2017-06-03 21:33:44 +02:00
|
|
|
void DeferredResume() noexcept;
|
|
|
|
void DeferredSeek() noexcept;
|
2014-05-02 22:31:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|