From e29ec47248269b1eef1d87416a0a67ca4d00819a Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Thu, 11 Nov 2021 18:47:14 +0100 Subject: [PATCH] MPV: Fix the MPV controls - Don't do anything if no MPV created - By default don't create the MPV, let the user do it himself --- src/UI/DocumentViews/MpvContainer.cc | 51 ++++++++++++++++++++-------- src/UI/DocumentViews/MpvControls.cc | 3 +- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/UI/DocumentViews/MpvContainer.cc b/src/UI/DocumentViews/MpvContainer.cc index fb094d4e..a9c2bbbd 100644 --- a/src/UI/DocumentViews/MpvContainer.cc +++ b/src/UI/DocumentViews/MpvContainer.cc @@ -3,6 +3,10 @@ using namespace Vivy; using namespace std::string_literals; +#define RETURN_IF_NULLPTR(ptr) \ + if ((ptr) == nullptr) \ + return + void MpvContainer::mpvEventWakeUpCB(void *user) noexcept { @@ -12,11 +16,8 @@ MpvContainer::mpvEventWakeUpCB(void *user) noexcept MpvContainer::MpvContainer(QWidget *parent) : QWidget(parent) - , mpv(mpv_create()) + , mpv(nullptr) { - if (mpv == nullptr) - throw std::runtime_error("Failed to create the MPV context"); - setAttribute(Qt::WA_DontCreateNativeAncestors); setAttribute(Qt::WA_NativeWindow); @@ -62,6 +63,7 @@ MpvContainer::reCreateMpvContext() throw std::runtime_error("Failed to create the MPV context"); initializeMpv(); loadFile(previousLoadedFile); + logInfo() << "Reloaded the MPV component"; } void @@ -83,17 +85,16 @@ MpvContainer::registerMpvDurationCallback(std::function<void(double)> callback) void MpvContainer::closeMpv() noexcept { - if (mpv) { - logDebug() << "Closing the MPV context"; - asyncCommand(AsyncCmdType::None, { "quit", nullptr }); - registerMpvTimeCallback(nullptr); - registerMpvDurationCallback(nullptr); - asyncCommand(AsyncCmdType::None, { "quit", nullptr }); - mpv_wait_async_requests(mpv); - mpv_destroy(mpv); - mpv = nullptr; // Stop all other callbacks here - isMpvAlreadyInitialized = false; // De-init - } + RETURN_IF_NULLPTR(mpv); + logDebug() << "Closing the MPV context"; + asyncCommand(AsyncCmdType::None, { "quit", nullptr }); + registerMpvTimeCallback(nullptr); + registerMpvDurationCallback(nullptr); + asyncCommand(AsyncCmdType::None, { "quit", nullptr }); + mpv_wait_async_requests(mpv); + mpv_destroy(mpv); + mpv = nullptr; // Stop all other callbacks here + isMpvAlreadyInitialized = false; // De-init } MpvContainer::~MpvContainer() noexcept { closeMpv(); } @@ -101,6 +102,8 @@ MpvContainer::~MpvContainer() noexcept { closeMpv(); } void MpvContainer::handleMpvEvent(const mpv_event *const event) noexcept { + RETURN_IF_NULLPTR(mpv); + // Declare here variables that can be used in the switch-case statements double time; QString msgText; @@ -189,6 +192,9 @@ MpvContainer::handleMpvEvent(const mpv_event *const event) noexcept int MpvContainer::getAssSid() const noexcept { + // TODO: Do something about that! + RETURN_IF_NULLPTR(mpv) 0; + bool conversionOk = false; const char *result = mpv_get_property_string(mpv, "sid"); const int ret = QString::fromUtf8(result).toInt(&conversionOk); @@ -198,6 +204,8 @@ MpvContainer::getAssSid() const noexcept void MpvContainer::handleMpvEventCommandReply(const AsyncCmdType type) noexcept { + RETURN_IF_NULLPTR(mpv); + switch (type) { case AsyncCmdType::None: break; @@ -245,6 +253,8 @@ MpvContainer::onMpvEvent() noexcept void MpvContainer::loadFile(const QString &filename) noexcept { + RETURN_IF_NULLPTR(mpv); + if (filename.size() == 0) return; @@ -258,6 +268,8 @@ MpvContainer::loadFile(const QString &filename) noexcept void MpvContainer::loadAssFile(const QString &ass) noexcept { + RETURN_IF_NULLPTR(mpv); + if (ass.isEmpty()) return; @@ -268,12 +280,14 @@ MpvContainer::loadAssFile(const QString &ass) noexcept void MpvContainer::reloadAssFile() noexcept { + RETURN_IF_NULLPTR(mpv); asyncCommand(AsyncCmdType::ReloadAss, { "sub-reload", nullptr }); } void MpvContainer::printMpvError(int rc) const noexcept { + RETURN_IF_NULLPTR(mpv); if (rc == MPV_ERROR_SUCCESS) return; @@ -283,6 +297,7 @@ MpvContainer::printMpvError(int rc) const noexcept void MpvContainer::mpvPlay() noexcept { + RETURN_IF_NULLPTR(mpv); if (isPlaybackPaused) mpvTogglePlayback(); } @@ -290,6 +305,7 @@ MpvContainer::mpvPlay() noexcept void MpvContainer::mpvPause() noexcept { + RETURN_IF_NULLPTR(mpv); if (!isPlaybackPaused) mpvTogglePlayback(); } @@ -297,6 +313,7 @@ MpvContainer::mpvPause() noexcept void MpvContainer::mpvTogglePlayback() noexcept { + RETURN_IF_NULLPTR(mpv); logDebug() << "MPV: Toggling the playback"; asyncCommand(AsyncCmdType::TogglePlayback, { "cycle", "pause", "up", nullptr }); } @@ -305,6 +322,8 @@ void MpvContainer::asyncCommand(const AsyncCmdType cmd, std::initializer_list<const char *> args) noexcept { + RETURN_IF_NULLPTR(mpv); + // NOTE: const_cast here, we have faith in MPV to not change the value of // the temporary pointers here. Should be OK anyway because the `args` init // list is a temporary and should be discarded afer the method call. @@ -314,12 +333,14 @@ MpvContainer::asyncCommand(const AsyncCmdType cmd, void MpvContainer::unloadAssFile() noexcept { + RETURN_IF_NULLPTR(mpv); asyncCommand(AsyncCmdType::UnloadAss, { "sub-remove", nullptr }); } void MpvContainer::seekInFile(const chrono::seconds time) noexcept { + RETURN_IF_NULLPTR(mpv); QByteArray seconds = QString::number(time.count()).toUtf8(); asyncCommand(AsyncCmdType::SeekTime, { "seek", seconds.data(), "absolute", nullptr }); } diff --git a/src/UI/DocumentViews/MpvControls.cc b/src/UI/DocumentViews/MpvControls.cc index a9d63a8f..c3b7e1a4 100644 --- a/src/UI/DocumentViews/MpvControls.cc +++ b/src/UI/DocumentViews/MpvControls.cc @@ -9,8 +9,7 @@ MpvControls::MpvControls(MpvContainer *passedContainer, QWidget *parent) noexcep { auto *progressBar = new QSlider(this); auto *togglePlaybackButton = new QPushButton(playIcon, "", this); // Be default MPV is paused - auto *reCreateMpvButton = new QPushButton( - reCreateMpvIcon, "", this); // Recreate the MPV context if something went wrong + auto *reCreateMpvButton = new QPushButton(reCreateMpvIcon, "", this); progressBar->setTracking(false); progressBar->setOrientation(Qt::Horizontal); -- GitLab