Skip to content

Commit 7534540

Browse files
committed
Remove use of QtMultimedia private API on Linux
Signed-off-by: Maxime Gervais <gervais.maxime@gmail.com>
1 parent 0e570ab commit 7534540

File tree

8 files changed

+122
-10
lines changed

8 files changed

+122
-10
lines changed

Project/QtCreator/qctools-gui/qctools-gui.pro

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ HEADERS += \
7878
$$SOURCES_PATH/GUI/playercontrol.h \
7979
$$SOURCES_PATH/GUI/panelsview.h \
8080
$$SOURCES_PATH/GUI/plotschooser.h \
81-
$$SOURCES_PATH/GUI/yminmaxselector.h
81+
$$SOURCES_PATH/GUI/yminmaxselector.h \
82+
$$SOURCES_PATH/GUI/utils.h
8283

8384
SOURCES += \
8485
$$SOURCES_PATH/GUI/FilesList.cpp \
@@ -112,7 +113,8 @@ SOURCES += \
112113
$$SOURCES_PATH/GUI/playercontrol.cpp \
113114
$$SOURCES_PATH/GUI/panelsview.cpp \
114115
$$SOURCES_PATH/GUI/plotschooser.cpp \
115-
$$SOURCES_PATH/GUI/yminmaxselector.cpp
116+
$$SOURCES_PATH/GUI/yminmaxselector.cpp \
117+
$$SOURCES_PATH/GUI/utils.cpp
116118

117119
include(../zlib.pri)
118120

@@ -213,7 +215,8 @@ include(../qwt.pri)
213215

214216
CONFIG -= no_keywords
215217

216-
DEFINES += QT_AVPLAYER_MULTIMEDIA
218+
!linux: DEFINES += QT_AVPLAYER_MULTIMEDIA
219+
217220
INCLUDEPATH += ../qctools-QtAVPlayer/src
218221
include(../qctools-QtAVPlayer/src/QtAVPlayer/QtAVPlayer.pri)
219222

Source/GUI/Plots.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
#include "GUI/CommentsEditor.h"
1515
#include "GUI/barchartconditioneditor.h"
1616
#include "GUI/barchartconditioninput.h"
17+
#include "GUI/utils.h"
1718
#include "Core/Core.h"
1819
#include <QtAVPlayer/qavplayer.h>
1920
#include "playercontrol.h"
2021
#include "yminmaxselector.h"
2122
#include <QComboBox>
2223
#include <QGridLayout>
24+
#include <QVideoFrame>
2325
#include <QEvent>
2426
#include <qwt_plot_layout.h>
2527
#include <qwt_plot_canvas.h>
@@ -31,13 +33,10 @@
3133
#include <QPushButton>
3234
#include <QCheckBox>
3335
#include <QToolButton>
34-
#include <QVideoFrame>
3536
#include <qwt_plot_curve.h>
3637
#include <QMessageBox>
3738
#include <QSettings>
3839

