6#include <QtCore/QCoreApplication>
7#include <QtCore/QDeadlineTimer>
8#include <QtCore/QThread>
9#include <QtCore/QTimer>
10#include <QtMultimedia/QVideoFrame>
11#include <QtMultimedia/QVideoSink>
30constexpr int kStuckWorkerChecks = 6;
36 , _workerThread(new QThread(this))
38 , _heartbeatTimer(new QTimer(this))
40 qCDebug(GstVideoReceiverLog) <<
this;
42 _heartbeatTimer->setSingleShot(
true);
44 (void) connect(_heartbeatTimer, &QTimer::timeout,
this, &GstVideoReceiver::_checkWorker);
55 _workerThread->setObjectName(QStringLiteral(
"GstVideoReceiver"));
56 _worker->moveToThread(_workerThread);
57 (void) connect(_workerThread, &QThread::finished, _worker, &QObject::deleteLater);
58 _workerThread->start();
64 if (_workerThread->wait(QDeadlineTimer(kWorkerShutdownTimeout))) {
65 qCDebug(GstVideoReceiverLog) <<
this;
72 qCWarning(GstVideoReceiverLog) <<
"Worker still busy after"
73 << std::chrono::milliseconds(kWorkerShutdownTimeout).count()
74 <<
"ms; leaving it to finish on its own";
75 _workerThread->setParent(
nullptr);
76 (void) connect(_workerThread, &QThread::finished, _workerThread, &QObject::deleteLater);
77 if (_workerThread->isFinished()) {
84 _heartbeatTimer->setInterval(interval);
100 _releaseDisplaySink();
101 if (QVideoSink*
const sink =
videoSink()) {
102 _bindVideoSink(sink);
110 ++_request.reconnects;
116 (void) disconnect(_videoSizeConnection);
120 _bindVideoSink(sink);
123 qCWarning(GstVideoReceiverLog) <<
"Video output" <<
videoOutput() <<
"has no videoSink; not displaying";
125 if (_sinkController) {
126 _sinkController->setVideoSink(QPointer<QVideoSink>());
127 _sinkController->followWindowVisibility(
nullptr);
135 const quint64
id = (_request.recording ? _request.recording->id : 0) + 1;
142 _request.recording.reset();
146void GstVideoReceiver::_postRequest()
152 if (displaySink != _request.
videoSink.get()) {
155 _postToWorker([request = _request](GstReceiverWorker& worker) { worker.
apply(request); });
158void GstVideoReceiver::_checkWorker()
160 const bool frameShown = std::exchange(_frameShown,
false);
161 const bool ranTask = _tasksDone != std::exchange(_tasksDoneAtCheck, _tasksDone);
162 if (_tasksDone == _tasksPosted) {
170 _postToWorker([](GstReceiverWorker& ) {});
171 }
else if (ranTask) {
174 }
else if (_workerStuck) {
175 _applyStuckStatus(frameShown);
176 }
else if (++_stalledChecks >= kStuckWorkerChecks) {
177 qCWarning(GstVideoReceiverLog) <<
"The worker of" <<
name() <<
"has run none of its tasks for"
178 << (kStuckWorkerChecks * _heartbeatTimer->intervalAsDuration()).count()
179 <<
"ms: a streaming thread it waits for doesn't return";
181 _applyStuckStatus(frameShown);
183 tr(
"The video decoder or recorder stopped responding. Restart %1 to get video working again.")
184 .arg(QCoreApplication::applicationName()));
187 _heartbeatTimer->start();
190void GstVideoReceiver::_onWorkerTaskDone()
194 qCDebug(GstVideoReceiverLog) <<
"The worker of" <<
name() <<
"runs its tasks again";
195 _workerStuck =
false;
196 _applyStatus(_workerStatus);
200void GstVideoReceiver::_applyStuckStatus(
bool frameShown)
205 GstReceiverStatus status = _workerStatus;
212 _applyStatus(status);
217 _workerStatus = status;
219 _applyStatus(status);
226 const bool wasDecoding =
decoding();
228 if (wasDecoding && !status.
decoding && _sinkController) {
229 _sinkController->releaseFrame();
236void GstVideoReceiver::_postToWorker(std::function<
void(
GstReceiverWorker&)> task)
238 GstReceiverWorker*
const worker = _worker;
239 (void) QMetaObject::invokeMethod(
241 [worker, task = std::move(task)]() {
245 Qt::QueuedConnection);
248 if (!_heartbeatTimer->isActive()) {
249 _heartbeatTimer->start();
253void GstVideoReceiver::_releaseDisplaySink()
255 (void) disconnect(_videoSizeConnection);
256 if (_sinkController) {
257 _sinkController->prepareForRelease();
258 _sinkController->deleteLater();
259 _sinkController =
nullptr;
261 _displaySink.reset();
264bool GstVideoReceiver::_buildDisplaySink(QVideoSink* videoSink)
269 QGCQVideoSinkController*
const controller =
272 qCWarning(GstVideoReceiverLog) <<
"Unable to create the video sink for" <<
name();
273 emit
errorOccurred(tr(
"Unable to display video: the video sink could not be created."));
276 _displaySink = std::move(sink);
277 _sinkController = controller;
281void GstVideoReceiver::_bindVideoSink(QVideoSink* videoSink)
283 (void) disconnect(_videoSizeConnection);
285 _sinkController->setVideoSink(QPointer<QVideoSink>(
videoSink));
286 }
else if (!_buildDisplaySink(
videoSink)) {
294 const auto sizeOf = [
this](
const QVideoFrame&
frame) {
295 return presentedSize(frame, _sinkController ? _sinkController->pictureSize() : QSize());
297 _videoSizeConnection =
298 connect(
videoSink, &QVideoSink::videoFrameChanged,
this, [
this, sizeOf](
const QVideoFrame& frame) {
299 _frameShown = _frameShown ||
frame.isValid();
302 if (
const QSize size = sizeOf(
videoSink->videoFrame()); size.isValid()) {
struct _GstElement GstElement
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void statusChanged(const GstReceiverStatus &status)
void errorOccurred(const QString &message)
void apply(const Request &request)
Steers the stream towards request, acting on what differs from the previous one.
void recordingStarted(const QString &file)
void shutdown()
Stops everything and quits the thread.
void graphicsDeviceLost()
void recordingFinished(const QString &file, const QString &error)
void onActiveChanged() override
void onReconnectRequested() override
void setHeartbeatInterval(std::chrono::milliseconds interval)
~GstVideoReceiver() override
Finalizes a recording and stops the pipeline, waiting at most a few seconds for the worker.
void onRestartRequested() override
GstVideoReceiver(QObject *parent=nullptr)
void onConfigChanged() override
static constexpr std::chrono::seconds kHeartbeatInterval
void onStartRecording(const QString &file, FileFormat format) override
void onVideoOutputChanged() override
Also called when the current output is set again.
void onStopRecording() override
void followWindowVisibility(QQuickItem *videoOutput)
static constexpr std::chrono::seconds kFinalizeTimeout
void setVideoSize(QSize size)
QQuickItem * videoOutput() const
VideoReceiver(QObject *parent=nullptr)
const DisplayConfig & displayConfig() const
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.
void recordingFinished(const QString &file, const QString &error)
A recording file closed. error is empty when the file was finalized cleanly.
static QSize presentedSize(const QVideoFrame &frame, QSize pictureSize={})
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 setDecoding(bool decoding)
void setRecording(bool recording)
void setState(State state)
const Config & config() const
@ Stopped
Inactive, no URI, or given up after a failure (autoReconnect off).
void errorOccurred(const QString &message)
GstElement * createVideoSink(const QByteArray &conversionElement, bool disablePixelAspectRatio, bool gpuZeroCopy)
std::shared_ptr< T > shareObject(T *object)
A copyable owner of a new ref on object, for tasks that must be copyable (std::function).
std::unique_ptr< GstElement, GstObjectDeleter > GstElementPtr
GstElementPtr adoptElement(GstElement *element) noexcept
QGCQVideoSinkController * setupQVideoSinkElement(GstElement *sinkBin, QVideoSink *videoSink, QObject *controllerParent)
std::optional< Frame > frame(std::string_view text)
VideoReceiver::Config config
std::shared_ptr< GstElement > videoSink
What the worker reports, as it stands after each change.
VideoReceiver::State state
VideoStreamStats stats
About once a second while streaming, and invalid once the session stops.
How the display sink presents frames. Construct-only: a change applies when restart() rebuilds the si...