summaryrefslogtreecommitdiff
path: root/Modulator/JttyTxStream.cpp
blob: 5e65c1cd78fc99fca4b5fc193582858200bdfb8e (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
#include "JttyTxStream.hpp"

#include "Audio/soundout.h"
#include "Logger.hpp"

#include "moc_JttyTxStream.cpp"

namespace
{
  // Trailing silence (samples) served past the last real sample before we trust
  // the queue has physically played out, when the device buffer size is unknown.
  // Generous by design: it only lengthens the PTT tail after the final message,
  // never the gap between chained messages.
  constexpr qint64 DEFAULT_DRAIN_GUARD = 9600;   // 200 ms at 48 kHz
  constexpr qint64 DRAIN_GUARD_MARGIN  = 4800;   // 100 ms safety margin
}

TxEvidence::TxStartSnapshot makeJttyTxStartSnapshot (TxEvidence::TxRequest const& request,
                                                      qint64 committedEndSample)
{
  TxEvidence::TxStartSnapshot snapshot;
  snapshot.session_id = request.session_id;
  snapshot.generation = request.generation;
  snapshot.mode = request.mode;
  snapshot.sample_rate_hz = 48000;
  snapshot.committed_end_sample = committedEndSample;
  snapshot.target_known = false;
  snapshot.diagnostic = "JTTY PCM extent may grow while messages are queued";
  return snapshot;
}

JttyTxStream::JttyTxStream (TxAudioQueue& queue, QObject * parent)
  : AudioDevice {parent}
  , m_queue {queue}
  , m_drainGuard {DEFAULT_DRAIN_GUARD}
  , m_drainTimer {new QTimer {this}}
  , m_active {false}
{
  m_drainTimer->setInterval (25);
  connect (m_drainTimer, &QTimer::timeout, this, &JttyTxStream::pollDrain);
}

void JttyTxStream::start (TxEvidence::TxRequest request, SoundOutput * stream)
{
  if (m_active) return;
  auto progress = m_queue.progress ();
  if (progress.epoch != request.queue_epoch)
    {
      LOG_DEBUG ("JTTY transmit stream start rejected before pending reset; "
                 "requested queue epoch=" << request.queue_epoch.value ()
                 << " current epoch=" << progress.epoch.value ());
      return;
    }
  m_queue.applyPendingReset ();
  progress = m_queue.progress ();
  if (progress.epoch != request.queue_epoch)
    {
      LOG_DEBUG ("JTTY transmit stream start rejected after pending reset; "
                 "requested queue epoch=" << request.queue_epoch.value ()
                 << " current epoch=" << progress.epoch.value ());
      return;
    }
  initialize (QIODevice::ReadOnly, request.channel);
  m_active = true;
  m_stream = stream;
  if (m_stream)
    {
      m_stream->restart (this);
      // Size the drain guard to the actual device buffer (now valid) plus a
      // margin; same audio thread as SoundOutput, so this is not a cross-thread
      // QAudioOutput access.
      qint64 const bufFrames = bytesPerFrame ()
        ? qint64 (m_stream->bufferSize ()) / qint64 (bytesPerFrame ())
        : 0;
      m_drainGuard.store (qMax (bufFrames, DEFAULT_DRAIN_GUARD) + DRAIN_GUARD_MARGIN,
                           std::memory_order_release);
    }
  if (!m_drainTimer->isActive ()) m_drainTimer->start ();
  Q_EMIT txSourceCommitted (makeJttyTxStartSnapshot (
    request, progress.total_samples > 0 ? progress.total_samples - 1 : -1));
}

void JttyTxStream::stop ()
{
  if (m_stream)
    {
      m_stream->stop ();
  }
  m_active = false;
  m_drainTimer->stop ();
  AudioDevice::close ();
  // Do not drop queued PCM here. A GUI stop for the previous session can cross
  // with the next enqueue; clear() is the explicit abort path.
}

qint64 JttyTxStream::readData (char * data, qint64 maxSize)
{
  if (maxSize == 0) return 0;
  Q_ASSERT (!(maxSize % qint64 (bytesPerFrame ()))); // no torn frames

  qint64 const numFrames {maxSize / qint64 (bytesPerFrame ())};
  qint16 * samples {reinterpret_cast<qint16 *> (data)};

  qint64 const drainGuard = m_drainGuard.load (std::memory_order_acquire);
  for (qint64 frame = 0; frame < numFrames; ++frame)
    {
      samples = load (m_queue.pullSample (drainGuard), samples);
    }

  return numFrames * qint64 (bytesPerFrame ());
}

void JttyTxStream::pollDrain ()
{
  auto const drain = m_queue.takeDrainReady ();
  if (drain.ready)
    {
      Q_EMIT drained (drain);
    }
}