MPD  0.20.6
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 {
39  };
40 
41  DeferredCall deferred_resume;
42  DeferredCall deferred_seek;
43 
44  HugeAllocation allocation;
45 
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 
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
void CommitWriteBuffer(size_t nbytes)
virtual void DoResume()=0
Implement code here that will resume the stream after it has been paused due to full input buffer...
std::exception_ptr postponed_exception
bool IsEOF() final
Returns true if the stream has reached end-of-file.
bool IsAvailable() final
Returns true if the next read operation will not block: either data is available, or end-of-stream ha...
The meta information about a song file.
Definition: Tag.hxx:34
virtual void DoSeek(offset_type new_offset)=0
The actual Seek() implementation.
::offset_type offset_type
Definition: InputStream.hxx:38
Automatic huge memory allocation management.
Helper class for moving asynchronous (non-blocking) InputStream implementations to the I/O thread...
Definition: Cond.hxx:41
Definition: Mutex.hxx:43
CircularBuffer< uint8_t >::Range PrepareWriteBuffer()
bool IsPaused() const
virtual ~AsyncInputStream()
bool IsSeekPending() const
Tag * ReadTag() final
Reads the tag from the stream.
Range Write()
Prepares writing.
bool IsBufferFull() const
constexpr size_type GetSpace() const
Returns the number of elements that can be added to this buffer.
void SetClosed()
Declare that the underlying stream was closed.
constexpr bool IsFull() const
constexpr bool IsEmpty() const
void SetTag(Tag *_tag)
Pass an tag from the I/O thread to the client thread.
gcc_pure size_t GetBufferSpace() const
Determine how many bytes can be added to the buffer.
uint64_t offset_type
A type for absolute offsets in a file.
Definition: Offset.hxx:30
size_t Read(void *ptr, size_t read_size) final
Reads data from the stream into the caller-supplied buffer.
void Seek(offset_type new_offset) final
Seeks to the specified position in the stream.
Invoke a method call in the EventLoop.
AsyncInputStream(const char *_url, Mutex &_mutex, Cond &_cond, size_t _buffer_size, size_t _resume_at)
bool IsBufferEmpty() const
void SeekDone()
Call this after seeking has finished.
#define gcc_pure
Definition: Compiler.h:116
void Check() final
Check for errors that may have occurred in the I/O thread.
void AppendToBuffer(const void *data, size_t append_size)
Append data to the buffer.