QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
StreamWatchdog.cc
Go to the documentation of this file.
1#include "StreamWatchdog.h"
2
3#include <algorithm>
4
5#include "MonotonicClock.h"
6
7namespace {
8
9constexpr qint64 kMinTimeoutMs = 1000;
10// The decoder may lag the source by up to this many timeouts before it counts as stalled.
11constexpr qint64 kDecoderTimeoutFactor = 2;
12
13} // namespace
14
16{
17 return MonotonicClock::nowMs();
18}
19
20void StreamWatchdog::start(std::chrono::seconds timeout, std::optional<std::chrono::milliseconds> firstFrameGrace)
21{
22 // A zero timeout would otherwise trip on every check.
23 _timeoutMs = std::max<qint64>(std::chrono::milliseconds(timeout).count(), kMinTimeoutMs);
24 _firstFrameGraceMs.reset();
25 if (firstFrameGrace) {
26 _firstFrameGraceMs = std::max<qint64>(firstFrameGrace->count(), 0);
27 }
28 _sourceFrames.store(0, std::memory_order_relaxed);
29 _lastSourceFrameMs.store(0, std::memory_order_relaxed);
30 _lastSinkFrameMs.store(0, std::memory_order_relaxed);
31 _sinkFrameSeen.store(false, std::memory_order_relaxed);
32}
33
35{
36 // The time before the count, which publishes it: check() reads them the other way round.
37 _lastSourceFrameMs.store(nowMs, std::memory_order_relaxed);
38 return _sourceFrames.fetch_add(1, std::memory_order_release) + 1;
39}
40
42{
43 _lastSinkFrameMs.store(nowMs, std::memory_order_relaxed);
44 return !_sinkFrameSeen.exchange(true, std::memory_order_relaxed);
45}
46
48{
49 _lastSinkFrameMs.store(nowMs, std::memory_order_relaxed);
50}
51
53{
54 _lastSinkFrameMs.store(nowMs, std::memory_order_relaxed);
55 _sinkFrameSeen.store(false, std::memory_order_relaxed);
56}
57
58StreamWatchdog::Result StreamWatchdog::check(qint64 nowMs, bool displaying)
59{
60 // The count before the time: a counted frame's time is then the one read, or a later one. Read the other way
61 // round, a first frame noted in between would get the short timeout measured from before it, a false stall.
62 const bool firstFrameSeen = _sourceFrames.load(std::memory_order_acquire) > 0;
63 qint64 lastSourceFrameMs = 0;
64 // The first check starts the clock, unless a frame's time got there first.
65 if (_lastSourceFrameMs.compare_exchange_strong(lastSourceFrameMs, nowMs, std::memory_order_relaxed)) {
66 lastSourceFrameMs = nowMs;
67 }
68
69 if (firstFrameSeen || _firstFrameGraceMs) {
70 const qint64 sourceTimeoutMs = firstFrameSeen ? _timeoutMs : (_timeoutMs + *_firstFrameGraceMs);
71 const qint64 sourceSilentMs = nowMs - lastSourceFrameMs;
72 if (sourceSilentMs > sourceTimeoutMs) {
73 return {.stall = Stall::Source, .silentMs = sourceSilentMs};
74 }
75 }
76
77 if (displaying) {
78 const qint64 sinkSilentMs = nowMs - _lastSinkFrameMs.load(std::memory_order_relaxed);
79 if (sinkSilentMs > (_timeoutMs * kDecoderTimeoutFactor)) {
80 return {.stall = Stall::Decoder, .silentMs = sinkSilentMs};
81 }
82 }
83
84 return {};
85}
bool noteSinkFrame(qint64 nowMs)
Returns true for the first sink frame since startSinkClock().
void start(std::chrono::seconds timeout, std::optional< std::chrono::milliseconds > firstFrameGrace=std::chrono::milliseconds{0})
Result check(qint64 nowMs, bool displaying)
void startSinkClock(qint64 nowMs)
A decoder starts displaying at nowMs: its silence counts from here until the first sink frame.
void noteFrameWithheld(qint64 nowMs)
quint64 noteSourceFrame(qint64 nowMs)
Returns the number of source frames since start().
static qint64 now()
@ Decoder
Displaying, but no frame reached the video sink within twice the timeout.
@ Source
No frame reached the tee within the timeout.