You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
171 lines
4.1 KiB
171 lines
4.1 KiB
2 years ago
|
/*
|
||
|
* Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||
|
*
|
||
|
* SPDX-License-Identifier: GPL-3.0-only
|
||
|
*/
|
||
|
|
||
|
#pragma once
|
||
|
|
||
2 years ago
|
#include <list>
|
||
|
#include <memory>
|
||
2 years ago
|
#include <mutex>
|
||
1 year ago
|
#include <shared_mutex>
|
||
2 years ago
|
#include <vector>
|
||
|
|
||
1 year ago
|
#include "audio_events.hpp"
|
||
1 year ago
|
#include "cppbor_parse.h"
|
||
1 year ago
|
#include "database.hpp"
|
||
1 year ago
|
#include "tasks.hpp"
|
||
2 years ago
|
#include "track.hpp"
|
||
|
|
||
|
namespace audio {
|
||
|
|
||
1 year ago
|
/*
|
||
|
* Utility that uses a Miller shuffle to yield well-distributed random indexes
|
||
|
* from within a range.
|
||
|
*/
|
||
|
class RandomIterator {
|
||
|
public:
|
||
1 year ago
|
RandomIterator();
|
||
1 year ago
|
RandomIterator(size_t size);
|
||
|
|
||
|
auto current() const -> size_t;
|
||
|
|
||
|
auto next() -> void;
|
||
|
auto prev() -> void;
|
||
|
|
||
|
// Note resizing has the side-effect of restarting iteration.
|
||
|
auto resize(size_t) -> void;
|
||
1 year ago
|
auto replay(bool) -> void;
|
||
1 year ago
|
|
||
1 year ago
|
auto seed() -> size_t& { return seed_; }
|
||
|
auto pos() -> size_t& { return pos_; }
|
||
|
auto size() -> size_t& { return size_; }
|
||
|
|
||
1 year ago
|
private:
|
||
|
size_t seed_;
|
||
|
size_t pos_;
|
||
|
size_t size_;
|
||
1 year ago
|
bool replay_;
|
||
1 year ago
|
};
|
||
|
|
||
2 years ago
|
/*
|
||
|
* Owns and manages a complete view of the playback queue. Includes the
|
||
|
* currently playing track, a truncated list of previously played tracks, and
|
||
|
* all future tracks that have been queued.
|
||
|
*
|
||
|
* In order to not use all of our memory, this class deals strictly with track
|
||
|
* ids. Consumers that need more data than this should fetch it from the
|
||
|
* database.
|
||
|
*
|
||
|
* Instances of this class are broadly safe to use from multiple tasks; each
|
||
|
* method represents an atomic operation. No guarantees are made about
|
||
1 year ago
|
* consistency between calls however.
|
||
2 years ago
|
*/
|
||
|
class TrackQueue {
|
||
|
public:
|
||
1 year ago
|
TrackQueue(tasks::WorkerPool& bg_worker);
|
||
1 year ago
|
|
||
2 years ago
|
/* Returns the currently playing track. */
|
||
1 year ago
|
auto current() const -> std::optional<database::TrackId>;
|
||
1 year ago
|
|
||
2 years ago
|
/* Returns, in order, tracks that have been queued to be played next. */
|
||
1 year ago
|
auto peekNext(std::size_t limit) const -> std::vector<database::TrackId>;
|
||
2 years ago
|
|
||
|
/*
|
||
1 year ago
|
* Returns the tracks in the queue that have already been played, ordered
|
||
|
* most recently played first.
|
||
2 years ago
|
*/
|
||
1 year ago
|
auto peekPrevious(std::size_t limit) const -> std::vector<database::TrackId>;
|
||
2 years ago
|
|
||
1 year ago
|
auto currentPosition() const -> size_t;
|
||
|
auto totalSize() const -> size_t;
|
||
2 years ago
|
|
||
1 year ago
|
using Item = std::variant<database::TrackId, database::TrackIterator>;
|
||
1 year ago
|
auto insert(Item, size_t index = 0) -> void;
|
||
1 year ago
|
auto append(Item i) -> void;
|
||
2 years ago
|
|
||
|
/*
|
||
|
* Advances to the next track in the queue, placing the current track at the
|
||
|
* front of the 'played' queue.
|
||
|
*/
|
||
1 year ago
|
auto next() -> void;
|
||
|
auto previous() -> void;
|
||
1 year ago
|
|
||
1 year ago
|
/*
|
||
1 year ago
|
* Called when the current track finishes
|
||
|
*/
|
||
|
auto finish() -> void;
|
||
|
|
||
1 year ago
|
auto skipTo(database::TrackId) -> void;
|
||
2 years ago
|
|
||
|
/*
|
||
|
* Removes all tracks from all queues, and stops any currently playing track.
|
||
|
*/
|
||
1 year ago
|
auto clear() -> void;
|
||
1 year ago
|
|
||
1 year ago
|
auto random(bool) -> void;
|
||
|
auto random() const -> bool;
|
||
|
|
||
|
auto repeat(bool) -> void;
|
||
|
auto repeat() const -> bool;
|
||
|
|
||
1 year ago
|
auto replay(bool) -> void;
|
||
|
auto replay() const -> bool;
|
||
|
|
||
1 year ago
|
auto serialise() -> std::string;
|
||
|
auto deserialise(const std::string&) -> void;
|
||
|
|
||
1 year ago
|
// Cannot be copied or moved.
|
||
2 years ago
|
TrackQueue(const TrackQueue&) = delete;
|
||
|
TrackQueue& operator=(const TrackQueue&) = delete;
|
||
|
|
||
|
private:
|
||
1 year ago
|
auto next(QueueUpdate::Reason r) -> void;
|
||
|
|
||
1 year ago
|
mutable std::shared_mutex mutex_;
|
||
1 year ago
|
|
||
1 year ago
|
tasks::WorkerPool& bg_worker_;
|
||
1 year ago
|
|
||
1 year ago
|
size_t pos_;
|
||
|
std::pmr::vector<database::TrackId> tracks_;
|
||
1 year ago
|
|
||
|
std::optional<RandomIterator> shuffle_;
|
||
|
bool repeat_;
|
||
1 year ago
|
bool replay_;
|
||
1 year ago
|
|
||
|
class QueueParseClient : public cppbor::ParseClient {
|
||
|
public:
|
||
|
QueueParseClient(TrackQueue& queue);
|
||
|
|
||
|
ParseClient* item(std::unique_ptr<cppbor::Item>& item,
|
||
|
const uint8_t* hdrBegin,
|
||
|
const uint8_t* valueBegin,
|
||
|
const uint8_t* end) override;
|
||
|
|
||
|
ParseClient* itemEnd(std::unique_ptr<cppbor::Item>& item,
|
||
|
const uint8_t* hdrBegin,
|
||
|
const uint8_t* valueBegin,
|
||
|
const uint8_t* end) override;
|
||
|
|
||
|
void error(const uint8_t* position,
|
||
|
const std::string& errorMessage) override {}
|
||
|
|
||
|
private:
|
||
|
TrackQueue& queue_;
|
||
|
|
||
|
enum class State {
|
||
|
kInit,
|
||
|
kRoot,
|
||
|
kMetadata,
|
||
|
kShuffle,
|
||
|
kTracks,
|
||
|
kFinished,
|
||
|
};
|
||
|
State state_;
|
||
|
size_t i_;
|
||
|
};
|
||
2 years ago
|
};
|
||
|
|
||
|
} // namespace audio
|