From 61da84fcb9cbbcdcaf39c41a57c36c532339c4ff Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Thu, 13 Feb 2020 11:04:50 +0200
Subject: [PATCH] Fix for negative timestamps in ftl files

---
 components/streams/include/ftl/streams/filestream.hpp | 1 +
 components/streams/src/filestream.cpp                 | 5 ++++-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/components/streams/include/ftl/streams/filestream.hpp b/components/streams/include/ftl/streams/filestream.hpp
index be4159890..a5d75b947 100644
--- a/components/streams/include/ftl/streams/filestream.hpp
+++ b/components/streams/include/ftl/streams/filestream.hpp
@@ -66,6 +66,7 @@ class File : public Stream {
 	int64_t timestart_;
 	int64_t timestamp_;
 	int64_t interval_;
+	int64_t first_ts_;
 	bool active_;
 	int version_;
 	ftl::timer::TimerHandle timer_;
diff --git a/components/streams/src/filestream.cpp b/components/streams/src/filestream.cpp
index f8ee805b0..705388a2e 100644
--- a/components/streams/src/filestream.cpp
+++ b/components/streams/src/filestream.cpp
@@ -41,6 +41,7 @@ bool File::_checkFile() {
 	int count = 10;
 	int64_t ts = -1000;
 	int min_ts_diff = 1000;
+	first_ts_ = 10000000;
 
 	while (count > 0) {
 		std::tuple<ftl::codecs::StreamPacket,ftl::codecs::Packet> data;
@@ -51,6 +52,8 @@ bool File::_checkFile() {
 		auto &spkt = std::get<0>(data);
 		auto &pkt = std::get<1>(data);
 
+		if (spkt.timestamp < first_ts_) first_ts_ = spkt.timestamp;
+
 		if (spkt.timestamp > 0 && int(spkt.channel) < 32) {
 			if (spkt.timestamp > ts) {
 				--count;
@@ -219,7 +222,7 @@ bool File::tick(int64_t ts) {
 
 		// Adjust timestamp
 		// FIXME: A potential bug where multiple times are merged into one?
-		std::get<0>(data).timestamp = ((std::get<0>(data).timestamp) / interval_) * interval_ + timestart_;
+		std::get<0>(data).timestamp = (((std::get<0>(data).timestamp) - first_ts_) / interval_) * interval_ + timestart_;
 
 		// Maintain availability of channels.
 		available(0) += std::get<0>(data).channel;
-- 
GitLab