PeerGateways.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Copyright 2025, Ableton AG, Berlin. All rights reserved.
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * If you would like to incorporate Link into a proprietary software application,
  17. * please contact <link-devs@ableton.com>.
  18. */
  19. #pragma once
  20. #include <ableton/discovery/AsioTypes.hpp>
  21. #include <ableton/link/NodeId.hpp>
  22. #include <ableton/util/Injected.hpp>
  23. #include <algorithm>
  24. #include <map>
  25. #include <memory>
  26. #include <optional>
  27. #include <type_traits>
  28. #include <vector>
  29. namespace ableton
  30. {
  31. namespace link_audio
  32. {
  33. template <typename GatewayFactory, typename IoContext>
  34. struct PeerGateways
  35. {
  36. using FactoryType = typename util::Injected<GatewayFactory>::type;
  37. using GatewayPtr = std::invoke_result_t<FactoryType, const discovery::IpAddress&>;
  38. using GatewayMap = std::map<discovery::IpAddress, GatewayPtr>;
  39. PeerGateways(util::Injected<GatewayFactory> factory, util::Injected<IoContext> io)
  40. : mIo(std::move(io))
  41. , mFactory(std::move(factory))
  42. {
  43. }
  44. template <typename Handler>
  45. void forEachGateway(Handler handler)
  46. {
  47. for (auto it = mGateways.begin(); it != mGateways.end(); ++it)
  48. {
  49. handler(it->second);
  50. }
  51. }
  52. template <typename Handler>
  53. void withGateways(Handler handler)
  54. {
  55. handler(mGateways.begin(), mGateways.end());
  56. }
  57. void clear()
  58. {
  59. mGateways.clear();
  60. mFactory->gatewaysChanged();
  61. }
  62. template <typename Announcement>
  63. void updateAnnouncement(const Announcement& announcement)
  64. {
  65. for (const auto& gateway : mGateways)
  66. {
  67. gateway.second->updateAnnouncement(announcement);
  68. }
  69. }
  70. void updateGateways(std::vector<discovery::IpAddress> linkGateways)
  71. {
  72. using namespace std;
  73. vector<discovery::IpAddress> curAddrs;
  74. curAddrs.reserve(mGateways.size());
  75. transform(mGateways.begin(),
  76. mGateways.end(),
  77. back_inserter(curAddrs),
  78. [](const auto& gateway) { return gateway.first; });
  79. vector<discovery::IpAddress> staleAddrs;
  80. set_difference(curAddrs.begin(),
  81. curAddrs.end(),
  82. linkGateways.begin(),
  83. linkGateways.end(),
  84. back_inserter(staleAddrs));
  85. for (const auto& addr : staleAddrs)
  86. {
  87. mGateways.erase(addr);
  88. }
  89. vector<discovery::IpAddress> newAddrs;
  90. set_difference(linkGateways.begin(),
  91. linkGateways.end(),
  92. curAddrs.begin(),
  93. curAddrs.end(),
  94. back_inserter(newAddrs));
  95. for (const auto& addr : newAddrs)
  96. {
  97. try
  98. {
  99. info(mIo->log()) << "initializing audio gateway on interface " << addr;
  100. mGateways.emplace(addr, (*mFactory)(addr));
  101. }
  102. catch (const std::runtime_error& e)
  103. {
  104. warning(mIo->log()) << "failed to init audio gateway on interface " << addr
  105. << " reason: " << e.what();
  106. }
  107. }
  108. if (!staleAddrs.empty() || !newAddrs.empty())
  109. {
  110. mFactory->gatewaysChanged();
  111. }
  112. }
  113. template <typename It>
  114. void updateSessionPeers(It peersBegin, It peersEnd)
  115. {
  116. forEachGateway([&](auto& gateway)
  117. { gateway->pruneChannelsEndpoints(peersBegin, peersEnd); });
  118. }
  119. void sawLinkAudioEndpoint(link::NodeId peerId,
  120. std::optional<discovery::UdpEndpoint> endpoint,
  121. discovery::IpAddress gateway)
  122. {
  123. if (auto it = mGateways.find(gateway); it != mGateways.end())
  124. {
  125. it->second->sawLinkAudioEndpoint(peerId, endpoint);
  126. }
  127. }
  128. private:
  129. util::Injected<IoContext> mIo;
  130. GatewayMap mGateways;
  131. util::Injected<GatewayFactory> mFactory;
  132. };
  133. } // namespace link_audio
  134. } // namespace ableton