From 1c07dccdcb88ec5df396f914854962031ffd2a61 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Fri, 17 Jul 2020 21:55:31 +0300
Subject: [PATCH] Code tidy of rgbd source

---
 applications/vision/src/main.cpp              |  2 +-
 .../rgbd-sources/include/ftl/rgbd/source.hpp  | 70 -------------------
 components/rgbd-sources/src/source.cpp        | 62 +---------------
 components/streams/src/feed.cpp               |  2 +-
 4 files changed, 5 insertions(+), 131 deletions(-)

diff --git a/applications/vision/src/main.cpp b/applications/vision/src/main.cpp
index 4be44e5d0..88069323f 100644
--- a/applications/vision/src/main.cpp
+++ b/applications/vision/src/main.cpp
@@ -140,7 +140,7 @@ static void run(ftl::Configurable *root) {
 		uri.to_json(root->getConfig()["source"]);
 	}
 	Source *source = nullptr;
-	source = ftl::create<Source>(root, "source", net);
+	source = ftl::create<Source>(root, "source");
 	
 	ftl::stream::Sender *sender = ftl::create<ftl::stream::Sender>(root, "sender");
 	ftl::stream::Net *outstream = ftl::create<ftl::stream::Net>(root, "stream", net);
diff --git a/components/rgbd-sources/include/ftl/rgbd/source.hpp b/components/rgbd-sources/include/ftl/rgbd/source.hpp
index 11299a536..7773756f2 100644
--- a/components/rgbd-sources/include/ftl/rgbd/source.hpp
+++ b/components/rgbd-sources/include/ftl/rgbd/source.hpp
@@ -42,7 +42,6 @@ class Source : public ftl::Configurable, public ftl::data::DiscreteSource {
 	public:
 	template <typename T, typename... ARGS>
 	friend T *ftl::config::create(ftl::config::json_t &, ARGS ...);
-	friend class VirtualSource;
 
 	// This class cannot be constructed directly, use ftl::create
 	Source()=delete;
@@ -53,7 +52,6 @@ class Source : public ftl::Configurable, public ftl::data::DiscreteSource {
 
 	protected:
 	explicit Source(ftl::config::json_t &cfg);
-	Source(ftl::config::json_t &cfg, ftl::net::Universe *net);
 
 	public:
 	virtual ~Source();
@@ -63,16 +61,6 @@ class Source : public ftl::Configurable, public ftl::data::DiscreteSource {
 	 */
 	bool isReady();
 
-	/**
-	 * Change the second channel source.
-	 */
-	[[deprecated]] bool setChannel(ftl::codecs::Channel c);
-
-	/**
-	 * Get the channel allocated to the second source.
-	 */
-	[[deprecated]] ftl::codecs::Channel getChannel() const { return channel_; }
-
 	/**
 	 * Perform the hardware or virtual frame grab operation. This should be
 	 * fast and non-blocking.
@@ -84,52 +72,15 @@ class Source : public ftl::Configurable, public ftl::data::DiscreteSource {
 	 */
 	bool retrieve(ftl::data::Frame &) override;
 
-	/**
-	 * Generate a thread job using the provided callback for the most recently
-	 * retrieved frame (matching the provided timestamp). If already busy
-	 * dispatching, returns false.
-	 */
-	bool dispatch(ftl::data::Frame &);
-
-	/**
-	 * Between frames, do any required buffer swaps.
-	 */
-	//void swap() { if (impl_) impl_->swap(); }
-
-	/**
-	 * Do any post-grab processing. This function
-	 * may take considerable time to return, especially for sources requiring
-	 * software stereo correspondance.
-	 */
-	//bool compute(int64_t ts);
-
-	//bool isVirtual() const { return impl_ == nullptr; }
-
-	/**
-	 * Change the camera extrinsics by providing a new pose matrix. For virtual
-	 * cameras this will move the camera, for physical cameras it is set by the
-	 * registration process as it attempts to work out a cameras relative pose.
-	 */
-	//[[deprecated]] virtual void setPose(const Eigen::Matrix4d &pose);
-
 	/**
 	 * Force the internal implementation to be reconstructed.
 	 */
 	void reset();
 
-	[[deprecated]] ftl::net::Universe *getNet() const { return net_; }
-
 	std::string getURI() { return value("uri", std::string("")); }
 
 	SHARED_MUTEX &mutex() { return mutex_; }
 
-	/**
-	 * Set the callback that receives decoded frames as they are generated.
-	 * There can be only a single such callback as the buffers can be swapped
-	 * by the callback.
-	 */
-	ftl::Handle onFrame(const std::function<bool(ftl::data::Frame&)> &cb);
-
 	/**
 	 * Check whether a given device URI is supported. This will check hardware
 	 * for physical availability of devices.
@@ -139,34 +90,13 @@ class Source : public ftl::Configurable, public ftl::data::DiscreteSource {
 
 	private:
 	BaseSourceImpl *impl_;
-	Eigen::Matrix4d pose_;
-	ftl::net::Universe *net_;
 	SHARED_MUTEX mutex_;
-	ftl::codecs::Channel channel_;
 	cudaStream_t stream_;
-	ftl::SingletonHandler<ftl::data::Frame&> callback_;
-	//ftl::rgbd::Frame frames_[2];
-	bool is_dispatching;
 	bool is_retrieving;
 
 	void _swap();
 };
 
-class SourceGenerator : public ftl::data::Generator {
-	public:
-	explicit SourceGenerator(Source *src) : source_(src) {}
-
-	inline ftl::Handle onFrameSet(const ftl::data::FrameSetCallback &cb) override {
-		return source_->onFrame([this,cb](ftl::data::Frame &frame) {
-			auto fs = ftl::data::FrameSet::fromFrame(frame);
-			return cb(fs);
-		});
-	}
-
-	private:
-	Source *source_;
-};
-
 }
 }
 
diff --git a/components/rgbd-sources/src/source.cpp b/components/rgbd-sources/src/source.cpp
index cb2b04eec..45018ee89 100644
--- a/components/rgbd-sources/src/source.cpp
+++ b/components/rgbd-sources/src/source.cpp
@@ -26,30 +26,12 @@ using ftl::rgbd::detail::StereoVideoSource;
 using ftl::rgbd::detail::ImageSource;
 using ftl::rgbd::detail::MiddleburySource;
 using ftl::rgbd::detail::ScreenCapture;
-using ftl::rgbd::capability_t;
 using ftl::codecs::Channel;
-//using ftl::rgbd::detail::FileSource;
 using ftl::rgbd::Camera;
 
-Source::Source(ftl::config::json_t &cfg) : Configurable(cfg), pose_(Eigen::Matrix4d::Identity()), net_(nullptr) {
+Source::Source(ftl::config::json_t &cfg) : Configurable(cfg) {
 	impl_ = nullptr;
-	//params_ = {};
 	stream_ = 0;
-	is_dispatching = false;
-	is_retrieving = false;
-	reset();
-
-	on("uri", [this](const ftl::config::Event &e) {
-		LOG(INFO) << "URI change for source: " << getURI();
-		reset();
-	});
-}
-
-Source::Source(ftl::config::json_t &cfg, ftl::net::Universe *net) : Configurable(cfg), pose_(Eigen::Matrix4d::Identity()), net_(net) {
-	impl_ = nullptr;
-	//params_ = {};
-	stream_ = 0;
-	is_dispatching = false;
 	is_retrieving = false;
 	reset();
 
@@ -142,7 +124,6 @@ static ftl::rgbd::BaseSourceImpl *createImplementation(const std::string &uristr
 
 void Source::reset() {
 	UNIQUE_LOCK(mutex_,lk);
-	channel_ = Channel::None;
 	if (impl_) delete impl_;
 	impl_ = nullptr;
 
@@ -170,7 +151,6 @@ void Source::reset() {
 }
 
 bool Source::capture(int64_t ts) {
-	//timestamp_ = ts;
 	if (impl_) return impl_->capture(ts);
 	else return true;
 }
@@ -181,43 +161,7 @@ bool Source::retrieve(ftl::data::Frame &f) {
 	bool status = false;
 	if (impl_) status = impl_->retrieve(f.cast<ftl::rgbd::Frame>());
 	is_retrieving = false;
-	return status; // && dispatch(f);
-}
-
-// TODO: Remove, unused
-bool Source::dispatch(ftl::data::Frame &f) {
-	if (!callback_) return true;
-	if (is_dispatching || is_retrieving) {
-		LOG(WARNING) << "Previous dispatch not completed";
-		return false;
-	}
-	is_dispatching = true;
-
-	/*ftl::pool.push([this, ff = std::move(f)] (int id) mutable {
-		try {
-			callback_.trigger(ff.cast<ftl::rgbd::Frame>());
-		} catch (const std::exception &e) {
-			LOG(ERROR) << "Source dispatch exception: " << e.what();
-		}
-		is_dispatching = false;
-	});*/
-	return true;
-}
-
-/*void Source::_swap() {
-	auto tmp = std::move(frames_[0]);
-	frames_[0] = std::move(frames_[1]);
-	frames_[1] = std::move(tmp);
-}*/
-
-bool Source::setChannel(ftl::codecs::Channel c) {
-	channel_ = c;
-	// FIXME:(Nick) Verify channel is supported by this source...
-	return true;
-}
-
-ftl::Handle Source::onFrame(const std::function<bool(ftl::data::Frame&)> &cb) {
-	return callback_.on(cb);
+	return status;
 }
 
 bool Source::supports(const std::string &puri) {
@@ -225,7 +169,7 @@ bool Source::supports(const std::string &puri) {
 
 	if (uri.getPathSegment(0) == "video") {
 		return StereoVideoSource::supported(uri.getPathSegment(0));
-	} else if (uri.getPathSegment(0) == "camera") {
+	} else if (uri.getPathSegment(0) == "camera" || uri.getPathSegment(0) == "stereo") {
 		return StereoVideoSource::supported(uri.getPathSegment(0));
 	} else if (uri.getPathSegment(0) == "pylon") {
 		return StereoVideoSource::supported(uri.getPathSegment(0));
diff --git a/components/streams/src/feed.cpp b/components/streams/src/feed.cpp
index 694f2353d..7a2fedd0d 100644
--- a/components/streams/src/feed.cpp
+++ b/components/streams/src/feed.cpp
@@ -645,7 +645,7 @@ uint32_t Feed::add(const std::string &path) {
 			// FIXME: pointer is deleted when removed from receiver
 			render_builders_.push_back(creator);
 		} else {
-			auto *dsource = ftl::create<ftl::rgbd::Source>(this, srcname, net_);
+			auto *dsource = ftl::create<ftl::rgbd::Source>(this, srcname);
 			devices_[fsid] = dsource;
 			source = dsource;
 			_createPipeline(fsid);
-- 
GitLab