39-
//---------------------------------------------------------------------------
40-
4140
class XAxisFormatBox: public QComboBox
4241
{
4342
public:
@@ -460,7 +459,7 @@ Plots::Plots( QWidget *parent, FileInformation* fileInformation ) :
460459
return panelsCount;
461460
}, [&, panelOutputIndex](int index) -> QImage {
462461
auto frame = m_fileInfoData->getPanelFrame(panelOutputIndex, index);
463-
QVideoFrame videoFrame = frame;
462+
QVideoFrame videoFrame = QAVV_QV(frame);
464463

465464
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
466465
auto panelImage = videoFrame.image();
@@ -482,7 +481,7 @@ Plots::Plots( QWidget *parent, FileInformation* fileInformation ) :
482481
}, [&, panelOutputIndex](int index) -> QImage {
483482
auto frame = m_fileInfoData->getPanelFrame(panelOutputIndex, index);
484483

485-
QVideoFrame videoFrame = frame;
484+
QVideoFrame videoFrame = QAVV_QV(frame);
486485

487486
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
488487
auto panelImage = videoFrame.image();

Source/GUI/TinyDisplay.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "GUI/TinyDisplay.h"
88
#include "GUI/player.h"
9+
#include "GUI/utils.h"
910

1011
#include "Core/FileInformation.h"
1112
#include "Core/CommonStats.h"
@@ -135,7 +136,7 @@ QPixmap toPixmap(const QByteArray& bytes)
135136

136137
QPixmap toPixmap(QAVVideoFrame frame) {
137138
#if QT_VERSION > QT_VERSION_CHECK(6, 0, 0)
138-
QVideoFrame vf = frame;
139+
QVideoFrame vf = QAVV_QV(frame);
139140
QImage img = vf.toImage();
140141
#elif QT_VERSION > QT_VERSION_CHECK(5, 13, 0)
141142
QVideoFrame vf = frame;

Source/GUI/mainwindow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <QPushButton>
1616
#include <QJsonDocument>
1717
#include <QPointer>
18+
#include <QVideoFrame>
1819

1920
#include <vector>
2021

Source/GUI/player.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "GUI/filterselector.h"
66
#include "GUI/Comments.h"
77
#include "GUI/Plots.h"
8+
#include "GUI/utils.h"
89
#include <QDir>
910
#include <QAction>
1011
#include <QStandardPaths>
@@ -39,7 +40,9 @@ Player::Player(QWidget *parent) :
3940
ui->commentsPlaceHolderFrame->setLayout(new QHBoxLayout);
4041
ui->commentsPlaceHolderFrame->layout()->setContentsMargins(0, 0, 0, 0);
4142

43+
#ifdef QT_AVPLAYER_MULTIMEDIA
4244
m_audioOutput.reset(new QAVAudioOutput);
45+
#endif // QT_AVPLAYER_MULTIMEDIA
4346

4447
QGraphicsScene* scene = new QGraphicsScene(ui->graphicsView);
4548
ui->graphicsView->setScene(scene);
@@ -50,14 +53,49 @@ Player::Player(QWidget *parent) :
5053

5154
m_player = new MediaPlayer();
5255

56+
#ifdef QT_AVPLAYER_MULTIMEDIA
5357
QObject::connect(m_player, &QAVPlayer::audioFrame, m_player, [this](const QAVAudioFrame &frame) {
5458
if(!ui->playerSlider->isSliderDown() && !m_mute)
5559
m_audioOutput->play(frame);
5660
}, Qt::DirectConnection);
61+
#else
62+
QObject::connect(m_player, &QAVPlayer::audioFrame, m_player, [this](const QAVAudioFrame &frame) {
63+
if(!ui->playerSlider->isSliderDown() && !m_mute)
64+
{
65+
QAudioFormat format;
66+
format.setSampleRate(frame.format().sampleRate());
67+
format.setChannelCount(frame.format().channelCount());
68+
switch (frame.format().sampleFormat())
69+
{
70+
case QAVAudioFormat::UInt8:
71+
format.setSampleFormat(QAudioFormat::UInt8); break;
72+
case QAVAudioFormat::Int16:
73+
format.setSampleFormat(QAudioFormat::Int16); break;
74+
case QAVAudioFormat::Int32:
75+
format.setSampleFormat(QAudioFormat::Int32); break;
76+
case QAVAudioFormat::Float:
77+
format.setSampleFormat(QAudioFormat::Float); break;
78+
default:
79+
format.setSampleFormat(QAudioFormat::Unknown); break;
80+
}
5781

58-
QObject::connect(m_player, &QAVPlayer::videoFrame, m_player, [this](const QAVVideoFrame &frame) {
82+
if (m_audioOutput.isNull() || m_audioOutput->state() == QAudio::StoppedState || m_audioOutput->format() != format)
83+
{
84+
m_audioOutput.reset(new QAudioSink(format));
85+
m_audioDevice = m_audioOutput->start();
86+
}
87+
88+
m_audioDevice->write(frame.data());
89+
}
90+
}, Qt::DirectConnection);
91+
#endif // QT_AVPLAYER_MULTIMEDIA
5992

93+
QObject::connect(m_player, &QAVPlayer::videoFrame, m_player, [this](const QAVVideoFrame &frame) {
94+
#ifdef QT_AVPLAYER_MULTIMEDIA
6095
videoFrame = frame.convertTo(AV_PIX_FMT_RGB32);
96+
#else
97+
videoFrame = QAVV_QV(frame);
98+
#endif //QT_AVPLAYER_MULTIMEDIA
6199

62100
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
63101
auto surface = m_w->videoSurface();

Source/GUI/player.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
#include <QWidget>
88
#include <QUrl>
99
#include <QVideoWidget>
10+
#include <QVideoFrame>
11+
#ifdef QT_AVPLAYER_MULTIMEDIA
1012
#include <QtAVPlayer/qavaudiooutput.h>
13+
#else
14+
#include <QAudioSink>
15+
#endif // QT_AVPLAYER_MULTIMEDIA
1116
#include <QtAVPlayer/qavplayer.h>
1217
#include "Core/FileInformation.h"
1318

@@ -265,7 +270,12 @@ private Q_SLOTS:
265270
MediaPlayer* m_player;
266271
bool m_mute { false };
267272

273+
#ifdef QT_AVPLAYER_MULTIMEDIA
268274
QScopedPointer<QAVAudioOutput> m_audioOutput;
275+
#else
276+
QScopedPointer<QAudioSink> m_audioOutput;
277+
QIODevice* m_audioDevice;
278+
#endif // QT_AVPLAYER_MULTIMEDIA
269279
QVideoFrame videoFrame;
270280

271281
bool m_handlePlayPauseClick;

Source/GUI/utils.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef QT_AVPLAYER_MULTIMEDIA
2+
#include "GUI/utils.h"
3+
4+
#include <QVideoFrame>
5+
#include <QVideoFrameFormat>
6+
#include <QtAVPlayer/qavplayer.h>
7+
8+
//---------------------------------------------------------------------------
9+
QVideoFrame QAVVideoFrame2QVideoFrame(const QAVVideoFrame& frame)
10+
{
11+
const QAVVideoFrame* in = &frame;
12+
QAVVideoFrame convertedFrame;
13+
14+
QVideoFrameFormat::PixelFormat pixelFormat;
15+
switch (in->frame()->format)
16+
{
17+
case AV_PIX_FMT_RGB32:
18+
pixelFormat = QVideoFrameFormat::Format_BGRA8888;
19+
break;
20+
default:
21+
// TODO: Add more supported formats instead of converting
22+
convertedFrame = in->convertTo(AV_PIX_FMT_RGB32);
23+
in = &convertedFrame;
24+
pixelFormat = QVideoFrameFormat::Format_BGRA8888;
25+
break;
26+
}
27+
28+
QVideoFrameFormat format(in->size(), pixelFormat);
29+
QVideoFrame videoFrame(format);
30+
if (!videoFrame.map(QVideoFrame::WriteOnly))
31+
return QVideoFrame();
32+
33+
for (int plane = 0; plane < 4; plane++)
34+
{
35+
if (!videoFrame.mappedBytes(plane))
36+
continue;
37+
38+
QAVVideoFrame::MapData mappedData = in->map();
39+
int bufferSize = qMin(in->size().height() * mappedData.bytesPerLine[plane], videoFrame.mappedBytes(plane));
40+
memcpy(videoFrame.bits(plane), mappedData.data[plane], bufferSize);
41+
}
42+
43+
videoFrame.unmap();
44+
45+
return videoFrame;
46+
}
47+
#endif // QT_AVPLAYER_MULTIMEDIA

Source/GUI/utils.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef UTILS_H
2+
#define UTILS_H
3+
4+
#ifdef QT_AVPLAYER_MULTIMEDIA
5+
#define QAVV_QV(x) x
6+
#else
7+
class QVideoFrame;
8+
class QAVVideoFrame;
9+
QVideoFrame QAVVideoFrame2QVideoFrame(const QAVVideoFrame& frame);
10+
#define QAVV_QV(x) QAVVideoFrame2QVideoFrame(x)
11+
#endif // QT_AVPLAYER_MULTIMEDIA
12+
13+
#endif // UTILS_H

0 commit comments

Comments
 (0)