QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QGCQVideoSinkController.cc
Go to the documentation of this file.
2
3#include <QtCore/QThread>
4#include <QtGui/QWindow>
5#include <QtMultimedia/QVideoFrame>
6#include <QtMultimedia/QVideoSink>
7#include <QtQuick/QQuickItem>
8#include <QtQuick/QQuickWindow>
9
10#include "QGCLoggingCategory.h"
11
12QGC_LOGGING_CATEGORY(QGCQVideoSinkControllerLog, "Video.GStreamer.QGCQVideoSinkController")
13
15 : QObject(parent)
16 , _element(element ? GStreamer::adoptElement(GST_ELEMENT(gst_object_ref(element))) : nullptr)
17{}
18
20{
21 _releaseElementBinding();
22}
23
25{
26 (void) QObject::disconnect(_visibilityConnection);
27 _videoOutput = videoOutput;
28 // Qt defect, seen in Qt 6.12.0: QQuickVideoOutput::itemChange() never calls QQuickItem::itemChange()
29 // (qtmultimedia/src/multimediaquick/qquickvideooutput.cpp), so the output emits no windowChanged. The window is
30 // taken now, and VideoStreamOutput.qml registers an output again after it moved to another window. Once the minimum
31 // Qt has the fix, connect to QQuickItem::windowChanged here and drop that re-registration.
32 if (QQuickWindow* window = videoOutput ? videoOutput->window() : nullptr) {
33 _visibilityConnection = QObject::connect(window, &QWindow::visibilityChanged, this,
34 &QGCQVideoSinkController::_applyWindowVisibility);
35 }
36 _applyWindowVisibility();
37}
38
39void QGCQVideoSinkController::_applyWindowVisibility()
40{
41 const QWindow* const window = _videoOutput ? _videoOutput->window() : nullptr;
42 const QWindow::Visibility visibility = window ? window->visibility() : QWindow::Hidden;
43 setActive((visibility != QWindow::Hidden) && (visibility != QWindow::Minimized));
44}
45
47{
48 if (thread() != QThread::currentThread()) {
49 qCCritical(QGCQVideoSinkControllerLog) << "called from wrong thread";
50 return;
51 }
52 _active = active;
53 if (!_element || _bindingReleased)
54 return;
55 g_object_set(_element.get(), "active", active ? TRUE : FALSE, nullptr);
56}
57
58void QGCQVideoSinkController::setVideoSink(QPointer<QVideoSink> sink)
59{
60 if (thread() != QThread::currentThread()) {
61 qCCritical(QGCQVideoSinkControllerLog) << "called from wrong thread";
62 return;
63 }
64 if (!_element)
65 return;
66 if (_sinkDestroyedConnection) {
67 QObject::disconnect(_sinkDestroyedConnection);
68 _sinkDestroyedConnection = {};
69 }
70 QVideoSink* raw = sink.data();
71 if (!raw) {
72 // Caller's QVideoSink was destroyed between the call site and here — clear the
73 // element's snapshot and gate show_frame until a replacement sink is installed.
74 _releaseElementBinding();
75 return;
76 }
77 // Unbinding turned the element off: bound again, it goes on as setActive() last asked.
78 g_object_set(_element.get(), "qvideosink", static_cast<gpointer>(raw), "active", _active ? TRUE : FALSE, nullptr);
79 if (_videoSink != raw) {
80 _clearVideoSinkFrame();
81 }
82 _videoSink = raw;
83 _sinkDestroyedConnection =
84 QObject::connect(raw, &QObject::destroyed, this, [this]() { setVideoSink(QPointer<QVideoSink>()); });
85 _bindingReleased = false;
86}
87
89{
90 if (thread() != QThread::currentThread()) {
91 qCCritical(QGCQVideoSinkControllerLog) << "called from wrong thread";
92 return;
93 }
94 _releaseElementBinding();
95}
96
98{
99 if (thread() != QThread::currentThread()) {
100 qCCritical(QGCQVideoSinkControllerLog) << "called from wrong thread";
101 return;
102 }
103 if (_videoSink) {
104 _videoSink->setVideoFrame(QVideoFrame());
105 }
106}
107
109{
110 if (!_element) {
111 return {};
112 }
113 gint width = 0;
114 gint height = 0;
115 g_object_get(_element.get(), "picture-width", &width, "picture-height", &height, nullptr);
116 return ((width > 0) && (height > 0)) ? QSize(width, height) : QSize();
117}
118
119void QGCQVideoSinkController::_releaseElementBinding()
120{
121 if (!_element || _bindingReleased)
122 return;
123
124 if (_sinkDestroyedConnection) {
125 QObject::disconnect(_sinkDestroyedConnection);
126 _sinkDestroyedConnection = {};
127 }
128 g_object_set(_element.get(), "active", FALSE, "qvideosink", static_cast<gpointer>(nullptr), nullptr);
129 _bindingReleased = true;
130 _clearVideoSinkFrame();
131}
132
133void QGCQVideoSinkController::_clearVideoSinkFrame()
134{
135 releaseFrame();
136 _videoSink.clear();
137}
struct _GstElement GstElement
#define QGC_LOGGING_CATEGORY(name, categoryStr)
QGCQVideoSinkController(GstElement *element, QObject *parent=nullptr)
void setVideoSink(QPointer< QVideoSink > sink)
void followWindowVisibility(QQuickItem *videoOutput)