From 4159fe63ff427dd678c557277701ad6746a12f2b Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Sat, 29 Feb 2020 10:50:38 +0200
Subject: [PATCH] Add buffer unit test

---
 components/common/cpp/src/exception.cpp   |  4 +-
 components/streams/test/receiver_unit.cpp | 75 +++++++++++++++++++++++
 2 files changed, 77 insertions(+), 2 deletions(-)

diff --git a/components/common/cpp/src/exception.cpp b/components/common/cpp/src/exception.cpp
index 81110d98e..cf74b297a 100644
--- a/components/common/cpp/src/exception.cpp
+++ b/components/common/cpp/src/exception.cpp
@@ -24,9 +24,9 @@ static std::string getBackTrace() {
     char **messages = backtrace_symbols(trace, trace_size);
 
     /* skip first stack frame (points here) */
-    for (int i=1; i<trace_size; ++i) {
+    for (int i=2; i<trace_size; ++i) {
         //printf("[bt] #%d %s\n", i, messages[i]);
-        result += string("[bt] #") + std::to_string(i) + string(" ") + messages[i];
+        result += string("[bt] #") + std::to_string(i-1) + string(" ") + messages[i] + string("\n");
     }
 	return result;
 
diff --git a/components/streams/test/receiver_unit.cpp b/components/streams/test/receiver_unit.cpp
index 56b1c64f7..2aadaa8e4 100644
--- a/components/streams/test/receiver_unit.cpp
+++ b/components/streams/test/receiver_unit.cpp
@@ -331,3 +331,78 @@ TEST_CASE( "Receiver sync bugs" ) {
 	//while (ftl::pool.n_idle() != ftl::pool.size()) std::this_thread::sleep_for(std::chrono::milliseconds(10));
 	delete receiver;
 }
+
+TEST_CASE( "Receiver non zero buffer" ) {
+	json_t global = json_t{{"$id","ftl://test"}};
+	ftl::config::configure(global);
+
+	json_t cfg = json_t{
+		{"$id","ftl://test/1"}
+	};
+	auto *receiver = ftl::create<Receiver>(cfg);
+
+	json_t cfg2 = json_t{
+		{"$id","ftl://test/2"}
+	};
+	TestStream stream(cfg2);
+	receiver->setStream(&stream);
+	receiver->set("frameset_buffer_size", 1);
+
+	ftl::codecs::NvPipeEncoder encoder(definition_t::HD1080, definition_t::SD480);
+
+	ftl::codecs::Packet pkt;
+	pkt.codec = codec_t::Any;
+	pkt.bitrate = 255;
+	pkt.definition = definition_t::Any;
+	pkt.flags = 0;
+	pkt.frame_count = 1;
+
+	ftl::codecs::StreamPacket spkt;
+	spkt.version = 4;
+	spkt.timestamp = 10;
+	spkt.frame_number = 0;
+	spkt.channel = Channel::Colour;
+	spkt.streamID = 0;
+
+	ftl::rgbd::Frame dummy;
+	ftl::rgbd::FrameState state;
+	state.getLeft().width = 1280;
+	state.getLeft().height = 720;
+	dummy.setOrigin(&state);
+	ftl::stream::injectCalibration(&stream, dummy, 0, 0, 0);
+
+	ftl::timer::start(false);
+
+	SECTION("Fixed buffer delay") {
+		cv::cuda::GpuMat m(cv::Size(1280,720), CV_8UC4, cv::Scalar(0));
+
+		bool r = encoder.encode(m, pkt);
+		REQUIRE( r );
+
+		int count = 0;
+		receiver->onFrameSet([&count](ftl::rgbd::FrameSet &fs) {
+			++count;
+
+			REQUIRE( fs.timestamp == 10 );
+			REQUIRE( fs.frames.size() == 1 );
+			REQUIRE( fs.frames[0].hasChannel(Channel::Colour) );
+			REQUIRE( fs.frames[0].get<cv::cuda::GpuMat>(Channel::Colour).rows == 720 );
+			REQUIRE( fs.frames[0].get<cv::cuda::GpuMat>(Channel::Colour).type() == CV_8UC4 );
+
+			return true;
+		});
+
+		stream.post(spkt, pkt);
+		spkt.timestamp += 10;
+		stream.post(spkt, pkt);
+
+		int i=10;
+		while (i-- > 0 && count < 1) std::this_thread::sleep_for(std::chrono::milliseconds(10));
+
+		REQUIRE( count == 1 );
+	}
+
+	ftl::timer::stop(true);
+	//while (ftl::pool.n_idle() != ftl::pool.size()) std::this_thread::sleep_for(std::chrono::milliseconds(10));
+	delete receiver;
+}
-- 
GitLab