summaryrefslogtreecommitdiff
path: root/Transceiver/TxInhibitTransceiver.cpp
blob: 6261432beee161f8889d6b0d098bafac74930b41 (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
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
#include "TxInhibitTransceiver.hpp"

#include <algorithm>
#include <limits>

#include <QSet>
#include <QStringList>
#include <QTimer>

#include "moc_TxInhibitTransceiver.cpp"

namespace
{
  QString const overflow_hold_key;

  QString sanitize_holder (QString const& value)
  {
    QString result;
    result.reserve (std::min (value.size (), 64));
    for (auto const character : value.left (64))
      {
        if (character.isPrint ()) result.append (character);
      }
    return result.trimmed ();
  }
}

TxInhibitTransceiver::TxInhibitTransceiver (logger_type * logger,
                                            std::unique_ptr<Transceiver> wrapped)
  : Transceiver {logger, nullptr}
  , wrapped_ {std::move (wrapped)}
  , expiry_timer_ {new QTimer {this}}
{
  Q_ASSERT (wrapped_);

  expiry_timer_->setSingleShot (true);
  connect (expiry_timer_, &QTimer::timeout, this, &TxInhibitTransceiver::expire_holds);

  connect (wrapped_.get (), &Transceiver::resolution, this, &Transceiver::resolution);
  connect (wrapped_.get (), &Transceiver::tciframeswritten, this, &Transceiver::tciframeswritten);
  connect (wrapped_.get (), &Transceiver::receiveAudio, this, &Transceiver::receiveAudio);
  connect (wrapped_.get (), &Transceiver::tci_mod_active, this, &Transceiver::tci_mod_active);
  connect (wrapped_.get (), &Transceiver::txSourceCommitted, this, &Transceiver::txSourceCommitted);
  connect (wrapped_.get (), &Transceiver::rawTxPlayoutSnapshot, this, &Transceiver::rawTxPlayoutSnapshot);
  connect (wrapped_.get (), &Transceiver::jtty_drained, this, &Transceiver::jtty_drained);
  connect (wrapped_.get (), &Transceiver::jtty_enqueue_accepted, this, &Transceiver::jtty_enqueue_accepted);
  connect (wrapped_.get (), &Transceiver::jtty_enqueue_failed, this, &Transceiver::jtty_enqueue_failed);
  connect (wrapped_.get (), &Transceiver::update, this, &Transceiver::update);
  connect (wrapped_.get (), &Transceiver::finished, this, &Transceiver::finished);
  connect (wrapped_.get (), &Transceiver::failure, this, &Transceiver::failure);
}

void TxInhibitTransceiver::set (TransceiverState const& state,
                                unsigned sequence_number) noexcept
{
  requested_ = state;
  sequence_number_ = sequence_number;
  have_requested_state_ = true;
  apply_requested_state ();
}

void TxInhibitTransceiver::start (unsigned sequence_number) noexcept
{
  sequence_number_ = sequence_number;
  have_effective_state_ = false;
  monotonic_clock_.start ();
  started_ = true;
  emit_status ();
  wrapped_->start (sequence_number);
}

void TxInhibitTransceiver::stop () noexcept
{
  if (!started_) return;

  expiry_timer_->stop ();
  holds_.clear ();
  started_ = false;
  emit_status ();
  wrapped_->stop ();
  have_requested_state_ = false;
  have_effective_state_ = false;
}

void TxInhibitTransceiver::enqueue_jtty_pcm (QByteArray const& samples,
                                             TxAudioQueueEpoch epoch,
                                             qint64 enqueue_id) noexcept
{
  wrapped_->enqueue_jtty_pcm (samples, epoch, enqueue_id);
}

void TxInhibitTransceiver::clear_jtty_pcm (TxAudioQueueEpoch epoch) noexcept
{
  wrapped_->clear_jtty_pcm (epoch);
}

void TxInhibitTransceiver::apply_requested_state () noexcept
{
  if (!have_requested_state_ || !started_) return;

  auto effective = requested_;
  if (!holds_.isEmpty ())
    {
      effective.ptt (false);
      effective.tune (false);
    }

  auto const previous_ptt = have_effective_state_ && last_effective_.ptt ();
  auto final_sent = false;
  if (effective.ptt () != previous_ptt)
    {
      if (effective.ptt ())
        {
          auto prepare = effective;
          prepare.ptt (false);
          if (!have_effective_state_ || prepare != last_effective_)
            {
              wrapped_->set (prepare, sequence_number_);
            }
        }
      else
        {
          auto unkey = last_effective_;
          unkey.ptt (false);
          wrapped_->set (unkey, sequence_number_);
          final_sent = unkey == effective;
        }
    }
  if (!final_sent) wrapped_->set (effective, sequence_number_);
  last_effective_ = effective;
  have_effective_state_ = true;
}

void TxInhibitTransceiver::emit_status ()
{
  Q_EMIT statusChanged (started_, started_ && !holds_.isEmpty (),
                        holder_summary (), hold_rx_, release_rx_, expiries_, invalid_);
}

void TxInhibitTransceiver::tx_inhibit_invalid (quint64 count)
{
  if (!started_ || !count) return;
  invalid_ += static_cast<quint32> (count);
  emit_status ();
}

void TxInhibitTransceiver::expire_holds ()
{
  auto const was_inhibited = !holds_.isEmpty ();
  auto const previous_holder = holder_summary ();
  auto expired = false;
  auto const now = monotonic_clock_.elapsed ();
  for (auto it = holds_.begin (); it != holds_.end ();)
    {
      if (it->expires_at <= now)
        {
          it = holds_.erase (it);
          ++expiries_;
          expired = true;
        }
      else
        {
          ++it;
        }
    }

  if (was_inhibited != !holds_.isEmpty ()) apply_requested_state ();
  if (expired || was_inhibited != !holds_.isEmpty ()
      || previous_holder != holder_summary ())
    {
      emit_status ();
    }
  schedule_expiry ();
}

QString TxInhibitTransceiver::holder_summary () const
{
  QStringList holders;
  QSet<QString> seen;
  for (auto it = holds_.cbegin (); it != holds_.cend (); ++it)
    {
      if (!it->holder.isEmpty () && !seen.contains (it->holder))
        {
          seen.insert (it->holder);
          holders.append (it->holder);
        }
    }
  holders.sort (Qt::CaseInsensitive);
  return holders.join (QStringLiteral (", "));
}

void TxInhibitTransceiver::tx_inhibit_command (QString controller, quint32 ttl_ms, QString station)
{
  if (!started_) return;
  auto const was_inhibited = !holds_.isEmpty ();
  if (!ttl_ms)
    {
      ++release_rx_;
      holds_.remove (controller);
    }
  else
    {
      ++hold_rx_;
      auto const expires_at = monotonic_clock_.elapsed () + ttl_ms;
      auto const holder = sanitize_holder (station.isEmpty () ? controller : station);
      auto existing = holds_.find (controller);
      if (existing != holds_.end ())
        {
          *existing = Hold {expires_at, holder};
        }
      else
        {
          auto const tracked_count = holds_.size ()
            - (holds_.contains (overflow_hold_key) ? 1 : 0);
          if (tracked_count < maximum_tracked_holds)
            {
              holds_.insert (controller, Hold {expires_at, holder});
            }
          else
            {
              auto overflow = holds_.find (overflow_hold_key);
              if (overflow == holds_.end ())
                {
                  holds_.insert (overflow_hold_key, Hold {expires_at, {}});
                }
              else
                {
                  overflow->expires_at = std::max (overflow->expires_at, expires_at);
                }
            }
        }
    }

  if (was_inhibited != !holds_.isEmpty ()) apply_requested_state ();
  emit_status ();
  schedule_expiry ();
}

void TxInhibitTransceiver::schedule_expiry ()
{
  if (holds_.isEmpty ())
    {
      expiry_timer_->stop ();
      return;
    }

  auto next_expiry = std::numeric_limits<qint64>::max ();
  for (auto it = holds_.cbegin (); it != holds_.cend (); ++it)
    {
      next_expiry = std::min (next_expiry, it->expires_at);
    }
  auto const delay = std::max<qint64> (1, next_expiry - monotonic_clock_.elapsed ());
  expiry_timer_->start (static_cast<int> (std::min<qint64> (delay,
                                                            std::numeric_limits<int>::max ())));
}