QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstContextBridgeBase.cc
Go to the documentation of this file.
2
3#include <glib.h>
4#include <utility>
5
6#include <QtCore/QCoreApplication>
7#include <QtCore/QLoggingCategory>
8#include <QtCore/QMutexLocker>
9
10#include "HwBuffers.h"
11
12namespace {
13
16GStreamer::GstElementPtr topLevelBinOf(GstElement* element)
17{
18 GStreamer::GstObjectPtr top = GStreamer::adoptObject(GST_OBJECT(gst_object_ref(element)));
19 while (GStreamer::GstObjectPtr parent = GStreamer::adoptObject(gst_object_get_parent(top.get()))) {
20 top = std::move(parent);
21 }
22 return GStreamer::adoptElement(GST_ELEMENT(top.release()));
23}
24
25} // namespace
26
27void GstContextBridgeBase::WeakRefDeleter::operator()(GWeakRef* ref) const noexcept
28{
29 g_weak_ref_clear(ref);
30 delete ref;
31}
32
33const char* GstContextBridgeBase::matchContextType(const char* type) const
34{
35 if (!type) {
36 return nullptr;
37 }
38 for (const char* const known : _contextTypes) {
39 if (g_strcmp0(type, known) == 0) {
40 return known;
41 }
42 }
43 return nullptr;
44}
45
46bool GstContextBridgeBase::primeIfNeededLocked()
47{
48 if (_primed) {
49 return true;
50 }
51 if (_primeAttempted) {
52 return false;
53 }
54 _primeAttempted = true;
55 _primed = primeLocked();
56 if (_primed) {
57 _deviceLost.store(false, std::memory_order_release);
58 }
59 return _primed;
60}
61
62void GstContextBridgeBase::rememberLocked(std::unique_ptr<WeakRefs>& bins, GstElement* topLevelBin,
63 std::vector<GStreamer::GstElementPtr>& released)
64{
65 if (!bins) {
66 bins = std::make_unique<WeakRefs>();
67 }
68 bool known = false;
69 std::erase_if(*bins, [&](const auto& ref) {
70 GStreamer::GstElementPtr bin = GStreamer::adoptElement(GST_ELEMENT(g_weak_ref_get(ref.get())));
71 if (!bin) {
72 return true;
73 }
74 known = known || (bin.get() == topLevelBin);
75 released.push_back(std::move(bin));
76 return false;
77 });
78 if (!known) {
79 GWeakRef* const ref = new GWeakRef;
80 g_weak_ref_init(ref, topLevelBin);
81 bins->emplace_back(ref);
82 }
83}
84
85std::vector<GStreamer::GstElementPtr> GstContextBridgeBase::takeWaitingLocked(
86 std::vector<GStreamer::GstElementPtr>& released)
87{
88 std::vector<GStreamer::GstElementPtr> bins;
89 if (const std::unique_ptr<WeakRefs> waiting = std::move(_waiting)) {
90 for (const auto& ref : *waiting) {
91 if (GstElement* const bin = GST_ELEMENT(g_weak_ref_get(ref.get()))) {
92 bins.push_back(GStreamer::adoptElement(bin));
93 }
94 }
95 }
96 // They are given the contexts next.
97 for (const GStreamer::GstElementPtr& bin : bins) {
98 rememberLocked(_served, bin.get(), released);
99 }
100 return bins;
101}
102
103std::vector<GStreamer::GstElementPtr> GstContextBridgeBase::takeServed()
104{
105 std::vector<GStreamer::GstElementPtr> bins;
106 QMutexLocker lock(&_mutex);
107 if (const std::unique_ptr<WeakRefs> served = std::move(_served)) {
108 for (const auto& ref : *served) {
109 if (GstElement* const bin = GST_ELEMENT(g_weak_ref_get(ref.get()))) {
110 bins.push_back(GStreamer::adoptElement(bin));
111 }
112 }
113 }
114 return bins;
115}
116
117void GstContextBridgeBase::noteDeviceRemoved()
118{
119 _deviceLost.store(true, std::memory_order_release);
120 // The device may notify with its own locks held, on a thread it joins when it is disposed: nothing is dropped,
121 // and no bridge's mutex taken, before the GUI thread gets to it.
122 if (QCoreApplication* const app = QCoreApplication::instance()) {
123 (void) QMetaObject::invokeMethod(app, [] { HwBuffers::onGraphicsDeviceRemoved(); }, Qt::QueuedConnection);
124 }
125}
126
127void GstContextBridgeBase::giveContexts(const std::vector<GStreamer::GstElementPtr>& topLevelBins)
128{
129 for (const GStreamer::GstElementPtr& bin : topLevelBins) {
130 bool given = false;
131 for (const char* const contextType : _contextTypes) {
132 if (const GStreamer::GstContextPtr context = makeContext(contextType)) {
133 gst_element_set_context(bin.get(), context.get());
134 given = true;
135 qCDebug(category()) << "Provided" << _apiName << contextType << "context to"
136 << GST_ELEMENT_NAME(bin.get()) << "which asked before the bridge was primed";
137 }
138 }
139 if (given) {
140 // An element that has a device ignores the context: only the bin's owner can replace it.
141 (void) gst_element_post_message(
142 bin.get(), gst_message_new_element(GST_OBJECT(bin.get()),
143 gst_structure_new_empty(HwBuffers::kGraphicsDeviceServedMessage)));
144 }
145 }
146}
147
148GStreamer::GstContextPtr GstContextBridgeBase::makeContext(const char* contextType, GstElement* topLevelBin,
149 bool mayWait)
150{
152 // Released, and served, without the mutex.
153 std::vector<GStreamer::GstElementPtr> released;
154 std::vector<GStreamer::GstElementPtr> bins;
155 {
156 QMutexLocker lock(&_mutex);
157 const bool wasPrimed = _primed;
158 if (!primeIfNeededLocked()) {
159 if (topLevelBin && mayWait) {
160 rememberLocked(_waiting, topLevelBin, released);
161 }
162 return nullptr;
163 }
164 if (!wasPrimed) {
165 bins = takeWaitingLocked(released);
166 }
167 if (topLevelBin) {
168 rememberLocked(_served, topLevelBin, released);
169 }
170 object = GStreamer::adoptObject(refObject(contextType));
171 }
172 giveContexts(bins);
173 if (!object) {
174 return nullptr;
175 }
176 // The context holds its own ref on the object.
177 return GStreamer::adoptContext(buildContext(contextType, object.get()));
178}
179
180GstBusSyncReply GstContextBridgeBase::handleSyncMessage(GstMessage* message)
181{
182 if (GST_MESSAGE_TYPE(message) != GST_MESSAGE_NEED_CONTEXT) {
183 return GST_BUS_PASS;
184 }
185 const gchar* contextType = nullptr;
186 if (!gst_message_parse_context_type(message, &contextType)) {
187 return GST_BUS_PASS;
188 }
189 const char* matched = matchContextType(contextType);
190 if (!matched) {
191 return GST_BUS_PASS;
192 }
193 GstElement* element = GST_ELEMENT(GST_MESSAGE_SRC(message));
194 if (!element) {
195 return GST_BUS_PASS;
196 }
197
198 const GStreamer::GstElementPtr topLevelBin = topLevelBinOf(element);
199 const GStreamer::GstContextPtr context = makeContext(matched, topLevelBin.get(), /*mayWait=*/true);
200 if (!context) {
201 return GST_BUS_PASS;
202 }
203 // The bin sets it on @p element and on every other element it holds or is given later.
204 gst_element_set_context(topLevelBin.get(), context.get());
205 qCDebug(category()) << "Provided" << _apiName << matched << "context to" << GST_ELEMENT_NAME(topLevelBin.get())
206 << "for" << GST_ELEMENT_NAME(element);
207 // `element` is borrowed from the message, so the message must outlive its last use.
208 gst_message_unref(message);
209 return GST_BUS_DROP;
210}
211
212bool GstContextBridgeBase::answerContextQuery(GstQuery* query, GstElement* asker)
213{
214 if (!query || GST_QUERY_TYPE(query) != GST_QUERY_CONTEXT) {
215 return false;
216 }
217 const gchar* contextType = nullptr;
218 if (!gst_query_parse_context_type(query, &contextType)) {
219 return false;
220 }
221 const char* matched = matchContextType(contextType);
222 if (!matched) {
223 return false;
224 }
225
226 // The element that asks takes the device from the answer, without a NEED_CONTEXT that would name its pipeline.
227 const GStreamer::GstElementPtr topLevelBin = asker ? topLevelBinOf(asker) : nullptr;
228 const GStreamer::GstContextPtr context = makeContext(matched, topLevelBin.get());
229 if (!context) {
230 return false;
231 }
232 gst_query_set_context(query, context.get());
233 return true;
234}
235
236bool GstContextBridgeBase::prime()
237{
238 std::vector<GStreamer::GstElementPtr> released;
239 std::vector<GStreamer::GstElementPtr> bins;
240 {
241 QMutexLocker lock(&_mutex);
242 const bool wasPrimed = _primed;
243 if (!primeIfNeededLocked()) {
244 return false;
245 }
246 if (!wasPrimed) {
247 bins = takeWaitingLocked(released);
248 }
249 }
250 giveContexts(bins);
251 return true;
252}
253
254void GstContextBridgeBase::serveWaitingPipelines()
255{
256 {
257 QMutexLocker lock(&_mutex);
258 if (!_waiting || _waiting->empty()) {
259 return;
260 }
261 }
262 (void) prime();
263}
264
265void GstContextBridgeBase::reset()
266{
267 // Dropped without the mutex.
268 const std::vector<GStreamer::GstElementPtr> served = takeServed();
269 QMutexLocker lock(&_mutex);
270 clearLocked();
271 _primed = false;
272 _primeAttempted = false;
273 _failedPrimes = 0;
274 qCDebug(category()) << _apiName << "bridge reset";
275}
276
277void GstContextBridgeBase::rearm()
278{
279 QMutexLocker lock(&_mutex);
280 if (!_primed && _primeAttempted && (_failedPrimes > kMaxPrimeRetries)) {
281 _primeAttempted = false;
282 _failedPrimes = 0;
283 qCDebug(category()) << _apiName << "bridge rearmed after exhausting its prime retries";
284 }
285}
286
287bool GstContextBridgeBase::retryLater() noexcept
288{
289 _primeAttempted = false;
290 return false;
291}
292
293bool GstContextBridgeBase::retryWithinBudget() noexcept
294{
295 ++_failedPrimes;
296 if (_failedPrimes <= kMaxPrimeRetries) {
297 _primeAttempted = false;
298 return true;
299 }
300 return false;
301}
struct _GstElement GstElement
std::unique_ptr< GstObject, GstObjectDeleter > GstObjectPtr
Definition GstScoped.h:59
GstContextPtr adoptContext(GstContext *context) noexcept
Definition GstScoped.h:124
std::unique_ptr< GstElement, GstObjectDeleter > GstElementPtr
Definition GstScoped.h:60
GstObjectPtr adoptObject(GstObject *object) noexcept
Definition GstScoped.h:74
GstElementPtr adoptElement(GstElement *element) noexcept
Definition GstScoped.h:79
std::unique_ptr< GstContext, GstContextDeleter > GstContextPtr
Definition GstScoped.h:69
void onGraphicsDeviceRemoved()
Definition HwBuffers.cc:102
constexpr char kGraphicsDeviceServedMessage[]
Definition HwBuffers.h:22