QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
PipelineDotDumper.cc
Go to the documentation of this file.
1#include "PipelineDotDumper.h"
2
3#include <gst/gst.h>
4
5#include <QtCore/QDateTime>
6#include <QtCore/QDir>
7#include <QtCore/QFile>
8#include <QtCore/QFileInfo>
9#include <QtCore/QStandardPaths>
10#include <QtCore/QStringList>
11
12#include "GstScoped.h"
13#include "GstVideoReceiver.h"
14#include "QGCFileHelper.h"
15
16QString PipelineDotDumper::dotFileTag(QStringView tag)
17{
18 // Fits a 255-byte file name with the "-yyyyMMdd-HHmmss-zzz.dot" suffix writePipelineDot() adds, even at 4 UTF-8
19 // bytes a character.
20 constexpr qsizetype kMaxTagLength = (255 - 24) / 4;
21
22 QString safe;
23 for (const QChar c : tag.left(kMaxTagLength)) {
24 safe.append((c.isLetterOrNumber() || (c == u'-') || (c == u'_')) ? c : u'_');
25 }
26 return safe.isEmpty() ? QStringLiteral("graph") : safe;
27}
28
29QString PipelineDotDumper::writePipelineDot(GstElement* pipeline, QStringView tag, const QString& directory)
30{
31 // kMaxDotFiles: cap retained .dot pipeline graphs; below 5 makes retro-debugging hard.
32 constexpr int kMaxDotFiles = 10;
33
34 if (!pipeline || directory.isEmpty()) {
35 return {};
36 }
37 const QDir dir(directory);
38
39 // Rotate: remove oldest .dot files until under cap. Sort by mtime ascending.
40 QFileInfoList existing =
41 dir.entryInfoList(QStringList{QStringLiteral("*.dot")}, QDir::Files, QDir::Time | QDir::Reversed);
42 while (existing.size() >= kMaxDotFiles) {
43 QFile::remove(existing.takeFirst().absoluteFilePath());
44 }
45
46 const GStreamer::GCharPtr data =
47 GStreamer::adoptGChar(gst_debug_bin_to_dot_data(GST_BIN(pipeline), kDiagnosticDotGraphDetails));
48 if (!data) {
49 return {};
50 }
51 const QString fullPath = dir.absoluteFilePath(
52 QStringLiteral("%1-%2.dot")
53 .arg(dotFileTag(tag), QDateTime::currentDateTimeUtc().toString(QStringLiteral("yyyyMMdd-HHmmss-zzz"))));
54 // Creates the directory when needed.
55 const bool wrote = QGCFileHelper::atomicWrite(
56 fullPath, QByteArray::fromRawData(data.get(), static_cast<qsizetype>(qstrlen(data.get()))));
57 return wrote ? fullPath : QString{};
58}
59
60void PipelineDotDumper::setReceiverName(const QString& receiverName)
61{
62 _pipelineName = receiverName.isEmpty() ? QByteArrayLiteral("receiver")
63 : (QStringLiteral("receiver-") + dotFileTag(receiverName)).toUtf8();
64}
65
66void PipelineDotDumper::dump(GstElement* pipeline, const char* tag) const
67{
68 // GST_DEBUG_BIN_TO_DOT_FILE is a no-op unless GST_DEBUG_DUMP_DOT_DIR is set; surface that
69 // once per process so field debugging doesn't require re-reading the source.
70 [[maybe_unused]] static const bool dotDirHinted =
71 []() {
72 if (qEnvironmentVariableIsEmpty("GST_DEBUG_DUMP_DOT_DIR")) {
73 qCDebug(GstVideoReceiverLog)
74 << "Pipeline dot-graph dumps are disabled. Set GST_DEBUG_DUMP_DOT_DIR=/path/to/dir to enable.";
75 }
76 return true;
77 }();
78
79 if (!pipeline) {
80 return;
81 }
82 const QByteArray fileName = _pipelineName + '-' + tag;
83 GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(pipeline), kDiagnosticDotGraphDetails, fileName.constData());
84}
85
86void PipelineDotDumper::snapshot(GstElement* pipeline, const QString& tag) const
87{
88 if (!pipeline) {
89 return;
90 }
91 const QString safeTag = dotFileTag(tag);
92 dump(pipeline, safeTag.toUtf8().constData());
93
94 // The native dump already holds it.
95 if (!qEnvironmentVariableIsEmpty("GST_DEBUG_DUMP_DOT_DIR")) {
96 return;
97 }
98 const QString cacheRoot = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
99 if (cacheRoot.isEmpty()) {
100 return;
101 }
102 const QString dotPath = writePipelineDot(pipeline, QString::fromUtf8(_pipelineName) + u'-' + safeTag,
103 cacheRoot + QStringLiteral("/qgc-pipeline-dot"));
104 if (!dotPath.isEmpty()) {
105 qCDebug(GstVideoReceiverLog) << "Pipeline graph saved to" << dotPath;
106 }
107}
struct _GstElement GstElement
static QString dotFileTag(QStringView tag)
static QString writePipelineDot(GstElement *pipeline, QStringView tag, const QString &directory)
static constexpr GstDebugGraphDetails kDiagnosticDotGraphDetails
Every pipeline graph QGC writes omits element properties, because source properties can contain crede...
void snapshot(GstElement *pipeline, const QString &tag) const
void dump(GstElement *pipeline, const char *tag) const
Into GST_DEBUG_DUMP_DOT_DIR as "<pipeline name>-<tag>.dot", when that is set. No-op without a pipelin...
void setReceiverName(const QString &receiverName)
GCharPtr adoptGChar(gchar *str) noexcept
Definition GstScoped.h:134
std::unique_ptr< gchar, GFreeDeleter > GCharPtr
Definition GstScoped.h:71
bool atomicWrite(const QString &filePath, const QByteArray &data)