MPD  0.20.18
Public Member Functions | Protected Member Functions
ThreadInputStream Class Referenceabstract

Helper class for moving InputStream implementations with blocking backend library implementation to a dedicated thread. More...

#include <ThreadInputStream.hxx>

Inheritance diagram for ThreadInputStream:
[legend]
Collaboration diagram for ThreadInputStream:
[legend]

Public Member Functions

 ThreadInputStream (const char *_plugin, const char *_uri, Mutex &_mutex, Cond &_cond, size_t _buffer_size)
 
virtual ~ThreadInputStream ()
 
void Start ()
 Initialize the object and start the thread. More...
 
void Check () override final
 Check for errors that may have occurred in the I/O thread. More...
 
bool IsEOF () noexcept final
 Returns true if the stream has reached end-of-file. More...
 
bool IsAvailable () noexcept final
 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. More...
 
size_t Read (void *ptr, size_t size) override final
 Reads data from the stream into the caller-supplied buffer. More...
 
- Public Member Functions inherited from InputStream
 InputStream (const char *_uri, Mutex &_mutex, Cond &_cond)
 
virtual ~InputStream ()
 Close the input stream and free resources. More...
 
const char * GetURI () const
 The absolute URI which was used to open this stream. More...
 
void Lock ()
 
void Unlock ()
 
virtual void Update ()
 Update the public attributes. More...
 
void SetReady ()
 
bool IsReady () const
 Return whether the stream is ready for reading and whether the other attributes in this struct are valid. More...
 
void WaitReady ()
 
void LockWaitReady ()
 Wrapper for WaitReady() which locks and unlocks the mutex; the caller must not be holding it already. More...
 
gcc_pure bool HasMimeType () const noexcept
 
gcc_pure const char * GetMimeType () const noexcept
 
void ClearMimeType () noexcept
 
gcc_nonnull_all void SetMimeType (const char *_mime)
 
void SetMimeType (std::string &&_mime)
 
gcc_pure bool KnownSize () const noexcept
 
gcc_pure offset_type GetSize () const noexcept
 
void AddOffset (offset_type delta) noexcept
 
gcc_pure offset_type GetOffset () const noexcept
 
gcc_pure offset_type GetRest () const noexcept
 
gcc_pure bool IsSeekable () const noexcept
 
gcc_pure bool CheapSeeking () const noexcept
 Determines whether seeking is cheap. More...
 
virtual void Seek (offset_type offset)
 Seeks to the specified position in the stream. More...
 
void LockSeek (offset_type offset)
 Wrapper for Seek() which locks and unlocks the mutex; the caller must not be holding it already. More...
 
void Rewind ()
 Rewind to the beginning of the stream. More...
 
void LockRewind ()
 
void Skip (offset_type _offset)
 Skip input bytes. More...
 
void LockSkip (offset_type _offset)
 
gcc_pure bool LockIsEOF () noexcept
 Wrapper for IsEOF() which locks and unlocks the mutex; the caller must not be holding it already. More...
 
virtual gcc_malloc TagReadTag ()
 Reads the tag from the stream. More...
 
gcc_malloc TagLockReadTag ()
 Wrapper for ReadTag() which locks and unlocks the mutex; the caller must not be holding it already. More...
 
gcc_nonnull_all size_t LockRead (void *ptr, size_t size)
 Wrapper for Read() which locks and unlocks the mutex; the caller must not be holding it already. More...
 
gcc_nonnull_all void ReadFull (void *ptr, size_t size)
 Reads the whole data from the stream into the caller-supplied buffer. More...
 
gcc_nonnull_all void LockReadFull (void *ptr, size_t size)
 Wrapper for ReadFull() which locks and unlocks the mutex; the caller must not be holding it already. More...
 

Protected Member Functions

void SetMimeType (const char *_mime)
 
virtual void Open ()
 Optional initialization after entering the thread. More...
 
virtual size_t ThreadRead (void *ptr, size_t size)=0
 Read from the stream. More...
 
virtual void Close ()
 Optional deinitialization before leaving the thread. More...
 
virtual void Cancel ()
 Called from the client thread to cancel a Read() inside the thread. More...
 

Additional Inherited Members

