Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
rs_internal.hpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2017 Intel Corporation. All Rights Reserved.
3 
4 #ifndef LIBREALSENSE_RS2_INTERNAL_HPP
5 #define LIBREALSENSE_RS2_INTERNAL_HPP
6 
7 #include "rs_types.hpp"
8 #include "rs_device.hpp"
9 #include "rs_context.hpp"
10 #include "../h/rs_internal.h"
11 
12 namespace rs2
13 {
14  class recording_context : public context
15  {
16  public:
21  recording_context(const std::string& filename,
22  const std::string& section = "",
24  {
25  rs2_error* e = nullptr;
26  _context = std::shared_ptr<rs2_context>(
27  rs2_create_recording_context(RS2_API_VERSION, filename.c_str(), section.c_str(), mode, &e),
29  error::handle(e);
30  }
31 
32  recording_context() = delete;
33  };
34 
35  class mock_context : public context
36  {
37  public:
43  mock_context(const std::string& filename,
44  const std::string& section = "",
45  const std::string& min_api_version = "0.0.0")
46  {
47  rs2_error* e = nullptr;
48  _context = std::shared_ptr<rs2_context>(
49  rs2_create_mock_context_versioned(RS2_API_VERSION, filename.c_str(), section.c_str(), min_api_version.c_str(), &e),
51  error::handle(e);
52  }
53 
54  mock_context() = delete;
55  };
56 
57  namespace internal
58  {
62  inline double get_time()
63  {
64  rs2_error* e = nullptr;
65  auto time = rs2_get_time( &e);
66 
67  error::handle(e);
68 
69  return time;
70  }
71  }
72 
73  template<class T>
75  {
76  T on_destruction_function;
77  public:
78  explicit software_device_destruction_callback(T on_destruction) : on_destruction_function(on_destruction) {}
79 
80  void on_destruction() override
81  {
82  on_destruction_function();
83  }
84 
85  void release() override { delete this; }
86  };
87 
88  class software_sensor : public sensor
89  {
90  public:
96  stream_profile add_video_stream(rs2_video_stream video_stream, bool is_default=false)
97  {
98  rs2_error* e = nullptr;
99 
100  auto profile = rs2_software_sensor_add_video_stream_ex(_sensor.get(), video_stream, is_default, &e);
101  error::handle(e);
102 
103  stream_profile stream(profile);
104  return stream;
105  }
106 
112  stream_profile add_motion_stream(rs2_motion_stream motion_stream, bool is_default=false)
113  {
114  rs2_error* e = nullptr;
115 
116  auto profile = rs2_software_sensor_add_motion_stream_ex(_sensor.get(), motion_stream, is_default, &e);
117  error::handle(e);
118 
119  stream_profile stream(profile);
120  return stream;
121  }
122 
128  stream_profile add_pose_stream(rs2_pose_stream pose_stream, bool is_default=false)
129  {
130  rs2_error* e = nullptr;
131 
132  auto profile = rs2_software_sensor_add_pose_stream_ex(_sensor.get(), pose_stream, is_default, &e);
133  error::handle(e);
134 
135  stream_profile stream(profile);
136  return stream;
137  }
138 
145  {
146  rs2_error* e = nullptr;
148  error::handle(e);
149  }
150 
157  {
158  rs2_error* e = nullptr;
160  error::handle(e);
161  }
162 
169  {
170  rs2_error* e = nullptr;
172  error::handle(e);
173  }
174 
181  {
182  rs2_error* e = nullptr;
183  rs2_software_sensor_set_metadata(_sensor.get(), value, type, &e);
184  error::handle(e);
185  }
186 
193  void add_read_only_option(rs2_option option, float val)
194  {
195  rs2_error* e = nullptr;
196  rs2_software_sensor_add_read_only_option(_sensor.get(), option, val, &e);
197  error::handle(e);
198  }
199 
206  void set_read_only_option(rs2_option option, float val)
207  {
208  rs2_error* e = nullptr;
210  error::handle(e);
211  }
218  void add_option(rs2_option option, const option_range& range, bool is_writable=true)
219  {
220  rs2_error* e = nullptr;
221  rs2_software_sensor_add_option(_sensor.get(), option, range.min,
222  range.max, range.step, range.def, is_writable, &e);
223  error::handle(e);
224  }
225 
227  {
228  rs2_error * e = nullptr;
230  error::handle(e);
231  }
237  void detach()
238  {
239  rs2_error * e = nullptr;
241  error::handle(e);
242  }
243 
244  private:
245  friend class software_device;
246 
247  software_sensor(std::shared_ptr<rs2_sensor> s)
248  : rs2::sensor(s)
249  {
250  rs2_error* e = nullptr;
252  {
253  _sensor = nullptr;
254  }
256  }
257  };
258 
259 
260  class software_device : public device
261  {
262  std::shared_ptr<rs2_device> create_device_ptr(std::function<void(rs2_device*)> deleter)
263  {
264  rs2_error* e = nullptr;
265  std::shared_ptr<rs2_device> dev(
267  deleter);
268  error::handle(e);
269  return dev;
270  }
271 
272  public:
273  software_device(std::function<void(rs2_device*)> deleter = &rs2_delete_device)
274  : device(create_device_ptr(deleter))
275  {
276  this->set_destruction_callback([]{});
277  }
278 
279  software_device(std::string name)
280  : device(create_device_ptr(&rs2_delete_device))
281  {
283  }
284 
290  software_sensor add_sensor(std::string name)
291  {
292  rs2_error* e = nullptr;
293  std::shared_ptr<rs2_sensor> sensor(
294  rs2_software_device_add_sensor(_dev.get(), name.c_str(), &e),
296  error::handle(e);
297 
298  return software_sensor(sensor);
299  }
300 
305  template<class T>
306  void set_destruction_callback(T callback) const
307  {
308  rs2_error* e = nullptr;
310  new software_device_destruction_callback<T>(std::move(callback)), &e);
311  error::handle(e);
312  }
313 
321  void add_to(context& ctx)
322  {
323  rs2_error* e = nullptr;
324  rs2_context_add_software_device(ctx._context.get(), _dev.get(), &e);
325  error::handle(e);
326  }
327 
334  void register_info(rs2_camera_info info, const std::string& val)
335  {
336  rs2_error* e = nullptr;
337  rs2_software_device_register_info(_dev.get(), info, val.c_str(), &e);
338  error::handle(e);
339  }
340 
347  void update_info(rs2_camera_info info, const std::string& val)
348  {
349  rs2_error* e = nullptr;
350  rs2_software_device_update_info(_dev.get(), info, val.c_str(), &e);
351  error::handle(e);
352  }
353 
359  {
360  rs2_error* e = nullptr;
361  rs2_software_device_create_matcher(_dev.get(), matcher, &e);
362  error::handle(e);
363  }
364  };
365 
366 }
367 #endif // LIBREALSENSE_RS2_INTERNAL_HPP
void rs2_software_sensor_set_metadata(rs2_sensor *sensor, rs2_frame_metadata_value value, rs2_metadata_type type, rs2_error **error)
void rs2_software_device_set_destruction_callback_cpp(const rs2_device *dev, rs2_software_device_destruction_callback *callback, rs2_error **error)
Definition: rs_frame.hpp:22
rs2_camera_info
Read-only strings that can be queried from the device. Not all information attributes are available o...
Definition: rs_sensor.h:22
recording_context(const std::string &filename, const std::string &section="", rs2_recording_mode mode=RS2_RECORDING_MODE_BLANK_FRAMES)
Definition: rs_internal.hpp:21
Definition: rs_sensor.hpp:103
stream_profile add_pose_stream(rs2_pose_stream pose_stream, bool is_default=false)
Definition: rs_internal.hpp:128
Definition: rs_frame.hpp:336
#define RS2_API_VERSION
Definition: rs.h:41
rs2_option
Defines general configuration controls. These can generally be mapped to camera UVC controls...
Definition: rs_option.h:22
void register_info(rs2_camera_info info, const std::string &val)
Definition: rs_internal.hpp:334
rs2_recording_mode
Definition: rs_internal.h:25
rs2_stream_profile * rs2_software_sensor_add_pose_stream_ex(rs2_sensor *sensor, rs2_pose_stream pose_stream, int is_default, rs2_error **error)
void rs2_software_sensor_add_option(rs2_sensor *sensor, rs2_option option, float min, float max, float step, float def, int is_writable, rs2_error **error)
void rs2_delete_device(rs2_device *device)
void rs2_delete_context(rs2_context *context)
Frees the relevant context object.
void on_video_frame(rs2_software_video_frame frame)
Definition: rs_internal.hpp:144
All the parameters required to define a video stream.
Definition: rs_internal.h:34
void set_metadata(rs2_frame_metadata_value value, rs2_metadata_type type)
Definition: rs_internal.hpp:180
All the parameters required to define a pose frame.
Definition: rs_internal.h:93
Definition: rs_internal.hpp:74
software_device_destruction_callback(T on_destruction)
Definition: rs_internal.hpp:78
float min
Definition: rs_types.hpp:171
Definition: rs_types.hpp:48
Definition: rs_context.hpp:11
Definition: rs_sensor.h:23
void on_motion_frame(rs2_software_motion_frame frame)
Definition: rs_internal.hpp:156
software_sensor add_sensor(std::string name)
Definition: rs_internal.hpp:290
software_device(std::function< void(rs2_device *)> deleter=&rs2_delete_device)
Definition: rs_internal.hpp:273
stream_profile add_motion_stream(rs2_motion_stream motion_stream, bool is_default=false)
Definition: rs_internal.hpp:112
Definition: rs_context.hpp:96
void set_read_only_option(rs2_option option, float val)
Definition: rs_internal.hpp:206
float max
Definition: rs_types.hpp:172
float step
Definition: rs_types.hpp:174
Definition: rs_internal.hpp:35
rs2_context * rs2_create_recording_context(int api_version, const char *filename, const char *section, rs2_recording_mode mode, rs2_error **error)
Definition: rs_types.h:160
void rs2_software_sensor_detach(rs2_sensor *sensor, rs2_error **error)
rs2_stream_profile * rs2_software_sensor_add_video_stream_ex(rs2_sensor *sensor, rs2_video_stream video_stream, int is_default, rs2_error **error)
rs2_time_t rs2_get_time(rs2_error **error)
std::shared_ptr< rs2_sensor > _sensor
Definition: rs_sensor.hpp:354
Definition: rs_internal.hpp:14
std::shared_ptr< rs2_context > _context
Definition: rs_context.hpp:218
All the parameters required to define a pose stream.
Definition: rs_internal.h:59
Definition: rs_internal.hpp:88
void rs2_delete_sensor(rs2_sensor *sensor)
Definition: rs_types.hpp:169
rs2_matchers
Specifies types of different matchers.
Definition: rs_types.h:189
void rs2_software_sensor_on_pose_frame(rs2_sensor *sensor, rs2_software_pose_frame frame, rs2_error **error)
std::shared_ptr< rs2_device > _dev
Definition: rs_device.hpp:146
rs2_device * rs2_create_software_device(rs2_error **error)
void rs2_software_sensor_add_read_only_option(rs2_sensor *sensor, rs2_option option, float val, rs2_error **error)
mock_context()=delete
void create_matcher(rs2_matchers matcher)
Definition: rs_internal.hpp:358
All the parameters required to define a motion stream.
Definition: rs_internal.h:48
rs2_stream_profile * rs2_software_sensor_add_motion_stream_ex(rs2_sensor *sensor, rs2_motion_stream motion_stream, int is_default, rs2_error **error)
void update_info(rs2_camera_info info, const std::string &val)
Definition: rs_internal.hpp:347
Definition: rs_internal.hpp:260
static void handle(rs2_error *e)
Definition: rs_types.hpp:137
void release() override
Definition: rs_internal.hpp:85
rs2_sensor * rs2_software_device_add_sensor(rs2_device *dev, const char *sensor_name, rs2_error **error)
void rs2_software_sensor_update_read_only_option(rs2_sensor *sensor, rs2_option option, float val, rs2_error **error)
Definition: rs_internal.h:27
void rs2_software_sensor_on_notification(rs2_sensor *sensor, rs2_software_notification notif, rs2_error **error)
void on_notification(rs2_software_notification notif)
Definition: rs_internal.hpp:226
void add_to(context &ctx)
Definition: rs_internal.hpp:321
void rs2_software_device_create_matcher(rs2_device *dev, rs2_matchers matcher, rs2_error **error)
int rs2_is_sensor_extendable_to(const rs2_sensor *sensor, rs2_extension extension, rs2_error **error)
All the parameters required to define a sensor notification.
Definition: rs_internal.h:115
long long rs2_metadata_type
Definition: rs_types.h:256
rs2_context * rs2_create_mock_context_versioned(int api_version, const char *filename, const char *section, const char *min_api_version, rs2_error **error)
void on_pose_frame(rs2_software_pose_frame frame)
Definition: rs_internal.hpp:168
float def
Definition: rs_types.hpp:173
void detach()
Definition: rs_internal.hpp:237
void rs2_context_add_software_device(rs2_context *ctx, rs2_device *dev, rs2_error **error)
All the parameters required to define a motion frame.
Definition: rs_internal.h:82
struct rs2_device rs2_device
Definition: rs_types.h:216
void rs2_software_sensor_on_motion_frame(rs2_sensor *sensor, rs2_software_motion_frame frame, rs2_error **error)
software_device(std::string name)
Definition: rs_internal.hpp:279
void add_option(rs2_option option, const option_range &range, bool is_writable=true)
Definition: rs_internal.hpp:218
void set_destruction_callback(T callback) const
Definition: rs_internal.hpp:306
void add_read_only_option(rs2_option option, float val)
Definition: rs_internal.hpp:193
void rs2_software_sensor_on_video_frame(rs2_sensor *sensor, rs2_software_video_frame frame, rs2_error **error)
All the parameters required to define a video frame.
Definition: rs_internal.h:69
struct rs2_error rs2_error
Definition: rs_types.h:217
void rs2_software_device_register_info(rs2_device *dev, rs2_camera_info info, const char *val, rs2_error **error)
Definition: rs_device.hpp:18
stream_profile add_video_stream(rs2_video_stream video_stream, bool is_default=false)
Definition: rs_internal.hpp:96
double get_time()
Definition: rs_internal.hpp:62
rs2_frame_metadata_value
Per-Frame-Metadata is the set of read-only properties that might be exposed for each individual frame...
Definition: rs_frame.h:29
mock_context(const std::string &filename, const std::string &section="", const std::string &min_api_version="0.0.0")
Definition: rs_internal.hpp:43
void rs2_software_device_update_info(rs2_device *dev, rs2_camera_info info, const char *val, rs2_error **error)
void on_destruction() override
Definition: rs_internal.hpp:80