From c7e8d869d7800737cffc7c590ca9cb816bf2fc6b Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Mon, 27 Jul 2020 20:09:39 +0300
Subject: [PATCH] Make sure net streams are unique

---
 .../streams/include/ftl/streams/feed.hpp      |  2 +-
 components/streams/src/feed.cpp               | 62 +++++++------------
 2 files changed, 23 insertions(+), 41 deletions(-)

diff --git a/components/streams/include/ftl/streams/feed.hpp b/components/streams/include/ftl/streams/feed.hpp
index 2b2d22ed0..72075b74f 100644
--- a/components/streams/include/ftl/streams/feed.hpp
+++ b/components/streams/include/ftl/streams/feed.hpp
@@ -193,7 +193,7 @@ private:
 	std::list<ftl::streams::ManualSourceBuilder*> render_builders_;
 	std::function<void(ftl::operators::Graph*)> pipe_creator_;
 
-	std::vector<std::string> netcams_;
+	std::unordered_set<std::string> netcams_;
 	ftl::Handler<const std::vector<std::string> &> new_sources_cb_;
 	ftl::Handler<uint32_t> add_src_cb_;
 	ftl::Handler<uint32_t> remove_sources_cb_;
diff --git a/components/streams/src/feed.cpp b/components/streams/src/feed.cpp
index 873f94f38..76cfc60e5 100644
--- a/components/streams/src/feed.cpp
+++ b/components/streams/src/feed.cpp
@@ -487,7 +487,7 @@ void Feed::removeFilter(Feed::Filter* filter) {
 
 void Feed::_updateNetSources(ftl::net::Peer *p, const std::string &s, bool autoadd) {
 	UNIQUE_LOCK(mtx_, lk);
-	netcams_.push_back(s);
+	netcams_.insert(s);
 
 	// TODO: Auto add source
 
@@ -508,50 +508,31 @@ void Feed::_updateNetSources(ftl::net::Peer *p, bool autoadd) {
 	//auto netcams =
 	//	net_->findAll<std::string>("list_streams");
 
-	auto peerstreams = p->call<std::vector<std::string>>("list_streams");
-
-	UNIQUE_LOCK(mtx_, lk);
-	//netcams_ = std::move(netcams);
-	netcams_.insert(netcams_.end(), peerstreams.begin(), peerstreams.end());
-
-	for (const auto &s : peerstreams) {
-		ftl::URI uri(s);
-		_add_recent_source(uri)["host"] = p->getURI();
-
-		if (autoadd || value("auto_host_sources", false)) {
-			ftl::pool.push([this, uri](int id) { add(uri); });
-		}
-	}
-
-	/*if (value("auto_host_sources", false)) {
-		for (auto s : netcams_) {
-			const std::string group = uri.getAttribute<std::string>("group");
-
-			if (fsid_lookup_.count(uri.getBaseURI()) == 0) {
-				auto *stream = ftl::create<ftl::stream::Net>
-					(this, std::string("netstream")
-					+std::to_string(fsid_lookup_.size()), net_);
-
-				int fsid = allocateFrameSetId(group);
+	// Peer may not have a list_streams binding yet
+	try {
+		auto peerstreams = p->call<std::vector<std::string>>("list_streams");
+	
 
-				stream->set("uri", s);
-				add(fsid, uri.getBaseURI(), stream);
+		UNIQUE_LOCK(mtx_, lk);
+		//netcams_ = std::move(netcams);
+		netcams_.insert(peerstreams.begin(), peerstreams.end());
 
-				LOG(INFO)	<< "Add Stream: "
-							<< stream->value("uri", std::string("NONE"))
-							<< " (" << fsid << ")";
+		for (const auto &s : peerstreams) {
+			ftl::URI uri(s);
+			_add_recent_source(uri)["host"] = p->getURI();
 
-				//cv_net_connect_.notify_one();
-			}
-			else {
-				LOG(INFO) << "Stream exists: " << s;
+			if (autoadd || value("auto_host_sources", false)) {
+				ftl::pool.push([this, uri](int id) { add(uri); });
 			}
 		}
-	}*/
 
-	ftl::pool.push([this, peerstreams](int id) {
-		new_sources_cb_.trigger(peerstreams);
-	});
+		ftl::pool.push([this, peerstreams](int id) {
+			new_sources_cb_.trigger(peerstreams);
+		});
+
+	} catch (const ftl::exception &e) {
+		
+	}
 
 	/* done by add()
 	if (n > 0) {
@@ -561,7 +542,8 @@ void Feed::_updateNetSources(ftl::net::Peer *p, bool autoadd) {
 
 std::vector<std::string> Feed::availableNetworkSources() {
 	SHARED_LOCK(mtx_, lk);
-	return netcams_;
+	std::vector<std::string> result(netcams_.begin(), netcams_.end());
+	return result;;
 }
 
 std::vector<std::string> Feed::availableGroups() {
-- 
GitLab