9#include <QtGui/private/qopenglextensions_p.h>
10#include <qopenglcontext.h>
11#include <qopenglfunctions.h>
13#include <private/qhwvideobuffer_p.h>
14#include <private/qvideoframeconverter_p.h>
15#include <private/qplatformvideosink_p.h>
16#include <private/qvideoframe_p.h>
17#include <qvideosink.h>
18#include <qvideoframeformat.h>
29 m_tex.reset(rhi->newTexture(QRhiTexture::RGBA8, size, 1));
30 m_tex->createFrom({quint64(handle), 0});
35 return plane == 0 ? m_tex.get() :
nullptr;
39 std::unique_ptr<QRhiTexture> m_tex;
42class AndroidTextureVideoBuffer
final :
public QHwVideoBuffer
54 m_mapMode = QVideoFrame::NotMapped;
64 std::unique_ptr<QRhiTexture> m_tex;
66 QVideoFrame::MapMode m_mapMode = QVideoFrame::NotMapped;
69class ImageFromVideoFrameHelper
final :
public QHwVideoBuffer
76 return m_atvb.mapTextures(rhi);
83 AndroidTextureVideoBuffer &m_atvb;
88 QAbstractVideoBuffer::MapData mapData;
90 if (m_mapMode == QVideoFrame::NotMapped && mode == QVideoFrame::ReadOnly) {
91 m_mapMode = QVideoFrame::ReadOnly;
92 m_image = qImageFromVideoFrame(QVideoFramePrivate::createFrame(
93 std::make_unique<ImageFromVideoFrameHelper>(*
this),
94 QVideoFrameFormat(m_size, QVideoFrameFormat::Format_RGBA8888)));
95 mapData.planeCount = 1;
96 mapData.bytesPerLine[0] = m_image.bytesPerLine();
97 mapData.dataSize[0] =
static_cast<
int>(m_image.sizeInBytes());
98 mapData.data[0] = m_image.bits();
105 -1.f, -1.f, 0.f, 0.f,
113 static QShader getShader(
const QString &name)
116 if (f.open(QIODevice::ReadOnly))
117 return QShader::fromSerialized(f.readAll());
125 m_vertexBuffer.reset(m_rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer,
sizeof(g_quad)));
126 m_vertexBuffer->create();
128 m_uniformBuffer.reset(m_rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64 + 64 + 4 + 4));
129 m_uniformBuffer->create();
131 m_sampler.reset(m_rhi->newSampler(QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
132 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge));
135 m_srb.reset(m_rhi->newShaderResourceBindings());
137 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, m_uniformBuffer.get()),
138 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, externalTex, m_sampler.get())
142 m_vertexShader = getShader(QStringLiteral(
":/qt-project.org/multimedia/shaders/externalsampler.vert.qsb"));
143 Q_ASSERT(m_vertexShader.isValid());
144 m_fragmentShader = getShader(QStringLiteral(
":/qt-project.org/multimedia/shaders/externalsampler.frag.qsb"));
145 Q_ASSERT(m_fragmentShader.isValid());
153 QRhi *m_rhi =
nullptr;
154 std::unique_ptr<QRhiBuffer> m_vertexBuffer;
155 std::unique_ptr<QRhiBuffer> m_uniformBuffer;
156 std::unique_ptr<QRhiSampler> m_sampler;
157 std::unique_ptr<QRhiShaderResourceBindings> m_srb;
158 QShader m_vertexShader;
159 QShader m_fragmentShader;
163static std::unique_ptr<QRhiGraphicsPipeline> newGraphicsPipeline(QRhi *rhi,
164 QRhiShaderResourceBindings *shaderResourceBindings,
165 QRhiRenderPassDescriptor *renderPassDescriptor,
166 QShader vertexShader,
167 QShader fragmentShader)
169 std::unique_ptr<QRhiGraphicsPipeline> gp(rhi->newGraphicsPipeline());
170 gp->setTopology(QRhiGraphicsPipeline::TriangleFan);
171 gp->setShaderStages({
172 { QRhiShaderStage::Vertex, vertexShader },
173 { QRhiShaderStage::Fragment, fragmentShader }
175 QRhiVertexInputLayout inputLayout;
176 inputLayout.setBindings({
177 { 4 *
sizeof(
float) }
179 inputLayout.setAttributes({
180 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
181 { 0, 1, QRhiVertexInputAttribute::Float2, 2 *
sizeof(
float) }
183 gp->setVertexInputLayout(inputLayout);
184 gp->setShaderResourceBindings(shaderResourceBindings);
185 gp->setRenderPassDescriptor(renderPassDescriptor);
191std::unique_ptr<QRhiTexture> TextureCopy::copyExternalGlTexture(QSize size,
const QMatrix4x4 &externalTexMatrix)
193 std::unique_ptr<QRhiTexture> tex(m_rhi->newTexture(QRhiTexture::RGBA8, size, 1, QRhiTexture::RenderTarget));
194 if (!tex->create()) {
195 qWarning(
"Failed to create frame texture");
199 std::unique_ptr<QRhiTextureRenderTarget> renderTarget(m_rhi->newTextureRenderTarget({ { tex.get() } }));
200 std::unique_ptr<QRhiRenderPassDescriptor> renderPassDescriptor(renderTarget->newCompatibleRenderPassDescriptor());
201 renderTarget->setRenderPassDescriptor(renderPassDescriptor.get());
202 renderTarget->create();
204 QRhiResourceUpdateBatch *rub = m_rhi->nextResourceUpdateBatch();
205 rub->uploadStaticBuffer(m_vertexBuffer.get(), g_quad);
208 char *p = m_uniformBuffer->beginFullDynamicBufferUpdateForCurrentFrame();
209 memcpy(p, identity.constData(), 64);
210 memcpy(p + 64, externalTexMatrix.constData(), 64);
211 float opacity = 1.0f;
212 memcpy(p + 64 + 64, &opacity, 4);
213 m_uniformBuffer->endFullDynamicBufferUpdateForCurrentFrame();
215 auto graphicsPipeline = newGraphicsPipeline(m_rhi, m_srb.get(), renderPassDescriptor.get(),
216 m_vertexShader, m_fragmentShader);
218 const QRhiCommandBuffer::VertexInput vbufBinding(m_vertexBuffer.get(), 0);
220 QRhiCommandBuffer *cb =
nullptr;
221 if (m_rhi->beginOffscreenFrame(&cb) != QRhi::FrameOpSuccess)
224 cb->beginPass(renderTarget.get(), Qt::transparent, { 1.0f, 0 }, rub);
225 cb->setGraphicsPipeline(graphicsPipeline.get());
226 cb->setViewport({0, 0,
float(size.width()),
float(size.height())});
227 cb->setShaderResources(m_srb.get());
228 cb->setVertexInput(0, 1, &vbufBinding);
231 m_rhi->endOffscreenFrame();
233 QOpenGLContext *ctx = QOpenGLContext::currentContext();
234 QOpenGLFunctions *f = ctx->functions();
235 static_cast<QOpenGLExtensions *>(f)->flushShared();
240static QMatrix4x4 extTransformMatrix(AndroidSurfaceTexture *surfaceTexture)
242 QMatrix4x4 m = surfaceTexture->getTransformMatrix();
246 static const QMatrix4x4 flipV(1.0f, 0.0f, 0.0f, 0.0f,
247 0.0f, -1.0f, 0.0f, 1.0f,
248 0.0f, 0.0f, 1.0f, 0.0f,
249 0.0f, 0.0f, 0.0f, 1.0f);
347 <<
"Attempting to use Qt Multimedia VideoOutput on a Qt build without OpenGL support. "
348 <<
"This is not supported by the native Android media backend.";
359 std::shared_ptr<QRhi> m_rhi;
360 std::unique_ptr<AndroidSurfaceTexture> m_surfaceTexture;
361 std::unique_ptr<QRhiTexture> m_texture;
362 std::unique_ptr<TextureCopy> m_textureCopy;
371 qDebug() <<
"Cannot create QAndroidTextureVideoOutput without a sink.";
372 m_surfaceThread =
nullptr;
376 startNewSurfaceThread();
381 m_surfaceThread = std::make_shared<AndroidTextureThread>(
this);
382 connect(m_surfaceThread.get(), &AndroidTextureThread::newFrame,
383 this, &QAndroidTextureVideoOutput::newFrame);
384 m_surfaceThread->start();
390 QMetaObject::invokeMethod(m_surfaceThread.get(),
391 &AndroidTextureThread::clearSurfaceTexture, Qt::BlockingQueuedConnection);
397 auto *sink = m_sink->platformVideoSink();
399 sink->setSubtitleText(subtitle);
405 return m_sink->rhi() && m_surfaceCreatedWithoutRhi;
414 QMetaObject::invokeMethod(m_surfaceThread.get(), [&]() {
415 auto rhi = m_sink->rhi();
417 m_surfaceCreatedWithoutRhi =
true;
419 else if (m_surfaceCreatedWithoutRhi) {
420 m_surfaceThread->clearSurfaceTexture();
421 m_surfaceCreatedWithoutRhi =
false;
423 surface = m_surfaceThread->createSurfaceTexture(rhi);
425 Qt::BlockingQueuedConnection);
431 if (m_nativeSize == size)
435 QMetaObject::invokeMethod(m_surfaceThread.get(),
436 [&](){ m_surfaceThread->setFrameSize(size); },
437 Qt::BlockingQueuedConnection);
443 QMetaObject::invokeMethod(m_surfaceThread.get(), [&](){ m_surfaceThread->clearFrame(); });
449 m_sink->platformVideoSink()->setVideoFrame({});
450 QMetaObject::invokeMethod(m_surfaceThread.get(), &AndroidTextureThread::clearSurfaceTexture);
456 m_sink->setVideoFrame(frame);
461#include "qandroidvideooutput.moc"
462#include "moc_qandroidvideooutput_p.cpp"
MapData map(QVideoFrame::MapMode mode) override
Maps the planes of a video buffer to memory.
void unmap() override
Releases the memory mapped by the map() function.
AndroidTextureVideoBuffer(std::unique_ptr< QRhiTexture > tex, const QSize &size)
QVideoFrameTexturesUPtr mapTextures(QRhi &rhi) override
void unmap() override
Releases the memory mapped by the map() function.
QVideoFrameTexturesUPtr mapTextures(QRhi &rhi) override
ImageFromVideoFrameHelper(AndroidTextureVideoBuffer &atvb)
MapData map(QVideoFrame::MapMode) override
Maps the planes of a video buffer to memory.
void setSubtitle(const QString &subtitle)
void setVideoSize(const QSize &) override
AndroidSurfaceTexture * surfaceTexture() override
bool shouldTextureBeUpdated() const
~QAndroidTextureVideoOutput() override
QAndroidVideoFrameTextures(QRhi *rhi, QSize size, quint64 handle)
QRhiTexture * texture(uint plane) const override
TextureCopy(QRhi *rhi, QRhiTexture *externalTex)
Combined button and popup list for selecting options.
static const float g_quad[]