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

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
parent 4d375e40
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!26MPV: Fix the MPV controls
Pipeline #2448 réussi
...@@ -3,6 +3,10 @@ ...@@ -3,6 +3,10 @@
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
void void
MpvContainer::mpvEventWakeUpCB(void *user) noexcept MpvContainer::mpvEventWakeUpCB(void *user) noexcept
{ {
...@@ -12,11 +16,8 @@ MpvContainer::mpvEventWakeUpCB(void *user) noexcept ...@@ -12,11 +16,8 @@ MpvContainer::mpvEventWakeUpCB(void *user) noexcept
MpvContainer::MpvContainer(QWidget *parent) MpvContainer::MpvContainer(QWidget *parent)
: 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_DontCreateNativeAncestors);
setAttribute(Qt::WA_NativeWindow); setAttribute(Qt::WA_NativeWindow);
...@@ -62,6 +63,7 @@ MpvContainer::reCreateMpvContext() ...@@ -62,6 +63,7 @@ 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
...@@ -83,17 +85,16 @@ MpvContainer::registerMpvDurationCallback(std::function<void(double)> callback) ...@@ -83,17 +85,16 @@ MpvContainer::registerMpvDurationCallback(std::function<void(double)> callback)
void void
MpvContainer::closeMpv() noexcept MpvContainer::closeMpv() noexcept
{ {
if (mpv) { RETURN_IF_NULLPTR(mpv);
logDebug() << "Closing the MPV context"; logDebug() << "Closing the MPV context";
asyncCommand(AsyncCmdType::None, { "quit", nullptr }); asyncCommand(AsyncCmdType::None, { "quit", nullptr });
registerMpvTimeCallback(nullptr); registerMpvTimeCallback(nullptr);
registerMpvDurationCallback(nullptr); registerMpvDurationCallback(nullptr);
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(); }
...@@ -101,6 +102,8 @@ MpvContainer::~MpvContainer() noexcept { closeMpv(); } ...@@ -101,6 +102,8 @@ MpvContainer::~MpvContainer() noexcept { closeMpv(); }
void void
MpvContainer::handleMpvEvent(const mpv_event *const event) noexcept MpvContainer::handleMpvEvent(const mpv_event *const event) noexcept
{ {
RETURN_IF_NULLPTR(mpv);
// Declare here variables that can be used in the switch-case statements // Declare here variables that can be used in the switch-case statements
double time; double time;
QString msgText; QString msgText;
...@@ -189,6 +192,9 @@ MpvContainer::handleMpvEvent(const mpv_event *const event) noexcept ...@@ -189,6 +192,9 @@ 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);
...@@ -198,6 +204,8 @@ MpvContainer::getAssSid() const noexcept ...@@ -198,6 +204,8 @@ MpvContainer::getAssSid() const noexcept
void void
MpvContainer::handleMpvEventCommandReply(const AsyncCmdType type) noexcept MpvContainer::handleMpvEventCommandReply(const AsyncCmdType type) noexcept
{ {
RETURN_IF_NULLPTR(mpv);
switch (type) { switch (type) {
case AsyncCmdType::None: break; case AsyncCmdType::None: break;
...@@ -245,6 +253,8 @@ MpvContainer::onMpvEvent() noexcept ...@@ -245,6 +253,8 @@ 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;
...@@ -258,6 +268,8 @@ MpvContainer::loadFile(const QString &filename) noexcept ...@@ -258,6 +268,8 @@ 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;
...@@ -268,12 +280,14 @@ MpvContainer::loadAssFile(const QString &ass) noexcept ...@@ -268,12 +280,14 @@ 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;
...@@ -283,6 +297,7 @@ MpvContainer::printMpvError(int rc) const noexcept ...@@ -283,6 +297,7 @@ MpvContainer::printMpvError(int rc) const noexcept
void void
MpvContainer::mpvPlay() noexcept MpvContainer::mpvPlay() noexcept
{ {
RETURN_IF_NULLPTR(mpv);
if (isPlaybackPaused) if (isPlaybackPaused)
mpvTogglePlayback(); mpvTogglePlayback();
} }
...@@ -290,6 +305,7 @@ MpvContainer::mpvPlay() noexcept ...@@ -290,6 +305,7 @@ MpvContainer::mpvPlay() noexcept
void void
MpvContainer::mpvPause() noexcept MpvContainer::mpvPause() noexcept
{ {
RETURN_IF_NULLPTR(mpv);
if (!isPlaybackPaused) if (!isPlaybackPaused)
mpvTogglePlayback(); mpvTogglePlayback();
} }
...@@ -297,6 +313,7 @@ MpvContainer::mpvPause() noexcept ...@@ -297,6 +313,7 @@ MpvContainer::mpvPause() noexcept
void void
MpvContainer::mpvTogglePlayback() noexcept MpvContainer::mpvTogglePlayback() noexcept
{ {
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 });
} }
...@@ -305,6 +322,8 @@ void ...@@ -305,6 +322,8 @@ 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.
...@@ -314,12 +333,14 @@ MpvContainer::asyncCommand(const AsyncCmdType cmd, ...@@ -314,12 +333,14 @@ 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,8 +9,7 @@ MpvControls::MpvControls(MpvContainer *passedContainer, QWidget *parent) noexcep ...@@ -9,8 +9,7 @@ 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( auto *reCreateMpvButton = new QPushButton(reCreateMpvIcon, "", this);
reCreateMpvIcon, "", this); // Recreate the MPV context if something went wrong
progressBar->setTracking(false); progressBar->setTracking(false);
progressBar->setOrientation(Qt::Horizontal); progressBar->setOrientation(Qt::Horizontal);
......
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