QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
VideoReceiver.cc
Go to the documentation of this file.
1#include "VideoReceiver.h"
2
3#include <QtConcurrent/QtConcurrentRun>
4#include <QtCore/QCoreApplication>
5#include <QtCore/QFileInfo>
6#include <QtGui/QImage>
7#include <QtMultimedia/QVideoFrame>
8#include <QtMultimedia/QVideoSink>
9#include <QtQuick/QQuickItem>
10
11#include "QGCLoggingCategory.h"
12#include "VideoScreenshot.h"
13
14QGC_LOGGING_CATEGORY(VideoReceiverLog, "Video.VideoReceiver")
15
17 : QObject(parent)
18{}
19
21
23{
24 return QStringLiteral("%1 %2").arg(QCoreApplication::applicationName(), QCoreApplication::applicationVersion());
25}
26
28{
29 switch (format) {
30 case FileFormat::Mkv:
31 return QStringLiteral("mkv");
32 case FileFormat::Mov:
33 return QStringLiteral("mov");
34 case FileFormat::Mp4:
35 return QStringLiteral("mp4");
36 }
37 return QStringLiteral("mkv");
38}
39
41{
42 return (format >= static_cast<int>(FileFormat::Mkv)) && (format <= static_cast<int>(FileFormat::Mp4));
43}
44
45QSize VideoReceiver::presentedSize(const QVideoFrame& frame, QSize pictureSize)
46{
47 if (!frame.isValid()) {
48 return {};
49 }
50 const QVideoFrameFormat format = frame.surfaceFormat();
51 const QRect viewport = format.viewport();
52 const QSize size = pictureSize.isValid() ? pictureSize : (viewport.isValid() ? viewport.size() : frame.size());
53 // As Qt's qRotatedFramePresentationSize(): the frame's and the format's rotation add up; mirroring keeps the size.
54 const int degrees = static_cast<int>(frame.rotation()) + static_cast<int>(format.rotation());
55 return ((degrees % 180) != 0) ? size.transposed() : size;
56}
57
58void VideoReceiver::setName(const QString& name)
59{
60 _name = name;
61}
62
64{
65 if (config == _config) {
66 return;
67 }
68 _config = config;
70}
71
73{
74 if (active == _active) {
75 return;
76 }
77 _active = active;
79}
80
82{
83 if (_active) {
85 }
86}
87
89{
90 if (_active) {
92 }
93}
94
95QQuickItem* VideoReceiver::videoOutput() const
96{
97 return _videoOutput;
98}
99
101{
102 if (!videoOutput && !_videoOutput) {
103 return;
104 }
105 if (videoOutput != _videoOutput) {
106 (void) disconnect(_videoOutputDestroyed);
107 _videoOutput = videoOutput;
108 if (videoOutput) {
109 _videoOutputDestroyed =
110 connect(videoOutput, &QObject::destroyed, this, &VideoReceiver::_onVideoOutputDestroyed);
111 }
112 }
114}
115
116void VideoReceiver::_onVideoOutputDestroyed()
117{
118 _videoOutput = nullptr;
120}
121
122void VideoReceiver::startRecording(const QString& file, FileFormat format)
123{
124 if (!_active) {
125 qCWarning(VideoReceiverLog) << "Not recording" << _name << "- the stream is not active";
126 emit errorOccurred(tr("Unable to record video: the video stream is not active."));
127 return;
128 }
129 if (file.isEmpty()) {
130 qCWarning(VideoReceiverLog) << "Not recording" << _name << "- no file name";
131 emit errorOccurred(tr("Unable to record video: no file name."));
132 return;
133 }
134 onStartRecording(file, format);
135}
136
141
143{
144 setActive(false);
145}
146
147bool VideoReceiver::takeScreenshot(const QString& file, const VideoScreenshot::Exif& exif)
148{
149 if (!videoSink()) {
150 qCWarning(VideoReceiverLog) << "No video output to take a screenshot from" << _name;
151 return false;
152 }
153 return onTakeScreenshot(file, exif);
154}
155
156bool VideoReceiver::onTakeScreenshot(const QString& file, const VideoScreenshot::Exif& exif)
157{
158 // VideoOutput takes only multiples of 90 degrees, which are QtVideo::Rotation's values once within a turn.
159 const int orientation = _videoOutput->property("orientation").toInt() % 360;
160 const auto outputRotation = static_cast<QtVideo::Rotation>((orientation < 0) ? (orientation + 360) : orientation);
161 const bool outputMirrored = _videoOutput->property("mirrored").toBool();
162 // Converted on this thread, which a frame in GPU memory requires; encoding and writing would stall the UI.
163 const QImage image = VideoScreenshot::displayedImage(videoSink()->videoFrame(), outputRotation, outputMirrored);
164 if (image.isNull()) {
165 qCWarning(VideoReceiverLog) << "No video frame to take a screenshot from" << _name;
166 return false;
167 }
168 (void) QtConcurrent::run([image, file, exif] {
169 return VideoScreenshot::saveImage(image, file, exif);
170 }).then(this, [this, file](bool saved) {
171 if (!saved) {
172 emit errorOccurred(tr("Unable to save the photo %1.").arg(QFileInfo(file).fileName()));
173 }
174 });
175 return true;
176}
177
178QVideoSink* VideoReceiver::videoSink() const
179{
180 return _videoOutput ? _videoOutput->property("videoSink").value<QVideoSink*>() : nullptr;
181}
182
184{
185 if (state == _state) {
186 return;
187 }
188 const bool wasStreaming = streaming();
189 _state = state;
190 emit stateChanged(_state);
191 if (wasStreaming != streaming()) {
193 }
194}
195
197{
198 if (decoding != _decoding) {
199 _decoding = decoding;
200 emit decodingChanged(_decoding);
201 }
202}
203
205{
206 if (recording != _recording) {
207 _recording = recording;
208 emit recordingChanged(_recording);
209 }
210}
211
213{
214 if (size != _videoSize) {
215 _videoSize = size;
216 emit videoSizeChanged(_videoSize);
217 }
218}
219
221{
222 if (stats != _stats) {
223 _stats = stats;
224 emit statsChanged(_stats);
225 }
226}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
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 recordingChanged(bool recording)
virtual void onVideoOutputChanged()=0
Also called when the current output is set again.
void setConfig(const Config &config)
State state() 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
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
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 setDecoding(bool decoding)
void decodingChanged(bool decoding)
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
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.
bool saveImage(const QImage &image, const QString &file, const Exif &exif={})
QImage displayedImage(const QVideoFrame &frame, QtVideo::Rotation outputRotation=QtVideo::Rotation::None, bool outputMirrored=false)