rusty_link 0.4.9

Rust bindings for Ableton Link through the official C Wrapper (abl_link)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/* 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 <http://www.gnu.org/licenses/>.
 *
 *  If you would like to incorporate Link into a proprietary software application,
 *  please contact <link-devs@ableton.com>.
 */

#pragma once

#include <ableton/discovery/AsioTypes.hpp>
#include <ableton/link_audio/ChannelAnnouncements.hpp>
#include <ableton/link_audio/Id.hpp>
#include <ableton/link_audio/PeerAnnouncement.hpp>
#include <ableton/util/Injected.hpp>
#include <algorithm>
#include <cassert>
#include <chrono>
#include <map>
#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
#include <tuple>
#include <vector>

namespace ableton
{
namespace link_audio
{

template <typename IoContext, typename Callback, typename Interface>
class Channels
{
  // non-movable private implementation type
  struct Impl;

public:
  struct SendHandler
  {
    SendHandler(discovery::UdpEndpoint endpoint, std::shared_ptr<Interface> interface)
      : mEndpoint(endpoint)
      , mpInterface(interface)
    {
    }

    std::size_t operator()(const uint8_t* const pData, const size_t numBytes)
    {
      if (auto interface = mpInterface.lock())
      {
        try
        {
          return interface->send(pData, numBytes, mEndpoint);
        }
        catch (const std::runtime_error&)
        {
        }
      }
      return 0;
    }

    discovery::UdpEndpoint endpoint() const { return mEndpoint; }

  private:
    discovery::UdpEndpoint mEndpoint;
    std::weak_ptr<Interface> mpInterface;
  };

  struct Channel
  {
    std::string name;
    Id id;
    std::string peerName;
    Id peerId;
    Id sessionId;

    friend bool operator==(const Channel& lhs, const Channel& rhs)
    {
      return std::tie(lhs.name, lhs.id, lhs.peerName, lhs.peerId, lhs.sessionId)
             == std::tie(rhs.name, rhs.id, rhs.peerName, rhs.peerId, rhs.sessionId);
    }
  };

  struct ChannelInfo
  {
    Channel channel;
    discovery::IpAddress gatewayAddr;
  };

  struct ChannelInfoCompare
  {
    bool operator()(const ChannelInfo& lhs, const ChannelInfo& rhs) const
    {
      if (lhs.channel.sessionId == rhs.channel.sessionId)
      {
        if (lhs.channel.peerId == rhs.channel.peerId)
        {
          if (lhs.channel.id == rhs.channel.id)
          {
            return lhs.gatewayAddr < rhs.gatewayAddr;
          }
          return lhs.channel.channelId < rhs.channel.channelId;
        }
        return lhs.channel.peerId < rhs.channel.peerId;
      }
      return lhs.channel.sessionId < rhs.channel.sessionId;
    }
  };

  Channels(util::Injected<IoContext> io, Callback callback)
    : mpImpl(std::make_shared<Impl>(std::move(io), std::move(callback)))
  {
  }

  std::vector<Channel> sessionChannels(const link::SessionId& sessionId) const
  {
    using namespace std;
    vector<Channel> result;
    auto& channelsVec = mpImpl->mChannels;
    for (const auto& channel : mpImpl->mChannels)
    {
      if (channel.channel.sessionId == sessionId)
      {
        result.push_back(channel.channel);
      }
    }
    return result;
  }

  std::vector<Channel> uniqueSessionChannels(const link::SessionId& sessionId) const
  {
    auto channels = sessionChannels(sessionId);
    auto it = std::unique(begin(channels),
                          end(channels),
                          [](const auto& a, const auto& b) { return a.id == b.id; });
    return {begin(channels), it};
  }

  std::optional<SendHandler> peerSendHandler(const Id peerId) const
  {
    auto it = mpImpl->mPeerSendHandlers.find(peerId);
    return it != mpImpl->mPeerSendHandlers.end() ? std::optional{it->second.handler}
                                                 : std::nullopt;
  }

  std::optional<SendHandler> channelSendHandler(const Id channelId) const
  {
    const auto& channels = mpImpl->mChannels;
    const auto it =
      std::find_if(begin(channels),
                   end(channels),
                   [&](const auto& info) { return info.channel.id == channelId; });
    return it != end(channels) ? peerSendHandler(it->channel.peerId) : std::nullopt;
  }

