1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
/*
* Copyright (C) 2025 Igalia S.L
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* aint with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "GStreamerVideoFrameConverter.h"
#if ENABLE(VIDEO) && USE(GSTREAMER)
#include "GStreamerCommon.h"
#include <gst/allocators/gstdmabuf.h>
#include <gst/app/gstappsink.h>
#include <gst/app/gstappsrc.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/Scope.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/glib/RunLoopSourcePriority.h>
#if USE(GSTREAMER_GL)
#include <gst/gl/gl.h>
#endif
namespace WebCore {
WTF_MAKE_TZONE_ALLOCATED_IMPL(GStreamerVideoFrameConverter);
WTF_MAKE_TZONE_ALLOCATED_IMPL(GStreamerVideoFrameConverter::Pipeline);
GST_DEBUG_CATEGORY_STATIC(webkit_gst_video_frame_converter_debug);
#define GST_CAT_DEFAULT webkit_gst_video_frame_converter_debug
static const Seconds s_releaseUnusedPipelinesTimerInterval = 30_s;
GStreamerVideoFrameConverter::Pipeline::Pipeline(Type type)
: m_type(type)
, m_src(makeGStreamerElement("appsrc", nullptr))
, m_sink(makeGStreamerElement("appsink", nullptr))
{
g_object_set(m_sink.get(), "enable-last-sample", FALSE, "max-buffers", 1, nullptr);
switch (m_type) {
case Type::SystemMemory: {
auto videoconvert = makeGStreamerElement("videoconvert", nullptr);
auto videoscale = makeGStreamerElement("videoscale", nullptr);
m_pipeline = gst_element_factory_make("pipeline", "video-frame-converter");
gst_bin_add_many(GST_BIN_CAST(m_pipeline.get()), m_src.get(), videoconvert, videoscale, m_sink.get(), nullptr);
gst_element_link_many(m_src.get(), videoconvert, videoscale, m_sink.get(), nullptr);
break;
}
#if USE(GSTREAMER_GL)
case Type::GLMemory: {
auto glcolorconvert = makeGStreamerElement("glcolorconvert", nullptr);
auto gldownload = makeGStreamerElement("gldownload", nullptr);
auto videoscale = makeGStreamerElement("videoscale", nullptr);
m_pipeline = gst_element_factory_make("pipeline", "video-frame-converter-gl");
gst_bin_add_many(GST_BIN_CAST(m_pipeline.get()), m_src.get(), glcolorconvert, gldownload, videoscale, m_sink.get(), nullptr);
gst_element_link_many(m_src.get(), glcolorconvert, gldownload, videoscale, m_sink.get(), nullptr);
break;
}
case Type::DMABufMemory: {
auto glupload = makeGStreamerElement("glupload", nullptr);
m_capsfilter = makeGStreamerElement("capsfilter", nullptr);
auto glcolorconvert = makeGStreamerElement("glcolorconvert", nullptr);
auto gldownload = makeGStreamerElement("gldownload", nullptr);
auto videoscale = makeGStreamerElement("videoscale", nullptr);
m_pipeline = gst_element_factory_make("pipeline", "video-frame-converter-gl");
gst_bin_add_many(GST_BIN_CAST(m_pipeline.get()), m_src.get(), glupload, m_capsfilter.get(), glcolorconvert, gldownload, videoscale, m_sink.get(), nullptr);
gst_element_link_many(m_src.get(), glupload, m_capsfilter.get(), glcolorconvert, gldownload, videoscale, m_sink.get(), nullptr);
}
#endif
}
}
GStreamerVideoFrameConverter::Pipeline::~Pipeline() = default;
GRefPtr<GstSample> GStreamerVideoFrameConverter::Pipeline::run(const GRefPtr<GstSample>& sample, GstCaps* destinationCaps)
{
#if USE(GSTREAMER_GL)
if (m_type == Type::GLMemory || m_type == Type::DMABufMemory) {
if (!setGstElementGLContext(m_pipeline.get(), GST_GL_DISPLAY_CONTEXT_TYPE))
return nullptr;
if (!setGstElementGLContext(m_pipeline.get(), "gst.gl.app_context"))
return nullptr;
if (m_type == Type::DMABufMemory) {
GRefPtr<GstCaps> outputCaps = adoptGRef(gst_caps_copy(destinationCaps));
gst_caps_set_features(outputCaps.get(), 0, gst_caps_features_new(GST_CAPS_FEATURE_MEMORY_GL_MEMORY, nullptr));
gst_caps_set_simple(outputCaps.get(), "format", G_TYPE_STRING, "RGBA", nullptr);
g_object_set(m_capsfilter.get(), "caps", outputCaps.get(), nullptr);
}
}
#endif
unsigned capsSize = gst_caps_get_size(destinationCaps);
auto newCaps = adoptGRef(gst_caps_new_empty());
for (unsigned i = 0; i < capsSize; i++) {
auto structure = gst_caps_get_structure(destinationCaps, i);
auto modifiedStructure = gst_structure_copy(structure);
gst_structure_remove_field(modifiedStructure, "framerate");
gst_caps_append_structure(newCaps.get(), modifiedStructure);
}
GST_TRACE_OBJECT(m_pipeline.get(), "Converting sample with caps %" GST_PTR_FORMAT " to %" GST_PTR_FORMAT, gst_sample_get_caps(sample.get()), newCaps.get());
g_object_set(m_sink.get(), "caps", newCaps.get(), nullptr);
auto scopeExit = WTF::makeScopeExit([&] {
gst_element_set_state(m_pipeline.get(), GST_STATE_NULL);
});
gst_element_set_state(m_pipeline.get(), GST_STATE_PAUSED);
gst_app_src_push_sample(GST_APP_SRC_CAST(m_src.get()), sample.get());
auto bus = adoptGRef(gst_element_get_bus(m_pipeline.get()));
auto message = adoptGRef(gst_bus_timed_pop_filtered(bus.get(), 200 * GST_MSECOND, static_cast<GstMessageType>(GST_MESSAGE_ERROR | GST_MESSAGE_ASYNC_DONE)));
if (!message) {
GST_ERROR_OBJECT(m_pipeline.get(), "Video frame conversion 200ms timeout expired.");
return nullptr;
}
if (GST_MESSAGE_TYPE(message.get()) == GST_MESSAGE_ERROR) {
GST_ERROR_OBJECT(m_pipeline.get(), "Unable to convert video frame. Error: %" GST_PTR_FORMAT, message.get());
return nullptr;
}
return adoptGRef(gst_app_sink_pull_preroll(GST_APP_SINK_CAST(m_sink.get())));
}
GStreamerVideoFrameConverter& GStreamerVideoFrameConverter::singleton()
{
static NeverDestroyed<GStreamerVideoFrameConverter> sharedInstance;
return sharedInstance;
}
GStreamerVideoFrameConverter::GStreamerVideoFrameConverter()
{
ensureGStreamerInitialized();
GST_DEBUG_CATEGORY_INIT(webkit_gst_video_frame_converter_debug, "webkitvideoframeconverter", 0, "WebKit GStreamer Video Frame Converter");
}
GStreamerVideoFrameConverter::Pipeline& GStreamerVideoFrameConverter::ensurePipeline(GstCaps* caps)
{
#if USE(GSTREAMER_GL)
auto* features = gst_caps_get_features(caps, 0);
if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF)) {
if (!m_dmabufMemoryPipeline) {
m_dmabufMemoryPipeline = makeUnique<Pipeline>(Pipeline::Type::DMABufMemory);
m_releaseUnusedDMABufMemoryPipelineTimer = makeUnique<RunLoop::Timer>(RunLoop::current(), this, &GStreamerVideoFrameConverter::releaseUnusedDMABufMemoryPipelineTimerFired);
m_releaseUnusedDMABufMemoryPipelineTimer->setPriority(RunLoopSourcePriority::ReleaseUnusedResourcesTimer);
}
m_releaseUnusedDMABufMemoryPipelineTimer->startOneShot(s_releaseUnusedPipelinesTimerInterval);
return *m_dmabufMemoryPipeline;
}
if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_GL_MEMORY)) {
if (!m_glMemoryPipeline) {
m_glMemoryPipeline = makeUnique<Pipeline>(Pipeline::Type::GLMemory);
m_releaseUnusedGLMemoryPipelineTimer = makeUnique<RunLoop::Timer>(RunLoop::current(), this, &GStreamerVideoFrameConverter::releaseUnusedGLMemoryPipelineTimerFired);
m_releaseUnusedGLMemoryPipelineTimer->setPriority(RunLoopSourcePriority::ReleaseUnusedResourcesTimer);
}
m_releaseUnusedGLMemoryPipelineTimer->startOneShot(s_releaseUnusedPipelinesTimerInterval);
return *m_glMemoryPipeline;
}
#else
UNUSED_PARAM(caps);
#endif
if (!m_systemMemoryPipeline) {
m_systemMemoryPipeline = makeUnique<Pipeline>(Pipeline::Type::SystemMemory);
m_releaseUnusedSystemMemoryPipelineTimer = makeUnique<RunLoop::Timer>(RunLoop::current(), this, &GStreamerVideoFrameConverter::releaseUnusedSystemMemoryPipelineTimerFired);
m_releaseUnusedSystemMemoryPipelineTimer->setPriority(RunLoopSourcePriority::ReleaseUnusedResourcesTimer);
}
m_releaseUnusedSystemMemoryPipelineTimer->startOneShot(s_releaseUnusedPipelinesTimerInterval);
return *m_systemMemoryPipeline;
}
GRefPtr<GstSample> GStreamerVideoFrameConverter::convert(const GRefPtr<GstSample>& sample, const GRefPtr<GstCaps>& destinationCaps)
{
auto inputCaps = gst_sample_get_caps(sample.get());
if (gst_caps_is_equal(inputCaps, destinationCaps.get()))
return GRefPtr(sample);
auto outputSample = ensurePipeline(inputCaps).run(sample, destinationCaps.get());
if (!outputSample)
return nullptr;
auto convertedSample = adoptGRef(gst_sample_make_writable(outputSample.leakRef()));
gst_sample_set_caps(convertedSample.get(), destinationCaps.get());
GRefPtr buffer = gst_sample_get_buffer(convertedSample.get());
auto writableBuffer = adoptGRef(gst_buffer_make_writable(buffer.leakRef()));
if (auto meta = gst_buffer_get_video_meta(writableBuffer.get()))
gst_buffer_remove_meta(writableBuffer.get(), GST_META_CAST(meta));
if (auto meta = gst_buffer_get_parent_buffer_meta(writableBuffer.get()))
gst_buffer_remove_meta(writableBuffer.get(), GST_META_CAST(meta));
auto structure = gst_caps_get_structure(destinationCaps.get(), 0);
auto width = gstStructureGet<int>(structure, "width"_s);
auto height = gstStructureGet<int>(structure, "height"_s);
auto formatStringView = gstStructureGetString(structure, "format"_s);
if (width && height && !formatStringView.isEmpty()) {
auto format = gst_video_format_from_string(formatStringView.toStringWithoutCopying().ascii().data());
gst_buffer_add_video_meta(writableBuffer.get(), GST_VIDEO_FRAME_FLAG_NONE, format, *width, *height);
}
gst_sample_set_buffer(convertedSample.get(), writableBuffer.get());
return convertedSample;
}
void GStreamerVideoFrameConverter::releaseUnusedSystemMemoryPipelineTimerFired()
{
m_systemMemoryPipeline = nullptr;
m_releaseUnusedSystemMemoryPipelineTimer = nullptr;
}
#if USE(GSTREAMER_GL)
void GStreamerVideoFrameConverter::releaseUnusedGLMemoryPipelineTimerFired()
{
m_glMemoryPipeline = nullptr;
m_releaseUnusedGLMemoryPipelineTimer = nullptr;
}
void GStreamerVideoFrameConverter::releaseUnusedDMABufMemoryPipelineTimerFired()
{
m_dmabufMemoryPipeline = nullptr;
m_releaseUnusedDMABufMemoryPipelineTimer = nullptr;
}
#endif
#undef GST_CAT_DEFAULT
} // namespace WebCore
#endif // ENABLE(VIDEO) && USE(GSTREAMER)
|