6#include <QtCore/QString>
18constexpr char kDroppedOnLatencyKey[] =
"qgc-dropped-on-latency";
20struct JitterBufferTotals
23 quint64 packetsPushed = 0;
25 quint64 packetsLost = 0;
26 quint64 packetsLate = 0;
27 quint64 packetsDuplicate = 0;
28 quint64 rtxRequested = 0;
29 quint64 rtxRecovered = 0;
30 guint64 maxJitterNs = 0;
34JitterBufferTotals jitterBufferTotals(
GstElement* bin)
36 JitterBufferTotals totals;
37 if (!bin || !GST_IS_BIN(bin)) {
40 const auto addStats = [](
const GValue* item, gpointer data) {
41 auto*
const element = GST_ELEMENT(g_value_get_object(item));
45 GstStructure* rawStats =
nullptr;
46 g_object_get(element,
"stats", &rawStats,
nullptr);
48 const auto field = [&stats](
const char* name) -> guint64 {
return GstQgc::frameStatsField(stats.get(), name); };
49 auto*
const sum =
static_cast<JitterBufferTotals*
>(data);
51 sum->packetsPushed += field(
"num-pushed");
53 field(
"num-lost") + GPOINTER_TO_SIZE(g_object_get_data(G_OBJECT(element), kDroppedOnLatencyKey));
54 sum->packetsLate += field(
"num-late");
55 sum->packetsDuplicate += field(
"num-duplicates");
56 sum->rtxRequested += field(
"rtx-count");
57 sum->rtxRecovered += field(
"rtx-success-count");
58 sum->maxJitterNs = std::max(sum->maxJitterNs, field(
"avg-jitter"));
60 GstIterator*
const elements = gst_bin_iterate_recurse(GST_BIN(bin));
62 while (gst_iterator_foreach(elements, addStats, &totals) == GST_ITERATOR_RESYNC) {
63 gst_iterator_resync(elements);
66 gst_iterator_free(elements);
71QString codecName(
const GstCaps* caps)
73 if (!caps || gst_caps_is_empty(caps) || gst_caps_is_any(caps)) {
76 const GstStructure*
const structure = gst_caps_get_structure(caps, 0);
77 static constexpr std::pair<const char*, const char*> kCodecs[] = {
78 {
"video/x-h264",
"H.264"}, {
"video/x-h265",
"H.265"}, {
"video/x-av1",
"AV1"},
79 {
"video/x-vp8",
"VP8"}, {
"video/x-vp9",
"VP9"}, {
"image/jpeg",
"MJPEG"},
81 for (
const auto& [mediaType, codec] : kCodecs) {
82 if (gst_structure_has_name(structure, mediaType)) {
83 return QString::fromLatin1(codec);
87 if (gst_structure_has_name(structure,
"video/mpeg") &&
88 gst_structure_get_int(structure,
"mpegversion", &mpegVersion)) {
89 return QStringLiteral(
"MPEG-%1").arg(mpegVersion);
91 return QString::fromUtf8(gst_structure_get_name(structure));
98 (void) _bytesReceived.fetch_add(gst_buffer_get_size(buffer), std::memory_order_relaxed);
99 if (GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_DELTA_UNIT)) {
102 const GstClockTime time = GST_BUFFER_PTS_IS_VALID(buffer) ? GST_BUFFER_PTS(buffer) : GST_BUFFER_DTS(buffer);
103 if (GST_CLOCK_TIME_IS_VALID(time)) {
110 const GstStructure*
const structure = gst_message_get_structure(message);
111 GstObject*
const jitterBuffer = GST_MESSAGE_SRC(message);
114 if (!structure || !jitterBuffer || !gst_structure_has_name(structure,
"drop-msg") ||
115 !gst_structure_get_uint(structure,
"num-drop-on-latency", &dropped) || (dropped == 0)) {
118 const gsize total = GPOINTER_TO_SIZE(g_object_get_data(G_OBJECT(jitterBuffer), kDroppedOnLatencyKey)) + dropped;
119 g_object_set_data(G_OBJECT(jitterBuffer), kDroppedOnLatencyKey, GSIZE_TO_POINTER(total));
122void PipelineStatsProbe::_noteKeyframe(GstClockTime time)
124 if (_keyframeTimeCount > 0) {
125 const GstClockTime previous = _keyframeTimes[_keyframeTimeCount - 1];
127 if (time == previous) {
131 if (time < previous) {
132 _keyframeTimeCount = 0;
135 if (_keyframeTimeCount == kKeyframeWindow) {
136 std::shift_left(_keyframeTimes.begin(), _keyframeTimes.end(), 1);
137 --_keyframeTimeCount;
139 _keyframeTimes[_keyframeTimeCount++] = time;
140 if (_keyframeTimeCount >= 2) {
141 const GstClockTime span = _keyframeTimes[_keyframeTimeCount - 1] - _keyframeTimes[0];
142 _keyframeInterval.store(span / (_keyframeTimeCount - 1), std::memory_order_relaxed);
149 counters.
bytesReceived = _bytesReceived.load(std::memory_order_relaxed);
151 counters.
rtp.reset();
157 const JitterBufferTotals jitterBuffers = jitterBufferTotals(source);
158 if (jitterBuffers.found) {
160 .packetsLost = jitterBuffers.packetsLost};
161 stats.
jitterMs =
static_cast<double>(jitterBuffers.maxJitterNs) / GST_MSECOND;
162 stats.
packetsLate =
static_cast<qint64
>(jitterBuffers.packetsLate);
163 stats.
packetsDuplicate =
static_cast<qint64
>(jitterBuffers.packetsDuplicate);
164 stats.
rtxRequested =
static_cast<qint64
>(jitterBuffers.rtxRequested);
165 stats.
rtxRecovered =
static_cast<qint64
>(jitterBuffers.rtxRecovered);
169 _rtpPacketsLost = std::max(_rtpPacketsLost, packets->packetsLost);
171 .packetsLost = _rtpPacketsLost};
175 const GstClockTime keyframeInterval = _keyframeInterval.load(std::memory_order_relaxed);
177 GST_CLOCK_TIME_IS_VALID(keyframeInterval) ?
static_cast<double>(keyframeInterval) / GST_MSECOND : -1.0;
179 GstClockTime minLatency = 0;
180 GstClockTime maxLatency = 0;
182 queryLatency(pipeline, minLatency, maxLatency) ?
static_cast<double>(minLatency) / GST_MSECOND : -1.0;
187 stats.
codec = codecName(caps.get());
196 if (!gst_element_query(pipeline, query.get())) {
199 gboolean live = FALSE;
200 gst_query_parse_latency(query.get(), &live, &min, &max);
struct _GstElement GstElement
static void noteElementMessage(GstMessage *message)
void sample(GstElement *pipeline, GstElement *source, GstElement *tee, VideoStreamStatsMeter::Counters &counters, VideoStreamStats &stats)
void noteBuffer(GstBuffer *buffer)
Streaming thread (the tee's sink pad, so one at a time): buffer reached the tee.
static bool queryLatency(GstElement *pipeline, GstClockTime &min, GstClockTime &max)
The latency pipeline reports; false when it doesn't answer the latency query.
std::unique_ptr< GstCaps, GstCapsDeleter > GstCapsPtr
GstStructurePtr adoptStructure(GstStructure *structure) noexcept
GstQueryPtr adoptQuery(GstQuery *query) noexcept
std::unique_ptr< GstStructure, GstStructureDeleter > GstStructurePtr
std::optional< RtpPacketCounts > rtpPacketCounts(GstElement *bin)
What countRtpPackets() counted for bin so far, from any thread; nullopt for a bin without it,...
const char * factoryNameOf(GstObject *object)
The name of the factory that made object ("rtph264depay"); null when it is no element or has no facto...
GstCapsPtr adoptCaps(GstCaps *caps) noexcept
std::unique_ptr< GstPad, GstObjectDeleter > GstPadPtr
std::unique_ptr< GstQuery, GstQueryDeleter > GstQueryPtr
GstPadPtr adoptPad(GstPad *pad) noexcept
quint64 frameStatsField(const GstStructure *stats, const char *field) noexcept
The guint64 field of stats, a "frame-stats" or any other structure; 0 without the structure or the fi...
std::optional< quint64 > bytesReceived
Only for a backend that counts the bytes it receives: bitrateKbps is -1 without.
std::optional< RtpCounters > rtp
Only for an RTP stream whose packets the backend counts.
QString codec
"H.264", "H.265", ..., or a USB camera's capture format, such as "MJPEG" or "YUYV".
double keyframeIntervalMs
Between keyframes, from their timestamps, averaged over the last few.
double latencyMs
The pipeline's minimum latency, the playout delay it adds on top of the network's.
double jitterMs
RTP interarrival jitter (RFC 3550), averaged over the last packets.
qint64 packetsLate
Since the stream started: RTP packets that arrived after their playout time, and duplicate RTP packet...
qint64 rtxRequested
Since the stream started: RTP retransmissions requested (RTSP), and how many arrived in time.