Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
sync.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 
4 #pragma once
5 #ifndef LIBREALSENSE_SYNC_H
6 #define LIBREALSENSE_SYNC_H
7 
8 #include "archive.h"
9 #include <atomic>
10 #include "timestamps.h"
11 #include <chrono>
12 
13 namespace rsimpl
14 {
15  class fps_calc
16  {
17  public:
18  fps_calc(unsigned long long in_number_of_frames_to_sampling, int expected_fps)
19  : _number_of_frames_to_sample(in_number_of_frames_to_sampling),
20  _frame_counter(0),
21  _actual_fps(expected_fps)
22  {
23  _time_samples.reserve(2);
24  }
25  double calc_fps(std::chrono::time_point<std::chrono::system_clock>& now_time)
26  {
27  ++_frame_counter;
28  if (_frame_counter == _number_of_frames_to_sample || _frame_counter == 1)
29  {
30  _time_samples.push_back(now_time);
31  if (_time_samples.size() == 2)
32  {
33  auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
34  _time_samples[1] - _time_samples[0]).count();
35  _actual_fps = (((double)_frame_counter - 1.) / duration) * 1000.;
36  _frame_counter = 0;
37  _time_samples.clear();
38  }
39  }
40 
41  return _actual_fps;
42  }
43 
44  private:
45  double _actual_fps;
46  unsigned long long _number_of_frames_to_sample;
47  unsigned long long _frame_counter;
48  std::vector<std::chrono::time_point<std::chrono::system_clock>> _time_samples;
49  };
50 
52  {
53  private:
54  // This data will be left constant after creation, and accessed from all threads
56  rs_stream key_stream;
57  std::vector<rs_stream> other_streams;
58 
59  // This data will be read and written exclusively from the application thread
60  frameset frontbuffer;
61 
62  // This data will be read and written by all threads, and synchronized with a mutex
63  std::vector<frame> frames[RS_STREAM_NATIVE_COUNT];
64  std::condition_variable_any cv;
65 
66  void get_next_frames();
67  void dequeue_frame(rs_stream stream);
68  void discard_frame(rs_stream stream);
69  void cull_frames();
70 
71  timestamp_corrector ts_corrector;
72  public:
73  syncronizing_archive(const std::vector<subdevice_mode_selection> & selection,
74  rs_stream key_stream,
75  std::atomic<uint32_t>* max_size,
76  std::atomic<uint32_t>* event_queue_size,
77  std::atomic<uint32_t>* events_timeout,
78  std::chrono::high_resolution_clock::time_point capture_started = std::chrono::high_resolution_clock::now());
79 
80  // Application thread API
81  void wait_for_frames();
82  bool poll_for_frames();
83 
85  bool poll_for_frames_safe(frameset ** frames);
86 
89  const byte * get_frame_data(rs_stream stream) const;
90  double get_frame_timestamp(rs_stream stream) const;
91  unsigned long long get_frame_number(rs_stream stream) const;
92  long long get_frame_system_time(rs_stream stream) const;
94  int get_frame_bpp(rs_stream stream) const;
95 
97 
98  // Frame callback thread API
100 
101  void flush() override;
102 
104  void on_timestamp(rs_timestamp_data data);
105 
106  };
107 }
108 
109 #endif
rsimpl::fps_calc
Definition: sync.h:15
rsimpl::syncronizing_archive::get_frame_system_time
long long get_frame_system_time(rs_stream stream) const
rs_stream
rs_stream
Streams are different types of data provided by RealSense devices.
Definition: rs.h:33
rs_frame_metadata
rs_frame_metadata
Types of value provided from the device with each frame.
Definition: rs.h:203
rs::stream
stream
Streams are different types of data provided by RealSense devices.
Definition: rs.hpp:24
rsimpl::subdevice_mode_selection
Definition: types.h:293
rsimpl::fps_calc::calc_fps
double calc_fps(std::chrono::time_point< std::chrono::system_clock > &now_time)
Definition: sync.h:25
rsimpl::syncronizing_archive::get_frame_stride
int get_frame_stride(rs_stream stream) const
rsimpl::syncronizing_archive::on_timestamp
void on_timestamp(rs_timestamp_data data)
rsimpl::frame_archive::frameset
Definition: archive.h:185
rsimpl::syncronizing_archive
Definition: sync.h:51
rsimpl::syncronizing_archive::commit_frame
void commit_frame(rs_stream stream)
rsimpl::syncronizing_archive::syncronizing_archive
syncronizing_archive(const std::vector< subdevice_mode_selection > &selection, rs_stream key_stream, std::atomic< uint32_t > *max_size, std::atomic< uint32_t > *event_queue_size, std::atomic< uint32_t > *events_timeout, std::chrono::high_resolution_clock::time_point capture_started=std::chrono::high_resolution_clock::now())
rsimpl::syncronizing_archive::get_frame_metadata
double get_frame_metadata(rs_stream stream, rs_frame_metadata frame_metadata) const
rsimpl::syncronizing_archive::get_frame_timestamp
double get_frame_timestamp(rs_stream stream) const
rsimpl::syncronizing_archive::wait_for_frames
void wait_for_frames()
rsimpl::syncronizing_archive::clone_frontbuffer
frameset * clone_frontbuffer()
rsimpl
Definition: archive.h:12
RS_STREAM_NATIVE_COUNT
const uint8_t RS_STREAM_NATIVE_COUNT
Definition: types.h:27
rsimpl::syncronizing_archive::get_frame_data
const byte * get_frame_data(rs_stream stream) const
rsimpl::timestamp_corrector
Definition: timestamps.h:49
rsimpl::syncronizing_archive::poll_for_frames
bool poll_for_frames()
rsimpl::syncronizing_archive::wait_for_frames_safe
frameset * wait_for_frames_safe()
rsimpl::syncronizing_archive::correct_timestamp
void correct_timestamp(rs_stream stream)
rsimpl::syncronizing_archive::flush
void flush() override
rs::frame_metadata
frame_metadata
Types of value provided from the device with each frame.
Definition: rs.hpp:160
rsimpl::syncronizing_archive::get_frame_number
unsigned long long get_frame_number(rs_stream stream) const
archive.h
rsimpl::syncronizing_archive::supports_frame_metadata
bool supports_frame_metadata(rs_stream stream, rs_frame_metadata frame_metadata) const
rs_timestamp_data
Timestamp data from the motion microcontroller.
Definition: rs.h:339
rsimpl::frame_archive::capture_started
std::chrono::high_resolution_clock::time_point capture_started
Definition: archive.h:226
rsimpl::syncronizing_archive::get_frame_bpp
int get_frame_bpp(rs_stream stream) const
timestamps.h
rsimpl::fps_calc::fps_calc
fps_calc(unsigned long long in_number_of_frames_to_sampling, int expected_fps)
Definition: sync.h:18
rsimpl::syncronizing_archive::poll_for_frames_safe
bool poll_for_frames_safe(frameset **frames)
rsimpl::frame_archive
Definition: archive.h:15