From 4be759b653e73d4430d150ab1e834484f0840c0a Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Sat, 6 Jun 2020 20:18:46 +0300
Subject: [PATCH] Abstract local source

---
 .../src/sources/stereovideo/device.hpp        | 55 +++++++++++++++++++
 .../src/sources/stereovideo/local.cpp         | 21 ++++---
 .../src/sources/stereovideo/local.hpp         | 44 +++++----------
 .../src/sources/stereovideo/stereovideo.cpp   | 16 ++++--
 .../src/sources/stereovideo/stereovideo.hpp   |  4 +-
 5 files changed, 93 insertions(+), 47 deletions(-)
 create mode 100644 components/rgbd-sources/src/sources/stereovideo/device.hpp

diff --git a/components/rgbd-sources/src/sources/stereovideo/device.hpp b/components/rgbd-sources/src/sources/stereovideo/device.hpp
new file mode 100644
index 000000000..7a7fcb45b
--- /dev/null
+++ b/components/rgbd-sources/src/sources/stereovideo/device.hpp
@@ -0,0 +1,55 @@
+#ifndef _FTL_RGBD_STEREOVIDEO_DEVICE_HPP_
+#define _FTL_RGBD_STEREOVIDEO_DEVICE_HPP_
+
+#include <ftl/configurable.hpp>
+#include <ftl/cuda_common.hpp>
+#include <string>
+
+namespace ftl {
+namespace rgbd {
+namespace detail {
+
+class Calibrate;
+
+struct DeviceDetails {
+	std::string name;
+	int id;
+	size_t maxwidth;
+	size_t maxheight;
+};
+
+/**
+ * Abstract base class for camera or stereo camera sources. Just wraps the
+ * basic grab and retrieve functionality with rectification.
+ * 
+ * @see OpenCVDevice
+ * @see PylonDevice
+ */
+class Device : public Configurable {
+	public:
+	explicit Device(nlohmann::json &config);
+	virtual ~Device();
+
+	//virtual const std::vector<DeviceDetails> &listDevices()=0;
+
+	virtual bool grab()=0;
+	virtual bool get(cv::cuda::GpuMat &l, cv::cuda::GpuMat &r, cv::cuda::GpuMat &h_l, cv::Mat &h_r, Calibrate *c, cv::cuda::Stream &stream)=0;
+
+	virtual unsigned int width() const =0;
+	virtual unsigned int height() const =0;
+
+	virtual unsigned int fullWidth() const =0;
+	virtual unsigned int fullHeight() const =0;
+
+	inline bool hasHigherRes() const { return fullWidth() != width(); }
+	
+	virtual double getTimestamp() const =0;
+	
+	virtual bool isStereo() const =0;
+};
+
+}
+}
+}
+
+#endif
\ No newline at end of file
diff --git a/components/rgbd-sources/src/sources/stereovideo/local.cpp b/components/rgbd-sources/src/sources/stereovideo/local.cpp
index a6f401748..7b01d33f2 100644
--- a/components/rgbd-sources/src/sources/stereovideo/local.cpp
+++ b/components/rgbd-sources/src/sources/stereovideo/local.cpp
@@ -30,7 +30,7 @@
 #pragma comment(lib, "mfuuid.lib")
 #endif
 
-using ftl::rgbd::detail::LocalSource;
+using ftl::rgbd::detail::OpenCVDevice;
 using ftl::rgbd::detail::Calibrate;
 using cv::Mat;
 using cv::VideoCapture;
@@ -42,8 +42,8 @@ using std::chrono::high_resolution_clock;
 using std::chrono::milliseconds;
 using std::this_thread::sleep_for;
 
