QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
StreamSupervisor.h
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <cstdint>
5
6#include <QtCore/QElapsedTimer>
7#include <QtCore/QObject>
8#include <QtCore/QString>
9#include <QtCore/QTimer>
10
11#include "ExponentialBackoff.h"
12#include "VideoReceiver.h"
13
23class StreamSupervisor : public QObject
24{
25 Q_OBJECT
26
27public:
28 enum class Phase : uint8_t
29 {
35 };
36
38 enum class Failure : uint8_t
39 {
42 };
43
45 {
46 bool halted = false;
48 std::chrono::milliseconds reconnectDelay{0};
49 };
50
51 enum class AutoReconnectChange : uint8_t
52 {
56 };
57
60 static constexpr ExponentialBackoff kDefaultBackoff{std::chrono::seconds(1), 2, std::chrono::seconds(30)};
61
62 explicit StreamSupervisor(QObject* parent = nullptr, ExponentialBackoff backoff = kDefaultBackoff);
63
64 Phase phase() const { return _phase; }
65
67
68 bool halted() const { return _phase == Phase::Halted; }
69
71 bool reconnectPending() const { return _reconnectTimer.isActive(); }
72
74 const QString& lastFailure() const { return _lastFailure; }
75
77 int reconnectAttempts() const { return _backoff.attempts(); }
78
80 int reconnects() const;
82 std::chrono::milliseconds uptime() const;
83
86 void startOver();
88 void stop();
90 void sessionStarted();
92 void sessionStreaming();
95 FailureOutcome sessionFailed(const QString& message, Failure failure, bool autoReconnect);
97 AutoReconnectChange autoReconnectChanged(bool autoReconnect);
100 void reconnectNow();
101
102signals:
106
107private:
108 void _setPhase(Phase phase);
109 void _endSession();
110
111 ExponentialBackoff _backoff;
112 QTimer _reconnectTimer;
113 Phase _phase = Phase::Idle;
114 bool _haltedPermanently = false;
116 bool _sessionStreamed = false;
117 int _streamedSessions = 0;
118 QElapsedTimer _sinceStreaming;
119 QString _lastFailure;
120};
Retry delays that start at an initial value and grow by a factor per attempt, up to a cap.
static constexpr ExponentialBackoff kDefaultBackoff
@ Resumable
Turned on while halted after a failure that wasn't permanent: the halt is lifted.
bool reconnectPending() const
A failure's backoff is running; reconnectDue() ends it.
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.
@ Transient
It may, so reconnecting is worth it.
Phase phase() const
bool halted() const
const QString & lastFailure() const
The failure the stream halted or reconnects after.
int reconnectAttempts() const
Failures backed off from since the backoff last started over.
@ 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)
std::chrono::milliseconds reconnectDelay
Until reconnectDue(), when not halted.