sensor.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 
6 #include "backend.h"
7 
8 #include <chrono>
9 #include <memory>
10 #include <vector>
11 #include <unordered_set>
12 #include <unordered_map>
13 #include <limits.h>
14 #include <atomic>
15 #include <functional>
16 #include "core/debug.h"
17 
18 #include "archive.h"
19 #include "core/streaming.h"
20 #include "core/roi.h"
21 #include "core/options.h"
22 #include "source.h"
23 #include "core/extension.h"
26 
27 namespace librealsense
28 {
29  class device;
30  class option;
31 
32  typedef std::function<void(std::vector<platform::stream_profile>)> on_open;
33 
35  {
37 
38  virtual double get_frame_timestamp(const std::shared_ptr<frame_interface>& frame) = 0;
39  virtual unsigned long long get_frame_counter(const std::shared_ptr<frame_interface>& frame) const = 0;
40  virtual rs2_timestamp_domain get_frame_timestamp_domain(const std::shared_ptr<frame_interface>& frame) const = 0;
41  virtual void reset() = 0;
42  };
43 
44  class sensor_base : public std::enable_shared_from_this<sensor_base>,
45  public virtual sensor_interface, public options_container, public virtual info_container, public recommended_proccesing_blocks_base
46  {
47  public:
48  explicit sensor_base(std::string name,
49  device* device,
51  virtual ~sensor_base() override { _source.flush(); }
52 
53  void set_source_owner(sensor_base* owner); // will direct the source to the top in the source hierarchy.
55  stream_profiles get_stream_profiles(int tag = profile_tag::PROFILE_TAG_ANY) const override;
56  stream_profiles get_active_streams() const override;
57  notifications_callback_ptr get_notifications_callback() const override;
58  void register_notifications_callback(notifications_callback_ptr callback) override;
59  int register_before_streaming_changes_callback(std::function<void(bool)> callback) override;
60  void unregister_before_start_callback(int token) override;
61  virtual std::shared_ptr<notifications_processor> get_notifications_processor() const;
62  virtual frame_callback_ptr get_frames_callback() const override;
63  virtual void set_frames_callback(frame_callback_ptr callback) override;
64  bool is_streaming() const override;
65  virtual bool is_opened() const;
66  virtual void register_metadata(rs2_frame_metadata_value metadata, std::shared_ptr<md_attribute_parser_base> metadata_parser) const;
67  void register_on_open(on_open callback)
68  {
69  _on_open = callback;
70  }
71  device_interface& get_device() override;
72 
73  // Make sensor inherit its owning device info by default
74  const std::string& get_info(rs2_camera_info info) const override;
75  bool supports_info(rs2_camera_info info) const override;
76 
78  {
79  return {};
80  }
81 
82  std::shared_ptr<std::map<uint32_t, rs2_format>>& get_fourcc_to_rs2_format_map();
83  std::shared_ptr<std::map<uint32_t, rs2_stream>>& get_fourcc_to_rs2_stream_map();
84 
85  rs2_format fourcc_to_rs2_format(uint32_t format) const;
86  rs2_stream fourcc_to_rs2_stream(uint32_t fourcc_format) const;
87 
88  protected:
89  void raise_on_before_streaming_changes(bool streaming);
90  void set_active_streams(const stream_profiles& requests);
91 
92  void assign_stream(const std::shared_ptr<stream_interface>& stream,
93  std::shared_ptr<stream_profile_interface> target) const;
94 
95  std::shared_ptr<frame> generate_frame_from_data(const platform::frame_object& fo,
96  frame_timestamp_reader* timestamp_reader,
97  const rs2_time_t& last_timestamp,
98  const unsigned long long& last_frame_number,
99  std::shared_ptr<stream_profile_interface> profile);
100 
101  std::vector<platform::stream_profile> _internal_config;
102 
103  std::atomic<bool> _is_streaming;
104  std::atomic<bool> _is_opened;
105  std::shared_ptr<notifications_processor> _notifications_processor;
107  std::shared_ptr<metadata_parser_map> _metadata_parsers = nullptr;
108 
109  sensor_base* _source_owner = nullptr;
111  device* _owner;
112  std::vector<platform::stream_profile> _uvc_profiles;
113 
114  std::shared_ptr<std::map<uint32_t, rs2_format>> _fourcc_to_rs2_format;
115  std::shared_ptr<std::map<uint32_t, rs2_stream>> _fourcc_to_rs2_stream;
116 
117  private:
120  mutable std::mutex _active_profile_mutex;
122  };
123 
124  class processing_block;
125 
126  class motion_sensor : public recordable<motion_sensor>
127  {
128  public:
129  virtual ~motion_sensor() = default;
130 
131  void create_snapshot(std::shared_ptr<motion_sensor>& snapshot) const override;
132  void enable_recording(std::function<void(const motion_sensor&)> recording_function) override {};
133  };
134 
136 
138  {
139  public:
141 
142  void update(std::shared_ptr<extension_snapshot> ext) override
143  {
144  }
145 
146  void create_snapshot(std::shared_ptr<motion_sensor>& snapshot) const override
147  {
148  snapshot = std::make_shared<motion_sensor_snapshot>(*this);
149  }
150 
151  void enable_recording(std::function<void(const motion_sensor&)> recording_function) override
152  {
153  //empty
154  }
155  };
156 
157 
158  class fisheye_sensor : public recordable<fisheye_sensor>
159  {
160  public:
161  virtual ~fisheye_sensor() = default;
162 
163  void create_snapshot(std::shared_ptr<fisheye_sensor>& snapshot) const override;
164  void enable_recording(std::function<void(const fisheye_sensor&)> recording_function) override {};
165  };
166 
168 
170  {
171  public:
173 
174  void update(std::shared_ptr<extension_snapshot> ext) override
175  {
176  }
177 
178  void create_snapshot(std::shared_ptr<fisheye_sensor>& snapshot) const override
179  {
180  snapshot = std::make_shared<fisheye_sensor_snapshot>(*this);
181  }
182  void enable_recording(std::function<void(const fisheye_sensor&)> recording_function) override
183  {
184  //empty
185  }
186  };
187 
189  public sensor_base
190  {
191  public:
193  std::shared_ptr<sensor_base> sensor,
194  device* device,
195  const std::map<uint32_t, rs2_format>& fourcc_to_rs2_format_map = std::map<uint32_t, rs2_format>(),
196  const std::map<uint32_t, rs2_stream>& fourcc_to_rs2_stream_map = std::map<uint32_t, rs2_stream>());
197  ~synthetic_sensor() override;
198 
199  virtual void register_option(rs2_option id, std::shared_ptr<option> option);
200  virtual bool try_register_option(rs2_option id, std::shared_ptr<option> option);
201  void unregister_option(rs2_option id);
202  void register_pu(rs2_option id);
203  bool try_register_pu(rs2_option id);
204 
205  virtual stream_profiles init_stream_profiles() override;
206 
207  void open(const stream_profiles& requests) override;
208  void close() override;
209  void start(frame_callback_ptr callback) override;
210  void stop() override;
211 
212  void register_processing_block(const std::vector<stream_profile>& from,
213  const std::vector<stream_profile>& to,
214  std::function<std::shared_ptr<processing_block>(void)> generate_func);
215  void register_processing_block(const processing_block_factory& pbf);
216  void register_processing_block(const std::vector<processing_block_factory>& pbfs);
217 
218  std::shared_ptr<sensor_base> get_raw_sensor() const { return _raw_sensor; };
219  frame_callback_ptr get_frames_callback() const override;
220  void set_frames_callback(frame_callback_ptr callback) override;
221  void register_notifications_callback(notifications_callback_ptr callback) override;
222  int register_before_streaming_changes_callback(std::function<void(bool)> callback) override;
223  void unregister_before_start_callback(int token) override;
224  void register_metadata(rs2_frame_metadata_value metadata, std::shared_ptr<md_attribute_parser_base> metadata_parser) const override;
225  bool is_streaming() const override;
226  bool is_opened() const override;
227 
228  protected:
229  void add_source_profiles_missing_data();
230 
231  private:
232  stream_profiles resolve_requests(const stream_profiles& requests);
233  std::shared_ptr<stream_profile_interface> filter_frame_by_requests(const frame_interface* f);
234  void sort_profiles(stream_profiles * profiles);
235  std::pair<std::shared_ptr<processing_block_factory>, stream_profiles> find_requests_best_pb_match(const stream_profiles& sp);
236  void add_source_profile_missing_data(std::shared_ptr<stream_profile_interface>& source_profile);
237  bool is_duplicated_profile(const std::shared_ptr<stream_profile_interface>& duplicate, const stream_profiles& profiles);
238  std::shared_ptr<stream_profile_interface> clone_profile(const std::shared_ptr<stream_profile_interface>& profile);
239  void register_processing_block_options(const processing_block& pb);
240  void unregister_processing_block_options(const processing_block& pb);
241 
243 
245  std::shared_ptr<sensor_base> _raw_sensor;
246  std::vector<std::shared_ptr<processing_block_factory>> _pb_factories;
247  std::unordered_map<processing_block_factory*, stream_profiles> _pbf_supported_profiles;
248  std::unordered_map<std::shared_ptr<stream_profile_interface>, std::unordered_set<std::shared_ptr<processing_block>>> _profiles_to_processing_block;
249  std::unordered_map<std::shared_ptr<stream_profile_interface>, stream_profiles> _source_to_target_profiles_map;
250  std::unordered_map<stream_profile, stream_profiles> _target_to_source_profiles_map;
251  std::unordered_map<rs2_format, stream_profiles> _cached_requests;
252  std::vector<rs2_option> _cached_processing_blocks_options;
253  };
254 
256  {
257  static const int sensors = 2;
258  bool started;
259  mutable std::vector<int64_t> counter;
260  mutable std::recursive_mutex _mtx;
261  public:
263 
264  void reset() override;
265 
266  rs2_time_t get_frame_timestamp(const std::shared_ptr<frame_interface>& frame) override;
267 
268  bool has_metadata(const std::shared_ptr<frame_interface>& frame) const;
269 
270  unsigned long long get_frame_counter(const std::shared_ptr<frame_interface>& frame) const override;
271 
272  rs2_timestamp_domain get_frame_timestamp_domain(const std::shared_ptr<frame_interface>& frame) const override;
273  };
274 
275  class hid_sensor : public sensor_base
276  {
277  public:
278  explicit hid_sensor(std::shared_ptr<platform::hid_device> hid_device,
279  std::unique_ptr<frame_timestamp_reader> hid_iio_timestamp_reader,
280  std::unique_ptr<frame_timestamp_reader> custom_hid_timestamp_reader,
281  const std::map<rs2_stream, std::map<unsigned, unsigned>>& fps_and_sampling_frequency_per_rs2_stream,
282  const std::vector<std::pair<std::string, stream_profile>>& sensor_name_and_hid_profiles,
283  device* dev);
284 
285  ~hid_sensor() override;
286 
287  void open(const stream_profiles& requests) override;
288  void close() override;
289  void start(frame_callback_ptr callback) override;
290  void stop() override;
291 
292  std::vector<uint8_t> get_custom_report_data(const std::string& custom_sensor_name,
293  const std::string& report_name,
294  platform::custom_sensor_report_field report_field) const;
295 
296  protected:
298 
299  private:
300  const std::map<rs2_stream, uint32_t> stream_and_fourcc = {{RS2_STREAM_GYRO, rs_fourcc('G','Y','R','O')},
301  {RS2_STREAM_ACCEL, rs_fourcc('A','C','C','L')},
302  {RS2_STREAM_GPIO, rs_fourcc('G','P','I','O')}};
303 
304  const std::vector<std::pair<std::string, stream_profile>> _sensor_name_and_hid_profiles;
305  std::map<rs2_stream, std::map<uint32_t, uint32_t>> _fps_and_sampling_frequency_per_rs2_stream;
306  std::shared_ptr<platform::hid_device> _hid_device;
307  std::mutex _configure_lock;
308  std::map<std::string, std::shared_ptr<stream_profile_interface>> _configured_profiles;
309  std::vector<bool> _is_configured_stream;
310  std::vector<platform::hid_sensor> _hid_sensors;
311  std::unique_ptr<frame_timestamp_reader> _hid_iio_timestamp_reader;
312  std::unique_ptr<frame_timestamp_reader> _custom_hid_timestamp_reader;
313 
314  stream_profiles get_sensor_profiles(std::string sensor_name) const;
315 
316  const std::string& rs2_stream_to_sensor_name(rs2_stream stream) const;
317 
318  uint32_t stream_to_fourcc(rs2_stream stream) const;
319 
320  uint32_t fps_to_sampling_frequency(rs2_stream stream, uint32_t fps) const;
321  };
322 
323  class uvc_sensor : public sensor_base
324  {
325  public:
326  explicit uvc_sensor(std::string name, std::shared_ptr<platform::uvc_device> uvc_device,
327  std::unique_ptr<frame_timestamp_reader> timestamp_reader, device* dev);
328  virtual ~uvc_sensor() override;
329 
330  void open(const stream_profiles& requests) override;
331  void close() override;
332  void start(frame_callback_ptr callback) override;
333  void stop() override;
334  void register_xu(platform::extension_unit xu);
335  void register_pu(rs2_option id);
336 
337  std::vector<platform::stream_profile> get_configuration() const { return _internal_config; }
338  std::shared_ptr<platform::uvc_device> get_uvc_device() { return _device; }
339  platform::usb_spec get_usb_specification() const { return _device->get_usb_specification(); }
340  std::string get_device_path() const { return _device->get_device_location(); }
341 
342  template<class T>
344  -> decltype(action(*static_cast<platform::uvc_device*>(nullptr)))
345  {
346  power on(std::dynamic_pointer_cast<uvc_sensor>(shared_from_this()));
347  return action(*_device);
348  }
349 
350  protected:
352  rs2_extension stream_to_frame_types(rs2_stream stream) const;
353 
354  private:
355  void acquire_power();
356  void release_power();
357  void reset_streaming();
358 
359  struct power
360  {
361  explicit power(std::weak_ptr<uvc_sensor> owner)
362  : _owner(owner)
363  {
364  auto strong = _owner.lock();
365  if (strong)
366  {
367  strong->acquire_power();
368  }
369  }
370 
372  {
373  if (auto strong = _owner.lock())
374  {
375  try
376  {
377  strong->release_power();
378  }
379  catch (...) {}
380  }
381  }
382  private:
383  std::weak_ptr<uvc_sensor> _owner;
384  };
385 
386  std::shared_ptr<platform::uvc_device> _device;
387  std::atomic<int> _user_count;
388  std::mutex _power_lock;
389  std::mutex _configure_lock;
390  std::vector<platform::extension_unit> _xus;
391  std::unique_ptr<power> _power;
392  std::unique_ptr<frame_timestamp_reader> _timestamp_reader;
393  };
394 
397 }
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
frame_source _source
Definition: sensor.h:110
void enable_recording(std::function< void(const fisheye_sensor &)> recording_function) override
Definition: sensor.h:164
boost_foreach_argument_dependent_lookup_hack tag
Definition: foreach_fwd.hpp:31
std::string get_device_path() const
Definition: sensor.h:340
std::atomic< bool > _is_opened
Definition: sensor.h:104
GLuint const GLchar * name
auto invoke_powered(T action) -> decltype(action(*static_cast< platform::uvc_device * >(nullptr)))
Definition: sensor.h:343
rs2_option
Defines general configuration controls. These can generally be mapped to camera UVC controls...
Definition: rs_option.h:22
void enable_recording(std::function< void(const fisheye_sensor &)> recording_function) override
Definition: sensor.h:182
std::function< void(std::vector< platform::stream_profile >)> on_open
Definition: sensor.h:30
std::shared_ptr< rs2_frame_callback > frame_callback_ptr
Definition: src/types.h:1071
void create_snapshot(std::shared_ptr< motion_sensor > &snapshot) const override
Definition: sensor.h:146
std::shared_ptr< sensor_base > get_raw_sensor() const
Definition: sensor.h:218
std::mutex _synthetic_configure_lock
Definition: sensor.h:242
processing_blocks get_color_recommended_proccesing_blocks()
Definition: sensor.cpp:216
std::unordered_map< processing_block_factory *, stream_profiles > _pbf_supported_profiles
Definition: sensor.h:247
void update(std::shared_ptr< extension_snapshot > ext) override
Definition: sensor.h:174
std::vector< platform::hid_sensor > _hid_sensors
Definition: sensor.h:310
void create_snapshot(std::shared_ptr< fisheye_sensor > &snapshot) const override
Definition: sensor.h:178
std::unordered_map< stream_profile, stream_profiles > _target_to_source_profiles_map
Definition: sensor.h:250
GLsizei const GLchar *const * string
std::mutex _power_lock
Definition: sensor.h:388
virtual ~sensor_base() override
Definition: sensor.h:51
std::shared_ptr< platform::hid_device > _hid_device
Definition: sensor.h:306
static const textual_icon stop
Definition: model-views.h:225
std::shared_ptr< platform::uvc_device > _device
Definition: sensor.h:386
frame_callback_ptr _post_process_callback
Definition: sensor.h:244
std::mutex _active_profile_mutex
Definition: sensor.h:120
std::vector< platform::extension_unit > _xus
Definition: sensor.h:390
std::vector< bool > _is_configured_stream
Definition: sensor.h:309
std::shared_ptr< sensor_base > _raw_sensor
Definition: sensor.h:245
std::shared_ptr< platform::uvc_device > get_uvc_device()
Definition: sensor.h:338
std::unique_ptr< frame_timestamp_reader > _hid_iio_timestamp_reader
Definition: sensor.h:311
std::atomic< int > _user_count
Definition: sensor.h:387
def info(name, value, persistent=False)
Definition: test.py:301
GLdouble f
std::unordered_map< rs2_format, stream_profiles > _cached_requests
Definition: sensor.h:251
std::unique_ptr< power > _power
Definition: sensor.h:391
std::mutex _configure_lock
Definition: sensor.h:307
processing_blocks get_depth_recommended_proccesing_blocks()
Definition: sensor.cpp:228
void update(std::shared_ptr< extension_snapshot > ext) override
Definition: sensor.h:142
std::shared_ptr< std::map< uint32_t, rs2_format > > _fourcc_to_rs2_format
Definition: sensor.h:114
usb_device_info get_info(const std::wstring device_wstr)
stream_profiles _active_profiles
Definition: sensor.h:119
std::unique_ptr< frame_timestamp_reader > _custom_hid_timestamp_reader
Definition: sensor.h:312
std::vector< rs2::stream_profile > init_stream_profiles(sw_context &sctx, std::shared_ptr< rs2::software_sensor > ss, std::string sensor_name, rs2::video_stream_profile c, rs2::video_stream_profile d)
unsigned int uint32_t
Definition: stdint.h:80
std::shared_ptr< rs2_notifications_callback > notifications_callback_ptr
Definition: src/types.h:1073
uint32_t rs_fourcc(const T a, const T b, const T c, const T d)
Definition: src/types.h:1846
std::shared_ptr< notifications_processor > _notifications_processor
Definition: sensor.h:105
std::vector< platform::stream_profile > _uvc_profiles
Definition: sensor.h:112
GLint GLint GLsizei GLint GLenum format
GLenum target
Definition: glext.h:1565
def callback(frame)
Definition: t265_stereo.py:91
GLuint start
const std::vector< std::pair< std::string, stream_profile > > _sensor_name_and_hid_profiles
Definition: sensor.h:304
rs2_format
A stream&#39;s format identifies how binary data is encoded within a frame.
Definition: rs_sensor.h:59
std::vector< platform::stream_profile > _internal_config
Definition: sensor.h:101
platform::usb_spec get_usb_specification() const
Definition: sensor.h:339
signal< sensor_base, bool > on_before_streaming_changes
Definition: sensor.h:121
action
Definition: enums.py:62
std::vector< std::shared_ptr< stream_profile_interface >> stream_profiles
Definition: streaming.h:165
rs2_stream
Streams are different types of data provided by RealSense devices.
Definition: rs_sensor.h:42
void enable_recording(std::function< void(const motion_sensor &)> recording_function) override
Definition: sensor.h:151
std::mutex _configure_lock
Definition: sensor.h:389
virtual unsigned long long get_frame_counter(const std::shared_ptr< frame_interface > &frame) const =0
std::vector< platform::stream_profile > get_configuration() const
Definition: sensor.h:337
std::vector< int64_t > counter
Definition: sensor.h:259
rs2_extension
Specifies advanced interfaces (capabilities) objects may implement.
Definition: rs_types.h:166
std::unique_ptr< frame_timestamp_reader > _timestamp_reader
Definition: sensor.h:392
::realsense_legacy_msgs::metadata_< std::allocator< void > > metadata
virtual rs2_timestamp_domain get_frame_timestamp_domain(const std::shared_ptr< frame_interface > &frame) const =0
lazy< stream_profiles > _profiles
Definition: sensor.h:118
void register_on_open(on_open callback)
Definition: sensor.h:67
std::vector< std::shared_ptr< processing_block_factory > > _pb_factories
Definition: sensor.h:246
std::unordered_map< std::shared_ptr< stream_profile_interface >, std::unordered_set< std::shared_ptr< processing_block > > > _profiles_to_processing_block
Definition: sensor.h:248
power(std::weak_ptr< uvc_sensor > owner)
Definition: sensor.h:361
std::unordered_map< std::shared_ptr< stream_profile_interface >, stream_profiles > _source_to_target_profiles_map
Definition: sensor.h:249
std::vector< rs2_option > _cached_processing_blocks_options
Definition: sensor.h:252
static rs2::device get_device()
virtual double get_frame_timestamp(const std::shared_ptr< frame_interface > &frame)=0
std::atomic< bool > _is_streaming
Definition: sensor.h:103
double rs2_time_t
Definition: rs_types.h:300
std::shared_ptr< std::map< uint32_t, rs2_stream > > _fourcc_to_rs2_stream
Definition: sensor.h:115
std::map< rs2_stream, std::map< uint32_t, uint32_t > > _fps_and_sampling_frequency_per_rs2_stream
Definition: sensor.h:305
std::map< std::string, std::shared_ptr< stream_profile_interface > > _configured_profiles
Definition: sensor.h:308
GeneratorWrapper< T > map(Func &&function, GeneratorWrapper< U > &&generator)
Definition: catch.hpp:4271
auto device
Definition: pyrs_net.cpp:17
void enable_recording(std::function< void(const motion_sensor &)> recording_function) override
Definition: sensor.h:132
processing_blocks get_recommended_processing_blocks() const override
Definition: sensor.h:77
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
MAP_EXTENSION(RS2_EXTENSION_POINTS, librealsense::points)
std::weak_ptr< uvc_sensor > _owner
Definition: sensor.h:383
rs2_timestamp_domain
Specifies the clock in relation to which the frame timestamp was measured.
Definition: rs_frame.h:19


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:41