From 2b154981a0f4c853104b6a0aedcfa1f56a54d0c4 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Fri, 19 Nov 2021 11:04:08 +0100
Subject: [PATCH] MPV: Try to create the MPV context if trying to play and the
 MPV context is not create

---
 src/UI/DocumentViews/MpvContainer.cc | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/UI/DocumentViews/MpvContainer.cc b/src/UI/DocumentViews/MpvContainer.cc
index a9c2bbbd..74c75fa1 100644
--- a/src/UI/DocumentViews/MpvContainer.cc
+++ b/src/UI/DocumentViews/MpvContainer.cc
@@ -7,6 +7,12 @@ using namespace std::string_literals;
     if ((ptr) == nullptr)      \
     return
 
+#define TRY_CREATE_MPV(ptr)                                                         \
+    if ((ptr) == nullptr) {                                                         \
+        logInfo() << "Asked to play MPV but context not created, try to create it"; \
+        reCreateMpvContext();                                                       \
+    }
+
 void
 MpvContainer::mpvEventWakeUpCB(void *user) noexcept
 {
@@ -297,6 +303,7 @@ MpvContainer::printMpvError(int rc) const noexcept
 void
 MpvContainer::mpvPlay() noexcept
 {
+    TRY_CREATE_MPV(mpv);
     RETURN_IF_NULLPTR(mpv);
     if (isPlaybackPaused)
         mpvTogglePlayback();
@@ -313,6 +320,7 @@ MpvContainer::mpvPause() noexcept
 void
 MpvContainer::mpvTogglePlayback() noexcept
 {
+    TRY_CREATE_MPV(mpv);
     RETURN_IF_NULLPTR(mpv);
     logDebug() << "MPV: Toggling the playback";
     asyncCommand(AsyncCmdType::TogglePlayback, { "cycle", "pause", "up", nullptr });
-- 
GitLab