QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
VideoManager.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <chrono>
5
6#include <QtCore/QFuture>
7#include <QtCore/QMutex>
8#include <QtCore/QPromise>
9#include <QtCore/QObject>
10#include <QtCore/QSize>
11#include <QtQmlIntegration/QtQmlIntegration>
12
13#ifdef QGC_UNITTEST_BUILD
14#include <functional>
15#endif
16
17class QQuickWindow;
18class SubtitleWriter;
19class Vehicle;
20class VideoReceiver;
21class VideoSettings;
22
23class VideoManager : public QObject
24{
25 Q_OBJECT
26 QML_ELEMENT
27 QML_UNCREATABLE("")
28 Q_MOC_INCLUDE("Vehicle.h")
29
31 Q_PROPERTY(bool decoding READ decoding NOTIFY decodingChanged)
32 Q_PROPERTY(bool fullScreen READ fullScreen WRITE setfullScreen NOTIFY fullScreenChanged)
33 Q_PROPERTY(bool hasThermal READ hasThermal NOTIFY decodingChanged)
34 Q_PROPERTY(bool hasVideo READ hasVideo NOTIFY hasVideoChanged)
35 Q_PROPERTY(bool isStreamSource READ isStreamSource NOTIFY isStreamSourceChanged)
36 Q_PROPERTY(bool isUvc READ isUvc NOTIFY isUvcChanged)
37 Q_PROPERTY(bool recording READ recording NOTIFY recordingChanged)
38 Q_PROPERTY(bool streaming READ streaming NOTIFY streamingChanged)
39 Q_PROPERTY(double aspectRatio READ aspectRatio NOTIFY aspectRatioChanged)
40 Q_PROPERTY(double hfov READ hfov NOTIFY aspectRatioChanged)
41 Q_PROPERTY(double thermalAspectRatio READ thermalAspectRatio NOTIFY aspectRatioChanged)
42 Q_PROPERTY(double thermalHfov READ thermalHfov NOTIFY aspectRatioChanged)
43 Q_PROPERTY(QSize videoSize READ videoSize NOTIFY videoSizeChanged)
44 Q_PROPERTY(QString imageFile READ imageFile NOTIFY imageFileChanged)
45 Q_PROPERTY(QString uvcVideoSourceID READ uvcVideoSourceID NOTIFY uvcVideoSourceIDChanged)
46
48
49public:
50 explicit VideoManager(QObject *parent = nullptr);
51 ~VideoManager();
52
53 static VideoManager *instance();
54
55 Q_INVOKABLE void grabImage(const QString &imageFile = QString());
56 Q_INVOKABLE void startRecording(const QString &videoFile = QString());
57 Q_INVOKABLE void startVideo();
58 Q_INVOKABLE void stopRecording();
59 Q_INVOKABLE void stopVideo();
60
61 void init(QQuickWindow *mainWindow);
63 bool waitForVideoBackendReady(std::chrono::milliseconds timeout = std::chrono::minutes(1));
64 void cleanup();
65 bool autoStreamConfigured() const;
66 bool decoding() const { return _decoding; }
67 bool fullScreen() const { return _fullScreen; }
68 bool hasThermal() const;
69 bool hasVideo() const;
70 bool isStreamSource() const;
71 bool isUvc() const;
72 bool recording() const { return _recording; }
73 bool streaming() const { return _streaming; }
74 double aspectRatio() const;
75 double hfov() const;
76 double thermalAspectRatio() const;
77 double thermalHfov() const;
78 QSize videoSize() const { return _videoSize; }
79 QString imageFile() const { return _imageFile; }
80 QString uvcVideoSourceID() const { return _uvcVideoSourceID; }
81 void setfullScreen(bool on);
82
83signals:
89 void imageFileChanged(const QString &filename);
94 void recordingStarted(const QString &filename);
98
99private slots:
100 void _communicationLostChanged(bool communicationLost);
101 void _setActiveVehicle(Vehicle *vehicle);
102 void _videoSourceChanged();
103
104private:
105 enum class InitState : uint8_t {
106 NotStarted,
107 Pending,
108 BackendReady,
109 QmlReady,
110 Running,
111 Failed
112 };
113
114 void _initAfterQmlIsReady();
115 void _onBackendInitComplete(bool success);
116 void _createVideoReceivers();
117 void _initVideoReceiver(VideoReceiver *receiver, QQuickWindow *window);
118 bool _updateAutoStream(VideoReceiver *receiver);
119 bool _updateUVC(VideoReceiver *receiver);
120 bool _updateSettings(VideoReceiver *receiver);
121 bool _updateVideoUri(VideoReceiver *receiver, const QString &uri);
122 void _restartAllVideos();
123 void _restartVideo(VideoReceiver *receiver);
124 void _startReceiver(VideoReceiver *receiver);
125 void _stopReceiver(VideoReceiver *receiver);
126 static void _cleanupOldVideos();
127
128 QList<VideoReceiver*> _videoReceivers;
129 SubtitleWriter *_subtitleWriter = nullptr;
130 VideoSettings *_videoSettings = nullptr;
131 QQuickWindow *_mainWindow = nullptr;
132 Vehicle *_activeVehicle = nullptr;
133
134 std::atomic<InitState> _initState = InitState::NotStarted;
135 // Orders _backendInitFuture publication against cross-thread waiters.
136 QMutex _initFutureMutex;
137 QFuture<bool> _backendInitFuture;
138 bool _initialized = false;
139 bool _backendDisabledForTests = false;
140 bool _fullScreen = false;
141
142 QAtomicInteger<bool> _decoding = false;
143 QAtomicInteger<bool> _recording = false;
144 QAtomicInteger<bool> _streaming = false;
145 QSize _videoSize;
146 QString _imageFile;
147 QString _uvcVideoSourceID;
148
149#ifdef QGC_UNITTEST_BUILD
150 std::function<void()> _createVideoReceiversForTest;
151#endif
152};
void recordingStarted(const QString &filename)
bool isStreamSource() const
bool fullScreen() const
void decodingChanged()
bool isUvc() const
void setfullScreen(bool on)
double hfov() const
void startVideoBackendInit()
Q_INVOKABLE void stopRecording()
bool waitForVideoBackendReady(std::chrono::milliseconds timeout=std::chrono::minutes(1))
void hasVideoChanged()
void uvcVideoSourceIDChanged()
Q_INVOKABLE void startVideo()
bool hasThermal() const
bool autoStreamConfigured() const
Q_INVOKABLE void startRecording(const QString &videoFile=QString())
double thermalAspectRatio() const
void isStreamSourceChanged()
static VideoManager * instance()
QString uvcVideoSourceID() const
bool decoding() const
void autoStreamConfiguredChanged()
void streamingChanged()
void fullScreenChanged()
bool hasVideo() const
void init(QQuickWindow *mainWindow)
bool recording() const
Q_INVOKABLE void stopVideo()
double aspectRatio() const
friend class VideoManagerInitTest
void isUvcChanged()
double thermalHfov() const
void imageFileChanged(const QString &filename)
QSize videoSize() const
void recordingChanged(bool recording)
bool streaming() const
void isAutoStreamChanged()
void videoSizeChanged()
QString imageFile() const
void aspectRatioChanged()
Q_INVOKABLE void grabImage(const QString &imageFile=QString())