QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GStreamerSelfTestMedia.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <atomic>
5#include <gst/app/gstappsrc.h>
6#include <mutex>
7
9#include "GStreamerHelpers.h"
10#include "GstScoped.h"
11#include "RecordingBranch.h"
12
13using namespace std::chrono_literals;
14
15namespace GStreamer::SelfTest {
16
17namespace {
18
19GstCapsPtr byteStreamCaps(const Sample& sample)
20{
21 return adoptCaps(gst_caps_new_simple(sample.mediaType, "stream-format", G_TYPE_STRING, "byte-stream", nullptr));
22}
23
24QString errorText(GstMessage* message)
25{
26 const auto [error, debug] = parseMessageError(message);
27 QString text = QStringLiteral("%1: %2").arg(QString::fromUtf8(GST_MESSAGE_SRC_NAME(message)),
28 error ? QString::fromUtf8(error->message) : QStringLiteral("error"));
29 if (debug) {
30 text += QStringLiteral(" (%1)").arg(QString::fromUtf8(debug.get()));
31 }
32 return text;
33}
34
37QString playToEos(GstElement* pipeline, GstElement* source, const QByteArray& data, std::chrono::milliseconds timeout)
38{
39 const GstBusPtr bus = adoptBus(gst_element_get_bus(pipeline));
40 const bool started = gst_element_set_state(pipeline, GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE;
41 if (started && source) {
42 (void) gst_app_src_push_buffer(GST_APP_SRC(source),
43 gst_buffer_new_memdup(data.constData(), static_cast<gsize>(data.size())));
44 (void) gst_app_src_end_of_stream(GST_APP_SRC(source));
45 }
46 const GstClockTime wait =
47 started ? static_cast<GstClockTime>(std::chrono::nanoseconds(std::max(timeout, 0ms)).count()) : 0;
48 const GstMessagePtr message = adoptMessage(
49 gst_bus_timed_pop_filtered(bus.get(), wait, static_cast<GstMessageType>(GST_MESSAGE_EOS | GST_MESSAGE_ERROR)));
50 (void) gst_element_set_state(pipeline, GST_STATE_NULL);
51
52 if (message && (GST_MESSAGE_TYPE(message.get()) == GST_MESSAGE_EOS)) {
53 return {};
54 }
55 if (message) {
56 return errorText(message.get());
57 }
58 return started ? QStringLiteral("no end of stream within %1 ms").arg(timeout.count())
59 : QStringLiteral("the pipeline failed to start");
60}
61
62void linkNewSrcPad([[maybe_unused]] GstElement* element, GstPad* pad, gpointer sinkPad)
63{
64 if (GST_PAD_IS_SRC(pad) && !gst_pad_is_linked(GST_PAD(sinkPad))) {
65 (void) gst_pad_link(pad, GST_PAD(sinkPad));
66 }
67}
68
69GstPadProbeReturn countBuffer([[maybe_unused]] GstPad* pad, [[maybe_unused]] GstPadProbeInfo* info, gpointer count)
70{
71 static_cast<std::atomic<int>*>(count)->fetch_add(1);
72 return GST_PAD_PROBE_OK;
73}
74
76struct DecoderWatch
77{
78 std::mutex mutex;
79 QStringList decoders;
80 bool hardware = false;
81};
82
83void watchDecoder([[maybe_unused]] GstBin* bin, [[maybe_unused]] GstBin* subBin, GstElement* element, gpointer data)
84{
85 GstElementFactory* const factory = gst_element_get_factory(element);
86 if (!factory || !gst_element_factory_list_is_type(factory, GST_ELEMENT_FACTORY_TYPE_DECODER)) {
87 return;
88 }
89 auto* const watch = static_cast<DecoderWatch*>(data);
90 const std::lock_guard lock(watch->mutex);
91 watch->decoders.append(QString::fromUtf8(gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory))));
92 watch->hardware = watch->hardware || isHardwareDecoderFactory(factory);
93}
94
95} // namespace
96
98bool hasDecoderFor(const Sample& sample)
99{
100 const GstCapsPtr caps = adoptCaps(gst_caps_new_empty_simple(sample.mediaType));
101 bool found = false;
102 forEachVideoDecoderFactory(GST_RANK_MARGINAL, [&caps, &found](GstElementFactory* factory) {
103 found = found || gst_element_factory_can_sink_any_caps(factory, caps.get());
104 });
105 return found;
106}
107
109DecodeResult decodeSample(const Sample& sample, const QByteArray& data, std::chrono::milliseconds timeout)
110{
111 DecoderWatch watch;
112 std::atomic<int> frames = 0;
113 const GstElementPtr pipeline = adoptFloating(gst_pipeline_new("selftest-decode"));
114 GstElementPtr sourceOwner = makeElement("appsrc");
115 GstElementPtr parserOwner = makeElement(sample.parser);
116 GstElementPtr decodebinOwner = makeElement("decodebin3");
117 GstElementPtr sinkOwner = makeElement("appsink");
118 DecodeResult result;
119 if (!pipeline || !sourceOwner || !parserOwner || !decodebinOwner || !sinkOwner) {
120 result.failure = QStringLiteral("cannot create appsrc, %1, decodebin3 or appsink").arg(sample.parser);
121 return result;
122 }
123
124 const GstCapsPtr inputCaps = byteStreamCaps(sample);
125 const GstCapsPtr rawCaps = adoptCaps(gst_caps_new_empty_simple("video/x-raw"));
126 g_object_set(sourceOwner.get(), "caps", inputCaps.get(), nullptr);
127 // Nothing pulls the samples: keep only the latest, and post the EOS without waiting for them to be taken.
128 g_object_set(sinkOwner.get(), "caps", rawCaps.get(), "sync", FALSE, "max-buffers", 1u, "drop", TRUE, "wait-on-eos",
129 FALSE, nullptr);
130
131 GstElement* const source = addToBin(pipeline.get(), std::move(sourceOwner), "appsrc");
132 GstElement* const parser = addToBin(pipeline.get(), std::move(parserOwner), sample.parser);
133 GstElement* const decodebin = addToBin(pipeline.get(), std::move(decodebinOwner), "decodebin3");
134 GstElement* const sink = addToBin(pipeline.get(), std::move(sinkOwner), "appsink");
135 if (!source || !parser || !decodebin || !sink || !gst_element_link_many(source, parser, decodebin, nullptr)) {
136 result.failure = QStringLiteral("cannot link appsrc ! %1 ! decodebin3").arg(sample.parser);
137 return result;
138 }
139
140 const GstPadPtr sinkPad = adoptPad(gst_element_get_static_pad(sink, "sink"));
141 (void) gst_pad_add_probe(sinkPad.get(), GST_PAD_PROBE_TYPE_BUFFER, countBuffer, &frames, nullptr);
142 (void) g_signal_connect(decodebin, "pad-added", G_CALLBACK(linkNewSrcPad), sinkPad.get());
143 (void) g_signal_connect(pipeline.get(), "deep-element-added", G_CALLBACK(watchDecoder), &watch);
144
145 result.failure = playToEos(pipeline.get(), source, data, timeout);
146 if (result.failure.isEmpty() && (frames.load() == 0)) {
147 result.failure = QStringLiteral("no decoded frame reached the sink");
148 }
149
150 const std::lock_guard lock(watch.mutex);
151 result.decoders = watch.decoders;
152 result.decoders.removeDuplicates();
153 result.hardware = watch.hardware;
154 return result;
155}
156
158QString recordSample(const QString& file, VideoReceiver::FileFormat format, const QByteArray& h264,
159 std::chrono::milliseconds timeout)
160{
161 const GstElementPtr pipeline = adoptFloating(gst_pipeline_new("selftest-record"));
162 const GstCapsPtr caps = byteStreamCaps(kH264Sample);
163 GstElementPtr sourceOwner = makeElement("appsrc");
164 GstElementPtr fileSinkOwner = RecordingBranch::makeFileSink(file, format, caps.get());
165 if (!pipeline || !sourceOwner || !fileSinkOwner) {
166 return QStringLiteral("cannot create appsrc or the recording sink");
167 }
168 g_object_set(sourceOwner.get(), "caps", caps.get(), nullptr);
169
170 GstElement* const source = addToBin(pipeline.get(), std::move(sourceOwner), "appsrc");
171 GstElement* const fileSink = addToBin(pipeline.get(), std::move(fileSinkOwner), "recording sink");
172 if (!source || !fileSink || !gst_element_link(source, fileSink)) {
173 return QStringLiteral("cannot link appsrc to the recording sink");
174 }
175 return playToEos(pipeline.get(), source, h264, timeout);
176}
177
179QString readBack(const QString& file, const char* demuxer, std::chrono::milliseconds timeout)
180{
181 std::atomic<int> buffers = 0;
182 const GstElementPtr pipeline = adoptFloating(gst_pipeline_new("selftest-read-back"));
183 GstElementPtr sourceOwner = makeElement("filesrc");
184 GstElementPtr demuxerOwner = makeElement(demuxer);
185 GstElementPtr sinkOwner = makeElement("fakesink");
186 if (!pipeline || !sourceOwner || !demuxerOwner || !sinkOwner) {
187 return QStringLiteral("cannot create filesrc, %1 or fakesink").arg(demuxer);
188 }
189 g_object_set(sourceOwner.get(), "location", glibFileName(file).constData(), nullptr);
190 g_object_set(sinkOwner.get(), "sync", FALSE, nullptr);
191
192 GstElement* const source = addToBin(pipeline.get(), std::move(sourceOwner), "filesrc");
193 GstElement* const demux = addToBin(pipeline.get(), std::move(demuxerOwner), demuxer);
194 GstElement* const sink = addToBin(pipeline.get(), std::move(sinkOwner), "fakesink");
195 if (!source || !demux || !sink || !gst_element_link(source, demux)) {
196 return QStringLiteral("cannot link filesrc ! %1").arg(demuxer);
197 }
198
199 const GstPadPtr sinkPad = adoptPad(gst_element_get_static_pad(sink, "sink"));
200 (void) gst_pad_add_probe(sinkPad.get(), GST_PAD_PROBE_TYPE_BUFFER, countBuffer, &buffers, nullptr);
201 (void) g_signal_connect(demux, "pad-added", G_CALLBACK(linkNewSrcPad), sinkPad.get());
202
203 QString failure = playToEos(pipeline.get(), nullptr, {}, timeout);
204 if (!failure.isEmpty()) {
205 return failure;
206 }
207 return (buffers.load() > 0) ? QString() : QStringLiteral("%1 found no video in it").arg(demuxer);
208}
209
210} // namespace GStreamer::SelfTest
struct _GstElement GstElement
static GStreamer::GstElementPtr makeFileSink(const QString &file, VideoReceiver::FileFormat format, const GstCaps *inputCaps)
FileFormat
Values are persisted by VideoSettings::recordingFormat.
The self-test's media pipelines: decoding the embedded samples and recording and reading back a file.
bool hasDecoderFor(const Sample &sample)
Whether decodebin3 has a decoder for sample's codec to plug: one ranked at least MARGINAL.
QString recordSample(const QString &file, VideoReceiver::FileFormat format, const QByteArray &h264, std::chrono::milliseconds timeout)
appsrc ! RecordingBranch's file sink (h264parse ! muxer ! filesink), as a recording is muxed.
DecodeResult decodeSample(const Sample &sample, const QByteArray &data, std::chrono::milliseconds timeout)
appsrc ! <parser> ! decodebin3 ! appsink, the sink taking raw video in system memory,...
QString readBack(const QString &file, const char *demuxer, std::chrono::milliseconds timeout)
filesrc ! <demuxer> ! fakesink: the recorded file opens and holds video.
std::unique_ptr< GstBus, GstObjectDeleter > GstBusPtr
Definition GstScoped.h:62
std::unique_ptr< GstCaps, GstCapsDeleter > GstCapsPtr
Definition GstScoped.h:66
GstBusPtr adoptBus(GstBus *bus) noexcept
Definition GstScoped.h:89
GstElementPtr makeElement(const char *factory, const char *name=nullptr)
Definition GstScoped.h:178
GstElement * addToBin(GstElement *bin, GstElementPtr element, const char *what)
std::unique_ptr< T, GstObjectDeleter > adoptFloating(T *object) noexcept
Sinks the floating ref of a new object (pipeline, bin, pad, element) and owns it. Null for null.
Definition GstScoped.h:171
GstCapsPtr adoptCaps(GstCaps *caps) noexcept
Definition GstScoped.h:109
std::unique_ptr< GstElement, GstObjectDeleter > GstElementPtr
Definition GstScoped.h:60
MessageError parseMessageError(GstMessage *message)
gst_message_parse_error(), or gst_message_parse_warning() for a WARNING message.
Definition GstScoped.h:152
std::unique_ptr< GstPad, GstObjectDeleter > GstPadPtr
Definition GstScoped.h:61
bool isHardwareDecoderFactory(GstElementFactory *factory)
QByteArray glibFileName(const QString &file)
std::unique_ptr< GstMessage, GstMessageDeleter > GstMessagePtr
Definition GstScoped.h:67
GstMessagePtr adoptMessage(GstMessage *message) noexcept
Definition GstScoped.h:114
void forEachVideoDecoderFactory(GstRank minRank, const std::function< void(GstElementFactory *)> &visitor)
GstPadPtr adoptPad(GstPad *pad) noexcept
Definition GstScoped.h:84
An encoded sample in the app's resources (Media/generate.sh makes them).