From 712efcb4f70352665209198e32abd3ee72e7dffc Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Fri, 3 Sep 2021 18:25:03 +0200 Subject: Use the new subtitle infrastructure on Android Move the subtitle support on Android over to use the new platform independent infrastructure. Pick-to: 6.2 Change-Id: I51fc9a72a3241fbbd2d39c859f46527306e76363 Reviewed-by: Samuel Mira Reviewed-by: Assam Boudjelthia --- .../android/common/qandroidvideooutput.cpp | 107 +-------------------- .../android/common/qandroidvideooutput_p.h | 17 ---- 2 files changed, 3 insertions(+), 121 deletions(-) (limited to 'src/multimedia') diff --git a/src/multimedia/platform/android/common/qandroidvideooutput.cpp b/src/multimedia/platform/android/common/qandroidvideooutput.cpp index e71d160f3..66fc65150 100644 --- a/src/multimedia/platform/android/common/qandroidvideooutput.cpp +++ b/src/multimedia/platform/android/common/qandroidvideooutput.cpp @@ -93,34 +93,12 @@ bool AndroidTextureVideoBuffer::updateReadbackFrame() return (m_textureUpdated = m_output->renderAndReadbackFrame()); } -void AndroidTextureVideoBuffer::mapSubtitle() -{ - if (m_output->m_subtitleText.isEmpty()) - return; - - QReadLocker locker(&m_output->m_subtitleLock); - const QPixmap &map = m_output->m_subtitlePixmap; - - // horizontally center the subtitle - // put it vertically in lower part of the video but not stuck to the bottom - int bottomMargin = m_size.height() / 12; - QPoint subtitleStartingPoint(m_size.width() / 2 - map.size().width() / 2, - m_size.height() - map.size().height() - bottomMargin); - - // set compositionmode SourceOver and SmoothPixmapTransform to false to make QPainter as - // performant as possible - QPainter painter(&m_image); - painter.setCompositionMode(QPainter::CompositionMode_SourceOver); - painter.drawPixmap(subtitleStartingPoint, map); -} - QAbstractVideoBuffer::MapData AndroidTextureVideoBuffer::map(QVideoFrame::MapMode mode) { MapData mapData; if (m_mapMode == QVideoFrame::NotMapped && mode == QVideoFrame::ReadOnly && updateReadbackFrame()) { m_mapMode = mode; m_image = m_output->m_readbackImage; - mapSubtitle(); mapData.nPlanes = 1; mapData.bytesPerLine[0] = m_image.bytesPerLine(); mapData.size[0] = static_cast(m_image.sizeInBytes()); @@ -181,28 +159,10 @@ QAndroidTextureVideoOutput::~QAndroidTextureVideoOutput() void QAndroidTextureVideoOutput::setSubtitle(const QString &subtitle) { - if (m_subtitleText == subtitle) - return; - - m_subtitleText = subtitle; - - if (m_subtitleText.isEmpty()) { - // prevent starting a new thread just to reset image - onSubtitleAvailable(QPixmap()); + if (!m_sink) return; - } - - // start thread to create a new subtitle image - QSubtitleWorkerThread *worker = new QSubtitleWorkerThread(m_subtitleText, m_nativeSize); - connect(worker, &QSubtitleWorkerThread::subtitleAvaliable, this, &QAndroidTextureVideoOutput::onSubtitleAvailable); - connect(worker, &QSubtitleWorkerThread::finished, worker, &QObject::deleteLater); - worker->start(); -} - -void QAndroidTextureVideoOutput::onSubtitleAvailable(QPixmap pixmap) -{ - QWriteLocker locker(&m_subtitleLock); - m_subtitlePixmap = QPixmap(pixmap); + auto *sink = m_sink->platformVideoSink(); + sink->setSubtitleText(subtitle); } QVideoSink *QAndroidTextureVideoOutput::surface() const @@ -502,65 +462,4 @@ void QAndroidTextureVideoOutput::ensureExternalTexture(QRhi *rhi) } } -QSubtitleWorkerThread::QSubtitleWorkerThread(const QString &text, const QSize &videoSize) - : m_text(text), m_videoSize(videoSize) -{ -} - -void QSubtitleWorkerThread::run() -{ - QFont font({ QStringLiteral("Sans-Serif") }); - // 0.07 - based on this https://www.md-subs.com/saa-subtitle-font-size - qreal fontSize = m_videoSize.height() * 0.07; - font.setPointSize(fontSize); - font.setBold(true); - - QPen strokePen(Qt::black); - strokePen.setWidth(3); - strokePen.setJoinStyle(Qt::PenJoinStyle::RoundJoin); - strokePen.setCapStyle(Qt::PenCapStyle::RoundCap); - - QTextLayout textLayout(m_text); - textLayout.setFont(font); - - QFontMetrics metrics(font); - QRect maxSubtitleRect = { 0, 0, m_videoSize.width(), m_videoSize.height() / 4 }; - QRect textBoundingbox = metrics.boundingRect(maxSubtitleRect, 0, m_text); - - QTextLayout::FormatRange range; - range.start = 0; - range.length = m_text.length(); - range.format.setTextOutline(strokePen); - range.format.setFont(font); - range.format.setForeground(QBrush(Qt::white)); - textLayout.setFormats({ range }); - - int leading = metrics.leading(); - qreal height = 0; - textLayout.beginLayout(); - while (1) { - QTextLine line = textLayout.createLine(); - if (!line.isValid()) - break; - - line.setLineWidth(textBoundingbox.width()); - height += leading; - line.setPosition(QPointF(0, height)); - height += line.height(); - } - textLayout.endLayout(); - - // match the height of box with the height of textLayout - textBoundingbox.setHeight(height); - - QPixmap pixmap(textBoundingbox.size()); - pixmap.fill(Qt::transparent); - QPainter painter(&pixmap); - painter.setRenderHint(QPainter::Antialiasing, true); - - textLayout.draw(&painter, QPoint(0, 0)); - - emit subtitleAvaliable(pixmap); -} - QT_END_NAMESPACE diff --git a/src/multimedia/platform/android/common/qandroidvideooutput_p.h b/src/multimedia/platform/android/common/qandroidvideooutput_p.h index edeb99026..fc85d74fc 100644 --- a/src/multimedia/platform/android/common/qandroidvideooutput_p.h +++ b/src/multimedia/platform/android/common/qandroidvideooutput_p.h @@ -123,7 +123,6 @@ public: void setSubtitle(const QString &subtitle); private Q_SLOTS: void onFrameAvailable(); - void onSubtitleAvailable(QPixmap pixmap); private: void initSurfaceTexture(); @@ -197,7 +196,6 @@ public: private: bool updateReadbackFrame(); - void mapSubtitle(); QVideoFrame::MapMode m_mapMode = QVideoFrame::NotMapped; QAndroidTextureVideoOutput *m_output = nullptr; @@ -207,21 +205,6 @@ private: bool m_textureUpdated = false; }; -class QSubtitleWorkerThread : public QThread -{ - Q_OBJECT -public: - QSubtitleWorkerThread(const QString &text, const QSize &videoSize); - void run() override; - -signals: - void subtitleAvaliable(QPixmap subtitle); - -private: - QString m_text; - QSize m_videoSize; -}; - QT_END_NAMESPACE Q_DECLARE_METATYPE(QList) -- cgit v1.2.3