diff --git a/src/UI/DocumentViews/MpvContainer.cc b/src/UI/DocumentViews/MpvContainer.cc index 26890478fca5ca99cacdc16db235b5d48147a5ea..9693d75ee92fb112eb4159e38da75dc98c7269c5 100644 --- a/src/UI/DocumentViews/MpvContainer.cc +++ b/src/UI/DocumentViews/MpvContainer.cc @@ -6,7 +6,7 @@ using namespace Vivy; using namespace std::string_literals; void -MpvContainer::mpvEventWakeUpCB(void *user) +MpvContainer::mpvEventWakeUpCB(void *user) noexcept { MpvContainer *container = reinterpret_cast<MpvContainer *>(user); emit container->mpvEvent(); @@ -35,16 +35,31 @@ MpvContainer::MpvContainer() throw std::runtime_error("Failed to initialize the mpv context"); } -MpvContainer::~MpvContainer() noexcept +void +MpvContainer::registerMpvTimeCallback(void (*callback)(double)) noexcept +{ + if (mpv_time_callback) + qWarning() << "Override a previous mpv callback, was" << mpv_time_callback + << "and now will be" << callback; + mpv_time_callback = callback; +} + +void +MpvContainer::closeMpv() noexcept { - // TODO: Do that in a private method if (mpv) { + registerMpvTimeCallback(nullptr); mpv_handle *tmp_mpv = mpv; mpv = nullptr; // Stop all other callbacks here mpv_destroy(tmp_mpv); } } +MpvContainer::~MpvContainer() noexcept +{ + closeMpv(); +} + void MpvContainer::handleMpvEvent(mpv_event *event) noexcept { @@ -53,14 +68,11 @@ MpvContainer::handleMpvEvent(mpv_event *event) noexcept union { mpv_event_log_message *msg; mpv_event_property *prop; - mpv_handle *tmp_mpv; }; switch (event->event_id) { case MPV_EVENT_SHUTDOWN: - tmp_mpv = mpv; - mpv = nullptr; - mpv_destroy(mpv); + closeMpv(); break; case MPV_EVENT_VIDEO_RECONFIG: @@ -86,6 +98,31 @@ MpvContainer::handleMpvEvent(mpv_event *event) noexcept } } break; + + // Explicitly ignored + case MPV_EVENT_NONE: + case MPV_EVENT_GET_PROPERTY_REPLY: + case MPV_EVENT_SET_PROPERTY_REPLY: + case MPV_EVENT_COMMAND_REPLY: + case MPV_EVENT_START_FILE: + case MPV_EVENT_END_FILE: + case MPV_EVENT_FILE_LOADED: + case MPV_EVENT_TRACKS_CHANGED: + case MPV_EVENT_TRACK_SWITCHED: + case MPV_EVENT_IDLE: + case MPV_EVENT_PAUSE: + case MPV_EVENT_UNPAUSE: + case MPV_EVENT_TICK: + case MPV_EVENT_SCRIPT_INPUT_DISPATCH: + case MPV_EVENT_CLIENT_MESSAGE: + case MPV_EVENT_AUDIO_RECONFIG: + case MPV_EVENT_METADATA_UPDATE: + case MPV_EVENT_SEEK: + case MPV_EVENT_PLAYBACK_RESTART: + case MPV_EVENT_CHAPTER_CHANGE: + case MPV_EVENT_QUEUE_OVERFLOW: + case MPV_EVENT_HOOK: + break; } } diff --git a/src/UI/DocumentViews/MpvContainer.hh b/src/UI/DocumentViews/MpvContainer.hh index bda0f585818e74e0c7d5bc1a528dd7888f04a056..f56fdc2de694c1cbd0890689ae1ffc7eddb42032 100644 --- a/src/UI/DocumentViews/MpvContainer.hh +++ b/src/UI/DocumentViews/MpvContainer.hh @@ -19,14 +19,21 @@ class MpvContainer : public QWidget { private: mpv_handle *mpv{ nullptr }; + void (*mpv_time_callback)(double){ nullptr }; public: explicit MpvContainer(); ~MpvContainer() noexcept override; + // Register a callback for time change, don't use Qt's signals for that. + void registerMpvTimeCallback(void (*)(double)) noexcept; + private: void handleMpvEvent(mpv_event *) noexcept; - static void mpvEventWakeUpCB(void *); // Must be static for function ptr + void closeMpv() noexcept; + + // Must be static to be passed as a function ptr + static void mpvEventWakeUpCB(void *) noexcept; private slots: void onMpvEvent() noexcept;