Skip to content
Extraits de code Groupes Projets
Vérifiée Valider bf5ab9de rédigé par Kubat's avatar Kubat
Parcourir les fichiers

MPV: Checkout a working implementation of the MPV viewer

parent b0b9461f
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!26MPV: Fix the MPV controls
Pipeline #2506 réussi
...@@ -3,16 +3,6 @@ ...@@ -3,16 +3,6 @@
using namespace Vivy; using namespace Vivy;
using namespace std::string_literals; using namespace std::string_literals;
#define RETURN_IF_NULLPTR(ptr) \
if ((ptr) == nullptr) \
return
#define TRY_CREATE_MPV(ptr) \
if ((ptr) == nullptr) { \
logInfo() << "Asked to play MPV but context not created, try to create it"; \
reCreateMpvContext(); \
}
void void
MpvContainer::mpvEventWakeUpCB(void *user) noexcept MpvContainer::mpvEventWakeUpCB(void *user) noexcept
{ {
...@@ -22,8 +12,11 @@ MpvContainer::mpvEventWakeUpCB(void *user) noexcept ...@@ -22,8 +12,11 @@ MpvContainer::mpvEventWakeUpCB(void *user) noexcept
MpvContainer::MpvContainer(QWidget *parent) MpvContainer::MpvContainer(QWidget *parent)
: QWidget(parent) : QWidget(parent)
, mpv(nullptr) , mpv(mpv_create())
{ {
if (mpv == nullptr)
throw std::runtime_error("Failed to create the MPV context");
setAttribute(Qt::WA_DontCreateNativeAncestors); setAttribute(Qt::WA_DontCreateNativeAncestors);
setAttribute(Qt::WA_NativeWindow); setAttribute(Qt::WA_NativeWindow);
...@@ -69,7 +62,6 @@ MpvContainer::reCreateMpvContext() ...@@ -69,7 +62,6 @@ MpvContainer::reCreateMpvContext()
throw std::runtime_error("Failed to create the MPV context"); throw std::runtime_error("Failed to create the MPV context");
initializeMpv(); initializeMpv();
loadFile(previousLoadedFile); loadFile(previousLoadedFile);
logInfo() << "Reloaded the MPV component";
} }
void void
...@@ -99,9 +91,9 @@ MpvContainer::closeMpv() noexcept ...@@ -99,9 +91,9 @@ MpvContainer::closeMpv() noexcept
asyncCommand(AsyncCmdType::None, { "quit", nullptr }); asyncCommand(AsyncCmdType::None, { "quit", nullptr });
mpv_wait_async_requests(mpv); mpv_wait_async_requests(mpv);
mpv_destroy(mpv); mpv_destroy(mpv);
mpv = nullptr; // Stop all other callbacks here mpv = nullptr; // Stop all other callbacks here
isMpvAlreadyInitialized = false; // De-init
} }
isMpvAlreadyInitialized = false; // De-init
} }
MpvContainer::~MpvContainer() noexcept { closeMpv(); } MpvContainer::~MpvContainer() noexcept { closeMpv(); }
...@@ -197,9 +189,6 @@ MpvContainer::handleMpvEvent(const mpv_event *const event) noexcept ...@@ -197,9 +189,6 @@ MpvContainer::handleMpvEvent(const mpv_event *const event) noexcept
int int
MpvContainer::getAssSid() const noexcept MpvContainer::getAssSid() const noexcept
{ {
// TODO: Do something about that!
RETURN_IF_NULLPTR(mpv) 0;
bool conversionOk = false; bool conversionOk = false;
const char *result = mpv_get_property_string(mpv, "sid"); const char *result = mpv_get_property_string(mpv, "sid");
const int ret = QString::fromUtf8(result).toInt(&conversionOk); const int ret = QString::fromUtf8(result).toInt(&conversionOk);
...@@ -256,8 +245,6 @@ MpvContainer::onMpvEvent() noexcept ...@@ -256,8 +245,6 @@ MpvContainer::onMpvEvent() noexcept
void void
MpvContainer::loadFile(const QString &filename) noexcept MpvContainer::loadFile(const QString &filename) noexcept
{ {
RETURN_IF_NULLPTR(mpv);
if (filename.size() == 0) if (filename.size() == 0)
return; return;
...@@ -271,8 +258,6 @@ MpvContainer::loadFile(const QString &filename) noexcept ...@@ -271,8 +258,6 @@ MpvContainer::loadFile(const QString &filename) noexcept
void void
MpvContainer::loadAssFile(const QString &ass) noexcept MpvContainer::loadAssFile(const QString &ass) noexcept
{ {
RETURN_IF_NULLPTR(mpv);
if (ass.isEmpty()) if (ass.isEmpty())
return; return;
...@@ -283,14 +268,12 @@ MpvContainer::loadAssFile(const QString &ass) noexcept ...@@ -283,14 +268,12 @@ MpvContainer::loadAssFile(const QString &ass) noexcept
void void
MpvContainer::reloadAssFile() noexcept MpvContainer::reloadAssFile() noexcept
{ {
RETURN_IF_NULLPTR(mpv);
asyncCommand(AsyncCmdType::ReloadAss, { "sub-reload", nullptr }); asyncCommand(AsyncCmdType::ReloadAss, { "sub-reload", nullptr });
} }
void void
MpvContainer::printMpvError(int rc) const noexcept MpvContainer::printMpvError(int rc) const noexcept
{ {
RETURN_IF_NULLPTR(mpv);
if (rc == MPV_ERROR_SUCCESS) if (rc == MPV_ERROR_SUCCESS)
return; return;
...@@ -300,8 +283,6 @@ MpvContainer::printMpvError(int rc) const noexcept ...@@ -300,8 +283,6 @@ MpvContainer::printMpvError(int rc) const noexcept
void void
MpvContainer::mpvPlay() noexcept MpvContainer::mpvPlay() noexcept
{ {
TRY_CREATE_MPV(mpv);
RETURN_IF_NULLPTR(mpv);
if (isPlaybackPaused) if (isPlaybackPaused)
mpvTogglePlayback(); mpvTogglePlayback();
} }
...@@ -309,7 +290,6 @@ MpvContainer::mpvPlay() noexcept ...@@ -309,7 +290,6 @@ MpvContainer::mpvPlay() noexcept
void void
MpvContainer::mpvPause() noexcept MpvContainer::mpvPause() noexcept
{ {
RETURN_IF_NULLPTR(mpv);
if (!isPlaybackPaused) if (!isPlaybackPaused)
mpvTogglePlayback(); mpvTogglePlayback();
} }
...@@ -317,8 +297,6 @@ MpvContainer::mpvPause() noexcept ...@@ -317,8 +297,6 @@ MpvContainer::mpvPause() noexcept
void void
MpvContainer::mpvTogglePlayback() noexcept MpvContainer::mpvTogglePlayback() noexcept
{ {
TRY_CREATE_MPV(mpv);
RETURN_IF_NULLPTR(mpv);
logDebug() << "MPV: Toggling the playback"; logDebug() << "MPV: Toggling the playback";
asyncCommand(AsyncCmdType::TogglePlayback, { "cycle", "pause", "up", nullptr }); asyncCommand(AsyncCmdType::TogglePlayback, { "cycle", "pause", "up", nullptr });
} }
...@@ -327,8 +305,6 @@ void ...@@ -327,8 +305,6 @@ void
MpvContainer::asyncCommand(const AsyncCmdType cmd, MpvContainer::asyncCommand(const AsyncCmdType cmd,
std::initializer_list<const char *> args) noexcept 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 // 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 // the temporary pointers here. Should be OK anyway because the `args` init
// list is a temporary and should be discarded afer the method call. // list is a temporary and should be discarded afer the method call.
...@@ -338,14 +314,12 @@ MpvContainer::asyncCommand(const AsyncCmdType cmd, ...@@ -338,14 +314,12 @@ MpvContainer::asyncCommand(const AsyncCmdType cmd,
void void
MpvContainer::unloadAssFile() noexcept MpvContainer::unloadAssFile() noexcept
{ {
RETURN_IF_NULLPTR(mpv);
asyncCommand(AsyncCmdType::UnloadAss, { "sub-remove", nullptr }); asyncCommand(AsyncCmdType::UnloadAss, { "sub-remove", nullptr });
} }
void void
MpvContainer::seekInFile(const chrono::seconds time) noexcept MpvContainer::seekInFile(const chrono::seconds time) noexcept
{ {
RETURN_IF_NULLPTR(mpv);
QByteArray seconds = QString::number(time.count()).toUtf8(); QByteArray seconds = QString::number(time.count()).toUtf8();
asyncCommand(AsyncCmdType::SeekTime, { "seek", seconds.data(), "absolute", nullptr }); asyncCommand(AsyncCmdType::SeekTime, { "seek", seconds.data(), "absolute", nullptr });
} }
...@@ -9,6 +9,8 @@ MpvControls::MpvControls(MpvContainer *passedContainer, QWidget *parent) noexcep ...@@ -9,6 +9,8 @@ MpvControls::MpvControls(MpvContainer *passedContainer, QWidget *parent) noexcep
{ {
auto *progressBar = new QSlider(this); auto *progressBar = new QSlider(this);
auto *togglePlaybackButton = new QPushButton(playIcon, "", this); // Be default MPV is paused 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
progressBar->setTracking(false); progressBar->setTracking(false);
progressBar->setOrientation(Qt::Horizontal); progressBar->setOrientation(Qt::Horizontal);
...@@ -54,6 +56,7 @@ MpvControls::MpvControls(MpvContainer *passedContainer, QWidget *parent) noexcep ...@@ -54,6 +56,7 @@ MpvControls::MpvControls(MpvContainer *passedContainer, QWidget *parent) noexcep
}); });
connect(togglePlaybackButton, &QAbstractButton::clicked, mpv, &MpvContainer::mpvTogglePlayback); connect(togglePlaybackButton, &QAbstractButton::clicked, mpv, &MpvContainer::mpvTogglePlayback);
connect(reCreateMpvButton, &QAbstractButton::clicked, mpv, &MpvContainer::reCreateMpvContext);
connect(mpv, &MpvContainer::mpvPlaybackToggled, this, connect(mpv, &MpvContainer::mpvPlaybackToggled, this,
[this, togglePlaybackButton](bool isPlay) noexcept -> void { [this, togglePlaybackButton](bool isPlay) noexcept -> void {
togglePlaybackButton->setIcon(isPlay ? pauseIcon : playIcon); togglePlaybackButton->setIcon(isPlay ? pauseIcon : playIcon);
...@@ -61,6 +64,7 @@ MpvControls::MpvControls(MpvContainer *passedContainer, QWidget *parent) noexcep ...@@ -61,6 +64,7 @@ MpvControls::MpvControls(MpvContainer *passedContainer, QWidget *parent) noexcep
auto *centralLayout = new QHBoxLayout(this); auto *centralLayout = new QHBoxLayout(this);
centralLayout->addWidget(togglePlaybackButton); centralLayout->addWidget(togglePlaybackButton);
centralLayout->addWidget(reCreateMpvButton);
centralLayout->addWidget(progressBar, 1); centralLayout->addWidget(progressBar, 1);
setLayout(centralLayout); setLayout(centralLayout);
} }
...@@ -23,6 +23,7 @@ private: ...@@ -23,6 +23,7 @@ private:
const QIcon playIcon{ VIVY_ICON_PLAY }; const QIcon playIcon{ VIVY_ICON_PLAY };
const QIcon pauseIcon{ VIVY_ICON_PAUSE }; const QIcon pauseIcon{ VIVY_ICON_PAUSE };
const QIcon reCreateMpvIcon{ VIVY_ICON_RUN };
public: public:
explicit MpvControls(MpvContainer *mpv, QWidget *parent) noexcept; explicit MpvControls(MpvContainer *mpv, QWidget *parent) noexcept;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter