QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GStreamerUdpSrcWorkaround.cc
Go to the documentation of this file.
2
3#include <atomic>
4#include <gst/base/gstbasesrc.h>
5#include <gst/gst.h>
6#include <mutex>
7
8#include "GstScoped.h"
10
11QGC_LOGGING_CATEGORY(GStreamerUdpSrcWorkaroundLog, "Video.GStreamer.UdpSrcWorkaround")
12
13namespace GStreamer {
14
15namespace {
16
17using BaseSrcUnlockFunc = gboolean (*)(GstBaseSrc*);
18
19BaseSrcUnlockFunc s_udpSrcUnlock = nullptr;
20BaseSrcUnlockFunc s_udpSrcUnlockStop = nullptr;
21// Not the object lock: a distro build that backported the upstream fix already takes it inside these calls.
22std::mutex s_udpSrcUnlockMutex;
23
24gboolean _udpSrcUnlockSerialized(GstBaseSrc* src)
25{
26 const std::lock_guard lock(s_udpSrcUnlockMutex);
27 return s_udpSrcUnlock(src);
28}
29
30gboolean _udpSrcUnlockStopSerialized(GstBaseSrc* src)
31{
32 const std::lock_guard lock(s_udpSrcUnlockMutex);
33 return s_udpSrcUnlockStop(src);
34}
35
36std::atomic_bool s_serialized{false};
37
38} // namespace
39
41{
42 static std::once_flag s_once;
43 std::call_once(s_once, [] {
44 const GstFactoryPtr factory = adoptFactory(gst_element_factory_find("udpsrc"));
45 if (!factory || gst_plugin_feature_check_version(GST_PLUGIN_FEATURE(factory.get()), 1, 24, 10)) {
46 return;
47 }
48
49 const GstFeaturePtr loaded = adoptFeature(gst_plugin_feature_load(GST_PLUGIN_FEATURE(factory.get())));
50 const GType type =
51 loaded ? gst_element_factory_get_element_type(GST_ELEMENT_FACTORY(loaded.get())) : G_TYPE_INVALID;
52 if (!g_type_is_a(type, GST_TYPE_BASE_SRC)) {
53 qCWarning(GStreamerUdpSrcWorkaroundLog)
54 << "udpsrc failed to load; stopping a UDP stream right after it starts may crash";
55 return;
56 }
57
58 // Never released, so the patched class outlives every udpsrc.
59 auto* const klass = static_cast<GstBaseSrcClass*>(g_type_class_ref(type));
60 s_udpSrcUnlock = klass->unlock;
61 s_udpSrcUnlockStop = klass->unlock_stop;
62 if (s_udpSrcUnlock && s_udpSrcUnlockStop) {
63 klass->unlock = _udpSrcUnlockSerialized;
64 klass->unlock_stop = _udpSrcUnlockStopSerialized;
65 s_serialized.store(true, std::memory_order_release);
66 qCDebug(GStreamerUdpSrcWorkaroundLog)
67 << "Serialized udpsrc unlock/unlock_stop (gst-plugins-good before 1.24.10)";
68 }
69 });
70}
71
73{
74 return s_serialized.load(std::memory_order_acquire);
75}
76
77} // namespace GStreamer
#define QGC_LOGGING_CATEGORY(name, categoryStr)
bool udpSrcUnlockSerialized()
Whether serializeUdpSrcUnlock() patched udpsrc.
std::unique_ptr< GstPluginFeature, GstObjectDeleter > GstFeaturePtr
Definition GstScoped.h:64
std::unique_ptr< GstElementFactory, GstObjectDeleter > GstFactoryPtr
Definition GstScoped.h:63
GstFactoryPtr adoptFactory(GstElementFactory *factory) noexcept
Definition GstScoped.h:94
GstFeaturePtr adoptFeature(GstPluginFeature *feature) noexcept
Definition GstScoped.h:99