MainProcessor.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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/link_audio/ChannelAnnouncements.hpp>
  21. #include <ableton/link_audio/Sink.hpp>
  22. #include <ableton/link_audio/SinkProcessor.hpp>
  23. #include <ableton/link_audio/Source.hpp>
  24. #include <ableton/link_audio/SourceProcessor.hpp>
  25. #include <ableton/util/Injected.hpp>
  26. #include <ableton/util/SafeAsyncHandler.hpp>
  27. namespace ableton
  28. {
  29. namespace link_audio
  30. {
  31. using SharedSink = std::shared_ptr<Sink>;
  32. using SharedSource = std::shared_ptr<Source>;
  33. template <typename ChannelsChangedCallback,
  34. typename GetSender,
  35. typename GetNodeId,
  36. typename Random,
  37. typename IoContext>
  38. struct MainProcessor
  39. {
  40. using IoType = typename util::Injected<IoContext>::type;
  41. static constexpr auto kProcessTimerPeriod = std::chrono::milliseconds(1);
  42. MainProcessor(util::Injected<IoContext> io,
  43. util::Injected<ChannelsChangedCallback> callback)
  44. : mpImpl(std::make_shared<Impl>(std::move(io), std::move(callback)))
  45. {
  46. }
  47. ~MainProcessor() { stop(); }
  48. void stop() { mpImpl->stop(); }
  49. void addSink(SharedSink pSink,
  50. util::Injected<GetSender> getSender,
  51. util::Injected<GetNodeId> getNodeId)
  52. {
  53. mpImpl->addSink(pSink, std::move(getSender), std::move(getNodeId));
  54. }
  55. void addSource(SharedSource pSource,
  56. util::Injected<GetSender> getSender,
  57. util::Injected<GetNodeId> getNodeId)
  58. {
  59. mpImpl->addSource(pSource, std::move(getSender), std::move(getNodeId));
  60. }
  61. ChannelAnnouncements channelAnnouncements() const
  62. {
  63. return mpImpl->channelAnnouncements();
  64. }
  65. template <typename Request>
  66. void receiveChannelRequest(Request request, uint8_t ttl)
  67. {
  68. mpImpl->receiveChannelRequest(std::move(request), ttl);
  69. }
  70. template <typename It>
  71. void receiveAudioBuffer(It begin, It end)
  72. {
  73. mpImpl->receiveAudioBuffer(begin, end);
  74. }
  75. private:
  76. struct Impl : std::enable_shared_from_this<Impl>
  77. {
  78. using Timer = typename util::Injected<IoContext>::type::Timer;
  79. using MainSinkProcessor = SinkProcessor<GetSender, GetNodeId, IoContext&>;
  80. using MainSourceProcessor = SourceProcessor<GetSender, GetNodeId, IoContext&>;
  81. Impl(util::Injected<IoContext> io, util::Injected<ChannelsChangedCallback> callback)
  82. : mIo{std::move(io)}
  83. , mChannelsChangedCallback{std::move(callback)}
  84. , mProcessTimer{mIo->makeTimer()}
  85. {
  86. }
  87. void addSink(SharedSink pSink,
  88. util::Injected<GetSender> getSender,
  89. util::Injected<GetNodeId> getNodeId)
  90. {
  91. auto processor = std::make_unique<MainSinkProcessor>(
  92. util::injectRef(*mIo), pSink, std::move(getSender), std::move(getNodeId));
  93. mSinks.push_back(std::move(processor));
  94. start();
  95. }
  96. void addSource(SharedSource pSource,
  97. util::Injected<GetSender> getSender,
  98. util::Injected<GetNodeId> getNodeId)
  99. {
  100. auto pProcessor = std::make_unique<MainSourceProcessor>(
  101. util::injectRef(*mIo), pSource, std::move(getSender), std::move(getNodeId));
  102. mSources.push_back(std::move(pProcessor));
  103. start();
  104. }
  105. void start() { (*this)(); }
  106. void stop() { mProcessTimer.cancel(); }
  107. void operator()()
  108. {
  109. processSinks();
  110. processSources();
  111. if (!mSinks.empty() || !mSources.empty())
  112. {
  113. mProcessTimer.expires_from_now(kProcessTimerPeriod);
  114. mProcessTimer.async_wait(
  115. [this](const auto e)
  116. {
  117. if (!e)
  118. {
  119. (*this)();
  120. }
  121. });
  122. }
  123. }
  124. void processSinks()
  125. {
  126. auto channelsChanged = false;
  127. for (auto it = mSinks.begin(); it != mSinks.end();)
  128. {
  129. if ((*it)->nameChanged())
  130. {
  131. channelsChanged = true;
  132. }
  133. if ((*it)->process())
  134. {
  135. ++it;
  136. }
  137. else
  138. {
  139. it = mSinks.erase(it);
  140. channelsChanged = true;
  141. }
  142. }
  143. if (channelsChanged)
  144. {
  145. (*mChannelsChangedCallback)();
  146. }
  147. }
  148. void processSources()
  149. {
  150. auto channelsChanged = false;
  151. for (auto it = mSources.begin(); it != mSources.end();)
  152. {
  153. if ((*it)->process())
  154. {
  155. ++it;
  156. }
  157. else
  158. {
  159. it = mSources.erase(it);
  160. }
  161. }
  162. }
  163. ChannelAnnouncements channelAnnouncements() const
  164. {
  165. auto announcements = ChannelAnnouncements{};
  166. for (auto& sink : mSinks)
  167. {
  168. announcements.channels.emplace_back(
  169. ChannelAnnouncement{sink->name(), sink->id()});
  170. }
  171. return announcements;
  172. }
  173. template <typename Request>
  174. void receiveChannelRequest(Request request, uint8_t ttl)
  175. {
  176. auto it =
  177. std::find_if(mSinks.begin(),
  178. mSinks.end(),
  179. [&](const auto& pSink) { return request.channelId == pSink->id(); });
  180. if (it != mSinks.end())
  181. {
  182. it->get()->receiveChannelRequest(std::move(request), ttl);
  183. }
  184. }
  185. template <typename It>
  186. void receiveAudioBuffer(It begin, It end)
  187. {
  188. static auto audioBuffer = AudioBuffer{};
  189. AudioBuffer::fromNetworkByteStream(audioBuffer, begin, end);
  190. auto it = std::find_if(mSources.begin(),
  191. mSources.end(),
  192. [&](const auto& pSource)
  193. { return audioBuffer.channelId == pSource->id(); });
  194. if (it != mSources.end())
  195. {
  196. it->get()->receiveAudioBuffer(audioBuffer);
  197. }
  198. }
  199. util::Injected<IoContext> mIo;
  200. util::Injected<ChannelsChangedCallback> mChannelsChangedCallback;
  201. std::vector<std::unique_ptr<MainSinkProcessor>> mSinks;
  202. std::vector<std::unique_ptr<MainSourceProcessor>> mSources;
  203. Timer mProcessTimer;
  204. };
  205. std::shared_ptr<Impl> mpImpl;
  206. };
  207. } // namespace link_audio
  208. } // namespace ableton