MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

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() final;
86 void Seek(offset_type new_offset) final;
87 Tag *ReadTag() final;
88 bool IsAvailable() final;
89 size_t Read(void *ptr, size_t read_size) final;
90
91 protected:
95 void SetTag(Tag *_tag);
96
97 void ClearTag() {
98 SetTag(nullptr);
99 }
100
101 void Pause();
102
103 bool IsPaused() const {
104 return paused;
105 }
106
112 void SetClosed() {
113 open = false;
114 }
115
116 bool IsBufferEmpty() const {
117 return buffer.IsEmpty();
118 }
119
120 bool IsBufferFull() const {
121 return buffer.IsFull();
122 }
123
127 gcc_pure
128 size_t GetBufferSpace() const {
129 return buffer.GetSpace();
130 }
131
132 CircularBuffer<uint8_t>::Range PrepareWriteBuffer() {
133 return buffer.Write();
134 }
135
136 void CommitWriteBuffer(size_t nbytes);
137
142 void AppendToBuffer(const void *data, size_t append_size);
143
148 virtual void DoResume() = 0;
149
155 virtual void DoSeek(offset_type new_offset) = 0;
156
157 bool IsSeekPending() const {
158 return seek_state == SeekState::PENDING;
159 }
160
165 void SeekDone();
166
167 private:
168 void Resume();
169
170 /* for DeferredCall */
171 void DeferredResume();
172 void DeferredSeek();
173 };
174
175 #endif
AsyncInputStream::CommitWriteBuffer
void CommitWriteBuffer(size_t nbytes)
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::IsEOF
bool IsEOF() final
Returns true if the stream has reached end-of-file.
AsyncInputStream::IsAvailable
bool IsAvailable() final
Returns true if the next read operation will not block: either data is available, or end-of-stream ha...
Tag
The meta information about a song file.
Definition: Tag.hxx:34
AsyncInputStream::DoSeek
virtual void DoSeek(offset_type new_offset)=0
The actual Seek() implementation.
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
Cond
Definition: Cond.hxx:41
Mutex
Definition: Mutex.hxx:43
HugeAllocator.hxx
AsyncInputStream::PrepareWriteBuffer
CircularBuffer< uint8_t >::Range PrepareWriteBuffer()
Definition: AsyncInputStream.hxx:132
AsyncInputStream::IsPaused
bool IsPaused() const
Definition: AsyncInputStream.hxx:103
AsyncInputStream::~AsyncInputStream
virtual ~AsyncInputStream()
AsyncInputStream::IsSeekPending
bool IsSeekPending() const
Definition: AsyncInputStream.hxx:157
AsyncInputStream::ReadTag
Tag * ReadTag() final
Reads the tag from the stream.
DeferredCall.hxx
InputStream.hxx
CircularBuffer::Write
Range Write()
Prepares writing.
Definition: CircularBuffer.hxx:126
AsyncInputStream::IsBufferFull
bool IsBufferFull() const
Definition: AsyncInputStream.hxx:120
CircularBuffer::GetSpace
constexpr size_type GetSpace() const
Returns the number of elements that can be added to this buffer.
Definition: CircularBuffer.hxx:114
AsyncInputStream::ClearTag
void ClearTag()
Definition: AsyncInputStream.hxx:97
AsyncInputStream::SeekState::PENDING
AsyncInputStream::SetClosed
void SetClosed()
Declare that the underlying stream was closed.
Definition: AsyncInputStream.hxx:112
CircularBuffer< uint8_t >
AsyncInputStream::SeekState::NONE
CircularBuffer::IsFull
constexpr bool IsFull() const
Definition: CircularBuffer.hxx:97
AsyncInputStream::Pause
void Pause()
CircularBuffer::IsEmpty
constexpr bool IsEmpty() const
Definition: CircularBuffer.hxx:93
AsyncInputStream::SetTag
void SetTag(Tag *_tag)
Pass an tag from the I/O thread to the client thread.
AsyncInputStream::GetBufferSpace
gcc_pure size_t GetBufferSpace() const
Determine how many bytes can be added to the buffer.
Definition: AsyncInputStream.hxx:128
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
AsyncInputStream::SeekState::SCHEDULED
AsyncInputStream::IsBufferEmpty
bool IsBufferEmpty() const
Definition: AsyncInputStream.hxx:116
AsyncInputStream::SeekDone
void SeekDone()
Call this after seeking has finished.
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::AppendToBuffer
void AppendToBuffer(const void *data, size_t append_size)
Append data to the buffer.