From 789d7cdf5d160e1723e9d5346147fc689a1fcd58 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nicolas.pope@utu.fi>
Date: Thu, 5 Dec 2019 15:12:33 +0200
Subject: [PATCH] Resolves #260 scene recording

---
 applications/reconstruct/src/main.cpp         | 29 +++++++++----------
 .../reconstruct/src/reconstruction.cpp        |  4 +++
 .../reconstruct/src/reconstruction.hpp        |  2 ++
 3 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/applications/reconstruct/src/main.cpp b/applications/reconstruct/src/main.cpp
index c4905e1e5..41be18a13 100644
--- a/applications/reconstruct/src/main.cpp
+++ b/applications/reconstruct/src/main.cpp
@@ -284,21 +284,24 @@ static void run(ftl::Configurable *root) {
 	stream->add(vs);
 
 	// ---- Recording code -----------------------------------------------------
-	/*
 	std::ofstream fileout;
 	ftl::codecs::Writer writer(fileout);
-	auto recorder = [&writer,&groups](ftl::rgbd::Source *src, const ftl::codecs::StreamPacket &spkt, const ftl::codecs::Packet &pkt) {
-		ftl::codecs::StreamPacket s = spkt;
-
-		// Patch stream ID to match order in group
-		s.streamID = groups[0]->streamID(src);
-		writer.write(s, pkt);
-	};
 
 	root->set("record", false);
 
+	// Add a recording callback to all reconstruction scenes
+	for (size_t i=0; i<sources.size(); ++i) {
+		sources[i]->addRawCallback([&writer,&groups,i](ftl::rgbd::Source *src, const ftl::codecs::StreamPacket &spkt, const ftl::codecs::Packet &pkt) {
+			ftl::codecs::StreamPacket s = spkt;
+
+			// Patch stream ID to match order in group
+			s.streamID = i;
+			writer.write(s, pkt);
+		});
+	}
+
 	// Allow stream recording
-	root->on("record", [&groups,&fileout,&writer,&recorder](const ftl::config::Event &e) {
+	root->on("record", [&groups,&fileout,&writer,&sources](const ftl::config::Event &e) {
 		if (e.entity->value("record", false)) {
 			char timestamp[18];
 			std::time_t t=std::time(NULL);
@@ -306,24 +309,20 @@ static void run(ftl::Configurable *root) {
 			fileout.open(std::string(timestamp) + ".ftl");
 
 			writer.begin();
-			// TODO: Add to all grounds / reconstructions
-			groups[0]->addRawCallback(std::function(recorder));
 
 			// TODO: Write pose+calibration+config packets
-			auto sources = groups[0]->sources();
+
 			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()); 
 			}
 		} else {
-			// FIXME: Remove doesn't work, so multiple records fail.
-			groups[0]->removeRawCallback(recorder);
 			writer.end();
 			fileout.close();
 		}
 	});
-	*/
+
 	// -------------------------------------------------------------------------
 
 	stream->setLatency(6);  // FIXME: This depends on source!?
diff --git a/applications/reconstruct/src/reconstruction.cpp b/applications/reconstruct/src/reconstruction.cpp
index a2aa11093..c9ce1bf89 100644
--- a/applications/reconstruct/src/reconstruction.cpp
+++ b/applications/reconstruct/src/reconstruction.cpp
@@ -82,6 +82,10 @@ void Reconstruction::addSource(ftl::rgbd::Source *src) {
 	group_->addSource(src); // TODO: check if source is already in group?
 }
 
+void Reconstruction::addRawCallback(const std::function<void(ftl::rgbd::Source *src, const ftl::codecs::StreamPacket &spkt, const ftl::codecs::Packet &pkt)> &cb) {
+	group_->addRawCallback(cb);
+}
+
 void Reconstruction::render(ftl::rgbd::VirtualSource *vs, ftl::rgbd::Frame &out) {
 	// Create scene transform, intended for axis aligning the walls and floor
 	Eigen::Matrix4d transform;
diff --git a/applications/reconstruct/src/reconstruction.hpp b/applications/reconstruct/src/reconstruction.hpp
index 340e65bf2..6546f85c1 100644
--- a/applications/reconstruct/src/reconstruction.hpp
+++ b/applications/reconstruct/src/reconstruction.hpp
@@ -18,6 +18,8 @@ class Reconstruction : public ftl::Configurable {
 
 	void addSource(ftl::rgbd::Source *);
 
+	void addRawCallback(const std::function<void(ftl::rgbd::Source *src, const ftl::codecs::StreamPacket &spkt, const ftl::codecs::Packet &pkt)> &cb);
+
 	/**
 	 * Do the render for a specified virtual camera.
 	 */
-- 
GitLab