6#ifndef OPENEV_READERS_PLAYER_HPP
7#define OPENEV_READERS_PLAYER_HPP
17#include <opencv2/imgcodecs.hpp>
22[[maybe_unused]]
constexpr bool USING_PLAYER_HPP =
true;
25template <
typename R,
typename =
void>
26struct has_frames_ : std::false_type {};
28struct has_frames_<R, std::void_t<decltype(std::declval<const R &>().frames())>> : std::true_type {};
30template <
typename R,
typename =
void>
31struct has_imu_ : std::false_type {};
33struct has_imu_<R, std::void_t<decltype(std::declval<const R &>().imu())>> : std::true_type {};
41template <
typename Reader>
48 explicit Player_(Reader &reader) : reader_{reader} {}
62 [[nodiscard]]
inline double speed()
const {
70 inline void pause(
const bool paused) {
107 [[nodiscard]]
inline std::size_t
loops()
const {
132 advance_(elapsed_(), events,
nullptr,
nullptr);
140 advance_(elapsed_(), events, &frames,
nullptr);
147 void next(
Queue &events, StampedMatQueue &frames, ImuQueue &imus) {
148 advance_(elapsed_(), events, &frames, &imus);
152 static constexpr double SECONDS_TO_MICROSECONDS = 1e6;
153 static constexpr std::size_t BATCH = 256;
156 std::chrono::steady_clock::time_point last_;
157 bool started_{
false};
160 bool finished_{
false};
161 bool anchored_{
false};
163 TimeType playhead_{0};
164 std::size_t frame_{0};
165 std::size_t sample_{0};
166 std::size_t loops_{0};
168 [[nodiscard]] std::size_t batch_()
const {
169 const std::size_t level = reader_.prefetchCapacity();
170 return level > 0 ? std::min(level, BATCH) : BATCH;
173 [[nodiscard]] TimeType elapsed_() {
174 const auto now = std::chrono::steady_clock::now();
175 const TimeType elapsed = started_ && !paused_ ? std::chrono::duration<double>(now - last_).count() * SECONDS_TO_MICROSECONDS * speed_ : 0;
183 void advance_(
const TimeType time, Queue &events, StampedMatQueue *frames, ImuQueue *imus) {
190 first = &reader_.events(batch_());
192 if(!first->empty()) {
193 playhead_ = first->front().t;
201 if(pending->empty()) {
202 pending = &reader_.events(batch_());
211 finished_ = reader_.events(batch_()).empty();
214 const Event &e = queue.front();
215 if(e.t > playhead_) {
222 if constexpr(has_frames_<Reader>::value) {
223 if(frames !=
nullptr) {
224 const auto &all = reader_.frames();
225 while(frame_ < all.size() && all[frame_].t <= playhead_) {
227 cv::imread(all[frame_].path, cv::IMREAD_GRAYSCALE).copyTo(frame);
228 frame.t = all[frame_].t;
237 if constexpr(has_imu_<Reader>::value) {
238 if(imus !=
nullptr) {
239 const auto &all = reader_.imu();
240 while(sample_ < all.size() && all[sample_].t <= playhead_) {
241 imus->push(all[sample_]);
double speed() const
Get the playback speed.
Definition player.hpp:62
bool isFinished() const
Check if the reader ran out of events with setLoop(false).
Definition player.hpp:123
std::size_t loops() const
Number of times the playback has started again, either by restart() or by reaching the end.
Definition player.hpp:107
void pause(const bool paused)
Hold the playhead, so next() delivers nothing.
Definition player.hpp:70
void setSpeed(const double speed)
Set the playback speed.
Definition player.hpp:54
void next(Queue &events, StampedMatQueue &frames, ImuQueue &imus)
Definition player.hpp:147
void next(Queue &events, StampedMatQueue &frames)
Definition player.hpp:139
void setLoop(const bool loop)
Choose what happens when the reader runs out of events.
Definition player.hpp:86
Player_(Reader &reader)
Construct the player. The playback starts with the first call to next().
Definition player.hpp:48
void next(Queue &events)
Deliver what happened since the previous call.
Definition player.hpp:131
TimeType playhead() const
Current position of the playback, which starts at the timestamp of the first event.
Definition player.hpp:115
void restart()
Start again from the beginning.
Definition player.hpp:93
bool isPaused() const
Check if the playback is paused.
Definition player.hpp:78
Queue container for basic event structures, safe between a thread that pushes and a thread that pops.
ConcurrentQueuei ConcurrentQueue
Definition concurrent_queue.hpp:93
Queue container for basic event structures.
Queuei Queue
Definition queue.hpp:40
Basic event-based vision structures based on OpenCV components.
Eventi Event
Definition types.hpp:262