QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
StreamSupervisor.cc
Go to the documentation of this file.
1#include "StreamSupervisor.h"
2
3#include <algorithm>
4
6 : QObject(parent)
7 , _backoff(backoff)
8 , _reconnectTimer(this)
9{
10 _reconnectTimer.setSingleShot(true);
11 // Backoff delays are asserted by tests; a coarse timer may fire up to 5% early.
12 _reconnectTimer.setTimerType(Qt::PreciseTimer);
13 (void) connect(&_reconnectTimer, &QTimer::timeout, this, &StreamSupervisor::reconnectDue);
14}
15
31
33{
34 return std::max(0, _streamedSessions - 1);
35}
36
37std::chrono::milliseconds StreamSupervisor::uptime() const
38{
39 return std::chrono::milliseconds(_sinceStreaming.isValid() ? _sinceStreaming.elapsed() : 0);
40}
41
43{
44 _reconnectTimer.stop();
45 _backoff.reset();
46 _streamedSessions = 0;
47 _haltedPermanently = false;
48 if (_phase == Phase::Halted) {
49 _setPhase(Phase::Idle);
50 }
51}
52
54{
55 _reconnectTimer.stop();
56 _endSession();
57 _haltedPermanently = false;
58 _setPhase(Phase::Idle);
59}
60
62{
63 _reconnectTimer.stop();
64 _endSession();
65 _setPhase(Phase::Connecting);
66}
67
69{
70 if (!_sessionStreamed) {
71 _sessionStreamed = true;
72 ++_streamedSessions;
73 _sinceStreaming.start();
74 }
75 _setPhase(Phase::Streaming);
76}
77
79 bool autoReconnect)
80{
81 const bool permanent = (failure == Failure::Permanent);
82 _lastFailure = message;
83 if (_sessionStreamed) {
84 _backoff.reset();
85 }
86 _endSession();
87
88 if (permanent || !autoReconnect) {
89 _reconnectTimer.stop();
90 _haltedPermanently = permanent;
91 _setPhase(Phase::Halted);
92 return {.halted = true};
93 }
94
95 const std::chrono::milliseconds delay = _backoff.next();
96 _reconnectTimer.start(delay);
97 _setPhase(Phase::Reconnecting);
98 return {.halted = false, .reconnectDelay = delay};
99}
100
102{
103 if (!autoReconnect && _reconnectTimer.isActive()) {
104 _reconnectTimer.stop();
105 _haltedPermanently = false;
106 _setPhase(Phase::Halted);
108 }
109 if (autoReconnect && (_phase == Phase::Halted) && !_haltedPermanently) {
110 _setPhase(Phase::Idle);
112 }
114}
115
117{
118 if (!_reconnectTimer.isActive()) {
119 return;
120 }
121 _reconnectTimer.stop();
122 _backoff.reset();
123 emit reconnectDue();
124}
125
126void StreamSupervisor::_setPhase(Phase phase)
127{
128 if (phase == _phase) {
129 return;
130 }
131 const VideoReceiver::State previous = state();
132 _phase = phase;
133 if (state() != previous) {
134 emit stateChanged(state());
135 }
136}
137
138void StreamSupervisor::_endSession()
139{
140 _sessionStreamed = false;
141 _sinceStreaming.invalidate();
142}
Retry delays that start at an initial value and grow by a factor per attempt, up to a cap.
@ Halted
Turned off while a reconnect was pending: the stream halted.
@ Resumable
Turned on while halted after a failure that wasn't permanent: the halt is lifted.
int reconnects() const
For VideoStreamStats::reconnects: sessions that streamed since startOver(), less the first.
void sessionStreaming()
The session delivers. The first call per session counts it as streamed.
void sessionStarted()
A new session runs.
FailureOutcome sessionFailed(const QString &message, Failure failure, bool autoReconnect)
void reconnectDue()
A failure's backoff has passed, or reconnectNow() cut it short: the receiver starts the next session.
StreamSupervisor(QObject *parent=nullptr, ExponentialBackoff backoff=kDefaultBackoff)
void stop()
The stream isn't wanted (anymore): Idle, without a reconnect.
Failure
Whether another session could fare better than the one that failed.
@ Permanent
It can't: an unsupported stream, a missing plugin.
Phase phase() const
@ Halted
Given up after a failure, until startOver() or autoReconnect resumes it.
@ Reconnecting
After a failure, until the session that replaces it starts.
@ Streaming
The session delivers.
@ Connecting
A session runs, without data yet.
@ Idle
Not streaming and not about to: not asked to, or before the first session.
std::chrono::milliseconds uptime() const
For VideoStreamStats::uptimeMs: since the current session started streaming; zero while it isn't.
AutoReconnectChange autoReconnectChanged(bool autoReconnect)
The config's autoReconnect became autoReconnect.
VideoReceiver::State state() const
void stateChanged(VideoReceiver::State state)
@ Reconnecting
Waiting out the backoff after a failure.
@ Streaming
Data is arriving.
@ Stopped
Inactive, no URI, or given up after a failure (autoReconnect off).
@ Connecting
Pipeline running, no data yet.