-LocalSource::LocalSource(nlohmann::json &config)
-		: Configurable(config), timestamp_(0.0) {
+OpenCVDevice::OpenCVDevice(nlohmann::json &config)
+		: ftl::rgbd::detail::Device(config), timestamp_(0.0) {
 
 	std::vector<ftl::rgbd::detail::DeviceDetails> devices = _selectDevices();
 
@@ -142,12 +142,11 @@ LocalSource::LocalSource(nlohmann::json &config)
 	hres_hm_ = cv::cuda::HostMem(height_, width_, CV_8UC4);
 }
 
-LocalSource::LocalSource(nlohmann::json &config, const string &vid)
-	:	Configurable(config), timestamp_(0.0) {
-	LOG(FATAL) << "Stereo video file sources no longer supported";
+OpenCVDevice::~OpenCVDevice() {
+
 }
 
-std::vector<ftl::rgbd::detail::DeviceDetails> LocalSource::_selectDevices() {
+std::vector<ftl::rgbd::detail::DeviceDetails> OpenCVDevice::_selectDevices() {
 	std::vector<ftl::rgbd::detail::DeviceDetails> devices;
 
 #ifdef WIN32
@@ -295,7 +294,7 @@ std::vector<ftl::rgbd::detail::DeviceDetails> LocalSource::_selectDevices() {
 }
 
 
-bool LocalSource::grab() {
+bool OpenCVDevice::grab() {
 	if (!camera_a_) return false;
 
 	if (camera_b_) {
@@ -312,7 +311,7 @@ bool LocalSource::grab() {
 	return true;
 }
 
-bool LocalSource::get(cv::cuda::GpuMat &l_out, cv::cuda::GpuMat &r_out,
+bool OpenCVDevice::get(cv::cuda::GpuMat &l_out, cv::cuda::GpuMat &r_out,
 	cv::cuda::GpuMat &l_hres_out, cv::Mat &r_hres_out, Calibrate *c, cv::cuda::Stream &stream) {
 	
 	Mat l, r ,hres;
@@ -410,11 +409,11 @@ bool LocalSource::get(cv::cuda::GpuMat &l_out, cv::cuda::GpuMat &r_out,
 	return true;
 }
 
-double LocalSource::getTimestamp() const {
+double OpenCVDevice::getTimestamp() const {
 	return timestamp_;
 }
 
-bool LocalSource::isStereo() const {
+bool OpenCVDevice::isStereo() const {
 	return stereo_ && !nostereo_;
 }
 
diff --git a/components/rgbd-sources/src/sources/stereovideo/local.hpp b/components/rgbd-sources/src/sources/stereovideo/local.hpp
index 243df4c47..1db067b8d 100644
--- a/components/rgbd-sources/src/sources/stereovideo/local.hpp
+++ b/components/rgbd-sources/src/sources/stereovideo/local.hpp
@@ -1,9 +1,7 @@
 #ifndef _FTL_LOCAL_HPP_
 #define _FTL_LOCAL_HPP_
 
-#include <ftl/configurable.hpp>
-#include <ftl/cuda_common.hpp>
-#include <string>
+#include "device.hpp"
 
 namespace cv {
 	class Mat;
@@ -14,39 +12,25 @@ namespace ftl {
 namespace rgbd {
 namespace detail {
 
-class Calibrate;
-
-struct DeviceDetails {
-	std::string name;
-	int id;
-	size_t maxwidth;
-	size_t maxheight;
-};
-
-class LocalSource : public Configurable {
+class OpenCVDevice : public ftl::rgbd::detail::Device {
 	public:
-	explicit LocalSource(nlohmann::json &config);
-	LocalSource(nlohmann::json &config, const std::string &vid);
-	
-	//bool left(cv::Mat &m);
-	//bool right(cv::Mat &m);
-	bool grab();
-	bool get(cv::cuda::GpuMat &l, cv::cuda::GpuMat &r, cv::cuda::GpuMat &h_l, cv::Mat &h_r, Calibrate *c, cv::cuda::Stream &stream);
+	explicit OpenCVDevice(nlohmann::json &config);
+	~OpenCVDevice();
 
-	unsigned int width() const { return dwidth_; }
-	unsigned int height() const { return dheight_; }
+	static std::vector<DeviceDetails> listDevices();
+	
+	bool grab() override;
+	bool get(cv::cuda::GpuMat &l, cv::cuda::GpuMat &r, cv::cuda::GpuMat &h_l, cv::Mat &h_r, Calibrate *c, cv::cuda::Stream &stream) override;
 
-	unsigned int fullWidth() const { return width_; }
-	unsigned int fullHeight() const { return height_; }
+	unsigned int width() const override { return dwidth_; }
+	unsigned int height() const override { return dheight_; }
 
-	inline bool hasHigherRes() const { return dwidth_ != width_; }
-	
-	//void setFramerate(float fps);
-	//float getFramerate() const;
+	unsigned int fullWidth() const override { return width_; }
+	unsigned int fullHeight() const override { return height_; }
 	
-	double getTimestamp() const;
+	double getTimestamp() const override;
 	
-	bool isStereo() const;
+	bool isStereo() const override;
 	
 	private:
 	double timestamp_;
diff --git a/components/rgbd-sources/src/sources/stereovideo/stereovideo.cpp b/components/rgbd-sources/src/sources/stereovideo/stereovideo.cpp
index dd4e2dc3e..aeff9c431 100644
--- a/components/rgbd-sources/src/sources/stereovideo/stereovideo.cpp
+++ b/components/rgbd-sources/src/sources/stereovideo/stereovideo.cpp
@@ -28,11 +28,19 @@
 #include "disparity.hpp"
 
 using ftl::rgbd::detail::Calibrate;
-using ftl::rgbd::detail::LocalSource;
+using ftl::rgbd::detail::OpenCVDevice;
 using ftl::rgbd::detail::StereoVideoSource;
 using ftl::codecs::Channel;
 using std::string;
 
+ftl::rgbd::detail::Device::Device(nlohmann::json &config) : Configurable(config) {
+
+}
+
+ftl::rgbd::detail::Device::~Device() {
+
+}
+
 StereoVideoSource::StereoVideoSource(ftl::rgbd::Source *host)
 		: ftl::rgbd::detail::Source(host), ready_(false) {
 	init("");
@@ -55,7 +63,7 @@ void StereoVideoSource::init(const string &file) {
 	if (ftl::is_video(file)) {
 		// Load video file
 		LOG(INFO) << "Using video file...";
-		lsrc_ = ftl::create<LocalSource>(host_, "feed", file);
+		//lsrc_ = ftl::create<LocalSource>(host_, "feed", file);
 	} else if (ftl::is_directory(file)) {
 		// FIXME: This is not an ideal solution...
 		ftl::config::addPath(file);
@@ -65,13 +73,13 @@ void StereoVideoSource::init(const string &file) {
 			LOG(FATAL) << "No video.mp4 file found in provided paths (" << file << ")";
 		} else {
 			LOG(INFO) << "Using test directory...";
-			lsrc_ = ftl::create<LocalSource>(host_, "feed", *vid);
+			//lsrc_ = ftl::create<LocalSource>(host_, "feed", *vid);
 		}
 	}
 	else {
 		// Use cameras
 		LOG(INFO) << "Using cameras...";
-		lsrc_ = ftl::create<LocalSource>(host_, "feed");
+		lsrc_ = ftl::create<OpenCVDevice>(host_, "feed");
 	}
 
 	color_size_ = cv::Size(lsrc_->width(), lsrc_->height());
diff --git a/components/rgbd-sources/src/sources/stereovideo/stereovideo.hpp b/components/rgbd-sources/src/sources/stereovideo/stereovideo.hpp
index 52ec6f393..fba038df6 100644
--- a/components/rgbd-sources/src/sources/stereovideo/stereovideo.hpp
+++ b/components/rgbd-sources/src/sources/stereovideo/stereovideo.hpp
@@ -11,7 +11,7 @@ namespace ftl {
 namespace rgbd {
 namespace detail {
 
-class LocalSource;
+class Device;
 class Calibrate;
 class Disparity;
 
@@ -36,7 +36,7 @@ class StereoVideoSource : public detail::Source {
 	private:
 	void updateParameters();
 
-	LocalSource *lsrc_;
+	Device *lsrc_;
 	Calibrate *calib_;
 	int64_t capts_;
 
-- 
GitLab