5#include <gst/app/gstappsrc.h>
13using namespace std::chrono_literals;
21 return adoptCaps(gst_caps_new_simple(sample.mediaType,
"stream-format", G_TYPE_STRING,
"byte-stream",
nullptr));
24QString errorText(GstMessage* message)
27 QString text = QStringLiteral(
"%1: %2").arg(QString::fromUtf8(GST_MESSAGE_SRC_NAME(message)),
28 error ? QString::fromUtf8(error->message) : QStringLiteral(
"error"));
30 text += QStringLiteral(
" (%1)").arg(QString::fromUtf8(debug.get()));
37QString playToEos(
GstElement* pipeline,
GstElement* source,
const QByteArray& data, std::chrono::milliseconds timeout)
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));
46 const GstClockTime wait =
47 started ?
static_cast<GstClockTime
>(std::chrono::nanoseconds(std::max(timeout, 0ms)).count()) : 0;
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);
52 if (message && (GST_MESSAGE_TYPE(message.get()) == GST_MESSAGE_EOS)) {
56 return errorText(message.get());
58 return started ? QStringLiteral(
"no end of stream within %1 ms").arg(timeout.count())
59 : QStringLiteral(
"the pipeline failed to start");
62void linkNewSrcPad([[maybe_unused]]
GstElement* element, GstPad* pad, gpointer sinkPad)
64 if (GST_PAD_IS_SRC(pad) && !gst_pad_is_linked(GST_PAD(sinkPad))) {
65 (void) gst_pad_link(pad, GST_PAD(sinkPad));
69GstPadProbeReturn countBuffer([[maybe_unused]] GstPad* pad, [[maybe_unused]] GstPadProbeInfo* info, gpointer count)
71 static_cast<std::atomic<int>*
>(count)->fetch_add(1);
72 return GST_PAD_PROBE_OK;
80 bool hardware =
false;
83void watchDecoder([[maybe_unused]] GstBin* bin, [[maybe_unused]] GstBin* subBin,
GstElement* element, gpointer data)
85 GstElementFactory*
const factory = gst_element_get_factory(element);
86 if (!factory || !gst_element_factory_list_is_type(factory, GST_ELEMENT_FACTORY_TYPE_DECODER)) {
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))));
103 found = found || gst_element_factory_can_sink_any_caps(factory, caps.get());
112 std::atomic<int> frames = 0;
119 if (!pipeline || !sourceOwner || !parserOwner || !decodebinOwner || !sinkOwner) {
120 result.
failure = QStringLiteral(
"cannot create appsrc, %1, decodebin3 or appsink").arg(sample.
parser);
124 const GstCapsPtr inputCaps = byteStreamCaps(sample);
126 g_object_set(sourceOwner.get(),
"caps", inputCaps.get(),
nullptr);
128 g_object_set(sinkOwner.get(),
"caps", rawCaps.get(),
"sync", FALSE,
"max-buffers", 1u,
"drop", TRUE,
"wait-on-eos",
131 GstElement*
const source =
addToBin(pipeline.get(), std::move(sourceOwner),
"appsrc");
133 GstElement*
const decodebin =
addToBin(pipeline.get(), std::move(decodebinOwner),
"decodebin3");
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);
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);
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");
150 const std::lock_guard lock(watch.mutex);
159 std::chrono::milliseconds timeout)
165 if (!pipeline || !sourceOwner || !fileSinkOwner) {
166 return QStringLiteral(
"cannot create appsrc or the recording sink");
168 g_object_set(sourceOwner.get(),
"caps", caps.get(),
nullptr);
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");
175 return playToEos(pipeline.get(), source, h264, timeout);
179QString
readBack(
const QString& file,
const char* demuxer, std::chrono::milliseconds timeout)
181 std::atomic<int> buffers = 0;
186 if (!pipeline || !sourceOwner || !demuxerOwner || !sinkOwner) {
187 return QStringLiteral(
"cannot create filesrc, %1 or fakesink").arg(demuxer);
189 g_object_set(sourceOwner.get(),
"location",
glibFileName(file).constData(),
nullptr);
190 g_object_set(sinkOwner.get(),
"sync", FALSE,
nullptr);
192 GstElement*
const source =
addToBin(pipeline.get(), std::move(sourceOwner),
"filesrc");
195 if (!source || !demux || !sink || !gst_element_link(source, demux)) {
196 return QStringLiteral(
"cannot link filesrc ! %1").arg(demuxer);
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());
203 QString failure = playToEos(pipeline.get(),
nullptr, {}, timeout);
204 if (!failure.isEmpty()) {
207 return (buffers.load() > 0) ? QString() : QStringLiteral(
"%1 found no video in it").arg(demuxer);
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.
constexpr Sample kH264Sample
std::unique_ptr< GstBus, GstObjectDeleter > GstBusPtr
std::unique_ptr< GstCaps, GstCapsDeleter > GstCapsPtr
GstBusPtr adoptBus(GstBus *bus) noexcept
GstElementPtr makeElement(const char *factory, const char *name=nullptr)
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.
GstCapsPtr adoptCaps(GstCaps *caps) noexcept
std::unique_ptr< GstElement, GstObjectDeleter > GstElementPtr
MessageError parseMessageError(GstMessage *message)
gst_message_parse_error(), or gst_message_parse_warning() for a WARNING message.
std::unique_ptr< GstPad, GstObjectDeleter > GstPadPtr
bool isHardwareDecoderFactory(GstElementFactory *factory)
QByteArray glibFileName(const QString &file)
std::unique_ptr< GstMessage, GstMessageDeleter > GstMessagePtr
GstMessagePtr adoptMessage(GstMessage *message) noexcept
void forEachVideoDecoderFactory(GstRank minRank, const std::function< void(GstElementFactory *)> &visitor)
GstPadPtr adoptPad(GstPad *pad) noexcept
An encoded sample in the app's resources (Media/generate.sh makes them).