Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
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 #include "archive.h"
8 
9 #include "core/streaming.h"
10 #include "core/roi.h"
11 #include "core/options.h"
12 #include "source.h"
13 
14 #include <chrono>
15 #include <memory>
16 #include <vector>
17 #include <unordered_set>
18 #include <limits.h>
19 #include <atomic>
20 #include <functional>
21 #include <core/debug.h>
22 
23 namespace librealsense
24 {
25  class device;
26  class option;
27 
28  typedef std::function<void(rs2_stream, frame_interface*, callback_invocation_holder)> on_before_frame_callback;
29  typedef std::function<void(std::vector<platform::stream_profile>)> on_open;
30 
31  class sensor_base : public std::enable_shared_from_this<sensor_base>,
32  public virtual sensor_interface, public options_container, public virtual info_container
33  {
34  public:
35  explicit sensor_base(std::string name,
36  device* device);
37 
39 
41  {
42  return *_profiles;
43  }
44 
45  virtual stream_profiles get_active_streams() const override;
48  int register_before_streaming_changes_callback(std::function<void(bool)> callback) override;
49  void unregister_before_start_callback(int token) override;
50  std::shared_ptr<notifications_processor> get_notifications_processor();
51  virtual frame_callback_ptr get_frames_callback() const override;
52  virtual void set_frames_callback(frame_callback_ptr callback) override;
53 
54  bool is_streaming() const override
55  {
56  return _is_streaming;
57  }
58 
59  virtual ~sensor_base() { _source.flush(); }
60 
61  void register_metadata(rs2_frame_metadata_value metadata, std::shared_ptr<md_attribute_parser_base> metadata_parser) const;
62 
63  void register_on_open(on_open callback)
64  {
65  _on_open = callback;
66  }
67 
68  void register_on_before_frame_callback(on_before_frame_callback callback)
69  {
70  _on_before_frame_callback = callback;
71  }
72 
73  const device_interface& get_device() override;
74 
77 
78  // Make sensor inherit its owning device info by default
79  const std::string& get_info(rs2_camera_info info) const override;
80  bool supports_info(rs2_camera_info info) const override;
81 
82  protected:
83  void raise_on_before_streaming_changes(bool streaming);
84  void set_active_streams(const stream_profiles& requests);
85  bool try_get_pf(const platform::stream_profile& p, native_pixel_format& result) const;
86 
87  void assign_stream(const std::shared_ptr<stream_interface>& stream,
88  std::shared_ptr<stream_profile_interface> target) const;
89 
90  std::vector<request_mapping> resolve_requests(stream_profiles requests);
91  std::shared_ptr<stream_profile_interface> map_requests(std::shared_ptr<stream_profile_interface> request);
92 
93  std::vector<platform::stream_profile> _internal_config;
94 
95  std::atomic<bool> _is_streaming;
96  std::atomic<bool> _is_opened;
97  std::shared_ptr<notifications_processor> _notifications_processor;
98  on_before_frame_callback _on_before_frame_callback;
100  std::shared_ptr<metadata_parser_map> _metadata_parsers = nullptr;
101 
103  device* _owner;
104  std::vector<platform::stream_profile> _uvc_profiles;
105 
106  private:
107  lazy<stream_profiles> _profiles;
108  stream_profiles _active_profiles;
109  std::vector<native_pixel_format> _pixel_formats;
110  signal<sensor_base, bool> on_before_streaming_changes;
111  };
112 
114  {
116 
117  virtual double get_frame_timestamp(const request_mapping& mode, const platform::frame_object& fo) = 0;
118  virtual unsigned long long get_frame_counter(const request_mapping& mode, const platform::frame_object& fo) const = 0;
119  virtual rs2_timestamp_domain get_frame_timestamp_domain(const request_mapping & mode, const platform::frame_object& fo) const = 0;
120  virtual void reset() = 0;
121  };
122 
123  class hid_sensor : public sensor_base
124  {
125  public:
126  explicit hid_sensor(std::shared_ptr<platform::hid_device> hid_device,
127  std::unique_ptr<frame_timestamp_reader> hid_iio_timestamp_reader,
128  std::unique_ptr<frame_timestamp_reader> custom_hid_timestamp_reader,
129  std::map<rs2_stream, std::map<unsigned, unsigned>> fps_and_sampling_frequency_per_rs2_stream,
130  std::vector<std::pair<std::string, stream_profile>> sensor_name_and_hid_profiles,
131  device* dev);
132 
133  ~hid_sensor();
134 
135  void open(const stream_profiles& requests) override;
136 
137  void close() override;
138 
139  void start(frame_callback_ptr callback) override;
140 
141  void stop() override;
142 
143  std::vector<uint8_t> get_custom_report_data(const std::string& custom_sensor_name,
144  const std::string& report_name,
145  platform::custom_sensor_report_field report_field) const;
146 
147  protected:
149 
150  private:
151  const std::map<rs2_stream, uint32_t> stream_and_fourcc = {{RS2_STREAM_GYRO, 'GYRO'},
152  {RS2_STREAM_ACCEL, 'ACCL'},
153  {RS2_STREAM_GPIO, 'GPIO'}};
154 
155  const std::vector<std::pair<std::string, stream_profile>> _sensor_name_and_hid_profiles;
156  std::map<rs2_stream, std::map<uint32_t, uint32_t>> _fps_and_sampling_frequency_per_rs2_stream;
157  std::shared_ptr<platform::hid_device> _hid_device;
158  std::mutex _configure_lock;
159  std::map<std::string, stream_profile> _configured_profiles;
160  std::vector<bool> _is_configured_stream;
161  std::vector<platform::hid_sensor> _hid_sensors;
162  std::map<std::string, request_mapping> _hid_mapping;
163  std::unique_ptr<frame_timestamp_reader> _hid_iio_timestamp_reader;
164  std::unique_ptr<frame_timestamp_reader> _custom_hid_timestamp_reader;
165 
166  stream_profiles get_sensor_profiles(std::string sensor_name) const;
167 
168  const std::string& rs2_stream_to_sensor_name(rs2_stream stream) const;
169 
170  uint32_t stream_to_fourcc(rs2_stream stream) const;
171 
172  uint32_t fps_to_sampling_frequency(rs2_stream stream, uint32_t fps) const;
173  };
174 
175  class uvc_sensor : public sensor_base,
176  public roi_sensor_interface
177  {
178  public:
179  explicit uvc_sensor(std::string name, std::shared_ptr<platform::uvc_device> uvc_device,
180  std::unique_ptr<frame_timestamp_reader> timestamp_reader, device* dev);
181 
182  ~uvc_sensor();
183 
184  region_of_interest_method& get_roi_method() const override;
185  void set_roi_method(std::shared_ptr<region_of_interest_method> roi_method) override;
186 
187  void open(const stream_profiles& requests) override;
188 
189  void close() override;
190 
191  std::vector<platform::stream_profile> get_configuration() const { return _internal_config; }
192 
193  void register_xu(platform::extension_unit xu);
194 
195  template<class T>
196  auto invoke_powered(T action)
197  -> decltype(action(*static_cast<platform::uvc_device*>(nullptr)))
198  {
199  power on(std::dynamic_pointer_cast<uvc_sensor>(shared_from_this()));
200  return action(*_device);
201  }
202 
203  void register_pu(rs2_option id);
204  void try_register_pu(rs2_option id);
205 
206  void start(frame_callback_ptr callback) override;
207 
208  void stop() override;
209 
210  platform::usb_spec get_usb_specification() const { return _device->get_usb_specification(); }
211  std::string get_device_path() const { return _device->get_device_location(); }
212 
213  protected:
215 
216  rs2_extension stream_to_frame_types(rs2_stream stream) const;
217 
218  private:
219  void acquire_power();
220 
221  void release_power();
222 
223  void reset_streaming();
224 
225  struct power
226  {
227  explicit power(std::weak_ptr<uvc_sensor> owner)
228  : _owner(owner)
229  {
230  auto strong = _owner.lock();
231  if (strong)
232  {
233  strong->acquire_power();
234  }
235  }
236 
237  ~power()
238  {
239  if (auto strong = _owner.lock())
240  {
241  try
242  {
243  strong->release_power();
244  }
245  catch (...) {}
246  }
247  }
248  private:
249  std::weak_ptr<uvc_sensor> _owner;
250  };
251 
252  std::shared_ptr<platform::uvc_device> _device;
253  std::atomic<int> _user_count;
254  std::mutex _power_lock;
255  std::mutex _configure_lock;
256  std::vector<platform::extension_unit> _xus;
257  std::unique_ptr<power> _power;
258  std::unique_ptr<frame_timestamp_reader> _timestamp_reader;
259  std::shared_ptr<region_of_interest_method> _roi_method = nullptr;
260  };
261 }
std::shared_ptr< notifications_processor > get_notifications_processor()
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
Definition: options.h:48
bool is_streaming() const override
Definition: sensor.h:54
auto invoke_powered(T action) -> decltype(action(*static_cast< platform::uvc_device *>(nullptr)))
Definition: sensor.h:196
frame_source _source
Definition: sensor.h:102
notifications_callback_ptr get_notifications_callback() const override
void register_on_before_frame_callback(on_before_frame_callback callback)
Definition: sensor.h:68
void register_metadata(rs2_frame_metadata_value metadata, std::shared_ptr< md_attribute_parser_base > metadata_parser) const
std::atomic< bool > _is_opened
Definition: sensor.h:96
const device_interface & get_device() override
Definition: backend.h:376
std::vector< request_mapping > resolve_requests(stream_profiles requests)
rs2_option
Defines general configuration controls. These can generally be mapped to camera UVC controls...
Definition: rs_option.h:22
std::function< void(std::vector< platform::stream_profile >)> on_open
Definition: sensor.h:29
Definition: info.h:22
std::function< void(rs2_stream, frame_interface *, callback_invocation_holder)> on_before_frame_callback
Definition: sensor.h:26
std::shared_ptr< rs2_frame_callback > frame_callback_ptr
Definition: types.h:896
Definition: options.h:20
Definition: types.h:589
virtual void start(frame_callback_ptr callback)=0
std::vector< platform::stream_profile > get_configuration() const
Definition: sensor.h:191
bool supports_info(rs2_camera_info info) const override
sensor_base(std::string name, device *device)
Definition: rs_sensor.h:47
Definition: rs_sensor.h:45
Definition: streaming.h:131
Definition: types.h:1414
usb_spec
Definition: backend.h:180
on_open _on_open
Definition: sensor.h:99
void set_active_streams(const stream_profiles &requests)
Definition: algo.h:16
virtual stream_profiles init_stream_profiles()=0
device * _owner
Definition: sensor.h:103
void raise_on_before_streaming_changes(bool streaming)
Definition: backend.h:167
std::shared_ptr< rs2_notifications_callback > notifications_callback_ptr
Definition: types.h:898
platform::usb_spec get_usb_specification() const
Definition: sensor.h:210
virtual stream_profiles get_active_streams() const override
std::shared_ptr< notifications_processor > _notifications_processor
Definition: sensor.h:97
std::vector< platform::stream_profile > _uvc_profiles
Definition: sensor.h:104
virtual void open(const stream_profiles &requests)=0
on_before_frame_callback _on_before_frame_callback
Definition: sensor.h:98
std::vector< platform::stream_profile > _internal_config
Definition: sensor.h:93
virtual frame_callback_ptr get_frames_callback() const override
virtual ~frame_timestamp_reader()
Definition: sensor.h:115
std::vector< std::shared_ptr< stream_profile_interface >> stream_profiles
Definition: streaming.h:104
rs2_stream
Streams are different types of data provided by RealSense devices.
Definition: rs_sensor.h:38
custom_sensor_report_field
Definition: backend.h:373
Definition: rs_sensor.h:46
void unregister_before_start_callback(int token) override
rs2_extension
Specifies advanced interfaces (capabilities) objects may implement.
Definition: rs_types.h:94
Definition: stream.h:14
void remove_pixel_format(native_pixel_format pf)
std::shared_ptr< metadata_parser_map > _metadata_parsers
Definition: sensor.h:100
void register_on_open(on_open callback)
Definition: sensor.h:63
const std::string & get_info(rs2_camera_info info) const override
std::string get_device_path() const
Definition: sensor.h:211
Definition: streaming.h:106
void register_pixel_format(native_pixel_format pf)
int register_before_streaming_changes_callback(std::function< void(bool)> callback) override
void register_notifications_callback(notifications_callback_ptr callback) override
virtual ~sensor_base()
Definition: sensor.h:59
std::shared_ptr< stream_profile_interface > map_requests(std::shared_ptr< stream_profile_interface > request)
std::atomic< bool > _is_streaming
Definition: sensor.h:95
Definition: sensor.h:123
void assign_stream(const std::shared_ptr< stream_interface > &stream, std::shared_ptr< stream_profile_interface > target) const
Definition: sensor.h:113
bool try_get_pf(const platform::stream_profile &p, native_pixel_format &result) const
Definition: sensor.h:31
Definition: types.h:606
Definition: device.h:43
rs2_frame_metadata_value
Per-Frame-Metadata are set of read-only properties that might be exposed for each individual frame...
Definition: rs_frame.h:28
Definition: sensor.h:175
virtual void set_frames_callback(frame_callback_ptr callback) override
stream_profiles get_stream_profiles() const override
Definition: sensor.h:40
Definition: source.h:15
rs2_timestamp_domain
Specifies the clock in relation to which the frame timestamp was measured.
Definition: rs_frame.h:19