From 01aa5049c6778548c07e5df97bfd35128bbb668b Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nicolas.pope@utu.fi>
Date: Thu, 16 Jun 2022 06:57:37 +0000
Subject: [PATCH] #56 Add frame active method to streams

---
 include/ftl/protocol/broadcaster.hpp |  1 +
 include/ftl/protocol/muxer.hpp       |  1 +
 include/ftl/protocol/streams.hpp     |  2 ++
 src/streams/broadcaster.cpp          | 11 +++++++++++
 src/streams/muxer.cpp                |  9 +++++++++
 src/streams/netstream.cpp            |  5 +++++
 src/streams/netstream.hpp            |  1 +
 src/streams/streams.cpp              |  4 ++++
 test/stream_integration.cpp          |  4 ++++
 9 files changed, 38 insertions(+)

diff --git a/include/ftl/protocol/broadcaster.hpp b/include/ftl/protocol/broadcaster.hpp
index 55c88d0..a1d3f89 100644
--- a/include/ftl/protocol/broadcaster.hpp
+++ b/include/ftl/protocol/broadcaster.hpp
@@ -32,6 +32,7 @@ class Broadcast : public Stream {
     bool begin() override;
     bool end() override;
     bool active() override;
+    bool active(FrameID id) override;
 
     void reset() override;
 
diff --git a/include/ftl/protocol/muxer.hpp b/include/ftl/protocol/muxer.hpp
index 425216c..4cb37d3 100644
--- a/include/ftl/protocol/muxer.hpp
+++ b/include/ftl/protocol/muxer.hpp
@@ -61,6 +61,7 @@ class Muxer : public Stream {
     bool begin() override;
     bool end() override;
     bool active() override;
+    bool active(FrameID id) override;
 
     void reset() override;
 
diff --git a/include/ftl/protocol/streams.hpp b/include/ftl/protocol/streams.hpp
index 90265b4..1035790 100644
--- a/include/ftl/protocol/streams.hpp
+++ b/include/ftl/protocol/streams.hpp
@@ -146,6 +146,8 @@ class Stream {
      */
     virtual bool active() = 0;
 
+    virtual bool active(FrameID id);
+
     /**
      * @brief Clear all state. This will remove all information about available
      * and enabled frames or channels. You will then need to enable frames and
diff --git a/src/streams/broadcaster.cpp b/src/streams/broadcaster.cpp
index 441a755..0103b33 100644
--- a/src/streams/broadcaster.cpp
+++ b/src/streams/broadcaster.cpp
@@ -89,6 +89,17 @@ bool Broadcast::active() {
     return r;
 }
 
+bool Broadcast::active(FrameID id) {
+    if (streams_.size() == 0) return false;
+
+    for (auto &s : streams_) {
+        if (s.stream->active(id)) {
+            return true;
+        }
+    }
+    return false;
+}
+
 void Broadcast::reset() {
     SHARED_LOCK(mtx_, lk);
     for (auto &s : streams_) {
diff --git a/src/streams/muxer.cpp b/src/streams/muxer.cpp
index 0df1a67..b0f6b57 100644
--- a/src/streams/muxer.cpp
+++ b/src/streams/muxer.cpp
@@ -293,6 +293,15 @@ bool Muxer::active() {
     return r;
 }
 
+bool Muxer::active(FrameID id) {
+    auto p = _mapToOutput(id);
+    if (p.second) {
+        return p.second->stream->active(p.first);
+    } else {
+        return false;
+    }
+}
+
 void Muxer::reset() {
     for (auto &s : streams_) {
         s.stream->reset();
diff --git a/src/streams/netstream.cpp b/src/streams/netstream.cpp
index 590b265..91e9ac9 100644
--- a/src/streams/netstream.cpp
+++ b/src/streams/netstream.cpp
@@ -554,6 +554,11 @@ bool Net::active() {
     return active_;
 }
 
+bool Net::active(FrameID id) {
+    SHARED_LOCK(mtx_, lk);
+    return active_ && clients_.count(id) > 0;
+}
+
 void Net::setProperty(ftl::protocol::StreamProperty opt, std::any value) {
     switch (opt) {
     case StreamProperty::kBitrate       :
diff --git a/src/streams/netstream.hpp b/src/streams/netstream.hpp
index 565b942..eeedaaa 100644
--- a/src/streams/netstream.hpp
+++ b/src/streams/netstream.hpp
@@ -47,6 +47,7 @@ class Net : public Stream {
     bool begin() override;
     bool end() override;
     bool active() override;
+    bool active(FrameID id) override;
 
     bool enable(FrameID id) override;
     bool enable(FrameID id, ftl::protocol::Channel c) override;
diff --git a/src/streams/streams.cpp b/src/streams/streams.cpp
index 198230c..79fc283 100644
--- a/src/streams/streams.cpp
+++ b/src/streams/streams.cpp
@@ -176,3 +176,7 @@ void Stream::request(const ftl::protocol::Request &req) {
 void Stream::error(ftl::protocol::Error err, const std::string &str) {
     error_cb_.trigger(err, str);
 }
+
+bool Stream::active(FrameID id) {
+    return active();
+}
diff --git a/test/stream_integration.cpp b/test/stream_integration.cpp
index fd98ade..161b566 100644
--- a/test/stream_integration.cpp
+++ b/test/stream_integration.cpp
@@ -111,11 +111,15 @@ TEST_CASE("TCP Stream", "[net]") {
         s1->begin();
         s2->begin();
 
+        REQUIRE(s1->active(FrameID(0, 0)) == false);
+
         s2->enable(FrameID(0, 0));
 
         // TODO: Find better option
         std::this_thread::sleep_for(std::chrono::milliseconds(10));
 
+        REQUIRE(s1->active(FrameID(0, 0)) == true);
+
         ftl::protocol::StreamPacket spkt;
         spkt.streamID = 0;
         spkt.frame_number = 0;
-- 
GitLab