Skip to content

Commit

Permalink
[添加QPlayer示例项目]: 在examples目录下添加了QPlayer示例项目,支持了新的播放器功能。
Browse files Browse the repository at this point in the history
- 在CMakeLists.txt中添加了新的子目录qplayer,并在其中定义了QPlayer的构建规则。
- 添加了qplayer相关的源文件和头文件,包括主窗口类、视频显示控件等。
- 在qplayer的CMakeLists.txt中链接了Qt6的MultimediaWidgets、Network等模块。
- 修改了其他示例项目和库以适应新的QPlayer项目结构,并确保所有项目都能正确链接Qt6库。
- 更新了3rdparty库的CMakeLists.txt,确保它们与Qt6库兼容。
  • Loading branch information
RealChuan committed Oct 10, 2024
1 parent 4a34089 commit b84e990
Show file tree
Hide file tree
Showing 30 changed files with 858 additions and 176 deletions.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_subdirectory(ffmpegplayer)
add_subdirectory(ffmpegtranscoder)
add_subdirectory(qplayer)

if(BUILD_MPV)
add_subdirectory(mpvplayer)
Expand Down
7 changes: 2 additions & 5 deletions examples/common/controlwidget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,6 @@ ControlWidget::ControlWidget(QWidget *parent)
: QWidget{parent}
, d_ptr(new ControlWidgetPrivate(this))
{
setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::Tool);
setAttribute(Qt::WA_TranslucentBackground); //设置窗口背景透明

d_ptr->setupUI();
buildConnect();
d_ptr->initModelButton();
Expand Down Expand Up @@ -213,7 +210,7 @@ void ControlWidget::setDuration(int value)
setPosition(0);
}

