From 0a88bb6615cb560d09b51965393a4f555a7305ff Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Sun, 19 Jul 2020 09:13:43 +0300
Subject: [PATCH] Fix realsense render baseline bug

---
 .../sources/realsense/realsense_source.cpp    | 35 ++++++++++++++++++-
 .../sources/realsense/realsense_source.hpp    |  1 +
 2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/components/rgbd-sources/src/sources/realsense/realsense_source.cpp b/components/rgbd-sources/src/sources/realsense/realsense_source.cpp
index d0147712e..982f4209a 100644
--- a/components/rgbd-sources/src/sources/realsense/realsense_source.cpp
+++ b/components/rgbd-sources/src/sources/realsense/realsense_source.cpp
@@ -2,11 +2,27 @@
 #include <loguru.hpp>
 #include <ftl/threads.hpp>
 #include <ftl/rgbd/source.hpp>
+#include <ftl/rgbd/capabilities.hpp>
 
 using ftl::rgbd::detail::RealsenseSource;
 using std::string;
 using ftl::codecs::Channel;
 using cv::cuda::GpuMat;
+using ftl::rgbd::Capability;
+
+static std::string get_device_name(const rs2::device& dev) {
+	// Each device provides some information on itself, such as name:
+	std::string name = "Unknown Device";
+	if (dev.supports(RS2_CAMERA_INFO_NAME))
+		name = dev.get_info(RS2_CAMERA_INFO_NAME);
+
+	// and the serial number of the device:
+	std::string sn = "########";
+	if (dev.supports(RS2_CAMERA_INFO_SERIAL_NUMBER))
+		sn = std::string("#") + dev.get_info(RS2_CAMERA_INFO_SERIAL_NUMBER);
+
+	return name + " " + sn;
+}
 
 RealsenseSource::RealsenseSource(ftl::rgbd::Source *host)
         : ftl::rgbd::BaseSourceImpl(host), align_to_depth_(RS2_STREAM_COLOR) {
@@ -20,11 +36,13 @@ RealsenseSource::RealsenseSource(ftl::rgbd::Source *host)
     //pipe_.start(cfg);
     rs2::pipeline_profile profile = pipe_.start(cfg);
     rs2::device dev = profile.get_device();
+	name_ = get_device_name(dev);
     rs2_intrinsics intrin = profile.get_stream(rs2_stream::RS2_STREAM_DEPTH).as<rs2::video_stream_profile>().get_intrinsics();
 
     rs2::depth_sensor ds = dev.query_sensors().front().as<rs2::depth_sensor>();
     scale_ = ds.get_depth_scale();
-    LOG(INFO) << "RS Scale = " << scale_;
+
+	LOG(INFO) << "Realsense device: " << name_;
 
     params_.width = intrin.width;
     params_.height = intrin.height;
@@ -35,6 +53,7 @@ RealsenseSource::RealsenseSource(ftl::rgbd::Source *host)
     params_.maxDepth = 3.0;
     params_.minDepth = 0.1;
 	params_.doffs = 0.0;
+	params_.baseline = 0.055f;  // TODO: Get from device extrinsics
 
     do_update_params_ = true;
 
@@ -56,6 +75,20 @@ bool RealsenseSource::retrieve(ftl::rgbd::Frame &frame) {
 		do_update_params_ = false;
 		frame.setLeft() = params_;
 		frame.setPose() = Eigen::Matrix4d::Identity();
+
+		auto &meta = frame.create<std::map<std::string,std::string>>(Channel::MetaData);
+		meta["name"] = host_->value("name", host_->getID());
+		meta["id"] = host_->getID();
+		meta["uri"] = host_->value("uri", std::string(""));
+		meta["device"] = name_;
+
+		if (!frame.has(Channel::Capabilities)) {
+			auto &cap = frame.create<std::unordered_set<Capability>>(Channel::Capabilities);
+			cap.emplace(Capability::VIDEO);
+			cap.emplace(Capability::LIVE);
+			cap.emplace(Capability::ACTIVE);
+			cap.emplace(Capability::ADJUSTABLE);
+		}
 	}
 
     rs2::frameset frames;
diff --git a/components/rgbd-sources/src/sources/realsense/realsense_source.hpp b/components/rgbd-sources/src/sources/realsense/realsense_source.hpp
index 41de10788..ec95ca006 100644
--- a/components/rgbd-sources/src/sources/realsense/realsense_source.hpp
+++ b/components/rgbd-sources/src/sources/realsense/realsense_source.hpp
@@ -31,6 +31,7 @@ class RealsenseSource : public ftl::rgbd::BaseSourceImpl {
     rs2::align align_to_depth_;
 	rs2::frame rscolour_;
 	ftl::rgbd::Camera params_;
+	std::string name_;
 };
 
 }
-- 
GitLab