QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
VideoStreamStatsMeter.cc
Go to the documentation of this file.
2
3#include <utility>
4
5#include "MonotonicClock.h"
6#include "VideoStreamStats.h"
7
8namespace {
9
11quint64 statsCounterGrowth(quint64 now, quint64 before)
12{
13 return (now >= before) ? (now - before) : now;
14}
15
16} // namespace
17
19 : _clock(clock ? std::move(clock) : Clock(MonotonicClock::nowMs))
20{}
21
23{
24 _lastSampleMs.reset();
25 _last = {};
26 _framesDisplayed = 0;
27 _bytesReceived = 0;
28 _framesDropped = 0;
29 _packetsLost = 0;
30 _window.clear();
31}
32
34{
35 const qint64 nowMs = _clock();
36 if (_lastSampleMs && (nowMs <= *_lastSampleMs)) {
37 return false;
38 }
39
40 // Totals count from reset(), the baseline sample included.
41 _framesDisplayed += statsCounterGrowth(counters.framesDisplayed, _last.framesDisplayed);
42 _bytesReceived += statsCounterGrowth(counters.bytesReceived.value_or(0), _last.bytesReceived.value_or(0));
43 const quint64 framesDropped = statsCounterGrowth(counters.framesDropped, _last.framesDropped);
44 _framesDropped += framesDropped;
45 quint64 packetsReceived = 0;
46 quint64 packetsLost = 0;
47 if (counters.rtp) {
48 const RtpCounters last = _last.rtp.value_or(RtpCounters{});
49 packetsReceived = statsCounterGrowth(counters.rtp->packetsReceived, last.packetsReceived);
50 packetsLost = statsCounterGrowth(counters.rtp->packetsLost, last.packetsLost);
51 _packetsLost += packetsLost;
52 }
53
54 const std::optional<qint64> lastSampleMs = std::exchange(_lastSampleMs, nowMs);
55 _last = counters;
56 _window.push_back({.ms = nowMs, .framesDisplayed = _framesDisplayed, .bytesReceived = _bytesReceived});
57 if (_window.size() > static_cast<std::size_t>(kRateWindowIntervals) + 1) {
58 _window.pop_front();
59 }
60 if (!lastSampleMs) {
61 return false;
62 }
63
64 const double seconds = static_cast<double>(nowMs - *lastSampleMs) / 1000.0;
65 const RatePoint& oldest = _window.front();
66 const double windowSeconds = static_cast<double>(nowMs - oldest.ms) / 1000.0;
67 stats.valid = true;
68 stats.fps = static_cast<double>(_framesDisplayed - oldest.framesDisplayed) / windowSeconds;
69 stats.bitrateKbps =
70 counters.bytesReceived
71 ? (static_cast<double>(_bytesReceived - oldest.bytesReceived) * 8.0 / 1000.0 / windowSeconds)
72 : -1.0;
73 stats.framesDropped = static_cast<qint64>(_framesDropped);
74 stats.droppedFps = static_cast<double>(framesDropped) / seconds;
75 if (counters.rtp) {
76 const quint64 packetsDue = packetsReceived + packetsLost;
77 stats.packetsLost = static_cast<qint64>(_packetsLost);
78 stats.lossPercent =
79 (packetsDue > 0) ? (100.0 * static_cast<double>(packetsLost) / static_cast<double>(packetsDue)) : 0.0;
80 } else {
81 stats.packetsLost = -1;
82 stats.lossPercent = -1.0;
83 }
84 return true;
85}
void reset()
A new stream starts: its counters start from zero, and the next update() only sets the baseline for r...
VideoStreamStatsMeter(Clock clock={})
clock defaults to the steady clock.
static constexpr int kRateWindowIntervals
bool update(const Counters &counters, VideoStreamStats &stats)
std::function< qint64()> Clock
Monotonic milliseconds.
std::optional< quint64 > bytesReceived
Only for a backend that counts the bytes it receives: bitrateKbps is -1 without.
std::optional< RtpCounters > rtp
Only for an RTP stream whose packets the backend counts.
double lossPercent
Share of the RTP packets due in the last interval that were lost.
qint64 framesDropped
Since the stream started: frames the display dropped to keep up with the stream.
bool valid
Measured at least once for the current stream; the other fields mean nothing while false.
qint64 packetsLost
RTP packets lost since the stream started.
double droppedFps
Frames dropped per second in the last interval.