#ifdef MPV_ON
#if defined(MPV_ON)
void ControlWidget::setChapters(const Mpv::Chapters &chapters)
{
QVector<qint64> nodes;
Expand All @@ -222,7 +219,7 @@ void ControlWidget::setChapters(const Mpv::Chapters &chapters)
}
d_ptr->slider->setNodes(nodes);
}
#else
#elif defined(FFMPEG_ON)
void ControlWidget::setChapters(const Ffmpeg::Chapters &chapters)
{
QVector<qint64> nodes;
Expand Down
8 changes: 4 additions & 4 deletions examples/common/controlwidget.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef CONTROLWIDGET_HPP
#define CONTROLWIDGET_HPP

#ifdef MPV_ON
#if defined(MPV_ON)
#include <mpv/mediainfo.hpp>
#else
#elif defined(FFMPEG_ON)
#include <ffmpeg/mediainfo.hpp>
#endif

Expand All @@ -21,9 +21,9 @@ class ControlWidget : public QWidget

void setDuration(int value);
[[nodiscard]] auto duration() const -> int;
#ifdef MPV_ON
#if defined(MPV_ON)
void setChapters(const Mpv::Chapters &chapters);
#else
#elif defined(FFMPEG_ON)
void setChapters(const Ffmpeg::Chapters &chapters);
#endif
[[nodiscard]] auto sliderGlobalPos() const -> QPoint;
Expand Down
2 changes: 0 additions & 2 deletions examples/common/titlewidget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ TitleWidget::TitleWidget(QWidget *parent)
: QWidget{parent}
, d_ptr(new TitleWidgetPrivate(this))
{
setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::Tool);
setAttribute(Qt::WA_TranslucentBackground);

setupUI();
buildConnect();
Expand Down
3 changes: 2 additions & 1 deletion examples/examples.pro
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ CONFIG += ordered

SUBDIRS += \
ffmpegplayer \
ffmpegtranscoder
ffmpegtranscoder \
qplayer

win32 {
exists("C:/3rd/x64/mpv/include"){
Expand Down
7 changes: 4 additions & 3 deletions examples/ffmpegplayer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ set(PROJECT_SOURCES
mainwindow.h)

qt_add_executable(FfmpegPlayer MANUAL_FINALIZATION ${PROJECT_SOURCES})
target_compile_definitions(FfmpegPlayer PRIVATE "FFMPEG_ON")
target_link_libraries(
FfmpegPlayer
PRIVATE ffmpeg
mediaconfig
thirdparty
dump
utils
Qt6::Widgets
Qt6::Multimedia
Qt6::OpenGLWidgets)
Qt::Widgets
Qt::Multimedia
Qt::OpenGLWidgets)
target_link_libraries(FfmpegPlayer PRIVATE PkgConfig::ffmpeg)
if(CMAKE_HOST_APPLE)
target_link_libraries(
Expand Down
2 changes: 2 additions & 0 deletions examples/ffmpegplayer/ffmpegplayer.pro
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ TEMPLATE = app

TARGET = FfmpegPlayer

DEFINES += FFMPEG_ON

LIBS += \
-l$$replaceLibName(ffmpeg) \
-l$$replaceLibName(mediaconfig) \
Expand Down
92 changes: 47 additions & 45 deletions examples/ffmpegplayer/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <ffmpeg/videorender/videorendercreate.hpp>
#include <ffmpeg/widgets/mediainfodialog.hpp>

#include <QMediaDevices>
#include <QtWidgets>

class MainWindow::MainWindowPrivate
Expand All @@ -32,24 +33,16 @@ class MainWindow::MainWindowPrivate
titleWidget = new TitleWidget(q_ptr);
titleWidget->setMinimumHeight(80);
titleWidget->setVisible(false);
auto *bottomLayout = new QHBoxLayout;
bottomLayout->addStretch();
bottomLayout->addWidget(controlWidget);
bottomLayout->addStretch();
controlLayout = new QVBoxLayout;
controlLayout->addWidget(titleWidget);
controlLayout->addStretch();
controlLayout->addLayout(bottomLayout);

playlistModel = new PlaylistModel(q_ptr);
playlistView = new PlayListView(q_ptr);
playlistView->setModel(playlistModel);
playlistView->setCurrentIndex(
playlistModel->index(playlistModel->playlist()->currentIndex(), 0));
//playlistView->setMaximumWidth(250);
playListMenu = new QMenu(q_ptr);

menu = new QMenu(q_ptr);

videoMenu = new QMenu(QCoreApplication::translate("MainWindowPrivate", "Video"), q_ptr);
audioMenu = new QMenu(QCoreApplication::translate("MainWindowPrivate", "Audio"), q_ptr);
subMenu = new QMenu(QCoreApplication::translate("MainWindowPrivate", "Subtitles"), q_ptr);
Expand All @@ -64,18 +57,29 @@ class MainWindow::MainWindowPrivate
mediaInfoAction = new QAction(QCoreApplication::translate("MainWindowPrivate", "Media Info"),
q_ptr);

playListMenu = new QMenu(q_ptr);

fpsTimer = new QTimer(q_ptr);

splitter = new QSplitter(q_ptr);
splitter->addWidget(playlistView);

initShortcut();
}

~MainWindowPrivate() = default;

void setupUI()
{
auto *bottomLayout = new QHBoxLayout;
bottomLayout->addStretch();
bottomLayout->addWidget(controlWidget);
bottomLayout->addStretch();
controlLayout = new QVBoxLayout;
controlLayout->addWidget(titleWidget);
controlLayout->addStretch();
controlLayout->addLayout(bottomLayout);

splitter = new QSplitter(q_ptr);
splitter->addWidget(playlistView);
q_ptr->setCentralWidget(splitter);
}

auto createToneMappingMenu() -> QMenu *
{
auto *group = new QActionGroup(q_ptr);
Expand Down Expand Up @@ -242,6 +246,24 @@ class MainWindow::MainWindowPrivate
controlWidget->setPosition(0);
}

void addToPlaylist(const QList<QUrl> &urls)
{
auto *playlist = playlistModel->playlist();
const int previousMediaCount = playlist->mediaCount();
for (const auto &url : urls) {
if (isPlaylist(url)) {
playlist->load(url);
} else {
playlist->addMedia(url);
}
}
if (playlist->mediaCount() > previousMediaCount) {
auto index = playlistModel->index(previousMediaCount, 0);
playlistView->setCurrentIndex(index);
q_ptr->jump(index);
}
}

MainWindow *q_ptr;

QScopedPointer<Ffmpeg::Player> playerPtr;
Expand All @@ -254,6 +276,8 @@ class MainWindow::MainWindowPrivate

PlayListView *playlistView;
PlaylistModel *playlistModel;
QMenu *playListMenu;
QSplitter *splitter;

QMenu *menu;

Expand All @@ -271,12 +295,8 @@ class MainWindow::MainWindowPrivate

QAction *mediaInfoAction;

QMenu *playListMenu;

QTimer *fpsTimer;

QSplitter *splitter;

Ffmpeg::ToneMapping::Type toneMappingType = Ffmpeg::ToneMapping::Type::AUTO;
Ffmpeg::ColorUtils::Primaries::Type primarisType = Ffmpeg::ColorUtils::Primaries::Type::AUTO;
};
Expand All @@ -287,7 +307,12 @@ MainWindow::MainWindow(QWidget *parent)
{
Ffmpeg::printFfmpegInfo();

setupUI();
// QPlatformMediaDevices中有一个static std::unique_ptr<QPlatformMediaDevices> create();
// 这个QPlatformMediaDevices最终会在主线程释放,需要在主线程创建
// 如果在主线程创建,会导致退出程序崩溃,ASSERT: "m_threadId == GetCurrentThreadId()"
QMediaDevices mediaDevices;

d_ptr->setupUI();
buildConnect();
initMenu();
initPlayListMenu();
Expand Down Expand Up @@ -369,7 +394,7 @@ void MainWindow::onOpenLocalMedia()
if (urls.isEmpty()) {
return;
}
addToPlaylist(urls);
d_ptr->addToPlaylist(urls);
}

void MainWindow::onOpenWebMedia()
Expand All @@ -379,7 +404,7 @@ void MainWindow::onOpenWebMedia()
if (dialog.exec() != QDialog::Accepted) {
return;
}
addToPlaylist({QUrl(dialog.url())});
d_ptr->addToPlaylist({QUrl(dialog.url())});
}

