312 lines
7.3 KiB
C++
312 lines
7.3 KiB
C++
#include "echoion/core/playback/QueueService.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <chrono>
|
|
|
|
namespace Echoion::Core
|
|
{
|
|
|
|
Api::Result<uint64_t> QueueService::enqueue(const Api::TrackInfo& track,
|
|
std::string addedBy,
|
|
std::size_t maxSize)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
if (entries_.size() >= maxSize) {
|
|
return Api::fail(Api::ErrorCode::InvalidArgument, "Queue is full");
|
|
}
|
|
|
|
const uint64_t queueId = nextQueueId_++;
|
|
entries_.push_back(
|
|
Entry{.queueId = queueId,
|
|
.track = track,
|
|
.addedAt = std::chrono::system_clock::now(),
|
|
.addedBy = std::move(addedBy),
|
|
.isPlaying = false,
|
|
.position = std::nullopt});
|
|
|
|
if (shuffle_) {
|
|
// Insert the new position at a random spot among the remaining
|
|
// (not yet played) shuffled picks.
|
|
const std::size_t newPos = entries_.size() - 1;
|
|
if (shuffleIndex_ < shuffledIndices_.size()) {
|
|
std::uniform_int_distribution<std::size_t> dist(
|
|
shuffleIndex_, shuffledIndices_.size());
|
|
shuffledIndices_.insert(
|
|
shuffledIndices_.begin() +
|
|
static_cast<std::ptrdiff_t>(dist(rng_)),
|
|
newPos);
|
|
} else {
|
|
shuffledIndices_.push_back(newPos);
|
|
}
|
|
}
|
|
|
|
return queueId;
|
|
}
|
|
|
|
Api::Result<QueueService::Entry> QueueService::removeAt(uint32_t position)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
if (position >= entries_.size()) {
|
|
return Api::fail(Api::ErrorCode::InvalidArgument,
|
|
"Invalid position");
|
|
}
|
|
|
|
Entry removed = entries_[position];
|
|
entries_.erase(entries_.begin() + position);
|
|
|
|
if (shuffle_ && !shuffledIndices_.empty()) {
|
|
auto it = std::find(shuffledIndices_.begin(),
|
|
shuffledIndices_.end(), position);
|
|
if (it != shuffledIndices_.end()) {
|
|
const std::size_t removedShuffleIndex = static_cast<std::size_t>(
|
|
std::distance(shuffledIndices_.begin(), it));
|
|
shuffledIndices_.erase(it);
|
|
if (removedShuffleIndex < shuffleIndex_) {
|
|
--shuffleIndex_;
|
|
}
|
|
}
|
|
for (auto& idx : shuffledIndices_) {
|
|
if (idx > position) {
|
|
--idx;
|
|
}
|
|
}
|
|
}
|
|
|
|
return removed;
|
|
}
|
|
|
|
uint32_t QueueService::clear()
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
const auto previous = static_cast<uint32_t>(entries_.size());
|
|
entries_.clear();
|
|
shuffledIndices_.clear();
|
|
shuffleIndex_ = 0;
|
|
return previous;
|
|
}
|
|
|
|
Api::Result<uint64_t> QueueService::move(uint32_t from, uint32_t to)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
if (from >= entries_.size() || to >= entries_.size()) {
|
|
return Api::fail(Api::ErrorCode::InvalidArgument,
|
|
"Invalid position");
|
|
}
|
|
|
|
Entry entry = entries_[from];
|
|
entries_.erase(entries_.begin() + from);
|
|
entries_.insert(entries_.begin() + to, entry);
|
|
|
|
if (shuffle_ && !shuffledIndices_.empty()) {
|
|
for (auto& idx : shuffledIndices_) {
|
|
if (from < to) {
|
|
// Moving forward: positions in (from, to] shift down.
|
|
if (idx > from && idx <= to) {
|
|
--idx;
|
|
} else if (idx == from) {
|
|
idx = to;
|
|
}
|
|
} else if (from > to) {
|
|
// Moving backward: positions in [to, from) shift up.
|
|
if (idx >= to && idx < from) {
|
|
++idx;
|
|
} else if (idx == from) {
|
|
idx = to;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return entry.queueId;
|
|
}
|
|
|
|
std::optional<QueueService::Entry> QueueService::popNext()
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return popNextLocked();
|
|
}
|
|
|
|
std::optional<QueueService::Entry> QueueService::popIfNext(uint64_t queueId)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
auto pos = nextPositionLocked();
|
|
if (!pos || entries_[*pos].queueId != queueId) {
|
|
return std::nullopt;
|
|
}
|
|
return popNextLocked();
|
|
}
|
|
|
|
void QueueService::replaceAll(std::vector<Entry> entries)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
entries_ = std::move(entries);
|
|
// Keep queue ids unique after a restore.
|
|
for (const auto& entry : entries_) {
|
|
nextQueueId_ = std::max(nextQueueId_, entry.queueId + 1);
|
|
}
|
|
if (shuffle_ && !entries_.empty()) {
|
|
rebuildShuffledIndicesLocked();
|
|
} else {
|
|
shuffledIndices_.clear();
|
|
shuffleIndex_ = 0;
|
|
}
|
|
}
|
|
|
|
void QueueService::shuffleInPlace()
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
std::shuffle(entries_.begin(), entries_.end(), rng_);
|
|
if (shuffle_) {
|
|
rebuildShuffledIndicesLocked();
|
|
}
|
|
}
|
|
|
|
bool QueueService::toggleShuffle()
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
shuffle_ = !shuffle_;
|
|
if (shuffle_) {
|
|
rebuildShuffledIndicesLocked();
|
|
} else {
|
|
shuffledIndices_.clear();
|
|
shuffleIndex_ = 0;
|
|
}
|
|
return shuffle_;
|
|
}
|
|
|
|
void QueueService::setShuffle(bool enabled)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
if (shuffle_ == enabled) {
|
|
return;
|
|
}
|
|
shuffle_ = enabled;
|
|
if (shuffle_) {
|
|
rebuildShuffledIndicesLocked();
|
|
} else {
|
|
shuffledIndices_.clear();
|
|
shuffleIndex_ = 0;
|
|
}
|
|
}
|
|
|
|
bool QueueService::shuffleEnabled() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return shuffle_;
|
|
}
|
|
|
|
bool QueueService::toggleRepeat()
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
repeat_ = !repeat_;
|
|
return repeat_;
|
|
}
|
|
|
|
void QueueService::setRepeat(bool enabled)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
repeat_ = enabled;
|
|
}
|
|
|
|
bool QueueService::repeatEnabled() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return repeat_;
|
|
}
|
|
|
|
std::optional<QueueService::Entry> QueueService::peekNext() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
auto pos = nextPositionLocked();
|
|
if (!pos) {
|
|
return std::nullopt;
|
|
}
|
|
return entries_[*pos];
|
|
}
|
|
|
|
std::vector<QueueService::Entry> QueueService::snapshot() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return entries_;
|
|
}
|
|
|
|
std::size_t QueueService::size() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return entries_.size();
|
|
}
|
|
|
|
bool QueueService::empty() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return entries_.empty();
|
|
}
|
|
|
|
uint64_t QueueService::allocateQueueId()
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return nextQueueId_++;
|
|
}
|
|
|
|
void QueueService::rebuildShuffledIndicesLocked()
|
|
{
|
|
shuffledIndices_.clear();
|
|
shuffledIndices_.reserve(entries_.size());
|
|
for (std::size_t i = 0; i < entries_.size(); ++i) {
|
|
shuffledIndices_.push_back(i);
|
|
}
|
|
std::shuffle(shuffledIndices_.begin(), shuffledIndices_.end(), rng_);
|
|
shuffleIndex_ = 0;
|
|
}
|
|
|
|
std::optional<std::size_t> QueueService::nextPositionLocked() const
|
|
{
|
|
if (entries_.empty()) {
|
|
return std::nullopt;
|
|
}
|
|
if (shuffle_ && !shuffledIndices_.empty() &&
|
|
shuffleIndex_ < shuffledIndices_.size()) {
|
|
const std::size_t pos = shuffledIndices_[shuffleIndex_];
|
|
if (pos < entries_.size()) {
|
|
return pos;
|
|
}
|
|
// Permutation out of sync (defensive) — fall through to front.
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
std::optional<QueueService::Entry> QueueService::popNextLocked()
|
|
{
|
|
auto posOpt = nextPositionLocked();
|
|
if (!posOpt) {
|
|
return std::nullopt;
|
|
}
|
|
const std::size_t pos = *posOpt;
|
|
Entry entry = entries_[pos];
|
|
entries_.erase(entries_.begin() + static_cast<std::ptrdiff_t>(pos));
|
|
|
|
if (shuffle_ && !shuffledIndices_.empty() &&
|
|
shuffleIndex_ < shuffledIndices_.size()) {
|
|
++shuffleIndex_;
|
|
if (shuffleIndex_ >= shuffledIndices_.size()) {
|
|
rebuildShuffledIndicesLocked();
|
|
} else {
|
|
// Fix up remaining picks for the removed position.
|
|
for (std::size_t i = shuffleIndex_;
|
|
i < shuffledIndices_.size(); ++i) {
|
|
if (shuffledIndices_[i] > pos) {
|
|
--shuffledIndices_[i];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return entry;
|
|
}
|
|
|
|
} // namespace Echoion::Core
|