QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstHwVideoBuffer.h
Go to the documentation of this file.
1#pragma once
2
3#include <QtCore/QLoggingCategory>
4#include <QtCore/qglobal.h>
5#include <QtMultimedia/QVideoFrame>
6#include <QtMultimedia/QVideoFrameFormat>
7#include <algorithm>
8#include <atomic>
9#include <gst/gst.h>
10#include <gst/video/video-info.h>
11#include <private/qhwvideobuffer_p.h>
12
13namespace GstHw {
14
16constexpr int kMaxPlanes = 4;
17
20{
21 std::atomic<bool> loggedFirstSuccess{false};
22 std::atomic<bool> loggedNullSample{false};
23 std::atomic<bool> loggedWrongThread{false};
24 std::atomic<bool> loggedBadBackend{false};
25 std::atomic<bool> loggedNullBuffer{false};
26 std::atomic<bool> loggedTextureCreateFail{false};
27};
28
29} // namespace GstHw
30
32#define QGC_HW_WARN_ONCE(LOGCAT, FLAG, ...) \
33 do { \
34 if (!(FLAG).exchange(true, std::memory_order_relaxed)) { \
35 qCWarning(LOGCAT) << __VA_ARGS__; \
36 } \
37 } while (0)
38
40class GstHwVideoBuffer : public QHwVideoBuffer
41{
42public:
43 GstHwVideoBuffer(QVideoFrame::HandleType handleType, GstSample* sample, const GstVideoInfo& videoInfo,
44 QVideoFrameFormat format);
45 ~GstHwVideoBuffer() override;
46
47 QVideoFrameFormat format() const override { return _format; }
48
49 MapData map(QVideoFrame::MapMode /*mode*/) override { return {}; }
50
52 virtual bool validatePlaneHandles() const { return true; }
53
55 virtual const char* storageTag() const { return "Unknown"; }
56
58 GstSample* takeSample() noexcept
59 {
60 GstSample* s = _sample;
61 _sample = nullptr;
62 return s;
63 }
64
66 static void logFirstSuccess(std::atomic<bool>& flag, const QLoggingCategory& cat, const char* tag, QSize frameSize,
67 QVideoFrameFormat::PixelFormat pixelFormat, int planes);
68
69protected:
71 template <class PlanePred>
72 bool validatePlanes(PlanePred&& planePred) const
73 {
74 if (!_sample)
75 return false;
76 GstBuffer* buffer = gst_sample_get_buffer(_sample);
77 if (!buffer)
78 return false;
79 const int memCount = (std::min)(int(gst_buffer_n_memory(buffer)), GstHw::kMaxPlanes);
80 if (memCount <= 0)
81 return false;
82 for (int i = 0; i < memCount; ++i) {
83 GstMemory* mem = gst_buffer_peek_memory(buffer, i);
84 if (!planePred(mem))
85 return false;
86 }
87 return true;
88 }
89
91 bool checkMapPreconditions(const QRhi& rhi, int expectedBackend, const QLoggingCategory& cat,
92 GstHw::MapDiagnostics& diag, GstBuffer*& outBuffer) const;
93
94 GstSample* _sample = nullptr;
95 GstVideoInfo _videoInfo = {};
96 QVideoFrameFormat _format;
97};
Common base for GStreamer-backed QHwVideoBuffer subclasses.
GstSample * takeSample() noexcept
Transfers GstSample ownership out for early pool-slot release in mapTextures.
GstVideoInfo _videoInfo
virtual const char * storageTag() const
Human-readable GPU path identifier (e.g. "DMABuf"); string literal, safe from any thread.
~GstHwVideoBuffer() override
MapData map(QVideoFrame::MapMode) override
QVideoFrameFormat format() const override
static void logFirstSuccess(std::atomic< bool > &flag, const QLoggingCategory &cat, const char *tag, QSize frameSize, QVideoFrameFormat::PixelFormat pixelFormat, int planes)
One-shot "first zero-copy success" info line; silent after flag first flips.
QVideoFrameFormat _format
bool checkMapPreconditions(const QRhi &rhi, int expectedBackend, const QLoggingCategory &cat, GstHw::MapDiagnostics &diag, GstBuffer *&outBuffer) const
Shared mapTextures preamble; warns once per cause via diag flags, returns false on first failure.
bool validatePlanes(PlanePred &&planePred) const
Validate each plane satisfies planePred; predicate runs once per plane on the streaming thread.
virtual bool validatePlaneHandles() const
Streaming-thread sanity check on per-plane handles; failure routes to CPU memcpy.
constexpr int kMaxPlanes
Matches GST_VIDEO_MAX_PLANES (gst-video pins it at 4); single source of truth for every per-platform ...
One-shot warning flags per failure cause; paths with extra causes (D3D, IOSurface) derive and add mem...
std::atomic< bool > loggedNullSample
std::atomic< bool > loggedTextureCreateFail
std::atomic< bool > loggedNullBuffer
std::atomic< bool > loggedBadBackend
std::atomic< bool > loggedFirstSuccess
std::atomic< bool > loggedWrongThread