QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstRtpPacketProbe.cc
Go to the documentation of this file.
1#include "GstRtpPacketProbe.h"
2
3#include <atomic>
4#include <memory>
5
6#include "RtpPacketCounter.h"
7
8namespace {
9
10constexpr char kRtpPacketProbeKey[] = "qgc-rtp-packet-probe";
11// RFC 3550, section 5.1: the version, the sequence number, the timestamp and the SSRC are in the first 12 bytes.
12constexpr gsize kRtpFixedHeaderSize = 12;
13constexpr guint8 kRtpVersion = 2;
14// RFC 5761, section 4: a sender may put its RTCP packets, version 2 as well, on the RTP port. Their second byte is a
15// packet type in this range, which RTP payload types leave free for it.
16constexpr guint8 kRtcpPacketTypeFirst = 192;
17constexpr guint8 kRtcpPacketTypeLast = 223;
18
19struct RtpPacketProbe
20{
21 explicit RtpPacketProbe(int clockRate)
22 : counter(clockRate)
23 {}
24
25 void count(GstBuffer* buffer)
26 {
27 // Copied out rather than mapped: mapping a buffer of several memories merges them into a new one.
28 guint8 header[kRtpFixedHeaderSize];
29 if ((gst_buffer_extract(buffer, 0, header, sizeof(header)) != sizeof(header)) ||
30 ((header[0] >> 6) != kRtpVersion) ||
31 ((header[1] >= kRtcpPacketTypeFirst) && (header[1] <= kRtcpPacketTypeLast))) {
32 return;
33 }
34 const GstClockTime arrival = GST_BUFFER_DTS_OR_PTS(buffer);
35 counter.add(GST_READ_UINT16_BE(header + 2), GST_READ_UINT32_BE(header + 4), GST_READ_UINT32_BE(header + 8),
36 GST_CLOCK_TIME_IS_VALID(arrival) ? std::optional(static_cast<qint64>(arrival)) : std::nullopt);
37 }
38
39 void publish()
40 {
41 packetsReceived.store(counter.packetsReceived(), std::memory_order_relaxed);
42 packetsLost.store(counter.packetsLost(), std::memory_order_relaxed);
43 jitterNs.store(counter.jitterNs().value_or(-1), std::memory_order_relaxed);
44 }
45
47 RtpPacketCounter counter;
48 std::atomic<quint64> packetsReceived = 0;
49 std::atomic<quint64> packetsLost = 0;
50 std::atomic<qint64> jitterNs = -1;
51};
52
53// The pad's probe and the bin each hold one, so neither is left with a probe the other one freed.
54using SharedRtpPacketProbe = std::shared_ptr<RtpPacketProbe>;
55
56void freeRtpPacketProbe(gpointer data)
57{
58 delete static_cast<SharedRtpPacketProbe*>(data);
59}
60
61GstPadProbeReturn onRtpPackets([[maybe_unused]] GstPad* pad, GstPadProbeInfo* info, gpointer data)
62{
63 RtpPacketProbe& probe = **static_cast<SharedRtpPacketProbe*>(data);
64 if (GST_PAD_PROBE_INFO_TYPE(info) & GST_PAD_PROBE_TYPE_BUFFER_LIST) {
65 GstBufferList* const list = GST_PAD_PROBE_INFO_BUFFER_LIST(info);
66 const guint packets = list ? gst_buffer_list_length(list) : 0;
67 for (guint i = 0; i < packets; ++i) {
68 probe.count(gst_buffer_list_get(list, i));
69 }
70 } else if (GstBuffer* const buffer = GST_PAD_PROBE_INFO_BUFFER(info)) {
71 probe.count(buffer);
72 }
73 probe.publish();
74 return GST_PAD_PROBE_OK;
75}
76
77} // namespace
78
79namespace GStreamer {
80
81bool countRtpPackets(GstElement* bin, GstPad* pad, int clockRate)
82{
83 if (!bin || !pad) {
84 return false;
85 }
86 auto* const padProbe = new SharedRtpPacketProbe(std::make_shared<RtpPacketProbe>(clockRate));
87 auto* const binProbe = new SharedRtpPacketProbe(*padProbe);
88 if (gst_pad_add_probe(pad, static_cast<GstPadProbeType>(GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_BUFFER_LIST),
89 onRtpPackets, padProbe, freeRtpPacketProbe) == 0) {
90 delete padProbe;
91 delete binProbe;
92 return false;
93 }
94 g_object_set_data_full(G_OBJECT(bin), kRtpPacketProbeKey, binProbe, freeRtpPacketProbe);
95 return true;
96}
97
98std::optional<RtpPacketCounts> rtpPacketCounts(GstElement* bin)
99{
100 const auto* const shared =
101 bin ? static_cast<const SharedRtpPacketProbe*>(g_object_get_data(G_OBJECT(bin), kRtpPacketProbeKey)) : nullptr;
102 if (!shared) {
103 return std::nullopt;
104 }
105 const RtpPacketProbe& probe = **shared;
106 const qint64 jitterNs = probe.jitterNs.load(std::memory_order_relaxed);
107 return RtpPacketCounts{
108 .packetsReceived = probe.packetsReceived.load(std::memory_order_relaxed),
109 .packetsLost = probe.packetsLost.load(std::memory_order_relaxed),
110 .jitterMs = (jitterNs >= 0) ? (static_cast<double>(jitterNs) / GST_MSECOND) : -1.0,
111 };
112}
113
114} // namespace GStreamer
struct _GstElement GstElement
std::optional< RtpPacketCounts > rtpPacketCounts(GstElement *bin)
What countRtpPackets() counted for bin so far, from any thread; nullopt for a bin without it,...
bool countRtpPackets(GstElement *bin, GstPad *pad, int clockRate)
What countRtpPackets() has counted for a bin since it was installed.