- Public Types inherited from InputStream
typedef ::offset_type offset_type
 
- Static Public Member Functions inherited from InputStream
static gcc_nonnull_all InputStreamPtr Open (const char *uri, Mutex &mutex, Cond &cond)
 Opens a new input stream. More...
 
static gcc_nonnull_all InputStreamPtr OpenReady (const char *uri, Mutex &mutex, Cond &cond)
 Just like Open(), but waits for the stream to become ready. More...
 
- Data Fields inherited from InputStream
Mutexmutex
 A mutex that protects the mutable attributes of this object and its implementation. More...
 
Condcond
 A cond that gets signalled when the state of this object changes from the I/O thread. More...
 
- Protected Attributes inherited from InputStream
bool ready
 indicates whether the stream is ready for reading and whether the other attributes in this struct are valid More...
 
bool seekable
 if true, then the stream is fully seekable More...
 
offset_type size
 the size of the resource, or UNKNOWN_SIZE if unknown More...
 
offset_type offset
 the current offset within the stream More...
 
- Static Protected Attributes inherited from InputStream
static constexpr offset_type UNKNOWN_SIZE = -1
 

Detailed Description

Helper class for moving InputStream implementations with blocking backend library implementation to a dedicated thread.

Data is being read into a ring buffer, and that buffer is then consumed by another thread using the regular InputStream API. This class manages the thread and the buffer.

This works only for "streams": unknown length, no seeking, no tags.

Definition at line 43 of file ThreadInputStream.hxx.

Constructor & Destructor Documentation

◆ ThreadInputStream()

ThreadInputStream::ThreadInputStream ( const char *  _plugin,
const char *  _uri,
Mutex _mutex,
Cond _cond,
size_t  _buffer_size 
)
inline

Definition at line 71 of file ThreadInputStream.hxx.

◆ ~ThreadInputStream()

virtual ThreadInputStream::~ThreadInputStream ( )
virtual

Member Function Documentation

◆ Cancel()

virtual void ThreadInputStream::Cancel ( )
inlineprotectedvirtual

Called from the client thread to cancel a Read() inside the thread.

The InputStream is not locked.

Definition at line 138 of file ThreadInputStream.hxx.

◆ Check()

void ThreadInputStream::Check ( )
finaloverridevirtual

Check for errors that may have occurred in the I/O thread.

Throws std::runtime_error on error.

Reimplemented from InputStream.

◆ Close()

virtual void ThreadInputStream::Close ( )
inlineprotectedvirtual

Optional deinitialization before leaving the thread.

The InputStream is not locked.

Definition at line 130 of file ThreadInputStream.hxx.

◆ IsAvailable()

bool ThreadInputStream::IsAvailable ( )
finalvirtualnoexcept

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.

Reimplemented from InputStream.

◆ IsEOF()

bool ThreadInputStream::IsEOF ( )
finalvirtualnoexcept

Returns true if the stream has reached end-of-file.

The caller must lock the mutex.

Implements InputStream.

◆ Open()

virtual void ThreadInputStream::Open ( )
inlineprotectedvirtual

Optional initialization after entering the thread.

After this returns with success, the InputStream::ready flag is set.

The InputStream is locked. Unlock/relock it if you do a blocking operation.

Throws std::runtime_error on error.

Definition at line 111 of file ThreadInputStream.hxx.

◆ Read()

size_t ThreadInputStream::Read ( void *  ptr,
size_t  size 
)
finaloverridevirtual

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.

Throws std::runtime_error on error.

Parameters
ptrthe buffer to read into
sizethe maximum number of bytes to read
Returns
the number of bytes read

Implements InputStream.

◆ SetMimeType()

void ThreadInputStream::SetMimeType ( const char *  _mime)
inlineprotected

Definition at line 93 of file ThreadInputStream.hxx.

◆ Start()

void ThreadInputStream::Start ( )

Initialize the object and start the thread.

◆ ThreadRead()

virtual size_t ThreadInputStream::ThreadRead ( void *  ptr,
size_t  size 
)
protectedpure virtual

Read from the stream.

The InputStream is not locked.

Throws std::runtime_error on error.

Returns
0 on end-of-file

The documentation for this class was generated from the following file: