diff --git a/daemon/browser.cpp b/daemon/browser.cpp index efd0a69..7f6ba58 100644 --- a/daemon/browser.cpp +++ b/daemon/browser.cpp @@ -72,24 +72,23 @@ bool Browser::worker() { BOOST_LOG_TRIVIAL(debug) << "browser:: received SAP message for " << id; std::unique_lock sources_lock(sources_mutex_); + last_update_ = + duration_cast(steady_clock::now() - startup_).count(); auto it = sources_.get().find(id); if (it == sources_.end()) { // Source is not in the map if (is_announce) { // annoucement, add new source - sources_.insert( - {id, - "SAP", - ip::address_v4(ntohl(addr)).to_string(), - sdp_get_subject(sdp), - {}, - sdp_get_origin(sdp), - sdp, - static_cast( - duration_cast(steady_clock::now() - startup_) - .count()), - config_->get_sap_interval()}); + sources_.insert({id, + "SAP", + ip::address_v4(ntohl(addr)).to_string(), + sdp_get_subject(sdp), + {}, + sdp_get_origin(sdp), + sdp, + last_update_, + config_->get_sap_interval()}); } } else { // Source is already in the map @@ -97,12 +96,10 @@ bool Browser::worker() { BOOST_LOG_TRIVIAL(debug) << "browser:: refreshing SAP source " << it->id; // annoucement, update last seen and announce period - uint32_t last_seen = - duration_cast(steady_clock::now() - startup_).count(); auto upd_source{*it}; - if ((last_seen - upd_source.last_seen) != 0) { - upd_source.announce_period = last_seen - upd_source.last_seen; - upd_source.last_seen = last_seen; + if ((last_update_ - upd_source.last_seen) != 0) { + upd_source.announce_period = last_update_ - upd_source.last_seen; + upd_source.last_seen = last_update_; sources_.replace(it, upd_source); } } else { @@ -130,6 +127,8 @@ bool Browser::worker() { BOOST_LOG_TRIVIAL(info) << "browser:: SAP source " << it->id << " timeout"; it = sources_.erase(it); + last_update_ = + duration_cast(steady_clock::now() - startup_).count(); } else { it++; } @@ -150,9 +149,9 @@ bool Browser::worker() { void Browser::on_change_rtsp_source(const std::string& name, const std::string& domain, const RtspSource& s) { - uint32_t last_seen = - duration_cast(steady_clock::now() - startup_).count(); std::unique_lock sources_lock(sources_mutex_); + last_update_ = + duration_cast(steady_clock::now() - startup_).count(); /* search by name */ auto rng = sources_.get().equal_range(name); while (rng.first != rng.second) { @@ -166,7 +165,7 @@ void Browser::on_change_rtsp_source(const std::string& name, upd_source.address = s.address; upd_source.origin = sdp_get_origin(s.sdp); upd_source.sdp = s.sdp; - upd_source.last_seen = last_seen; + upd_source.last_seen = last_update_; sources_.get().replace(it, upd_source); return; } @@ -176,7 +175,7 @@ void Browser::on_change_rtsp_source(const std::string& name, BOOST_LOG_TRIVIAL(info) << "browser:: adding RTSP source " << s.id << " name " << name << " domain " << domain; sources_.insert({s.id, s.source, s.address, name, domain, - sdp_get_origin(s.sdp), s.sdp, last_seen, 0}); + sdp_get_origin(s.sdp), s.sdp, last_update_, 0}); } void Browser::on_remove_rtsp_source(const std::string& name, @@ -191,6 +190,8 @@ void Browser::on_remove_rtsp_source(const std::string& name, << "browser:: removing RTSP source " << it->id << " name " << it->name << " domain " << it->domain; name_idx.erase(it); + last_update_ = + duration_cast(steady_clock::now() - startup_).count(); break; } ++rng.first; @@ -206,6 +207,7 @@ bool Browser::init() { running_ = true; res_ = std::async(std::launch::async, &Browser::worker, this); } + last_update_ = 0; return true; } diff --git a/daemon/browser.hpp b/daemon/browser.hpp index 381bec9..716ca88 100644 --- a/daemon/browser.hpp +++ b/daemon/browser.hpp @@ -61,6 +61,7 @@ class Browser : public MDNSClient { bool init() override; bool terminate() override; + uint32_t get_last_update_ts() const { return last_update_; } std::list get_remote_sources( const std::string& source = "all") const; @@ -99,6 +100,7 @@ class Browser : public MDNSClient { SAP sap_{config_->get_sap_mcast_addr()}; IGMP igmp_; std::chrono::time_point startup_; + uint32_t last_update_{0}; /* seconds from daemon startup */ }; #endif diff --git a/daemon/session_manager.cpp b/daemon/session_manager.cpp index 43805c4..c84a484 100644 --- a/daemon/session_manager.cpp +++ b/daemon/session_manager.cpp @@ -1056,12 +1056,18 @@ std::list SessionManager::get_updated_sinks( void SessionManager::update_sinks() { if (config_->get_auto_sinks_update()) { - std::list remote_sources = browser_->get_remote_sources(); - auto sinks_list = get_updated_sinks(remote_sources); - for (auto& sink : sinks_list) { - // Re-add sink with new SDP, since the sink.id is the same there will be - // an update - add_sink(sink); + uint32_t last_update = browser_->get_last_update_ts(); + // check remote sources only if an update arrived + if (last_update && last_sink_update_ != last_update) { + BOOST_LOG_TRIVIAL(debug) << "Updating sinks ..."; + std::list remote_sources = browser_->get_remote_sources(); + auto sinks_list = get_updated_sinks(remote_sources); + for (auto& sink : sinks_list) { + // Re-add sink with new SDP, since the sink.id is the same there will be + // an update + add_sink(sink); + } + last_sink_update_ = last_update; } } } diff --git a/daemon/session_manager.hpp b/daemon/session_manager.hpp index 5de2495..5d0ef68 100644 --- a/daemon/session_manager.hpp +++ b/daemon/session_manager.hpp @@ -114,6 +114,7 @@ class SessionManager { bool init() { if (!running_) { running_ = true; + // to have an increasing session versions between restarts res_ = std::async(std::launch::async, &SessionManager::worker, this); } return true; @@ -241,6 +242,7 @@ class SessionManager { SAP sap_{config_->get_sap_mcast_addr()}; IGMP igmp_; + uint32_t last_sink_update_{0}; /* used to handle session versioning */ inline static std::atomic g_session_version{0};