QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
TeeBranchHead.cc
Go to the documentation of this file.
1#include "TeeBranchHead.h"
2
3#include "GStreamerHelpers.h"
4#include "GstVideoReceiver.h"
5
6namespace {
7
8GQuark flowErrorPostedQuark()
9{
10 static const GQuark quark = g_quark_from_static_string("qgc-flow-error-posted");
11 return quark;
12}
13
16GstFlowReturn absorbFlowError(GstPad* pad, GstObject* parent, GstFlowReturn result)
17{
18 if (result >= GST_FLOW_EOS) {
19 return result;
20 }
21 // The pad's stream lock serializes the pushes.
22 if (!g_object_get_qdata(G_OBJECT(pad), flowErrorPostedQuark())) {
23 g_object_set_qdata(G_OBJECT(pad), flowErrorPostedQuark(), GINT_TO_POINTER(TRUE));
24 GST_ELEMENT_FLOW_ERROR(GST_ELEMENT_CAST(parent), result);
25 }
26 return GST_FLOW_OK;
27}
28
29GstFlowReturn guardedChain(GstPad* pad, GstObject* parent, GstBuffer* buffer)
30{
31 return absorbFlowError(pad, parent, gst_proxy_pad_chain_default(pad, parent, buffer));
32}
33
34GstFlowReturn guardedChainList(GstPad* pad, GstObject* parent, GstBufferList* list)
35{
36 return absorbFlowError(pad, parent, gst_proxy_pad_chain_list_default(pad, parent, list));
37}
38
39} // namespace
40
42{
43 const GStreamer::GstPadPtr sink = GStreamer::adoptPad(gst_element_get_static_pad(part, "sink"));
44 // Wrapping the ghost pad's default functions is only right for a pad that still has them.
45 if (!sink || !GST_IS_GHOST_PAD(sink.get()) || (GST_PAD_CHAINFUNC(sink.get()) != gst_proxy_pad_chain_default) ||
46 (GST_PAD_CHAINLISTFUNC(sink.get()) != gst_proxy_pad_chain_list_default)) {
47 qCWarning(GstVideoReceiverLog) << "Unable to keep the flow errors of" << GST_OBJECT_NAME(part)
48 << "out of the tee";
49 return;
50 }
51 gst_pad_set_chain_function(sink.get(), guardedChain);
52 gst_pad_set_chain_list_function(sink.get(), guardedChainList);
53}
54
55bool TeeBranchHead::build(GstElement* pipeline, GstElement* tee, GStreamer::PipelineBranch branch, const char* what)
56{
57 reset();
58
61 if (!queue || !valve) {
62 qCWarning(GstVideoReceiverLog) << "Failed to create the queue and valve of the" << what << "branch";
63 return false;
64 }
65 g_object_set(valve.get(), "drop", TRUE, nullptr);
66 GStreamer::markBranch(queue.get(), branch);
67 GStreamer::markBranch(valve.get(), branch);
68
69 GstElement* const binQueue = GStreamer::addToBin(pipeline, std::move(queue), "queue");
70 GstElement* const binValve = GStreamer::addToBin(pipeline, std::move(valve), "valve");
71 if (!binQueue || !binValve || !gst_element_link_many(tee, binQueue, binValve, nullptr)) {
72 qCWarning(GstVideoReceiverLog) << "Unable to link the" << what << "branch";
73 return false;
74 }
75 _queue = binQueue;
76 _valve = binValve;
77 return true;
78}
79
81{
82 _overrun.disconnect();
83 _queue = nullptr;
84 _valve = nullptr;
85}
86
87void TeeBranchHead::onOverrun(GCallback callback, gpointer data)
88{
89 if (_queue) {
90 (void) _overrun.connect(_queue, "overrun", callback, data);
91 }
92}
93
95{
96 if (!_valve) {
97 return;
98 }
99 g_object_set(_valve, "drop", FALSE, nullptr);
101}
102
104{
105 if (_valve) {
106 g_object_set(_valve, "drop", TRUE, nullptr);
107 }
108}
109
111{
112 if (_valve) {
113 const GStreamer::GstPadPtr valveSink = GStreamer::adoptPad(gst_element_get_static_pad(_valve, "sink"));
114 GStreamer::requestKeyframe(valveSink.get());
115 }
116}
117
118bool TeeBranchHead::contains(GstObject* object) const
119{
120 return (object == GST_OBJECT_CAST(_queue)) || (object == GST_OBJECT_CAST(_valve));
121}
struct _GstElement GstElement
void open()
Lets frames through and asks upstream for a keyframe to start from.
void reset()
Forgets the elements, which the pipeline still owns, and disconnects the overrun handler.
bool contains(GstObject *object) const
Whether object is the queue or the valve.
GstElement * valve() const
void close()
Drops frames again; nothing before build().
bool build(GstElement *pipeline, GstElement *tee, GStreamer::PipelineBranch branch, const char *what)
void requestKeyframe() const
Asks upstream for a keyframe; nothing before build().
GstElement * queue() const
void onOverrun(GCallback callback, gpointer data)
static void absorbFlowErrors(GstElement *part)
GstElementPtr makeElement(const char *factory, const char *name=nullptr)
Definition GstScoped.h:178
void markBranch(GstElement *element, PipelineBranch branch)
Marks element and everything inside it as part of branch.
PipelineBranch
The tee branch a pipeline element belongs to, so a bus message can be blamed on the part that failed.
GstElement * addToBin(GstElement *bin, GstElementPtr element, const char *what)
std::unique_ptr< GstElement, GstObjectDeleter > GstElementPtr
Definition GstScoped.h:60
std::unique_ptr< GstPad, GstObjectDeleter > GstPadPtr
Definition GstScoped.h:61
GstPadPtr adoptPad(GstPad *pad) noexcept
Definition GstScoped.h:84
void requestKeyframe(GstPad *sinkPad)