summaryrefslogtreecommitdiffstats
path: root/src/multimedia/video/qvideoframe.cpp
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2021-08-30 15:44:53 +0200
committerLars Knoll <lars.knoll@qt.io>2021-09-07 09:15:11 +0200
commit2c0b2b11a5c3d2972bacf5d4d1de34ab16126f79 (patch)
tree8031c8d9c3bbe73a93cea82f9523ea896fdf09ab /src/multimedia/video/qvideoframe.cpp
parent1e97d96ab0647005dd655961874f640984d66134 (diff)
Implement platform independent subtitle rendering
Add support for rendering subtitles in a platform independent way using our own text rendering infrastructure. Implement support for subtitle rendering in QVideoFrame::paint(). Add a flag to disable subtitle the rendering if not desired. Support for Qt Quick VideoOuput is still missing, and will come in a follow-up change. Implement setting the subtitle text correctly on macOS/iOS. Other platforms will be done in follow-up changes. Pick-to: 6.2 Change-Id: If5c689d4919d7a8df23399184f6e724028b0e980 Reviewed-by: Samuel Mira <samuel.mira@qt.io> Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
Diffstat (limited to 'src/multimedia/video/qvideoframe.cpp')
-rw-r--r--src/multimedia/video/qvideoframe.cpp32
1 files changed, 30 insertions, 2 deletions
diff --git a/src/multimedia/video/qvideoframe.cpp b/src/multimedia/video/qvideoframe.cpp
index 400e122ab..71e16162c 100644
--- a/src/multimedia/video/qvideoframe.cpp
+++ b/src/multimedia/video/qvideoframe.cpp
@@ -44,6 +44,7 @@
#include "qvideoframeconversionhelper_p.h"
#include "qvideoframeformat.h"
#include "qpainter.h"
+#include <qtextlayout.h>
#include <qimage.h>
#include <qmutex.h>
@@ -114,7 +115,7 @@ public:
QAbstractVideoBuffer *buffer = nullptr;
int mappedCount = 0;
QMutex mapMutex;
- QVariantMap metadata;
+ QString subtitleText;
private:
Q_DISABLE_COPY(QVideoFramePrivate)
@@ -715,6 +716,22 @@ QImage QVideoFrame::toImage() const
}
/*!
+ Returns the subtitle text that should be rendered together with this video frame.
+*/
+QString QVideoFrame::subtitleText() const
+{
+ return d->subtitleText;
+}
+
+/*!
+ Sets the subtitle text that should be rendered together with this video frame to \a text.
+*/
+void QVideoFrame::setSubtitleText(const QString &text)
+{
+ d->subtitleText = text;
+}
+
+/*!
Uses a QPainter, \a{painter}, to render this QVideoFrame to \a rect.
The PaintOptions \a options can be used to specify a background color and
how \a rect should be filled with the video.
@@ -725,7 +742,7 @@ QImage QVideoFrame::toImage() const
void QVideoFrame::paint(QPainter *painter, const QRectF &rect, const PaintOptions &options)
{
if (!isValid()) {
- painter->fillRect(rect, painter->background());
+ painter->fillRect(rect, options.backgroundColor);
return;
}
@@ -793,6 +810,17 @@ void QVideoFrame::paint(QPainter *painter, const QRectF &rect, const PaintOption
} else {
painter->fillRect(rect, Qt::black);
}
+
+ if ((options.paintFlags & PaintOptions::DontDrawSubtitles) || d->subtitleText.isEmpty())
+ return;
+
+ // draw subtitles
+ auto text = d->subtitleText;
+ text.replace(QLatin1Char('\n'), QChar::LineSeparator);
+
+ QVideoTextureHelper::SubtitleLayout layout;
+ layout.updateFromVideoFrame(*this);
+ layout.draw(painter, targetRect);
}
#ifndef QT_NO_DEBUG_STREAM