From 27edb4f96ef35cf0651741620d4f50f3a13b3339 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Thu, 9 Jul 2020 21:13:49 +0300
Subject: [PATCH] Configurable aliasing and non stereo rectify bug fix

---
 .../common/cpp/include/ftl/configuration.hpp  |  5 ++++
 components/common/cpp/src/configuration.cpp   | 11 ++++++++-
 .../src/sources/stereovideo/rectification.cpp |  1 +
 .../src/sources/stereovideo/stereovideo.cpp   |  2 ++
 components/streams/src/netstream.cpp          | 23 +++++++++++++++++++
 5 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/components/common/cpp/include/ftl/configuration.hpp b/components/common/cpp/include/ftl/configuration.hpp
index 7ad81440b..5b0106fe0 100644
--- a/components/common/cpp/include/ftl/configuration.hpp
+++ b/components/common/cpp/include/ftl/configuration.hpp
@@ -79,6 +79,11 @@ json_t &resolveWait(const std::string &);
  */
 Configurable *find(const std::string &uri);
 
+/**
+ * Add an alternative URI for a configurable.
+ */
+void alias(const std::string &uri, Configurable *cfg);
+
 /**
  * Get all configurables that contain a specified tag. Tags are given under the
  * "tags" property as an array of strings, but only during configurable
diff --git a/components/common/cpp/src/configuration.cpp b/components/common/cpp/src/configuration.cpp
index 5e7bd0ea2..9c1154d6e 100644
--- a/components/common/cpp/src/configuration.cpp
+++ b/components/common/cpp/src/configuration.cpp
@@ -238,6 +238,7 @@ static bool mergeConfig(const string path) {
 
 static std::map<std::string, json_t*> config_index;
 static std::map<std::string, ftl::Configurable*> config_instance;
+static std::map<std::string, ftl::Configurable*> config_alias;
 static std::map<std::string, std::vector<ftl::Configurable*>> tag_index;
 
 /*
@@ -269,10 +270,18 @@ ftl::Configurable *ftl::config::find(const std::string &uri) {
 	}
 	
 	auto ix = config_instance.find(actual_uri);
-	if (ix == config_instance.end()) return nullptr;
+	if (ix == config_instance.end()) {
+		auto ix = config_alias.find(actual_uri);
+		if (ix == config_instance.end()) return nullptr;
+		else return (*ix).second;
+	}
 	else return (*ix).second;
 }
 
+void ftl::config::alias(const std::string &uri, Configurable *cfg) {
+	config_alias[uri] = cfg;
+}
+
 const std::vector<Configurable*> &ftl::config::findByTag(const std::string &tag) {
 	return tag_index[tag];
 }
diff --git a/components/rgbd-sources/src/sources/stereovideo/rectification.cpp b/components/rgbd-sources/src/sources/stereovideo/rectification.cpp
index 78c935ecc..a514c54f1 100644
--- a/components/rgbd-sources/src/sources/stereovideo/rectification.cpp
+++ b/components/rgbd-sources/src/sources/stereovideo/rectification.cpp
@@ -181,6 +181,7 @@ double StereoRectification::baseline() {
 }
 
 double StereoRectification::doff() {
+	if (!enabled_ || !valid_) return 0.0;
 	return -(Q_.at<double>(3,3) * baseline_);
 }
 
diff --git a/components/rgbd-sources/src/sources/stereovideo/stereovideo.cpp b/components/rgbd-sources/src/sources/stereovideo/stereovideo.cpp
index 815e42657..5416b167f 100644
--- a/components/rgbd-sources/src/sources/stereovideo/stereovideo.cpp
+++ b/components/rgbd-sources/src/sources/stereovideo/stereovideo.cpp
@@ -172,6 +172,8 @@ void StereoVideoSource::updateParameters(ftl::rgbd::Frame &frame) {
 	calibration_change_ = frame.onChange(Channel::CalibrationData, [this]
 			(ftl::data::Frame& frame, ftl::codecs::Channel) {
 
+		if (!lsrc_->isStereo()) return true;
+
 		auto &change = frame.get<ftl::calibration::CalibrationData>(Channel::CalibrationData);
 		try {
 			change.writeFile(fname_calib_);
diff --git a/components/streams/src/netstream.cpp b/components/streams/src/netstream.cpp
index 105152c14..73c758f49 100644
--- a/components/streams/src/netstream.cpp
+++ b/components/streams/src/netstream.cpp
@@ -3,6 +3,11 @@
 #define LOGURU_REPLACE_GLOG 1
 #include <loguru.hpp>
 
+#ifndef WIN32
+#include <unistd.h>
+#include <limits.h>
+#endif
+
 using ftl::stream::Net;
 using ftl::codecs::StreamPacket;
 using ftl::codecs::Packet;
@@ -229,6 +234,24 @@ bool Net::begin() {
 		// TODO: Register URI as available.
 		host_ = true;
 
+		// Alias the URI to the configurable if not already
+		// Allows the URI to be used to get config data.
+		if (ftl::config::find(uri_) == nullptr) {
+			ftl::config::alias(uri_, this);
+		}
+
+		// Automatically set name if missing
+		if (!get<std::string>("name")) {
+			char hostname[1024] = {0};
+			#ifdef WIN32
+			GetComputerName(hostname, 1024);
+			#else
+			gethostname(hostname, 1024);
+			#endif
+
+			set("name", std::string(hostname));
+		}
+
 		net_->broadcast("add_stream", uri_);
 
 		return true;
-- 
GitLab