Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 658abf03 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 8788a71f
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!15Video playback with mpv
......@@ -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 });
}
......@@ -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;
......
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