QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstContextBridgeBase.h
Go to the documentation of this file.
1#pragma once
2
3#include <QtCore/qglobal.h>
4
5#if defined(QGC_HAS_ANY_GPU_PATH)
6
7#include <atomic>
8#include <gst/gst.h>
9#include <memory>
10#include <span>
11#include <vector>
12
13#include <QtCore/QMutex>
14
15#include "GstScoped.h"
16
17QT_FORWARD_DECLARE_CLASS(QLoggingCategory)
18
19
26class GstContextBridgeBase
27{
28public:
29 GstContextBridgeBase(const GstContextBridgeBase&) = delete;
30 GstContextBridgeBase& operator=(const GstContextBridgeBase&) = delete;
31
35 GstBusSyncReply handleSyncMessage(GstMessage* message);
36
39 bool answerContextQuery(GstQuery* query, GstElement* asker = nullptr);
40
42 bool prime();
43
48 void serveWaitingPipelines();
49
52 void reset();
53
56 std::vector<GStreamer::GstElementPtr> takeServed();
57
59 bool deviceLost() const noexcept { return _deviceLost.load(std::memory_order_acquire); }
60
62 void rearm();
63
64protected:
66 static constexpr int kMaxPrimeRetries = 16;
67
68 constexpr GstContextBridgeBase(const char* api, std::span<const char* const> contextTypes,
69 const QLoggingCategory& (*logCategory)()) noexcept
70 : _apiName(api)
71 , _contextTypes(contextTypes)
72 , _category(logCategory)
73 {}
74
75 ~GstContextBridgeBase() = default;
76
79 virtual bool primeLocked() = 0;
80
82 virtual GstObject* refObject(const char* contextType) = 0;
83
85 virtual GstContext* buildContext(const char* contextType, GstObject* object) = 0;
86
88 virtual void clearLocked() = 0;
89
91 bool retryLater() noexcept;
92
95 bool retryWithinBudget() noexcept;
96
99 void noteDeviceRemoved();
100
102 bool retriesJustExhausted() const noexcept { return _failedPrimes == kMaxPrimeRetries + 1; }
103
105 int failedPrimes() const noexcept { return _failedPrimes; }
106
107 const QLoggingCategory& category() const { return _category(); }
108
109 const char* apiName() const noexcept { return _apiName; }
110
111 QMutex _mutex;
112
113private:
114 struct WeakRefDeleter
115 {
116 void operator()(GWeakRef* ref) const noexcept;
117 };
118
119 using WeakRefs = std::vector<std::unique_ptr<GWeakRef, WeakRefDeleter>>;
120
121 const char* matchContextType(const char* type) const;
122 bool primeIfNeededLocked();
126 GStreamer::GstContextPtr makeContext(const char* contextType, GstElement* topLevelBin = nullptr,
127 bool mayWait = false);
130 static void rememberLocked(std::unique_ptr<WeakRefs>& bins, GstElement* topLevelBin,
131 std::vector<GStreamer::GstElementPtr>& released);
134 std::vector<GStreamer::GstElementPtr> takeWaitingLocked(std::vector<GStreamer::GstElementPtr>& released);
136 void giveContexts(const std::vector<GStreamer::GstElementPtr>& topLevelBins);
137
138 const char* _apiName;
139 std::span<const char* const> _contextTypes;
140 const QLoggingCategory& (*_category)();
141 bool _primed = false;
142 bool _primeAttempted = false;
143 int _failedPrimes = 0;
146 std::unique_ptr<WeakRefs> _waiting;
148 std::unique_ptr<WeakRefs> _served;
149 std::atomic<bool> _deviceLost{false};
150};
151
152#endif // QGC_HAS_ANY_GPU_PATH
struct _GstElement GstElement
std::unique_ptr< GstContext, GstContextDeleter > GstContextPtr
Definition GstScoped.h:69