MPD: input/AsyncInputStream.hxx Source File

MPD 0.20.18

AsyncInputStream.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_ASYNC_INPUT_STREAM_HXX
21 #define MPD_ASYNC_INPUT_STREAM_HXX
22
23 #include "InputStream.hxx"
24 #include "event/DeferredCall.hxx"
25 #include "util/HugeAllocator.hxx"
26 #include "util/CircularBuffer.hxx"
27
28 #include <exception>
29
36 class AsyncInputStream : public InputStream {
37 enum class SeekState : uint8_t {
38 NONE, SCHEDULED, PENDING
39 };
40
41 DeferredCall deferred_resume;
42 DeferredCall deferred_seek;
43
44 HugeAllocation allocation;
45
46 CircularBuffer<uint8_t> buffer;
47 const size_t resume_at;
48
49 bool open;
50
56 bool paused;
57
58 SeekState seek_state;
59
64 Tag *tag;
65
66 offset_type seek_offset;
67
68 protected:
69 std::exception_ptr postponed_exception;
70
71 public:
76 AsyncInputStream(const char *_url,
77 Mutex &_mutex, Cond &_cond,
78 size_t _buffer_size,
79 size_t _resume_at);
80
81 virtual ~AsyncInputStream();
82
83 /* virtual methods from InputStream */
84 void Check() final;
85 bool IsEOF() noexcept final;
86 void Seek(offset_type new_offset) final;
87 Tag *ReadTag() final;
88 bool IsAvailable() noexcept final;
89 size_t Read(void *ptr, size_t read_size) final;
90
91 protected:
95 void SetTag(Tag *_tag) noexcept;
96
97 void ClearTag() noexcept {
98 SetTag(nullptr);
99 }
100
101 void Pause() noexcept;
102
103 bool IsPaused() const noexcept {
104 return paused;
105 }
106
112 void SetClosed() noexcept {
113 open = false;
114 }
115
116 bool IsBufferEmpty() const noexcept {
117 return buffer.IsEmpty();
118 }
119
120 bool IsBufferFull() const noexcept {
121 return buffer.IsFull();
122 }
123
127 gcc_pure
128 size_t GetBufferSpace() const noexcept {
129 return buffer.GetSpace();
130 }
131
132 CircularBuffer<uint8_t>::Range PrepareWriteBuffer() noexcept {
133 return buffer.Write();
134 }
135
136 void CommitWriteBuffer(size_t nbytes) noexcept;
137
142 void AppendToBuffer(const void *data, size_t append_size) noexcept;
143
148 virtual void DoResume() = 0;
149
155 virtual void DoSeek(offset_type new_offset) = 0;
156
157 bool IsSeekPending() const noexcept {
158 return seek_state == SeekState::PENDING;
159 }
160
165 void SeekDone() noexcept;
166
167 private:
168 void Resume();
169
170 /* for DeferredCall */
171 void DeferredResume() noexcept;
172 void DeferredSeek() noexcept;
173 };
174
175 #endif
AsyncInputStream::IsBufferEmpty
bool IsBufferEmpty() const noexcept
Definition: AsyncInputStream.hxx:116
AsyncInputStream::DoResume
virtual void DoResume()=0
Implement code here that will resume the stream after it has been paused due to full input buffer...
AsyncInputStream::postponed_exception
std::exception_ptr postponed_exception
Definition: AsyncInputStream.hxx:69
AsyncInputStream::SetClosed
void SetClosed() noexcept
Declare that the underlying stream was closed.
Definition: AsyncInputStream.hxx:112
Tag
The meta information about a song file.
Definition: Tag.hxx:34
DecoderCommand::NONE
AsyncInputStream::DoSeek
virtual void DoSeek(offset_type new_offset)=0
The actual Seek() implementation.
CircularBuffer::IsFull
constexpr bool IsFull() const
Definition: CircularBuffer.hxx:97
InputStream::offset_type
::offset_type offset_type
Definition: InputStream.hxx:38
HugeAllocation
Automatic huge memory allocation management.
Definition: HugeAllocator.hxx:117
AsyncInputStream
Helper class for moving asynchronous (non-blocking) InputStream implementations to the I/O thread...
Definition: AsyncInputStream.hxx:36
InputStream
Definition: InputStream.hxx:36
AsyncInputStream::IsAvailable
bool IsAvailable() noexcept final
Returns true if the next read operation will not block: either data is available, or end-of-stream ha...
Cond
Definition: Cond.hxx:41
Mutex
Definition: Mutex.hxx:43
HugeAllocator.hxx
AsyncInputStream::~AsyncInputStream
virtual ~AsyncInputStream()
AsyncInputStream::IsSeekPending
bool IsSeekPending() const noexcept
Definition: AsyncInputStream.hxx:157
AsyncInputStream::SeekDone
void SeekDone() noexcept
Call this after seeking has finished.
CircularBuffer::GetSpace
constexpr size_type GetSpace() const
Returns the number of elements that can be added to this buffer.
Definition: CircularBuffer.hxx:114
AsyncInputStream::ReadTag
Tag * ReadTag() final
Reads the tag from the stream.
DeferredCall.hxx
InputStream.hxx
AsyncInputStream::GetBufferSpace
gcc_pure size_t GetBufferSpace() const noexcept
Determine how many bytes can be added to the buffer.
Definition: AsyncInputStream.hxx:128
AsyncInputStream::SetTag
void SetTag(Tag *_tag) noexcept
Pass an tag from the I/O thread to the client thread.
AsyncInputStream::ClearTag
void ClearTag() noexcept
Definition: AsyncInputStream.hxx:97
CircularBuffer::Write
Range Write()
Prepares writing.
Definition: CircularBuffer.hxx:126
CircularBuffer::IsEmpty
constexpr bool IsEmpty() const
Definition: CircularBuffer.hxx:93
CircularBuffer< uint8_t >
AsyncInputStream::IsPaused
bool IsPaused() const noexcept
Definition: AsyncInputStream.hxx:103
AsyncInputStream::IsBufferFull
bool IsBufferFull() const noexcept
Definition: AsyncInputStream.hxx:120
AsyncInputStream::IsEOF
bool IsEOF() noexcept final
Returns true if the stream has reached end-of-file.
AsyncInputStream::Pause
void Pause() noexcept
offset_type
uint64_t offset_type
A type for absolute offsets in a file.
Definition: Offset.hxx:30
AsyncInputStream::Read
size_t Read(void *ptr, size_t read_size) final
Reads data from the stream into the caller-supplied buffer.
AsyncInputStream::Seek
void Seek(offset_type new_offset) final
Seeks to the specified position in the stream.
DeferredCall
Invoke a method call in the EventLoop.
Definition: DeferredCall.hxx:32
AsyncInputStream::AsyncInputStream
AsyncInputStream(const char *_url, Mutex &_mutex, Cond &_cond, size_t _buffer_size, size_t _resume_at)
CircularBuffer.hxx
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
AsyncInputStream::Check
void Check() final
Check for errors that may have occurred in the I/O thread.
AsyncInputStream::CommitWriteBuffer
void CommitWriteBuffer(size_t nbytes) noexcept
AsyncInputStream::AppendToBuffer
void AppendToBuffer(const void *data, size_t append_size) noexcept
Append data to the buffer.
AsyncInputStream::PrepareWriteBuffer
CircularBuffer< uint8_t >::Range PrepareWriteBuffer() noexcept
Definition: AsyncInputStream.hxx:132