/* Copyright 2025, Ableton AG, Berlin. All rights reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * If you would like to incorporate Link into a proprietary software application, * please contact . */ #pragma once #include #include #include #include #include #include #include #include #include namespace ableton { namespace link_audio { template struct SourceProcessor { using Timer = typename util::Injected::type::Timer; static constexpr uint8_t kTtl = 5; SourceProcessor(util::Injected io, std::shared_ptr pSource, util::Injected getSender, util::Injected getNodeId) : mpImpl(std::make_shared( std::move(io), pSource, std::move(getSender), std::move(getNodeId))) { mpImpl->sendAudioRequest(); } ~SourceProcessor() { if (mpImpl != nullptr) { mpImpl->sendChannelStopRequest(); } } bool process() { return mpImpl->process(); } void receiveAudioBuffer(const AudioBuffer& buffer) { mpImpl->receiveAudioBuffer(buffer); } const Id& id() const { return mpImpl->id(); } struct Impl : public std::enable_shared_from_this { Impl(util::Injected io, std::shared_ptr pSource, util::Injected getSender, util::Injected getNodeId) : mTimer(io->makeTimer()) , mpSource(pSource) , mGetSender(std::move(getSender)) , mGetNodeId(std::move(getNodeId)) , mBuffer(4096 * 2) , mDecoder(util::injectVal(Callback{this}), 4096) { } template void sendMessage(const Payload& payload, const v1::MessageType messageType, const uint8_t ttl) { v1::MessageBuffer buffer; const auto messageBegin = buffer.begin(); const auto messageEnd = v1::detail::encodeMessage( (*mGetNodeId)(), ttl, messageType, payload, messageBegin); const auto numBytes = static_cast(std::distance(messageBegin, messageEnd)); auto oSender = mGetSender->forChannel(mpSource->id()); if (oSender) { try { (*oSender)(buffer.data(), numBytes); } catch (const std::runtime_error&) { } } } void sendAudioRequest() { mTimer.expires_from_now(std::chrono::seconds(kTtl)); mTimer.async_wait( [this](const auto e) { if (!e) { sendAudioRequest(); } }); const auto request = ChannelRequest{(*mGetNodeId)(), mpSource->id()}; sendMessage(toPayload(request), v1::kChannelRequest, kTtl); } void sendChannelStopRequest() { const auto stopRequest = ChannelStopRequest{(*mGetNodeId)(), mpSource->id()}; sendMessage(toPayload(stopRequest), v1::kStopChannelRequest, 0); } bool process() { return mpSource.use_count() > 1; } void receiveAudioBuffer(const AudioBuffer& buffer) { mDecoder(buffer); } const Id& id() const { return mpSource->id(); } private: struct Callback { void operator()(BufferCallbackHandle> buffer) { pImpl->mpSource->callback(buffer); } Impl* pImpl; }; Timer mTimer; std::shared_ptr mpSource; util::Injected mGetSender; util::Injected mGetNodeId; Buffer mBuffer; PCMDecoder mDecoder; }; std::shared_ptr mpImpl; }; } // namespace link_audio } // namespace ableton