From 9ea66697fcc5fb2c06f9d9bfe553c19b152974aa Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nicolas.pope@utu.fi>
Date: Fri, 11 Nov 2022 07:46:27 +0000
Subject: [PATCH] Cleanup and stats for netstream buffering

---
 include/ftl/protocol/streams.hpp |  4 +++-
 src/streams/netstream.cpp        | 14 ++++++++++----
 src/streams/netstream.hpp        |  2 ++
 3 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/include/ftl/protocol/streams.hpp b/include/ftl/protocol/streams.hpp
index 7f08b4d..250c195 100644
--- a/include/ftl/protocol/streams.hpp
+++ b/include/ftl/protocol/streams.hpp
@@ -64,7 +64,9 @@ enum struct StreamProperty {
     kTags,
     kUser,
     kRequestSize,
-    kBuffering
+    kBuffering,
+    kUnderunCount,
+    kDropCount
 };
 
 /**
diff --git a/src/streams/netstream.cpp b/src/streams/netstream.cpp
index 4c809d8..e025c49 100644
--- a/src/streams/netstream.cpp
+++ b/src/streams/netstream.cpp
@@ -342,12 +342,12 @@ void Net::_run() {
 
                     // Not ready to display this one yet.
                     if (ats > cts) {
-                        LOG(INFO) << "Next presentation in: " << (ats - cts);
+                        if (ats - cts > 100) {
+                            ++drops_;
+                        }
                         nextTs = std::min(nextTs, ats + state->base_local_ts_);
                         hasNext = true;
                         continue;
-                    } else {
-                        LOG(INFO) << "Presentation error = " << (cts - ats);
                     }
 
                     auto current = state->buffer.begin();
@@ -390,6 +390,7 @@ void Net::_run() {
                         hasNext = true;
                     } else {
                         LOG(WARNING) << "Buffer underun " << now;
+                        ++underuns_;
                     }
 
                     auto it = state->buffer.begin();
@@ -406,7 +407,6 @@ void Net::_run() {
 
             auto used = ftl::time::get_time();
             int64_t spare = (hasNext) ? nextTs - used : 10;
-            if (activeStates > 0) LOG(INFO) << "Sleeping for " << spare;
             sleep_for(milliseconds(std::max(int64_t(1), spare)));
         }
 #ifdef WIN32
@@ -748,6 +748,8 @@ void Net::setProperty(ftl::protocol::StreamProperty opt, std::any value) {
     case StreamProperty::kBytesReceived :
     case StreamProperty::kLatency       :
     case StreamProperty::kFrameRate     :
+    case StreamProperty::kUnderunCount  :
+    case StreamProperty::kDropCount     :
     case StreamProperty::kURI           :  throw FTL_Error("Readonly property");
     default                             :  throw FTL_Error("Unsupported property");
     }
@@ -767,6 +769,8 @@ std::any Net::getProperty(ftl::protocol::StreamProperty opt) {
     case StreamProperty::kName          :  return name_;
     case StreamProperty::kBuffering     :  return static_cast<float>(buffering_) / 1000.0f;
     case StreamProperty::kRequestSize   :  return frames_to_request_;
+    case StreamProperty::kUnderunCount  :  return static_cast<int>(underuns_);
+    case StreamProperty::kDropCount     :  return static_cast<int>(drops_);
     default                             :  throw FTL_Error("Unsupported property");
     }
 }
@@ -784,6 +788,8 @@ bool Net::supportsProperty(ftl::protocol::StreamProperty opt) {
     case StreamProperty::kName          :
     case StreamProperty::kRequestSize   :
     case StreamProperty::kBuffering     :
+    case StreamProperty::kUnderunCount  :
+    case StreamProperty::kDropCount     :
     case StreamProperty::kURI           :  return true;
     default                             :  return false;
     }
diff --git a/src/streams/netstream.hpp b/src/streams/netstream.hpp
index a006438..196323e 100644
--- a/src/streams/netstream.hpp
+++ b/src/streams/netstream.hpp
@@ -103,6 +103,8 @@ class Net : public Stream {
     ftl::PacketManager mgr_;
     ftl::Handler<ftl::net::Peer*> connect_cb_;
     int64_t buffering_ = 0;
+    std::atomic_int underuns_ = 0;
+    std::atomic_int drops_ = 0;
 
     static std::atomic_size_t req_bitrate__;
     static std::atomic_size_t tx_bitrate__;
-- 
GitLab