From 9601cf0cb8a97fe3afb0e82b79c2bbeb93989033 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Sun, 2 Jun 2019 13:09:25 +0300
Subject: [PATCH] Use correct framerate

---
 .../include/ftl/rgbd_streamer.hpp             |  2 +-
 components/rgbd-sources/src/rgbd_streamer.cpp | 32 +++++++++++++++----
 2 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/components/rgbd-sources/include/ftl/rgbd_streamer.hpp b/components/rgbd-sources/include/ftl/rgbd_streamer.hpp
index 98409b79a..8eb7460c7 100644
--- a/components/rgbd-sources/include/ftl/rgbd_streamer.hpp
+++ b/components/rgbd-sources/include/ftl/rgbd_streamer.hpp
@@ -49,7 +49,7 @@ class Streamer : public ftl::Configurable {
 	void remove(RGBDSource *);
 	void remove(const std::string &);
 
-	void run();
+	void run(bool block=false);
 	void stop();
 
 	RGBDSource *get(const std::string &uri);
diff --git a/components/rgbd-sources/src/rgbd_streamer.cpp b/components/rgbd-sources/src/rgbd_streamer.cpp
index 6048df87b..b4e256169 100644
--- a/components/rgbd-sources/src/rgbd_streamer.cpp
+++ b/components/rgbd-sources/src/rgbd_streamer.cpp
@@ -83,7 +83,7 @@ void Streamer::add(RGBDSource *src) {
 	sources_[src->getURI()] = s;
 }
 
-void Streamer::_addClient(const string &source, int N, int rate, const UUID &peer, const string &dest) {
+void Streamer::_addClient(const string &source, int N, int rate, const ftl::UUID &peer, const string &dest) {
 	shared_lock<shared_mutex> slk(mutex_);
 	if (sources_.find(source) == sources_.end()) return;
 
@@ -113,19 +113,37 @@ void Streamer::stop() {
 	active_ = false;
 }
 
-void Streamer::run() {
+void Streamer::run(bool block) {
 	active_ = true;
 
-	// Create thread job for frame ticking
-	pool_.push([this](int id) {
+	if (block) {
 		while (active_) {
+			double wait = 1.0f / 25.0f;
+			auto start = std::chrono::high_resolution_clock::now();
 			// Create frame jobs at correct FPS interval
 			_schedule();
 
-			// TODO Proper time control
-			sleep_for(milliseconds(40));
+			std::chrono::duration<double> elapsed =
+				std::chrono::high_resolution_clock::now() - start;
+
+			sleep_for(milliseconds((long long)((wait - elapsed.count()) * 1000.0f)));
 		}
-	});
+	} else {
+		// Create thread job for frame ticking
+		pool_.push([this](int id) {
+			while (active_) {
+				double wait = 1.0f / 25.0f;
+				auto start = std::chrono::high_resolution_clock::now();
+				// Create frame jobs at correct FPS interval
+				_schedule();
+
+				std::chrono::duration<double> elapsed =
+					std::chrono::high_resolution_clock::now() - start;
+
+				sleep_for(milliseconds((long long)((wait - elapsed.count()) * 1000.0f)));
+			}
+		});
+	}
 }
 
 void Streamer::_swap(StreamSource &src) {
-- 
GitLab