summaryrefslogtreecommitdiff
path: root/src/audio/track_queue.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2024-02-05 11:02:45 +1100
committerjacqueline <me@jacqueline.id.au>2024-02-05 11:03:52 +1100
commit299f3cc48f683d3e6dec1efb4957fdb49b4de2c3 (patch)
tree6eb0445c300492e53cdc048f12cc6780a12c55eb /src/audio/track_queue.cpp
parent811c335c2ac425320a1949ab23378172e86ae60a (diff)
downloadtangara-fw-299f3cc48f683d3e6dec1efb4957fdb49b4de2c3.tar.gz
Preserve the queue when going into standby
Diffstat (limited to 'src/audio/track_queue.cpp')
-rw-r--r--src/audio/track_queue.cpp71
1 files changed, 71 insertions, 0 deletions
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 <stdint.h>
#include <algorithm>
#include <cstdint>
@@ -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<database::TrackId>& tracks)
+ : pos_(pos),
+ tracks_(tracks),
+ in_root_array_(false),
+ in_track_list_(false) {}
+
+ ParseClient* item(std::unique_ptr<cppbor::Item>& 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<size_t>(val);
+ }
+ }
+ return this;
+ }
+
+ ParseClient* itemEnd(std::unique_ptr<cppbor::Item>& 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<database::TrackId>& 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<const uint8_t*>(s.data());
+ cppbor::parse(data, data + s.size(), &client);
+ notifyChanged(true);
+}
+
} // namespace audio