QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstScoped.h
Go to the documentation of this file.
1#pragma once
2
3#include <gst/gst.h>
4#include <memory>
5
11namespace GStreamer {
12
14{
15 // gst_object_unref takes gpointer; the wrapper keeps the pointer types clean at call sites.
16 void operator()(gpointer obj) const noexcept { gst_object_unref(obj); }
17};
18
20{
21 void operator()(GstQuery* query) const noexcept { gst_query_unref(query); }
22};
23
25{
26 void operator()(GstCaps* caps) const noexcept { gst_caps_unref(caps); }
27};
28
30{
31 void operator()(GstMessage* message) const noexcept { gst_message_unref(message); }
32};
33
35{
36 void operator()(GstEvent* event) const noexcept { gst_event_unref(event); }
37};
38
40{
41 void operator()(GstContext* context) const noexcept { gst_context_unref(context); }
42};
43
45{
46 void operator()(GstStructure* structure) const noexcept { gst_structure_free(structure); }
47};
48
50{
51 void operator()(gpointer mem) const noexcept { g_free(mem); }
52};
53
55{
56 void operator()(GError* error) const noexcept { g_error_free(error); }
57};
58
59using GstObjectPtr = std::unique_ptr<GstObject, GstObjectDeleter>;
60using GstElementPtr = std::unique_ptr<GstElement, GstObjectDeleter>;
61using GstPadPtr = std::unique_ptr<GstPad, GstObjectDeleter>;
62using GstBusPtr = std::unique_ptr<GstBus, GstObjectDeleter>;
63using GstFactoryPtr = std::unique_ptr<GstElementFactory, GstObjectDeleter>;
64using GstFeaturePtr = std::unique_ptr<GstPluginFeature, GstObjectDeleter>;
65using GstQueryPtr = std::unique_ptr<GstQuery, GstQueryDeleter>;
66using GstCapsPtr = std::unique_ptr<GstCaps, GstCapsDeleter>;
67using GstMessagePtr = std::unique_ptr<GstMessage, GstMessageDeleter>;
68using GstEventPtr = std::unique_ptr<GstEvent, GstEventDeleter>;
69using GstContextPtr = std::unique_ptr<GstContext, GstContextDeleter>;
70using GstStructurePtr = std::unique_ptr<GstStructure, GstStructureDeleter>;
71using GCharPtr = std::unique_ptr<gchar, GFreeDeleter>;
72using GErrorPtr = std::unique_ptr<GError, GErrorDeleter>;
73
74inline GstObjectPtr adoptObject(GstObject* object) noexcept
75{
76 return GstObjectPtr(object);
77}
78
79inline GstElementPtr adoptElement(GstElement* element) noexcept
80{
81 return GstElementPtr(element);
82}
83
84inline GstPadPtr adoptPad(GstPad* pad) noexcept
85{
86 return GstPadPtr(pad);
87}
88
89inline GstBusPtr adoptBus(GstBus* bus) noexcept
90{
91 return GstBusPtr(bus);
92}
93
94inline GstFactoryPtr adoptFactory(GstElementFactory* factory) noexcept
95{
96 return GstFactoryPtr(factory);
97}
98
99inline GstFeaturePtr adoptFeature(GstPluginFeature* feature) noexcept
100{
101 return GstFeaturePtr(feature);
102}
103
104inline GstQueryPtr adoptQuery(GstQuery* query) noexcept
105{
106 return GstQueryPtr(query);
107}
108
109inline GstCapsPtr adoptCaps(GstCaps* caps) noexcept
110{
111 return GstCapsPtr(caps);
112}
113
114inline GstMessagePtr adoptMessage(GstMessage* message) noexcept
115{
116 return GstMessagePtr(message);
117}
118
119inline GstEventPtr adoptEvent(GstEvent* event) noexcept
120{
121 return GstEventPtr(event);
122}
123
124inline GstContextPtr adoptContext(GstContext* context) noexcept
125{
126 return GstContextPtr(context);
127}
128
129inline GstStructurePtr adoptStructure(GstStructure* structure) noexcept
130{
131 return GstStructurePtr(structure);
132}
133
134inline GCharPtr adoptGChar(gchar* str) noexcept
135{
136 return GCharPtr(str);
137}
138
139inline GErrorPtr adoptError(GError* error) noexcept
140{
141 return GErrorPtr(error);
142}
143
150
152inline MessageError parseMessageError(GstMessage* message)
153{
154 GError* error = nullptr;
155 gchar* debug = nullptr;
156 const auto parse =
157 (GST_MESSAGE_TYPE(message) == GST_MESSAGE_WARNING) ? gst_message_parse_warning : gst_message_parse_error;
158 parse(message, &error, &debug);
159 return {adoptError(error), adoptGChar(debug)};
160}
161
163template <typename T>
164std::shared_ptr<T> shareObject(T* object)
165{
166 return object ? std::shared_ptr<T>(static_cast<T*>(gst_object_ref(object)), GstObjectDeleter{}) : nullptr;
167}
168
170template <typename T>
171std::unique_ptr<T, GstObjectDeleter> adoptFloating(T* object) noexcept
172{
173 return std::unique_ptr<T, GstObjectDeleter>(object ? static_cast<T*>(gst_object_ref_sink(object)) : nullptr);
174}
175
178inline GstElementPtr makeElement(const char* factory, const char* name = nullptr)
179{
180 return adoptFloating(gst_element_factory_make(factory, name));
181}
182
183inline bool hasElementFactory(const char* name)
184{
185 return static_cast<bool>(adoptFactory(gst_element_factory_find(name)));
186}
187
191{
192public:
193 ScopedSignal() = default;
194
196
197 ScopedSignal(const ScopedSignal&) = delete;
199
201 bool connect(gpointer object, const char* signal, GCallback callback, gpointer data)
202 {
203 disconnect();
204 _id = g_signal_connect(object, signal, callback, data);
205 if (_id != 0) {
206 g_weak_ref_init(&_object, object);
207 }
208 return _id != 0;
209 }
210
211 void disconnect() noexcept
212 {
213 if (_id == 0) {
214 return;
215 }
216 if (gpointer object = g_weak_ref_get(&_object)) {
217 g_signal_handler_disconnect(object, _id);
218 g_object_unref(object);
219 }
220 g_weak_ref_clear(&_object);
221 _id = 0;
222 }
223
224private:
225 GWeakRef _object{};
226 gulong _id = 0;
227};
228
232{
233public:
234 PadProbe() = default;
235
237
238 PadProbe(const PadProbe&) = delete;
239 PadProbe& operator=(const PadProbe&) = delete;
240
242 bool add(GstPad* pad, GstPadProbeType mask, GstPadProbeCallback callback, gpointer userData,
243 GDestroyNotify destroyData = nullptr)
244 {
245 remove();
246 _id = gst_pad_add_probe(pad, mask, callback, userData, destroyData);
247 if (_id != 0) {
248 _pad = adoptPad(GST_PAD(gst_object_ref(pad)));
249 }
250 return _id != 0;
251 }
252
253 void remove() noexcept
254 {
255 if (_id != 0) {
256 gst_pad_remove_probe(_pad.get(), _id);
257 _id = 0;
258 }
259 _pad.reset();
260 }
261
262private:
263 GstPadPtr _pad;
264 gulong _id = 0;
265};
266
267} // namespace GStreamer
struct _GstElement GstElement
bool add(GstPad *pad, GstPadProbeType mask, GstPadProbeCallback callback, gpointer userData, GDestroyNotify destroyData=nullptr)
gst_pad_add_probe() on pad, replacing the probe held so far. False when the probe wasn't added.
Definition GstScoped.h:242
void remove() noexcept
Definition GstScoped.h:253
PadProbe & operator=(const PadProbe &)=delete
PadProbe(const PadProbe &)=delete
void disconnect() noexcept
Definition GstScoped.h:211
ScopedSignal & operator=(const ScopedSignal &)=delete
bool connect(gpointer object, const char *signal, GCallback callback, gpointer data)
g_signal_connect() on object, replacing the handler held so far. False when nothing was connected.
Definition GstScoped.h:201
ScopedSignal(const ScopedSignal &)=delete
std::unique_ptr< GstBus, GstObjectDeleter > GstBusPtr
Definition GstScoped.h:62
std::unique_ptr< GstCaps, GstCapsDeleter > GstCapsPtr
Definition GstScoped.h:66
std::unique_ptr< GstObject, GstObjectDeleter > GstObjectPtr
Definition GstScoped.h:59
std::unique_ptr< GstEvent, GstEventDeleter > GstEventPtr
Definition GstScoped.h:68
GCharPtr adoptGChar(gchar *str) noexcept
Definition GstScoped.h:134
std::unique_ptr< GError, GErrorDeleter > GErrorPtr
Definition GstScoped.h:72
GstStructurePtr adoptStructure(GstStructure *structure) noexcept
Definition GstScoped.h:129
GstQueryPtr adoptQuery(GstQuery *query) noexcept
Definition GstScoped.h:104
GstBusPtr adoptBus(GstBus *bus) noexcept
Definition GstScoped.h:89
std::unique_ptr< GstPluginFeature, GstObjectDeleter > GstFeaturePtr
Definition GstScoped.h:64
GstElementPtr makeElement(const char *factory, const char *name=nullptr)
Definition GstScoped.h:178
GstContextPtr adoptContext(GstContext *context) noexcept
Definition GstScoped.h:124
std::unique_ptr< GstStructure, GstStructureDeleter > GstStructurePtr
Definition GstScoped.h:70
GstEventPtr adoptEvent(GstEvent *event) noexcept
Definition GstScoped.h:119
std::unique_ptr< GstElementFactory, GstObjectDeleter > GstFactoryPtr
Definition GstScoped.h:63
GstFactoryPtr adoptFactory(GstElementFactory *factory) noexcept
Definition GstScoped.h:94
std::unique_ptr< T, GstObjectDeleter > adoptFloating(T *object) noexcept
Sinks the floating ref of a new object (pipeline, bin, pad, element) and owns it. Null for null.
Definition GstScoped.h:171
GstCapsPtr adoptCaps(GstCaps *caps) noexcept
Definition GstScoped.h:109
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
MessageError parseMessageError(GstMessage *message)
gst_message_parse_error(), or gst_message_parse_warning() for a WARNING message.
Definition GstScoped.h:152
std::unique_ptr< GstPad, GstObjectDeleter > GstPadPtr
Definition GstScoped.h:61
GErrorPtr adoptError(GError *error) noexcept
Definition GstScoped.h:139
GstObjectPtr adoptObject(GstObject *object) noexcept
Definition GstScoped.h:74
GstElementPtr adoptElement(GstElement *element) noexcept
Definition GstScoped.h:79
std::unique_ptr< GstMessage, GstMessageDeleter > GstMessagePtr
Definition GstScoped.h:67
std::unique_ptr< GstQuery, GstQueryDeleter > GstQueryPtr
Definition GstScoped.h:65
GstFeaturePtr adoptFeature(GstPluginFeature *feature) noexcept
Definition GstScoped.h:99
std::unique_ptr< GstContext, GstContextDeleter > GstContextPtr
Definition GstScoped.h:69
GstMessagePtr adoptMessage(GstMessage *message) noexcept
Definition GstScoped.h:114
bool hasElementFactory(const char *name)
Definition GstScoped.h:183
std::unique_ptr< gchar, GFreeDeleter > GCharPtr
Definition GstScoped.h:71
GstPadPtr adoptPad(GstPad *pad) noexcept
Definition GstScoped.h:84
void operator()(GError *error) const noexcept
Definition GstScoped.h:56
void operator()(gpointer mem) const noexcept
Definition GstScoped.h:51
void operator()(GstCaps *caps) const noexcept
Definition GstScoped.h:26
void operator()(GstContext *context) const noexcept
Definition GstScoped.h:41
void operator()(GstEvent *event) const noexcept
Definition GstScoped.h:36
void operator()(GstMessage *message) const noexcept
Definition GstScoped.h:31
void operator()(gpointer obj) const noexcept
Definition GstScoped.h:16
void operator()(GstQuery *query) const noexcept
Definition GstScoped.h:21
void operator()(GstStructure *structure) const noexcept
Definition GstScoped.h:46
What an ERROR or WARNING message carries, owned. The debug detail may be null.
Definition GstScoped.h:146