summaryrefslogtreecommitdiff
path: root/Audio/TxAudioQueue.cpp
blob: 9217d199d20452a0851f59d3dc5a58e9c5da4da9 (plain)
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
#include "TxAudioQueue.hpp"

#include <atomic>

#include "TxAudioQueueEpochSnapshot.hpp"
#include "Modulator/JttyPcmFifo.hpp"

class TxAudioQueue::Impl
{
public:
  explicit Impl (qint64 capacity_samples)
    : fifo {capacity_samples}
  {
  }

  bool tryEpoch (TxAudioQueueEpoch& result) const noexcept
  {
    auto const before = epoch_sequence.load (std::memory_order_acquire);
    if (before & 1)
      {
        return false;
      }

    auto const current_epoch = epoch.load (std::memory_order_relaxed);
    // Keep payload reads ahead of the sequence validation load.
    std::atomic_thread_fence (std::memory_order_acquire);
    auto const after = epoch_sequence.load (std::memory_order_relaxed);
    return TxAudioQueueDetail::tryMakeEpochSnapshot (
      before, current_epoch, after, result);
  }

  void clear (TxAudioQueueEpoch value)
  {
    epoch_sequence.fetch_add (1, std::memory_order_acq_rel);
    fifo.clear (value.value ());
    epoch.store (value.value (), std::memory_order_relaxed);
    epoch_sequence.fetch_add (1, std::memory_order_release);
  }

  JttyPcmFifo fifo;
  std::atomic<qint64> epoch {0};
  std::atomic<quint64> epoch_sequence {0};
};

TxAudioQueue::TxAudioQueue ()
  : TxAudioQueue {defaultCapacity ()}
{
}

TxAudioQueue::TxAudioQueue (qint64 capacity_samples)
  : impl_ {new Impl {capacity_samples}}
{
}

TxAudioQueue::~TxAudioQueue () = default;

TxAudioQueueEnqueueResult TxAudioQueue::enqueue (
    QVector<qint16> const& samples, TxAudioQueueEpoch epoch)
{
  return enqueue (samples.constData (), samples.size (), epoch);
}

TxAudioQueueEnqueueResult TxAudioQueue::enqueue (
    qint16 const * samples, qint64 count, TxAudioQueueEpoch epoch)
{
  bool accepted {false};
  TxAudioQueueEpoch current_epoch;
  if (impl_->tryEpoch (current_epoch) && epoch == current_epoch)
    {
      accepted = impl_->fifo.enqueue (samples, count, epoch.value ());
    }
  return TxAudioQueueEnqueueResult {accepted, progress ()};
}

void TxAudioQueue::clear (TxAudioQueueEpoch epoch)
{
  impl_->clear (epoch);
}

void TxAudioQueue::applyPendingReset () noexcept
{
  impl_->fifo.applyPendingReset ();
}

qint16 TxAudioQueue::pullSample (qint64 drain_guard) noexcept
{
  return impl_->fifo.pullSample (drain_guard);
}

TxAudioQueueDrainState TxAudioQueue::takeDrainReady () noexcept
{
  TxAudioQueueEpoch epoch_before;
  if (!impl_->tryEpoch (epoch_before))
    {
      return TxAudioQueueDrainState {};
    }

  auto const raw = impl_->fifo.takeDrainReady ();
  TxAudioQueueEpoch epoch;
  if (!impl_->tryEpoch (epoch) || !raw.ready
      || epoch_before != epoch
      || raw.epoch != epoch.value ())
    {
      return TxAudioQueueDrainState {false, epoch, 0};
    }
  return TxAudioQueueDrainState {true, epoch, raw.totalAtDrain};
}

TxAudioQueueProgress TxAudioQueue::progress () const noexcept
{
  TxAudioQueueEpoch epoch;
  impl_->tryEpoch (epoch);
  return TxAudioQueueProgress {
    epoch,
    impl_->fifo.queuedReal (),
    impl_->fifo.servedReal (),
    impl_->fifo.totalReal ()
  };
}

qint64 TxAudioQueue::capacity () const noexcept
{
  return impl_->fifo.capacity ();
}