summaryrefslogtreecommitdiff
path: root/src/audio/include/fatfs_audio_input.hpp
blob: b7b1d18e3441626bd0869e17a776f5f5cbbe1951 (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
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#pragma once

#include <cstddef>
#include <cstdint>
#include <future>
#include <memory>
#include <string>

#include "ff.h"
#include "freertos/portmacro.h"

#include "audio_source.hpp"
#include "codec.hpp"
#include "future_fetcher.hpp"
#include "tag_parser.hpp"
#include "types.hpp"

namespace audio {

/*
 * Audio source that fetches data from a FatFs (or exfat i guess) filesystem.
 *
 * All public methods are safe to call from any task.
 */
class FatfsAudioInput : public IAudioSource {
 public:
  explicit FatfsAudioInput(database::ITagParser& tag_parser);
  ~FatfsAudioInput();

  /*
   * Immediately cease reading any current source, and begin reading from the
   * given file path.
   */
  auto SetPath(std::future<std::optional<std::pmr::string>>) -> void;
  auto SetPath(const std::pmr::string&) -> void;
  auto SetPath() -> void;

  auto HasNewStream() -> bool override;
  auto NextStream() -> std::shared_ptr<codecs::IStream> override;

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

 private:
  auto OpenFile(const std::pmr::string& path) -> bool;

  auto ContainerToStreamType(database::Container)
      -> std::optional<codecs::StreamType>;

  database::ITagParser& tag_parser_;

  std::mutex new_stream_mutex_;
  std::shared_ptr<codecs::IStream> new_stream_;

  SemaphoreHandle_t has_new_stream_;

  std::unique_ptr<database::FutureFetcher<std::optional<std::pmr::string>>>
      pending_path_;
};

}  // namespace audio