From d2f66c699c1b6b95e8aff17b47fe028c05ca2391 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Wed, 8 Sep 2021 14:18:55 +0200 Subject: [PATCH] UI: Try to fix crashes when creating MPV Try to force the window creation before passing the WID to MPV to fix the possibility where the window is not already created and thus everything crashes. --- src/UI/DocumentViews/MpvContainer.cc | 8 ++++++++ src/UI/DocumentViews/MpvContainer.hh | 2 ++ src/UI/DocumentViews/VideoView.cc | 2 ++ 3 files changed, 12 insertions(+) diff --git a/src/UI/DocumentViews/MpvContainer.cc b/src/UI/DocumentViews/MpvContainer.cc index 425346ed..9b7b9ed7 100644 --- a/src/UI/DocumentViews/MpvContainer.cc +++ b/src/UI/DocumentViews/MpvContainer.cc @@ -19,6 +19,14 @@ MpvContainer::MpvContainer(QWidget *parent) setAttribute(Qt::WA_DontCreateNativeAncestors); setAttribute(Qt::WA_NativeWindow); +} + +void +MpvContainer::intializeMpv() +{ + if (isMpvAlreadyInitialized) + throw std::logic_error("MPV is already initialized!"); + isMpvAlreadyInitialized = true; quint64 wid = winId(); mpv_set_option(mpv, "wid", MPV_FORMAT_INT64, &wid); diff --git a/src/UI/DocumentViews/MpvContainer.hh b/src/UI/DocumentViews/MpvContainer.hh index 760d5e4a..f44d2359 100644 --- a/src/UI/DocumentViews/MpvContainer.hh +++ b/src/UI/DocumentViews/MpvContainer.hh @@ -29,6 +29,7 @@ class MpvContainer final : public QWidget { private: bool isPlaybackPaused{ true }; + bool isMpvAlreadyInitialized{ false }; mpv_handle *volatile mpv{ nullptr }; qint64 sid{ -1 }; std::function<void(double)> mpvTimeCallback{ nullptr }; @@ -36,6 +37,7 @@ private: public: explicit MpvContainer(QWidget *parent); + void intializeMpv(); ~MpvContainer() noexcept override; void loadFile(const QString &) noexcept; diff --git a/src/UI/DocumentViews/VideoView.cc b/src/UI/DocumentViews/VideoView.cc index 30eee0e0..2e45fcfd 100644 --- a/src/UI/DocumentViews/VideoView.cc +++ b/src/UI/DocumentViews/VideoView.cc @@ -14,6 +14,8 @@ VideoView::VideoView(QWidget *parent) noexcept centralLayout->addWidget(new MpvControls(mpv, this)); setLayout(centralLayout); + + mpv->intializeMpv(); } void -- GitLab