summaryrefslogtreecommitdiff
path: root/src/audio/include/track_queue.hpp
blob: 49c0d61b1cf13e3d2a1ac4eb36bf220145b30bd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#pragma once

#include <list>
#include <memory>
#include <mutex>
#include <vector>

#include "source.hpp"
#include "track.hpp"

namespace audio {

/*
 * 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
 * consistency between calls however. For example, there may be data changes
 * between consecutive calls to AddNext() and GetUpcoming();
 */
class TrackQueue {
 public:
  TrackQueue();

  /* Returns the currently playing track. */
  auto GetCurrent() const -> std::optional<database::TrackId>;
  /* Returns, in order, tracks that have been queued to be played next. */
  auto GetUpcoming(std::size_t limit) const -> std::vector<database::TrackId>;

  /*
   * Enqueues a track, placing it immediately after the current track and
   * before anything already queued.
   *
   * If there is no current track, the given track will begin playback.
   */
  auto AddNext(database::TrackId) -> void;
  auto AddNext(std::shared_ptr<playlist::ISource>) -> void;

  auto IncludeNext(std::shared_ptr<playlist::IResetableSource>) -> void;

  /*
   * Enqueues a track, placing it the end of all enqueued tracks.
   *
   * If there is no current track, the given track will begin playback.
   */
  auto AddLast(database::TrackId) -> void;
  auto AddLast(std::shared_ptr<playlist::ISource>) -> void;

  auto IncludeLast(std::shared_ptr<playlist::IResetableSource>) -> void;

  /*
   * Advances to the next track in the queue, placing the current track at the
   * front of the 'played' queue.
   */
  auto Next() -> void;
  auto Previous() -> void;

  /*
   * Removes all tracks from all queues, and stops any currently playing track.
   */
  auto Clear() -> void;

  TrackQueue(const TrackQueue&) = delete;
  TrackQueue& operator=(const TrackQueue&) = delete;

 private:
  mutable std::mutex mutex_;

  std::list<std::variant<database::TrackId,
                         std::shared_ptr<playlist::IResetableSource>>>
      played_;
  std::list<std::variant<database::TrackId,
                         std::shared_ptr<playlist::ISource>,
                         std::shared_ptr<playlist::IResetableSource>>>
      enqueued_;
};

}  // namespace audio