diff --git a/src/UI/DocumentViews/MpvContainer.cc b/src/UI/DocumentViews/MpvContainer.cc index b0e87326fbccfa5f9142cd0f41fbc7f7a1f320f7..fdd40369c04930c944f4f01227f996d1e30d42a7 100644 --- a/src/UI/DocumentViews/MpvContainer.cc +++ b/src/UI/DocumentViews/MpvContainer.cc @@ -149,20 +149,7 @@ MpvContainer::handleMpvEvent(mpv_event *event) noexcept case MPV_EVENT_COMMAND_REPLY: qDebug() << "Got return of" << event->reply_userdata; - switch (static_cast<AsyncCmdType>(event->reply_userdata)) { - case AsyncCmdType::None: - break; - - case AsyncCmdType::LoadFile: - qDebug() << "MPV - CMD: File loaded by mpv"; - isPlaybackPaused = false; - mpvPause(); - break; - - case AsyncCmdType::TogglePlayback: - qDebug() << "MPV - CMD: Playback was toggled"; - break; - } + handleMpvEventCommandReply(static_cast<AsyncCmdType>(event->reply_userdata)); break; // Explicitly ignored @@ -187,6 +174,42 @@ MpvContainer::handleMpvEvent(mpv_event *event) noexcept } } +void +MpvContainer::handleMpvEventCommandReply(const AsyncCmdType type) noexcept +{ + int rc; + + switch (type) { + case AsyncCmdType::None: + break; + + case AsyncCmdType::LoadAssFile: + case AsyncCmdType::ReloadAss: + mpv_get_property(mpv, "sid", MPV_FORMAT_INT64, &sid); + qDebug() << "Load ASS file with id:" << sid; + break; + case AsyncCmdType::UnloadAss: + rc = mpv_get_property(mpv, "sid", MPV_FORMAT_INT64, &sid); + qDebug() << "Unloaded Ass, rc =" << rc << "and sid =" << sid; + if (rc != MPV_ERROR_SUCCESS) + sid = -1; + else + unloadAssFile(); + break; + + case AsyncCmdType::LoadFile: + qDebug() << "MPV - CMD: File loaded by mpv"; + isPlaybackPaused = false; + mpvPause(); + unloadAssFile(); + break; + + case AsyncCmdType::TogglePlayback: + qDebug() << "MPV - CMD: Playback was toggled"; + break; + } +} + void MpvContainer::onMpvEvent() noexcept { @@ -204,9 +227,24 @@ MpvContainer::loadFile(const QString &filename) noexcept if (filename.isEmpty()) return; - const QByteArray c_filename = filename.toUtf8(); - const char *argsLoad[] = { "loadfile", c_filename.data(), nullptr }; - printMpvError(mpv_command_async(mpv, AsyncCmdType::LoadFile, argsLoad)); + const QByteArray cFileName = filename.toUtf8(); + asyncCommand(AsyncCmdType::LoadFile, { "loadfile", cFileName.data(), nullptr }); +} + +void +MpvContainer::loadAssFile(const QString &ass) noexcept +{ + if (ass.isEmpty()) + return; + + const QByteArray cFileName = ass.toUtf8(); + asyncCommand(AsyncCmdType::LoadAssFile, { "sub-add", cFileName.data(), nullptr }); +} + +void +MpvContainer::reloadAssFile() noexcept +{ + asyncCommand(AsyncCmdType::ReloadAss, { "sub-reload", nullptr }); } void @@ -235,6 +273,22 @@ MpvContainer::mpvPause() noexcept void MpvContainer::mpvTogglePlayback() noexcept { - const char *cmd[] = { "cycle", "pause", "up", nullptr }; - printMpvError(mpv_command_async(mpv, AsyncCmdType::TogglePlayback, cmd)); + asyncCommand(AsyncCmdType::TogglePlayback, { "cycle", "pause", "up", nullptr }); +} + +void +MpvContainer::asyncCommand(const AsyncCmdType cmd, + std::initializer_list<const char *> args) noexcept +{ + // 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. + printMpvError(mpv_command_async(mpv, cmd, const_cast<const char **>(std::data(args)))); +} + +void +MpvContainer::unloadAssFile() noexcept +{ + // XXX: Debug thing for now + asyncCommand(AsyncCmdType::UnloadAss, { "sub-remove", nullptr }); } diff --git a/src/UI/DocumentViews/MpvContainer.hh b/src/UI/DocumentViews/MpvContainer.hh index 7170634b3f211226c823cfae092e7eba35c08c89..b6ab1d9fd4d5cb6e9776a88e3d8e49689f5de5cf 100644 --- a/src/UI/DocumentViews/MpvContainer.hh +++ b/src/UI/DocumentViews/MpvContainer.hh @@ -7,6 +7,7 @@ #include "../../Lib/Utils.hh" #include <functional> +#include <initializer_list> extern "C" { struct mpv_handle; @@ -22,12 +23,16 @@ class MpvContainer final : public QWidget { enum AsyncCmdType : uint64_t { None, LoadFile, + LoadAssFile, + ReloadAss, + UnloadAss, TogglePlayback, }; private: bool isPlaybackPaused{ true }; mpv_handle *mpv{ nullptr }; + qint64 sid{ -1 }; std::function<void(double)> mpvTimeCallback{ nullptr }; std::function<void(double)> mpvDurationCallback{ nullptr }; @@ -36,6 +41,8 @@ public: ~MpvContainer() noexcept override; void loadFile(const QString &) noexcept; + void loadAssFile(const QString &) noexcept; + void reloadAssFile() noexcept; // Register a callback for time change, don't use Qt's signals for that. // Here the function will likely be moved if necessary (I hope...). @@ -46,6 +53,9 @@ private: void handleMpvEvent(mpv_event *) noexcept; void closeMpv() noexcept; void printMpvError(int) const noexcept; + void asyncCommand(const AsyncCmdType, std::initializer_list<const char *>) noexcept; + void unloadAssFile() noexcept; + void handleMpvEventCommandReply(const AsyncCmdType) noexcept; // Must be static to be passed as a function ptr static void mpvEventWakeUpCB(void *) noexcept;