QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
RtpPacketCounter.cc
Go to the documentation of this file.
1#include "RtpPacketCounter.h"
2
3#include <algorithm>
4#include <cstddef>
5#include <cstdlib>
6
7namespace {
8
9constexpr qint64 kRtpNsPerSecond = 1'000'000'000;
10
12constexpr int rtpSequencesAhead(quint16 sequence, quint16 reference)
13{
14 return static_cast<qint16>(static_cast<quint16>(sequence - reference));
15}
16
17} // namespace
18
20 : _clockRate(clockRate)
21{}
22
23void RtpPacketCounter::add(quint16 sequence, quint32 timestamp, quint32 ssrc, std::optional<qint64> arrivalNs)
24{
25 if (!_ssrc) {
26 _startSequence(sequence, ssrc);
27 } else if (!_continueSequence(sequence, ssrc)) {
28 return;
29 }
30 _notePacketRate(timestamp);
31 if (arrivalNs) {
32 _noteArrival(timestamp, *arrivalNs);
33 }
34}
35
36void RtpPacketCounter::_startSequence(quint16 sequence, quint32 ssrc)
37{
38 _lostBefore += _sequenceLost();
39 _receivedBefore += _received;
40
41 _ssrc = ssrc;
42 _baseSeq = sequence;
43 _maxSeq = sequence;
44 _received = 1;
45 _seen.reset();
46 _seen.set(0);
47 _restart.reset();
48 // Its timestamps have an origin of their own.
49 _lastArrivalNs.reset();
50 _rateSeq.reset();
51}
52
53bool RtpPacketCounter::_continueSequence(quint16 sequence, quint32 ssrc)
54{
55 const qint64 maxDropout = _maxDropout();
56 int ahead = rtpSequencesAhead(sequence, static_cast<quint16>(_maxSeq));
57 if ((ssrc != *_ssrc) || (ahead >= maxDropout) || (ahead <= -kReorderWindow)) {
58 // RFC 3550 starts over only with the very next sequence number, which a sender that restarted into a lossy
59 // link may never show.
60 const int afterRestart =
61 (_restart && (_restart->ssrc == ssrc)) ? rtpSequencesAhead(sequence, _restart->sequence) : 0;
62 if ((afterRestart <= 0) || (afterRestart >= maxDropout)) {
63 _restart = Restart{.ssrc = ssrc, .sequence = sequence};
64 return false;
65 }
66 _startSequence(_restart->sequence, ssrc);
67 ahead = afterRestart;
68 }
69 _restart.reset();
70
71 if (ahead > 0) {
72 _seen <<= static_cast<std::size_t>(ahead);
73 _seen.set(0);
74 _maxSeq += ahead;
75 ++_received;
76 return true;
77 }
78
79 const auto behind = static_cast<std::size_t>(-ahead);
80 if (_seen.test(behind) || ((_maxSeq + ahead) < _baseSeq)) {
81 return false;
82 }
83 _seen.set(behind);
84 ++_received;
85 return true;
86}
87
88quint64 RtpPacketCounter::_sequenceLost() const
89{
90 // Never negative: every packet counted is one of these sequence numbers, counted once.
91 return _ssrc ? (static_cast<quint64>(_maxSeq - _baseSeq + 1) - _received) : 0;
92}
93
94qint64 RtpPacketCounter::_maxDropout() const
95{
96 return (_packetRate > 0) ? std::max<qint64>(_packetRate * kMaxDropoutSeconds, kMinDropout) : kDefaultMaxDropout;
97}
98
99void RtpPacketCounter::_notePacketRate(quint32 timestamp)
100{
101 if (_clockRate <= 0) {
102 return;
103 }
104 if (!_rateSeq) {
105 _rateSeq = _maxSeq;
106 _rateTimestamp = timestamp;
107 return;
108 }
109 // Over a second of the sender's time at least: the packets of a frame share a timestamp. Negative for a packet
110 // sent before the one the measurement started at.
111 const qint64 ticks = static_cast<qint32>(timestamp - _rateTimestamp);
112 if (ticks < _clockRate) {
113 return;
114 }
115 const qint64 rate = (_maxSeq - *_rateSeq) * _clockRate / ticks;
116 // A rate that fell lowers the limit only gradually: the sender may be back at the higher one when packets are
117 // lost.
118 _packetRate = (rate >= _packetRate) ? rate : ((3 * _packetRate) + rate) / 4;
119 _rateSeq = _maxSeq;
120 _rateTimestamp = timestamp;
121}
122
123void RtpPacketCounter::_noteArrival(quint32 timestamp, qint64 arrivalNs)
124{
125 if (_clockRate <= 0) {
126 return;
127 }
128 if (_lastArrivalNs) {
129 // Timestamps wrap at 32 bits, and go back for a packet sent before the previous one.
130 const qint64 sentApartNs =
131 static_cast<qint64>(static_cast<qint32>(timestamp - _lastTimestamp)) * kRtpNsPerSecond / _clockRate;
132 const qint64 deviationNs = std::abs((arrivalNs - *_lastArrivalNs) - sentApartNs);
133 _jitterNs = (deviationNs + (15 * _jitterNs.value_or(0))) / 16;
134 }
135 _lastArrivalNs = arrivalNs;
136 _lastTimestamp = timestamp;
137}
static constexpr int kMinDropout
RtpPacketCounter(int clockRate)
clockRate: of the RTP timestamps, in Hz. Jitter is measured only with one.
static constexpr int kDefaultMaxDropout
void add(quint16 sequence, quint32 timestamp, quint32 ssrc, std::optional< qint64 > arrivalNs)
static constexpr int kMaxDropoutSeconds
static constexpr int kReorderWindow
GPSCommandSequence sequence(std::span< const Setting > settings, char port)
settings for port, each acknowledged by an ACK.