12 #include <json/json.h>
26 if (all_device_info.empty()) {
30 size_t sensor_index = 0;
31 for (
auto& dev_info : all_device_info) {
34 for (
auto& config : dev_info.valid_configs)
36 fmt::join(config.second,
" | "));
39 "CloudViewer only supports synchronized color and depth "
41 "(color_fps = depth_fps).");
48 std::vector<RealSenseValidConfigs> all_device_info;
49 for (
auto&& dev : ctx.query_devices()) {
51 dev.get_info(RS2_CAMERA_INFO_NAME),
52 {{
"color_format", {}},
53 {
"color_resolution", {}},
56 {
"depth_resolution", {}},
58 {
"visual_preset", {}}}};
59 for (
auto&& sensor : dev.query_sensors()) {
60 for (
auto&& stream : sensor.get_stream_profiles()) {
61 if (stream.stream_type() == RS2_STREAM_COLOR) {
63 enum_to_string(stream.format()));
64 cfg.valid_configs[
"color_resolution"].insert(
fmt::format(
66 stream.as<rs2::video_stream_profile>().width(),
67 stream.as<rs2::video_stream_profile>().height()));
68 cfg.valid_configs[
"color_fps"].insert(
70 }
else if (stream.stream_type() == RS2_STREAM_DEPTH) {
71 cfg.valid_configs[
"depth_format"].insert(
72 enum_to_string(stream.format()));
73 cfg.valid_configs[
"depth_resolution"].insert(
fmt::format(
75 stream.as<rs2::video_stream_profile>().width(),
76 stream.as<rs2::video_stream_profile>().height()));
77 cfg.valid_configs[
"depth_fps"].insert(
81 if (sensor.supports(RS2_OPTION_VISUAL_PRESET)) {
82 std::string product_line =
83 dev.get_info(RS2_CAMERA_INFO_PRODUCT_LINE);
84 if (product_line ==
"L500") {
86 k < static_cast<int>(RS2_L500_VISUAL_PRESET_COUNT);
88 cfg.valid_configs[
"visual_preset"].insert(
90 static_cast<rs2_l500_visual_preset
>(
92 }
else if (product_line ==
"RS400") {
94 k < static_cast<int>(RS2_RS400_VISUAL_PRESET_COUNT);
96 cfg.valid_configs[
"visual_preset"].insert(
98 static_cast<rs2_rs400_visual_preset
>(
100 }
else if (product_line ==
"SR300") {
102 k < static_cast<int>(RS2_SR300_VISUAL_PRESET_COUNT);
104 cfg.valid_configs[
"visual_preset"].insert(
106 static_cast<rs2_sr300_visual_preset
>(
111 all_device_info.emplace_back(cfg);
113 return all_device_info;
117 : pipe_{new
rs2::pipeline},
118 align_to_color_{new
rs2::align(rs2_stream::RS2_STREAM_COLOR)},
119 rs_config_{new
rs2::config} {
129 if (sensor_config.
config_.find(
"serial") == sensor_config.
config_.cend()) {
132 auto device_list = ctx.query_devices();
133 if (sensor_index >= device_list.size()) {
135 "No device for sensor_index {}. Only {} devices detected.",
136 sensor_index, device_list.size());
138 rs_config_->enable_device(
139 device_list[(uint32_t)sensor_index].get_info(
140 RS2_CAMERA_INFO_SERIAL_NUMBER));
145 enable_recording_ =
false;
148 const std::string parent_dir =
154 enable_recording_ =
true;
157 enable_recording_ =
false;
159 auto profile = rs_config_->resolve(*pipe_);
160 auto dev =
profile.get_device().first<rs2::depth_sensor>();
161 auto it = sensor_config.
config_.find(
"visual_preset");
163 std::string option_str{
"RS2_VISUAL_PRESET_DEFAULT"};
164 if (it != sensor_config.
config_.cend() && !it->second.empty())
165 option_str = it->second;
166 std::string product_line = dev.get_info(RS2_CAMERA_INFO_PRODUCT_LINE);
167 if (product_line ==
"L500") {
168 rs2_l500_visual_preset option;
169 enum_from_string(option_str, option);
170 if (option != RS2_L500_VISUAL_PRESET_DEFAULT)
171 dev.set_option(RS2_OPTION_VISUAL_PRESET,
172 static_cast<float>(option));
173 }
else if (product_line ==
"RS400") {
174 rs2_rs400_visual_preset option;
175 enum_from_string(option_str, option);
176 if (option != RS2_RS400_VISUAL_PRESET_DEFAULT)
177 dev.set_option(RS2_OPTION_VISUAL_PRESET,
178 static_cast<float>(option));
179 }
else if (product_line ==
"SR300") {
180 rs2_sr300_visual_preset option;
181 enum_from_string(option_str, option);
182 if (option != RS2_SR300_VISUAL_PRESET_DEFAULT)
183 dev.set_option(RS2_OPTION_VISUAL_PRESET,
184 static_cast<float>(option));
192 "Invalid RealSense camera configuration, or camera not connected:"
194 rs2_exception_type_to_string(e.get_type()), e.what());
204 is_recording_ = enable_recording_ && start_record;
205 if (is_recording_) rs_config_->enable_record_to_file(filename_);
206 const auto profile = pipe_->start(*rs_config_);
212 is_capturing_ =
true;
214 "Capture started with RealSense camera {}",
215 profile.get_device().get_info(RS2_CAMERA_INFO_SERIAL_NUMBER));
216 if (enable_recording_) {
218 start_record ?
"" :
"[Paused] ", filename_);
223 rs2_exception_type_to_string(e.get_type()), e.what());
229 if (!enable_recording_ || !is_recording_)
return;
230 pipe_->get_active_profile().get_device().as<rs2::recorder>().pause();
231 is_recording_ =
false;
236 if (!enable_recording_ || is_recording_)
return;
238 if (
auto dev = pipe_->get_active_profile()
240 .as<rs2::recorder>()) {
244 rs_config_->enable_record_to_file(filename_);
245 pipe_.reset(
new rs2::pipeline);
246 pipe_->start(*rs_config_);
249 is_recording_ =
true;
252 rs2_exception_type_to_string(e.get_type()), e.what());
257 bool align_depth_to_color) {
258 if (!is_capturing_) {
263 rs2::frameset frames;
264 if (!((wait && pipe_->try_wait_for_frames(&frames)) ||
265 (!wait && pipe_->poll_for_frames(&frames))))
267 if (align_depth_to_color) frames = align_to_color_->process(frames);
268 timestamp_ = uint64_t(frames.get_timestamp() * MILLISEC_TO_MICROSEC);
270 const auto& color_frame = frames.get_color_frame();
272 static_cast<const uint8_t*
>(color_frame.get_data()),
273 {color_frame.get_height(), color_frame.get_width(),
274 metadata_.color_channels_},
276 const auto& depth_frame = frames.get_depth_frame();
278 static_cast<const uint16_t*
>(depth_frame.get_data()),
279 {depth_frame.get_height(), depth_frame.get_width()},
281 return current_frame_;
284 rs2_exception_type_to_string(e.get_type()), e.what());
292 is_recording_ =
false;
293 is_capturing_ =
false;
filament::Texture::InternalFormat format
CloudViewerScene::LightingProfile profile
RGBDImage A pair of color and depth images.
Image depth_
The depth image.
Image color_
The color image.
rs2::config ConvertToNativeConfig() const
Convert to RealSense config.
static void GetPixelDtypes(const rs2::pipeline_profile &profile, RGBDVideoMetadata &metadata)
std::unordered_map< std::string, std::string > config_
static Json::Value GetMetadataJson(const rs2::pipeline_profile &profile)
Get metadata for a streaming RealSense camera or bag file.
virtual bool InitSensor(const RealSenseSensorConfig &sensor_config=RealSenseSensorConfig{}, size_t sensor_index=0, const std::string &filename="")
virtual ~RealSenseSensor() override
static bool ListDevices()
virtual void StopCapture() override
Stop capturing frames.
static std::vector< RealSenseValidConfigs > EnumerateDevices()
virtual void PauseRecord() override
RealSenseSensor()
Default constructor. Initialize with default settings.
virtual void ResumeRecord() override
virtual geometry::RGBDImage CaptureFrame(bool wait=true, bool align_depth_to_color=true) override
virtual bool StartCapture(bool start_record=false) override
static void error(char *msg)
bool MakeDirectoryHierarchy(const std::string &directory)
std::string GetFileParentDirectory(const std::string &filename)
bool DirectoryExists(const std::string &directory)
bool FileExists(const std::string &filename)
Generic file read and write utility for python interface.
std::string to_string(const T &n)
std::unordered_map< std::string, std::set< std::string > > valid_configs