From 2385a206dc1a6d254b12bdb4aafe34ea3ca7f7e9 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Mon, 6 Jul 2020 09:24:18 +0300
Subject: [PATCH] Some fixes for pylon and vision

---
 applications/vision/src/main.cpp                         | 7 ++++++-
 .../rgbd-sources/src/sources/stereovideo/pylon.cpp       | 4 ++--
 .../rgbd-sources/src/sources/stereovideo/stereovideo.cpp | 9 +++++++--
 components/streams/src/sender.cpp                        | 7 ++++++-
 components/structures/src/new_frame.cpp                  | 8 ++++++++
 5 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/applications/vision/src/main.cpp b/applications/vision/src/main.cpp
index 82da61dcf..d6c8e3eb0 100644
--- a/applications/vision/src/main.cpp
+++ b/applications/vision/src/main.cpp
@@ -163,7 +163,12 @@ static void run(ftl::Configurable *root) {
 	// Send channels on flush
 	auto flushhandle = pool.onFlushSet([sender](ftl::data::FrameSet &fs, ftl::codecs::Channel c) {
 		// TODO: Check the channel to see if it should be sent or not
-		sender->post(fs, c);
+		switch (c) {
+		case Channel::Colour	:
+		case Channel::Colour2	:
+		case Channel::Depth		: sender->post(fs, c); break;
+		default: break;
+		}
 		return true;
 	});
 
diff --git a/components/rgbd-sources/src/sources/stereovideo/pylon.cpp b/components/rgbd-sources/src/sources/stereovideo/pylon.cpp
index b8155489e..7bec885a7 100644
--- a/components/rgbd-sources/src/sources/stereovideo/pylon.cpp
+++ b/components/rgbd-sources/src/sources/stereovideo/pylon.cpp
@@ -182,7 +182,7 @@ bool PylonDevice::get(cv::cuda::GpuMat &l_out, cv::cuda::GpuMat &r_out, cv::cuda
 				CV_8UC1,
 				(uint8_t*)result_right->GetBuffer());
 
-				cv::cvtColor(wrap_right, rfull, cv::COLOR_BayerBG2BGRA);
+				cv::cvtColor(wrap_right, rfull, cv::COLOR_BayerRG2BGRA);
 
 				if (isStereo()) {
 					c->rectifyRight(rfull);
@@ -218,7 +218,7 @@ bool PylonDevice::get(cv::cuda::GpuMat &l_out, cv::cuda::GpuMat &r_out, cv::cuda
 			CV_8UC1,
 			(uint8_t*)result_left->GetBuffer());
 
-		cv::cvtColor(wrap_left, lfull, cv::COLOR_BayerBG2BGRA);
+		cv::cvtColor(wrap_left, lfull, cv::COLOR_BayerRG2BGRA);
 
 		if (isStereo()) {
 			c->rectifyLeft(lfull);
diff --git a/components/rgbd-sources/src/sources/stereovideo/stereovideo.cpp b/components/rgbd-sources/src/sources/stereovideo/stereovideo.cpp
index 1fcd53110..191c5ed83 100644
--- a/components/rgbd-sources/src/sources/stereovideo/stereovideo.cpp
+++ b/components/rgbd-sources/src/sources/stereovideo/stereovideo.cpp
@@ -329,12 +329,17 @@ bool StereoVideoSource::retrieve(ftl::rgbd::Frame &frame) {
 	if (lsrc_->isStereo()) {
 		cv::cuda::GpuMat &left = frame.create<cv::cuda::GpuMat>(Channel::Left);
 		cv::cuda::GpuMat &right = frame.create<cv::cuda::GpuMat>(Channel::Right);
-		lsrc_->get(left, right, hres, hres_r, calib_, stream2_);
+		if (!lsrc_->get(left, right, hres, hres_r, calib_, stream2_)) {
+			frame.remove(Channel::Left);
+			frame.remove(Channel::Right);
+		}
 	}
 	else {
 		cv::cuda::GpuMat &left = frame.create<cv::cuda::GpuMat>(Channel::Left);
 		cv::cuda::GpuMat right;
-		lsrc_->get(left, right, hres, hres_r, calib_, stream2_);
+		if (!lsrc_->get(left, right, hres, hres_r, calib_, stream2_)) {
+			frame.remove(Channel::Left);
+		}
 	}
 
 	//LOG(INFO) << "Channel size: " << hres.size();
diff --git a/components/streams/src/sender.cpp b/components/streams/src/sender.cpp
index 4a294c697..6bf1a4fd3 100644
--- a/components/streams/src/sender.cpp
+++ b/components/streams/src/sender.cpp
@@ -397,6 +397,11 @@ void Sender::_encodeVideoChannel(ftl::data::FrameSet &fs, Channel c, bool reset)
 		//	fs.frames[offset].upload(cc);
 		//}
 
+		if (!fs.frames[offset].hasChannel(cc)) {
+			offset++;
+			continue;
+		}
+
 		StreamPacket spkt;
 		spkt.version = 4;
 		spkt.timestamp = fs.timestamp();
@@ -464,7 +469,7 @@ void Sender::_encodeVideoChannel(ftl::data::FrameSet &fs, Channel c, bool reset)
 					cv::imshow("Test", tmp);
 					cv::waitKey(1);*/
 				} else {
-					LOG(ERROR) << "Encoding failed";
+					LOG(ERROR) << "Encoding failed for channel " << (int)cc;
 				}
 			} catch (std::exception &e) {
 				LOG(ERROR) << "Exception in encoder: " << e.what();
diff --git a/components/structures/src/new_frame.cpp b/components/structures/src/new_frame.cpp
index 3ce0238b1..3181e31ee 100644
--- a/components/structures/src/new_frame.cpp
+++ b/components/structures/src/new_frame.cpp
@@ -89,6 +89,14 @@ bool ftl::data::Frame::has(ftl::codecs::Channel c) const {
 	}
 }
 
+void ftl::data::Frame::remove(ftl::codecs::Channel c) {
+	const auto &i = data_.find(c);
+	if (i != data_.end()) {
+		i->second.status = ftl::data::ChannelStatus::INVALID;
+		changed_.erase(c);
+	}
+}
+
 Frame::ChannelData &Frame::_getData(ftl::codecs::Channel c) {
 	if (status_ == FrameStatus::RELEASED) throw FTL_Error("Reading a released frame");
 	const auto &i = data_.find(c);
-- 
GitLab