  template <typename PeerIdIt>
  void prunePeerChannels(PeerIdIt connectedPeersBegin, PeerIdIt connectedPeersEnd)
  {
    mpImpl->prunePeerChannels(connectedPeersBegin, connectedPeersEnd);
  }

  struct GatewayObserver
  {
    using GatewayObserverAnnouncement = PeerAnnouncement;
    using GatewayObserverNodeId = link::NodeId;

    GatewayObserver(std::shared_ptr<Impl> pImpl, discovery::IpAddress addr)
      : mpImpl(std::move(pImpl))
      , mAddr(std::move(addr))
    {
    }

    GatewayObserver(const GatewayObserver&) = delete;

    GatewayObserver(GatewayObserver&& rhs)
      : mpImpl(std::move(rhs.mpImpl))
      , mAddr(std::move(rhs.mAddr))
    {
    }

    ~GatewayObserver()
    {
      // Check to handle the moved from case
      if (mpImpl)
      {
        mpImpl->gatewayClosed(mAddr);
      }
    }

    // model the observer concept from Channels
    template <typename Announcement>
    friend void sawAnnouncement(GatewayObserver& observer, Announcement announcement)
    {
      auto pImpl = observer.mpImpl;
      auto addr = observer.mAddr;
      assert(pImpl);
      pImpl->sawAnnouncementOnGateway(std::move(announcement), std::move(addr));
    }

    template <typename It>
    friend void channelsLeft(GatewayObserver& observer, It channelsBegin, It channelsEnd)
    {
      auto pImpl = observer.mpImpl;
      auto addr = observer.mAddr;
      pImpl->channelsLeftGateway(std::move(addr), channelsBegin, channelsEnd);
    }

    std::shared_ptr<Impl> mpImpl;
    discovery::IpAddress mAddr;
  };

  // Factory function for the gateway observer
  friend GatewayObserver makeGatewayObserver(Channels& channels,
                                             discovery::IpAddress addr)
  {
    return GatewayObserver{channels.mpImpl, std::move(addr)};
  }

private:
  using Timer = typename util::Injected<IoContext>::type::Timer;
  using TimerError = typename Timer::ErrorCode;
  using TimePoint = typename Timer::TimePoint;

  struct TimeoutCompare
  {
    bool operator()(const std::tuple<TimePoint, discovery::IpAddress, Id>& lhs,
                    const std::tuple<TimePoint, discovery::IpAddress, Id>& rhs) const
    {
      return std::get<TimePoint>(lhs) < std::get<TimePoint>(rhs);
    }
  };

  struct Impl
  {
    Impl(util::Injected<IoContext> io, Callback callback)
      : mIo(std::move(io))
      , mCallback(std::move(callback))
      , mPruneTimer(mIo->makeTimer())
    {
    }

