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 @@
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,7 +85,7 @@ MpvContainer::registerMpvDurationCallback(std::function<void(double)> callback)
void
MpvContainer::closeMpv() noexcept
{
if (mpv) {
RETURN_IF_NULLPTR(mpv);
logDebug() << "Closing the MPV context";
asyncCommand(AsyncCmdType::None, { "quit", nullptr });
registerMpvTimeCallback(nullptr);
......@@ -94,13 +96,14 @@ MpvContainer::closeMpv() noexcept
mpv = nullptr; // Stop all other callbacks here
isMpvAlreadyInitialized = false; // De-init
}
}
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 });
}
......@@ -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);
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter