SessionController.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/Controller.hpp>
  21. #include <ableton/link_audio/Id.hpp>
  22. namespace ableton
  23. {
  24. namespace link_audio
  25. {
  26. template <typename PeerCountCallback,
  27. typename TempoCallback,
  28. typename StartStopStateCallback,
  29. typename Clock,
  30. typename Random,
  31. typename IoContext>
  32. class SessionController : public Controller<PeerCountCallback,
  33. TempoCallback,
  34. StartStopStateCallback,
  35. Clock,
  36. Random,
  37. IoContext,
  38. SessionController<PeerCountCallback,
  39. TempoCallback,
  40. StartStopStateCallback,
  41. Clock,
  42. Random,
  43. IoContext>>
  44. {
  45. public:
  46. using LinkAudioController = Controller<PeerCountCallback,
  47. TempoCallback,
  48. StartStopStateCallback,
  49. Clock,
  50. Random,
  51. IoContext,
  52. SessionController<PeerCountCallback,
  53. TempoCallback,
  54. StartStopStateCallback,
  55. Clock,
  56. Random,
  57. IoContext>>;
  58. SessionController(link::Tempo tempo,
  59. PeerCountCallback peerCallback,
  60. TempoCallback tempoCallback,
  61. StartStopStateCallback startStopStateCallback,
  62. Clock clock)
  63. : LinkAudioController(
  64. tempo, peerCallback, tempoCallback, startStopStateCallback, clock)
  65. {
  66. this->mRtClientStateSetter.start();
  67. }
  68. ~SessionController()
  69. {
  70. this->mIo->async([this]() { this->stopAudio(); });
  71. this->shutdown();
  72. }
  73. void sessionMembershipCallback()
  74. {
  75. this->mSessionPeerCounter();
  76. this->updateLinkAudio();
  77. }
  78. void joinSessionCallback(const link::Session& session)
  79. {
  80. this->joinSession(session);
  81. this->updateLinkAudio();
  82. }
  83. template <typename Peer, typename Handler>
  84. void measurePeerCallback(Peer peer, Handler handler)
  85. {
  86. this->measurePeer(std::move(peer), std::move(handler));
  87. }
  88. void updateDiscoveryCallback()
  89. {
  90. this->updateDiscovery();
  91. this->updateAudioDiscovery();
  92. }
  93. void gatewaysChangedCallback() { this->updateLinkAudioGateways(); }
  94. void updateRtStatesCallback()
  95. {
  96. // Process the pending client states first to make sure we don't push one
  97. // after we have joined a running session when enabling
  98. this->mRtClientStateSetter.processPendingClientStates();
  99. this->mRtClientStateSetter.updateEnabled();
  100. this->updateIsLinkAudioEnabled();
  101. }
  102. void sawAudioEndpointCallback(Id peerId,
  103. std::optional<discovery::UdpEndpoint> endpoint,
  104. discovery::IpAddress gateway)
  105. {
  106. this->sawLinkAudioEndpoint(peerId, endpoint, gateway);
  107. }
  108. };
  109. } // namespace link_audio
  110. } // namespace ableton