void MainWindow::onRenderChanged(QAction *action)
Expand Down Expand Up @@ -552,7 +577,7 @@ auto MainWindow::eventFilter(QObject *watched, QEvent *event) -> bool
auto *ev = dynamic_cast<QDropEvent *>(event);
auto urls = ev->mimeData()->urls();
if (!urls.isEmpty()) {
addToPlaylist(urls);
d_ptr->addToPlaylist(urls);
}
} break;
case QEvent::ContextMenu: {
Expand Down Expand Up @@ -619,11 +644,6 @@ void MainWindow::keyPressEvent(QKeyEvent *event)
}
}

void MainWindow::setupUI()
{
setCentralWidget(d_ptr->splitter);
}

void MainWindow::buildConnect()
{
connect(d_ptr->playerPtr.data(),
Expand Down Expand Up @@ -776,21 +796,3 @@ void MainWindow::initPlayListMenu()
d_ptr->playlistModel->playlist()->clear();
});
}

void MainWindow::addToPlaylist(const QList<QUrl> &urls)
{
auto *playlist = d_ptr->playlistModel->playlist();
const int previousMediaCount = playlist->mediaCount();
for (const auto &url : urls) {
if (isPlaylist(url)) {
playlist->load(url);
} else {
playlist->addMedia(url);
}
}
if (playlist->mediaCount() > previousMediaCount) {
auto index = d_ptr->playlistModel->index(previousMediaCount, 0);
d_ptr->playlistView->setCurrentIndex(index);
jump(index);
}
}
2 changes: 0 additions & 2 deletions examples/ffmpegplayer/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ private slots:
void keyPressEvent(QKeyEvent *event) override;

private:
void setupUI();
void buildConnect();
void initMenu();
void renderMenu();
void initPlayListMenu();
void addToPlaylist(const QList<QUrl> &urls);

class MainWindowPrivate;
QScopedPointer<MainWindowPrivate> d_ptr;
Expand Down
6 changes: 3 additions & 3 deletions examples/ffmpegtranscoder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ target_link_libraries(
thirdparty
dump
utils
Qt6::Widgets
Qt6::Multimedia
Qt6::OpenGLWidgets)
Qt::Widgets
Qt::Multimedia
Qt::OpenGLWidgets)
target_link_libraries(FfmpegTranscoder PRIVATE PkgConfig::ffmpeg)

if(CMAKE_HOST_APPLE)
Expand Down
8 changes: 4 additions & 4 deletions examples/mpvplayer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ target_link_libraries(
thirdparty
dump
utils
Qt6::Widgets
Qt6::Multimedia
Qt6::Network
Qt6::OpenGLWidgets)
Qt::Widgets
Qt::Multimedia
Qt::Network
Qt::OpenGLWidgets)
if(CMAKE_HOST_WIN32)
target_link_libraries(MpvPlayer PRIVATE C:\\3rd\\x64\\mpv\\libmpv.dll.a)
file(COPY C:\\3rd\\x64\\mpv\\libmpv-2.dll
Expand Down
Loading

0 comments on commit b84e990

Please sign in to comment.