QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
VideoStreamController.cc
Go to the documentation of this file.
2
3#include <utility>
4
5#include <QtQuick/QQuickItem>
6
7#include "Fact.h"
9#include "QGCNetworkHelper.h"
10#include "QGCVideoStreamInfo.h"
11#include "RecordingPlan.h"
12#include "VideoSettings.h"
13
14QGC_LOGGING_CATEGORY(VideoStreamControllerLog, "Video.VideoStreamController")
15
16namespace {
17
18QString nameForRole(VideoStreamResolver::Role role)
19{
20 switch (role) {
22 break;
24 return QStringLiteral("thermalVideo");
26 return QStringLiteral("uvcCamera");
28 return QStringLiteral("videoTile");
29 }
30 return QStringLiteral("videoContent");
31}
32
34int shownRotation(const QGCVideoStreamInfo* streamInfo)
35{
36 if (!streamInfo) {
37 return 0;
38 }
39 const int degrees = streamInfo->rotation() % 360;
40 if ((degrees % 90) != 0) {
41 qCDebug(VideoStreamControllerLog) << "Ignoring the stream's rotation of" << degrees << "degrees";
42 return 0;
43 }
44 return degrees;
45}
46
47} // namespace
48
50 VideoReceiver* ownReceiver)
51 : QObject(parent)
52 , _role(role)
53 , _name(nameForRole(role))
54 , _videoSettings(videoSettings)
55{
56 // The setting stands in for the primary stream's ratio, which a stream in a tile may become.
57 (void) connect(_videoSettings->aspectRatio(), &Fact::rawValueChanged, this,
59 if (ownReceiver) {
60 setReceiver(ownReceiver);
61 _hasOwnReceiver = true;
62 }
63}
64
66
68{
69 if (role == _role) {
70 return;
71 }
72 _role = role;
73 emit roleChanged();
74 emit aspectRatioChanged();
76}
77
79{
80 if (tile != _tile) {
81 _tile = tile;
82 emit tileChanged();
83 }
84}
85
87{
88 if (displayName != _displayName) {
89 _displayName = displayName;
90 emit displayNameChanged();
91 }
92}
93
94QString VideoStreamController::taggedFile(const QString& file) const
95{
96 if (_role != Role::Thermal) {
97 return file;
98 }
99 return RecordingPlan::insertBeforeExtension(file, QLatin1Char('.') + name());
100}
101
103{
104 if (!_hasOwnReceiver) {
105 setReceiver(nullptr);
106 return;
107 }
108 // Disabled first: a pending _apply() must not bring the receiver back.
109 _enabled = false;
110 _receiver->shutDown();
111}
112
114{
115 if (_hasOwnReceiver || (receiver == _receiver)) {
116 return;
117 }
118
119 const bool wasDecoding = decoding();
120 const bool wasRecording = recording();
121 const bool wasEncoding = encodesRecordings();
122 const QSize oldVideoSize = videoSize();
123 const VideoStreamStats oldStats = stats();
124
125 // Detach the old receiver first: what it emits while being destroyed must not reach a half-destroyed object.
126 // No receiver is set while it goes: one that finishes a recording runs the event loop in its destructor, from
127 // where this function may be called again.
128 _decoded = false;
129 if (VideoReceiver* const oldReceiver = std::exchange(_receiver, nullptr)) {
130 (void) disconnect(oldReceiver, nullptr, this, nullptr);
131 delete oldReceiver;
132 }
133
134 if (_receiver) {
135 // Set by such a call, which came after this one: its receiver stays.
136 if (receiver != _receiver) {
137 delete receiver;
138 }
139 } else if (receiver) {
140 _receiver = receiver;
141 _receiver->setParent(this);
142 _receiver->setName(name());
143 (void) connect(_receiver, &VideoReceiver::decodingChanged, this, [this] {
144 // Updated first: a stream that loses its decoder is reconnecting by the time decodingChanged arrives, so
145 // QML never sees it neither decoding nor reconnecting, which would clear the frame it keeps.
146 if (decoding()) {
147 _decoded = true;
148 }
149 _updateReconnecting();
150 emit decodingChanged();
151 });
152 (void) connect(_receiver, &VideoReceiver::stateChanged, this, &VideoStreamController::_onReceiverStateChanged);
154 (void) connect(_receiver, &VideoReceiver::videoSizeChanged, this, [this](QSize size) {
155 qCDebug(VideoStreamControllerLog) << name() << "video size" << size;
156 emit videoSizeChanged();
157 emit aspectRatioChanged();
158 });
159 (void) connect(_receiver, &VideoReceiver::statsChanged, this, &VideoStreamController::statsChanged);
162 (void) connect(_receiver, &VideoReceiver::errorOccurred, this, &VideoStreamController::errorOccurred);
163 _receiver->setDisplayConfig(_resolvedDisplayConfig());
164 _receiver->setRecordingEncoding(_recordingEncoding);
165 _receiver->setVideoOutput(_displaySuspended ? nullptr : _videoOutput.data());
166 _apply();
167 }
168
169 if (decoding() != wasDecoding) {
170 emit decodingChanged();
171 }
172 if (recording() != wasRecording) {
174 }
175 if (encodesRecordings() != wasEncoding) {
177 }
178 if (videoSize() != oldVideoSize) {
179 emit videoSizeChanged();
180 emit aspectRatioChanged();
181 }
182 if (stats() != oldStats) {
183 emit statsChanged();
184 }
185 _updateReconnecting();
186}
187
189{
190 return _streamInfo;
191}
192
194{
195 if (streamInfo == _streamInfo) {
196 return;
197 }
198
199 if (_streamInfo) {
200 (void) disconnect(_streamInfo, nullptr, this, nullptr);
201 }
202 _streamInfo = streamInfo;
203 if (_streamInfo) {
204 (void) connect(_streamInfo, &QGCVideoStreamInfo::infoChanged, this,
205 &VideoStreamController::_onStreamInfoChanged);
206 (void) connect(_streamInfo, &QObject::destroyed, this, &VideoStreamController::_onStreamInfoChanged);
207 }
208 _onStreamInfoChanged();
209}
210
211void VideoStreamController::_onStreamInfoChanged()
212{
213 emit streamInfoChanged();
214 if (const int rotation = shownRotation(_streamInfo); rotation != _rotation) {
215 qCDebug(VideoStreamControllerLog) << name() << "rotation" << rotation;
216 _rotation = rotation;
217 emit rotationChanged();
218 emit videoSizeChanged();
219 }
220 emit aspectRatioChanged();
222}
223
224void VideoStreamController::setCamera(const QString& camera)
225{
226 if (camera != _camera) {
227 _camera = camera;
229 }
230}
231
233{
234 if (enabled != _enabled) {
235 _enabled = enabled;
237 }
238}
239
240std::optional<VideoReceiver::Config> VideoStreamController::_resolvedConfig() const
241{
242 if (_role == Role::Camera) {
243 if (_camera.isEmpty()) {
244 return std::nullopt;
245 }
246 VideoReceiver::Config config;
247 config.uri = _camera;
248 return config;
249 }
250 return VideoStreamResolver::resolve(VideoStreamResolver::snapshot(_videoSettings), _streamInfo, _role);
251}
252
253VideoReceiver::DisplayConfig VideoStreamController::_resolvedDisplayConfig() const
254{
256}
257
259{
260 if (_applyPending) {
261 return;
262 }
263 _applyPending = true;
264 (void) QMetaObject::invokeMethod(this, &VideoStreamController::_apply, Qt::QueuedConnection);
265}
266
267void VideoStreamController::_apply()
268{
269 _applyPending = false;
270 const std::optional<VideoReceiver::Config> config = _resolvedConfig();
271 if (const QString uri = config ? config->uri : QString(); uri != _uri) {
272 _uri = uri;
273 _forgetDecoded();
274 emit uriChanged();
275 }
276 if (!_receiver) {
277 return;
278 }
279
280 const bool active = _enabled && config.has_value();
281 if (!active) {
282 _forgetDecoded();
283 }
284 qCDebug(VideoStreamControllerLog) << name() << "active:" << active << "uri:" << uri();
285
286 // Configure before activating and deactivate before configuring, so the receiver never starts a stream that is
287 // about to change.
288 if (active) {
289 _receiver->setConfig(*config);
290 _receiver->setActive(true);
291 } else {
292 _receiver->setActive(false);
293 if (config) {
294 _receiver->setConfig(*config);
295 }
296 }
297}
298
300{
301 _enabled = false;
302 _apply();
303}
304
306{
307 if (_receiver) {
308 _receiver->setDisplayConfig(_resolvedDisplayConfig());
309 _receiver->restart();
310 }
311}
312
314{
315 if (_receiver) {
316 _receiver->reconnectNow();
317 }
318}
319
320void VideoStreamController::_onReceiverStateChanged(VideoReceiver::State state)
321{
322 if (state == VideoReceiver::State::Stopped) {
323 _decoded = false;
324 }
325 _updateReconnecting();
326}
327
328void VideoStreamController::_forgetDecoded()
329{
330 _decoded = false;
331 _updateReconnecting();
332}
333
334void VideoStreamController::_updateReconnecting()
335{
336 const bool reconnecting =
337 _decoded && _receiver && !_receiver->decoding() && (_receiver->state() != VideoReceiver::State::Stopped);
338 if (reconnecting != _reconnecting) {
339 _reconnecting = reconnecting;
340 qCDebug(VideoStreamControllerLog) << name() << "reconnecting" << reconnecting;
341 emit reconnectingChanged();
342 }
343}
344
346{
347 return _videoOutput;
348}
349
351{
352 if (!videoOutput) {
353 return;
354 }
355
356 qCDebug(VideoStreamControllerLog) << name()
357 << ((videoOutput == _videoOutput) ? "re-attaching video output" : "video output")
358 << videoOutput;
359 _videoOutput = videoOutput;
360 // Also for the current output: the receiver binds it anew.
361 if (_receiver && !_displaySuspended) {
362 _receiver->setVideoOutput(videoOutput);
363 }
364}
365
367{
368 if (!videoOutput || (videoOutput != _videoOutput)) {
369 return;
370 }
371
372 qCDebug(VideoStreamControllerLog) << name() << "video output removed" << videoOutput;
373 _videoOutput = nullptr;
374 // Queued: the output that takes this one's place, in the main picture or in a tile, registers in the same pass.
375 (void) QMetaObject::invokeMethod(this, &VideoStreamController::_detachVideoOutput, Qt::QueuedConnection);
376}
377
378void VideoStreamController::_detachVideoOutput()
379{
380 if (!_videoOutput && _receiver) {
381 // Not decoded without an output, and decoded again from the next keyframe once it has one: the stream wasn't
382 // lost, so the output it returns to waits for video rather than report a reconnect.
383 _forgetDecoded();
384 _receiver->setVideoOutput(nullptr);
385 }
386}
387
389{
390 if (suspended == _displaySuspended) {
391 return;
392 }
393 _displaySuspended = suspended;
394 qCDebug(VideoStreamControllerLog) << name() << (suspended ? "display suspended" : "display resumed");
395 if (!_receiver || !_videoOutput) {
396 return;
397 }
398 if (suspended) {
399 // As for an output that was removed: the stream is not lost, and its output waits for video on its return.
400 _forgetDecoded();
401 _receiver->setVideoOutput(nullptr);
402 } else {
403 _receiver->setVideoOutput(_videoOutput);
404 }
405}
406
408{
409 return _receiver && _receiver->active();
410}
411
413{
414 if (_receiver) {
415 _receiver->startRecording(file, format);
416 }
417}
418
420{
421 if (_receiver) {
422 _receiver->stopRecording();
423 }
424}
425
427{
428 return _receiver && _receiver->encodesRecordings();
429}
430
432{
433 _recordingEncoding = encoding;
434 if (_receiver) {
435 _receiver->setRecordingEncoding(encoding);
436 }
437}
438
440{
441 return _receiver && _receiver->streaming() && _receiver->videoOutput();
442}
443
444bool VideoStreamController::takeScreenshot(const QString& file, const VideoScreenshot::Exif& exif) const
445{
446 return _receiver && _receiver->takeScreenshot(file, exif);
447}
448
450{
451 return _receiver && _receiver->decoding();
452}
453
455{
456 return _receiver && _receiver->recording();
457}
458
460{
461 const QSize size = _receiver ? _receiver->videoSize() : QSize();
462 return ((_rotation % 180) != 0) ? size.transposed() : size;
463}
464
466{
467 const QSize size = videoSize();
468 if (!size.isEmpty()) {
469 return static_cast<double>(size.width()) / size.height();
470 }
471 if (_streamInfo) {
472 const double advertised = _streamInfo->aspectRatio();
473 return (((_rotation % 180) != 0) && (advertised > 0.0)) ? (1.0 / advertised) : advertised;
474 }
475 return (_role == Role::Primary) ? _videoSettings->aspectRatio()->rawValue().toDouble() : 1.0;
476}
477
479{
480 return _receiver ? _receiver->stats() : VideoStreamStats{};
481}
482
484{
485 if (_uri.isEmpty() || (_role == Role::Camera)) {
486 return _uri;
487 }
489}
490
492{
493 return (_role == Role::Camera) ? QString() : VideoStreamResolver::transportName(_uri);
494}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void rawValueChanged(const QVariant &value)
Encapsulates the contents of a VIDEO_STREAM_INFORMATION message.
quint16 rotation() const
static QString insertBeforeExtension(const QString &file, const QString &tag)
"<base><tag>.<ext>" for file "<base>.<ext>", and "<file><tag>" for one without an extension.
void statsChanged(const VideoStreamStats &stats)
void videoSizeChanged(QSize size)
void recordingChanged(bool recording)
void setVideoOutput(QQuickItem *videoOutput)
void stateChanged(VideoReceiver::State state)
void recordingFinished(const QString &file, const QString &error)
A recording file closed. error is empty when the file was finalized cleanly.
FileFormat
Values are persisted by VideoSettings::recordingFormat.
void recordingStarted(const QString &file)
A recording file opened: the first one after startRecording(), or the recording's next part.
void decodingChanged(bool decoding)
@ Stopped
Inactive, no URI, or given up after a failure (autoReconnect off).
void errorOccurred(const QString &message)
void setDisplaySuspended(bool suspended)
QGCVideoStreamInfo * streamInfo() const
void recordingStarted(const QString &file)
void setDisplayName(const QString &displayName)
QQuickItem * videoOutput() const
void recordingFinished(const QString &file, const QString &error)
void restart()
Reconnects now and rebuilds the receiver's display sink, so construct-only sink settings apply.
void setCamera(const QString &camera)
~VideoStreamController() override
void startRecording(const QString &file, VideoReceiver::FileFormat format)
VideoReceiver * receiver() const
QString taggedFile(const QString &file) const
file for this source: the thermal stream's name goes before the extension, "<base>....
bool takeScreenshot(const QString &file, const VideoScreenshot::Exif &exif={}) const
void setReceiver(VideoReceiver *receiver)
void setEnabled(bool enabled)
The stream plays while it is enabled and configured().
VideoStreamResolver::Role Role
The source is shown in a tile of the video view, rather than as the main picture or with it.
void setRecordingEncoding(const VideoReceiver::RecordingEncoding &encoding)
VideoReceiver::setRecordingEncoding(), for the receiver and for each one set later.
void reconnectNow()
VideoReceiver::reconnectNow(): the stream's source is likely back, so a reconnect doesn't wait out it...
QSize videoSize() const
The size the stream is shown at: the receiver's video size, turned by rotation().
bool active() const
True when the receiver is active, accepting startRecording().
VideoStreamStats stats() const
The receiver's VideoReceiver::stats(); invalid without a receiver.
Q_INVOKABLE void registerVideoOutput(QQuickItem *videoOutput)
VideoStreamController(Role role, VideoSettings *videoSettings, QObject *parent=nullptr, VideoReceiver *ownReceiver=nullptr)
ownReceiver, when given, is the source's receiver for good; the source takes ownership of it.
Q_INVOKABLE void unregisterVideoOutput(QQuickItem *videoOutput)
void setRole(Role role)
Makes a stream in a tile the primary stream, or the reverse. What the source plays stays.
void recordingChanged(bool recording)
void setStreamInfo(QGCVideoStreamInfo *streamInfo)
The stream the camera advertises for this role, which takes precedence over the manual settings.
void stop()
Disables the stream and stops the receiver now.
void errorOccurred(const QString &message)
bool canTakeScreenshot() const
True while the stream is streaming into a registered output.
QString redactedUrlForLogging(const QUrl &url)
Settings snapshot(VideoSettings *videoSettings)
VideoReceiver::DisplayConfig displayConfig(const Settings &settings)
The display sink settings: GPU zero-copy unless the CPU path or a software decoder is forced.
Role
What a video source, each with a VideoStreamController, is. VideoSourceRole.Primary,...
@ Primary
The stream to display: a vehicle's current camera's current one, else the one the settings configure.
@ Camera
The local camera: the one the video source setting names or, beside a stream, the one for a tile.
@ Tile
Another stream a vehicle's cameras advertise, shown in a tile.
@ Thermal
The thermal stream a vehicle's current camera advertises.
QString transportName(const QString &uri)
std::optional< VideoReceiver::Config > resolve(const Settings &settings, const QGCVideoStreamInfo *streamInfo, Role role)
How the display sink presents frames. Construct-only: a change applies when restart() rebuilds the si...