From 06bda4c399ab97aa9503475f8d17b81661083472 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Fri, 14 Aug 2020 21:28:14 +0300
Subject: [PATCH] Fix for missing middle tile

---
 components/streams/src/sender.cpp       |  6 ++---
 components/streams/test/sender_unit.cpp | 31 +++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/components/streams/src/sender.cpp b/components/streams/src/sender.cpp
index 7429bce94..ebd778a32 100644
--- a/components/streams/src/sender.cpp
+++ b/components/streams/src/sender.cpp
@@ -773,12 +773,12 @@ int Sender::_generateTiles(const ftl::rgbd::FrameSet &fs, int offset, Channel c,
 
 	// Loop over tiles with ROI mats and do colour conversions.
 	while (tilecount > 0 && count+offset < fs.frames.size()) {
-		if (fs.hasFrame(offset+count)) {
-			cframe = &fs.frames[offset+count];
+		cframe = &fs.frames[offset+count];
+
+		if (fs.hasFrame(offset+count) && cframe->hasChannel(c)) {	
 			auto &m = cframe->get<cv::cuda::GpuMat>(c);
 			cv::Rect roi((count % tx)*rwidth, (count / tx)*rheight, (stereo) ? rwidth/2 : rwidth, rheight);
 			cv::cuda::GpuMat sroi = surface.surface(roi);
-
 			m.copyTo(sroi, stream);
 		} else {
 			cv::Rect roi((count % tx)*rwidth, (count / tx)*rheight, rwidth, rheight);
diff --git a/components/streams/test/sender_unit.cpp b/components/streams/test/sender_unit.cpp
index f16abf8ad..753fde010 100644
--- a/components/streams/test/sender_unit.cpp
+++ b/components/streams/test/sender_unit.cpp
@@ -220,6 +220,37 @@ TEST_CASE( "Sender::post() video frames" ) {
 		REQUIRE( ftl::codecs::hevc::validNAL(pkt.data.data(), pkt.data.size()) );
 	}
 
+	SECTION("4 depth frames tiled, missing middle") {
+		stream.select(0, {Channel::Depth}, true);
+
+		fs.resize(4);
+
+		fs.mask = 0xF;
+		fs.frames[0].create<cv::cuda::GpuMat>(Channel::Depth).create(cv::Size(1280,720), CV_32F);
+		fs.frames[0].set<cv::cuda::GpuMat>(Channel::Depth).setTo(cv::Scalar(0.0f));
+
+		for (int i=1; i<4; ++i) {
+			fs.frames[i].store();
+			if (i == 3) continue;
+			fs.frames[i].create<cv::cuda::GpuMat>(Channel::Depth).create(cv::Size(1280,720), CV_32F);
+			fs.frames[i].set<cv::cuda::GpuMat>(Channel::Depth).setTo(cv::Scalar(0.0f));
+		}
+
+		sender->post(fs, Channel::Depth);
+
+		REQUIRE( count == 1 );
+		REQUIRE( spkt.version == 5 );
+		REQUIRE( spkt.timestamp == 1000 );
+		REQUIRE( (int)spkt.frame_number == 0 );
+		REQUIRE( spkt.streamID == 0 );
+		REQUIRE( spkt.channel == Channel::Depth );
+		REQUIRE( pkt.codec == codec_t::HEVC );
+		REQUIRE( pkt.data.size() > 0 );
+		REQUIRE( pkt.flags == (ftl::codecs::kFlagFloat | ftl::codecs::kFlagMappedDepth) );
+		REQUIRE( pkt.frame_count == 4 );
+		REQUIRE( ftl::codecs::hevc::validNAL(pkt.data.data(), pkt.data.size()) );
+	}
+
 	SECTION("two lossless depth frames tiled") {
 		stream.select(0, {Channel::Depth}, true);
 
-- 
GitLab