From 299f3cc48f683d3e6dec1efb4957fdb49b4de2c3 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Mon, 5 Feb 2024 11:02:45 +1100 Subject: Preserve the queue when going into standby --- src/audio/track_queue.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'src/audio/track_queue.cpp') diff --git a/src/audio/track_queue.cpp b/src/audio/track_queue.cpp index 33858e0a..d68f2821 100644 --- a/src/audio/track_queue.cpp +++ b/src/audio/track_queue.cpp @@ -5,6 +5,7 @@ */ #include "track_queue.hpp" +#include #include #include @@ -310,4 +311,74 @@ auto TrackQueue::repeat() const -> bool { return repeat_; } +auto TrackQueue::serialise() -> std::string { + cppbor::Array tracks{}; + for (database::TrackId track : tracks_) { + tracks.add(cppbor::Uint(track)); + } + // FIXME: this should include the RandomIterator's seed as well. + cppbor::Array encoded{ + cppbor::Uint{pos_}, + std::move(tracks), + }; + return encoded.toString(); +} + +class QueueParseClient : public cppbor::ParseClient { + public: + QueueParseClient(size_t& pos, std::pmr::vector& tracks) + : pos_(pos), + tracks_(tracks), + in_root_array_(false), + in_track_list_(false) {} + + ParseClient* item(std::unique_ptr& item, + const uint8_t* hdrBegin, + const uint8_t* valueBegin, + const uint8_t* end) override { + if (item->type() == cppbor::ARRAY) { + if (!in_root_array_) { + in_root_array_ = true; + } else { + in_track_list_ = true; + } + } else if (item->type() == cppbor::UINT) { + auto val = item->asUint()->unsignedValue(); + if (in_track_list_) { + tracks_.push_back(val); + } else { + pos_ = static_cast(val); + } + } + return this; + } + + ParseClient* itemEnd(std::unique_ptr& item, + const uint8_t* hdrBegin, + const uint8_t* valueBegin, + const uint8_t* end) override { + return this; + } + + void error(const uint8_t* position, + const std::string& errorMessage) override {} + + private: + size_t& pos_; + std::pmr::vector& tracks_; + + bool in_root_array_; + bool in_track_list_; +}; + +auto TrackQueue::deserialise(const std::string& s) -> void { + if (s.empty()) { + return; + } + QueueParseClient client{pos_, tracks_}; + const uint8_t* data = reinterpret_cast(s.data()); + cppbor::parse(data, data + s.size(), &client); + notifyChanged(true); +} + } // namespace audio -- cgit v1.2.3