archive.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_ARCHIVE_H
6 #define LIBREALSENSE_ARCHIVE_H
7 
8 #include "types.h"
9 #include <atomic>
10 #include "timestamps.h"
11 
12 namespace rsimpl
13 {
14  // Defines general frames storage model
16  {
17  public:
19  {
20  double actual_fps = 0;
21  double timestamp = 0;
22  double exposure_value = 0;
23  unsigned long long frame_number = 0;
24  long long system_time = 0;
25  int width = 0;
26  int height = 0;
27  int fps = 0;
28  int stride_x = 0;
29  int stride_y = 0;
30  int bpp = 1;
34  int pad = 0;
35  std::shared_ptr<std::vector<rs_frame_metadata>> supported_metadata_vector;
36  std::chrono::high_resolution_clock::time_point frame_callback_started {};
37 
39 
40  frame_additional_data(double in_timestamp, unsigned long long in_frame_number, long long in_system_time,
41  int in_width, int in_height, int in_fps,
42  int in_stride_x, int in_stride_y, int in_bpp,
43  const rs_format in_format, rs_stream in_stream_type, int in_pad, std::shared_ptr<std::vector<rs_frame_metadata>> in_supported_metadata_vector, double in_exposure_value, double in_actual_fps)
44  : timestamp(in_timestamp),
45  frame_number(in_frame_number),
46  system_time(in_system_time),
47  width(in_width),
48  height(in_height),
49  fps(in_fps),
50  stride_x(in_stride_x),
51  stride_y(in_stride_y),
52  bpp(in_bpp),
53  format(in_format),
54  stream_type(in_stream_type),
55  pad(in_pad),
56  supported_metadata_vector(in_supported_metadata_vector),
57  exposure_value(in_exposure_value),
58  actual_fps(in_actual_fps){}
59  };
60 
61  // Define a movable but explicitly noncopyable buffer type to hold our frame data
63  {
64  private:
65  // TODO: check boost::intrusive_ptr or an alternative
66  std::atomic<int> ref_count; // the reference count is on how many times this placeholder has been observed (not lifetime, not content)
67  frame_archive * owner; // pointer to the owner to be returned to by last observe
69 
70  public:
71  std::vector<byte> data;
73 
74  explicit frame() : ref_count(0), owner(nullptr), on_release(){}
75  frame(const frame & r) = delete;
76  frame(frame && r)
77  : ref_count(r.ref_count.exchange(0)),
78  owner(r.owner), on_release()
79  {
80  *this = std::move(r); // TODO: This is not very safe, refactor later
81  }
82 
83  frame & operator=(const frame & r) = delete;
85  {
86  data = move(r.data);
87  owner = r.owner;
88  ref_count = r.ref_count.exchange(0);
89  on_release = std::move(r.on_release);
90  additional_data = std::move(r.additional_data);
91  return *this;
92  }
93 
94  ~frame() { on_release.reset(); }
95 
96  double get_frame_metadata(rs_frame_metadata frame_metadata) const override;
97  bool supports_frame_metadata(rs_frame_metadata frame_metadata) const override;
98  const byte* get_frame_data() const;
99  double get_frame_timestamp() const;
100  rs_timestamp_domain get_frame_timestamp_domain() const;
101  void set_timestamp(double new_ts) override { additional_data.timestamp = new_ts; }
102  unsigned long long get_frame_number() const override;
104  long long get_frame_system_time() const;
105  int get_width() const;
106  int get_height() const;
107  int get_framerate() const;
108  int get_stride() const;
109  int get_bpp() const;
110  rs_format get_format() const;
111  rs_stream get_stream_type() const override;
112 
113  std::chrono::high_resolution_clock::time_point get_frame_callback_start_time_point() const;
114  void update_frame_callback_start_ts(std::chrono::high_resolution_clock::time_point ts);
115 
116  void acquire() { ref_count.fetch_add(1); }
117  void release();
118  frame* publish();
119  void update_owner(frame_archive * new_owner) { owner = new_owner; }
120  void attach_continuation(frame_continuation&& continuation) { on_release = std::move(continuation); }
121  void disable_continuation() { on_release.reset(); }
122  };
123 
124  class frame_ref : public rs_frame_ref // esentially an intrusive shared_ptr<frame>
125  {
127  public:
128  frame_ref() : frame_ptr(nullptr) {}
129 
130  explicit frame_ref(frame* frame) : frame_ptr(frame)
131  {
132  if (frame) frame->acquire();
133  }
134 
135  frame_ref(const frame_ref& other) : frame_ptr(other.frame_ptr)
136  {
137  if (frame_ptr) frame_ptr->acquire();
138  }
139 
140  frame_ref(frame_ref&& other) : frame_ptr(other.frame_ptr)
141  {
142  other.frame_ptr = nullptr;
143  }
144 
145  frame_ref& operator =(frame_ref other)
146  {
147  swap(other);
148  return *this;
149  }
150 
152  {
153  if (frame_ptr) frame_ptr->release();
154  }
155 
156  void swap(frame_ref& other)
157  {
158  std::swap(frame_ptr, other.frame_ptr);
159  }
160 
162  {
163  if (frame_ptr) frame_ptr->disable_continuation();
164  }
165 
166  double get_frame_metadata(rs_frame_metadata frame_metadata) const override;
167  bool supports_frame_metadata(rs_frame_metadata frame_metadata) const override;
168  const byte* get_frame_data() const override;
169  double get_frame_timestamp() const override;
170  unsigned long long get_frame_number() const override;
171  long long get_frame_system_time() const override;
172  rs_timestamp_domain get_frame_timestamp_domain() const override;
173  int get_frame_width() const override;
174  int get_frame_height() const override;
175  int get_frame_framerate() const override;
176  int get_frame_stride() const override;
177  int get_frame_bpp() const override;
178  rs_format get_frame_format() const override;
179  rs_stream get_stream_type() const override;
180  std::chrono::high_resolution_clock::time_point get_frame_callback_start_time_point() const;
181  void update_frame_callback_start_ts(std::chrono::high_resolution_clock::time_point ts);
182  void log_callback_start(std::chrono::high_resolution_clock::time_point capture_start_time);
183  };
184 
185  class frameset
186  {
188  public:
189 
190  frame_ref detach_ref(rs_stream stream);
191  void place_frame(rs_stream stream, frame&& new_frame);
192 
194  {
195  return &buffer[stream];
196  }
197 
200  const byte * get_frame_data(rs_stream stream) const { return buffer[stream].get_frame_data(); }
201  double get_frame_timestamp(rs_stream stream) const { return buffer[stream].get_frame_timestamp(); }
202  unsigned long long get_frame_number(rs_stream stream) const { return buffer[stream].get_frame_number(); }
203  long long get_frame_system_time(rs_stream stream) const { return buffer[stream].get_frame_system_time(); }
204  int get_frame_stride(rs_stream stream) const { return buffer[stream].get_frame_stride(); }
205  int get_frame_bpp(rs_stream stream) const { return buffer[stream].get_frame_bpp(); }
206 
207  void cleanup();
208 
209  };
210 
211  private:
212  // This data will be left constant after creation, and accessed from all threads
214 
215  std::atomic<uint32_t>* max_frame_queue_size;
220 
221 
222  protected:
223  frame backbuffer[RS_STREAM_NATIVE_COUNT]; // receive frame here
224  std::vector<frame> freelist; // return frames here
225  std::recursive_mutex mutex;
226  std::chrono::high_resolution_clock::time_point capture_started;
227 
228  public:
229  frame_archive(const std::vector<subdevice_mode_selection> & selection, std::atomic<uint32_t>* max_frame_queue_size, std::chrono::high_resolution_clock::time_point capture_started = std::chrono::high_resolution_clock::now());
230 
231  // Safe to call from any thread
232  bool is_stream_enabled(rs_stream stream) const { return modes[stream].mode.pf.fourcc != 0; }
233  const subdevice_mode_selection & get_mode(rs_stream stream) const { return modes[stream]; }
234 
236  {
237  published_sets.deallocate(frameset);
238  }
240 
241  void unpublish_frame(frame * frame);
242  frame * publish_frame(frame && frame);
243 
244  frame_ref * detach_frame_ref(frameset * frameset, rs_stream stream);
245  frame_ref * clone_frame(frame_ref * frameset);
247  {
248  detached_refs.deallocate(ref);
249  }
250 
251  // Frame callback thread API
252  byte * alloc_frame(rs_stream stream, const frame_additional_data& additional_data, bool requires_memory);
253  frame_ref * track_frame(rs_stream stream);
254  void attach_continuation(rs_stream stream, frame_continuation&& continuation);
255  void log_frame_callback_end(frame* frame);
256  void log_callback_start(frame_ref* frame_ref, std::chrono::high_resolution_clock::time_point capture_start_time);
257 
258  virtual void flush();
259 
260  virtual ~frame_archive() {};
261  };
262 }
263 
264 #endif
subdevice_mode_selection modes[RS_STREAM_NATIVE_COUNT]
Definition: archive.h:213
void release_frameset(frameset *frameset)
Definition: archive.h:235
frame_ref * track_frame(rs_stream stream)
Definition: archive.cpp:130
frame_ref(frame_ref &&other)
Definition: archive.h:140
double get_frame_timestamp(rs_stream stream) const
Definition: archive.h:201
std::atomic< uint32_t > * max_frame_queue_size
Definition: archive.h:215
frame backbuffer[RS_STREAM_NATIVE_COUNT]
Definition: archive.h:223
unsigned long long get_frame_number(rs_stream stream) const
Definition: archive.h:202
std::atomic< int > ref_count
Definition: archive.h:66
void attach_continuation(frame_continuation &&continuation)
Definition: archive.h:120
bool supports_frame_metadata(rs_frame_metadata frame_metadata) const override
Definition: archive.cpp:201
frame_metadata
Types of value provided from the device with each frame.
Definition: rs.hpp:160
GLint GLint GLsizei GLsizei height
Definition: glext.h:112
small_heap< frame, RS_USER_QUEUE_SIZE *RS_STREAM_COUNT > published_frames
Definition: archive.h:217
virtual void flush()
Definition: archive.cpp:144
native_pixel_format pf
Definition: types.h:158
byte * alloc_frame(rs_stream stream, const frame_additional_data &additional_data, bool requires_memory)
Definition: archive.cpp:88
frame_archive(const std::vector< subdevice_mode_selection > &selection, std::atomic< uint32_t > *max_frame_queue_size, std::chrono::high_resolution_clock::time_point capture_started=std::chrono::high_resolution_clock::now())
Definition: archive.cpp:7
void swap(frame_ref &other)
Definition: archive.h:156
const rs_frame_ref * get_frame(rs_stream stream) const
Definition: archive.h:193
double get_frame_metadata(rs_frame_metadata frame_metadata) const override
Definition: archive.cpp:196
frame_continuation on_release
Definition: archive.h:68
int get_frame_stride(rs_stream stream) const
Definition: archive.h:204
void set_timestamp(double new_ts) override
Definition: archive.h:101
frame_ref * detach_frame_ref(frameset *frameset, rs_stream stream)
Definition: archive.cpp:67
Definition: archive.h:12
void release_frame_ref(frame_ref *ref)
Definition: archive.h:246
frame_ref * clone_frame(frame_ref *frameset)
Definition: archive.cpp:77
frame * publish_frame(frame &&frame)
Definition: archive.cpp:51
GLuint GLuint stream
Definition: glext.h:1774
int get_frame_bpp() const override
Definition: archive.cpp:251
rs_timestamp_domain
Specifies the clock in relation to which the frame timestamp was measured.
Definition: rs.h:292
GLuint buffer
Definition: glext.h:528
std::chrono::high_resolution_clock::time_point frame_callback_started
Definition: archive.h:36
std::shared_ptr< std::vector< rs_frame_metadata > > supported_metadata_vector
Definition: archive.h:35
GLenum GLint ref
Definition: glext.h:652
const uint8_t RS_STREAM_NATIVE_COUNT
Definition: types.h:27
virtual ~frame_archive()
Definition: archive.h:260
bool supports_frame_metadata(rs_stream stream, rs_frame_metadata frame_metadata) const
Definition: archive.h:199
unsigned long long get_frame_number() const override
Definition: archive.cpp:216
void update_owner(frame_archive *new_owner)
Definition: archive.h:119
int get_frame_bpp(rs_stream stream) const
Definition: archive.h:205
frame_additional_data additional_data
Definition: archive.h:72
void log_callback_start(frame_ref *frame_ref, std::chrono::high_resolution_clock::time_point capture_start_time)
void deallocate(T *item)
Definition: types.h:532
double get_frame_timestamp() const override
Definition: archive.cpp:211
std::vector< byte > data
Definition: archive.h:71
long long get_frame_system_time() const override
Definition: archive.cpp:221
rs_format
Formats: defines how each stream can be encoded.
Definition: rs.h:53
small_heap< frame_ref, RS_USER_QUEUE_SIZE *RS_STREAM_COUNT > detached_refs
Definition: archive.h:219
std::recursive_mutex mutex
Definition: archive.h:225
frame & operator=(frame &&r)
Definition: archive.h:84
void set_timestamp_domain(rs_timestamp_domain timestamp_domain) override
Definition: archive.h:103
const byte * get_frame_data(rs_stream stream) const
Definition: archive.h:200
frameset * clone_frameset(frameset *frameset)
Definition: archive.cpp:25
std::chrono::high_resolution_clock::time_point capture_started
Definition: archive.h:226
void attach_continuation(rs_stream stream, frame_continuation &&continuation)
Definition: archive.cpp:125
const byte * get_frame_data() const override
Definition: archive.cpp:206
uint8_t byte
Definition: types.h:42
rs_stream
Streams are different types of data provided by RealSense devices.
Definition: rs.h:33
GLint GLint GLsizei width
Definition: glext.h:112
long long get_frame_system_time(rs_stream stream) const
Definition: archive.h:203
frame_ref(const frame_ref &other)
Definition: archive.h:135
void log_frame_callback_end(frame *frame)
Definition: archive.cpp:383
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glext.h:112
small_heap< frameset, RS_USER_QUEUE_SIZE *RS_STREAM_COUNT > published_sets
Definition: archive.h:218
void unpublish_frame(frame *frame)
Definition: archive.cpp:35
rs_frame_metadata
Types of value provided from the device with each frame.
Definition: rs.h:203
std::vector< frame > freelist
Definition: archive.h:224
const subdevice_mode_selection & get_mode(rs_stream stream) const
Definition: archive.h:233
frame_additional_data(double in_timestamp, unsigned long long in_frame_number, long long in_system_time, int in_width, int in_height, int in_fps, int in_stride_x, int in_stride_y, int in_bpp, const rs_format in_format, rs_stream in_stream_type, int in_pad, std::shared_ptr< std::vector< rs_frame_metadata >> in_supported_metadata_vector, double in_exposure_value, double in_actual_fps)
Definition: archive.h:40
bool is_stream_enabled(rs_stream stream) const
Definition: archive.h:232
frame_archive * owner
Definition: archive.h:67
std::atomic< uint32_t > published_frames_per_stream[RS_STREAM_COUNT]
Definition: archive.h:216
GLdouble GLdouble GLdouble r
Definition: glext.h:247
double get_frame_metadata(rs_stream stream, rs_frame_metadata frame_metadata) const
Definition: archive.h:198
int get_frame_stride() const override
Definition: archive.cpp:246


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Fri Mar 13 2020 03:16:11