libcluon  0.0.148
Player.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017-2018 Christian Berger
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 #ifndef CLUON_PLAYER_HPP
10 #define CLUON_PLAYER_HPP
11 
12 #include "cluon/cluon.hpp"
13 #include "cluon/cluonDataStructures.hpp"
14 
15 #include <cstdint>
16 #include <deque>
17 #include <fstream>
18 #include <functional>
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <thread>
24 #include <utility>
25 
26 namespace cluon {
27 
29  public:
30  IndexEntry() = default;
31  IndexEntry(const int64_t &sampleTimeStamp, const uint64_t &filePosition) noexcept;
32 
33  public:
34  int64_t m_sampleTimeStamp{0};
35  uint64_t m_filePosition{0};
36  bool m_available{0};
37 };
38 
40  private:
41  enum {
42  ONE_MILLISECOND_IN_MICROSECONDS = 1000,
43  ONE_SECOND_IN_MICROSECONDS = 1000 * ONE_MILLISECOND_IN_MICROSECONDS,
44  MAX_DELAY_IN_MICROSECONDS = 1 * ONE_SECOND_IN_MICROSECONDS,
45  LOOK_AHEAD_IN_S = 30,
46  MIN_ENTRIES_FOR_LOOK_AHEAD = 5000,
47  };
48 
49  private:
50  Player(const Player &) = delete;
51  Player(Player &&) = delete;
52  Player &operator=(Player &&) = delete;
53  Player &operator=(const Player &other) = delete;
54 
55  public:
63  Player(const std::string &file, const bool &autoRewind, const bool &threading) noexcept;
64  ~Player();
65 
70  std::pair<bool, cluon::data::Envelope> getNextEnvelopeToBeReplayed() noexcept;
71 
75  uint32_t delay() const noexcept;
76 
80  bool hasMoreData() const noexcept;
81 
85  void rewind() noexcept;
86 
87  void seekTo(float ratio) noexcept;
88 
92  uint32_t totalNumberOfEnvelopesInRecFile() const noexcept;
93 
94  private:
95  // Internal methods without Lock.
96  bool hasMoreDataFromRecFile() const noexcept;
97 
103  void initializeIndex() noexcept;
104 
109  void computeInitialCacheLevelAndFillCache() noexcept;
110 
114  void resetCaches() noexcept;
115 
119  inline void resetIterators() noexcept;
120 
128  uint32_t fillEnvelopeCache(const uint32_t &maxNumberOfEntriesToReadFromFile) noexcept;
129 
134  inline void checkAvailabilityOfNextEnvelopeToBeReplayed() noexcept;
135 
136  private: // Data for the Player.
137  bool m_threading;
138 
139  std::string m_file;
140 
141  // Handle to .rec file.
142  std::fstream m_recFile;
143  bool m_recFileValid;
144 
145  private: // Player states.
146  bool m_autoRewind;
147 
148  private: // Index and cache management.
149  // Global index: Mapping SampleTimeStamp --> cache entry (holding the actual content from .rec file).
150  mutable std::mutex m_indexMutex;
151  std::multimap<int64_t, IndexEntry> m_index;
152 
153  // Pointers to the current envelope to be replayed and the
154  // envelope that has be replayed from the global index.
155  std::multimap<int64_t, IndexEntry>::iterator m_previousPreviousEnvelopeAlreadyReplayed;
156  std::multimap<int64_t, IndexEntry>::iterator m_previousEnvelopeAlreadyReplayed;
157  std::multimap<int64_t, IndexEntry>::iterator m_currentEnvelopeToReplay;
158 
159  // Information about the index.
160  std::multimap<int64_t, IndexEntry>::iterator m_nextEntryToReadFromRecFile;
161 
162  uint32_t m_desiredInitialLevel;
163 
164  // Fields to compute replay throughput for cache management.
165  cluon::data::TimeStamp m_firstTimePointReturningAEnvelope;
166  uint64_t m_numberOfReturnedEnvelopesInTotal;
167 
168  uint32_t m_delay;
169 
170  private:
176  void setEnvelopeCacheFillingRunning(const bool &running) noexcept;
177  bool isEnvelopeCacheFillingRunning() const noexcept;
178 
182  void manageCache() noexcept;
183 
191  float checkRefillingCache(const uint32_t &numberOfEntries, float refillMultiplicator) noexcept;
192 
193  private:
194  mutable std::mutex m_envelopeCacheFillingThreadIsRunningMutex;
195  bool m_envelopeCacheFillingThreadIsRunning;
196  std::thread m_envelopeCacheFillingThread;
197 
198  // Mapping of pos_type (within .rec file) --> cluon::data::Envelope (read from .rec file).
199  std::map<uint64_t, cluon::data::Envelope> m_envelopeCache;
200 
201  public:
202  void setPlayerListener(std::function<void(cluon::data::PlayerStatus playerStatus)> playerListener) noexcept;
203 
204  private:
205  std::mutex m_playerListenerMutex;
206  std::function<void(cluon::data::PlayerStatus playerStatus)> m_playerListener{nullptr};
207 };
208 
209 } // namespace cluon
210 
211 #endif
Definition: Player.hpp:39
Definition: cluon.hpp:65
#define LIBCLUON_API
Definition: cluon.hpp:56
Definition: Player.hpp:28