From 4028afaf3cb9adf54420a6ab578b01769e547e24 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Fri, 24 Jan 2020 08:52:11 +0200
Subject: [PATCH] Bug fix for builder callback

---
 components/rgbd-sources/src/frameset.cpp  | 9 +++++++++
 components/streams/src/receiver.cpp       | 1 +
 components/streams/test/receiver_unit.cpp | 2 ++
 3 files changed, 12 insertions(+)

diff --git a/components/rgbd-sources/src/frameset.cpp b/components/rgbd-sources/src/frameset.cpp
index 8064aaff0..a2d3f57da 100644
--- a/components/rgbd-sources/src/frameset.cpp
+++ b/components/rgbd-sources/src/frameset.cpp
@@ -124,6 +124,15 @@ size_t Builder::size() {
 }
 
 void Builder::onFrameSet(const std::function<bool(ftl::rgbd::FrameSet &)> &cb) {
+	if (!cb) {
+		main_id_.cancel();
+		return;
+	}
+
+	if (main_id_.id() != -1) {
+		main_id_.cancel();
+	}
+
 	// 3. Issue IO retrieve ad compute jobs before finding a valid
 	// frame at required latency to pass to callback.
 	main_id_ = ftl::timer::add(ftl::timer::kTimerMain, [this,cb](int64_t ts) {
diff --git a/components/streams/src/receiver.cpp b/components/streams/src/receiver.cpp
index cba34010b..88bb1e858 100644
--- a/components/streams/src/receiver.cpp
+++ b/components/streams/src/receiver.cpp
@@ -25,6 +25,7 @@ Receiver::~Receiver() {
 	//if (stream_) {
 	//	stream_->onPacket(nullptr);
 	//}
+
 	builder_.onFrameSet(nullptr);
 }
 
diff --git a/components/streams/test/receiver_unit.cpp b/components/streams/test/receiver_unit.cpp
index 86ce60b6c..faee6add4 100644
--- a/components/streams/test/receiver_unit.cpp
+++ b/components/streams/test/receiver_unit.cpp
@@ -215,5 +215,7 @@ TEST_CASE( "Receiver generating onFrameSet" ) {
 	}
 
 	ftl::timer::stop(true);
+	//while (ftl::pool.n_idle() != ftl::pool.size()) std::this_thread::sleep_for(std::chrono::milliseconds(10));
 	delete receiver;
+	//ftl::config::cleanup();
 }
-- 
GitLab