2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2016-12-12 15:24:38 +01:00
|
|
|
|
|
|
|
#ifndef SHARED_PIPE_CONSUMER_HXX
|
|
|
|
#define SHARED_PIPE_CONSUMER_HXX
|
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2016-12-12 15:24:38 +01:00
|
|
|
|
|
|
|
struct MusicChunk;
|
|
|
|
class MusicPipe;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A utility class which helps with consuming data from a #MusicPipe.
|
2016-12-26 20:09:10 +01:00
|
|
|
*
|
|
|
|
* This class is intentionally not thread-safe. Since it is designed
|
|
|
|
* to be called from two distinct threads (PlayerThread=feeder and
|
|
|
|
* OutputThread=consumer), all methods must be called with a mutex
|
|
|
|
* locked to serialize access. Usually, this is #AudioOutput::mutex.
|
2016-12-12 15:24:38 +01:00
|
|
|
*/
|
|
|
|
class SharedPipeConsumer {
|
|
|
|
/**
|
|
|
|
* The music pipe which provides music chunks to be played.
|
|
|
|
*/
|
|
|
|
const MusicPipe *pipe = nullptr;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The #MusicChunk which is currently being played. All
|
|
|
|
* chunks before this one may be returned to the #MusicBuffer,
|
|
|
|
* because they are not going to be used by this output
|
|
|
|
* anymore.
|
|
|
|
*/
|
|
|
|
const MusicChunk *chunk;
|
|
|
|
|
|
|
|
/**
|
2016-12-20 16:35:14 +01:00
|
|
|
* Has the output finished playing #chunk?
|
2016-12-12 15:24:38 +01:00
|
|
|
*/
|
|
|
|
bool consumed;
|
|
|
|
|
|
|
|
public:
|
2023-11-26 08:48:08 +01:00
|
|
|
constexpr void Init(const MusicPipe &_pipe) noexcept {
|
2016-12-12 15:24:38 +01:00
|
|
|
pipe = &_pipe;
|
|
|
|
chunk = nullptr;
|
|
|
|
}
|
|
|
|
|
2023-11-26 08:48:08 +01:00
|
|
|
constexpr const MusicPipe &GetPipe() noexcept {
|
2016-12-12 15:24:38 +01:00
|
|
|
assert(pipe != nullptr);
|
|
|
|
|
|
|
|
return *pipe;
|
|
|
|
}
|
|
|
|
|
2023-11-26 08:48:08 +01:00
|
|
|
constexpr bool IsInitial() const noexcept {
|
2016-12-12 15:24:38 +01:00
|
|
|
return chunk == nullptr;
|
|
|
|
}
|
|
|
|
|
2023-11-26 08:48:08 +01:00
|
|
|
constexpr void Cancel() noexcept {
|
2016-12-12 15:24:38 +01:00
|
|
|
chunk = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-05-08 14:44:49 +02:00
|
|
|
const MusicChunk *Get() noexcept;
|
2016-12-12 15:24:38 +01:00
|
|
|
|
2023-11-26 08:48:08 +01:00
|
|
|
constexpr void Consume([[maybe_unused]] const MusicChunk &_chunk) noexcept {
|
2016-12-12 15:24:38 +01:00
|
|
|
assert(chunk != nullptr);
|
|
|
|
assert(chunk == &_chunk);
|
|
|
|
|
|
|
|
consumed = true;
|
|
|
|
}
|
|
|
|
|
2023-03-06 15:57:36 +01:00
|
|
|
[[gnu::pure]]
|
2017-05-08 14:44:49 +02:00
|
|
|
bool IsConsumed(const MusicChunk &_chunk) const noexcept;
|
2016-12-12 15:24:38 +01:00
|
|
|
|
2023-11-26 08:48:08 +01:00
|
|
|
constexpr void ClearTail([[maybe_unused]] const MusicChunk &_chunk) noexcept {
|
2016-12-12 15:24:38 +01:00
|
|
|
assert(chunk == &_chunk);
|
|
|
|
assert(consumed);
|
|
|
|
chunk = nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|