func-common.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2021 Intel Corporation. All Rights Reserved.
3 
4 #include "../test.h"
5 #include "librealsense2/rs.hpp"
6 #include <condition_variable>
7 #include "hw-monitor.h"
8 
9 using namespace rs2;
10 
11 
13 {
15  rs2::device_list devices_list = ctx.query_devices();
16  if( devices_list.size() == 0 )
17  {
18  std::cout << "No device was found; skipping test" << std::endl;
19  exit( 0 );
20  }
21  return devices_list[0];
22 }
23 
25 {
27  rs2::device_list devices_list = ctx.query_devices( product );
28  if( devices_list.size() == 0 )
29  {
30  std::cout << "No device of the " << product << " product line was found; skipping test"
31  << std::endl;
32  exit( 0 );
33  }
34 
35  return devices_list;
36 }
37 
39 {
40  std::string fw_version;
42 
43  if (librealsense::firmware_version(fw_version) < version)
44  {
45  std::cout << "FW version " << fw_version << " is under the minimum requiered FW version "
46  << version << std::endl;
47  exit( 0 );
48  }
49 }
50 
51 // This function returns the first device found that it's name contains the given string,
52 // if there is no such device it will exit the test.
53 // Can get as input a full device name or short like "L515/D435..."
55 {
57  std::vector< rs2::device > devices_list = ctx.query_devices();
58 
59  auto dev_iter
60  = std::find_if( devices_list.begin(), devices_list.end(), [dev_name]( rs2::device dev ) {
61  return dev.supports( RS2_CAMERA_INFO_NAME )
62  && std::string( dev.get_info( RS2_CAMERA_INFO_NAME ) ).find( dev_name )
63  != std::string::npos;
64  } );
65 
66  if( dev_iter != devices_list.end() )
67  {
68  return *dev_iter;
69  }
70 
71  std::cout << "No " << dev_name << " device was found; skipping test" << std::endl;
72  exit( 0 );
73 }
74 
75 // Return the first device that supports the input option or exits.
76 // Can get as input a full device name or short like "L515/D435..."
78  rs2_option opt)
79 {
80  auto dev = find_first_device_by_name_or_exit(dev_name);
81 
82  auto ds = dev.first< rs2::depth_sensor >();
83  if (!ds.supports(opt))
84  {
85  std::cout << "Device: " << dev_name << " does not support option: " << rs2_option_to_string(opt) << std::endl;
86  exit(0);
87  }
88 
89  return ds;
90 }
91 
92 // Remove the frame's stream (or streams if a frameset) from the list of streams we expect to arrive
93 // If any stream is unexpected, it is ignored
95  std::vector< rs2::stream_profile > & expected_streams )
96 {
97  auto remove_stream = [&]() {
98  auto it = std::remove_if( expected_streams.begin(),
99  expected_streams.end(),
100  [&]( rs2::stream_profile s ) {
101  return s.stream_type() == f.get_profile().stream_type();
102  } );
103 
104 
105  if( it != expected_streams.end() )
106  expected_streams.erase( it );
107  };
108 
109  if( f.is< rs2::frameset >() )
110  {
111  auto set = f.as< rs2::frameset >();
112  set.foreach_rs( [&]( rs2::frame fr ) { remove_stream(); } );
113  }
114  else
115  {
116  remove_stream();
117  }
118 }
119 
121 {
122  std::vector< stream_profile > stream_profiles;
123  REQUIRE_NOTHROW(stream_profiles = depth_sens.get_stream_profiles());
124 
125  auto depth_profile
126  = std::find_if(stream_profiles.begin(), stream_profiles.end(), [](stream_profile sp) {
127  return sp.is_default() && sp.stream_type() == RS2_STREAM_DEPTH;
128  });
129 
130  REQUIRE(depth_profile != stream_profiles.end());
131  return *depth_profile;
132 }
133 
135 {
136  std::vector< stream_profile > stream_profiles;
137  REQUIRE_NOTHROW(stream_profiles = depth_sens.get_stream_profiles());
138 
139  auto ir_profile
140  = std::find_if(stream_profiles.begin(), stream_profiles.end(), [](stream_profile sp) {
141  return sp.is_default() && sp.stream_type() == RS2_STREAM_INFRARED;
142  });
143 
144  REQUIRE(ir_profile != stream_profiles.end());
145  return *ir_profile;
146 }
147 
150 {
151  std::vector< stream_profile > stream_profiles;
152  REQUIRE_NOTHROW(stream_profiles = depth_sens.get_stream_profiles());
153 
154  auto confidence_profile
155  = std::find_if(stream_profiles.begin(), stream_profiles.end(), [&](stream_profile sp) {
156  return sp.stream_type() == RS2_STREAM_CONFIDENCE
157  && sp.as< rs2::video_stream_profile >().width()
158  == depth_profile.as< rs2::video_stream_profile >().width()
159  && sp.as< rs2::video_stream_profile >().height()
160  == depth_profile.as< rs2::video_stream_profile >().height();
161  });
162 
163  REQUIRE(confidence_profile != stream_profiles.end());
164  return *confidence_profile;
165 }
166 
167 template < class T >
168 inline void start_default_l500_depth_profiles( rs2::depth_sensor depth_sens, T callback, bool with_confidence = false )
169 {
170  auto depth = find_default_depth_profile( depth_sens );
171  auto ir = find_default_ir_profile( depth_sens );
172 
173  if (with_confidence)
174  {
176  REQUIRE_NOTHROW( depth_sens.open( { depth, ir, confidence } ) );
177  }
178  else
179  {
180  REQUIRE_NOTHROW( depth_sens.open( { depth, ir } ) );
181  }
182 
183  REQUIRE_NOTHROW( depth_sens.start( callback ) );
184 }
185 
186 inline stream_profile
188 {
189  std::vector< stream_profile > stream_profiles;
190  REQUIRE_NOTHROW( stream_profiles = depth_sens.get_stream_profiles() );
191 
192  std::map< rs2_sensor_mode, std::pair< uint32_t, uint32_t > > sensor_mode_to_resolution
193  = { { { RS2_SENSOR_MODE_VGA }, { 640, 480 } },
194  { { RS2_SENSOR_MODE_XGA }, { 1024, 768 } },
195  { { RS2_SENSOR_MODE_QVGA }, { 320, 240 } } };
196 
197 
198  auto profile
199  = std::find_if( stream_profiles.begin(), stream_profiles.end(), [&]( stream_profile sp ) {
200  auto vp = sp.as< video_stream_profile >();
201  if( vp )
202  {
203  return sp.stream_type() == stream
204  && vp.width() == sensor_mode_to_resolution[mode].first
205  && vp.height() == sensor_mode_to_resolution[mode].second;
206  }
207  return false;
208  } );
209 
210  REQUIRE( profile != stream_profiles.end() );
211  return *profile;
212 }
213 
214 inline void do_while_streaming( rs2::sensor depth_sens,
215  std::vector< stream_profile > profiles,
216  std::function< void() > action )
217 {
218  REQUIRE_NOTHROW( depth_sens.open( profiles ) );
219  REQUIRE_NOTHROW( depth_sens.start( [&]( rs2::frame f ) {} ) );
220 
221  action();
222 
223  depth_sens.stop();
224  depth_sens.close();
225 }
226 
230  int width = -1,
231  int height = -1,
232  int fps = -1,
233  int stream_index = -1 )
234 {
235  auto profiles = s.get_stream_profiles();
236  auto found_profile
237  = std::find_if( profiles.begin(), profiles.end(), [=]( rs2::stream_profile sp ) {
238  auto vp = sp.as< rs2::video_stream_profile >();
239  return ( ( RS2_STREAM_ANY == stream || sp.stream_type() == stream )
240  && ( RS2_FORMAT_ANY == format || sp.format() == format )
241  && ( ( -1 == width ) || ( vp.width() == width ) )
242  && ( ( -1 == height ) || ( vp.height() == height ) )
243  && ( ( -1 == fps ) || ( vp.fps() == fps )
244  && ( ( -1 == stream_index ) || vp.stream_index() == stream_index ) ) );
245  } );
246 
247  if( found_profile != profiles.end() )
248  return *found_profile;
249  else
250  return rs2::stream_profile();
251 }
252 
void start_default_l500_depth_profiles(rs2::depth_sensor depth_sens, T callback, bool with_confidence=false)
Definition: func-common.h:168
rs2_option
Defines general configuration controls. These can generally be mapped to camera UVC controls...
Definition: rs_option.h:22
GLdouble s
void remove_all_streams_arrived(rs2::frame f, std::vector< rs2::stream_profile > &expected_streams)
Definition: func-common.h:94
const char * rs2_option_to_string(rs2_option option)
Definition: rs.cpp:1265
stream_profile find_default_depth_profile(rs2::depth_sensor depth_sens)
Definition: func-common.h:120
rs2::device find_first_device_or_exit()
Definition: func-common.h:12
stream_profile get_profile() const
Definition: rs_frame.hpp:557
device_list query_devices() const
Definition: rs_context.hpp:112
uint32_t size() const
Definition: rs_device.hpp:711
GLint GLint GLsizei GLsizei GLsizei depth
Definition: cah-model.h:10
GLsizei const GLchar *const * string
void do_while_streaming(rs2::sensor depth_sens, std::vector< stream_profile > profiles, std::function< void() > action)
Definition: func-common.h:214
GLuint GLuint stream
Definition: glext.h:1790
rs2::device find_first_device_by_name_or_exit(const std::string &dev_name)
Definition: func-common.h:54
GLdouble f
bool is() const
Definition: rs_frame.hpp:570
GLenum mode
std::ostream & cout()
REQUIRE(n_callbacks==1)
rs2::device_list find_devices_by_product_line_or_exit(int product)
Definition: func-common.h:24
rs2::depth_sensor find_first_supported_depth_sensor_or_exit(const std::string &dev_name, rs2_option opt)
Definition: func-common.h:77
const char * get_info(rs2_camera_info info) const
Definition: rs_device.hpp:79
GLint GLsizei GLsizei height
GLint GLint GLsizei GLint GLenum format
def find(dir, mask)
Definition: file.py:25
def callback(frame)
Definition: t265_stereo.py:91
stream_profile find_default_ir_profile(rs2::depth_sensor depth_sens)
Definition: func-common.h:134
rs2_format
A stream&#39;s format identifies how binary data is encoded within a frame.
Definition: rs_sensor.h:59
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
static const textual_icon exit
Definition: model-views.h:254
rs2_sensor_mode
For setting the camera_mode option.
Definition: rs_option.h:165
void foreach_rs(T action) const
Definition: rs_frame.hpp:1107
stream_profile find_confidence_corresponding_to_depth(rs2::depth_sensor depth_sens, stream_profile depth_profile)
Definition: func-common.h:148
static auto it
void open(const stream_profile &profile) const
Definition: rs_sensor.hpp:111
REQUIRE_NOTHROW(rs2_log(RS2_LOG_SEVERITY_INFO,"Log message using rs2_log()", nullptr))
void exit_if_fw_version_is_under(rs2::device &dev, librealsense::firmware_version version)
Definition: func-common.h:38
void close() const
Definition: rs_sensor.hpp:173
std::vector< stream_profile > get_stream_profiles() const
Definition: rs_sensor.hpp:219
rs2_stream stream_type() const
Definition: rs_frame.hpp:39
stream_profile find_profile(rs2::depth_sensor depth_sens, rs2_stream stream, rs2_sensor_mode mode)
Definition: func-common.h:187
void start(T callback) const
Definition: rs_sensor.hpp:185
GLint GLsizei width
void stop() const
Definition: rs_sensor.hpp:195
rs2::stream_profile get_profile_by_stream_parameters(rs2::sensor s, rs2_stream stream=RS2_STREAM_ANY, rs2_format format=RS2_FORMAT_ANY, int width=-1, int height=-1, int fps=-1, int stream_index=-1)
Definition: func-common.h:227
T as() const
Definition: rs_frame.hpp:580


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