QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstQgcBinChain.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <gst/gst.h>
6#include <initializer_list>
7
8namespace GstQgc {
9
13{
14public:
16 static constexpr std::size_t kMaxElements = 5;
17
18 explicit BinChain(GstBin* bin)
19 : _bin(bin)
20 {}
21
23 {
24 if (_committed) {
25 return;
26 }
27 if (_ghost) {
28 gst_element_remove_pad(GST_ELEMENT(_bin), _ghost);
29 }
30 while (_ownedCount > 0) {
31 gst_bin_remove(_bin, _owned[--_ownedCount]);
32 }
33 }
34
35 BinChain(const BinChain&) = delete;
36 BinChain& operator=(const BinChain&) = delete;
37
41 {
42 if (!element) {
43 return nullptr;
44 }
45 // A failed gst_bin_add() sinks and drops a floating ref itself; holding a full ref keeps ours the one to drop.
46 if (g_object_is_floating(element)) {
47 (void) gst_object_ref_sink(element);
48 }
49 const bool added = (_ownedCount < _owned.size()) && gst_bin_add(_bin, element);
50 gst_object_unref(element);
51 if (!added) {
52 return nullptr;
53 }
54 _owned[_ownedCount++] = element;
55 return element;
56 }
57
58 bool linkChain(std::initializer_list<GstElement*> chain)
59 {
60 GstElement* prev = nullptr;
61 for (GstElement* element : chain) {
62 if (!element) {
63 return false;
64 }
65 if (prev && !gst_element_link(prev, element)) {
66 return false;
67 }
68 prev = element;
69 }
70 return true;
71 }
72
74 {
75 GstPad* sinkpad = gst_element_get_static_pad(head, "sink");
76 if (!sinkpad) {
77 return false;
78 }
79 _ghost = gst_ghost_pad_new("sink", sinkpad);
80 gst_object_unref(sinkpad);
81 if (!_ghost) {
82 return false;
83 }
84 if (!gst_element_add_pad(GST_ELEMENT(_bin), _ghost)) {
85 // gst_element_add_pad() consumed the floating ghost pad.
86 _ghost = nullptr;
87 return false;
88 }
89 return true;
90 }
91
92 void commit() { _committed = true; }
93
94private:
95 GstBin* _bin;
96 std::array<GstElement*, kMaxElements> _owned{};
97 std::size_t _ownedCount = 0;
98 GstPad* _ghost = nullptr;
99 bool _committed = false;
100};
101
102} // namespace GstQgc
struct _GstElement GstElement
BinChain & operator=(const BinChain &)=delete
BinChain(const BinChain &)=delete
bool ghostSink(GstElement *head)
bool linkChain(std::initializer_list< GstElement * > chain)
static constexpr std::size_t kMaxElements
The longest chain qgcvideosinkbin builds: videoconvert ! videoscale ! glupload ! glcolorconvert !...
GstElement * adopt(GstElement *element)
BinChain(GstBin *bin)
GstVideoSink that pushes decoded frames into a Qt QVideoSink; sole sink in qgcvideosinkbin....