    template <typename Announcement>
    void sawAnnouncementOnGateway(Announcement incoming, discovery::IpAddress gatewayAddr)
    {
      using namespace std;

      const auto peerSession = incoming.announcement.sessionId;
      const auto nodeId = incoming.announcement.nodeId;
      const auto peerInfo = incoming.announcement.peerInfo;
      const auto peerAudioChannels = incoming.announcement.channels.channels;
      const auto ttl = incoming.ttl;
      auto sendHandler = SendHandler(incoming.from, incoming.pInterface);
      const auto networkQuality = incoming.networkQuality;
      bool didChannelsChange = false;

      for (const auto& peerChannel : peerAudioChannels)
      {
        const auto channelInfo = ChannelInfo{
          {peerChannel.name, peerChannel.id, peerInfo.name, nodeId, peerSession},
          gatewayAddr};

        {
          const auto timeout =
            std::find_if(begin(mChannelTimeouts),
                         end(mChannelTimeouts),
                         [&](const auto& timeout)
                         {
                           return std::get<discovery::IpAddress>(timeout) == gatewayAddr
                                  && std::get<Id>(timeout) == channelInfo.channel.id;
                         });
          if (timeout != end(mChannelTimeouts))
          {
            mChannelTimeouts.erase(timeout);
          }

          auto newTo = std::make_tuple(mPruneTimer.now() + std::chrono::seconds(ttl),
                                       gatewayAddr,
                                       channelInfo.channel.id);
          mChannelTimeouts.insert(
            upper_bound(
              begin(mChannelTimeouts), end(mChannelTimeouts), newTo, TimeoutCompare{}),
            newTo);
        }

        const auto idRange = equal_range(begin(mChannels),
                                         end(mChannels),
                                         channelInfo,
                                         [](const auto& a, const auto& b)
                                         { return a.channel.id < b.channel.id; });

        if (idRange.first == idRange.second)
        {
          // This channel is not currently known on any gateway
          didChannelsChange = true;
          mChannels.insert(idRange.first, std::move(channelInfo));
        }
        else
        {
          // was it on this gateway?
          const auto addrRange = equal_range(idRange.first,
                                             idRange.second,
                                             channelInfo,
                                             [](const auto& a, const auto& b)
                                             { return a.gatewayAddr < b.gatewayAddr; });

          if (addrRange.first == addrRange.second)
          {
            // First time on this gateway, add it
            mChannels.insert(addrRange.first, std::move(channelInfo));
          }
          else
          {
            // We have an entry for this channel on this gateway, update it
            // callback if the name of the first entry changed

            auto cmp = [](const auto& lhs, const auto& rhs)
            {
              return std::tie(lhs.name, lhs.sessionId, lhs.peerName, lhs.peerId)
                     == std::tie(rhs.name, rhs.sessionId, rhs.peerName, rhs.peerId);
            };
            const auto firstChannelChanged =
              addrRange.first == idRange.first
              && !cmp(addrRange.first->channel, channelInfo.channel);
            didChannelsChange = didChannelsChange || firstChannelChanged;
            *addrRange.first = std::move(channelInfo);
          }
        }
      }

      if (mPeerSendHandlers.count(nodeId) == 0)
      {
        mPeerSendHandlers.emplace(nodeId, PeerSendHandler{sendHandler, networkQuality});
      }
      else if (mPeerSendHandlers.at(nodeId).networkQuality < networkQuality)
      {
        mPeerSendHandlers.insert_or_assign(
          nodeId, PeerSendHandler{sendHandler, networkQuality});
      }

      // Invoke callbacks outside the critical section
      if (didChannelsChange)
      {
        mCallback();
      }

      scheduleNextPruning();
    }

    template <typename PeerIdIt>
    void prunePeerSendHandlers(PeerIdIt connectedPeersBegin, PeerIdIt connectedPeersEnd)
    {
      using namespace std;
      for (auto it = begin(mPeerSendHandlers); it != end(mPeerSendHandlers);)
      {
        if (none_of(connectedPeersBegin,
                    connectedPeersEnd,
                    [&](const auto& peerId) { return peerId == it->first; }))
        {
          it = mPeerSendHandlers.erase(it);
        }
        else
        {
          ++it;
        }
      }
    }

    void pruneSendHandlers()
    {
      using namespace std;
      auto peerIds = vector<Id>{};
      peerIds.reserve(mChannels.size());
      transform(begin(mChannels),
                end(mChannels),
                back_inserter(peerIds),
                [](const auto& info) { return info.channel.peerId; });
      prunePeerSendHandlers(begin(peerIds), end(peerIds));
    }

    template <typename PeerIdIt>
    void prunePeerChannels(PeerIdIt connectedPeersBegin, PeerIdIt connectedPeersEnd)
    {
      using namespace std;

      vector<Id> removedChannelIds;
      for (const auto& info : mChannels)
      {
        if (none_of(connectedPeersBegin,
                    connectedPeersEnd,
                    [&](const auto& peerId) { return peerId == info.channel.peerId; }))
        {
          removedChannelIds.push_back(info.channel.id);
        }
      }

      auto it = remove_if(begin(mChannels),
                          end(mChannels),
                          [&](auto& info)
                          {
                            return none_of(connectedPeersBegin,
                                           connectedPeersEnd,
                                           [&](const auto& peerId)
                                           { return peerId == info.channel.peerId; });
                          });

      const auto channelsChanged = it != end(mChannels);
      mChannels.erase(it, end(mChannels));

      mChannelTimeouts.erase(
        remove_if(begin(mChannelTimeouts),
                  end(mChannelTimeouts),
                  [&](const auto& timeout)
                  {
                    const auto& id = std::get<Id>(timeout);
                    return find(begin(removedChannelIds), end(removedChannelIds), id)
                           != end(removedChannelIds);
                  }),
        end(mChannelTimeouts));

      scheduleNextPruning();

      prunePeerSendHandlers(connectedPeersBegin, connectedPeersEnd);

      if (channelsChanged)
      {
        mCallback();
      }
    }

