From 6ae407088ed82727c85fbe37c860f0014c5322c1 Mon Sep 17 00:00:00 2001
From: Iiro Rastas <iitara@utu.fi>
Date: Wed, 11 Dec 2019 15:37:42 +0200
Subject: [PATCH] Fix reconstruction snapshots

Reconstruction snapshots now include pose and calibration packets, so
that they can actually be reconstructed. Recording options in the
configuration have also been moved from the root to controls.
---
 applications/gui/src/media_panel.cpp          | 18 +++++++-----
 applications/reconstruct/src/main.cpp         | 24 ++++++++++-----
 .../rgbd-sources/include/ftl/rgbd/source.hpp  | 29 -------------------
 components/rgbd-sources/src/source.cpp        | 18 ------------
 4 files changed, 28 insertions(+), 61 deletions(-)

diff --git a/applications/gui/src/media_panel.cpp b/applications/gui/src/media_panel.cpp
index e83626abc..c2fa4285e 100644
--- a/applications/gui/src/media_panel.cpp
+++ b/applications/gui/src/media_panel.cpp
@@ -308,8 +308,10 @@ void MediaPanel::snapshot3D(ftl::gui::Camera *camera, const std::string &filenam
 		auto tagvalue = tag.value();
 		auto configurables = ftl::config::findByTag(tagvalue);
 		if (configurables.size() > 0) {
-			ftl::Configurable *configurable = configurables[0];
-			configurable->set("3D-snapshot", filename);
+			ftl::Configurable *configurable = ftl::config::find(configurables[0]->getID() + "/controls");
+			if (configurable) {
+				configurable->set("3D-snapshot", filename);
+			}
 		}
 	}
 }
@@ -320,11 +322,13 @@ void MediaPanel::startRecording3D(ftl::gui::Camera *camera, const std::string &f
 		auto tagvalue = tag.value();
 		auto configurables = ftl::config::findByTag(tagvalue);
 		if (configurables.size() > 0) {
-			ftl::Configurable *configurable = configurables[0];
-			configurable->set("record-name", filename);
-			configurable->set("record", true);
-			sceneRecording_ = std::optional<ftl::Configurable*>(configurable);
-			recordbutton_->setTextColor(nanogui::Color(1.0f,0.1f,0.1f,1.0f));
+			ftl::Configurable *configurable = ftl::config::find(configurables[0]->getID() + "/controls");
+			if (configurable) {
+				configurable->set("record-name", filename);
+				configurable->set("record", true);
+				sceneRecording_ = std::optional<ftl::Configurable*>(configurable);
+				recordbutton_->setTextColor(nanogui::Color(1.0f,0.1f,0.1f,1.0f));
+			}
 		}
 	}
 }
