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

UI: Add the load/reload/unload ASS files to mpv container. By default will unload all ASS files

parent 20d9530e
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Ce commit fait partie de la requête de fusion !15. Les commentaires créés ici seront créés dans le contexte de cette requête de fusion.
...@@ -149,20 +149,7 @@ MpvContainer::handleMpvEvent(mpv_event *event) noexcept ...@@ -149,20 +149,7 @@ MpvContainer::handleMpvEvent(mpv_event *event) noexcept
case MPV_EVENT_COMMAND_REPLY: case MPV_EVENT_COMMAND_REPLY:
qDebug() << "Got return of" << event->reply_userdata; qDebug() << "Got return of" << event->reply_userdata;
switch (static_cast<AsyncCmdType>(event->reply_userdata)) { handleMpvEventCommandReply(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;
}
break; break;
// Explicitly ignored // Explicitly ignored
...@@ -187,6 +174,42 @@ MpvContainer::handleMpvEvent(mpv_event *event) noexcept ...@@ -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 void
MpvContainer::onMpvEvent() noexcept MpvContainer::onMpvEvent() noexcept
{ {
...@@ -204,9 +227,24 @@ MpvContainer::loadFile(const QString &filename) noexcept ...@@ -204,9 +227,24 @@ MpvContainer::loadFile(const QString &filename) noexcept
if (filename.isEmpty()) if (filename.isEmpty())
return; return;
const QByteArray c_filename = filename.toUtf8(); const QByteArray cFileName = filename.toUtf8();
const char *argsLoad[] = { "loadfile", c_filename.data(), nullptr }; asyncCommand(AsyncCmdType::LoadFile, { "loadfile", cFileName.data(), nullptr });
printMpvError(mpv_command_async(mpv, AsyncCmdType::LoadFile, argsLoad)); }
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 void
...@@ -235,6 +273,22 @@ MpvContainer::mpvPause() noexcept ...@@ -235,6 +273,22 @@ MpvContainer::mpvPause() noexcept
void void
MpvContainer::mpvTogglePlayback() noexcept MpvContainer::mpvTogglePlayback() noexcept
{ {
const char *cmd[] = { "cycle", "pause", "up", nullptr }; asyncCommand(AsyncCmdType::TogglePlayback, { "cycle", "pause", "up", nullptr });
printMpvError(mpv_command_async(mpv, AsyncCmdType::TogglePlayback, cmd)); }
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 });
} }
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "../../Lib/Utils.hh" #include "../../Lib/Utils.hh"
#include <functional> #include <functional>
#include <initializer_list>
extern "C" { extern "C" {
struct mpv_handle; struct mpv_handle;
...@@ -22,12 +23,16 @@ class MpvContainer final : public QWidget { ...@@ -22,12 +23,16 @@ class MpvContainer final : public QWidget {
enum AsyncCmdType : uint64_t { enum AsyncCmdType : uint64_t {
None, None,
LoadFile, LoadFile,
LoadAssFile,
ReloadAss,
UnloadAss,
TogglePlayback, TogglePlayback,
}; };
private: private:
bool isPlaybackPaused{ true }; bool isPlaybackPaused{ true };
mpv_handle *mpv{ nullptr }; mpv_handle *mpv{ nullptr };
qint64 sid{ -1 };
std::function<void(double)> mpvTimeCallback{ nullptr }; std::function<void(double)> mpvTimeCallback{ nullptr };
std::function<void(double)> mpvDurationCallback{ nullptr }; std::function<void(double)> mpvDurationCallback{ nullptr };
...@@ -36,6 +41,8 @@ public: ...@@ -36,6 +41,8 @@ public:
~MpvContainer() noexcept override; ~MpvContainer() noexcept override;
void loadFile(const QString &) noexcept; 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. // 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...). // Here the function will likely be moved if necessary (I hope...).
...@@ -46,6 +53,9 @@ private: ...@@ -46,6 +53,9 @@ private:
void handleMpvEvent(mpv_event *) noexcept; void handleMpvEvent(mpv_event *) noexcept;
void closeMpv() noexcept; void closeMpv() noexcept;
void printMpvError(int) const 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 // Must be static to be passed as a function ptr
static void mpvEventWakeUpCB(void *) noexcept; static void mpvEventWakeUpCB(void *) noexcept;
......
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