    template <typename It>
    void channelsLeftGateway(const discovery::IpAddress& gatewayAddr,
                             It channelsBegin,
                             It channelsEnd)
    {
      using namespace std;

      auto channelsChanged = false;

      for (auto byeIt = channelsBegin; byeIt != channelsEnd; ++byeIt)
      {
        auto bye = *byeIt;

        auto it = remove_if(
          begin(mChannels),
          end(mChannels),
          [&](const auto& info)
          { return info.gatewayAddr == gatewayAddr && bye == info.channel.id; });
        channelsChanged = it != end(mChannels);
        mChannels.erase(it, end(mChannels));

        mChannelTimeouts.erase(remove_if(begin(mChannelTimeouts),
                                         end(mChannelTimeouts),
                                         [&](const auto& timeout)
                                         {
                                           return std::get<discovery::IpAddress>(timeout)
                                                    == gatewayAddr
                                                  && std::get<Id>(timeout) == bye;
                                         }),
                               end(mChannelTimeouts));
      }

      scheduleNextPruning();

      if (channelsChanged)
      {
        pruneSendHandlers();
        mCallback();
      }
    }

    void gatewayClosed(const discovery::IpAddress& gatewayAddr)
    {
      using namespace std;
      auto it =
        remove_if(begin(mChannels),
                  end(mChannels),
                  [&](const auto& info) { return info.gatewayAddr == gatewayAddr; });

      const auto channelsChanged = it != end(mChannels);

      mChannels.erase(it, end(mChannels));

      mChannelTimeouts.erase(
        std::remove_if(
          begin(mChannelTimeouts),
          end(mChannelTimeouts),
          [&](const auto& timeout)
          { return std::get<discovery::IpAddress>(timeout) == gatewayAddr; }),
        end(mChannelTimeouts));

      scheduleNextPruning();

      if (channelsChanged)
      {
        pruneSendHandlers();
        mCallback();
      }
    }

    void pruneExpiredChannels()
    {
      using namespace std;

      const auto test = make_tuple(mPruneTimer.now(), discovery::IpAddress{}, Id{});

      const auto endExpired = lower_bound(
        begin(mChannelTimeouts), end(mChannelTimeouts), test, TimeoutCompare{});

      auto channelsChanged = false;
      if (endExpired != begin(mChannelTimeouts))
      {
        channelsChanged = true;

        for_each(
          begin(mChannelTimeouts),
          endExpired,
          [&](const auto& timeout)
          {
            const auto& id = std::get<Id>(timeout);
            const auto& gatewayAddr = std::get<discovery::IpAddress>(timeout);

            auto it = remove_if(
              begin(mChannels),
              end(mChannels),
              [&](const auto& info)
              { return info.channel.id == id && info.gatewayAddr == gatewayAddr; });

            mChannels.erase(it, end(mChannels));
          });
      }

      mChannelTimeouts.erase(begin(mChannelTimeouts), endExpired);

      scheduleNextPruning();

      if (channelsChanged)
      {
        pruneSendHandlers();
        mCallback();
      }
    }

    void scheduleNextPruning()
    {
      if (!mChannelTimeouts.empty())
      {
        // Add a second of padding to the timer to avoid over-eager timeouts
        const auto t =
          std::get<TimePoint>(mChannelTimeouts.front()) + std::chrono::seconds(1);
        mPruneTimer.expires_at(t);
        mPruneTimer.async_wait(
          [this](const TimerError e)
          {
            if (!e)
            {
              pruneExpiredChannels();
            }
          });
      }
    }

    util::Injected<IoContext> mIo;
    Callback mCallback;
    std::vector<ChannelInfo> mChannels;

    struct PeerSendHandler
    {
      SendHandler handler;
      double networkQuality;
    };
    std::map<Id, PeerSendHandler> mPeerSendHandlers;

    using ChannelTimeout = std::tuple<TimePoint, discovery::IpAddress, Id>;
    using ChannelTimeouts = std::vector<ChannelTimeout>;
    ChannelTimeouts mChannelTimeouts;
    Timer mPruneTimer;
  };

  std::shared_ptr<Impl> mpImpl;
};

} // namespace link_audio
} // namespace ableton