diff --git a/applications/reconstruct/src/main.cpp b/applications/reconstruct/src/main.cpp
index e1e7adcd8..33db96ae0 100644
--- a/applications/reconstruct/src/main.cpp
+++ b/applications/reconstruct/src/main.cpp
@@ -137,10 +137,10 @@ static void run(ftl::Configurable *root) {
 
 	// Controls
 	auto *controls = ftl::create<ftl::Configurable>(root, "controls");
-	
+
 	net->start();
 	net->waitConnections();
-	
+
 	std::vector<int> sourcecounts;
 
 	// Add sources from the configuration file as a single group.
@@ -275,7 +275,7 @@ static void run(ftl::Configurable *root) {
 	std::ofstream snapshotout;
 	ftl::codecs::Writer snapshotwriter(snapshotout);
 
-	root->set("record", false);
+	controls->set("record", false);
 
 	int64_t timestamp = -1;
 	bool writingSnapshot = false;
@@ -294,8 +294,12 @@ static void run(ftl::Configurable *root) {
 				// IFrame is one of the first two frames seen. In this case a
 				// part of the frame might already have been missed, so the
 				// IFrame after that one is captured instead.
-				if (precedingFrames.size() >= 2) {
-					bool isIFrame = false;
+
+				// Write all pose and calibration packets.
+				if ((int)spkt.channel >= 64) {
+					snapshotwriter.write(s, pkt);
+				} else if (precedingFrames.size() >= 2) {
+					bool isIFrame = true;
 					switch (pkt.codec) {
 						case ftl::codecs::codec_t::H264:
 							isIFrame = ftl::codecs::h264::isIFrame(pkt.data);
@@ -326,7 +330,7 @@ static void run(ftl::Configurable *root) {
 	}
 
 	// Allow stream recording
-	root->on("record", [&fileout,&writer,&sources](const ftl::config::Event &e) {
+	controls->on("record", [&fileout,&writer,&sources](const ftl::config::Event &e) {
 		if (e.entity->value("record", false)) {
 			char timestamp[18];
 			std::time_t t=std::time(NULL);
@@ -348,7 +352,7 @@ static void run(ftl::Configurable *root) {
 		}
 	});
 
-	root->on("3D-snapshot", [&snapshotout,&snapshotwriter,&writingSnapshot,&precedingFrames,&followingFrames](const ftl::config::Event &e) {
+	controls->on("3D-snapshot", [&snapshotout,&snapshotwriter,&writingSnapshot,&precedingFrames,&followingFrames,&sources](const ftl::config::Event &e) {
 		if (!snapshotwriter.active()) {
 			char timestamp[18];
 			std::time_t t=std::time(NULL);
@@ -358,6 +362,12 @@ static void run(ftl::Configurable *root) {
 			precedingFrames.clear();
 			followingFrames.clear();
 			snapshotwriter.begin();
+
+			for (size_t i=0; i<sources.size(); ++i) {
+				//writeSourceProperties(writer, i, sources[i]);
+				sources[i]->inject(Channel::Calibration, sources[i]->parameters(), Channel::Left, sources[i]->getCapabilities());
+				sources[i]->inject(sources[i]->getPose());
+			}
 		}
 	});
 
diff --git a/components/rgbd-sources/include/ftl/rgbd/source.hpp b/components/rgbd-sources/include/ftl/rgbd/source.hpp
index f77f2456b..895817359 100644
--- a/components/rgbd-sources/include/ftl/rgbd/source.hpp
+++ b/components/rgbd-sources/include/ftl/rgbd/source.hpp
@@ -220,11 +220,6 @@ class Source : public ftl::Configurable {
 
 	void inject(const Eigen::Matrix4d &pose);
 
-	template <typename... ARGS>
-	std::pair<ftl::codecs::StreamPacket, ftl::codecs::Packet> make_packet(ftl::codecs::Channel c, ARGS... args);
-
-	std::pair<ftl::codecs::StreamPacket, ftl::codecs::Packet> make_packet(const Eigen::Matrix4d &pose);
-
 	protected:
 	detail::Source *impl_;
 	Eigen::Matrix4d pose_;
@@ -260,30 +255,6 @@ class VectorBuffer {
 	std::vector<unsigned char> &vector_;
 };
 
-template <typename... ARGS>
-std::pair<ftl::codecs::StreamPacket, ftl::codecs::Packet> ftl::rgbd::Source::make_packet(ftl::codecs::Channel c, ARGS... args) {
-	auto data = std::make_tuple(args...);
-
-	ftl::codecs::StreamPacket spkt;
-	ftl::codecs::Packet pkt;
-
-	spkt.timestamp = impl_->timestamp_;
-	std::cout << "Timestamp set." << '\n';
-	spkt.channel = c;
-	spkt.channel_count = 0;
-	spkt.streamID = 0;
-	pkt.codec = ftl::codecs::codec_t::MSGPACK;
-	pkt.block_number = 0;
-	pkt.block_total = 1;
-	pkt.definition = ftl::codecs::definition_t::Any;
-	pkt.flags = 0;
-
-	VectorBuffer buf(pkt.data);
-	msgpack::pack(buf, data);
-
-	return std::make_pair(spkt, pkt);
-}
-
 template <typename... ARGS>
 void ftl::rgbd::Source::inject(ftl::codecs::Channel c, ARGS... args) {
 	if (!impl_) return;
diff --git a/components/rgbd-sources/src/source.cpp b/components/rgbd-sources/src/source.cpp
index 5bf3a92f2..13cdd5487 100644
--- a/components/rgbd-sources/src/source.cpp
+++ b/components/rgbd-sources/src/source.cpp
@@ -327,24 +327,6 @@ void Source::notify(int64_t ts, cv::cuda::GpuMat &c1, cv::cuda::GpuMat &c2) {
 	if (callback_) callback_(ts, c1, c2);
 }
 
-std::pair<ftl::codecs::StreamPacket, ftl::codecs::Packet> Source::make_packet(const Eigen::Matrix4d &pose) {
-	ftl::codecs::StreamPacket spkt;
-	ftl::codecs::Packet pkt;
-
-	spkt.timestamp = impl_->timestamp_;
-	spkt.channel_count = 0;
-	spkt.channel = Channel::Pose;
-	spkt.streamID = 0;
-	pkt.codec = ftl::codecs::codec_t::POSE;
-	pkt.definition = ftl::codecs::definition_t::Any;
-	pkt.block_number = 0;
-	pkt.block_total = 1;
-	pkt.flags = 0;
-	pkt.data = std::move(std::vector<uint8_t>((uint8_t*)pose.data(), (uint8_t*)pose.data() + 4*4*sizeof(double)));
-
-	return std::make_pair(spkt, pkt);
-}
-
 void Source::inject(const Eigen::Matrix4d &pose) {
 	ftl::codecs::StreamPacket spkt;
 	ftl::codecs::Packet pkt;
-- 
GitLab