QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
VideoReceiver.h
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <cstdint>
5
6#include <QtCore/QByteArray>
7#include <QtCore/QObject>
8#include <QtCore/QPointer>
9#include <QtCore/QSize>
10#include <QtCore/QString>
11#include <QtMultimedia/QMediaFormat>
12#include <QtMultimedia/QMediaRecorder>
13#include <QtQmlIntegration/QtQmlIntegration>
14
15#include "VideoScreenshot.h"
16#include "VideoStreamStats.h"
17
18class QQuickItem;
19class QVideoFrame;
20class QVideoSink;
21
31class VideoReceiver : public QObject
32{
33 Q_OBJECT
34 QML_ELEMENT
35 QML_UNCREATABLE("")
36 Q_PROPERTY(QString name READ name CONSTANT FINAL)
37 Q_PROPERTY(State state READ state NOTIFY stateChanged FINAL)
38 Q_PROPERTY(bool streaming READ streaming NOTIFY streamingChanged FINAL)
39 Q_PROPERTY(bool decoding READ decoding NOTIFY decodingChanged FINAL)
40 Q_PROPERTY(bool recording READ recording NOTIFY recordingChanged FINAL)
41 Q_PROPERTY(QSize videoSize READ videoSize NOTIFY videoSizeChanged FINAL)
42 Q_PROPERTY(VideoStreamStats stats READ stats NOTIFY statsChanged FINAL)
43
44public:
45 enum class State : uint8_t
46 {
51 };
52 Q_ENUM(State)
53
54
55 enum class FileFormat : uint8_t
56 {
57 Mkv = 0,
58 Mov = 1,
59 Mp4 = 2,
60 };
61 Q_ENUM(FileFormat)
62
63 struct Config
64 {
65 QString uri;
68 std::chrono::seconds timeout{3};
69 bool lowLatency = false;
72 bool autoReconnect = true;
73
74 friend bool operator==(const Config&, const Config&) = default;
75 };
76
79 {
80 QByteArray conversionElement;
82 bool gpuZeroCopy = true;
83
84 friend bool operator==(const DisplayConfig&, const DisplayConfig&) = default;
85 };
86
90 {
92 QMediaFormat::VideoCodec codec = QMediaFormat::VideoCodec::Unspecified;
93 QMediaRecorder::Quality quality = QMediaRecorder::HighQuality;
94
95 friend bool operator==(const RecordingEncoding&, const RecordingEncoding&) = default;
96 };
97
98 explicit VideoReceiver(QObject* parent = nullptr);
99 ~VideoReceiver() override;
100
102 static QString fileExtension(FileFormat format);
104 static QString recordingComment();
105 static bool isValidFileFormat(int format);
109 static QSize presentedSize(const QVideoFrame& frame, QSize pictureSize = {});
110
111 QString name() const { return _name; }
112
114 void setName(const QString& name);
115
116 const Config& config() const { return _config; }
117
118 const DisplayConfig& displayConfig() const { return _displayConfig; }
119
120 bool active() const { return _active; }
121
122 QQuickItem* videoOutput() const;
123
124 State state() const { return _state; }
125
126 bool streaming() const { return _state == State::Streaming; }
127
128 bool decoding() const { return _decoding; }
129
130 bool recording() const { return _recording; }
131
133 QSize videoSize() const { return _videoSize; }
134
136 VideoStreamStats stats() const { return _stats; }
137
140 void setConfig(const Config& config);
141
143 void setDisplayConfig(const DisplayConfig& config) { _displayConfig = config; }
144
147 virtual bool encodesRecordings() const { return false; }
148
149 const RecordingEncoding& recordingEncoding() const { return _recordingEncoding; }
150
152 void setRecordingEncoding(const RecordingEncoding& encoding) { _recordingEncoding = encoding; }
153
155 void setActive(bool active);
158 virtual void shutDown();
161 void restart();
164 void reconnectNow();
168 void setVideoOutput(QQuickItem* videoOutput);
169
175 void startRecording(const QString& file, FileFormat format);
176 void stopRecording();
177
183 bool takeScreenshot(const QString& file, const VideoScreenshot::Exif& exif = {});
184
185signals:
191 void recordingStarted(const QString& file);
193 void recordingFinished(const QString& file, const QString& error);
194 void videoSizeChanged(QSize size);
198 void errorOccurred(const QString& message);
199
200protected:
201 virtual void onConfigChanged() = 0;
202 virtual void onActiveChanged() = 0;
203 virtual void onRestartRequested() = 0;
204 virtual void onReconnectRequested() = 0;
206 virtual void onVideoOutputChanged() = 0;
207 virtual void onStartRecording(const QString& file, FileFormat format) = 0;
208 virtual void onStopRecording() = 0;
210 virtual bool onTakeScreenshot(const QString& file, const VideoScreenshot::Exif& exif);
211
213 QVideoSink* videoSink() const;
214
215 // Each emits its NOTIFY signal (and streamingChanged for setState()) when the value changes.
216 void setState(State state);
217 void setDecoding(bool decoding);
218 void setRecording(bool recording);
219 void setVideoSize(QSize size);
221 void setStats(const VideoStreamStats& stats);
222
223private:
224 void _onVideoOutputDestroyed();
225
226 QString _name;
227 Config _config;
228 DisplayConfig _displayConfig;
229 RecordingEncoding _recordingEncoding;
230 bool _active = false;
231 QPointer<QQuickItem> _videoOutput;
232 QMetaObject::Connection _videoOutputDestroyed;
233 State _state = State::Stopped;
234 bool _decoding = false;
235 bool _recording = false;
236 QSize _videoSize;
237 VideoStreamStats _stats;
238};
void statsChanged(const VideoStreamStats &stats)
void setVideoSize(QSize size)
void setName(const QString &name)
Names the stream in logs and pipeline dumps. Call once, before the first setActive(true).
QQuickItem * videoOutput() const
bool streaming() const
void videoSizeChanged(QSize size)
virtual void onStopRecording()=0
void setRecordingEncoding(const RecordingEncoding &encoding)
Stores encoding for the recordings that start from now on; one in progress keeps its own.
void recordingChanged(bool recording)
virtual void onVideoOutputChanged()=0
Also called when the current output is set again.
void setConfig(const Config &config)
State state() const
virtual bool encodesRecordings() const
void startRecording(const QString &file, FileFormat format)
void setVideoOutput(QQuickItem *videoOutput)
virtual void shutDown()
VideoReceiver(QObject *parent=nullptr)
virtual void onStartRecording(const QString &file, FileFormat format)=0
const DisplayConfig & displayConfig() const
void stateChanged(VideoReceiver::State state)
void streamingChanged(bool streaming)
void setStats(const VideoStreamStats &stats)
Backends publish a new measurement, and an invalid one once the stream stops or reconnects.
QVideoSink * videoSink() const
The VideoOutput's QVideoSink, or nullptr.
virtual void onActiveChanged()=0
bool recording() const
void recordingFinished(const QString &file, const QString &error)
A recording file closed. error is empty when the file was finalized cleanly.
void setDisplayConfig(const DisplayConfig &config)
Stores config for the display sink restart() builds next.
static QSize presentedSize(const QVideoFrame &frame, QSize pictureSize={})
QString name() const
FileFormat
Values are persisted by VideoSettings::recordingFormat.
void setActive(bool active)
Streams while true and the URI is set. False stops the stream, any pending reconnect and any recordin...
static QString recordingComment()
The comment recordings carry, of GStreamer and of a USB camera alike: the application and its version...
static bool isValidFileFormat(int format)
virtual void onConfigChanged()=0
void recordingStarted(const QString &file)
A recording file opened: the first one after startRecording(), or the recording's next part.
void setDecoding(bool decoding)
void decodingChanged(bool decoding)
const RecordingEncoding & recordingEncoding() const
void setRecording(bool recording)
bool takeScreenshot(const QString &file, const VideoScreenshot::Exif &exif={})
void setState(State state)
static QString fileExtension(FileFormat format)
"mkv", "mov" or "mp4".
VideoStreamStats stats() const
The stream's health, measured about once a second while it streams; invalid while it doesn't.
virtual void onReconnectRequested()=0
const Config & config() const
~VideoReceiver() override
@ 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.
QSize videoSize() const
The size of the frames as they are presented, before the orientation the VideoOutput adds.
bool decoding() const
bool active() const
void errorOccurred(const QString &message)
virtual void onRestartRequested()=0
virtual bool onTakeScreenshot(const QString &file, const VideoScreenshot::Exif &exif)
takeScreenshot() with an output set: saves the frame on display, unless overridden.
std::chrono::seconds timeout
bool autoReconnect
Reconnect with backoff after a failure. Applies at once; changing any other field restarts the stream...
friend bool operator==(const Config &, const Config &)=default
How the display sink presents frames. Construct-only: a change applies when restart() rebuilds the si...
bool gpuZeroCopy
Off when the CPU video path or a software decoder is forced.
friend bool operator==(const DisplayConfig &, const DisplayConfig &)=default
QByteArray conversionElement
Converter element factory; empty for the backend's default.
QMediaFormat::VideoCodec codec
Tried first; Unspecified for the first codec that encodes.
QMediaRecorder::Quality quality
friend bool operator==(const RecordingEncoding &, const RecordingEncoding &)=default