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 
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 
92  std::vector<platform::stream_profile> _internal_config;
93 
94  std::atomic<bool> _is_streaming;
95  std::atomic<bool> _is_opened;
96  std::shared_ptr<notifications_processor> _notifications_processor;
99  std::shared_ptr<metadata_parser_map> _metadata_parsers = nullptr;
100 
103 
104  private:
105  lazy<stream_profiles> _profiles;
106  stream_profiles _active_profiles;
107  std::vector<native_pixel_format> _pixel_formats;
108  signal<sensor_base, bool> on_before_streaming_changes;
109  };
110 
112  {
114 
115  virtual double get_frame_timestamp(const request_mapping& mode, const platform::frame_object& fo) = 0;
116  virtual unsigned long long get_frame_counter(const request_mapping& mode, const platform::frame_object& fo) const = 0;
118  virtual void reset() = 0;
119  };
120 
121  class hid_sensor : public sensor_base
122  {
123  public:
124  explicit hid_sensor(std::shared_ptr<platform::hid_device> hid_device,
125  std::unique_ptr<frame_timestamp_reader> hid_iio_timestamp_reader,
126  std::unique_ptr<frame_timestamp_reader> custom_hid_timestamp_reader,
127  std::map<rs2_stream, std::map<unsigned, unsigned>> fps_and_sampling_frequency_per_rs2_stream,
128  std::vector<std::pair<std::string, stream_profile>> sensor_name_and_hid_profiles,
129  device* dev);
130 
131  ~hid_sensor();
132 
133  void open(const stream_profiles& requests) override;
134 
135  void close() override;
136 
137  void start(frame_callback_ptr callback) override;
138 
139  void stop() override;
140 
141  std::vector<uint8_t> get_custom_report_data(const std::string& custom_sensor_name,
142  const std::string& report_name,
143  platform::custom_sensor_report_field report_field) const;
144 
145  protected:
147 
148  private:
149  const std::map<rs2_stream, uint32_t> stream_and_fourcc = {{RS2_STREAM_GYRO, 'GYRO'},
150  {RS2_STREAM_ACCEL, 'ACCL'},
151  {RS2_STREAM_GPIO, 'GPIO'}};
152 
153  const std::vector<std::pair<std::string, stream_profile>> _sensor_name_and_hid_profiles;
154  std::map<rs2_stream, std::map<uint32_t, uint32_t>> _fps_and_sampling_frequency_per_rs2_stream;
155  std::shared_ptr<platform::hid_device> _hid_device;
156  std::mutex _configure_lock;
157  std::map<std::string, stream_profile> _configured_profiles;
158  std::vector<bool> _is_configured_stream;
159  std::vector<platform::hid_sensor> _hid_sensors;
160  std::map<std::string, request_mapping> _hid_mapping;
161  std::unique_ptr<frame_timestamp_reader> _hid_iio_timestamp_reader;
162  std::unique_ptr<frame_timestamp_reader> _custom_hid_timestamp_reader;
163 
164  stream_profiles get_sensor_profiles(std::string sensor_name) const;
165 
166  const std::string& rs2_stream_to_sensor_name(rs2_stream stream) const;
167 
168  uint32_t stream_to_fourcc(rs2_stream stream) const;
169 
170  uint32_t fps_to_sampling_frequency(rs2_stream stream, uint32_t fps) const;
171  };
172 
173  class uvc_sensor : public sensor_base,
174  public roi_sensor_interface
175  {
176  public:
177  explicit uvc_sensor(std::string name, std::shared_ptr<platform::uvc_device> uvc_device,
178  std::unique_ptr<frame_timestamp_reader> timestamp_reader, device* dev);
179 
180  ~uvc_sensor();
181 
182  region_of_interest_method& get_roi_method() const override;
183  void set_roi_method(std::shared_ptr<region_of_interest_method> roi_method) override;
184 
185  void open(const stream_profiles& requests) override;
186 
187  void close() override;
188 
189  std::vector<platform::stream_profile> get_configuration() const { return _internal_config; }
190 
192 
193  template<class T>
194  auto invoke_powered(T action)
195  -> decltype(action(*static_cast<platform::uvc_device*>(nullptr)))
196  {
197  power on(std::dynamic_pointer_cast<uvc_sensor>(shared_from_this()));
198  return action(*_device);
199  }
200 
201  void register_pu(rs2_option id);
202  void try_register_pu(rs2_option id);
203 
204  void start(frame_callback_ptr callback) override;
205 
206  void stop() override;
207 
208  protected:
210 
212 
213  private:
214  void acquire_power();
215 
216  void release_power();
217 
218  void reset_streaming();
219 
220  struct power
221  {
222  explicit power(std::weak_ptr<uvc_sensor> owner)
223  : _owner(owner)
224  {
225  auto strong = _owner.lock();
226  if (strong)
227  {
228  strong->acquire_power();
229  }
230  }
231 
232  ~power()
233  {
234  auto strong = _owner.lock();
235  if (strong) strong->release_power();
236  }
237  private:
238  std::weak_ptr<uvc_sensor> _owner;
239  };
240 
241  std::shared_ptr<platform::uvc_device> _device;
242  std::atomic<int> _user_count;
243  std::mutex _power_lock;
244  std::mutex _configure_lock;
245  std::vector<platform::extension_unit> _xus;
246  std::unique_ptr<power> _power;
247  std::unique_ptr<frame_timestamp_reader> _timestamp_reader;
248  std::shared_ptr<region_of_interest_method> _roi_method = nullptr;
249  };
250 }
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:194
frame_source _source
Definition: sensor.h:101
notifications_callback_ptr get_notifications_callback() const override
void stop() override
virtual double get_frame_timestamp(const request_mapping &mode, const platform::frame_object &fo)=0
void register_xu(platform::extension_unit xu)
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:95
const device_interface & get_device() override
Definition: backend.h:347
std::vector< request_mapping > resolve_requests(stream_profiles requests)
stream_profiles init_stream_profiles() override
rs2_option
Defines general configuration controls. These can generally be mapped to camera UVC controls...
Definition: rs_option.h:22
hid_sensor(std::shared_ptr< platform::hid_device > hid_device, std::unique_ptr< frame_timestamp_reader > hid_iio_timestamp_reader, std::unique_ptr< frame_timestamp_reader > custom_hid_timestamp_reader, std::map< rs2_stream, std::map< unsigned, unsigned >> fps_and_sampling_frequency_per_rs2_stream, std::vector< std::pair< std::string, stream_profile >> sensor_name_and_hid_profiles, device *dev)
uvc_sensor(std::string name, std::shared_ptr< platform::uvc_device > uvc_device, std::unique_ptr< frame_timestamp_reader > timestamp_reader, device *dev)
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:822
Definition: options.h:20
Definition: types.h:539
void start(frame_callback_ptr callback) override
std::vector< platform::stream_profile > get_configuration() const
Definition: sensor.h:189
bool supports_info(rs2_camera_info info) const override
void register_pu(rs2_option id)
sensor_base(std::string name, device *device)
Definition: rs_sensor.h:45
void open(const stream_profiles &requests) override
Definition: rs_sensor.h:43
Definition: streaming.h:131
Definition: types.h:1340
void close() override
on_open _on_open
Definition: sensor.h:98
void open(const stream_profiles &requests) override
void close() override
void set_active_streams(const stream_profiles &requests)
Definition: algo.h:16
virtual stream_profiles init_stream_profiles()=0
device * _owner
Definition: sensor.h:102
void raise_on_before_streaming_changes(bool streaming)
Definition: backend.h:167
std::shared_ptr< rs2_notifications_callback > notifications_callback_ptr
Definition: types.h:824
virtual stream_profiles get_active_streams() const override
std::shared_ptr< notifications_processor > _notifications_processor
Definition: sensor.h:96
void try_register_pu(rs2_option id)
on_before_frame_callback _on_before_frame_callback
Definition: sensor.h:97
std::vector< uint8_t > get_custom_report_data(const std::string &custom_sensor_name, const std::string &report_name, platform::custom_sensor_report_field report_field) const
std::vector< platform::stream_profile > _internal_config
Definition: sensor.h:92
virtual frame_callback_ptr get_frames_callback() const override
virtual ~frame_timestamp_reader()
Definition: sensor.h:113
rs2_stream
Streams are different types of data provided by RealSense devices.
Definition: rs_sensor.h:36
custom_sensor_report_field
Definition: backend.h:344
region_of_interest_method & get_roi_method() const override
Definition: rs_sensor.h:44
void unregister_before_start_callback(int token) override
rs2_extension
Specifies advanced interfaces (capabilities) objects may implement.
Definition: rs_types.h:93
rs2_extension stream_to_frame_types(rs2_stream stream) const
Definition: stream.h:14
void remove_pixel_format(native_pixel_format pf)
std::shared_ptr< metadata_parser_map > _metadata_parsers
Definition: sensor.h:99
void register_on_open(on_open callback)
Definition: sensor.h:63
const std::string & get_info(rs2_camera_info info) const override
void stop() override
std::vector< std::shared_ptr< stream_profile_interface > > stream_profiles
Definition: streaming.h:104
Definition: streaming.h:106
virtual rs2_timestamp_domain get_frame_timestamp_domain(const request_mapping &mode, const platform::frame_object &fo) const =0
void register_pixel_format(native_pixel_format pf)
int register_before_streaming_changes_callback(std::function< void(bool)> callback) override
void start(frame_callback_ptr callback) override
void register_notifications_callback(notifications_callback_ptr callback) override
virtual ~sensor_base()
Definition: sensor.h:59
stream_profiles init_stream_profiles() override
std::atomic< bool > _is_streaming
Definition: sensor.h:94
Definition: sensor.h:121
void assign_stream(const std::shared_ptr< stream_interface > &stream, std::shared_ptr< stream_profile_interface > target) const
Definition: sensor.h:111
void set_roi_method(std::shared_ptr< region_of_interest_method > roi_method) override
bool try_get_pf(const platform::stream_profile &p, native_pixel_format &result) const
Definition: sensor.h:31
Definition: types.h:556
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:173
virtual void set_frames_callback(frame_callback_ptr callback) override
virtual unsigned long long get_frame_counter(const request_mapping &mode, const platform::frame_object &fo) const =0
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