OpenDaVINCI  4.16.0
Player.h
Go to the documentation of this file.
1 
20 #ifndef OPENDAVINCI_TOOLS_PLAYER_PLAYER_H_
21 #define OPENDAVINCI_TOOLS_PLAYER_PLAYER_H_
22 
23 #include <deque>
24 #include <fstream>
25 #include <map>
26 #include <memory>
27 #include <thread>
28 
35 
37 
38 namespace odtools {
39  namespace player {
40 
41  class PlayerDelegate;
42  class RecMemIndex;
43 
44  using namespace std;
45 
46  class IndexEntry {
47  public:
48  IndexEntry();
49  IndexEntry(const int64_t &sampleTimeStamp, const uint64_t &filePosition);
50  IndexEntry(const int64_t &sampleTimeStamp, const uint64_t &filePosition, const string &nameOfSharedMemorySegment, const uint32_t &sizeOfSharedMemorySegment);
51 
52  public:
54  uint64_t m_filePosition;
58  };
59 
65  private:
66  enum {
67  ONE_MILLISECOND_IN_MICROSECONDS = 1000,
68  ONE_SECOND_IN_MICROSECONDS = 1000 * ONE_MILLISECOND_IN_MICROSECONDS,
69  MAX_DELAY_IN_MICROSECONDS = 5 * ONE_SECOND_IN_MICROSECONDS,
70  LOOK_AHEAD_IN_S = 30,
71  MIN_ENTRIES_FOR_LOOK_AHEAD = 5000,
72  };
73 
74  private:
82  Player(const Player &/*obj*/) = delete;
83 
92  Player& operator=(const Player &/*obj*/) = delete;
93 
94  public:
104  Player(const odcore::io::URL &url, const bool &autoRewind, const uint32_t &memorySegmentSize, const uint32_t &numberOfMemorySegments, const bool &threading);
105 
106  virtual ~Player();
107 
113  odcore::data::Container getNextContainerToBeSent() throw (odcore::exceptions::ArrayIndexOutOfBoundsException);
114 
120  uint32_t getDelay() const;
121 
129  uint32_t getCorrectedDelay() const;
130 
136  bool hasMoreData() const;
137 
141  void rewind();
142 
148  uint32_t getTotalNumberOfContainersInRecFile() const;
149 
150  private:
151  // Internal methods without Lock.
152  bool hasMoreDataFromRecFile() const;
153  bool hasMoreDataFromRecMemFile() const;
154 
161  void initializeIndex();
162 
167  void computeInitialCacheLevelAndFillCache();
168 
172  void resetCaches();
173 
177  inline void resetIterators();
178 
186  uint32_t fillContainerCache(const uint32_t &maxNumberOfEntriesToReadFromFile);
187 
192  inline void checkAvailabilityOfNextContainerToBeReplayed();
193 
194  private:
202  inline void checkForEndOfIndexAndThrowExceptionOrAutoRewind() throw (odcore::exceptions::ArrayIndexOutOfBoundsException);
203 
204  private: // Data for the Player.
205  bool m_threading;
206 
207  odcore::io::URL m_url;
208 
209  // Handle to .rec file.
210  fstream m_recFile;
211  bool m_recFileValid;
212 
213  private: // Player states.
214  bool m_autoRewind;
215 
216  private: // Index and cache management.
217  // Global index: Mapping SampleTimeStamp --> cache entry (holding the actual content from .rec, .rec.mem, or .h264 file)
218  mutable odcore::base::Mutex m_indexMutex;
219  multimap<int64_t, IndexEntry> m_index;
220 
221  // Pointers to the current container to be replayed and the
222  // container that has be replayed from the global index.
223  multimap<int64_t, IndexEntry>::iterator m_previousPreviousContainerAlreadyReplayed;
224  multimap<int64_t, IndexEntry>::iterator m_previousContainerAlreadyReplayed;
225  multimap<int64_t, IndexEntry>::iterator m_currentContainerToReplay;
226 
227  // Information about the index.
228  multimap<int64_t, IndexEntry>::iterator m_nextEntryToReadFromRecFile;
229 
230  uint32_t m_desiredInitialLevel;
231 
232  // Fields to compute replay throughput for cache management.
233  odcore::data::TimeStamp m_firstTimePointReturningAContainer;
234  uint64_t m_numberOfReturnedContainersInTotal;
235 
236  uint32_t m_delay;
237  uint32_t m_correctedDelay;
238 
239  private:
245  void setContainerCacheFillingRunning(const bool &running);
246  bool isContainerCacheFillingRunning() const;
247 
251  void manageCache();
252 
260  float checkRefillingCache(const uint32_t &numberOfEntries, float refillMultiplicator);
261 
262  private:
263  mutable odcore::base::Mutex m_containerCacheFillingThreadIsRunningMutex;
264  bool m_containerCacheFillingThreadIsRunning;
265  std::thread m_containerCacheFillingThread;
266 
267  // Mapping of pos_type (within .rec file) --> Container (read from .rec file).
268  map<uint64_t, odcore::data::Container> m_containerCache;
269 
270  private:
271  unique_ptr<RecMemIndex> m_recMemIndex;
272 
273  public:
281  void registerPlayerDelegate(const uint32_t &containerID, PlayerDelegate* p);
282 
283  private:
284  // Map to handle PlayerDelegates.
285  odcore::base::Mutex m_mapOfPlayerDelegatesMutex;
286  map<int32_t, PlayerDelegate*> m_mapOfPlayerDelegates;
287 
288  public:
289  void setPlayerListener(PlayerListener *l);
290 
291  private:
292  odcore::base::Mutex m_playerListenerMutex;
293  PlayerListener *m_playerListener;
294  };
295 
296  } // player
297 } // tools
298 
299 #endif /*OPENDAVINCI_TOOLS_PLAYER_PLAYER_H_*/
Definition: URL.h:38
Definition: base/Mutex.h:49
Definition: Player.h:46
Definition: Container.h:40
int64_t m_sampleTimeStamp
Definition: Player.h:53
#define OPENDAVINCI_API
Definition: opendavinci.h:67
Definition: PlayerDelegate.h:41
Definition: PlayerListener.h:41
uint32_t m_sizeOfSharedMemorySegment
Definition: Player.h:56
uint64_t m_filePosition
Definition: Player.h:54
Definition: TimeStamp.h:38
string m_nameOfSharedMemorySegment
Definition: Player.h:55
bool m_available
Definition: Player.h:57
Definition: Player.h:64