From f9f126d3a2656a1c97088be4ea1c0c5c4a7644c1 Mon Sep 17 00:00:00 2001
From: Sebastian Hahta <joseha@utu.fi>
Date: Thu, 12 Sep 2024 18:20:03 +0300
Subject: [PATCH] add option to disable buffering for certain channel

---
 include/ftl/protocol/streams.hpp |  1 +
 src/streams/netstream.cpp        | 19 ++++++++++++++++++-
 src/streams/netstream.hpp        |  2 ++
 3 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/include/ftl/protocol/streams.hpp b/include/ftl/protocol/streams.hpp
index 148bfb8..39ea3a9 100644
--- a/include/ftl/protocol/streams.hpp
+++ b/include/ftl/protocol/streams.hpp
@@ -68,6 +68,7 @@ enum struct StreamProperty {
     kUnderunCount,
     kDropCount,
     kAutoBufferAdjust, /// When enabled, buffer size may change runtime to minimize delay (and no underruns).
+    kDisableBuffering, /// enable/disable buffering for specific channel
 };
 
 /**
diff --git a/src/streams/netstream.cpp b/src/streams/netstream.cpp
index 6f9def8..52c617e 100644
--- a/src/streams/netstream.cpp
+++ b/src/streams/netstream.cpp
@@ -640,6 +640,13 @@ void Net::netstream_thread_() {
     pending_packets_.wait();
 }
 
+void Net::disableBuffering(ftl::protocol::Channel c, bool disable)
+{
+    auto lk = std::unique_lock(mtx_);
+    if (disable) { buffering_disabled_channels_.insert(c); }
+    else         { buffering_disabled_channels_.erase(c); }
+}
+
 bool Net::begin() {
     if (active_) return true;
 
@@ -679,7 +686,14 @@ bool Net::begin() {
                 _processPacket(&p, ttimeoff, spkt, pkt);
                 return;
             }
-            queuePacket_(&p, std::move(spkt), std::move(pkt));
+            if (buffering_disabled_channels_.count(spkt.channel) > 0)
+            {
+                _processPacket(&p, ttimeoff, spkt, pkt);
+            }
+            else 
+            {
+                queuePacket_(&p, std::move(spkt), std::move(pkt));
+            }
         });
     }
 
@@ -1000,6 +1014,7 @@ void Net::setProperty(ftl::protocol::StreamProperty opt, std::any value) {
     case StreamProperty::kFrameRate     :
     case StreamProperty::kUnderunCount  :
     case StreamProperty::kDropCount     :
+    case StreamProperty::kDisableBuffering : disableBuffering(std::any_cast<ftl::protocol::Channel>(value), true); break;
     case StreamProperty::kURI           :  throw FTL_Error("Readonly property");
     default                             :  throw FTL_Error("Unsupported property");
     }
@@ -1022,6 +1037,7 @@ std::any Net::getProperty(ftl::protocol::StreamProperty opt) {
     case StreamProperty::kRequestSize   :  return frames_to_request_;
     case StreamProperty::kUnderunCount  :  return static_cast<int>(underruns_);
     case StreamProperty::kDropCount     :  return static_cast<int>(-1);
+    case StreamProperty::kDisableBuffering : return buffering_disabled_channels_.size();
     default                             :  throw FTL_Error("Unsupported property");
     }
 }
@@ -1044,6 +1060,7 @@ bool Net::supportsProperty(ftl::protocol::StreamProperty opt) {
                                              // stream is at capacity (more data is being passed than can be actually
                                              // sent) and any drops must happen at the source.
     case StreamProperty::kAutoBufferAdjust :
+    case StreamProperty::kDisableBuffering :
     case StreamProperty::kURI           :  return true;
     default                             :  return false;
     }
diff --git a/src/streams/netstream.hpp b/src/streams/netstream.hpp
index 4b83fb1..020c997 100644
--- a/src/streams/netstream.hpp
+++ b/src/streams/netstream.hpp
@@ -66,6 +66,7 @@ class Net : public Stream {
     void refresh() override;
 
     void setBuffering(float seconds);
+    void disableBuffering(ftl::protocol::Channel c, bool value);
 
     void setAutoBufferAdjust(bool enable);
 
@@ -209,6 +210,7 @@ class Net : public Stream {
     };
 
     bool netstream_thread_waiting_ = false;
+    std::unordered_set<ftl::protocol::Channel> buffering_disabled_channels_;
     std::unordered_map<ftl::protocol::FrameID, PacketQueue> packet_queue_;
     ftl::TaskQueue pending_packets_;
 
-- 
GitLab