QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
DecodingBranch.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <atomic>
5#include <gst/gst.h>
6#include <memory>
7
8#include <QtCore/QString>
9
10#include "BranchContext.h"
11#include "GstScoped.h"
13#include "TeeBranchHead.h"
14
15namespace GstQgc {
16struct PathStats;
17}
18
36{
37public:
39 {
40 public:
41 virtual void onDecodingChanged(bool decoding) = 0;
42 virtual void onDecoderNameChanged(const QString& name) = 0;
45 virtual void onDecoderFailing() = 0;
46
47 protected:
48 ~Listener() = default;
49 };
50
51 static constexpr int kMaxDecoderRestarts = 3;
52
55 {
57 quint64 framesDisplayed = 0;
61 quint64 framesDropped = 0;
64 QString displayPath;
66 quint64 gpuFallbacks = 0;
68 quint64 decoderRestarts = 0;
69 };
70
72 DecodingBranch(const BranchContext& context, Listener& listener, bool lowLatency);
74
77
79 [[nodiscard]] bool build();
81 void reset();
82
84 void setVideoSink(GstElement* sink);
86 void onSourceStreaming();
88 void restartDecoder(const char* reason);
94
96 bool displaying() const { return _decoder != nullptr; }
97
98 GstElement* valve() const { return _head.valve(); }
99
102 bool isCurrent(GstObject* source) const;
103
107
109 StatsSample sampleStats();
110
112 const QString& decoderName() const { return _decoderName; }
113
115 bool hardwareDecoder() const { return _hardwareDecoder; }
116
117private:
119 void _insertDecoder();
121 [[nodiscard]] bool _tryInsertDecoder();
123 void _removeDecoder();
124 void _linkDecoderPad(GstPad* pad);
126 void _demoteFailedDecoder();
127 void _onFirstFrame(quint64 insertion);
128 void _setDecoding(bool decoding);
129 void _setDecoderName(const QString& name);
132 void _retireSinkCounts();
134 void _countLateFrames();
135
136 static void _onQueueOverrun(GstElement* queue, gpointer data);
137 static GstPadProbeReturn _gateProbe(GstPad* pad, GstPadProbeInfo* info, gpointer data);
138 static GstPadProbeReturn _sinkProbe(GstPad* pad, GstPadProbeInfo* info, gpointer data);
139 static void _onDecoderPadAdded(GstElement* decoder, GstPad* pad, gpointer data);
140 static void _onDecoderElementAdded(GstBin* decodebin, GstBin* subBin, GstElement* element, gpointer data);
141
142 const BranchContext _context;
143 Listener& _listener;
144 const bool _lowLatency;
145
146 TeeBranchHead _head;
147
150 // The sink's "frame-stats" when setVideoSink() took it: the sink outlives sessions and its counters don't restart.
151 GStreamer::GstStructurePtr _sinkStatsAtStart;
152 // The process-wide GPU-path counters then, which never restart either.
153 GstHwPathTelemetry::Snapshot _gpuCountersAtStart;
154 // The sink's per-path delivered counts at the previous sampleStats(), absolute like its "frame-stats".
155 std::array<quint64, GstHw::kPathCount> _pathFramesAtSample{};
156 // Displayed, superseded and GPU-fallback frames of the sinks replaced since the branch was built.
157 quint64 _retiredFramesDisplayed = 0;
158 quint64 _retiredFramesSuperseded = 0;
159 quint64 _retiredGpuFallbacks = 0;
160 // Frames the video sinks dropped as late since the branch was built, and the current sink's own count of them
161 // when they were last added.
162 quint64 _lateFramesDropped = 0;
163 quint64 _sinkLateFramesSeen = 0;
164 QString _displayPath;
165 GStreamer::PadProbe _sinkFrameProbe;
166 GStreamer::PadProbe _keyframeGate;
167 GStreamer::ScopedSignal _decoderPadAdded;
168 GStreamer::ScopedSignal _decoderElementAdded;
169
171 std::atomic<quint64> _overruns = 0;
173 std::atomic<bool> _gateClosed = false;
175 std::atomic<qint64> _gateClosedAtMs = 0;
177 std::atomic<bool> _gateTimeoutWarned = false;
179 std::atomic<quint64> _framesDropped = 0;
181 std::atomic<quint64> _insertions = 0;
182
183 bool _streaming = false;
184 bool _decoding = false;
185 int _restartsWithoutFrame = 0;
186 quint64 _decoderRestarts = 0;
187 QString _decoderName;
188 bool _hardwareDecoder = false;
189 // The decoder decodebin3 plugged last, of the factory _decoderName names.
190 std::shared_ptr<GstElement> _pluggedDecoder;
191};
struct _GstElement GstElement
virtual void onDecodingChanged(bool decoding)=0
virtual void onDecoderFailing()=0
virtual void onDecoderNameChanged(const QString &name)=0
GstElement * valve() const
bool displaying() const
A decoder is inserted, so frames are due at the sink.
bool hardwareDecoder() const
Whether decoderName() is a hardware decoder.
const QString & decoderName() const
The factory of the decoder decodebin3 plugged last; empty while no decoder is inserted or it has plug...
void onSourceStreaming()
The first frame reached the tee: inserts the decoder when a video sink is waiting for it.
static constexpr int kMaxDecoderRestarts
DecodingBranch(const DecodingBranch &)=delete
bool build()
Adds the branch's leaky queue and closed valve behind the tee.
void onGraphicsDeviceServed()
void reset()
Takes the video sink and decoder out of the pipeline, which must be in NULL, and releases the sink.
GstQgc::PathStats sinkPathStats() const
DecodingBranch & operator=(const DecodingBranch &)=delete
bool isCurrent(GstObject *source) const
void setVideoSink(GstElement *sink)
Decodes into sink from now on, or stops decoding for nullptr.
StatsSample sampleStats()
Not const: displayPath compares the sink's counts with the previous sample's.
void restartDecoder(const char *reason)
Replaces the decoder and resets the sink, leaving the rest of the pipeline (and a recording) running.
DecodingBranch(const BranchContext &context, Listener &listener, bool lowLatency)
lowLatency: frames are shown as soon as decoded instead of at their presentation time.
The tee ! queue ! valve head every tee branch starts with.
std::unique_ptr< GstStructure, GstStructureDeleter > GstStructurePtr
Definition GstScoped.h:70
std::unique_ptr< GstElement, GstObjectDeleter > GstElementPtr
Definition GstScoped.h:60
GstVideoSink that pushes decoded frames into a Qt QVideoSink; sole sink in qgcvideosinkbin....
What the branch measured for VideoStreamStats.
quint64 framesDisplayed
Since the branch was built: frames the video sink delivered that no newer frame replaced before displ...
quint64 gpuFallbacks
Since the branch was built: frames a GPU path was tried for but the CPU delivered.
quint64 decoderRestarts
Since the branch was built: decoders replaced by restartDecoder().
Every counter and fallback reason at one instant; a reader's baseline for counts since then.
Formatted per-path frame counts + the total delivered through GPU paths.