QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GStreamerHelpers.cc
Go to the documentation of this file.
1#include "GStreamerHelpers.h"
2
3#include <algorithm>
4#include <gst/video/video-event.h>
5
6#include <QtCore/QByteArray>
7#include <QtCore/QFile>
8#include <QtCore/QLatin1String>
9#include <QtCore/QString>
10
11#include "GstScoped.h"
12#include "QGCLoggingCategory.h"
13
14QGC_LOGGING_CATEGORY(GStreamerHelpersLog, "Video.GStreamer.GStreamerHelpers")
15
16namespace GStreamer {
17
18namespace {
19
20void removeAndStop(GstElement* element, bool flush)
21{
22 const GstObjectPtr parent = element ? adoptObject(gst_object_get_parent(GST_OBJECT(element))) : nullptr;
23 if (!parent) {
24 return;
25 }
26
27 // gst_bin_remove drops the bin's ref, which may be the last one, before the element reaches NULL.
28 const GstElementPtr keepAlive = adoptElement(GST_ELEMENT(gst_object_ref(element)));
29 // Under the bin's state lock, which a state change of the bin holds while it sets its children's states: one that
30 // took the element from the bin's children before the removal (as the continuation GStreamer's thread pool runs
31 // once an async sink is gone) would otherwise set it to the bin's state after it reached NULL here.
32 GST_STATE_LOCK(parent.get());
33 (void) gst_bin_remove(GST_BIN(parent.get()), element);
34 if (flush) {
35 (void) gst_element_foreach_sink_pad(
36 element,
37 [](GstElement* /*element*/, GstPad* pad, gpointer /*userData*/) -> gboolean {
38 (void) gst_pad_send_event(pad, gst_event_new_flush_start());
39 return TRUE;
40 },
41 nullptr);
42 }
43 // Synchronous: gst_element_change_state() commits an ASYNC return at once when the target is READY or NULL, so
44 // the streaming threads have stopped when this returns.
45 (void) gst_element_set_state(element, GST_STATE_NULL);
46 GST_STATE_UNLOCK(parent.get());
47}
48
49} // namespace
50
52{
53 removeAndStop(element, false);
54}
55
57{
58 removeAndStop(element, true);
59}
60
62{
63 if (!element) {
64 return;
65 }
66#if GST_CHECK_VERSION(1, 28, 0)
67 // Replaces gst_element_call_async(), deprecated in 1.28.
68 gst_object_call_async(
69 GST_OBJECT_CAST(element), [](GstObject* object, gpointer /*data*/) { disposeChild(GST_ELEMENT_CAST(object)); },
70 nullptr);
71#else
72 gst_element_call_async(
73 element, [](GstElement* child, gpointer /*data*/) { disposeChild(child); }, nullptr, nullptr);
74#endif
75}
76
77GstElement* addToBin(GstElement* bin, GstElementPtr element, const char* what)
78{
79 if (!element || g_object_is_floating(element.get())) {
80 qCCritical(GStreamerHelpersLog) << "Missing or floating element for" << what;
81 return nullptr;
82 }
83 GstElement* const added = element.release();
84 const bool ok = gst_bin_add(GST_BIN(bin), added);
85 // The bin took its own reference; drop ours.
86 gst_object_unref(added);
87 if (!ok) {
88 qCWarning(GStreamerHelpersLog) << "gst_bin_add() failed for" << what;
89 return nullptr;
90 }
91 return added;
92}
93
94QByteArray glibFileName(const QString& file)
95{
96#ifdef Q_OS_WIN
97 return file.toUtf8();
98#else
99 return QFile::encodeName(file);
100#endif
101}
102
103void requestKeyframe(GstPad* sinkPad)
104{
105 if (!sinkPad) {
106 return;
107 }
108 (void) gst_pad_push_event(sinkPad, gst_video_event_new_upstream_force_key_unit(GST_CLOCK_TIME_NONE, TRUE, 0));
109}
110
111void sendEventAsync(GstPad* pad, GstEvent* event)
112{
113 struct PendingEvent
114 {
115 GstPadPtr pad;
116 GstEventPtr event;
117 };
118
119 GstElementPtr parent = pad ? adoptElement(gst_pad_get_parent_element(pad)) : nullptr;
120 if (!parent) {
121 gst_event_unref(event);
122 return;
123 }
124 auto* const pending = new PendingEvent{.pad = adoptPad(GST_PAD(gst_object_ref(pad))), .event = adoptEvent(event)};
125#if GST_CHECK_VERSION(1, 28, 0)
126 // Replaces gst_element_call_async(), deprecated in 1.28. It takes no destroy notify: the call frees its data.
127 gst_object_call_async(
128 GST_OBJECT_CAST(parent.get()),
129 [](GstObject* /*object*/, gpointer data) {
130 const std::unique_ptr<PendingEvent> owned(static_cast<PendingEvent*>(data));
131 (void) gst_pad_send_event(owned->pad.get(), owned->event.release());
132 },
133 pending);
134#else
135 gst_element_call_async(
136 parent.get(),
137 [](GstElement* /*element*/, gpointer data) {
138 auto* const queued = static_cast<PendingEvent*>(data);
139 (void) gst_pad_send_event(queued->pad.get(), queued->event.release());
140 },
141 pending, [](gpointer data) { delete static_cast<PendingEvent*>(data); });
142#endif
143}
144
146{
147 GstPad* first = nullptr;
148 // Handles iterator resync itself, unlike a bare gst_iterator_next loop.
149 (void) gst_element_foreach_src_pad(
150 element,
151 [](GstElement* /*element*/, GstPad* pad, gpointer userData) -> gboolean {
152 *static_cast<GstPad**>(userData) = GST_PAD(gst_object_ref(pad));
153 return FALSE;
154 },
155 &first);
156 return adoptPad(first);
157}
158
159const char* factoryNameOf(GstObject* object)
160{
161 GstElementFactory* const factory =
162 (object && GST_IS_ELEMENT(object)) ? gst_element_get_factory(GST_ELEMENT_CAST(object)) : nullptr;
163 return factory ? GST_OBJECT_NAME(factory) : nullptr;
164}
165
166std::function<void()> holdPadUntilLinked(GstPad* pad, std::function<void(GstPad* pad)> link)
167{
168 // Returning OK from a blocking probe keeps the pad blocked until the probe is removed.
169 const gulong blockId = gst_pad_add_probe(
170 pad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
171 [](GstPad* /*pad*/, GstPadProbeInfo* /*info*/, gpointer /*data*/) { return GST_PAD_PROBE_OK; }, nullptr,
172 nullptr);
173 return [blockId, heldPad = shareObject(pad), link = std::move(link)]() {
174 link(heldPad.get());
175 gst_pad_remove_probe(heldPad.get(), blockId);
176 };
177}
178
179void forEachPlugin(GstRegistry* registry, const std::function<void(GstPlugin*)>& visitor)
180{
181 if (!registry || !visitor)
182 return;
183 GList* plugins = gst_registry_get_plugin_list(registry);
184 for (GList* node = plugins; node != nullptr; node = node->next) {
185 GstPlugin* plugin = static_cast<GstPlugin*>(node->data);
186 if (plugin)
187 visitor(plugin);
188 }
189 gst_plugin_list_free(plugins);
190}
191
192} // namespace GStreamer
struct _GstElement GstElement
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void disposeChild(GstElement *element)
void disposeChildAsync(GstElement *element)
GstPadPtr firstSrcPad(GstElement *element)
element's first src pad, or null when it has none (yet).
std::unique_ptr< GstObject, GstObjectDeleter > GstObjectPtr
Definition GstScoped.h:59
std::unique_ptr< GstEvent, GstEventDeleter > GstEventPtr
Definition GstScoped.h:68
std::function< void()> holdPadUntilLinked(GstPad *pad, std::function< void(GstPad *pad)> link)
GstEventPtr adoptEvent(GstEvent *event) noexcept
Definition GstScoped.h:119
void forEachPlugin(GstRegistry *registry, const std::function< void(GstPlugin *)> &visitor)
const char * factoryNameOf(GstObject *object)
The name of the factory that made object ("rtph264depay"); null when it is no element or has no facto...
GstElement * addToBin(GstElement *bin, GstElementPtr element, const char *what)
void sendEventAsync(GstPad *pad, GstEvent *event)
std::shared_ptr< T > shareObject(T *object)
A copyable owner of a new ref on object, for tasks that must be copyable (std::function).
Definition GstScoped.h:164
std::unique_ptr< GstElement, GstObjectDeleter > GstElementPtr
Definition GstScoped.h:60
std::unique_ptr< GstPad, GstObjectDeleter > GstPadPtr
Definition GstScoped.h:61
GstObjectPtr adoptObject(GstObject *object) noexcept
Definition GstScoped.h:74
GstElementPtr adoptElement(GstElement *element) noexcept
Definition GstScoped.h:79
QByteArray glibFileName(const QString &file)
void flushAndDisposeChild(GstElement *element)
GstPadPtr adoptPad(GstPad *pad) noexcept
Definition GstScoped.h:84
void requestKeyframe(GstPad *sinkPad)