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


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Fri Aug 2 2024 08:30:01