pipeline/config.cpp
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 #include "config.h"
5 #include "pipeline.h"
6 
7 namespace librealsense
8 {
9  namespace pipeline
10  {
12  {
13  //empty
14  }
16  {
17  std::lock_guard<std::mutex> lock(_mtx);
18  _resolved_profile.reset();
19  _stream_requests[{stream, index}] = { format, stream, index, width, height, fps };
20  auto position = std::find(_streams_to_disable.begin(), _streams_to_disable.end(), std::pair<rs2_stream, int>{stream, index});
21  if (position != _streams_to_disable.end())
22  _streams_to_disable.erase(position); //means the element was found
23  }
24 
26  {
27  std::lock_guard<std::mutex> lock(_mtx);
28  _resolved_profile.reset();
29  _stream_requests.clear();
30  _enable_all_streams = true;
31  _streams_to_disable.clear();
32  }
33 
34  void config::enable_device(const std::string& serial)
35  {
36  std::lock_guard<std::mutex> lock(_mtx);
37  _resolved_profile.reset();
38  _device_request.serial = serial;
39  }
40 
41  void config::enable_device_from_file(const std::string& file, bool repeat_playback = true)
42  {
43  std::lock_guard<std::mutex> lock(_mtx);
44  if (!_device_request.record_output.empty())
45  {
46  throw std::runtime_error("Configuring both device from file, and record to file is unsupported");
47  }
48  _resolved_profile.reset();
51  }
52 
53  void config::enable_record_to_file(const std::string& file)
54  {
55  std::lock_guard<std::mutex> lock(_mtx);
56  if (!_device_request.filename.empty())
57  {
58  throw std::runtime_error("Configuring both device from file, and record to file is unsupported");
59  }
60  _resolved_profile.reset();
62  }
63 
64  std::shared_ptr<profile> config::get_cached_resolved_profile()
65  {
66  std::lock_guard<std::mutex> lock(_mtx);
67  return _resolved_profile;
68  }
69 
71  {
72  std::lock_guard<std::mutex> lock(_mtx);
73  _streams_to_disable.push_back({ stream, std::max(index, 0) }); // for this DB default index should be 0 not -1 because it is compared to get_stream_index() API that returns 0 as default index
74 
75  auto itr = std::begin(_stream_requests);
76  while (itr != std::end(_stream_requests))
77  {
78  //if this is the same stream type and also the user either requested all or it has the same index
79  if (itr->first.first == stream && (index == -1 || itr->first.second == index))
80  {
81  itr = _stream_requests.erase(itr);
82  }
83  else
84  {
85  ++itr;
86  }
87  }
88  _resolved_profile.reset();
89  }
90 
92  {
93  std::lock_guard<std::mutex> lock(_mtx);
94  _stream_requests.clear();
95  _enable_all_streams = false;
96  _resolved_profile.reset();
97  _streams_to_disable.clear();
98  }
99 
101  {
103  if (!_streams_to_disable.empty())
104  {
105  for (auto prof : profiles)
106  {
107  bool disable_stream = false;
108  auto p = prof.get();
109  auto vp = dynamic_cast<video_stream_profile*>(p);
110  for (auto& st : _streams_to_disable)
111  {
112  if (st.first == p->get_stream_type())
113  {
114  if (st.second != p->get_stream_index())
115  break; // don't disable stream if indexes don't match
116  disable_stream = true;
117  break;
118  }
119  }
120  if (disable_stream)
121  continue;
122  if (vp)
123  config.enable_stream(vp->get_stream_type(), vp->get_stream_index(), vp->get_width(), vp->get_height(), vp->get_format(), vp->get_framerate());
124  else
125  config.enable_stream(p->get_stream_type(), p->get_stream_index(), 0, 0, p->get_format(), p->get_framerate());
126  }
127  }
128  else
129  {
130  config.enable_streams(profiles);
131  }
132 
133  return config;
134  }
135 
136  std::shared_ptr<profile> config::resolve(std::shared_ptr<device_interface> dev)
137  {
139  util::config filtered_config;
140 
141  //if the user requested all streams
143  {
145  for (size_t i = 0; i < dev->get_sensors_count(); ++i)
146  {
147  auto&& sub = dev->get_sensor(i);
148  auto p = sub.get_stream_profiles(PROFILE_TAG_SUPERSET);
149  profiles.insert(profiles.end(), p.begin(), p.end());
150  }
151  filtered_config = filter_stream_requests(profiles);
152  return std::make_shared<profile>(dev, filtered_config, _device_request.record_output);
153  }
154 
155  //If the user did not request anything, give it the default, on playback all recorded streams are marked as default.
156  if (_stream_requests.empty())
157  {
158  auto default_profiles = get_default_configuration(dev);
159  filtered_config = filter_stream_requests(default_profiles);
160  return std::make_shared<profile>(dev, filtered_config, _device_request.record_output);
161  }
162 
163  //Enabled requested streams
164  for (auto&& req : _stream_requests)
165  {
166  bool disable_stream = false;
167  auto r = req.second;
168  for (auto& st : _streams_to_disable)
169  {
170  if (st.first == r.stream)
171  {
172  if (st.second > 0 && st.second != r.index) break; // don't disable stream if indexes don't match
173  disable_stream = true;
174  break;
175  }
176  }
177  if (disable_stream) continue;
178  config.enable_stream(r.stream, r.index, r.width, r.height, r.format, r.fps);
179  }
180  return std::make_shared<profile>(dev, config, _device_request.record_output);
181  }
182 
183  std::shared_ptr<profile> config::resolve(std::shared_ptr<pipeline> pipe, const std::chrono::milliseconds& timeout)
184  {
185  std::lock_guard<std::mutex> lock(_mtx);
186  _resolved_profile.reset();
187 
188  //Resolve the the device that was specified by the user, this call will wait in case the device is not availabe.
189  auto requested_device = resolve_device_requests(pipe, timeout);
190  if (requested_device != nullptr)
191  {
192  _resolved_profile = resolve(requested_device);
193  return _resolved_profile;
194  }
195 
196  //Look for satisfy device in case the user did not specify one.
197  auto devs = pipe->get_context()->query_devices(RS2_PRODUCT_LINE_ANY_INTEL);
198  for (auto dev_info : devs)
199  {
200  try
201  {
202  auto dev = dev_info->create_device();
204  return _resolved_profile;
205  }
206  catch (const std::exception& e)
207  {
208  LOG_DEBUG("Iterate available devices - config can not be resolved. " << e.what());
209  }
210  }
211 
212  //If no device found wait for one
213  auto dev = pipe->wait_for_device(timeout);
214  if (dev != nullptr)
215  {
216  _resolved_profile = resolve(dev);
217  return _resolved_profile;
218  }
219 
220  throw std::runtime_error("Failed to resolve request. No device found that satisfies all requirements");
221 
222  assert(0); //Unreachable code
223  }
224 
225  bool config::can_resolve(std::shared_ptr<pipeline> pipe)
226  {
227  try
228  { // Try to resolve from connected devices. Non-blocking call
229  resolve(pipe);
230  _resolved_profile.reset();
231  }
232  catch (const std::exception& e)
233  {
234  LOG_DEBUG("Config can not be resolved. " << e.what());
235  return false;
236  }
237  catch (...)
238  {
239  return false;
240  }
241  return true;
242  }
243 
244  std::shared_ptr<device_interface> config::get_or_add_playback_device(std::shared_ptr<context> ctx, const std::string& file)
245  {
246  //Check if the file is already loaded to context, and if so return that device
247  for (auto&& d : ctx->query_devices(RS2_PRODUCT_LINE_ANY))
248  {
249  auto playback_devs = d->get_device_data().playback_devices;
250  for (auto&& p : playback_devs)
251  {
252  if (p.file_path == file)
253  {
254  return d->create_device();
255  }
256  }
257  }
258 
259  return ctx->add_device(file)->create_device();
260  }
261 
262  std::shared_ptr<device_interface> config::resolve_device_requests(std::shared_ptr<pipeline> pipe, const std::chrono::milliseconds& timeout)
263  {
264  //Prefer filename over serial
265  if (!_device_request.filename.empty())
266  {
267  std::shared_ptr<device_interface> dev;
268  try
269  {
270  dev = get_or_add_playback_device(pipe->get_context(), _device_request.filename);
271  }
272  catch (const std::exception& e)
273  {
274  throw std::runtime_error(to_string() << "Failed to resolve request. Request to enable_device_from_file(\"" << _device_request.filename << "\") was invalid, Reason: " << e.what());
275  }
276  //check if a serial number was also requested, and check again the device
277  if (!_device_request.serial.empty())
278  {
279  if (!dev->supports_info(RS2_CAMERA_INFO_SERIAL_NUMBER))
280  {
281  throw std::runtime_error(to_string() << "Failed to resolve request. "
282  "Conflic between enable_device_from_file(\"" << _device_request.filename
283  << "\") and enable_device(\"" << _device_request.serial << "\"), "
284  "File does not contain a device with such serial");
285  }
286  else
287  {
288  std::string s = dev->get_info(RS2_CAMERA_INFO_SERIAL_NUMBER);
289  if (s != _device_request.serial)
290  {
291  throw std::runtime_error(to_string() << "Failed to resolve request. "
292  "Conflic between enable_device_from_file(\"" << _device_request.filename
293  << "\") and enable_device(\"" << _device_request.serial << "\"), "
294  "File contains device with different serial number (" << s << "\")");
295  }
296  }
297  }
298  return dev;
299  }
300 
301  if (!_device_request.serial.empty())
302  {
303  return pipe->wait_for_device(timeout, _device_request.serial);
304  }
305 
306  return nullptr;
307  }
308 
309  stream_profiles config::get_default_configuration(std::shared_ptr<device_interface> dev)
310  {
311  stream_profiles default_profiles;
312 
313  for (unsigned int i = 0; i < dev->get_sensors_count(); i++)
314  {
315  auto&& sensor = dev->get_sensor(i);
316  auto profiles = sensor.get_stream_profiles(profile_tag::PROFILE_TAG_DEFAULT);
317  default_profiles.insert(std::end(default_profiles), std::begin(profiles), std::end(profiles));
318  }
319 
320  return default_profiles;
321  }
322 
324  return _playback_loop;
325  }
326  }
327 }
static const textual_icon lock
Definition: model-views.h:219
GLuint GLuint end
GLdouble s
void enable_streams(stream_profiles profiles)
Definition: resolver.h:182
std::shared_ptr< device_interface > get_or_add_playback_device(std::shared_ptr< context > ctx, const std::string &file)
bool can_resolve(std::shared_ptr< pipeline > pipe)
void enable_device(const std::string &serial)
d
Definition: rmse.py:171
#define LOG_DEBUG(...)
Definition: easyloggingpp.h:55
e
Definition: rmse.py:177
void enable_device_from_file(const std::string &file, bool repeat_playback)
std::shared_ptr< device_interface > resolve_device_requests(std::shared_ptr< pipeline > pipe, const std::chrono::milliseconds &timeout)
GLuint index
not_this_one begin(...)
GLdouble GLdouble r
double p[GRIDW][GRIDH]
Definition: wave.c:109
void disable_stream(rs2_stream stream, int index=-1)
#define assert(condition)
Definition: lz4.c:245
unsigned int uint32_t
Definition: stdint.h:80
GLint GLsizei GLsizei height
GLint GLint GLsizei GLint GLenum format
std::shared_ptr< profile > get_cached_resolved_profile()
def find(dir, mask)
Definition: file.py:45
rs2_format
A stream&#39;s format identifies how binary data is encoded within a frame.
Definition: rs_sensor.h:59
stream_profiles get_default_configuration(std::shared_ptr< device_interface > dev)
#define RS2_PRODUCT_LINE_ANY_INTEL
Definition: rs_context.h:92
rs2_stream
Streams are different types of data provided by RealSense devices.
Definition: rs_sensor.h:42
void enable_stream(rs2_stream stream, int index, uint32_t width, uint32_t height, rs2_format format, uint32_t fps)
Definition: resolver.h:205
#define RS2_PRODUCT_LINE_ANY
Definition: rs_context.h:91
std::shared_ptr< profile > _resolved_profile
Definition: config.h:62
device_request _device_request
Definition: config.h:58
GLbitfield GLuint64 timeout
std::vector< std::shared_ptr< stream_profile_interface > > stream_profiles
Definition: streaming.h:79
int i
std::vector< std::pair< rs2_stream, int > > _streams_to_disable
Definition: config.h:64
void enable_record_to_file(const std::string &file)
util::config filter_stream_requests(const stream_profiles &profiles) const
std::map< std::pair< rs2_stream, int >, stream_profile > _stream_requests
Definition: config.h:59
void enable_stream(rs2_stream stream, int index, uint32_t width, uint32_t height, rs2_format format, uint32_t framerate)
GLint GLsizei width
std::shared_ptr< profile > resolve(std::shared_ptr< pipeline > pipe, const std::chrono::milliseconds &timeout=std::chrono::milliseconds(0))
std::string to_string(T value)


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Thu Dec 22 2022 03:43:16