SourceProcessor.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/ChannelRequests.hpp>
  22. #include <ableton/link_audio/Id.hpp>
  23. #include <ableton/link_audio/PCMCodec.hpp>
  24. #include <ableton/link_audio/Source.hpp>
  25. #include <ableton/link_audio/v1/Messages.hpp>
  26. #include <ableton/util/Injected.hpp>
  27. #include <optional>
  28. #include <string>
  29. namespace ableton
  30. {
  31. namespace link_audio
  32. {
  33. template <typename GetSender, typename GetNodeId, typename IoContext>
  34. struct SourceProcessor
  35. {
  36. using Timer = typename util::Injected<IoContext>::type::Timer;
  37. static constexpr uint8_t kTtl = 5;
  38. SourceProcessor(util::Injected<IoContext> io,
  39. std::shared_ptr<Source> pSource,
  40. util::Injected<GetSender> getSender,
  41. util::Injected<GetNodeId> getNodeId)
  42. : mpImpl(std::make_shared<Impl>(
  43. std::move(io), pSource, std::move(getSender), std::move(getNodeId)))
  44. {
  45. mpImpl->sendAudioRequest();
  46. }
  47. ~SourceProcessor()
  48. {
  49. if (mpImpl != nullptr)
  50. {
  51. mpImpl->sendChannelStopRequest();
  52. }
  53. }
  54. bool process() { return mpImpl->process(); }
  55. void receiveAudioBuffer(const AudioBuffer& buffer)
  56. {
  57. mpImpl->receiveAudioBuffer(buffer);
  58. }
  59. const Id& id() const { return mpImpl->id(); }
  60. struct Impl : public std::enable_shared_from_this<Impl>
  61. {
  62. Impl(util::Injected<IoContext> io,
  63. std::shared_ptr<Source> pSource,
  64. util::Injected<GetSender> getSender,
  65. util::Injected<GetNodeId> getNodeId)
  66. : mTimer(io->makeTimer())
  67. , mpSource(pSource)
  68. , mGetSender(std::move(getSender))
  69. , mGetNodeId(std::move(getNodeId))
  70. , mBuffer(4096 * 2)
  71. , mDecoder(util::injectVal(Callback{this}), 4096)
  72. {
  73. }
  74. template <typename Payload>
  75. void sendMessage(const Payload& payload,
  76. const v1::MessageType messageType,
  77. const uint8_t ttl)
  78. {
  79. v1::MessageBuffer buffer;
  80. const auto messageBegin = buffer.begin();
  81. const auto messageEnd = v1::detail::encodeMessage(
  82. (*mGetNodeId)(), ttl, messageType, payload, messageBegin);
  83. const auto numBytes = static_cast<size_t>(std::distance(messageBegin, messageEnd));
  84. auto oSender = mGetSender->forChannel(mpSource->id());
  85. if (oSender)
  86. {
  87. try
  88. {
  89. (*oSender)(buffer.data(), numBytes);
  90. }
  91. catch (const std::runtime_error&)
  92. {
  93. }
  94. }
  95. }
  96. void sendAudioRequest()
  97. {
  98. mTimer.expires_from_now(std::chrono::seconds(kTtl));
  99. mTimer.async_wait(
  100. [this](const auto e)
  101. {
  102. if (!e)
  103. {
  104. sendAudioRequest();
  105. }
  106. });
  107. const auto request = ChannelRequest{(*mGetNodeId)(), mpSource->id()};
  108. sendMessage(toPayload(request), v1::kChannelRequest, kTtl);
  109. }
  110. void sendChannelStopRequest()
  111. {
  112. const auto stopRequest = ChannelStopRequest{(*mGetNodeId)(), mpSource->id()};
  113. sendMessage(toPayload(stopRequest), v1::kStopChannelRequest, 0);
  114. }
  115. bool process() { return mpSource.use_count() > 1; }
  116. void receiveAudioBuffer(const AudioBuffer& buffer) { mDecoder(buffer); }
  117. const Id& id() const { return mpSource->id(); }
  118. private:
  119. struct Callback
  120. {
  121. void operator()(BufferCallbackHandle<Buffer<int16_t>> buffer)
  122. {
  123. pImpl->mpSource->callback(buffer);
  124. }
  125. Impl* pImpl;
  126. };
  127. Timer mTimer;
  128. std::shared_ptr<Source> mpSource;
  129. util::Injected<GetSender> mGetSender;
  130. util::Injected<GetNodeId> mGetNodeId;
  131. Buffer<int16_t> mBuffer;
  132. PCMDecoder<int16_t, Callback> mDecoder;
  133. };
  134. std::shared_ptr<Impl> mpImpl;
  135. };
  136. } // namespace link_audio
  137. } // namespace ableton