From 19a387b1f64a53f1dc955f378c42ab39da441aab Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Sun, 5 Jul 2020 15:34:56 +0300
Subject: [PATCH] Use thread to do pipeline in vision

---
 applications/vision/src/main.cpp | 44 ++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 17 deletions(-)

diff --git a/applications/vision/src/main.cpp b/applications/vision/src/main.cpp
index f567fa80f..a75f9cd64 100644
--- a/applications/vision/src/main.cpp
+++ b/applications/vision/src/main.cpp
@@ -179,28 +179,38 @@ static void run(ftl::Configurable *root) {
 	pipeline->append<ftl::operators::ArUco>("aruco")->value("enabled", false);
 	pipeline->append<ftl::operators::DepthChannel>("depth");  // Ensure there is a depth channel
 
-	auto h = creator.onFrameSet([sender,&stats_count,&latency,&frames,pipeline](const ftl::data::FrameSetPtr &fs) {
-		// Make a frameset first
-		//auto fs = ftl::data::FrameSet::fromFrame(f);
+	bool busy = false;
 
-		// Lock and send colour right now to encode in parallel
+	auto h = creator.onFrameSet([sender,&stats_count,&latency,&frames,pipeline,&busy](const ftl::data::FrameSetPtr &fs) {
+		if (busy) return true;
+		busy = true;
+
+		// Lock colour right now to encode in parallel
 		fs->flush(ftl::codecs::Channel::Colour);
-		sender->post(*fs, ftl::codecs::Channel::Colour);
 
-		// Do pipeline here...
-		pipeline->apply(*fs, *fs);
-		if (fs->firstFrame().has(ftl::codecs::Channel::Depth)) sender->post(*fs, ftl::codecs::Channel::Depth);
+		// Do all processing in another thread...
+		ftl::pool.push([sender,&stats_count,&latency,&frames,pipeline,&busy,fs](int id) {
+			// Do pipeline here...
+			pipeline->apply(*fs, *fs);
+			if (fs->firstFrame().has(ftl::codecs::Channel::Depth)) sender->post(*fs, ftl::codecs::Channel::Depth);
+
+			++frames;
+			latency += float(ftl::timer::get_time() - fs->timestamp());
+
+			if (--stats_count <= 0) {
+				latency /= float(frames);
+				LOG(INFO) << "Frame rate: " << frames << ", Latency: " << latency;
+				stats_count = 20;
+				frames = 0;
+				latency = 0.0f;
+			}
 
-		++frames;
-		latency += float(ftl::timer::get_time() - fs->timestamp());
+			busy = false;
+		});
+
+		// Actually send colour in this original thread
+		sender->post(*fs, ftl::codecs::Channel::Colour);
 
-		if (--stats_count <= 0) {
-			latency /= float(frames);
-			LOG(INFO) << "Frame rate: " << frames << ", Latency: " << latency;
-			stats_count = 20;
-			frames = 0;
-			latency = 0.0f;
-		}
 		return true;
 	});
 
-- 
GitLab